kadabra, the app auto-updater #268

Merged
moritz merged 21 commits from moritz/abra:update_daemon into main 2023-02-08 18:54:06 +00:00
1 changed files with 34 additions and 52 deletions
Showing only changes of commit 474cbd904b - Show all commits

View File

@ -20,7 +20,6 @@ import (
"github.com/docker/docker/api/types/filters"
dockerclient "github.com/docker/docker/client"
// "github.com/docker/cli/cli/command/stack/swarm"
"github.com/sirupsen/logrus"
moritz marked this conversation as resolved Outdated

To remove?

To remove?
"github.com/urfave/cli"
)
@ -34,6 +33,13 @@ var majorFlag = &cli.BoolFlag{
Destination: &majorUpdate,
}
var updateAll bool
var allFlag = &cli.BoolFlag{
Name: "all, a",
Usage: "Update all deployed apps",
Destination: &updateAll,
}
// Check for available upgrades
var Notify = cli.Command{
Name: "notify",
moritz marked this conversation as resolved Outdated

If you don't add more info on top of the Usage, then maybe we can skip it?

If you don't add more info on top of the `Usage`, then maybe we can skip it?
@ -51,8 +57,6 @@ var Notify = cli.Command{
if err != nil {
logrus.Fatal(err)
}
// can't import this lib:
// stacks := swarm.GetStacks(cl)
stacks, err := stack.GetStacks(cl)
if err != nil {
logrus.Fatal(err)
@ -68,70 +72,48 @@ var Notify = cli.Command{
},
}
// Upgrade a specific app
// Upgrade apps
var UpgradeApp = cli.Command{
Name: "appupgrade",
Aliases: []string{"a"},
Usage: "Upgrade an app",
Name: "upgrade",
Aliases: []string{"u"},
Usage: "Upgrade apps",
ArgsUsage: "<stack_name> <recipe>",
Flags: []cli.Flag{
internal.DebugFlag,
internal.ForceFlag,
internal.ChaosFlag,
majorFlag,
allFlag,
},
Before: internal.SubCommandBefore,
Description: `Upgrade an app by specifying its stack name and recipe. By passing --all instead every deployed app is upgraded. For each apps with enabled auto updates the deployed version is compared with the current recipe catalogue version. If a new patch/minor version is available the app is upgraded. To include major versions use the --major flag. Don't do that, it will probably break things. Only apps that are not deployed with --chaos are upgraded, to update chaos deployments use the --chaos flag. Use it with care.`,
Action: func(c *cli.Context) error {
stackName := c.Args().Get(0)
recipeName := c.Args().Get(1)
cl, err := client.New("default")
if err != nil {
logrus.Fatal(err)
}
upgradeVersion := getLatestUpgrade(cl, stackName, recipeName)
if upgradeVersion != "" {
upgrade(cl, stackName, recipeName, upgradeVersion)
}
return nil
},
}
// Upgrade all apps
var UpgradeAll = cli.Command{
Name: "upgrade",
Aliases: []string{"u"},
Usage: "Upgrade all apps",
Flags: []cli.Flag{
internal.DebugFlag,
internal.ForceFlag,
majorFlag,
},
Before: internal.SubCommandBefore,
Description: `Upgrade all deployed apps`,
Action: func(c *cli.Context) error {
cl, err := client.New("default")
if err != nil {
logrus.Fatal(err)
}
// can't import this lib:
// stacks := swarm.GetStacks(cl)
stacks, err := stack.GetStacks(cl)
if err != nil {
logrus.Fatal(err)
}
for _, stackInfo := range stacks {
stackName := stackInfo.Name
recipeName := getLabel(cl, stackName, "recipe")
chaos := getBoolLabel(cl, stackName, "chaos")
updatesEnabled := getBoolLabel(cl, stackName, "autoupdate")
if recipeName != "" && updatesEnabled && (!chaos || internal.Force) {
upgradeVersion := getLatestUpgrade(cl, stackName, recipeName)
if upgradeVersion != "" {
upgrade(cl, stackName, recipeName, upgradeVersion)
if !updateAll {
stackName := c.Args().Get(0)
recipeName := c.Args().Get(1)
err = tryUpgrade(cl, stackName, recipeName)
moritz marked this conversation as resolved Outdated

Could we merge this code with the appupgrade command, rename to upgrade and use a -a/--all flag to trigger upgrade for all? Then you can do some bookkeeping logic for the flag / args to make a decision on what to do? Smaller command surface seems good.

Could we merge this code with the `appupgrade` command, rename to `upgrade` and use a -a/--all flag to trigger upgrade for all? Then you can do some bookkeeping logic for the flag / args to make a decision on what to do? Smaller command surface seems good.
if err != nil {
logrus.Fatal(err)
}
moritz marked this conversation as resolved Outdated

Nitpick, you could if !updateAll { ... return nil } and then avoid the else indentation on the next block.

Nitpick, you could `if !updateAll { ... return nil }` and then avoid the `else` indentation on the next block.
} else {
stacks, err := stack.GetStacks(cl)
if err != nil {
logrus.Fatal(err)
}
for _, stackInfo := range stacks {
stackName := stackInfo.Name
moritz marked this conversation as resolved Outdated

I think it may make sense to document here that we're accepting a <stack_name> and not an <app_name> because we don't have access to the ~/.abra/... files? Further context on when and where this is supposed to be run?

I think it may make sense to document here that we're accepting a `<stack_name>` and not an `<app_name>` because we don't have access to the `~/.abra/...` files? Further context on when and where this is supposed to be run?
recipeName, err := getLabel(cl, stackName, "recipe")
if err != nil {
logrus.Fatal(err)
}
err = tryUpgrade(cl, stackName, recipeName)
if err != nil {
logrus.Fatal(err)
}
} else {
logrus.Debugf("Don't update %s due to missing recipe name, disabled updates or chaos deployment", stackName)
}
}
return nil