github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/command/deployment_promote.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/nomad/api" 8 flaghelper "github.com/hashicorp/nomad/helper/flag-helpers" 9 ) 10 11 type DeploymentPromoteCommand struct { 12 Meta 13 } 14 15 func (c *DeploymentPromoteCommand) Help() string { 16 helpText := ` 17 Usage: nomad deployment promote [options] <deployment id> 18 19 Promote is used to promote task groups in a deployment. Promotion should occur 20 when the deployment has placed canaries for a task group and those canaries have 21 been deemed healthy. When a task group is promoted, the rolling upgrade of the 22 remaining allocations is unblocked. If the canaries are found to be unhealthy, 23 the deployment may either be failed using the "nomad deployment fail" command, 24 the job can be failed forward by submitting a new version or failed backwards by 25 reverting to an older version using the "nomad job revert" command. 26 27 General Options: 28 29 ` + generalOptionsUsage() + ` 30 31 Promote Options: 32 33 -group 34 Group may be specified many times and is used to promote that particular 35 group. If no specific groups are specified, all groups are promoted. 36 37 -detach 38 Return immediately instead of entering monitor mode. After deployment 39 resume, the evaluation ID will be printed to the screen, which can be used 40 to examine the evaluation using the eval-status command. 41 42 -verbose 43 Display full information. 44 ` 45 return strings.TrimSpace(helpText) 46 } 47 48 func (c *DeploymentPromoteCommand) Synopsis() string { 49 return "Promote canaries in a deployment" 50 } 51 52 func (c *DeploymentPromoteCommand) Run(args []string) int { 53 var detach, verbose bool 54 var groups []string 55 56 flags := c.Meta.FlagSet("deployment promote", FlagSetClient) 57 flags.Usage = func() { c.Ui.Output(c.Help()) } 58 flags.BoolVar(&detach, "detach", false, "") 59 flags.BoolVar(&verbose, "verbose", false, "") 60 flags.Var((*flaghelper.StringFlag)(&groups), "group", "") 61 62 if err := flags.Parse(args); err != nil { 63 return 1 64 } 65 66 // Check that we got no arguments 67 args = flags.Args() 68 if l := len(args); l != 1 { 69 c.Ui.Error(c.Help()) 70 return 1 71 } 72 dID := args[0] 73 74 // Truncate the id unless full length is requested 75 length := shortId 76 if verbose { 77 length = fullId 78 } 79 80 // Get the HTTP client 81 client, err := c.Meta.Client() 82 if err != nil { 83 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 84 return 1 85 } 86 87 // Do a prefix lookup 88 deploy, possible, err := getDeployment(client.Deployments(), dID) 89 if err != nil { 90 c.Ui.Error(fmt.Sprintf("Error retrieving deployment: %s", err)) 91 return 1 92 } 93 94 if len(possible) != 0 { 95 c.Ui.Error(fmt.Sprintf("Prefix matched multiple deployments\n\n%s", formatDeployments(possible, length))) 96 return 1 97 } 98 99 var u *api.DeploymentUpdateResponse 100 if len(groups) == 0 { 101 u, _, err = client.Deployments().PromoteAll(deploy.ID, nil) 102 } else { 103 u, _, err = client.Deployments().PromoteGroups(deploy.ID, groups, nil) 104 } 105 106 if err != nil { 107 c.Ui.Error(fmt.Sprintf("Error promoting deployment: %s", err)) 108 return 1 109 } 110 111 // Nothing to do 112 evalCreated := u.EvalID != "" 113 if detach || !evalCreated { 114 return 0 115 } 116 117 mon := newMonitor(c.Ui, client, length) 118 return mon.monitor(u.EvalID, false) 119 }