1package command
2
3import (
4	"fmt"
5	"strings"
6
7	"github.com/hashicorp/nomad/api/contexts"
8	"github.com/posener/complete"
9)
10
11type DeploymentResumeCommand struct {
12	Meta
13}
14
15func (c *DeploymentResumeCommand) Help() string {
16	helpText := `
17Usage: nomad deployment resume [options] <deployment id>
18
19  Resume is used to unpause a paused deployment. Resuming a deployment will
20  resume the placement of new allocations as part of rolling deployment.
21
22General Options:
23
24  ` + generalOptionsUsage() + `
25
26Resume Options:
27
28  -detach
29    Return immediately instead of entering monitor mode. After deployment
30    resume, the evaluation ID will be printed to the screen, which can be used
31    to examine the evaluation using the eval-status command.
32
33  -verbose
34    Display full information.
35`
36	return strings.TrimSpace(helpText)
37}
38
39func (c *DeploymentResumeCommand) Synopsis() string {
40	return "Resume a paused deployment"
41}
42
43func (c *DeploymentResumeCommand) AutocompleteFlags() complete.Flags {
44	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
45		complete.Flags{
46			"-detach":  complete.PredictNothing,
47			"-verbose": complete.PredictNothing,
48		})
49}
50
51func (c *DeploymentResumeCommand) AutocompleteArgs() complete.Predictor {
52	return complete.PredictFunc(func(a complete.Args) []string {
53		client, err := c.Meta.Client()
54		if err != nil {
55			return nil
56		}
57
58		resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Deployments, nil)
59		if err != nil {
60			return []string{}
61		}
62		return resp.Matches[contexts.Deployments]
63	})
64}
65
66func (c *DeploymentResumeCommand) Name() string { return "deployment resume" }
67func (c *DeploymentResumeCommand) Run(args []string) int {
68	var detach, verbose bool
69
70	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
71	flags.Usage = func() { c.Ui.Output(c.Help()) }
72	flags.BoolVar(&detach, "detach", false, "")
73	flags.BoolVar(&verbose, "verbose", false, "")
74
75	if err := flags.Parse(args); err != nil {
76		return 1
77	}
78
79	// Check that we got exactly one argument
80	args = flags.Args()
81	if l := len(args); l != 1 {
82		c.Ui.Error("This command takes one argument: <deployment id>")
83		c.Ui.Error(commandErrorText(c))
84		return 1
85	}
86
87	dID := args[0]
88
89	// Truncate the id unless full length is requested
90	length := shortId
91	if verbose {
92		length = fullId
93	}
94
95	// Get the HTTP client
96	client, err := c.Meta.Client()
97	if err != nil {
98		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
99		return 1
100	}
101
102	// Do a prefix lookup
103	deploy, possible, err := getDeployment(client.Deployments(), dID)
104	if err != nil {
105		c.Ui.Error(fmt.Sprintf("Error retrieving deployment: %s", err))
106		return 1
107	}
108
109	if len(possible) != 0 {
110		c.Ui.Error(fmt.Sprintf("Prefix matched multiple deployments\n\n%s", formatDeployments(possible, length)))
111		return 1
112	}
113
114	u, _, err := client.Deployments().Pause(deploy.ID, false, nil)
115	if err != nil {
116		c.Ui.Error(fmt.Sprintf("Error resuming deployment: %s", err))
117		return 1
118	}
119
120	c.Ui.Output(fmt.Sprintf("Deployment %q resumed", deploy.ID))
121	evalCreated := u.EvalID != ""
122
123	// Nothing to do
124	if detach || !evalCreated {
125		return 0
126	}
127
128	c.Ui.Output("")
129	mon := newMonitor(c.Ui, client, length)
130	return mon.monitor(u.EvalID, false)
131}
132