From 87663b95c257cdbcb06703aaa14b5a3bf481a5cb Mon Sep 17 00:00:00 2001 From: p4u1 Date: Thu, 30 Jan 2025 14:11:27 +0100 Subject: [PATCH 1/3] fix: Sorts git tags with tagcmp --- pkg/recipe/git.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/recipe/git.go b/pkg/recipe/git.go index e2f0982d..4b0ffd2a 100644 --- a/pkg/recipe/git.go +++ b/pkg/recipe/git.go @@ -4,11 +4,13 @@ import ( "fmt" "os" "slices" + "sort" "strings" "coopcloud.tech/abra/pkg/formatter" gitPkg "coopcloud.tech/abra/pkg/git" "coopcloud.tech/abra/pkg/log" + "coopcloud.tech/tagcmp" "github.com/distribution/reference" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" @@ -345,6 +347,18 @@ func (r Recipe) Tags() ([]string, error) { return tags, err } + sort.Slice(tags, func(i, j int) bool { + version1, err := tagcmp.Parse(tags[i]) + if err != nil { + return false + } + version2, err := tagcmp.Parse(tags[j]) + if err != nil { + return false + } + return version1.IsLessThan(version2) + }) + log.Debugf("detected %s as tags for recipe %s", strings.Join(tags, ", "), r.Name) return tags, nil -- 2.47.2 From 8a01b8eb41e56562c618169a0fd60745d7945203 Mon Sep 17 00:00:00 2001 From: p4u1 Date: Thu, 30 Jan 2025 14:14:23 +0100 Subject: [PATCH 2/3] fix: Changes how the deploy version is detected in app deploy command --- cli/app/deploy.go | 91 +++++++++++-------- tests/integration/app_deploy_env_version.bats | 25 +++-- 2 files changed, 72 insertions(+), 44 deletions(-) diff --git a/cli/app/deploy.go b/cli/app/deploy.go index 8ae945e8..c2cef8bd 100644 --- a/cli/app/deploy.go +++ b/cli/app/deploy.go @@ -46,7 +46,8 @@ checkout as-is. Recipe commit hashes are also supported as values for ValidArgsFunction: func( cmd *cobra.Command, args []string, - toComplete string) ([]string, cobra.ShellCompDirective) { + toComplete string, + ) ([]string, cobra.ShellCompDirective) { switch l := len(args); l { case 0: return autocomplete.AppNameComplete() @@ -99,27 +100,9 @@ checkout as-is. Recipe commit hashes are also supported as values for log.Fatalf("%s is already deployed", app.Name) } - if len(args) == 2 && args[1] != "" { - toDeployVersion = args[1] - } - - if !deployMeta.IsDeployed && - toDeployVersion == "" && - app.Recipe.EnvVersion != "" && !internal.IgnoreEnvVersion { - log.Debugf("new deployment, choosing .env version: %s", app.Recipe.EnvVersion) - toDeployVersion = app.Recipe.EnvVersion - } - - if !internal.Chaos && toDeployVersion == "" { - if err := getLatestVersionOrCommit(app, &toDeployVersion); err != nil { - log.Fatal(err) - } - } - - if internal.Chaos { - if err := getChaosVersion(app, &toDeployVersion, &toDeployChaosVersion); err != nil { - log.Fatal(err) - } + toDeployVersion, toDeployChaosVersion, err = getDeployVersion(args, deployMeta, app) + if err != nil { + log.Fatal(err) } if !internal.Chaos { @@ -271,32 +254,22 @@ func getChaosVersion(app app.App, toDeployVersion, toDeployChaosVersion *string) return nil } -func getLatestVersionOrCommit(app app.App, toDeployVersion *string) error { +func getLatestVersionOrCommit(app app.App) (string, string, error) { versions, err := app.Recipe.Tags() if err != nil { - return err + return "", "", err } if len(versions) > 0 && !internal.Chaos { - *toDeployVersion = versions[len(versions)-1] - - log.Debugf("choosing %s as version to deploy", *toDeployVersion) - - if _, err := app.Recipe.EnsureVersion(*toDeployVersion); err != nil { - return err - } - - return nil + return versions[len(versions)-1], "", nil } head, err := app.Recipe.Head() if err != nil { - return err + return "", "", err } - *toDeployVersion = formatter.SmallSHA(head.String()) - - return nil + return "", formatter.SmallSHA(head.String()), nil } // validateArgsAndFlags ensures compatible args/flags. @@ -323,6 +296,50 @@ func validateSecrets(cl *dockerClient.Client, app app.App) error { return nil } +func getDeployVersion(cliArgs []string, deployMeta stack.DeployMeta, app app.App) (string, string, error) { + // Chaos mode overrides everything + if internal.Chaos { + v, err := app.Recipe.ChaosVersion() + if err != nil { + return "", "", err + } + cv, err := app.Recipe.GetVersionLabelLocal() + if err != nil { + return "", "", err + } + log.Debugf("version: taking chaos version: %s, %s", v, cv) + return v, cv, nil + } + + // Check if the deploy version is set with a cli argument + if len(cliArgs) == 2 && cliArgs[1] != "" { + log.Debugf("version: taking version from cli arg: %s", cliArgs[1]) + return cliArgs[1], "", nil + } + + // Check if the recipe has a version in the .env file + if app.Recipe.EnvVersion != "" && !internal.IgnoreEnvVersion { + log.Debugf("version: taking version from .env file: %s", app.Recipe.EnvVersion) + return app.Recipe.EnvVersion, "", nil + } + + // Take deployed version + if deployMeta.IsDeployed { + log.Debugf("version: taking deployed version: %s", deployMeta.Version) + return deployMeta.Version, "", nil + } + + v, vc, err := getLatestVersionOrCommit(app) + log.Debugf("version: taking new recipe versio: %s, %s", v, vc) + if err != nil { + log.Fatal(err) + } + if v == "" { + return vc, vc, nil + } + return v, vc, nil +} + func init() { AppDeployCommand.Flags().BoolVarP( &internal.Chaos, diff --git a/tests/integration/app_deploy_env_version.bats b/tests/integration/app_deploy_env_version.bats index 013e0a68..e89b79f9 100644 --- a/tests/integration/app_deploy_env_version.bats +++ b/tests/integration/app_deploy_env_version.bats @@ -54,13 +54,21 @@ teardown(){ } # bats test_tags=slow -@test "chaos commit written to env" { +@test "deploy commit written to env and redeploy keeps that version" { run $ABRA app deploy "$TEST_APP_DOMAIN" "1e83340e" --no-input --no-converge-checks assert_success run grep -q "TYPE=$TEST_RECIPE:1e83340e" \ "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" assert_success + + run $ABRA app deploy "$TEST_APP_DOMAIN" \ + --force --no-input --no-converge-checks + assert_success + + run grep -q "TYPE=$TEST_RECIPE:1e83340e" \ + "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" + assert_success } # bats test_tags=slow @@ -98,12 +106,15 @@ teardown(){ } # bats test_tags=slow -@test "deploy overwrites chaos deploy" { - run $ABRA app deploy "$TEST_APP_DOMAIN" "1e83340e" \ - --no-input --no-converge-checks +@test "takes deployed version when no .env version is present " { + run $ABRA app deploy "$TEST_APP_DOMAIN" "0.1.0+1.20.0" --no-input --no-converge-checks --ignore-env-version assert_success - run grep -q "TYPE=$TEST_RECIPE:1e83340e" \ + run grep -q "TYPE=$TEST_RECIPE:0.1.0+1.20.0" \ + "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" + assert_success + + run sed -i 's/TYPE=abra-test-recipe:.*/TYPE=abra-test-recipe/g' \ "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" assert_success @@ -111,7 +122,7 @@ teardown(){ --force --no-input --no-converge-checks assert_success - run grep -q "TYPE=$TEST_RECIPE:1e83340e" \ + run grep -q "TYPE=$TEST_RECIPE:0.1.0+1.20.0" \ "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" - assert_failure + assert_success } -- 2.47.2 From 33d5885f6f85de878bcb2c06485b4eb969ccd405 Mon Sep 17 00:00:00 2001 From: p4u1 Date: Mon, 3 Feb 2025 15:31:38 +0100 Subject: [PATCH 3/3] fix: Adds chaos flag to restart command --- cli/app/restart.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cli/app/restart.go b/cli/app/restart.go index eea10397..184ca0fc 100644 --- a/cli/app/restart.go +++ b/cli/app/restart.go @@ -123,6 +123,13 @@ Pass "--all-services/-a" to restart all services.`, var allServices bool func init() { + AppRestartCommand.Flags().BoolVarP( + &internal.Chaos, + "chaos", + "C", + false, + "ignore uncommitted recipes changes", + ) AppRestartCommand.Flags().BoolVarP( &allServices, "all-services", -- 2.47.2