Post-deploy abra.sh hooks #292

Merged
moritz merged 5 commits from post_deploy_cmds into main 2023-04-14 14:12:33 +00:00
2 changed files with 68 additions and 0 deletions
Showing only changes of commit 14bdb57ac7 - Show all commits

View File

@ -205,6 +205,14 @@ recipes.
logrus.Fatal(err)
}
postDeployCmds, ok := app.Env["POST_UPGRADE_CMDS"]
if ok && !internal.DontWaitConverge {
logrus.Debugf("Run the following post-deploy commands: %s", postDeployCmds)
moritz marked this conversation as resolved
Review

nitpick: Run -> run since the rest of the codebase uses lowercase

nitpick: `Run` -> `run` since the rest of the codebase uses lowercase
if err := internal.PostCmds(cl, app, postDeployCmds); err != nil {
logrus.Fatal(err)
moritz marked this conversation as resolved
Review

logrus.Fatalf("attempting to run post deploy commands, saw: %s", err)?

`logrus.Fatalf("attempting to run post deploy commands, saw: %s", err)`?
}
}
return nil
},
BashComplete: autocomplete.AppNameComplete,

View File

@ -18,6 +18,7 @@ import (
"coopcloud.tech/abra/pkg/runtime"
"coopcloud.tech/abra/pkg/upstream/stack"
"github.com/AlecAivazis/survey/v2"
dockerClient "github.com/docker/docker/client"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
@ -164,6 +165,65 @@ func DeployAction(c *cli.Context) error {
logrus.Fatal(err)
}
postDeployCmds, ok := app.Env["POST_DEPLOY_CMDS"]
if ok && !DontWaitConverge {
logrus.Debugf("Run the following post-deploy commands: %s", postDeployCmds)
moritz marked this conversation as resolved
Review

nitpick: Run -> run since the rest of the codebase uses lowercase

nitpick: Run -> run since the rest of the codebase uses lowercase
if err := PostCmds(cl, app, postDeployCmds); err != nil {
logrus.Fatal(err)
moritz marked this conversation as resolved
Review

logrus.Fatalf("attempting to run post deploy commands, saw: %s", err)?

logrus.Fatalf("attempting to run post deploy commands, saw: %s", err)?
}
}
return nil
}
func PostCmds(cl *dockerClient.Client, app config.App, commands string) error {
moritz marked this conversation as resolved
Review

Missing doc string?

Missing doc string?
abraSh := path.Join(config.RECIPES_DIR, app.Recipe, "abra.sh")
if _, err := os.Stat(abraSh); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf(fmt.Sprintf("%s does not exist for %s?", abraSh, app.Name))
}
return err
}
for _, command := range strings.Split(commands, "|") {
commandParts := strings.Split(command, " ")
if len(commandParts) < 2 {
return fmt.Errorf(fmt.Sprintf("not enough arguments: %s", command))
}
targetServiceName := commandParts[0]
cmdName := commandParts[1]
parsedCmdArgs := ""
if len(commandParts) > 2 {
parsedCmdArgs = fmt.Sprintf("%s ", strings.Join(commandParts[2:], " "))
}
logrus.Infof("Running post-command '%s %s' in container %s", cmdName, parsedCmdArgs, targetServiceName)
moritz marked this conversation as resolved
Review

nitpick: Running -> running since the rest of the codebase uses lowercase

nitpick: Running -> running since the rest of the codebase uses lowercase
if err := EnsureCommand(abraSh, app.Recipe, cmdName); err != nil {
return err
}
serviceNames, err := config.GetAppServiceNames(app.Name)
if err != nil {
return err
}
matchingServiceName := false
for _, serviceName := range serviceNames {
if serviceName == targetServiceName {
matchingServiceName = true
}
}
if !matchingServiceName {
return fmt.Errorf(fmt.Sprintf("no service %s for %s?", targetServiceName, app.Name))
}
logrus.Debugf("running command %s %s within the context of %s_%s", cmdName, parsedCmdArgs, app.StackName(), targetServiceName)
if err := RunCmdRemote(cl, app, abraSh, targetServiceName, cmdName, parsedCmdArgs); err != nil {
return err
}
}
return nil
}