Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/15262.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: Added `-wait` flag to `deployment status` for use with `-monitor` mode
```
26 changes: 16 additions & 10 deletions command/deployment_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Status Options:
-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.

-t
Format and display deployment using a Go template.
`
Expand Down Expand Up @@ -87,6 +91,7 @@ func (c *DeploymentStatusCommand) Name() string { return "deployment status" }

func (c *DeploymentStatusCommand) Run(args []string) int {
var json, verbose, monitor bool
var wait time.Duration
var tmpl string

flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
Expand All @@ -95,6 +100,7 @@ func (c *DeploymentStatusCommand) Run(args []string) int {
flags.BoolVar(&json, "json", false, "")
flags.BoolVar(&monitor, "monitor", false, "")
flags.StringVar(&tmpl, "t", "", "")
flags.DurationVar(&wait, "wait", 2*time.Second, "")

if err := flags.Parse(args); err != nil {
return 1
Expand Down Expand Up @@ -172,19 +178,19 @@ func (c *DeploymentStatusCommand) Run(args []string) int {

c.Ui.Output(fmt.Sprintf("%s: Monitoring deployment %q",
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
}
c.Ui.Output(c.Colorize().Color(formatDeployment(client, deploy, length)))
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() {
return c.ttyMonitor(client, deployID, index, verbose)
return c.ttyMonitor(client, deployID, index, wait, verbose)
} else {
return c.defaultMonitor(client, deployID, index, verbose)
return c.defaultMonitor(client, deployID, index, wait, verbose)
}
}

Expand All @@ -207,7 +213,7 @@ func isStdoutTerminal() bool {
// but only used for tty and non-Windows machines since glint doesn't work with
// cmd/PowerShell and non-interactive interfaces
// 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
if verbose {
length = fullId
Expand All @@ -233,7 +239,7 @@ func (c *DeploymentStatusCommand) ttyMonitor(client *api.Client, deployID string
q := api.QueryOptions{
AllowStale: true,
WaitIndex: index,
WaitTime: 2 * time.Second,
WaitTime: wait,
}

var statusComponent *glint.LayoutComponent
Expand Down Expand Up @@ -331,7 +337,7 @@ UPDATE:
}

d.Close()
c.ttyMonitor(client, rollback.ID, index, verbose)
c.ttyMonitor(client, rollback.ID, index, wait, verbose)
return
} else {
endSpinner = glint.Layout(
Expand Down Expand Up @@ -361,7 +367,7 @@ UPDATE:
}

// 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.Start()
defer writer.Stop()
Expand All @@ -376,7 +382,7 @@ func (c *DeploymentStatusCommand) defaultMonitor(client *api.Client, deployID st
q := api.QueryOptions{
AllowStale: true,
WaitIndex: index,
WaitTime: 2 * time.Second,
WaitTime: wait,
}

for {
Expand Down Expand Up @@ -438,7 +444,7 @@ func (c *DeploymentStatusCommand) defaultMonitor(client *api.Client, deployID st
if rollback.ID == deploy.ID {
return
}
c.defaultMonitor(client, rollback.ID, index, verbose)
c.defaultMonitor(client, rollback.ID, index, wait, verbose)
}
return

Expand Down
2 changes: 1 addition & 1 deletion command/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (m *monitor) monitor(evalID string) int {
meta := new(Meta)
meta.Ui = m.ui
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 {
return 1
}
Expand Down
6 changes: 4 additions & 2 deletions website/content/docs/commands/deployment/status.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ nomad deployment status [options] <deployment id>
The `deployment status` command requires a single argument, a deployment ID or
prefix.

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
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
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.

Expand All @@ -38,6 +38,8 @@ capability for the deployment's namespace.
- `-t` : Format and display the deployment using a Go template.
- `-verbose`: Show full information.
- `-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

Expand Down