Skip to content

feat(cli): add --provisioner-log-debug option #14558

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

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(cli): add --debug-provisioner argument
  • Loading branch information
johnstcn committed Sep 4, 2024
commit 0c7ddb0ab4bcac344a2d818cf11ad91574700311
14 changes: 11 additions & 3 deletions cli/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (

// nolint
func (r *RootCmd) deleteWorkspace() *serpent.Command {
var orphan bool
var (
orphan bool
buildDebug buildDebugFlags
)
client := new(codersdk.Client)
cmd := &serpent.Command{
Annotations: workspaceCommand,
Expand Down Expand Up @@ -40,11 +43,15 @@ func (r *RootCmd) deleteWorkspace() *serpent.Command {
}

var state []byte
build, err := client.CreateWorkspaceBuild(inv.Context(), workspace.ID, codersdk.CreateWorkspaceBuildRequest{
req := codersdk.CreateWorkspaceBuildRequest{
Transition: codersdk.WorkspaceTransitionDelete,
ProvisionerState: state,
Orphan: orphan,
})
}
if buildDebug.provisioner {
req.LogLevel = codersdk.ProvisionerLogLevelDebug
}
build, err := client.CreateWorkspaceBuild(inv.Context(), workspace.ID, req)
if err != nil {
return err
}
Expand All @@ -71,5 +78,6 @@ func (r *RootCmd) deleteWorkspace() *serpent.Command {
},
cliui.SkipPromptOption(),
}
cmd.Options = append(cmd.Options, buildDebug.cliOptions()...)
return cmd
}
17 changes: 17 additions & 0 deletions cli/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,20 @@ func parseParameterMapFile(parameterFile string) (map[string]string, error) {
}
return parameterMap, nil
}

// buildDebugFlags contains options relating to troubleshooting build issues.
type buildDebugFlags struct {
provisioner bool
}

func (bdf *buildDebugFlags) cliOptions() []serpent.Option {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we instead hook into the CODER_VERBOSE setting?
I'm curious why we need a new setting.

Copy link
Member Author

@johnstcn johnstcn Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enabling provisioner debug logs is only allowed if --enable-terraform-debug-mode is set. Hooking into CODER_VERBOSE would potentially make unrelated stuff fail if this was not set.

If we did want to re-use this flag, we would need to first fetch the deployment config and check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of piggy-backing on verbose either, it essentially changes the behavior of a build, not just the output of the CLI.

return []serpent.Option{
{
Flag: "debug-provisioner",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not so much debugging the provisioner as we're debugging the build via increased provisioner logs. I think we can do a bit better on the naming but I don't have any great ideas. Maybe --debug-build?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could just straight-up make it --provisioner-log-debug? WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That still sounds like provisioner debugging IMO, not build debugging 😅

Description: `Sets the provisioner log level to debug.
This will print additional information about the build process.
This is useful for troubleshooting build issues.`,
Value: serpent.BoolOf(&bdf.provisioner),
},
}
}
18 changes: 13 additions & 5 deletions cli/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
)

