Skip to content

Commit bcf9bc3

Browse files
authored
feat(cli): add --provisioner-log-debug option (coder#14558)
Allows starting a build in debug mode from the CLI without needing to have the build fail first by adding `--provisioner-log-debug`.
1 parent bd90740 commit bcf9bc3

File tree

8 files changed

+77
-24
lines changed

8 files changed

+77
-24
lines changed

cli/delete.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import (
1111

1212
// nolint
1313
func (r *RootCmd) deleteWorkspace() *serpent.Command {
14-
var orphan bool
14+
var (
15+
orphan bool
16+
prov buildFlags
17+
)
1518
client := new(codersdk.Client)
1619
cmd := &serpent.Command{
1720
Annotations: workspaceCommand,
@@ -40,11 +43,15 @@ func (r *RootCmd) deleteWorkspace() *serpent.Command {
4043
}
4144

4245
var state []byte
43-
build, err := client.CreateWorkspaceBuild(inv.Context(), workspace.ID, codersdk.CreateWorkspaceBuildRequest{
46+
req := codersdk.CreateWorkspaceBuildRequest{
4447
Transition: codersdk.WorkspaceTransitionDelete,
4548
ProvisionerState: state,
4649
Orphan: orphan,
47-
})
50+
}
51+
if prov.provisionerLogDebug {
52+
req.LogLevel = codersdk.ProvisionerLogLevelDebug
53+
}
54+
build, err := client.CreateWorkspaceBuild(inv.Context(), workspace.ID, req)
4855
if err != nil {
4956
return err
5057
}
@@ -71,5 +78,6 @@ func (r *RootCmd) deleteWorkspace() *serpent.Command {
7178
},
7279
cliui.SkipPromptOption(),
7380
}
81+
cmd.Options = append(cmd.Options, prov.cliOptions()...)
7482
return cmd
7583
}

cli/parameter.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,21 @@ func parseParameterMapFile(parameterFile string) (map[string]string, error) {
127127
}
128128
return parameterMap, nil
129129
}
130+
131+
// buildFlags contains options relating to troubleshooting provisioner jobs.
132+
type buildFlags struct {
133+
provisionerLogDebug bool
134+
}
135+
136+
func (bf *buildFlags) cliOptions() []serpent.Option {
137+
return []serpent.Option{
138+
{
139+
Flag: "provisioner-log-debug",
140+
Description: `Sets the provisioner log level to debug.
141+
This will print additional information about the build process.
142+
This is useful for troubleshooting build issues.`,
143+
Value: serpent.BoolOf(&bf.provisionerLogDebug),
144+
Hidden: true,
145+
},
146+
}
147+
}

cli/restart.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import (
1414
)
1515

1616
func (r *RootCmd) restart() *serpent.Command {
17-
var parameterFlags workspaceParameterFlags
17+
var (
18+
parameterFlags workspaceParameterFlags
19+
bflags buildFlags
20+
)
1821

1922
client := new(codersdk.Client)
2023
cmd := &serpent.Command{
@@ -35,7 +38,7 @@ func (r *RootCmd) restart() *serpent.Command {
3538
return err
3639
}
3740

38-
startReq, err := buildWorkspaceStartRequest(inv, client, workspace, parameterFlags, WorkspaceRestart)
41+
startReq, err := buildWorkspaceStartRequest(inv, client, workspace, parameterFlags, bflags, WorkspaceRestart)
3942
if err != nil {
4043
return err
4144
}
@@ -48,9 +51,13 @@ func (r *RootCmd) restart() *serpent.Command {
4851
return err
4952
}
5053

51-
build, err := client.CreateWorkspaceBuild(ctx, workspace.ID, codersdk.CreateWorkspaceBuildRequest{
54+
wbr := codersdk.CreateWorkspaceBuildRequest{
5255
Transition: codersdk.WorkspaceTransitionStop,
53-
})
56+
}
57+
if bflags.provisionerLogDebug {
58+
wbr.LogLevel = codersdk.ProvisionerLogLevelDebug
59+
}
60+
build, err := client.CreateWorkspaceBuild(ctx, workspace.ID, wbr)
5461
if err != nil {
5562
return err
5663
}
@@ -65,7 +72,7 @@ func (r *RootCmd) restart() *serpent.Command {
6572
// workspaces with the active version.
6673
if cerr, ok := codersdk.AsError(err); ok && cerr.StatusCode() == http.StatusForbidden {
6774
_, _ = fmt.Fprintln(inv.Stdout, "Unable to restart the workspace with the template version from the last build. Policy may require you to restart with the current active template version.")
68-
build, err = startWorkspace(inv, client, workspace, parameterFlags, WorkspaceUpdate)
75+
build, err = startWorkspace(inv, client, workspace, parameterFlags, bflags, WorkspaceUpdate)
6976
if err != nil {
7077
return xerrors.Errorf("start workspace with active template version: %w", err)
7178
}
@@ -87,6 +94,7 @@ func (r *RootCmd) restart() *serpent.Command {
8794
}
8895

8996
cmd.Options = append(cmd.Options, parameterFlags.allOptions()...)
97+
cmd.Options = append(cmd.Options, bflags.cliOptions()...)
9098

9199
return cmd
92100
}

cli/ssh.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,9 @@ func getWorkspaceAndAgent(ctx context.Context, inv *serpent.Invocation, client *
649649
// It's possible for a workspace build to fail due to the template requiring starting
650650
// workspaces with the active version.
651651
_, _ = fmt.Fprintf(inv.Stderr, "Workspace was stopped, starting workspace to allow connecting to %q...\n", workspace.Name)
652-
_, err = startWorkspace(inv, client, workspace, workspaceParameterFlags{}, WorkspaceStart)
652+
_, err = startWorkspace(inv, client, workspace, workspaceParameterFlags{}, buildFlags{}, WorkspaceStart)
653653
if cerr, ok := codersdk.AsError(err); ok && cerr.StatusCode() == http.StatusForbidden {
654-
_, err = startWorkspace(inv, client, workspace, workspaceParameterFlags{}, WorkspaceUpdate)
654+
_, err = startWorkspace(inv, client, workspace, workspaceParameterFlags{}, buildFlags{}, WorkspaceUpdate)
655655
if err != nil {
656656
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.Errorf("start workspace with active template version: %w", err)
657657
}

cli/start.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import (
1313
)
1414

1515
func (r *RootCmd) start() *serpent.Command {
16-
var parameterFlags workspaceParameterFlags
16+
var (
17+
parameterFlags workspaceParameterFlags
18+
bflags buildFlags
19+
)
1720

1821
client := new(codersdk.Client)
1922
cmd := &serpent.Command{
@@ -45,12 +48,12 @@ func (r *RootCmd) start() *serpent.Command {
4548
)
4649
build = workspace.LatestBuild
4750
default:
48-
build, err = startWorkspace(inv, client, workspace, parameterFlags, WorkspaceStart)
51+
build, err = startWorkspace(inv, client, workspace, parameterFlags, bflags, WorkspaceStart)
4952
// It's possible for a workspace build to fail due to the template requiring starting
5053
// workspaces with the active version.
5154
if cerr, ok := codersdk.AsError(err); ok && cerr.StatusCode() == http.StatusForbidden {
5255
_, _ = fmt.Fprintln(inv.Stdout, "Unable to start the workspace with the template version from the last build. Policy may require you to restart with the current active template version.")
53-
build, err = startWorkspace(inv, client, workspace, parameterFlags, WorkspaceUpdate)
56+
build, err = startWorkspace(inv, client, workspace, parameterFlags, bflags, WorkspaceUpdate)
5457
if err != nil {
5558
return xerrors.Errorf("start workspace with active template version: %w", err)
5659
}
@@ -73,11 +76,12 @@ func (r *RootCmd) start() *serpent.Command {
7376
}
7477

7578
cmd.Options = append(cmd.Options, parameterFlags.allOptions()...)
79+
cmd.Options = append(cmd.Options, bflags.cliOptions()...)
7680

7781
return cmd
7882
}
7983

80-
func buildWorkspaceStartRequest(inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace, parameterFlags workspaceParameterFlags, action WorkspaceCLIAction) (codersdk.CreateWorkspaceBuildRequest, error) {
84+
func buildWorkspaceStartRequest(inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace, parameterFlags workspaceParameterFlags, buildFlags buildFlags, action WorkspaceCLIAction) (codersdk.CreateWorkspaceBuildRequest, error) {
8185
version := workspace.LatestBuild.TemplateVersionID
8286

8387
if workspace.AutomaticUpdates == codersdk.AutomaticUpdatesAlways || action == WorkspaceUpdate {
@@ -124,14 +128,19 @@ func buildWorkspaceStartRequest(inv *serpent.Invocation, client *codersdk.Client
124128
return codersdk.CreateWorkspaceBuildRequest{}, err
125129
}
126130

127-
return codersdk.CreateWorkspaceBuildRequest{
131+
wbr := codersdk.CreateWorkspaceBuildRequest{
128132
Transition: codersdk.WorkspaceTransitionStart,
129133
RichParameterValues: buildParameters,
130134
TemplateVersionID: version,
131-
}, nil
135+
}
136+
if buildFlags.provisionerLogDebug {
137+
wbr.LogLevel = codersdk.ProvisionerLogLevelDebug
138+
}
139+
140+
return wbr, nil
132141
}
133142

134-
func startWorkspace(inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace, parameterFlags workspaceParameterFlags, action WorkspaceCLIAction) (codersdk.WorkspaceBuild, error) {
143+
func startWorkspace(inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace, parameterFlags workspaceParameterFlags, buildFlags buildFlags, action WorkspaceCLIAction) (codersdk.WorkspaceBuild, error) {
135144
if workspace.DormantAt != nil {
136145
_, _ = fmt.Fprintln(inv.Stdout, "Activating dormant workspace...")
137146
err := client.UpdateWorkspaceDormancy(inv.Context(), workspace.ID, codersdk.UpdateWorkspaceDormancy{
@@ -141,7 +150,7 @@ func startWorkspace(inv *serpent.Invocation, client *codersdk.Client, workspace
141150
return codersdk.WorkspaceBuild{}, xerrors.Errorf("activate workspace: %w", err)
142151
}
143152
}
144-
req, err := buildWorkspaceStartRequest(inv, client, workspace, parameterFlags, action)
153+
req, err := buildWorkspaceStartRequest(inv, client, workspace, parameterFlags, buildFlags, action)
145154
if err != nil {
146155
return codersdk.WorkspaceBuild{}, err
147156
}

cli/stop.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
)
1111

1212
func (r *RootCmd) stop() *serpent.Command {
13+
var bflags buildFlags
1314
client := new(codersdk.Client)
1415
cmd := &serpent.Command{
1516
Annotations: workspaceCommand,
@@ -35,9 +36,13 @@ func (r *RootCmd) stop() *serpent.Command {
3536
if err != nil {
3637
return err
3738
}
38-
build, err := client.CreateWorkspaceBuild(inv.Context(), workspace.ID, codersdk.CreateWorkspaceBuildRequest{
39+
wbr := codersdk.CreateWorkspaceBuildRequest{
3940
Transition: codersdk.WorkspaceTransitionStop,
40-
})
41+
}
42+
if bflags.provisionerLogDebug {
43+
wbr.LogLevel = codersdk.ProvisionerLogLevelDebug
44+
}
45+
build, err := client.CreateWorkspaceBuild(inv.Context(), workspace.ID, wbr)
4146
if err != nil {
4247
return err
4348
}
@@ -56,5 +61,7 @@ func (r *RootCmd) stop() *serpent.Command {
5661
return nil
5762
},
5863
}
64+
cmd.Options = append(cmd.Options, bflags.cliOptions()...)
65+
5966
return cmd
6067
}

cli/update.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
)
1111

1212
func (r *RootCmd) update() *serpent.Command {
13-
var parameterFlags workspaceParameterFlags
14-
13+
var (
14+
parameterFlags workspaceParameterFlags
15+
bflags buildFlags
16+
)
1517
client := new(codersdk.Client)
1618
cmd := &serpent.Command{
1719
Annotations: workspaceCommand,
@@ -32,7 +34,7 @@ func (r *RootCmd) update() *serpent.Command {
3234
return nil
3335
}
3436

35-
build, err := startWorkspace(inv, client, workspace, parameterFlags, WorkspaceUpdate)
37+
build, err := startWorkspace(inv, client, workspace, parameterFlags, bflags, WorkspaceUpdate)
3638
if err != nil {
3739
return xerrors.Errorf("start workspace: %w", err)
3840
}
@@ -54,5 +56,6 @@ func (r *RootCmd) update() *serpent.Command {
5456
}
5557

5658
cmd.Options = append(cmd.Options, parameterFlags.allOptions()...)
59+
cmd.Options = append(cmd.Options, bflags.cliOptions()...)
5760
return cmd
5861
}

scripts/develop.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ fatal() {
150150
trap 'fatal "Script encountered an error"' ERR
151151

152152
cdroot
153-
DEBUG_DELVE="${debug}" start_cmd API "" "${CODER_DEV_SHIM}" server --http-address 0.0.0.0:3000 --swagger-enable --access-url "${CODER_DEV_ACCESS_URL}" --dangerous-allow-cors-requests=true "$@"
153+
DEBUG_DELVE="${debug}" start_cmd API "" "${CODER_DEV_SHIM}" server --http-address 0.0.0.0:3000 --swagger-enable --access-url "${CODER_DEV_ACCESS_URL}" --dangerous-allow-cors-requests=true --enable-terraform-debug-mode "$@"
154154

155155
echo '== Waiting for Coder to become ready'
156156
# Start the timeout in the background so interrupting this script

0 commit comments

Comments
 (0)