-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ritsec/3-action-deploy-of-obiii-merges-to…
…-main OBIII Scheduled Updates and `/update` Command
- Loading branch information
Showing
8 changed files
with
321 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
config.yml | ||
private/ | ||
data.sqlite | ||
OBIII | ||
OBIII | ||
OBIII.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package scheduled | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
"github.com/robfig/cron" | ||
"gitlab.ritsec.cloud/1nv8rZim/ops-bot-iii/helpers" | ||
"gitlab.ritsec.cloud/1nv8rZim/ops-bot-iii/logging" | ||
"gitlab.ritsec.cloud/1nv8rZim/ops-bot-iii/structs" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
// updates repo, build binary, and exists if update is available | ||
func updateOBIII(s *discordgo.Session, ctx ddtrace.SpanContext) { | ||
span := tracer.StartSpan( | ||
"commands.scheduled.update:updateOBIII", | ||
tracer.ResourceName("Scheduled.Update:updateOBIII"), | ||
tracer.ChildOf(ctx), | ||
) | ||
defer span.Finish() | ||
|
||
logging.Debug(s, "Checking for update", nil, span) | ||
|
||
update, err := helpers.UpdateMainBranch() | ||
if err != nil { | ||
logging.Error(s, err.Error(), nil, span) | ||
return | ||
} | ||
|
||
if update { | ||
logging.Critical(s, "Update available; updating", nil, span) | ||
|
||
err = helpers.BuildOBIII() | ||
if err != nil { | ||
logging.Error(s, err.Error(), nil, span) | ||
return | ||
} | ||
|
||
err = helpers.Exit() | ||
if err != nil { | ||
logging.Error(s, err.Error(), nil, span) | ||
return | ||
} | ||
} else { | ||
logging.Debug(s, "No update available", nil, span) | ||
} | ||
} | ||
|
||
// checks for update every day at 2am and runs if available | ||
func Update() *structs.ScheduledEvent { | ||
return structs.NewScheduledTask( | ||
func(s *discordgo.Session, quit chan interface{}) error { | ||
span := tracer.StartSpan( | ||
"commands.scheduled.update:Update", | ||
tracer.ResourceName("Scheduled.Update"), | ||
) | ||
defer span.Finish() | ||
|
||
est, err := time.LoadLocation("America/New_York") | ||
if err != nil { | ||
logging.Error(s, err.Error(), nil, span) | ||
return err | ||
} | ||
|
||
c := cron.NewWithLocation(est) | ||
|
||
// every day at 2am | ||
err = c.AddFunc("0 0 2 * * *", func() { updateOBIII(s, span.Context()) }) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.Start() | ||
<-quit | ||
c.Stop() | ||
|
||
return nil | ||
}, | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package slash | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
"github.com/sirupsen/logrus" | ||
"gitlab.ritsec.cloud/1nv8rZim/ops-bot-iii/commands/slash/permission" | ||
"gitlab.ritsec.cloud/1nv8rZim/ops-bot-iii/helpers" | ||
"gitlab.ritsec.cloud/1nv8rZim/ops-bot-iii/logging" | ||
"gitlab.ritsec.cloud/1nv8rZim/ops-bot-iii/structs" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
func Update() *structs.SlashCommand { | ||
return &structs.SlashCommand{ | ||
Command: &discordgo.ApplicationCommand{ | ||
Name: "update", | ||
Description: "Update the bot", | ||
DefaultMemberPermissions: &permission.Admin, | ||
Options: []*discordgo.ApplicationCommandOption{ | ||
{ | ||
Name: "force", | ||
Description: "Force the bot to reboot", | ||
Type: discordgo.ApplicationCommandOptionBoolean, | ||
Required: false, | ||
}, | ||
}, | ||
}, | ||
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
span := tracer.StartSpan( | ||
"commands.slash.update:Update", | ||
tracer.ResourceName("/update"), | ||
) | ||
defer span.Finish() | ||
|
||
logging.Debug(s, "Update command received", i.Member.User, span) | ||
|
||
force := false | ||
if len(i.ApplicationCommandData().Options) != 0 { | ||
force = i.ApplicationCommandData().Options[0].BoolValue() | ||
} | ||
|
||
update, err := helpers.UpdateMainBranch() | ||
if err != nil { | ||
logging.Error(s, "Error updating main branch", i.Member.User, span, logrus.Fields{"err": err.Error()}) | ||
return | ||
} | ||
|
||
if !update { | ||
logging.Debug(s, "No update available", i.Member.User, span) | ||
|
||
if !force { | ||
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ | ||
Type: discordgo.InteractionResponseChannelMessageWithSource, | ||
Data: &discordgo.InteractionResponseData{ | ||
Content: "No update available\nIf you want to force an update, use `/update force`", | ||
Flags: discordgo.MessageFlagsEphemeral, | ||
}, | ||
}) | ||
if err != nil { | ||
logging.Error(s, "Error responding to interaction", i.Member.User, span, logrus.Fields{"err": err.Error()}) | ||
return | ||
} | ||
|
||
return | ||
} else { | ||
logging.Debug(s, "Forcing update", i.Member.User, span) | ||
|
||
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ | ||
Type: discordgo.InteractionResponseChannelMessageWithSource, | ||
Data: &discordgo.InteractionResponseData{ | ||
Content: "No update available; forcing update\nBot will be up temporarily once done updating", | ||
Flags: discordgo.MessageFlagsEphemeral, | ||
}, | ||
}) | ||
if err != nil { | ||
logging.Error(s, "Error responding to interaction", i.Member.User, span, logrus.Fields{"err": err.Error()}) | ||
return | ||
} | ||
|
||
err = helpers.BuildOBIII() | ||
if err != nil { | ||
logging.Error(s, "Error building OBIII", i.Member.User, span, logrus.Fields{"err": err.Error()}) | ||
|
||
content := fmt.Sprintf("Error building OBIII\n\nError:\n%s", err.Error()) | ||
_, err = s.InteractionResponseEdit( | ||
i.Interaction, | ||
&discordgo.WebhookEdit{ | ||
Content: &content, | ||
}, | ||
) | ||
if err != nil { | ||
logging.Error(s, "Error editing interaction response", i.Member.User, span, logrus.Fields{"err": err.Error()}) | ||
} | ||
|
||
return | ||
} | ||
|
||
err = helpers.Exit() | ||
if err != nil { | ||
logging.Error(s, "Error exiting", i.Member.User, span, logrus.Fields{"err": err.Error()}) | ||
return | ||
} | ||
|
||
return | ||
} | ||
} | ||
|
||
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ | ||
Type: discordgo.InteractionResponseChannelMessageWithSource, | ||
Data: &discordgo.InteractionResponseData{ | ||
Content: "Update available; restarting now\nBot will be up temporarily once done updating", | ||
Flags: discordgo.MessageFlagsEphemeral, | ||
}, | ||
}) | ||
if err != nil { | ||
logging.Error(s, "Error responding to interaction", i.Member.User, span, logrus.Fields{"err": err.Error()}) | ||
return | ||
} | ||
|
||
err = helpers.BuildOBIII() | ||
if err != nil { | ||
content := fmt.Sprintf("Error building OBIII\n\nError:\n%s", err.Error()) | ||
_, err = s.InteractionResponseEdit( | ||
i.Interaction, | ||
&discordgo.WebhookEdit{ | ||
Content: &content, | ||
}, | ||
) | ||
|
||
logging.Error(s, "Error building OBIII", i.Member.User, span, logrus.Fields{"err": err.Error()}) | ||
return | ||
} | ||
|
||
err = helpers.Exit() | ||
if err != nil { | ||
logging.Error(s, "Error exiting", i.Member.User, span, logrus.Fields{"err": err.Error()}) | ||
return | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package helpers | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
// UpdateMainBranch switches to the main branch, fetches from origin, pulls from origin, and returns true if an update was pulled | ||
func UpdateMainBranch() (bool, error) { | ||
switchCmd := exec.Command("git", "switch", "main") | ||
|
||
stderr := &bytes.Buffer{} | ||
switchCmd.Stderr = stderr | ||
|
||
err := switchCmd.Run() | ||
if err != nil { | ||
return false, fmt.Errorf("error switching to main branch: %s", stderr.String()) | ||
} | ||
|
||
fetchCmd := exec.Command("git", "fetch", "origin", "main") | ||
|
||
stderr = &bytes.Buffer{} | ||
fetchCmd.Stderr = stderr | ||
|
||
err = fetchCmd.Run() | ||
if err != nil { | ||
return false, fmt.Errorf("error fetching from origin: %s", stderr.String()) | ||
} | ||
|
||
commitCount := exec.Command("git", "rev-list", "--count", "HEAD...origin/main") | ||
|
||
stdout := &bytes.Buffer{} | ||
stderr = &bytes.Buffer{} | ||
|
||
commitCount.Stdout = stdout | ||
commitCount.Stderr = stderr | ||
|
||
err = commitCount.Run() | ||
if err != nil { | ||
return false, fmt.Errorf("error getting commit count: %s", stderr.String()) | ||
} | ||
|
||
if stdout.String() == "0\n" { | ||
return false, nil | ||
} | ||
|
||
pullCmd := exec.Command("git", "pull", "origin", "main") | ||
|
||
stderr = &bytes.Buffer{} | ||
pullCmd.Stderr = stderr | ||
|
||
err = pullCmd.Run() | ||
if err != nil { | ||
return false, fmt.Errorf("error pulling from origin: %s", stderr.String()) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// BuildOBIII builds the OBIII binary | ||
func BuildOBIII() error { | ||
buildCmd := exec.Command("/usr/local/go/bin/go", "build", "-o", "OBIII", "main.go") | ||
|
||
stderr := &bytes.Buffer{} | ||
buildCmd.Stderr = stderr | ||
|
||
err := buildCmd.Run() | ||
if err != nil { | ||
return fmt.Errorf("error building obiii:\n%s\n%s", err.Error(), stderr.String()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Exit restarts the OBIII service | ||
func Exit() error { | ||
exitCmd := exec.Command("systemctl", "restart", "OBIII") | ||
|
||
stderr := &bytes.Buffer{} | ||
exitCmd.Stderr = stderr | ||
|
||
err := exitCmd.Run() | ||
if err != nil { | ||
return fmt.Errorf("error restarting obiii: %s", stderr.String()) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters