package app import ( "context" "fmt" "coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/pkg/autocomplete" "coopcloud.tech/abra/pkg/secret" "coopcloud.tech/abra/pkg/client" "coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/dns" "coopcloud.tech/abra/pkg/formatter" "coopcloud.tech/abra/pkg/git" "coopcloud.tech/abra/pkg/lint" "coopcloud.tech/abra/pkg/recipe" "coopcloud.tech/abra/pkg/upstream/stack" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) var appDeployCommand = cli.Command{ Name: "deploy", Aliases: []string{"d"}, Usage: "Deploy an app", ArgsUsage: " []", Flags: []cli.Flag{ internal.DebugFlag, internal.NoInputFlag, internal.ForceFlag, internal.ChaosFlag, internal.NoDomainChecksFlag, internal.DontWaitConvergeFlag, internal.OfflineFlag, }, Before: internal.SubCommandBefore, Description: ` Deploy an app. It does not support incrementing the version of a deployed app, for this you need to look at the "abra app upgrade " command. You may pass "--force" to re-deploy the same version again. This can be useful if the container runtime has gotten into a weird state. Chaos mode ("--chaos") will deploy your local checkout of a recipe as-is, including unstaged changes and can be useful for live hacking and testing new recipes. `, BashComplete: autocomplete.AppNameComplete, Action: func(c *cli.Context) error { app := internal.ValidateApp(c) stackName := app.StackName() specificVersion := c.Args().Get(1) if specificVersion != "" && internal.Chaos { logrus.Fatal("cannot use and --chaos together") } if err := recipe.EnsureExists(app.Recipe); err != nil { logrus.Fatal(err) } if !internal.Chaos { if err := recipe.EnsureIsClean(app.Recipe); err != nil { logrus.Fatal(err) } if !internal.Offline { if err := recipe.EnsureUpToDate(app.Recipe); err != nil { logrus.Fatal(err) } } if err := recipe.EnsureLatest(app.Recipe); err != nil { logrus.Fatal(err) } } r, err := recipe.Get(app.Recipe, internal.Offline) if err != nil { logrus.Fatal(err) } if err := lint.LintForErrors(r); err != nil { logrus.Fatal(err) } logrus.Debugf("checking whether %s is already deployed", stackName) cl, err := client.New(app.Server) if err != nil { logrus.Fatal(err) } isDeployed, deployedVersion, err := stack.IsDeployed(context.Background(), cl, stackName) if err != nil { logrus.Fatal(err) } secStats, err := secret.PollSecretsStatus(cl, app) if err != nil { logrus.Fatal(err) } for _, secStat := range secStats { if !secStat.CreatedOnRemote { logrus.Fatalf("unable to deploy, secrets not generated (%s)?", secStat.LocalName) } } if isDeployed { if internal.Force || internal.Chaos { logrus.Warnf("%s is already deployed but continuing (--force/--chaos)", app.Name) } else { logrus.Fatalf("%s is already deployed", app.Name) } } version := deployedVersion if specificVersion != "" { version = specificVersion logrus.Debugf("choosing %s as version to deploy", version) if err := recipe.EnsureVersion(app.Recipe, version); err != nil { logrus.Fatal(err) } } if !internal.Chaos && specificVersion == "" { catl, err := recipe.ReadRecipeCatalogue(internal.Offline) if err != nil { logrus.Fatal(err) } versions, err := recipe.GetRecipeCatalogueVersions(app.Recipe, catl) if err != nil { logrus.Fatal(err) } if len(versions) == 0 && !internal.Chaos { logrus.Warn("no published versions in catalogue, trying local recipe repository") recipeVersions, err := recipe.GetRecipeVersions(app.Recipe, internal.Offline) if err != nil { logrus.Warn(err) } for _, recipeVersion := range recipeVersions { for version := range recipeVersion { versions = append(versions, version) } } } if len(versions) > 0 && !internal.Chaos { version = versions[len(versions)-1] logrus.Debugf("choosing %s as version to deploy", version) if err := recipe.EnsureVersion(app.Recipe, version); err != nil { logrus.Fatal(err) } } else { head, err := git.GetRecipeHead(app.Recipe) if err != nil { logrus.Fatal(err) } version = formatter.SmallSHA(head.String()) logrus.Warn("no versions detected, using latest commit") } } if internal.Chaos { logrus.Warnf("chaos mode engaged") var err error version, err = recipe.ChaosVersion(app.Recipe) if err != nil { logrus.Fatal(err) } } abraShPath := fmt.Sprintf("%s/%s/%s", config.RECIPES_DIR, app.Recipe, "abra.sh") abraShEnv, err := config.ReadAbraShEnvVars(abraShPath) if err != nil { logrus.Fatal(err) } for k, v := range abraShEnv { app.Env[k] = v } composeFiles, err := config.GetComposeFiles(app.Recipe, app.Env) if err != nil { logrus.Fatal(err) } deployOpts := stack.Deploy{ Composefiles: composeFiles, Namespace: stackName, Prune: false, ResolveImage: stack.ResolveImageAlways, } compose, err := config.GetAppComposeConfig(app.Name, deployOpts, app.Env) if err != nil { logrus.Fatal(err) } config.ExposeAllEnv(stackName, compose, app.Env) config.SetRecipeLabel(compose, stackName, app.Recipe) config.SetChaosLabel(compose, stackName, internal.Chaos) config.SetChaosVersionLabel(compose, stackName, version) config.SetUpdateLabel(compose, stackName, app.Env) envVars, err := config.CheckEnv(app) if err != nil { logrus.Fatal(err) } for _, envVar := range envVars { if !envVar.Present { logrus.Warnf("env var %s missing from %s.env, present in recipe .env.sample", envVar.Name, app.Domain) } } if err := internal.DeployOverview(app, version, "continue with deployment?"); err != nil { logrus.Fatal(err) } if !internal.NoDomainChecks { domainName, ok := app.Env["DOMAIN"] if ok { if _, err = dns.EnsureDomainsResolveSameIPv4(domainName, app.Server); err != nil { logrus.Fatal(err) } } else { logrus.Warn("skipping domain checks as no DOMAIN=... configured for app") } } else { logrus.Warn("skipping domain checks as requested") } stack.WaitTimeout, err = config.GetTimeoutFromLabel(compose, stackName) if err != nil { logrus.Fatal(err) } logrus.Debugf("set waiting timeout to %d s", stack.WaitTimeout) if err := stack.RunDeploy(cl, deployOpts, compose, app.Name, internal.DontWaitConverge); err != nil { logrus.Fatal(err) } postDeployCmds, ok := app.Env["POST_DEPLOY_CMDS"] if ok && !internal.DontWaitConverge { logrus.Debugf("run the following post-deploy commands: %s", postDeployCmds) if err := internal.PostCmds(cl, app, postDeployCmds); err != nil { logrus.Fatalf("attempting to run post deploy commands, saw: %s", err) } } return nil }, }