Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c7ddb0

Browse files
committed
feat(cli): add --debug-provisioner argument
1 parent bd90740 commit 0c7ddb0

18 files changed

+141
-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+
buildDebug buildDebugFlags
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 buildDebug.provisioner {
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, buildDebug.cliOptions()...)
7482
return cmd
7583
}

cli/parameter.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,20 @@ func parseParameterMapFile(parameterFile string) (map[string]string, error) {
127127
}
128128
return parameterMap, nil
129129
}
130+
131+
// buildDebugFlags contains options relating to troubleshooting build issues.
132+
type buildDebugFlags struct {
133+
provisioner bool
134+
}
135+
136+
func (bdf *buildDebugFlags) cliOptions() []serpent.Option {
137+
return []serpent.Option{
138+
{
139+
Flag: "debug-provisioner",
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(&bdf.provisioner),
144+
},
145+
}
146+
}

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+
debugFlags buildDebugFlags
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, debugFlags, 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 debugFlags.provisioner {
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, debugFlags, 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, debugFlags.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{}, buildDebugFlags{}, 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{}, buildDebugFlags{}, 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+
debugFlags buildDebugFlags
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, debugFlags, 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, debugFlags, 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, debugFlags.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, debugFlags buildDebugFlags, 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 debugFlags.provisioner {
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, debugFlags buildDebugFlags, 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, debugFlags, 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 debugFlags buildDebugFlags
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 debugFlags.provisioner {
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, debugFlags.cliOptions()...)
65+
5966
return cmd
6067
}

cli/testdata/coder_delete_--help.golden

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ USAGE:
88
Aliases: rm
99

1010
OPTIONS:
11+
--debug-provisioner bool
12+
Sets the provisioner log level to debug.
13+
This will print additional information about the build process.
14+
This is useful for troubleshooting build issues.
15+
1116
--orphan bool
1217
Delete a workspace without deleting its resources. This can delete a
1318
workspace in a broken state, but may also lead to unaccounted cloud

cli/testdata/coder_restart_--help.golden

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ OPTIONS:
1616
--build-options bool
1717
Prompt for one-time build options defined with ephemeral parameters.
1818

19+
--debug-provisioner bool
20+
Sets the provisioner log level to debug.
21+
This will print additional information about the build process.
22+
This is useful for troubleshooting build issues.
23+
1924
--parameter string-array, $CODER_RICH_PARAMETER
2025
Rich parameter value in the format "name=value".
2126

cli/testdata/coder_start_--help.golden

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ OPTIONS:
1616
--build-options bool
1717
Prompt for one-time build options defined with ephemeral parameters.
1818

19+
--debug-provisioner bool
20+
Sets the provisioner log level to debug.
21+
This will print additional information about the build process.
22+
This is useful for troubleshooting build issues.
23+
1924
--parameter string-array, $CODER_RICH_PARAMETER
2025
Rich parameter value in the format "name=value".
2126

cli/testdata/coder_stop_--help.golden

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ USAGE:
66
Stop a workspace
77

88
OPTIONS:
9+
--debug-provisioner bool
10+
Sets the provisioner log level to debug.
11+
This will print additional information about the build process.
12+
This is useful for troubleshooting build issues.
13+
914
-y, --yes bool
1015
Bypass prompts.
1116

cli/testdata/coder_update_--help.golden

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ OPTIONS:
1818
--build-options bool
1919
Prompt for one-time build options defined with ephemeral parameters.
2020

21+
--debug-provisioner bool
22+
Sets the provisioner log level to debug.
23+
This will print additional information about the build process.
24+
This is useful for troubleshooting build issues.
25+
2126
--parameter string-array, $CODER_RICH_PARAMETER
2227
Rich parameter value in the format "name=value".
2328

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+
debugFlags buildDebugFlags
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, debugFlags, 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, debugFlags.cliOptions()...)
5760
return cmd
5861
}

docs/reference/cli/delete.md

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/cli/restart.md

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/cli/start.md

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/cli/stop.md

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/cli/update.md

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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)