func (r *RootCmd) restart() *serpent.Command {
var parameterFlags workspaceParameterFlags
var (
parameterFlags workspaceParameterFlags
debugFlags buildDebugFlags
)

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

startReq, err := buildWorkspaceStartRequest(inv, client, workspace, parameterFlags, WorkspaceRestart)
startReq, err := buildWorkspaceStartRequest(inv, client, workspace, parameterFlags, debugFlags, WorkspaceRestart)
if err != nil {
return err
}
Expand All @@ -48,9 +51,13 @@ func (r *RootCmd) restart() *serpent.Command {
return err
}

build, err := client.CreateWorkspaceBuild(ctx, workspace.ID, codersdk.CreateWorkspaceBuildRequest{
wbr := codersdk.CreateWorkspaceBuildRequest{
Transition: codersdk.WorkspaceTransitionStop,
})
}
if debugFlags.provisioner {
wbr.LogLevel = codersdk.ProvisionerLogLevelDebug
}
build, err := client.CreateWorkspaceBuild(ctx, workspace.ID, wbr)
if err != nil {
return err
}
Expand All @@ -65,7 +72,7 @@ func (r *RootCmd) restart() *serpent.Command {
// workspaces with the active version.
if cerr, ok := codersdk.AsError(err); ok && cerr.StatusCode() == http.StatusForbidden {
_, _ = 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.")
build, err = startWorkspace(inv, client, workspace, parameterFlags, WorkspaceUpdate)
build, err = startWorkspace(inv, client, workspace, parameterFlags, debugFlags, WorkspaceUpdate)
if err != nil {
return xerrors.Errorf("start workspace with active template version: %w", err)
}
Expand All @@ -87,6 +94,7 @@ func (r *RootCmd) restart() *serpent.Command {
}

cmd.Options = append(cmd.Options, parameterFlags.allOptions()...)
cmd.Options = append(cmd.Options, debugFlags.cliOptions()...)

return cmd
}
4 changes: 2 additions & 2 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,9 @@ func getWorkspaceAndAgent(ctx context.Context, inv *serpent.Invocation, client *
// It's possible for a workspace build to fail due to the template requiring starting
// workspaces with the active version.
_, _ = fmt.Fprintf(inv.Stderr, "Workspace was stopped, starting workspace to allow connecting to %q...\n", workspace.Name)
_, err = startWorkspace(inv, client, workspace, workspaceParameterFlags{}, WorkspaceStart)
_, err = startWorkspace(inv, client, workspace, workspaceParameterFlags{}, buildDebugFlags{}, WorkspaceStart)
if cerr, ok := codersdk.AsError(err); ok && cerr.StatusCode() == http.StatusForbidden {
_, err = startWorkspace(inv, client, workspace, workspaceParameterFlags{}, WorkspaceUpdate)
_, err = startWorkspace(inv, client, workspace, workspaceParameterFlags{}, buildDebugFlags{}, WorkspaceUpdate)
if err != nil {
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.Errorf("start workspace with active template version: %w", err)
}
Expand Down
25 changes: 17 additions & 8 deletions cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
)

func (r *RootCmd) start() *serpent.Command {
var parameterFlags workspaceParameterFlags
var (
parameterFlags workspaceParameterFlags
debugFlags buildDebugFlags
)

client := new(codersdk.Client)
cmd := &serpent.Command{
Expand Down Expand Up @@ -45,12 +48,12 @@ func (r *RootCmd) start() *serpent.Command {
)
build = workspace.LatestBuild
default:
build, err = startWorkspace(inv, client, workspace, parameterFlags, WorkspaceStart)
build, err = startWorkspace(inv, client, workspace, parameterFlags, debugFlags, WorkspaceStart)
// It's possible for a workspace build to fail due to the template requiring starting
// workspaces with the active version.
if cerr, ok := codersdk.AsError(err); ok && cerr.StatusCode() == http.StatusForbidden {
_, _ = 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.")
build, err = startWorkspace(inv, client, workspace, parameterFlags, WorkspaceUpdate)
build, err = startWorkspace(inv, client, workspace, parameterFlags, debugFlags, WorkspaceUpdate)
if err != nil {
return xerrors.Errorf("start workspace with active template version: %w", err)
}
Expand All @@ -73,11 +76,12 @@ func (r *RootCmd) start() *serpent.Command {
}

cmd.Options = append(cmd.Options, parameterFlags.allOptions()...)
cmd.Options = append(cmd.Options, debugFlags.cliOptions()...)

return cmd
}

func buildWorkspaceStartRequest(inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace, parameterFlags workspaceParameterFlags, action WorkspaceCLIAction) (codersdk.CreateWorkspaceBuildRequest, error) {
func buildWorkspaceStartRequest(inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace, parameterFlags workspaceParameterFlags, debugFlags buildDebugFlags, action WorkspaceCLIAction) (codersdk.CreateWorkspaceBuildRequest, error) {
version := workspace.LatestBuild.TemplateVersionID

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

return codersdk.CreateWorkspaceBuildRequest{
wbr := codersdk.CreateWorkspaceBuildRequest{
Transition: codersdk.WorkspaceTransitionStart,
RichParameterValues: buildParameters,
TemplateVersionID: version,
}, nil
}
if debugFlags.provisioner {
wbr.LogLevel = codersdk.ProvisionerLogLevelDebug
}

return wbr, nil
}

func startWorkspace(inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace, parameterFlags workspaceParameterFlags, action WorkspaceCLIAction) (codersdk.WorkspaceBuild, error) {
func startWorkspace(inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace, parameterFlags workspaceParameterFlags, debugFlags buildDebugFlags, action WorkspaceCLIAction) (codersdk.WorkspaceBuild, error) {
if workspace.DormantAt != nil {
_, _ = fmt.Fprintln(inv.Stdout, "Activating dormant workspace...")
err := client.UpdateWorkspaceDormancy(inv.Context(), workspace.ID, codersdk.UpdateWorkspaceDormancy{
Expand All @@ -141,7 +150,7 @@ func startWorkspace(inv *serpent.Invocation, client *codersdk.Client, workspace
return codersdk.WorkspaceBuild{}, xerrors.Errorf("activate workspace: %w", err)
}
}
req, err := buildWorkspaceStartRequest(inv, client, workspace, parameterFlags, action)
req, err := buildWorkspaceStartRequest(inv, client, workspace, parameterFlags, debugFlags, action)
if err != nil {
return codersdk.WorkspaceBuild{}, err
}
Expand Down
11 changes: 9 additions & 2 deletions cli/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func (r *RootCmd) stop() *serpent.Command {
var debugFlags buildDebugFlags
client := new(codersdk.Client)
cmd := &serpent.Command{
Annotations: workspaceCommand,
Expand All @@ -35,9 +36,13 @@ func (r *RootCmd) stop() *serpent.Command {
if err != nil {
return err
}
build, err := client.CreateWorkspaceBuild(inv.Context(), workspace.ID, codersdk.CreateWorkspaceBuildRequest{
wbr := codersdk.CreateWorkspaceBuildRequest{
Transition: codersdk.WorkspaceTransitionStop,
})
}
if debugFlags.provisioner {
wbr.LogLevel = codersdk.ProvisionerLogLevelDebug
}
build, err := client.CreateWorkspaceBuild(inv.Context(), workspace.ID, wbr)
if err != nil {
return err
}
Expand All @@ -56,5 +61,7 @@ func (r *RootCmd) stop() *serpent.Command {
return nil
},
}
cmd.Options = append(cmd.Options, debugFlags.cliOptions()...)

return cmd
}
5 changes: 5 additions & 0 deletions cli/testdata/coder_delete_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ USAGE:
Aliases: rm

OPTIONS:
--debug-provisioner bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep this flag hidden? I believe this is only for template admins as it may leak some secrets in the TF verbose mode.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to hide it 👍

Sets the provisioner log level to debug.
This will print additional information about the build process.
This is useful for troubleshooting build issues.

--orphan bool
Delete a workspace without deleting its resources. This can delete a
workspace in a broken state, but may also lead to unaccounted cloud
Expand Down
5 changes: 5 additions & 0 deletions cli/testdata/coder_restart_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ OPTIONS:
--build-options bool
Prompt for one-time build options defined with ephemeral parameters.

--debug-provisioner bool
Sets the provisioner log level to debug.
This will print additional information about the build process.
This is useful for troubleshooting build issues.

--parameter string-array, $CODER_RICH_PARAMETER
Rich parameter value in the format "name=value".

Expand Down
5 changes: 5 additions & 0 deletions cli/testdata/coder_start_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ OPTIONS:
--build-options bool
Prompt for one-time build options defined with ephemeral parameters.

--debug-provisioner bool
Sets the provisioner log level to debug.
This will print additional information about the build process.
This is useful for troubleshooting build issues.

--parameter string-array, $CODER_RICH_PARAMETER
Rich parameter value in the format "name=value".

Expand Down
5 changes: 5 additions & 0 deletions cli/testdata/coder_stop_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ USAGE:
Stop a workspace

OPTIONS:
--debug-provisioner bool
Sets the provisioner log level to debug.
This will print additional information about the build process.
This is useful for troubleshooting build issues.

-y, --yes bool
Bypass prompts.

Expand Down
5 changes: 5 additions & 0 deletions cli/testdata/coder_update_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ OPTIONS:
--build-options bool
Prompt for one-time build options defined with ephemeral parameters.

--debug-provisioner bool
Sets the provisioner log level to debug.
This will print additional information about the build process.
This is useful for troubleshooting build issues.

--parameter string-array, $CODER_RICH_PARAMETER
Rich parameter value in the format "name=value".

Expand Down
9 changes: 6 additions & 3 deletions cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
)

func (r *RootCmd) update() *serpent.Command {
var parameterFlags workspaceParameterFlags

var (
parameterFlags workspaceParameterFlags
debugFlags buildDebugFlags
)
client := new(codersdk.Client)
cmd := &serpent.Command{
Annotations: workspaceCommand,
Expand All @@ -32,7 +34,7 @@ func (r *RootCmd) update() *serpent.Command {
return nil
}

build, err := startWorkspace(inv, client, workspace, parameterFlags, WorkspaceUpdate)
build, err := startWorkspace(inv, client, workspace, parameterFlags, debugFlags, WorkspaceUpdate)
if err != nil {
return xerrors.Errorf("start workspace: %w", err)
}
Expand All @@ -54,5 +56,6 @@ func (r *RootCmd) update() *serpent.Command {
}

cmd.Options = append(cmd.Options, parameterFlags.allOptions()...)
cmd.Options = append(cmd.Options, debugFlags.cliOptions()...)
return cmd
}
8 changes: 8 additions & 0 deletions docs/reference/cli/delete.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions docs/reference/cli/restart.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions docs/reference/cli/start.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions docs/reference/cli/stop.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions docs/reference/cli/update.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/develop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fatal() {
trap 'fatal "Script encountered an error"' ERR

cdroot
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 "$@"
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 "$@"

echo '== Waiting for Coder to become ready'
# Start the timeout in the background so interrupting this script
Expand Down
Loading