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 3 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
25 changes: 15 additions & 10 deletions command/deployment_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ Status Options:
-monitor
Enter monitor mode to poll for updates to the deployment status.

-wait-time
How long to wait before polling an update, used in conjunction with monitor mode. Defaults to 2s.
jdockerty marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

if err := flags.Parse(args); err != nil {
return 1
Expand Down Expand Up @@ -172,19 +177,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, waitTime, 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 +212,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 +238,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 +336,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 +366,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 +381,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 +443,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