From 2f9b11f389a28c8fe7ef438c0712ab2a6e9939d3 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Mon, 18 Oct 2021 08:14:06 +0200 Subject: [PATCH] feat: support deploying with chaos mode --- cli/app/deploy.go | 29 ++++++++++++++++++++++--- cli/internal/common.go | 12 +++++++++++ pkg/git/read.go | 48 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 pkg/git/read.go diff --git a/cli/app/deploy.go b/cli/app/deploy.go index 027523621..735608ae1 100644 --- a/cli/app/deploy.go +++ b/cli/app/deploy.go @@ -10,6 +10,7 @@ import ( "coopcloud.tech/abra/pkg/client" stack "coopcloud.tech/abra/pkg/client/stack" "coopcloud.tech/abra/pkg/config" + "coopcloud.tech/abra/pkg/git" "coopcloud.tech/abra/pkg/recipe" "github.com/AlecAivazis/survey/v2" "github.com/sirupsen/logrus" @@ -22,6 +23,7 @@ var appDeployCommand = &cli.Command{ Usage: "Deploy an app", Flags: []cli.Flag{ internal.ForceFlag, + internal.ChaosFlag, }, Description: ` This command deploys a new instance of an app. It does not support changing the @@ -50,14 +52,16 @@ hacking. if isDeployed { if internal.Force { - logrus.Infof("continuing with deployment due to '--force/-f' being set") + logrus.Warnf("'%s' already deployed but continuing (--force)", stackName) + } else if internal.Chaos { + logrus.Warnf("'%s' already deployed but continuing (--chaos)", stackName) } else { logrus.Fatalf("'%s' is already deployed", stackName) } } version := deployedVersion - if version == "" { + if version == "" && !internal.Chaos { versions, err := catalogue.GetRecipeCatalogueVersions(app.Type) if err != nil { logrus.Fatal(err) @@ -70,13 +74,32 @@ hacking. } } else { version = "latest commit" - logrus.Warning("no versions detected, using latest commit") + logrus.Warn("no versions detected, using latest commit") if err := recipe.EnsureLatest(app.Type); err != nil { logrus.Fatal(err) } } } + if internal.Chaos { + logrus.Warnf("chaos mode engaged") + head, err := git.GetRecipeHead(app.Type) + if err != nil { + logrus.Fatal(err) + } + + version = head.String()[:8] + + isClean, err := git.IsClean(app.Type) + if err != nil { + logrus.Fatal(err) + } + + if !isClean { + version = fmt.Sprintf("%s + unstaged changes", version) + } + } + abraShPath := fmt.Sprintf("%s/%s/%s", config.APPS_DIR, app.Type, "abra.sh") abraShEnv, err := config.ReadAbraShEnvVars(abraShPath) if err != nil { diff --git a/cli/internal/common.go b/cli/internal/common.go index e1c928fa7..ab1368659 100644 --- a/cli/internal/common.go +++ b/cli/internal/common.go @@ -49,3 +49,15 @@ var ForceFlag = &cli.BoolFlag{ Aliases: []string{"f"}, Destination: &Force, } + +// Chaos engages chaos mode. +var Chaos bool + +// ChaosFlag turns on/off chaos functionality. +var ChaosFlag = &cli.BoolFlag{ + Name: "chaos", + Value: false, + Aliases: []string{"ch"}, + Usage: "Deploy uncommitted recipes changes. Use with care!", + Destination: &Chaos, +} diff --git a/pkg/git/read.go b/pkg/git/read.go new file mode 100644 index 000000000..9e33dcfc9 --- /dev/null +++ b/pkg/git/read.go @@ -0,0 +1,48 @@ +package git + +import ( + "path" + + "coopcloud.tech/abra/pkg/config" + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" +) + +// GetRecipeHead retrieves latest HEAD metadata. +func GetRecipeHead(recipeName string) (*plumbing.Reference, error) { + recipeDir := path.Join(config.ABRA_DIR, "apps", recipeName) + + repo, err := git.PlainOpen(recipeDir) + if err != nil { + return nil, err + } + + head, err := repo.Head() + if err != nil { + return nil, err + } + + return head, nil +} + +// IsClean checks if a repo has unstaged changes +func IsClean(recipeName string) (bool, error) { + recipeDir := path.Join(config.ABRA_DIR, "apps", recipeName) + + repo, err := git.PlainOpen(recipeDir) + if err != nil { + return false, err + } + + worktree, err := repo.Worktree() + if err != nil { + return false, err + } + + status, err := worktree.Status() + if err != nil { + return false, err + } + + return status.IsClean(), nil +}