cli: `wait` flag for use with `deployment status -monitor` (#15262)

This commit is contained in:
Jack 2022-11-23 21:36:13 +00:00 committed by GitHub
parent 918a91f8bf
commit 62f7de7ed5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 13 deletions

3
.changelog/15262.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
cli: Added `-wait` flag to `deployment status` for use with `-monitor` mode
```

View File

@ -48,6 +48,10 @@ Status Options:
-monitor -monitor
Enter monitor mode to poll for updates to the deployment status. Enter monitor mode to poll for updates to the deployment status.
-wait
How long to wait before polling an update, used in conjunction with monitor
mode. Defaults to 2s.
-t -t
Format and display deployment using a Go template. Format and display deployment using a Go template.
` `
@ -87,6 +91,7 @@ func (c *DeploymentStatusCommand) Name() string { return "deployment status" }
func (c *DeploymentStatusCommand) Run(args []string) int { func (c *DeploymentStatusCommand) Run(args []string) int {
var json, verbose, monitor bool var json, verbose, monitor bool
var wait time.Duration
var tmpl string var tmpl string
flags := c.Meta.FlagSet(c.Name(), FlagSetClient) flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
@ -95,6 +100,7 @@ func (c *DeploymentStatusCommand) Run(args []string) int {
flags.BoolVar(&json, "json", false, "") flags.BoolVar(&json, "json", false, "")
flags.BoolVar(&monitor, "monitor", false, "") flags.BoolVar(&monitor, "monitor", false, "")
flags.StringVar(&tmpl, "t", "", "") flags.StringVar(&tmpl, "t", "", "")
flags.DurationVar(&wait, "wait", 2*time.Second, "")
if err := flags.Parse(args); err != nil { if err := flags.Parse(args); err != nil {
return 1 return 1
@ -172,7 +178,7 @@ func (c *DeploymentStatusCommand) Run(args []string) int {
c.Ui.Output(fmt.Sprintf("%s: Monitoring deployment %q", c.Ui.Output(fmt.Sprintf("%s: Monitoring deployment %q",
formatTime(time.Now()), limit(deploy.ID, length))) formatTime(time.Now()), limit(deploy.ID, length)))
c.monitor(client, deploy.ID, meta.LastIndex, verbose) c.monitor(client, deploy.ID, meta.LastIndex, wait, verbose)
return 0 return 0
} }
@ -180,11 +186,11 @@ func (c *DeploymentStatusCommand) Run(args []string) int {
return 0 return 0
} }
func (c *DeploymentStatusCommand) monitor(client *api.Client, deployID string, index uint64, verbose bool) (status string, err error) { func (c *DeploymentStatusCommand) monitor(client *api.Client, deployID string, index uint64, wait time.Duration, verbose bool) (status string, err error) {
if isStdoutTerminal() { if isStdoutTerminal() {
return c.ttyMonitor(client, deployID, index, verbose) return c.ttyMonitor(client, deployID, index, wait, verbose)
} else { } else {
return c.defaultMonitor(client, deployID, index, verbose) return c.defaultMonitor(client, deployID, index, wait, verbose)
} }
} }
@ -207,7 +213,7 @@ func isStdoutTerminal() bool {
// but only used for tty and non-Windows machines since glint doesn't work with // but only used for tty and non-Windows machines since glint doesn't work with
// cmd/PowerShell and non-interactive interfaces // cmd/PowerShell and non-interactive interfaces
// Margins are used to match the text alignment from job run // Margins are used to match the text alignment from job run
func (c *DeploymentStatusCommand) ttyMonitor(client *api.Client, deployID string, index uint64, verbose bool) (status string, err error) { func (c *DeploymentStatusCommand) ttyMonitor(client *api.Client, deployID string, index uint64, wait time.Duration, verbose bool) (status string, err error) {
var length int var length int
if verbose { if verbose {
length = fullId length = fullId
@ -233,7 +239,7 @@ func (c *DeploymentStatusCommand) ttyMonitor(client *api.Client, deployID string
q := api.QueryOptions{ q := api.QueryOptions{
AllowStale: true, AllowStale: true,
WaitIndex: index, WaitIndex: index,
WaitTime: 2 * time.Second, WaitTime: wait,
} }
var statusComponent *glint.LayoutComponent var statusComponent *glint.LayoutComponent
@ -331,7 +337,7 @@ UPDATE:
} }
d.Close() d.Close()
c.ttyMonitor(client, rollback.ID, index, verbose) c.ttyMonitor(client, rollback.ID, index, wait, verbose)
return return
} else { } else {
endSpinner = glint.Layout( endSpinner = glint.Layout(
@ -361,7 +367,7 @@ UPDATE:
} }
// Used for Windows and non-tty // Used for Windows and non-tty
func (c *DeploymentStatusCommand) defaultMonitor(client *api.Client, deployID string, index uint64, verbose bool) (status string, err error) { func (c *DeploymentStatusCommand) defaultMonitor(client *api.Client, deployID string, index uint64, wait time.Duration, verbose bool) (status string, err error) {
writer := uilive.New() writer := uilive.New()
writer.Start() writer.Start()
defer writer.Stop() defer writer.Stop()
@ -376,7 +382,7 @@ func (c *DeploymentStatusCommand) defaultMonitor(client *api.Client, deployID st
q := api.QueryOptions{ q := api.QueryOptions{
AllowStale: true, AllowStale: true,
WaitIndex: index, WaitIndex: index,
WaitTime: 2 * time.Second, WaitTime: wait,
} }
for { for {
@ -438,7 +444,7 @@ func (c *DeploymentStatusCommand) defaultMonitor(client *api.Client, deployID st
if rollback.ID == deploy.ID { if rollback.ID == deploy.ID {
return return
} }
c.defaultMonitor(client, rollback.ID, index, verbose) c.defaultMonitor(client, rollback.ID, index, wait, verbose)
} }
return return

View File

@ -299,7 +299,7 @@ func (m *monitor) monitor(evalID string) int {
meta := new(Meta) meta := new(Meta)
meta.Ui = m.ui meta.Ui = m.ui
cmd := &DeploymentStatusCommand{Meta: *meta} cmd := &DeploymentStatusCommand{Meta: *meta}
status, err := cmd.monitor(m.client, dID, 0, verbose) status, err := cmd.monitor(m.client, dID, 0, m.state.wait, verbose)
if err != nil || status != api.DeploymentStatusSuccessful { if err != nil || status != api.DeploymentStatusSuccessful {
return 1 return 1
} }

View File

@ -20,8 +20,8 @@ nomad deployment status [options] <deployment id>
The `deployment status` command requires a single argument, a deployment ID or The `deployment status` command requires a single argument, a deployment ID or
prefix. prefix.
A `-monitor` flag can be used to update the current deployment status until completion. A `-monitor` flag can be used to update the current deployment status until completion.
When combined with `-verbose`, it will also display the allocations for the given When combined with `-verbose`, it will also display the allocations for the given
deployment. If the deployment fails and [`auto_revert`] is set to `true`, it will monitor deployment. If the deployment fails and [`auto_revert`] is set to `true`, it will monitor
the entire process, showing the failure and then monitoring the deployment of the rollback. the entire process, showing the failure and then monitoring the deployment of the rollback.
@ -38,6 +38,8 @@ capability for the deployment's namespace.
- `-t` : Format and display the deployment using a Go template. - `-t` : Format and display the deployment using a Go template.
- `-verbose`: Show full information. - `-verbose`: Show full information.
- `-monitor`: Enter monitor mode to poll for updates to the deployment status. - `-monitor`: Enter monitor mode to poll for updates to the deployment status.
- `-wait`: How long to wait before polling an update, used in conjunction with monitor
mode. Defaults to 2s.
## Examples ## Examples