run start: add --wait to block until the run finishes - #87
Conversation
`run start --wait` polls the newly created run to a terminal state, streaming each status transition, and maps the outcome to an exit code: applied / planned_and_finished / planned_and_saved succeed, while errored, canceled, discarded, and policy failures exit non-zero. A plan that finishes but needs a manual apply (auto-apply disabled) stops rather than hanging. --timeout bounds the wait; if it elapses, tfctl stops watching and exits non-zero, but the run keeps running in HCP Terraform. On completion the elapsed time and run URL are printed. The final summary reuses the same renderer as `run status`.
brandonc
left a comment
There was a problem hiding this comment.
Thanks for the PR! I'm going to use your commits and implement the review feedback I've given here.
| }, | ||
| { | ||
| Name: "timeout", | ||
| Description: "With --wait, the maximum time to wait for the run to finish (e.g. 30m). Defaults to waiting indefinitely.", |
There was a problem hiding this comment.
Let's mention the valid units and parsing arrangement.
| Description: "With --wait, the maximum time to wait for the run to finish (e.g. 30m). Defaults to waiting indefinitely.", | |
| Description: "With --wait, the maximum time to wait for the run to finish. Defaults to waiting indefinitely. Examples include \"30s\", \"1.5h\" or \"2h45m\". Valid time units are \"s\", \"m\", \"h\".", |
| start := time.Now() | ||
| fmt.Fprintf(io.Err(), "%s %s created; waiting for it to finish...\n", cs.SuccessIcon(), runID) | ||
|
|
||
| _, outcome, err := pollRunUntilSettled(ctx, opts.APIClient, runID, io, opts.PollInterval, opts.Timeout) |
There was a problem hiding this comment.
We should use a context timeout instead of a loop condition so that the timeout is more strictly adhered to.
| _, outcome, err := pollRunUntilSettled(ctx, opts.APIClient, runID, io, opts.PollInterval, opts.Timeout) | |
| ctx, cancel := context.WithTimeoutCause(ctx, opts.Timeout, errors.New("--wait timeout exceeded")) | |
| defer cancel() | |
| _, outcome, err := pollRunUntilSettled(ctx, opts.APIClient, runID, io, opts.PollInterval, opts.Timeout) |
| // line reads as cleanly as the succeeded/failed cases. | ||
| if outcome == runAwaitingConfirm { | ||
| summary.Message = "Plan finished; a manual apply is required (auto-apply is off)." | ||
| } |
There was a problem hiding this comment.
Can you promote this message to NewRunSummary/buildRunSummary? I think this could be valuable for any command that uses NewRunSummary.
NewRunSummary reads the Run and can use Actions.IsConfirmable to refine the message based on the status in buildRunSummary... and export the value so you can read it off summary as a caller.
|
Made some changes and opened #107 |
Description
run startreturns as soon as the run is created, so automating a plan/apply means hand-rolling a poll loop againstrun status. This adds--wait, which blocks until the run reaches a terminal state and maps the outcome to an exit code, sorun startis usable directly in scripts and CI.Behavior
applied/planned_and_finished/planned_and_savedexit0;errored,canceled,discarded, and mandatory policy failures (policy_soft_failed,policy_override) exit non-zero.actions.is-confirmable.--timeoutbounds the wait. If it elapses, tfctl stops watching and exits non-zero, but the run keeps running in HCP Terraform (this is a watch budget, not a cancel); the run URL is surfaced so you can follow up.run status(diagnostics, policy failures, task results).Example Output
Wait through an auto-apply run:
Plan-only, or a workspace without auto-apply, stops for a manual apply rather than hanging:
Timeout leaves the run running server-side and exits non-zero:
PR Checklist
npx changie newor install changie to prepare a new changelog entry for the next set of release notes..changes/unreleased/ENHANCEMENTS-*.yaml(kind: ENHANCEMENTS).--json— Force machine readable output to stdout. Does not apply to stderr.--markdown— Force markdown output to stdout. Does not apply to stderr.--dry-run— Don't make any actual writes or other mutations. Describe what would have changed to stderr.--quiet— Don't render output to stdout.run statusOutputter, so it honors--json/--markdown/--quietidentically torun status.--waitis a no-op under--dry-run(dry-run returns before any run is created).make gen/screenshotif the root command output changes.run startgains flags; the root command output is unchanged.Autocompletefield to positional arguments and flags to assist shell autocomplete.--waitis boolean and--timeouttakes a freeform duration; neither has an enumerable completion set (consistent with the siblingrun startboolean/value flags).PCI review checklist
I have documented a clear reason for, and description of, the change I am making.
If applicable, I've documented a plan to revert these changes if they require more than reverting the pull request.
If applicable, I've documented the impact of any changes to security controls.
--waitonly polls read-only run status with the existing token; no auth, permission, or other security-control surface is changed.