github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/command/deployment_pause.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/nomad/api/contexts" 8 "github.com/posener/complete" 9 ) 10 11 type DeploymentPauseCommand struct { 12 Meta 13 } 14 15 func (c *DeploymentPauseCommand) Help() string { 16 helpText := ` 17 Usage: nomad deployment pause [options] <deployment id> 18 19 Pause is used to pause a deployment. Pausing a deployment will pause the 20 placement of new allocations as part of rolling deployment. 21 22 General Options: 23 24 ` + generalOptionsUsage() + ` 25 26 Pause Options: 27 28 -verbose 29 Display full information. 30 ` 31 return strings.TrimSpace(helpText) 32 } 33 34 func (c *DeploymentPauseCommand) Synopsis() string { 35 return "Pause a deployment" 36 } 37 38 func (c *DeploymentPauseCommand) AutocompleteFlags() complete.Flags { 39 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 40 complete.Flags{ 41 "-verbose": complete.PredictNothing, 42 }) 43 } 44 45 func (c *DeploymentPauseCommand) AutocompleteArgs() complete.Predictor { 46 return complete.PredictFunc(func(a complete.Args) []string { 47 client, err := c.Meta.Client() 48 if err != nil { 49 return nil 50 } 51 52 resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Deployments, nil) 53 if err != nil { 54 return []string{} 55 } 56 return resp.Matches[contexts.Deployments] 57 }) 58 } 59 60 func (c *DeploymentPauseCommand) Run(args []string) int { 61 var verbose bool 62 63 flags := c.Meta.FlagSet("deployment pause", FlagSetClient) 64 flags.Usage = func() { c.Ui.Output(c.Help()) } 65 flags.BoolVar(&verbose, "verbose", false, "") 66 67 if err := flags.Parse(args); err != nil { 68 return 1 69 } 70 71 // Check that we got no arguments 72 args = flags.Args() 73 if l := len(args); l != 1 { 74 c.Ui.Error(c.Help()) 75 return 1 76 } 77 78 dID := args[0] 79 80 // Truncate the id unless full length is requested 81 length := shortId 82 if verbose { 83 length = fullId 84 } 85 86 // Get the HTTP client 87 client, err := c.Meta.Client() 88 if err != nil { 89 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 90 return 1 91 } 92 93 // Do a prefix lookup 94 deploy, possible, err := getDeployment(client.Deployments(), dID) 95 if err != nil { 96 c.Ui.Error(fmt.Sprintf("Error retrieving deployment: %s", err)) 97 return 1 98 } 99 100 if len(possible) != 0 { 101 c.Ui.Error(fmt.Sprintf("Prefix matched multiple deployments\n\n%s", formatDeployments(possible, length))) 102 return 1 103 } 104 105 if _, _, err := client.Deployments().Pause(deploy.ID, true, nil); err != nil { 106 c.Ui.Error(fmt.Sprintf("Error pausing deployment: %s", err)) 107 return 1 108 } 109 110 c.Ui.Output(fmt.Sprintf("Deployment %q paused", deploy.ID)) 111 return 0 112 }