diff --git a/command/deploy.go b/command/deploy.go index ea828dd20..09d47a28c 100644 --- a/command/deploy.go +++ b/command/deploy.go @@ -62,6 +62,10 @@ General Options: Use the taskgroup count from the Nomad jobfile instead of the count that is currently set in a running job. + -blue-green + Set the max-parallel and the canary count of each task group to the current + count for a blue-green type release + -ignore-no-changes By default if no changes are detected when running a deployment Levant will exit with a status 1 to indicate a deployment didn't happen. This behaviour @@ -120,6 +124,7 @@ func (c *DeployCommand) Run(args []string) int { flags.BoolVar(&config.Deploy.Force, "force", false, "") flags.BoolVar(&config.Deploy.ForceBatch, "force-batch", false, "") flags.BoolVar(&config.Deploy.ForceCount, "force-count", false, "") + flags.BoolVar(&config.Deploy.BlueGreen, "blue-green", false, "") flags.BoolVar(&config.Plan.IgnoreNoChanges, "ignore-no-changes", false, "") flags.StringVar(&level, "log-level", "INFO", "") flags.StringVar(&format, "log-format", "HUMAN", "") @@ -179,6 +184,18 @@ func (c *DeployCommand) Run(args []string) int { } } + if config.Deploy.BlueGreen { + if err = c.checkBlueGreen(config.Template.Job); err != nil { + c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err)) + return 1 + } + for _, taskGroup := range config.Template.Job.TaskGroups { + blueGreenCount := taskGroup.Count + taskGroup.Update.MaxParallel = blueGreenCount + taskGroup.Update.Canary = blueGreenCount + } + } + if !config.Deploy.Force { p := levant.PlanConfig{ Client: config.Client, @@ -230,3 +247,15 @@ func (c *DeployCommand) checkForceBatch(job *nomad.Job, forceBatch bool) error { return fmt.Errorf("force-batch passed but job is not periodic") } + +func (c *DeployCommand) checkBlueGreen(job *nomad.Job) error { + + if job.IsPeriodic() { + return fmt.Errorf("blue-green passed but job is periodic") + } + if *job.Type == "system" { + return fmt.Errorf(`blue-green passed but job type is "system"`) + } + + return nil +} diff --git a/docs/commands.md b/docs/commands.md index 313a7fd18..d364277ae 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -20,6 +20,8 @@ Levant supports a number of command line arguments which provide control over th * **-force-count** (bool: false) Use the taskgroup count from the Nomad job file instead of the count that is obtained from the running job count. +* **-blue-green** (bool: false) Set the max-parallel and the canary count of each task group to the current count for a blue-green type release + * **-ignore-no-changes** (bool: false) By default if no changes are detected when running a deployment Levant will exit with a status 1 to indicate a deployment didn't happen. This behaviour can be changed using this flag so that Levant will exit cleanly ensuring CD pipelines don't fail when no changes are detected * **-log-level** (string: "INFO") The level at which Levant will log to. Valid values are DEBUG, INFO, WARN, ERROR and FATAL. diff --git a/levant/structs/config.go b/levant/structs/config.go index dba4fde32..50f2857ef 100644 --- a/levant/structs/config.go +++ b/levant/structs/config.go @@ -39,6 +39,10 @@ type DeployConfig struct { // and force the count based on the rendered job file. ForceCount bool + // BlueGreen is a boolean flag that can be used to run a blue-green release, + // overwriting the max-parallel and the canary count variables + BlueGreen bool + // EnvVault is a boolean flag that can be used to enable reading the VAULT_TOKEN // from the enviromment. EnvVault bool