diff --git a/cmd/ci/ci.go b/cmd/ci/ci.go index 5b0e4d595e..95443f8ef7 100644 --- a/cmd/ci/ci.go +++ b/cmd/ci/ci.go @@ -215,49 +215,23 @@ func (cmd *CICmd) execute(ctx context.Context, source string) (err error) { return err } - devsyConfig, cfgErr := config.LoadConfig(cmd.Context, cmd.Provider) + devsyConfig, cfgErr := cmd.loadConfig() if cfgErr != nil { return cfgErr } - if devsyConfig.ContextOption(config.ContextOptionSSHStrictHostKeyChecking) == config.BoolTrue { - cmd.StrictHostKeyChecking = true - } - workspaceClient, cleanup, teardown, resolveErr := cmd.resolveWorkspace(ctx, devsyConfig, source) + resolved, resolveErr := cmd.resolveWorkspace(ctx, devsyConfig, source) if resolveErr != nil { return resolveErr } - // cleanup tears down the workspace; join error with the result. + // release tears down the workspace; join error with the result. defer func() { - var cleanupErr error - if teardown { - cleanupErr = status.Run(context.Background(), reporter, status.Operation{ - Phase: status.PhaseDeletingWorkspace, - }, func(context.Context) error { - return cleanup() - }) - } else { - cleanupErr = cleanup() - } - err = errors.Join(err, cleanupErr) + err = errors.Join(err, resolved.release(reporter)) }() + workspaceClient := resolved.client - if _, ok := workspaceClient.(client.WorkspaceClient); !ok { - return fmt.Errorf("ci is currently not supported for proxy providers") - } - - cmd.WaitFor = string(setup.PhasePostCreate) - - if _, err := up.RunHeadless(ctx, workspaceClient, up.HeadlessOptions{ - GlobalFlags: cmd.GlobalFlags, - DevsyConfig: devsyConfig, - CLIOptions: cmd.CLIOptions, - Reporter: reporter, - ProviderOptions: cmd.ProviderOptions, - SecretsFile: cmd.SecretsFile, - FeatureSecretsFile: cmd.FeatureSecretsFile, - }); err != nil { - return fmt.Errorf("start devcontainer: %w", err) + if err := cmd.startWorkspace(ctx, devsyConfig, workspaceClient, reporter); err != nil { + return err } log.Debugf("running command in devcontainer: %s", strings.Join(cmd.RunCmd, " ")) @@ -269,21 +243,53 @@ func (cmd *CICmd) execute(ctx context.Context, source string) (err error) { }) } +func (cmd *CICmd) loadConfig() (*config.Config, error) { + devsyConfig, err := config.LoadConfig(cmd.Context, cmd.Provider) + if err != nil { + return nil, err + } + if devsyConfig.ContextOption(config.ContextOptionSSHStrictHostKeyChecking) == config.BoolTrue { + cmd.StrictHostKeyChecking = true + } + return devsyConfig, nil +} + +type resolvedWorkspace struct { + client client.BaseWorkspaceClient + cleanup func() error + teardown bool +} + +func (r *resolvedWorkspace) release(reporter status.Reporter) error { + if !r.teardown { + return r.cleanup() + } + return status.Run(context.Background(), reporter, status.Operation{ + Phase: status.PhaseDeletingWorkspace, + }, func(context.Context) error { + return r.cleanup() + }) +} + // resolveWorkspace resolves (creating if needed) the workspace to run in. func (cmd *CICmd) resolveWorkspace( ctx context.Context, devsyConfig *config.Config, source string, -) (client.BaseWorkspaceClient, func() error, bool, error) { +) (*resolvedWorkspace, error) { var args []string if source != "" { args = []string{source} } - exists := workspace2.Exists(ctx, devsyConfig, args, "", cmd.Owner) + exists := workspace2.Exists( + ctx, + devsyConfig, + workspace2.LookupOptions{Args: args, Owner: cmd.Owner}, + ) sshConfigPath, cleanupSSH, err := workspacecmd.NewTempSSHConfig() if err != nil { - return nil, nil, false, err + return nil, err } workspaceClient, err := workspace2.Resolve(ctx, devsyConfig, workspace2.ResolveParams{ @@ -300,7 +306,7 @@ func (cmd *CICmd) resolveWorkspace( }) if err != nil { cleanupSSH() - return nil, nil, false, err + return nil, err } teardown := exists == "" && !cmd.Keep @@ -311,7 +317,7 @@ func (cmd *CICmd) resolveWorkspace( } return nil } - return workspaceClient, cleanup, teardown, nil + return &resolvedWorkspace{client: workspaceClient, cleanup: cleanup, teardown: teardown}, nil } // runInContainer runs the command inside the started container. @@ -344,3 +350,31 @@ func (cmd *CICmd) validateEnv() error { } return nil } + +// startWorkspace brings up the workspace for the CI command and waits for the +// post-create phase. +func (cmd *CICmd) startWorkspace( + ctx context.Context, + devsyConfig *config.Config, + workspaceClient client.BaseWorkspaceClient, + reporter status.Reporter, +) error { + if _, ok := workspaceClient.(client.WorkspaceClient); !ok { + return fmt.Errorf("ci is currently not supported for proxy providers") + } + + cmd.WaitFor = string(setup.PhasePostCreate) + + if _, err := up.RunHeadless(ctx, workspaceClient, up.HeadlessOptions{ + GlobalFlags: cmd.GlobalFlags, + DevsyConfig: devsyConfig, + CLIOptions: cmd.CLIOptions, + Reporter: reporter, + ProviderOptions: cmd.ProviderOptions, + SecretsFile: cmd.SecretsFile, + FeatureSecretsFile: cmd.FeatureSecretsFile, + }); err != nil { + return fmt.Errorf("start devcontainer: %w", err) + } + return nil +} diff --git a/cmd/completion/suggestions.go b/cmd/completion/suggestions.go index c98eae0752..801af1995f 100644 --- a/cmd/completion/suggestions.go +++ b/cmd/completion/suggestions.go @@ -17,11 +17,13 @@ func RegisterFlagCompletionFuns(rootCmd *cobra.Command, globalFlags *flags.Globa func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return GetProviderSuggestions( rootCmd, - globalFlags.Context, - globalFlags.Provider, - args, - toComplete, - globalFlags.Owner, + SuggestionRequest{ + Context: globalFlags.Context, + Provider: globalFlags.Provider, + Args: args, + ToComplete: toComplete, + Owner: globalFlags.Owner, + }, ) }, ); err != nil { @@ -33,11 +35,13 @@ func RegisterFlagCompletionFuns(rootCmd *cobra.Command, globalFlags *flags.Globa func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return GetContextSuggestions( rootCmd, - globalFlags.Context, - globalFlags.Provider, - args, - toComplete, - globalFlags.Owner, + SuggestionRequest{ + Context: globalFlags.Context, + Provider: globalFlags.Provider, + Args: args, + ToComplete: toComplete, + Owner: globalFlags.Owner, + }, ) }, ); err != nil { @@ -47,13 +51,20 @@ func RegisterFlagCompletionFuns(rootCmd *cobra.Command, globalFlags *flags.Globa return nil } +// SuggestionRequest carries the inputs of the completion helpers. +type SuggestionRequest struct { + Context string + Provider string + Args []string + ToComplete string + Owner platform.OwnerFilter +} + func GetWorkspaceSuggestions( rootCmd *cobra.Command, - context, provider string, - args []string, - toComplete string, - owner platform.OwnerFilter, + req SuggestionRequest, ) ([]string, cobra.ShellCompDirective) { + context, provider, toComplete, owner := req.Context, req.Provider, req.ToComplete, req.Owner devsyConfig, err := config.LoadConfig(context, provider) if err != nil { return nil, cobra.ShellCompDirectiveError @@ -75,11 +86,9 @@ func GetWorkspaceSuggestions( func GetProviderSuggestions( rootCmd *cobra.Command, - context, provider string, - args []string, - toComplete string, - owner platform.OwnerFilter, + req SuggestionRequest, ) ([]string, cobra.ShellCompDirective) { + context, provider, toComplete := req.Context, req.Provider, req.ToComplete devsyConfig, err := config.LoadConfig(context, provider) if err != nil { return nil, cobra.ShellCompDirectiveError @@ -101,11 +110,9 @@ func GetProviderSuggestions( func GetContextSuggestions( rootCmd *cobra.Command, - context, provider string, - args []string, - toComplete string, - owner platform.OwnerFilter, + req SuggestionRequest, ) ([]string, cobra.ShellCompDirective) { + context, provider, toComplete := req.Context, req.Provider, req.ToComplete devsyConfig, err := config.LoadConfig(context, provider) if err != nil { return nil, cobra.ShellCompDirectiveError diff --git a/cmd/config/apply.go b/cmd/config/apply.go index e9870b34cf..f082505e26 100644 --- a/cmd/config/apply.go +++ b/cmd/config/apply.go @@ -23,6 +23,7 @@ import ( "github.com/devsy-org/devsy/pkg/status" "github.com/devsy-org/devsy/pkg/types" pkgworkspace "github.com/devsy-org/devsy/pkg/workspace" + "github.com/devsy-org/devsy/pkg/workspacelogs" "github.com/spf13/cobra" ) @@ -111,8 +112,30 @@ func (cmd *ApplyCmd) Run(ctx context.Context) error { EnvArgs: envArgs, Workdir: workdir, User: devcconfig.GetRemoteUser(result), + Recorder: workspacelogs.RecorderFrom(ctx), } + if err := cmd.runLifecycle(ctx, reporter, params, result); err != nil { + return err + } + + log.Debugf("apply completed for container %s", containerDetails.ID) + if emitJSON { + return devcconfig.WriteResultJSON(os.Stdout, devcconfig.ResultEnvelope{ + ContainerID: containerDetails.ID, + RemoteUser: devcconfig.GetRemoteUser(result), + RemoteWorkspaceFolder: workdir, + }) + } + return nil +} + +func (cmd *ApplyCmd) runLifecycle( + ctx context.Context, + reporter status.Reporter, + params *workspace.LifecycleExecParams, + result *devcconfig.Result, +) error { if err := status.Run( ctx, reporter, @@ -123,26 +146,14 @@ func (cmd *ApplyCmd) Run(ctx context.Context) error { ); err != nil { return err } - if err := status.Run( + return status.Run( ctx, reporter, status.Operation{Phase: status.PhaseReady}, func(context.Context) error { return nil }, - ); err != nil { - return err - } - - log.Debugf("apply completed for container %s", containerDetails.ID) - if emitJSON { - return devcconfig.WriteResultJSON(os.Stdout, devcconfig.ResultEnvelope{ - ContainerID: containerDetails.ID, - RemoteUser: devcconfig.GetRemoteUser(result), - RemoteWorkspaceFolder: workdir, - }) - } - return nil + ) } func (cmd *ApplyCmd) resolveContainer( @@ -333,8 +344,7 @@ func (cmd *ApplyCmd) installFeatures( if err := cmd.copyAndExecFeatures( ctx, helper, - featureSets, - featureStageDir, + featureStage{featureSets: featureSets, dir: featureStageDir}, emitJSON, ); err != nil { return err @@ -359,13 +369,20 @@ func (cmd *ApplyCmd) resolveFeatureSets( return featureSets, nil } +// featureStage is the feature set to copy into the container and the local +// folder it is staged in. +type featureStage struct { + featureSets []*devcconfig.FeatureSet + dir string +} + func (cmd *ApplyCmd) copyAndExecFeatures( ctx context.Context, helper *docker.DockerHelper, - featureSets []*devcconfig.FeatureSet, - featureStageDir string, + stage featureStage, emitJSON bool, ) error { + featureSets, featureStageDir := stage.featureSets, stage.dir containerFeaturesPath := "/tmp/build-features" streams := docker.Streams{Stdout: os.Stdout, Stderr: os.Stderr} if emitJSON { @@ -466,8 +483,10 @@ func (cmd *ApplyCmd) runApplyLifecycleHooks( } for _, hook := range hooks { - for _, h := range hook.cmds { - if err := workspace.ExecLifecycleHook(params, hook.name, h); err != nil { + for i, h := range hook.cmds { + indexed := *params + indexed.HookIndex = i + if err := workspace.ExecLifecycleHook(&indexed, hook.name, h); err != nil { return fmt.Errorf("lifecycle hooks: %s: %w", hook.name, err) } } diff --git a/cmd/context/list.go b/cmd/context/list.go index 9a27331ace..5ef22deaa0 100644 --- a/cmd/context/list.go +++ b/cmd/context/list.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "os" "sort" "strconv" @@ -83,7 +84,7 @@ func (cmd *ListCmd) Run(ctx context.Context) error { if err != nil { return err } - fmt.Print(string(out)) + _, _ = fmt.Fprint(os.Stdout, string(out)) } return nil diff --git a/cmd/env/bind_test.go b/cmd/env/bind_test.go index 05ce0c17ff..3a0886f594 100644 --- a/cmd/env/bind_test.go +++ b/cmd/env/bind_test.go @@ -48,7 +48,10 @@ func TestAttachAndDetachEnvironmentVariable(t *testing.T) { func TestAttachRejectsSecret(t *testing.T) { globalFlags := setupEnvCommandTest(t) - store, err := secrets.NewStoreForConfig(mustLoadConfig(t)) + //nolint:staticcheck // test needs the full Store interface, not the secret-only view + store, err := secrets.NewStoreForConfig( + mustLoadConfig(t), + ) //nolint:staticcheck // test needs the full Store interface require.NoError(t, err) require.NoError(t, store.Set(config.DefaultContext, "API_TOKEN", "secret", secrets.KindSecret)) err = (&AttachCmd{GlobalFlags: globalFlags}).Run(context.Background(), "API_TOKEN") @@ -76,7 +79,10 @@ func TestDeleteUnbindsEnvironmentVariable(t *testing.T) { func TestSetRejectsConvertingAttachedSecretToEnvironment(t *testing.T) { globalFlags := setupEnvCommandTest(t) cfg := mustLoadConfig(t) - store, err := secrets.NewStoreForConfig(cfg) + //nolint:staticcheck // test needs the full Store interface, not the secret-only view + store, err := secrets.NewStoreForConfig( + cfg, + ) //nolint:staticcheck // test needs the full Store interface require.NoError(t, err) require.NoError(t, store.Set(config.DefaultContext, "TOKEN", "secret", secrets.KindSecret)) cfg.Current().Secrets = []string{"TOKEN"} diff --git a/cmd/feature/outdated_test.go b/cmd/feature/outdated_test.go index 8c08b08d0b..3e239febce 100644 --- a/cmd/feature/outdated_test.go +++ b/cmd/feature/outdated_test.go @@ -7,6 +7,10 @@ import ( "github.com/stretchr/testify/require" ) +const ( + value100 = "1.0.0" +) + func TestParseSemver_BareMajor(t *testing.T) { v, err := parseSemver("2") require.NoError(t, err) @@ -55,7 +59,7 @@ func TestFindLatestVersion_SemverTags(t *testing.T) { } func TestFindLatestVersion_FullSemver(t *testing.T) { - tags := []string{"1.0.0", "1.1.0", "2.0.0", "2.1.0"} + tags := []string{value100, "1.1.0", "2.0.0", "2.1.0"} result := findLatestVersion("1.1.0", tags) assert.Equal(t, "2.1.0", result) } diff --git a/cmd/feature/package_test.go b/cmd/feature/package_test.go index c7253987eb..3dfe355c23 100644 --- a/cmd/feature/package_test.go +++ b/cmd/feature/package_test.go @@ -208,7 +208,7 @@ func TestPackageCmd_IDMustMatchDirectory(t *testing.T) { feat := localFeature{ dir: "actual-dir", - config: &config.FeatureConfig{ID: "different-id", Version: "1.0.0", Name: "Test"}, + config: &config.FeatureConfig{ID: "different-id", Version: value100, Name: "Test"}, } _, err := cmd.packageFeature(feat, targetDir, outputDir) require.Error(t, err) @@ -236,7 +236,7 @@ func TestPackageCmd_MissingName(t *testing.T) { feat := localFeature{ dir: testFeatureIDPkg, - config: &config.FeatureConfig{ID: testFeatureIDPkg, Version: "1.0.0"}, + config: &config.FeatureConfig{ID: testFeatureIDPkg, Version: value100}, } _, err := cmd.packageFeature(feat, targetDir, outputDir) require.Error(t, err) diff --git a/cmd/internal/agent_daemon_test.go b/cmd/internal/agent_daemon_test.go index f8aed77eca..b5ff4ac57a 100644 --- a/cmd/internal/agent_daemon_test.go +++ b/cmd/internal/agent_daemon_test.go @@ -18,6 +18,14 @@ import ( "github.com/stretchr/testify/require" ) +const ( + wsTestValue = "ws-test" +) + +const ( + wsTest = wsTestValue +) + const testEcho = "echo" type recordingDiagnostics struct { @@ -38,7 +46,7 @@ func writeWorkspaceConfig(t *testing.T, dir string, shutdown types.StrArray) str t.Helper() require.NoError(t, os.MkdirAll(dir, 0o750)) - info := &provider2.AgentWorkspaceInfo{Workspace: &provider2.Workspace{ID: "ws-test"}} + info := &provider2.AgentWorkspaceInfo{Workspace: &provider2.Workspace{ID: wsTest}} info.Agent.Exec.Shutdown = shutdown data, err := json.Marshal(info) @@ -56,7 +64,7 @@ func TestGetActivity_ShutdownConfigured(t *testing.T) { require.NoError(t, err) require.NotNil(t, activity) require.NotNil(t, ws) - assert.Equal(t, "ws-test", ws.Workspace.ID) + assert.Equal(t, wsTest, ws.Workspace.ID) stat, err := os.Stat(cfg) require.NoError(t, err) @@ -108,7 +116,7 @@ func TestFindLatestActivity_PicksLatest(t *testing.T) { } func workspaceWithShutdownAction(action string) *provider2.AgentWorkspaceInfo { - ws := &provider2.AgentWorkspaceInfo{Workspace: &provider2.Workspace{ID: "ws-test"}} + ws := &provider2.AgentWorkspaceInfo{Workspace: &provider2.Workspace{ID: wsTest}} if action != "" { ws.LastDevContainerConfig = &config.DevContainerConfigWithPath{ Config: &config.DevContainerConfig{ @@ -135,7 +143,7 @@ func TestEffectiveShutdownAction(t *testing.T) { t.Run("falls back to flag when config action is empty", func(t *testing.T) { cmd := &DaemonCmd{ShutdownAction: config.ShutdownActionNone} ws := &provider2.AgentWorkspaceInfo{ - Workspace: &provider2.Workspace{ID: "ws-test"}, + Workspace: &provider2.Workspace{ID: wsTest}, LastDevContainerConfig: &config.DevContainerConfigWithPath{ Config: &config.DevContainerConfig{}, }, @@ -336,8 +344,8 @@ func TestDiagnosticsRetainsErrorHistoryWithoutInventingStartupPatrol(t *testing. func TestUpdateDiagnosticsRetainsWorkspaceContextAfterShutdownFailure(t *testing.T) { recorder := &recordingDiagnostics{} cmd := &DaemonCmd{recorder: recorder, startedAt: time.Now().UTC()} - workspaces := []machinediagnostics.WorkspaceStatus{{ID: "ws-test"}} - candidate := &machinediagnostics.ShutdownCandidate{WorkspaceID: "ws-test"} + workspaces := []machinediagnostics.WorkspaceStatus{{ID: wsTest}} + candidate := &machinediagnostics.ShutdownCandidate{WorkspaceID: wsTest} cmd.updateDiagnostics(diagnosticsUpdate{ state: machinediagnostics.DaemonRunning, diff --git a/cmd/internal/agentcontainer/browser_async.go b/cmd/internal/agentcontainer/browser_async.go index 50fafe854f..e093ccb6ba 100644 --- a/cmd/internal/agentcontainer/browser_async.go +++ b/cmd/internal/agentcontainer/browser_async.go @@ -57,9 +57,14 @@ var browserIDEs = []browserIDE{ short: "Starts openvscode", defaultPort: openvscode.DefaultVSCodePort, newServer: func(s browserServerSpec) browserServer { - return openvscode.NewOpenVSCodeServer( - s.extensions, s.settings, s.userName, s.host, s.port, s.values, - ) + return openvscode.NewOpenVSCodeServer(openvscode.ServerConfig{ + Extensions: s.extensions, + Settings: s.settings, + UserName: s.userName, + Host: s.host, + Port: s.port, + Values: s.values, + }) }, }, { diff --git a/cmd/internal/agentcontainer/container.go b/cmd/internal/agentcontainer/container.go index 9f9469802f..8ad00f9875 100644 --- a/cmd/internal/agentcontainer/container.go +++ b/cmd/internal/agentcontainer/container.go @@ -21,6 +21,7 @@ func NewContainerCmd(flags *flags.GlobalFlags) *cobra.Command { containerCmd.AddCommand(NewSetupContainerCmd(flags)) containerCmd.AddCommand(NewDeferredHooksCmd(flags)) containerCmd.AddCommand(NewPostAttachCmd(flags)) + containerCmd.AddCommand(NewSetupLogsCmd(flags)) containerCmd.AddCommand(NewDaemonCmd()) containerCmd.AddCommand(NewVSCodeAsyncCmd()) for _, b := range browserIDEs { diff --git a/cmd/internal/agentcontainer/credentials_server.go b/cmd/internal/agentcontainer/credentials_server.go index a01068da0a..a2e24db30f 100644 --- a/cmd/internal/agentcontainer/credentials_server.go +++ b/cmd/internal/agentcontainer/credentials_server.go @@ -122,22 +122,7 @@ func (cmd *CredentialsServerCmd) Run(ctx context.Context, port int) error { } cmd.maybeForwardPorts(runCtx, tunnelClient) - githubState := "disabled" - if cmd.ConfigureGitHubHelper { - result, installErr := ghcredentials.Install(runCtx, port, cmd.User) - githubState = result.Status - if err := installErr; err != nil { - log.Warnf("GitHub CLI wrapper installation unavailable: %v", err) - } - } - status := credentials.Readiness{ - Version: credentials.ReadinessVersion, - Owner: cmd.User, - HostOwner: cmd.HostOwner, - Instance: rand.Text(), - GitHubAllowed: cmd.ConfigureGitHubHelper, - GitHubState: githubState, - } + status := cmd.readiness(runCtx, port) serverDone := make(chan error, 1) go func() { serverDone <- credentials.RunPreparedCredentialsServer(runCtx, ln, tunnelClient, status) }() @@ -161,6 +146,25 @@ func (cmd *CredentialsServerCmd) Run(ctx context.Context, port int) error { return <-serverDone } +func (cmd *CredentialsServerCmd) readiness(ctx context.Context, port int) credentials.Readiness { + githubState := "disabled" + if cmd.ConfigureGitHubHelper { + result, err := ghcredentials.Install(ctx, port, cmd.User) + githubState = result.Status + if err != nil { + log.Warnf("GitHub CLI wrapper installation unavailable: %v", err) + } + } + return credentials.Readiness{ + Version: credentials.ReadinessVersion, + Owner: cmd.User, + HostOwner: cmd.HostOwner, + Instance: rand.Text(), + GitHubAllowed: cmd.ConfigureGitHubHelper, + GitHubState: githubState, + } +} + // errPortOwnedByAnotherSession marks a bind failure as another session // already owning the port, not a real error. var errPortOwnedByAnotherSession = errors.New("credentials server port owned by another session") diff --git a/cmd/internal/agentcontainer/daemon.go b/cmd/internal/agentcontainer/daemon.go index 6f975bd0e6..ae5d6b47a1 100644 --- a/cmd/internal/agentcontainer/daemon.go +++ b/cmd/internal/agentcontainer/daemon.go @@ -235,6 +235,7 @@ func runNetworkServer( ctx context.Context, cmd *DaemonCmd, ) error { + //nolint:gosec // daemon directory must be readable by the container user if err := os.MkdirAll(RootDir, os.ModePerm); err != nil { return err } diff --git a/cmd/internal/agentcontainer/deferred_hooks.go b/cmd/internal/agentcontainer/deferred_hooks.go index 77d8796b47..02bf7c5729 100644 --- a/cmd/internal/agentcontainer/deferred_hooks.go +++ b/cmd/internal/agentcontainer/deferred_hooks.go @@ -101,7 +101,7 @@ func NewDeferredHooksCmd(flags *flags.GlobalFlags) *cobra.Command { } // Run executes the deferred lifecycle hooks. -func (cmd *DeferredHooksCmd) Run(ctx context.Context) error { +func (cmd *DeferredHooksCmd) Run(ctx context.Context) (retErr error) { decompressed, err := compress.Decompress(cmd.SetupInfo) if err != nil { return err @@ -118,6 +118,11 @@ func (cmd *DeferredHooksCmd) Run(ctx context.Context) error { } defer stopActivity() + secretsEnv := secretsEnvFromEnvironment() + session := startWorkerRun(secretsEnv) + ctx = session.Context(ctx) + defer func() { session.Finish(retErr, false) }() + log.Debugf("running deferred lifecycle hooks") deferred, err := setup.RunPreAttachHooks(ctx, setupInfo, setup.PreAttachOptions{ Prebuild: cmd.Prebuild, @@ -126,7 +131,7 @@ func (cmd *DeferredHooksCmd) Run(ctx context.Context) error { InstallScript: cmd.DotfilesScript, RemoteUser: config.GetRemoteUser(setupInfo), }, - SecretsEnv: secretsEnvFromEnvironment(), + SecretsEnv: secretsEnv, SecretsMount: setup.MountSecretsForRedaction(), }) if err != nil { diff --git a/cmd/internal/agentcontainer/post_attach.go b/cmd/internal/agentcontainer/post_attach.go index 4bc5c02484..7fa80123d8 100644 --- a/cmd/internal/agentcontainer/post_attach.go +++ b/cmd/internal/agentcontainer/post_attach.go @@ -45,7 +45,7 @@ func NewPostAttachCmd(flags *flags.GlobalFlags) *cobra.Command { } // Run runs the postAttachCommand lifecycle hooks. -func (cmd *PostAttachCmd) Run(ctx context.Context) error { +func (cmd *PostAttachCmd) Run(ctx context.Context) (retErr error) { decompressed, err := compress.Decompress(cmd.SetupInfo) if err != nil { return err @@ -62,14 +62,17 @@ func (cmd *PostAttachCmd) Run(ctx context.Context) error { } defer stopActivity() + secretsEnv := secretsEnvFromEnvironment() + session := startWorkerRun(secretsEnv) + ctx = session.Context(ctx) + defer func() { session.Finish(retErr, false) }() + log.Debugf("running postAttachCommand hooks") // Mount values aren't in this process's env; read them back for redaction. - if err := setup.RunPostAttachHooks( - ctx, - setupInfo, - secretsEnvFromEnvironment(), - setup.MountSecretsForRedaction(), - ); err != nil { + if err := setup.RunPostAttachHooks(ctx, setupInfo, setup.PostAttachOptions{ + SecretsEnv: secretsEnv, + SecretsMount: setup.MountSecretsForRedaction(), + }); err != nil { if ctx.Err() != nil { return context.Cause(ctx) } diff --git a/cmd/internal/agentcontainer/setup.go b/cmd/internal/agentcontainer/setup.go index 8138e442fd..fc2272ab17 100644 --- a/cmd/internal/agentcontainer/setup.go +++ b/cmd/internal/agentcontainer/setup.go @@ -10,6 +10,7 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strconv" "strings" @@ -74,7 +75,9 @@ type containerState struct { tunnelClient tunnel.TunnelClient reporter status.Reporter secretsEnv []string + secretsMount []string lifecycle *lifecycleHandshake + run *containerRun } // Run runs the command logic. @@ -116,13 +119,7 @@ func (cmd *SetupContainerCmd) Run(ctx context.Context) error { ctx, tunnelClient, func(reportCtx context.Context) (*config.Result, error) { - if err := cmd.prepareWorkspace(reportCtx, state); err != nil { - return nil, err - } - if err := cmd.finalizeSetup(reportCtx, state); err != nil { - return nil, err - } - return state.setupInfo, nil + return cmd.reportSetup(reportCtx, state) }, ) if state.lifecycle != nil { @@ -131,6 +128,53 @@ func (cmd *SetupContainerCmd) Run(ctx context.Context) error { return err } +func (cmd *SetupContainerCmd) reportSetup( + ctx context.Context, + state *containerState, +) (*config.Result, error) { + ctx, err := cmd.beginRun(ctx, state) + if err != nil { + return nil, err + } + hop := state.run.mirror(ctx, state.tunnelClient) + defer hop.Close() + err = cmd.runSetup(ctx, state) + state.run.finish(err) + // Drain before the result is reported and stdio is torn down. + state.run.drain(ctx, hop) + if err != nil { + return nil, err + } + return state.setupInfo, nil +} + +// beginRun fetches the workspace secrets once and starts the run's recorder +// with them so their values are redacted before anything is stored. +func (cmd *SetupContainerCmd) beginRun( + ctx context.Context, + state *containerState, +) (context.Context, error) { + env, mount, err := fetchSecrets(ctx, state.tunnelClient) + if err != nil { + return ctx, err + } + state.secretsEnv, state.secretsMount = env, mount + state.run = startContainerRun(state.workspaceInfo, append(slices.Clone(env), mount...)) + if state.run == nil { + return ctx, nil + } + + return state.run.session.Context(ctx), nil +} + +func (cmd *SetupContainerCmd) runSetup(ctx context.Context, state *containerState) error { + if err := cmd.prepareWorkspace(ctx, state); err != nil { + return err + } + + return cmd.finalizeSetup(ctx, state) +} + func (cmd *SetupContainerCmd) registerFlags(setupContainerCmd *cobra.Command) { cmd.registerBehaviorFlags(setupContainerCmd) cmd.registerWorkspaceInfoFlags(setupContainerCmd) @@ -278,11 +322,7 @@ func fetchSecrets( } func (cmd *SetupContainerCmd) finalizeSetup(ctx context.Context, state *containerState) error { - secretsEnv, secretsMount, err := fetchSecrets(ctx, state.tunnelClient) - if err != nil { - return err - } - state.secretsEnv = secretsEnv + secretsEnv, secretsMount := state.secretsEnv, state.secretsMount cfg := &setup.ContainerSetupConfig{ SetupInfo: state.setupInfo, @@ -327,11 +367,8 @@ func (cmd *SetupContainerCmd) setupPostAttach( return err } - if state.lifecycle == nil { - shutdownAction := state.setupInfo.MergedConfig.ShutdownAction - if err := cmd.startContainerDaemon(state.workspaceInfo, shutdownAction); err != nil { - return err - } + if err := cmd.startDaemonUnlessLeased(state); err != nil { + return err } resolvedSetupInfo, err := compressSetupInfo(state.setupInfo) @@ -339,21 +376,34 @@ func (cmd *SetupContainerCmd) setupPostAttach( return fmt.Errorf("re-serialize setup info: %w", err) } - var workers *workerReadiness - if state.lifecycle != nil { - workers, err = newWorkerReadiness() - if err != nil { - return err - } + workers, err := newLeasedWorkerReadiness(state) + if err != nil { + return err + } + if workers != nil { defer workers.close() } + state.run.reserveWorkers( + !deferred.Empty(), + len(state.setupInfo.MergedConfig.PostAttachCommands) > 0, + ) + cmd.launchWorkers(state, deferred, resolvedSetupInfo, workers) + state.run.sealDiscovery() + + return armLifecycle(state, workers) +} + +// launchWorkers starts the deferred and post-attach hook workers. A launch +// failure is logged; the workers are optional and the workspace stays usable. +func (cmd *SetupContainerCmd) launchWorkers( + state *containerState, + deferred setup.DeferredHooks, + resolvedSetupInfo string, + workers *workerReadiness, +) { if !deferred.Empty() { - err = cmd.startDeferredHooks( - resolvedSetupInfo, cmd.DotfilesRepo, cmd.DotfilesScript, - state.secretsEnv, workers, - ) - if err != nil { + if err := cmd.startDeferredHooks(state, resolvedSetupInfo, workers); err != nil { log.Errorf("failed to start deferred lifecycle hooks: %v", err) } } @@ -361,15 +411,32 @@ func (cmd *SetupContainerCmd) setupPostAttach( if err := cmd.startPostAttachHooks(state, workers); err != nil { log.Errorf("failed to start postAttachCommand: %v", err) } +} - // Arm last, once the spawned workers hold the workspace active themselves. +func (cmd *SetupContainerCmd) startDaemonUnlessLeased(state *containerState) error { if state.lifecycle != nil { - if err := workers.wait(state.lifecycle.ctx); err != nil { - log.Errorf("hook protection not confirmed before arming: %v", err) - } - return state.lifecycle.ready() + return nil } - return nil + shutdownAction := state.setupInfo.MergedConfig.ShutdownAction + return cmd.startContainerDaemon(state.workspaceInfo, shutdownAction) +} + +func newLeasedWorkerReadiness(state *containerState) (*workerReadiness, error) { + if state.lifecycle == nil { + return nil, nil + } + return newWorkerReadiness() +} + +// armLifecycle runs last, once the spawned workers hold the workspace active themselves. +func armLifecycle(state *containerState, workers *workerReadiness) error { + if state.lifecycle == nil { + return nil + } + if err := workers.wait(state.lifecycle.ctx); err != nil { + log.Errorf("hook protection not confirmed before arming: %v", err) + } + return state.lifecycle.ready() } // compressSetupInfo marshals and compresses a config.Result for passing @@ -383,31 +450,44 @@ func compressSetupInfo(setupInfo *config.Result) (string, error) { } func (cmd *SetupContainerCmd) startDeferredHooks( - setupInfo, dotfilesRepo, dotfilesScript string, secretsEnv []string, + state *containerState, + setupInfo string, workers *workerReadiness, ) error { - return workers.launch("deferred-hooks", func(prepare func(*exec.Cmd)) error { + built := false + err := workers.launch("deferred-hooks", func(prepare func(*exec.Cmd)) error { return command.StartBackgroundOnce("devsy.deferred-hooks", func() (*exec.Cmd, error) { log.Debugf("starting deferred lifecycle hooks as background process") - execCmd, err := buildDeferredHooksCmd( - setupInfo, - cmd.Prebuild, - dotfilesRepo, - dotfilesScript, - secretsEnv, - ) + execCmd, err := buildDeferredHooksCmd(deferredHooksArgs{ + setupInfo: setupInfo, + prebuild: cmd.Prebuild, + dotfilesRepo: cmd.DotfilesRepo, + dotfilesScript: cmd.DotfilesScript, + secretsEnv: state.secretsEnv, + }) if err != nil { return nil, err } + execCmd.Env = state.run.workerEnv(execCmd.Env, state.run.deferredChild()) + built = true prepare(execCmd) return execCmd, nil }) }) + state.run.launchOutcome(state.run.deferredChild(), built, err) + + return err } -func buildDeferredHooksCmd( - setupInfo string, prebuild bool, dotfilesRepo, dotfilesScript string, secretsEnv []string, -) (*exec.Cmd, error) { +type deferredHooksArgs struct { + setupInfo string + prebuild bool + dotfilesRepo string + dotfilesScript string + secretsEnv []string +} + +func buildDeferredHooksCmd(a deferredHooksArgs) (*exec.Cmd, error) { binaryPath, err := os.Executable() if err != nil { return nil, err @@ -415,22 +495,22 @@ func buildDeferredHooksCmd( args := []string{ cmdInternal, cmdAgent, cmdContainer, "deferred-hooks", - names.Flag(names.SetupInfo), setupInfo, + names.Flag(names.SetupInfo), a.setupInfo, } - if prebuild { + if a.prebuild { args = append(args, names.Flag(names.Prebuild)) } - if dotfilesRepo != "" { - args = append(args, names.Flag(names.DotfilesRepo), dotfilesRepo) + if a.dotfilesRepo != "" { + args = append(args, names.Flag(names.DotfilesRepo), a.dotfilesRepo) } - if dotfilesScript != "" { - args = append(args, names.Flag(names.DotfilesScript), dotfilesScript) + if a.dotfilesScript != "" { + args = append(args, names.Flag(names.DotfilesScript), a.dotfilesScript) } cmd := &exec.Cmd{ Path: binaryPath, Args: append([]string{binaryPath}, args...), } - cmd.Env = secretsEnvOverride(secretsEnv) + cmd.Env = secretsEnvOverride(a.secretsEnv) return cmd, nil } @@ -586,7 +666,8 @@ func (cmd *SetupContainerCmd) startPostAttachHooks( return nil } - return workers.launch("post-attach", func(prepare func(*exec.Cmd)) error { + built := false + err := workers.launch("post-attach", func(prepare func(*exec.Cmd)) error { return command.StartBackground("devsy.post-attach", func() (*exec.Cmd, error) { log.Debugf("starting postAttachCommand as background process") binaryPath, err := os.Executable() @@ -603,10 +684,15 @@ func (cmd *SetupContainerCmd) startPostAttachHooks( Args: append([]string{binaryPath}, args...), } execCmd.Env = secretsEnvOverride(state.secretsEnv) + execCmd.Env = state.run.workerEnv(execCmd.Env, state.run.postAttachChild()) + built = true prepare(execCmd) return execCmd, nil }) }) + state.run.launchOutcome(state.run.postAttachChild(), built, err) + + return err } func fillContainerEnv(setupInfo *config.Result) error { diff --git a/cmd/internal/agentcontainer/setup_logs.go b/cmd/internal/agentcontainer/setup_logs.go new file mode 100644 index 0000000000..400c399287 --- /dev/null +++ b/cmd/internal/agentcontainer/setup_logs.go @@ -0,0 +1,90 @@ +package agentcontainer + +import ( + "encoding/json" + "errors" + "fmt" + + "github.com/devsy-org/devsy/cmd/flags" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/spf13/cobra" +) + +// resolveSpool is replaced in tests. +var resolveSpool = workspacelogs.ResolveSpool + +// SetupLogsCmd reads one bounded page of the container spool. +type SetupLogsCmd struct { + *flags.GlobalFlags + + Run string + Hook string + Invocation string + After string + Limit int +} + +// NewSetupLogsCmd creates the hidden read-only spool reader. It never creates +// the spool, installs anything or runs a hook. +func NewSetupLogsCmd(globalFlags *flags.GlobalFlags) *cobra.Command { + cmd := &SetupLogsCmd{GlobalFlags: globalFlags} + c := &cobra.Command{ + Use: "setup-logs", + Short: "Print one page of retained setup output from the container spool", + Hidden: true, + Args: cobra.NoArgs, + RunE: func(c *cobra.Command, _ []string) error { + return cmd.run(c) + }, + } + c.Flags().StringVar(&cmd.Run, "run", "", "The run ID") + c.Flags().StringVar(&cmd.Hook, "hook", "", "Only this lifecycle hook") + c.Flags().StringVar(&cmd.Invocation, "invocation", "", "Only this invocation ID") + c.Flags().StringVar(&cmd.After, "after", "", "Cursor of the previous page") + c.Flags().IntVar(&cmd.Limit, "limit", 0, "Most records in the page") + _ = c.MarkFlagRequired("run") + + return c +} + +func (cmd *SetupLogsCmd) run(c *cobra.Command) error { + read := workspacelogs.ReadRequest{ + RunID: cmd.Run, + Hook: cmd.Hook, + InvocationID: cmd.Invocation, + After: cmd.After, + Limit: cmd.Limit, + } + if err := workspacelogs.ValidateReadSelectors(read); err != nil { + return err + } + root, err := resolveSpool() + if errors.Is(err, workspacelogs.ErrNotFound) { + return unavailable(c, workspacelogs.ReasonNoSpool, err) + } + if err != nil { + return err + } + resp, err := workspacelogs.ReadSpoolPage(workspacelogs.SpoolSelector{ + Root: root, + Context: cmd.Context, + WorkspaceUID: cmd.UID, + Read: read, + }) + if errors.Is(err, workspacelogs.ErrNotFound) { + return unavailable(c, workspacelogs.ReasonNoSpool, err) + } + if err != nil { + return err + } + + return json.NewEncoder(c.OutOrStdout()).Encode(resp) +} + +// unavailable prints the typed reason on stderr so the caller can tell absent +// history from a command error. +func unavailable(c *cobra.Command, reason string, cause error) error { + _, _ = fmt.Fprint(c.ErrOrStderr(), workspacelogs.FormatUnavailable(reason)) + + return cause +} diff --git a/cmd/internal/agentcontainer/setup_logs_test.go b/cmd/internal/agentcontainer/setup_logs_test.go new file mode 100644 index 0000000000..cbce520527 --- /dev/null +++ b/cmd/internal/agentcontainer/setup_logs_test.go @@ -0,0 +1,128 @@ +package agentcontainer + +import ( + "bytes" + "encoding/json" + "path/filepath" + "testing" + + "github.com/devsy-org/devsy/cmd/flags" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + logsContext = "default" + logsUID = "uid-1" + flagContext = "--context" + flagRun = "--run" +) + +func runSetupLogs(t *testing.T, args ...string) (stdout, stderr string, err error) { + t.Helper() + global := &flags.GlobalFlags{} + cmd := NewSetupLogsCmd(global) + // Global flags are persistent on the real root command. + cmd.Flags().StringVar(&global.Context, "context", "", "") + cmd.Flags().StringVar(&global.UID, "uid", "", "") + cmd.SilenceUsage = true + out, errOut := &bytes.Buffer{}, &bytes.Buffer{} + cmd.SetOut(out) + cmd.SetErr(errOut) + cmd.SetArgs(args) + err = cmd.Execute() + + return out.String(), errOut.String(), err +} + +func seedSpool(t *testing.T) (root, runID string) { + t.Helper() + root = filepath.Join(t.TempDir(), workspacelogs.SpoolDirName) + store, err := workspacelogs.Open(workspacelogs.Options{Root: root}) + require.NoError(t, err) + m, err := store.CreateRun(workspacelogs.RunSpec{ + Context: logsContext, WorkspaceUID: logsUID, Command: "up", + }) + require.NoError(t, err) + rec, err := workspacelogs.NewRecorder(workspacelogs.RecorderOptions{ + Backend: store, RunID: m.RunID, Location: workspacelogs.LocationContainer, + }) + require.NoError(t, err) + inv := workspacelogs.NewID() + rec.Invocation(workspacelogs.Invocation{ + ID: inv, Phase: "setup", Hook: "postCreateCommand", State: workspacelogs.InvocationRunning, + }) + w := rec.Writer(inv, workspacelogs.StreamStdout) + _, _ = w.Write([]byte("hi\n")) + _ = w.Close() + rec.Close(workspacelogs.ProducerTerminal, true) + + return root, m.RunID +} + +func withSpool(t *testing.T, root string, err error) { + t.Helper() + orig := resolveSpool + resolveSpool = func() (string, error) { return root, err } + t.Cleanup(func() { resolveSpool = orig }) +} + +func TestSetupLogsPrintsOnePageAsJSON(t *testing.T) { + root, runID := seedSpool(t) + withSpool(t, root, nil) + + stdout, _, err := runSetupLogs(t, flagRun, runID, flagContext, logsContext, "--uid", logsUID) + + require.NoError(t, err) + var resp workspacelogs.ReadResponse + require.NoError(t, json.Unmarshal([]byte(stdout), &resp)) + assert.Equal(t, runID, resp.Run.RunID) + assert.NotEmpty(t, resp.Records) +} + +func TestSetupLogsNoSpoolIsTypedUnavailable(t *testing.T) { + withSpool(t, "", workspacelogs.ErrNotFound) + + stdout, stderr, err := runSetupLogs(t, flagRun, workspacelogs.NewID(), flagContext, logsContext) + + require.Error(t, err) + assert.Empty(t, stdout) + reason, ok := workspacelogs.ParseUnavailable(stderr) + require.True(t, ok) + assert.Equal(t, workspacelogs.ReasonNoSpool, reason) +} + +func TestSetupLogsRejectsWrongIdentity(t *testing.T) { + root, runID := seedSpool(t) + withSpool(t, root, nil) + + for name, args := range map[string][]string{ + "uid": {flagRun, runID, flagContext, logsContext, "--uid", "uid-2"}, + "no uid": {flagRun, runID, flagContext, logsContext}, + "context": {flagRun, runID, flagContext, "elsewhere", "--uid", logsUID}, + } { + stdout, _, err := runSetupLogs(t, args...) + require.Error(t, err, name) + assert.Empty(t, stdout, name) + } +} + +func TestSetupLogsValidatesSelectorsBeforeTouchingTheSpool(t *testing.T) { + called := false + orig := resolveSpool + resolveSpool = func() (string, error) { called = true; return "", nil } + t.Cleanup(func() { resolveSpool = orig }) + + for _, args := range [][]string{ + {flagRun, "../escape"}, + {flagRun, workspacelogs.NewID(), "--hook", "nope"}, + {flagRun, workspacelogs.NewID(), "--invocation", "x"}, + {flagRun, workspacelogs.NewID(), "--after", "a b"}, + {flagRun, workspacelogs.NewID(), "--limit", "99999"}, + } { + _, _, err := runSetupLogs(t, args...) + require.Error(t, err) + } + assert.False(t, called) +} diff --git a/cmd/internal/agentcontainer/setup_run.go b/cmd/internal/agentcontainer/setup_run.go new file mode 100644 index 0000000000..bef7a79ddb --- /dev/null +++ b/cmd/internal/agentcontainer/setup_run.go @@ -0,0 +1,246 @@ +//go:build !windows + +package agentcontainer + +import ( + "context" + "encoding/json" + "os" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "github.com/devsy-org/devsy/pkg/agent/tunnelserver" + pkgconfig "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/devcontainer/setup" + "github.com/devsy-org/devsy/pkg/log" + provider2 "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/secrets" + "github.com/devsy-org/devsy/pkg/workspacelogs" +) + +// setupRunRef is the non-secret reference a background worker uses to join the +// container's run. The producer ID was reserved by the launching process. +type setupRunRef struct { + RunID string `json:"runId"` + ProducerID string `json:"producerId"` + ParentProducerID string `json:"parentProducerId"` +} + +// containerRun is the container setup process's view of the run: its own +// session and the producers it reserved for the workers it may launch. +type containerRun struct { + session *workspacelogs.Session + deferredID string + postAttachID string +} + +// startContainerRun joins the run named by the CLI options in the container's +// spool. It returns nil when no run is requested or history cannot be kept. +func startContainerRun( + info *provider2.ContainerWorkspaceInfo, + secretEntries []string, +) *containerRun { + ref := info.SetupRun + if ref == nil || !workspacelogs.ValidID(ref.RunID) { + return nil + } + root := setup.WorkspaceLogsRoot() + if root == "" { + return nil + } + store, err := workspacelogs.Open(workspacelogs.Options{Root: root}) + if err != nil { + log.Debugf("setup history unavailable: %v", err) + return nil + } + session := workspacelogs.StartSessionIn(store, workspacelogs.SessionOptions{ + RunID: ref.RunID, + ParentProducerID: validParent(ref.ParentProducerID), + Location: workspacelogs.LocationContainer, + Redactor: runRedactor(secretEntries), + Run: workspacelogs.RunSpec{ + Context: ref.Context, + WorkspaceUID: ref.WorkspaceUID, + WorkspaceIDAtStart: ref.WorkspaceID, + Command: "up", + }, + }) + if session == nil { + return nil + } + + return &containerRun{session: session} +} + +func validParent(id string) string { + if workspacelogs.ValidID(id) { + return id + } + + return "" +} + +func runRedactor(secretEntries []string) *secrets.Redactor { + return secrets.Combine( + secrets.NewEnvironmentRedactor(os.Environ()), + secrets.NewRedactor(secretEntries), + ) +} + +// producerID returns this process's producer ID, or empty without history. +func (r *containerRun) producerID() string { + if r == nil { + return "" + } + + return r.session.ProducerID() +} + +// reserveWorkers reserves a producer for each worker that will be launched, so +// the run knows about it before it starts. +func (r *containerRun) reserveWorkers(deferred, postAttach bool) { + if r == nil { + return + } + if deferred { + r.deferredID = r.session.Recorder.ReserveChild(workspacelogs.LocationContainer) + } + if postAttach { + r.postAttachID = r.session.Recorder.ReserveChild(workspacelogs.LocationContainer) + } +} + +func (r *containerRun) deferredChild() string { + if r == nil { + return "" + } + + return r.deferredID +} + +func (r *containerRun) postAttachChild() string { + if r == nil { + return "" + } + + return r.postAttachID +} + +// sealDiscovery closes producer discovery once every worker is reserved or +// excluded. +func (r *containerRun) sealDiscovery() { + if r != nil { + r.session.Recorder.SealDiscovery() + } +} + +// launchOutcome records what is known about a reserved worker after its launch +// call. A nil error does not prove this run's worker started, so a launch that +// built no command is recorded as unavailable instead of an empty success. +func (r *containerRun) launchOutcome(childID string, built bool, err error) { + if r == nil || childID == "" { + return + } + switch { + case err != nil: + r.session.Recorder.MarkChild(childID, workspacelogs.ProducerUnavailable, "launch_failed") + case !built: + r.session.Recorder.MarkChild(childID, workspacelogs.ProducerUnavailable, "already_running") + } +} + +// workerEnv appends the run reference for a worker to env (the current +// environment when env is nil). +func (r *containerRun) workerEnv(env []string, childID string) []string { + if r == nil || childID == "" { + return env + } + ref, err := json.Marshal(setupRunRef{ + RunID: r.session.RunID, ProducerID: childID, ParentProducerID: r.producerID(), + }) + if err != nil { + return env + } + if env == nil { + env = os.Environ() + } + + return append(env, pkgconfig.EnvSetupRun+"="+string(ref)) +} + +// mirror starts sending this process's records to the provider host. +func (r *containerRun) mirror( + ctx context.Context, + client tunnel.TunnelClient, +) *tunnelserver.SetupLogHop { + if r == nil { + return nil + } + + return tunnelserver.NewSetupLogHop(ctx, tunnelserver.HopOptions{ + Session: r.session, Upstream: client, + }) +} + +// drain sends what is left within the sender's own cleanup budget. An +// incomplete drain marks capture partial and never fails the setup. +func (r *containerRun) drain(ctx context.Context, hop *tunnelserver.SetupLogHop) { + if r == nil || hop.Flush(ctx) { + return + } + _ = r.session.Store.AddCondition(r.session.RunID, workspacelogs.ConditionInterrupted) +} + +// finish records the container process's outcome. +func (r *containerRun) finish(err error) { + if r != nil { + r.session.Finish(err, false) + } +} + +// startWorkerRun joins the run a background worker was launched for. It reads +// and clears the reference so hooks do not inherit it. It returns nil when the +// worker was launched without history. +func startWorkerRun(secretEntries []string) *workspacelogs.Session { + raw := os.Getenv(pkgconfig.EnvSetupRun) + _ = os.Unsetenv(pkgconfig.EnvSetupRun) + if raw == "" { + return nil + } + var ref setupRunRef + if json.Unmarshal([]byte(raw), &ref) != nil || !workspacelogs.ValidID(ref.RunID) || + !workspacelogs.ValidID(ref.ProducerID) { + return nil + } + root := setup.WorkspaceLogsRoot() + if root == "" { + return nil + } + store, err := workspacelogs.Open(workspacelogs.Options{Root: root}) + if err != nil { + return nil + } + m, err := store.Manifest(ref.RunID) + if err != nil { + return nil + } + + return workspacelogs.StartSessionIn(store, workspacelogs.SessionOptions{ + RunID: ref.RunID, + ProducerID: ref.ProducerID, + Resumed: true, + ParentProducerID: validParent(ref.ParentProducerID), + Location: workspacelogs.LocationContainer, + Redactor: runRedactor(secretEntries), + Run: workspacelogs.RunSpec{ + Context: m.Context, Command: m.Command, WorkspaceUID: workspaceUIDOf(m), + }, + }) +} + +func workspaceUIDOf(m *workspacelogs.RunManifest) string { + if m.WorkspaceUID == nil { + return "" + } + + return *m.WorkspaceUID +} diff --git a/cmd/internal/agentworkspace/build.go b/cmd/internal/agentworkspace/build.go index eb18e4c575..15ea520fb5 100644 --- a/cmd/internal/agentworkspace/build.go +++ b/cmd/internal/agentworkspace/build.go @@ -14,6 +14,7 @@ import ( "github.com/devsy-org/devsy/pkg/flags/names" "github.com/devsy-org/devsy/pkg/log" provider2 "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/workspacelogs" "github.com/spf13/cobra" ) @@ -85,11 +86,23 @@ func (cmd *BuildCmd) Run(ctx context.Context) error { return err } + run := startProviderRun(workspaceInfo, nil) + hop := tunnelserver.NewSetupLogHop(cancelCtx, tunnelserver.HopOptions{ + Session: run, Upstream: tunnelClient, + }) + defer hop.Close() _, err = tunnelserver.ReportResult( cancelCtx, tunnelClient, func(ctx context.Context) (*config2.Result, error) { - return nil, buildAndPushImages(ctx, runner, workspaceInfo) + buildErr := buildAndPushImages(run.Context(ctx), runner, workspaceInfo) + run.Finish(buildErr, false) + // Drain before the result is reported and the temporary workspace + // is removed; an incomplete drain only marks capture partial. + if !hop.Flush(ctx) && run != nil { + _ = run.Store.AddCondition(run.RunID, workspacelogs.ConditionInterrupted) + } + return nil, buildErr }, ) return err diff --git a/cmd/internal/agentworkspace/install_dotfiles.go b/cmd/internal/agentworkspace/install_dotfiles.go index eeb0b508b8..eeaf91e6bf 100644 --- a/cmd/internal/agentworkspace/install_dotfiles.go +++ b/cmd/internal/agentworkspace/install_dotfiles.go @@ -62,6 +62,7 @@ func NewInstallDotfilesCmd(flags *flags.GlobalFlags) *cobra.Command { func (cmd *InstallDotfilesCmd) Run(ctx context.Context) error { targetDir := filepath.Join(os.Getenv("HOME"), "dotfiles") + //nolint:gosec // path is the user dotfiles location _, err := os.Stat(targetDir) if err != nil { log.Infof("Cloning dotfiles %s", cmd.Repository) diff --git a/cmd/internal/agentworkspace/logs.go b/cmd/internal/agentworkspace/logs.go index 2726a3b687..1310bb5bd3 100644 --- a/cmd/internal/agentworkspace/logs.go +++ b/cmd/internal/agentworkspace/logs.go @@ -2,23 +2,48 @@ package agentworkspace import ( "context" + "errors" "fmt" + "io" "os" + "strconv" + "strings" + "al.essio.dev/pkg/shellescape" "github.com/devsy-org/devsy/cmd/flags" "github.com/devsy-org/devsy/pkg/agent" "github.com/devsy-org/devsy/pkg/config" "github.com/devsy-org/devsy/pkg/devcontainer" + "github.com/devsy-org/devsy/pkg/driver" cliflags "github.com/devsy-org/devsy/pkg/flags" "github.com/devsy-org/devsy/pkg/flags/names" + provider2 "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/workspacelogs" "github.com/spf13/cobra" ) +const ( + sourceContainer = "container" + sourceSetup = "setup" + + // containerRootUser owns the container spool. + containerRootUser = "root" + // maxRemoteStderr bounds the diagnostics kept from the container reader. + maxRemoteStderr = 4 * 1024 +) + // LogsCmd holds the cmd flags. type LogsCmd struct { *flags.GlobalFlags ID string + + Source string + RunID string + Hook string + Invocation string + After string + Limit int } // NewLogsCmd creates a new command. @@ -36,11 +61,21 @@ func NewLogsCmd(flags *flags.GlobalFlags) *cobra.Command { } cliflags.Add(c, cliflags.String(&cmd.ID, names.ID, "", "The workspace id")) _ = c.MarkFlagRequired(names.ID) + c.Flags().StringVar(&cmd.Source, names.Source, sourceContainer, "container or setup") + c.Flags().StringVar(&cmd.RunID, "run", "", "The run ID (setup source)") + c.Flags().StringVar(&cmd.Hook, "hook", "", "Only this lifecycle hook (setup source)") + c.Flags().StringVar(&cmd.Invocation, "invocation", "", "Only this invocation ID (setup source)") + c.Flags().StringVar(&cmd.After, "after", "", "Cursor of the previous page (setup source)") + c.Flags().IntVar(&cmd.Limit, "limit", 0, "Most records in the page (setup source)") return c } func (cmd *LogsCmd) Run(ctx context.Context) error { + if cmd.Source == sourceSetup { + return cmd.runSetup(ctx) + } + shouldExit, workspaceInfo, err := agent.ReadAgentWorkspaceInfo( cmd.AgentDir, cmd.Context, @@ -67,3 +102,123 @@ func (cmd *LogsCmd) Run(ctx context.Context) error { return runner.Logs(ctx, os.Stdout) } + +// runSetup relays one bounded page from the container spool. It only reads +// existing state: no agent info is provisioned, no elevation is requested and +// the runtime is asked to exec only, never to start anything. +func (cmd *LogsCmd) runSetup(ctx context.Context) error { + read := workspacelogs.ReadRequest{ + RunID: cmd.RunID, Hook: cmd.Hook, InvocationID: cmd.Invocation, + After: cmd.After, Limit: cmd.Limit, + } + if err := workspacelogs.ValidateReadSelectors(read); err != nil { + return err + } + info, err := agent.ReadExistingAgentWorkspaceInfo(cmd.AgentDir, cmd.Context, cmd.ID) + if err != nil { + return unavailable(os.Stderr, workspacelogs.ReasonRefreshFailed, err) + } + if cmd.UID != "" && cmd.UID != info.Workspace.UID { + return fmt.Errorf("workspace %q does not match the requested workspace UID", cmd.ID) + } + runner, err := newReaderRunner(ctx, info) + if err != nil { + return unavailable(os.Stderr, workspacelogs.ReasonRefreshFailed, err) + } + + relay := &setupRelay{ + runner: runner, info: info, read: read, stdout: os.Stdout, stderr: os.Stderr, + } + + return relay.run(ctx) +} + +// newReaderRunner builds a runner from a copy of the workspace info with +// auto-start disabled. The copy is never persisted. +func newReaderRunner( + ctx context.Context, + info *provider2.AgentWorkspaceInfo, +) (devcontainer.Runner, error) { + reader := *info + reader.CLIOptions.NoAutoStart = true + + return devcontainer.NewRunner( + ctx, + config.ContainerDevsyHelperLocation, + config.DefaultAgentDownloadURL(), + &reader, + ) +} + +// setupRelay runs the container reader for one page and forwards its stdout. +type setupRelay struct { + runner devcontainer.Runner + info *provider2.AgentWorkspaceInfo + read workspacelogs.ReadRequest + stdout io.Writer + stderr io.Writer +} + +func (r *setupRelay) run(ctx context.Context) error { + remoteErr := &workspacelogs.BoundedBuffer{Max: maxRemoteStderr} + err := r.runner.Command(ctx, devcontainer.CommandParams{ + User: containerRootUser, + Command: setupReaderCommand(r.info, r.read), + Stdout: r.stdout, + Stderr: remoteErr, + RawStdout: true, + RequireRunning: true, + }) + if err == nil { + return nil + } + reason, ok := workspacelogs.ParseUnavailable(remoteErr.String()) + if !ok { + reason = classifyReaderError(err) + err = fmt.Errorf("%w: %s", err, remoteErr.String()) + } + + return unavailable(r.stderr, reason, err) +} + +func classifyReaderError(err error) string { + switch { + case errors.Is(err, devcontainer.ErrRequireRunningUnsupported): + return workspacelogs.ReasonRefreshUnsupported + case errors.Is(err, driver.ErrNotRunning): + return workspacelogs.ReasonSourceOffline + default: + return workspacelogs.ReasonRefreshFailed + } +} + +// setupReaderCommand builds the container command. Every value is validated +// and quoted; nothing the caller supplied is a path. +func setupReaderCommand(info *provider2.AgentWorkspaceInfo, read workspacelogs.ReadRequest) string { + args := []string{ + shellescape.Quote(info.Agent.ContainerInstallPath()), + "internal", "agent", "container", "setup-logs", + "--context", shellescape.Quote(info.Workspace.Context), + "--uid", shellescape.Quote(info.Workspace.UID), + "--run", shellescape.Quote(read.RunID), + } + for _, opt := range []struct{ flag, value string }{ + {"--hook", read.Hook}, {"--invocation", read.InvocationID}, {"--after", read.After}, + } { + if opt.value != "" { + args = append(args, opt.flag, shellescape.Quote(opt.value)) + } + } + if read.Limit > 0 { + args = append(args, "--limit", strconv.Itoa(read.Limit)) + } + + return strings.Join(args, " ") +} + +// unavailable prints the typed reason for the caller and returns cause. +func unavailable(w io.Writer, reason string, cause error) error { + _, _ = fmt.Fprint(w, workspacelogs.FormatUnavailable(reason)) + + return cause +} diff --git a/cmd/internal/agentworkspace/logs_setup_test.go b/cmd/internal/agentworkspace/logs_setup_test.go new file mode 100644 index 0000000000..be2c3c3209 --- /dev/null +++ b/cmd/internal/agentworkspace/logs_setup_test.go @@ -0,0 +1,164 @@ +package agentworkspace + +import ( + "bytes" + "context" + "errors" + "strings" + "testing" + + "github.com/devsy-org/devsy/cmd/flags" + "github.com/devsy-org/devsy/pkg/devcontainer" + "github.com/devsy-org/devsy/pkg/driver" + provider2 "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type fakeRunner struct { + devcontainer.Runner + + got devcontainer.CommandParams + stdout string + stderr string + err error + commands int +} + +func (f *fakeRunner) Command(_ context.Context, p devcontainer.CommandParams) error { + f.commands++ + f.got = p + _, _ = p.Stdout.Write([]byte(f.stdout)) + _, _ = p.Stderr.Write([]byte(f.stderr)) + + return f.err +} + +const testContextName = "default" + +func testInfo() *provider2.AgentWorkspaceInfo { + return &provider2.AgentWorkspaceInfo{ + Workspace: &provider2.Workspace{ID: "ws", UID: "uid-1", Context: testContextName}, + } +} + +func newRelay(f *fakeRunner) (*setupRelay, *bytes.Buffer, *bytes.Buffer) { + out, errOut := &bytes.Buffer{}, &bytes.Buffer{} + + return &setupRelay{ + runner: f, info: testInfo(), read: workspacelogs.ReadRequest{RunID: workspacelogs.NewID()}, + stdout: out, stderr: errOut, + }, out, errOut +} + +func TestSetupRelayIsExecOnlyAndRaw(t *testing.T) { + f := &fakeRunner{stdout: `{"schemaVersion":1}`} + relay, out, errOut := newRelay(f) + + require.NoError(t, relay.run(context.Background())) + + assert.True(t, f.got.RequireRunning) + assert.True(t, f.got.RawStdout) + assert.Equal(t, containerRootUser, f.got.User) + assert.Equal(t, `{"schemaVersion":1}`, out.String()) + assert.Empty(t, errOut.String()) + assert.Contains(t, f.got.Command, "/usr/local/bin/devsy internal agent container setup-logs") +} + +func TestSetupRelayReportsTypedReasons(t *testing.T) { + tests := []struct { + name string + runner *fakeRunner + reason string + }{ + { + name: "unsupported runtime", + runner: &fakeRunner{err: devcontainer.ErrRequireRunningUnsupported}, + reason: workspacelogs.ReasonRefreshUnsupported, + }, + { + name: "not running", + runner: &fakeRunner{err: driver.ErrNotRunning}, + reason: workspacelogs.ReasonSourceOffline, + }, + { + name: "remote marker wins", + runner: &fakeRunner{ + err: errors.New("exit 1"), + stderr: workspacelogs.FormatUnavailable(workspacelogs.ReasonNoSpool), + }, + reason: workspacelogs.ReasonNoSpool, + }, + { + name: "other failure", + runner: &fakeRunner{err: errors.New("boom")}, + reason: workspacelogs.ReasonRefreshFailed, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + relay, _, errOut := newRelay(tt.runner) + + err := relay.run(context.Background()) + + require.Error(t, err) + reason, ok := workspacelogs.ParseUnavailable(errOut.String()) + require.True(t, ok) + assert.Equal(t, tt.reason, reason) + assert.Equal(t, 1, tt.runner.commands, "no retry and no fallback command") + }) + } +} + +func TestSetupReaderCommandQuotesEveryValue(t *testing.T) { + info := testInfo() + info.Workspace.Context = "a b; rm -rf /" + info.Workspace.UID = "$(touch /tmp/x)" + info.Agent.Driver = provider2.KubernetesDriver + info.Agent.Kubernetes.AgentInstallPath = "/home/dev bin/devsy" + read := workspacelogs.ReadRequest{ + RunID: workspacelogs.NewID(), Hook: "postCreateCommand", + InvocationID: workspacelogs.NewID(), After: "abc_-", Limit: 25, + } + + cmd := setupReaderCommand(info, read) + + assert.True( + t, + strings.HasPrefix(cmd, "'/home/dev bin/devsy' internal agent container setup-logs"), + ) + assert.Contains(t, cmd, "--context 'a b; rm -rf /'") + assert.Contains(t, cmd, "--uid '$(touch /tmp/x)'") + assert.Contains(t, cmd, "--hook postCreateCommand") + assert.Contains(t, cmd, "--after abc_-") + assert.Contains(t, cmd, "--limit 25") +} + +func TestSetupSourceRejectsBadSelectorsBeforeAnyRead(t *testing.T) { + for name, cmd := range map[string]*LogsCmd{ + "run": {Source: sourceSetup, RunID: "../x"}, + "hook": {Source: sourceSetup, RunID: workspacelogs.NewID(), Hook: "nope"}, + "cursor": {Source: sourceSetup, RunID: workspacelogs.NewID(), After: "a;b"}, + "limit": {Source: sourceSetup, RunID: workspacelogs.NewID(), Limit: 99999}, + } { + t.Run(name, func(t *testing.T) { + cmd.GlobalFlags = &flags.GlobalFlags{AgentDir: t.TempDir(), Context: testContextName} + cmd.ID = "ws" + + require.Error(t, cmd.Run(context.Background())) + }) + } +} + +func TestSetupSourceMissingWorkspaceInfoIsUnavailableNotProvisioned(t *testing.T) { + dir := t.TempDir() + cmd := &LogsCmd{ + GlobalFlags: &flags.GlobalFlags{AgentDir: dir, Context: testContextName}, + ID: "ws", Source: sourceSetup, RunID: workspacelogs.NewID(), + } + + require.Error(t, cmd.Run(context.Background())) + // Nothing may be created for a history read. + assert.NoDirExists(t, dir+"/contexts") +} diff --git a/cmd/internal/agentworkspace/setup_run.go b/cmd/internal/agentworkspace/setup_run.go new file mode 100644 index 0000000000..1b6fa74e37 --- /dev/null +++ b/cmd/internal/agentworkspace/setup_run.go @@ -0,0 +1,45 @@ +package agentworkspace + +import ( + "os" + "slices" + + "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/secrets" + "github.com/devsy-org/devsy/pkg/workspacelogs" +) + +// startProviderRun joins the run the CLI options name, in the provider host's +// own store, so initialization and image builds that run here are retained. +// The run reference is validated against the resolved workspace. It returns +// nil when no run is requested or history cannot be kept. +func startProviderRun( + info *provider.AgentWorkspaceInfo, + secretEntries []string, +) *workspacelogs.Session { + opts := info.CLIOptions + ws := info.Workspace + if !workspacelogs.ValidID(opts.SetupRunID) || ws == nil { + return nil + } + parent := "" + if workspacelogs.ValidID(opts.SetupParentProducerID) { + parent = opts.SetupParentProducerID + } + + return workspacelogs.StartSession(workspacelogs.SessionOptions{ + RunID: opts.SetupRunID, + ParentProducerID: parent, + Location: workspacelogs.LocationProviderHost, + Redactor: secrets.Combine( + secrets.NewEnvironmentRedactor(os.Environ()), + secrets.NewRedactor(slices.Clone(secretEntries)), + ), + Run: workspacelogs.RunSpec{ + Context: ws.Context, + WorkspaceUID: ws.UID, + WorkspaceIDAtStart: ws.ID, + Command: "up", + }, + }) +} diff --git a/cmd/internal/agentworkspace/status.go b/cmd/internal/agentworkspace/status.go index 99584b458f..5c8c569b50 100644 --- a/cmd/internal/agentworkspace/status.go +++ b/cmd/internal/agentworkspace/status.go @@ -3,6 +3,7 @@ package agentworkspace import ( "context" "fmt" + "os" "github.com/devsy-org/devsy/cmd/flags" "github.com/devsy-org/devsy/pkg/agent" @@ -61,20 +62,20 @@ func (cmd *StatusCmd) Run(ctx context.Context) error { if err != nil { return err } else if containerDetails == nil { - fmt.Print(client.StatusNotFound) + _, _ = fmt.Fprint(os.Stdout, client.StatusNotFound) return nil } // is running? switch containerDetails.State.Status { case config2.ContainerStatusRunning: - fmt.Print(client.StatusRunning) + _, _ = fmt.Fprint(os.Stdout, client.StatusRunning) return nil case config2.ContainerStatusExited: - fmt.Print(client.StatusStopped) + _, _ = fmt.Fprint(os.Stdout, client.StatusStopped) return nil } - fmt.Print(client.StatusBusy) + _, _ = fmt.Fprint(os.Stdout, client.StatusBusy) return nil } diff --git a/cmd/internal/agentworkspace/up.go b/cmd/internal/agentworkspace/up.go index 6b7b8a207f..bcd740b7df 100644 --- a/cmd/internal/agentworkspace/up.go +++ b/cmd/internal/agentworkspace/up.go @@ -9,6 +9,7 @@ import ( "os/exec" "path/filepath" "runtime" + "slices" "strconv" "github.com/devsy-org/devsy/cmd/flags" @@ -33,9 +34,14 @@ import ( "github.com/devsy-org/devsy/pkg/provider" "github.com/devsy-org/devsy/pkg/status" "github.com/devsy-org/devsy/pkg/util" + "github.com/devsy-org/devsy/pkg/workspacelogs" "github.com/spf13/cobra" ) +const ( + docker = "docker" +) + // UpCmd holds the up cmd flags. type UpCmd struct { *flags.GlobalFlags @@ -208,16 +214,31 @@ func (cmd *UpCmd) devsyUp( workspaceInfo.CLIOptions.SecretsEnv = nil workspaceInfo.CLIOptions.SecretsMount = nil + run := startProviderRun(workspaceInfo, append(slices.Clone(secretsEnv), secretsMount...)) + cliOptions.SetupParentProducerID = run.ProducerID() + hop := tunnelserver.NewSetupLogHop(ctx, tunnelserver.HopOptions{ + Session: run, Upstream: tunnelClient, Receive: true, + }) + defer hop.Close() + ctx = tunnelserver.WithSetupLogOptions(run.Context(ctx), hop.Options()) + runner, err := CreateRunner(ctx, workspaceInfo) if err != nil { + run.Finish(err, false) return nil, err } reporter := tunnelserver.NewTunnelStatusReporter(ctx, tunnelClient) - return runner.Up(ctx, devcontainer.UpOptions{ + result, err := runner.Up(ctx, devcontainer.UpOptions{ CLIOptions: cliOptions, RegistryCache: workspaceInfo.RegistryCache, }, workspaceInfo.InjectTimeout, reporter) + run.Finish(err, false) + if !hop.Flush(ctx) { + _ = run.Store.AddCondition(run.RunID, workspacelogs.ConditionInterrupted) + } + + return result, err } func splitSecrets(secrets []*tunnel.Secret) (env, mount []string) { @@ -492,7 +513,7 @@ func (w *workspaceInitializer) ensureDockerInstalled(ctx context.Context) (strin return "", nil } - if dockerCmd != "docker" { + if dockerCmd != docker { path, err := exec.LookPath(dockerCmd) if err != nil { return "", fmt.Errorf("custom docker path %q not found: %w", dockerCmd, err) @@ -500,7 +521,7 @@ func (w *workspaceInitializer) ensureDockerInstalled(ctx context.Context) (strin return path, nil } - if dockerCmd == "docker" && runtime.GOOS == "darwin" { + if dockerCmd == docker && runtime.GOOS == "darwin" { return findDarwinDockerCLI() } @@ -531,7 +552,7 @@ func findDarwinDockerCLI() (string, error) { func darwinDockerCandidatePaths(homeDir string) []string { paths := append([]string(nil), darwinDockerPaths...) if homeDir != "" { - paths = append(paths, filepath.Join(homeDir, ".rd", "bin", "docker")) + paths = append(paths, filepath.Join(homeDir, ".rd", "bin", docker)) } return paths } @@ -554,7 +575,7 @@ func (w *workspaceInitializer) getDockerCommand() string { log.Debugf("using custom docker path %s", w.workspaceInfo.Agent.Docker.Path) return w.workspaceInfo.Agent.Docker.Path } - return "docker" + return docker } func (w *workspaceInitializer) isDockerInstallDisabled() bool { @@ -579,7 +600,7 @@ func (w *workspaceInitializer) waitForDocker(resultChan <-chan dockerInstallResu result := <-resultChan dockerPath := w.workspaceInfo.Agent.Docker.Path - if result.path != "" && (dockerPath == "" || dockerPath == "docker") { + if result.path != "" && (dockerPath == "" || dockerPath == docker) { w.workspaceInfo.Agent.Docker.Path = result.path log.Debugf("set docker path to %s", result.path) } @@ -615,12 +636,7 @@ type prepareWorkspaceParams struct { // prepareWorkspace initializes the workspace content folder and downloads/prepares the workspace source. // Note: This function modifies params.workspaceInfo.ContentFolder when platform is enabled with a local folder. func prepareWorkspace(ctx context.Context, params prepareWorkspaceParams) error { - if params.workspaceInfo.CLIOptions.Platform.Enabled && - params.workspaceInfo.Workspace.Source.LocalFolder != "" { - params.workspaceInfo.ContentFolder = agent.GetAgentWorkspaceContentDir( - params.workspaceInfo.Origin, - ) - } + useAgentContentDir(params.workspaceInfo) folder, err := initContentFolder(ctx, params.workspaceInfo) if err != nil { @@ -631,13 +647,7 @@ func prepareWorkspace(ctx context.Context, params prepareWorkspaceParams) error return nil } - phase := status.Phase("") - switch { - case params.workspaceInfo.CLIOptions.Reset: - phase = status.PhaseResettingWorkspace - case params.workspaceInfo.CLIOptions.Recreate: - phase = status.PhaseRebuildingWorkspace - } + phase := prepareWorkspacePhase(params.workspaceInfo) err = prepareWorkspaceWithStatus(ctx, params.reporter, phase, func(ctx context.Context) error { return prepareWorkspaceSource(ctx, params, folder.skipSource) }) @@ -652,6 +662,23 @@ func prepareWorkspace(ctx context.Context, params prepareWorkspaceParams) error return err } +func useAgentContentDir(workspaceInfo *provider.AgentWorkspaceInfo) { + if workspaceInfo.CLIOptions.Platform.Enabled && + workspaceInfo.Workspace.Source.LocalFolder != "" { + workspaceInfo.ContentFolder = agent.GetAgentWorkspaceContentDir(workspaceInfo.Origin) + } +} + +func prepareWorkspacePhase(workspaceInfo *provider.AgentWorkspaceInfo) status.Phase { + switch { + case workspaceInfo.CLIOptions.Reset: + return status.PhaseResettingWorkspace + case workspaceInfo.CLIOptions.Recreate: + return status.PhaseRebuildingWorkspace + } + return "" +} + func prepareWorkspaceWithStatus( ctx context.Context, reporter status.Reporter, @@ -950,7 +977,7 @@ func tryMergeRootlessDockerConfig() error { return err } - dockerConfigDir := filepath.Join(homeDir, ".config", "docker") + dockerConfigDir := filepath.Join(homeDir, ".config", docker) if _, err := os.Stat(dockerConfigDir); errors.Is(err, os.ErrNotExist) { return err } diff --git a/cmd/internal/fleet_server.go b/cmd/internal/fleet_server.go index a82718c723..779cac0f87 100644 --- a/cmd/internal/fleet_server.go +++ b/cmd/internal/fleet_server.go @@ -91,17 +91,7 @@ func (c *FleetServerCmd) Run(cmd *cobra.Command, _ []string) error { for { select { case <-time.After(time.Second * 10): - if activity.lost() { - activity.release() - } - switch active := fleetSessionActive(logFile); { - case active && !activity.held(): - if err := activity.acquire(cmd.Context()); err != nil { - log.Errorf("register fleet session activity: %v", err) - } - case !active: - activity.release() - } + syncFleetActivity(cmd.Context(), &activity, logFile) case <-cmd.Context().Done(): // context is done - either canceled or time is up for timeout return nil @@ -109,6 +99,20 @@ func (c *FleetServerCmd) Run(cmd *cobra.Command, _ []string) error { } } +func syncFleetActivity(ctx context.Context, activity *fleetActivity, logFile string) { + if activity.lost() { + activity.release() + } + switch active := fleetSessionActive(logFile); { + case active && !activity.held(): + if err := activity.acquire(ctx); err != nil { + log.Errorf("register fleet session activity: %v", err) + } + case !active: + activity.release() + } +} + // fleetSessionActive reports whether the Fleet log shows a connected client. // Before the first client connects, the connected/disconnected lines are ignored. func fleetSessionActive(logFile string) bool { diff --git a/cmd/internal/get_image.go b/cmd/internal/get_image.go index e4580edd9a..74121306e8 100644 --- a/cmd/internal/get_image.go +++ b/cmd/internal/get_image.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "os" "github.com/devsy-org/devsy/cmd/flags" "github.com/devsy-org/devsy/pkg/image" @@ -44,7 +45,7 @@ func (cmd *GetImageCommand) Run(ctx context.Context, args []string) error { return err } - fmt.Println(string(out)) + _, _ = fmt.Fprintln(os.Stdout, string(out)) return nil } diff --git a/cmd/internal/get_provider_name.go b/cmd/internal/get_provider_name.go index b5a3b352fe..5110a4e2c1 100644 --- a/cmd/internal/get_provider_name.go +++ b/cmd/internal/get_provider_name.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "os" "github.com/devsy-org/devsy/cmd/flags" "github.com/devsy-org/devsy/pkg/provider" @@ -45,6 +46,6 @@ func (cmd *GetProviderNameCmd) Run(ctx context.Context, args []string) error { return fmt.Errorf("parse provider: %w", err) } - fmt.Print(providerConfig.Name) + _, _ = fmt.Fprint(os.Stdout, providerConfig.Name) return nil } diff --git a/cmd/internal/get_workspace_config.go b/cmd/internal/get_workspace_config.go index f5901f9c74..f1d733286d 100644 --- a/cmd/internal/get_workspace_config.go +++ b/cmd/internal/get_workspace_config.go @@ -101,8 +101,12 @@ func (cmd *GetWorkspaceConfigCommand) Run( ctx, rawSource, tmpDir, - cmd.maxDepth, - devsyConfig.ContextOptionBool(config.ContextOptionSSHStrictHostKeyChecking), + devcontainer.FindOptions{ + MaxDepth: cmd.maxDepth, + StrictHostKeyChecking: devsyConfig.ContextOptionBool( + config.ContextOptionSSHStrictHostKeyChecking, + ), + }, ) if err != nil { errChan <- err @@ -121,7 +125,7 @@ func (cmd *GetWorkspaceConfigCommand) Run( if err != nil { return err } - fmt.Println(string(out)) + _, _ = fmt.Fprintln(os.Stdout, string(out)) } defer close(done) diff --git a/cmd/internal/get_workspace_name.go b/cmd/internal/get_workspace_name.go index e8c6336e6a..41c1ebd897 100644 --- a/cmd/internal/get_workspace_name.go +++ b/cmd/internal/get_workspace_name.go @@ -3,6 +3,7 @@ package cmdinternal import ( "context" "fmt" + "os" "github.com/devsy-org/devsy/cmd/flags" "github.com/devsy-org/devsy/pkg/file" @@ -37,6 +38,6 @@ func (cmd *GetWorkspaceNameCommand) Run(ctx context.Context, args []string) erro _, name := file.IsLocalDir(args[0]) workspaceID := workspace.ToID(name) - fmt.Print(workspaceID) + _, _ = fmt.Fprint(os.Stdout, workspaceID) return nil } diff --git a/cmd/internal/get_workspace_uid.go b/cmd/internal/get_workspace_uid.go index 77ba081925..196b9f3c02 100644 --- a/cmd/internal/get_workspace_uid.go +++ b/cmd/internal/get_workspace_uid.go @@ -3,6 +3,7 @@ package cmdinternal import ( "context" "fmt" + "os" "github.com/devsy-org/devsy/cmd/flags" "github.com/devsy-org/devsy/pkg/encoding" @@ -30,7 +31,7 @@ func NewGetWorkspaceUIDCmd(flags *flags.GlobalFlags) *cobra.Command { } func (cmd *GetWorkspaceUIDCommand) Run(ctx context.Context, args []string) error { - fmt.Print(encoding.CreateNewUID("", "")) + _, _ = fmt.Fprint(os.Stdout, encoding.CreateNewUID("", "")) return nil } diff --git a/cmd/internal/git_credentials.go b/cmd/internal/git_credentials.go index 6e630e98d2..ea7ca2e304 100644 --- a/cmd/internal/git_credentials.go +++ b/cmd/internal/git_credentials.go @@ -148,7 +148,7 @@ func doRequest( credentials *gitcredentials.GitCredentials, url string, ) (*gitcredentials.GitCredentials, error) { - rawJSON, err := json.Marshal(credentials) + rawJSON, err := json.Marshal(credentials) //nolint:gosec // credential protocol payload if err != nil { return nil, fmt.Errorf("error marshalling credentials: %w", err) } diff --git a/cmd/internal/git_ssh_signature_test.go b/cmd/internal/git_ssh_signature_test.go index 552906800b..438e4d7abe 100644 --- a/cmd/internal/git_ssh_signature_test.go +++ b/cmd/internal/git_ssh_signature_test.go @@ -7,6 +7,22 @@ import ( "github.com/stretchr/testify/suite" ) +const ( + sign = "sign" +) + +const ( + git = "git" +) + +const ( + tmpBuffer = "/tmp/buffer" +) + +const ( + pathToKeyPub = "/path/to/key.pub" +) + type GitSSHSignatureTestSuite struct { suite.Suite } @@ -16,49 +32,49 @@ func TestGitSSHSignatureSuite(t *testing.T) { } func (s *GitSSHSignatureTestSuite) TestParseBasicSignArgs() { - args := []string{"-Y", "sign", "-n", "git", "-f", "/path/to/key.pub", "/tmp/buffer"} + args := []string{"-Y", sign, "-n", git, "-f", pathToKeyPub, tmpBuffer} result := parseSSHKeygenArgs(args) - assert.Equal(s.T(), "sign", result.command) - assert.Equal(s.T(), "git", result.namespace) - assert.Equal(s.T(), "/path/to/key.pub", result.certPath) - assert.Equal(s.T(), "/tmp/buffer", result.bufferFile) + assert.Equal(s.T(), sign, result.command) + assert.Equal(s.T(), git, result.namespace) + assert.Equal(s.T(), pathToKeyPub, result.certPath) + assert.Equal(s.T(), tmpBuffer, result.bufferFile) } func (s *GitSSHSignatureTestSuite) TestParseWithAgentFlag() { // When the signing key is loaded in the ssh-agent, git passes -U (a boolean // "use agent" flag) immediately before the buffer file. The buffer file must // still be recognised as the last non-flag argument. - args := []string{"-Y", "sign", "-n", "git", "-f", "/path/to/key.pub", "-U", "/tmp/buffer"} + args := []string{"-Y", sign, "-n", git, "-f", pathToKeyPub, "-U", tmpBuffer} result := parseSSHKeygenArgs(args) - assert.Equal(s.T(), "sign", result.command) - assert.Equal(s.T(), "/path/to/key.pub", result.certPath) - assert.Equal(s.T(), "/tmp/buffer", result.bufferFile) + assert.Equal(s.T(), sign, result.command) + assert.Equal(s.T(), pathToKeyPub, result.certPath) + assert.Equal(s.T(), tmpBuffer, result.bufferFile) } func (s *GitSSHSignatureTestSuite) TestParseNonSignCommand() { - args := []string{"-Y", "verify", "-n", "git", "-f", "/path/to/key.pub", "/tmp/buffer"} + args := []string{"-Y", "verify", "-n", git, "-f", pathToKeyPub, tmpBuffer} result := parseSSHKeygenArgs(args) assert.Equal(s.T(), "verify", result.command) } func (s *GitSSHSignatureTestSuite) TestParseMissingBufferFile() { // All args end in a flag - no buffer file present. - args := []string{"-Y", "sign", "-n", "git", "-f", "/path/to/key.pub", "-U"} + args := []string{"-Y", sign, "-n", git, "-f", pathToKeyPub, "-U"} result := parseSSHKeygenArgs(args) assert.Equal(s.T(), "", result.bufferFile) } func (s *GitSSHSignatureTestSuite) TestParseDefaultsToSign() { // If -Y is absent the command defaults to "sign". - args := []string{"-n", "git", "-f", "/path/to/key.pub", "/tmp/buffer"} + args := []string{"-n", git, "-f", pathToKeyPub, tmpBuffer} result := parseSSHKeygenArgs(args) - assert.Equal(s.T(), "sign", result.command) - assert.Equal(s.T(), "/tmp/buffer", result.bufferFile) + assert.Equal(s.T(), sign, result.command) + assert.Equal(s.T(), tmpBuffer, result.bufferFile) } func (s *GitSSHSignatureTestSuite) TestParseEmptyArgs() { result := parseSSHKeygenArgs([]string{}) - assert.Equal(s.T(), "sign", result.command) + assert.Equal(s.T(), sign, result.command) assert.Equal(s.T(), "", result.bufferFile) assert.Equal(s.T(), "", result.certPath) assert.Equal(s.T(), "", result.namespace) @@ -67,14 +83,14 @@ func (s *GitSSHSignatureTestSuite) TestParseEmptyArgs() { func (s *GitSSHSignatureTestSuite) TestParseWithUFlag() { // Git passes -U when using a literal SSH key value. The parser must // still identify certPath and bufferFile with -U present. - args := []string{"-Y", "sign", "-n", "git", "-f", "/key.pub", "-U", "/tmp/buf"} + args := []string{"-Y", sign, "-n", git, "-f", "/key.pub", "-U", "/tmp/buf"} result := parseSSHKeygenArgs(args) assert.Equal(s.T(), "/key.pub", result.certPath) assert.Equal(s.T(), "/tmp/buf", result.bufferFile) } func (s *GitSSHSignatureTestSuite) TestParseBufferFileWithSpaces() { - args := []string{"-Y", "sign", "-n", "git", "-f", "/key.pub", "/tmp/my buffer file"} + args := []string{"-Y", sign, "-n", git, "-f", "/key.pub", "/tmp/my buffer file"} result := parseSSHKeygenArgs(args) assert.Equal(s.T(), "/tmp/my buffer file", result.bufferFile) } diff --git a/cmd/internal/logs_daemon.go b/cmd/internal/logs_daemon.go index 025757ebe7..ccfb17c0e9 100644 --- a/cmd/internal/logs_daemon.go +++ b/cmd/internal/logs_daemon.go @@ -46,22 +46,9 @@ func (cmd *LogsDaemonCmd) Run(ctx context.Context, args []string) error { return err } - baseClient, err := workspace.Get(ctx, workspace.GetOptions{ - DevsyConfig: devsyConfig, - Args: args, - Owner: cmd.Owner, - }) + workspaceClient, err := cmd.machineWorkspaceClient(ctx, devsyConfig, args) if err != nil { return err - } else if baseClient.WorkspaceConfig().Machine.ID == "" { - return fmt.Errorf( - "selected workspace is not a machine provider, there is not daemon running", - ) - } - - workspaceClient, ok := baseClient.(client.WorkspaceClient) - if !ok { - return fmt.Errorf("this command is not supported for proxy providers") } _, agentInfo, err := workspaceClient.AgentInfo(provider2.CLIOptions{}) @@ -92,6 +79,36 @@ func (cmd *LogsDaemonCmd) Run(ctx context.Context, args []string) error { if err := json.Unmarshal(stdout.Bytes(), &response); err != nil { return fmt.Errorf("decode remote daemon diagnostics: %w", err) } + printDaemonDiagnostics(response) + return nil +} + +func (cmd *LogsDaemonCmd) machineWorkspaceClient( + ctx context.Context, + devsyConfig *config.Config, + args []string, +) (client.WorkspaceClient, error) { + baseClient, err := workspace.Get(ctx, workspace.GetOptions{ + DevsyConfig: devsyConfig, + Args: args, + Owner: cmd.Owner, + }) + if err != nil { + return nil, err + } else if baseClient.WorkspaceConfig().Machine.ID == "" { + return nil, fmt.Errorf( + "selected workspace is not a machine provider, there is not daemon running", + ) + } + + workspaceClient, ok := baseClient.(client.WorkspaceClient) + if !ok { + return nil, fmt.Errorf("this command is not supported for proxy providers") + } + return workspaceClient, nil +} + +func printDaemonDiagnostics(response machinediagnostics.ReadResponse) { for _, event := range response.Events { _, _ = fmt.Fprintf( os.Stdout, @@ -105,5 +122,4 @@ func (cmd *LogsDaemonCmd) Run(ctx context.Context, args []string) error { if response.Availability != machinediagnostics.AvailabilityAvailable { _, _ = fmt.Fprintf(os.Stdout, "Daemon diagnostics: %s\n", response.Availability) } - return nil } diff --git a/cmd/internal/runusercommands.go b/cmd/internal/runusercommands.go index 2d137b4315..7b922bc54a 100644 --- a/cmd/internal/runusercommands.go +++ b/cmd/internal/runusercommands.go @@ -19,6 +19,7 @@ import ( "github.com/devsy-org/devsy/pkg/status" "github.com/devsy-org/devsy/pkg/types" workspace2 "github.com/devsy-org/devsy/pkg/workspace" + "github.com/devsy-org/devsy/pkg/workspacelogs" "github.com/spf13/cobra" ) @@ -41,6 +42,13 @@ type RunUserCommandsCmd struct { SkipOnCreate bool SkipUpdateContent bool statusReporter status.Reporter + // identity is the resolved Devsy workspace, set only on the + // workspace-resolved path. + identity *workspaceIdentity +} + +type workspaceIdentity struct { + context, uid, id string } // NewRunUserCommandsCmd creates a new run-user-commands command. @@ -121,7 +129,10 @@ func (cmd *RunUserCommandsCmd) Run(ctx context.Context) error { return err } - if err := cmd.runLifecycleHooks(params, result); err != nil { + recording := cmd.beginRunRecording(params) + err = cmd.runLifecycleHooks(params, result) + recording.end(ctx, err) + if err != nil { return err } @@ -295,6 +306,7 @@ func (cmd *RunUserCommandsCmd) runWithContainerID(ctx context.Context) error { EnvArgs: envArgs, Workdir: workdir, User: devcconfig.GetRemoteUser(result), + Recorder: workspacelogs.RecorderFrom(ctx), } if err := cmd.runLifecycleHooks(params, result); err != nil { @@ -422,6 +434,9 @@ func (cmd *RunUserCommandsCmd) resolveContainer( } workspaceConfig := client.WorkspaceConfig() + cmd.identity = &workspaceIdentity{ + context: workspaceConfig.Context, uid: workspaceConfig.UID, id: workspaceConfig.ID, + } runtime, err := workspace2.NewContainerRuntime(workspaceConfig, cmd.DockerPath) if err != nil { return nil, nil, err @@ -533,8 +548,10 @@ func execLifecycleHooks( name string, cmds []types.LifecycleHook, ) error { - for _, h := range cmds { - if err := workspace.ExecLifecycleHook(params, name, h); err != nil { + for i, h := range cmds { + indexed := *params + indexed.HookIndex = i + if err := workspace.ExecLifecycleHook(&indexed, name, h); err != nil { return fmt.Errorf("lifecycle hooks: %s: %w", name, err) } } @@ -563,3 +580,72 @@ func resolveWaitForBoundary(result *devcconfig.Result) int { } return 1 } + +// runRecording is the fresh run opened for one run-user-commands invocation. +// A nil value records nothing. +type runRecording struct { + store *workspacelogs.Store + rec *workspacelogs.Recorder + runID string +} + +// beginRunRecording opens a new run for the resolved workspace and installs a +// recorder on params. It never fails the command: any store error is logged +// at debug level and recording is skipped. A container without a Devsy +// workspace identity is not recorded. +func (cmd *RunUserCommandsCmd) beginRunRecording( + params *workspace.LifecycleExecParams, +) *runRecording { + id := cmd.identity + if id == nil || id.uid == "" { + return nil + } + store, err := workspacelogs.OpenDefault() + if err != nil { + log.Debugf("workspace history unavailable: %v", err) + return nil + } + m, err := store.CreateRun(workspacelogs.RunSpec{ + Context: id.context, WorkspaceIDAtStart: id.id, Command: "run-user-commands", + }) + if err == nil { + err = store.BindWorkspace(m.RunID, id.uid, id.id) + } + if err != nil { + log.Debugf("workspace history unavailable: %v", err) + return nil + } + rec, err := workspacelogs.NewRecorder(workspacelogs.RecorderOptions{ + Backend: store, + RunID: m.RunID, + Location: workspacelogs.LocationContainer, + Redactor: workspace.EnvArgsRedactor(params.EnvArgs), + }) + if err != nil { + log.Debugf("workspace history recorder unavailable: %v", err) + } + _ = store.SetCommandState(m.RunID, workspacelogs.CommandRunning) + params.Recorder = rec + params.Ctx = workspacelogs.WithRecorder(params.Ctx, rec) + + return &runRecording{store: store, rec: rec, runID: m.RunID} +} + +// end closes the recorder and records the command outcome. +func (r *runRecording) end(ctx context.Context, runErr error) { + if r == nil { + return + } + r.rec.Close(workspacelogs.ProducerTerminal, true) + state := workspacelogs.CommandSucceeded + switch { + case runErr == nil: + case ctx.Err() != nil: + state = workspacelogs.CommandCanceled + default: + state = workspacelogs.CommandFailed + } + if err := r.store.SetCommandState(r.runID, state); err != nil { + log.Debugf("record run command state: %v", err) + } +} diff --git a/cmd/internal/runusercommands_record_test.go b/cmd/internal/runusercommands_record_test.go new file mode 100644 index 0000000000..1c51d644e1 --- /dev/null +++ b/cmd/internal/runusercommands_record_test.go @@ -0,0 +1,102 @@ +//go:build !windows + +package cmdinternal + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/devsy-org/devsy/cmd/workspace" + "github.com/devsy-org/devsy/pkg/docker" + "github.com/devsy-org/devsy/pkg/types" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func fakeDockerParams(t *testing.T) *workspace.LifecycleExecParams { + t.Helper() + path := filepath.Join(t.TempDir(), "docker") + script := "#!/bin/sh\necho hook-out\nexit \"${FAKE_EXIT:-0}\"\n" + require.NoError(t, os.WriteFile(path, []byte(script), 0o700)) //nolint:gosec // test script + + return &workspace.LifecycleExecParams{ + Ctx: context.Background(), Helper: &docker.DockerHelper{DockerCommand: path}, + ContainerID: "c1", + } +} + +func TestRunRecordingWithoutWorkspaceIdentityRecordsNothing(t *testing.T) { + t.Setenv("DEVSY_HOME", t.TempDir()) + cmd := &RunUserCommandsCmd{} + params := fakeDockerParams(t) + + recording := cmd.beginRunRecording(params) + + assert.Nil(t, recording) + assert.Nil(t, params.Recorder) + recording.end(context.Background(), nil) +} + +func TestRunRecordingCreatesFreshBoundRun(t *testing.T) { + t.Setenv("DEVSY_HOME", t.TempDir()) + cmd := &RunUserCommandsCmd{identity: &workspaceIdentity{ + context: "default", uid: "uid-1", id: "my-ws", + }} + hooks := []types.LifecycleHook{{"": {"echo a"}}, {"named": {"echo b"}}} + + var runIDs []string + for range 2 { + params := fakeDockerParams(t) + recording := cmd.beginRunRecording(params) + require.NotNil(t, recording) + err := execLifecycleHooks(params, "postStartCommand", hooks) + recording.end(context.Background(), err) + require.NoError(t, err) + runIDs = append(runIDs, recording.runID) + } + assert.NotEqual(t, runIDs[0], runIDs[1], "every invocation allocates a new run") + + store, err := workspacelogs.OpenDefault() + require.NoError(t, err) + resp, err := store.Read(workspacelogs.ReadRequest{RunID: runIDs[0]}) + require.NoError(t, err) + require.NotNil(t, resp.Run.WorkspaceUID) + assert.Equal(t, "uid-1", *resp.Run.WorkspaceUID) + assert.Equal(t, workspacelogs.CommandSucceeded, resp.Run.CommandState) + assert.True(t, resp.Complete) + + indexes := map[int]string{} + var out string + for _, r := range resp.Records { + if r.Kind == workspacelogs.KindInvocation && r.State == workspacelogs.InvocationSucceeded { + indexes[*r.HookIndex] = r.HookKey + } + if r.Kind == workspacelogs.KindOutput { + out += r.Text + } + } + assert.Equal(t, map[int]string{0: "", 1: "named"}, indexes) + assert.Equal(t, "hook-out\nhook-out\n", out) +} + +func TestRunRecordingMarksFailedCommand(t *testing.T) { + t.Setenv("DEVSY_HOME", t.TempDir()) + t.Setenv("FAKE_EXIT", "9") + cmd := &RunUserCommandsCmd{identity: &workspaceIdentity{context: "default", uid: "uid-1"}} + params := fakeDockerParams(t) + + recording := cmd.beginRunRecording(params) + require.NotNil(t, recording) + err := execLifecycleHooks(params, "postCreateCommand", []types.LifecycleHook{{"": {"x"}}}) + require.Error(t, err) + recording.end(context.Background(), err) + + store, err := workspacelogs.OpenDefault() + require.NoError(t, err) + resp, err := store.Read(workspacelogs.ReadRequest{RunID: recording.runID}) + require.NoError(t, err) + assert.Equal(t, workspacelogs.CommandFailed, resp.Run.CommandState) +} diff --git a/cmd/internal/ssh_client.go b/cmd/internal/ssh_client.go index 08b4fd6e20..361907db39 100644 --- a/cmd/internal/ssh_client.go +++ b/cmd/internal/ssh_client.go @@ -71,7 +71,7 @@ func (cmd *SSHClient) getConfig() (*ssh.ClientConfig, error) { clientConfig := &ssh.ClientConfig{ User: cmd.User, Auth: []ssh.AuthMethod{}, - HostKeyCallback: ssh.InsecureIgnoreHostKey(), + HostKeyCallback: ssh.InsecureIgnoreHostKey(), //nolint:gosec // no known_hosts for workspaces } // key file authentication? diff --git a/cmd/internal/ssh_git_clone.go b/cmd/internal/ssh_git_clone.go index fe7b233bcc..2fc2b0675e 100644 --- a/cmd/internal/ssh_git_clone.go +++ b/cmd/internal/ssh_git_clone.go @@ -99,6 +99,7 @@ func runSSHSession(sshConfig *ssh.ClientConfig, addr string, sshCmdArgs []string func getConfig(userName string, keyFilePaths []string) (*ssh.ClientConfig, error) { signers := []ssh.Signer{} for _, keyFilePath := range keyFilePaths { + //nolint:gosec // keyFilePath is an SSH identity file the user configured out, err := os.ReadFile(keyFilePath) if err != nil { return nil, fmt.Errorf("read private ssh key: %w", err) @@ -115,7 +116,7 @@ func getConfig(userName string, keyFilePaths []string) (*ssh.ClientConfig, error return &ssh.ClientConfig{ User: userName, Auth: []ssh.AuthMethod{ssh.PublicKeys(signers...)}, - HostKeyCallback: ssh.InsecureIgnoreHostKey(), + HostKeyCallback: ssh.InsecureIgnoreHostKey(), //nolint:gosec // existing behaviour; host key checks are out of scope }, nil } diff --git a/cmd/internal/ssh_server.go b/cmd/internal/ssh_server.go index 478bfb6e23..6b261f1629 100644 --- a/cmd/internal/ssh_server.go +++ b/cmd/internal/ssh_server.go @@ -87,9 +87,12 @@ func (cmd *sshServerCmd) run(ctx context.Context) error { log.Debugf("starting ssh server: stdio=%v", cmd.stdio) sshserver.SweepStaleAgentSockets() - server, err := sshserver.NewServer( - cmd.address, hostKey, keys, cmd.workdir, cmd.reuseSSHAuthSock, - ) + server, err := sshserver.NewServer(cmd.address, sshserver.ServerOptions{ + HostKey: hostKey, + Keys: keys, + Workdir: cmd.workdir, + ReuseSock: cmd.reuseSSHAuthSock, + }) if err != nil { return fmt.Errorf("create ssh server: %w", err) } diff --git a/cmd/machine/ssh.go b/cmd/machine/ssh.go index 350b68cb1f..169fe888b8 100644 --- a/cmd/machine/ssh.go +++ b/cmd/machine/ssh.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "strings" + "time" "al.essio.dev/pkg/shellescape" "github.com/devsy-org/devsy/cmd/flags" @@ -149,41 +150,48 @@ func (cmd *SSHCmd) Run(ctx context.Context, args []string) error { TermMode: cmd.TermMode, InstallTerminfo: cmd.InstallTerminfo, }, - Exec: func(ctx context.Context, stdin io.Reader, stdout io.Writer, stderr io.Writer) error { - c := strings.Builder{} - _, _ = fmt.Fprintf( - &c, - "%s internal ssh-server --stdio", - shellescape.Quote(machineClient.AgentPath()), - ) - if cmd.Debug { - _, _ = c.WriteString(" --debug") - } - command := c.String() - return devagent.InjectAgent(ctx, &devagent.InjectOptions{ - Exec: func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error { - return machineClient.Command(ctx, client.CommandOptions{ - Command: command, - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, - RawStdout: true, - }) - }, - IsLocal: machineClient.AgentLocal(), - RemoteAgentPath: machineClient.AgentPath(), - DownloadURL: machineClient.AgentURL(), - Command: command, - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, - Timeout: timeout, - }) - }, + Exec: cmd.injectExec(machineClient, timeout), Stderr: streamer, }) } +func (cmd *SSHCmd) injectExec( + machineClient client.MachineClient, + timeout time.Duration, +) ExecFunc { + return func(ctx context.Context, stdin io.Reader, stdout io.Writer, stderr io.Writer) error { + c := strings.Builder{} + _, _ = fmt.Fprintf( + &c, + "%s internal ssh-server --stdio", + shellescape.Quote(machineClient.AgentPath()), + ) + if cmd.Debug { + _, _ = c.WriteString(" --debug") + } + command := c.String() + return devagent.InjectAgent(ctx, &devagent.InjectOptions{ + Exec: func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error { + return machineClient.Command(ctx, client.CommandOptions{ + Command: command, + Stdin: stdin, + Stdout: stdout, + Stderr: stderr, + RawStdout: true, + }) + }, + IsLocal: machineClient.AgentLocal(), + RemoteAgentPath: machineClient.AgentPath(), + DownloadURL: machineClient.AgentURL(), + Command: command, + Stdin: stdin, + Stdout: stdout, + Stderr: stderr, + Timeout: timeout, + }) + } +} + type ExecFunc func(ctx context.Context, stdin io.Reader, stdout io.Writer, stderr io.Writer) error func StartSSHSession(ctx context.Context, options StartSSHSessionOptions) error { @@ -247,10 +255,8 @@ func RunSSHSession(ctx context.Context, sshClient *ssh.Client, options RunSSHSes return err } defer func() { _ = session.Close() }() - for key, value := range options.SessionOptions.EnvVars { - if err := session.Setenv(key, value); err != nil { - return fmt.Errorf("set SSH session environment: %w", err) - } + if err := setSessionEnv(session, options.SessionOptions.EnvVars); err != nil { + return err } if err := configureAgentForwarding(sshClient, session, options.AgentForwarding); err != nil { @@ -275,6 +281,15 @@ func RunSSHSession(ctx context.Context, sshClient *ssh.Client, options RunSSHSes return nil } +func setSessionEnv(session *ssh.Session, envVars map[string]string) error { + for key, value := range envVars { + if err := session.Setenv(key, value); err != nil { + return fmt.Errorf("set SSH session environment: %w", err) + } + } + return nil +} + func setupPTYIfNeeded( ctx context.Context, sshClient *ssh.Client, @@ -632,8 +647,7 @@ func waitRemoteCommand(ctx context.Context, session *ssh.Session) (int, error) { return 0, nil } - var exitErr *ssh.ExitError - if errors.As(err, &exitErr) { + if exitErr, ok := errors.AsType[*ssh.ExitError](err); ok { return exitErr.ExitStatus(), nil } diff --git a/cmd/machine/status.go b/cmd/machine/status.go index 2d917e6916..729cd7662f 100644 --- a/cmd/machine/status.go +++ b/cmd/machine/status.go @@ -76,7 +76,7 @@ func (cmd *StatusCmd) Run(ctx context.Context, args []string) error { return err } - fmt.Print(string(out)) + _, _ = fmt.Fprint(os.Stdout, string(out)) } return nil diff --git a/cmd/mcp/serve_test.go b/cmd/mcp/serve_test.go index aee0ede1c8..5d37389c7b 100644 --- a/cmd/mcp/serve_test.go +++ b/cmd/mcp/serve_test.go @@ -10,6 +10,10 @@ import ( sdkmcp "github.com/modelcontextprotocol/go-sdk/mcp" ) +const ( + workspaceExec = "workspace_exec" +) + func TestServer_ListsAllTools(t *testing.T) { home := t.TempDir() t.Setenv("DEVSY_HOME", home) @@ -42,7 +46,7 @@ func TestServer_ListsAllTools(t *testing.T) { } wantNames := []string{ "workspace_list", "workspace_status", "workspace_start", "workspace_stop", - "workspace_delete", "workspace_create", "workspace_exec", + "workspace_delete", "workspace_create", workspaceExec, "provider_list", "provider_add", "provider_delete", "provider_use", } have := map[string]bool{} @@ -99,7 +103,7 @@ func TestServer_WorkspaceExecRespectsSemaphore(t *testing.T) { blockedCtx, blockedCancel := context.WithTimeout(ctx, 200*time.Millisecond) defer blockedCancel() _, callErr := session.CallTool(blockedCtx, &sdkmcp.CallToolParams{ - Name: "workspace_exec", + Name: workspaceExec, Arguments: execArgs, }) if callErr == nil { @@ -109,7 +113,7 @@ func TestServer_WorkspaceExecRespectsSemaphore(t *testing.T) { release() res, callErr := session.CallTool(ctx, &sdkmcp.CallToolParams{ - Name: "workspace_exec", + Name: workspaceExec, Arguments: execArgs, }) if callErr != nil { diff --git a/cmd/mcp/tools_provider.go b/cmd/mcp/tools_provider.go index 21c279e71e..92c071e258 100644 --- a/cmd/mcp/tools_provider.go +++ b/cmd/mcp/tools_provider.go @@ -158,7 +158,7 @@ func runProviderDelete(ctx context.Context, g *flags.GlobalFlags, name string) e if err != nil { return err } - return cmdprovider.DeleteProvider(ctx, devsyConfig, name, false, false) + return cmdprovider.DeleteProvider(ctx, devsyConfig, name, cmdprovider.DeleteOptions{}) } func runProviderUse(_ context.Context, g *flags.GlobalFlags, name string) error { diff --git a/cmd/pro/check_update.go b/cmd/pro/check_update.go index 2b947f153a..717d1bf93a 100644 --- a/cmd/pro/check_update.go +++ b/cmd/pro/check_update.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "os" "github.com/devsy-org/devsy/cmd/pro/cmdutil" "github.com/devsy-org/devsy/cmd/pro/flags" @@ -68,7 +69,7 @@ func (cmd *CheckUpdateCmd) Run( return err } - fmt.Print(string(out)) + _, _ = fmt.Fprint(os.Stdout, string(out)) return nil } diff --git a/cmd/pro/cluster/add.go b/cmd/pro/cluster/add.go index bad282b197..ba5429ae63 100644 --- a/cmd/pro/cluster/add.go +++ b/cmd/pro/cluster/add.go @@ -365,6 +365,7 @@ func installAgent( errChan := make(chan error) go func() { + //nolint:gosec // command is a fixed CLI with arguments built internally helmCmd := exec.CommandContext(ctx, "helm", helmArgs...) helmCmd.Stdout = log.Writer(log.LevelDebug) diff --git a/cmd/pro/cmdutil/cmdutil.go b/cmd/pro/cmdutil/cmdutil.go index 66d67a90cb..72262e2d01 100644 --- a/cmd/pro/cmdutil/cmdutil.go +++ b/cmd/pro/cmdutil/cmdutil.go @@ -110,7 +110,14 @@ func AddHostFlag(c *cobra.Command, dest *string, opts ...HostFlagOption) { toComplete string, ) ([]string, cobra.ShellCompDirective) { return completion.GetPlatformHostSuggestions( - rootCmd, gf.Context, gf.Provider, args, toComplete, gf.Owner, + rootCmd, + completion.SuggestionRequest{ + Context: gf.Context, + Provider: gf.Provider, + Args: args, + ToComplete: toComplete, + Owner: gf.Owner, + }, ) }) } diff --git a/cmd/pro/completion/suggestions.go b/cmd/pro/completion/suggestions.go index 08fa327c85..7db9d421c2 100644 --- a/cmd/pro/completion/suggestions.go +++ b/cmd/pro/completion/suggestions.go @@ -9,13 +9,20 @@ import ( "github.com/spf13/cobra" ) +// SuggestionRequest carries the inputs of the completion helpers. +type SuggestionRequest struct { + Context string + Provider string + Args []string + ToComplete string + Owner platform.OwnerFilter +} + func GetPlatformHostSuggestions( rootCmd *cobra.Command, - context, provider string, - args []string, - toComplete string, - owner platform.OwnerFilter, + req SuggestionRequest, ) ([]string, cobra.ShellCompDirective) { + context, provider, toComplete := req.Context, req.Provider, req.ToComplete devsyConfig, err := config.LoadConfig(context, provider) if err != nil { return nil, cobra.ShellCompDirectiveError diff --git a/cmd/pro/daemon/start.go b/cmd/pro/daemon/start.go index 03e60fdec3..12fca03112 100644 --- a/cmd/pro/daemon/start.go +++ b/cmd/pro/daemon/start.go @@ -140,10 +140,10 @@ func ensureDaemonDir(context, providerName string) (string, error) { func printStatus(status daemon.Status) { out, err := json.Marshal(status) if err != nil { - fmt.Printf("failed to marshal status: %v\n", err) + _, _ = fmt.Fprintf(os.Stdout, "failed to marshal status: %v\n", err) os.Exit(1) } - fmt.Println(string(out)) + _, _ = fmt.Fprintln(os.Stdout, string(out)) } func getUserName(self *managementv1.Self) string { diff --git a/cmd/pro/daemon/status.go b/cmd/pro/daemon/status.go index d1f5e36b29..ea84a07911 100644 --- a/cmd/pro/daemon/status.go +++ b/cmd/pro/daemon/status.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "os" "github.com/devsy-org/devsy/cmd/pro/cmdutil" platformflags "github.com/devsy-org/devsy/cmd/pro/flags" @@ -50,7 +51,7 @@ func (cmd *StatusCmd) Run( return err } - fmt.Print(string(out)) + _, _ = fmt.Fprint(os.Stdout, string(out)) return nil } diff --git a/cmd/pro/list.go b/cmd/pro/list.go index d39668066f..1b88acecea 100644 --- a/cmd/pro/list.go +++ b/cmd/pro/list.go @@ -170,15 +170,11 @@ func checkLogin( platformInstance *provider.PlatformInstance, ) error { // for every platform instance, check auth status by calling login - if err := login( - ctx, - devsyConfig, - platformInstance.Host, - platformInstance.Provider, - "", - true, - false, - ); err != nil { + if err := login(ctx, devsyConfig, loginRequest{ + URL: platformInstance.Host, + ProviderName: platformInstance.Provider, + SkipBrowserLogin: true, + }); err != nil { return fmt.Errorf("not logged into %s", platformInstance.Host) } diff --git a/cmd/pro/login.go b/cmd/pro/login.go index 40121ba0bc..5074b32d66 100644 --- a/cmd/pro/login.go +++ b/cmd/pro/login.go @@ -19,6 +19,7 @@ import ( "github.com/devsy-org/devsy/pkg/types" versionpkg "github.com/devsy-org/devsy/pkg/version" "github.com/devsy-org/devsy/pkg/workspace" + "github.com/gofrs/flock" "github.com/spf13/cobra" ) @@ -138,40 +139,48 @@ func (cmd *LoginCmd) prepareProvider(ctx context.Context, fullURL string) (*conf defer func() { _ = loginLock.Unlock() }() for { - devsyConfig, currentInstance, resolveErr := cmd.resolveInstance( - fullURL, - initial.DefaultContext, - ) - if resolveErr != nil || currentInstance != nil { - return devsyConfig, resolveErr - } - providerName := cmd.Provider - opLock, lockErr := provider.GetProviderOperationLock( - devsyConfig.DefaultContext, - providerName, - ) - if lockErr != nil { - return nil, fmt.Errorf("get operation lock: %w", lockErr) - } - if lockErr := opLock.Lock(); lockErr != nil { - return nil, fmt.Errorf("acquire operation lock: %w", lockErr) + devsyConfig, done, err := cmd.configureOnce(ctx, fullURL, initial) + if done { + return devsyConfig, err } + } +} - devsyConfig, currentInstance, resolveErr = cmd.resolveInstance( - fullURL, - initial.DefaultContext, - ) - if resolveErr != nil || currentInstance != nil || cmd.Provider != providerName { - _ = opLock.Unlock() - if resolveErr != nil || currentInstance != nil { - return devsyConfig, resolveErr - } - continue - } - configured, configureErr := cmd.ensureProvider(ctx, devsyConfig, nil, fullURL) +// configureOnce reports done=false when the provider changed while waiting +// for its operation lock and resolution must be retried. +func (cmd *LoginCmd) configureOnce( + ctx context.Context, + fullURL string, + initial *config.Config, +) (*config.Config, bool, error) { + devsyConfig, currentInstance, resolveErr := cmd.resolveInstance( + fullURL, + initial.DefaultContext, + ) + if resolveErr != nil || currentInstance != nil { + return devsyConfig, true, resolveErr + } + providerName := cmd.Provider + opLock, lockErr := lockProvider(devsyConfig.DefaultContext, providerName) + if lockErr != nil { + return nil, true, lockErr + } + + devsyConfig, currentInstance, resolveErr = cmd.resolveInstance( + fullURL, + initial.DefaultContext, + ) + settled := resolveErr != nil || currentInstance != nil + if settled || cmd.Provider != providerName { _ = opLock.Unlock() - return configured, configureErr + if settled { + return devsyConfig, true, resolveErr + } + return nil, false, nil } + configured, configureErr := cmd.ensureProvider(ctx, devsyConfig, nil, fullURL) + _ = opLock.Unlock() + return configured, true, configureErr } func (cmd *LoginCmd) normalizeURL(fullURL string) (string, error) { @@ -334,15 +343,12 @@ func (cmd *LoginCmd) loginAndConfigure( } if cmd.Login { - err = login( - ctx, - devsyConfig, - fullURL, - cmd.Provider, - cmd.AccessKey, - false, - cmd.ForceBrowser, - ) + err = login(ctx, devsyConfig, loginRequest{ + URL: fullURL, + ProviderName: cmd.Provider, + AccessKey: cmd.AccessKey, + ForceBrowser: cmd.ForceBrowser, + }) if err != nil { return err } @@ -350,33 +356,8 @@ func (cmd *LoginCmd) loginAndConfigure( } if cmd.Use { - opLock, err := provider.GetProviderOperationLock( - devsyConfig.DefaultContext, - providerConfig.Name, - ) - if err != nil { - return fmt.Errorf("get operation lock: %w", err) - } - if err := opLock.Lock(); err != nil { - return fmt.Errorf("acquire operation lock: %w", err) - } - defer func() { _ = opLock.Unlock() }() - providerConfig, err = provider.LoadProviderConfig( - devsyConfig.DefaultContext, - cmd.Provider, - ) - if err != nil { - return fmt.Errorf("reload provider: %w", err) - } - - // Post-login: preserve user values; resolver prunes anything stale. - err = providercmd.ConfigureProvider(ctx, providercmd.ProviderOptionsConfig{ - Provider: providerConfig, - ContextName: devsyConfig.DefaultContext, - UserOptions: cmd.Options, - }) - if err != nil { - return fmt.Errorf("configure provider: %w", err) + if err := cmd.useProvider(ctx, devsyConfig, providerConfig.Name); err != nil { + return err } } @@ -384,6 +365,47 @@ func (cmd *LoginCmd) loginAndConfigure( return nil } +func (cmd *LoginCmd) useProvider( + ctx context.Context, + devsyConfig *config.Config, + providerName string, +) error { + opLock, err := lockProvider(devsyConfig.DefaultContext, providerName) + if err != nil { + return err + } + defer func() { _ = opLock.Unlock() }() + providerConfig, err := provider.LoadProviderConfig( + devsyConfig.DefaultContext, + cmd.Provider, + ) + if err != nil { + return fmt.Errorf("reload provider: %w", err) + } + + // Post-login: preserve user values; resolver prunes anything stale. + err = providercmd.ConfigureProvider(ctx, providercmd.ProviderOptionsConfig{ + Provider: providerConfig, + ContextName: devsyConfig.DefaultContext, + UserOptions: cmd.Options, + }) + if err != nil { + return fmt.Errorf("configure provider: %w", err) + } + return nil +} + +func lockProvider(contextName, providerName string) (*flock.Flock, error) { + opLock, err := provider.GetProviderOperationLock(contextName, providerName) + if err != nil { + return nil, fmt.Errorf("get operation lock: %w", err) + } + if err := opLock.Lock(); err != nil { + return nil, fmt.Errorf("acquire operation lock: %w", err) + } + return opLock, nil +} + func (cmd *LoginCmd) addDevsyProvider( ctx context.Context, devsyConfig *config.Config, @@ -438,14 +460,22 @@ func (cmd *LoginCmd) resolveProviderSource(url string) error { return nil } +// loginRequest carries the inputs of login. +type loginRequest struct { + URL string + ProviderName string + AccessKey string + SkipBrowserLogin bool + ForceBrowser bool +} + func login( ctx context.Context, devsyConfig *config.Config, - url string, - providerName string, - accessKey string, - skipBrowserLogin, forceBrowser bool, + req loginRequest, ) error { + url, providerName, accessKey := req.URL, req.ProviderName, req.AccessKey + skipBrowserLogin, forceBrowser := req.SkipBrowserLogin, req.ForceBrowser configPath, err := platform.DevsyConfigPath(devsyConfig.DefaultContext, providerName) if err != nil { return err diff --git a/cmd/pro/logout.go b/cmd/pro/logout.go index b363a22220..93543f3d7f 100644 --- a/cmd/pro/logout.go +++ b/cmd/pro/logout.go @@ -97,13 +97,10 @@ func (cmd *LogoutCmd) Run(ctx context.Context, args []string) error { if err != nil { log.Warnf("failed to list workspaces: %v", err) } else { - cleanupLocalWorkspaces( - ctx, - devsyConfig, - workspaces, - providerConfig.Name, - cmd.Owner, - ) + cleanupLocalWorkspaces(ctx, devsyConfig, workspaces, workspaceCleanup{ + providerName: providerConfig.Name, + owner: cmd.Owner, + }) } daemonClient := daemon.NewLocalClient(platformInstanceConfig.Provider) @@ -160,13 +157,19 @@ func deletePlatformInstanceFiles(contextName string, instance *provider.Platform return nil } +// workspaceCleanup selects the provider and owner whose workspaces are removed. +type workspaceCleanup struct { + providerName string + owner platform.OwnerFilter +} + func cleanupLocalWorkspaces( ctx context.Context, devsyConfig *config.Config, workspaces []*provider.Workspace, - providerName string, - owner platform.OwnerFilter, + scope workspaceCleanup, ) { + providerName, owner := scope.providerName, scope.owner usedWorkspaces := []*provider.Workspace{} for _, workspace := range workspaces { diff --git a/cmd/pro/provider/create/workspace.go b/cmd/pro/provider/create/workspace.go index 582d0b398a..65e3a312a7 100644 --- a/cmd/pro/provider/create/workspace.go +++ b/cmd/pro/provider/create/workspace.go @@ -139,14 +139,12 @@ func createFromEnv(ctx context.Context, baseClient client.Client) error { return fmt.Errorf("unable to create new instance through CLI if stdin is not a terminal") } - instance, err = form.CreateInstance( - ctx, - baseClient, - env.id, - env.uid, - env.source, - env.picture, - ) + instance, err = form.CreateInstance(ctx, baseClient, form.InstanceSpec{ + ID: env.id, + UID: env.uid, + Source: env.source, + Picture: env.picture, + }) if err != nil { return err } diff --git a/cmd/pro/provider/exec.go b/cmd/pro/provider/exec.go index 3da7ded547..a201542c38 100644 --- a/cmd/pro/provider/exec.go +++ b/cmd/pro/provider/exec.go @@ -45,7 +45,11 @@ func dialAndExecute(ctx context.Context, params dialAndExecuteParams) error { return err } - _, err = remotecommand.ExecuteConn(ctx, conn, params.stdin, params.stdout, params.stderr) + _, err = remotecommand.ExecuteConn( + ctx, + conn, + remotecommand.Streams{Stdin: params.stdin, Stdout: params.stdout, Stderr: params.stderr}, + ) if err != nil { return fmt.Errorf("error executing: %w", err) } diff --git a/cmd/pro/provider/get/self.go b/cmd/pro/provider/get/self.go index 299b71747c..eeaf5063f6 100644 --- a/cmd/pro/provider/get/self.go +++ b/cmd/pro/provider/get/self.go @@ -49,7 +49,7 @@ func (cmd *SelfCmd) Run( if err != nil { return err } - fmt.Println(string(out)) + _, _ = fmt.Fprintln(os.Stdout, string(out)) return nil } diff --git a/cmd/pro/provider/get/version.go b/cmd/pro/provider/get/version.go index d007b7b60d..c13c123df0 100644 --- a/cmd/pro/provider/get/version.go +++ b/cmd/pro/provider/get/version.go @@ -91,7 +91,7 @@ func (cmd *VersionCmd) Run( return err } - fmt.Println(string(out)) + _, _ = fmt.Fprintln(os.Stdout, string(out)) return nil } diff --git a/cmd/pro/provider/get/workspace.go b/cmd/pro/provider/get/workspace.go index 01cf1fbf6c..c1ab6099f7 100644 --- a/cmd/pro/provider/get/workspace.go +++ b/cmd/pro/provider/get/workspace.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "os" "github.com/devsy-org/devsy/cmd/pro/flags" "github.com/devsy-org/devsy/pkg/platform" @@ -58,7 +59,7 @@ func (cmd *WorkspaceCmd) Run(ctx context.Context) error { return nil } - fmt.Println(string(instanceBytes)) + _, _ = fmt.Fprintln(os.Stdout, string(instanceBytes)) return nil } diff --git a/cmd/pro/provider/health.go b/cmd/pro/provider/health.go index 66fbb9901a..291df3ff4c 100644 --- a/cmd/pro/provider/health.go +++ b/cmd/pro/provider/health.go @@ -67,7 +67,7 @@ func (cmd *HealthCmd) Run( return err } - fmt.Println(string(out)) + _, _ = fmt.Fprintln(os.Stdout, string(out)) return nil } diff --git a/cmd/pro/provider/list/clusters.go b/cmd/pro/provider/list/clusters.go index dd28197e16..d0e77aa28c 100644 --- a/cmd/pro/provider/list/clusters.go +++ b/cmd/pro/provider/list/clusters.go @@ -56,7 +56,7 @@ func (cmd *ClustersCmd) Run(ctx context.Context) error { if err != nil { return err } - fmt.Println(string(out)) + _, _ = fmt.Fprintln(os.Stdout, string(out)) return nil } diff --git a/cmd/pro/provider/list/projects.go b/cmd/pro/provider/list/projects.go index 68fcecdf87..2a5ae61672 100644 --- a/cmd/pro/provider/list/projects.go +++ b/cmd/pro/provider/list/projects.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "os" managementv1 "github.com/devsy-org/api/pkg/apis/management/v1" "github.com/devsy-org/devsy/cmd/pro/flags" @@ -50,7 +51,7 @@ func (cmd *ProjectsCmd) Run(ctx context.Context) error { return err } - fmt.Println(string(out)) + _, _ = fmt.Fprintln(os.Stdout, string(out)) return nil } diff --git a/cmd/pro/provider/list/templates.go b/cmd/pro/provider/list/templates.go index 866234cbdc..3aa9f44dae 100644 --- a/cmd/pro/provider/list/templates.go +++ b/cmd/pro/provider/list/templates.go @@ -62,7 +62,7 @@ func (cmd *TemplatesCmd) Run(ctx context.Context) error { if err != nil { return err } - fmt.Println(string(out)) + _, _ = fmt.Fprintln(os.Stdout, string(out)) return nil } diff --git a/cmd/pro/provider/list/workspaces.go b/cmd/pro/provider/list/workspaces.go index 53efff5b2a..ff848ae7d3 100644 --- a/cmd/pro/provider/list/workspaces.go +++ b/cmd/pro/provider/list/workspaces.go @@ -81,7 +81,7 @@ func (cmd *WorkspacesCmd) Run(ctx context.Context) error { if err != nil { return fmt.Errorf("marshal workspaces: %w", err) } - fmt.Println(string(wBytes)) + _, _ = fmt.Fprintln(os.Stdout, string(wBytes)) return nil } diff --git a/cmd/pro/provider/rebuild.go b/cmd/pro/provider/rebuild.go index fc3e8a9b14..434270151e 100644 --- a/cmd/pro/provider/rebuild.go +++ b/cmd/pro/provider/rebuild.go @@ -78,9 +78,7 @@ func (cmd *RebuildCmd) Run(ctx context.Context, args []string) error { _, err = remotecommand.ExecuteConn( ctx, conn, - os.Stdin, - os.Stdout, - os.Stderr, + remotecommand.Streams{Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr}, ) if err != nil { return fmt.Errorf("error executing: %w", err) diff --git a/cmd/pro/provider/status.go b/cmd/pro/provider/status.go index 4aabc8b494..ea1a76eee1 100644 --- a/cmd/pro/provider/status.go +++ b/cmd/pro/provider/status.go @@ -69,7 +69,7 @@ func (cmd *StatusCmd) Run( return err } - fmt.Println(string(out)) + _, _ = fmt.Fprintln(os.Stdout, string(out)) return nil } @@ -86,9 +86,7 @@ func (cmd *StatusCmd) Run( _, err = remotecommand.ExecuteConn( ctx, conn, - stdin, - stdout, - stderr, + remotecommand.Streams{Stdin: stdin, Stdout: stdout, Stderr: stderr}, ) if err != nil { return fmt.Errorf("error executing: %w", err) diff --git a/cmd/pro/provider/up.go b/cmd/pro/provider/up.go index d3afc68594..13a42a4bae 100644 --- a/cmd/pro/provider/up.go +++ b/cmd/pro/provider/up.go @@ -118,9 +118,11 @@ func (cmd *UpCmd) up( _, err = remotecommand.ExecuteConn( ctx, conn, - cmd.streams.Stdin, - cmd.streams.Stdout, - cmd.streams.Stderr, + remotecommand.Streams{ + Stdin: cmd.streams.Stdin, + Stdout: cmd.streams.Stdout, + Stderr: cmd.streams.Stderr, + }, ) if err != nil { return fmt.Errorf("error executing: %w", err) diff --git a/cmd/pro/start.go b/cmd/pro/start.go index 8e4650651f..c4a3aeb4c1 100644 --- a/cmd/pro/start.go +++ b/cmd/pro/start.go @@ -41,10 +41,16 @@ import ( "k8s.io/kubectl/pkg/util/term" ) +const ( + adminValue = "admin" + namespaceValue = "--namespace" + kubeContextValue = "--kube-context" +) + const ( DevsyRouterDomainSecret = "loft-router-domain" // #nosec G101 passwordChangedHint = "(has been changed)" - defaultUser = "admin" + defaultUser = adminValue ingressNginx = "ingress-nginx" helmRepositoryConfig = "--repository-config=''" @@ -269,10 +275,12 @@ func (cmd *StartCmd) retryUpgradeAfterPurge( manifests, err := getReleaseManifests( ctx, - chartName, - chartRepo, - cmd.Context, - cmd.Namespace, + helmTarget{ + chartName: chartName, + chartRepo: chartRepo, + kubeContext: cmd.Context, + namespace: cmd.Namespace, + }, extraArgs, ) if err != nil { @@ -284,10 +292,12 @@ func (cmd *StartCmd) retryUpgradeAfterPurge( // Retry Devsy installation err = upgradeRelease( ctx, - chartName, - chartRepo, - cmd.Context, - cmd.Namespace, + helmTarget{ + chartName: chartName, + chartRepo: chartRepo, + kubeContext: cmd.Context, + namespace: cmd.Namespace, + }, extraArgs, ) if err != nil { @@ -321,10 +331,12 @@ func (cmd *StartCmd) upgrade(ctx context.Context) error { err = upgradeRelease( ctx, - chartName, - chartRepo, - cmd.Context, - cmd.Namespace, + helmTarget{ + chartName: chartName, + chartRepo: chartRepo, + kubeContext: cmd.Context, + namespace: cmd.Namespace, + }, extraArgs, ) if err != nil { @@ -348,7 +360,7 @@ func (cmd *StartCmd) purgeManifests(ctx context.Context, manifests string) { kubectlDelete := exec.CommandContext(ctx, // #nosec G204 -- args are internally constructed "kubectl", "delete", "-f", "-", "--context", cmd.Context, - "--namespace", cmd.Namespace, + namespaceValue, cmd.Namespace, "--ignore-not-found=true", "--grace-period=0", "--force", ) @@ -469,7 +481,7 @@ func (cmd *StartCmd) printRemoteSuccessMessage(host string) { ########################## LOGIN ############################ -Username: `+greenBold("admin")+` +Username: `+greenBold(adminValue)+` Password: `+greenBold(password)+` # Change via UI or via: `+greenBold("devsy platform user reset-password")+` Login via UI: %s @@ -549,7 +561,7 @@ func (cmd *StartCmd) successLocal() error { ########################## LOGIN ############################ -Username: `+greenBold("admin")+` +Username: `+greenBold(adminValue)+` Password: `+greenBold(password)+` # Change via UI or via: `+greenBold("devsy platform user reset-password")+` Login via UI: %s @@ -891,7 +903,7 @@ func (cmd *StartCmd) pingDevsyRouter(ctx context.Context, devsyPod *corev1.Pod) httpClient := &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{ - InsecureSkipVerify: true, + InsecureSkipVerify: true, //nolint:gosec // self-signed platform cert }, }, } @@ -946,7 +958,7 @@ func (cmd *StartCmd) successDevsyRouter(url string) error { ########################## LOGIN ############################ -Username: `+greenBold("admin")+` +Username: `+greenBold(adminValue)+` Password: `+greenBold(password)+` # Change via UI or via: `+greenBold("devsy platform user reset-password")+` Login via UI: %s @@ -1003,13 +1015,15 @@ func (cmd *StartCmd) loginViaCLI(url string) error { Username: defaultUser, Password: cmd.Password, } - loginRequestBytes, err := json.Marshal(loginRequest) + loginRequestBytes, err := json.Marshal(loginRequest) //nolint:gosec // login request body if err != nil { return err } loginRequestBuf := bytes.NewBuffer(loginRequestBytes) tr := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, //nolint:gosec // existing behaviour for self-signed platform endpoints + }, //nolint:gosec // self-signed platform cert } httpClient := &http.Client{Transport: tr} @@ -1203,9 +1217,9 @@ func installNginxIngress( helmRepositoryConfig, "--repo", "https://kubernetes.github.io/ingress-nginx", - "--kube-context", + kubeContextValue, kubeContext, - "--namespace", + namespaceValue, ingressNginx, "--create-namespace", "--set-string", @@ -1287,19 +1301,19 @@ func ensureAdminPassword( return false, err } - admin, err := devsyClient.StorageV1().Users().Get(ctx, "admin", metav1.GetOptions{}) + admin, err := devsyClient.StorageV1().Users().Get(ctx, adminValue, metav1.GetOptions{}) switch { case err != nil && !kerrors.IsNotFound(err): return false, err case admin == nil: admin, err = devsyClient.StorageV1().Users().Create(ctx, &storagev1.User{ ObjectMeta: metav1.ObjectMeta{ - Name: "admin", + Name: adminValue, }, Spec: storagev1.UserSpec{ - Username: "admin", + Username: adminValue, Email: "test@domain.tld", - Subject: "admin", + Subject: adminValue, Groups: []string{"system:masters"}, PasswordRef: &storagev1.SecretRef{ SecretName: "loft-user-secret-admin", @@ -1356,7 +1370,7 @@ func ensureAdminPasswordSecret( return false, nil } - // create the password secret if it was not found, this can happen if you delete the loft namespace without deleting the admin user + // recreate the password secret if missing, e.g. after the loft namespace was deleted without the admin user secret = &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: ref.SecretName, @@ -1418,7 +1432,9 @@ func isHostReachable(ctx context.Context, host string) (bool, error) { transport := http.DefaultTransport.(*http.Transport).Clone() // we disable http2 as Kubernetes has problems with this transport.ForceAttemptHTTP2 = false - transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + transport.TLSClientConfig = &tls.Config{ + InsecureSkipVerify: true, //nolint:gosec // existing behaviour for self-signed platform endpoints + } //nolint:gosec // self-signed platform cert // wait until devsy is reachable at the given url client := &http.Client{Transport: transport} url := "https://" + host + "/version" @@ -1457,26 +1473,34 @@ func isHostReachable(ctx context.Context, host string) (bool, error) { return false, nil } +// helmTarget names the chart and the cluster a helm call acts on. +type helmTarget struct { + chartName string + chartRepo string + kubeContext string + namespace string +} + func upgradeRelease( ctx context.Context, - chartName, chartRepo, kubeContext, namespace string, + target helmTarget, extraArgs []string, ) error { // now we install devsy args := []string{ "upgrade", defaultReleaseName, - chartName, + target.chartName, "--install", "--create-namespace", helmRepositoryConfig, - "--kube-context", - kubeContext, - "--namespace", - namespace, + kubeContextValue, + target.kubeContext, + namespaceValue, + target.namespace, } - if chartRepo != "" { - args = append(args, "--repo", chartRepo) + if target.chartRepo != "" { + args = append(args, "--repo", target.chartRepo) } args = append(args, extraArgs...) @@ -1487,8 +1511,8 @@ func upgradeRelease( ctx, "helm", args...) // #nosec G204 -- helm args are constructed internally - if chartRepo != "" { - helmWorkDir, err := getHelmWorkdir(chartName) + if target.chartRepo != "" { + helmWorkDir, err := getHelmWorkdir(target.chartName) if err != nil { return err } @@ -1506,21 +1530,21 @@ func upgradeRelease( func getReleaseManifests( ctx context.Context, - chartName, chartRepo, kubeContext, namespace string, + target helmTarget, extraArgs []string, ) (string, error) { args := []string{ "template", defaultReleaseName, - chartName, + target.chartName, helmRepositoryConfig, - "--kube-context", - kubeContext, - "--namespace", - namespace, + kubeContextValue, + target.kubeContext, + namespaceValue, + target.namespace, } - if chartRepo != "" { - args = append(args, "--repo", chartRepo) + if target.chartRepo != "" { + args = append(args, "--repo", target.chartRepo) } args = append(args, extraArgs...) @@ -1528,8 +1552,8 @@ func getReleaseManifests( ctx, "helm", args...) // #nosec G204 -- helm args are constructed internally - if chartRepo != "" { - helmWorkDir, err := getHelmWorkdir(chartName) + if target.chartRepo != "" { + helmWorkDir, err := getHelmWorkdir(target.chartName) if err != nil { return "", err } diff --git a/cmd/pro/update_provider.go b/cmd/pro/update_provider.go index 996f7cc3ef..8ada3cdc61 100644 --- a/cmd/pro/update_provider.go +++ b/cmd/pro/update_provider.go @@ -42,10 +42,10 @@ func NewUpdateProviderCmd(globalFlags *flags.GlobalFlags) *cobra.Command { } func (cmd *UpdateProviderCmd) Run(ctx context.Context, args []string) error { - if len(args) != 1 { - return fmt.Errorf("new version is missing") + newVersion, err := newVersionArg(args) + if err != nil { + return err } - newVersion := args[0] devsyConfig, err := config.LoadConfig(cmd.Context, cmd.Provider) if err != nil { @@ -64,18 +64,28 @@ func (cmd *UpdateProviderCmd) Run(ctx context.Context, args []string) error { return err } defer unlock() - devsyConfig, err = config.LoadConfig(devsyConfig.DefaultContext, cmd.Provider) - if err != nil { + devsyConfig, provider, err = cmd.reloadProvider(ctx, devsyConfig.DefaultContext) + if err != nil || provider == nil { return err } - provider, err = workspace.ProviderFromHost(ctx, devsyConfig, cmd.Host) - if err != nil { - return fmt.Errorf("reload provider: %w", err) - } - if provider.Source.Internal { - return nil + + return cmd.applyVersion(ctx, devsyConfig, provider, newVersion) +} + +func newVersionArg(args []string) (string, error) { + if len(args) != 1 { + return "", fmt.Errorf("new version is missing") } + return args[0], nil +} + +func (cmd *UpdateProviderCmd) applyVersion( + ctx context.Context, + devsyConfig *config.Config, + provider *provider2.ProviderConfig, + newVersion string, +) error { providerSource, err := resolveNewProviderSource(devsyConfig, provider.Name, newVersion) if err != nil { return err @@ -106,6 +116,27 @@ func (cmd *UpdateProviderCmd) Run(ctx context.Context, args []string) error { return nil } +// reloadProvider reads the config and provider again under the provider lock. +// A nil provider with a nil error means the provider is internal and needs no +// update. +func (cmd *UpdateProviderCmd) reloadProvider( + ctx context.Context, + contextName string, +) (*config.Config, *provider2.ProviderConfig, error) { + devsyConfig, err := config.LoadConfig(contextName, cmd.Provider) + if err != nil { + return nil, nil, err + } + provider, err := workspace.ProviderFromHost(ctx, devsyConfig, cmd.Host) + if err != nil { + return nil, nil, fmt.Errorf("reload provider: %w", err) + } + if provider.Source.Internal { + return nil, nil, nil + } + return devsyConfig, provider, nil +} + func resolveNewProviderSource( devsyConfig *config.Config, providerName, newVersion string, diff --git a/cmd/pro/workspace/import.go b/cmd/pro/workspace/import.go index 7154c0d44a..5154113082 100644 --- a/cmd/pro/workspace/import.go +++ b/cmd/pro/workspace/import.go @@ -257,12 +257,11 @@ func (cmd *ImportCmd) writeWorkspaceDefinition( devsyConfig, err := options.ResolveOptions( context.Background(), - devsyConfig, - provider, - instanceOpts, - false, - false, - nil, + options.ResolveOptionsRequest{ + DevConfig: devsyConfig, + ProviderConfig: provider, + UserOptions: instanceOpts, + }, ) if err != nil { return fmt.Errorf("resolve options: %w", err) diff --git a/cmd/pro/workspace/rebuild.go b/cmd/pro/workspace/rebuild.go index 2177862a64..be2a33ed22 100644 --- a/cmd/pro/workspace/rebuild.go +++ b/cmd/pro/workspace/rebuild.go @@ -98,9 +98,7 @@ func execRebuild( _, err = remotecommand.ExecuteConn( ctx, conn, - os.Stdin, - os.Stdout, - os.Stderr, + remotecommand.Streams{Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr}, ) if err != nil { return fmt.Errorf("error executing: %w", err) diff --git a/cmd/provider/add.go b/cmd/provider/add.go index 5bde1e79b1..2b2c602d28 100644 --- a/cmd/provider/add.go +++ b/cmd/provider/add.go @@ -95,7 +95,7 @@ func (cmd *AddCmd) Run(ctx context.Context, devsyConfig *config.Config, args []s reporter, err := newStatusReporter( cmd.ResultFormat, os.Stdout, - cmd.Verbosity > 0 || cmd.Debug, + cmd.verbose(), cmd.NoStatus, ) if err != nil { @@ -107,41 +107,17 @@ func (cmd *AddCmd) Run(ctx context.Context, devsyConfig *config.Config, args []s } providerName = resolved.name - names := []string{providerName} - if cmd.FromExisting != "" && cmd.FromExisting != providerName { - names = append(names, cmd.FromExisting) - sort.Strings(names) - } - for _, name := range names { - unlock, lockErr := provider.LockProviderNamed(devsyConfig.DefaultContext, name) - if lockErr != nil { - return lockErr - } - defer unlock() + unlockProviders, err := cmd.lockProviders(devsyConfig, providerName) + if err != nil { + return err } + defer unlockProviders() devsyConfig, err = config.LoadConfig(devsyConfig.DefaultContext, cmd.Provider) if err != nil { return err } - var providerConfig *provider.ProviderConfig - var options []string - err = status.Run( - ctx, - reporter, - status.Operation{Phase: status.PhaseInstallingProvider, Step: providerName}, - func(ctx context.Context) error { - var resolveErr error - providerConfig, options, resolveErr = cmd.resolveProviderConfig( - ctx, - devsyConfig, - providerName, - resolved.raw, - resolved.source, - ) - return resolveErr - }, - ) + providerConfig, options, err := cmd.install(ctx, reporter, devsyConfig, resolved) if err != nil { return err } @@ -158,9 +134,44 @@ func (cmd *AddCmd) Run(ctx context.Context, devsyConfig *config.Config, args []s reporter, status.Operation{Phase: status.PhaseReady, Step: providerConfig.Name}, func(ctx context.Context) error { - return cmd.useProvider(ctx, devsyConfig, providerConfig, options, reporter) + return cmd.useProvider( + ctx, + devsyConfig, + installedProvider{config: providerConfig, options: options}, + reporter, + ) + }, + ) +} + +func (cmd *AddCmd) install( + ctx context.Context, + reporter status.Reporter, + devsyConfig *config.Config, + resolved resolvedAddSource, +) (*provider.ProviderConfig, []string, error) { + var providerConfig *provider.ProviderConfig + var options []string + err := status.Run( + ctx, + reporter, + status.Operation{Phase: status.PhaseInstallingProvider, Step: resolved.name}, + func(ctx context.Context) error { + var resolveErr error + providerConfig, options, resolveErr = cmd.resolveProviderConfig( + ctx, + devsyConfig, + resolved, + ) + return resolveErr }, ) + + return providerConfig, options, err +} + +func (cmd *AddCmd) verbose() bool { + return cmd.Verbosity > 0 || cmd.Debug } type resolvedAddSource struct { @@ -217,10 +228,9 @@ func validateOptionalProviderName(providerName string) error { func (cmd *AddCmd) resolveProviderConfig( ctx context.Context, devsyConfig *config.Config, - providerName string, - raw []byte, - source *provider.ProviderSource, + resolved resolvedAddSource, ) (*provider.ProviderConfig, []string, error) { + providerName, raw, source := resolved.name, resolved.raw, resolved.source if cmd.FromExisting != "" { if devsyConfig.Current() == nil || devsyConfig.Current().Providers[cmd.FromExisting] == nil { @@ -255,13 +265,19 @@ func (cmd *AddCmd) resolveProviderConfig( return c, cmd.Options, nil } +// installedProvider is a provider that was installed, with the options it used. +type installedProvider struct { + config *provider.ProviderConfig + options []string +} + func (cmd *AddCmd) useProvider( ctx context.Context, devsyConfig *config.Config, - providerConfig *provider.ProviderConfig, - options []string, + installed installedProvider, reporter status.Reporter, ) error { + providerConfig, options := installed.config, installed.options configureErr := ConfigureProvider(ctx, ProviderOptionsConfig{ Provider: providerConfig, ContextName: devsyConfig.DefaultContext, @@ -276,7 +292,12 @@ func (cmd *AddCmd) useProvider( return err } - err = DeleteProvider(ctx, devsyConfig, providerConfig.Name, true, true) + err = DeleteProvider( + ctx, + devsyConfig, + providerConfig.Name, + DeleteOptions{IgnoreNotFound: true, Force: true}, + ) if err != nil { return fmt.Errorf("delete provider: %w", err) } @@ -328,3 +349,28 @@ func getUserOption(allOptions []string, optionKey string) (string, bool) { return "", false } + +// lockProviders locks the new provider and the one it copies from, in name +// order. The returned function releases every lock acquired so far. +func (cmd *AddCmd) lockProviders(devsyConfig *config.Config, providerName string) (func(), error) { + names := []string{providerName} + if cmd.FromExisting != "" && cmd.FromExisting != providerName { + names = append(names, cmd.FromExisting) + sort.Strings(names) + } + var unlocks []func() + release := func() { + for _, unlock := range unlocks { + unlock() + } + } + for _, name := range names { + unlock, err := provider.LockProviderNamed(devsyConfig.DefaultContext, name) + if err != nil { + release() + return nil, err + } + unlocks = append(unlocks, unlock) + } + return release, nil +} diff --git a/cmd/provider/configure_shared.go b/cmd/provider/configure_shared.go index 5a5d51d317..41ced50911 100644 --- a/cmd/provider/configure_shared.go +++ b/cmd/provider/configure_shared.go @@ -96,8 +96,8 @@ func configureProviderOptions( func(ctx context.Context) error { var resolveErr error resolvedConfig, resolveErr = options2.ResolveOptions( - ctx, devsyConfig, cfg.Provider, options, - cfg.SkipRequired, cfg.SkipSubOptions, cfg.SingleMachine, + ctx, + cfg.resolveRequest(devsyConfig, options), ) return resolveErr }, @@ -153,9 +153,8 @@ func runProviderInit( func(ctx context.Context) error { return initProvider( ctx, - devsyConfig.DefaultContext, + devsyConfig, cfg.Provider, - devsyConfig.ProviderOptions(cfg.Provider.Name), initIO{stdout: stdout, stderr: stderr}, ) }, @@ -178,6 +177,21 @@ func applyResolvedOptions(contextName, providerName string, resolvedConfig *conf }) } +// resolveRequest builds the option resolution request for this provider. +func (cfg ProviderOptionsConfig) resolveRequest( + devsyConfig *config.Config, + options map[string]string, +) options2.ResolveOptionsRequest { + return options2.ResolveOptionsRequest{ + DevConfig: devsyConfig, + ProviderConfig: cfg.Provider, + UserOptions: options, + SkipRequired: cfg.SkipRequired, + SkipSubOptions: cfg.SkipSubOptions, + SingleMachine: cfg.SingleMachine, + } +} + func parseAndMergeOptions( cfg ProviderOptionsConfig, devsyConfig *config.Config, @@ -236,11 +250,13 @@ type initIO struct { func initProvider( ctx context.Context, - contextName string, + devsyConfig *config.Config, provider *provider2.ProviderConfig, - providerOptions map[string]config.OptionValue, io2 initIO, ) error { + contextName, providerOptions := devsyConfig.DefaultContext, devsyConfig.ProviderOptions( + provider.Name, + ) lock, err := provider2.GetProviderInitLock(contextName, provider.Name) if err != nil { return fmt.Errorf("get init lock: %w", err) diff --git a/cmd/provider/delete.go b/cmd/provider/delete.go index a9a5d61482..22706e71aa 100644 --- a/cmd/provider/delete.go +++ b/cmd/provider/delete.go @@ -38,14 +38,20 @@ func NewDeleteCmd(flags *flags.GlobalFlags) *cobra.Command { RunE: func(cobraCmd *cobra.Command, args []string) error { return cmd.Run(cobraCmd.Context(), args) }, - ValidArgsFunction: func(rootCmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + ValidArgsFunction: func( + rootCmd *cobra.Command, + args []string, + toComplete string, + ) ([]string, cobra.ShellCompDirective) { return completion.GetProviderSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, } @@ -85,7 +91,12 @@ func (cmd *DeleteCmd) Run(ctx context.Context, args []string) error { } // delete the provider - err = DeleteProvider(ctx, devsyConfig, provider, cmd.IgnoreNotFound, cmd.Force) + err = DeleteProvider( + ctx, + devsyConfig, + provider, + DeleteOptions{IgnoreNotFound: cmd.IgnoreNotFound, Force: cmd.Force}, + ) if err != nil { return err } @@ -94,12 +105,19 @@ func (cmd *DeleteCmd) Run(ctx context.Context, args []string) error { return nil } +// DeleteOptions controls DeleteProvider. +type DeleteOptions struct { + IgnoreNotFound bool + Force bool +} + func DeleteProvider( ctx context.Context, devsyConfig *config.Config, provider string, - ignoreNotFound, force bool, + opts DeleteOptions, ) error { + ignoreNotFound, force := opts.IgnoreNotFound, opts.Force // if force is not set, check if the provider is associated with a platform instance or workspace if !force { // check if this provider is associated with a platform instance diff --git a/cmd/provider/get.go b/cmd/provider/get.go index d6dc0a4043..17c92d2b13 100644 --- a/cmd/provider/get.go +++ b/cmd/provider/get.go @@ -39,14 +39,20 @@ func NewGetCmd(flags *flags.GlobalFlags) *cobra.Command { RunE: func(cobraCmd *cobra.Command, args []string) error { return cmd.Run(cobraCmd.Context(), args) }, - ValidArgsFunction: func(rootCmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + ValidArgsFunction: func( + rootCmd *cobra.Command, + args []string, + toComplete string, + ) ([]string, cobra.ShellCompDirective) { return completion.GetProviderSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, } diff --git a/cmd/provider/init.go b/cmd/provider/init.go index 9a50ad5714..fe9b208b43 100644 --- a/cmd/provider/init.go +++ b/cmd/provider/init.go @@ -30,54 +30,7 @@ func NewInitCmd(f *flags.GlobalFlags) *cobra.Command { Short: "Initialize a provider and resolve its options", Args: cobra.MaximumNArgs(1), RunE: func(cobraCmd *cobra.Command, args []string) error { - devsyConfig, err := config.LoadConfig(cmd.Context, cmd.Provider) - if err != nil { - return err - } - name, err := resolveProviderName(args, devsyConfig.Current().DefaultProvider) - if err != nil { - return err - } - - unlock, err := provider2.LockProvider(devsyConfig.DefaultContext, name) - if err != nil { - return err - } - defer unlock() - - devsyConfig, err = config.LoadConfig(devsyConfig.DefaultContext, cmd.Provider) - if err != nil { - return err - } - p, err := workspace.FindProvider(devsyConfig, name) - if err != nil { - return err - } - - reporter, err := newStatusReporter( - cmd.ResultFormat, - os.Stdout, - cmd.Verbosity > 0 || cmd.Debug, - cmd.NoStatus, - ) - if err != nil { - return err - } - return status.Run( - cobraCmd.Context(), reporter, - status.Operation{Phase: status.PhaseReady, Step: name}, - func(ctx context.Context) error { - return ConfigureProvider(ctx, ProviderOptionsConfig{ - Provider: p.Config, - ContextName: devsyConfig.DefaultContext, - UserOptions: cmd.Options, - DiscardPriorValues: cmd.Reset, - SkipInit: cmd.SkipInit, - SingleMachine: &cmd.SingleMachine, - Reporter: reporter, - }) - }, - ) + return cmd.run(cobraCmd.Context(), args) }, ValidArgsFunction: func( rootCmd *cobra.Command, @@ -86,14 +39,67 @@ func NewInitCmd(f *flags.GlobalFlags) *cobra.Command { ) ([]string, cobra.ShellCompDirective) { return completion.GetProviderSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, } cmd.registerFlags(initCmd) return initCmd } + +func (cmd *InitCmd) run(ctx context.Context, args []string) error { + devsyConfig, err := config.LoadConfig(cmd.Context, cmd.Provider) + if err != nil { + return err + } + name, err := resolveProviderName(args, devsyConfig.Current().DefaultProvider) + if err != nil { + return err + } + + unlock, err := provider2.LockProvider(devsyConfig.DefaultContext, name) + if err != nil { + return err + } + defer unlock() + + devsyConfig, err = config.LoadConfig(devsyConfig.DefaultContext, cmd.Provider) + if err != nil { + return err + } + p, err := workspace.FindProvider(devsyConfig, name) + if err != nil { + return err + } + + reporter, err := newStatusReporter( + cmd.ResultFormat, + os.Stdout, + cmd.Verbosity > 0 || cmd.Debug, + cmd.NoStatus, + ) + if err != nil { + return err + } + return status.Run( + ctx, reporter, + status.Operation{Phase: status.PhaseReady, Step: name}, + func(ctx context.Context) error { + return ConfigureProvider(ctx, ProviderOptionsConfig{ + Provider: p.Config, + ContextName: devsyConfig.DefaultContext, + UserOptions: cmd.Options, + DiscardPriorValues: cmd.Reset, + SkipInit: cmd.SkipInit, + SingleMachine: &cmd.SingleMachine, + Reporter: reporter, + }) + }, + ) +} diff --git a/cmd/provider/rename.go b/cmd/provider/rename.go index be2e909b3a..137da648a4 100644 --- a/cmd/provider/rename.go +++ b/cmd/provider/rename.go @@ -49,40 +49,54 @@ func (cmd *RenameCmd) Run(ctx context.Context, args []string) error { return err } + devsyConfig, unlock, err := cmd.lockAndReload(oldName, newName) + if err != nil { + return err + } + defer unlock() + + if err := validateProviderRename(devsyConfig, oldName); err != nil { + return err + } + + if devsyConfig.Current().Providers[newName] != nil { + return fmt.Errorf("provider %s already exists", newName) + } + + return renameProvider(ctx, devsyConfig, oldName, newName) +} + +// lockAndReload locks both providers in a stable order and reloads the config +// under the locks. The returned func releases both locks. +func (cmd *RenameCmd) lockAndReload(oldName, newName string) (*config.Config, func(), error) { names := []string{oldName, newName} sort.Strings(names) devsyConfig, err := config.LoadConfig(cmd.Context, cmd.Provider) if err != nil { - return err + return nil, nil, err } unlock1, err := provider.LockProviderNamed(devsyConfig.DefaultContext, names[0]) if err != nil { - return err + return nil, nil, err } - defer unlock1() - unlock2, err := provider.LockProviderNamed(devsyConfig.DefaultContext, names[1]) if err != nil { - return err + unlock1() + return nil, nil, err + } + unlock := func() { + unlock2() + unlock1() } - defer unlock2() devsyConfig, err = config.LoadConfig(devsyConfig.DefaultContext, cmd.Provider) if err != nil { - return err + unlock() + return nil, nil, err } - - if err := validateProviderRename(devsyConfig, oldName); err != nil { - return err - } - - if devsyConfig.Current().Providers[newName] != nil { - return fmt.Errorf("provider %s already exists", newName) - } - - return renameProvider(ctx, devsyConfig, oldName, newName) + return devsyConfig, unlock, nil } // validateProviderName checks that the given name is non-empty, matches the @@ -178,7 +192,8 @@ func switchMachines(machines []*provider.Machine, newName string) ([]*provider.M return switched, nil } -// setDefaultProvider updates the default provider setting if it currently +// setDefaultProvider points the default provider at newName when it currently +// equals oldName. func setDefaultProvider(devsyConfig *config.Config, oldName, newName string) (bool, error) { if devsyConfig.Current().DefaultProvider != oldName { return false, nil diff --git a/cmd/provider/set.go b/cmd/provider/set.go index 43239af78e..18d98b992b 100644 --- a/cmd/provider/set.go +++ b/cmd/provider/set.go @@ -38,14 +38,20 @@ func NewSetCmd(f *flags.GlobalFlags) *cobra.Command { RunE: func(cobraCmd *cobra.Command, args []string) error { return cmd.Run(cobraCmd.Context(), args) }, - ValidArgsFunction: func(rootCmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + ValidArgsFunction: func( + rootCmd *cobra.Command, + args []string, + toComplete string, + ) ([]string, cobra.ShellCompDirective) { return completion.GetProviderSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, } diff --git a/cmd/provider/set_source.go b/cmd/provider/set_source.go index bc32f7408b..6a4de6b95c 100644 --- a/cmd/provider/set_source.go +++ b/cmd/provider/set_source.go @@ -77,14 +77,20 @@ func (cmd *SetSourceCmd) Run(ctx context.Context, devsyConfig *config.Config, ar return cmd.runPinVersion(ctx, devsyConfig, args) } - if len(args) != 1 && len(args) != 2 { - return fmt.Errorf("specify either a local file, URL or Git repository. " + - "E.g. devsy provider set-source my-provider " + config.ProviderPrefix + "gcloud") - } + return cmd.setSource(ctx, devsyConfig, providerName, args) +} - providerSource := "" - if len(args) == 2 { - providerSource = args[1] +// setSource points the provider at the source in args and optionally activates it. +// The caller holds the provider lock. +func (cmd *SetSourceCmd) setSource( + ctx context.Context, + devsyConfig *config.Config, + providerName string, + args []string, +) error { + providerSource, err := sourceArg(args) + if err != nil { + return err } reporter, err := newStatusReporter( @@ -97,34 +103,65 @@ func (cmd *SetSourceCmd) Run(ctx context.Context, devsyConfig *config.Config, ar return err } + providerConfig, err := updateProviderSource(ctx, reporter, devsyConfig, providerSourceChange{ + name: providerName, + source: providerSource, + }) + if err != nil { + return err + } + + log.Infof("updated provider: providerName=%s", providerConfig.Name) + if !cmd.Use { + log.Infof("to initialize the provider, run: devsy provider init %s", providerConfig.Name) + // No PhaseReady: not ready until a following `provider init` runs. + return nil + } + + return cmd.activateProvider(ctx, devsyConfig, providerConfig, reporter) +} + +func sourceArg(args []string) (string, error) { + switch len(args) { + case 1: + return "", nil + case 2: + return args[1], nil + default: + return "", fmt.Errorf("specify either a local file, URL or Git repository. " + + "E.g. devsy provider set-source my-provider " + config.ProviderPrefix + "gcloud") + } +} + +// providerSourceChange names a provider and the source to switch it to. +type providerSourceChange struct { + name, source string +} + +func updateProviderSource( + ctx context.Context, + reporter status.Reporter, + devsyConfig *config.Config, + change providerSourceChange, +) (*provider2.ProviderConfig, error) { + providerName, providerSource := change.name, change.source var providerConfig *provider2.ProviderConfig - err = status.Run( + err := status.Run( ctx, reporter, - status.Operation{Phase: status.PhaseInstallingProvider, Step: args[0]}, + status.Operation{Phase: status.PhaseInstallingProvider, Step: providerName}, func(ctx context.Context) error { var updateErr error providerConfig, updateErr = workspace.UpdateProvider( ctx, devsyConfig, - args[0], + providerName, providerSource, ) return updateErr }, ) - if err != nil { - return err - } - - log.Infof("updated provider: providerName=%s", providerConfig.Name) - if !cmd.Use { - log.Infof("to initialize the provider, run: devsy provider init %s", providerConfig.Name) - // No PhaseReady: not ready until a following `provider init` runs. - return nil - } - - return cmd.activateProvider(ctx, devsyConfig, providerConfig, reporter) + return providerConfig, err } // activateProvider configures and activates a newly sourced provider, diff --git a/cmd/provider/use.go b/cmd/provider/use.go index 90b2778c3d..76b8bfb750 100644 --- a/cmd/provider/use.go +++ b/cmd/provider/use.go @@ -66,11 +66,13 @@ func NewUseCmd(f *flags.GlobalFlags) *cobra.Command { ) ([]string, cobra.ShellCompDirective) { return completion.GetProviderSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, } diff --git a/cmd/provider/versions.go b/cmd/provider/versions.go index 5536ea3dc1..48ddc55e6e 100644 --- a/cmd/provider/versions.go +++ b/cmd/provider/versions.go @@ -73,11 +73,13 @@ func completeProviderName( return func(rootCmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return completion.GetProviderSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) } } diff --git a/cmd/root.go b/cmd/root.go index 4603589de6..dab59e846e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -43,6 +43,7 @@ import ( "github.com/devsy-org/devsy/pkg/workspace" "github.com/go-logr/logr" "github.com/spf13/cobra" + "github.com/spf13/pflag" "golang.org/x/crypto/ssh" "golang.org/x/term" "k8s.io/klog/v2" @@ -310,32 +311,41 @@ func renderCLIError(cliErr *clierr.CLIError, machineMode bool) { } return } + writeCLIErrorText(cliErr) +} + +func writeCLIErrorText(cliErr *clierr.CLIError) { redactor := secrets.NewEnvironmentRedactor(os.Environ()) fmt.Fprintf(os.Stderr, "Error: %s\n", redactor.Redact(cliErr.Message)) if cliErr.Code != "" && cliErr.Code != clierr.CodeUnknown { fmt.Fprintf(os.Stderr, "Error code: %s\n", redactor.Redact(string(cliErr.Code))) } - if len(cliErr.Context) > 0 { - fmt.Fprintln(os.Stderr, "Context:") - keys := make([]string, 0, len(cliErr.Context)) - for key := range cliErr.Context { - keys = append(keys, key) - } - slices.Sort(keys) - for _, key := range keys { - fmt.Fprintf( - os.Stderr, - " %s: %s\n", - redactor.Redact(key), - redactor.Redact(cliErr.Context[key]), - ) - } - } + writeCLIErrorContext(cliErr.Context, redactor) if cliErr.Hint != "" { fmt.Fprintf(os.Stderr, "Try: %s\n", redactor.Redact(cliErr.Hint)) } } +func writeCLIErrorContext(errContext map[string]string, redactor *secrets.Redactor) { + if len(errContext) == 0 { + return + } + fmt.Fprintln(os.Stderr, "Context:") + keys := make([]string, 0, len(errContext)) + for key := range errContext { + keys = append(keys, key) + } + slices.Sort(keys) + for _, key := range keys { + fmt.Fprintf( + os.Stderr, + " %s: %s\n", + redactor.Redact(key), + redactor.Redact(errContext[key]), + ) + } +} + // BuildRoot constructs the root command and returns it alongside the parsed // global flags struct so callers (Execute, tests) can inspect parsed state // without reaching for package-level mutable state. @@ -357,54 +367,7 @@ func BuildRoot() (*cobra.Command, *flags.GlobalFlags) { _ = completion.RegisterFlagCompletionFuns(rootCmd, globalFlags) rootCmd.PersistentPreRunE = func(cobraCmd *cobra.Command, _ []string) error { - if err := validateLogLevel(globalFlags.LogLevel); err != nil { - return err - } - if globalFlags.DevsyHome != "" { - _ = os.Setenv(config.EnvHome, globalFlags.DevsyHome) - } - // A crashed deletion must be resumed before using or mutating its - // partially cleaned stores. This check reads only recovery metadata; - // the matching delete command validates and resumes under the config lock. - parent := cobraCmd.Parent() - isContextDelete := cobraCmd.Name() == "delete" && parent != nil && - parent.Name() == "context" - if !isContextDelete { - if err := config.CheckPendingContextDeletion(); err != nil { - return err - } - } - cobraCmd.SilenceUsage = true - logFormat := globalFlags.LogOutput - if logFormat == "" { - if isMachineConsumer("", topLevelCommand(cobraCmd) == internalCommand) { - logFormat = logOutputJSON - } else { - logFormat = logOutputText - } - } - - log.Init(log.Config{ - Verbosity: globalFlags.Verbosity, - Quiet: globalFlags.Quiet, - Debug: globalFlags.Debug, - Level: globalFlags.LogLevel, - DefaultLevel: defaultLogLevel(globalFlags), - VerbositySet: persistentFlags.Changed(names.Verbose), - Format: logFormat, - Redactor: secrets.NewEnvironmentRedactor(os.Environ()), - }) - klog.SetLogger(logr.New(log.LogrSink())) - - devsyConfig, err := config.LoadConfig(globalFlags.Context, globalFlags.Provider) - if err == nil { - current := telemetry.FromContext(cobraCmd.Context()) - cobraCmd.SetContext(telemetry.WithCollector( - cobraCmd.Context(), - telemetry.ApplyCLIConfig(devsyConfig, current), - )) - } - return nil + return rootPreRun(cobraCmd, globalFlags, persistentFlags) } rootCmd.PersistentPostRunE = func(_ *cobra.Command, _ []string) error { if globalFlags.DevsyHome != "" { @@ -441,3 +404,66 @@ func registerSubcommands(rootCmd *cobra.Command, globalFlags *flags.GlobalFlags) rootCmd.AddCommand(pro.NewPlatformCmd(globalFlags)) } } + +func rootPreRun( + cobraCmd *cobra.Command, + globalFlags *flags.GlobalFlags, + persistentFlags *pflag.FlagSet, +) error { + if err := validateLogLevel(globalFlags.LogLevel); err != nil { + return err + } + if globalFlags.DevsyHome != "" { + _ = os.Setenv(config.EnvHome, globalFlags.DevsyHome) + } + if err := checkPendingContextDeletion(cobraCmd); err != nil { + return err + } + cobraCmd.SilenceUsage = true + + log.Init(log.Config{ + Verbosity: globalFlags.Verbosity, + Quiet: globalFlags.Quiet, + Debug: globalFlags.Debug, + Level: globalFlags.LogLevel, + DefaultLevel: defaultLogLevel(globalFlags), + VerbositySet: persistentFlags.Changed(names.Verbose), + Format: resolveLogFormat(cobraCmd, globalFlags.LogOutput), + Redactor: secrets.NewEnvironmentRedactor(os.Environ()), + }) + klog.SetLogger(logr.New(log.LogrSink())) + + devsyConfig, err := config.LoadConfig(globalFlags.Context, globalFlags.Provider) + if err == nil { + current := telemetry.FromContext(cobraCmd.Context()) + cobraCmd.SetContext(telemetry.WithCollector( + cobraCmd.Context(), + telemetry.ApplyCLIConfig(devsyConfig, current), + )) + } + return nil +} + +// checkPendingContextDeletion blocks commands until a crashed deletion is +// resumed, because its stores are only partially cleaned. It reads only +// recovery metadata; the matching delete command validates and resumes under +// the config lock. +func checkPendingContextDeletion(cobraCmd *cobra.Command) error { + parent := cobraCmd.Parent() + isContextDelete := cobraCmd.Name() == "delete" && parent != nil && + parent.Name() == "context" + if isContextDelete { + return nil + } + return config.CheckPendingContextDeletion() +} + +func resolveLogFormat(cobraCmd *cobra.Command, logFormat string) string { + if logFormat != "" { + return logFormat + } + if isMachineConsumer("", topLevelCommand(cobraCmd) == internalCommand) { + return logOutputJSON + } + return logOutputText +} diff --git a/cmd/root_test.go b/cmd/root_test.go index ec19bccb49..5b54f789b7 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -19,6 +19,10 @@ import ( "github.com/stretchr/testify/require" ) +const ( + devsy = "devsy" +) + func TestExitCodeForError_WorkspaceNotFound(t *testing.T) { err := fmt.Errorf("get workspace: %w", workspace.ErrWorkspaceNotFound) assert.Equal(t, exitcode.Retryable, exitCodeForError(err, true)) @@ -211,7 +215,7 @@ func TestConfigureOutput_SilencesCobra(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Setenv(config.EnvUI, "") rootCmd, globalFlags := BuildRoot() - os.Args = append([]string{"devsy"}, tc.args...) + os.Args = append([]string{devsy}, tc.args...) machineMode := configureOutput(rootCmd, globalFlags, tc.isInternal) assert.Equal(t, tc.wantSilent, machineMode) assert.True(t, rootCmd.SilenceErrors) @@ -237,7 +241,7 @@ func TestConfigureOutput_SelectsDiagnosticLogFormat(t *testing.T) { t.Run(tc.name, func(t *testing.T) { rootCmd, globalFlags := BuildRoot() globalFlags.Verbosity = 1 - os.Args = []string{"devsy", flagLogOutput, tc.flag} + os.Args = []string{devsy, flagLogOutput, tc.flag} configureOutput(rootCmd, globalFlags, false) var sink bytes.Buffer @@ -295,7 +299,7 @@ func TestConfigureOutput_DefaultMachineModeUsesJSONLogs(t *testing.T) { t.Cleanup(func() { os.Args = originalArgs }) rootCmd, globalFlags := BuildRoot() globalFlags.Verbosity = 1 - os.Args = []string{"devsy", internalCommand} + os.Args = []string{devsy, internalCommand} assert.True(t, configureOutput(rootCmd, globalFlags, true)) var sink bytes.Buffer diff --git a/cmd/secrets/bind.go b/cmd/secrets/bind.go index 60fbf64e54..1b27a18c40 100644 --- a/cmd/secrets/bind.go +++ b/cmd/secrets/bind.go @@ -68,6 +68,24 @@ func verifySecretReference( return nil } +func rejectEnvVarConflict( + devsyConfig *config.Config, + contextName string, + ref secrets.SecretRef, +) error { + if ref.Source != secrets.LocalSourceName { + return nil + } + current := devsyConfig.Contexts[contextName] + if current != nil && slices.Contains(current.EnvVars, ref.Name) { + return fmt.Errorf( + "%q is attached as an environment variable; detach it before attaching it as a secret", + ref.Name, + ) + } + return nil +} + func (cmd *AttachCmd) Run(ctx context.Context, name string) error { ref, err := secrets.ParseRef(name) if err != nil { @@ -86,19 +104,18 @@ func (cmd *AttachCmd) Run(ctx context.Context, name string) error { } contextName := devsyConfig.DefaultContext - if ref.Source == secrets.LocalSourceName { - if current := devsyConfig.Contexts[contextName]; current != nil && - slices.Contains(current.EnvVars, ref.Name) { - return fmt.Errorf( - "%q is attached as an environment variable; detach it before attaching it as a secret", - ref.Name, - ) - } + if err := rejectEnvVarConflict(devsyConfig, contextName, ref); err != nil { + return err } if err := verifySecretReference(ctx, devsyConfig, ref); err != nil { return err } + return attachToContext(devsyConfig, contextName, canonical) +} +// attachToContext adds the canonical secret reference to the context and saves +// the config. Attaching a secret that is already attached is a no-op. +func attachToContext(devsyConfig *config.Config, contextName, canonical string) error { ctxConfig := devsyConfig.Contexts[contextName] if ctxConfig == nil { return fmt.Errorf("context %q doesn't exist", contextName) diff --git a/cmd/secrets/set.go b/cmd/secrets/set.go index 19f2c6253c..6fd722a4bf 100644 --- a/cmd/secrets/set.go +++ b/cmd/secrets/set.go @@ -74,6 +74,12 @@ func (cmd *SetCmd) Run(_ context.Context, name string) error { return err } defer unlock() + + return cmd.setLocked(name, value) +} + +// setLocked writes the secret while the config lock is held. +func (cmd *SetCmd) setLocked(name, value string) error { devsyConfig, err := config.LoadConfig(cmd.Context, "") if err != nil { return err @@ -83,12 +89,8 @@ func (cmd *SetCmd) Run(_ context.Context, name string) error { if err != nil { return err } - if current := devsyConfig.Contexts[contextName]; current != nil && - slices.Contains(current.EnvVars, name) { - return fmt.Errorf( - "%q is attached as an environment variable; detach it before converting it to a secret", - name, - ) + if err := rejectEnvVarSecret(devsyConfig, contextName, name); err != nil { + return err } envs, err := envstore.NewStoreForConfig(devsyConfig) @@ -109,6 +111,19 @@ func (cmd *SetCmd) Run(_ context.Context, name string) error { return nil } +// rejectEnvVarSecret refuses to convert a name that is attached as an +// environment variable into a secret. +func rejectEnvVarSecret(devsyConfig *config.Config, contextName, name string) error { + current := devsyConfig.Contexts[contextName] + if current == nil || !slices.Contains(current.EnvVars, name) { + return nil + } + return fmt.Errorf( + "%q is attached as an environment variable; detach it before converting it to a secret", + name, + ) +} + func (cmd *SetCmd) resolveValue(name string) (string, error) { sources := 0 for _, on := range []bool{cmd.valueSet, cmd.FromFile != "", cmd.Stdin} { diff --git a/cmd/secrets/set_internal_test.go b/cmd/secrets/set_internal_test.go index 56834ccd93..a7f73999cf 100644 --- a/cmd/secrets/set_internal_test.go +++ b/cmd/secrets/set_internal_test.go @@ -46,9 +46,9 @@ func TestSetRejectsConvertingAttachedEnvironmentToSecret(t *testing.T) { })) cfg, err := config.LoadConfig("", "") require.NoError(t, err) - store, err := devsysecrets.NewStoreForConfig(cfg) + envs, err := envstore.NewStoreForConfig(cfg) require.NoError(t, err) - require.NoError(t, store.Set(config.DefaultContext, "TOKEN", "plain", devsysecrets.KindEnv)) + require.NoError(t, envs.Set(config.DefaultContext, "TOKEN", "plain")) err = (&SetCmd{ GlobalFlags: &flags.GlobalFlags{}, @@ -57,9 +57,9 @@ func TestSetRejectsConvertingAttachedEnvironmentToSecret(t *testing.T) { }).Run(context.Background(), "TOKEN") require.Error(t, err) require.Contains(t, err.Error(), "attached as an environment variable") - meta, err := store.Meta(config.DefaultContext, "TOKEN") + value, err := envs.Get(config.DefaultContext, "TOKEN") require.NoError(t, err) - require.Equal(t, devsysecrets.KindEnv, meta.Kind) + require.Equal(t, "plain", value) } func TestResolveValue_Stdin(t *testing.T) { diff --git a/cmd/workspace/build.go b/cmd/workspace/build.go index 1989ef43c6..d5d8244a5f 100644 --- a/cmd/workspace/build.go +++ b/cmd/workspace/build.go @@ -6,8 +6,10 @@ import ( "os" cmdflags "github.com/devsy-org/devsy/cmd/flags" + "github.com/devsy-org/devsy/pkg/agent/tunnelserver" "github.com/devsy-org/devsy/pkg/client" "github.com/devsy-org/devsy/pkg/client/clientimplementation" + "github.com/devsy-org/devsy/pkg/clierr" "github.com/devsy-org/devsy/pkg/config" devcconfig "github.com/devsy-org/devsy/pkg/devcontainer/config" cliflags "github.com/devsy-org/devsy/pkg/flags" @@ -16,8 +18,10 @@ import ( "github.com/devsy-org/devsy/pkg/log" "github.com/devsy-org/devsy/pkg/output" "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/secrets" "github.com/devsy-org/devsy/pkg/status" workspace2 "github.com/devsy-org/devsy/pkg/workspace" + "github.com/devsy-org/devsy/pkg/workspacelogs" "github.com/spf13/cobra" ) @@ -145,7 +149,11 @@ func (cmd *BuildCmd) execute(ctx context.Context, args []string) error { return err } - exists := workspace2.Exists(ctx, devsyConfig, args, "", cmd.Owner) + exists := workspace2.Exists( + ctx, + devsyConfig, + workspace2.LookupOptions{Args: args, Owner: cmd.Owner}, + ) sshConfigPath, cleanup, err := NewTempSSHConfig() if err != nil { return err @@ -277,8 +285,33 @@ func (cmd *BuildCmd) build( log.Debugf("cleaning up temporary workspace") }() + run := cmd.startSetupRun(workspaceClient) + ctx = run.Context(ctx) + hop := tunnelserver.NewSetupLogHop(ctx, tunnelserver.HopOptions{Session: run, Receive: true}) + defer hop.Close() + + result, err := cmd.buildDevcontainer(ctx, reporter, workspaceClient, hop) + run.Finish(err, true) + if err != nil { + return clierr.WithContext(err, run.FailureContext()) + } + + if !emitJSON { + return nil + } + + writeBuildResultJSON(result, run) + return nil +} + +func (cmd *BuildCmd) buildDevcontainer( + ctx context.Context, + reporter status.Reporter, + workspaceClient client.WorkspaceClient, + hop *tunnelserver.SetupLogHop, +) (*devcconfig.Result, error) { var result *devcconfig.Result - err = status.Run(ctx, reporter, status.Operation{ + err := status.Run(ctx, reporter, status.Operation{ Phase: status.PhaseBuildingImage, Step: "building devcontainer", }, func(ctx context.Context) error { @@ -289,23 +322,43 @@ func (cmd *BuildCmd) build( WorkspaceClient: workspaceClient, CLIOptions: cmd.CLIOptions, AgentCommand: "build", + TunnelOptions: hop.Options(), }, ) return buildErr }) - if err != nil { - return err - } + return result, err +} - if !emitJSON { +// startSetupRun starts the run that retains this build's output and installs +// its identity in the CLI options sent to the provider host. A storage problem +// leaves history unrecorded and never fails the build. +func (cmd *BuildCmd) startSetupRun(workspaceClient client.WorkspaceClient) *workspacelogs.Session { + cmd.SetupRunID = "" + cmd.SetupParentProducerID = "" + + run := workspacelogs.StartSession(workspacelogs.SessionOptions{ + Location: workspacelogs.LocationHost, + Redactor: secrets.NewEnvironmentRedactor(os.Environ()), + Run: workspacelogs.RunSpec{ + Context: workspaceClient.Context(), + WorkspaceIDAtStart: workspaceClient.Workspace(), + Command: "build", + }, + }) + if run == nil { return nil } + if ws := workspaceClient.WorkspaceConfig(); ws != nil { + run.BindWorkspace(ws.UID, workspaceClient.Workspace()) + } + cmd.SetupRunID = run.RunID + cmd.SetupParentProducerID = run.ProducerID() - writeBuildResultJSON(result) - return nil + return run } -func writeBuildResultJSON(result *devcconfig.Result) { +func writeBuildResultJSON(result *devcconfig.Result, run *workspacelogs.Session) { workdir := "" if result != nil && result.SubstitutionContext != nil { workdir = result.SubstitutionContext.ContainerWorkspaceFolder @@ -314,5 +367,14 @@ func writeBuildResultJSON(result *devcconfig.Result) { ContainerID: devcconfig.GetContainerID(result), RemoteUser: devcconfig.GetRemoteUser(result), RemoteWorkspaceFolder: workdir, + RunID: runIDOf(run), }) } + +func runIDOf(run *workspacelogs.Session) string { + if run == nil { + return "" + } + + return run.RunID +} diff --git a/cmd/workspace/delete.go b/cmd/workspace/delete.go index 204380583e..8051e7dd75 100644 --- a/cmd/workspace/delete.go +++ b/cmd/workspace/delete.go @@ -53,11 +53,13 @@ Use --ignore-not-found to treat a missing workspace as success.`, ) ([]string, cobra.ShellCompDirective) { return completion.GetWorkspaceSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, } @@ -113,7 +115,12 @@ func (cmd *DeleteCmd) Run(cobraCmd *cobra.Command, args []string) error { args, ) } - return cmd.deleteMultiple(ctx, devsyConfig, reporter, targets, args) + return cmd.deleteMultiple( + ctx, + devsyConfig, + reporter, + deleteBatch{targets: targets, args: args}, + ) }, ) @@ -226,13 +233,19 @@ func (cmd *DeleteCmd) deleteSingle( return nil } +// deleteBatch is the resolved targets and the raw arguments of a multi-delete. +type deleteBatch struct { + targets map[string]resolvedDeleteTarget + args []string +} + func (cmd *DeleteCmd) deleteMultiple( ctx context.Context, devsyConfig *config.Config, reporter status.Reporter, - targets map[string]resolvedDeleteTarget, - args []string, + batch deleteBatch, ) error { + targets, args := batch.targets, batch.args var errs []error for _, arg := range args { target := targets[arg] diff --git a/cmd/workspace/delete_test.go b/cmd/workspace/delete_test.go index 7e15cc27a8..2f90b4b444 100644 --- a/cmd/workspace/delete_test.go +++ b/cmd/workspace/delete_test.go @@ -178,8 +178,7 @@ func TestDeleteCmd_DeleteMultipleReusesResolvedClients(t *testing.T) { t.Context(), devsyConfig, reporter, - targets, - []string{argA, argB}, + deleteBatch{targets: targets, args: []string{argA, argB}}, )) }) require.True(t, fakeA.deleted) diff --git a/cmd/workspace/describe.go b/cmd/workspace/describe.go index 54b4cf5f46..3c855ba250 100644 --- a/cmd/workspace/describe.go +++ b/cmd/workspace/describe.go @@ -109,11 +109,13 @@ func (cmd *DescribeCmd) validArgs( ) ([]string, cobra.ShellCompDirective) { return completion.GetWorkspaceSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) } diff --git a/cmd/workspace/events.go b/cmd/workspace/events.go index 679592d7c0..43f9ebbb99 100644 --- a/cmd/workspace/events.go +++ b/cmd/workspace/events.go @@ -29,14 +29,13 @@ func NewEventsCmd(globalFlags *flags.GlobalFlags) *cobra.Command { Short: "Show workspace operation events", RunE: func(c *cobra.Command, args []string) error { return cmd.execute(c.Context(), args) }, ValidArgsFunction: func(root *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return completion.GetWorkspaceSuggestions( - root, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, - ) + return completion.GetWorkspaceSuggestions(root, completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }) }, } cliflags.Add( diff --git a/cmd/workspace/exec.go b/cmd/workspace/exec.go index 846e514766..8689f879b6 100644 --- a/cmd/workspace/exec.go +++ b/cmd/workspace/exec.go @@ -47,7 +47,14 @@ func NewExecCmd(f *flags.GlobalFlags) *cobra.Command { cobraCmd *cobra.Command, args []string, toComplete string, ) ([]string, cobra.ShellCompDirective) { return completion.GetWorkspaceSuggestions( - cobraCmd.Root(), cmd.Context, cmd.Provider, args, toComplete, cmd.Owner, + cobraCmd.Root(), + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, RunE: func(cobraCmd *cobra.Command, args []string) error { @@ -76,6 +83,12 @@ func NewExecCmd(f *flags.GlobalFlags) *cobra.Command { }, } + addExecFlags(execCmd, cmd) + + return execCmd +} + +func addExecFlags(execCmd *cobra.Command, cmd *ExecCmd) { cliflags.Add(execCmd, cliflags.String(&cmd.WorkspaceFolder, names.WorkspaceFolder, "", "Path to the workspace folder"), @@ -93,8 +106,6 @@ func NewExecCmd(f *flags.GlobalFlags) *cobra.Command { IDLabels: &cmd.IDLabels, ContainerDataFolder: &cmd.ContainerDataFolder, }) - - return execCmd } func (cmd *ExecCmd) Run(ctx context.Context, args []string) error { diff --git a/cmd/workspace/export.go b/cmd/workspace/export.go index fb2b34d35e..4e281d2e59 100644 --- a/cmd/workspace/export.go +++ b/cmd/workspace/export.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "os" "github.com/devsy-org/devsy/cmd/completion" "github.com/devsy-org/devsy/cmd/flags" @@ -41,11 +42,13 @@ func NewExportCmd(globalFlags *flags.GlobalFlags) *cobra.Command { ) ([]string, cobra.ShellCompDirective) { return completion.GetWorkspaceSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, } @@ -77,7 +80,7 @@ func (cmd *ExportCmd) Run(ctx context.Context, devsyConfig *config.Config, args return err } - fmt.Println(string(out)) + _, _ = fmt.Fprintln(os.Stdout, string(out)) return nil } diff --git a/cmd/workspace/lifecycle.go b/cmd/workspace/lifecycle.go index d3e840e3bf..7d9d015c23 100644 --- a/cmd/workspace/lifecycle.go +++ b/cmd/workspace/lifecycle.go @@ -2,14 +2,21 @@ package workspace import ( "context" + "errors" "fmt" + "io" "os" + "os/exec" "sort" + "strings" + "time" devcconfig "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/docker" "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/secrets" "github.com/devsy-org/devsy/pkg/types" + "github.com/devsy-org/devsy/pkg/workspacelogs" ) // DockerExecSubcommand is the docker subcommand used to exec into a container. @@ -56,6 +63,23 @@ type LifecycleExecParams struct { EnvArgs []string Workdir string User string + // Recorder optionally retains each command's output and outcome. Nil + // records nothing. + Recorder *workspacelogs.Recorder + // HookIndex is the merged-config position of the hook being run. + HookIndex int +} + +// EnvArgsRedactor masks sensitive values passed to docker exec as -e entries. +func EnvArgsRedactor(envArgs []string) *secrets.Redactor { + entries := make([]string, 0, len(envArgs)/2) + for _, arg := range envArgs { + if arg != "-e" { + entries = append(entries, arg) + } + } + + return secrets.NewEnvironmentRedactor(entries) } // ExecLifecycleHook runs a single devcontainer lifecycle hook against a container. @@ -77,18 +101,118 @@ func ExecLifecycleHook(params *LifecycleExecParams, name string, hook types.Life WorkspaceFolder: params.Workdir, Command: command, }) - if err := params.Helper.Run(params.Ctx, args, docker.Streams{ + rec := newLifecycleRecording(params, name, key) + started := time.Now() + err := params.Helper.Run(params.Ctx, args, docker.Streams{ Stdin: os.Stdin, - Stdout: os.Stdout, - Stderr: os.Stderr, - }); err != nil { - return fmt.Errorf("command %q failed: %w", key, err) + Stdout: rec.tee(os.Stdout, workspacelogs.StreamStdout), + Stderr: rec.tee(os.Stderr, workspacelogs.StreamStderr), + }) + rec.finish(params.Ctx, time.Since(started), err) + if err != nil { + return fmt.Errorf("command %q failed%s: %w", key, rec.hint(), err) } } return nil } +// lifecycleRecording retains one command's output and outcome. A nil value +// records nothing and passes streams through untouched. +type lifecycleRecording struct { + rec *workspacelogs.Recorder + inv workspacelogs.Invocation + stdout, stderr io.WriteCloser +} + +func newLifecycleRecording(params *LifecycleExecParams, hook, key string) *lifecycleRecording { + if params.Recorder == nil { + return nil + } + id := workspacelogs.NewID() + index := params.HookIndex + r := &lifecycleRecording{ + rec: params.Recorder, + inv: workspacelogs.Invocation{ + ID: id, Phase: "lifecycle", Hook: hook, Index: &index, Key: key, + }, + stdout: params.Recorder.Writer(id, workspacelogs.StreamStdout), + stderr: params.Recorder.Writer(id, workspacelogs.StreamStderr), + } + r.record(workspacelogs.InvocationRunning, "") + + return r +} + +// tee keeps dst as the only functional destination when nothing is recorded. +func (r *lifecycleRecording) tee(dst io.Writer, stream workspacelogs.Stream) io.Writer { + if r == nil { + return dst + } + if stream == workspacelogs.StreamStderr { + return io.MultiWriter(dst, r.stderr) + } + + return io.MultiWriter(dst, r.stdout) +} + +func (r *lifecycleRecording) record(state workspacelogs.InvocationState, reason string) { + inv := r.inv + inv.State = state + inv.Reason = reason + r.rec.Invocation(inv) +} + +// finish flushes output, then records the terminal state. +func (r *lifecycleRecording) finish(ctx context.Context, elapsed time.Duration, runErr error) { + if r == nil { + return + } + _ = r.stdout.Close() + _ = r.stderr.Close() + inv := r.inv + inv.Duration = elapsed + inv.State, inv.Reason = lifecycleOutcome(ctx, runErr) + if exitErr, ok := errors.AsType[*exec.ExitError](runErr); ok { + if sig, isSig := strings.CutPrefix(exitErr.String(), "signal: "); isSig { + inv.Signal = sig + } else { + code := exitErr.ExitCode() + inv.ExitCode = &code + } + } else if runErr == nil { + code := 0 + inv.ExitCode = &code + } + r.rec.Invocation(inv) +} + +func lifecycleOutcome(ctx context.Context, runErr error) (workspacelogs.InvocationState, string) { + switch { + case runErr == nil: + return workspacelogs.InvocationSucceeded, "" + case ctx != nil && ctx.Err() != nil: + return workspacelogs.InvocationCanceled, "" + } + if _, ok := errors.AsType[*exec.ExitError](runErr); !ok { + return workspacelogs.InvocationFailed, "start_failed" + } + + return workspacelogs.InvocationFailed, "" +} + +// hint points at retained output, or is empty when no run is recorded. +func (r *lifecycleRecording) hint() string { + if r == nil || r.rec.RunID() == "" { + return "" + } + + return fmt.Sprintf( + " (full output: devsy workspace logs --source setup --run %s --hook %s)", + r.rec.RunID(), r.inv.Hook, + ) +} + // BuildLifecycleEnvArgs converts a merged config's RemoteEnv into -e KEY=VALUE // pairs suitable for `docker exec`. func BuildLifecycleEnvArgs(result *devcconfig.Result) []string { diff --git a/cmd/workspace/lifecycle_record_test.go b/cmd/workspace/lifecycle_record_test.go new file mode 100644 index 0000000000..87544330b0 --- /dev/null +++ b/cmd/workspace/lifecycle_record_test.go @@ -0,0 +1,206 @@ +//go:build !windows + +package workspace + +import ( + "context" + "os" + "path/filepath" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/docker" + "github.com/devsy-org/devsy/pkg/types" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const fakeDockerScript = `#!/bin/sh +echo "fake-out" +echo "fake-err" >&2 +[ -n "$FAKE_SLEEP" ] && sleep "$FAKE_SLEEP" +exit "${FAKE_EXIT:-0}" +` + +func fakeDocker(t *testing.T) *docker.DockerHelper { + t.Helper() + path := filepath.Join(t.TempDir(), "docker") + err := os.WriteFile(path, []byte(fakeDockerScript), 0o700) //nolint:gosec // test script + require.NoError(t, err) + + return &docker.DockerHelper{DockerCommand: path} +} + +func redirectStdio(t *testing.T) (stdout, stderr string) { + t.Helper() + dir := t.TempDir() + outPath, errPath := filepath.Join(dir, "out"), filepath.Join(dir, "err") + outFile, err := os.Create(outPath) //nolint:gosec // test file + require.NoError(t, err) + errFile, err := os.Create(errPath) //nolint:gosec // test file + require.NoError(t, err) + origOut, origErr := os.Stdout, os.Stderr + os.Stdout, os.Stderr = outFile, errFile + t.Cleanup(func() { os.Stdout, os.Stderr = origOut, origErr }) + + return outPath, errPath +} + +func readFile(t *testing.T, path string) string { + t.Helper() + data, err := os.ReadFile(path) //nolint:gosec // test file + require.NoError(t, err) + + return string(data) +} + +type recordedLifecycle struct { + store *workspacelogs.Store + runID string + rec *workspacelogs.Recorder +} + +func newRecordedLifecycle(t *testing.T) *recordedLifecycle { + t.Helper() + store, err := workspacelogs.Open(workspacelogs.Options{Root: t.TempDir()}) + require.NoError(t, err) + m, err := store.CreateRun( + workspacelogs.RunSpec{Context: "default", Command: "run-user-commands"}, + ) + require.NoError(t, err) + rec, err := workspacelogs.NewRecorder(workspacelogs.RecorderOptions{ + Backend: store, RunID: m.RunID, Location: workspacelogs.LocationContainer, + }) + require.NoError(t, err) + + return &recordedLifecycle{store: store, runID: m.RunID, rec: rec} +} + +func (r *recordedLifecycle) records(t *testing.T) []workspacelogs.Record { + t.Helper() + r.rec.Close(workspacelogs.ProducerTerminal, true) + resp, err := r.store.Read(workspacelogs.ReadRequest{RunID: r.runID}) + require.NoError(t, err) + + return resp.Records +} + +func outputOf(recs []workspacelogs.Record, stream workspacelogs.Stream) string { + var out string + for i := range recs { + if recs[i].Kind == workspacelogs.KindOutput && recs[i].Stream == stream { + out += recs[i].Text + } + } + + return out +} + +func lastInvocation(recs []workspacelogs.Record) *workspacelogs.Record { + var last *workspacelogs.Record + for i := range recs { + if recs[i].Kind == workspacelogs.KindInvocation { + last = &recs[i] + } + } + + return last +} + +func execParams(t *testing.T, rec *workspacelogs.Recorder) *LifecycleExecParams { + t.Helper() + + return &LifecycleExecParams{ + Ctx: context.Background(), Helper: fakeDocker(t), ContainerID: "c1", + Recorder: rec, HookIndex: 2, + } +} + +func TestExecLifecycleHookRecordsOutputAndKeepsPassthrough(t *testing.T) { + stdoutPath, stderrPath := redirectStdio(t) + run := newRecordedLifecycle(t) + + err := ExecLifecycleHook(execParams(t, run.rec), "postCreateCommand", + types.LifecycleHook{"setup": {"echo hi"}}) + require.NoError(t, err) + + assert.Equal(t, "fake-out\n", readFile(t, stdoutPath)) + assert.Equal(t, "fake-err\n", readFile(t, stderrPath)) + recs := run.records(t) + assert.Equal(t, "fake-out\n", outputOf(recs, workspacelogs.StreamStdout)) + assert.Equal(t, "fake-err\n", outputOf(recs, workspacelogs.StreamStderr)) + inv := lastInvocation(recs) + require.NotNil(t, inv) + assert.Equal(t, workspacelogs.InvocationSucceeded, inv.State) + assert.Equal(t, "postCreateCommand", inv.Hook) + assert.Equal(t, "setup", inv.HookKey) + require.NotNil(t, inv.HookIndex) + assert.Equal(t, 2, *inv.HookIndex) + require.NotNil(t, inv.ExitCode) + assert.Zero(t, *inv.ExitCode) +} + +func TestExecLifecycleHookRecordsFailureWithHint(t *testing.T) { + redirectStdio(t) + t.Setenv("FAKE_EXIT", "3") + run := newRecordedLifecycle(t) + + err := ExecLifecycleHook(execParams(t, run.rec), "postStartCommand", + types.LifecycleHook{"": {"false"}}) + require.Error(t, err) + assert.Contains(t, err.Error(), "--source setup --run "+run.runID+" --hook postStartCommand") + + inv := lastInvocation(run.records(t)) + require.NotNil(t, inv) + assert.Equal(t, workspacelogs.InvocationFailed, inv.State) + require.NotNil(t, inv.ExitCode) + assert.Equal(t, 3, *inv.ExitCode) +} + +func TestExecLifecycleHookRecordsStartFailure(t *testing.T) { + redirectStdio(t) + run := newRecordedLifecycle(t) + params := execParams(t, run.rec) + params.Helper = &docker.DockerHelper{DockerCommand: filepath.Join(t.TempDir(), "missing")} + + require.Error(t, ExecLifecycleHook(params, "postCreateCommand", + types.LifecycleHook{"": {"true"}})) + + inv := lastInvocation(run.records(t)) + require.NotNil(t, inv) + assert.Equal(t, workspacelogs.InvocationFailed, inv.State) + assert.Equal(t, "start_failed", inv.Reason) + assert.Nil(t, inv.ExitCode) +} + +func TestExecLifecycleHookRecordsCancellation(t *testing.T) { + redirectStdio(t) + t.Setenv("FAKE_SLEEP", "30") + run := newRecordedLifecycle(t) + ctx, cancel := context.WithCancel(context.Background()) + time.AfterFunc(500*time.Millisecond, cancel) + params := execParams(t, run.rec) + params.Ctx = ctx + + require.Error(t, ExecLifecycleHook(params, "postCreateCommand", + types.LifecycleHook{"": {"x"}})) + + inv := lastInvocation(run.records(t)) + require.NotNil(t, inv) + assert.Equal(t, workspacelogs.InvocationCanceled, inv.State) +} + +func TestExecLifecycleHookWithoutRecorderIsUnchanged(t *testing.T) { + stdoutPath, _ := redirectStdio(t) + + require.NoError(t, ExecLifecycleHook(execParams(t, nil), "postCreateCommand", + types.LifecycleHook{"": {"x"}})) + assert.Equal(t, "fake-out\n", readFile(t, stdoutPath)) + + t.Setenv("FAKE_EXIT", "1") + err := ExecLifecycleHook(execParams(t, nil), "postCreateCommand", + types.LifecycleHook{"": {"x"}}) + require.Error(t, err) + assert.NotContains(t, err.Error(), "full output") +} diff --git a/cmd/workspace/logs.go b/cmd/workspace/logs.go index ffdab3424c..5465672357 100644 --- a/cmd/workspace/logs.go +++ b/cmd/workspace/logs.go @@ -23,6 +23,8 @@ import ( // LogsCmd holds the configuration. type LogsCmd struct { *flags.GlobalFlags + + setupLogsFlags } // NewLogsCmd creates a new destroy command. @@ -41,20 +43,32 @@ func NewLogsCmd(globalFlags *flags.GlobalFlags) *cobra.Command { ) ([]string, cobra.ShellCompDirective) { return completion.GetWorkspaceSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, } + registerSetupLogsFlags(startCmd, &cmd.setupLogsFlags) + return startCmd } // Run runs the command logic. func (cmd *LogsCmd) Run(ctx context.Context, args []string) error { + // Selectors are checked before any config, provider or storage access. + if err := cmd.validateSource(args); err != nil { + return err + } + if cmd.Source == sourceSetup { + return cmd.runSetup(ctx, args) + } + devsyConfig, err := config.LoadConfig(cmd.Context, cmd.Provider) if err != nil { return err diff --git a/cmd/workspace/ping.go b/cmd/workspace/ping.go index c6cca21dd6..ed5176edd6 100644 --- a/cmd/workspace/ping.go +++ b/cmd/workspace/ping.go @@ -32,11 +32,13 @@ func NewPingCmd(globalFlags *flags.GlobalFlags) *cobra.Command { ) ([]string, cobra.ShellCompDirective) { return completion.GetWorkspaceSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, Hidden: true, diff --git a/cmd/workspace/port_forward.go b/cmd/workspace/port_forward.go index c57f763062..6c6781d7db 100644 --- a/cmd/workspace/port_forward.go +++ b/cmd/workspace/port_forward.go @@ -63,24 +63,26 @@ var ( logPrefix: "Forwarding", logLabel: "port-forward", forward: func(ctx context.Context, client *ssh.Client, mapping port.Mapping, timeout time.Duration) error { - return devssh.PortForward( - ctx, client, - mapping.Host.Protocol, mapping.Host.Address, - mapping.Container.Protocol, mapping.Container.Address, - timeout, - ) + return devssh.PortForward(ctx, client, devssh.PortForwardOpts{ + LocalNetwork: mapping.Host.Protocol, + LocalAddr: mapping.Host.Address, + RemoteNetwork: mapping.Container.Protocol, + RemoteAddr: mapping.Container.Address, + ExitAfterTimeout: timeout, + }) }, } directionReverse = forwardDirection{ logPrefix: "Reverse forwarding", logLabel: "reverse port-forward", forward: func(ctx context.Context, client *ssh.Client, mapping port.Mapping, timeout time.Duration) error { - return devssh.ReversePortForward( - ctx, client, - mapping.Host.Protocol, mapping.Host.Address, - mapping.Container.Protocol, mapping.Container.Address, - timeout, - ) + return devssh.ReversePortForward(ctx, client, devssh.PortForwardOpts{ + LocalNetwork: mapping.Container.Protocol, + LocalAddr: mapping.Container.Address, + RemoteNetwork: mapping.Host.Protocol, + RemoteAddr: mapping.Host.Address, + ExitAfterTimeout: timeout, + }) }, } ) diff --git a/cmd/workspace/rename.go b/cmd/workspace/rename.go index a620f4575e..2d6ee96258 100644 --- a/cmd/workspace/rename.go +++ b/cmd/workspace/rename.go @@ -38,11 +38,13 @@ func NewRenameCmd(globalFlags *flags.GlobalFlags) *cobra.Command { if len(args) == 0 { return completion.GetWorkspaceSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) } return nil, cobra.ShellCompDirectiveNoFileComp @@ -71,7 +73,11 @@ func (cmd *RenameCmd) Run(ctx context.Context, args []string) error { return err } - if workspace.Exists(ctx, devsyConfig, []string{newName}, "", cmd.Owner) != "" { + if workspace.Exists( + ctx, + devsyConfig, + workspace.LookupOptions{Args: []string{newName}, Owner: cmd.Owner}, + ) != "" { return fmt.Errorf("workspace %s already exists", newName) } diff --git a/cmd/workspace/set_ide.go b/cmd/workspace/set_ide.go index f2aa2a7aad..2f81e75ea0 100644 --- a/cmd/workspace/set_ide.go +++ b/cmd/workspace/set_ide.go @@ -48,11 +48,13 @@ The setting is saved to the workspace config and applied on the next if len(args) == 0 { return completion.GetWorkspaceSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) } return nil, cobra.ShellCompDirectiveNoFileComp diff --git a/cmd/workspace/setup_logs.go b/cmd/workspace/setup_logs.go new file mode 100644 index 0000000000..a6ab219828 --- /dev/null +++ b/cmd/workspace/setup_logs.go @@ -0,0 +1,772 @@ +package workspace + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "strings" + "time" + "unicode" + + "al.essio.dev/pkg/shellescape" + clientpkg "github.com/devsy-org/devsy/pkg/client" + "github.com/devsy-org/devsy/pkg/config" + cliflags "github.com/devsy-org/devsy/pkg/flags" + "github.com/devsy-org/devsy/pkg/flags/names" + "github.com/devsy-org/devsy/pkg/output" + "github.com/devsy-org/devsy/pkg/workspace" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/spf13/cobra" +) + +const ( + cmdInternal = "internal" + cmdWorkspace = "workspace" + sourceContainer = "container" + sourceSetup = "setup" + runLatest = "latest" + + flagList = "list" + flagRun = "run" + flagHook = "hook" + flagInvocation = "invocation" + flagAfter = "after" + flagLimit = "limit" + + // refreshDeadline bounds one read-only refresh from the workspace. + refreshDeadline = 5 * time.Second + // minPollGap is the least time between completed polls in follow mode. + minPollGap = 500 * time.Millisecond + // maxRefreshPages bounds the pages one refresh may import. + maxRefreshPages = 16 + // maxRefreshFailures ends follow after repeated failed refreshes. + maxRefreshFailures = 3 + // maxRemoteStderr bounds the diagnostics kept from the remote reader. + maxRemoteStderr = 4 * 1024 + + reasonNoWorkspace = "no_workspace" +) + +// setupLogsFlags are the flags that only apply to --source setup. +type setupLogsFlags struct { + Source string + List bool + RunID string + Hook string + Invocation string + Follow bool + After string + Limit int +} + +func registerSetupLogsFlags(c *cobra.Command, f *setupLogsFlags) { + cliflags.Add(c, + cliflags.String( + &f.Source, + names.Source, + sourceContainer, + "What to read: container (runtime logs) or setup (retained build and lifecycle output)", + ), + cliflags.Bool(&f.List, flagList, false, + "List retained setup runs, newest first (--source setup)"), + cliflags.String(&f.RunID, flagRun, "", + "Run ID or latest (--source setup, default latest for a workspace)"), + cliflags.String(&f.Hook, flagHook, "", + "Only this lifecycle hook, such as postCreateCommand (--source setup)"), + cliflags.String(&f.Invocation, flagInvocation, "", + "Only this invocation ID (--source setup)"), + cliflags.Bool(&f.Follow, names.Follow, false, + "Keep reading the run until it completes (--source setup)").Shorthand("f"), + cliflags.String(&f.After, flagAfter, "", + "Cursor from the previous page (--source setup)"), + cliflags.Int(&f.Limit, flagLimit, 0, + "Most records per page, or runs with --list (--source setup)"), + ) +} + +// validateSource rejects bad or conflicting selectors before any I/O. +func (cmd *LogsCmd) validateSource(args []string) error { + switch cmd.Source { + case "", sourceContainer: + return cmd.rejectSetupOnlyFlags() + case sourceSetup: + return cmd.validateSetup(args) + default: + return fmt.Errorf("unknown --source %q, choose container or setup", cmd.Source) + } +} + +func (cmd *LogsCmd) rejectSetupOnlyFlags() error { + for flag, set := range map[string]bool{ + flagList: cmd.List, flagRun: cmd.RunID != "", flagHook: cmd.Hook != "", + flagInvocation: cmd.Invocation != "", names.Follow: cmd.Follow, + flagAfter: cmd.After != "", flagLimit: cmd.Limit != 0, + } { + if set { + return fmt.Errorf("--%s requires --source setup", flag) + } + } + + return nil +} + +func (cmd *LogsCmd) validateSetup(args []string) error { + if err := cmd.validateShape(args); err != nil { + return err + } + if _, err := output.ResolveMode(cmd.ResultFormat); err != nil { + return err + } + if cmd.List { + return validateIndexLimit(cmd.Limit) + } + + return workspacelogs.ValidateReadSelectors(cmd.readRequest(cmd.RunID)) +} + +// validateShape checks the argument count and selector combinations. +func (cmd *LogsCmd) validateShape(args []string) error { + if len(args) > 1 { + return errors.New("at most one workspace argument is allowed with --source setup") + } + if err := cmd.validateListConflicts(); err != nil { + return err + } + if len(args) == 0 && !cmd.List && (cmd.RunID == "" || cmd.RunID == runLatest) { + return errors.New("without a workspace argument, --run must be an explicit run ID") + } + + return nil +} + +func (cmd *LogsCmd) validateListConflicts() error { + if !cmd.List { + return nil + } + for flag, set := range map[string]bool{ + flagRun: cmd.RunID != "", flagHook: cmd.Hook != "", + flagInvocation: cmd.Invocation != "", names.Follow: cmd.Follow, + } { + if set { + return fmt.Errorf("--%s cannot be combined with --%s", flagList, flag) + } + } + + return nil +} + +func validateIndexLimit(limit int) error { + if limit < 0 || limit > workspacelogs.MaxIndexLimit { + return fmt.Errorf( + "--limit must be between 1 and %d with --list", + workspacelogs.MaxIndexLimit, + ) + } + + return nil +} + +// readRequest builds the page request. A run of latest is validated later, once +// it resolves to an ID. +func (cmd *LogsCmd) readRequest(runID string) workspacelogs.ReadRequest { + if runID == "" || runID == runLatest { + runID = workspacelogs.NewID() + } + + return workspacelogs.ReadRequest{ + RunID: runID, Hook: cmd.Hook, InvocationID: cmd.Invocation, + After: cmd.After, Limit: cmd.Limit, + } +} + +// setupTarget is the verified identity a read is bound to. +type setupTarget struct { + context string + workspaceID string + uid string + // client is nil when no supported connection exists. + client clientpkg.WorkspaceClient + // named is true when the caller passed a workspace argument. + named bool +} + +func (cmd *LogsCmd) runSetup(ctx context.Context, args []string) error { + mode, err := output.ResolveMode(cmd.ResultFormat) + if err != nil { + return err + } + devsyConfig, err := config.LoadConfig(cmd.Context, cmd.Provider) + if err != nil { + return err + } + store, err := workspacelogs.OpenDefault() + if err != nil { + return fmt.Errorf("open workspace logs: %w", err) + } + explicit := cmd.RunID != "" && cmd.RunID != runLatest + target, err := cmd.resolveTarget(ctx, devsyConfig, args, explicit) + if err != nil { + return err + } + r := &setupReader{ + store: store, json: mode == output.ModeJSON, out: os.Stdout, errOut: os.Stderr, + refresher: newRefresher(target), flags: cmd.setupLogsFlags, + } + + return r.run(ctx, target) +} + +// resolveTarget finds the workspace identity. A retained explicit run stays +// readable when the workspace itself is gone. +func (cmd *LogsCmd) resolveTarget( + ctx context.Context, devsyConfig *config.Config, args []string, explicit bool, +) (setupTarget, error) { + target := setupTarget{context: devsyConfig.DefaultContext} + if len(args) == 0 { + return target, nil + } + target.named, target.workspaceID = true, args[0] + base, err := workspace.Get(ctx, workspace.GetOptions{ + DevsyConfig: devsyConfig, Args: args, Owner: cmd.Owner, + }) + if err != nil { + if explicit { + return target, nil + } + + return target, fmt.Errorf("get workspace for setup logs: %w", err) + } + target.context, target.workspaceID = base.Context(), base.Workspace() + if wc := base.WorkspaceConfig(); wc != nil { + target.uid = wc.UID + } + target.client, _ = base.(clientpkg.WorkspaceClient) + + return target, nil +} + +// refresher imports missing records from the workspace. It returns an +// availability reason, or empty when the refresh succeeded. +type refresher interface { + refresh(ctx context.Context, store *workspacelogs.Store, runID string) string +} + +type noRefresher struct{ reason string } + +func (n noRefresher) refresh(context.Context, *workspacelogs.Store, string) string { + return n.reason +} + +func newRefresher(t setupTarget) refresher { + switch { + case t.client == nil && t.named: + return noRefresher{reason: workspacelogs.ReasonRefreshUnsupported} + case t.client == nil: + return noRefresher{reason: reasonNoWorkspace} + default: + return &agentRefresher{client: t.client, context: t.context, uid: t.uid} + } +} + +// setupReader reads local history, refreshes it, and prints it. +type setupReader struct { + store *workspacelogs.Store + json bool + out io.Writer + errOut io.Writer + refresher refresher + flags setupLogsFlags +} + +func (r *setupReader) run(ctx context.Context, t setupTarget) error { + if r.flags.List { + return r.list(t) + } + runID, err := r.resolveRun(t) + if err != nil { + return err + } + if r.flags.Follow { + return r.follow(ctx, runID) + } + + return r.snapshot(ctx, runID) +} + +func (r *setupReader) request(runID, after string) workspacelogs.ReadRequest { + return workspacelogs.ReadRequest{ + RunID: runID, Hook: r.flags.Hook, InvocationID: r.flags.Invocation, + After: after, Limit: r.flags.Limit, + } +} + +// resolveRun resolves latest once and checks the run belongs to the target. +func (r *setupReader) resolveRun(t setupTarget) (string, error) { + runID := r.flags.RunID + if runID == "" || runID == runLatest { + return r.latestRun(t) + } + m, err := r.store.Manifest(runID) + if err != nil { + return "", runLookupError(err, runID) + } + + return runID, checkRunBelongsTo(m, t) +} + +func (r *setupReader) latestRun(t setupTarget) (string, error) { + if t.uid == "" { + return "", errors.New( + "cannot resolve the latest run without a workspace; pass --run with a run ID", + ) + } + resp, err := r.store.ListRuns(workspacelogs.IndexRequest{ + Context: t.context, WorkspaceUID: t.uid, Limit: 1, + }) + if err != nil { + return "", err + } + if len(resp.Runs) == 0 { + return "", fmt.Errorf("no retained setup runs for workspace %q", t.workspaceID) + } + + return resp.Runs[0].RunID, nil +} + +func runLookupError(err error, runID string) error { + if errors.Is(err, workspacelogs.ErrNotFound) { + return fmt.Errorf("no retained setup run %q in this context", runID) + } + + return err +} + +// checkRunBelongsTo matches the stored identity against the workspace the +// caller named. A display name alone never binds an old run to a new workspace. +func checkRunBelongsTo(m *workspacelogs.RunManifest, t setupTarget) error { + if m.Context != t.context { + return fmt.Errorf("run %s belongs to another context", m.RunID) + } + if t.named && !sameWorkspace(m, t) { + return fmt.Errorf("run %s belongs to another workspace", m.RunID) + } + + return nil +} + +// sameWorkspace compares UIDs when both sides have one. Otherwise it falls back +// to the display ID the run started with. +func sameWorkspace(m *workspacelogs.RunManifest, t setupTarget) bool { + if m.WorkspaceUID != nil && t.uid != "" { + return *m.WorkspaceUID == t.uid + } + if m.WorkspaceUID == nil && m.WorkspaceIDAtStart == "" { + return true + } + + return m.WorkspaceIDAtStart == t.workspaceID +} + +// refreshUnlessComplete runs one refresh for a run that may still be missing +// records. A completed mirrored run needs no provider access. +func (r *setupReader) refreshUnlessComplete(ctx context.Context, runID string) string { + m, err := r.store.Manifest(runID) + if err != nil || m.Complete() { + return "" + } + + return r.refresher.refresh(ctx, r.store, runID) +} + +func (r *setupReader) snapshot(ctx context.Context, runID string) error { + reason := r.refreshUnlessComplete(ctx, runID) + if r.json { + resp, err := r.store.Read(r.request(runID, r.flags.After)) + if err != nil { + return err + } + addRefreshAvailability(resp, reason) + + return writeJSONLine(r.out, resp) + } + + return r.plainSnapshot(runID, reason) +} + +func (r *setupReader) plainSnapshot(runID, reason string) error { + pw := &plainWriter{out: r.out, errOut: r.errOut} + after := r.flags.After + for { + resp, err := r.store.Read(r.request(runID, after)) + if err != nil { + return err + } + pw.page(resp) + if !resp.HasMore { + pw.finish(resp, reason) + + return nil + } + after = resp.NextCursor + } +} + +// follow drains the run, then polls it. It never reruns anything: a refresh +// only reads the workspace. +func (r *setupReader) follow(ctx context.Context, runID string) error { + pw := &plainWriter{out: r.out, errOut: r.errOut} + after, failures := r.flags.After, 0 + var lastPoll time.Time + for { + reason := r.pollRefresh(ctx, runID, &lastPoll) + if ctx.Err() != nil { + return nil + } + resp, next, err := r.drain(runID, after, pw) + if err != nil { + return err + } + after = next + failures = nextFailures(failures, reason) + if done, stop := r.followDone(resp, reason, failures); done { + r.finishFollow(pw, resp, stop) + + return nil + } + } +} + +// pollRefresh waits out the minimum gap, then refreshes without overlap. +func (r *setupReader) pollRefresh(ctx context.Context, runID string, last *time.Time) string { + if wait := minPollGap - time.Since(*last); !last.IsZero() && wait > 0 { + select { + case <-ctx.Done(): + return "" + case <-time.After(wait): + } + } + reason := r.refreshUnlessComplete(ctx, runID) + *last = time.Now() + + return reason +} + +func nextFailures(failures int, reason string) int { + if reason == workspacelogs.ReasonRefreshFailed { + return failures + 1 + } + + return 0 +} + +// drain reads every page available now and prints it. +func (r *setupReader) drain( + runID, after string, pw *plainWriter, +) (*workspacelogs.ReadResponse, string, error) { + for { + resp, err := r.store.Read(r.request(runID, after)) + if err != nil { + return nil, after, err + } + after = resp.NextCursor + if r.json && len(resp.Records) > 0 { + if err = writeJSONLine(r.out, resp); err != nil { + return nil, after, err + } + } else if !r.json { + pw.page(resp) + } + if !resp.HasMore { + return resp, after, nil + } + } +} + +// followDone decides whether to stop. The second result is the availability +// reason to report when the stop is not a completed run. +func (r *setupReader) followDone( + resp *workspacelogs.ReadResponse, reason string, failures int, +) (bool, string) { + if resp.Complete { + return true, "" + } + switch reason { + case workspacelogs.ReasonSourceOffline, workspacelogs.ReasonNoSpool: + return true, reason + } + if failures >= maxRefreshFailures { + return true, workspacelogs.ReasonRefreshFailed + } + if m, err := r.store.Manifest(resp.Run.RunID); err == nil && !m.Active() { + return true, reason + } + + return false, "" +} + +func (r *setupReader) finishFollow( + pw *plainWriter, + resp *workspacelogs.ReadResponse, + reason string, +) { + addRefreshAvailability(resp, reason) + if r.json { + resp.Records = []workspacelogs.Record{} + _ = writeJSONLine(r.out, resp) + + return + } + pw.finish(resp, reason) +} + +func addRefreshAvailability(resp *workspacelogs.ReadResponse, reason string) { + if reason == "" { + return + } + resp.Availability = append(resp.Availability, + workspacelogs.SourceAvailability{Available: false, Reason: reason}) + resp.Complete = false +} + +func (r *setupReader) list(t setupTarget) error { + resp, err := r.store.ListRuns(workspacelogs.IndexRequest{ + Context: t.context, WorkspaceUID: t.uid, After: r.flags.After, Limit: r.flags.Limit, + }) + if err != nil { + return err + } + if r.json { + return writeJSONLine(r.out, resp) + } + for i := range resp.Runs { + run := &resp.Runs[i] + _, _ = fmt.Fprintf(r.out, "%s %s %s command=%s capture=%s workspace=%s\n", + run.RunID, run.CreatedAt.Local().Format(time.RFC3339), run.Command, + run.CommandState, run.CaptureState, run.WorkspaceIDAtStart) + } + if resp.HasMore { + _, _ = fmt.Fprintf(r.errOut, "more runs: --after %s\n", resp.NextCursor) + } + + return nil +} + +// agentRefresher imports missing records through the installed agent on the +// workspace host. The reader there only execs into a running workload. +type agentRefresher struct { + client clientpkg.WorkspaceClient + context string + uid string +} + +func (a *agentRefresher) refresh( + ctx context.Context, store *workspacelogs.Store, runID string, +) string { + ctx, cancel := context.WithTimeout(ctx, refreshDeadline) + defer cancel() + for range maxRefreshPages { + more, reason := a.refreshPage(ctx, store, runID) + if reason != "" || !more { + return reason + } + } + + return "" +} + +func (a *agentRefresher) refreshPage( + ctx context.Context, store *workspacelogs.Store, runID string, +) (bool, string) { + pos, err := store.LocalPositions(runID) + if err != nil { + return false, workspacelogs.ReasonRefreshFailed + } + token, err := workspacelogs.PositionCursor(runID, pos) + if err != nil { + return false, workspacelogs.ReasonRefreshFailed + } + resp, reason := a.fetch(ctx, runID, token) + if reason != "" { + return false, reason + } + res, err := store.ImportRemote(workspacelogs.ImportRequest{ + RunID: runID, Context: a.context, WorkspaceUID: a.uid, Response: resp, + }) + if err != nil { + return false, workspacelogs.ReasonRefreshFailed + } + + return resp.HasMore && res.Written > 0 && len(res.Stalled) == 0, "" +} + +func (a *agentRefresher) fetch( + ctx context.Context, runID, after string, +) (*workspacelogs.ReadResponse, string) { + stdout := &workspacelogs.BoundedBuffer{Max: workspacelogs.MaxPageBytes + 1} + stderr := &workspacelogs.BoundedBuffer{Max: maxRemoteStderr} + err := a.client.Command(ctx, clientpkg.CommandOptions{ + Command: a.command(runID, after), + Stdout: stdout, + Stderr: stderr, + RawStdout: true, + }) + if err != nil { + return nil, classifyRemoteFailure(stderr.String()) + } + resp, err := workspacelogs.DecodeReadResponse(strings.NewReader(stdout.String())) + if err != nil { + return nil, workspacelogs.ReasonRefreshFailed + } + + return resp, "" +} + +func (a *agentRefresher) command(runID, after string) string { + words := []string{ + shellescape.Quote(a.client.AgentPath()), + cmdInternal, "agent", cmdWorkspace, "logs", "--source", sourceSetup, + "--context", shellescape.Quote(a.client.Context()), + "--id", shellescape.Quote(a.client.Workspace()), + "--uid", shellescape.Quote(a.uid), + "--run", shellescape.Quote(runID), + } + if after != "" { + words = append(words, "--after", shellescape.Quote(after)) + } + + return strings.Join(words, " ") +} + +// classifyRemoteFailure maps remote stderr to an availability reason. An agent +// that predates the reader rejects the flag or command, which is unsupported +// rather than a failure. +func classifyRemoteFailure(stderr string) string { + if reason, ok := workspacelogs.ParseUnavailable(stderr); ok { + return reason + } + if strings.Contains(stderr, "unknown flag") || strings.Contains(stderr, "unknown command") { + return workspacelogs.ReasonRefreshUnsupported + } + + return workspacelogs.ReasonRefreshFailed +} + +func writeJSONLine(w io.Writer, v any) error { + return json.NewEncoder(w).Encode(v) +} + +// plainWriter prints a transcript. Output text is literal; each line carries +// its stream label so stdout and stderr stay distinguishable. Capture notes go +// to stderr with the availability notices. +type plainWriter struct { + out io.Writer + errOut io.Writer + key string + midLine bool +} + +func (w *plainWriter) page(resp *workspacelogs.ReadResponse) { + for i := range resp.Records { + w.record(&resp.Records[i]) + } +} + +func (w *plainWriter) record(rec *workspacelogs.Record) { + switch rec.Kind { + case workspacelogs.KindOutput: + w.output(rec) + case workspacelogs.KindInvocation: + w.endLine() + _, _ = fmt.Fprintln(w.out, invocationLine(rec)) + case workspacelogs.KindCapture: + _, _ = fmt.Fprintf(w.errOut, "capture: %s %s\n", rec.Condition, rec.Detail) + case workspacelogs.KindProducer: + } +} + +func (w *plainWriter) output(rec *workspacelogs.Record) { + key := rec.InvocationID + "/" + string(rec.Stream) + if w.midLine && key != w.key { + w.endLine() + } + w.key = key + text := stripControl(rec.Text) + if rec.Elided { + w.endLine() + text = fmt.Sprintf("[%d bytes not retained]\n", rec.Bytes) + } + label := string(rec.Stream) + " | " + for text != "" { + if !w.midLine { + _, _ = io.WriteString(w.out, label) + w.midLine = true + } + line, rest, hasNewline := strings.Cut(text, "\n") + if !hasNewline { + _, _ = io.WriteString(w.out, line) + + return + } + _, _ = io.WriteString(w.out, line+"\n") + w.midLine, text = false, rest + } +} + +func (w *plainWriter) endLine() { + if w.midLine { + _, _ = io.WriteString(w.out, "\n") + w.midLine = false + } +} + +// finish writes the notices that describe how much history is available. +func (w *plainWriter) finish(resp *workspacelogs.ReadResponse, reason string) { + w.endLine() + for _, a := range resp.Availability { + if !a.Available { + _, _ = fmt.Fprintf(w.errOut, "unavailable: %s %s\n", a.ProducerID, a.Reason) + } + } + if reason != "" { + _, _ = fmt.Fprintf(w.errOut, "unavailable: %s\n", reason) + } + if !resp.Complete { + _, _ = fmt.Fprintf(w.errOut, "history is incomplete (capture %s)\n", resp.Run.CaptureState) + } +} + +func invocationLine(rec *workspacelogs.Record) string { + name := rec.Hook + if name == "" { + name = rec.Phase + } + line := "# " + name + if rec.HookKey != "" { + line += " (" + stripControl(rec.HookKey) + ")" + } + line += ": " + string(rec.State) + if rec.Reason != "" { + line += " " + stripControl(rec.Reason) + } + if rec.ExitCode != nil { + line += fmt.Sprintf(" exit=%d", *rec.ExitCode) + } + if rec.Signal != "" { + line += " signal=" + stripControl(rec.Signal) + } + + return line +} + +func stripControl(s string) string { + return strings.Map(func(r rune) rune { + if unicode.IsControl(r) && r != '\n' && r != '\t' { + return -1 + } + + return r + }, s) +} diff --git a/cmd/workspace/setup_logs_test.go b/cmd/workspace/setup_logs_test.go new file mode 100644 index 0000000000..414e5f4947 --- /dev/null +++ b/cmd/workspace/setup_logs_test.go @@ -0,0 +1,877 @@ +package workspace + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "strings" + "testing" + "time" + + "github.com/devsy-org/devsy/cmd/flags" + clientpkg "github.com/devsy-org/devsy/pkg/client" + "github.com/devsy-org/devsy/pkg/secrets" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + testCtx = "default" + testUID = "uid-1" + testSecret = "s3cr3t-token-value" + errCombined = "cannot be combined" + hookCreate = "postCreateCommand" +) + +type fakeRefresher struct { + reason string + calls int + // onRefresh may add records to the store, like a successful import. + onRefresh func(call int) +} + +func (f *fakeRefresher) refresh(context.Context, *workspacelogs.Store, string) string { + f.calls++ + if f.onRefresh != nil { + f.onRefresh(f.calls) + } + + return f.reason +} + +func newStore(t *testing.T) *workspacelogs.Store { + t.Helper() + s, err := workspacelogs.Open(workspacelogs.Options{Root: t.TempDir()}) + require.NoError(t, err) + + return s +} + +type seededRun struct { + runID string + recorder *workspacelogs.Recorder + inv string +} + +func seedRun(t *testing.T, s *workspacelogs.Store, uid string) *seededRun { + t.Helper() + m, err := s.CreateRun(workspacelogs.RunSpec{ + Context: testCtx, WorkspaceUID: uid, WorkspaceIDAtStart: "ws", Command: "up", + }) + require.NoError(t, err) + rec, err := workspacelogs.NewRecorder(workspacelogs.RecorderOptions{ + Backend: s, RunID: m.RunID, Location: workspacelogs.LocationContainer, + Redactor: secrets.NewRedactor([]string{"TOKEN=" + testSecret}), + }) + require.NoError(t, err) + + return &seededRun{runID: m.RunID, recorder: rec, inv: workspacelogs.NewID()} +} + +func (r *seededRun) hook(state workspacelogs.InvocationState, exit *int) { + r.recorder.Invocation(workspacelogs.Invocation{ + ID: r.inv, Phase: "setup", Hook: hookCreate, State: state, ExitCode: exit, + }) +} + +func (r *seededRun) write(stream workspacelogs.Stream, text string) { + w := r.recorder.Writer(r.inv, stream) + _, _ = w.Write([]byte(text)) + _ = w.Close() +} + +func (r *seededRun) finish() { r.recorder.Close(workspacelogs.ProducerTerminal, true) } + +type harness struct { + reader *setupReader + out *bytes.Buffer + errOut *bytes.Buffer +} + +func newHarness(s *workspacelogs.Store, f setupLogsFlags, ref refresher, jsonMode bool) *harness { + h := &harness{out: &bytes.Buffer{}, errOut: &bytes.Buffer{}} + h.reader = &setupReader{ + store: s, json: jsonMode, out: h.out, errOut: h.errOut, refresher: ref, flags: f, + } + + return h +} + +func target() setupTarget { + return setupTarget{context: testCtx, workspaceID: "ws", uid: testUID, named: true} +} + +type validateCase struct { + name string + flags setupLogsFlags + args []string + format string + wantErr string +} + +func runValidateCases(t *testing.T, tests []validateCase) { + t.Helper() + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmd := &LogsCmd{GlobalFlags: &flags.GlobalFlags{ResultFormat: formatPlain}} + if tt.format != "" { + cmd.ResultFormat = tt.format + } + cmd.setupLogsFlags = tt.flags + + err := cmd.validateSource(tt.args) + + if tt.wantErr == "" { + require.NoError(t, err) + + return + } + require.ErrorContains(t, err, tt.wantErr) + }) + } +} + +func TestValidateSourceRejectsSetupOnlyFlagsForContainer(t *testing.T) { + run := workspacelogs.NewID() + runValidateCases(t, []validateCase{ + {name: "container default", flags: setupLogsFlags{Source: "container"}}, + {name: "empty source", flags: setupLogsFlags{}}, + { + name: "unknown source", + flags: setupLogsFlags{Source: "bogus"}, + wantErr: "unknown --source", + }, + { + name: "list with container", + flags: setupLogsFlags{List: true}, + wantErr: "--list requires", + }, + {name: "run with container", flags: setupLogsFlags{RunID: run}, wantErr: "--run requires"}, + { + name: "hook with container", + flags: setupLogsFlags{Hook: hookCreate}, + wantErr: "--hook requires", + }, + { + name: "follow with container", + flags: setupLogsFlags{Follow: true}, + wantErr: "--follow requires", + }, + { + name: "limit with container", + flags: setupLogsFlags{Limit: 5}, + wantErr: "--limit requires", + }, + { + name: "after with container", + flags: setupLogsFlags{After: "x"}, + wantErr: "--after requires", + }, + }) +} + +func TestValidateSourceSetupSelectors(t *testing.T) { + run := workspacelogs.NewID() + runValidateCases(t, []validateCase{ + {name: "setup latest", flags: setupLogsFlags{Source: sourceSetup}, args: []string{"ws"}}, + { + name: "setup explicit run no workspace", + flags: setupLogsFlags{Source: sourceSetup, RunID: run}, + }, + { + name: "setup no workspace needs run", + flags: setupLogsFlags{Source: sourceSetup}, + wantErr: "explicit run ID", + }, + { + name: "setup no workspace rejects latest", + flags: setupLogsFlags{Source: sourceSetup, RunID: "latest"}, + wantErr: "explicit run ID", + }, + {name: "setup list no workspace", flags: setupLogsFlags{Source: sourceSetup, List: true}}, + }) +} + +func TestValidateSourceSetupConflictsAndBadValues(t *testing.T) { + run := workspacelogs.NewID() + runValidateCases(t, []validateCase{ + { + name: "list with run", + flags: setupLogsFlags{Source: sourceSetup, List: true, RunID: run}, + args: []string{"ws"}, + wantErr: errCombined, + }, + { + name: "list with hook", + flags: setupLogsFlags{Source: sourceSetup, List: true, Hook: hookCreate}, + wantErr: errCombined, + }, + { + name: "list with follow", + flags: setupLogsFlags{Source: sourceSetup, List: true, Follow: true}, + wantErr: errCombined, + }, + { + name: "list limit too large", + flags: setupLogsFlags{Source: sourceSetup, List: true, Limit: 101}, + wantErr: "--limit must be between", + }, + { + name: "read limit too large", + flags: setupLogsFlags{Source: sourceSetup, Limit: 2001}, + args: []string{"ws"}, + wantErr: "limit must be", + }, + }) +} + +func TestValidateSourceSetupRejectsBadValues(t *testing.T) { + runValidateCases(t, []validateCase{ + { + name: "unknown hook", + flags: setupLogsFlags{Source: sourceSetup, Hook: "nope"}, + args: []string{"ws"}, + wantErr: "unknown hook", + }, + { + name: "bad run id", + flags: setupLogsFlags{Source: sourceSetup, RunID: "../etc/passwd"}, + args: []string{"ws"}, + wantErr: "invalid identifier", + }, + { + name: "bad invocation id", + flags: setupLogsFlags{Source: sourceSetup, Invocation: "x"}, + args: []string{"ws"}, + wantErr: "invalid identifier", + }, + { + name: "bad cursor", + flags: setupLogsFlags{Source: sourceSetup, After: "a b;rm"}, + args: []string{"ws"}, + wantErr: "malformed cursor", + }, + { + name: "two workspaces", + flags: setupLogsFlags{Source: sourceSetup}, + args: []string{"a", "b"}, + wantErr: "at most one", + }, + { + name: "bad result format", + flags: setupLogsFlags{Source: sourceSetup}, + args: []string{"ws"}, + format: "yaml", + wantErr: "unexpected output format", + }, + }) +} + +func TestCompletedRunReadsWithoutRefresh(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + exit := 3 + run.hook(workspacelogs.InvocationRunning, nil) + run.write(workspacelogs.StreamStdout, "built\n") + run.hook(workspacelogs.InvocationFailed, &exit) + run.finish() + ref := &fakeRefresher{reason: workspacelogs.ReasonSourceOffline} + h := newHarness(s, setupLogsFlags{RunID: run.runID}, ref, false) + + err := h.reader.run(context.Background(), target()) + + require.NoError(t, err, "a failed historical hook is data, not a command error") + assert.Zero(t, ref.calls, "a completed run needs no provider access") + assert.Contains(t, h.out.String(), "stdout | built\n") + assert.Contains(t, h.out.String(), "# postCreateCommand: failed exit=3") + assert.Empty(t, h.errOut.String()) +} + +func TestHistorySurvivesWorkspaceRemoval(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationSucceeded, nil) + run.write(workspacelogs.StreamStdout, "kept\n") + run.finish() + // No workspace: no uid, no client, only the run ID. + t0 := setupTarget{context: testCtx} + h := newHarness( + s, + setupLogsFlags{RunID: run.runID}, + noRefresher{reason: reasonNoWorkspace}, + false, + ) + + require.NoError(t, h.reader.run(context.Background(), t0)) + assert.Contains(t, h.out.String(), "kept") +} + +func TestRunOfAnotherWorkspaceIsRejected(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, "uid-old") + run.finish() + h := newHarness(s, setupLogsFlags{RunID: run.runID}, noRefresher{}, false) + + err := h.reader.run(context.Background(), target()) + + require.ErrorContains(t, err, "another workspace") + assert.Empty(t, h.out.String()) +} + +func TestRunOfAnotherContextIsRejected(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.finish() + other := target() + other.context = "elsewhere" + h := newHarness(s, setupLogsFlags{RunID: run.runID}, noRefresher{}, false) + + require.ErrorContains(t, h.reader.run(context.Background(), other), "another context") +} + +func TestLatestResolvesOnceToNewestRun(t *testing.T) { + s := newStore(t) + older := seedRun(t, s, testUID) + older.hook(workspacelogs.InvocationSucceeded, nil) + older.write(workspacelogs.StreamStdout, "old\n") + older.finish() + time.Sleep(1100 * time.Millisecond) + newer := seedRun(t, s, testUID) + newer.hook(workspacelogs.InvocationSucceeded, nil) + newer.write(workspacelogs.StreamStdout, "new\n") + newer.finish() + h := newHarness(s, setupLogsFlags{}, noRefresher{}, false) + + require.NoError(t, h.reader.run(context.Background(), target())) + assert.Contains(t, h.out.String(), "new") + assert.NotContains(t, h.out.String(), "old") +} + +func TestLatestWithoutRunsIsAnError(t *testing.T) { + h := newHarness(newStore(t), setupLogsFlags{}, noRefresher{}, false) + + require.ErrorContains(t, h.reader.run(context.Background(), target()), "no retained setup runs") +} + +func TestUnknownRunIsALookupError(t *testing.T) { + h := newHarness(newStore(t), setupLogsFlags{RunID: workspacelogs.NewID()}, noRefresher{}, false) + + require.ErrorContains(t, h.reader.run(context.Background(), target()), "no retained setup run") +} + +func TestNonexistentInvocationIsALookupError(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationSucceeded, nil) + run.finish() + h := newHarness(s, setupLogsFlags{RunID: run.runID, Invocation: workspacelogs.NewID()}, + noRefresher{}, false) + + err := h.reader.run(context.Background(), target()) + + require.ErrorIs(t, err, workspacelogs.ErrNotFound) + assert.Empty(t, h.out.String()) +} + +func TestSilentHookReturnsMetadataWithEmptyOutput(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + run.hook(workspacelogs.InvocationSucceeded, nil) + run.finish() + h := newHarness(s, setupLogsFlags{RunID: run.runID, Hook: hookCreate}, noRefresher{}, true) + + require.NoError(t, h.reader.run(context.Background(), target())) + var resp workspacelogs.ReadResponse + require.NoError(t, json.Unmarshal(h.out.Bytes(), &resp)) + assert.NotEmpty(t, resp.Records) + for _, rec := range resp.Records { + assert.NotEqual(t, workspacelogs.KindOutput, rec.Kind) + } +} + +func TestCursorBindsRunAndFilters(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + for range 6 { + run.write(workspacelogs.StreamStdout, "line\n") + } + run.hook(workspacelogs.InvocationSucceeded, nil) + run.finish() + + first := newHarness(s, setupLogsFlags{RunID: run.runID, Limit: 2}, noRefresher{}, true) + require.NoError(t, first.reader.run(context.Background(), target())) + var page workspacelogs.ReadResponse + require.NoError(t, json.Unmarshal(first.out.Bytes(), &page)) + require.True(t, page.HasMore) + + resumed := newHarness(s, setupLogsFlags{RunID: run.runID, Limit: 2, After: page.NextCursor}, + noRefresher{}, true) + require.NoError(t, resumed.reader.run(context.Background(), target())) + + changed := newHarness(s, setupLogsFlags{ + RunID: run.runID, Limit: 2, After: page.NextCursor, Hook: hookCreate, + }, noRefresher{}, true) + require.ErrorIs(t, changed.reader.run(context.Background(), target()), + workspacelogs.ErrInvalidCursor) + + other := seedRun(t, s, testUID) + other.finish() + moved := newHarness(s, setupLogsFlags{RunID: other.runID, After: page.NextCursor}, + noRefresher{}, true) + require.ErrorIs(t, moved.reader.run(context.Background(), target()), + workspacelogs.ErrInvalidCursor) +} + +func TestPlainTranscriptPagesPastFiveHundredRecords(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + const lines = 1300 + for i := range lines { + run.write(workspacelogs.StreamStdout, fmt.Sprintf("line-%04d\n", i)) + } + run.hook(workspacelogs.InvocationSucceeded, nil) + run.finish() + h := newHarness(s, setupLogsFlags{RunID: run.runID}, noRefresher{}, false) + + require.NoError(t, h.reader.run(context.Background(), target())) + + assert.Equal(t, lines, strings.Count(h.out.String(), "stdout | line-")) + assert.Contains(t, h.out.String(), "line-0000") + assert.Contains(t, h.out.String(), fmt.Sprintf("line-%04d", lines-1)) +} + +func TestPlainTranscriptKeepsStreamLabelsAndLiteralText(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + run.write(workspacelogs.StreamStdout, `{"kind":"status","x":1}`+"\n") + run.write(workspacelogs.StreamStderr, "warn\n\n indented") + run.hook(workspacelogs.InvocationSucceeded, nil) + run.finish() + h := newHarness(s, setupLogsFlags{RunID: run.runID}, noRefresher{}, false) + + require.NoError(t, h.reader.run(context.Background(), target())) + + out := h.out.String() + assert.Contains(t, out, `stdout | {"kind":"status","x":1}`+"\n") + assert.Contains(t, out, "stderr | warn\n") + assert.Contains(t, out, "stderr | \n") + assert.Contains(t, out, "stderr | indented") +} + +func TestSecretNeverPresentInOutput(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + w := run.recorder.Writer(run.inv, workspacelogs.StreamStdout) + _, _ = w.Write([]byte("token=" + testSecret[:7])) + _, _ = w.Write([]byte(testSecret[7:] + "\n")) + _ = w.Close() + run.hook(workspacelogs.InvocationSucceeded, nil) + run.finish() + + for _, jsonMode := range []bool{false, true} { + h := newHarness(s, setupLogsFlags{RunID: run.runID}, noRefresher{}, jsonMode) + require.NoError(t, h.reader.run(context.Background(), target())) + assert.NotContains(t, h.out.String(), testSecret) + assert.NotContains(t, h.errOut.String(), testSecret) + } +} + +func TestIncompleteRunReportsRefreshReasonOnStderr(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + run.write(workspacelogs.StreamStdout, "partial\n") + ref := &fakeRefresher{reason: workspacelogs.ReasonRefreshUnsupported} + h := newHarness(s, setupLogsFlags{RunID: run.runID}, ref, false) + + require.NoError(t, h.reader.run(context.Background(), target()), "unsupported is not a failure") + + assert.Equal(t, 1, ref.calls) + assert.Contains(t, h.out.String(), "partial") + assert.NotContains(t, h.out.String(), "unavailable") + assert.Contains(t, h.errOut.String(), "unavailable: refresh_unsupported") + assert.Contains(t, h.errOut.String(), "history is incomplete") +} + +func TestIncompleteRunReportsRefreshReasonInJSON(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + ref := &fakeRefresher{reason: workspacelogs.ReasonSourceOffline} + h := newHarness(s, setupLogsFlags{RunID: run.runID}, ref, true) + + require.NoError(t, h.reader.run(context.Background(), target())) + + var resp workspacelogs.ReadResponse + require.NoError(t, json.Unmarshal(h.out.Bytes(), &resp)) + assert.False(t, resp.Complete) + require.NotEmpty(t, resp.Availability) + last := resp.Availability[len(resp.Availability)-1] + assert.False(t, last.Available) + assert.Equal(t, workspacelogs.ReasonSourceOffline, last.Reason) + assert.Empty(t, h.errOut.String(), "JSON carries notices as metadata") +} + +func TestFollowJSONIsNewlineDelimitedValidJSON(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + run.write(workspacelogs.StreamStdout, "first\n") + ref := &fakeRefresher{} + ref.onRefresh = func(call int) { + if call == 1 { + run.write(workspacelogs.StreamStdout, "second\n") + run.hook(workspacelogs.InvocationSucceeded, nil) + run.finish() + } + } + h := newHarness(s, setupLogsFlags{RunID: run.runID, Follow: true}, ref, true) + + require.NoError(t, h.reader.run(context.Background(), target())) + + lines := strings.Split(strings.TrimSpace(h.out.String()), "\n") + require.GreaterOrEqual(t, len(lines), 2) + var all strings.Builder + for _, line := range lines { + var resp workspacelogs.ReadResponse + require.NoError(t, json.Unmarshal([]byte(line), &resp), line) + assert.LessOrEqual(t, len(line), workspacelogs.MaxPageBytes) + for _, rec := range resp.Records { + all.WriteString(rec.Text) + } + } + assert.Equal(t, "first\nsecond\n", all.String()) + var last workspacelogs.ReadResponse + require.NoError(t, json.Unmarshal([]byte(lines[len(lines)-1]), &last)) + assert.True(t, last.Complete) + assert.Empty(t, h.errOut.String(), "no plain notices in JSON follow") +} + +func TestFollowStopsWhenSourceIsOfflineWithNoReadPath(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + run.write(workspacelogs.StreamStdout, "so far\n") + ref := &fakeRefresher{reason: workspacelogs.ReasonSourceOffline} + h := newHarness(s, setupLogsFlags{RunID: run.runID, Follow: true}, ref, false) + + done := make(chan error, 1) + go func() { done <- h.reader.run(context.Background(), target()) }() + + select { + case err := <-done: + require.NoError(t, err) + case <-time.After(5 * time.Second): + t.Fatal("follow kept polling an offline source") + } + assert.Equal(t, 1, ref.calls) + assert.Contains(t, h.out.String(), "so far") + assert.Contains(t, h.errOut.String(), "unavailable: source_offline") +} + +func TestFollowWaitsAtLeastMinimumBetweenPolls(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + var stamps []time.Time + ref := &fakeRefresher{} + ref.onRefresh = func(call int) { + stamps = append(stamps, time.Now()) + if call == 3 { + run.hook(workspacelogs.InvocationSucceeded, nil) + run.finish() + } + } + h := newHarness(s, setupLogsFlags{RunID: run.runID, Follow: true}, ref, false) + + require.NoError(t, h.reader.run(context.Background(), target())) + + require.Len(t, stamps, 3) + for i := 1; i < len(stamps); i++ { + assert.GreaterOrEqual(t, stamps[i].Sub(stamps[i-1]), minPollGap-20*time.Millisecond) + } +} + +func TestFollowStopsOnCancelWithoutError(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + ctx, cancel := context.WithCancel(context.Background()) + ref := &fakeRefresher{} + ref.onRefresh = func(int) { cancel() } + h := newHarness(s, setupLogsFlags{RunID: run.runID, Follow: true}, ref, false) + + require.NoError(t, h.reader.run(ctx, target())) +} + +func TestFollowStopsAfterRepeatedRefreshFailures(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + ref := &fakeRefresher{reason: workspacelogs.ReasonRefreshFailed} + h := newHarness(s, setupLogsFlags{RunID: run.runID, Follow: true}, ref, false) + + require.NoError(t, h.reader.run(context.Background(), target())) + + assert.Equal(t, maxRefreshFailures, ref.calls) + assert.Contains(t, h.errOut.String(), "refresh_failed") +} + +func TestListShowsRunsAndContinuationHint(t *testing.T) { + s := newStore(t) + for range 3 { + seedRun(t, s, testUID).finish() + } + h := newHarness(s, setupLogsFlags{List: true, Limit: 2}, noRefresher{}, false) + + require.NoError(t, h.reader.run(context.Background(), target())) + + assert.Len(t, strings.Split(strings.TrimSpace(h.out.String()), "\n"), 2) + assert.Contains(t, h.errOut.String(), "more runs: --after ") +} + +func TestListWithoutWorkspaceIncludesAllRunsInContext(t *testing.T) { + s := newStore(t) + seedRun(t, s, "uid-a").finish() + seedRun(t, s, "uid-b").finish() + h := newHarness(s, setupLogsFlags{List: true}, noRefresher{}, true) + + require.NoError(t, h.reader.run(context.Background(), setupTarget{context: testCtx})) + + var resp workspacelogs.IndexResponse + require.NoError(t, json.Unmarshal(h.out.Bytes(), &resp)) + assert.Len(t, resp.Runs, 2) +} + +func TestNewRefresherChoosesNoReadPath(t *testing.T) { + assert.Equal(t, reasonNoWorkspace, + newRefresher(setupTarget{}).(noRefresher).reason) + assert.Equal(t, workspacelogs.ReasonRefreshUnsupported, + newRefresher(setupTarget{named: true}).(noRefresher).reason) +} + +func TestClassifyRemoteFailure(t *testing.T) { + assert.Equal(t, workspacelogs.ReasonSourceOffline, + classifyRemoteFailure(workspacelogs.FormatUnavailable(workspacelogs.ReasonSourceOffline))) + assert.Equal(t, workspacelogs.ReasonRefreshUnsupported, + classifyRemoteFailure("Error: unknown flag: --source")) + assert.Equal(t, workspacelogs.ReasonRefreshUnsupported, + classifyRemoteFailure(`Error: unknown command "setup-logs"`)) + assert.Equal(t, workspacelogs.ReasonRefreshFailed, classifyRemoteFailure("boom")) +} + +// fakeClient embeds the interface so any call other than the ones overridden +// panics: a refresh must only use Command. +type fakeClient struct { + clientpkg.WorkspaceClient + + stdout string + stderr string + err error + commands []clientpkg.CommandOptions + block bool +} + +func (f *fakeClient) AgentPath() string { return "/opt/devsy agent/devsy" } +func (f *fakeClient) Context() string { return testCtx } +func (f *fakeClient) Workspace() string { return "ws" } + +func (f *fakeClient) Command(ctx context.Context, o clientpkg.CommandOptions) error { + f.commands = append(f.commands, o) + if f.block { + <-ctx.Done() + + return ctx.Err() + } + _, _ = o.Stdout.Write([]byte(f.stdout)) + _, _ = o.Stderr.Write([]byte(f.stderr)) + + return f.err +} + +// remoteResponse builds the JSON a container reader would print for a finished +// run that the local store does not have yet. +func remoteResponse(t *testing.T) (runID, body string) { + t.Helper() + remote := newStore(t) + run := seedRun(t, remote, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + run.write(workspacelogs.StreamStdout, "from container\n") + run.hook(workspacelogs.InvocationSucceeded, nil) + run.finish() + resp, err := remote.Read(workspacelogs.ReadRequest{RunID: run.runID}) + require.NoError(t, err) + data, err := json.Marshal(resp) + require.NoError(t, err) + + return run.runID, string(data) +} + +func localMirror(t *testing.T, runID string) *workspacelogs.Store { + t.Helper() + s := newStore(t) + _, err := s.CreateRun(workspacelogs.RunSpec{ + RunID: runID, + Context: testCtx, + WorkspaceUID: testUID, + WorkspaceIDAtStart: "ws", + Command: "up", + }) + require.NoError(t, err) + + return s +} + +func TestAgentRefresherImportsThroughReadOnlyCommand(t *testing.T) { + runID, body := remoteResponse(t) + local := localMirror(t, runID) + client := &fakeClient{stdout: body} + a := &agentRefresher{client: client, context: testCtx, uid: testUID} + + assert.Empty(t, a.refresh(context.Background(), local, runID)) + + require.Len(t, client.commands, 1) + cmd := client.commands[0] + assert.True(t, cmd.RawStdout) + assert.Nil(t, cmd.Stdin) + assert.True(t, strings.HasPrefix(cmd.Command, + "'/opt/devsy agent/devsy' internal agent workspace logs --source setup")) + assert.Contains(t, cmd.Command, "--run "+runID) + assert.Contains(t, cmd.Command, "--uid "+testUID) + h := newHarness(local, setupLogsFlags{RunID: runID}, noRefresher{}, false) + require.NoError(t, h.reader.run(context.Background(), target())) + assert.Contains(t, h.out.String(), "from container") +} + +func TestSetupReaderRefreshesIncompleteRunOnceThenReadsLocal(t *testing.T) { + runID, body := remoteResponse(t) + local := localMirror(t, runID) + client := &fakeClient{stdout: body} + h := newHarness(local, setupLogsFlags{RunID: runID}, + &agentRefresher{client: client, context: testCtx, uid: testUID}, false) + + require.NoError(t, h.reader.run(context.Background(), target())) + require.NoError(t, h.reader.run(context.Background(), target())) + + assert.Len(t, client.commands, 1, "the second read finds a completed local run") + assert.Equal(t, 2, strings.Count(h.out.String(), "from container")) +} + +func TestAgentRefresherMapsFailuresToAvailability(t *testing.T) { + tests := []struct { + name string + client *fakeClient + reason string + }{ + { + name: "old agent", + client: &fakeClient{err: errors.New("exit 1"), stderr: "Error: unknown flag: --source"}, + reason: workspacelogs.ReasonRefreshUnsupported, + }, + { + name: "offline", + client: &fakeClient{ + err: errors.New("exit 1"), + stderr: workspacelogs.FormatUnavailable(workspacelogs.ReasonSourceOffline), + }, + reason: workspacelogs.ReasonSourceOffline, + }, + { + name: "invalid response", + client: &fakeClient{stdout: "not json"}, + reason: workspacelogs.ReasonRefreshFailed, + }, + { + name: "oversized response", + client: &fakeClient{stdout: strings.Repeat(" ", workspacelogs.MaxPageBytes+10)}, + reason: workspacelogs.ReasonRefreshFailed, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + a := &agentRefresher{client: tt.client, context: testCtx, uid: testUID} + + assert.Equal(t, tt.reason, a.refresh(context.Background(), s, run.runID)) + assert.Len(t, tt.client.commands, 1, "no retry and no fallback command") + }) + } +} + +func TestAgentRefresherRejectsResponseForAnotherWorkspace(t *testing.T) { + runID, body := remoteResponse(t) + local := localMirror(t, runID) + a := &agentRefresher{client: &fakeClient{stdout: body}, context: testCtx, uid: "uid-other"} + + assert.Equal( + t, + workspacelogs.ReasonRefreshFailed, + a.refresh(context.Background(), local, runID), + ) + h := newHarness(local, setupLogsFlags{RunID: runID}, noRefresher{}, false) + require.NoError(t, h.reader.run(context.Background(), target())) + assert.NotContains(t, h.out.String(), "from container") +} + +func TestAgentRefresherHonorsDeadline(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + a := &agentRefresher{client: &fakeClient{block: true}, context: testCtx, uid: testUID} + start := time.Now() + + reason := a.refresh(context.Background(), s, run.runID) + + assert.Equal(t, workspacelogs.ReasonRefreshFailed, reason) + assert.Less(t, time.Since(start), refreshDeadline+2*time.Second) + assert.GreaterOrEqual(t, time.Since(start), refreshDeadline-100*time.Millisecond) +} + +func TestFollowKeepsPollingLocalRunWithoutRefreshPath(t *testing.T) { + s := newStore(t) + run := seedRun(t, s, testUID) + run.hook(workspacelogs.InvocationRunning, nil) + ref := &fakeRefresher{reason: reasonNoWorkspace} + ref.onRefresh = func(call int) { + if call == 2 { + run.write(workspacelogs.StreamStdout, "late line\n") + run.hook(workspacelogs.InvocationSucceeded, nil) + run.finish() + } + } + h := newHarness(s, setupLogsFlags{RunID: run.runID, Follow: true}, ref, false) + + require.NoError(t, h.reader.run(context.Background(), target())) + + assert.GreaterOrEqual(t, ref.calls, 2) + assert.Contains(t, h.out.String(), "late line") +} + +func TestPlainWriterStripsTerminalControlCharacters(t *testing.T) { + var out, errOut bytes.Buffer + w := &plainWriter{out: &out, errOut: &errOut} + exit := 1 + + w.record(&workspacelogs.Record{ + Kind: workspacelogs.KindOutput, InvocationID: workspacelogs.NewID(), + Stream: workspacelogs.StreamStdout, Text: "a\x1b[31mred\x07\u009b\r\tb\n", + }) + w.record(&workspacelogs.Record{ + Kind: workspacelogs.KindInvocation, Hook: hookCreate, State: workspacelogs.InvocationFailed, + HookKey: "k\x1b]0;x\x07", Reason: "r\x1bz", Signal: "S\x00IG", ExitCode: &exit, + }) + + assert.Equal(t, "stdout | a[31mred\tb\n", strings.SplitN(out.String(), "# ", 2)[0]) + assert.Contains(t, out.String(), "# "+hookCreate+" (k]0;x): failed rz exit=1 signal=SIG") + assert.NotContains(t, out.String(), "\x1b") +} diff --git a/cmd/workspace/ssh.go b/cmd/workspace/ssh.go index 4458485f63..d0fecfbc6f 100644 --- a/cmd/workspace/ssh.go +++ b/cmd/workspace/ssh.go @@ -82,11 +82,13 @@ func NewSSHCmd(f *flags.GlobalFlags) *cobra.Command { ) ([]string, cobra.ShellCompDirective) { return completion.GetWorkspaceSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, } @@ -586,13 +588,10 @@ func (cmd *SSHCmd) startTunnelServices( ) == config.BoolTrue, GitSSHSigningKey: cmd.GitSSHSigningKey, } - status := cmd.startServices( - ctx, - devsyConfig, - containerClient, - workspaceClient.WorkspaceConfig(), - opts, - ) + status := cmd.startServices(ctx, devsyConfig, serviceTarget{ + client: containerClient, + workspace: workspaceClient.WorkspaceConfig(), + }, opts) return tunnel.GitHubSessionEnvironment(status) } @@ -672,13 +671,19 @@ type startServicesOptions struct { GitSSHSigningKey string } +// serviceTarget is the container client and workspace that services run against. +type serviceTarget struct { + client *ssh.Client + workspace *provider.Workspace +} + func (cmd *SSHCmd) startServices( ctx context.Context, devsyConfig *config.Config, - containerClient *ssh.Client, - workspace *provider.Workspace, + target serviceTarget, opts startServicesOptions, ) credentials.Readiness { + containerClient, workspace := target.client, target.workspace if cmd.User != "" { return tunnel.StartPreparedServices( ctx, diff --git a/cmd/workspace/ssh_keepalive_test.go b/cmd/workspace/ssh_keepalive_test.go index b91dc92cc4..f9112b0071 100644 --- a/cmd/workspace/ssh_keepalive_test.go +++ b/cmd/workspace/ssh_keepalive_test.go @@ -73,13 +73,10 @@ func TestCheckKeepAliveResponseWithDevsySSHServer(t *testing.T) { require.NoError(t, err) defer func() { _ = listener.Close() }() - server, err := sshserver.NewServer( - listener.Addr().String(), - hostKeyPEM, - nil, - t.TempDir(), - "", - ) + server, err := sshserver.NewServer(listener.Addr().String(), sshserver.ServerOptions{ + HostKey: hostKeyPEM, + Workdir: t.TempDir(), + }) require.NoError(t, err) serveDone := make(chan error, 1) diff --git a/cmd/workspace/status.go b/cmd/workspace/status.go index 6f60e48aab..fb7c7fa58b 100644 --- a/cmd/workspace/status.go +++ b/cmd/workspace/status.go @@ -41,14 +41,20 @@ func NewStatusCmd(globalFlags *flags.GlobalFlags) *cobra.Command { RunE: func(cobraCmd *cobra.Command, args []string) error { return cmd.execute(cobraCmd.Context(), args) }, - ValidArgsFunction: func(rootCmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + ValidArgsFunction: func( + rootCmd *cobra.Command, + args []string, + toComplete string, + ) ([]string, cobra.ShellCompDirective) { return completion.GetWorkspaceSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, } @@ -109,7 +115,7 @@ func (cmd *StatusCmd) Run( return err } - fmt.Print(string(out)) + _, _ = fmt.Fprint(os.Stdout, string(out)) } return nil diff --git a/cmd/workspace/stop.go b/cmd/workspace/stop.go index fe8f01a89d..a8983f974a 100644 --- a/cmd/workspace/stop.go +++ b/cmd/workspace/stop.go @@ -36,14 +36,20 @@ func NewStopCmd(flags *flags.GlobalFlags) *cobra.Command { RunE: func(cobraCmd *cobra.Command, args []string) error { return cmd.runArgs(cobraCmd.Context(), args) }, - ValidArgsFunction: func(rootCmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + ValidArgsFunction: func( + rootCmd *cobra.Command, + args []string, + toComplete string, + ) ([]string, cobra.ShellCompDirective) { return completion.GetWorkspaceSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, } @@ -146,6 +152,19 @@ func (cmd *StopCmd) run( return err } + if err := cmd.stopRunning(ctx, devsyConfig, client); err != nil { + return err + } + + opener.KillBrowserTunnel(client.Context(), client.Workspace()) + return nil +} + +func (cmd *StopCmd) stopRunning( + ctx context.Context, + devsyConfig *config.Config, + client client2.BaseWorkspaceClient, +) error { if err := checkRunning(ctx, client); err != nil { return err } @@ -155,14 +174,10 @@ func (cmd *StopCmd) run( if err != nil { return err } - if !wasStopped { - if err := stopResources(ctx, client); err != nil { - return err - } + if wasStopped { + return nil } - - opener.KillBrowserTunnel(client.Context(), client.Workspace()) - return nil + return stopResources(ctx, client) } func waitForLock(ctx context.Context, client client2.BaseWorkspaceClient) error { diff --git a/cmd/workspace/task.go b/cmd/workspace/task.go index cf6ec49388..865c5f44ae 100644 --- a/cmd/workspace/task.go +++ b/cmd/workspace/task.go @@ -247,12 +247,7 @@ func readTaskForFollower( } func emitTaskTransition(last, current *task.State, emitJSON bool) { - if last != nil && last.Phase == current.Phase && last.Step == current.Step && - last.Status == current.Status && last.OperationID == current.OperationID && - (!emitJSON || last.DurationMs == current.DurationMs) { - return - } - if current.Phase == "" { + if taskTransitionUnchanged(last, current, emitJSON) || current.Phase == "" { return } event := taskStatusEvent(current) @@ -263,6 +258,15 @@ func emitTaskTransition(last, current *task.State, emitJSON bool) { _, _ = fmt.Fprintf(os.Stdout, "task %s: %s\n", current.ID, current.Phase) } +func taskTransitionUnchanged(last, current *task.State, emitJSON bool) bool { + if last == nil { + return false + } + same := last.Phase == current.Phase && last.Step == current.Step && + last.Status == current.Status && last.OperationID == current.OperationID + return same && (!emitJSON || last.DurationMs == current.DurationMs) +} + func taskStatusEvent(current *task.State) status.Event { event := status.Event{ Phase: status.Phase(current.Phase), @@ -439,7 +443,7 @@ func taskStateError(state *task.State) error { func resultEnvelopeFrom(state *task.State) config2.ResultEnvelope { if state.Result == nil { - return config2.ResultEnvelope{} + return config2.ResultEnvelope{RunID: state.RunID} } var workspaceFolder string if state.Result.SubstitutionContext != nil { @@ -451,5 +455,6 @@ func resultEnvelopeFrom(state *task.State) config2.ResultEnvelope { RemoteWorkspaceFolder: workspaceFolder, Warnings: state.Result.HostWarnings, Recovery: state.Result.RecoveryContainer, + RunID: state.RunID, } } diff --git a/cmd/workspace/troubleshoot.go b/cmd/workspace/troubleshoot.go index 6078429cd6..85f8952951 100644 --- a/cmd/workspace/troubleshoot.go +++ b/cmd/workspace/troubleshoot.go @@ -35,14 +35,20 @@ func NewTroubleshootCmd(flags *flags.GlobalFlags) *cobra.Command { Run: func(cobraCmd *cobra.Command, args []string) { cmd.Run(cobraCmd.Context(), args) }, - ValidArgsFunction: func(rootCmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + ValidArgsFunction: func( + rootCmd *cobra.Command, + args []string, + toComplete string, + ) ([]string, cobra.ShellCompDirective) { return completion.GetWorkspaceSuggestions( rootCmd, - cmd.Context, - cmd.Provider, - args, - toComplete, - cmd.Owner, + completion.SuggestionRequest{ + Context: cmd.Context, + Provider: cmd.Provider, + Args: args, + ToComplete: toComplete, + Owner: cmd.Owner, + }, ) }, Hidden: true, diff --git a/cmd/workspace/up/agent.go b/cmd/workspace/up/agent.go index de7de21d5a..8d54a288db 100644 --- a/cmd/workspace/up/agent.go +++ b/cmd/workspace/up/agent.go @@ -92,16 +92,17 @@ func (cmd *UpCmd) devsyUpProxy( errChan <- cmd.runProxyUpCommand(ctx, client, stdinReader, stdoutWriter) }() - result, err := tunnelserver.RunUpServer( - cancelCtx, - stdoutReader, - stdinWriter, - true, - true, - client.WorkspaceConfig(), - tunnelserver.WithGitToken(cmd.GitToken), - tunnelserver.WithStatusReporter(cmd.reporter()), - ) + result, err := tunnelserver.RunUpServer(cancelCtx, tunnelserver.UpSpec{ + Reader: stdoutReader, + Writer: stdinWriter, + AllowGitCredentials: true, + AllowDockerCredentials: true, + Workspace: client.WorkspaceConfig(), + Options: append([]tunnelserver.Option{ + tunnelserver.WithGitToken(cmd.GitToken), + tunnelserver.WithStatusReporter(cmd.reporter()), + }, cmd.setupHop.Options()...), + }) if err != nil { return nil, fmt.Errorf("run tunnel machine: %w", err) } @@ -231,17 +232,18 @@ func (cmd *UpCmd) devsyUpMachineSSH( SSHCommand: sshTunnelCmd, Command: agentCommand, TunnelServerFunc: func(ctx context.Context, stdin io.WriteCloser, stdout io.Reader) (*config2.Result, error) { - return tunnelserver.RunUpServer( - ctx, - stdout, - stdin, - client.AgentInjectGitCredentials(cmd.CLIOptions), - client.AgentInjectDockerCredentials(cmd.CLIOptions), - client.WorkspaceConfig(), - tunnelserver.WithSecrets(cmd.SecretsEnv, cmd.SecretsMount), - tunnelserver.WithGitToken(cmd.GitToken), - tunnelserver.WithStatusReporter(cmd.reporter()), - ) + return tunnelserver.RunUpServer(ctx, tunnelserver.UpSpec{ + Reader: stdout, + Writer: stdin, + AllowGitCredentials: client.AgentInjectGitCredentials(cmd.CLIOptions), + AllowDockerCredentials: client.AgentInjectDockerCredentials(cmd.CLIOptions), + Workspace: client.WorkspaceConfig(), + Options: append([]tunnelserver.Option{ + tunnelserver.WithSecrets(cmd.SecretsEnv, cmd.SecretsMount), + tunnelserver.WithGitToken(cmd.GitToken), + tunnelserver.WithStatusReporter(cmd.reporter()), + }, cmd.setupHop.Options()...), + }) }, }) } diff --git a/cmd/workspace/up/detach.go b/cmd/workspace/up/detach.go index 16556c24d1..a1a3e6723f 100644 --- a/cmd/workspace/up/detach.go +++ b/cmd/workspace/up/detach.go @@ -37,31 +37,40 @@ func (cmd *UpCmd) runDetached(args []string, cfg *config.Config) error { if err != nil { return err } + runID := provisionalRun(cfg, workspaceID) t, err := store.CreateLaunch(task.CreateOptions{ Command: "up", WorkspaceID: workspaceID, + RunID: runID, }) if err != nil { + linkRunToTask(runID, "", err) return err } + linkRunToTask(runID, t.ID(), nil) if err := launchDetached(t, cmd.detachedInvocationEnvironment()); err != nil { + linkRunToTask(runID, t.ID(), err) return errors.Join(fmt.Errorf("launch detached up: %w", err), t.FailLaunch(err)) } - return cmd.renderDetachedTask(t) + return cmd.renderDetachedTask(t, runID) } -func (cmd *UpCmd) renderDetachedTask(t *task.Task) error { +func (cmd *UpCmd) renderDetachedTask(t *task.Task, runID string) error { mode, err := output.ResolveMode(cmd.ResultFormat) if err != nil { return err } if mode == output.ModeJSON { - return config2.WriteTaskJSON(cmd.stdout(), t.ID()) + return config2.WriteTaskRunJSON(cmd.stdout(), t.ID(), runID) } _, err = fmt.Fprintf(cmd.stdout(), "Submitted task %s. Poll with 'workspace task get %s' or 'workspace task logs %s -f'.\n", t.ID(), t.ID(), t.ID()) + if err == nil && runID != "" { + _, err = fmt.Fprintf(cmd.stdout(), + "Build and lifecycle output: 'workspace logs --source setup --run %s'.\n", runID) + } return err } @@ -244,14 +253,19 @@ func advanceTaskLaunch( traceDetachedAdmission("checking launcher") return false, err } - if state.PID != 0 && !state.WorkerReady { - if err := t.WorkerLaunchReady(os.Getpid()); err != nil { - traceDetachedAdmission("acknowledging worker") - return false, err - } - return false, nil + return false, acknowledgeWorkerLaunch(t, state) +} + +func acknowledgeWorkerLaunch(t *task.Task, state *task.State) error { + if state.PID == 0 || state.WorkerReady { + return nil + } + if err := t.WorkerLaunchReady(os.Getpid()); err != nil { + traceDetachedAdmission("acknowledging worker") + return err } - return false, nil + + return nil } func traceDetachedAdmission(stage string) { diff --git a/cmd/workspace/up/secrets_test.go b/cmd/workspace/up/secrets_test.go index ea7162ece0..5e41f8a3d9 100644 --- a/cmd/workspace/up/secrets_test.go +++ b/cmd/workspace/up/secrets_test.go @@ -11,6 +11,10 @@ import ( "github.com/stretchr/testify/require" ) +const ( + defaultValue = "default" +) + const ( secretAPIKey = "API_KEY" secretToken = "TOKEN" @@ -21,17 +25,17 @@ const ( func testConfig(bound ...string) *config.Config { return &config.Config{ - DefaultContext: "default", + DefaultContext: defaultValue, Contexts: map[string]*config.ContextConfig{ - "default": {Secrets: bound}, + defaultValue: {Secrets: bound}, }, } } func testEnvConfig(bound ...string) *config.Config { return &config.Config{ - DefaultContext: "default", - Contexts: map[string]*config.ContextConfig{"default": {EnvVars: bound}}, + DefaultContext: defaultValue, + Contexts: map[string]*config.ContextConfig{defaultValue: {EnvVars: bound}}, } } diff --git a/cmd/workspace/up/setup_run.go b/cmd/workspace/up/setup_run.go new file mode 100644 index 0000000000..73eec86181 --- /dev/null +++ b/cmd/workspace/up/setup_run.go @@ -0,0 +1,84 @@ +package up + +import ( + "os" + + client2 "github.com/devsy-org/devsy/pkg/client" + "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/secrets" + "github.com/devsy-org/devsy/pkg/workspacelogs" +) + +// startSetupRun starts or joins the run that retains this invocation's build +// and lifecycle output and installs its identity in the CLI options sent down +// the setup chain. A detached worker joins the run its submission allocated. +// A storage problem leaves history unrecorded and never fails the command. +func (cmd *UpCmd) startSetupRun(client client2.BaseWorkspaceClient) *workspacelogs.Session { + // A run reference never carries over from earlier state. + cmd.SetupRunID = "" + cmd.SetupParentProducerID = "" + + spec := workspacelogs.RunSpec{ + Context: client.Context(), + WorkspaceIDAtStart: client.Workspace(), + Command: "up", + TaskID: cmd.taskID, + } + opts := workspacelogs.SessionOptions{ + Run: spec, + Location: workspacelogs.LocationHost, + Redactor: secrets.NewEnvironmentRedactor(os.Environ()), + } + if cmd.workerTask != nil { + opts.RunID = cmd.workerTask.RunID() + } + session := workspacelogs.StartSession(opts) + if session == nil { + return nil + } + if ws := client.WorkspaceConfig(); ws != nil { + session.BindWorkspace(ws.UID, client.Workspace()) + } + cmd.SetupRunID = session.RunID + cmd.SetupParentProducerID = session.ProducerID() + + return session +} + +// provisionalRun allocates the run for a detached submission before the +// workspace is resolved. Its workspace UID is bound later by the worker. It +// returns an empty ID when history cannot be recorded. +func provisionalRun(cfg *config.Config, workspaceID string) string { + store, err := workspacelogs.OpenDefault() + if err != nil { + return "" + } + m, err := store.CreateRun(workspacelogs.RunSpec{ + Context: cfg.DefaultContext, + WorkspaceIDAtStart: workspaceID, + Command: "up", + }) + if err != nil { + return "" + } + + return m.RunID +} + +// linkRunToTask records the task that owns a provisional run, or marks the run +// failed when the task never launched. +func linkRunToTask(runID, taskID string, launchErr error) { + if runID == "" { + return + } + store, err := workspacelogs.OpenDefault() + if err != nil { + return + } + if launchErr != nil { + _ = store.SetCommandState(runID, workspacelogs.CommandFailed) + + return + } + _ = store.SetTaskID(runID, taskID) +} diff --git a/cmd/workspace/up/setup_run_test.go b/cmd/workspace/up/setup_run_test.go new file mode 100644 index 0000000000..e17c51b709 --- /dev/null +++ b/cmd/workspace/up/setup_run_test.go @@ -0,0 +1,51 @@ +package up + +import ( + "errors" + "testing" + + "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func useTempHome(t *testing.T) { + t.Helper() + t.Setenv(config.EnvHome, t.TempDir()) + config.ResetPathManager() + t.Cleanup(config.ResetPathManager) +} + +func TestProvisionalRunIsUnboundAndLinkedToTask(t *testing.T) { + useTempHome(t) + cfg := &config.Config{DefaultContext: "work"} + + runID := provisionalRun(cfg, "my-ws") + require.NotEmpty(t, runID) + linkRunToTask(runID, "task-1", nil) + + store, err := workspacelogs.OpenDefault() + require.NoError(t, err) + m, err := store.Manifest(runID) + require.NoError(t, err) + assert.Nil(t, m.WorkspaceUID, "the UID is bound by the worker after resolution") + assert.Equal(t, "work", m.Context) + assert.Equal(t, "my-ws", m.WorkspaceIDAtStart) + assert.Equal(t, "task-1", m.TaskID) +} + +func TestLinkRunToTaskMarksLaunchFailure(t *testing.T) { + useTempHome(t) + runID := provisionalRun(&config.Config{DefaultContext: "work"}, "ws") + require.NotEmpty(t, runID) + + linkRunToTask(runID, "", errors.New("could not start")) + linkRunToTask("", "t", nil) + + store, err := workspacelogs.OpenDefault() + require.NoError(t, err) + m, err := store.Manifest(runID) + require.NoError(t, err) + assert.Equal(t, workspacelogs.CommandFailed, m.CommandState) +} diff --git a/cmd/workspace/up/up.go b/cmd/workspace/up/up.go index 04f617948c..cf04cf7999 100644 --- a/cmd/workspace/up/up.go +++ b/cmd/workspace/up/up.go @@ -11,7 +11,9 @@ import ( "syscall" "github.com/devsy-org/devsy/cmd/flags" + "github.com/devsy-org/devsy/pkg/agent/tunnelserver" client2 "github.com/devsy-org/devsy/pkg/client" + "github.com/devsy-org/devsy/pkg/clierr" "github.com/devsy-org/devsy/pkg/config" config2 "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/flags/names" @@ -70,6 +72,8 @@ type UpCmd struct { Out io.Writer // Set at the start of Run; nil until then. statusReporter status.Reporter + // setupHop mirrors retained setup history into this process's store. + setupHop *tunnelserver.SetupLogHop // resolveWorkspace overrides workspace.Resolve; nil falls back to workspace.Resolve. resolveWorkspace func( @@ -545,6 +549,7 @@ func emitUpResult(wctx *workspaceContext, ideURL string, out io.Writer) { URL: ideURL, Warnings: warnings, Recovery: recovery, + RunID: wctx.runID, }) } @@ -601,6 +606,7 @@ type workspaceContext struct { user string workdir string tunnelPort int + runID string } func (cmd *UpCmd) applyPullFromInsideContainerOverride(cobraCmd *cobra.Command) { @@ -660,11 +666,35 @@ func (cmd *UpCmd) prepareWorkspace(client client2.BaseWorkspaceClient) { } } -// executeDevsyUp runs the agent and returns workspace context. +// executeDevsyUp runs the agent and returns workspace context. The run that +// retains its build and lifecycle output starts here so the foreground command, +// the detached worker and the headless path all record the same way. func (cmd *UpCmd) executeDevsyUp( ctx context.Context, devsyConfig *config.Config, client client2.BaseWorkspaceClient, +) (*workspaceContext, error) { + run := cmd.startSetupRun(client) + cmd.setupHop = tunnelserver.NewSetupLogHop(ctx, tunnelserver.HopOptions{ + Session: run, Receive: true, + }) + defer cmd.setupHop.Close() + wctx, err := cmd.runDevsyUp(run.Context(ctx), devsyConfig, client) + run.Finish(err, true) + if err != nil { + return nil, clierr.WithContext(err, run.FailureContext()) + } + if wctx != nil && run != nil { + wctx.runID = run.RunID + } + + return wctx, nil +} + +func (cmd *UpCmd) runDevsyUp( + ctx context.Context, + devsyConfig *config.Config, + client client2.BaseWorkspaceClient, ) (*workspaceContext, error) { result, err := cmd.devsyUp(ctx, devsyConfig, client) if err := validateUpResult(result, err); err != nil { diff --git a/cmd/workspace/up/up_client.go b/cmd/workspace/up/up_client.go index 96c5589ab5..817c6827e7 100644 --- a/cmd/workspace/up/up_client.go +++ b/cmd/workspace/up/up_client.go @@ -109,10 +109,18 @@ func (cmd *UpCmd) checkWorkspaceExisted( ) bool { switch { case cmd.ID != "": - return workspace2.Exists(ctx, devsyConfig, nil, cmd.ID, cmd.Owner) != "" + return workspace2.Exists( + ctx, + devsyConfig, + workspace2.LookupOptions{ID: cmd.ID, Owner: cmd.Owner}, + ) != "" case len(args) > 0: _, name := file.IsLocalDir(args[0]) - return workspace2.Exists(ctx, devsyConfig, nil, workspace2.ToID(name), cmd.Owner) != "" + return workspace2.Exists( + ctx, + devsyConfig, + workspace2.LookupOptions{ID: workspace2.ToID(name), Owner: cmd.Owner}, + ) != "" default: return true } @@ -248,26 +256,15 @@ func (cmd *UpCmd) prepareSecretsWithProject( return err } - if cmd.SecretsFile != "" { - parsed, err := secrets.ParseSecretsFile(cmd.SecretsFile) - if err != nil { - return err - } - for k, v := range parsed { - cmd.SecretsEnv = append(cmd.SecretsEnv, k+"="+v) - } + if err := cmd.appendSecretsFile(); err != nil { + return err } if err := cmd.resolveStoredSecrets(ctx, devsyConfig, project); err != nil { return err } - if cmd.FeatureSecretsFile == "" { - cmd.FeatureSecretsFile = os.Getenv("DEVCONTAINER_SECRETS_FILE") - } - if cmd.FeatureSecretsFile != "" { - cmd.CLIOptions.FeatureSecretsFile = cmd.FeatureSecretsFile - } + cmd.resolveFeatureSecretsFile() cmd.WorkspaceEnv = options2.InheritFromEnvironment( cmd.WorkspaceEnv, @@ -278,6 +275,29 @@ func (cmd *UpCmd) prepareSecretsWithProject( return nil } +func (cmd *UpCmd) appendSecretsFile() error { + if cmd.SecretsFile == "" { + return nil + } + parsed, err := secrets.ParseSecretsFile(cmd.SecretsFile) + if err != nil { + return err + } + for k, v := range parsed { + cmd.SecretsEnv = append(cmd.SecretsEnv, k+"="+v) + } + return nil +} + +func (cmd *UpCmd) resolveFeatureSecretsFile() { + if cmd.FeatureSecretsFile == "" { + cmd.FeatureSecretsFile = os.Getenv("DEVCONTAINER_SECRETS_FILE") + } + if cmd.FeatureSecretsFile != "" { + cmd.CLIOptions.FeatureSecretsFile = cmd.FeatureSecretsFile + } +} + func (cmd *UpCmd) prepareBootstrapGitToken( ctx context.Context, devsyConfig *config.Config, @@ -322,6 +342,15 @@ func (cmd *UpCmd) resolveStoredSecrets( } return cmd.applyEnvVars(ctx, devsyConfig, resolver) } + return cmd.resolveWithStore(ctx, devsyConfig, project, requests) +} + +func (cmd *UpCmd) resolveWithStore( + ctx context.Context, + devsyConfig *config.Config, + project *projectSecretContext, + requests []secretRequest, +) error { resolver, err := secrets.NewResolverForConfig(devsyConfig, cmd.unlockOptions()) if err != nil { return err @@ -395,27 +424,9 @@ func collectEnvVarRequests(flags []string, devsyConfig *config.Config) ([]envVar explicit = append(explicit, req) explicitRefs[req.ref.String()] = struct{}{} } - implicit := make([]envVarRequest, 0) - if current := devsyConfig.Current(); current != nil { - for _, name := range current.EnvVars { - ref, err := secrets.ParseRef(name) - if err != nil { - return nil, fmt.Errorf("invalid attached environment variable %q: %w", name, err) - } - if ref.Source != secrets.LocalSourceName || - (ref.Type != "" && ref.Type != secrets.LocalSourceName) { - return nil, fmt.Errorf( - "attached environment variable %q must use the local Devsy store", - name, - ) - } - if _, overridden := explicitRefs[ref.String()]; overridden { - continue - } - implicit = append(implicit, envVarRequest{ - ref: ref, target: ref.Name, origin: envVarAttached, - }) - } + implicit, err := attachedEnvVarRequests(devsyConfig, explicitRefs) + if err != nil { + return nil, err } sort.Slice( implicit, @@ -424,28 +435,69 @@ func collectEnvVarRequests(flags []string, devsyConfig *config.Config) ([]envVar return append(implicit, explicit...), nil } -func parseEnvVarRequest(entry string) (envVarRequest, error) { - name, target, hasTarget := strings.Cut(entry, "=") +func attachedEnvVarRequests( + devsyConfig *config.Config, + explicitRefs map[string]struct{}, +) ([]envVarRequest, error) { + implicit := make([]envVarRequest, 0) + current := devsyConfig.Current() + if current == nil { + return implicit, nil + } + for _, name := range current.EnvVars { + ref, err := secrets.ParseRef(name) + if err != nil { + return nil, fmt.Errorf("invalid attached environment variable %q: %w", name, err) + } + if !isLocalStoreRef(ref) { + return nil, fmt.Errorf( + "attached environment variable %q must use the local Devsy store", + name, + ) + } + if _, overridden := explicitRefs[ref.String()]; overridden { + continue + } + implicit = append(implicit, envVarRequest{ + ref: ref, target: ref.Name, origin: envVarAttached, + }) + } + return implicit, nil +} + +func isLocalStoreRef(ref secrets.SecretRef) bool { + return ref.Source == secrets.LocalSourceName && + (ref.Type == "" || ref.Type == secrets.LocalSourceName) +} + +func validateEnvTarget(entry, name, target string, hasTarget bool) error { if hasTarget && target == "" { - return envVarRequest{}, fmt.Errorf( + return fmt.Errorf( "invalid --env %q: target after %q= must not be empty", entry, name, ) } if hasTarget && strings.Contains(target, "=") { - return envVarRequest{}, fmt.Errorf( + return fmt.Errorf( "invalid --env %q: target %q must not contain =", entry, target, ) } + return nil +} + +func parseEnvVarRequest(entry string) (envVarRequest, error) { + name, target, hasTarget := strings.Cut(entry, "=") + if err := validateEnvTarget(entry, name, target, hasTarget); err != nil { + return envVarRequest{}, err + } ref, err := secrets.ParseRef(name) if err != nil { return envVarRequest{}, err } - if ref.Source != secrets.LocalSourceName || - (ref.Type != "" && ref.Type != secrets.LocalSourceName) { + if !isLocalStoreRef(ref) { return envVarRequest{}, fmt.Errorf( "--env only accepts Devsy-managed values; use --secret %s instead", name, diff --git a/desktop/e2e/fixtures/mock-devsy.cjs b/desktop/e2e/fixtures/mock-devsy.cjs index 4e39f3130c..ca9f7f82ac 100755 --- a/desktop/e2e/fixtures/mock-devsy.cjs +++ b/desktop/e2e/fixtures/mock-devsy.cjs @@ -597,6 +597,93 @@ function handleRename(args) { out("") } +// Retained setup history for `workspace logs --source setup`. Two fixed runs +// for ws-001 let Desktop specs exercise list, read, hook filters and the +// read-only branch without a real store. +const SETUP_RUNS = { + "11111111-1111-4111-8111-111111111111": { + runId: "11111111-1111-4111-8111-111111111111", + context: "default", + workspaceUid: "ws-001", + workspaceIdAtStart: "example-repo", + command: "up", + createdAt: "2026-10-30T10:00:00Z", + commandState: "failed", + captureState: "complete", + }, + "22222222-2222-4222-8222-222222222222": { + runId: "22222222-2222-4222-8222-222222222222", + context: "default", + workspaceUid: "ws-gone", + workspaceIdAtStart: "deleted-project", + command: "up", + createdAt: "2026-10-29T10:00:00Z", + commandState: "failed", + captureState: "complete", + }, +} +const SETUP_PRODUCER = "b0000000-0000-4000-8000-000000000001" +const SETUP_INV = "a1111111-0000-4000-8000-000000000001" + +function setupRecords(runId) { + let sequence = 0 + const rec = (patch) => ({ + schemaVersion: 1, + runId, + producerId: SETUP_PRODUCER, + sequence: ++sequence, + time: "2026-10-30T10:00:01Z", + ...patch, + }) + return [ + rec({ kind: "invocation", invocationId: SETUP_INV, hook: "postCreateCommand", phase: "lifecycle", state: "running" }), + rec({ kind: "output", invocationId: SETUP_INV, stream: "stdout", text: "npm install\n", bytes: 12 }), + rec({ kind: "output", invocationId: SETUP_INV, stream: "stderr", text: "{\"level\":\"error\",\"msg\":\"literal\"}\n", bytes: 33 }), + rec({ kind: "invocation", invocationId: SETUP_INV, hook: "postCreateCommand", phase: "lifecycle", state: "failed", exitCode: 1 }), + ] +} + +function handleSetupLogs(args) { + const flag = (name) => { + const i = args.indexOf(name) + return i === -1 ? undefined : args[i + 1] + } + if (args.includes("--list")) { + // A leading workspace argument limits the index to that workspace. + const scoped = args.length > 0 && !args[0].startsWith("-") + const runs = Object.values(SETUP_RUNS).filter( + (r) => + r.context === variableContext && + !(scoped && r.workspaceUid === "ws-gone"), + ) + out({ schemaVersion: 1, runs, hasMore: false }) + return + } + const run = SETUP_RUNS[flag("--run")] + if (!run) { + process.stderr.write("mock-devsy: run not found\n") + process.exit(1) + } + out({ + schemaVersion: 1, + run, + records: setupRecords(run.runId), + nextCursor: "mock-cursor", + hasMore: false, + complete: true, + }) +} + +function handleLogs(args) { + if (flagValue(args, "--source") === "setup") handleSetupLogs(args) + else out("") +} + +function flagValue(args, name) { + const i = args.indexOf(name) + return i === -1 ? undefined : args[i + 1] +} + // Unimplemented workspace verbs: exit 0 with empty output. function handleNoop() { out("") @@ -613,7 +700,7 @@ const workspaceHandlers = { stop: handleStop, delete: handleDelete, rename: handleRename, - logs: handleNoop, + logs: handleLogs, exec: handleNoop, build: handleNoop, export: handleNoop, diff --git a/desktop/e2e/workspace-logs.e2e.ts b/desktop/e2e/workspace-logs.e2e.ts new file mode 100644 index 0000000000..6faa083962 --- /dev/null +++ b/desktop/e2e/workspace-logs.e2e.ts @@ -0,0 +1,62 @@ +import type { ElectronApplication, Page } from "@playwright/test" +import { expect, test } from "@playwright/test" +import { launchApp, resetMockState } from "./electron-app.js" + +const FAILED_RUN = "11111111-1111-4111-8111-111111111111" +const REMOVED_RUN = "22222222-2222-4222-8222-222222222222" +const INVOCATION = "a1111111-0000-4000-8000-000000000001" + +let app: ElectronApplication +let page: Page + +test.beforeAll(async () => { + resetMockState() + ;({ app, page } = await launchApp()) +}) +test.afterAll(async () => { + await app?.close() +}) + +test("shows retained setup history in the Logs tab", async () => { + await page.evaluate(() => { + window.location.hash = "#/workspaces/test-workspace?tab=logs" + }) + const history = page.getByTestId("workspace-run-logs") + await expect(history).toBeVisible() + const steps = history.getByRole("list", { name: "Build and lifecycle steps" }) + await expect(steps.getByText("postCreateCommand")).toBeVisible() + await expect(steps.getByText("Failed")).toBeVisible() + await expect(history.getByText("npm install")).toBeVisible() + // Output that looks like a log line stays literal text. + await expect( + history.getByText('{"level":"error","msg":"literal"}'), + ).toBeVisible() + await expect(page.getByText("Desktop diagnostic files")).toBeVisible() +}) + +test("opens a pinned run and invocation from a View logs link", async () => { + const route = `#/workspaces/test-workspace?tab=logs&context=default&run=${FAILED_RUN}&invocation=${INVOCATION}` + await page.evaluate((hash) => { + window.location.hash = hash + }, route) + const history = page.getByTestId("workspace-run-logs") + await expect(history.getByText("npm install")).toBeVisible() + await expect(history.getByLabel("Run")).toHaveValue(FAILED_RUN) +}) + +test("renders a removed workspace's run read-only", async () => { + const route = `#/workspaces/deleted-project?tab=logs&context=default&run=${REMOVED_RUN}` + await page.evaluate((hash) => { + window.location.hash = hash + }, route) + await expect(page.getByText(/no longer exists/)).toBeVisible() + await expect(page.getByText(/Read-only history/)).toBeVisible() + await expect(page.getByRole("button", { name: "Start" })).toHaveCount(0) +}) + +test("keeps the plain not-found view when no run is linked", async () => { + await page.evaluate(() => { + window.location.hash = "#/workspaces/never-existed" + }) + await expect(page.getByText("Workspace not found.")).toBeVisible() +}) diff --git a/desktop/src/main/__tests__/app-route.test.ts b/desktop/src/main/__tests__/app-route.test.ts index aec7c74356..76a0377a74 100644 --- a/desktop/src/main/__tests__/app-route.test.ts +++ b/desktop/src/main/__tests__/app-route.test.ts @@ -4,6 +4,7 @@ import { isAppRoute, newWorkspaceRoute, settingsRoute, + workspaceLogsRoute, workspaceRoute, workspacesRoute, } from "../../shared/app-route.js" @@ -32,3 +33,30 @@ describe("application routes", () => { expect(isAppRoute("/settings#external")).toBe(false) }) }) + +describe("workspace logs route", () => { + const run = "11111111-1111-4111-8111-111111111111" + const inv = "a1111111-0000-4000-8000-000000000001" + + it("encodes context, run and invocation as query parameters", () => { + const route = workspaceLogsRoute("api", { + context: "my ctx&x", + runId: run, + invocationId: inv, + }) + expect(isAppRoute(route)).toBe(true) + const url = new URL(route, "http://x") + expect(url.pathname).toBe("/workspaces/api") + expect(url.searchParams.get("tab")).toBe("logs") + expect(url.searchParams.get("context")).toBe("my ctx&x") + expect(url.searchParams.get("run")).toBe(run) + expect(url.searchParams.get("invocation")).toBe(inv) + }) + + it("keeps the plain logs route when no run is known", () => { + expect(workspaceLogsRoute("api")).toBe("/workspaces/api?tab=logs") + expect(workspaceLogsRoute("api", { invocationId: inv })).toBe( + "/workspaces/api?tab=logs", + ) + }) +}) diff --git a/desktop/src/main/__tests__/cli-error-envelope.test.ts b/desktop/src/main/__tests__/cli-error-envelope.test.ts index 018bfecdd9..5c87f3e60e 100644 --- a/desktop/src/main/__tests__/cli-error-envelope.test.ts +++ b/desktop/src/main/__tests__/cli-error-envelope.test.ts @@ -215,3 +215,44 @@ describe("CLI envelopes", () => { expect(cliErrorFromLegacy(structured)?.context).toEqual({ attempt: "1" }) }) }) + +describe("run identity compatibility", () => { + const run = "11111111-1111-4111-8111-111111111111" + + it("keeps status envelope schema v1 strict", () => { + const status = { + kind: "status", + schemaVersion: 1, + phase: "building_image", + state: "started", + } + expect(parseCliEnvelope(JSON.stringify(status))).toBeDefined() + expect( + parseCliEnvelope(JSON.stringify({ ...status, runId: run })), + ).toBeUndefined() + }) + + it("parses result envelopes with and without a run ID", () => { + const result = { + kind: "result", + outcome: "success", + containerId: "c", + remoteUser: "u", + remoteWorkspaceFolder: "/w", + } + expect(parseCliEnvelope(JSON.stringify(result))).toMatchObject(result) + expect( + parseCliEnvelope(JSON.stringify({ ...result, runId: run })), + ).toMatchObject({ runId: run }) + }) + + it("carries run identity in string-valued error context", () => { + const err = cliErrorFromEnvelope({ + kind: "error", + outcome: "error", + message: "hook failed", + context: { runId: run, failedInvocationId: run }, + }) + expect(err?.context).toEqual({ runId: run, failedInvocationId: run }) + }) +}) diff --git a/desktop/src/main/__tests__/fixtures/go-index-response.json b/desktop/src/main/__tests__/fixtures/go-index-response.json new file mode 100644 index 0000000000..0ed6d49ec1 --- /dev/null +++ b/desktop/src/main/__tests__/fixtures/go-index-response.json @@ -0,0 +1,15 @@ +{ + "hasMore": false, + "runs": [ + { + "captureState": "complete", + "command": "up", + "commandState": "running", + "context": "default", + "createdAt": "2026-10-30T12:00:00Z", + "runId": "00000000-0000-4000-8000-000000000001", + "workspaceUid": "uid" + } + ], + "schemaVersion": 1 +} diff --git a/desktop/src/main/__tests__/fixtures/go-read-response.json b/desktop/src/main/__tests__/fixtures/go-read-response.json new file mode 100644 index 0000000000..31964cde98 --- /dev/null +++ b/desktop/src/main/__tests__/fixtures/go-read-response.json @@ -0,0 +1,117 @@ +{ + "availability": [ + { + "available": true, + "producerId": "00000000-0000-4000-8000-000000000001" + } + ], + "complete": true, + "hasMore": false, + "nextCursor": "opaque-cursor", + "records": [ + { + "hook": "postCreateCommand", + "invocationId": "00000000-0000-4000-8000-000000000002", + "kind": "invocation", + "phase": "setup", + "producerId": "00000000-0000-4000-8000-000000000001", + "runId": "00000000-0000-4000-8000-000000000003", + "schemaVersion": 1, + "sequence": 1, + "state": "running", + "time": "2026-10-30T12:00:00Z" + }, + { + "hook": "postCreateCommand", + "invocationId": "00000000-0000-4000-8000-000000000002", + "kind": "invocation", + "phase": "setup", + "producerId": "00000000-0000-4000-8000-000000000001", + "runId": "00000000-0000-4000-8000-000000000003", + "schemaVersion": 1, + "sequence": 2, + "state": "succeeded", + "time": "2026-10-30T12:00:00Z" + }, + { + "hook": "postStartCommand", + "invocationId": "00000000-0000-4000-8000-000000000004", + "kind": "invocation", + "phase": "setup", + "producerId": "00000000-0000-4000-8000-000000000001", + "runId": "00000000-0000-4000-8000-000000000003", + "schemaVersion": 1, + "sequence": 3, + "state": "running", + "time": "2026-10-30T12:00:00Z" + }, + { + "bytes": 6, + "invocationId": "00000000-0000-4000-8000-000000000004", + "kind": "output", + "producerId": "00000000-0000-4000-8000-000000000001", + "runId": "00000000-0000-4000-8000-000000000003", + "schemaVersion": 1, + "sequence": 4, + "stream": "stdout", + "text": "out-1\n", + "time": "2026-10-30T12:00:00Z" + }, + { + "bytes": 6, + "invocationId": "00000000-0000-4000-8000-000000000004", + "kind": "output", + "producerId": "00000000-0000-4000-8000-000000000001", + "runId": "00000000-0000-4000-8000-000000000003", + "schemaVersion": 1, + "sequence": 5, + "stream": "stderr", + "text": "err-1\n", + "time": "2026-10-30T12:00:00Z" + }, + { + "bytes": 6, + "invocationId": "00000000-0000-4000-8000-000000000004", + "kind": "output", + "producerId": "00000000-0000-4000-8000-000000000001", + "runId": "00000000-0000-4000-8000-000000000003", + "schemaVersion": 1, + "sequence": 6, + "stream": "stdout", + "text": "out-2\n", + "time": "2026-10-30T12:00:00Z" + }, + { + "hook": "postStartCommand", + "invocationId": "00000000-0000-4000-8000-000000000004", + "kind": "invocation", + "phase": "setup", + "producerId": "00000000-0000-4000-8000-000000000001", + "runId": "00000000-0000-4000-8000-000000000003", + "schemaVersion": 1, + "sequence": 7, + "state": "failed", + "time": "2026-10-30T12:00:00Z" + }, + { + "discoverySealed": true, + "kind": "producer", + "producerId": "00000000-0000-4000-8000-000000000001", + "producerState": "terminal", + "runId": "00000000-0000-4000-8000-000000000003", + "schemaVersion": 1, + "sequence": 8, + "time": "2026-10-30T12:00:00Z" + } + ], + "run": { + "captureState": "complete", + "command": "up", + "commandState": "running", + "context": "default", + "createdAt": "2026-10-30T12:00:00Z", + "runId": "00000000-0000-4000-8000-000000000003", + "workspaceUid": "uid" + }, + "schemaVersion": 1 +} diff --git a/desktop/src/main/__tests__/ipc-workspace-jobs.test.ts b/desktop/src/main/__tests__/ipc-workspace-jobs.test.ts index 785d6c1b38..5e8a9ad08c 100644 --- a/desktop/src/main/__tests__/ipc-workspace-jobs.test.ts +++ b/desktop/src/main/__tests__/ipc-workspace-jobs.test.ts @@ -98,6 +98,11 @@ function invoke(channel: string, args: unknown = { workspaceId: "ws" }) { if (!handler) throw new Error(`No handler registered for ${channel}`) return handler(trustedEvent, args) } +const RESULT_LINE = + '{"kind":"result","outcome":"success","containerId":"c","remoteUser":"u","remoteWorkspaceFolder":"/w"}' +const errorLine = (code: string, message: string) => + JSON.stringify({ kind: "error", outcome: "error", code, message }) +const DETACHED = new Set(["rebuild", "reset"]) async function flush() { for (let i = 0; i < 20; i++) await Promise.resolve() } @@ -131,7 +136,13 @@ describe("workspace lifecycle IPC", () => { expect(ctx.cli.runStreaming).not.toHaveBeenCalled() release() await flush() - ctx.exit(0) + // Rebuild and reset follow a detached task and end on its result. + if (DETACHED.has(action)) { + expect(ctx.cli.run).toHaveBeenCalledWith( + expect.arrayContaining(["workspace", "up", "ws", "--detach"]), + ) + ctx.line(RESULT_LINE) + } else ctx.exit(0) await flush() expect(ctx.jobs.get("ws")?.state).toBe("succeeded") }, @@ -526,7 +537,11 @@ describe("workspace lifecycle IPC", () => { ), ).not.toThrow() expect(ctx.jobs.get("ws")?.state).toBe("running") - ctx.exit(exitCode) + ctx.line( + exitCode === 0 + ? RESULT_LINE + : errorLine("action_failed", "Command exited with code 1"), + ) await flush() expect(ctx.jobs.get("ws")).toMatchObject({ state: exitCode === 0 ? "succeeded" : "failed", @@ -543,10 +558,11 @@ describe("workspace lifecycle IPC", () => { ctx.closeLog.mockRejectedValueOnce(new Error("disk unavailable")) invoke("workspace_rebuild") await flush() - ctx.exit(exitCode, { - code: "action_failed", - message: "Cannot rebuild private-token", - }) + ctx.line( + exitCode === 0 + ? RESULT_LINE + : errorLine("action_failed", "Cannot rebuild private-token"), + ) await flush() expect(ctx.jobs.get("ws")).toMatchObject({ state: exitCode === 0 ? "reconciling" : "failed", @@ -585,4 +601,127 @@ describe("workspace lifecycle IPC", () => { await completion expect(ctx.jobs.get("ws")?.state).toBe("succeeded") }) + + describe("run identity", () => { + const RUN = "11111111-1111-4111-8111-111111111111" + const INV = "a1111111-0000-4000-8000-000000000001" + + it.each([ + ["rebuild", "--recreate"], + ["reset", "--reset"], + ])( + "submits %s as a detached up and keeps the run ID", + async (action, flag) => { + const ctx = setup() + ctx.cli.run.mockResolvedValue({ + kind: "task", + id: "task-9", + runId: RUN, + }) + invoke(`workspace_${action}`) + await flush() + expect(ctx.cli.run).toHaveBeenCalledWith([ + "workspace", + "up", + "ws", + flag, + "--detach", + ]) + expect(ctx.cli.runStreaming.mock.calls[0][0]).toEqual([ + "workspace", + "task", + "logs", + "task-9", + "--follow", + ]) + expect(ctx.jobs.get("ws")?.runId).toBe(RUN) + ctx.line(RESULT_LINE) + await flush() + expect(ctx.jobs.get("ws")).toMatchObject({ + state: "succeeded", + runId: RUN, + }) + }, + ) + + it("takes the run ID from a result envelope when submission lacks one", async () => { + const ctx = setup() + invoke("workspace_rebuild") + await flush() + expect(ctx.jobs.get("ws")?.runId).toBeUndefined() + ctx.line(RESULT_LINE.replace("}", `,"runId":"${RUN}"}`)) + await flush() + expect(ctx.jobs.get("ws")?.runId).toBe(RUN) + }) + + it("takes run and failed invocation from error context", async () => { + const ctx = setup() + invoke("workspace_reset") + await flush() + ctx.line( + JSON.stringify({ + kind: "error", + outcome: "error", + code: "hook_failed", + message: "postCreateCommand failed", + context: { runId: RUN, failedInvocationId: INV }, + }), + ) + await flush() + expect(ctx.jobs.get("ws")).toMatchObject({ + state: "failed", + runId: RUN, + failedInvocationId: INV, + }) + }) + + it("keeps an old-envelope failure usable without run identity", async () => { + const ctx = setup() + invoke("workspace_rebuild") + await flush() + ctx.line(errorLine("x", "plain failure")) + await flush() + const job = ctx.jobs.get("ws") + expect(job).toMatchObject({ state: "failed", error: "plain failure" }) + expect(job?.runId).toBeUndefined() + }) + + it("ignores identity that is not a canonical UUID", async () => { + const ctx = setup() + ctx.cli.run.mockResolvedValue({ kind: "task", id: "t", runId: "op-1" }) + invoke("workspace_rebuild") + await flush() + expect(ctx.jobs.get("ws")?.runId).toBeUndefined() + }) + }) + + describe("history reader", () => { + it("cancels only readers and leaves the task follower running", async () => { + const ctx = setup() + invoke("workspace_rebuild") + await flush() + const follower = ctx.actions.tunnelProcesses.get("ws") + expect(follower).toBeDefined() + ctx.cli.cancelFor.mockClear() + await invoke("workspace_log_run_cancel") + expect(ctx.cli.cancelFor).not.toHaveBeenCalled() + expect(ctx.actions.tunnelProcesses.get("ws")).toBe(follower) + expect(ctx.jobs.get("ws")?.state).toBe("running") + }) + + it("returns null for a superseded read instead of an error", async () => { + const ctx = setup() + ctx.cli.run.mockImplementation(() => new Promise(() => undefined)) + const request = { + context: "default", + runId: "11111111-1111-4111-8111-111111111111", + } + const first = invoke( + "workspace_log_run_read", + request, + ) as Promise + await invoke("workspace_log_run_cancel") + await expect(first).resolves.toBeNull() + }) + }) }) diff --git a/desktop/src/main/__tests__/workspace-jobs.test.ts b/desktop/src/main/__tests__/workspace-jobs.test.ts index 04ecd95603..cfcb58125f 100644 --- a/desktop/src/main/__tests__/workspace-jobs.test.ts +++ b/desktop/src/main/__tests__/workspace-jobs.test.ts @@ -119,4 +119,21 @@ describe("WorkspaceJobs", () => { await jobs.finish("ws", generation) expect(jobs.generation("ws")).not.toBe(generation) }) + + it("associates a run with the owning command only", async () => { + const jobs = new WorkspaceJobs() + const run = "11111111-1111-4111-8111-111111111111" + const other = "22222222-2222-4222-8222-222222222222" + const generation = jobs.start("ws", "rebuilding", "cmd") + jobs.setRun("ws", "stale", { runId: other }) + expect(jobs.get("ws")?.runId).toBeUndefined() + jobs.setRun("ws", "cmd", { runId: run }) + jobs.setRun("ws", "cmd", { runId: other }) + jobs.setRun("ws", "cmd", { runId: run, failedInvocationId: other }) + await jobs.finish("ws", generation, "failed") + expect(jobs.get("ws")).toMatchObject({ + runId: run, + failedInvocationId: other, + }) + }) }) diff --git a/desktop/src/main/__tests__/workspace-logs-contract.test.ts b/desktop/src/main/__tests__/workspace-logs-contract.test.ts new file mode 100644 index 0000000000..eb5a15abef --- /dev/null +++ b/desktop/src/main/__tests__/workspace-logs-contract.test.ts @@ -0,0 +1,336 @@ +import { describe, expect, it } from "vitest" +import { + MOCK_INV_POST_CREATE_OK, + MOCK_INV_SILENT_OK, + MOCK_INV_UNCAPTURED, + MOCK_RUN_FAILED, + MOCK_RUN_OK, + MOCK_RUN_PARTIAL, + mockLogRunRead, + mockLogRuns, +} from "../../renderer/src/lib/ipc/mock.js" +import { + runIdentityFromContext, + runIdFromPayload, +} from "../../shared/cli-error.js" +import { + InvocationTable, + type LogRecord, + outputPresence, + parseIndexResponse, + parseReadResponse, + TranscriptBuilder, + validateRunExportRequest, + validateRunReadRequest, + validateRunsRequest, + WorkspaceLogContractError, +} from "../../shared/workspace-logs.js" + +const RUN = "11111111-1111-4111-8111-111111111111" +const INV = "a1111111-0000-4000-8000-000000000001" +const PROD = "b0000000-0000-4000-8000-000000000001" + +function output(sequence: number, text: string, stream = "stdout"): LogRecord { + return { + schemaVersion: 1, + kind: "output", + runId: RUN, + producerId: PROD, + sequence, + time: "2026-10-30T00:00:00Z", + invocationId: INV, + stream: stream as LogRecord["stream"], + text, + } +} + +describe("run identity parsing", () => { + it("accepts string-valued run context and drops anything else", () => { + expect( + runIdentityFromContext({ runId: RUN, failedInvocationId: INV }), + ).toEqual({ runId: RUN, failedInvocationId: INV }) + expect(runIdentityFromContext({ runId: "op-1" })).toEqual({}) + expect(runIdentityFromContext({ failedInvocationId: INV })).toEqual({}) + expect(runIdentityFromContext(undefined)).toEqual({}) + }) + + it("reads an optional run ID from result and submission payloads", () => { + expect(runIdFromPayload({ kind: "result", runId: RUN })).toBe(RUN) + expect(runIdFromPayload({ kind: "task", id: "t" })).toBeUndefined() + expect(runIdFromPayload({ runId: 7 })).toBeUndefined() + expect(runIdFromPayload(null)).toBeUndefined() + }) +}) + +describe("request validation", () => { + it("rejects selectors that could reach the command line as flags", () => { + expect(() => validateRunsRequest({ context: "--help" })).toThrow( + WorkspaceLogContractError, + ) + expect(() => + validateRunReadRequest({ context: "default", runId: "latest" }), + ).toThrow("runId") + expect(() => + validateRunReadRequest({ context: "default", runId: RUN, hook: "x" }), + ).toThrow("hook") + expect(() => + validateRunReadRequest({ + context: "default", + runId: RUN, + after: "../../etc", + }), + ).toThrow("cursor") + expect(() => + validateRunReadRequest({ context: "default", runId: RUN, limit: 5000 }), + ).toThrow("limit") + }) + + it("accepts a bounded, well-formed request", () => { + expect( + validateRunReadRequest({ + context: "default", + runId: RUN, + hook: "postCreateCommand", + after: "abc_-=", + limit: 500, + }), + ).toMatchObject({ hook: "postCreateCommand", limit: 500 }) + expect(() => + validateRunExportRequest({ + context: "default", + runId: RUN, + hook: "postCreateCommand", + invocationId: INV, + }), + ).toThrow("cannot be combined") + }) +}) + +describe("response parsing", () => { + it("parses mock fixtures that mirror the Go contract", () => { + const res = parseReadResponse(mockLogRunRead({ runId: MOCK_RUN_FAILED })) + expect(res.records.length).toBeGreaterThan(0) + expect( + parseIndexResponse(mockLogRuns({ context: "default" })).runs, + ).toHaveLength(4) + }) + + it("rejects wrong schema versions, null records and foreign records", () => { + const good = mockLogRunRead({ runId: MOCK_RUN_FAILED }) + expect(() => parseReadResponse({ ...good, schemaVersion: 2 })).toThrow( + "schema", + ) + expect(() => parseReadResponse({ ...good, records: null })).toThrow("array") + const foreign = { ...good.records[0], runId: MOCK_RUN_OK } + expect(() => parseReadResponse({ ...good, records: [foreign] })).toThrow( + "different run", + ) + expect(() => + parseReadResponse({ + ...good, + records: [{ ...good.records[0], kind: "bogus" }], + }), + ).toThrow("record kind") + }) +}) + +function view(map: Map, id: string): T { + const found = map.get(id) + if (!found) throw new Error(`missing invocation ${id}`) + return found +} + +describe("invocation table", () => { + const views = (runId: string) => { + const table = new InvocationTable() + const res = mockLogRunRead({ runId }) + table.apply(res.records) + table.markUnavailable( + (res.availability ?? []) + .filter((a) => !a.available) + .map((a) => a.producerId), + ) + return new Map(table.list().map((v) => [v.id, v])) + } + + it("separates zero-byte success from output never captured", () => { + const ok = views(MOCK_RUN_OK) + expect(outputPresence(view(ok, MOCK_INV_SILENT_OK))).toBe("empty") + expect(outputPresence(view(ok, MOCK_INV_POST_CREATE_OK))).toBe("has-output") + const partial = views(MOCK_RUN_PARTIAL) + expect(outputPresence(view(partial, MOCK_INV_UNCAPTURED))).toBe( + "not-captured", + ) + }) + + it("keeps deferred, skipped and failed states with reasons", () => { + const failed = [...views(MOCK_RUN_FAILED).values()] + expect(failed.map((v) => v.state)).toEqual(["failed", "skipped"]) + expect(failed[0].exitCode).toBe(1) + expect(failed[1].reason).toBe("already_attempted") + const states = [...views(MOCK_RUN_OK).values()].map((v) => v.state) + expect(states).toContain("deferred") + }) + + it("orders transitions by sequence, not arrival", () => { + const table = new InvocationTable() + const base = { + schemaVersion: 1, + kind: "invocation" as const, + runId: RUN, + producerId: PROD, + time: "t", + invocationId: INV, + phase: "lifecycle", + hook: "postCreateCommand", + } + table.apply([ + { ...base, sequence: 3, state: "succeeded", exitCode: 0 }, + { ...base, sequence: 2, state: "running" }, + ]) + expect(table.list()[0].state).toBe("succeeded") + }) + + it("keeps a worker result recorded at a lower sequence than the parent plan", () => { + const table = new InvocationTable() + const base = { + schemaVersion: 1, + kind: "invocation" as const, + runId: RUN, + invocationId: INV, + phase: "lifecycle", + hook: "postCreateCommand", + } + const worker = "b0000000-0000-4000-8000-000000000002" + table.apply([ + { + ...base, + producerId: PROD, + sequence: 9, + time: "2026-01-01T00:00:01Z", + state: "deferred", + }, + { + ...base, + producerId: worker, + sequence: 2, + time: "2026-01-01T00:00:05Z", + state: "failed", + exitCode: 4, + }, + ]) + const [view] = table.list() + expect(view.state).toBe("failed") + expect(view.exitCode).toBe(4) + }) + + it("shows unfinished steps as completion unknown once the run is settled", () => { + const base = { + schemaVersion: 1, + kind: "invocation" as const, + runId: RUN, + producerId: PROD, + time: "t", + phase: "lifecycle", + hook: "postCreateCommand", + } + const table = new InvocationTable() + table.apply([ + { ...base, invocationId: INV, sequence: 1, state: "running" }, + { + ...base, + invocationId: "c0000000-0000-4000-8000-000000000003", + sequence: 2, + state: "pending", + }, + { + ...base, + invocationId: "c0000000-0000-4000-8000-000000000004", + sequence: 3, + state: "skipped", + reason: "not_reached", + }, + ]) + expect(table.list().map((v) => v.state)).toEqual([ + "running", + "pending", + "skipped", + ]) + table.markSettled(true) + expect(table.list().map((v) => v.state)).toEqual([ + "completion_unknown", + "completion_unknown", + "skipped", + ]) + }) + + it("does not let a later marker skip replace an executed result", () => { + const table = new InvocationTable() + const base = { + schemaVersion: 1, + kind: "invocation" as const, + runId: RUN, + invocationId: INV, + phase: "lifecycle", + hook: "postCreateCommand", + } + const worker = "b0000000-0000-4000-8000-000000000002" + table.apply([ + { + ...base, + producerId: PROD, + sequence: 4, + time: "2026-01-01T00:00:01Z", + state: "succeeded", + exitCode: 0, + }, + { + ...base, + producerId: worker, + sequence: 1, + time: "2026-01-01T00:00:09Z", + state: "skipped", + reason: "already_attempted", + }, + ]) + const [view] = table.list() + expect(view.state).toBe("succeeded") + expect(view.exitCode).toBe(0) + }) +}) + +describe("transcript builder", () => { + it("treats JSON, HTML and log-looking text as literal lines", () => { + const text = + '{"level":"error","msg":"x"}\nbold\n2026-01-01 INFO hi\n' + const builder = new TranscriptBuilder() + builder.append([output(1, text)]) + expect(builder.snapshot().map((l) => l.text)).toEqual([ + '{"level":"error","msg":"x"}', + "bold", + "2026-01-01 INFO hi", + ]) + }) + + it("preserves blank lines, indentation and a final fragment", () => { + const builder = new TranscriptBuilder() + builder.append([output(1, "a\n\n b\npart")]) + builder.append([output(2, "ial")]) + expect(builder.snapshot().map((l) => l.text)).toEqual([ + "a", + "", + " b", + "partial", + ]) + }) + + it("keeps stream provenance and does not merge interleaved streams", () => { + const builder = new TranscriptBuilder() + builder.append([output(1, "out-", "stdout"), output(2, "err\n", "stderr")]) + builder.append([output(3, "rest\n", "stdout")]) + expect(builder.snapshot().map((l) => `${l.stream}:${l.text}`)).toEqual([ + "stdout:out-rest", + "stderr:err", + ]) + }) +}) diff --git a/desktop/src/main/__tests__/workspace-logs-golden.test.ts b/desktop/src/main/__tests__/workspace-logs-golden.test.ts new file mode 100644 index 0000000000..7d0805282a --- /dev/null +++ b/desktop/src/main/__tests__/workspace-logs-golden.test.ts @@ -0,0 +1,41 @@ +import { readFileSync } from "node:fs" +import { join } from "node:path" +import { describe, expect, it } from "vitest" +import { + parseIndexResponse, + parseReadResponse, +} from "../../shared/workspace-logs.js" + +// The fixtures are written by the Go test TestGoldenResponsesMatchDesktopFixtures +// from real CLI output, so a field rename on either side fails here. +function fixture(name: string): unknown { + return JSON.parse(readFileSync(join(__dirname, "fixtures", name), "utf8")) +} + +describe("Go workspace log responses", () => { + it("parses a real read response", () => { + const response = parseReadResponse(fixture("go-read-response.json")) + + expect(response.complete).toBe(true) + expect(response.hasMore).toBe(false) + const output = response.records.filter((r) => r.kind === "output") + expect(output.map((r) => (r.kind === "output" ? r.text : ""))).toEqual([ + "out-1\n", + "err-1\n", + "out-2\n", + ]) + const states = response.records + .filter((r) => r.kind === "invocation") + .map((r) => (r.kind === "invocation" ? r.state : "")) + expect(states).toContain("failed") + expect(states).toContain("succeeded") + }) + + it("parses a real index response", () => { + const response = parseIndexResponse(fixture("go-index-response.json")) + + expect(response.runs).toHaveLength(1) + expect(response.runs[0].commandState).toBeTruthy() + expect(response.hasMore).toBe(false) + }) +}) diff --git a/desktop/src/main/__tests__/workspace-logs.test.ts b/desktop/src/main/__tests__/workspace-logs.test.ts new file mode 100644 index 0000000000..82d3175a7a --- /dev/null +++ b/desktop/src/main/__tests__/workspace-logs.test.ts @@ -0,0 +1,323 @@ +// @vitest-environment node +import { mkdtemp, readdir, readFile, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" +import { + MOCK_RUN_FAILED, + MOCK_RUN_OK, + MOCK_RUN_PARTIAL, + mockLogRunRead, + mockLogRuns, +} from "../../renderer/src/lib/ipc/mock.js" +import type { LogReadResponse } from "../../shared/workspace-logs.js" +import { + MAX_RESPONSE_BYTES, + WorkspaceLogError, + WorkspaceLogReader, +} from "../workspace-logs.js" + +type Run = ( + args: string[], + options?: { maxBuffer?: number; signal?: AbortSignal }, +) => Promise + +function reader(run: Run, timeoutMs?: number) { + const cli = { run: vi.fn(run) as never } + return { + reader: new WorkspaceLogReader(cli, { timeoutMs }), + cli: cli as { run: ReturnType }, + } +} + +const readArgs = { context: "default", runId: MOCK_RUN_FAILED } + +describe("WorkspaceLogReader", () => { + it("builds the setup-source command with a byte cap", async () => { + const { reader: r, cli } = reader(async () => + mockLogRunRead({ runId: MOCK_RUN_FAILED }), + ) + await r.read({ + ...readArgs, + workspaceId: "my-project", + hook: "postCreateCommand", + after: "abc", + limit: 100, + }) + expect(cli.run).toHaveBeenCalledWith( + [ + "workspace", + "logs", + "my-project", + "--source", + "setup", + "--run", + MOCK_RUN_FAILED, + "--context", + "default", + "--hook", + "postCreateCommand", + "--limit", + "100", + "--after", + "abc", + "--result-format", + "json", + ], + { maxBuffer: MAX_RESPONSE_BYTES, signal: expect.any(AbortSignal) }, + ) + }) + + it("lists runs for a context without a workspace argument", async () => { + const { reader: r, cli } = reader(async () => + mockLogRuns({ context: "default" }), + ) + const res = await r.runs({ context: "default" }) + expect(res.runs).toHaveLength(4) + expect(cli.run.mock.calls[0][0]).toEqual([ + "workspace", + "logs", + "--source", + "setup", + "--list", + "--context", + "default", + "--result-format", + "json", + ]) + }) + + it("rejects invalid requests before running the CLI", async () => { + const { reader: r, cli } = reader(async () => ({})) + await expect( + r.read({ context: "default", runId: "latest" }), + ).rejects.toMatchObject({ code: "contract" }) + await expect(r.runs({ context: "-x" })).rejects.toBeInstanceOf( + WorkspaceLogError, + ) + expect(cli.run).not.toHaveBeenCalled() + }) + + it("rejects a response for another run, context or workspace", async () => { + const page = mockLogRunRead({ runId: MOCK_RUN_FAILED }) + const wrongRun = reader(async () => mockLogRunRead({ runId: MOCK_RUN_OK })) + await expect(wrongRun.reader.read(readArgs)).rejects.toThrow( + "different run", + ) + const wrongCtx = reader(async () => ({ + ...page, + run: { ...page.run, context: "other" }, + })) + await expect(wrongCtx.reader.read(readArgs)).rejects.toThrow( + "different context", + ) + const wrongUid = reader(async () => page) + await expect( + wrongUid.reader.read({ ...readArgs, workspaceUid: "ws-999" }), + ).rejects.toThrow("different workspace") + }) + + it("supersedes an in-flight read so only one stays active", async () => { + let calls = 0 + const releases: Array<() => void> = [] + const { reader: r } = reader( + () => + new Promise((resolve) => { + calls++ + releases.push(() => + resolve(mockLogRunRead({ runId: MOCK_RUN_FAILED })), + ) + }), + ) + const first = r.read(readArgs) + const second = r.read(readArgs) + await expect(first).rejects.toMatchObject({ code: "cancelled" }) + for (const release of releases) release() + await expect(second).resolves.toBeDefined() + expect(calls).toBe(2) + }) + + it("cancels only readers, never other CLI work", async () => { + const cancelFor = vi.fn() + const { reader: r } = reader(() => new Promise(() => undefined)) + const pending = r.read(readArgs) + r.cancel() + await expect(pending).rejects.toMatchObject({ code: "cancelled" }) + expect(cancelFor).not.toHaveBeenCalled() + }) + + it("times out a stuck read", async () => { + vi.useFakeTimers() + try { + const { reader: r } = reader(() => new Promise(() => undefined), 1000) + const pending = r.read(readArgs) + const assertion = expect(pending).rejects.toMatchObject({ + code: "timeout", + }) + await vi.advanceTimersByTimeAsync(1001) + await assertion + } finally { + vi.useRealTimers() + } + }) + + it("aborts the CLI child when a read is cancelled or times out", async () => { + const signals: (AbortSignal | undefined)[] = [] + const stuck: Run = (_args, options) => { + signals.push(options?.signal) + return new Promise(() => undefined) + } + const { reader: r } = reader(stuck) + const cancelled = r.read(readArgs) + r.cancel() + await expect(cancelled).rejects.toMatchObject({ code: "cancelled" }) + expect(signals[0]?.aborted).toBe(true) + + vi.useFakeTimers() + try { + const { reader: timed } = reader(stuck, 1000) + const pending = timed.read(readArgs) + const assertion = expect(pending).rejects.toMatchObject({ + code: "timeout", + }) + await vi.advanceTimersByTimeAsync(1001) + await assertion + expect(signals[1]?.aborted).toBe(true) + } finally { + vi.useRealTimers() + } + }) + + it("wraps CLI failures", async () => { + const { reader: r } = reader(async () => { + throw new Error("exit 1") + }) + await expect(r.read(readArgs)).rejects.toMatchObject({ + code: "cli", + message: "exit 1", + }) + }) +}) + +describe("export", () => { + let dir: string + beforeEach(async () => { + dir = await mkdtemp(join(tmpdir(), "devsy-export-")) + }) + afterEach(async () => { + await rm(dir, { recursive: true, force: true }) + }) + + function pagedRun(): Run { + const full = mockLogRunRead({ runId: MOCK_RUN_FAILED }) + const pages: LogReadResponse[] = [ + { + ...full, + records: full.records.slice(0, 3), + hasMore: true, + nextCursor: "c1", + }, + { + ...full, + records: full.records.slice(3), + hasMore: false, + nextCursor: "c2", + }, + ] + return async (args) => { + const after = args.indexOf("--after") + return after === -1 ? pages[0] : pages[1] + } + } + + it("writes bounded pages to a temp file and renames on success", async () => { + const { reader: r, cli } = reader(pagedRun()) + const dest = join(dir, "out.txt") + const result = await r.exportRun(readArgs, dest) + expect(cli.run).toHaveBeenCalledTimes(2) + expect(await readdir(dir)).toEqual(["out.txt"]) + const text = await readFile(dest, "utf8") + expect(text).toContain("npm install") + // Output stays literal even when it looks like JSON or HTML. + expect(text).toContain('{"level":"error","msg":"not a log line"}') + expect(text).toContain("literal") + expect(text).toContain("[postCreateCommand [Running]][out] npm install") + expect(result.gaps).toBe(false) + expect(result.bytes).toBeGreaterThan(0) + }) + + it("states gaps in the saved file for partial history", async () => { + const { reader: r } = reader(async () => + mockLogRunRead({ runId: MOCK_RUN_PARTIAL }), + ) + const dest = join(dir, "partial.txt") + const result = await r.exportRun( + { context: "default", runId: MOCK_RUN_PARTIAL }, + dest, + ) + expect(result.gaps).toBe(true) + const text = await readFile(dest, "utf8") + expect(text).toContain("# Capture condition: source unavailable") + expect(text).toContain("# History is incomplete") + }) + + it("removes the partial file when a page fails", async () => { + let n = 0 + const { reader: r } = reader(async (args) => { + if (n++ === 1) throw new Error("boom") + return pagedRun()(args) + }) + await expect(r.exportRun(readArgs, join(dir, "out.txt"))).rejects.toThrow( + "boom", + ) + expect(await readdir(dir)).toEqual([]) + }) + + it("treats cancellation as a clean stop and removes the partial file", async () => { + let release!: () => void + const { reader: r } = reader( + (args) => + new Promise((resolve) => { + if (args.includes("--after")) return + release = () => resolve(pagedRun()(args)) + }), + ) + const pending = r.exportRun(readArgs, join(dir, "out.txt")) + await vi.waitFor(() => expect(release).toBeDefined()) + r.cancel() + await expect(pending).rejects.toMatchObject({ code: "cancelled" }) + expect(await readdir(dir)).toEqual([]) + }) + + it("cancels an export that is waiting on a later page", async () => { + let started = false + const { reader: r } = reader( + (args) => + new Promise((resolve) => { + if (args.includes("--after")) { + started = true + return + } + resolve(pagedRun()(args)) + }), + ) + const pending = r.exportRun(readArgs, join(dir, "out.txt")) + await vi.waitFor(() => expect(started).toBe(true)) + r.cancel() + await expect(pending).rejects.toMatchObject({ code: "cancelled" }) + expect(await readdir(dir)).toEqual([]) + }) + + it("fails when paging does not advance", async () => { + const full = mockLogRunRead({ runId: MOCK_RUN_FAILED }) + const { reader: r } = reader(async () => ({ + ...full, + hasMore: true, + nextCursor: undefined, + })) + await expect(r.exportRun(readArgs, join(dir, "out.txt"))).rejects.toThrow( + "did not advance", + ) + expect(await readdir(dir)).toEqual([]) + }) +}) diff --git a/desktop/src/main/cli.ts b/desktop/src/main/cli.ts index f936523098..a4403dd82b 100644 --- a/desktop/src/main/cli.ts +++ b/desktop/src/main/cli.ts @@ -51,6 +51,7 @@ export interface CliInvocationOptions { unlockRetried?: boolean /** Raises the stdout cap of run() above Node's 1 MiB default. */ maxBuffer?: number + signal?: AbortSignal } export interface CliInvocationPolicy { @@ -372,6 +373,9 @@ export class CliRunner { ...(options.maxBuffer === undefined ? {} : { maxBuffer: options.maxBuffer }), + ...(options.signal === undefined + ? {} + : { signal: options.signal }), }) ).stdout : await this.spawnWithStdin( diff --git a/desktop/src/main/index.ts b/desktop/src/main/index.ts index fe32573de8..98cd46412a 100644 --- a/desktop/src/main/index.ts +++ b/desktop/src/main/index.ts @@ -1,7 +1,11 @@ import { homedir } from "node:os" import { join } from "node:path" import { app, BrowserWindow, ipcMain, Notification, session } from "electron" -import { settingsRoute, workspaceRoute } from "../shared/app-route.js" +import { + settingsRoute, + workspaceLogsRoute, + workspaceRoute, +} from "../shared/app-route.js" import { initAnalytics, shutdownAnalytics, trackEvent } from "./analytics.js" import { isAppQuitting, markAppQuitting } from "./app-lifecycle.js" import { AppNavigationController } from "./app-navigation.js" @@ -276,7 +280,18 @@ app.whenReady().then(async () => { notification.show() }, openWorkspace: (id) => showDevsy(workspaceRoute(id)), - openWorkspaceLogs: (id) => showDevsy(workspaceRoute(id, "logs")), + openWorkspaceLogs: (id) => { + const job = workspaceJobs.get(id) + showDevsy( + job?.state === "failed" && job.runId + ? workspaceLogsRoute(id, { + context: state.workspaceContext(id), + runId: job.runId, + invocationId: job.failedInvocationId, + }) + : workspaceRoute(id, "logs"), + ) + }, openUpdates: () => showDevsy(settingsRoute()), }) workspaceJobs.onChange(() => notifier.onJobsChanged(workspaceJobs.snapshot())) diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts index 753a9b6d07..996d743a08 100644 --- a/desktop/src/main/ipc.ts +++ b/desktop/src/main/ipc.ts @@ -12,8 +12,11 @@ import type { CLIError, OperationStatus } from "../shared/cli-error.js" import { normalizeOperationStatus, parseCliEnvelope, + runIdentityFromContext, + runIdFromPayload, } from "../shared/cli-error.js" import type { ProviderIconRequest } from "../shared/provider-icon.js" +import type { LogRunExportResult } from "../shared/workspace-logs.js" import type { WorkspaceActivity } from "../shared/workspace-operation.js" import { hashWorkspaceRef, trackEvent } from "./analytics.js" import type { AppNavigationController } from "./app-navigation.js" @@ -46,6 +49,7 @@ import { } from "./updater.js" import { type ProviderEntry, parseProviderEntries } from "./watcher.js" import type { WorkspaceJobs } from "./workspace-jobs.js" +import { WorkspaceLogError, WorkspaceLogReader } from "./workspace-logs.js" import { normalizeWorkspaceStatus } from "./workspace-status.js" const execFileAsync = promisify(execFile) @@ -936,13 +940,15 @@ export function registerIpcHandlers(deps: IpcDependencies): { * workspace's log directory. */ async function quiesceWorkspace(workspaceId: string): Promise { - await serializePerWorkspace(workspaceId, async () => { - await cancelActiveUp(workspaceId) - await Promise.all([ - cli.cancelFor(workspaceId), - pty.cancelFor(workspaceId), - ]) - }) + await serializePerWorkspace(workspaceId, () => + quiesceWorkspaceLocked(workspaceId), + ) + } + + /** Callers must already hold the per-workspace lock. */ + async function quiesceWorkspaceLocked(workspaceId: string): Promise { + await cancelActiveUp(workspaceId) + await Promise.all([cli.cancelFor(workspaceId), pty.cancelFor(workspaceId)]) } function errorMessage(error: unknown): string { @@ -2040,6 +2046,52 @@ export function registerIpcHandlers(deps: IpcDependencies): { }, ) + // Retained build and lifecycle history. The reader owns only its own + // requests; closing a history view must not touch the task follower. + const logReader = new WorkspaceLogReader(cli) + const cancelledAsNull = async (read: Promise): Promise => { + try { + return await read + } catch (error) { + if (error instanceof WorkspaceLogError && error.code === "cancelled") + return null + throw error + } + } + ipcMain.handle("workspace_log_runs", (_event, args: unknown) => + cancelledAsNull(logReader.runs(args)), + ) + ipcMain.handle("workspace_log_run_read", (_event, args: unknown) => + cancelledAsNull(logReader.read(args)), + ) + ipcMain.handle("workspace_log_run_cancel", () => logReader.cancel()) + ipcMain.handle( + "workspace_log_run_export", + async (_event, args: unknown): Promise => { + const runId = + args && typeof args === "object" + ? String((args as { runId?: unknown }).runId ?? "") + : "" + const options = { + defaultPath: `devsy-setup-${runId.slice(0, 8) || "run"}.txt`, + filters: [{ name: "Text", extensions: ["txt"] }], + } + const win = deps.getMainWindow() + const picked = win + ? await dialog.showSaveDialog(win, options) + : await dialog.showSaveDialog(options) + if (picked.canceled || !picked.filePath) return { status: "canceled" } + try { + const done = await logReader.exportRun(args, picked.filePath) + return { status: "saved", path: picked.filePath, ...done } + } catch (error) { + if (error instanceof WorkspaceLogError && error.code === "cancelled") + return { status: "canceled" } + throw error + } + }, + ) + const runWorkspaceUp = (args: { source: string workspaceId?: string @@ -2074,6 +2126,32 @@ export function registerIpcHandlers(deps: IpcDependencies): { const cmdId = args.commandId ?? crypto.randomUUID() const activity = args.workspaceId ? "creating" : "starting" const generation = workspaceJobs.start(wsId, activity, cmdId) + const completion = runDetachedUp({ + wsId, + cmdId, + generation, + cliArgs, + prepare: () => cancelActiveUp(wsId), + }) + return { commandId: cmdId, completion } + } + + type DetachedUpRun = { + wsId: string + cmdId: string + generation: number + cliArgs: string[] + /** Runs under the per-workspace lock, before the task is submitted. */ + prepare: () => Promise + } + + /** + * Submits `workspace up --detach` and follows its task. Shared by create, + * start, rebuild and reset so task ownership, cancellation and result + * handling live in one place. + */ + function runDetachedUp(run: DetachedUpRun): Promise { + const { wsId, cmdId, generation, cliArgs } = run const started = performance.now() const timing = (stage: string) => mainLog.debug( @@ -2081,7 +2159,7 @@ export function registerIpcHandlers(deps: IpcDependencies): { ) timing("accepted") let failureSink = createLogSink(deps.getMainWindow, cmdId) - const completion = (async () => { + return (async () => { const logPath = logStore.createLogFile(state.workspaceContext(wsId), wsId) const sink = createLogSink( deps.getMainWindow, @@ -2100,25 +2178,29 @@ export function registerIpcHandlers(deps: IpcDependencies): { await workspaceJobs.finish(wsId, generation, error, flushError) } + workspaceJobs.phase(wsId, cmdId, "Closing connections") await serializePerWorkspace(wsId, async () => { // Tear down any prior run for this workspace before starting a new // one. let taskId: string try { - workspaceJobs.phase(wsId, cmdId, "Closing connections") - await cancelActiveUp(wsId) + await run.prepare() timing("shutdown-complete") workspaceJobs.phase(wsId, cmdId, "Launching command") timing("cli-launch") // Submit: returns immediately with the background task's id. - const submitted = await cli.run<{ kind: string; id: string }>([ - ...cliArgs, - "--detach", - ]) + const submitted = await cli.run<{ + kind: string + id: string + runId?: string + }>([...cliArgs, "--detach"]) if (!submitted?.id) { throw new Error("workspace up --detach returned no task id") } taskId = submitted.id + workspaceJobs.setRun(wsId, cmdId, { + runId: runIdFromPayload(submitted), + }) } catch (error) { const safe = redactCLIError(cliErrorOrFallback(error, "up_failed")) await finishUp( @@ -2182,6 +2264,9 @@ export function registerIpcHandlers(deps: IpcDependencies): { if (envelope?.kind === "result") { signalledDone = true + workspaceJobs.setRun(wsId, cmdId, { + runId: runIdFromPayload(envelope), + }) releaseTask() timing("completed") void finishUp(formatted, { success: true }) @@ -2190,6 +2275,11 @@ export function registerIpcHandlers(deps: IpcDependencies): { if (envelope?.kind === "error") { signalledDone = true + workspaceJobs.setRun( + wsId, + cmdId, + runIdentityFromContext(envelope.context), + ) releaseTask() const safe = redactCLIError({ code: envelope.code ?? "up_failed", @@ -2272,7 +2362,6 @@ export function registerIpcHandlers(deps: IpcDependencies): { } await workspaceJobs.finish(wsId, generation, safe.message, flushError) }) - return { commandId: cmdId, completion } } ipcMain.handle( "workspace_up", @@ -2308,6 +2397,9 @@ export function registerIpcHandlers(deps: IpcDependencies): { for (const line of raw.split("\n")) { const envelope = parseCliEnvelope(line) if (envelope?.kind === "result") { + workspaceJobs.setRun(wsId, commandId, { + runId: runIdFromPayload(envelope), + }) release() const flushError = await sink.done(formatLogLine("Completed"), { success: true, @@ -2332,6 +2424,7 @@ export function registerIpcHandlers(deps: IpcDependencies): { status: string error?: string errorCode?: string + runId?: string }> >(["workspace", "task", "list"]) const task = tasks.find((task) => task.id === taskId) @@ -2339,6 +2432,9 @@ export function registerIpcHandlers(deps: IpcDependencies): { task?.status === "failed" && workspaceJobs.owns(wsId, commandId) ) { + workspaceJobs.setRun(wsId, commandId, { + runId: runIdFromPayload(task), + }) release() const safe = redactCLIError({ code: task.errorCode ?? "up_failed", @@ -2497,6 +2593,32 @@ export function registerIpcHandlers(deps: IpcDependencies): { return { commandId, completion } } + /** Rebuild and reset: a detached up, so run identity reaches the job. */ + function startWorkspaceUp( + args: StopWorkspaceArgs, + activity: "rebuilding" | "resetting", + flag: "--recreate" | "--reset", + ): { commandId: string; completion: Promise } { + const commandId = args.commandId ?? crypto.randomUUID() + const generation = workspaceJobs.start( + args.workspaceId, + activity, + commandId, + ) + trackEvent(`workspace_${activity === "rebuilding" ? "rebuild" : "reset"}`, { + workspace_ref: hashWorkspaceRef(args.workspaceId), + source: "renderer", + }) + const completion = runDetachedUp({ + wsId: args.workspaceId, + cmdId: commandId, + generation, + cliArgs: ["workspace", "up", args.workspaceId, flag], + prepare: () => quiesceWorkspaceLocked(args.workspaceId), + }) + return { commandId, completion } + } + function startWorkspaceStop( args: StopWorkspaceArgs, source: WorkspaceActionSource, @@ -2526,22 +2648,12 @@ export function registerIpcHandlers(deps: IpcDependencies): { ipcMain.handle("workspace_rebuild", (event, args: StopWorkspaceArgs) => { if (!isTrustedSecretIpcSender(event, getMainWindow(), secretDocumentURL)) return secretRequestDenied - return startWorkspaceAction(args, "rebuilding", [ - "workspace", - "up", - args.workspaceId, - "--recreate", - ]).commandId + return startWorkspaceUp(args, "rebuilding", "--recreate").commandId }) ipcMain.handle("workspace_reset", (event, args: StopWorkspaceArgs) => { if (!isTrustedSecretIpcSender(event, getMainWindow(), secretDocumentURL)) return secretRequestDenied - return startWorkspaceAction(args, "resetting", [ - "workspace", - "up", - args.workspaceId, - "--reset", - ]).commandId + return startWorkspaceUp(args, "resetting", "--reset").commandId }) // ── Terminal ── diff --git a/desktop/src/main/workspace-jobs.ts b/desktop/src/main/workspace-jobs.ts index 62686ee7b3..9617a83d27 100644 --- a/desktop/src/main/workspace-jobs.ts +++ b/desktop/src/main/workspace-jobs.ts @@ -75,6 +75,24 @@ export class WorkspaceJobs { this.jobs.set(id, { ...job, status, phase }) this.emit() } + /** Associates durable history with the job that still owns the command. */ + setRun( + id: string, + commandId: string, + run: { runId?: string; failedInvocationId?: string }, + ): void { + const job = this.jobs.get(id) + if (!job || job.commandId !== commandId || !run.runId) return + if (job.runId && job.runId !== run.runId) return + const failedInvocationId = run.failedInvocationId ?? job.failedInvocationId + if ( + job.runId === run.runId && + job.failedInvocationId === failedInvocationId + ) + return + this.jobs.set(id, { ...job, runId: run.runId, failedInvocationId }) + this.emit() + } clear(id: string): void { this.generations.set(id, ++this.lastGeneration) if (this.jobs.delete(id)) this.emit() diff --git a/desktop/src/main/workspace-logs.ts b/desktop/src/main/workspace-logs.ts new file mode 100644 index 0000000000..1baf53bf7a --- /dev/null +++ b/desktop/src/main/workspace-logs.ts @@ -0,0 +1,344 @@ +import { randomUUID } from "node:crypto" +import { open, rename, rm } from "node:fs/promises" +import { + captureNotes, + checkIndexIdentity, + checkReadIdentity, + InvocationTable, + type InvocationView, + invocationStateLabel, + invocationTitle, + type LogIndexResponse, + type LogReadResponse, + type LogRecord, + type LogRunExportRequest, + type LogRunReadRequest, + type LogRunsRequest, + parseIndexResponse, + parseReadResponse, + validateRunExportRequest, + validateRunReadRequest, + validateRunsRequest, +} from "../shared/workspace-logs.js" + +/** Largest CLI stdout accepted for one page (the Go side caps pages at 256 KiB). */ +export const MAX_RESPONSE_BYTES = 1024 * 1024 +export const READ_TIMEOUT_MS = 20_000 +export const EXPORT_PAGE_LIMIT = 500 +export const MAX_EXPORT_PAGES = 400 +export const MAX_EXPORT_BYTES = 64 * 1024 * 1024 + +export type WorkspaceLogErrorCode = + | "cancelled" + | "timeout" + | "contract" + | "limit" + | "cli" + +export class WorkspaceLogError extends Error { + constructor( + readonly code: WorkspaceLogErrorCode, + message: string, + readonly cause?: unknown, + ) { + super(message) + this.name = "WorkspaceLogError" + } +} + +export interface WorkspaceLogCli { + run( + args: string[], + options?: { maxBuffer?: number; signal?: AbortSignal }, + ): Promise +} + +export interface ExportFs { + open: typeof open + rename: typeof rename + rm: typeof rm +} + +export interface WorkspaceLogReaderOptions { + timeoutMs?: number + fs?: ExportFs +} + +type Slot = "list" | "read" | "export" + +/** + * Small adapter over `workspace logs --source setup`. It keeps no archive of + * its own. Each slot holds at most one in-flight request, and cancel() stops + * only these readers, never the workspace's task follower. + */ +export class WorkspaceLogReader { + private inflight = new Map() + private readonly timeoutMs: number + private readonly fs: ExportFs + + constructor( + private readonly cli: WorkspaceLogCli, + options: WorkspaceLogReaderOptions = {}, + ) { + this.timeoutMs = options.timeoutMs ?? READ_TIMEOUT_MS + this.fs = options.fs ?? { open, rename, rm } + } + + /** Cancels every in-flight reader request. Safe when none is active. */ + cancel(): void { + for (const controller of this.inflight.values()) controller.abort() + this.inflight.clear() + } + + async runs(raw: unknown): Promise { + const req = this.validated(() => validateRunsRequest(raw)) + const signal = this.begin("list") + try { + const out = await this.exec(runsArgs(req), signal) + try { + const res = parseIndexResponse(out) + checkIndexIdentity(req, res) + return res + } catch (error) { + throw contractError(error) + } + } finally { + this.release("list", signal) + } + } + + async read(raw: unknown): Promise { + const req = this.validated(() => validateRunReadRequest(raw)) + const signal = this.begin("read") + try { + return await this.readPage(req, signal) + } finally { + this.release("read", signal) + } + } + + /** + * Streams bounded pages into a temp file beside the destination and + * renames it on success. Any failure or cancellation removes the temp file. + */ + async exportRun( + raw: unknown, + destination: string, + ): Promise<{ bytes: number; gaps: boolean }> { + const req = this.validated(() => validateRunExportRequest(raw)) + const signal = this.begin("export") + try { + const temp = `${destination}.${randomUUID()}.tmp` + const handle = await this.fs.open(temp, "wx", 0o600) + try { + const result = await this.writePages(req, signal, handle) + await handle.close() + await this.fs.rename(temp, destination) + return result + } catch (error) { + await handle.close().catch(() => undefined) + await this.fs.rm(temp, { force: true }).catch(() => undefined) + throw error + } + } finally { + this.release("export", signal) + } + } + + private async writePages( + req: LogRunExportRequest, + signal: AbortSignal, + handle: Awaited>, + ): Promise<{ bytes: number; gaps: boolean }> { + const formatter = new ExportFormatter() + let bytes = 0 + let after: string | undefined + for (let page = 0; page < MAX_EXPORT_PAGES; page++) { + const res = await this.readPage( + { ...req, after, limit: EXPORT_PAGE_LIMIT }, + signal, + ) + bytes += await writeText(handle, formatter.page(res), bytes) + if (!res.hasMore) { + bytes += await writeText(handle, formatter.footer(res), bytes) + return { bytes, gaps: formatter.gaps || !res.complete } + } + if (!res.nextCursor || res.nextCursor === after) + throw new WorkspaceLogError("contract", "Log paging did not advance") + after = res.nextCursor + } + throw new WorkspaceLogError("limit", "Export exceeds the page limit") + } + + private async readPage( + req: LogRunReadRequest, + signal: AbortSignal, + ): Promise { + const out = await this.exec(readArgs(req), signal) + try { + const res = parseReadResponse(out) + checkReadIdentity(req, res) + return res + } catch (error) { + throw contractError(error) + } + } + + private validated(fn: () => T): T { + try { + return fn() + } catch (error) { + throw contractError(error) + } + } + + /** Replaces any earlier request in the slot, so only one stays in flight. */ + private begin(slot: Slot): AbortSignal { + this.inflight.get(slot)?.abort() + const controller = new AbortController() + this.inflight.set(slot, controller) + return controller.signal + } + + private release(slot: Slot, signal: AbortSignal): void { + if (this.inflight.get(slot)?.signal === signal) this.inflight.delete(slot) + } + + private async exec(args: string[], signal: AbortSignal): Promise { + const child = new AbortController() + let timer: ReturnType | undefined + let onAbort: (() => void) | undefined + const guard = new Promise((_, reject) => { + timer = setTimeout(() => { + child.abort() + reject(new WorkspaceLogError("timeout", "Log read timed out")) + }, this.timeoutMs) + onAbort = () => { + child.abort() + reject(new WorkspaceLogError("cancelled", "Log read was cancelled")) + } + if (signal.aborted) onAbort() + else signal.addEventListener("abort", onAbort, { once: true }) + }) + try { + return await Promise.race([ + this.cli.run(args, { + maxBuffer: MAX_RESPONSE_BYTES, + signal: child.signal, + }), + guard, + ]) + } catch (error) { + if (error instanceof WorkspaceLogError) throw error + throw new WorkspaceLogError("cli", messageOf(error), error) + } finally { + clearTimeout(timer) + if (onAbort) signal.removeEventListener("abort", onAbort) + } + } +} + +function runsArgs(req: LogRunsRequest): string[] { + const args = ["workspace", "logs"] + if (req.workspaceId) args.push(req.workspaceId) + args.push("--source", "setup", "--list", "--context", req.context) + if (req.limit) args.push("--limit", String(req.limit)) + if (req.after) args.push("--after", req.after) + args.push("--result-format", "json") + return args +} + +function readArgs(req: LogRunReadRequest): string[] { + const args = ["workspace", "logs"] + if (req.workspaceId) args.push(req.workspaceId) + args.push("--source", "setup", "--run", req.runId, "--context", req.context) + if (req.hook) args.push("--hook", req.hook) + if (req.invocationId) args.push("--invocation", req.invocationId) + if (req.limit) args.push("--limit", String(req.limit)) + if (req.after) args.push("--after", req.after) + args.push("--result-format", "json") + return args +} + +function messageOf(error: unknown): string { + return error instanceof Error ? error.message : String(error) +} + +function contractError(error: unknown): WorkspaceLogError { + if (error instanceof WorkspaceLogError) return error + return new WorkspaceLogError("contract", messageOf(error), error) +} + +async function writeText( + handle: Awaited>, + text: string, + written: number, +): Promise { + const size = Buffer.byteLength(text) + if (written + size > MAX_EXPORT_BYTES) + throw new WorkspaceLogError("limit", "Export exceeds the size limit") + if (size > 0) await handle.write(text) + return size +} + +const STREAM_TAG = { stdout: "out", stderr: "err", combined: "out+err" } + +/** + * Renders pages as text, tagging each line with its invocation and stream. + * Output is written literally. Gaps and unavailable sources are stated in + * trailing comment lines so a saved file never looks complete when it is not. + */ +class ExportFormatter { + gaps = false + private table = new InvocationTable() + private open: string | undefined + private started = false + + page(res: LogReadResponse): string { + this.table.apply(res.records) + this.table.markSettled(res.complete && !res.hasMore) + const parts: string[] = [] + if (!this.started) { + this.started = true + parts.push( + `# Devsy retained setup output\n# run ${res.run.runId} (context ${res.run.context})\n`, + ) + } + const views = new Map(this.table.list().map((v) => [v.id, v])) + for (const r of res.records) { + if (r.kind === "capture") this.gaps = true + if (r.kind === "output") parts.push(this.outputText(r, views)) + } + return parts.join("") + } + + footer(res: LogReadResponse): string { + const notes = captureNotes(res.run, res.availability, res.complete) + if (notes.length > 0) this.gaps = true + const end = this.open ? "\n" : "" + this.open = undefined + return `${end}${notes.map((n) => `# ${n}\n`).join("")}` + } + + private outputText(r: LogRecord, views: Map): string { + const slot = `${r.invocationId}\u0000${r.stream}` + const view = views.get(r.invocationId as string) + const label = view + ? `${invocationTitle(view)} [${invocationStateLabel(view.state)}]` + : (r.invocationId as string) + const tag = `[${label}][${STREAM_TAG[r.stream as keyof typeof STREAM_TAG]}] ` + const text = r.text ?? "" + if (text === "") return "" + let out = "" + if (this.open && this.open !== slot) out += "\n" + const continuing = this.open === slot + const lines = text.split("\n") + lines.forEach((line, i) => { + const last = i === lines.length - 1 + if (last && line === "") return + out += (i === 0 && continuing ? "" : tag) + line + (last ? "" : "\n") + }) + this.open = text.endsWith("\n") ? undefined : slot + return out + } +} diff --git a/desktop/src/renderer/src/lib/components/log/LogTable.svelte b/desktop/src/renderer/src/lib/components/log/LogTable.svelte index 55bbe63db7..1f66dbb311 100644 --- a/desktop/src/renderer/src/lib/components/log/LogTable.svelte +++ b/desktop/src/renderer/src/lib/components/log/LogTable.svelte @@ -2,26 +2,37 @@ import { badgeVariants } from "$lib/components/ui/badge/index.js" import { parseLogLine } from "$lib/utils/log-parser.js" import { cn } from "$lib/utils.js" +import type { TranscriptLine } from "$shared/workspace-logs.js" let { lines, + transcript = undefined, class: className = "", maxHeightClass = "max-h-96", follow = false, maxLines = 5000, }: { lines: string[] + /** Typed transcript mode: literal text, parseLogLine is never called. */ + transcript?: TranscriptLine[] class?: string maxHeightClass?: string follow?: boolean maxLines?: number } = $props() +let total = $derived(transcript ? transcript.length : lines.length) + let viewport = $state(null) let pinnedToBottom = $state(true) -let hiddenCount = $derived(Math.max(0, lines.length - maxLines)) +let hiddenCount = $derived(Math.max(0, total - maxLines)) let visible = $derived(hiddenCount > 0 ? lines.slice(-maxLines) : lines) +let visibleTranscript = $derived( + transcript && hiddenCount > 0 + ? transcript.slice(-maxLines) + : (transcript ?? []), +) function levelRowClass(level: string | undefined): string { if (level === "fatal" || level === "error") return "bg-destructive/5" @@ -38,7 +49,7 @@ function onScroll() { $effect(() => { // Follow the tail as new lines arrive, but only while the user is at the bottom. - void lines.length + void total if (follow && pinnedToBottom && viewport) { viewport.scrollTop = viewport.scrollHeight } @@ -49,7 +60,7 @@ $effect(() => { bind:this={viewport} onscroll={onScroll} role="table" - aria-rowcount={lines.length + 1} + aria-rowcount={total + 1} class={cn("relative overflow-auto rounded-md border", maxHeightClass, className)} >
{ aria-rowindex={1} class="sticky top-0 z-10 grid grid-cols-[5rem_5rem_minmax(0,1fr)] gap-2 border-b bg-background px-2 py-1 text-start text-xs font-medium" > -
Time
-
Level
-
Message
+ {#if transcript} +
Stream
+
Step
+
Output
+ {:else} +
Time
+
Level
+
Message
+ {/if}
{#if hiddenCount > 0} @@ -68,6 +85,20 @@ $effect(() => { {/if} + {#if transcript} + {#each visibleTranscript as entry, i (entry.key)} +
+ {entry.stream === "combined" ? "out+err" : entry.stream} + {entry.label} + {entry.text === "" ? "\u00a0" : entry.text} +
+ {/each} + {:else} {#each visible as raw, i (i)} {@const line = parseLogLine(raw)}
{ {line.message}
{/each} + {/if} diff --git a/desktop/src/renderer/src/lib/components/log/LogTable.test.ts b/desktop/src/renderer/src/lib/components/log/LogTable.test.ts new file mode 100644 index 0000000000..875f698265 --- /dev/null +++ b/desktop/src/renderer/src/lib/components/log/LogTable.test.ts @@ -0,0 +1,67 @@ +import { cleanup, render } from "@testing-library/svelte" +import { afterEach, describe, expect, it, vi } from "vitest" +import * as parser from "$lib/utils/log-parser.js" +import type { TranscriptLine } from "$shared/workspace-logs.js" +import LogTable from "./LogTable.svelte" + +afterEach(cleanup) + +const line = ( + key: number, + text: string, + stream: TranscriptLine["stream"] = "stdout", +): TranscriptLine => ({ + key, + text, + stream, + invocationId: "a1111111-0000-4000-8000-000000000001", + label: "postCreateCommand", +}) + +describe("LogTable transcript mode", () => { + it("never parses lines and renders JSON and HTML as literal text", () => { + const spy = vi.spyOn(parser, "parseLogLine") + const ui = render(LogTable, { + lines: [], + transcript: [ + line(0, '{"level":"error","msg":"nope","ts":"2026-01-01T00:00:00Z"}'), + line(1, "bold", "stderr"), + line(2, "2026-01-01 ERROR looks like a log"), + line(3, " indented"), + line(4, ""), + ], + }) + expect(spy).not.toHaveBeenCalled() + expect( + ui.getByText( + '{"level":"error","msg":"nope","ts":"2026-01-01T00:00:00Z"}', + ), + ).toBeTruthy() + expect(ui.getByText("bold")).toBeTruthy() + expect(ui.container.querySelector("script")).toBeNull() + expect(ui.container.querySelector("b")).toBeNull() + expect(ui.getByText("2026-01-01 ERROR looks like a log")).toBeTruthy() + spy.mockRestore() + }) + + it("labels each row with its stream and step", () => { + const ui = render(LogTable, { + lines: [], + transcript: [line(0, "x", "stderr"), line(1, "y", "combined")], + }) + const rows = [...ui.container.querySelectorAll("[data-stream]")] + expect(rows.map((r) => r.getAttribute("data-stream"))).toEqual([ + "stderr", + "combined", + ]) + expect(ui.getAllByText("postCreateCommand")).toHaveLength(2) + expect(ui.getByText("out+err")).toBeTruthy() + }) + + it("keeps diagnostic mode parsing log lines", () => { + const spy = vi.spyOn(parser, "parseLogLine") + render(LogTable, { lines: ["plain text"] }) + expect(spy).toHaveBeenCalledWith("plain text") + spy.mockRestore() + }) +}) diff --git a/desktop/src/renderer/src/lib/components/log/WorkspaceRunLogs.svelte b/desktop/src/renderer/src/lib/components/log/WorkspaceRunLogs.svelte new file mode 100644 index 0000000000..5c26a464d4 --- /dev/null +++ b/desktop/src/renderer/src/lib/components/log/WorkspaceRunLogs.svelte @@ -0,0 +1,365 @@ + + +
+ {#if runsError} + + {/if} + + {#if runsLoaded && runs.length === 0 && !pinnedRunId && !runsError} +

+ No retained setup history for this workspace yet. Output from builds and lifecycle hooks is kept here after the next run. +

+ {:else if selectedRunId} +
+ + + {#if reader.run} + + Command {reader.run.commandState} + + Capture {reader.run.captureState} + {/if} + {#if readOnly && reader.run} + + Read-only history{reader.run.workspaceIdAtStart ? ` for ${reader.run.workspaceIdAtStart}` : ""} + + {/if} +
+ + +
+
+ + {#if reader.error} + + {/if} + + {#each notes as note (note)} +

{note}

+ {/each} + + {#if views.length > 0} +
    + {#each views as view (view.id)} +
  • + +
  • + {/each} +
+ {/if} + + {#if focus.mode === "failed" && focus.fallback} +

+ The linked step is not in this run. Showing its failed steps instead. +

+ {/if} + + {#if focusView && presenceMessage(focusView) && transcript.length === 0} +

{presenceMessage(focusView)}

+ {:else if transcript.length === 0} +

+ {reader.loading ? "Loading..." : "No output retained for this selection."} +

+ {:else} + + {/if} + + {#if reader.viewTruncated} +

+ Only the first part of this run is shown. Export to save everything that was retained. +

+ {/if} + {/if} +
diff --git a/desktop/src/renderer/src/lib/components/log/WorkspaceRunLogs.test.ts b/desktop/src/renderer/src/lib/components/log/WorkspaceRunLogs.test.ts new file mode 100644 index 0000000000..e5c82831e5 --- /dev/null +++ b/desktop/src/renderer/src/lib/components/log/WorkspaceRunLogs.test.ts @@ -0,0 +1,209 @@ +import { cleanup, fireEvent, render, waitFor } from "@testing-library/svelte" +import type { ComponentProps } from "svelte" +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" +import { mockInvoke, resetTauriMocks } from "$lib/__mocks__/tauri.js" +import { + MOCK_INV_POST_CREATE_FAILED, + MOCK_INV_POST_CREATE_OK, + MOCK_INV_SILENT_OK, + MOCK_INV_UNCAPTURED, + MOCK_RUN_FAILED, + MOCK_RUN_OK, + MOCK_RUN_PARTIAL, + MOCK_RUN_REMOVED, + mockLogRunRead, + mockLogRuns, +} from "$lib/ipc/mock.js" +import { toasts } from "$lib/stores/toasts.js" +import WorkspaceRunLogs from "./WorkspaceRunLogs.svelte" + +vi.mock("$lib/stores/toasts.js", () => ({ + toasts: { success: vi.fn(), error: vi.fn(), info: vi.fn() }, +})) + +afterEach(cleanup) +beforeEach(() => { + resetTauriMocks() + vi.clearAllMocks() + mockInvoke.mockImplementation( + async (cmd: string, args?: Record) => { + if (cmd === "workspace_log_runs") return mockLogRuns(args ?? {}) + if (cmd === "workspace_log_run_read") return mockLogRunRead(args ?? {}) + if (cmd === "workspace_log_run_cancel") return undefined + if (cmd === "workspace_log_run_export") return { status: "canceled" } + throw new Error(`unexpected ${cmd}`) + }, + ) +}) + +const base = { + context: "default", + workspaceId: "my-project", + workspaceUid: "ws-001", +} + +// "context" is also a Svelte mount option, so props must be nested. +const mount = (props: ComponentProps) => + render(WorkspaceRunLogs, { props }) + +function reads() { + return mockInvoke.mock.calls.filter((c) => c[0] === "workspace_log_run_read") +} + +describe("WorkspaceRunLogs", () => { + it("defaults to the latest run and lists its steps with outcomes", async () => { + const ui = mount(base) + await waitFor(() => expect(ui.getByText("Failed")).toBeTruthy()) + expect(reads()[0][1]).toMatchObject({ + runId: MOCK_RUN_FAILED, + context: "default", + }) + expect(ui.getByText("Skipped")).toBeTruthy() + expect(ui.getByText("Already attempted, not run again")).toBeTruthy() + expect(ui.getByText(/exit code 1/)).toBeTruthy() + }) + + it("shows a skipped step reason even with zero output", async () => { + const ui = mount({ + ...base, + pinnedRunId: MOCK_RUN_FAILED, + pinnedInvocationId: "a1111111-0000-4000-8000-000000000002", + }) + await waitFor(() => + expect( + ui.getAllByText("Already attempted, not run again").length, + ).toBeGreaterThan(1), + ) + }) + + it("distinguishes silent success from output that was never captured", async () => { + const ok = mount({ + ...base, + pinnedRunId: MOCK_RUN_OK, + pinnedInvocationId: MOCK_INV_SILENT_OK, + }) + await waitFor(() => + expect( + ok.getByText("Completed with no output (captured, 0 bytes)."), + ).toBeTruthy(), + ) + cleanup() + const partial = mount({ + ...base, + pinnedRunId: MOCK_RUN_PARTIAL, + pinnedInvocationId: MOCK_INV_UNCAPTURED, + }) + await waitFor(() => + expect( + partial.getByText("Output was not captured for this step."), + ).toBeTruthy(), + ) + }) + + it("renders deferred hooks and successful output as literal text", async () => { + const ui = mount({ + ...base, + pinnedRunId: MOCK_RUN_OK, + pinnedInvocationId: MOCK_INV_POST_CREATE_OK, + }) + await waitFor(() => + expect(ui.getByText("installed 12 packages")).toBeTruthy(), + ) + expect(ui.getByText("no trailing newline")).toBeTruthy() + expect(ui.getByText("Deferred")).toBeTruthy() + }) + + it("opens a failing invocation directly and keeps JSON and HTML literal", async () => { + const ui = mount({ + ...base, + pinnedRunId: MOCK_RUN_FAILED, + pinnedInvocationId: MOCK_INV_POST_CREATE_FAILED, + }) + await waitFor(() => + expect( + ui.getByText('{"level":"error","msg":"not a log line"}'), + ).toBeTruthy(), + ) + expect(ui.getByText("literal")).toBeTruthy() + expect(ui.container.querySelector("b")).toBeNull() + }) + + it("falls back to the run's failed steps when the invocation is unknown", async () => { + const ui = mount({ + ...base, + pinnedRunId: MOCK_RUN_FAILED, + pinnedInvocationId: "a9999999-0000-4000-8000-000000000009", + }) + await waitFor(() => + expect(ui.getByText(/The linked step is not in this run/)).toBeTruthy(), + ) + await waitFor(() => expect(ui.getByText("npm install")).toBeTruthy()) + }) + + it("keeps an explicit older run when a new operation run arrives", async () => { + const ui = mount(base) + await waitFor(() => expect(ui.getByText("Failed")).toBeTruthy()) + const select = ui.getByLabelText("Run") as HTMLSelectElement + await fireEvent.change(select, { target: { value: MOCK_RUN_OK } }) + await waitFor(() => expect(ui.getByText("Deferred")).toBeTruthy()) + await ui.rerender({ + operationRunId: "55555555-5555-4555-8555-555555555555", + }) + await waitFor(() => expect(select.value).toBe(MOCK_RUN_OK)) + }) + + it("renders a read-only branch for a removed workspace with stored identity", async () => { + const ui = mount({ + context: "default", + readOnly: true, + pinnedRunId: MOCK_RUN_REMOVED, + }) + await waitFor(() => expect(ui.getByText(/Read-only history/)).toBeTruthy()) + expect(ui.getByText(/deleted-project/)).toBeTruthy() + const [, args] = reads()[0] + expect(args).toMatchObject({ runId: MOCK_RUN_REMOVED }) + expect(args.workspaceId).toBeUndefined() + expect(args.workspaceUid).toBeUndefined() + const listCall = mockInvoke.mock.calls.find( + (c) => c[0] === "workspace_log_runs", + ) + expect(listCall?.[1].workspaceId).toBeUndefined() + }) + + it("asks only for readers to cancel when unmounted", async () => { + const ui = mount(base) + await waitFor(() => expect(ui.getByText("Failed")).toBeTruthy()) + ui.unmount() + const channels = mockInvoke.mock.calls.map((c) => c[0]) + expect(channels).toContain("workspace_log_run_cancel") + expect(channels).not.toContain("workspace_cancel") + }) + + it("exports through the main process and treats cancel as normal", async () => { + const ui = mount(base) + await waitFor(() => expect(ui.getByText("Failed")).toBeTruthy()) + await fireEvent.click(ui.getByRole("button", { name: "Export" })) + await waitFor(() => + expect(mockInvoke).toHaveBeenCalledWith("workspace_log_run_export", { + context: "default", + runId: MOCK_RUN_FAILED, + workspaceId: "my-project", + workspaceUid: "ws-001", + invocationId: undefined, + }), + ) + expect(toasts.error).not.toHaveBeenCalled() + }) + + it("explains an empty history", async () => { + mockInvoke.mockImplementation(async (cmd: string) => + cmd === "workspace_log_runs" + ? { schemaVersion: 1, runs: [], hasMore: false } + : undefined, + ) + const ui = mount(base) + await waitFor(() => + expect(ui.getByText(/No retained setup history/)).toBeTruthy(), + ) + }) +}) diff --git a/desktop/src/renderer/src/lib/components/workspace/WorkspaceLaunchProgress.output.test.ts b/desktop/src/renderer/src/lib/components/workspace/WorkspaceLaunchProgress.output.test.ts new file mode 100644 index 0000000000..5e7a851ad5 --- /dev/null +++ b/desktop/src/renderer/src/lib/components/workspace/WorkspaceLaunchProgress.output.test.ts @@ -0,0 +1,58 @@ +import { cleanup, fireEvent, render } from "@testing-library/svelte" +import { afterEach, describe, expect, it, vi } from "vitest" +import WorkspaceLaunchProgress from "./WorkspaceLaunchProgress.svelte" + +afterEach(cleanup) + +const hook = (state: "running" | "succeeded" | "skipped") => ({ + key: "hook", + operationId: "hook", + phase: "running_lifecycle_hook", + state, + firstSeenAtMs: 0, +}) + +function mount( + state: "running" | "succeeded" | "skipped", + onViewOutput?: () => void, +) { + return render(WorkspaceLaunchProgress, { + props: { + workspaceId: "demo", + timeline: { operations: [hook(state)] }, + running: false, + confirmationState: "confirmed" as const, + elapsedMs: 1000, + // No streamed lines: availability comes from recorded metadata. + logsAvailable: false, + logsOpen: false, + onToggleLogs: vi.fn(), + onViewOutput, + }, + }) +} + +describe("WorkspaceLaunchProgress view output", () => { + it.each(["succeeded", "skipped"] as const)( + "offers output for a %s hook without streamed lines", + async (state) => { + const onViewOutput = vi.fn() + const ui = mount(state, onViewOutput) + await fireEvent.click(ui.getByRole("button", { name: /View output for/ })) + expect(onViewOutput).toHaveBeenCalledOnce() + expect(ui.getByRole("button", { name: "View setup output" })).toBeTruthy() + }, + ) + + it("does not offer output while the hook is still running or when no run is recorded", () => { + expect( + mount("running", vi.fn()).queryByRole("button", { + name: /View output for/, + }), + ).toBeNull() + cleanup() + const ui = mount("succeeded") + expect(ui.queryByRole("button", { name: /View output for/ })).toBeNull() + expect(ui.queryByRole("button", { name: "View setup output" })).toBeNull() + }) +}) diff --git a/desktop/src/renderer/src/lib/components/workspace/WorkspaceLaunchProgress.svelte b/desktop/src/renderer/src/lib/components/workspace/WorkspaceLaunchProgress.svelte index fcb32d5250..7d4e3a50fa 100644 --- a/desktop/src/renderer/src/lib/components/workspace/WorkspaceLaunchProgress.svelte +++ b/desktop/src/renderer/src/lib/components/workspace/WorkspaceLaunchProgress.svelte @@ -25,6 +25,8 @@ interface Props { logsAvailable: boolean logsOpen: boolean onToggleLogs: () => void + /** Set when the run recorded retained output, even for successful hooks. */ + onViewOutput?: () => void } let { @@ -43,6 +45,7 @@ let { logsAvailable, logsOpen, onToggleLogs, + onViewOutput, }: Props = $props() const visibleOperations = $derived(topLevelLaunchOperations(timeline)) @@ -175,6 +178,14 @@ function elapsed(value: number): string { {duration(operation.durationMs)} {/if} + {#if onViewOutput && operation.phase === "running_lifecycle_hook" && operation.state !== "running"} + + {/if} {#if presentation.descendants.length}
{#each presentation.descendants as descendant (descendant.operation.key)} @@ -214,6 +225,13 @@ function elapsed(value: number): string {

{error}

{/if} + {#if onViewOutput} + + {/if} + {#if logsAvailable}

Direct terminal CLI commands use their own context default, which is Warn unless overridden.

+

These levels only affect diagnostic files. Build and lifecycle output is retained separately and shown under Logs > Setup and build history, whatever level is set here.

diff --git a/desktop/src/renderer/src/pages/WorkspaceDetailPage.svelte b/desktop/src/renderer/src/pages/WorkspaceDetailPage.svelte index 061332593e..44bc028167 100644 --- a/desktop/src/renderer/src/pages/WorkspaceDetailPage.svelte +++ b/desktop/src/renderer/src/pages/WorkspaceDetailPage.svelte @@ -35,6 +35,7 @@ import * as Accordion from "$lib/components/ui/accordion/index.js" import * as Tabs from "$lib/components/ui/tabs/index.js" import ConfirmDialog from "$lib/components/layout/ConfirmDialog.svelte" import LogTable from "$lib/components/log/LogTable.svelte" +import WorkspaceRunLogs from "$lib/components/log/WorkspaceRunLogs.svelte" import TerminalComponent from "$lib/components/terminal/Terminal.svelte" import WorkspaceOperation from "$lib/components/workspace/WorkspaceOperation.svelte" import { @@ -57,6 +58,7 @@ import { workspaceLogsList, workspaceLogRead, workspaceLogDelete, + workspaceLogRunRead, } from "$lib/ipc/commands.js" import { onCommandProgress } from "$lib/ipc/events.js" import { @@ -71,6 +73,7 @@ import { extractErrorMessage } from "$lib/utils/error.js" import { trackEngagement } from "$lib/analytics.js" import type { CommandProgress, LogEntry } from "$lib/types/index.js" import type { UnlistenFn } from "$lib/ipc/types.js" +import { isRunId } from "$shared/workspace-logs.js" import { describeWorkspaceSource } from "$lib/utils/workspace-source.js" import { formatTimestamp } from "$lib/utils/time.js" import { @@ -148,6 +151,79 @@ $effect(() => { if (tab === "overview" || tab === "logs" || tab === "terminal") activeTab = tab }) +// A run link (notification, toast, wizard) pins the history view to one run +// and, when known, one invocation. +let linkedRun = $derived.by(() => { + const qs = new URLSearchParams($querystring ?? "") + const runId = qs.get("run") + if (!isRunId(runId)) return undefined + const invocation = qs.get("invocation") + return { + runId, + context: qs.get("context") ?? undefined, + invocationId: isRunId(invocation) ? invocation : undefined, + } +}) +let viewLogsPinned = $state(false) +let jobPin = $derived.by(() => { + const job = $workspaceJobs[id] + return viewLogsPinned && job?.state === "failed" && job.runId + ? { runId: job.runId, invocationId: job.failedInvocationId } + : undefined +}) +let pin = $derived(linkedRun ?? jobPin) +let logContext = $derived(linkedRun?.context ?? workspace?.context ?? "default") +// "pending" until a linked run is checked against the live workspace. +let probed = $state<{ key: string; owned: boolean } | undefined>() +let linkKey = $derived( + linkedRun && workspace + ? `${linkedRun.runId}|${linkedRun.context ?? ""}|${workspace.uid ?? ""}` + : "", +) +// Only a linked run on a live workspace needs the check. The history view +// stays unmounted until it settles, because the main process keeps one read +// in flight and the probe must not be superseded by the view's own read. +let linkOwnership = $derived( + !linkKey + ? ("owned" as const) + : probed?.key !== linkKey + ? ("pending" as const) + : probed.owned + ? ("owned" as const) + : ("foreign" as const), +) +let readOnlyHistory = $derived( + !!linkedRun && (!workspace || linkOwnership === "foreign"), +) + +$effect(() => { + const link = linkedRun + const ws = workspace + const key = linkKey + if (!link || !ws || !key) return + let current = true + workspaceLogRunRead({ + context: link.context ?? ws.context ?? "default", + runId: link.runId, + limit: 1, + }) + .then((res) => { + if (!current || !res) return + probed = { + key, + owned: + res.run.workspaceUid !== null && + res.run.workspaceUid === ws.uid && + res.run.context === (ws.context ?? res.run.context), + } + }) + .catch(() => { + if (current) probed = { key, owned: false } + }) + return () => { + current = false + } +}) let outputLines = $state([]) let commandId = $state(null) let operationLabel = $state("") @@ -628,7 +704,23 @@ async function handleRenameConfirmed() { Workspaces - {#if !workspace} + {#if readOnlyHistory && linkedRun} +
+

+ {workspace + ? "This run belongs to a different workspace generation. Showing its retained history read-only." + : "This workspace no longer exists. Showing its retained history read-only."} +

+ +
+ {:else if workspace && linkOwnership === "pending"} +

Checking run history...

+ {:else if !workspace}

Workspace not found.

{:else}
@@ -667,7 +759,7 @@ async function handleRenameConfirmed() { {/if}
- (activeTab = "logs")} /> + { viewLogsPinned = true; activeTab = "logs" }} />
Provider {workspace.provider?.name ?? "Not configured"} @@ -934,7 +1026,18 @@ async function handleRenameConfirmed() { -
+
+
+

Setup and build history

+ +
@@ -976,7 +1079,7 @@ async function handleRenameConfirmed() { - Log Files + Desktop diagnostic files {#if logEntries.length > 0} ({logEntries.length} files) {/if} diff --git a/desktop/src/shared/app-route.ts b/desktop/src/shared/app-route.ts index 415d86290b..7d01f79da7 100644 --- a/desktop/src/shared/app-route.ts +++ b/desktop/src/shared/app-route.ts @@ -43,3 +43,23 @@ export function workspaceRoute( const route = `/workspaces/${encodeURIComponent(workspaceId)}` return tab ? `${route}?tab=${encodeURIComponent(tab)}` : route } + +export interface WorkspaceLogsTarget { + context?: string + runId?: string + invocationId?: string +} + +/** Opens the Logs tab, optionally pinned to a retained run and invocation. */ +export function workspaceLogsRoute( + workspaceId: string, + target: WorkspaceLogsTarget = {}, +): AppRoute { + const params = new URLSearchParams({ tab: "logs" }) + if (target.runId) { + if (target.context) params.set("context", target.context) + params.set("run", target.runId) + if (target.invocationId) params.set("invocation", target.invocationId) + } + return `/workspaces/${encodeURIComponent(workspaceId)}?${params.toString()}` +} diff --git a/desktop/src/shared/cli-error.ts b/desktop/src/shared/cli-error.ts index e7f0a463f1..b7fc582fac 100644 --- a/desktop/src/shared/cli-error.ts +++ b/desktop/src/shared/cli-error.ts @@ -53,6 +53,8 @@ export interface CliResultEnvelope { url?: string warnings?: string[] recovery?: boolean + /** Durable history identity. Absent from envelopes written by older CLIs. */ + runId?: string } export interface CliErrorEnvelope { @@ -64,6 +66,40 @@ export interface CliErrorEnvelope { context?: Record } +const RUN_UUID = + /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/ + +export interface RunIdentity { + runId?: string + failedInvocationId?: string +} + +/** + * Reads optional history identity from string-valued error context. Values + * that are not canonical UUIDs are dropped, never trusted. + */ +export function runIdentityFromContext( + context: Record | undefined, +): RunIdentity { + const identity: RunIdentity = {} + if (context?.runId && RUN_UUID.test(context.runId)) + identity.runId = context.runId + if ( + identity.runId && + context?.failedInvocationId && + RUN_UUID.test(context.failedInvocationId) + ) + identity.failedInvocationId = context.failedInvocationId + return identity +} + +/** Run ID from a result envelope or task submission, when present and valid. */ +export function runIdFromPayload(value: unknown): string | undefined { + if (!value || typeof value !== "object") return undefined + const runId = (value as { runId?: unknown }).runId + return typeof runId === "string" && RUN_UUID.test(runId) ? runId : undefined +} + export function cliErrorFromEnvelope(value: unknown): CLIError | undefined { if (!value || typeof value !== "object" || Array.isArray(value)) return undefined diff --git a/desktop/src/shared/workspace-logs.ts b/desktop/src/shared/workspace-logs.ts new file mode 100644 index 0000000000..c018fa490f --- /dev/null +++ b/desktop/src/shared/workspace-logs.ts @@ -0,0 +1,801 @@ +// Typed contract for retained build and lifecycle history. It mirrors the +// JSON written by `devsy workspace logs --source setup --result-format=json` +// and is deliberately separate from the status envelope (schema v1). + +export const WORKSPACE_LOG_SCHEMA_VERSION = 1 + +export const MAX_READ_LIMIT = 2000 +export const MAX_INDEX_LIMIT = 100 +export const MAX_CURSOR_BYTES = 8 * 1024 + +export type LogRecordKind = "producer" | "invocation" | "output" | "capture" +export type LogStream = "stdout" | "stderr" | "combined" +export type InvocationState = + | "pending" + | "deferred" + | "running" + | "succeeded" + | "failed" + | "skipped" + | "canceled" + | "completion_unknown" +export type ProducerState = "reserved" | "running" | "terminal" | "unavailable" +export type CommandState = + | "pending" + | "running" + | "succeeded" + | "failed" + | "canceled" + | "unknown" +export type CaptureState = + | "pending" + | "recording" + | "complete" + | "partial" + | "unavailable" +export type CaptureCondition = + | "truncated" + | "retention_gap" + | "write_error" + | "unsupported_source" + | "source_unavailable" + | "interrupted" + | "capacity" + | "producer_cap" + | "malformed_record" + +export const LIFECYCLE_HOOKS = [ + "initializeCommand", + "onCreateCommand", + "updateContentCommand", + "postCreateCommand", + "postStartCommand", + "postAttachCommand", +] as const + +const INVOCATION_STATES: readonly string[] = [ + "pending", + "deferred", + "running", + "succeeded", + "failed", + "skipped", + "canceled", + "completion_unknown", +] +const PRODUCER_STATES: readonly string[] = [ + "reserved", + "running", + "terminal", + "unavailable", +] +const COMMAND_STATES: readonly string[] = [ + "pending", + "running", + "succeeded", + "failed", + "canceled", + "unknown", +] +const CAPTURE_STATES: readonly string[] = [ + "pending", + "recording", + "complete", + "partial", + "unavailable", +] +const CONDITIONS: readonly string[] = [ + "truncated", + "retention_gap", + "write_error", + "unsupported_source", + "source_unavailable", + "interrupted", + "capacity", + "producer_cap", + "malformed_record", +] + +export interface LogRecord { + schemaVersion: number + kind: LogRecordKind + runId: string + producerId: string + sequence: number + time: string + parentProducerId?: string + producerState?: ProducerState + discoverySealed?: boolean + invocationId?: string + phase?: string + hook?: string + hookIndex?: number + hookKey?: string + location?: "host" | "provider_host" | "container" + state?: InvocationState + reason?: string + durationMillis?: number + exitCode?: number + signal?: string + stream?: LogStream + text?: string + bytes?: number + elided?: boolean + condition?: CaptureCondition + detail?: string + droppedBytes?: number +} + +export interface LogRunSummary { + runId: string + context: string + workspaceUid: string | null + workspaceIdAtStart?: string + command: string + taskId?: string + createdAt: string + commandState: CommandState + captureState: CaptureState + conditions?: CaptureCondition[] +} + +export interface LogSourceAvailability { + producerId: string + available: boolean + reason?: string +} + +export interface LogReadResponse { + schemaVersion: number + run: LogRunSummary + records: LogRecord[] + nextCursor?: string + hasMore: boolean + availability?: LogSourceAvailability[] + complete: boolean +} + +export interface LogIndexResponse { + schemaVersion: number + runs: LogRunSummary[] + nextCursor?: string + hasMore: boolean +} + +/** Identity the renderer holds for a run. The context is always explicit. */ +export interface LogRunIdentity { + context: string + runId: string + /** Stored owner of the run. Null or absent when unresolved or unknown. */ + workspaceUid?: string | null +} + +export interface LogRunsRequest { + context: string + workspaceId?: string + workspaceUid?: string + after?: string + limit?: number +} + +export interface LogRunReadRequest extends LogRunIdentity { + workspaceId?: string + hook?: string + invocationId?: string + after?: string + limit?: number +} + +export type LogRunExportRequest = Omit + +export type LogRunExportResult = + | { status: "saved"; path: string; bytes: number; gaps: boolean } + | { status: "canceled" } + +export class WorkspaceLogContractError extends Error { + constructor(message: string) { + super(message) + this.name = "WorkspaceLogContractError" + } +} + +const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/ +const CONTEXT = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/ +const WORKSPACE_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/ +const CURSOR = /^[A-Za-z0-9_\-=.]+$/ + +export const isRunId = (value: unknown): value is string => + typeof value === "string" && UUID.test(value) + +export const isLifecycleHook = (value: unknown): boolean => + typeof value === "string" && + (LIFECYCLE_HOOKS as readonly string[]).includes(value) + +function fail(message: string): never { + throw new WorkspaceLogContractError(message) +} + +function requireSelector( + name: string, + value: unknown, + pattern: RegExp, +): string { + if (typeof value !== "string" || !pattern.test(value)) fail(`invalid ${name}`) + return value +} + +function optionalCursor(value: unknown): string | undefined { + if (value === undefined || value === "") return undefined + if ( + typeof value !== "string" || + value.length > MAX_CURSOR_BYTES || + !CURSOR.test(value) + ) + fail("invalid cursor") + return value +} + +function optionalLimit(value: unknown, max: number): number | undefined { + if (value === undefined) return undefined + if ( + !Number.isInteger(value) || + (value as number) < 1 || + (value as number) > max + ) + fail("invalid limit") + return value as number +} + +function asRecord(value: unknown, what: string): Record { + if (!value || typeof value !== "object" || Array.isArray(value)) + fail(`${what} is not an object`) + return value as Record +} + +/** Validates a renderer request. Throws WorkspaceLogContractError. */ +export function validateRunsRequest(value: unknown): LogRunsRequest { + const v = asRecord(value, "request") + return { + context: requireSelector("context", v.context, CONTEXT), + workspaceId: + v.workspaceId === undefined + ? undefined + : requireSelector("workspaceId", v.workspaceId, WORKSPACE_ID), + workspaceUid: + v.workspaceUid === undefined + ? undefined + : requireSelector("workspaceUid", v.workspaceUid, /^[\w.-]{1,128}$/), + after: optionalCursor(v.after), + limit: optionalLimit(v.limit, MAX_INDEX_LIMIT), + } +} + +export function validateRunExportRequest(value: unknown): LogRunExportRequest { + const v = asRecord(value, "request") + const req: LogRunExportRequest = { + context: requireSelector("context", v.context, CONTEXT), + runId: requireSelector("runId", v.runId, UUID), + } + if (v.workspaceId !== undefined) + req.workspaceId = requireSelector( + "workspaceId", + v.workspaceId, + WORKSPACE_ID, + ) + if (v.workspaceUid !== undefined && v.workspaceUid !== null) + req.workspaceUid = requireSelector( + "workspaceUid", + v.workspaceUid, + /^[\w.-]{1,128}$/, + ) + if (v.hook !== undefined) { + if (!isLifecycleHook(v.hook)) fail("invalid hook") + req.hook = v.hook as string + } + if (v.invocationId !== undefined) + req.invocationId = requireSelector("invocationId", v.invocationId, UUID) + if (req.hook && req.invocationId) + fail("hook and invocationId cannot be combined") + return req +} + +export function validateRunReadRequest(value: unknown): LogRunReadRequest { + const v = asRecord(value, "request") + return { + ...validateRunExportRequest(value), + after: optionalCursor(v.after), + limit: optionalLimit(v.limit, MAX_READ_LIMIT), + } +} + +function optionalString(v: Record, key: string): void { + if (v[key] !== undefined && typeof v[key] !== "string") + fail(`${key} must be a string`) +} + +function optionalNumber(v: Record, key: string): void { + if ( + v[key] !== undefined && + (typeof v[key] !== "number" || !Number.isFinite(v[key])) + ) + fail(`${key} must be a number`) +} + +function oneOf(value: unknown, allowed: readonly string[], what: string): void { + if (typeof value !== "string" || !allowed.includes(value)) + fail(`invalid ${what}`) +} + +const RECORD_STRINGS = [ + "parentProducerId", + "invocationId", + "phase", + "hook", + "hookKey", + "reason", + "signal", + "text", + "detail", +] +const RECORD_NUMBERS = [ + "hookIndex", + "durationMillis", + "exitCode", + "bytes", + "droppedBytes", +] + +function parseRecord(value: unknown): LogRecord { + const v = asRecord(value, "record") + if (v.schemaVersion !== WORKSPACE_LOG_SCHEMA_VERSION) + fail("unsupported record schema version") + oneOf(v.kind, ["producer", "invocation", "output", "capture"], "record kind") + if (!isRunId(v.runId)) fail("invalid record runId") + if (!isRunId(v.producerId)) fail("invalid record producerId") + if (!Number.isSafeInteger(v.sequence) || (v.sequence as number) < 1) + fail("invalid record sequence") + if (typeof v.time !== "string") fail("invalid record time") + for (const key of RECORD_STRINGS) optionalString(v, key) + for (const key of RECORD_NUMBERS) optionalNumber(v, key) + if (v.invocationId !== undefined && !isRunId(v.invocationId)) + fail("invalid record invocationId") + if (v.kind === "invocation") oneOf(v.state, INVOCATION_STATES, "state") + if (v.kind === "output") { + oneOf(v.stream, ["stdout", "stderr", "combined"], "stream") + if (v.text !== undefined && typeof v.text !== "string") + fail("text must be a string") + } + if (v.producerState !== undefined) + oneOf(v.producerState, PRODUCER_STATES, "producerState") + if (v.location !== undefined) + oneOf(v.location, ["host", "provider_host", "container"], "location") + if (v.kind === "capture") oneOf(v.condition, CONDITIONS, "condition") + return v as unknown as LogRecord +} + +function parseSummary(value: unknown): LogRunSummary { + const v = asRecord(value, "run") + if (!isRunId(v.runId)) fail("invalid run id") + if (typeof v.context !== "string" || v.context.length === 0) + fail("invalid run context") + if (v.workspaceUid !== null && typeof v.workspaceUid !== "string") + fail("invalid run workspaceUid") + for (const key of ["workspaceIdAtStart", "taskId"]) optionalString(v, key) + if (typeof v.command !== "string") fail("invalid run command") + if (typeof v.createdAt !== "string") fail("invalid run createdAt") + oneOf(v.commandState, COMMAND_STATES, "commandState") + oneOf(v.captureState, CAPTURE_STATES, "captureState") + if (v.conditions !== undefined) { + if (!Array.isArray(v.conditions)) fail("invalid conditions") + for (const c of v.conditions) oneOf(c, CONDITIONS, "condition") + } + return v as unknown as LogRunSummary +} + +function checkPaging(v: Record): void { + if (v.schemaVersion !== WORKSPACE_LOG_SCHEMA_VERSION) + fail("unsupported schema version") + if (typeof v.hasMore !== "boolean") fail("invalid hasMore") + if (v.nextCursor !== undefined) optionalCursor(v.nextCursor) +} + +/** Parses and checks one read page. Records must be an array, never null. */ +export function parseReadResponse(value: unknown): LogReadResponse { + const v = asRecord(value, "read response") + checkPaging(v) + if (typeof v.complete !== "boolean") fail("invalid complete") + if (!Array.isArray(v.records)) fail("records must be an array") + const run = parseSummary(v.run) + const records = v.records.map(parseRecord) + for (const r of records) + if (r.runId !== run.runId) fail("record belongs to a different run") + const availability = v.availability + if (availability !== undefined) { + if (!Array.isArray(availability)) fail("invalid availability") + for (const a of availability) { + const item = asRecord(a, "availability") + if (typeof item.producerId !== "string") fail("invalid availability") + if (typeof item.available !== "boolean") fail("invalid availability") + optionalString(item, "reason") + } + } + return { ...v, run, records } as unknown as LogReadResponse +} + +export function parseIndexResponse(value: unknown): LogIndexResponse { + const v = asRecord(value, "index response") + checkPaging(v) + if (!Array.isArray(v.runs)) fail("runs must be an array") + return { ...v, runs: v.runs.map(parseSummary) } as unknown as LogIndexResponse +} + +/** Rejects a response that answers a different selection than requested. */ +export function checkReadIdentity( + req: LogRunIdentity, + res: LogReadResponse, +): void { + if (res.run.runId !== req.runId) fail("response is for a different run") + if (res.run.context !== req.context) + fail("response is for a different context") + if ( + typeof req.workspaceUid === "string" && + res.run.workspaceUid !== req.workspaceUid + ) + fail("response is for a different workspace") +} + +export function checkIndexIdentity( + req: LogRunsRequest, + res: LogIndexResponse, +): void { + for (const run of res.runs) { + if (run.context !== req.context) + fail("index entry from a different context") + if (req.workspaceUid && run.workspaceUid !== req.workspaceUid) + fail("index entry from a different workspace") + } +} + +// --------------------------------------------------------------------------- +// Presentation model shared by main (export) and renderer (list, transcript). + +export interface InvocationView { + id: string + producerId: string + phase: string + hook?: string + hookIndex?: number + hookKey?: string + state: InvocationState + reason?: string + exitCode?: number + signal?: string + durationMillis?: number + location?: string + outputBytes: number + outputRecords: number + /** True when output for this invocation may be missing from the archive. */ + uncaptured: boolean + conditions: CaptureCondition[] + firstSequence: number +} + +const UNCAPTURED_CONDITIONS: readonly CaptureCondition[] = [ + "unsupported_source", + "source_unavailable", + "interrupted", + "retention_gap", + "write_error", + "capacity", + "malformed_record", +] + +interface TransitionMark { + producerId: string + sequence: number + time: number + rank: number +} + +function stateRank(state: InvocationState | undefined): number { + switch (state) { + case "pending": + return 0 + case "deferred": + return 1 + case "running": + return 2 + case "skipped": + return 3 + default: + return 4 + } +} + +/** + * Sequences are only comparable inside one producer. Across producers a + * terminal state is never replaced by an earlier phase, and equal phases fall + * back to record time. + */ +function supersedes(prior: TransitionMark, next: TransitionMark): boolean { + if (prior.producerId === next.producerId) + return next.sequence < prior.sequence + if (prior.rank !== next.rank) return next.rank < prior.rank + return next.time < prior.time +} + +const IN_FLIGHT_STATES: readonly InvocationState[] = [ + "pending", + "deferred", + "running", +] + +/** + * Folds record pages into per-invocation views. Pages may arrive in several + * calls and late records may precede earlier ones, so each transition is + * ordered by (producer, sequence) rather than arrival. + */ +export class InvocationTable { + private views = new Map() + private latest = new Map() + private producerConditions = new Map>() + private unavailable = new Set() + private settled = false + + apply(records: LogRecord[]): void { + for (const r of records) { + if (r.kind === "invocation" && r.invocationId) this.transition(r) + else if (r.kind === "output" && r.invocationId) this.output(r) + else if (r.kind === "capture" && r.condition) this.capture(r) + else if (r.kind === "producer" && r.producerState === "unavailable") + this.unavailable.add(r.producerId) + } + } + + markUnavailable(producerIds: string[]): void { + for (const id of producerIds) this.unavailable.add(id) + } + + /** + * Declares that every producer of the run has finished. A step that never + * reached a result is then shown as completion unknown instead of pending, + * deferred or running forever. + */ + markSettled(settled: boolean): void { + this.settled = settled + } + + list(): InvocationView[] { + return [...this.views.values()] + .map((v) => this.finish(v)) + .sort((a, b) => a.firstSequence - b.firstSequence) + } + + private view(r: LogRecord): InvocationView { + let v = this.views.get(r.invocationId as string) + if (!v) { + v = { + id: r.invocationId as string, + producerId: r.producerId, + phase: r.phase ?? "", + state: "pending", + outputBytes: 0, + outputRecords: 0, + uncaptured: false, + conditions: [], + firstSequence: r.sequence, + } + this.views.set(v.id, v) + } + v.firstSequence = Math.min(v.firstSequence, r.sequence) + return v + } + + private transition(r: LogRecord): void { + const v = this.view(r) + const mark: TransitionMark = { + producerId: r.producerId, + sequence: r.sequence, + time: Date.parse(r.time) || 0, + rank: stateRank(r.state), + } + const prior = this.latest.get(v.id) + if (prior && supersedes(prior, mark)) return + this.latest.set(v.id, mark) + v.state = r.state as InvocationState + v.phase = r.phase ?? v.phase + v.hook = r.hook ?? v.hook + v.hookIndex = r.hookIndex ?? v.hookIndex + v.hookKey = r.hookKey ?? v.hookKey + v.location = r.location ?? v.location + v.reason = r.reason + v.exitCode = r.exitCode + v.signal = r.signal + v.durationMillis = r.durationMillis ?? v.durationMillis + } + + private output(r: LogRecord): void { + const v = this.view(r) + v.outputRecords++ + v.outputBytes += r.bytes ?? (r.text ? r.text.length : 0) + if (r.elided) this.note(v, "capacity") + } + + private capture(r: LogRecord): void { + const condition = r.condition as CaptureCondition + if (r.invocationId) { + this.note(this.view(r), condition) + return + } + const set = this.producerConditions.get(r.producerId) ?? new Set() + this.producerConditions.set(r.producerId, set) + set.add(condition) + } + + private note(v: InvocationView, condition: CaptureCondition): void { + if (!v.conditions.includes(condition)) v.conditions.push(condition) + } + + private finish(v: InvocationView): InvocationView { + const conditions = [...v.conditions] + for (const c of this.producerConditions.get(v.producerId) ?? []) + if (!conditions.includes(c)) conditions.push(c) + const missing = + this.unavailable.has(v.producerId) || + conditions.some((c) => UNCAPTURED_CONDITIONS.includes(c)) + const state = + this.settled && IN_FLIGHT_STATES.includes(v.state) + ? "completion_unknown" + : v.state + return { ...v, state, conditions, uncaptured: missing } + } +} + +export type OutputPresence = "has-output" | "empty" | "not-captured" | "none" + +/** Distinguishes a silent success from output that was never captured. */ +export function outputPresence(v: InvocationView): OutputPresence { + if (v.outputRecords > 0) return "has-output" + if (v.state === "skipped" || v.state === "pending" || v.state === "deferred") + return "none" + if (v.uncaptured) return "not-captured" + if (v.state === "running" || v.state === "completion_unknown") return "none" + return "empty" +} + +const STATE_LABEL: Record = { + pending: "Pending", + deferred: "Deferred", + running: "Running", + succeeded: "Succeeded", + failed: "Failed", + skipped: "Skipped", + canceled: "Canceled", + completion_unknown: "Completion unknown", +} + +export const invocationStateLabel = (state: InvocationState): string => + STATE_LABEL[state] ?? state + +export function invocationTitle(v: InvocationView): string { + const base = v.hook ?? (v.phase || "step") + const key = v.hookKey ? ` (${v.hookKey})` : "" + const index = + v.hookIndex !== undefined && v.hookIndex > 0 ? ` #${v.hookIndex + 1}` : "" + return `${base}${key}${index}` +} + +const REASON_LABEL: Record = { + already_attempted: "Already attempted, not run again", + no_command: "No command configured", + launch_not_observed: "Launch was not observed", + already_running: "Another worker was already running", +} + +export function invocationDetail(v: InvocationView): string { + const parts: string[] = [] + if (v.reason) + parts.push(REASON_LABEL[v.reason] ?? v.reason.replaceAll("_", " ")) + if (v.exitCode !== undefined) parts.push(`exit code ${v.exitCode}`) + if (v.signal) parts.push(`signal ${v.signal}`) + if (v.durationMillis !== undefined) + parts.push(`${(v.durationMillis / 1000).toFixed(1)}s`) + return parts.join(", ") +} + +export function failedInvocations(views: InvocationView[]): InvocationView[] { + return views.filter((v) => v.state === "failed") +} + +export interface TranscriptLine { + /** Stable key within one builder. */ + key: number + text: string + stream: LogStream + invocationId: string + label: string +} + +/** + * Turns output records into literal lines. Text is never parsed: JSON, HTML + * and log-looking content stay as typed. A chunk that ends without a newline + * continues on the next chunk of the same invocation and stream. + */ +export class TranscriptBuilder { + private lines: TranscriptLine[] = [] + private open = new Map() + private nextKey = 0 + + constructor(private labels: (invocationId: string) => string = () => "") {} + + append(records: LogRecord[]): void { + for (const r of records) { + if (r.kind !== "output" || !r.invocationId || !r.stream) continue + this.appendText(r.invocationId, r.stream, r.text ?? "") + } + } + + snapshot(): TranscriptLine[] { + return this.lines.slice() + } + + get length(): number { + return this.lines.length + } + + private appendText(id: string, stream: LogStream, text: string): void { + if (text === "") return + const slot = `${id}\u0000${stream}` + const parts = text.split("\n") + parts.forEach((part, i) => { + const last = i === parts.length - 1 + if (last && part === "") { + this.open.delete(slot) + return + } + const continuing = i === 0 ? this.open.get(slot) : undefined + const line = continuing ?? this.newLine(id, stream) + line.text += part + if (last) this.open.set(slot, line) + else this.open.delete(slot) + }) + } + + private newLine(id: string, stream: LogStream): TranscriptLine { + const line: TranscriptLine = { + key: this.nextKey++, + text: "", + stream, + invocationId: id, + label: this.labels(id), + } + this.lines.push(line) + return line + } +} + +/** Plain text for copy and export. Gaps are stated, never hidden. */ +export function transcriptText( + lines: TranscriptLine[], + notes: string[] = [], +): string { + const body = lines.map((l) => l.text).join("\n") + return notes.length > 0 + ? `${notes.map((n) => `# ${n}`).join("\n")}\n${body}` + : body +} + +export function captureNotes( + run: LogRunSummary, + availability: LogSourceAvailability[] | undefined, + complete: boolean, +): string[] { + const notes: string[] = [] + for (const c of run.conditions ?? []) + notes.push(`Capture condition: ${c.replaceAll("_", " ")}`) + for (const a of availability ?? []) + if (!a.available) + notes.push(`Source unavailable: ${a.reason ?? "unknown reason"}`) + if (!complete) notes.push("History is incomplete") + return notes +} diff --git a/desktop/src/shared/workspace-operation.ts b/desktop/src/shared/workspace-operation.ts index 151265c440..5c917d1263 100644 --- a/desktop/src/shared/workspace-operation.ts +++ b/desktop/src/shared/workspace-operation.ts @@ -16,6 +16,9 @@ export interface WorkspaceJob { error?: string logError?: string refreshError?: string + /** Durable history identity for this operation, when the CLI reported one. */ + runId?: string + failedInvocationId?: string } export function workspaceJobBusy(job?: WorkspaceJob): boolean { diff --git a/e2e/e2e_suite_test.go b/e2e/e2e_suite_test.go index dd956526ac..8e2d06f9ad 100644 --- a/e2e/e2e_suite_test.go +++ b/e2e/e2e_suite_test.go @@ -1,5 +1,6 @@ package e2e +// Blank imports register the e2e test suites. import ( "context" "os" @@ -9,7 +10,6 @@ import ( "github.com/devsy-org/devsy/cmd" "github.com/devsy-org/devsy/e2e/framework" - // Register tests. _ "github.com/devsy-org/devsy/e2e/tests/build" _ "github.com/devsy-org/devsy/e2e/tests/ci" _ "github.com/devsy-org/devsy/e2e/tests/configapply" diff --git a/e2e/framework/command.go b/e2e/framework/command.go index d351e9c77f..5942c89b96 100644 --- a/e2e/framework/command.go +++ b/e2e/framework/command.go @@ -19,6 +19,10 @@ import ( "github.com/devsy-org/devsy/pkg/workspace" ) +const ( + contextValue = "context" +) + var ( flagResultFormat = names.Flag(names.ResultFormat) flagIDE = names.Flag(names.IDE) @@ -251,19 +255,13 @@ func (f *Framework) DevsySSHOnce( } func (f *Framework) DevsySSHEchoTestString(ctx context.Context, workspace string) error { - err := f.ExecCommand( - ctx, - true, - true, - "mYtEsTsTrInG", - []string{ - cmdWorkspace, - cmdSSH, - flagCommand, - "echo 'bVl0RXNUc1RySW5H' | base64 -d", - workspace, - }, - ) + err := f.ExecCommandContaining(ctx, "mYtEsTsTrInG", []string{ + cmdWorkspace, + cmdSSH, + flagCommand, + "echo 'bVl0RXNUc1RySW5H' | base64 -d", + workspace, + }) if err != nil { return fmt.Errorf("devsy ssh failed: %s", err.Error()) } @@ -274,7 +272,7 @@ func (f *Framework) DevsyProviderOptionsCheckNamespaceDescription( ctx context.Context, provider, searchStr string, ) error { - err := f.ExecCommand(ctx, true, true, searchStr, []string{cmdProvider, cmdGet, provider}) + err := f.ExecCommandContaining(ctx, searchStr, []string{cmdProvider, cmdGet, provider}) if err != nil { return fmt.Errorf( "did not found value %s in devsy provider options output. error: %s", @@ -287,7 +285,7 @@ func (f *Framework) DevsyProviderOptionsCheckNamespaceDescription( func (f *Framework) DevsyProviderList(ctx context.Context, extraArgs ...string) error { baseArgs := []string{cmdProvider, cmdList} - err := f.ExecCommand(ctx, false, true, "", append(baseArgs, extraArgs...)) + err := f.ExecCommandStdout(ctx, append(baseArgs, extraArgs...)) if err != nil { return fmt.Errorf("devsy provider list failed: %s", err.Error()) } @@ -301,18 +299,12 @@ func (f *Framework) DevsyProviderUse( ) error { if len(extraArgs) > 0 { configureArgs := []string{cmdProvider, "init", provider} - if err := f.ExecCommand( - ctx, - false, - true, - "", - append(configureArgs, extraArgs...), - ); err != nil { + if err := f.ExecCommandStdout(ctx, append(configureArgs, extraArgs...)); err != nil { return fmt.Errorf("devsy provider init failed: %s", err.Error()) } } useArgs := []string{cmdProvider, cmdUse, provider} - if err := f.ExecCommand(ctx, false, true, "", useArgs); err != nil { + if err := f.ExecCommandStdout(ctx, useArgs); err != nil { return fmt.Errorf("devsy provider use failed: %s", err.Error()) } return nil @@ -341,7 +333,7 @@ func (f *Framework) DevsyStatus( func (f *Framework) DevsyStop(ctx context.Context, workspace string) error { baseArgs := []string{cmdWorkspace, "stop"} baseArgs = append(baseArgs, workspace) - err := f.ExecCommand(ctx, false, false, "", baseArgs) + err := f.ExecCommandStdout(ctx, baseArgs) if err != nil { return fmt.Errorf("devsy stop failed: %s", err.Error()) } @@ -427,7 +419,7 @@ func (f *Framework) DevsyProviderRename( ) error { baseArgs := []string{cmdProvider, "rename", oldName, newName} baseArgs = append(baseArgs, args...) - err := f.ExecCommand(ctx, false, false, "", baseArgs) + err := f.ExecCommandStdout(ctx, baseArgs) if err != nil { return fmt.Errorf("devsy provider rename failed: %s", err.Error()) } @@ -443,7 +435,7 @@ func (f *Framework) DevsyRename( ) error { baseArgs := []string{cmdWorkspace, "rename", oldName, newName} baseArgs = append(baseArgs, args...) - err := f.ExecCommand(ctx, false, false, "", baseArgs) + err := f.ExecCommandStdout(ctx, baseArgs) if err != nil { return fmt.Errorf("devsy rename failed: %s", err.Error()) } @@ -467,7 +459,7 @@ func (f *Framework) DevsyProviderOptionsJSON( func (f *Framework) DevsyProviderSetSource(ctx context.Context, args ...string) error { baseArgs := []string{cmdProvider, "set-source"} baseArgs = append(baseArgs, args...) - err := f.ExecCommand(ctx, false, false, "", baseArgs) + err := f.ExecCommandStdout(ctx, baseArgs) if err != nil { return fmt.Errorf("devsy provider set-source failed: %s", err.Error()) } @@ -477,7 +469,7 @@ func (f *Framework) DevsyProviderSetSource(ctx context.Context, args ...string) func (f *Framework) DevsyMachineCreate(ctx context.Context, args []string) error { baseArgs := []string{"machine", "create"} baseArgs = append(baseArgs, args...) - err := f.ExecCommand(ctx, false, false, "", baseArgs) + err := f.ExecCommandStdout(ctx, baseArgs) if err != nil { return fmt.Errorf("devsy machine create failed: %s", err.Error()) } @@ -487,7 +479,7 @@ func (f *Framework) DevsyMachineCreate(ctx context.Context, args []string) error func (f *Framework) DevsyMachineDelete(ctx context.Context, args []string) error { baseArgs := []string{"machine", cmdDelete} baseArgs = append(baseArgs, args...) - err := f.ExecCommand(ctx, false, false, "", baseArgs) + err := f.ExecCommandStdout(ctx, baseArgs) if err != nil { return fmt.Errorf("devsy machine delete failed: %s", err.Error()) } @@ -524,7 +516,7 @@ func (f *Framework) DevsyWorkspaceDelete( } baseArgs = append(baseArgs, extraArgs...) - return f.ExecCommand(ctx, false, true, fmt.Sprintf("deleted workspace %s", workspace), baseArgs) + return f.ExecCommandStdout(ctx, baseArgs) } // DetachedCleanupContext bounds cleanup work to timeout without inheriting @@ -641,7 +633,7 @@ func (f *Framework) DevsyProviderFindOption( extraArgs ...string, ) error { baseArgs := []string{cmdProvider, cmdGet, provider} - err := f.ExecCommand(ctx, false, true, searchStr, append(baseArgs, extraArgs...)) + err := f.ExecCommandStdout(ctx, append(baseArgs, extraArgs...)) if err != nil { return fmt.Errorf("devsy provider get failed: %s", err.Error()) } @@ -653,8 +645,8 @@ func (f *Framework) DevsyContextCreate( name string, extraArgs ...string, ) error { - baseArgs := []string{"context", "create", name} - err := f.ExecCommand(ctx, false, true, "", append(baseArgs, extraArgs...)) + baseArgs := []string{contextValue, "create", name} + err := f.ExecCommandStdout(ctx, append(baseArgs, extraArgs...)) if err != nil { return fmt.Errorf("devsy context create failed: %s", err.Error()) } @@ -662,8 +654,8 @@ func (f *Framework) DevsyContextCreate( } func (f *Framework) DevsyContextUse(ctx context.Context, name string, extraArgs ...string) error { - baseArgs := []string{"context", cmdUse, name} - err := f.ExecCommand(ctx, false, true, "", append(baseArgs, extraArgs...)) + baseArgs := []string{contextValue, cmdUse, name} + err := f.ExecCommandStdout(ctx, append(baseArgs, extraArgs...)) if err != nil { return fmt.Errorf("devsy context use failed: %s", err.Error()) } @@ -675,8 +667,8 @@ func (f *Framework) DevsyContextDelete( name string, extraArgs ...string, ) error { - baseArgs := []string{"context", cmdDelete, name} - err := f.ExecCommand(ctx, false, true, "", append(baseArgs, extraArgs...)) + baseArgs := []string{contextValue, cmdDelete, name} + err := f.ExecCommandStdout(ctx, append(baseArgs, extraArgs...)) if err != nil { return fmt.Errorf("devsy context delete failed: %s", err.Error()) } @@ -685,7 +677,7 @@ func (f *Framework) DevsyContextDelete( func (f *Framework) DevsyIDEUse(ctx context.Context, ide string, extraArgs ...string) error { baseArgs := []string{"ide", cmdUse, ide} - err := f.ExecCommand(ctx, false, true, "", append(baseArgs, extraArgs...)) + err := f.ExecCommandStdout(ctx, append(baseArgs, extraArgs...)) if err != nil { return fmt.Errorf("devsy ide use failed %s", err.Error()) } diff --git a/e2e/framework/exec.go b/e2e/framework/exec.go index 45642c116a..3af73a9fc3 100644 --- a/e2e/framework/exec.go +++ b/e2e/framework/exec.go @@ -25,6 +25,7 @@ const execWaitDelay = 30 * time.Second func (f *Framework) ExecCommandOutput(ctx context.Context, args []string) (string, error) { var execOut bytes.Buffer + //nolint:gosec // e2e harness runs its own test commands by design cmd := exec.CommandContext(ctx, filepath.Join(f.DevsyBinDir, f.DevsyBinName), args...) docker.PrepareForGroupCancellation(cmd) cmd.WaitDelay = execWaitDelay @@ -40,6 +41,7 @@ func (f *Framework) ExecCommandOutput(ctx context.Context, args []string) (strin // ExecCommandStdout executes the command string with the devsy test binary. func (f *Framework) ExecCommandStdout(ctx context.Context, args []string) error { + //nolint:gosec // e2e harness runs its own test commands by design cmd := exec.CommandContext(ctx, filepath.Join(f.DevsyBinDir, f.DevsyBinName), args...) docker.PrepareForGroupCancellation(cmd) cmd.WaitDelay = execWaitDelay @@ -52,15 +54,16 @@ func (f *Framework) ExecCommandStdout(ctx context.Context, args []string) error return nil } -// ExecCommand executes the command string with the devsy test binary. -func (f *Framework) ExecCommand( +// ExecCommandContaining executes the command string with the devsy test binary and +// fails unless its stdout contains searchString. +func (f *Framework) ExecCommandContaining( ctx context.Context, - captureStdOut, searchForString bool, searchString string, args []string, ) error { var execOut bytes.Buffer + //nolint:gosec // e2e harness runs its own test commands by design cmd := exec.CommandContext(ctx, filepath.Join(f.DevsyBinDir, f.DevsyBinName), args...) docker.PrepareForGroupCancellation(cmd) cmd.WaitDelay = execWaitDelay @@ -71,22 +74,20 @@ func (f *Framework) ExecCommand( return err } - if captureStdOut && searchForString { - if strings.Contains(execOut.String(), searchString) { - return nil - } - + if !strings.Contains(execOut.String(), searchString) { return fmt.Errorf("expected to find string %s in output", searchString) } return nil } -// ExecCommandCapture executes the command string with the devsy test binary, and returns stdout, stderr, and any error that occurred. +// ExecCommandCapture executes the command string with the devsy test binary, and returns +// stdout, stderr, and any error that occurred. func (f *Framework) ExecCommandCapture(ctx context.Context, args []string) (string, string, error) { var execOut bytes.Buffer var execErr bytes.Buffer + //nolint:gosec // e2e harness runs its own test commands by design cmd := exec.CommandContext(ctx, filepath.Join(f.DevsyBinDir, f.DevsyBinName), args...) docker.PrepareForGroupCancellation(cmd) cmd.WaitDelay = execWaitDelay diff --git a/e2e/framework/helper.go b/e2e/framework/helper.go index 6d3a296b10..3a4872d6e6 100644 --- a/e2e/framework/helper.go +++ b/e2e/framework/helper.go @@ -22,8 +22,9 @@ func ExpectNoError(err error, explain ...any) { ExpectNoErrorWithOffset(1, err, explain...) } -// ExpectNoErrorWithOffset checks if "err" is set, and if so, fails assertion while logging the error at "offset" levels above its caller -// (for example, for call chain f -> g -> ExpectNoErrorWithOffset(1, ...) error would be logged for "f"). +// ExpectNoErrorWithOffset checks if "err" is set, and if so, fails assertion while logging +// the error at "offset" levels above its caller (for example, for call chain f -> g -> ExpectNoErrorWithOffset(1, ...) +// error would be logged for "f"). func ExpectNoErrorWithOffset(offset int, err error, explain ...any) { gomega.ExpectWithOffset(1+offset, err).NotTo(gomega.HaveOccurred(), explain...) } diff --git a/e2e/framework/retry.go b/e2e/framework/retry.go index 8a919e617a..6a1696783c 100644 --- a/e2e/framework/retry.go +++ b/e2e/framework/retry.go @@ -93,98 +93,100 @@ func isRetryableDockerError(stderr string) bool { return false } -// execWithDockerRetry runs fn and retries if the failure looks transient: -// either a Docker registry error or a retryable SSH setup error in stderr -// (up commands build the tunnel before the workspace starts). Returns the -// last stdout, stderr, and error. -func execWithDockerRetry( - ctx context.Context, - fn func(ctx context.Context) (stdout, stderr string, err error), -) (string, string, error) { - var lastStdout, lastStderr string - var lastErr error - attempt := 0 +type execFunc func(ctx context.Context) (stdout, stderr string, err error) + +// retryPolicy describes one retry loop. label prefixes the retry log line and +// kind names the error class in the budget-exhausted message. +type retryPolicy struct { + backoff wait.Backoff + retryable func(err error, stderr string) bool + label string + kind string +} + +// retryResult is the outcome of runWithRetry. gaveUp is set when the loop +// stopped on a failed attempt, so callers add the attempt count to err. +type retryResult struct { + stdout, stderr string + attempts int + err error + gaveUp bool +} - for attempt = 1; attempt <= dockerPullBackoff.Steps; attempt++ { +func runWithRetry(ctx context.Context, policy retryPolicy, fn execFunc) retryResult { + var res retryResult + for res.attempts = 1; res.attempts <= policy.backoff.Steps; res.attempts++ { if err := ctx.Err(); err != nil { - return lastStdout, lastStderr, err + res.err = err + return res } - lastStdout, lastStderr, lastErr = fn(ctx) - if lastErr == nil { - return lastStdout, lastStderr, nil + res.stdout, res.stderr, res.err = fn(ctx) + if res.err == nil { + return res } - retryable := isRetryableDockerError(lastStderr) || isRetryableSSHError(lastErr, lastStderr) - if !retryable || attempt == dockerPullBackoff.Steps { + if !policy.retryable(res.err, res.stderr) || res.attempts == policy.backoff.Steps { break } - delay := nextBackoffDelay(dockerPullBackoff, attempt) + delay := nextBackoffDelay(policy.backoff, res.attempts) if !retryFitsBudget(ctx, delay) { - return lastStdout, lastStderr, fmt.Errorf( - "after %d attempts: retryable transient error; retry not attempted because "+ + res.err = fmt.Errorf( + "after %d attempts: %s; retry not attempted because "+ "remaining deadline budget was insufficient (next retry delay: %s): %w", - attempt, delay, lastErr, + res.attempts, policy.kind, delay, res.err, ) + return res } ginkgo.GinkgoWriter.Printf( - "[retry] attempt %d failed with transient error, retrying after %s: %s\n", - attempt, delay, lastErr, + "%s attempt %d failed with transient error, retrying after %s: %s\n", + policy.label, res.attempts, delay, res.err, ) if err := waitForRetry(ctx, delay); err != nil { - return lastStdout, lastStderr, err + res.err = err + return res } } - return lastStdout, lastStderr, fmt.Errorf("after %d attempts: %w", attempt, lastErr) + res.gaveUp = true + return res +} + +// execWithDockerRetry runs fn and retries if the failure looks transient: +// either a Docker registry error or a retryable SSH setup error in stderr +// (up commands build the tunnel before the workspace starts). Returns the +// last stdout, stderr, and error. +func execWithDockerRetry(ctx context.Context, fn execFunc) (string, string, error) { + res := runWithRetry(ctx, retryPolicy{ + backoff: dockerPullBackoff, + retryable: func(err error, stderr string) bool { + return isRetryableDockerError(stderr) || isRetryableSSHError(err, stderr) + }, + label: "[retry]", + kind: "retryable transient error", + }, fn) + if res.gaveUp { + return res.stdout, res.stderr, fmt.Errorf("after %d attempts: %w", res.attempts, res.err) + } + return res.stdout, res.stderr, res.err } // execWithSSHRetry runs fn and retries if the error indicates a transient SSH // connection failure. The fn callback must return stdout, stderr, and error so // that stderr can be inspected for SSH-specific patterns. -func execWithSSHRetry( - ctx context.Context, - workspace string, - fn func(ctx context.Context) (stdout, stderr string, err error), -) (string, error) { - var lastOut string - var lastStderr string - var lastErr error - attempt := 0 - - for attempt = 1; attempt <= sshBackoff.Steps; attempt++ { - if err := ctx.Err(); err != nil { - return lastOut, err - } - lastOut, lastStderr, lastErr = fn(ctx) - if lastErr == nil { - return lastOut, nil - } - if !isRetryableSSHError(lastErr, lastStderr) || attempt == sshBackoff.Steps { - break - } - delay := nextBackoffDelay(sshBackoff, attempt) - if !retryFitsBudget(ctx, delay) { - return lastOut, fmt.Errorf( - "after %d attempts: retryable SSH error; retry not attempted because "+ - "remaining deadline budget was insufficient (next retry delay: %s): %w", - attempt, delay, lastErr, - ) - } - ginkgo.GinkgoWriter.Printf( - "[retry] ssh %s: attempt %d failed with transient error, retrying after %s: %s\n", - workspace, attempt, delay, lastErr, - ) - if err := waitForRetry(ctx, delay); err != nil { - return lastOut, err - } +func execWithSSHRetry(ctx context.Context, workspace string, fn execFunc) (string, error) { + res := runWithRetry(ctx, retryPolicy{ + backoff: sshBackoff, + retryable: isRetryableSSHError, + label: "[retry] ssh " + workspace + ":", + kind: "retryable SSH error", + }, fn) + if !res.gaveUp || res.err == nil { + return res.stdout, res.err } - if lastErr != nil { - if lastStderr != "" { - return lastOut, fmt.Errorf( - "after %d attempts: %w (stderr: %s)", attempt, lastErr, lastStderr, - ) - } - return lastOut, fmt.Errorf("after %d attempts: %w", attempt, lastErr) + if res.stderr != "" { + return res.stdout, fmt.Errorf( + "after %d attempts: %w (stderr: %s)", res.attempts, res.err, res.stderr, + ) } - return lastOut, lastErr + return res.stdout, fmt.Errorf("after %d attempts: %w", res.attempts, res.err) } func retryFitsBudget(ctx context.Context, delay time.Duration) bool { diff --git a/e2e/framework/runenv/exec.go b/e2e/framework/runenv/exec.go index e2439f24ab..8ff84f9daa 100644 --- a/e2e/framework/runenv/exec.go +++ b/e2e/framework/runenv/exec.go @@ -52,6 +52,15 @@ func (ExecRunner) Run(ctx context.Context, c Command) (CommandResult, error) { err := cmd.Run() res := CommandResult{Stdout: stdout.String(), Stderr: stderr.String()} + return finishRun(ctx, c.Path, res, err) +} + +func finishRun( + ctx context.Context, + path string, + res CommandResult, + err error, +) (CommandResult, error) { if errors.Is(err, exec.ErrWaitDelay) { err = nil } @@ -71,7 +80,7 @@ func (ExecRunner) Run(ctx context.Context, c Command) (CommandResult, error) { return res, ctxErr } // Report the path only: argv may carry sensitive values. - return res, fmt.Errorf("run %s: %w", c.Path, err) + return res, fmt.Errorf("run %s: %w", path, err) } func teeWriter(capture io.Writer, live io.Writer) io.Writer { diff --git a/e2e/framework/server_utils.go b/e2e/framework/server_utils.go index 2dc09a4260..2657eb2a06 100644 --- a/e2e/framework/server_utils.go +++ b/e2e/framework/server_utils.go @@ -9,6 +9,10 @@ import ( "path/filepath" ) +const ( + value0000 = "0.0.0.0" +) + // ServeAgent will be a simple http file server that will expose our // freshly compiled devsy binaries to be downloaded as agents. // useful for non-linux runners. @@ -54,14 +58,14 @@ func getIP() string { // Get a list of network interfaces ifaces, err := net.Interfaces() if err != nil { - return "0.0.0.0" + return value0000 } // Iterate over each network interface for _, iface := range ifaces { addrs, err := iface.Addrs() if err != nil { - return "0.0.0.0" + return value0000 } for _, addr := range addrs { @@ -71,7 +75,7 @@ func getIP() string { } } - return "0.0.0.0" + return value0000 } func matchIPv4(addr net.Addr) string { diff --git a/e2e/framework/setuplogs.go b/e2e/framework/setuplogs.go new file mode 100644 index 0000000000..dba8d5e0cf --- /dev/null +++ b/e2e/framework/setuplogs.go @@ -0,0 +1,143 @@ +package framework + +import ( + "context" + "encoding/json" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/workspacelogs" +) + +// DevsySetupLogs runs `workspace logs --source setup` and returns stdout and stderr. +func (f *Framework) DevsySetupLogs(ctx context.Context, args ...string) (string, string, error) { + full := append([]string{cmdWorkspace, "logs", "--source", "setup"}, args...) + stdout, stderr, err := f.ExecCommandCapture(ctx, full) + if err != nil { + return stdout, stderr, fmt.Errorf("devsy setup logs failed: %w (stderr: %s)", err, stderr) + } + + return stdout, stderr, nil +} + +// DevsySetupLogsRead reads every page of a run as JSON and merges the records. +func (f *Framework) DevsySetupLogsRead( + ctx context.Context, + args ...string, +) (*workspacelogs.ReadResponse, error) { + var merged *workspacelogs.ReadResponse + after := "" + for { + pageArgs := append([]string{flagResultFormat, formatJSON}, args...) + if after != "" { + pageArgs = append(pageArgs, "--after", after) + } + stdout, _, err := f.DevsySetupLogs(ctx, pageArgs...) + if err != nil { + return nil, err + } + page := &workspacelogs.ReadResponse{} + if err := json.Unmarshal([]byte(stdout), page); err != nil { + return nil, fmt.Errorf("setup logs page is not valid JSON: %w", err) + } + if merged == nil { + merged = page + } else { + merged.Records = append(merged.Records, page.Records...) + merged.Complete, merged.HasMore = page.Complete, page.HasMore + merged.Availability = page.Availability + } + if !page.HasMore { + return merged, nil + } + after = page.NextCursor + } +} + +// DevsySetupRuns lists retained runs, newest first. Pass a workspace to scope it. +func (f *Framework) DevsySetupRuns( + ctx context.Context, + args ...string, +) (*workspacelogs.IndexResponse, error) { + listArgs := append([]string{"--list", flagResultFormat, formatJSON, "--limit", "100"}, args...) + stdout, _, err := f.DevsySetupLogs(ctx, listArgs...) + if err != nil { + return nil, err + } + resp := &workspacelogs.IndexResponse{} + if err := json.Unmarshal([]byte(stdout), resp); err != nil { + return nil, fmt.Errorf("setup run index is not valid JSON: %w", err) + } + + return resp, nil +} + +// SetupLogsText joins the output text of one stream. An empty stream joins all. +func SetupLogsText(resp *workspacelogs.ReadResponse, stream workspacelogs.Stream) string { + var sb strings.Builder + for i := range resp.Records { + rec := &resp.Records[i] + if rec.Kind == workspacelogs.KindOutput && (stream == "" || rec.Stream == stream) { + sb.WriteString(rec.Text) + } + } + + return sb.String() +} + +// SetupInvocations returns the last transition of each invocation, keyed by ID. +func SetupInvocations(resp *workspacelogs.ReadResponse) map[string]workspacelogs.Record { + final := map[string]workspacelogs.Record{} + for i := range resp.Records { + if rec := resp.Records[i]; rec.Kind == workspacelogs.KindInvocation { + final[rec.InvocationID] = rec + } + } + + return final +} + +// SetupInvocationsFor returns the final transitions of one hook. +func SetupInvocationsFor( + resp *workspacelogs.ReadResponse, + hook string, +) []workspacelogs.Record { + var out []workspacelogs.Record + for _, rec := range SetupInvocations(resp) { + if rec.Hook == hook { + out = append(out, rec) + } + } + + return out +} + +// SetupLogsDir returns the local directory that holds retained setup history. +func SetupLogsDir() (string, error) { + return config.DefaultPathManager().WorkspaceLogsDir() +} + +// FindInFiles returns the files under dir whose bytes contain needle. +func FindInFiles(dir, needle string) ([]string, error) { + var hits []string + err := filepath.WalkDir(dir, func(path string, d os.DirEntry, walkErr error) error { + if walkErr != nil || d.IsDir() { + return walkErr + } + // #nosec G304 G122 -- walks a test-owned directory. + data, readErr := os.ReadFile(path) + if readErr != nil { + return readErr + } + if strings.Contains(string(data), needle) { + hits = append(hits, path) + } + + return nil + }) + + return hits, err +} diff --git a/e2e/framework/util.go b/e2e/framework/util.go index b21147666b..3487d335da 100644 --- a/e2e/framework/util.go +++ b/e2e/framework/util.go @@ -86,7 +86,7 @@ func CleanupTempDir(initialDir, tempDir string) { err = os.RemoveAll(tempDir) if err != nil { - fmt.Println("WARN:", err) + _, _ = fmt.Fprintln(os.Stdout, "WARN:", err) } } @@ -165,6 +165,7 @@ func copyDir(src, dst string) error { } func copyFile(src, dst string) error { + //nolint:gosec // test helper opens paths the test itself created srcFile, err := os.Open(src) if err != nil { return err @@ -182,6 +183,7 @@ func copyFile(src, dst string) error { return err } + //nolint:gosec // test helper opens paths the test itself created dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode) if err != nil { return err diff --git a/e2e/tests/ci/ci.go b/e2e/tests/ci/ci.go index 71a80e328d..4a5f397702 100644 --- a/e2e/tests/ci/ci.go +++ b/e2e/tests/ci/ci.go @@ -17,6 +17,10 @@ import ( "github.com/onsi/gomega" ) +const ( + cat = "cat" +) + const ( ciCommand = "ci" secretCmd = "secret" @@ -74,6 +78,37 @@ var _ = ginkgo.Describe("devsy ci test suite", ginkgo.Label("ci"), ginkgo.Ordere expectWorkspaceGone(ctx, f, tempDir) }, ginkgo.SpecTimeout(framework.TimeoutShort())) + ginkgo.It("should keep setup history readable after teardown and keep the exit status", + func(ctx context.Context) { + tempDir, f := setupCIFrom(initialDir, "tests/ci/testdata/ci-setup-logs") + + _, _, err := f.ExecCommandCapture(ctx, []string{ + ciCommand, tempDir, "--", "sh", "-c", "exit 3", + }) + var exitErr *exec.ExitError + gomega.Expect(errors.As(err, &exitErr)).To(gomega.BeTrue(), + "expected an exec.ExitError, got %v", err) + gomega.Expect(exitErr.ExitCode()).To(gomega.Equal(3)) + expectWorkspaceGone(ctx, f, tempDir) + + runs, err := f.DevsySetupRuns(ctx, "--context", "default") + framework.ExpectNoError(err) + runID := "" + for _, run := range runs.Runs { + if run.WorkspaceIDAtStart == workspace.ToID(tempDir) { + runID = run.RunID + break + } + } + gomega.Expect(runID).ToNot(gomega.BeEmpty(), "the ci run should stay in the index") + + stdout, _, err := f.DevsySetupLogs(ctx, "--run", runID, "--context", "default", + resultFormatPlainFlag) + framework.ExpectNoError(err) + gomega.Expect(stdout).To(gomega.ContainSubstring("stdout | ci-out-marker")) + gomega.Expect(stdout).To(gomega.ContainSubstring("stderr | ci-err-marker")) + }, ginkgo.SpecTimeout(framework.TimeoutShort())) + ginkgo.DescribeTable( "should preserve child streams and control progress visibility", func(ctx context.Context, flags []string, wantStatus, wantDebug bool) { @@ -341,7 +376,7 @@ var _ = ginkgo.Describe("devsy ci test suite", ginkgo.Label("ci"), ginkgo.Ordere stdout, _, err := f.ExecCommandCapture(ctx, []string{ ciCommand, tempDir, "--workspace-env", "WS_CI_VAR=ws_value", - "--", "cat", "/etc/envfile.json", + "--", cat, "/etc/envfile.json", }) framework.ExpectNoError(err) gomega.Expect(stdout).To(gomega.ContainSubstring(`"WS_CI_VAR":"ws_value"`)) @@ -370,7 +405,7 @@ var _ = ginkgo.Describe("devsy ci test suite", ginkgo.Label("ci"), ginkgo.Ordere stdout, _, err := f.ExecCommandCapture(ctx, []string{ ciCommand, tempDir, "--secrets-file", secretsFile, - "--", "cat", "/tmp/ci-secret.txt", + "--", cat, "/tmp/ci-secret.txt", }) framework.ExpectNoError(err) gomega.Expect(strings.TrimSpace(stdout)).To(gomega.ContainSubstring("s3cr3t")) @@ -392,7 +427,7 @@ var _ = ginkgo.Describe("devsy ci test suite", ginkgo.Label("ci"), ginkgo.Ordere stdout, _, err := f.ExecCommandCapture(ctx, []string{ ciCommand, tempDir, "--secret", ciSecretName, - "--", "cat", "/tmp/ci-secret.txt", + "--", cat, "/tmp/ci-secret.txt", }) framework.ExpectNoError(err) gomega.Expect(strings.TrimSpace(stdout)).To(gomega.ContainSubstring("managed-ci-42")) diff --git a/e2e/tests/ci/testdata/ci-setup-logs/.devcontainer.json b/e2e/tests/ci/testdata/ci-setup-logs/.devcontainer.json new file mode 100644 index 0000000000..b2d1e4a229 --- /dev/null +++ b/e2e/tests/ci/testdata/ci-setup-logs/.devcontainer.json @@ -0,0 +1,5 @@ +{ + "image": "ghcr.io/devsy-org/test-images/go:1", + "waitFor": "postCreateCommand", + "postCreateCommand": "echo ci-out-marker; echo ci-err-marker >&2" +} diff --git a/e2e/tests/configread/configread.go b/e2e/tests/configread/configread.go index 8259dff18a..44924e23ec 100644 --- a/e2e/tests/configread/configread.go +++ b/e2e/tests/configread/configread.go @@ -12,6 +12,15 @@ import ( "github.com/onsi/gomega" ) +const ( + workspaceFolder = "--workspace-folder" + includeMergedConfiguration = "--include-merged-configuration" +) + +const ( + containerId = "--container-id" +) + const ( cmdConfig = "config" cmdRead = "read" @@ -32,7 +41,7 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, - "--workspace-folder", tempDir, + workspaceFolder, tempDir, }) framework.ExpectNoError(err) @@ -66,7 +75,7 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, - "--workspace-folder", tempDir, + workspaceFolder, tempDir, "--include-features-configuration", }) framework.ExpectNoError(err) @@ -86,8 +95,8 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, - "--workspace-folder", tempDir, - "--include-merged-configuration", + workspaceFolder, tempDir, + includeMergedConfiguration, }) framework.ExpectNoError(err) @@ -107,7 +116,7 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func _, _, err := f.ExecCommandCapture(ctx, []string{ cmdConfig, cmdRead, - "--workspace-folder", "/nonexistent/path/that/does/not/exist", + workspaceFolder, "/nonexistent/path/that/does/not/exist", }) framework.ExpectError(err) }, ginkgo.SpecTimeout(framework.TimeoutShort())) @@ -128,8 +137,8 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, - "--workspace-folder", tempDir, - "--include-merged-configuration", + workspaceFolder, tempDir, + includeMergedConfiguration, }) framework.ExpectNoError(err) @@ -162,8 +171,8 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, - "--workspace-folder", tempDir, - "--include-merged-configuration", + workspaceFolder, tempDir, + includeMergedConfiguration, }) framework.ExpectNoError(err) @@ -194,7 +203,7 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, - "--workspace-folder", tempDir, + workspaceFolder, tempDir, }) framework.ExpectNoError(err) @@ -213,8 +222,8 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, - "--workspace-folder", tempDir, - "--include-merged-configuration", + workspaceFolder, tempDir, + includeMergedConfiguration, }) framework.ExpectNoError(err) @@ -233,7 +242,7 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, - "--workspace-folder", tempDir, + workspaceFolder, tempDir, }) framework.ExpectNoError(err) @@ -262,7 +271,7 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, "--id-label", "devsy.readconfig.test=custom", - "--include-merged-configuration", + includeMergedConfiguration, }) framework.ExpectNoError(err) @@ -294,7 +303,7 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, - "--container-id", containerID, + containerId, containerID, }) framework.ExpectNoError(err) @@ -316,7 +325,7 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func _, _, err := f.ExecCommandCapture(ctx, []string{ cmdConfig, cmdRead, - "--container-id", "nonexistent-container-id-12345", + containerId, "nonexistent-container-id-12345", }) framework.ExpectError(err) }, ginkgo.SpecTimeout(framework.TimeoutShort())) @@ -343,7 +352,7 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func stdout, _, err := f.ExecCommandCapture(ctx, []string{ cmdConfig, cmdRead, - "--container-id", containerID, + containerId, containerID, }) if err == nil { var result map[string]any @@ -376,7 +385,7 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, - "--container-id", containerID, + containerId, containerID, "--docker-path", "docker", }) framework.ExpectNoError(err) @@ -394,7 +403,7 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func _, _, err := f.ExecCommandCapture(ctx, []string{ cmdConfig, cmdRead, - "--container-id", "any-container", + containerId, "any-container", "--docker-path", "/nonexistent/path/to/docker", }) framework.ExpectError(err) @@ -408,8 +417,8 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, - "--workspace-folder", tempDir, - "--include-merged-configuration", + workspaceFolder, tempDir, + includeMergedConfiguration, }) framework.ExpectNoError(err) diff --git a/e2e/tests/dockerbuildkit/docker_internal_buildkit.go b/e2e/tests/dockerbuildkit/docker_internal_buildkit.go index 2205a281c3..1691c0e429 100644 --- a/e2e/tests/dockerbuildkit/docker_internal_buildkit.go +++ b/e2e/tests/dockerbuildkit/docker_internal_buildkit.go @@ -197,11 +197,14 @@ var _ = ginkgo.Describe( }) var logs bytes.Buffer - err = buildkit.Build(ctx, buildKitClient, &logs, "", &build.BuildOptions{ - Dockerfile: filepath.Join(contextDir, "Dockerfile"), - Context: contextDir, - Images: []string{tag}, - Load: true, + err = buildkit.Build(ctx, buildKitClient, buildkit.BuildRequest{ + Writer: &logs, + Options: &build.BuildOptions{ + Dockerfile: filepath.Join(contextDir, "Dockerfile"), + Context: contextDir, + Images: []string{tag}, + Load: true, + }, }) framework.ExpectNoError(err, "internal BuildKit build failed:\n%s", logs.String()) diff --git a/e2e/tests/exec/exec.go b/e2e/tests/exec/exec.go index 01ab4c19d7..a599f0ebf5 100644 --- a/e2e/tests/exec/exec.go +++ b/e2e/tests/exec/exec.go @@ -14,6 +14,10 @@ import ( "github.com/onsi/gomega" ) +const ( + hello = "hello" +) + const ( cmdWorkspace = "workspace" execCommand = "exec" @@ -42,10 +46,10 @@ var _ = ginkgo.Describe("devsy exec test suite", ginkgo.Label("exec"), ginkgo.Or stdout, _, err := f.ExecCommandCapture(ctx, []string{ cmdWorkspace, execCommand, workspaceFolderFlag, tempDir, - "--", echoCommand, "-n", "hello", + "--", echoCommand, "-n", hello, }) framework.ExpectNoError(err) - gomega.Expect(stdout).To(gomega.Equal("hello")) + gomega.Expect(stdout).To(gomega.Equal(hello)) }, ginkgo.SpecTimeout(framework.TimeoutShort())) ginkgo.It("should pass remote-env to the container", @@ -180,10 +184,10 @@ var _ = ginkgo.Describe("devsy exec test suite", ginkgo.Label("exec"), ginkgo.Or stdout, _, err := f.ExecCommandCapture(ctx, []string{ cmdWorkspace, execCommand, wsName, - "--", echoCommand, "-n", "hello", + "--", echoCommand, "-n", hello, }) framework.ExpectNoError(err) - gomega.Expect(stdout).To(gomega.Equal("hello")) + gomega.Expect(stdout).To(gomega.Equal(hello)) }, ginkgo.SpecTimeout(framework.TimeoutShort())) ginkgo.It("should error when both name and --workspace-folder are given", @@ -201,7 +205,7 @@ var _ = ginkgo.Describe("devsy exec test suite", ginkgo.Label("exec"), ginkgo.Or cmdWorkspace, execCommand, wsName, workspaceFolderFlag, tempDir, - "--", echoCommand, "-n", "hello", + "--", echoCommand, "-n", hello, }) framework.ExpectError(err) gomega.Expect(stderr).To(gomega.ContainSubstring( @@ -244,10 +248,10 @@ var _ = ginkgo.Describe("devsy exec test suite", ginkgo.Label("exec"), ginkgo.Or cmdWorkspace, execCommand, "--result-format", "json", workspaceFolderFlag, tempDir, - "--", echoCommand, "-n", "hello", + "--", echoCommand, "-n", hello, }) framework.ExpectNoError(err) - gomega.Expect(stdout).To(gomega.Equal("hello")) + gomega.Expect(stdout).To(gomega.Equal(hello)) lines := strings.Split(strings.TrimSpace(stderr), "\n") gomega.Expect(lines).NotTo(gomega.BeEmpty()) @@ -271,10 +275,10 @@ var _ = ginkgo.Describe("devsy exec test suite", ginkgo.Label("exec"), ginkgo.Or cmdWorkspace, execCommand, "--result-format", "plain", workspaceFolderFlag, tempDir, - "--", echoCommand, "-n", "hello", + "--", echoCommand, "-n", hello, }) framework.ExpectNoError(err) - gomega.Expect(stdout).To(gomega.Equal("hello")) + gomega.Expect(stdout).To(gomega.Equal(hello)) for line := range strings.SplitSeq(stderr, "\n") { var envelope config.ResultEnvelope diff --git a/e2e/tests/feature/features.go b/e2e/tests/feature/features.go index ace374288a..0916ad2507 100644 --- a/e2e/tests/feature/features.go +++ b/e2e/tests/feature/features.go @@ -21,6 +21,15 @@ import ( "github.com/onsi/gomega" ) +const ( + orgOpencontainersImageVersion = "org.opencontainers.image.version" +) + +const ( + orgOpencontainersImageTitle = "org.opencontainers.image.title" + value100 = "1.0.0" +) + const ( subCmdManifest = "manifest" subCmdTags = "tags" @@ -229,9 +238,9 @@ var _ = ginkgo.Describe("feature commands", ginkgo.Label("feature"), func() { featureRef := regHost + "/test/features/go:1.0.0" pushFeatureWithAnnotations(featureRef, map[string]string{ - "org.opencontainers.image.title": "Go", + orgOpencontainersImageTitle: "Go", "org.opencontainers.image.description": "Install Go toolchain", - "org.opencontainers.image.version": "1.0.0", + orgOpencontainersImageVersion: value100, "org.opencontainers.image.source": "https://github.com/test/features", }) @@ -242,7 +251,7 @@ var _ = ginkgo.Describe("feature commands", ginkgo.Label("feature"), func() { framework.ExpectNoError(err) gomega.Expect(stdout).To(gomega.ContainSubstring("Go")) - gomega.Expect(stdout).To(gomega.ContainSubstring("1.0.0")) + gomega.Expect(stdout).To(gomega.ContainSubstring(value100)) }, ginkgo.SpecTimeout(framework.TimeoutShort())) ginkgo.It("outputs JSON when --output=json is specified", func(ctx context.Context) { @@ -255,8 +264,8 @@ var _ = ginkgo.Describe("feature commands", ginkgo.Label("feature"), func() { featureRef := regHost + "/test/features/node:1.0.0" pushFeatureWithAnnotations(featureRef, map[string]string{ - "org.opencontainers.image.title": "Node.js", - "org.opencontainers.image.version": "1.0.0", + orgOpencontainersImageTitle: "Node.js", + orgOpencontainersImageVersion: value100, }) stdout, _, err := f.ExecCommandCapture(ctx, []string{ @@ -267,7 +276,7 @@ var _ = ginkgo.Describe("feature commands", ginkgo.Label("feature"), func() { var result map[string]any gomega.Expect(json.Unmarshal([]byte(stdout), &result)).To(gomega.Succeed()) gomega.Expect(result["id"]).To(gomega.Equal("node")) - gomega.Expect(result["version"]).To(gomega.Equal("1.0.0")) + gomega.Expect(result["version"]).To(gomega.Equal(value100)) }, ginkgo.SpecTimeout(framework.TimeoutShort())) ginkgo.It("lists tags with --show-tags", func(ctx context.Context) { @@ -287,7 +296,7 @@ var _ = ginkgo.Describe("feature commands", ginkgo.Label("feature"), func() { }) framework.ExpectNoError(err) - gomega.Expect(stdout).To(gomega.ContainSubstring("1.0.0")) + gomega.Expect(stdout).To(gomega.ContainSubstring(value100)) gomega.Expect(stdout).To(gomega.ContainSubstring("2.0.0")) }, ginkgo.SpecTimeout(framework.TimeoutShort())) @@ -302,7 +311,7 @@ var _ = ginkgo.Describe("feature commands", ginkgo.Label("feature"), func() { featureRef := regHost + "/test/features/go:1.0.0" pushFeatureWithAnnotations(featureRef, map[string]string{ - "org.opencontainers.image.title": "Go", + orgOpencontainersImageTitle: "Go", }) stdout, _, err := f.ExecCommandCapture(ctx, []string{ @@ -350,8 +359,8 @@ var _ = ginkgo.Describe("feature commands", ginkgo.Label("feature"), func() { featureRef := regHost + "/test/features/go:1.0.0" pushFeatureWithAnnotations(featureRef, map[string]string{ - "org.opencontainers.image.title": "Go", - "org.opencontainers.image.version": "1.0.0", + orgOpencontainersImageTitle: "Go", + orgOpencontainersImageVersion: value100, }) stdout, _, err := f.ExecCommandCapture(ctx, []string{ @@ -365,7 +374,7 @@ var _ = ginkgo.Describe("feature commands", ginkgo.Label("feature"), func() { annotations, ok := manifest["annotations"].(map[string]any) gomega.Expect(ok).To(gomega.BeTrue()) gomega.Expect(annotations).To(gomega.HaveKeyWithValue( - "org.opencontainers.image.title", "Go", + orgOpencontainersImageTitle, "Go", )) }, ginkgo.SpecTimeout(framework.TimeoutShort())) @@ -417,7 +426,7 @@ var _ = ginkgo.Describe("feature commands", ginkgo.Label("feature"), func() { framework.ExpectNoError(err) gomega.Expect(stdout).To(gomega.ContainSubstring("Available Tags:")) - gomega.Expect(stdout).To(gomega.ContainSubstring("1.0.0")) + gomega.Expect(stdout).To(gomega.ContainSubstring(value100)) gomega.Expect(stdout).To(gomega.ContainSubstring("2.0.0")) gomega.Expect(stdout).To(gomega.ContainSubstring("latest")) }, ginkgo.SpecTimeout(framework.TimeoutShort())) @@ -445,7 +454,7 @@ var _ = ginkgo.Describe("feature commands", ginkgo.Label("feature"), func() { gomega.Expect(result).To(gomega.HaveKey("tags")) tags, ok := result["tags"].([]any) gomega.Expect(ok).To(gomega.BeTrue()) - gomega.Expect(tags).To(gomega.ContainElement("1.0.0")) + gomega.Expect(tags).To(gomega.ContainElement(value100)) gomega.Expect(tags).To(gomega.ContainElement("2.0.0")) }, ginkgo.SpecTimeout(framework.TimeoutShort())) @@ -480,7 +489,7 @@ var _ = ginkgo.Describe("feature commands", ginkgo.Label("feature"), func() { func pushFeatureWithAnnotations(refStr string, annotations map[string]string) { featureJSON := `{"id":"` + featureIDFromRef(refStr) + `","name":"` + - annotationOrDefault(annotations, "org.opencontainers.image.title", "Test Feature") + + annotationOrDefault(annotations, orgOpencontainersImageTitle, "Test Feature") + `","version":"1.0.0","description":"` + annotationOrDefault( annotations, diff --git a/e2e/tests/ide/browser_returns.go b/e2e/tests/ide/browser_returns.go index 9647175d88..5c7cb0c804 100644 --- a/e2e/tests/ide/browser_returns.go +++ b/e2e/tests/ide/browser_returns.go @@ -389,18 +389,12 @@ var _ = ginkgo.Describe( err = f.DevsyContextUse(ctx, contextName) framework.ExpectNoError(err) - err = f.ExecCommand( - ctx, - false, - true, - "", - []string{ - "context", - "set", - names.Flag(names.Option), - config.ContextOptionGPGAgentForwarding + "=" + config.BoolTrue, - }, - ) + err = f.ExecCommandStdout(ctx, []string{ + "context", + "set", + names.Flag(names.Option), + config.ContextOptionGPGAgentForwarding + "=" + config.BoolTrue, + }) framework.ExpectNoError(err) err = f.DevsyProviderAdd(ctx, "docker") diff --git a/e2e/tests/logs/setup_logs.go b/e2e/tests/logs/setup_logs.go new file mode 100644 index 0000000000..925af9d105 --- /dev/null +++ b/e2e/tests/logs/setup_logs.go @@ -0,0 +1,351 @@ +package logs + +import ( + "context" + "encoding/json" + "os" + "strings" + "time" + + "github.com/devsy-org/devsy/e2e/framework" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" +) + +const ( + hookPostCreate = "postCreateCommand" + hookPostStart = "postStartCommand" + stateSucceeded = workspacelogs.InvocationSucceeded + stateFailed = workspacelogs.InvocationFailed + pollTimeout = 2 * time.Minute + pollInterval = 2 * time.Second + defaultContext = "default" + testdataBasic = "tests/logs/testdata/setup-basic" + testdataSecret = "tests/logs/testdata/setup-secret" + // #nosec G101 -- test value, not a credential. + secretValue = "setup-logs-secret-93817" + resultFormatFlag = "--result-format" +) + +// eventually polls check until it passes, bounded by pollTimeout. +func eventually(ctx context.Context, check func(g gomega.Gomega)) { + gomega.Eventually(check). + WithContext(ctx). + WithTimeout(pollTimeout). + WithPolling(pollInterval). + Should(gomega.Succeed()) +} + +// latestRunID returns the newest retained run of the workspace. +func latestRunID(ctx context.Context, f *framework.Framework, ws string) string { + runs, err := f.DevsySetupRuns(ctx, ws) + framework.ExpectNoError(err) + gomega.Expect(runs.Runs).ToNot(gomega.BeEmpty(), "workspace should have a retained run") + + return runs.Runs[0].RunID +} + +// onlyInvocation returns the single final invocation record of a hook. +func onlyInvocation( + g gomega.Gomega, + resp *workspacelogs.ReadResponse, + hook string, +) workspacelogs.Record { + // A deferred hook has a planning record plus a separate executed record. + // Only the executed one carries the outcome. + var executed []workspacelogs.Record + for _, rec := range framework.SetupInvocationsFor(resp, hook) { + if rec.State != workspacelogs.InvocationDeferred { + executed = append(executed, rec) + } + } + g.Expect(executed).To(gomega.HaveLen(1), "expected one executed %s invocation", hook) + + return executed[0] +} + +var _ = ginkgo.Describe( + "devsy setup logs test suite", + ginkgo.Label("logs", "setup-logs"), + func() { + var initialDir string + + ginkgo.BeforeEach(func() { + var err error + initialDir, err = os.Getwd() + framework.ExpectNoError(err) + }) + + ginkgo.It("retains stdout and stderr of a foreground create without debug", + ginkgo.SpecTimeout(framework.TimeoutShort()), + func(ctx context.Context) { + ws, f, err := framework.SetupDockerWorkspace(testdataBasic, initialDir) + framework.ExpectNoError(err) + framework.ExpectNoError(f.DevsyUp(ctx, ws)) + + stdout, _, err := f.DevsySetupLogs(ctx, ws, resultFormatFlag+"=plain") + framework.ExpectNoError(err) + gomega.Expect(stdout).To(gomega.ContainSubstring("stdout | out-marker-basic")) + gomega.Expect(stdout).To(gomega.ContainSubstring("stderr | err-marker-basic")) + + resp, err := f.DevsySetupLogsRead(ctx, ws) + framework.ExpectNoError(err) + gomega.Expect(framework.SetupLogsText(resp, workspacelogs.StreamStdout)). + To(gomega.ContainSubstring("out-marker-basic")) + gomega.Expect(framework.SetupLogsText(resp, workspacelogs.StreamStderr)). + To(gomega.ContainSubstring("err-marker-basic")) + gomega.Expect(framework.SetupLogsText(resp, workspacelogs.StreamStdout)). + ToNot(gomega.ContainSubstring("err-marker-basic")) + }) + + ginkgo.It("shows output before and after a delayed default waitFor hook", + ginkgo.SpecTimeout(framework.TimeoutShort()), + func(ctx context.Context) { + ws, f, err := framework.SetupDockerWorkspace( + "tests/logs/testdata/setup-waitfor", initialDir, + ) + framework.ExpectNoError(err) + framework.ExpectNoError(f.DevsyUp(ctx, ws)) + + eventually(ctx, func(g gomega.Gomega) { + resp, readErr := f.DevsySetupLogsRead(ctx, ws) + g.Expect(readErr).ToNot(gomega.HaveOccurred()) + g.Expect(framework.SetupLogsText(resp, "")). + To(gomega.ContainSubstring("part-before")) + g.Expect(framework.SetupLogsText(resp, "")). + ToNot(gomega.ContainSubstring("part-after")) + }) + + // The hook waits for this file, so no test sleep is needed. + _, err = f.DevsySSH(ctx, ws, "touch /tmp/release-hook") + framework.ExpectNoError(err) + + eventually(ctx, func(g gomega.Gomega) { + resp, readErr := f.DevsySetupLogsRead(ctx, ws) + g.Expect(readErr).ToNot(gomega.HaveOccurred()) + text := framework.SetupLogsText(resp, "") + g.Expect(text).To(gomega.ContainSubstring("part-before")) + g.Expect(text).To(gomega.ContainSubstring("part-after")) + g.Expect(onlyInvocation(g, resp, hookPostCreate).State). + To(gomega.Equal(stateSucceeded)) + }) + }) + + ginkgo.It("keeps early lines and the failure of a deferred hook", + ginkgo.SpecTimeout(framework.TimeoutShort()), + func(ctx context.Context) { + ws, f, err := framework.SetupDockerWorkspace( + "tests/logs/testdata/setup-deferred-fail", initialDir, + ) + framework.ExpectNoError(err) + // A deferred hook failure does not fail the foreground up. + framework.ExpectNoError(f.DevsyUp(ctx, ws)) + + eventually(ctx, func(g gomega.Gomega) { + resp, readErr := f.DevsySetupLogsRead(ctx, ws, "--hook", hookPostCreate) + g.Expect(readErr).ToNot(gomega.HaveOccurred()) + inv := onlyInvocation(g, resp, hookPostCreate) + g.Expect(inv.State).To(gomega.Equal(stateFailed)) + g.Expect(inv.ExitCode).ToNot(gomega.BeNil()) + g.Expect(*inv.ExitCode).To(gomega.Equal(1)) + }) + + stdout, _, err := f.DevsySetupLogs(ctx, ws, "--hook", hookPostCreate, + resultFormatFlag+"=plain") + framework.ExpectNoError(err) + // Line 1 is outside the last 200 lines the old failure tail kept. + gomega.Expect(stdout).To(gomega.ContainSubstring("stdout | early-line-1\n")) + gomega.Expect(stdout).To(gomega.ContainSubstring("stdout | early-line-300\n")) + gomega.Expect(stdout).To(gomega.ContainSubstring("failed")) + }) + + ginkgo.It("records a marker skip on start and never reruns hooks on read", + ginkgo.SpecTimeout(framework.TimeoutLong()), + func(ctx context.Context) { + ws, f, err := framework.SetupDockerWorkspace( + "tests/logs/testdata/setup-start-twice", initialDir, + ) + framework.ExpectNoError(err) + framework.ExpectNoError(f.DevsyUp(ctx, ws)) + createRun := latestRunID(ctx, f, ws) + + framework.ExpectNoError(f.DevsyStop(ctx, ws)) + framework.ExpectNoError(f.DevsyUp(ctx, ws)) + startRun := latestRunID(ctx, f, ws) + gomega.Expect(startRun).ToNot(gomega.Equal(createRun)) + + created, err := f.DevsySetupLogsRead(ctx, ws, "--run", createRun) + framework.ExpectNoError(err) + gomega.Expect(framework.SetupLogsText(created, workspacelogs.StreamStdout)). + To(gomega.ContainSubstring("create-output-marker")) + + started, err := f.DevsySetupLogsRead(ctx, ws, "--run", startRun) + framework.ExpectNoError(err) + skipped := onlyInvocation(gomega.Default, started, hookPostCreate) + gomega.Expect(skipped.State).To(gomega.Equal(workspacelogs.InvocationSkipped)) + gomega.Expect(skipped.Reason).To(gomega.Equal("already_attempted")) + gomega.Expect(framework.SetupLogsText(started, workspacelogs.StreamStdout)). + To(gomega.ContainSubstring("start-output-marker")) + gomega.Expect(onlyInvocation(gomega.Default, started, hookPostStart).State). + To(gomega.Equal(stateSucceeded)) + + // Repeated reads must not execute the hook again. + for range 2 { + _, _, err = f.DevsySetupLogs(ctx, ws, "--run", createRun) + framework.ExpectNoError(err) + } + count, err := f.DevsySSH(ctx, ws, "wc -l < /tmp/create-count") + framework.ExpectNoError(err) + gomega.Expect(strings.TrimSpace(count)).To(gomega.Equal("1")) + }) + + ginkgo.It("keeps named parallel hooks apart", + ginkgo.SpecTimeout(framework.TimeoutShort()), + func(ctx context.Context) { + ws, f, err := framework.SetupDockerWorkspace( + "tests/logs/testdata/setup-parallel", initialDir, + ) + framework.ExpectNoError(err) + framework.ExpectNoError(f.DevsyUp(ctx, ws)) + + var alpha, beta workspacelogs.Record + eventually(ctx, func(g gomega.Gomega) { + resp, readErr := f.DevsySetupLogsRead(ctx, ws, "--hook", hookPostCreate) + g.Expect(readErr).ToNot(gomega.HaveOccurred()) + byKey := map[string]workspacelogs.Record{} + for _, inv := range framework.SetupInvocationsFor(resp, hookPostCreate) { + byKey[inv.HookKey] = inv + } + g.Expect(byKey).To(gomega.HaveKey("alpha")) + g.Expect(byKey).To(gomega.HaveKey("beta")) + alpha, beta = byKey["alpha"], byKey["beta"] + g.Expect(alpha.State).To(gomega.Equal(stateSucceeded)) + g.Expect(beta.State).To(gomega.Equal(stateFailed)) + }) + gomega.Expect(*beta.ExitCode).To(gomega.Equal(4)) + + one, err := f.DevsySetupLogsRead(ctx, ws, "--invocation", alpha.InvocationID) + framework.ExpectNoError(err) + gomega.Expect(framework.SetupLogsText(one, "")). + To(gomega.ContainSubstring("alpha-output")) + gomega.Expect(framework.SetupLogsText(one, "")). + ToNot(gomega.ContainSubstring("beta-output")) + + two, err := f.DevsySetupLogsRead(ctx, ws, "--invocation", beta.InvocationID) + framework.ExpectNoError(err) + gomega.Expect(framework.SetupLogsText(two, workspacelogs.StreamStderr)). + To(gomega.ContainSubstring("beta-output")) + gomega.Expect(framework.SetupLogsText(two, "")). + ToNot(gomega.ContainSubstring("alpha-output")) + }) + + ginkgo.It("never exposes a known secret in output or stored files", + ginkgo.SpecTimeout(framework.TimeoutShort()), + func(ctx context.Context) { + ws, f, err := framework.SetupDockerWorkspace(testdataSecret, initialDir) + framework.ExpectNoError(err) + + secretsDir, err := framework.CreateTempDir() + framework.ExpectNoError(err) + ginkgo.DeferCleanup(func() { _ = os.RemoveAll(secretsDir) }) + secretsFile := secretsDir + "/secrets.json" + payload, err := json.Marshal(map[string]string{"MY_SECRET": secretValue}) + framework.ExpectNoError(err) + framework.ExpectNoError(os.WriteFile(secretsFile, payload, 0o600)) + + framework.ExpectNoError(f.DevsyUp(ctx, ws, "--secrets-file", secretsFile)) + + plain, plainErr, err := f.DevsySetupLogs(ctx, ws, resultFormatFlag+"=plain") + framework.ExpectNoError(err) + gomega.Expect(plain).To(gomega.ContainSubstring("secret-line="), + "the hook output should be retained, with the value redacted") + jsonOut, _, err := f.DevsySetupLogs(ctx, ws, resultFormatFlag+"=json") + framework.ExpectNoError(err) + for _, out := range []string{plain, plainErr, jsonOut} { + gomega.Expect(out).ToNot(gomega.ContainSubstring(secretValue)) + } + + dir, err := framework.SetupLogsDir() + framework.ExpectNoError(err) + hits, err := framework.FindInFiles(dir, secretValue) + framework.ExpectNoError(err) + gomega.Expect(hits).To(gomega.BeEmpty(), "stored files must not hold the secret") + }) + + ginkgo.It("reads a run after the workspace is deleted", + ginkgo.SpecTimeout(framework.TimeoutShort()), + func(ctx context.Context) { + ws, f, err := framework.SetupDockerWorkspace(testdataBasic, initialDir) + framework.ExpectNoError(err) + framework.ExpectNoError(f.DevsyUp(ctx, ws)) + runID := latestRunID(ctx, f, ws) + + framework.ExpectNoError(f.DevsyWorkspaceDelete(ctx, ws)) + + stdout, _, err := f.DevsySetupLogs(ctx, "--run", runID, + "--context", defaultContext, resultFormatFlag+"=plain") + framework.ExpectNoError(err) + gomega.Expect(stdout).To(gomega.ContainSubstring("out-marker-basic")) + + runs, err := f.DevsySetupRuns(ctx, "--context", defaultContext) + framework.ExpectNoError(err) + var ids []string + for _, run := range runs.Runs { + ids = append(ids, run.RunID) + } + gomega.Expect(ids).To(gomega.ContainElement(runID)) + }) + + ginkgo.It("reads cached history of a stopped workspace without starting it", + ginkgo.SpecTimeout(framework.TimeoutShort()), + func(ctx context.Context) { + ws, f, err := framework.SetupDockerWorkspace(testdataBasic, initialDir) + framework.ExpectNoError(err) + framework.ExpectNoError(f.DevsyUp(ctx, ws)) + framework.ExpectNoError(f.DevsyWorkspaceStop(ctx, ws)) + expectStopped(ctx, f, ws) + + stdout, _, err := f.DevsySetupLogs(ctx, ws, resultFormatFlag+"=plain") + framework.ExpectNoError(err) + gomega.Expect(stdout).To(gomega.ContainSubstring("out-marker-basic")) + expectStopped(ctx, f, ws) + }) + + ginkgo.It("returns hook output shaped like protocol envelopes as plain data", + ginkgo.SpecTimeout(framework.TimeoutShort()), + func(ctx context.Context) { + ws, f, err := framework.SetupDockerWorkspace(testdataBasic, initialDir) + framework.ExpectNoError(err) + + stdout, _, err := f.ExecCommandCapture(ctx, []string{ + "workspace", "up", ws, "--ide", "none", resultFormatFlag + "=json", + }) + framework.ExpectNoError(err) + // Status lines precede the result; every line must stay valid JSON. + lines := strings.Split(strings.TrimSpace(stdout), "\n") + for _, line := range lines { + gomega.Expect(json.Valid([]byte(line))).To(gomega.BeTrue(), line) + } + result := map[string]any{} + framework.ExpectNoError(json.Unmarshal([]byte(lines[len(lines)-1]), &result)) + gomega.Expect(result["kind"]).To(gomega.Equal("result")) + runID, _ := result["runId"].(string) + gomega.Expect(runID).To(gomega.Equal(latestRunID(ctx, f, ws))) + + resp, err := f.DevsySetupLogsRead(ctx, ws, "--run", runID) + framework.ExpectNoError(err) + text := framework.SetupLogsText(resp, workspacelogs.StreamStdout) + gomega.Expect(text).To(gomega.ContainSubstring(`{"kind":"status"`)) + gomega.Expect(text).To(gomega.ContainSubstring(`{"kind":"result"`)) + gomega.Expect(onlyInvocation(gomega.Default, resp, hookPostCreate).State). + To(gomega.Equal(stateSucceeded)) + }) + }, +) + +func expectStopped(ctx context.Context, f *framework.Framework, ws string) { + status, err := f.DevsyStatus(ctx, ws) + framework.ExpectNoError(err) + framework.ExpectEqual(strings.ToUpper(status.State), "STOPPED") +} diff --git a/e2e/tests/logs/testdata/setup-basic/.devcontainer.json b/e2e/tests/logs/testdata/setup-basic/.devcontainer.json new file mode 100644 index 0000000000..6bfa698c61 --- /dev/null +++ b/e2e/tests/logs/testdata/setup-basic/.devcontainer.json @@ -0,0 +1,5 @@ +{ + "image": "ghcr.io/devsy-org/test-images/go:1", + "waitFor": "postCreateCommand", + "postCreateCommand": "echo out-marker-basic; echo err-marker-basic >&2; echo '{\"kind\":\"status\",\"schemaVersion\":1,\"id\":\"op-99\"}'; echo '{\"kind\":\"result\",\"runId\":\"not-a-run\"}'" +} diff --git a/e2e/tests/logs/testdata/setup-deferred-fail/.devcontainer.json b/e2e/tests/logs/testdata/setup-deferred-fail/.devcontainer.json new file mode 100644 index 0000000000..3f771b9f4c --- /dev/null +++ b/e2e/tests/logs/testdata/setup-deferred-fail/.devcontainer.json @@ -0,0 +1,4 @@ +{ + "image": "ghcr.io/devsy-org/test-images/go:1", + "postCreateCommand": "i=1; while [ $i -le 300 ]; do echo early-line-$i; i=$((i+1)); done; exit 1" +} diff --git a/e2e/tests/logs/testdata/setup-parallel/.devcontainer.json b/e2e/tests/logs/testdata/setup-parallel/.devcontainer.json new file mode 100644 index 0000000000..2f57dff059 --- /dev/null +++ b/e2e/tests/logs/testdata/setup-parallel/.devcontainer.json @@ -0,0 +1,7 @@ +{ + "image": "ghcr.io/devsy-org/test-images/go:1", + "postCreateCommand": { + "alpha": "echo alpha-output", + "beta": "echo beta-output >&2; exit 4" + } +} diff --git a/e2e/tests/logs/testdata/setup-secret/.devcontainer.json b/e2e/tests/logs/testdata/setup-secret/.devcontainer.json new file mode 100644 index 0000000000..6fc2cca49e --- /dev/null +++ b/e2e/tests/logs/testdata/setup-secret/.devcontainer.json @@ -0,0 +1,5 @@ +{ + "image": "ghcr.io/devsy-org/test-images/go:1", + "waitFor": "postCreateCommand", + "postCreateCommand": "echo secret-line=$MY_SECRET; echo secret-err=$MY_SECRET >&2" +} diff --git a/e2e/tests/logs/testdata/setup-start-twice/.devcontainer.json b/e2e/tests/logs/testdata/setup-start-twice/.devcontainer.json new file mode 100644 index 0000000000..d36340728f --- /dev/null +++ b/e2e/tests/logs/testdata/setup-start-twice/.devcontainer.json @@ -0,0 +1,6 @@ +{ + "image": "ghcr.io/devsy-org/test-images/go:1", + "waitFor": "postStartCommand", + "postCreateCommand": "echo create-output-marker; echo run >> /tmp/create-count", + "postStartCommand": "echo start-output-marker" +} diff --git a/e2e/tests/logs/testdata/setup-waitfor/.devcontainer.json b/e2e/tests/logs/testdata/setup-waitfor/.devcontainer.json new file mode 100644 index 0000000000..b47d051f15 --- /dev/null +++ b/e2e/tests/logs/testdata/setup-waitfor/.devcontainer.json @@ -0,0 +1,4 @@ +{ + "image": "ghcr.io/devsy-org/test-images/go:1", + "postCreateCommand": "echo part-before; while [ ! -f /tmp/release-hook ]; do sleep 0.2; done; echo part-after" +} diff --git a/e2e/tests/ssh/detached_validation.go b/e2e/tests/ssh/detached_validation.go index 1a4d7db15d..8a58d628b0 100644 --- a/e2e/tests/ssh/detached_validation.go +++ b/e2e/tests/ssh/detached_validation.go @@ -246,26 +246,28 @@ func (cli detachedValidationCLI) launchWorker( return worker, nil }) if err != nil { - store, storeErr := task.NewStoreAt(filepath.Join(cli.home, "state", "tasks")) - if storeErr == nil { - state, stateErr := store.Get(tk.ID()) - if stateErr == nil { - reportDiagnosticState(state) - } - } - streamsPath, pathErr := config.DefaultPathManager(). - ProcessStreamsFile(task.WorkerProcessName(tk.ID())) - if pathErr == nil { - streams, readErr := readDiagnosticStreams(cli.home, streamsPath) - if readErr == nil { - ginkgo.GinkgoWriter.Printf("worker streams: %s\n", streams) - } - } + reportWorkerDiagnostics(cli.home, tk) return ref, errors.Join(err, tk.FailLaunch(err)) } return ref, nil } +func reportWorkerDiagnostics(home string, tk *task.Task) { + if store, err := task.NewStoreAt(filepath.Join(home, "state", "tasks")); err == nil { + if state, err := store.Get(tk.ID()); err == nil { + reportDiagnosticState(state) + } + } + streamsPath, err := config.DefaultPathManager(). + ProcessStreamsFile(task.WorkerProcessName(tk.ID())) + if err != nil { + return + } + if streams, err := readDiagnosticStreams(home, streamsPath); err == nil { + ginkgo.GinkgoWriter.Printf("worker streams: %s\n", streams) + } +} + func readDiagnosticStreams(home, path string) ([]byte, error) { root, err := os.OpenRoot(home) if err != nil { diff --git a/e2e/tests/ssh/gh_credentials.go b/e2e/tests/ssh/gh_credentials.go index 0f5f04c157..0af573951c 100644 --- a/e2e/tests/ssh/gh_credentials.go +++ b/e2e/tests/ssh/gh_credentials.go @@ -327,8 +327,11 @@ func startFixtureRelay( joined := make(chan struct{}) go func() { defer close(joined) - done <- tunnelserver.RunServicesServer(tunnelCtx, reader, writer, false, false, nil, nil, - tunnelserver.WithAllowGitHubCredentials(true)) + done <- tunnelserver.RunServicesServer(tunnelCtx, tunnelserver.ServicesSpec{ + Reader: reader, + Writer: writer, + Options: []tunnelserver.Option{tunnelserver.WithAllowGitHubCredentials(true)}, + }) }() var once sync.Once diff --git a/e2e/tests/up-docker-compose/build.go b/e2e/tests/up-docker-compose/build.go index 4da90dcebc..746cc5d048 100644 --- a/e2e/tests/up-docker-compose/build.go +++ b/e2e/tests/up-docker-compose/build.go @@ -19,6 +19,10 @@ import ( "github.com/onsi/gomega" ) +const ( + dockerValue = "docker" +) + var _ = ginkgo.Describe( "devsy up docker compose test suite", ginkgo.Label("up-docker-compose"), @@ -33,11 +37,11 @@ var _ = ginkgo.Describe( initialDir, err = os.Getwd() framework.ExpectNoError(err) - dockerHelper = &docker.DockerHelper{DockerCommand: "docker"} + dockerHelper = &docker.DockerHelper{DockerCommand: dockerValue} composeHelper, err = compose.NewComposeHelper(dockerHelper) framework.ExpectNoError(err) - f, err = framework.SetupDockerProvider(initialDir+"/bin", "docker") + f, err = framework.SetupDockerProvider(initialDir+"/bin", dockerValue) framework.ExpectNoError(err) }) @@ -99,10 +103,12 @@ var _ = ginkgo.Describe( err = os.Remove(origPath) framework.ExpectNoError(err) + //nolint:gosec // test fixture path inside the temp directory failingConfig, err := os.Open(filepath.Join(tempDir, "fail.devcontainer.json")) framework.ExpectNoError(err) defer func() { _ = failingConfig.Close() }() + //nolint:gosec // test fixture path inside the temp directory newConfig, err := os.Create(origPath) framework.ExpectNoError(err) defer func() { _ = newConfig.Close() }() diff --git a/e2e/tests/up-docker-compose/config.go b/e2e/tests/up-docker-compose/config.go index f4d0f09fa3..b167b09610 100644 --- a/e2e/tests/up-docker-compose/config.go +++ b/e2e/tests/up-docker-compose/config.go @@ -37,11 +37,11 @@ var _ = ginkgo.Describe( tc.initialDir, err = os.Getwd() framework.ExpectNoError(err) - tc.dockerHelper = &docker.DockerHelper{DockerCommand: "docker"} + tc.dockerHelper = &docker.DockerHelper{DockerCommand: dockerValue} tc.composeHelper, err = compose.NewComposeHelper(tc.dockerHelper) framework.ExpectNoError(err) - tc.f, err = framework.SetupDockerProvider(tc.initialDir+"/bin", "docker") + tc.f, err = framework.SetupDockerProvider(tc.initialDir+"/bin", dockerValue) framework.ExpectNoError(err) }) @@ -113,11 +113,7 @@ var _ = ginkgo.Describe( err = tc.f.DevsyUp(ctx, tempDir) framework.ExpectNoError(err) - restartIds, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + restartIds, err := tc.findComposeContainer(ctx, workspace.UID, "app", ) framework.ExpectNoError(err) @@ -145,16 +141,10 @@ var _ = ginkgo.Describe( framework.ExpectNoError(err) // Distinct markers verify the persisted selector survived restart. - err = tc.f.ExecCommand( - ctx, - true, - true, - "[max]", - []string{ - cmdWorkspace, cmdSSH, flagCommand, - "echo \"[$SELECTED_PROFILE]\"", workspace.ID, - }, - ) + err = tc.f.ExecCommandContaining(ctx, "[max]", []string{ + cmdWorkspace, cmdSSH, flagCommand, + "echo \"[$SELECTED_PROFILE]\"", workspace.ID, + }) framework.ExpectNoError(err) err = tc.f.DevsyWorkspaceDelete(ctx, tempDir) @@ -203,16 +193,10 @@ var _ = ginkgo.Describe( ) assertSelectionMarker := func(want string) { - err := tc.f.ExecCommand( - ctx, - true, - true, - "["+want+"]", - []string{ - cmdWorkspace, cmdSSH, flagCommand, - "echo \"[$SELECTION_MARKER]\"", workspace.ID, - }, - ) + err := tc.f.ExecCommandContaining(ctx, "["+want+"]", []string{ + cmdWorkspace, cmdSSH, flagCommand, + "echo \"[$SELECTION_MARKER]\"", workspace.ID, + }) framework.ExpectNoError(err) } assertSelectionMarker("legacy") @@ -249,10 +233,8 @@ var _ = ginkgo.Describe( tc.expectSingleAppContainer(ctx, workspace) - err = tc.f.ExecCommand( + err = tc.f.ExecCommandContaining( ctx, - true, - true, "BAR", []string{cmdWorkspace, cmdSSH, flagCommand, "echo $FOO", workspace.ID}, ) @@ -296,10 +278,8 @@ var _ = ginkgo.Describe( tc.expectSingleAppContainer(ctx, workspace) - err = tc.f.ExecCommand( + err = tc.f.ExecCommandContaining( ctx, - true, - true, "root", []string{cmdWorkspace, cmdSSH, flagCommand, "ps u -p 1", workspace.ID}, ) @@ -343,11 +323,7 @@ var _ = ginkgo.Describe( ws, err := devsyUpAndFindWorkspace(ctx, tc.f, tempDir) framework.ExpectNoError(err, "failed to setup workspace") - ids, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - ws.UID, + ids, err := tc.findComposeContainer(ctx, ws.UID, "webserver", ) framework.ExpectNoError(err) @@ -369,15 +345,15 @@ var _ = ginkgo.Describe( containerUID := getContainerUID(ctx, tc.f, tempDir, username) containerGID := getContainerGID(ctx, tc.f, tempDir, username) - verifyUIDMapping( - containerUID, - containerGID, - testUID, - testGID, - defaultUID, - defaultGID, - username, - ) + verifyUIDMapping(uidMapping{ + ContainerUID: containerUID, + ContainerGID: containerGID, + HostUID: testUID, + HostGID: testGID, + DefaultUID: defaultUID, + DefaultGID: defaultGID, + Username: username, + }) verifyHostFileAccess(hostFile, expectedContent) verifyHostFileOwnership(hostFile, testUID, testGID, testUID == 0) @@ -408,11 +384,7 @@ var _ = ginkgo.Describe( ws, err := devsyUpAndFindWorkspace(ctx, tc.f, tempDir) framework.ExpectNoError(err, "failed to setup workspace") - ids, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - ws.UID, + ids, err := tc.findComposeContainer(ctx, ws.UID, "devcontainer", ) framework.ExpectNoError(err) @@ -427,15 +399,15 @@ var _ = ginkgo.Describe( containerUID := getContainerUID(ctx, tc.f, tempDir, username) containerGID := getContainerGID(ctx, tc.f, tempDir, username) - verifyUIDMapping( - containerUID, - containerGID, - testUID, - testGID, - defaultUID, - defaultGID, - username, - ) + verifyUIDMapping(uidMapping{ + ContainerUID: containerUID, + ContainerGID: containerGID, + HostUID: testUID, + HostGID: testGID, + DefaultUID: defaultUID, + DefaultGID: defaultGID, + Username: username, + }) verifyHostFileAccess(hostFile, expectedContent) verifyHostFileOwnership(hostFile, testUID, testGID, testUID == 0) @@ -497,34 +469,22 @@ var _ = ginkgo.Describe( tc.expectSingleAppContainer(ctx, workspace) - err = tc.f.ExecCommand( - ctx, - true, - true, - "/home/vscode/remote-env.out", - []string{ - cmdWorkspace, - cmdSSH, - flagCommand, - "ls $HOME/remote-env.out", - workspace.ID, - }, - ) + err = tc.f.ExecCommandContaining(ctx, "/home/vscode/remote-env.out", []string{ + cmdWorkspace, + cmdSSH, + flagCommand, + "ls $HOME/remote-env.out", + workspace.ID, + }) framework.ExpectNoError(err) - err = tc.f.ExecCommand( - ctx, - true, - true, - "BAR", - []string{ - cmdWorkspace, - cmdSSH, - flagCommand, - "cat $HOME/remote-env.out", - workspace.ID, - }, - ) + err = tc.f.ExecCommandContaining(ctx, "BAR", []string{ + cmdWorkspace, + cmdSSH, + flagCommand, + "cat $HOME/remote-env.out", + workspace.ID, + }) framework.ExpectNoError(err) }, ginkgo.SpecTimeout(framework.TimeoutLong())) @@ -538,31 +498,19 @@ var _ = ginkgo.Describe( tc.expectSingleAppContainer(ctx, workspace) // Verify SET_VAR is set - err = tc.f.ExecCommand( - ctx, - true, - true, - "SET=hello", - []string{ - cmdWorkspace, cmdSSH, flagCommand, - "head -1 $HOME/remote-env-null.out", - workspace.ID, - }, - ) + err = tc.f.ExecCommandContaining(ctx, "SET=hello", []string{ + cmdWorkspace, cmdSSH, flagCommand, + "head -1 $HOME/remote-env-null.out", + workspace.ID, + }) framework.ExpectNoError(err) // Verify UNSET_VAR was unset (not just empty) - err = tc.f.ExecCommand( - ctx, - true, - true, - "UNSET=true", - []string{ - cmdWorkspace, cmdSSH, flagCommand, - "tail -1 $HOME/remote-env-null.out", - workspace.ID, - }, - ) + err = tc.f.ExecCommandContaining(ctx, "UNSET=true", []string{ + cmdWorkspace, cmdSSH, flagCommand, + "tail -1 $HOME/remote-env-null.out", + workspace.ID, + }) framework.ExpectNoError(err) }, ginkgo.SpecTimeout(framework.TimeoutLong())) @@ -575,19 +523,13 @@ var _ = ginkgo.Describe( tc.expectSingleAppContainer(ctx, workspace) - err = tc.f.ExecCommand( - ctx, - true, - true, - "root", - []string{ - cmdWorkspace, - cmdSSH, - flagCommand, - "cat $HOME/remote-user.out", - workspace.ID, - }, - ) + err = tc.f.ExecCommandContaining(ctx, "root", []string{ + cmdWorkspace, + cmdSSH, + flagCommand, + "cat $HOME/remote-user.out", + workspace.ID, + }) framework.ExpectNoError(err) }, ginkgo.SpecTimeout(framework.TimeoutLong())) @@ -819,7 +761,7 @@ var _ = ginkgo.Describe( btc.f, err = framework.SetupDockerProvider( filepath.Join(btc.initialDir, "bin"), - "docker", + dockerValue, ) framework.ExpectNoError(err) }) diff --git a/e2e/tests/up-docker-compose/helper.go b/e2e/tests/up-docker-compose/helper.go index b1c8f11fea..3f1b776e09 100644 --- a/e2e/tests/up-docker-compose/helper.go +++ b/e2e/tests/up-docker-compose/helper.go @@ -108,7 +108,7 @@ func (tc *testContext) getAppContainer( ctx context.Context, workspace *provider2.Workspace, ) (*container.InspectResponse, error) { - ids, err := findComposeContainer(ctx, tc.dockerHelper, tc.composeHelper, workspace.UID, "app") + ids, err := tc.findComposeContainer(ctx, workspace.UID, "app") if err != nil { return nil, err } @@ -119,11 +119,7 @@ func (tc *testContext) expectSingleAppContainer( ctx context.Context, workspace *provider2.Workspace, ) []string { - ids, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + ids, err := tc.findComposeContainer(ctx, workspace.UID, "app", ) framework.ExpectNoError(err) @@ -136,15 +132,11 @@ func (tc *testContext) findAppAndSidecar( workspaceUID string, ) (appIDs, sidecarIDs []string) { var err error - appIDs, err = findComposeContainer( - ctx, tc.dockerHelper, tc.composeHelper, workspaceUID, "app", - ) + appIDs, err = tc.findComposeContainer(ctx, workspaceUID, "app") framework.ExpectNoError(err) gomega.Expect(appIDs).To(gomega.HaveLen(1)) - sidecarIDs, err = findComposeContainer( - ctx, tc.dockerHelper, tc.composeHelper, workspaceUID, "sidecar", - ) + sidecarIDs, err = tc.findComposeContainer(ctx, workspaceUID, "sidecar") framework.ExpectNoError(err) gomega.Expect(sidecarIDs).To(gomega.HaveLen(1)) return appIDs, sidecarIDs @@ -198,17 +190,17 @@ func setupWorkspace(testdataPath, initialDir string, f *framework.Framework) (st // findComposeContainer returns the compose container IDs for a workspace's // service, resolving devsy's actual project name first since it may not be // the sanitized workspace UID. -func findComposeContainer( +func (tc *testContext) findComposeContainer( ctx context.Context, - dockerHelper *docker.DockerHelper, - composeHelper *compose.ComposeHelper, workspaceUID, serviceName string, ) ([]string, error) { - projectName, err := composeProjectForWorkspace(ctx, dockerHelper, composeHelper, workspaceUID) + projectName, err := composeProjectForWorkspace( + ctx, tc.dockerHelper, tc.composeHelper, workspaceUID, + ) if err != nil { return nil, err } - return dockerHelper.FindContainer(ctx, []string{ + return tc.dockerHelper.FindContainer(ctx, []string{ fmt.Sprintf("%s=%s", pkgconfig.ComposeProjectLabel, projectName), fmt.Sprintf("%s=%s", pkgconfig.ComposeServiceLabel, serviceName), }) @@ -301,10 +293,17 @@ func expectedUIDMapping(goos string, host, defaults userIDs) (int, int) { return defaults.uid, defaults.gid } -func verifyUIDMapping( - containerUID, containerGID, hostUID, hostGID, defaultUID, defaultGID int, - username string, -) { +// uidMapping holds the IDs compared by verifyUIDMapping. +type uidMapping struct { + ContainerUID, ContainerGID int + HostUID, HostGID int + DefaultUID, DefaultGID int + Username string +} + +func verifyUIDMapping(m uidMapping) { + containerUID, containerGID, hostUID, hostGID := m.ContainerUID, m.ContainerGID, m.HostUID, m.HostGID + defaultUID, defaultGID, username := m.DefaultUID, m.DefaultGID, m.Username expectedUID, expectedGID := expectedUIDMapping( runtime.GOOS, userIDs{uid: hostUID, gid: hostGID}, @@ -334,6 +333,7 @@ func verifyUIDMapping( } func verifyHostFileAccess(filePath, expectedContent string) { + //nolint:gosec // filePath is a host path the test created content, err := os.ReadFile(filePath) framework.ExpectNoError(err) gomega.Expect(string(content)). diff --git a/e2e/tests/up-docker-compose/up_docker_compose.go b/e2e/tests/up-docker-compose/up_docker_compose.go index 240aa91756..02e5e2a571 100644 --- a/e2e/tests/up-docker-compose/up_docker_compose.go +++ b/e2e/tests/up-docker-compose/up_docker_compose.go @@ -25,6 +25,11 @@ import ( "github.com/onsi/gomega" ) +const ( + userValue = "--user" + root = "root" +) + const ( composeSecretCommand = "secret" composeSecretSetCommand = "set" @@ -45,11 +50,11 @@ var _ = ginkgo.Describe( tc.initialDir, err = os.Getwd() framework.ExpectNoError(err) - tc.dockerHelper = &docker.DockerHelper{DockerCommand: "docker"} + tc.dockerHelper = &docker.DockerHelper{DockerCommand: dockerValue} tc.composeHelper, err = compose.NewComposeHelper(tc.dockerHelper) framework.ExpectNoError(err) - tc.f, err = framework.SetupDockerProvider(tc.initialDir+"/bin", "docker") + tc.f, err = framework.SetupDockerProvider(tc.initialDir+"/bin", dockerValue) framework.ExpectNoError(err) }) @@ -65,9 +70,9 @@ var _ = ginkgo.Describe( }) framework.ExpectNoError(tc.f.DevsyContextUse(ctx, contextName)) framework.ExpectNoError(tc.f.DevsyProviderAdd( - ctx, "docker", "-o", "DOCKER_PATH=docker", + ctx, dockerValue, "-o", "DOCKER_PATH=docker", )) - framework.ExpectNoError(tc.f.DevsyProviderUse(ctx, "docker")) + framework.ExpectNoError(tc.f.DevsyProviderUse(ctx, dockerValue)) tempDir, workspace, err := tc.setupAndStartWorkspace( ctx, "tests/up-docker-compose/testdata/docker-compose", @@ -223,9 +228,9 @@ var _ = ginkgo.Describe( }) framework.ExpectNoError(tc.f.DevsyContextUse(ctx, contextName)) framework.ExpectNoError(tc.f.DevsyProviderAdd( - ctx, "docker", "-o", "DOCKER_PATH=docker", + ctx, dockerValue, "-o", "DOCKER_PATH=docker", )) - framework.ExpectNoError(tc.f.DevsyProviderUse(ctx, "docker")) + framework.ExpectNoError(tc.f.DevsyProviderUse(ctx, dockerValue)) secretName := "COMPOSE_SESSION_SECRET" _, err := tc.f.ExecCommandOutput(ctx, []string{ @@ -345,11 +350,7 @@ var _ = ginkgo.Describe( ) framework.ExpectNoError(err) - ids, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + ids, err := tc.findComposeContainer(ctx, workspace.UID, "app", ) framework.ExpectNoError(err) @@ -363,8 +364,8 @@ var _ = ginkgo.Describe( flagCommand, "touch /home/vscode/mnt1/foo.txt", workspace.ID, - "--user", - "root", + userValue, + root, }, ) framework.ExpectNoError(err) @@ -377,8 +378,8 @@ var _ = ginkgo.Describe( flagCommand, "echo -n BAR > /home/vscode/mnt1/foo.txt", workspace.ID, - "--user", - "root", + userValue, + root, }, ) framework.ExpectNoError(err) @@ -400,11 +401,7 @@ var _ = ginkgo.Describe( ) framework.ExpectNoError(err) - ids, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + ids, err := tc.findComposeContainer(ctx, workspace.UID, "app", ) framework.ExpectNoError(err) @@ -434,8 +431,8 @@ var _ = ginkgo.Describe( flagCommand, "touch /home/vscode/mnt-ro/should-not-write.txt", workspace.ID, - "--user", - "root", + userValue, + root, }, ) framework.ExpectError( @@ -474,11 +471,7 @@ var _ = ginkgo.Describe( ) framework.ExpectNoError(err) - ids, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + ids, err := tc.findComposeContainer(ctx, workspace.UID, "app", ) framework.ExpectNoError(err) @@ -488,6 +481,7 @@ var _ = ginkgo.Describe( sshContext, sshCancel := context.WithCancel(context.Background()) go func() { + //nolint:gosec // e2e harness command by design cmd := exec.CommandContext( sshContext, filepath.Join(tc.f.DevsyBinDir, tc.f.DevsyBinName), @@ -540,11 +534,7 @@ var _ = ginkgo.Describe( ) framework.ExpectNoError(err) - ids, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + ids, err := tc.findComposeContainer(ctx, workspace.UID, "app", ) framework.ExpectNoError(err) @@ -612,11 +602,7 @@ var _ = ginkgo.Describe( ) framework.ExpectNoError(err) - ids, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + ids, err := tc.findComposeContainer(ctx, workspace.UID, "app", ) framework.ExpectNoError(err) @@ -741,9 +727,7 @@ var _ = ginkgo.Describe( To(gomega.Equal("overlay-init-ran")) expectedWorkspaceMountSource, err := filepath.EvalSymlinks(tempDir) framework.ExpectNoError(err) - appIDs, err := findComposeContainer( - ctx, tc.dockerHelper, tc.composeHelper, workspace.UID, "app", - ) + appIDs, err := tc.findComposeContainer(ctx, workspace.UID, "app") framework.ExpectNoError(err) app, err := tc.inspectContainer(ctx, appIDs) framework.ExpectNoError(err) @@ -768,9 +752,7 @@ var _ = ginkgo.Describe( framework.ExpectNoError(err) gomega.Expect(strings.TrimSpace(sidecarMarker)).To(gomega.Equal("sidecar-started")) - sidecarIDs, err := findComposeContainer( - ctx, tc.dockerHelper, tc.composeHelper, workspace.UID, "sidecar", - ) + sidecarIDs, err := tc.findComposeContainer(ctx, workspace.UID, "sidecar") framework.ExpectNoError(err) gomega.Expect(sidecarIDs).To(gomega.HaveLen(1)) sidecar, err := tc.inspectContainer(ctx, sidecarIDs) @@ -795,14 +777,10 @@ var _ = ginkgo.Describe( initMarker, err = tc.f.DevsySSH(ctx, tempDir, "cat /workspaces/overlay-init-marker") framework.ExpectNoError(err) gomega.Expect(strings.TrimSpace(initMarker)).To(gomega.Equal("overlay-init-ran")) - appIDs, err = findComposeContainer( - ctx, tc.dockerHelper, tc.composeHelper, workspace.UID, "app", - ) + appIDs, err = tc.findComposeContainer(ctx, workspace.UID, "app") framework.ExpectNoError(err) gomega.Expect(appIDs).To(gomega.HaveLen(1)) - alternateIDs, err := findComposeContainer( - ctx, tc.dockerHelper, tc.composeHelper, workspace.UID, "alternate", - ) + alternateIDs, err := tc.findComposeContainer(ctx, workspace.UID, "alternate") framework.ExpectNoError(err) gomega.Expect(alternateIDs).To(gomega.BeEmpty()) @@ -814,14 +792,10 @@ var _ = ginkgo.Describe( overlayPath, ) framework.ExpectNoError(err) - appIDs, err = findComposeContainer( - ctx, tc.dockerHelper, tc.composeHelper, workspace.UID, "app", - ) + appIDs, err = tc.findComposeContainer(ctx, workspace.UID, "app") framework.ExpectNoError(err) gomega.Expect(appIDs).To(gomega.BeEmpty()) - alternateIDs, err = findComposeContainer( - ctx, tc.dockerHelper, tc.composeHelper, workspace.UID, "alternate", - ) + alternateIDs, err = tc.findComposeContainer(ctx, workspace.UID, "alternate") framework.ExpectNoError(err) gomega.Expect(alternateIDs).To(gomega.HaveLen(1)) alternate, err := tc.inspectContainer(ctx, alternateIDs) @@ -835,9 +809,7 @@ var _ = ginkgo.Describe( framework.ExpectNoError(err) gomega.Expect(strings.TrimSpace(alternateMarker)). To(gomega.Equal("alternate-started")) - sidecarIDs, err = findComposeContainer( - ctx, tc.dockerHelper, tc.composeHelper, workspace.UID, "sidecar", - ) + sidecarIDs, err = tc.findComposeContainer(ctx, workspace.UID, "sidecar") framework.ExpectNoError(err) gomega.Expect(sidecarIDs).To(gomega.HaveLen(1)) sidecarMarker, err = tc.f.DevsySSH(ctx, tempDir, "cat /workspaces/sidecar-marker") @@ -874,9 +846,7 @@ var _ = ginkgo.Describe( overlayPath, ) framework.ExpectNoError(err) - alternateIDs, err = findComposeContainer( - ctx, tc.dockerHelper, tc.composeHelper, workspace.UID, "alternate", - ) + alternateIDs, err = tc.findComposeContainer(ctx, workspace.UID, "alternate") framework.ExpectNoError(err) gomega.Expect(alternateIDs).To(gomega.HaveLen(1)) alternate, err = tc.inspectContainer(ctx, alternateIDs) @@ -937,11 +907,7 @@ var _ = ginkgo.Describe( ) framework.ExpectNoError(err) - ids, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + ids, err := tc.findComposeContainer(ctx, workspace.UID, "app", ) framework.ExpectNoError(err) @@ -1098,6 +1064,7 @@ var _ = ginkgo.Describe( framework.ExpectNoError(err) gomega.Expect(ids).To(gomega.HaveLen(1), "1 compose container to be created") + //nolint:gosec // test output file inside the temp directory initializeCommand, err := os.ReadFile(filepath.Join(tempDir, "initialize-command.out")) framework.ExpectNoError(err) gomega.Expect(initializeCommand).To(gomega.ContainSubstring("initializeCommand")) @@ -1207,7 +1174,7 @@ var _ = ginkgo.Describe( ) framework.ExpectNoError(err) - ids, err := findComposeContainer(ctx, tc.dockerHelper, tc.composeHelper, ws.UID, "app") + ids, err := tc.findComposeContainer(ctx, ws.UID, "app") framework.ExpectNoError(err) gomega.Expect(ids).To(gomega.HaveLen(1), "1 compose container to be created") @@ -1223,21 +1190,13 @@ var _ = ginkgo.Describe( ) framework.ExpectNoError(err) - appIDs, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + appIDs, err := tc.findComposeContainer(ctx, workspace.UID, "app", ) framework.ExpectNoError(err) gomega.Expect(appIDs).To(gomega.HaveLen(1), "app container to be created") - dbIDs, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + dbIDs, err := tc.findComposeContainer(ctx, workspace.UID, "db", ) framework.ExpectNoError(err) @@ -1251,21 +1210,13 @@ var _ = ginkgo.Describe( ) framework.ExpectNoError(err) - appIDs, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + appIDs, err := tc.findComposeContainer(ctx, workspace.UID, "app", ) framework.ExpectNoError(err) gomega.Expect(appIDs).To(gomega.HaveLen(1), "app container to be created") - dbIDs, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + dbIDs, err := tc.findComposeContainer(ctx, workspace.UID, "db", ) framework.ExpectNoError(err) @@ -1301,11 +1252,7 @@ var _ = ginkgo.Describe( ) framework.ExpectNoError(err) - ids, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + ids, err := tc.findComposeContainer(ctx, workspace.UID, "app", ) framework.ExpectNoError(err) @@ -1325,11 +1272,7 @@ var _ = ginkgo.Describe( ) framework.ExpectNoError(err) - ids, err := findComposeContainer( - ctx, - tc.dockerHelper, - tc.composeHelper, - workspace.UID, + ids, err := tc.findComposeContainer(ctx, workspace.UID, "app", ) framework.ExpectNoError(err) diff --git a/e2e/tests/up-features/helper.go b/e2e/tests/up-features/helper.go index 1265a83ba2..c8e57d20a4 100644 --- a/e2e/tests/up-features/helper.go +++ b/e2e/tests/up-features/helper.go @@ -10,6 +10,7 @@ import ( ) func createTarGzArchive(outputFilePath string, filePaths []string) error { + //nolint:gosec // test helper reads files it created in its own temp directory outputFile, err := os.Create(outputFilePath) if err != nil { return err @@ -31,6 +32,7 @@ func createTarGzArchive(outputFilePath string, filePaths []string) error { } func addFileToTar(tarWriter *tar.Writer, filePath string) error { + //nolint:gosec // test helper reads files it created in its own temp directory file, err := os.Open(filePath) if err != nil { return err diff --git a/e2e/tests/up-features/up_features.go b/e2e/tests/up-features/up_features.go index 4c71df8e8e..bf65a89f2c 100644 --- a/e2e/tests/up-features/up_features.go +++ b/e2e/tests/up-features/up_features.go @@ -99,6 +99,7 @@ var _ = ginkgo.Describe("testing up command", ginkgo.Label("up-features"), func( err := createTarGzArchive(featureArchiveFilePath, featureFiles) framework.ExpectNoError(err) + //nolint:gosec // test helper reads files it created in its own temp directory devContainerFileBuf, err := os.ReadFile(path.Join(tempDir, ".devcontainer.json")) framework.ExpectNoError(err) @@ -110,6 +111,7 @@ var _ = ginkgo.Describe("testing up command", ginkgo.Label("up-features"), func( respHeader := http.Header{} respHeader.Set("Content-Disposition", "attachment; filename=devcontainer-feature-hello.tgz") + //nolint:gosec // test fixture path built in the temp directory featureArchiveFileBuf, err := os.ReadFile(featureArchiveFilePath) framework.ExpectNoError(err) diff --git a/e2e/tests/up/dockerfile_build.go b/e2e/tests/up/dockerfile_build.go index fd04e5345f..b5597214a0 100644 --- a/e2e/tests/up/dockerfile_build.go +++ b/e2e/tests/up/dockerfile_build.go @@ -14,6 +14,10 @@ import ( "github.com/onsi/gomega" ) +const ( + dockerValue = "docker" +) + var _ = ginkgo.Describe( "testing up command for dockerfile builds", ginkgo.Label("up-provider-docker"), @@ -27,9 +31,9 @@ var _ = ginkgo.Describe( initialDir, err = os.Getwd() framework.ExpectNoError(err) - dockerHelper = &docker.DockerHelper{DockerCommand: "docker"} + dockerHelper = &docker.DockerHelper{DockerCommand: dockerValue} - f, err = framework.SetupDockerProvider(initialDir+"/bin", "docker") + f, err = framework.SetupDockerProvider(initialDir+"/bin", dockerValue) framework.ExpectNoError(err) }) diff --git a/e2e/tests/up/git_repositories.go b/e2e/tests/up/git_repositories.go index ab18596d7b..5636224d16 100644 --- a/e2e/tests/up/git_repositories.go +++ b/e2e/tests/up/git_repositories.go @@ -9,6 +9,10 @@ import ( "github.com/onsi/ginkgo/v2" ) +const ( + subpathDevsyJupyterNotebookHelloWorld = "subpath--devsy-jupyter-notebook-hello-world" +) + var _ = ginkgo.Describe( "testing up command for working with git repositories", ginkgo.Label("up-workspaces"), @@ -76,7 +80,7 @@ var _ = ginkgo.Describe( ) framework.ExpectNoError(err) - id := "subpath--devsy-jupyter-notebook-hello-world" + id := subpathDevsyJupyterNotebookHelloWorld out, err := f.DevsySSH(ctx, id, "pwd") framework.ExpectNoError(err) framework.ExpectEqual(out, fmt.Sprintf("/workspaces/%s\n", id), "should be subpath") diff --git a/e2e/tests/up/helper.go b/e2e/tests/up/helper.go index 9d57a52422..869e8ac2b5 100644 --- a/e2e/tests/up/helper.go +++ b/e2e/tests/up/helper.go @@ -20,6 +20,10 @@ import ( "github.com/onsi/gomega" ) +const ( + attach = "attach" +) + const ( secretCmd = "secret" envCmd = "env" @@ -135,7 +139,11 @@ func (dtc *dockerTestContext) setupAndUp( testDataPath string, upArgs ...string, ) (string, error) { - return setupWorkspaceAndUp(ctx, testDataPath, dtc.initialDir, dtc.f, upArgs...) + return setupWorkspaceAndUp( + ctx, + dtc.f, + workspaceSource{testDataPath, dtc.initialDir}, + upArgs...) } func (dtc *dockerTestContext) findWorkspaceContainer( @@ -227,13 +235,18 @@ func setupWorkspace(testdataPath, initialDir string, f *framework.Framework) (st return tempDir, nil } +type workspaceSource struct { + testdataPath string + initialDir string +} + func setupWorkspaceAndUp( ctx context.Context, - testdataPath, initialDir string, f *framework.Framework, + source workspaceSource, args ...string, ) (string, error) { - tempDir, err := setupWorkspace(testdataPath, initialDir, f) + tempDir, err := setupWorkspace(source.testdataPath, source.initialDir, f) if err != nil { return "", err } @@ -273,7 +286,7 @@ func (dtc *dockerTestContext) verifyContextAttachment( tempDir, err := setupWorkspace(tc.testdataDir, dtc.initialDir, dtc.f) framework.ExpectNoError(err) tc.store(ctx, tc.name, "expected-value") - _, err = dtc.f.ExecCommandOutput(ctx, []string{tc.command, "attach", tc.name}) + _, err = dtc.f.ExecCommandOutput(ctx, []string{tc.command, attach, tc.name}) framework.ExpectNoError(err) // Intentionally no --env/--secret argument: the context binding is the source. diff --git a/e2e/tests/up/podman_rootful.go b/e2e/tests/up/podman_rootful.go index c0acd18c53..9ea7c26505 100644 --- a/e2e/tests/up/podman_rootful.go +++ b/e2e/tests/up/podman_rootful.go @@ -317,21 +317,7 @@ func attemptPodmanRecovery(ctx context.Context, wrapperPath string) (podmanHealt return podmanHealthOK, nil } -// setupRootfulPodman prepares the rootful Podman wrapper and docker provider -// and gates the shard on daemon health: once recovery has failed, remaining -// specs skip instead of re-failing on the same wedged infrastructure. -func setupRootfulPodman(ctx context.Context, initialDir string) *framework.Framework { - wrapperPath := initialDir + "/bin/" + podmanRootfulWrapperName - - managedDaemon := rootfulPodmanUsesManagedDaemon(os.Getenv("DOCKER_HOST")) - if since := rootfulDaemonGate.unhealthy(); managedDaemon && since != "" { - ginkgo.Skip(fmt.Sprintf( - "rootful Podman daemon unhealthy since first failure in %q; "+ - "skipping to avoid cascading infrastructure failures", - since, - )) - } - +func writeRootfulWrapper(wrapperPath string) { wrapper, err := os.Create(wrapperPath) //nolint:gosec // G304: test-controlled path framework.ExpectNoError(err) @@ -347,47 +333,89 @@ func setupRootfulPodman(ctx context.Context, initialDir string) *framework.Frame ginkgo.DeferCleanup(func() { _ = os.Remove(wrapperPath) }) +} + +// handleUnhealthyPodman fails the spec for an unhealthy daemon, attempting one +// shard-wide restart first when the daemon is managed and the class is +// recoverable. +// podmanHealthFailure is a failed runtime health check and its classification. +type podmanHealthFailure struct { + class podmanHealthClass + err error +} + +func handleUnhealthyPodman( + ctx context.Context, + wrapperPath string, + failure podmanHealthFailure, + managedDaemon bool, +) { + class, healthErr := failure.class, failure.err + ginkgo.GinkgoWriter.Printf( + "[podman-health] PODMAN_ROOTFUL_HEALTH_%s: %v\n", + strings.ToUpper(class.String()), + healthErr, + ) + collectPodmanDiagnostics(wrapperPath) + if !managedDaemon || !shouldAttemptPodmanRecovery(class) { + // The managed service cannot repair another endpoint or a + // responsive daemon error, so fail without gating. + framework.ExpectNoError(healthErr) + return + } + if !rootfulDaemonGate.claimRecovery() { + rootfulDaemonGate.markUnhealthy(ginkgo.CurrentSpecReport().FullText()) + framework.ExpectNoError( + fmt.Errorf( + "PODMAN_ROOTFUL_RUNTIME_POISONED: recovery already used: %w", + healthErr, + ), + ) + } + if postClass, recErr := attemptPodmanRecovery(ctx, wrapperPath); recErr != nil { + rootfulDaemonGate.markUnhealthy(ginkgo.CurrentSpecReport().FullText()) + framework.ExpectNoError(fmt.Errorf( + "PODMAN_ROOTFUL_RUNTIME_POISONED: rootful Podman daemon unhealthy; "+ + "single recovery attempt failed (class: %s): %w "+ + "(initial check: %v)", + postClass, + recErr, + healthErr, + )) + } + ginkgo.GinkgoWriter.Println( + "[podman-recovery] daemon healthy again after single restart", + ) +} + +// setupRootfulPodman prepares the rootful Podman wrapper and docker provider +// and gates the shard on daemon health: once recovery has failed, remaining +// specs skip instead of re-failing on the same wedged infrastructure. +func setupRootfulPodman(ctx context.Context, initialDir string) *framework.Framework { + wrapperPath := initialDir + "/bin/" + podmanRootfulWrapperName + + managedDaemon := rootfulPodmanUsesManagedDaemon(os.Getenv("DOCKER_HOST")) + if since := rootfulDaemonGate.unhealthy(); managedDaemon && since != "" { + ginkgo.Skip(fmt.Sprintf( + "rootful Podman daemon unhealthy since first failure in %q; "+ + "skipping to avoid cascading infrastructure failures", + since, + )) + } + + writeRootfulWrapper(wrapperPath) ginkgo.GinkgoWriter.Println("[podman-health] probe start") started := time.Now() class, healthErr := checkPodmanHealth(ctx, wrapperPath) ginkgo.GinkgoWriter.Printf("[podman-health] class=%s elapsed=%s\n", class, time.Since(started)) if healthErr != nil { - ginkgo.GinkgoWriter.Printf( - "[podman-health] PODMAN_ROOTFUL_HEALTH_%s: %v\n", - strings.ToUpper(class.String()), - healthErr, + handleUnhealthyPodman( + ctx, + wrapperPath, + podmanHealthFailure{class: class, err: healthErr}, + managedDaemon, ) - collectPodmanDiagnostics(wrapperPath) - if managedDaemon && shouldAttemptPodmanRecovery(class) { - if !rootfulDaemonGate.claimRecovery() { - rootfulDaemonGate.markUnhealthy(ginkgo.CurrentSpecReport().FullText()) - framework.ExpectNoError( - fmt.Errorf( - "PODMAN_ROOTFUL_RUNTIME_POISONED: recovery already used: %w", - healthErr, - ), - ) - } - if postClass, recErr := attemptPodmanRecovery(ctx, wrapperPath); recErr != nil { - rootfulDaemonGate.markUnhealthy(ginkgo.CurrentSpecReport().FullText()) - framework.ExpectNoError(fmt.Errorf( - "PODMAN_ROOTFUL_RUNTIME_POISONED: rootful Podman daemon unhealthy; "+ - "single recovery attempt failed (class: %s): %w "+ - "(initial check: %v)", - postClass, - recErr, - healthErr, - )) - } - ginkgo.GinkgoWriter.Println( - "[podman-recovery] daemon healthy again after single restart", - ) - } else { - // The managed service cannot repair another endpoint or a - // responsive daemon error, so fail without gating. - framework.ExpectNoError(healthErr) - } } f, err := framework.SetupDockerProvider(initialDir+"/bin", wrapperPath) @@ -395,6 +423,35 @@ func setupRootfulPodman(ctx context.Context, initialDir string) *framework.Frame return f } +// reportNonRootfulCleanupFailure keeps the minimal diagnostic for shards +// without the rootful wrapper. +func reportNonRootfulCleanupFailure() { + ginkgo.GinkgoWriter.Printf( + "cleanup failure podman ps -a:\n%s\n", + runDiagCommand(context.Background(), podmanBinName, "ps", "-a"), + ) +} + +func reportFailedCleanupRetry( + ctx context.Context, + wrapperPath, tempDir string, + retryErr error, +) { + class, healthErr := checkPodmanHealth(ctx, wrapperPath) + if healthErr != nil && shouldAttemptPodmanRecovery(class) { + rootfulDaemonGate.markUnhealthy(ginkgo.CurrentSpecReport().FullText()) + ginkgo.GinkgoWriter.Printf( + "[podman-health] PODMAN_ROOTFUL_RUNTIME_POISONED after cleanup retry: %v\n", + healthErr, + ) + } + ginkgo.GinkgoWriter.Printf( + "[podman-recovery] cleanup retry after daemon restart still failed for %s: %v\n", + tempDir, + retryErr, + ) +} + type podmanCleanupDirs struct { initialDir string tempDir string @@ -413,11 +470,7 @@ func recoverPodmanCleanup( ) error { wrapperPath := dirs.initialDir + "/bin/" + podmanRootfulWrapperName if _, err := os.Stat(wrapperPath); err != nil { - // Not a rootful shard: keep the previous minimal diagnostic. - ginkgo.GinkgoWriter.Printf( - "cleanup failure podman ps -a:\n%s\n", - runDiagCommand(context.Background(), podmanBinName, "ps", "-a"), - ) + reportNonRootfulCleanupFailure() return cleanupErr } @@ -447,22 +500,7 @@ func recoverPodmanCleanup( } retryErr := f.CleanupWorkspace(ctx, dirs.tempDir) if retryErr != nil { - if class, healthErr := checkPodmanHealth( - recoveryCtx, - wrapperPath, - ); healthErr != nil && - shouldAttemptPodmanRecovery(class) { - rootfulDaemonGate.markUnhealthy(ginkgo.CurrentSpecReport().FullText()) - ginkgo.GinkgoWriter.Printf( - "[podman-health] PODMAN_ROOTFUL_RUNTIME_POISONED after cleanup retry: %v\n", - healthErr, - ) - } - ginkgo.GinkgoWriter.Printf( - "[podman-recovery] cleanup retry after daemon restart still failed for %s: %v\n", - dirs.tempDir, - retryErr, - ) + reportFailedCleanupRetry(recoveryCtx, wrapperPath, dirs.tempDir, retryErr) return retryErr } ginkgo.GinkgoWriter.Printf( diff --git a/e2e/tests/up/podman_rootful_test.go b/e2e/tests/up/podman_rootful_test.go index 1a740335ee..ee96d76cc6 100644 --- a/e2e/tests/up/podman_rootful_test.go +++ b/e2e/tests/up/podman_rootful_test.go @@ -14,6 +14,10 @@ import ( "github.com/stretchr/testify/require" ) +const ( + podmanValue = "podman" +) + func expiredContext(t *testing.T) context.Context { t.Helper() ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond) @@ -104,7 +108,7 @@ func TestRunPodmanDiagnosticsStopsAtBudget(t *testing.T) { var commands []string runPodmanDiagnostics(ctx, []podmanDiagSection{ {"processes", "ps", nil}, - {"podman", "podman", nil}, + {podmanValue, podmanValue, nil}, }, func(_ context.Context, name string, _ ...string) string { commands = append(commands, name) cancel() @@ -131,30 +135,30 @@ func TestRootfulPodmanWrapper(t *testing.T) { { name: "configured endpoint", endpoint: &endpoint, - wantPrefix: []string{"podman", remoteFlag, urlFlag, endpoint}, + wantPrefix: []string{podmanValue, remoteFlag, urlFlag, endpoint}, args: []string{"ps", "-a"}, }, { name: "credential endpoint remains unchanged", endpoint: &credentialEndpoint, - wantPrefix: []string{"podman", remoteFlag, urlFlag, credentialEndpoint}, + wantPrefix: []string{podmanValue, remoteFlag, urlFlag, credentialEndpoint}, args: []string{"ps"}, }, { name: "preserves arguments", endpoint: &endpoint, - wantPrefix: []string{"podman", remoteFlag, urlFlag, endpoint}, + wantPrefix: []string{podmanValue, remoteFlag, urlFlag, endpoint}, args: []string{"run", "value with spaces", "$(printf unsafe)"}, }, { name: "unset endpoint uses local mode", - wantPrefix: []string{"podman"}, + wantPrefix: []string{podmanValue}, args: []string{"ps", "-a"}, }, { name: "empty endpoint uses local mode", endpoint: &emptyEndpoint, - wantPrefix: []string{"podman"}, + wantPrefix: []string{podmanValue}, args: []string{"ps", "-a"}, }, } diff --git a/e2e/tests/up/provider_docker.go b/e2e/tests/up/provider_docker.go index 97f20037e0..3f2016e2ae 100644 --- a/e2e/tests/up/provider_docker.go +++ b/e2e/tests/up/provider_docker.go @@ -23,6 +23,10 @@ import ( "github.com/onsi/gomega" ) +const ( + attachedSecret = "ATTACHED_SECRET" +) + const envEqualsIsolatedHomeLabel = "env-equals-isolated-home" var _ = ginkgo.Describe( @@ -54,10 +58,10 @@ var _ = ginkgo.Describe( framework.SetEnvForSpec(devsyconfig.EnvHome, home) } - dtc.dockerHelper = &docker.DockerHelper{DockerCommand: "docker"} + dtc.dockerHelper = &docker.DockerHelper{DockerCommand: dockerValue} dtc.f, err = framework.SetupDockerProvider( filepath.Join(dtc.initialDir, "bin"), - "docker", + dockerValue, ) framework.ExpectNoError(err) }) @@ -307,7 +311,7 @@ var _ = ginkgo.Describe( }) framework.ExpectNoError(dtc.f.DevsyContextUse(ctx, contextName)) framework.ExpectNoError( - dtc.f.DevsyProviderAdd(ctx, "docker", "-o", "DOCKER_PATH=docker"), + dtc.f.DevsyProviderAdd(ctx, dockerValue, "-o", "DOCKER_PATH=docker"), ) tempDir, err := setupWorkspace( @@ -978,9 +982,9 @@ printf "PLAIN=%s\nONE_EQ=%s\nTWO_EQ=%s\nCOMMA_EQ=%s\nTRAILING_EQ=%s\nFILE_ONE_EQ }) framework.ExpectNoError(dtc.f.DevsyContextUse(ctx, contextName)) framework.ExpectNoError(dtc.f.DevsyProviderAdd( - ctx, "docker", "-o", "DOCKER_PATH=docker", + ctx, dockerValue, "-o", "DOCKER_PATH=docker", )) - framework.ExpectNoError(dtc.f.DevsyProviderUse(ctx, "docker")) + framework.ExpectNoError(dtc.f.DevsyProviderUse(ctx, dockerValue)) tempDir, err := setupWorkspace( "tests/up/testdata/docker-managed-secret-runtime-mount", @@ -1084,9 +1088,9 @@ printf "PLAIN=%s\nONE_EQ=%s\nTWO_EQ=%s\nCOMMA_EQ=%s\nTRAILING_EQ=%s\nFILE_ONE_EQ }) framework.ExpectNoError(dtc.f.DevsyContextUse(ctx, contextName)) framework.ExpectNoError( - dtc.f.DevsyProviderAdd(ctx, "docker", "-o", "DOCKER_PATH=docker"), + dtc.f.DevsyProviderAdd(ctx, dockerValue, "-o", "DOCKER_PATH=docker"), ) - framework.ExpectNoError(dtc.f.DevsyProviderUse(ctx, "docker")) + framework.ExpectNoError(dtc.f.DevsyProviderUse(ctx, dockerValue)) tempDir, err := setupWorkspace( "tests/up/testdata/docker-managed-env-attached", @@ -1095,7 +1099,7 @@ printf "PLAIN=%s\nONE_EQ=%s\nTWO_EQ=%s\nCOMMA_EQ=%s\nTRAILING_EQ=%s\nFILE_ONE_EQ ) framework.ExpectNoError(err) dtc.storeEnv(ctx, "ATTACHED_ENV", "stored-value") - _, err = dtc.f.ExecCommandOutput(ctx, []string{envCmd, "attach", "ATTACHED_ENV"}) + _, err = dtc.f.ExecCommandOutput(ctx, []string{envCmd, attach, "ATTACHED_ENV"}) framework.ExpectNoError(err) framework.ExpectNoError(dtc.f.DevsyUp( @@ -1126,10 +1130,10 @@ printf "PLAIN=%s\nONE_EQ=%s\nTWO_EQ=%s\nCOMMA_EQ=%s\nTRAILING_EQ=%s\nFILE_ONE_EQ testdataDir: "tests/up/testdata/docker-managed-secret-attached", store: dtc.storeSecret, command: secretCmd, - name: "ATTACHED_SECRET", + name: attachedSecret, checkFile: "/tmp/attached-secret-check.out", sessionCheck: `if [ "$ATTACHED_SECRET" = expected-value ]; then printf present; else printf absent; fi`, - sessionFile: "ATTACHED_SECRET", + sessionFile: attachedSecret, }) }, ginkgo.SpecTimeout(framework.TimeoutShort()), @@ -1147,9 +1151,9 @@ printf "PLAIN=%s\nONE_EQ=%s\nTWO_EQ=%s\nCOMMA_EQ=%s\nTRAILING_EQ=%s\nFILE_ONE_EQ }) framework.ExpectNoError(dtc.f.DevsyContextUse(ctx, contextName)) framework.ExpectNoError(dtc.f.DevsyProviderAdd( - ctx, "docker", "-o", "DOCKER_PATH=docker", + ctx, dockerValue, "-o", "DOCKER_PATH=docker", )) - framework.ExpectNoError(dtc.f.DevsyProviderUse(ctx, "docker")) + framework.ExpectNoError(dtc.f.DevsyProviderUse(ctx, dockerValue)) tempDir, err := setupWorkspace( "tests/up/testdata/docker-managed-secret-collision", dtc.initialDir, dtc.f, @@ -1169,10 +1173,10 @@ printf "PLAIN=%s\nONE_EQ=%s\nTWO_EQ=%s\nCOMMA_EQ=%s\nTRAILING_EQ=%s\nFILE_ONE_EQ dtc.dockerHelper.Inspect(ctx, ids, "container", &beforeDetails), ) - dtc.storeSecret(ctx, "ATTACHED_SECRET", "sentinel-late-secret") + dtc.storeSecret(ctx, attachedSecret, "sentinel-late-secret") _, err = dtc.f.ExecCommandOutput( ctx, - []string{secretCmd, "attach", "ATTACHED_SECRET"}, + []string{secretCmd, attach, attachedSecret}, ) framework.ExpectNoError(err) secretList, _, err := dtc.f.ExecCommandCapture(ctx, diff --git a/e2e/tests/up/provider_kubernetes.go b/e2e/tests/up/provider_kubernetes.go index 1a7893fab3..b24b2abb9b 100644 --- a/e2e/tests/up/provider_kubernetes.go +++ b/e2e/tests/up/provider_kubernetes.go @@ -20,6 +20,7 @@ func waitForPodCount( ) *framework.PodList { list := &framework.PodList{} gomega.Eventually(func() int { + //nolint:gosec // e2e harness command by design cmd := exec.CommandContext( ctx, "kubectl", diff --git a/e2e/tests/up/provider_kubernetes_restricted.go b/e2e/tests/up/provider_kubernetes_restricted.go index 23fcafe40d..7a72089fcd 100644 --- a/e2e/tests/up/provider_kubernetes_restricted.go +++ b/e2e/tests/up/provider_kubernetes_restricted.go @@ -111,20 +111,14 @@ var _ = ginkgo.Describe( gomega.Expect(*sc.RunAsUser).To(gomega.Equal(int64(1000))) gomega.Expect(*sc.RunAsNonRoot).To(gomega.BeTrue()) - err = f.ExecCommand( - ctx, - true, - true, - "mYtEsTsTrInG", - []string{ - cmdWorkspace, - cmdSSH, - "--agent-forwarding=false", - flagCommand, - "echo 'bVl0RXNUc1RySW5H' | base64 -d", - tempDir, - }, - ) + err = f.ExecCommandContaining(ctx, "mYtEsTsTrInG", []string{ + cmdWorkspace, + cmdSSH, + "--agent-forwarding=false", + flagCommand, + "echo 'bVl0RXNUc1RySW5H' | base64 -d", + tempDir, + }) framework.ExpectNoError(err) }, ginkgo.SpecTimeout(framework.TimeoutModerate()), diff --git a/e2e/tests/up/provider_kubernetes_ssh_agent.go b/e2e/tests/up/provider_kubernetes_ssh_agent.go index 966468d9e9..fb9101aaa9 100644 --- a/e2e/tests/up/provider_kubernetes_ssh_agent.go +++ b/e2e/tests/up/provider_kubernetes_ssh_agent.go @@ -221,6 +221,14 @@ func privateSSHGitFixture( } return nil }).WithTimeout(time.Minute).WithPolling(time.Second).Should(gomega.Succeed()) + expectUnauthenticatedDenied(ctx, source, sshOptions) + + return source, sha +} + +// expectUnauthenticatedDenied asserts that the private repository refuses +// ls-remote when no agent key is offered. +func expectUnauthenticatedDenied(ctx context.Context, source, sshOptions string) { out, err := runFixtureCommand( ctx, 2*time.Minute, @@ -233,7 +241,6 @@ func privateSSHGitFixture( To(gomega.HaveOccurred(), "private repository must deny unauthenticated ls-remote: %s", out) gomega.Expect(out). To(gomega.ContainSubstring("Permission denied"), "unauthenticated failure must be authentication rejection") - return source, sha } func createKindSSHGitRepository(ctx context.Context, fixtureDir string) string { @@ -320,6 +327,11 @@ func startKindSSHGitServer(ctx context.Context, buildDir, id string) string { return host } +// sshArgv returns the ssh command line for the given options and arguments. +func sshArgv(options []string, args ...string) []string { + return append(append([]string{cmdSSH}, options...), args...) +} + func verifyKindSSHAgentCleanup( ctx context.Context, f *framework.Framework, @@ -341,31 +353,12 @@ func verifyKindSSHAgentCleanup( "-o", "ConnectTimeout=10", } - fixtureCommand( - ctx, - 2*time.Minute, - env, - append([]string{cmdSSH}, - append( - options, - "-o", - "ForwardAgent=yes", - "-o", - "ControlMaster=yes", - "-o", - "ControlPersist=60", - "-N", - "-f", - host, - )...)...) + fixtureCommand(ctx, 2*time.Minute, env, sshArgv(options, + "-o", "ForwardAgent=yes", "-o", "ControlMaster=yes", "-o", "ControlPersist=60", + "-N", "-f", host)...) closed := false closeMaster := func(cleanupCtx context.Context) { - fixtureCommand( - cleanupCtx, - 2*time.Minute, - env, - append([]string{cmdSSH}, - append(options, "-O", "exit", host)...)...) + fixtureCommand(cleanupCtx, 2*time.Minute, env, sshArgv(options, "-O", "exit", host)...) } ginkgo.DeferCleanup(func(specCtx ginkgo.SpecContext) { cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(specCtx), time.Minute) @@ -375,12 +368,7 @@ func verifyKindSSHAgentCleanup( } }) remote := func(command string) string { - return fixtureCommand( - ctx, - 2*time.Minute, - env, - append([]string{cmdSSH}, - append(options, host, "--", command)...)...) + return fixtureCommand(ctx, 2*time.Minute, env, sshArgv(options, host, "--", command)...) } socket := strings.TrimSpace(remote("printf %s \"$SSH_AUTH_SOCK\"")) gomega.Expect(socket).NotTo(gomega.BeEmpty()) diff --git a/e2e/tests/up/provider_podman_rootful_config.go b/e2e/tests/up/provider_podman_rootful_config.go index 199da707e2..fa2140690f 100644 --- a/e2e/tests/up/provider_podman_rootful_config.go +++ b/e2e/tests/up/provider_podman_rootful_config.go @@ -39,11 +39,12 @@ var _ = ginkgo.Describe( ginkgo.It("should substitute variables", func(ctx context.Context) { tempDir, err := setupWorkspaceAndUp( ctx, - "tests/up/testdata/docker-variables", - initialDir, f, - "--init-env", "CUSTOM_VAR=custom_value", - "--init-env", "CUSTOM_IMAGE=ghcr.io/devsy-org/test-images/base:alpine", + workspaceSource{"tests/up/testdata/docker-variables", initialDir}, + "--init-env", + "CUSTOM_VAR=custom_value", + "--init-env", + "CUSTOM_IMAGE=ghcr.io/devsy-org/test-images/base:alpine", ) framework.ExpectNoError(err) @@ -100,9 +101,8 @@ var _ = ginkgo.Describe( ginkgo.It("should substitute variables with defaults", func(ctx context.Context) { tempDir, err := setupWorkspaceAndUp( ctx, - "tests/up/testdata/docker-variables-defaults", - initialDir, f, + workspaceSource{"tests/up/testdata/docker-variables-defaults", initialDir}, ) framework.ExpectNoError(err) @@ -195,48 +195,89 @@ var _ = ginkgo.Describe( ginkgo.SpecTimeout(framework.TimeoutModerate()), ) - ginkgo.It("should select from multiple devcontainers", func(ctx context.Context) { - tempDir, err := setupWorkspace( - "tests/up/testdata/docker-multi-devcontainer", - initialDir, - f, - ) - framework.ExpectNoError(err) + ginkgo.It( //nolint:dupl // rootful and rootless suites mirror each other + "should select from multiple devcontainers", + func(ctx context.Context) { + tempDir, err := setupWorkspace( + "tests/up/testdata/docker-multi-devcontainer", + initialDir, + f, + ) + framework.ExpectNoError(err) - err = f.DevsyUp( - ctx, - tempDir, - names.Flag(names.DevContainer), - "id:python", - ) - framework.ExpectNoError(err) + err = f.DevsyUp( + ctx, + tempDir, + names.Flag(names.DevContainer), + "id:python", + ) + framework.ExpectNoError(err) - out, err := f.DevsySSH( - ctx, tempDir, "bash -l -c 'echo -n $DEVCONTAINER_TYPE'", - ) - framework.ExpectNoError(err) - framework.ExpectEqual(out, "python") + out, err := f.DevsySSH( + ctx, tempDir, "bash -l -c 'echo -n $DEVCONTAINER_TYPE'", + ) + framework.ExpectNoError(err) + framework.ExpectEqual(out, "python") - err = f.DevsyWorkspaceDelete(ctx, tempDir) - framework.ExpectNoError(err) + err = f.DevsyWorkspaceDelete(ctx, tempDir) + framework.ExpectNoError(err) - err = f.DevsyUp(ctx, tempDir, names.Flag(names.DevContainer), "id:go") - framework.ExpectNoError(err) + err = f.DevsyUp(ctx, tempDir, names.Flag(names.DevContainer), "id:go") + framework.ExpectNoError(err) - out, err = f.DevsySSH( - ctx, tempDir, "bash -l -c 'echo -n $DEVCONTAINER_TYPE'", - ) - framework.ExpectNoError(err) - framework.ExpectEqual(out, "go") + out, err = f.DevsySSH( + ctx, tempDir, "bash -l -c 'echo -n $DEVCONTAINER_TYPE'", + ) + framework.ExpectNoError(err) + framework.ExpectEqual(out, "go") - err = f.DevsyWorkspaceDelete(ctx, tempDir) - framework.ExpectNoError(err) - }, ginkgo.SpecTimeout(framework.TimeoutModerate())) + err = f.DevsyWorkspaceDelete(ctx, tempDir) + framework.ExpectNoError(err) + }, + ginkgo.SpecTimeout(framework.TimeoutModerate()), + ) }) }) }, ) +// rootfulSSHProbe counts SSH probe timeouts against the rootful podman runtime. +type rootfulSSHProbe struct { + dockerCommand string + consecutiveTimeouts int +} + +// failure classifies a failed SSH probe. It stops the wait only when the +// runtime is unavailable, either reported directly or found unhealthy after +// two consecutive timeouts. +func (p *rootfulSSHProbe) failure(ctx, probeCtx context.Context, err error) (bool, error) { + if errors.Is(err, docker.ErrRuntimeUnavailable) { + return false, gomega.StopTrying("SSH_RUNTIME_UNAVAILABLE").Wrap(err) + } + if !errors.Is(probeCtx.Err(), context.DeadlineExceeded) || ctx.Err() != nil { + p.consecutiveTimeouts = 0 + return false, nil + } + p.consecutiveTimeouts++ + if p.consecutiveTimeouts == 2 { + p.consecutiveTimeouts = 0 + healthErr := checkRootfulRuntimeHealth(ctx, p.dockerCommand) + if errors.Is(healthErr, docker.ErrRuntimeUnavailable) { + return false, gomega.StopTrying("SSH_RUNTIME_UNAVAILABLE").Wrap(healthErr) + } + } + + return false, nil +} + +func checkRootfulRuntimeHealth(ctx context.Context, dockerCommand string) error { + healthCtx, healthCancel := context.WithTimeout(ctx, 10*time.Second) + defer healthCancel() + helper := &docker.DockerHelper{DockerCommand: dockerCommand} + + return helper.CheckRuntimeHealth(healthCtx) +} + func eventuallySSH(f *framework.Framework, ctx context.Context, workspace, command string) string { const ( sshReadinessProbeTimeout = 8 * time.Second @@ -244,36 +285,20 @@ func eventuallySSH(f *framework.Framework, ctx context.Context, workspace, comma sshReadinessPollInterval = 2 * time.Second ) var output string - consecutiveTimeouts := 0 + probe := &rootfulSSHProbe{ + dockerCommand: filepath.Join(f.DevsyBinDir, podmanRootfulWrapperName), + } gomega.Eventually(func() (bool, error) { probeCtx, cancel := context.WithTimeout(ctx, sshReadinessProbeTimeout) defer cancel() out, err := f.DevsySSHOnce(probeCtx, workspace, command) if err != nil { - if errors.Is(err, docker.ErrRuntimeUnavailable) { - return false, gomega.StopTrying("SSH_RUNTIME_UNAVAILABLE").Wrap(err) - } - if errors.Is(probeCtx.Err(), context.DeadlineExceeded) && ctx.Err() == nil { - consecutiveTimeouts++ - if consecutiveTimeouts == 2 { - healthCtx, healthCancel := context.WithTimeout(ctx, 10*time.Second) - helper := &docker.DockerHelper{ - DockerCommand: filepath.Join(f.DevsyBinDir, podmanRootfulWrapperName), - } - healthErr := helper.CheckRuntimeHealth(healthCtx) - healthCancel() - consecutiveTimeouts = 0 - if errors.Is(healthErr, docker.ErrRuntimeUnavailable) { - return false, gomega.StopTrying("SSH_RUNTIME_UNAVAILABLE").Wrap(healthErr) - } - } - } else { - consecutiveTimeouts = 0 - } - return false, nil + return probe.failure(ctx, probeCtx, err) } output = strings.TrimSpace(out) + return true, nil }).WithTimeout(sshReadinessOverallTimeout).WithPolling(sshReadinessPollInterval).Should(gomega.BeTrue()) + return output } diff --git a/e2e/tests/up/provider_podman_rootful_features.go b/e2e/tests/up/provider_podman_rootful_features.go index ea867594ea..2c50609a34 100644 --- a/e2e/tests/up/provider_podman_rootful_features.go +++ b/e2e/tests/up/provider_podman_rootful_features.go @@ -33,9 +33,8 @@ var _ = ginkgo.Describe( ginkgo.It("should mount volumes", func(ctx context.Context) { tempDir, err := setupWorkspaceAndUp( ctx, - "tests/up/testdata/docker-mounts", - initialDir, f, + workspaceSource{"tests/up/testdata/docker-mounts", initialDir}, "--debug", ) framework.ExpectNoError(err) @@ -52,9 +51,8 @@ var _ = ginkgo.Describe( ginkgo.It("should use custom image", func(ctx context.Context) { tempDir, err := setupWorkspaceAndUp( ctx, - "tests/up/testdata/docker", - initialDir, f, + workspaceSource{"tests/up/testdata/docker", initialDir}, "--devcontainer-image", "ghcr.io/devsy-org/test-images/base:alpine", ) @@ -68,9 +66,11 @@ var _ = ginkgo.Describe( ginkgo.It("should skip build with custom image", func(ctx context.Context) { tempDir, err := setupWorkspaceAndUp( ctx, - "tests/up/testdata/docker-with-multi-stage-build", - initialDir, f, + workspaceSource{ + "tests/up/testdata/docker-with-multi-stage-build", + initialDir, + }, "--devcontainer-image", "ghcr.io/devsy-org/test-images/base:alpine", ) diff --git a/e2e/tests/up/provider_podman_rootful_lifecycle.go b/e2e/tests/up/provider_podman_rootful_lifecycle.go index 583384330e..ad7892db02 100644 --- a/e2e/tests/up/provider_podman_rootful_lifecycle.go +++ b/e2e/tests/up/provider_podman_rootful_lifecycle.go @@ -95,7 +95,7 @@ var _ = ginkgo.Describe( "postStartCommand should have run again after restart") }, ginkgo.SpecTimeout(framework.TimeoutModerate())) - ginkgo.It( + ginkgo.It( //nolint:dupl // rootful and rootless lifecycle suites mirror each other "should defer postCreateCommand to background with waitFor", func(ctx context.Context) { tempDir, err := setupWorkspace( diff --git a/e2e/tests/up/provider_podman_rootful_lifecycle_2.go b/e2e/tests/up/provider_podman_rootful_lifecycle_2.go index f4b8c94b73..045a6c3014 100644 --- a/e2e/tests/up/provider_podman_rootful_lifecycle_2.go +++ b/e2e/tests/up/provider_podman_rootful_lifecycle_2.go @@ -86,9 +86,11 @@ var _ = ginkgo.Describe( func(ctx context.Context) { tempDir, err := setupWorkspaceAndUp( ctx, - "tests/up/testdata/docker-initcmd-parallel", - initialDir, f, + workspaceSource{ + "tests/up/testdata/docker-initcmd-parallel", + initialDir, + }, ) framework.ExpectNoError(err) diff --git a/e2e/tests/up/provider_podman_rootless_config.go b/e2e/tests/up/provider_podman_rootless_config.go index fca84fa485..0a06a14f07 100644 --- a/e2e/tests/up/provider_podman_rootless_config.go +++ b/e2e/tests/up/provider_podman_rootless_config.go @@ -43,11 +43,12 @@ var _ = ginkgo.Describe( ginkgo.It("should substitute variables", func(ctx context.Context) { tempDir, err := setupWorkspaceAndUp( ctx, - "tests/up/testdata/docker-variables", - initialDir, f, - "--init-env", "CUSTOM_VAR=custom_value", - "--init-env", "CUSTOM_IMAGE=ghcr.io/devsy-org/test-images/base:alpine", + workspaceSource{"tests/up/testdata/docker-variables", initialDir}, + "--init-env", + "CUSTOM_VAR=custom_value", + "--init-env", + "CUSTOM_IMAGE=ghcr.io/devsy-org/test-images/base:alpine", ) framework.ExpectNoError(err) @@ -115,9 +116,8 @@ var _ = ginkgo.Describe( ginkgo.It("should substitute variables with defaults", func(ctx context.Context) { tempDir, err := setupWorkspaceAndUp( ctx, - "tests/up/testdata/docker-variables-defaults", - initialDir, f, + workspaceSource{"tests/up/testdata/docker-variables-defaults", initialDir}, ) framework.ExpectNoError(err) @@ -194,43 +194,47 @@ var _ = ginkgo.Describe( ginkgo.SpecTimeout(framework.TimeoutModerate()), ) - ginkgo.It("should select from multiple devcontainers", func(ctx context.Context) { - tempDir, err := setupWorkspace( - "tests/up/testdata/docker-multi-devcontainer", - initialDir, - f, - ) - framework.ExpectNoError(err) + ginkgo.It( //nolint:dupl // rootful and rootless suites mirror each other + "should select from multiple devcontainers", + func(ctx context.Context) { + tempDir, err := setupWorkspace( + "tests/up/testdata/docker-multi-devcontainer", + initialDir, + f, + ) + framework.ExpectNoError(err) - err = f.DevsyUp( - ctx, - tempDir, - names.Flag(names.DevContainer), - "id:python", - ) - framework.ExpectNoError(err) + err = f.DevsyUp( + ctx, + tempDir, + names.Flag(names.DevContainer), + "id:python", + ) + framework.ExpectNoError(err) - out, err := f.DevsySSH( - ctx, tempDir, "bash -l -c 'echo -n $DEVCONTAINER_TYPE'", - ) - framework.ExpectNoError(err) - framework.ExpectEqual(out, "python") + out, err := f.DevsySSH( + ctx, tempDir, "bash -l -c 'echo -n $DEVCONTAINER_TYPE'", + ) + framework.ExpectNoError(err) + framework.ExpectEqual(out, "python") - err = f.DevsyWorkspaceDelete(ctx, tempDir) - framework.ExpectNoError(err) + err = f.DevsyWorkspaceDelete(ctx, tempDir) + framework.ExpectNoError(err) - err = f.DevsyUp(ctx, tempDir, names.Flag(names.DevContainer), "id:go") - framework.ExpectNoError(err) + err = f.DevsyUp(ctx, tempDir, names.Flag(names.DevContainer), "id:go") + framework.ExpectNoError(err) - out, err = f.DevsySSH( - ctx, tempDir, "bash -l -c 'echo -n $DEVCONTAINER_TYPE'", - ) - framework.ExpectNoError(err) - framework.ExpectEqual(out, "go") + out, err = f.DevsySSH( + ctx, tempDir, "bash -l -c 'echo -n $DEVCONTAINER_TYPE'", + ) + framework.ExpectNoError(err) + framework.ExpectEqual(out, "go") - err = f.DevsyWorkspaceDelete(ctx, tempDir) - framework.ExpectNoError(err) - }, ginkgo.SpecTimeout(framework.TimeoutModerate())) + err = f.DevsyWorkspaceDelete(ctx, tempDir) + framework.ExpectNoError(err) + }, + ginkgo.SpecTimeout(framework.TimeoutModerate()), + ) ginkgo.It( "should preserve localEnv expressions in build metadata", @@ -270,9 +274,11 @@ var _ = ginkgo.Describe( tempDir, err := setupWorkspaceAndUp( ctx, - "tests/up/testdata/podman-local-env-metadata", - initialDir, f, + workspaceSource{ + "tests/up/testdata/podman-local-env-metadata", + initialDir, + }, ) framework.ExpectNoError(err) diff --git a/e2e/tests/up/provider_podman_rootless_features.go b/e2e/tests/up/provider_podman_rootless_features.go index 272e2c96e4..d0e1227aa0 100644 --- a/e2e/tests/up/provider_podman_rootless_features.go +++ b/e2e/tests/up/provider_podman_rootless_features.go @@ -35,9 +35,8 @@ var _ = ginkgo.Describe( ginkgo.It("should mount volumes", func(ctx context.Context) { tempDir, err := setupWorkspaceAndUp( ctx, - "tests/up/testdata/docker-mounts", - initialDir, f, + workspaceSource{"tests/up/testdata/docker-mounts", initialDir}, "--debug", ) framework.ExpectNoError(err) @@ -54,9 +53,8 @@ var _ = ginkgo.Describe( ginkgo.It("should use custom image", func(ctx context.Context) { tempDir, err := setupWorkspaceAndUp( ctx, - "tests/up/testdata/docker", - initialDir, f, + workspaceSource{"tests/up/testdata/docker", initialDir}, "--devcontainer-image", "ghcr.io/devsy-org/test-images/base:alpine", ) @@ -70,9 +68,11 @@ var _ = ginkgo.Describe( ginkgo.It("should skip build with custom image", func(ctx context.Context) { tempDir, err := setupWorkspaceAndUp( ctx, - "tests/up/testdata/docker-with-multi-stage-build", - initialDir, f, + workspaceSource{ + "tests/up/testdata/docker-with-multi-stage-build", + initialDir, + }, "--devcontainer-image", "ghcr.io/devsy-org/test-images/base:alpine", ) diff --git a/e2e/tests/up/provider_podman_rootless_lifecycle.go b/e2e/tests/up/provider_podman_rootless_lifecycle.go index 98158a015f..3f64c51759 100644 --- a/e2e/tests/up/provider_podman_rootless_lifecycle.go +++ b/e2e/tests/up/provider_podman_rootless_lifecycle.go @@ -88,7 +88,7 @@ var _ = ginkgo.Describe( "postStartCommand should have run again after restart") }, ginkgo.SpecTimeout(framework.TimeoutModerate())) - ginkgo.It( + ginkgo.It( //nolint:dupl // rootful and rootless lifecycle suites mirror each other "should defer postCreateCommand to background with waitFor", func(ctx context.Context) { tempDir, err := setupWorkspace( @@ -224,9 +224,11 @@ var _ = ginkgo.Describe( func(ctx context.Context) { tempDir, err := setupWorkspaceAndUp( ctx, - "tests/up/testdata/docker-initcmd-parallel", - initialDir, f, + workspaceSource{ + "tests/up/testdata/docker-initcmd-parallel", + initialDir, + }, ) framework.ExpectNoError(err) @@ -245,7 +247,7 @@ var _ = ginkgo.Describe( ginkgo.SpecTimeout(framework.TimeoutShort()), ) - ginkgo.It( + ginkgo.It( //nolint:dupl // rootful and rootless lifecycle suites mirror each other "should inject secrets-file env into lifecycle commands", func(ctx context.Context) { tempDir, err := setupWorkspace( diff --git a/e2e/tests/up/up.go b/e2e/tests/up/up.go index dbf7d2f504..0e3c68c9e1 100644 --- a/e2e/tests/up/up.go +++ b/e2e/tests/up/up.go @@ -36,11 +36,11 @@ var _ = ginkgo.Describe("testing up command", ginkgo.Label("up-workspaces"), fun initialDir, err = os.Getwd() framework.ExpectNoError(err) - dockerHelper = &docker.DockerHelper{DockerCommand: "docker"} + dockerHelper = &docker.DockerHelper{DockerCommand: dockerValue} }) ginkgo.It("with env vars", func(ctx context.Context) { - f, err := framework.SetupDockerProvider(filepath.Join(initialDir, "bin"), "docker") + f, err := framework.SetupDockerProvider(filepath.Join(initialDir, "bin"), dockerValue) framework.ExpectNoError(err) name := "vscode-remote-try-python" @@ -93,7 +93,7 @@ var _ = ginkgo.Describe("testing up command", ginkgo.Label("up-workspaces"), fun workspaceEnvFileInvalid := filepath.Join(tmpDir, ".invalid") err = os.WriteFile( workspaceEnvFileInvalid, - invalidData, 0o644) + invalidData, 0o600) framework.ExpectNoError(err) // set env var @@ -103,6 +103,7 @@ var _ = ginkgo.Describe("testing up command", ginkgo.Label("up-workspaces"), fun // create valid env file validData := []byte("TEST_VAR=" + value) workspaceEnvFileValid := filepath.Join(tmpDir, ".valid") + //nolint:gosec // test fixture file, world-readable is fine for the test err = os.WriteFile( workspaceEnvFileValid, validData, 0o644) @@ -129,6 +130,7 @@ var _ = ginkgo.Describe("testing up command", ginkgo.Label("up-workspaces"), fun // create a second valid env file with a different env var validData = []byte("TEST_OTHER_VAR=" + value) workspaceEnvFileValid2 := filepath.Join(tmpDir, ".valid2") + //nolint:gosec // test fixture file, world-readable is fine for the test err = os.WriteFile( workspaceEnvFileValid2, validData, 0o644) @@ -156,7 +158,7 @@ var _ = ginkgo.Describe("testing up command", ginkgo.Label("up-workspaces"), fun ginkgo.It("suppresses up progress while retaining workspace events", func(ctx context.Context) { - f, err := framework.SetupDockerProvider(filepath.Join(initialDir, "bin"), "docker") + f, err := framework.SetupDockerProvider(filepath.Join(initialDir, "bin"), dockerValue) framework.ExpectNoError(err) tempDir, err := framework.CopyToTempDir("tests/up/testdata/no-devcontainer") @@ -204,7 +206,7 @@ var _ = ginkgo.Describe("testing up command", ginkgo.Label("up-workspaces"), fun ginkgo.DeferCleanup(framework.CleanupTempDir, initialDir, tempDir) // provider add, use and delete afterwards - err = f.DevsyProviderAdd(ctx, "docker", "--name", providerName) + err = f.DevsyProviderAdd(ctx, dockerValue, "--name", providerName) framework.ExpectNoError(err) err = f.DevsyProviderUse(ctx, providerName) framework.ExpectNoError(err) @@ -292,7 +294,7 @@ var _ = ginkgo.Describe("testing up command", ginkgo.Label("up-workspaces"), fun ginkgo.DeferCleanup(framework.CleanupTempDir, initialDir, tempDir) // provider add, use and delete afterwards - err = f.DevsyProviderAdd(ctx, "docker", "--name", providerName) + err = f.DevsyProviderAdd(ctx, dockerValue, "--name", providerName) framework.ExpectNoError(err) err = f.DevsyProviderUse(ctx, providerName) framework.ExpectNoError(err) @@ -318,7 +320,7 @@ var _ = ginkgo.Describe("testing up command", ginkgo.Label("up-workspaces"), fun f := framework.NewDefaultFramework(initialDir + "/bin") // provider add, use and delete afterwards - err := f.DevsyProviderAdd(ctx, "docker", "--name", providerName) + err := f.DevsyProviderAdd(ctx, dockerValue, "--name", providerName) framework.ExpectNoError(err) err = f.DevsyProviderUse(ctx, providerName) framework.ExpectNoError(err) @@ -357,7 +359,7 @@ var _ = ginkgo.Describe("testing up command", ginkgo.Label("up-workspaces"), fun f := framework.NewDefaultFramework(initialDir + "/bin") // provider add, use and delete afterwards - err := f.DevsyProviderAdd(ctx, "docker", "--name", providerName) + err := f.DevsyProviderAdd(ctx, dockerValue, "--name", providerName) framework.ExpectNoError(err) err = f.DevsyProviderUse(ctx, providerName) framework.ExpectNoError(err) diff --git a/hack/ci/suites.yaml b/hack/ci/suites.yaml index f85cef8525..9c1e8e76e3 100644 --- a/hack/ci/suites.yaml +++ b/hack/ci/suites.yaml @@ -174,6 +174,8 @@ e2e: reason: sub-label that always accompanies the up-provider-kubernetes suite label - label: up-provider-kubernetes-image-config-file reason: sub-label that always accompanies the up-provider-kubernetes suite label + - label: setup-logs + reason: sub-label that always accompanies the logs suite label suites: # --- unprivileged --- diff --git a/hack/podman_ci/runner.go b/hack/podman_ci/runner.go index a6fa699258..f2b3f0cd8b 100644 --- a/hack/podman_ci/runner.go +++ b/hack/podman_ci/runner.go @@ -73,28 +73,35 @@ func (r *ExecRunner) Run(ctx context.Context, spec CommandSpec) CommandResult { Stderr: stderr, } if waitResult.timedOut { - result.Stderr += fmt.Sprintf( - "\nPODMAN_COMMAND_TIMEOUT: direct_process_stopped=%t", - waitResult.stopped, - ) - if !waitResult.stopped { - result.Stderr += "\nPODMAN_TERMINATION_FAILED" - } + result.Stderr += timeoutNotice(waitResult.stopped) return result } - code := 0 - if waitResult.err != nil { - if exitErr, ok := errors.AsType[*exec.ExitError](waitResult.err); ok { - code = exitErr.ExitCode() - } else { - code = -1 - result.Stderr += "\n" + waitResult.err.Error() - } - } + code := exitCodeOf(waitResult.err, &result) result.ExitCode = &code return result } +func timeoutNotice(stopped bool) string { + notice := fmt.Sprintf("\nPODMAN_COMMAND_TIMEOUT: direct_process_stopped=%t", stopped) + if !stopped { + notice += "\nPODMAN_TERMINATION_FAILED" + } + return notice +} + +// exitCodeOf returns the process exit code for err, appending non-exit errors +// to the captured stderr. +func exitCodeOf(err error, result *CommandResult) int { + if err == nil { + return 0 + } + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { + return exitErr.ExitCode() + } + result.Stderr += "\n" + err.Error() + return -1 +} + type commandCapture struct { stdout *os.File stderr *os.File diff --git a/pkg/agent/agent.go b/pkg/agent/agent.go index b2cd28410c..9b8e354d75 100644 --- a/pkg/agent/agent.go +++ b/pkg/agent/agent.go @@ -72,6 +72,7 @@ func readAgentWorkspaceInfo( func ParseAgentWorkspaceInfo(workspaceConfigFile string) (*provider2.AgentWorkspaceInfo, error) { // read workspace config + //nolint:gosec // path is inside the agent layout out, err := os.ReadFile(workspaceConfigFile) if err != nil { return nil, err @@ -134,6 +135,17 @@ func ReadAgentWorkspaceInfo( return false, workspaceInfo, nil } +// ReadExistingAgentWorkspaceInfo reads already persisted workspace info and +// nothing else. Unlike ReadAgentWorkspaceInfo it never re-runs as root, probes +// Docker or invokes sudo, so a read-only history view cannot prompt for +// elevation or provision state. A missing or unreadable file is an error the +// caller reports as unavailable. +func ReadExistingAgentWorkspaceInfo( + agentFolder, context, id string, +) (*provider2.AgentWorkspaceInfo, error) { + return readAgentWorkspaceInfo(agentFolder, context, id) +} + func logWorkspaceInfoReadError(err error, agentFolder, context, id string) { if errors.Is(err, ErrFindAgentHomeDir) { log.Debugf( @@ -317,6 +329,7 @@ func CreateWorkspaceBusyFile(folder string) { func HasWorkspaceBusyFile(folder string) bool { filePath := filepath.Join(folder, config.WorkspaceBusyFile) + //nolint:gosec // paths come from the internal agent layout _, err := os.Stat(filePath) return err == nil } @@ -391,7 +404,10 @@ func rerunAsRoot(workspaceInfo *provider2.AgentWorkspaceInfo) (bool, error) { args := []string{"--preserve-env", binary} args = append(args, os.Args[1:]...) log.Debugf("re-run as root: command=%s", strings.Join(args, " ")) - cmd := exec.Command("sudo", args...) + //nolint:gosec // sudo args are built internally from fixed flags + cmd := exec.Command( + "sudo", + args...) //nolint:gosec // sudo args are built internally from fixed flags cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr @@ -497,17 +513,6 @@ func sshServerCommand(agentPath string, debug bool) string { return shellescape.QuoteCommand(args) } -func applyDockerEnv(cmd *exec.Cmd, envs map[string]string) { - if len(envs) == 0 { - return - } - newEnvs := os.Environ() - for k, v := range envs { - newEnvs = append(newEnvs, k+"="+v) - } - cmd.Env = newEnvs -} - func dockerReachable(dockerOverride string, envs map[string]string) (bool, error) { docker := "docker" if dockerOverride != "" { @@ -534,18 +539,21 @@ func dockerReachable(dockerOverride string, envs map[string]string) (bool, error Redactor: secrets.NewEnvironmentRedactor(redactionEnv), }) if err != nil { - details := result.DiagnosticOutput() - if strings.Contains(strings.ToLower(details+" "+err.Error()), "permission denied") { - if dockerOverride == "" { - return true, nil - } - } - - if details != "" { - return false, fmt.Errorf("%s ps: %s: %w", docker, details, err) - } - return false, fmt.Errorf("%s ps: %w", docker, err) + return dockerPSError(docker, dockerOverride, result.DiagnosticOutput(), err) } return false, nil } + +// dockerPSError classifies a failed `docker ps`: a permission error on the +// default docker needs root to fix; anything else is returned to the caller. +func dockerPSError(docker, dockerOverride, details string, err error) (bool, error) { + if strings.Contains(strings.ToLower(details+" "+err.Error()), "permission denied") && + dockerOverride == "" { + return true, nil + } + if details != "" { + return false, fmt.Errorf("%s ps: %s: %w", docker, details, err) + } + return false, fmt.Errorf("%s ps: %w", docker, err) +} diff --git a/pkg/agent/delivery/exec_stream_cancellation_unix_test.go b/pkg/agent/delivery/exec_stream_cancellation_unix_test.go index 528b4930a4..0a419a0cc3 100644 --- a/pkg/agent/delivery/exec_stream_cancellation_unix_test.go +++ b/pkg/agent/delivery/exec_stream_cancellation_unix_test.go @@ -192,6 +192,30 @@ func awaitDeliveryRead(t *testing.T, sources *cancellationBinarySource) { } } +// awaitAttemptCancellation requires the attempt to be canceled with cause, which +// may arrive after delivery already returned. +func awaitAttemptCancellation( + t *testing.T, + canceled <-chan error, + call *cancellationDeliveryCall, + cause error, +) { + t.Helper() + select { + case got := <-canceled: + require.ErrorIs(t, got, cause) + case <-call.done: + select { + case got := <-canceled: + require.ErrorIs(t, got, cause) + default: + t.Fatalf("delivery returned before attempt cancellation: %v", call.err) + } + case <-time.After(execStreamCancellationTestBound): + t.Fatal("actual attempt cancellation was not observed") + } +} + func TestExecStreamAttemptCancellationOwnsSource(t *testing.T) { for _, testCase := range []struct { name string @@ -221,19 +245,7 @@ func TestExecStreamAttemptCancellationOwnsSource(t *testing.T) { outer, call := startCancellationDelivery(t, delivery, sources) awaitDeliveryRead(t, sources) if testCase.cause != nil { - select { - case cause := <-canceled: - require.ErrorIs(t, cause, testCase.cause) - case <-call.done: - select { - case cause := <-canceled: - require.ErrorIs(t, cause, testCase.cause) - default: - t.Fatalf("delivery returned before attempt cancellation: %v", call.err) - } - case <-time.After(execStreamCancellationTestBound): - t.Fatal("actual attempt cancellation was not observed") - } + awaitAttemptCancellation(t, canceled, call, testCase.cause) } select { case <-call.done: diff --git a/pkg/agent/delivery/kubernetes.go b/pkg/agent/delivery/kubernetes.go index 13b50dc521..4ee83e4022 100644 --- a/pkg/agent/delivery/kubernetes.go +++ b/pkg/agent/delivery/kubernetes.go @@ -80,16 +80,8 @@ func (d *KubernetesDelivery) DeliverPostStart(ctx context.Context, opts PostStar destPath := d.destPath() - // Skip delivery when the in-pod binary already matches. - if !opts.SkipVersionCheck { - expected := d.expectedVersion() - if actual := d.detectVersion(ctx, destPath); actual != "" && actual == expected { - log.Debugf( - "remote agent version matches expected version %s, skipping delivery", - expected, - ) - return nil - } + if !opts.SkipVersionCheck && d.versionMatches(ctx, destPath) { + return nil } var downloadErr error @@ -129,6 +121,17 @@ func (d *KubernetesDelivery) Cleanup(_ context.Context, _ string) error { return nil } +// versionMatches reports whether the in-pod binary already has the expected version. +func (d *KubernetesDelivery) versionMatches(ctx context.Context, destPath string) bool { + expected := d.expectedVersion() + actual := d.detectVersion(ctx, destPath) + if actual == "" || actual != expected { + return false + } + log.Debugf("remote agent version matches expected version %s, skipping delivery", expected) + return true +} + func (d *KubernetesDelivery) deliverViaDownload( ctx context.Context, destPath, downloadURL, arch string, @@ -530,16 +533,20 @@ func isTransientDeliveryError(err error) bool { if _, ok := errors.AsType[*permanentDeliveryError](err); ok { return false } - if errors.Is(err, context.DeadlineExceeded) { - return true - } - if errors.Is(err, errExecStreamIdleTimeout) || errors.Is(err, errExecStreamCompletionTimeout) { - return true - } - if _, ok := errors.AsType[net.Error](err); ok { + return isTimeoutError(err) || hasTransientMarker(err.Error()) +} + +func isTimeoutError(err error) bool { + if errors.Is(err, context.DeadlineExceeded) || + errors.Is(err, errExecStreamIdleTimeout) || + errors.Is(err, errExecStreamCompletionTimeout) { return true } - msg := err.Error() + _, ok := errors.AsType[net.Error](err) + return ok +} + +func hasTransientMarker(msg string) bool { for _, marker := range []string{"i/o timeout", "broken pipe", "connection reset", "unexpected EOF"} { if strings.Contains(msg, marker) { return true diff --git a/pkg/agent/delivery/kubernetes_test.go b/pkg/agent/delivery/kubernetes_test.go index fda4626fad..a376ecc4b3 100644 --- a/pkg/agent/delivery/kubernetes_test.go +++ b/pkg/agent/delivery/kubernetes_test.go @@ -23,6 +23,11 @@ import ( execerr "k8s.io/client-go/util/exec" ) +const ( + testBinaryContent = "test-binary-content" + httpsExampleComReleases = "https://example.com/releases" +) + const testVersion = "v1.2.3" // recordingExec records each call, replays stdouts[N] as stdout, and returns @@ -92,7 +97,7 @@ func TestKubernetesDelivery_DeliverPostStart_RequiresExec(t *testing.T) { } func TestKubernetesDelivery_DeliverPostStart_WritesBinary(t *testing.T) { - binaryData := "test-binary-content" + binaryData := testBinaryContent exec := &recordingExec{stdouts: []string{""}} d := &KubernetesDelivery{Exec: exec.fn, ExpectedVersion: testVersion} @@ -176,7 +181,7 @@ func TestKubernetesDelivery_DeliverPostStart_PrefersDownloadOverExecStream(t *te err := d.DeliverPostStart(context.Background(), PostStartOptions{ BinarySource: binarySourceFrom("should-not-be-streamed"), Arch: testArch, - DownloadURL: "https://example.com/releases", + DownloadURL: httpsExampleComReleases, PreferInContainerDownload: true, }) require.NoError(t, err) @@ -189,7 +194,7 @@ func TestKubernetesDelivery_DeliverPostStart_PrefersDownloadOverExecStream(t *te } func TestKubernetesDelivery_DeliverPostStart_FallsBackToExecStreamWhenNoDownloadTool(t *testing.T) { - binaryData := "test-binary-content" + binaryData := testBinaryContent exec := &recordingExec{ stdouts: []string{""}, errs: []error{ @@ -205,7 +210,7 @@ func TestKubernetesDelivery_DeliverPostStart_FallsBackToExecStreamWhenNoDownload err := d.DeliverPostStart(context.Background(), PostStartOptions{ BinarySource: binarySourceFrom(binaryData), Arch: testArch, - DownloadURL: "https://example.com/releases", + DownloadURL: httpsExampleComReleases, PreferInContainerDownload: true, }) require.NoError(t, err) @@ -215,7 +220,7 @@ func TestKubernetesDelivery_DeliverPostStart_FallsBackToExecStreamWhenNoDownload } func TestKubernetesDelivery_DeliverPostStart_SkipsDownloadWhenNoURLConfigured(t *testing.T) { - binaryData := "test-binary-content" + binaryData := testBinaryContent exec := &recordingExec{stdouts: []string{""}} d := &KubernetesDelivery{Exec: exec.fn, ExpectedVersion: testVersion} @@ -391,7 +396,7 @@ func TestKubernetesDelivery_PreservesDownloadAndStreamErrors(t *testing.T) { err := d.DeliverPostStart(context.Background(), PostStartOptions{ BinarySource: binarySourceFrom("binary"), Arch: testArch, - DownloadURL: "https://example.com/releases", + DownloadURL: httpsExampleComReleases, PreferInContainerDownload: true, }) require.Error(t, err) @@ -438,7 +443,7 @@ func TestIsTransientDeliveryError(t *testing.T) { } func TestKubernetesDelivery_DeliverPostStart_UsesInstallPathOverride(t *testing.T) { - binaryData := "test-binary-content" + binaryData := testBinaryContent exec := &recordingExec{stdouts: []string{""}} installPath := testKubernetesInstallPath d := &KubernetesDelivery{Exec: exec.fn, ExpectedVersion: testVersion, InstallPath: installPath} diff --git a/pkg/agent/delivery/local_docker.go b/pkg/agent/delivery/local_docker.go index a202573082..154556de29 100644 --- a/pkg/agent/delivery/local_docker.go +++ b/pkg/agent/delivery/local_docker.go @@ -16,6 +16,7 @@ import ( pkgconfig "github.com/devsy-org/devsy/pkg/config" "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/docker" + "github.com/devsy-org/devsy/pkg/driver" "github.com/devsy-org/devsy/pkg/log" "github.com/devsy-org/devsy/pkg/version" ) @@ -49,14 +50,8 @@ func (d *LocalDockerDelivery) DeliverPreStart(ctx context.Context, opts PreStart if opts.BinarySource == nil { return fmt.Errorf("binary source is required for local docker delivery") } - if mount := opts.RunOptions.WorkspaceMount; mount != nil && - isAgentMountTarget(mount.Target) { - return fmt.Errorf("agent mount target %s is already in use", volumeMountPath) - } - for _, mount := range opts.RunOptions.Mounts { - if mount != nil && isAgentMountTarget(mount.Target) { - return fmt.Errorf("agent mount target %s is already in use", volumeMountPath) - } + if err := rejectAgentMountConflicts(opts.RunOptions); err != nil { + return err } volumeName := volumePrefix + opts.WorkspaceID @@ -84,6 +79,19 @@ func (d *LocalDockerDelivery) DeliverPreStart(ctx context.Context, opts PreStart return nil } +func rejectAgentMountConflicts(run *driver.RunOptions) error { + if mount := run.WorkspaceMount; mount != nil && isAgentMountTarget(mount.Target) { + return fmt.Errorf("agent mount target %s is already in use", volumeMountPath) + } + for _, mount := range run.Mounts { + if mount != nil && isAgentMountTarget(mount.Target) { + return fmt.Errorf("agent mount target %s is already in use", volumeMountPath) + } + } + + return nil +} + func isAgentMountTarget(target string) bool { cleaned := path.Clean(target) return cleaned == volumeMountPath || strings.HasPrefix(cleaned, volumeMountPath+"/") diff --git a/pkg/agent/dockerless.go b/pkg/agent/dockerless.go index 24711d9912..d3034e7640 100644 --- a/pkg/agent/dockerless.go +++ b/pkg/agent/dockerless.go @@ -48,6 +48,7 @@ func GetDockerlessBuildContext() string { } func ImageConfigExists(path string) bool { + //nolint:gosec // paths come from the internal agent layout _, err := os.Stat(path) return err == nil } @@ -273,6 +274,7 @@ func runDockerlessBuild(ctx context.Context, args []string, debug bool) error { var stderrBuf bytes.Buffer stderrWriter := io.MultiWriter(errWriter, &stderrBuf) + //nolint:gosec // command is built from the agent configuration by design cmd := exec.CommandContext(ctx, "/.dockerless/dockerless", args...) if debug { debugWriter := log.Writer(log.LevelDebug) diff --git a/pkg/agent/inject_test.go b/pkg/agent/inject_test.go index 39e223c0f1..30815f1506 100644 --- a/pkg/agent/inject_test.go +++ b/pkg/agent/inject_test.go @@ -8,6 +8,7 @@ import ( "time" "github.com/devsy-org/devsy/pkg/docker" + "github.com/devsy-org/devsy/pkg/inject" "github.com/stretchr/testify/suite" ) @@ -55,7 +56,7 @@ func (s *InjectTestSuite) SetupTest() { func (s *InjectTestSuite) TestLocalInjection() { opts := &InjectOptions{ - Exec: (&MockExecFunc{}).Exec, + Exec: stubExec(""), IsLocal: true, Command: "echo hello", } @@ -80,9 +81,9 @@ func (s *InjectTestSuite) TestVersionChecker() { remoteVersion: "v1.0.0", skipCheck: false, } - mockExec := &MockExecFunc{Output: "v1.0.0\n"} + exec := stubExec("v1.0.0\n") - detected, err := vc.detectRemoteAgentVersion(s.ctx, mockExec.Exec, "/path") + detected, err := vc.detectRemoteAgentVersion(s.ctx, exec, "/path") s.NoError(err) s.Equal("v1.0.0", detected) }) @@ -92,9 +93,9 @@ func (s *InjectTestSuite) TestVersionChecker() { remoteVersion: "v1.0.0", skipCheck: true, } - mockExec := &MockExecFunc{Output: "v0.9.0\n"} + exec := stubExec("v0.9.0\n") - detected, err := vc.detectRemoteAgentVersion(s.ctx, mockExec.Exec, "/path") + detected, err := vc.detectRemoteAgentVersion(s.ctx, exec, "/path") s.NoError(err) s.Equal("v0.9.0", detected) }) @@ -130,23 +131,17 @@ func (s *InjectTestSuite) TestVersionChecker_BoundedByVersionCheckTimeout() { s.ErrorContains(err, "timed out") } -// MockExecFunc is a helper for testing. -type MockExecFunc struct { - CapturedCmd string - Output string - Err error -} - -func (m *MockExecFunc) Exec( - ctx context.Context, - cmd string, - stdin io.Reader, - stdout io.Writer, - stderr io.Writer, -) error { - m.CapturedCmd = cmd - if stdout != nil { - _, _ = stdout.Write([]byte(m.Output)) +func stubExec(output string) inject.ExecFunc { + return func( + _ context.Context, + _ string, + _ io.Reader, + stdout io.Writer, + _ io.Writer, + ) error { + if stdout != nil { + _, _ = stdout.Write([]byte(output)) + } + return nil } - return m.Err } diff --git a/pkg/agent/tunnel/setup_log_sender.go b/pkg/agent/tunnel/setup_log_sender.go new file mode 100644 index 0000000000..95a21fce49 --- /dev/null +++ b/pkg/agent/tunnel/setup_log_sender.go @@ -0,0 +1,489 @@ +package tunnel + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "sync" + "sync/atomic" + "time" + + "github.com/devsy-org/devsy/pkg/workspacelogs" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +// Sender defaults. +const ( + defaultCleanupTimeout = 5 * time.Second + defaultCallTimeout = 10 * time.Second + defaultPollInterval = 500 * time.Millisecond + maxRetryBackoff = 5 * time.Second + flushRetryPause = 50 * time.Millisecond + // batchReserve keeps room for the request envelope inside one page. + batchReserve = 4 * 1024 +) + +// SetupLogSource supplies a producer's committed records. See +// workspacelogs.StoreSource. +type SetupLogSource interface { + ReadAfter(runID, producerID string, after uint64, maxBytes int) ([]workspacelogs.Record, error) +} + +// SetupLogSenderOptions configures a SetupLogSender. +type SetupLogSenderOptions struct { + Client TunnelClient + Source SetupLogSource + RunID string + ProducerID string + // Acks stores the acknowledged position so a restart resumes from it. When + // nil the position lives in memory only. + Acks *workspacelogs.AckStore + // Hop names the position kept in Acks. Defaults to HopUpstream. + Hop workspacelogs.Hop + // CleanupTimeout bounds Flush. CallTimeout bounds one RPC. + CleanupTimeout time.Duration + CallTimeout time.Duration + PollInterval time.Duration +} + +// SetupLogSender mirrors one producer's records from a source store to the +// next host. Records stay in the source spool; the sender only remembers how +// far the peer has acknowledged, so a stalled peer never blocks the producer +// and never grows memory. +type SetupLogSender struct { + opts SetupLogSenderOptions + ctx context.Context + cancel context.CancelFunc + wake chan struct{} + done chan struct{} + // drainLock admits one drain at a time and can be waited on with a context. + drainLock chan struct{} + + disabled atomic.Bool + incomplete atomic.Bool + + mu sync.Mutex + acked uint64 + inflight context.CancelFunc +} + +// NewSetupLogSender starts the background worker. Call Close when done. +func NewSetupLogSender(ctx context.Context, opts SetupLogSenderOptions) *SetupLogSender { + if opts.Hop == "" { + opts.Hop = workspacelogs.HopUpstream + } + if opts.CleanupTimeout <= 0 { + opts.CleanupTimeout = defaultCleanupTimeout + } + if opts.CallTimeout <= 0 { + opts.CallTimeout = defaultCallTimeout + } + if opts.PollInterval <= 0 { + opts.PollInterval = defaultPollInterval + } + s := &SetupLogSender{ + opts: opts, + wake: make(chan struct{}, 1), + done: make(chan struct{}), + drainLock: make(chan struct{}, 1), + } + s.ctx, s.cancel = context.WithCancel(ctx) + if opts.Acks != nil { + if st, err := opts.Acks.Load(s.ackKey()); err == nil { + s.acked = st.Acked + s.incomplete.Store(st.Gapped) + } + } + go s.worker() + + return s +} + +// Notify tells the sender new records are available. It never blocks; wake-ups +// coalesce into one. +func (s *SetupLogSender) Notify() { + select { + case s.wake <- struct{}{}: + default: + } +} + +// Acked returns the highest sequence the peer acknowledged. +func (s *SetupLogSender) Acked() uint64 { + s.mu.Lock() + defer s.mu.Unlock() + + return s.acked +} + +// Disabled reports that the peer cannot take records (older peer or rejected +// batch). The source spool is intact. +func (s *SetupLogSender) Disabled() bool { return s.disabled.Load() } + +// Close stops the background worker. +func (s *SetupLogSender) Close() { + s.cancel() + <-s.done +} + +// Flush makes a bounded final drain. It always uses its own cleanup deadline, +// even when ctx is already canceled, and reports whether every retained record +// was acknowledged without gaps. +func (s *SetupLogSender) Flush(ctx context.Context) bool { + if s.disabled.Load() { + return false + } + cleanup, cancel := cleanupContext(ctx, s.opts.CleanupTimeout) + defer cancel() + s.cancelInflight() + if !s.lock(cleanup) { + return false + } + defer s.unlock() + for !s.drainOnce(cleanup) { + if s.disabled.Load() { + return false + } + select { + case <-cleanup.Done(): + return false + case <-time.After(flushRetryPause): + } + } + + return !s.incomplete.Load() && !s.disabled.Load() +} + +// drainOnce returns true when the source is exhausted or the peer is lost. +func (s *SetupLogSender) drainOnce(ctx context.Context) bool { + done, err := s.drain(ctx) + + return err == nil && done || s.disabled.Load() +} + +// cleanupContext bounds a final drain by its own deadline, whether or not the +// parent is already canceled. +func cleanupContext( + parent context.Context, + timeout time.Duration, +) (context.Context, context.CancelFunc) { + if parent.Err() != nil { + parent = context.WithoutCancel(parent) + } + + return context.WithTimeout(parent, timeout) +} + +func (s *SetupLogSender) lock(ctx context.Context) bool { + select { + case s.drainLock <- struct{}{}: + return true + case <-ctx.Done(): + return false + } +} + +func (s *SetupLogSender) unlock() { <-s.drainLock } + +func (s *SetupLogSender) cancelInflight() { + s.mu.Lock() + cancel := s.inflight + s.mu.Unlock() + if cancel != nil { + cancel() + } +} + +func (s *SetupLogSender) worker() { + defer close(s.done) + ticker := time.NewTicker(s.opts.PollInterval) + defer ticker.Stop() + backoff := time.Duration(0) + for { + select { + case <-s.ctx.Done(): + return + case <-s.wake: + case <-ticker.C: + } + ok, err := s.workerDrain() + if !ok { + return + } + backoff = nextBackoff(backoff, err) + if backoff > 0 && !s.sleep(backoff) { + return + } + } +} + +// workerDrain runs one drain. ok is false when the worker should exit. +func (s *SetupLogSender) workerDrain() (bool, error) { + if s.disabled.Load() || !s.lock(s.ctx) { + return false, nil + } + defer s.unlock() + _, err := s.drain(s.ctx) + + return true, err +} + +func (s *SetupLogSender) sleep(d time.Duration) bool { + select { + case <-s.ctx.Done(): + return false + case <-time.After(d): + return true + } +} + +func nextBackoff(cur time.Duration, err error) time.Duration { + switch { + case err == nil: + return 0 + case cur == 0: + return 100 * time.Millisecond + default: + return min(cur*2, maxRetryBackoff) + } +} + +var errNoProgress = errors.New("peer acknowledged no new records") + +// drain sends batches until the source has nothing past the acknowledged +// position. It returns true when caught up. +func (s *SetupLogSender) drain(ctx context.Context) (bool, error) { + for { + if s.disabled.Load() { + return false, nil + } + records, err := s.opts.Source.ReadAfter( + s.opts.RunID, s.opts.ProducerID, s.Acked(), workspacelogs.MaxPageBytes-batchReserve, + ) + if errors.Is(err, workspacelogs.ErrSourceGap) { + // The local spool lost the next records; nothing can be mirrored past it. + s.incomplete.Store(true) + s.disabled.Store(true) + + return false, nil + } + if err != nil { + return false, fmt.Errorf("read source: %w", err) + } + if len(records) == 0 { + return true, nil + } + if err = s.send(ctx, records); err != nil { + return false, err + } + } +} + +func (s *SetupLogSender) send(ctx context.Context, records []workspacelogs.Record) error { + req, err := buildRequest(s.opts.RunID, s.opts.ProducerID, records) + if err != nil { + s.incomplete.Store(true) + s.disabled.Store(true) + + return err + } + callCtx, cancel := context.WithTimeout(ctx, s.opts.CallTimeout) + s.setInflight(cancel) + defer func() { + s.setInflight(nil) + cancel() + }() + resp, err := s.opts.Client.AppendSetupLogs(callCtx, req) + if status.Code(err) == codes.Unimplemented { + s.disabled.Store(true) + + return nil + } + if err != nil { + return err + } + + return s.apply(req, resp) +} + +func (s *SetupLogSender) ackKey() workspacelogs.AckKey { + return workspacelogs.AckKey{RunID: s.opts.RunID, ProducerID: s.opts.ProducerID, Hop: s.opts.Hop} +} + +func (s *SetupLogSender) setInflight(cancel context.CancelFunc) { + s.mu.Lock() + s.inflight = cancel + s.mu.Unlock() +} + +func buildRequest( + runID, producerID string, records []workspacelogs.Record, +) (*AppendSetupLogsRequest, error) { + req := &AppendSetupLogsRequest{ + SchemaVersion: workspacelogs.SchemaVersion, + RunId: runID, + ProducerId: producerID, + FirstSequence: records[0].Sequence, + LastSequence: records[len(records)-1].Sequence, + } + for i := range records { + data, err := json.Marshal(&records[i]) + if err != nil { + return nil, fmt.Errorf("encode record: %w", err) + } + req.Records = append(req.Records, data) + } + + return req, nil +} + +// apply moves the acknowledged position by the peer's answer. The position +// never passes what this request carried. +func (s *SetupLogSender) apply(req *AppendSetupLogsRequest, resp *AppendSetupLogsResponse) error { + accepted := min(resp.GetAcceptedSequence(), req.GetLastSequence()) + switch resp.GetResult() { + case SetupLogResult_SETUP_LOG_RESULT_ACCEPTED: + return s.advance(accepted) + case SetupLogResult_SETUP_LOG_RESULT_RETENTION_GAP: + s.incomplete.Store(true) + last := min(resp.GetGapLastSequence(), req.GetLastSequence()) + if s.opts.Acks != nil { + _ = s.opts.Acks.RecordGap(s.ackKey(), resp.GetGapFirstSequence(), last) + } + + return s.advance(max(accepted, last)) + default: + // Rejected or unknown: retrying the same bytes cannot succeed. + s.incomplete.Store(true) + s.disabled.Store(true) + + return nil + } +} + +func (s *SetupLogSender) advance(acked uint64) error { + s.mu.Lock() + moved := acked > s.acked + if moved { + s.acked = acked + } + s.mu.Unlock() + if !moved { + return errNoProgress + } + if s.opts.Acks != nil { + return s.opts.Acks.Advance(s.ackKey(), acked) + } + + return nil +} + +// SetupLogForwarderOptions configures a SetupLogForwarder. +type SetupLogForwarderOptions struct { + Client TunnelClient + Source SetupLogSource + RunID string + Acks *workspacelogs.AckStore + // CleanupTimeout bounds one Flush across every producer. + CleanupTimeout time.Duration + CallTimeout time.Duration + PollInterval time.Duration +} + +// SetupLogForwarder is the intermediate mode for the provider host. It sends +// the original records received from containers upstream with unchanged run, +// producer and sequence identity, and tracks the caller's acknowledged position +// separately from what its own store accepted. +type SetupLogForwarder struct { + opts SetupLogForwarderOptions + ctx context.Context + + mu sync.Mutex + senders map[string]*SetupLogSender + closed bool +} + +// NewSetupLogForwarder returns a forwarder whose senders live until ctx ends +// or Close is called. +func NewSetupLogForwarder(ctx context.Context, opts SetupLogForwarderOptions) *SetupLogForwarder { + return &SetupLogForwarder{ctx: ctx, opts: opts, senders: map[string]*SetupLogSender{}} +} + +// Notify is the Sink's OnAccepted hook: records of producerID are now in the +// local store and can go upstream. It never blocks. +func (f *SetupLogForwarder) Notify(producerID string, _ uint64) { + if s := f.sender(producerID); s != nil { + s.Notify() + } +} + +// Flush drains every producer upstream within one cleanup budget and reports +// whether the caller holds everything the provider holds. A failure at this +// hop is reported even though the provider's own store accepted the records. +func (f *SetupLogForwarder) Flush(ctx context.Context) bool { + timeout := f.opts.CleanupTimeout + if timeout <= 0 { + timeout = defaultCleanupTimeout + } + budget, cancel := cleanupContext(ctx, timeout) + defer cancel() + + f.mu.Lock() + senders := make([]*SetupLogSender, 0, len(f.senders)) + for _, s := range f.senders { + senders = append(senders, s) + } + f.mu.Unlock() + + var ( + wg sync.WaitGroup + failures atomic.Int32 + ) + for _, s := range senders { + wg.Go(func() { + if !s.Flush(budget) { + failures.Add(1) + } + }) + } + wg.Wait() + + return failures.Load() == 0 +} + +// Close stops every sender. +func (f *SetupLogForwarder) Close() { + f.mu.Lock() + f.closed = true + senders := f.senders + f.senders = map[string]*SetupLogSender{} + f.mu.Unlock() + for _, s := range senders { + s.Close() + } +} + +func (f *SetupLogForwarder) sender(producerID string) *SetupLogSender { + f.mu.Lock() + defer f.mu.Unlock() + if f.closed { + return nil + } + if s, ok := f.senders[producerID]; ok { + return s + } + if len(f.senders) >= workspacelogs.MaxProducers { + return nil + } + s := NewSetupLogSender(f.ctx, SetupLogSenderOptions{ + Client: f.opts.Client, Source: f.opts.Source, RunID: f.opts.RunID, + ProducerID: producerID, Acks: f.opts.Acks, Hop: workspacelogs.HopUpstream, + CleanupTimeout: f.opts.CleanupTimeout, CallTimeout: f.opts.CallTimeout, + PollInterval: f.opts.PollInterval, + }) + f.senders[producerID] = s + + return s +} diff --git a/pkg/agent/tunnel/setup_log_sender_test.go b/pkg/agent/tunnel/setup_log_sender_test.go new file mode 100644 index 0000000000..dc75608cb1 --- /dev/null +++ b/pkg/agent/tunnel/setup_log_sender_test.go @@ -0,0 +1,353 @@ +package tunnel_test + +import ( + "context" + "net" + "os" + "path/filepath" + "strings" + "sync/atomic" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "github.com/devsy-org/devsy/pkg/agent/tunnelserver" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/require" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/status" + "google.golang.org/grpc/test/bufconn" +) + +const testHook = "postCreateCommand" + +// peer is one host: a store, a sink and an in-process gRPC server in front of +// the real tunnel server. +type peer struct { + root string + store *workspacelogs.Store + acks *workspacelogs.AckStore + sink *workspacelogs.Sink + client tunnel.TunnelClient + runID string + gate chan struct{} + calls atomic.Int32 +} + +// gatedServer lets tests stall or hide the RPC in front of the real server. +type gatedServer struct { + tunnel.TunnelServer + p *peer + unimpl bool +} + +func (g *gatedServer) AppendSetupLogs( + ctx context.Context, req *tunnel.AppendSetupLogsRequest, +) (*tunnel.AppendSetupLogsResponse, error) { + g.p.calls.Add(1) + if g.unimpl { + return nil, status.Error(codes.Unimplemented, "old peer") + } + if g.p.gate != nil { + select { + case <-g.p.gate: + case <-ctx.Done(): + return nil, ctx.Err() + } + } + + return g.TunnelServer.AppendSetupLogs(ctx, req) +} + +type peerOptions struct { + runID string + unimpl bool + stalled bool + onAccepted func(string, uint64) +} + +func newPeer(t *testing.T, o peerOptions) *peer { + t.Helper() + root := t.TempDir() + store, err := workspacelogs.Open(workspacelogs.Options{Root: root}) + require.NoError(t, err) + acks, err := workspacelogs.NewAckStore(filepath.Join(t.TempDir(), "acks")) + require.NoError(t, err) + p := &peer{root: root, store: store, acks: acks, runID: o.runID} + if p.runID == "" { + p.runID = workspacelogs.NewID() + } + if o.stalled { + p.gate = make(chan struct{}) + } + p.sink, err = workspacelogs.NewSink(workspacelogs.SinkOptions{ + Store: store, Acks: acks, OnAccepted: o.onAccepted, + Spec: workspacelogs.RunSpec{RunID: p.runID, Context: "default", Command: "up"}, + }) + require.NoError(t, err) + + lis := bufconn.Listen(1 << 20) + srv := grpc.NewServer() + real := tunnelserver.New(tunnelserver.WithSetupLogSink(p.sink)) + tunnel.RegisterTunnelServer(srv, &gatedServer{TunnelServer: real, p: p, unimpl: o.unimpl}) + go func() { _ = srv.Serve(lis) }() + t.Cleanup(srv.Stop) + + conn, err := grpc.NewClient( + "passthrough:///bufnet", + grpc.WithContextDialer( + func(context.Context, string) (net.Conn, error) { return lis.Dial() }, + ), + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) + require.NoError(t, err) + t.Cleanup(func() { _ = conn.Close() }) + p.client = tunnel.NewTunnelClient(conn) + + return p +} + +func (p *peer) release() { close(p.gate) } + +func (p *peer) output(t *testing.T) string { + t.Helper() + var b strings.Builder + after := "" + for { + resp, err := p.store.Read(workspacelogs.ReadRequest{RunID: p.runID, After: after}) + require.NoError(t, err) + for i := range resp.Records { + b.WriteString(resp.Records[i].Text) + } + if !resp.HasMore { + return b.String() + } + after = resp.NextCursor + } +} + +// source is the execution host: a store written through a Recorder. +type source struct { + store *workspacelogs.Store + rec *workspacelogs.Recorder + acks *workspacelogs.AckStore + inv string +} + +func newSource(t *testing.T, runID string) *source { + t.Helper() + store, err := workspacelogs.Open(workspacelogs.Options{Root: t.TempDir()}) + require.NoError(t, err) + _, err = store.CreateRun(workspacelogs.RunSpec{RunID: runID, Context: "default", Command: "up"}) + require.NoError(t, err) + rec, err := workspacelogs.NewRecorder(workspacelogs.RecorderOptions{ + Backend: store, RunID: runID, Location: workspacelogs.LocationContainer, + }) + require.NoError(t, err) + acks, err := workspacelogs.NewAckStore(filepath.Join(t.TempDir(), "acks")) + require.NoError(t, err) + + return &source{store: store, rec: rec, acks: acks, inv: workspacelogs.NewID()} +} + +func (s *source) write(t *testing.T, lines ...string) { + t.Helper() + w := s.rec.Writer(s.inv, workspacelogs.StreamStdout) + for _, l := range lines { + _, err := w.Write([]byte(l)) + require.NoError(t, err) + } + require.NoError(t, w.Close()) +} + +func (s *source) sender( + ctx context.Context, + client tunnel.TunnelClient, + runID string, +) *tunnel.SetupLogSender { + return tunnel.NewSetupLogSender(ctx, tunnel.SetupLogSenderOptions{ + Client: client, + Source: workspacelogs.NewStoreSource(s.store), + RunID: runID, + ProducerID: s.rec.ProducerID(), + Acks: s.acks, + CleanupTimeout: 2 * time.Second, + CallTimeout: time.Second, + PollInterval: 20 * time.Millisecond, + }) +} + +func TestSenderMirrorsAndResumesFromAcknowledgedPosition(t *testing.T) { + p := newPeer(t, peerOptions{}) + src := newSource(t, p.runID) + src.rec.Invocation(workspacelogs.Invocation{ + ID: src.inv, Phase: "setup", Hook: testHook, State: workspacelogs.InvocationRunning, + }) + src.write(t, "one\n", "two\n") + + s := src.sender(context.Background(), p.client, p.runID) + require.True(t, s.Flush(context.Background())) + s.Close() + require.Equal(t, "one\ntwo\n", p.output(t)) + callsBefore := p.calls.Load() + + // A new sender over the same acknowledgement store replays nothing. + src.write(t, "three\n") + again := src.sender(context.Background(), p.client, p.runID) + defer again.Close() + require.True(t, again.Flush(context.Background())) + require.Equal(t, "one\ntwo\nthree\n", p.output(t)) + require.Equal(t, callsBefore+1, p.calls.Load()) + + // Replaying from zero is a duplicate delivery and changes nothing. + fresh, err := workspacelogs.NewAckStore(filepath.Join(t.TempDir(), "fresh")) + require.NoError(t, err) + src.acks = fresh + replay := src.sender(context.Background(), p.client, p.runID) + defer replay.Close() + require.True(t, replay.Flush(context.Background())) + require.Equal(t, "one\ntwo\nthree\n", p.output(t)) +} + +func TestSenderNeverBlocksProducerOnStalledPeer(t *testing.T) { + p := newPeer(t, peerOptions{stalled: true}) + src := newSource(t, p.runID) + s := src.sender(context.Background(), p.client, p.runID) + defer s.Close() + + start := time.Now() + for range 500 { + src.write(t, "line\n") + s.Notify() + } + require.Less(t, time.Since(start), 5*time.Second) + + flushStart := time.Now() + require.False(t, s.Flush(context.Background())) + require.Less(t, time.Since(flushStart), 4*time.Second) + require.Empty(t, p.output(t)) + + p.release() + require.True(t, s.Flush(context.Background())) + require.Equal(t, strings.Repeat("line\n", 500), p.output(t)) +} + +func TestSenderFallsBackWhenPeerIsOld(t *testing.T) { + p := newPeer(t, peerOptions{unimpl: true}) + src := newSource(t, p.runID) + src.write(t, "kept\n") + s := src.sender(context.Background(), p.client, p.runID) + defer s.Close() + + require.False(t, s.Flush(context.Background())) + require.True(t, s.Disabled()) + require.Zero(t, s.Acked()) + require.Equal(t, "kept\n", sourceText(t, src, p.runID)) +} + +func TestSenderFinalDrainAfterCanceledContext(t *testing.T) { + p := newPeer(t, peerOptions{}) + src := newSource(t, p.runID) + src.write(t, "last words\n") + ctx, cancel := context.WithCancel(context.Background()) + s := src.sender(ctx, p.client, p.runID) + cancel() + s.Close() + + require.True(t, s.Flush(ctx)) + require.Equal(t, "last words\n", p.output(t)) +} + +func TestSenderStopsRetryingExcludedBytes(t *testing.T) { + p := newPeer(t, peerOptions{}) + src := newSource(t, p.runID) + src.write(t, "a\n") + s := src.sender(context.Background(), p.client, p.runID) + defer s.Close() + require.True(t, s.Flush(context.Background())) + + // Fill the run so the receiver store cannot take more bytes. + junk := filepath.Join(p.root, "runs", p.runID, "junk") + require.NoError(t, os.WriteFile(junk, make([]byte, workspacelogs.MaxRunBytes), 0o600)) + src.write(t, "b\n", "c\n") + require.False(t, s.Flush(context.Background())) + acked := s.Acked() + require.Greater(t, acked, uint64(2)) + + calls := p.calls.Load() + require.False(t, s.Flush(context.Background())) + require.Equal(t, calls, p.calls.Load()) + m, err := p.store.Manifest(p.runID) + require.NoError(t, err) + require.Contains(t, m.Conditions, workspacelogs.ConditionRetentionGap) +} + +func TestTwoHopAcknowledgementsAreIndependent(t *testing.T) { + runID := workspacelogs.NewID() + caller := newPeer(t, peerOptions{runID: runID, stalled: true}) + + var fwd *tunnel.SetupLogForwarder + provider := newPeer(t, peerOptions{ + runID: runID, + onAccepted: func(id string, n uint64) { fwd.Notify(id, n) }, + }) + fwd = tunnel.NewSetupLogForwarder(context.Background(), tunnel.SetupLogForwarderOptions{ + Client: caller.client, + Source: workspacelogs.NewStoreSource(provider.store), + RunID: runID, + Acks: provider.acks, + CleanupTimeout: 600 * time.Millisecond, + CallTimeout: time.Second, + PollInterval: 20 * time.Millisecond, + }) + defer fwd.Close() + + src := newSource(t, runID) + src.write(t, "from the container\n") + s := src.sender(context.Background(), provider.client, runID) + defer s.Close() + + // The provider spool accepts, so the container hop completes. + require.True(t, s.Flush(context.Background())) + require.Equal(t, "from the container\n", provider.output(t)) + // The caller has nothing yet, so the provider must not claim it. + require.False(t, fwd.Flush(context.Background())) + require.Empty(t, caller.output(t)) + up, err := provider.acks.Load(workspacelogs.AckKey{ + RunID: runID, ProducerID: src.rec.ProducerID(), Hop: workspacelogs.HopUpstream, + }) + require.NoError(t, err) + in, err := provider.acks.Load(workspacelogs.AckKey{ + RunID: runID, ProducerID: src.rec.ProducerID(), Hop: workspacelogs.HopInbound, + }) + require.NoError(t, err) + require.Zero(t, up.Acked) + require.Positive(t, in.Acked) + + caller.release() + require.True(t, fwd.Flush(context.Background())) + require.Equal(t, "from the container\n", caller.output(t)) + // Identity is unchanged across hops. + resp, err := caller.store.Read(workspacelogs.ReadRequest{RunID: runID}) + require.NoError(t, err) + require.Equal(t, src.rec.ProducerID(), resp.Records[0].ProducerID) + up, err = provider.acks.Load(workspacelogs.AckKey{ + RunID: runID, ProducerID: src.rec.ProducerID(), Hop: workspacelogs.HopUpstream, + }) + require.NoError(t, err) + require.Equal(t, in.Acked, up.Acked) +} + +func sourceText(t *testing.T, src *source, runID string) string { + t.Helper() + resp, err := src.store.Read(workspacelogs.ReadRequest{RunID: runID}) + require.NoError(t, err) + var b strings.Builder + for i := range resp.Records { + b.WriteString(resp.Records[i].Text) + } + + return b.String() +} diff --git a/pkg/agent/tunnel/tunnel.pb.go b/pkg/agent/tunnel/tunnel.pb.go index e9afcb320e..bc2aa164f9 100644 --- a/pkg/agent/tunnel/tunnel.pb.go +++ b/pkg/agent/tunnel/tunnel.pb.go @@ -3,7 +3,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.36.11 -// protoc v7.36.1 +// protoc v7.36.2 // source: tunnel.proto package tunnel @@ -78,6 +78,58 @@ func (LogLevel) EnumDescriptor() ([]byte, []int) { return file_tunnel_proto_rawDescGZIP(), []int{0} } +type SetupLogResult int32 + +const ( + SetupLogResult_SETUP_LOG_RESULT_UNSPECIFIED SetupLogResult = 0 + SetupLogResult_SETUP_LOG_RESULT_ACCEPTED SetupLogResult = 1 + SetupLogResult_SETUP_LOG_RESULT_RETENTION_GAP SetupLogResult = 2 + SetupLogResult_SETUP_LOG_RESULT_REJECTED_INVALID SetupLogResult = 3 +) + +// Enum value maps for SetupLogResult. +var ( + SetupLogResult_name = map[int32]string{ + 0: "SETUP_LOG_RESULT_UNSPECIFIED", + 1: "SETUP_LOG_RESULT_ACCEPTED", + 2: "SETUP_LOG_RESULT_RETENTION_GAP", + 3: "SETUP_LOG_RESULT_REJECTED_INVALID", + } + SetupLogResult_value = map[string]int32{ + "SETUP_LOG_RESULT_UNSPECIFIED": 0, + "SETUP_LOG_RESULT_ACCEPTED": 1, + "SETUP_LOG_RESULT_RETENTION_GAP": 2, + "SETUP_LOG_RESULT_REJECTED_INVALID": 3, + } +) + +func (x SetupLogResult) Enum() *SetupLogResult { + p := new(SetupLogResult) + *p = x + return p +} + +func (x SetupLogResult) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (SetupLogResult) Descriptor() protoreflect.EnumDescriptor { + return file_tunnel_proto_enumTypes[1].Descriptor() +} + +func (SetupLogResult) Type() protoreflect.EnumType { + return &file_tunnel_proto_enumTypes[1] +} + +func (x SetupLogResult) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use SetupLogResult.Descriptor instead. +func (SetupLogResult) EnumDescriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{1} +} + type StreamMountRequest struct { state protoimpl.MessageState `protogen:"open.v1"` Mount string `protobuf:"bytes,1,opt,name=mount,proto3" json:"mount,omitempty"` @@ -686,6 +738,171 @@ func (x *StatusError) GetContext() map[string]string { return nil } +// AppendSetupLogsRequest carries one contiguous batch of a producer's records. +// Each record is a JSON encoded workspacelogs.Record. The encoded request is +// capped at 256 KiB. +type AppendSetupLogsRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + SchemaVersion uint32 `protobuf:"varint,1,opt,name=schema_version,json=schemaVersion,proto3" json:"schema_version,omitempty"` + RunId string `protobuf:"bytes,2,opt,name=run_id,json=runId,proto3" json:"run_id,omitempty"` + ProducerId string `protobuf:"bytes,3,opt,name=producer_id,json=producerId,proto3" json:"producer_id,omitempty"` + FirstSequence uint64 `protobuf:"varint,4,opt,name=first_sequence,json=firstSequence,proto3" json:"first_sequence,omitempty"` + LastSequence uint64 `protobuf:"varint,5,opt,name=last_sequence,json=lastSequence,proto3" json:"last_sequence,omitempty"` + Records [][]byte `protobuf:"bytes,6,rep,name=records,proto3" json:"records,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *AppendSetupLogsRequest) Reset() { + *x = AppendSetupLogsRequest{} + mi := &file_tunnel_proto_msgTypes[12] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *AppendSetupLogsRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AppendSetupLogsRequest) ProtoMessage() {} + +func (x *AppendSetupLogsRequest) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[12] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AppendSetupLogsRequest.ProtoReflect.Descriptor instead. +func (*AppendSetupLogsRequest) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{12} +} + +func (x *AppendSetupLogsRequest) GetSchemaVersion() uint32 { + if x != nil { + return x.SchemaVersion + } + return 0 +} + +func (x *AppendSetupLogsRequest) GetRunId() string { + if x != nil { + return x.RunId + } + return "" +} + +func (x *AppendSetupLogsRequest) GetProducerId() string { + if x != nil { + return x.ProducerId + } + return "" +} + +func (x *AppendSetupLogsRequest) GetFirstSequence() uint64 { + if x != nil { + return x.FirstSequence + } + return 0 +} + +func (x *AppendSetupLogsRequest) GetLastSequence() uint64 { + if x != nil { + return x.LastSequence + } + return 0 +} + +func (x *AppendSetupLogsRequest) GetRecords() [][]byte { + if x != nil { + return x.Records + } + return nil +} + +type AppendSetupLogsResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + // Highest contiguous sequence persisted for the producer. + AcceptedSequence uint64 `protobuf:"varint,1,opt,name=accepted_sequence,json=acceptedSequence,proto3" json:"accepted_sequence,omitempty"` + Result SetupLogResult `protobuf:"varint,2,opt,name=result,proto3,enum=tunnel.SetupLogResult" json:"result,omitempty"` + // Sequences the receiver will never hold, set for RETENTION_GAP. + GapFirstSequence uint64 `protobuf:"varint,3,opt,name=gap_first_sequence,json=gapFirstSequence,proto3" json:"gap_first_sequence,omitempty"` + GapLastSequence uint64 `protobuf:"varint,4,opt,name=gap_last_sequence,json=gapLastSequence,proto3" json:"gap_last_sequence,omitempty"` + Detail string `protobuf:"bytes,5,opt,name=detail,proto3" json:"detail,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *AppendSetupLogsResponse) Reset() { + *x = AppendSetupLogsResponse{} + mi := &file_tunnel_proto_msgTypes[13] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *AppendSetupLogsResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AppendSetupLogsResponse) ProtoMessage() {} + +func (x *AppendSetupLogsResponse) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[13] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AppendSetupLogsResponse.ProtoReflect.Descriptor instead. +func (*AppendSetupLogsResponse) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{13} +} + +func (x *AppendSetupLogsResponse) GetAcceptedSequence() uint64 { + if x != nil { + return x.AcceptedSequence + } + return 0 +} + +func (x *AppendSetupLogsResponse) GetResult() SetupLogResult { + if x != nil { + return x.Result + } + return SetupLogResult_SETUP_LOG_RESULT_UNSPECIFIED +} + +func (x *AppendSetupLogsResponse) GetGapFirstSequence() uint64 { + if x != nil { + return x.GapFirstSequence + } + return 0 +} + +func (x *AppendSetupLogsResponse) GetGapLastSequence() uint64 { + if x != nil { + return x.GapLastSequence + } + return 0 +} + +func (x *AppendSetupLogsResponse) GetDetail() string { + if x != nil { + return x.Detail + } + return "" +} + type Empty struct { state protoimpl.MessageState `protogen:"open.v1"` unknownFields protoimpl.UnknownFields @@ -694,7 +911,7 @@ type Empty struct { func (x *Empty) Reset() { *x = Empty{} - mi := &file_tunnel_proto_msgTypes[12] + mi := &file_tunnel_proto_msgTypes[14] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -706,7 +923,7 @@ func (x *Empty) String() string { func (*Empty) ProtoMessage() {} func (x *Empty) ProtoReflect() protoreflect.Message { - mi := &file_tunnel_proto_msgTypes[12] + mi := &file_tunnel_proto_msgTypes[14] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -719,7 +936,7 @@ func (x *Empty) ProtoReflect() protoreflect.Message { // Deprecated: Use Empty.ProtoReflect.Descriptor instead. func (*Empty) Descriptor() ([]byte, []int) { - return file_tunnel_proto_rawDescGZIP(), []int{12} + return file_tunnel_proto_rawDescGZIP(), []int{14} } var File_tunnel_proto protoreflect.FileDescriptor @@ -765,18 +982,38 @@ const file_tunnel_proto_rawDesc = "" + "\acontext\x18\x04 \x03(\v2 .tunnel.StatusError.ContextEntryR\acontext\x1a:\n" + "\fContextEntry\x12\x10\n" + "\x03key\x18\x01 \x01(\tR\x03key\x12\x14\n" + - "\x05value\x18\x02 \x01(\tR\x05value:\x028\x01\"\a\n" + + "\x05value\x18\x02 \x01(\tR\x05value:\x028\x01\"\xdd\x01\n" + + "\x16AppendSetupLogsRequest\x12%\n" + + "\x0eschema_version\x18\x01 \x01(\rR\rschemaVersion\x12\x15\n" + + "\x06run_id\x18\x02 \x01(\tR\x05runId\x12\x1f\n" + + "\vproducer_id\x18\x03 \x01(\tR\n" + + "producerId\x12%\n" + + "\x0efirst_sequence\x18\x04 \x01(\x04R\rfirstSequence\x12#\n" + + "\rlast_sequence\x18\x05 \x01(\x04R\flastSequence\x12\x18\n" + + "\arecords\x18\x06 \x03(\fR\arecords\"\xe8\x01\n" + + "\x17AppendSetupLogsResponse\x12+\n" + + "\x11accepted_sequence\x18\x01 \x01(\x04R\x10acceptedSequence\x12.\n" + + "\x06result\x18\x02 \x01(\x0e2\x16.tunnel.SetupLogResultR\x06result\x12,\n" + + "\x12gap_first_sequence\x18\x03 \x01(\x04R\x10gapFirstSequence\x12*\n" + + "\x11gap_last_sequence\x18\x04 \x01(\x04R\x0fgapLastSequence\x12\x16\n" + + "\x06detail\x18\x05 \x01(\tR\x06detail\"\a\n" + "\x05Empty*A\n" + "\bLogLevel\x12\t\n" + "\x05DEBUG\x10\x00\x12\b\n" + "\x04INFO\x10\x01\x12\b\n" + "\x04DONE\x10\x02\x12\v\n" + "\aWARNING\x10\x03\x12\t\n" + - "\x05ERROR\x10\x042\xe4\a\n" + + "\x05ERROR\x10\x04*\x9c\x01\n" + + "\x0eSetupLogResult\x12 \n" + + "\x1cSETUP_LOG_RESULT_UNSPECIFIED\x10\x00\x12\x1d\n" + + "\x19SETUP_LOG_RESULT_ACCEPTED\x10\x01\x12\"\n" + + "\x1eSETUP_LOG_RESULT_RETENTION_GAP\x10\x02\x12%\n" + + "!SETUP_LOG_RESULT_REJECTED_INVALID\x10\x032\xba\b\n" + "\x06Tunnel\x12&\n" + "\x04Ping\x12\r.tunnel.Empty\x1a\r.tunnel.Empty\"\x00\x12*\n" + "\x03Log\x12\x12.tunnel.LogMessage\x1a\r.tunnel.Empty\"\x00\x12/\n" + - "\x06Status\x12\x14.tunnel.StatusUpdate\x1a\r.tunnel.Empty\"\x00\x12.\n" + + "\x06Status\x12\x14.tunnel.StatusUpdate\x1a\r.tunnel.Empty\"\x00\x12T\n" + + "\x0fAppendSetupLogs\x12\x1e.tunnel.AppendSetupLogsRequest\x1a\x1f.tunnel.AppendSetupLogsResponse\"\x00\x12.\n" + "\n" + "SendResult\x12\x0f.tunnel.Message\x1a\r.tunnel.Empty\"\x00\x127\n" + "\x11DockerCredentials\x12\x0f.tunnel.Message\x1a\x0f.tunnel.Message\"\x00\x124\n" + @@ -807,71 +1044,77 @@ func file_tunnel_proto_rawDescGZIP() []byte { return file_tunnel_proto_rawDescData } -var file_tunnel_proto_enumTypes = make([]protoimpl.EnumInfo, 1) -var file_tunnel_proto_msgTypes = make([]protoimpl.MessageInfo, 14) +var file_tunnel_proto_enumTypes = make([]protoimpl.EnumInfo, 2) +var file_tunnel_proto_msgTypes = make([]protoimpl.MessageInfo, 16) var file_tunnel_proto_goTypes = []any{ (LogLevel)(0), // 0: tunnel.LogLevel - (*StreamMountRequest)(nil), // 1: tunnel.StreamMountRequest - (*StopForwardPortRequest)(nil), // 2: tunnel.StopForwardPortRequest - (*StopForwardPortResponse)(nil), // 3: tunnel.StopForwardPortResponse - (*ForwardPortRequest)(nil), // 4: tunnel.ForwardPortRequest - (*ForwardPortResponse)(nil), // 5: tunnel.ForwardPortResponse - (*Message)(nil), // 6: tunnel.Message - (*Secret)(nil), // 7: tunnel.Secret - (*SecretsResponse)(nil), // 8: tunnel.SecretsResponse - (*Chunk)(nil), // 9: tunnel.Chunk - (*LogMessage)(nil), // 10: tunnel.LogMessage - (*StatusUpdate)(nil), // 11: tunnel.StatusUpdate - (*StatusError)(nil), // 12: tunnel.StatusError - (*Empty)(nil), // 13: tunnel.Empty - nil, // 14: tunnel.StatusError.ContextEntry + (SetupLogResult)(0), // 1: tunnel.SetupLogResult + (*StreamMountRequest)(nil), // 2: tunnel.StreamMountRequest + (*StopForwardPortRequest)(nil), // 3: tunnel.StopForwardPortRequest + (*StopForwardPortResponse)(nil), // 4: tunnel.StopForwardPortResponse + (*ForwardPortRequest)(nil), // 5: tunnel.ForwardPortRequest + (*ForwardPortResponse)(nil), // 6: tunnel.ForwardPortResponse + (*Message)(nil), // 7: tunnel.Message + (*Secret)(nil), // 8: tunnel.Secret + (*SecretsResponse)(nil), // 9: tunnel.SecretsResponse + (*Chunk)(nil), // 10: tunnel.Chunk + (*LogMessage)(nil), // 11: tunnel.LogMessage + (*StatusUpdate)(nil), // 12: tunnel.StatusUpdate + (*StatusError)(nil), // 13: tunnel.StatusError + (*AppendSetupLogsRequest)(nil), // 14: tunnel.AppendSetupLogsRequest + (*AppendSetupLogsResponse)(nil), // 15: tunnel.AppendSetupLogsResponse + (*Empty)(nil), // 16: tunnel.Empty + nil, // 17: tunnel.StatusError.ContextEntry } var file_tunnel_proto_depIdxs = []int32{ - 7, // 0: tunnel.SecretsResponse.secrets:type_name -> tunnel.Secret + 8, // 0: tunnel.SecretsResponse.secrets:type_name -> tunnel.Secret 0, // 1: tunnel.LogMessage.logLevel:type_name -> tunnel.LogLevel - 12, // 2: tunnel.StatusUpdate.error:type_name -> tunnel.StatusError - 14, // 3: tunnel.StatusError.context:type_name -> tunnel.StatusError.ContextEntry - 13, // 4: tunnel.Tunnel.Ping:input_type -> tunnel.Empty - 10, // 5: tunnel.Tunnel.Log:input_type -> tunnel.LogMessage - 11, // 6: tunnel.Tunnel.Status:input_type -> tunnel.StatusUpdate - 6, // 7: tunnel.Tunnel.SendResult:input_type -> tunnel.Message - 6, // 8: tunnel.Tunnel.DockerCredentials:input_type -> tunnel.Message - 6, // 9: tunnel.Tunnel.GitCredentials:input_type -> tunnel.Message - 13, // 10: tunnel.Tunnel.GitHubCredentials:input_type -> tunnel.Empty - 6, // 11: tunnel.Tunnel.GitSSHSignature:input_type -> tunnel.Message - 13, // 12: tunnel.Tunnel.GitUser:input_type -> tunnel.Empty - 6, // 13: tunnel.Tunnel.DevsyConfig:input_type -> tunnel.Message - 6, // 14: tunnel.Tunnel.GPGPublicKeys:input_type -> tunnel.Message - 6, // 15: tunnel.Tunnel.KubeConfig:input_type -> tunnel.Message - 13, // 16: tunnel.Tunnel.Secrets:input_type -> tunnel.Empty - 4, // 17: tunnel.Tunnel.ForwardPort:input_type -> tunnel.ForwardPortRequest - 2, // 18: tunnel.Tunnel.StopForwardPort:input_type -> tunnel.StopForwardPortRequest - 13, // 19: tunnel.Tunnel.StreamWorkspace:input_type -> tunnel.Empty - 1, // 20: tunnel.Tunnel.StreamMount:input_type -> tunnel.StreamMountRequest - 13, // 21: tunnel.Tunnel.StreamSnapshotVolumes:input_type -> tunnel.Empty - 13, // 22: tunnel.Tunnel.Ping:output_type -> tunnel.Empty - 13, // 23: tunnel.Tunnel.Log:output_type -> tunnel.Empty - 13, // 24: tunnel.Tunnel.Status:output_type -> tunnel.Empty - 13, // 25: tunnel.Tunnel.SendResult:output_type -> tunnel.Empty - 6, // 26: tunnel.Tunnel.DockerCredentials:output_type -> tunnel.Message - 6, // 27: tunnel.Tunnel.GitCredentials:output_type -> tunnel.Message - 6, // 28: tunnel.Tunnel.GitHubCredentials:output_type -> tunnel.Message - 6, // 29: tunnel.Tunnel.GitSSHSignature:output_type -> tunnel.Message - 6, // 30: tunnel.Tunnel.GitUser:output_type -> tunnel.Message - 6, // 31: tunnel.Tunnel.DevsyConfig:output_type -> tunnel.Message - 6, // 32: tunnel.Tunnel.GPGPublicKeys:output_type -> tunnel.Message - 6, // 33: tunnel.Tunnel.KubeConfig:output_type -> tunnel.Message - 8, // 34: tunnel.Tunnel.Secrets:output_type -> tunnel.SecretsResponse - 5, // 35: tunnel.Tunnel.ForwardPort:output_type -> tunnel.ForwardPortResponse - 3, // 36: tunnel.Tunnel.StopForwardPort:output_type -> tunnel.StopForwardPortResponse - 9, // 37: tunnel.Tunnel.StreamWorkspace:output_type -> tunnel.Chunk - 9, // 38: tunnel.Tunnel.StreamMount:output_type -> tunnel.Chunk - 9, // 39: tunnel.Tunnel.StreamSnapshotVolumes:output_type -> tunnel.Chunk - 22, // [22:40] is the sub-list for method output_type - 4, // [4:22] is the sub-list for method input_type - 4, // [4:4] is the sub-list for extension type_name - 4, // [4:4] is the sub-list for extension extendee - 0, // [0:4] is the sub-list for field type_name + 13, // 2: tunnel.StatusUpdate.error:type_name -> tunnel.StatusError + 17, // 3: tunnel.StatusError.context:type_name -> tunnel.StatusError.ContextEntry + 1, // 4: tunnel.AppendSetupLogsResponse.result:type_name -> tunnel.SetupLogResult + 16, // 5: tunnel.Tunnel.Ping:input_type -> tunnel.Empty + 11, // 6: tunnel.Tunnel.Log:input_type -> tunnel.LogMessage + 12, // 7: tunnel.Tunnel.Status:input_type -> tunnel.StatusUpdate + 14, // 8: tunnel.Tunnel.AppendSetupLogs:input_type -> tunnel.AppendSetupLogsRequest + 7, // 9: tunnel.Tunnel.SendResult:input_type -> tunnel.Message + 7, // 10: tunnel.Tunnel.DockerCredentials:input_type -> tunnel.Message + 7, // 11: tunnel.Tunnel.GitCredentials:input_type -> tunnel.Message + 16, // 12: tunnel.Tunnel.GitHubCredentials:input_type -> tunnel.Empty + 7, // 13: tunnel.Tunnel.GitSSHSignature:input_type -> tunnel.Message + 16, // 14: tunnel.Tunnel.GitUser:input_type -> tunnel.Empty + 7, // 15: tunnel.Tunnel.DevsyConfig:input_type -> tunnel.Message + 7, // 16: tunnel.Tunnel.GPGPublicKeys:input_type -> tunnel.Message + 7, // 17: tunnel.Tunnel.KubeConfig:input_type -> tunnel.Message + 16, // 18: tunnel.Tunnel.Secrets:input_type -> tunnel.Empty + 5, // 19: tunnel.Tunnel.ForwardPort:input_type -> tunnel.ForwardPortRequest + 3, // 20: tunnel.Tunnel.StopForwardPort:input_type -> tunnel.StopForwardPortRequest + 16, // 21: tunnel.Tunnel.StreamWorkspace:input_type -> tunnel.Empty + 2, // 22: tunnel.Tunnel.StreamMount:input_type -> tunnel.StreamMountRequest + 16, // 23: tunnel.Tunnel.StreamSnapshotVolumes:input_type -> tunnel.Empty + 16, // 24: tunnel.Tunnel.Ping:output_type -> tunnel.Empty + 16, // 25: tunnel.Tunnel.Log:output_type -> tunnel.Empty + 16, // 26: tunnel.Tunnel.Status:output_type -> tunnel.Empty + 15, // 27: tunnel.Tunnel.AppendSetupLogs:output_type -> tunnel.AppendSetupLogsResponse + 16, // 28: tunnel.Tunnel.SendResult:output_type -> tunnel.Empty + 7, // 29: tunnel.Tunnel.DockerCredentials:output_type -> tunnel.Message + 7, // 30: tunnel.Tunnel.GitCredentials:output_type -> tunnel.Message + 7, // 31: tunnel.Tunnel.GitHubCredentials:output_type -> tunnel.Message + 7, // 32: tunnel.Tunnel.GitSSHSignature:output_type -> tunnel.Message + 7, // 33: tunnel.Tunnel.GitUser:output_type -> tunnel.Message + 7, // 34: tunnel.Tunnel.DevsyConfig:output_type -> tunnel.Message + 7, // 35: tunnel.Tunnel.GPGPublicKeys:output_type -> tunnel.Message + 7, // 36: tunnel.Tunnel.KubeConfig:output_type -> tunnel.Message + 9, // 37: tunnel.Tunnel.Secrets:output_type -> tunnel.SecretsResponse + 6, // 38: tunnel.Tunnel.ForwardPort:output_type -> tunnel.ForwardPortResponse + 4, // 39: tunnel.Tunnel.StopForwardPort:output_type -> tunnel.StopForwardPortResponse + 10, // 40: tunnel.Tunnel.StreamWorkspace:output_type -> tunnel.Chunk + 10, // 41: tunnel.Tunnel.StreamMount:output_type -> tunnel.Chunk + 10, // 42: tunnel.Tunnel.StreamSnapshotVolumes:output_type -> tunnel.Chunk + 24, // [24:43] is the sub-list for method output_type + 5, // [5:24] is the sub-list for method input_type + 5, // [5:5] is the sub-list for extension type_name + 5, // [5:5] is the sub-list for extension extendee + 0, // [0:5] is the sub-list for field type_name } func init() { file_tunnel_proto_init() } @@ -884,8 +1127,8 @@ func file_tunnel_proto_init() { File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_tunnel_proto_rawDesc), len(file_tunnel_proto_rawDesc)), - NumEnums: 1, - NumMessages: 14, + NumEnums: 2, + NumMessages: 16, NumExtensions: 0, NumServices: 1, }, diff --git a/pkg/agent/tunnel/tunnel.proto b/pkg/agent/tunnel/tunnel.proto index 36c6441768..0ddead9a5c 100644 --- a/pkg/agent/tunnel/tunnel.proto +++ b/pkg/agent/tunnel/tunnel.proto @@ -9,6 +9,7 @@ service Tunnel { rpc Ping(Empty) returns (Empty) {} rpc Log(LogMessage) returns (Empty) {} rpc Status(StatusUpdate) returns (Empty) {} + rpc AppendSetupLogs(AppendSetupLogsRequest) returns (AppendSetupLogsResponse) {} rpc SendResult(Message) returns (Empty) {} rpc DockerCredentials(Message) returns (Message) {} @@ -97,6 +98,35 @@ message StatusError { map context = 4; } +// AppendSetupLogsRequest carries one contiguous batch of a producer's records. +// Each record is a JSON encoded workspacelogs.Record. The encoded request is +// capped at 256 KiB. +message AppendSetupLogsRequest { + uint32 schema_version = 1; + string run_id = 2; + string producer_id = 3; + uint64 first_sequence = 4; + uint64 last_sequence = 5; + repeated bytes records = 6; +} + +enum SetupLogResult { + SETUP_LOG_RESULT_UNSPECIFIED = 0; + SETUP_LOG_RESULT_ACCEPTED = 1; + SETUP_LOG_RESULT_RETENTION_GAP = 2; + SETUP_LOG_RESULT_REJECTED_INVALID = 3; +} + +message AppendSetupLogsResponse { + // Highest contiguous sequence persisted for the producer. + uint64 accepted_sequence = 1; + SetupLogResult result = 2; + // Sequences the receiver will never hold, set for RETENTION_GAP. + uint64 gap_first_sequence = 3; + uint64 gap_last_sequence = 4; + string detail = 5; +} + message Empty { } diff --git a/pkg/agent/tunnel/tunnel_grpc.pb.go b/pkg/agent/tunnel/tunnel_grpc.pb.go index 96f4cfaf6b..1e636ca638 100644 --- a/pkg/agent/tunnel/tunnel_grpc.pb.go +++ b/pkg/agent/tunnel/tunnel_grpc.pb.go @@ -3,7 +3,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: // - protoc-gen-go-grpc v1.6.2 -// - protoc v7.36.1 +// - protoc v7.36.2 // source: tunnel.proto package tunnel @@ -24,6 +24,7 @@ const ( Tunnel_Ping_FullMethodName = "/tunnel.Tunnel/Ping" Tunnel_Log_FullMethodName = "/tunnel.Tunnel/Log" Tunnel_Status_FullMethodName = "/tunnel.Tunnel/Status" + Tunnel_AppendSetupLogs_FullMethodName = "/tunnel.Tunnel/AppendSetupLogs" Tunnel_SendResult_FullMethodName = "/tunnel.Tunnel/SendResult" Tunnel_DockerCredentials_FullMethodName = "/tunnel.Tunnel/DockerCredentials" Tunnel_GitCredentials_FullMethodName = "/tunnel.Tunnel/GitCredentials" @@ -48,6 +49,7 @@ type TunnelClient interface { Ping(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*Empty, error) Log(ctx context.Context, in *LogMessage, opts ...grpc.CallOption) (*Empty, error) Status(ctx context.Context, in *StatusUpdate, opts ...grpc.CallOption) (*Empty, error) + AppendSetupLogs(ctx context.Context, in *AppendSetupLogsRequest, opts ...grpc.CallOption) (*AppendSetupLogsResponse, error) SendResult(ctx context.Context, in *Message, opts ...grpc.CallOption) (*Empty, error) DockerCredentials(ctx context.Context, in *Message, opts ...grpc.CallOption) (*Message, error) GitCredentials(ctx context.Context, in *Message, opts ...grpc.CallOption) (*Message, error) @@ -103,6 +105,16 @@ func (c *tunnelClient) Status(ctx context.Context, in *StatusUpdate, opts ...grp return out, nil } +func (c *tunnelClient) AppendSetupLogs(ctx context.Context, in *AppendSetupLogsRequest, opts ...grpc.CallOption) (*AppendSetupLogsResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(AppendSetupLogsResponse) + err := c.cc.Invoke(ctx, Tunnel_AppendSetupLogs_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + func (c *tunnelClient) SendResult(ctx context.Context, in *Message, opts ...grpc.CallOption) (*Empty, error) { cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) out := new(Empty) @@ -287,6 +299,7 @@ type TunnelServer interface { Ping(context.Context, *Empty) (*Empty, error) Log(context.Context, *LogMessage) (*Empty, error) Status(context.Context, *StatusUpdate) (*Empty, error) + AppendSetupLogs(context.Context, *AppendSetupLogsRequest) (*AppendSetupLogsResponse, error) SendResult(context.Context, *Message) (*Empty, error) DockerCredentials(context.Context, *Message) (*Message, error) GitCredentials(context.Context, *Message) (*Message, error) @@ -321,6 +334,9 @@ func (UnimplementedTunnelServer) Log(context.Context, *LogMessage) (*Empty, erro func (UnimplementedTunnelServer) Status(context.Context, *StatusUpdate) (*Empty, error) { return nil, status.Error(codes.Unimplemented, "method Status not implemented") } +func (UnimplementedTunnelServer) AppendSetupLogs(context.Context, *AppendSetupLogsRequest) (*AppendSetupLogsResponse, error) { + return nil, status.Error(codes.Unimplemented, "method AppendSetupLogs not implemented") +} func (UnimplementedTunnelServer) SendResult(context.Context, *Message) (*Empty, error) { return nil, status.Error(codes.Unimplemented, "method SendResult not implemented") } @@ -441,6 +457,24 @@ func _Tunnel_Status_Handler(srv interface{}, ctx context.Context, dec func(inter return interceptor(ctx, in, info, handler) } +func _Tunnel_AppendSetupLogs_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(AppendSetupLogsRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(TunnelServer).AppendSetupLogs(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Tunnel_AppendSetupLogs_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(TunnelServer).AppendSetupLogs(ctx, req.(*AppendSetupLogsRequest)) + } + return interceptor(ctx, in, info, handler) +} + func _Tunnel_SendResult_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(Message) if err := dec(in); err != nil { @@ -709,6 +743,10 @@ var Tunnel_ServiceDesc = grpc.ServiceDesc{ MethodName: "Status", Handler: _Tunnel_Status_Handler, }, + { + MethodName: "AppendSetupLogs", + Handler: _Tunnel_AppendSetupLogs_Handler, + }, { MethodName: "SendResult", Handler: _Tunnel_SendResult_Handler, diff --git a/pkg/agent/tunnelserver/options.go b/pkg/agent/tunnelserver/options.go index a44558680d..af7a10d3bf 100644 --- a/pkg/agent/tunnelserver/options.go +++ b/pkg/agent/tunnelserver/options.go @@ -9,6 +9,7 @@ import ( "github.com/devsy-org/devsy/pkg/netstat" provider2 "github.com/devsy-org/devsy/pkg/provider" "github.com/devsy-org/devsy/pkg/status" + "github.com/devsy-org/devsy/pkg/workspacelogs" ) type Option func(*tunnelServer) *tunnelServer @@ -115,6 +116,15 @@ func WithStatusReporter(reporter status.Reporter) Option { } } +// WithSetupLogSink accepts AppendSetupLogs batches for the sink's expected run. +// A nil sink leaves the RPC unimplemented, which senders treat as an old peer. +func WithSetupLogSink(sink *workspacelogs.Sink) Option { + return func(s *tunnelServer) *tunnelServer { + s.setupLogSink = sink + return s + } +} + func toSecrets(entries []string, mount bool) []*tunnel.Secret { secrets := make([]*tunnel.Secret, 0, len(entries)) for _, entry := range entries { diff --git a/pkg/agent/tunnelserver/setup_log_hop.go b/pkg/agent/tunnelserver/setup_log_hop.go new file mode 100644 index 0000000000..51f170506a --- /dev/null +++ b/pkg/agent/tunnelserver/setup_log_hop.go @@ -0,0 +1,145 @@ +package tunnelserver + +import ( + "context" + "path/filepath" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "github.com/devsy-org/devsy/pkg/workspacelogs" +) + +// SetupLogHop wires one process's side of setup history mirroring: it sends +// this process's own records to the next host, accepts records mirrored from the +// previous host, and forwards those on. Every method is safe on a nil hop, so a +// process without history changes nothing. +type SetupLogHop struct { + sender *tunnel.SetupLogSender + forwarder *tunnel.SetupLogForwarder + sink *workspacelogs.Sink +} + +// HopOptions selects what a hop does. +type HopOptions struct { + // Session is this process's run session. + Session *workspacelogs.Session + // Upstream is the client of the next host. Nil makes the hop receive-only. + Upstream tunnel.TunnelClient + // Receive accepts records mirrored from the previous host. + Receive bool +} + +// NewSetupLogHop builds the hop. It returns nil when the session is nil or the +// hop would do nothing. +func NewSetupLogHop(ctx context.Context, opts HopOptions) *SetupLogHop { + if opts.Session == nil || (opts.Upstream == nil && !opts.Receive) { + return nil + } + acks, err := workspacelogs.NewAckStore( + filepath.Join(opts.Session.Store.Root(), workspacelogs.AckDirName), + ) + if err != nil { + return nil + } + hop := &SetupLogHop{} + if opts.Upstream != nil { + source := workspacelogs.NewStoreSource(opts.Session.Store) + hop.sender = tunnel.NewSetupLogSender(ctx, tunnel.SetupLogSenderOptions{ + Client: opts.Upstream, Source: source, RunID: opts.Session.RunID, + ProducerID: opts.Session.ProducerID(), Acks: acks, + }) + hop.forwarder = tunnel.NewSetupLogForwarder(ctx, tunnel.SetupLogForwarderOptions{ + Client: opts.Upstream, Source: source, RunID: opts.Session.RunID, Acks: acks, + }) + } + if opts.Receive { + hop.sink = newHopSink(opts.Session, acks, hop.forwarder) + } + + return hop +} + +func newHopSink( + session *workspacelogs.Session, + acks *workspacelogs.AckStore, + fwd *tunnel.SetupLogForwarder, +) *workspacelogs.Sink { + m, err := session.Store.Manifest(session.RunID) + if err != nil { + return nil + } + spec := workspacelogs.RunSpec{RunID: m.RunID, Context: m.Context, Command: m.Command} + if m.WorkspaceUID != nil { + spec.WorkspaceUID = *m.WorkspaceUID + } + sink := workspacelogs.SinkOptions{Store: session.Store, Spec: spec, Acks: acks} + if fwd != nil { + sink.OnAccepted = fwd.Notify + } + built, err := workspacelogs.NewSink(sink) + if err != nil { + return nil + } + + return built +} + +// Options returns the tunnel server option that accepts mirrored records. +func (h *SetupLogHop) Options() []Option { + if h == nil || h.sink == nil { + return nil + } + + return []Option{WithSetupLogSink(h.sink)} +} + +// Flush drains everything upstream within the senders' own cleanup budget, +// even when ctx is already canceled. It reports whether the next host holds +// everything. Call it after the session is finished and before the result is +// reported. +func (h *SetupLogHop) Flush(ctx context.Context) bool { + if h == nil { + return true + } + complete := true + if h.sender != nil { + h.sender.Notify() + complete = h.sender.Flush(ctx) && complete + } + if h.forwarder != nil { + complete = h.forwarder.Flush(ctx) && complete + } + + return complete +} + +// Close stops the background senders. +func (h *SetupLogHop) Close() { + if h == nil { + return + } + if h.sender != nil { + h.sender.Close() + } + if h.forwarder != nil { + h.forwarder.Close() + } +} + +type hopOptionsKey struct{} + +// WithSetupLogOptions carries tunnel server options for setup history to code +// that starts the server several calls down. +func WithSetupLogOptions(ctx context.Context, opts []Option) context.Context { + if len(opts) == 0 { + return ctx + } + + return context.WithValue(ctx, hopOptionsKey{}, opts) +} + +// SetupLogOptionsFrom returns the options stored by WithSetupLogOptions. +func SetupLogOptionsFrom(ctx context.Context) []Option { + opts, _ := ctx.Value(hopOptionsKey{}).([]Option) + + return opts +} diff --git a/pkg/agent/tunnelserver/setup_log_hop_test.go b/pkg/agent/tunnelserver/setup_log_hop_test.go new file mode 100644 index 0000000000..38387e3c36 --- /dev/null +++ b/pkg/agent/tunnelserver/setup_log_hop_test.go @@ -0,0 +1,26 @@ +package tunnelserver + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNilHopIsInert(t *testing.T) { + var hop *SetupLogHop + + assert.Nil(t, hop.Options()) + assert.True(t, hop.Flush(context.Background())) + hop.Close() + assert.Nil(t, NewSetupLogHop(context.Background(), HopOptions{}), "no session means no hop") +} + +func TestSetupLogOptionsTravelInContext(t *testing.T) { + ctx := context.Background() + assert.Empty(t, SetupLogOptionsFrom(ctx)) + assert.Equal(t, ctx, WithSetupLogOptions(ctx, nil)) + + opts := []Option{WithAllowKubeConfig(true)} + assert.Len(t, SetupLogOptionsFrom(WithSetupLogOptions(ctx, opts)), 1) +} diff --git a/pkg/agent/tunnelserver/setup_logs.go b/pkg/agent/tunnelserver/setup_logs.go new file mode 100644 index 0000000000..02197fd871 --- /dev/null +++ b/pkg/agent/tunnelserver/setup_logs.go @@ -0,0 +1,61 @@ +package tunnelserver + +import ( + "context" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/proto" +) + +// AppendSetupLogs validates a batch against the expected run and persists it. +// Without a sink the RPC is unimplemented, like an older peer. +func (t *tunnelServer) AppendSetupLogs( + _ context.Context, req *tunnel.AppendSetupLogsRequest, +) (*tunnel.AppendSetupLogsResponse, error) { + if t.setupLogSink == nil { + return nil, status.Error(codes.Unimplemented, "setup log mirroring is not enabled") + } + if proto.Size(req) > workspacelogs.MaxPageBytes { + return rejectedBatch("batch exceeds page size"), nil + } + res, err := t.setupLogSink.Append(workspacelogs.BatchRequest{ + SchemaVersion: int(req.GetSchemaVersion()), + RunID: req.GetRunId(), + ProducerID: req.GetProducerId(), + FirstSequence: req.GetFirstSequence(), + LastSequence: req.GetLastSequence(), + Records: req.GetRecords(), + }) + if err != nil { + return nil, status.Error(codes.Unavailable, "setup log store failed") + } + + return &tunnel.AppendSetupLogsResponse{ + AcceptedSequence: res.Accepted, + Result: protoResult(res.Result), + GapFirstSequence: res.GapFirst, + GapLastSequence: res.GapLast, + Detail: res.Detail, + }, nil +} + +func rejectedBatch(detail string) *tunnel.AppendSetupLogsResponse { + return &tunnel.AppendSetupLogsResponse{ + Result: tunnel.SetupLogResult_SETUP_LOG_RESULT_REJECTED_INVALID, + Detail: detail, + } +} + +func protoResult(r workspacelogs.MirrorResult) tunnel.SetupLogResult { + switch r { + case workspacelogs.MirrorAccepted: + return tunnel.SetupLogResult_SETUP_LOG_RESULT_ACCEPTED + case workspacelogs.MirrorRetentionGap: + return tunnel.SetupLogResult_SETUP_LOG_RESULT_RETENTION_GAP + default: + return tunnel.SetupLogResult_SETUP_LOG_RESULT_REJECTED_INVALID + } +} diff --git a/pkg/agent/tunnelserver/setup_logs_test.go b/pkg/agent/tunnelserver/setup_logs_test.go new file mode 100644 index 0000000000..c2abf4025f --- /dev/null +++ b/pkg/agent/tunnelserver/setup_logs_test.go @@ -0,0 +1,51 @@ +package tunnelserver + +import ( + "context" + "path/filepath" + "testing" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/require" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +func TestAppendSetupLogsUnimplementedWithoutSink(t *testing.T) { + _, err := New().AppendSetupLogs(context.Background(), &tunnel.AppendSetupLogsRequest{}) + require.Equal(t, codes.Unimplemented, status.Code(err)) +} + +func TestAppendSetupLogsValidatesExpectedRun(t *testing.T) { + store, err := workspacelogs.Open( + workspacelogs.Options{Root: filepath.Join(t.TempDir(), "logs")}, + ) + require.NoError(t, err) + runID := workspacelogs.NewID() + sink, err := workspacelogs.NewSink(workspacelogs.SinkOptions{ + Store: store, + Spec: workspacelogs.RunSpec{RunID: runID, Context: "default", Command: "up"}, + }) + require.NoError(t, err) + srv := New(WithSetupLogSink(sink)) + + resp, err := srv.AppendSetupLogs(context.Background(), &tunnel.AppendSetupLogsRequest{ + SchemaVersion: workspacelogs.SchemaVersion, + RunId: workspacelogs.NewID(), + ProducerId: workspacelogs.NewID(), + FirstSequence: 1, LastSequence: 1, + Records: [][]byte{[]byte(`{}`)}, + }) + require.NoError(t, err) + require.Equal(t, tunnel.SetupLogResult_SETUP_LOG_RESULT_REJECTED_INVALID, resp.GetResult()) + + huge := make([]byte, workspacelogs.MaxPageBytes) + resp, err = srv.AppendSetupLogs(context.Background(), &tunnel.AppendSetupLogsRequest{ + SchemaVersion: workspacelogs.SchemaVersion, + RunId: runID, ProducerId: workspacelogs.NewID(), + FirstSequence: 1, LastSequence: 1, Records: [][]byte{huge}, + }) + require.NoError(t, err) + require.Equal(t, tunnel.SetupLogResult_SETUP_LOG_RESULT_REJECTED_INVALID, resp.GetResult()) +} diff --git a/pkg/agent/tunnelserver/tunnelserver.go b/pkg/agent/tunnelserver/tunnelserver.go index 6e09ce970f..4bf7c77c74 100644 --- a/pkg/agent/tunnelserver/tunnelserver.go +++ b/pkg/agent/tunnelserver/tunnelserver.go @@ -31,68 +31,81 @@ import ( provider2 "github.com/devsy-org/devsy/pkg/provider" "github.com/devsy-org/devsy/pkg/status" "github.com/devsy-org/devsy/pkg/stdio" + "github.com/devsy-org/devsy/pkg/workspacelogs" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/reflection" grpcstatus "google.golang.org/grpc/status" ) -func RunServicesServer( - ctx context.Context, - reader io.Reader, - writer io.WriteCloser, - allowGitCredentials, allowDockerCredentials bool, - forwarder netstat.Forwarder, - workspace *provider2.Workspace, - options ...Option, -) error { +// ServicesSpec configures RunServicesServer. +type ServicesSpec struct { + Reader io.Reader + Writer io.WriteCloser + AllowGitCredentials bool + AllowDockerCredentials bool + Forwarder netstat.Forwarder + Workspace *provider2.Workspace + Options []Option +} + +// UpSpec configures RunUpServer. +type UpSpec struct { + Reader io.Reader + Writer io.WriteCloser + AllowGitCredentials bool + AllowDockerCredentials bool + Workspace *provider2.Workspace + Options []Option +} + +// SetupSpec configures RunSetupServer. +type SetupSpec struct { + Reader io.Reader + Writer io.WriteCloser + AllowGitCredentials bool + AllowDockerCredentials bool + SetupInfo *config.Result + Options []Option +} + +func RunServicesServer(ctx context.Context, spec ServicesSpec) error { + options := slices.Clone(spec.Options) options = append(options, - WithForwarder(forwarder), - WithAllowGitCredentials(allowGitCredentials), - WithAllowDockerCredentials(allowDockerCredentials), - WithWorkspace(workspace), + WithForwarder(spec.Forwarder), + WithAllowGitCredentials(spec.AllowGitCredentials), + WithAllowDockerCredentials(spec.AllowDockerCredentials), + WithWorkspace(spec.Workspace), ) tunnelServ := New(options...) - return tunnelServ.Run(ctx, reader, writer) + return tunnelServ.Run(ctx, spec.Reader, spec.Writer) } -func RunUpServer( - ctx context.Context, - reader io.Reader, - writer io.WriteCloser, - allowGitCredentials, allowDockerCredentials bool, - workspace *provider2.Workspace, - options ...Option, -) (*config.Result, error) { +func RunUpServer(ctx context.Context, spec UpSpec) (*config.Result, error) { + options := slices.Clone(spec.Options) options = append(options, - WithWorkspace(workspace), - WithAllowGitCredentials(allowGitCredentials), - WithAllowDockerCredentials(allowDockerCredentials), + WithWorkspace(spec.Workspace), + WithAllowGitCredentials(spec.AllowGitCredentials), + WithAllowDockerCredentials(spec.AllowDockerCredentials), ) tunnelServ := New(options...) - return tunnelServ.RunWithResult(ctx, reader, writer) + return tunnelServ.RunWithResult(ctx, spec.Reader, spec.Writer) } -func RunSetupServer( - ctx context.Context, - reader io.Reader, - writer io.WriteCloser, - allowGitCredentials, allowDockerCredentials bool, - setupInfo *config.Result, - options ...Option, -) (*config.Result, error) { +func RunSetupServer(ctx context.Context, spec SetupSpec) (*config.Result, error) { + options := slices.Clone(spec.Options) options = append(options, - WithAllowGitCredentials(allowGitCredentials), - WithAllowDockerCredentials(allowDockerCredentials), + WithAllowGitCredentials(spec.AllowGitCredentials), + WithAllowDockerCredentials(spec.AllowDockerCredentials), ) - tunnelServ, err := newSetupServer(setupInfo, options...) + tunnelServ, err := newSetupServer(spec.SetupInfo, options...) if err != nil { return nil, err } - return tunnelServ.RunWithResult(ctx, reader, writer) + return tunnelServ.RunWithResult(ctx, spec.Reader, spec.Writer) } func newSetupServer(setupInfo *config.Result, options ...Option) (*tunnelServer, error) { @@ -133,6 +146,9 @@ func New(options ...Option) *tunnelServer { type tunnelServer struct { tunnel.UnimplementedTunnelServer + // setup log mirroring; nil leaves AppendSetupLogs unimplemented + setupLogSink *workspacelogs.Sink + // stream mounts mounts []*config.Mount workspaceMount *config.Mount @@ -256,7 +272,7 @@ func (t *tunnelServer) DockerCredentials( return nil, err } - out, err := json.Marshal(credentials) + out, err := json.Marshal(credentials) //nolint:gosec // credentials are the response payload if err != nil { return nil, err } @@ -351,7 +367,7 @@ func (t *tunnelServer) GitCredentials( return nil, err } - out, err := json.Marshal(credentials) + out, err := json.Marshal(credentials) //nolint:gosec // credentials are the response payload if err != nil { return nil, err } @@ -499,16 +515,28 @@ func (t *tunnelServer) Status( ctx context.Context, update *tunnel.StatusUpdate, ) (*tunnel.Empty, error) { + if err := validateStatusUpdate(update); err != nil { + return nil, err + } + t.statusReporter.Report(statusEvent(update)) + return &tunnel.Empty{}, nil +} + +func validateStatusUpdate(update *tunnel.StatusUpdate) error { if update == nil || update.Phase == "" || !status.ValidState(status.State(update.State)) || update.DurationMs < 0 { - return nil, fmt.Errorf( + return fmt.Errorf( "invalid status update: phase, duration, and recognized state are required", ) } if update.State == string(status.StateFailed) && (update.Error == nil || update.Error.Message == "") { - return nil, fmt.Errorf("invalid failed status update: structured error is required") + return fmt.Errorf("invalid failed status update: structured error is required") } + return nil +} + +func statusEvent(update *tunnel.StatusUpdate) status.Event { // No Pipeline: the tunnel only carries container-side up events, and the // host's reporter stamps the pipeline on arrival. e := status.Event{ @@ -528,8 +556,7 @@ func (t *tunnelServer) Status( Context: update.Error.Context, } } - t.statusReporter.Report(e) - return &tunnel.Empty{}, nil + return e } func (t *tunnelServer) StreamWorkspace( diff --git a/pkg/client/clientimplementation/daemonclient/client.go b/pkg/client/clientimplementation/daemonclient/client.go index 7b529dafc5..a0a7973b85 100644 --- a/pkg/client/clientimplementation/daemonclient/client.go +++ b/pkg/client/clientimplementation/daemonclient/client.go @@ -216,14 +216,13 @@ func (c *client) RefreshOptions( return fmt.Errorf("parse options: %w", err) } - workspace, err := options.ResolveAndSaveOptionsWorkspace( - ctx, - c.devsyConfig, - c.config, - c.workspace, - userOptions, - resolver.WithResolveSubOptions(), - ) + workspace, err := options.ResolveAndSaveOptionsWorkspace(ctx, options.ResolveWorkspaceRequest{ + DevConfig: c.devsyConfig, + ProviderConfig: c.config, + OriginalWorkspace: c.workspace, + UserOptions: userOptions, + Options: []resolver.Option{resolver.WithResolveSubOptions()}, + }) if err != nil { return err } diff --git a/pkg/client/clientimplementation/daemonclient/stop.go b/pkg/client/clientimplementation/daemonclient/stop.go index bd43542e32..2a835fbb00 100644 --- a/pkg/client/clientimplementation/daemonclient/stop.go +++ b/pkg/client/clientimplementation/daemonclient/stop.go @@ -48,7 +48,12 @@ func (c *client) Stop(ctx context.Context, opt clientpkg.StopOptions) error { return fmt.Errorf("no stop task id returned from server") } - _, err = observeTask(ctx, managementClient, workspace, retStop.Status.TaskID, status.Nop()) + _, err = observeTask(ctx, taskWait{ + managementClient: managementClient, + instance: workspace, + taskID: retStop.Status.TaskID, + reporter: status.Nop(), + }) if err != nil { return fmt.Errorf("stop: %w", err) } diff --git a/pkg/client/clientimplementation/daemonclient/up.go b/pkg/client/clientimplementation/daemonclient/up.go index 86d2afc154..a1dfbc6122 100644 --- a/pkg/client/clientimplementation/daemonclient/up.go +++ b/pkg/client/clientimplementation/daemonclient/up.go @@ -67,7 +67,12 @@ func (c *client) Up(ctx context.Context, opt clientpkg.UpOptions) (*config.Resul return nil, err } - return waitTaskDone(ctx, managementClient, instance, taskID, reporterOrNop(opt.Reporter)) + return waitTaskDone(ctx, taskWait{ + managementClient: managementClient, + instance: instance, + taskID: taskID, + reporter: reporterOrNop(opt.Reporter), + }) } func migratedRebuildError( @@ -210,14 +215,18 @@ func createUpTask(ctx context.Context, params createUpTaskParams) (string, error return task.Status.TaskID, nil } -func waitTaskDone( - ctx context.Context, - managementClient kube.Interface, - instance *managementv1.DevsyWorkspaceInstance, - taskID string, - reporter status.Reporter, -) (*config.Result, error) { - exitCode, err := observeTask(ctx, managementClient, instance, taskID, reporter) +// taskWait identifies the instance task to wait on and where to report progress. +type taskWait struct { + managementClient kube.Interface + instance *managementv1.DevsyWorkspaceInstance + taskID string + reporter status.Reporter +} + +func waitTaskDone(ctx context.Context, wait taskWait) (*config.Result, error) { + managementClient, instance := wait.managementClient, wait.instance + taskID := wait.taskID + exitCode, err := observeTask(ctx, wait) if err != nil { return nil, fmt.Errorf("up: %w", err) } else if exitCode != 0 { @@ -287,13 +296,9 @@ func printInstanceInfo(instance *managementv1.DevsyWorkspaceInstance) { log.Debug("Starting platform workspace with configuration", string(workspaceConfig)) } -func observeTask( - ctx context.Context, - managementClient kube.Interface, - instance *managementv1.DevsyWorkspaceInstance, - taskID string, - reporter status.Reporter, -) (int, error) { +func observeTask(ctx context.Context, wait taskWait) (int, error) { + managementClient, instance := wait.managementClient, wait.instance + taskID := wait.taskID var ( exitCode int err error @@ -307,7 +312,7 @@ func observeTask( // cancel ongoing task if context is done select { case <-ctx.Done(): - timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + timeoutCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second) defer cancel() defer cancelPrintCtx() @@ -327,7 +332,7 @@ func observeTask( } }() go func() { - exitCode, err = printLogs(printCtx, managementClient, instance, taskID, reporter) + exitCode, err = printLogs(printCtx, wait) errChan <- err }() @@ -348,13 +353,9 @@ type Message struct { Bytes []byte `json:"bytes,omitempty"` } -func printLogs( - ctx context.Context, - managementClient kube.Interface, - workspace *managementv1.DevsyWorkspaceInstance, - taskID string, - reporter status.Reporter, -) (int, error) { +func printLogs(ctx context.Context, wait taskWait) (int, error) { + managementClient, workspace := wait.managementClient, wait.instance + taskID, reporter := wait.taskID, wait.reporter logsReader, err := openTaskLogsStream(ctx, managementClient, workspace, taskID) if err != nil { return -1, err diff --git a/pkg/client/clientimplementation/machine_client.go b/pkg/client/clientimplementation/machine_client.go index 58612e5f60..a60b83e29d 100644 --- a/pkg/client/clientimplementation/machine_client.go +++ b/pkg/client/clientimplementation/machine_client.go @@ -102,12 +102,17 @@ func (e *machineExecutor) execute(ctx context.Context, cfg execConfig) error { } // lifecycleCommand executes a standard lifecycle operation (create/start/stop). -func (e *machineExecutor) lifecycleCommand( - ctx context.Context, - name string, - command types.StrArray, - verb, pastVerb string, -) error { +// lifecycleStep is one lifecycle command of a machine and its progress wording. +type lifecycleStep struct { + name string + command types.StrArray + verb string + pastVerb string +} + +func (e *machineExecutor) lifecycleCommand(ctx context.Context, step lifecycleStep) error { + name, command := step.name, step.command + verb, pastVerb := step.verb, step.pastVerb writer := log.Writer(log.LevelInfo) defer func() { _ = writer.Close() }() @@ -144,13 +149,12 @@ func (s *machineClient) RefreshOptions( return fmt.Errorf("parse options: %w", err) } - machine, err := options.ResolveAndSaveOptionsMachine( - ctx, - s.devsyConfig, - s.config, - s.machine, - userOptions, - ) + machine, err := options.ResolveAndSaveOptionsMachine(ctx, options.ResolveMachineRequest{ + DevConfig: s.devsyConfig, + ProviderConfig: s.config, + OriginalMachine: s.machine, + UserOptions: userOptions, + }) if err != nil { return err } @@ -181,15 +185,39 @@ func (s *machineClient) Context() string { } func (s *machineClient) Create(ctx context.Context) error { - return s.executor.lifecycleCommand(ctx, "create", s.config.Exec.Create, "creating", "created") + return s.executor.lifecycleCommand( + ctx, + lifecycleStep{ + name: "create", + command: s.config.Exec.Create, + verb: "creating", + pastVerb: "created", + }, + ) } func (s *machineClient) Start(ctx context.Context) error { - return s.executor.lifecycleCommand(ctx, "start", s.config.Exec.Start, "starting", "started") + return s.executor.lifecycleCommand( + ctx, + lifecycleStep{ + name: "start", + command: s.config.Exec.Start, + verb: "starting", + pastVerb: "started", + }, + ) } func (s *machineClient) Stop(ctx context.Context, options client.StopOptions) error { - return s.executor.lifecycleCommand(ctx, "stop", s.config.Exec.Stop, "stopping", "stopped") + return s.executor.lifecycleCommand( + ctx, + lifecycleStep{ + name: "stop", + command: s.config.Exec.Stop, + verb: "stopping", + pastVerb: "stopped", + }, + ) } func (s *machineClient) Command(ctx context.Context, commandOptions client.CommandOptions) error { diff --git a/pkg/client/clientimplementation/proxy_client.go b/pkg/client/clientimplementation/proxy_client.go index 1c7ca79cb8..4ad5a3554b 100644 --- a/pkg/client/clientimplementation/proxy_client.go +++ b/pkg/client/clientimplementation/proxy_client.go @@ -163,14 +163,13 @@ func (s *proxyClient) RefreshOptions( return fmt.Errorf("parse options: %w", err) } - workspace, err := options.ResolveAndSaveOptionsWorkspace( - ctx, - s.devsyConfig, - s.config, - s.workspace, - userOptions, - resolver.WithResolveSubOptions(), - ) + workspace, err := options.ResolveAndSaveOptionsWorkspace(ctx, options.ResolveWorkspaceRequest{ + DevConfig: s.devsyConfig, + ProviderConfig: s.config, + OriginalWorkspace: s.workspace, + UserOptions: userOptions, + Options: []resolver.Option{resolver.WithResolveSubOptions()}, + }) if err != nil { return err } diff --git a/pkg/client/clientimplementation/workspace_client.go b/pkg/client/clientimplementation/workspace_client.go index 84e5721a34..32b750d222 100644 --- a/pkg/client/clientimplementation/workspace_client.go +++ b/pkg/client/clientimplementation/workspace_client.go @@ -403,13 +403,12 @@ func (s *workspaceClient) refreshMachineOptions( return nil } - machine, err := options.ResolveAndSaveOptionsMachine( - ctx, - s.devsyConfig, - s.providerConfig, - s.providerMachine, - userOptions, - ) + machine, err := options.ResolveAndSaveOptionsMachine(ctx, options.ResolveMachineRequest{ + DevConfig: s.devsyConfig, + ProviderConfig: s.providerConfig, + OriginalMachine: s.providerMachine, + UserOptions: userOptions, + }) if err != nil { return err } @@ -422,13 +421,12 @@ func (s *workspaceClient) refreshWorkspaceOptions( ctx context.Context, userOptions map[string]string, ) error { - workspace, err := options.ResolveAndSaveOptionsWorkspace( - ctx, - s.devsyConfig, - s.providerConfig, - s.providerWorkspace, - userOptions, - ) + workspace, err := options.ResolveAndSaveOptionsWorkspace(ctx, options.ResolveWorkspaceRequest{ + DevConfig: s.devsyConfig, + ProviderConfig: s.providerConfig, + OriginalWorkspace: s.providerWorkspace, + UserOptions: userOptions, + }) if err != nil { return fmt.Errorf("resolve and save workspace options: %w", err) } @@ -1221,15 +1219,14 @@ func runTunnelServer( } tunnelOptions = append(tunnelOptions, opts.TunnelOptions...) - result, err := tunnelserver.RunUpServer( - ctx, - stdoutReader, - stdinWriter, - opts.WorkspaceClient.AgentInjectGitCredentials(opts.CLIOptions), - opts.WorkspaceClient.AgentInjectDockerCredentials(opts.CLIOptions), - opts.WorkspaceClient.WorkspaceConfig(), - tunnelOptions..., - ) + result, err := tunnelserver.RunUpServer(ctx, tunnelserver.UpSpec{ + Reader: stdoutReader, + Writer: stdinWriter, + AllowGitCredentials: opts.WorkspaceClient.AgentInjectGitCredentials(opts.CLIOptions), + AllowDockerCredentials: opts.WorkspaceClient.AgentInjectDockerCredentials(opts.CLIOptions), + Workspace: opts.WorkspaceClient.WorkspaceConfig(), + Options: tunnelOptions, + }) if err != nil { return nil, fmt.Errorf("run tunnel server: %w", err) } diff --git a/pkg/client/clientimplementation/workspace_client_test.go b/pkg/client/clientimplementation/workspace_client_test.go index fd9f3ac205..2c2d65e488 100644 --- a/pkg/client/clientimplementation/workspace_client_test.go +++ b/pkg/client/clientimplementation/workspace_client_test.go @@ -20,6 +20,10 @@ import ( "github.com/stretchr/testify/require" ) +const ( + binSh = "/bin/sh" +) + func TestHandleForceError(t *testing.T) { sentinel := errors.New("boom") @@ -121,7 +125,7 @@ func TestRunCommand(t *testing.T) { stderr := &bytes.Buffer{} err := RunCommand(context.Background(), RunCommandOptions{ Command: types.StrArray{ - "/bin/sh", + binSh, "-c", `printf '%s' "$DEVSY_RUN_COMMAND_SECRET"; printf '%s' "$DEVSY_RUN_COMMAND_SECRET" >&2`, }, @@ -142,7 +146,7 @@ func TestRunCommand_RawStdoutDoesNotHoldProtocolBytes(t *testing.T) { t, func(ctx context.Context, stdin io.Reader, stdout io.Writer) error { return RunCommand(ctx, RunCommandOptions{ - Command: types.StrArray{"/bin/sh", "-c", "printf h; read ack"}, + Command: types.StrArray{binSh, "-c", "printf h; read ack"}, Environ: []string{"DEVSY_RUN_COMMAND_SECRET=hidden"}, Stdin: stdin, Stdout: stdout, @@ -158,7 +162,7 @@ func TestRunCommand_RawStdoutStillRedactsStderr(t *testing.T) { var stdout, stderr bytes.Buffer err := RunCommand(context.Background(), RunCommandOptions{ Command: types.StrArray{ - "/bin/sh", + binSh, "-c", `printf '%s' "$DEVSY_RUN_COMMAND_SECRET"; printf '%s' "$DEVSY_RUN_COMMAND_SECRET" >&2`, }, @@ -208,7 +212,7 @@ func TestRunCommand_ReportsFlushFailureWithoutReplacingCommandError(t *testing.T for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := RunCommand(context.Background(), RunCommandOptions{ - Command: types.StrArray{"/bin/sh", "-c", tt.command}, + Command: types.StrArray{binSh, "-c", tt.command}, Environ: []string{"DEVSY_RUN_COMMAND_SECRET=" + secret}, Stdout: tt.stdout, Stderr: tt.stderr, @@ -236,7 +240,7 @@ func TestProxyUp_PreservesProtocolStdout(t *testing.T) { Name: "test", Exec: provider.ProviderCommands{ Proxy: &provider.ProxyCommands{ - Up: types.StrArray{"/bin/sh", "-c", "printf h; read ack"}, + Up: types.StrArray{binSh, "-c", "printf h; read ack"}, }, }, }, diff --git a/pkg/clierr/context_test.go b/pkg/clierr/context_test.go new file mode 100644 index 0000000000..1cb2b0340d --- /dev/null +++ b/pkg/clierr/context_test.go @@ -0,0 +1,33 @@ +package clierr + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const runKey = "runId" + +func TestWithContextAddsKeysAndKeepsChain(t *testing.T) { + root := errors.New("hook failed") + + got := WithContext(root, map[string]string{runKey: "r1"}) + + require.ErrorIs(t, got, root) + classified := Classify(got) + assert.Equal(t, "r1", classified.Context[runKey]) + assert.Equal(t, "hook failed", classified.Message) +} + +func TestWithContextKeepsExistingKeysAndNoopsWithoutValues(t *testing.T) { + base := &CLIError{Code: CodeUnknown, Message: "m", Context: map[string]string{runKey: "kept"}} + + got := Classify(WithContext(base, map[string]string{runKey: "new", "extra": "x"})) + + assert.Equal(t, "kept", got.Context[runKey]) + assert.Equal(t, "x", got.Context["extra"]) + assert.Equal(t, base, WithContext(base, nil)) + assert.NoError(t, WithContext(nil, map[string]string{"a": "b"})) +} diff --git a/pkg/clierr/errors.go b/pkg/clierr/errors.go index 59aca9dc91..3e2c3b3703 100644 --- a/pkg/clierr/errors.go +++ b/pkg/clierr/errors.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "strings" "github.com/devsy-org/devsy/pkg/secrets" @@ -104,6 +105,26 @@ func Recoverable(err error) error { return recoverableBuildError{err: err} } +// WithContext returns err classified, with values added to its context. Keys +// the error already carries are kept. The original error stays in the chain. +func WithContext(err error, values map[string]string) error { + if err == nil || len(values) == 0 { + return err + } + classified := Classify(err) + merged := make(map[string]string, len(classified.Context)+len(values)) + maps.Copy(merged, values) + maps.Copy(merged, classified.Context) + + return &CLIError{ + Code: classified.Code, + Message: classified.Message, + Hint: classified.Hint, + Context: merged, + wrapped: err, + } +} + func Classify(err error) *CLIError { if err == nil { return nil diff --git a/pkg/clihelp/help.go b/pkg/clihelp/help.go index ffa1971ee3..924f1abce3 100644 --- a/pkg/clihelp/help.go +++ b/pkg/clihelp/help.go @@ -112,20 +112,33 @@ func writeUsage(b *strings.Builder, cmd *cobra.Command) { } func writeCommands(b *strings.Builder, cmd *cobra.Command, width int) { + subs := listedSubcommands(cmd) + if len(subs) == 0 { + return + } + + section(b, "SUBCOMMANDS") + sort.Slice(subs, func(i, j int) bool { return subs[i].Name() < subs[j].Name() }) + labels, col := commandColumn(subs, width) + for i, sub := range subs { + writeCommandRow(b, commandRow{sub: sub, label: labels[i], col: col, width: width}) + } + b.WriteString("\n") +} + +func listedSubcommands(cmd *cobra.Command) []*cobra.Command { var subs []*cobra.Command for _, sub := range cmd.Commands() { if sub.IsAvailableCommand() || sub.IsAdditionalHelpTopicCommand() { subs = append(subs, sub) } } - if len(subs) == 0 { - return - } + return subs +} - section(b, "SUBCOMMANDS") - sort.Slice(subs, func(i, j int) bool { return subs[i].Name() < subs[j].Name() }) - // Keep the description column inside the terminal so wrapped text never - // runs past the right edge. +// Keep the description column inside the terminal so wrapped text never +// runs past the right edge. +func commandColumn(subs []*cobra.Command, width int) ([]string, int) { limit := clamp(width/3, nameColumn, maxNameCol) labels := make([]string, len(subs)) col := nameColumn @@ -133,27 +146,35 @@ func writeCommands(b *strings.Builder, cmd *cobra.Command, width int) { labels[i] = commandLabel(sub) col = clamp(len(labels[i])+2, col, limit) } - for i, sub := range subs { - b.WriteString(indent) - b.WriteString(theme.Command.Render(sub.Name())) - if names := aliasNames(sub); len(names) > 0 { - b.WriteString(theme.Muted.Render(" (" + strings.Join(names, ", ") + ")")) - } - if sub.Short == "" { - b.WriteString("\n") - continue - } - // Pad from the plain label - the styled string carries escape bytes - // that would otherwise be counted as visible columns. - if len(labels[i]) >= col { - // Label overflows the column: start the description on its own line. - b.WriteString("\n" + strings.Repeat(" ", len(indent)+col)) - } else { - b.WriteString(strings.Repeat(" ", col-len(labels[i]))) - } - b.WriteString(wrapHanging(sub.Short, width, len(indent)+col)) + return labels, col +} + +type commandRow struct { + sub *cobra.Command + label string + col int + width int +} + +func writeCommandRow(b *strings.Builder, row commandRow) { + b.WriteString(indent) + b.WriteString(theme.Command.Render(row.sub.Name())) + if names := aliasNames(row.sub); len(names) > 0 { + b.WriteString(theme.Muted.Render(" (" + strings.Join(names, ", ") + ")")) + } + if row.sub.Short == "" { b.WriteString("\n") + return + } + // Pad from the plain label - the styled string carries escape bytes + // that would otherwise be counted as visible columns. + if len(row.label) >= row.col { + // Label overflows the column: start the description on its own line. + b.WriteString("\n" + strings.Repeat(" ", len(indent)+row.col)) + } else { + b.WriteString(strings.Repeat(" ", row.col-len(row.label))) } + b.WriteString(wrapHanging(row.sub.Short, row.width, len(indent)+row.col)) b.WriteString("\n") } diff --git a/pkg/command/command.go b/pkg/command/command.go index 9d04fcf675..6262202b75 100644 --- a/pkg/command/command.go +++ b/pkg/command/command.go @@ -51,6 +51,7 @@ func ExistsForUser(cmd, user string) bool { return Exists(cmd) } - _, err = exec.Command("su", user, "-l", "-c", command).CombinedOutput() + su := exec.Command("su", user, "-l", "-c", command) //nolint:gosec // fixed binary + _, err = su.CombinedOutput() return err == nil } diff --git a/pkg/command/credential_supported.go b/pkg/command/credential_supported.go index c973211a6d..2a0e5bc9e5 100644 --- a/pkg/command/credential_supported.go +++ b/pkg/command/credential_supported.go @@ -17,12 +17,12 @@ func ForUser(cmd *exec.Cmd, userName string) error { return fmt.Errorf("failed to look up user %s: %w", userName, err) } - uid, err := strconv.Atoi(u.Uid) + uid, err := strconv.ParseUint(u.Uid, 10, 32) if err != nil { return fmt.Errorf("invalid UID %s: %w", u.Uid, err) } - gid, err := strconv.Atoi(u.Gid) + gid, err := strconv.ParseUint(u.Gid, 10, 32) if err != nil { return fmt.Errorf("invalid GID %s: %w", u.Gid, err) } diff --git a/pkg/compose/helper.go b/pkg/compose/helper.go index e8c8e02789..c892f88cbb 100644 --- a/pkg/compose/helper.go +++ b/pkg/compose/helper.go @@ -20,6 +20,8 @@ import ( "github.com/devsy-org/devsy/pkg/log" ) +const projectNameFlag = "--project-name" + func LoadDockerComposeProject( ctx context.Context, paths []string, @@ -89,10 +91,18 @@ func NewComposeHelper(dockerHelper *docker.DockerHelper) (*ComposeHelper, error) // tryDockerComposeV2 checks if Docker Compose V2 is available and returns a ComposeHelper if so. // The Compose V2 plugin can be installed on linux using -// sudo curl -SL https://github.com/docker/compose/releases/latest/download/docker-compose-linux-$(uname -m) -o /usr/libexec/docker/cli-plugins/docker-compose +// +// sudo curl -SL \ +// https://github.com/docker/compose/releases/latest/download/docker-compose-linux-$(uname -m) \ +// -o /usr/libexec/docker/cli-plugins/docker-compose +// // sudo chmod +x /usr/libexec/docker/cli-plugins/docker-compose // Docker Compose V2 requires the buildx plugin for building images and can be installed using -// sudo curl -SL https://github.com/docker/buildx/releases/latest/download/buildx-v0.30.1.linux-amd64 -o /usr/libexec/docker/cli-plugins/docker-buildx +// +// sudo curl -SL \ +// https://github.com/docker/buildx/releases/latest/download/buildx-v0.30.1.linux-amd64 \ +// -o /usr/libexec/docker/cli-plugins/docker-buildx +// // sudo chmod +x /usr/libexec/docker/cli-plugins/docker-buildx. func tryDockerComposeV2(dockerCmd string) (*ComposeHelper, error) { if exec.Command(dockerCmd, "compose").Run() != nil { @@ -202,17 +212,15 @@ func (h *ComposeHelper) FindDevContainer( func (h *ComposeHelper) Run( ctx context.Context, args []string, - stdin io.Reader, - stdout io.Writer, - stderr io.Writer, + streams docker.Streams, ) error { cmd := h.buildCmd(ctx, args...) - cmd.Stdin = stdin - cmd.Stdout = stdout + cmd.Stdin = streams.Stdin + cmd.Stdout = streams.Stdout var stderrBuf strings.Builder - if stderr != nil { - cmd.Stderr = io.MultiWriter(stderr, &stderrBuf) + if streams.Stderr != nil { + cmd.Stderr = io.MultiWriter(streams.Stderr, &stderrBuf) } else { cmd.Stderr = &stderrBuf } @@ -225,7 +233,7 @@ func (h *ComposeHelper) Run( } func (h *ComposeHelper) Stop(ctx context.Context, projectName string, args []string) error { - buildArgs := []string{"--project-name", projectName} + buildArgs := []string{projectNameFlag, projectName} buildArgs = append(buildArgs, args...) buildArgs = append(buildArgs, "stop") @@ -250,7 +258,7 @@ func (h *ComposeHelper) Remove( args []string, removeVolumes bool, ) error { - buildArgs := []string{"--project-name", projectName} + buildArgs := []string{projectNameFlag, projectName} buildArgs = append(buildArgs, args...) buildArgs = append(buildArgs, "down") if removeVolumes { @@ -294,7 +302,7 @@ func (h *ComposeHelper) FindProjectFiles( ctx context.Context, projectName string, ) ([]string, error) { - buildArgs := []string{"--project-name", projectName} + buildArgs := []string{projectNameFlag, projectName} buildArgs = append(buildArgs, "ls", "-a", "--filter", "name="+projectName, "--format", "json") rawOut, stderr, err := runCmdCapture(h.buildCmd(ctx, buildArgs...)) diff --git a/pkg/compose/helper_test.go b/pkg/compose/helper_test.go index 11f1b4d6f4..2dc0bf6558 100644 --- a/pkg/compose/helper_test.go +++ b/pkg/compose/helper_test.go @@ -12,10 +12,11 @@ import ( ) const ( - testPodmanCmd = "podman" - testDockerCmd = "docker" - testComposeArg = "compose" - testPodmanVersion = "2.32.4" + testPodmanCmd = "podman" + testDockerCmd = "docker" + testComposeArg = "compose" + testPodmanVersion = "2.32.4" + testComposeVersion = "2.37.1" ) type HelperTestSuite struct { @@ -35,18 +36,18 @@ func (s *HelperTestSuite) TestParseVersion() { }{ { name: "standard semver", - version: "2.37.1", - want: "2.37.1", + version: testComposeVersion, + want: testComposeVersion, }, { name: "semver with v prefix", version: "v2.37.1", - want: "2.37.1", + want: testComposeVersion, }, { name: "ubuntu package version", version: "2.37.1+ds1-0ubuntu2~24.04.1", - want: "2.37.1", + want: testComposeVersion, }, { name: "desktop version", @@ -56,7 +57,7 @@ func (s *HelperTestSuite) TestParseVersion() { { name: "another ubuntu variant", version: "2.37.1+ds1-0ubuntu1~24", - want: "2.37.1", + want: testComposeVersion, }, { name: "invalid version", diff --git a/pkg/config/config.go b/pkg/config/config.go index 450d375b44..2de0014eba 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -247,7 +247,10 @@ func LoadConfig(contextOverride string, providerOverride string) (*Config, error return nil, err } - configBytes, err := os.ReadFile(configOrigin) + //nolint:gosec // path is the user devsy config file + configBytes, err := os.ReadFile( + configOrigin, + ) //nolint:gosec // path is the user devsy config file if err != nil { if !os.IsNotExist(err) { return nil, fmt.Errorf("read config: %w", err) diff --git a/pkg/config/context.go b/pkg/config/context.go index 0584bf46db..f328b0966a 100644 --- a/pkg/config/context.go +++ b/pkg/config/context.go @@ -7,27 +7,29 @@ import ( ) const ( - ContextOptionSSHAddPrivateKeys = "SSH_ADD_PRIVATE_KEYS" - ContextOptionGPGAgentForwarding = "GPG_AGENT_FORWARDING" - ContextOptionGitSSHSignatureForwarding = "GIT_SSH_SIGNATURE_FORWARDING" + ContextOptionSSHAddPrivateKeys = "SSH_ADD_PRIVATE_KEYS" + ContextOptionGPGAgentForwarding = "GPG_AGENT_FORWARDING" + ContextOptionGitSSHSignatureForwarding = "GIT_SSH_SIGNATURE_FORWARDING" + //nolint:gosec // option name, not a credential ContextOptionSSHInjectDockerCredentials = "SSH_INJECT_DOCKER_CREDENTIALS" - ContextOptionSSHInjectGitCredentials = "SSH_INJECT_GIT_CREDENTIALS" - ContextOptionGitHubCLIForwarding = "SSH_INJECT_GITHUB_CREDENTIALS" - ContextOptionExitAfterTimeout = "EXIT_AFTER_TIMEOUT" - ContextOptionTelemetry = "TELEMETRY" - ContextOptionAgentURL = "AGENT_URL" - ContextOptionDotfilesURL = "DOTFILES_URL" - ContextOptionDotfilesScript = "DOTFILES_SCRIPT" - ContextOptionSSHAgentForwarding = "SSH_AGENT_FORWARDING" - ContextOptionSSHConfigPath = "SSH_CONFIG_PATH" - ContextOptionSSHConfigIncludePath = "SSH_CONFIG_INCLUDE_PATH" - ContextOptionAgentInjectTimeout = "AGENT_INJECT_TIMEOUT" - ContextOptionRegistryCache = "REGISTRY_CACHE" - ContextOptionSSHStrictHostKeyChecking = "SSH_STRICT_HOST_KEY_CHECKING" - ContextOptionSSHTunnelMode = "SSH_TUNNEL_MODE" - ContextOptionSecretsBackend = "SECRETS_BACKEND" - ContextOptionSnapshotRegistry = "SNAPSHOT_REGISTRY" - ContextOptionLogLevel = "LOG_LEVEL" + //nolint:gosec // option name, not a credential + ContextOptionSSHInjectGitCredentials = "SSH_INJECT_GIT_CREDENTIALS" + ContextOptionGitHubCLIForwarding = "SSH_INJECT_GITHUB_CREDENTIALS" + ContextOptionExitAfterTimeout = "EXIT_AFTER_TIMEOUT" + ContextOptionTelemetry = "TELEMETRY" + ContextOptionAgentURL = "AGENT_URL" + ContextOptionDotfilesURL = "DOTFILES_URL" + ContextOptionDotfilesScript = "DOTFILES_SCRIPT" + ContextOptionSSHAgentForwarding = "SSH_AGENT_FORWARDING" + ContextOptionSSHConfigPath = "SSH_CONFIG_PATH" + ContextOptionSSHConfigIncludePath = "SSH_CONFIG_INCLUDE_PATH" + ContextOptionAgentInjectTimeout = "AGENT_INJECT_TIMEOUT" + ContextOptionRegistryCache = "REGISTRY_CACHE" + ContextOptionSSHStrictHostKeyChecking = "SSH_STRICT_HOST_KEY_CHECKING" + ContextOptionSSHTunnelMode = "SSH_TUNNEL_MODE" + ContextOptionSecretsBackend = "SECRETS_BACKEND" + ContextOptionSnapshotRegistry = "SNAPSHOT_REGISTRY" + ContextOptionLogLevel = "LOG_LEVEL" ) var ContextOptions = []ContextOption{ @@ -40,50 +42,51 @@ var ContextOptions = []ContextOption{ { Name: ContextOptionSSHAddPrivateKeys, Description: "Specifies if Devsy should automatically add ssh-keys to the ssh-agent", - Default: "true", - Enum: []string{"true", "false"}, + Default: BoolTrue, + Enum: []string{BoolTrue, BoolFalse}, }, { Name: ContextOptionExitAfterTimeout, Description: "Specifies if Devsy should exit the process after the browser has been idle for a minute", - Default: "true", - Enum: []string{"true", "false"}, + Default: BoolTrue, + Enum: []string{BoolTrue, BoolFalse}, }, { Name: ContextOptionGPGAgentForwarding, Description: "Specifies if Devsy should do gpg-agent forwarding by default for ssh", - Default: "false", - Enum: []string{"true", "false"}, + Default: BoolFalse, + Enum: []string{BoolTrue, BoolFalse}, }, { - Name: ContextOptionGitSSHSignatureForwarding, - Description: "Specifies if Devsy should automatically detect ssh signature git setting and inject ssh signature helper", - Default: "true", - Enum: []string{"true", "false"}, + Name: ContextOptionGitSSHSignatureForwarding, + Description: "Specifies if Devsy should automatically detect ssh signature git setting " + + "and inject ssh signature helper", + Default: BoolTrue, + Enum: []string{BoolTrue, BoolFalse}, }, { Name: ContextOptionSSHInjectDockerCredentials, Description: "Specifies if Devsy should inject docker credentials into the workspace", - Default: "true", - Enum: []string{"true", "false"}, + Default: BoolTrue, + Enum: []string{BoolTrue, BoolFalse}, }, { Name: ContextOptionSSHInjectGitCredentials, Description: "Specifies if Devsy should inject git credentials into the workspace", - Default: "true", - Enum: []string{"true", "false"}, + Default: BoolTrue, + Enum: []string{BoolTrue, BoolFalse}, }, { Name: ContextOptionSSHAgentForwarding, Description: "Specifies if Devsy should do agent forwarding by default into the workspace", - Default: "true", - Enum: []string{"true", "false"}, + Default: BoolTrue, + Enum: []string{BoolTrue, BoolFalse}, }, { Name: ContextOptionTelemetry, Description: "Specifies if Devsy should send telemetry information", - Default: "true", - Enum: []string{"true", "false"}, + Default: BoolTrue, + Enum: []string{BoolTrue, BoolFalse}, }, { Name: ContextOptionAgentURL, @@ -102,8 +105,10 @@ var ContextOptions = []ContextOption{ Description: "Specifies the path where the ssh config should be written to", }, { - Name: ContextOptionSSHConfigIncludePath, - Description: "Specifies an alternate path where Devsy host entries should be written. Use this when your main SSH config is read-only (e.g., managed by Nix). Your main SSH config should have an Include directive pointing to this file.", + Name: ContextOptionSSHConfigIncludePath, + Description: "Specifies an alternate path where Devsy host entries should be written. " + + "Use this when your main SSH config is read-only (e.g., managed by Nix). " + + "Your main SSH config should have an Include directive pointing to this file.", }, { Name: ContextOptionAgentInjectTimeout, @@ -118,14 +123,14 @@ var ContextOptions = []ContextOption{ { Name: ContextOptionSSHStrictHostKeyChecking, Description: "Enables strict ssh host key checking for all operations", - Default: "false", - Enum: []string{"true", "false"}, + Default: BoolFalse, + Enum: []string{BoolTrue, BoolFalse}, }, { Name: ContextOptionSSHTunnelMode, Description: "Use a local TCP tunnel instead of ProxyCommand for SSH connections", - Default: "false", - Enum: []string{"true", "false"}, + Default: BoolFalse, + Enum: []string{BoolTrue, BoolFalse}, }, { Name: ContextOptionSecretsBackend, diff --git a/pkg/config/context_deletion.go b/pkg/config/context_deletion.go index 97df01c3b0..98462721be 100644 --- a/pkg/config/context_deletion.go +++ b/pkg/config/context_deletion.go @@ -336,10 +336,13 @@ func validateContextDeletionRoots(intent *ContextDeletionIntent) error { // Keep safe legacy names recoverable, including device names on POSIX. func validDeletionContextName(name string) bool { return name != DefaultContext && !invalidContextPathName(name) && - (runtime.GOOS != "windows" || !reservedWindowsContextName(name)) + (runtime.GOOS != goosWindows || !reservedWindowsContextName(name)) } -const maxContextPathComponentBytes = 255 +const ( + maxContextPathComponentBytes = 255 + goosWindows = "windows" +) // ValidateContextPathName accepts safe legacy context names while rejecting // values that could escape the context directory when used as one path part. @@ -350,7 +353,7 @@ func ValidateContextPathName(name string) error { if len(name) > maxContextPathComponentBytes { return errors.New("context name exceeds the path component limit") } - if runtime.GOOS == "windows" && reservedWindowsContextName(name) { + if runtime.GOOS == goosWindows && reservedWindowsContextName(name) { return errors.New("context name is a reserved Windows device name") } return nil @@ -437,7 +440,7 @@ func syncContextDeletionDirectory(dir string) error { } defer func() { _ = file.Close() }() err = file.Sync() - if runtime.GOOS == "windows" || errors.Is(err, os.ErrInvalid) { + if runtime.GOOS == goosWindows || errors.Is(err, os.ErrInvalid) { return nil } return err diff --git a/pkg/config/context_test.go b/pkg/config/context_test.go index f9c13b2dfb..9aaddf64d1 100644 --- a/pkg/config/context_test.go +++ b/pkg/config/context_test.go @@ -71,13 +71,16 @@ func TestCaseMergeContextOption(t *testing.T) { }, gocmp.Ignore())), ) if !ok { - fmt.Println(tc.description) + t.Log(tc.description) } } } func TestSnapshotRegistryOption_DefaultsEmpty(t *testing.T) { - c := &Config{Contexts: map[string]*ContextConfig{"default": {}}, DefaultContext: "default"} + c := &Config{ + Contexts: map[string]*ContextConfig{DefaultContext: {}}, + DefaultContext: DefaultContext, + } require.Equal(t, "", c.ContextOption(ContextOptionSnapshotRegistry)) } @@ -92,7 +95,10 @@ func TestSnapshotRegistryOption_Registered(t *testing.T) { } func TestLogLevelOption_DefaultAndEnum(t *testing.T) { - c := &Config{Contexts: map[string]*ContextConfig{"default": {}}, DefaultContext: "default"} + c := &Config{ + Contexts: map[string]*ContextConfig{DefaultContext: {}}, + DefaultContext: DefaultContext, + } require.Equal(t, "warn", c.ContextOption(ContextOptionLogLevel)) for _, option := range ContextOptions { diff --git a/pkg/config/env.go b/pkg/config/env.go index 35cc8ef0d7..f3fa99eaa3 100644 --- a/pkg/config/env.go +++ b/pkg/config/env.go @@ -86,6 +86,10 @@ const ( // EnvWorkspaceDaemonConfig holds the workspace daemon configuration. EnvWorkspaceDaemonConfig = "DEVSY_WORKSPACE_DAEMON_CONFIG" + // EnvSetupRun passes the non-secret run reference (JSON) from the container + // setup process to the background hook workers it launches. + EnvSetupRun = "DEVSY_SETUP_RUN" + // EnvSecretsEnv passes base64-encoded KEY=VALUE secrets (newline-joined) to // agent subprocesses via the environment (not argv, to keep them out of ps). EnvSecretsEnv = "DEVSY_SECRETS_ENV" // #nosec G101 -- env var name, not a credential. diff --git a/pkg/config/pathmanager.go b/pkg/config/pathmanager.go index 162c2e344a..281977fe06 100644 --- a/pkg/config/pathmanager.go +++ b/pkg/config/pathmanager.go @@ -113,6 +113,7 @@ type PathManager interface { // State sub-paths. LogDir() (string, error) TaskDir() (string, error) + WorkspaceLogsDir() (string, error) } // basePathManager implements every sub-path method by delegating the top-level @@ -276,6 +277,13 @@ func (b *basePathManager) TaskDir() (string, error) { return joinUnder(b.pm.StateDir, "tasks") } +// WorkspaceLogsDir holds retained build and lifecycle output for workspace +// runs. It sits outside the per-workspace directory so removing a workspace +// does not remove its history. +func (b *basePathManager) WorkspaceLogsDir() (string, error) { + return joinUnder(b.pm.StateDir, "workspace-logs") +} + // inContext returns elems joined beneath ContextDir(context). func (b *basePathManager) inContext(context string, elems ...string) (string, error) { return joinUnder(func() (string, error) { return b.ContextDir(context) }, elems...) diff --git a/pkg/config/pathmanager_linux_test.go b/pkg/config/pathmanager_linux_test.go index a8886e08f2..18fb4efcb6 100644 --- a/pkg/config/pathmanager_linux_test.go +++ b/pkg/config/pathmanager_linux_test.go @@ -101,21 +101,14 @@ func TestConfigFilePathDEVSYCONFIGOverride(t *testing.T) { } } -func TestContextDataSubPaths(t *testing.T) { - skipIfNotLinux(t) - - home := t.TempDir() - t.Setenv("HOME", home) - - pm := newTestLinuxPM() - ctx := "myctx" - base := filepath.Join(home, "."+RepoName, "contexts", ctx) +type subPathCase struct { + name string + fn func() (string, error) + want string +} - tests := []struct { - name string - fn func() (string, error) - want string - }{ +func contextDataSubPathCases(pm PathManager, ctx, base string) []subPathCase { + return []subPathCase{ {"ContextDir", func() (string, error) { return pm.ContextDir(ctx) }, base}, { "WorkspacesDir", @@ -163,6 +156,19 @@ func TestContextDataSubPaths(t *testing.T) { filepath.Join(base, "providers"), }, } +} + +func TestContextDataSubPaths(t *testing.T) { + skipIfNotLinux(t) + + home := t.TempDir() + t.Setenv("HOME", home) + + pm := newTestLinuxPM() + ctx := "myctx" + base := filepath.Join(home, "."+RepoName, "contexts", ctx) + + tests := contextDataSubPathCases(pm, ctx, base) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/config/pathmanager_test.go b/pkg/config/pathmanager_test.go index 7d82914957..5f67e89d2c 100644 --- a/pkg/config/pathmanager_test.go +++ b/pkg/config/pathmanager_test.go @@ -78,6 +78,7 @@ func TestDevsyHomeOverride(t *testing.T) { {"CacheDir", pm.CacheDir, filepath.Join(home, "cache")}, {"StateDir", pm.StateDir, filepath.Join(home, "state")}, {"RuntimeDir", pm.RuntimeDir, filepath.Join(home, "run")}, + {"WorkspaceLogsDir", pm.WorkspaceLogsDir, filepath.Join(home, "state", "workspace-logs")}, } for _, tt := range tests { diff --git a/pkg/copy/copy.go b/pkg/copy/copy.go index 78a2e7a2d4..df69093930 100644 --- a/pkg/copy/copy.go +++ b/pkg/copy/copy.go @@ -210,13 +210,17 @@ func copyByType(fileInfo os.FileInfo, sourcePath, destPath string) error { } func File(srcFile, dstFile string, perm os.FileMode) error { - out, err := os.OpenFile(dstFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm) + out, err := os.OpenFile( //nolint:gosec // destination path comes from the caller by design + dstFile, + os.O_RDWR|os.O_CREATE|os.O_TRUNC, + perm, + ) //nolint:gosec // caller-chosen path if err != nil { return err } defer func() { _ = out.Close() }() - in, err := os.Open(srcFile) + in, err := os.Open(srcFile) //nolint:gosec // caller-chosen path if err != nil { return err } diff --git a/pkg/daemon/agent/container_lifecycle_test.go b/pkg/daemon/agent/container_lifecycle_test.go index bdeaf7e3b1..48314c9457 100644 --- a/pkg/daemon/agent/container_lifecycle_test.go +++ b/pkg/daemon/agent/container_lifecycle_test.go @@ -100,11 +100,13 @@ func TestBuildWorkspaceDaemonConfig_ManagedWithoutPlatform(t *testing.T) { merged.RemoteUser = "vscode" cfg, err := BuildWorkspaceDaemonConfig( - devsy.PlatformOptions{}, - &provider2.Workspace{}, - &config.SubstitutionContext{ContainerWorkspaceFolder: testWorkdir}, - merged, - LifecycleOptions{Managed: true, Timeout: time.Minute}, + WorkspaceDaemonInput{ + PlatformOptions: devsy.PlatformOptions{}, + Workspace: &provider2.Workspace{}, + SubstitutionContext: &config.SubstitutionContext{ContainerWorkspaceFolder: testWorkdir}, + MergedConfig: merged, + Lifecycle: LifecycleOptions{Managed: true, Timeout: time.Minute}, + }, ) require.NoError(t, err) @@ -121,11 +123,17 @@ func TestBuildWorkspaceDaemonConfig_PlatformKeepsNetworking(t *testing.T) { merged.ShutdownAction = config.ShutdownActionNone cfg, err := BuildWorkspaceDaemonConfig( - devsy.PlatformOptions{AccessKey: "key", PlatformHost: "p", WorkspaceHost: "w"}, - &provider2.Workspace{}, - &config.SubstitutionContext{ContainerWorkspaceFolder: testWorkdir}, - merged, - LifecycleOptions{}, + WorkspaceDaemonInput{ + PlatformOptions: devsy.PlatformOptions{ + AccessKey: "key", + PlatformHost: "p", + WorkspaceHost: "w", + }, + Workspace: &provider2.Workspace{}, + SubstitutionContext: &config.SubstitutionContext{ContainerWorkspaceFolder: testWorkdir}, + MergedConfig: merged, + Lifecycle: LifecycleOptions{}, + }, ) require.NoError(t, err) diff --git a/pkg/daemon/agent/daemon.go b/pkg/daemon/agent/daemon.go index 99c41aba9a..68f0841201 100644 --- a/pkg/daemon/agent/daemon.go +++ b/pkg/daemon/agent/daemon.go @@ -33,27 +33,19 @@ type DaemonConfig struct { Lifecycle LifecycleConfig `json:"lifecycle,omitzero"` } -func BuildWorkspaceDaemonConfig( - platformOptions devsy.PlatformOptions, - workspaceConfig *provider2.Workspace, - substitutionContext *config.SubstitutionContext, - mergedConfig *config.MergedDevContainerConfig, - lifecycle LifecycleOptions, -) (*DaemonConfig, error) { - var workdir string - if workspaceConfig.Source.GitSubPath != "" { - substitutionContext.ContainerWorkspaceFolder = filepath.Join( - substitutionContext.ContainerWorkspaceFolder, - workspaceConfig.Source.GitSubPath, - ) - workdir = substitutionContext.ContainerWorkspaceFolder - } - if workdir == "" && mergedConfig != nil { - workdir = mergedConfig.WorkspaceFolder - } - if workdir == "" && substitutionContext != nil { - workdir = substitutionContext.ContainerWorkspaceFolder - } +// WorkspaceDaemonInput carries the inputs of the workspace daemon configuration. +type WorkspaceDaemonInput struct { + PlatformOptions devsy.PlatformOptions + Workspace *provider2.Workspace + SubstitutionContext *config.SubstitutionContext + MergedConfig *config.MergedDevContainerConfig + Lifecycle LifecycleOptions +} + +func BuildWorkspaceDaemonConfig(in WorkspaceDaemonInput) (*DaemonConfig, error) { + platformOptions, workspaceConfig := in.PlatformOptions, in.Workspace + substitutionContext, mergedConfig, lifecycle := in.SubstitutionContext, in.MergedConfig, in.Lifecycle + workdir := containerWorkdir(workspaceConfig, substitutionContext, mergedConfig) daemonConfig := &DaemonConfig{ Platform: platformOptions, @@ -81,20 +73,8 @@ func BuildWorkspaceDaemonConfig( return daemonConfig, nil } -func GetEncodedWorkspaceDaemonConfig( - platformOptions devsy.PlatformOptions, - workspaceConfig *provider2.Workspace, - substitutionContext *config.SubstitutionContext, - mergedConfig *config.MergedDevContainerConfig, - lifecycle LifecycleOptions, -) (string, error) { - daemonConfig, err := BuildWorkspaceDaemonConfig( - platformOptions, - workspaceConfig, - substitutionContext, - mergedConfig, - lifecycle, - ) +func GetEncodedWorkspaceDaemonConfig(in WorkspaceDaemonInput) (string, error) { + daemonConfig, err := BuildWorkspaceDaemonConfig(in) if err != nil { return "", err } @@ -554,3 +534,27 @@ func isDaemonProcess(pid string) bool { baseName = strings.TrimSuffix(baseName, " (deleted)") return baseName == pkgconfig.BinaryName } + +// containerWorkdir resolves the working directory inside the container, with +// the git sub path applied to the substitution context when there is one. +func containerWorkdir( + workspaceConfig *provider2.Workspace, + substitutionContext *config.SubstitutionContext, + mergedConfig *config.MergedDevContainerConfig, +) string { + var workdir string + if workspaceConfig.Source.GitSubPath != "" { + substitutionContext.ContainerWorkspaceFolder = filepath.Join( + substitutionContext.ContainerWorkspaceFolder, + workspaceConfig.Source.GitSubPath, + ) + workdir = substitutionContext.ContainerWorkspaceFolder + } + if workdir == "" && mergedConfig != nil { + workdir = mergedConfig.WorkspaceFolder + } + if workdir == "" && substitutionContext != nil { + workdir = substitutionContext.ContainerWorkspaceFolder + } + return workdir +} diff --git a/pkg/daemon/agent/daemon_test.go b/pkg/daemon/agent/daemon_test.go index d205f3b424..e9f79594c7 100644 --- a/pkg/daemon/agent/daemon_test.go +++ b/pkg/daemon/agent/daemon_test.go @@ -151,11 +151,13 @@ func TestBuildWorkspaceDaemonConfig_ShutdownAction(t *testing.T) { merged.ShutdownAction = tt.shutdownAction cfg, err := BuildWorkspaceDaemonConfig( - devsy.PlatformOptions{}, - &provider2.Workspace{}, - &config.SubstitutionContext{}, - merged, - LifecycleOptions{}, + WorkspaceDaemonInput{ + PlatformOptions: devsy.PlatformOptions{}, + Workspace: &provider2.Workspace{}, + SubstitutionContext: &config.SubstitutionContext{}, + MergedConfig: merged, + Lifecycle: LifecycleOptions{}, + }, ) if err != nil { t.Fatalf("unexpected error: %v", err) diff --git a/pkg/daemon/platform/daemon.go b/pkg/daemon/platform/daemon.go index 98151730d8..f3e52cbdf3 100644 --- a/pkg/daemon/platform/daemon.go +++ b/pkg/daemon/platform/daemon.go @@ -45,14 +45,13 @@ func Init(ctx context.Context, config InitConfig) (*Daemon, error) { } platformConfig := config.PlatformClient.Config() - tsServer, lc, err := newTSServer( - ctx, - platformConfig.Host, - platformConfig.AccessKey, - config.UserName, - config.RootDir, - platformConfig.Insecure, - ) + tsServer, lc, err := newTSServer(ctx, tsServerConfig{ + Host: platformConfig.Host, + AccessKey: platformConfig.AccessKey, + UserName: config.UserName, + RootDir: config.RootDir, + Insecure: platformConfig.Insecure, + }) if err != nil { return nil, fmt.Errorf("create tailscale server: %w", err) } diff --git a/pkg/daemon/platform/local_server.go b/pkg/daemon/platform/local_server.go index 5a839a8342..72f98010d5 100644 --- a/pkg/daemon/platform/local_server.go +++ b/pkg/daemon/platform/local_server.go @@ -57,7 +57,10 @@ var ( DaemonStatePending DaemonState = "pending" ) -const platformStatusCheckInterval = 10 * time.Second +const ( + platformStatusCheckInterval = 10 * time.Second + readHeaderTimeout = 10 * time.Second +) type platformStatus struct { mu sync.RWMutex @@ -81,7 +84,7 @@ var ( routeGetUserProfile = "/user-profile" routeUpdateUserProfile = "/update-user-profile" routeGitCredentials = "/git-credentials" - routeDockerCredentials = "/docker-credentials" + routeDockerCredentials = "/docker-credentials" //nolint:gosec // route path, not a credential ) func newLocalServer( @@ -122,7 +125,7 @@ func newLocalServer( handlers.PrintRecoveryStack(true), )(mux) handler = handlers.LoggingHandler(log.Writer(log.LevelDebug), handler) - l.httpServer = &http.Server{Handler: handler} + l.httpServer = &http.Server{Handler: handler, ReadHeaderTimeout: readHeaderTimeout} return l } diff --git a/pkg/daemon/platform/socket.go b/pkg/daemon/platform/socket.go index be1606e1f6..d281207662 100644 --- a/pkg/daemon/platform/socket.go +++ b/pkg/daemon/platform/socket.go @@ -31,6 +31,7 @@ func listen(addr string) (net.Listener, error) { sockDir := filepath.Dir(addr) if _, err := os.Stat(sockDir); errors.Is(err, os.ErrNotExist) { + //nolint:gosec // socket directory must be readable by the daemon user _ = os.MkdirAll(sockDir, 0o755) // best effort } pipe, err := net.Listen("unix", addr) diff --git a/pkg/daemon/platform/ts_server.go b/pkg/daemon/platform/ts_server.go index 5694116b48..8bbaf2be7b 100644 --- a/pkg/daemon/platform/ts_server.go +++ b/pkg/daemon/platform/ts_server.go @@ -16,11 +16,17 @@ import ( "tailscale.com/types/logger" ) -func newTSServer( - ctx context.Context, - host, accessKey, userName, rootDir string, - insecure bool, -) (*tsnet.Server, *local.Client, error) { +// tsServerConfig describes the tailnet server to start for the platform. +type tsServerConfig struct { + Host string + AccessKey string + UserName string + RootDir string + Insecure bool +} + +func newTSServer(ctx context.Context, cfg tsServerConfig) (*tsnet.Server, *local.Client, error) { + host, accessKey, userName, rootDir, insecure := cfg.Host, cfg.AccessKey, cfg.UserName, cfg.RootDir, cfg.Insecure // Build the platform URL baseUrl := url.URL{ Scheme: ts.GetEnvOrDefault("DEVSY_TSNET_SCHEME", "https"), diff --git a/pkg/devcontainer/build.go b/pkg/devcontainer/build.go index b15b65bade..53fc888c54 100644 --- a/pkg/devcontainer/build.go +++ b/pkg/devcontainer/build.go @@ -175,9 +175,11 @@ func (r *runner) buildAndExtendImage( imageBuildInfo, err := r.getImageBuildInfoFromDockerfile( ctx, substitutionContext, - string(base.content), - parsedConfig.Config.GetArgs(), - parsedConfig.Config.GetTarget(), + dockerfileInput{ + content: string(base.content), + buildArgs: parsedConfig.Config.GetArgs(), + target: parsedConfig.Config.GetTarget(), + }, ) if err != nil { return nil, fmt.Errorf("get image build info: %w", err) @@ -287,13 +289,19 @@ func imageBuildInfoFromDetails( }, nil } +// dockerfileInput is the Dockerfile content and build settings to inspect. +type dockerfileInput struct { + content string + buildArgs map[string]string + target string +} + func (r *runner) getImageBuildInfoFromDockerfile( ctx context.Context, substitutionContext *config.SubstitutionContext, - dockerFileContent string, - buildArgs map[string]string, - target string, + input dockerfileInput, ) (*config.ImageBuildInfo, error) { + dockerFileContent, buildArgs, target := input.content, input.buildArgs, input.target parsedDockerfile, err := dockerfile.Parse(dockerFileContent) if err != nil { return nil, fmt.Errorf("parse dockerfile: %w", err) @@ -477,6 +485,7 @@ func (r *runner) buildImage( return nil, err } if prebuilt != nil { + buildkit.RecordSkipped(ctx, "prebuild", "prebuild_reused") return prebuilt, nil } } diff --git a/pkg/devcontainer/build_record_test.go b/pkg/devcontainer/build_record_test.go new file mode 100644 index 0000000000..fa074cf6ac --- /dev/null +++ b/pkg/devcontainer/build_record_test.go @@ -0,0 +1,209 @@ +package devcontainer + +import ( + "context" + "net/http/httptest" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + "github.com/devsy-org/devsy/pkg/compose" + "github.com/devsy-org/devsy/pkg/devcontainer/config" + "github.com/devsy-org/devsy/pkg/devcontainer/feature" + "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/google/go-containerregistry/pkg/name" + "github.com/google/go-containerregistry/pkg/registry" + "github.com/google/go-containerregistry/pkg/v1/random" + "github.com/google/go-containerregistry/pkg/v1/remote" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/zap/zapcore" +) + +const ( + testBuildPhase = "build" + testArch = "amd64" +) + +var testComposeArgs = []string{overlayBuildField} + +func recordedContext(t *testing.T) (context.Context, *workspacelogs.Store, string) { + t.Helper() + store, err := workspacelogs.Open(workspacelogs.Options{Root: t.TempDir()}) + require.NoError(t, err) + m, err := store.CreateRun(workspacelogs.RunSpec{ + Context: recordTestContext, WorkspaceUID: "uid", Command: "up", + }) + require.NoError(t, err) + rec, err := workspacelogs.NewRecorder(workspacelogs.RecorderOptions{ + Backend: store, RunID: m.RunID, Location: workspacelogs.LocationProviderHost, + }) + require.NoError(t, err) + + return workspacelogs.WithRecorder(t.Context(), rec), store, m.RunID +} + +func readRecords(t *testing.T, store *workspacelogs.Store, runID string) []workspacelogs.Record { + t.Helper() + var all []workspacelogs.Record + req := workspacelogs.ReadRequest{RunID: runID} + for { + resp, err := store.Read(req) + require.NoError(t, err) + all = append(all, resp.Records...) + if !resp.HasMore { + return all + } + req.After = resp.NextCursor + } +} + +func buildInvocations(recs []workspacelogs.Record) []workspacelogs.Record { + var out []workspacelogs.Record + for i := range recs { + if recs[i].Kind == workspacelogs.KindInvocation { + out = append(out, recs[i]) + } + } + + return out +} + +func combinedText(recs []workspacelogs.Record) string { + var b strings.Builder + for i := range recs { + if recs[i].Kind == workspacelogs.KindOutput && + recs[i].Stream == workspacelogs.StreamCombined { + b.WriteString(recs[i].Text) + } + } + + return b.String() +} + +func composeScript(t *testing.T, body string) *compose.ComposeHelper { + t.Helper() + path := filepath.Join(t.TempDir(), "compose-fake") + //nolint:gosec // test helper script needs exec bit + require.NoError(t, os.WriteFile(path, []byte("#!/bin/sh\n"+body+"\n"), 0o755)) + + return &compose.ComposeHelper{Command: path} +} + +func TestRunComposeBuildRetainsOutputAtWarnLevel(t *testing.T) { + logs := log.InitTestObserved(t, zapcore.WarnLevel) + ctx, store, runID := recordedContext(t) + helper := composeScript(t, "echo 'compose out'; echo 'compose err' >&2") + + err := (&runner{}).runComposeBuild(ctx, helper, testComposeArgs) + + require.NoError(t, err) + assert.Empty(t, logs.All()) + recs := readRecords(t, store, runID) + // Compose pipes stdout and stderr separately, so the order is not fixed. + assert.Contains(t, combinedText(recs), "compose out\n") + assert.Contains(t, combinedText(recs), "compose err\n") + inv := buildInvocations(recs) + require.Len(t, inv, 2) + assert.Equal(t, "compose", inv[0].HookKey) + assert.Equal(t, testBuildPhase, inv[0].Phase) + assert.Empty(t, inv[0].Hook) + assert.Equal(t, workspacelogs.InvocationSucceeded, inv[1].State) +} + +func TestRunComposeBuildFailureKeepsErrorAndExitCode(t *testing.T) { + log.InitTestObserved(t, zapcore.WarnLevel) + ctx, store, runID := recordedContext(t) + helper := composeScript(t, "echo 'service failed' >&2; exit 4") + + err := (&runner{}).runComposeBuild(ctx, helper, testComposeArgs) + + require.Error(t, err) + assert.Contains(t, err.Error(), "service failed") + var exitErr *exec.ExitError + require.ErrorAs(t, err, &exitErr) + recs := readRecords(t, store, runID) + assert.Equal(t, "service failed\n", combinedText(recs)) + inv := buildInvocations(recs) + require.Len(t, inv, 2) + assert.Equal(t, workspacelogs.InvocationFailed, inv[1].State) + require.NotNil(t, inv[1].ExitCode) + assert.Equal(t, 4, *inv[1].ExitCode) +} + +func TestRunComposeBuildCanceledIsRecorded(t *testing.T) { + log.InitTestObserved(t, zapcore.WarnLevel) + ctx, store, runID := recordedContext(t) + ctx, cancel := context.WithCancel(ctx) + cancel() + + err := (&runner{}).runComposeBuild(ctx, composeScript(t, "sleep 5"), testComposeArgs) + + require.Error(t, err) + inv := buildInvocations(readRecords(t, store, runID)) + require.Len(t, inv, 2) + assert.Equal(t, workspacelogs.InvocationCanceled, inv[1].State) +} + +func TestRunComposeBuildWithoutRecorderLogsOnly(t *testing.T) { + logs := log.InitTestObserved(t, zapcore.InfoLevel) + + err := (&runner{}).runComposeBuild( + t.Context(), composeScript(t, "echo plain"), testComposeArgs, + ) + + require.NoError(t, err) + require.Len(t, logs.All(), 1) + assert.Equal(t, "plain", logs.All()[0].Message) +} + +func TestBuildImageRecordsPrebuildReuseWithoutOutput(t *testing.T) { + log.InitTestObserved(t, zapcore.WarnLevel) + ctx, store, runID := recordedContext(t) + server := httptest.NewServer(registry.New()) + t.Cleanup(server.Close) + repo := strings.TrimPrefix(server.URL, "http://") + "/prebuilds" + contextDir := t.TempDir() + parsed := &config.SubstitutedConfig{Config: &config.DevContainerConfig{}} + parsed.Config.Origin = filepath.Join(contextDir, "devcontainer.json") + hash, err := config.CalculatePrebuildHash(config.PrebuildHashParams{ + Config: parsed.Config, + Architecture: testArch, + ContextPath: config.GetContextPath(parsed.Config), + }) + require.NoError(t, err) + img, err := random.Image(64, 1) + require.NoError(t, err) + ref, err := name.ParseReference(repo + ":" + hash) + require.NoError(t, err) + require.NoError(t, remote.Write(ref, img)) + r := newTestRunner(&mockDriver{}) + r.imageBackend = &separateImages{ + details: &config.ImageDetails{ID: "prebuilt"}, + buildInfo: &config.BuildInfo{ImageName: "must-not-build"}, + } + + info, err := r.buildImage(ctx, &buildImageParams{ + parsedConfig: parsed, + extendedBuildInfo: &feature.ExtendedBuildInfo{}, + options: provider.BuildOptions{ + CLIOptions: provider.CLIOptions{PrebuildRepositories: []string{repo}}, + }, + }) + + require.NoError(t, err) + assert.Equal(t, repo+":"+hash, info.ImageName) + recs := readRecords(t, store, runID) + inv := buildInvocations(recs) + require.Len(t, inv, 1) + assert.Equal(t, workspacelogs.InvocationSkipped, inv[0].State) + assert.Equal(t, "prebuild_reused", inv[0].Reason) + assert.Equal(t, testBuildPhase, inv[0].Phase) + for i := range recs { + assert.NotEqual(t, workspacelogs.KindOutput, recs[i].Kind, "no build ran, so no output") + } +} diff --git a/pkg/devcontainer/build_test.go b/pkg/devcontainer/build_test.go index d8d75eba50..e2adcba9a6 100644 --- a/pkg/devcontainer/build_test.go +++ b/pkg/devcontainer/build_test.go @@ -6,6 +6,10 @@ import ( "gotest.tools/assert" ) +const ( + workspacesTest = "/workspaces/test" +) + func TestFallbackContainerContextAndDockerfile(t *testing.T) { testCases := []struct { name string @@ -22,7 +26,7 @@ func TestFallbackContainerContextAndDockerfile(t *testing.T) { name: "simple", localFolder: "/my/local/folder", - remoteFolder: "/workspaces/test", + remoteFolder: workspacesTest, context: "/my/local/folder/context", dockerfile: "/my/local/folder/Dockerfile", @@ -33,11 +37,11 @@ func TestFallbackContainerContextAndDockerfile(t *testing.T) { name: "windows", localFolder: "C:/my/local/folder", - remoteFolder: "/workspaces/test", + remoteFolder: workspacesTest, context: "C:/my/local/folder", dockerfile: "C:/my/local/folder/Dockerfile", - expectedContext: "/workspaces/test", + expectedContext: workspacesTest, expectedDockerfile: "/workspaces/test/Dockerfile", }, } diff --git a/pkg/devcontainer/buildkit/buildkit.go b/pkg/devcontainer/buildkit/buildkit.go index 491c432f07..529bcc8412 100644 --- a/pkg/devcontainer/buildkit/buildkit.go +++ b/pkg/devcontainer/buildkit/buildkit.go @@ -14,16 +14,19 @@ import ( buildkit "github.com/moby/buildkit/client" "github.com/moby/buildkit/session" "github.com/moby/buildkit/session/auth/authprovider" + "github.com/moby/buildkit/util/progress/progresswriter" "github.com/tonistiigi/fsutil" ) -func Build( - ctx context.Context, - client *buildkit.Client, - writer io.Writer, - platform string, - options *build.BuildOptions, -) error { +// BuildRequest describes one image build through BuildKit. +type BuildRequest struct { + Writer io.Writer + Platform string + Options *build.BuildOptions +} + +func Build(ctx context.Context, client *buildkit.Client, req BuildRequest) error { + writer, platform, options := req.Writer, req.Platform, req.Options solveOptions, err := buildkitSolveOpt(platform, options) if err != nil { return err @@ -34,8 +37,21 @@ func Build( return err } - // build - _, err = client.Solve(ctx, nil, solveOptions, pw.Status()) + return solve(ctx, client, solveOptions, pw) +} + +// solve runs the build and waits for the printer to render the final status. +// Solve always closes the status channel, so the printer finishes on every +// path and no output is lost before the caller closes its writers. +func solve( + ctx context.Context, + client *buildkit.Client, + opts buildkit.SolveOpt, + pw progresswriter.Writer, +) error { + _, err := client.Solve(ctx, nil, opts, pw.Status()) + <-pw.Done() + return err } diff --git a/pkg/devcontainer/buildkit/record.go b/pkg/devcontainer/buildkit/record.go new file mode 100644 index 0000000000..1c7b15ab77 --- /dev/null +++ b/pkg/devcontainer/buildkit/record.go @@ -0,0 +1,105 @@ +package buildkit + +import ( + "context" + "errors" + "io" + "os/exec" + "time" + + "github.com/devsy-org/devsy/pkg/secrets" + "github.com/devsy-org/devsy/pkg/workspacelogs" +) + +const buildPhase = "build" + +// Step records one build step as an invocation in the run found in the +// context. A nil Step does nothing, so callers need no recorder checks. +type Step struct { + rec *workspacelogs.Recorder + id string + started time.Time + writers []io.Closer + masked *secrets.Redactor +} + +// StartStep marks a build step running. It returns nil when no run is being +// recorded. +func StartStep(ctx context.Context, key string, buildSecrets ...string) *Step { + rec := workspacelogs.RecorderFrom(ctx) + if rec == nil { + return nil + } + s := &Step{ + rec: rec, id: workspacelogs.NewID(), started: time.Now(), + masked: secrets.NewRedactor(buildSecrets), + } + rec.Invocation(workspacelogs.Invocation{ + ID: s.id, Phase: buildPhase, Key: key, State: workspacelogs.InvocationRunning, + }) + + return s +} + +// RecordSkipped records a build step that was not needed, with the reason. +func RecordSkipped(ctx context.Context, key, reason string) { + rec := workspacelogs.RecorderFrom(ctx) + if rec == nil { + return + } + rec.Invocation(workspacelogs.Invocation{ + ID: workspacelogs.NewID(), Phase: buildPhase, Key: key, + State: workspacelogs.InvocationSkipped, Reason: reason, + }) +} + +// Tee returns a writer that records redacted output of the stream before +// forwarding to dst. The recorder comes first so the text is kept even when +// dst filters or fails. Without a step it returns dst unchanged. +func (s *Step) Tee(dst io.Writer, stream workspacelogs.Stream) io.Writer { + if s == nil { + return dst + } + w := s.rec.WriterRedacting(s.id, stream, s.masked) + s.writers = append(s.writers, w) + + return io.MultiWriter(w, dst) +} + +// Finish flushes every writer, then records the terminal state for err. +func (s *Step) Finish(ctx context.Context, err error) { + if s == nil { + return + } + for _, w := range s.writers { + _ = w.Close() + } + s.writers = nil + inv := workspacelogs.Invocation{ + ID: s.id, Phase: buildPhase, State: workspacelogs.InvocationSucceeded, + Duration: time.Since(s.started), + } + if err != nil { + inv.State = failureState(ctx, err) + inv.ExitCode = exitCode(err) + } + s.rec.Invocation(inv) +} + +func failureState(ctx context.Context, err error) workspacelogs.InvocationState { + if ctx.Err() != nil || errors.Is(err, context.Canceled) { + return workspacelogs.InvocationCanceled + } + + return workspacelogs.InvocationFailed +} + +func exitCode(err error) *int { + var exitErr *exec.ExitError + if !errors.As(err, &exitErr) { + return nil + } + code := exitErr.ExitCode() + + return &code +} diff --git a/pkg/devcontainer/buildkit/record_test.go b/pkg/devcontainer/buildkit/record_test.go new file mode 100644 index 0000000000..5097f8c84b --- /dev/null +++ b/pkg/devcontainer/buildkit/record_test.go @@ -0,0 +1,222 @@ +package buildkit + +import ( + "bytes" + "context" + "errors" + "os/exec" + "strings" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/moby/buildkit/client" + digest "github.com/opencontainers/go-digest" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/zap/zapcore" +) + +func recordedContext(t *testing.T) (context.Context, *workspacelogs.Store, string) { + t.Helper() + store, err := workspacelogs.Open(workspacelogs.Options{Root: t.TempDir()}) + require.NoError(t, err) + m, err := store.CreateRun(workspacelogs.RunSpec{ + Context: "default", WorkspaceUID: "uid", Command: "up", + }) + require.NoError(t, err) + rec, err := workspacelogs.NewRecorder(workspacelogs.RecorderOptions{ + Backend: store, RunID: m.RunID, Location: workspacelogs.LocationProviderHost, + }) + require.NoError(t, err) + + return workspacelogs.WithRecorder(t.Context(), rec), store, m.RunID +} + +func readRecords(t *testing.T, store *workspacelogs.Store, runID string) []workspacelogs.Record { + t.Helper() + var all []workspacelogs.Record + req := workspacelogs.ReadRequest{RunID: runID} + for { + resp, err := store.Read(req) + require.NoError(t, err) + all = append(all, resp.Records...) + if !resp.HasMore { + return all + } + req.After = resp.NextCursor + } +} + +func outputText(recs []workspacelogs.Record, stream workspacelogs.Stream) string { + var b strings.Builder + for i := range recs { + if recs[i].Kind == workspacelogs.KindOutput && recs[i].Stream == stream { + b.WriteString(recs[i].Text) + } + } + + return b.String() +} + +func invocations(recs []workspacelogs.Record) []workspacelogs.Record { + var out []workspacelogs.Record + for i := range recs { + if recs[i].Kind == workspacelogs.KindInvocation { + out = append(out, recs[i]) + } + } + + return out +} + +func TestStepRetainsOutputBelowLogLevel(t *testing.T) { + logs := log.InitTestObserved(t, zapcore.WarnLevel) + ctx, store, runID := recordedContext(t) + logWriter := log.Writer(log.LevelInfo) + step := StartStep(ctx, "buildkit") + + w := step.Tee(logWriter, workspacelogs.StreamCombined) + _, err := w.Write([]byte("step 1/2\nlast line without newline")) + require.NoError(t, err) + _ = logWriter.Close() + step.Finish(ctx, nil) + + assert.Empty(t, logs.All(), "logger at warn drops info output") + recs := readRecords(t, store, runID) + assert.Equal( + t, + "step 1/2\nlast line without newline", + outputText(recs, workspacelogs.StreamCombined), + ) + inv := invocations(recs) + require.Len(t, inv, 2) + assert.Equal(t, workspacelogs.InvocationRunning, inv[0].State) + assert.Equal(t, "build", inv[0].Phase) + assert.Empty(t, inv[0].Hook) + assert.Equal(t, "buildkit", inv[0].HookKey) + assert.Equal(t, workspacelogs.InvocationSucceeded, inv[1].State) +} + +func TestStepFinishStates(t *testing.T) { + exitErr := exec.Command("sh", "-c", "exit 5").Run() + var ee *exec.ExitError + require.ErrorAs(t, exitErr, &ee) + canceled, cancel := context.WithCancel(t.Context()) + cancel() + + tests := []struct { + name string + err error + ctxFor func(context.Context) context.Context + state workspacelogs.InvocationState + wantExit bool + }{ + { + name: "failed with exit code", + err: exitErr, + state: workspacelogs.InvocationFailed, + wantExit: true, + }, + {name: "failed", err: errors.New("boom"), state: workspacelogs.InvocationFailed}, + { + name: "canceled", + err: context.Canceled, + ctxFor: func(context.Context) context.Context { return canceled }, + state: workspacelogs.InvocationCanceled, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx, store, runID := recordedContext(t) + step := StartStep(ctx, "buildkit") + _, _ = step.Tee(&bytes.Buffer{}, workspacelogs.StreamStderr).Write([]byte("err tail")) + finishCtx := ctx + if tt.ctxFor != nil { + finishCtx = tt.ctxFor(ctx) + } + step.Finish(finishCtx, tt.err) + + recs := readRecords(t, store, runID) + inv := invocations(recs) + require.Len(t, inv, 2) + assert.Equal(t, tt.state, inv[1].State) + assert.Equal(t, tt.wantExit, inv[1].ExitCode != nil) + if tt.wantExit { + assert.Equal(t, 5, *inv[1].ExitCode) + } + assert.Equal(t, "err tail", outputText(recs, workspacelogs.StreamStderr)) + }) + } +} + +func TestStepWithoutRecorderForwardsUnchanged(t *testing.T) { + step := StartStep(t.Context(), "buildkit") + assert.Nil(t, step) + var buf bytes.Buffer + + w := step.Tee(&buf, workspacelogs.StreamStdout) + _, _ = w.Write([]byte("plain")) + step.Finish(t.Context(), errors.New("ignored")) + + assert.Same(t, &buf, w) + assert.Equal(t, "plain", buf.String()) + RecordSkipped(t.Context(), "image", "image_reused") +} + +func TestRecordSkippedWritesReason(t *testing.T) { + ctx, store, runID := recordedContext(t) + + RecordSkipped(ctx, "image", "image_reused") + + inv := invocations(readRecords(t, store, runID)) + require.Len(t, inv, 1) + assert.Equal(t, workspacelogs.InvocationSkipped, inv[0].State) + assert.Equal(t, "image_reused", inv[0].Reason) + assert.Equal(t, "build", inv[0].Phase) +} + +// The printer renders on its own goroutine, so the last status must be drained +// before the recorder writer closes. +func TestPrinterDrainKeepsFinalBytes(t *testing.T) { + logs := log.InitTestObserved(t, zapcore.WarnLevel) + ctx, store, runID := recordedContext(t) + step := StartStep(ctx, "buildkit-remote") + dgst := digest.FromString("vertex") + now := time.Now() + + pw, err := NewPrinter(ctx, step.Tee(log.Writer(log.LevelInfo), workspacelogs.StreamCombined)) + require.NoError(t, err) + pw.Status() <- &client.SolveStatus{ + Vertexes: []*client.Vertex{{Digest: dgst, Name: "RUN make", Started: &now}}, + Logs: []*client.VertexLog{ + {Vertex: dgst, Stream: 2, Data: []byte("fatal: final error line\n"), Timestamp: now}, + }, + } + close(pw.Status()) + <-pw.Done() + step.Finish(ctx, errors.New("solve failed")) + + assert.Empty(t, logs.All()) + text := outputText(readRecords(t, store, runID), workspacelogs.StreamCombined) + assert.Contains(t, text, "fatal: final error line") + assert.Contains(t, text, "RUN make") +} + +func TestStepMasksBuildSecretsInRecordedOutput(t *testing.T) { + ctx, store, runID := recordedContext(t) + step := StartStep(ctx, "buildkit", "TOKEN=hunter2-value") + + w := step.Tee(&bytes.Buffer{}, workspacelogs.StreamCombined) + _, err := w.Write([]byte("using hunter2-value here\nsplit hunter2")) + require.NoError(t, err) + _, err = w.Write([]byte("-value end")) + require.NoError(t, err) + step.Finish(ctx, nil) + + text := outputText(readRecords(t, store, runID), workspacelogs.StreamCombined) + assert.NotContains(t, text, "hunter2") + assert.Contains(t, text, "using ") + assert.Contains(t, text, " end") +} diff --git a/pkg/devcontainer/buildkit/remote.go b/pkg/devcontainer/buildkit/remote.go index b46736d7e6..a4ae9d7ec4 100644 --- a/pkg/devcontainer/buildkit/remote.go +++ b/pkg/devcontainer/buildkit/remote.go @@ -20,6 +20,7 @@ import ( "github.com/devsy-org/devsy/pkg/image" "github.com/devsy-org/devsy/pkg/log" "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/workspacelogs" "github.com/docker/cli/cli/config/types" "github.com/google/go-containerregistry/pkg/authn" "github.com/google/go-containerregistry/pkg/name" @@ -32,6 +33,11 @@ import ( "github.com/tonistiigi/fsutil" ) +const ( + localMountContext = "context" + localMountDockerfile = "dockerfile" +) + type BuildRemoteOptions struct { PrebuildHash string ParsedConfig *config.SubstitutedConfig @@ -79,10 +85,11 @@ func BuildRemote(ctx context.Context, opts BuildRemoteOptions) (*config.BuildInf } if err := executeBuild(executeBuildParams{ - Ctx: ctx, - Client: bk.Client, - Info: bk.Info, - SolveOpts: solveOpts, + Ctx: ctx, + Client: bk.Client, + Info: bk.Info, + SolveOpts: solveOpts, + BuildSecrets: opts.Options.BuildSecrets, }); err != nil { return nil, err } @@ -230,6 +237,7 @@ func checkExistingImage(params checkExistingImageParams) *config.BuildInfo { } log.Debugf("skipping build because an existing image was found %s", params.ImageName) + RecordSkipped(params.Ctx, "buildkit-remote", "image_reused") var imageMetadata *config.ImageMetadataConfig if params.Opts.ExtendedBuildInfo != nil { @@ -261,7 +269,12 @@ func setupRegistryAuth(ref name.Reference, keychain authn.Keychain) ([]session.A registry := ref.Context().RegistryStr() return []session.Attachable{ authprovider.NewDockerAuthProvider(authprovider.DockerAuthProviderConfig{ - AuthConfigProvider: func(ctx context.Context, host string, scope []string, cacheCheck authprovider.ExpireCachedAuthCheck) (types.AuthConfig, error) { + AuthConfigProvider: func( + ctx context.Context, + host string, + scope []string, + cacheCheck authprovider.ExpireCachedAuthCheck, + ) (types.AuthConfig, error) { if host == registry { return types.AuthConfig{ Username: authConfig.Username, @@ -318,8 +331,8 @@ func prepareSolveOptions( solveOpts := client.SolveOpt{ Frontend: "dockerfile.v0", FrontendAttrs: map[string]string{ - "filename": filepath.Base(buildOpts.Dockerfile), - "context": buildOpts.Context, + "filename": filepath.Base(buildOpts.Dockerfile), + localMountContext: buildOpts.Context, }, LocalMounts: localMounts, Session: authSession, @@ -388,8 +401,8 @@ func setupLocalMounts(buildOpts *build.BuildOptions) (map[string]fsutil.FS, erro } return map[string]fsutil.FS{ - "dockerfile": dockerfileMount, - "context": contextMount, + localMountDockerfile: dockerfileMount, + localMountContext: contextMount, }, nil } @@ -444,7 +457,7 @@ func addMultiContexts(solveOpts *client.SolveOpt, buildOpts *build.BuildOptions) } localName := k - if k == "context" || k == "dockerfile" { + if k == localMountContext || k == localMountDockerfile { localName = "_" + k } @@ -459,10 +472,11 @@ func addMultiContexts(solveOpts *client.SolveOpt, buildOpts *build.BuildOptions) } type executeBuildParams struct { - Ctx context.Context - Client *client.Client - Info *client.Info - SolveOpts client.SolveOpt + Ctx context.Context + Client *client.Client + Info *client.Info + SolveOpts client.SolveOpt + BuildSecrets []string } func executeBuild(params executeBuildParams) error { @@ -472,16 +486,23 @@ func executeBuild(params executeBuildParams) error { params.Info.BuildkitVersion.Version, ) + step := StartStep(params.Ctx, "buildkit-remote", params.BuildSecrets...) + err := runRemoteSolve(params, step) + step.Finish(params.Ctx, err) + + return err +} + +func runRemoteSolve(params executeBuildParams, step *Step) error { writer := log.Writer(log.LevelInfo) defer func() { _ = writer.Close() }() - pw, err := NewPrinter(params.Ctx, writer) + pw, err := NewPrinter(params.Ctx, step.Tee(writer, workspacelogs.StreamCombined)) if err != nil { return err } - _, err = params.Client.Solve(params.Ctx, nil, params.SolveOpts, pw.Status()) - return err + return solve(params.Ctx, params.Client, params.SolveOpts, pw) } func getImageDetails( diff --git a/pkg/devcontainer/capabilities_test.go b/pkg/devcontainer/capabilities_test.go index fa7e8aba99..021772671e 100644 --- a/pkg/devcontainer/capabilities_test.go +++ b/pkg/devcontainer/capabilities_test.go @@ -17,6 +17,13 @@ import ( "github.com/stretchr/testify/require" ) +const ( + imageValue = "image" + arm64 = "arm64" + workspaceUid = "workspace-uid" + alpine = "alpine" +) + type separateImages struct { driver.ImageBackend details *config.ImageDetails @@ -60,10 +67,10 @@ func TestSeparateImageBackend(t *testing.T) { } r := newTestRunner(&mockDriver{}) r.imageBackend = backend - details, err := r.inspectImage(ctx, "image") + details, err := r.inspectImage(ctx, imageValue) require.NoError(t, err) assert.Same(t, backend.details, details) - tag, err := r.getImageTag(ctx, "image") + tag, err := r.getImageTag(ctx, imageValue) require.NoError(t, err) assert.Equal(t, "backend:tag", tag) result, err := r.executeBuild( @@ -73,7 +80,7 @@ func TestSeparateImageBackend(t *testing.T) { options: provider.BuildOptions{}, }, "hash", - "arm64", + arm64, ) require.NoError(t, err) assert.Same(t, backend.buildInfo, result) @@ -82,15 +89,15 @@ func TestSeparateImageBackend(t *testing.T) { assert.Equal(t, []string{"registry/repo:prebuild", "registry/repo:extra"}, backend.tags) assert.Equal(t, backend.tags, backend.pushes) backend.err = errors.New("backend unavailable") - _, err = r.inspectImage(ctx, "image") + _, err = r.inspectImage(ctx, imageValue) assert.ErrorIs(t, err, backend.err) - _, err = r.getImageTag(ctx, "image") + _, err = r.getImageTag(ctx, imageValue) assert.ErrorIs(t, err, backend.err) _, err = r.executeBuild( ctx, &buildImageParams{parsedConfig: &config.SubstitutedConfig{}}, "hash", - "arm64", + arm64, ) assert.ErrorIs(t, err, backend.err) } @@ -115,12 +122,12 @@ func TestDeliveryArchitectureUsesRuntime(t *testing.T) { provider.KubernetesDriver, provider.MicrosandboxDriver, "external", } { t.Run(name, func(t *testing.T) { - d := &architectureDriver{arch: "arm64"} + d := &architectureDriver{arch: arm64} r := newTestRunner(d) r.workspaceConfig.Agent.Driver = name arch, err := r.deliveryArch(context.Background()) require.NoError(t, err) - assert.Equal(t, "arm64", arch) + assert.Equal(t, arm64, arch) assert.Equal(t, r.id, d.workspaceID) d.arch = "" _, err = r.deliveryArch(context.Background()) @@ -176,7 +183,7 @@ func TestBuildBackendSelection(t *testing.T) { _, err = r.executeBuild(context.Background(), params, "hash", "amd64") require.ErrorContains(t, err, "dockerless fallback is disabled") r.imageBackend = nil - tag, err := r.getImageTag(context.Background(), "image") + tag, err := r.getImageTag(context.Background(), imageValue) require.NoError(t, err) assert.Empty(t, tag) _, err = r.Build(context.Background(), provider.BuildOptions{}) @@ -216,7 +223,7 @@ func (d *imageOnlyRuntime) RunImageDevContainer( func TestImageRuntimeDoesNotNeedImageBackend(t *testing.T) { d := &imageOnlyRuntime{} r := newTestRunner(d) - r.workspaceConfig.Workspace = &provider.Workspace{UID: "workspace-uid"} + r.workspaceConfig.Workspace = &provider.Workspace{UID: workspaceUid} params := &resolveParams{ parsedConfig: &config.SubstitutedConfig{Config: &config.DevContainerConfig{}}, substitutionContext: &config.SubstitutionContext{}, @@ -227,7 +234,11 @@ func TestImageRuntimeDoesNotNeedImageBackend(t *testing.T) { } require.NoError( t, - r.runContainer(context.Background(), params, &config.MergedDevContainerConfig{}, info, nil), + r.runContainer( + context.Background(), + params, + containerLaunch{&config.MergedDevContainerConfig{}, info, nil}, + ), ) require.NotNil(t, d.params) assert.Equal(t, r.id, d.params.WorkspaceID) @@ -257,11 +268,11 @@ func TestImageRunValidationPreservesContainerBeforeRecreate(t *testing.T) { d.validationErr = sentinel } r := newTestRunner(d) - r.workspaceConfig.Workspace = &provider.Workspace{ID: r.id, UID: "workspace-uid"} - r.imageBackend = &separateImages{details: &config.ImageDetails{ID: "image"}} + r.workspaceConfig.Workspace = &provider.Workspace{ID: r.id, UID: workspaceUid} + r.imageBackend = &separateImages{details: &config.ImageDetails{ID: imageValue}} r.reporter = status.Nop() p := recreateResolveParams() - p.parsedConfig.Config.Image = "alpine" + p.parsedConfig.Config.Image = alpine p.parsedConfig.Raw = config.CloneDevContainerConfig(p.parsedConfig.Config) p.substitutionContext = &config.SubstitutionContext{} _, _, err := r.buildNewContainerConfig(context.Background(), p) @@ -322,14 +333,14 @@ func TestBuildNewContainerConfigRecoveryDisabledWrapsRecoverable(t *testing.T) { d := &mockDriver{} r := newTestRunner(d) r.imageBackend = &separateImages{ - details: &config.ImageDetails{ID: "image"}, + details: &config.ImageDetails{ID: imageValue}, err: buildErr, } r.reporter = status.Nop() p := recreateResolveParams() p.options.Recovery = false - p.parsedConfig.Config.Image = "alpine" + p.parsedConfig.Config.Image = alpine p.parsedConfig.Raw = config.CloneDevContainerConfig(p.parsedConfig.Config) p.substitutionContext = &config.SubstitutionContext{} @@ -426,16 +437,16 @@ func TestBuildNewContainerConfigRecoveryEnabledFailureDoesNotSetRecovering(t *te func TestBuildNewContainerConfigRecreateNonValidatorDeletesExisting(t *testing.T) { d := &mockDriver{} r := newTestRunner(d) - r.workspaceConfig.Workspace = &provider.Workspace{ID: r.id, UID: "workspace-uid"} + r.workspaceConfig.Workspace = &provider.Workspace{ID: r.id, UID: workspaceUid} r.imageBackend = &separateImages{ - details: &config.ImageDetails{ID: "image"}, + details: &config.ImageDetails{ID: imageValue}, buildInfo: &config.BuildInfo{ImageName: "built-image"}, } r.reporter = status.Nop() p := recreateResolveParams() p.options.Recreate = true - p.parsedConfig.Config.Image = "alpine" + p.parsedConfig.Config.Image = alpine p.parsedConfig.Raw = config.CloneDevContainerConfig(p.parsedConfig.Config) p.substitutionContext = &config.SubstitutionContext{} diff --git a/pkg/devcontainer/compose.go b/pkg/devcontainer/compose.go index 85d386ad7f..f691e73701 100644 --- a/pkg/devcontainer/compose.go +++ b/pkg/devcontainer/compose.go @@ -14,6 +14,7 @@ import ( pkgconfig "github.com/devsy-org/devsy/pkg/config" "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/devcontainer/metadata" + "github.com/devsy-org/devsy/pkg/docker" "github.com/devsy-org/devsy/pkg/driver" "github.com/devsy-org/devsy/pkg/flags/names" "github.com/devsy-org/devsy/pkg/log" @@ -347,23 +348,8 @@ func (r *runner) ensureComposeContainer( log.Info("workspace secrets require secure runtime mounts") } - // container already exists and is running, nothing to do - if containerDetails != nil && containerDetails.State.Status == config.ContainerStatusRunning && - !effectiveOptions.Recreate { - return containerDetails, nil - } - - if containerDetails != nil { - containerDetails, didStartProject := r.tryStartExistingProject(ctx, &existingProjectParams{ - parsedConfig: parsedConfig, - composeHelper: composeHelper, - project: project, - container: containerDetails, - recreate: effectiveOptions.Recreate, - }) - if didStartProject { - return containerDetails, nil - } + if existing, ok := r.reuseExistingCompose(ctx, params, containerDetails, effectiveOptions); ok { + return existing, nil } containerDetails, err = r.startContainer(ctx, &startContainerParams{ @@ -386,6 +372,30 @@ func (r *runner) ensureComposeContainer( return containerDetails, nil } +// reuseExistingCompose returns the existing container when it is already +// running, or after starting its existing project; ok is false when a fresh +// start is needed. +func (r *runner) reuseExistingCompose( + ctx context.Context, + params *composeContainerParams, + containerDetails *config.ContainerDetails, + options UpOptions, +) (*config.ContainerDetails, bool) { + if containerDetails == nil { + return nil, false + } + if containerDetails.State.Status == config.ContainerStatusRunning && !options.Recreate { + return containerDetails, true + } + return r.tryStartExistingProject(ctx, &existingProjectParams{ + parsedConfig: params.runParams.parsedConfig, + composeHelper: params.composeHelper, + project: params.project, + container: containerDetails, + recreate: options.Recreate, + }) +} + func (r *runner) composeSecretRuntimeOptions( details *config.ContainerDetails, options UpOptions, @@ -411,15 +421,13 @@ func (r *runner) composeSecretRuntimeOptions( // finalizeComposeContainer merges the container's metadata config and sets up // the running container. -func (r *runner) finalizeComposeContainer( - ctx context.Context, - runParams *runContainerParams, - project *composetypes.Project, +// verifyComposeContainer checks the container's compose contract and identity +// before its image metadata is trusted. +func (r *runner) verifyComposeContainer( containerDetails *config.ContainerDetails, -) (*config.Result, error) { - parsedConfig := runParams.parsedConfig - substitutionContext := runParams.substitutionContext - options := runParams.options + project *composetypes.Project, + parsedConfig *config.SubstitutedConfig, +) (*composeContract, error) { contract, err := verifiedComposeContract(containerDetails) if err != nil { return nil, err @@ -432,6 +440,23 @@ func (r *runner) finalizeComposeContainer( return nil, err } + return contract, nil +} + +func (r *runner) finalizeComposeContainer( + ctx context.Context, + runParams *runContainerParams, + project *composetypes.Project, + containerDetails *config.ContainerDetails, +) (*config.Result, error) { + parsedConfig := runParams.parsedConfig + substitutionContext := runParams.substitutionContext + options := runParams.options + contract, err := r.verifyComposeContainer(containerDetails, project, parsedConfig) + if err != nil { + return nil, err + } + imageMetadataConfig, err := metadata.GetImageMetadataFromContainer( containerDetails, substitutionContext, @@ -439,12 +464,7 @@ func (r *runner) finalizeComposeContainer( if err != nil { return nil, fmt.Errorf("get image metadata from container: %w", err) } - if contract != nil && contract.Overlay && - (parsedConfig.Overlay != nil || options.ExtraDevContainerPath != "") { - // Explicit up replaces the saved overlay entry; SSH uses it unchanged. - imageMetadataConfig.Raw = imageMetadataConfig.Raw[1:] - imageMetadataConfig.Config = imageMetadataConfig.Config[1:] - } + dropReplacedOverlay(contract, parsedConfig, options, imageMetadataConfig) mergedConfig, err := mergeImageMetadataConfig( ctx, @@ -461,14 +481,7 @@ func (r *runner) finalizeComposeContainer( return nil, err } - // expose the compose project name inside the container - if mergedConfig.RemoteEnv == nil { - mergedConfig.RemoteEnv = map[string]*string{} - } - composeName := project.Name - mergedConfig.RemoteEnv["DEVSY_COMPOSE_PROJECT_NAME"] = &composeName - composeAlias := project.Name - mergedConfig.RemoteEnv["COMPOSE_PROJECT_NAME"] = &composeAlias + exposeComposeProjectName(mergedConfig, project.Name) hostWarnings, err := r.composeHostWarnings(parsedConfig, substitutionContext, options) if err != nil { @@ -487,6 +500,33 @@ func (r *runner) finalizeComposeContainer( }) } +// dropReplacedOverlay removes the saved overlay entry when an explicit up +// replaces it; SSH uses it unchanged. +func dropReplacedOverlay( + contract *composeContract, + parsedConfig *config.SubstitutedConfig, + options UpOptions, + imageMetadataConfig *config.ImageMetadataConfig, +) { + if contract == nil || !contract.Overlay || + (parsedConfig.Overlay == nil && options.ExtraDevContainerPath == "") { + return + } + imageMetadataConfig.Raw = imageMetadataConfig.Raw[1:] + imageMetadataConfig.Config = imageMetadataConfig.Config[1:] +} + +// exposeComposeProjectName sets the compose project name inside the container. +func exposeComposeProjectName(mergedConfig *config.MergedDevContainerConfig, name string) { + if mergedConfig.RemoteEnv == nil { + mergedConfig.RemoteEnv = map[string]*string{} + } + composeName := name + mergedConfig.RemoteEnv["DEVSY_COMPOSE_PROJECT_NAME"] = &composeName + composeAlias := name + mergedConfig.RemoteEnv["COMPOSE_PROJECT_NAME"] = &composeAlias +} + // updateContainerUserUID updates the container user's UID/GID on Docker drivers. func (r *runner) updateContainerUserUID( ctx context.Context, @@ -598,7 +638,11 @@ func (r *runner) composeUpExistingProject( writer := log.Writer(log.LevelInfo) defer func() { _ = writer.Close() }() - if err := params.composeHelper.Run(ctx, upArgs, nil, writer, writer); err != nil { + if err := params.composeHelper.Run( + ctx, + upArgs, + docker.Streams{Stdout: writer, Stderr: writer}, + ); err != nil { log.Debugf("could not reuse persisted compose project: %v", err) return nil, err } @@ -852,17 +896,9 @@ func (r *runner) startContainer( return nil, err } - if r.overlayExisting != nil && options.Recreate { - if err := r.teardownOverlayExisting(ctx); err != nil { - return nil, err - } - r.overlayExisting = nil - container = nil - } - if container != nil && options.Recreate { - if err := r.recreateDevContainer(ctx, container); err != nil { - return nil, err - } + container, err = r.prepareComposeRecreate(ctx, container, options) + if err != nil { + return nil, err } return r.composeUpAndVerifyContainer(ctx, &composeUpRunParams{ @@ -875,6 +911,31 @@ func (r *runner) startContainer( }, overrides.contract, container == nil || options.Recreate) } +// prepareComposeRecreate tears down state replaced by a recreate and returns +// the container to reuse, which is nil when an overlay was torn down. +func (r *runner) prepareComposeRecreate( + ctx context.Context, + container *config.ContainerDetails, + options UpOptions, +) (*config.ContainerDetails, error) { + if !options.Recreate { + return container, nil + } + if r.overlayExisting != nil { + if err := r.teardownOverlayExisting(ctx); err != nil { + return nil, err + } + r.overlayExisting = nil + return nil, nil + } + if container != nil { + if err := r.recreateDevContainer(ctx, container); err != nil { + return nil, err + } + } + return container, nil +} + func (r *runner) resolveComposeStartOverrides( ctx context.Context, params *composeOverrideParams, @@ -1060,6 +1121,39 @@ func resolveComposeRuntimeImage( // generateComposeUpOverride merges the image metadata into the devcontainer // config and writes the compose "up" override file, returning its path. +// composeOverrideLabels labels the override image with the metadata and contract +// that a later up reads back to decide whether the image is still current. +func composeOverrideLabels( + imageDetails *config.ImageDetails, + metadataValue, contract []byte, + structure string, +) map[string]string { + return map[string]string{ + metadata.ImageMetadataLabel: string(metadataValue), + composeContractLabel: string(contract), + config.UserLabel: imageDetails.Config.User, + overlayStructureLabel: structure, + } +} + +// marshalComposeContract records what the compose override was built from, so a +// later up can tell whether the image is still current. +func (r *runner) marshalComposeContract( + imageDetails *config.ImageDetails, + metadataValue, featureSignature string, + hasOverlay bool, +) ([]byte, error) { + return json.Marshal(composeContract{ + Version: 1, + Workspace: r.id, + Image: imageDetails.ID, + Manifest: imageDetails.PlatformManifest, + Metadata: metadataDigest(metadataValue), + Features: featureSignature, + Overlay: hasOverlay, + }) +} + func (r *runner) generateComposeUpOverride( ctx context.Context, params *composeOverrideParams, @@ -1082,13 +1176,7 @@ func (r *runner) generateComposeUpOverride( if err != nil { return composeOverrideFile{}, err } - rawMetadata := extendResult.imageMetadata.Raw - if overlay != nil { - // Persist only the established runtime metadata fields, excluding feature - // options, host initialization, and source-selection inputs. - rawMetadata = append([]*config.ImageMetadata(nil), rawMetadata...) - rawMetadata[0] = metadata.DevContainerConfigToImageMetadata(overlay) - } + rawMetadata := overlayRawMetadata(extendResult.imageMetadata.Raw, overlay) metadataValue, err := metadata.MarshalImageMetadata(rawMetadata) if err != nil { return composeOverrideFile{}, err @@ -1098,24 +1186,18 @@ func (r *runner) generateComposeUpOverride( return composeOverrideFile{}, err } - contract, err := json.Marshal(composeContract{ - Version: 1, - Workspace: r.id, - Image: imageDetails.ID, - Manifest: imageDetails.PlatformManifest, - Metadata: metadataDigest(string(metadataValue)), - Features: featureSignature, - Overlay: overlay != nil, - }) + contract, err := r.marshalComposeContract( + imageDetails, + string(metadataValue), + featureSignature, + overlay != nil, + ) if err != nil { return composeOverrideFile{}, err } - additionalLabels := map[string]string{ - metadata.ImageMetadataLabel: string(metadataValue), - composeContractLabel: string(contract), - config.UserLabel: imageDetails.Config.User, - overlayStructureLabel: structuralSignature(start.parsedConfig.Config), - } + additionalLabels := composeOverrideLabels( + imageDetails, metadataValue, contract, structuralSignature(start.parsedConfig.Config), + ) overrideComposeUpFilePath, err := r.extendedDockerComposeUp(&composeUpParams{ parsedConfig: start.parsedConfig, mergedConfig: mergedConfig, @@ -1135,6 +1217,21 @@ func (r *runner) generateComposeUpOverride( return composeOverrideFile{path: overrideComposeUpFilePath, contract: string(contract)}, nil } +// overlayRawMetadata persists only the established runtime metadata fields of +// the overlay, excluding feature options, host initialization, and +// source-selection inputs. +func overlayRawMetadata( + raw []*config.ImageMetadata, + overlay *config.DevContainerConfig, +) []*config.ImageMetadata { + if overlay == nil { + return raw + } + raw = append([]*config.ImageMetadata(nil), raw...) + raw[0] = metadata.DevContainerConfigToImageMetadata(overlay) + return raw +} + // mergeImageMetadataConfig folds any extra devcontainer config into the image // metadata, merges it with the parsed config, and applies extra remote env, // returning the resulting merged devcontainer config. @@ -1213,7 +1310,11 @@ func (r *runner) composeUpAndFindContainer( writer := log.Writer(log.LevelInfo) defer func() { _ = writer.Close() }() - if err := params.composeHelper.Run(ctx, upArgs, nil, writer, writer); err != nil { + if err := params.composeHelper.Run( + ctx, + upArgs, + docker.Streams{Stdout: writer, Stderr: writer}, + ); err != nil { return nil, fmt.Errorf("docker-compose run: %w", err) } diff --git a/pkg/devcontainer/compose_build.go b/pkg/devcontainer/compose_build.go index d44d59862b..529b880233 100644 --- a/pkg/devcontainer/compose_build.go +++ b/pkg/devcontainer/compose_build.go @@ -13,11 +13,13 @@ import ( composetypes "github.com/compose-spec/compose-go/v2/types" "github.com/devsy-org/devsy/pkg/compose" + "github.com/devsy-org/devsy/pkg/devcontainer/buildkit" "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/devcontainer/feature" "github.com/devsy-org/devsy/pkg/docker" "github.com/devsy-org/devsy/pkg/dockerfile" "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/workspacelogs" "github.com/google/go-containerregistry/pkg/name" "gopkg.in/yaml.v3" ) @@ -34,13 +36,20 @@ const composeDefaultPlatformEnv = "DOCKER_DEFAULT_PLATFORM" // prepareComposeBuildInfo modifies a compose project's devcontainer Dockerfile // to ensure it can be extended with features. If an Image is specified instead // of a Build, the metadata from the Image is used to populate the build info. +// composeBuildSource is the compose service to build, the stage to build to, and +// the image the service resolved to. +type composeBuildSource struct { + service *composetypes.ServiceConfig + buildTarget string + resolved *config.ImageDetails +} + func (r *runner) prepareComposeBuildInfo( ctx context.Context, subCtx *config.SubstitutionContext, - composeService *composetypes.ServiceConfig, - buildTarget string, - resolvedImage *config.ImageDetails, + src composeBuildSource, ) (composeBuildInfo, error) { + composeService, buildTarget, resolvedImage := src.service, src.buildTarget, src.resolved if composeService.Build == nil { imageBuildInfo, err := imageBuildInfoFromDetails(subCtx, resolvedImage) if err != nil { @@ -105,9 +114,11 @@ func (r *runner) prepareComposeDockerfileBuildInfo( // through Dockerfile base-image inspection. ctx, subCtx, - string(originalDockerfile), - mappingToMap(composeService.Build.Args), - originalTarget, + dockerfileInput{ + content: string(originalDockerfile), + buildArgs: mappingToMap(composeService.Build.Args), + target: originalTarget, + }, ) if err != nil { return composeBuildInfo{}, err @@ -261,13 +272,11 @@ func (r *runner) prepareExtendedComposeBuild( } } - buildInfo, err := r.prepareComposeBuildInfo( - ctx, - params.substitutionContext, - params.composeService, - defaultBuildTarget, - resolvedImage, - ) + buildInfo, err := r.prepareComposeBuildInfo(ctx, params.substitutionContext, composeBuildSource{ + service: params.composeService, + buildTarget: defaultBuildTarget, + resolved: resolvedImage, + }) if err != nil { return preparedComposeBuild{}, err } @@ -409,13 +418,16 @@ func (r *runner) runComposeBuild( composeHelper *compose.ComposeHelper, buildArgs []string, ) error { - writer := log.Writer(log.LevelInfo) - defer func() { _ = writer.Close() }() + logWriter := log.Writer(log.LevelInfo) + defer func() { _ = logWriter.Close() }() + // One shared writer keeps stdout and stderr on a single pipe, so the + // recorded text is combined like the logged text. + step := buildkit.StartStep(ctx, "compose") + writer := step.Tee(logWriter, workspacelogs.StreamCombined) log.Debugf("Run %s %s", composeHelper.Command, strings.Join(buildArgs, " ")) - if err := composeHelper.Run(ctx, buildArgs, nil, writer, writer); err != nil { - return err - } - return nil + err := composeHelper.Run(ctx, buildArgs, docker.Streams{Stdout: writer, Stderr: writer}) + step.Finish(ctx, err) + return err } // featureBuildOverrideParams groups the inputs for writing the extended diff --git a/pkg/devcontainer/compose_image_test.go b/pkg/devcontainer/compose_image_test.go index 5c2864b45e..3683e8921d 100644 --- a/pkg/devcontainer/compose_image_test.go +++ b/pkg/devcontainer/compose_image_test.go @@ -246,7 +246,11 @@ func TestComposeMetadataUsesResolvedPlatform(t *testing.T) { } info, err := r.prepareComposeBuildInfo( context.Background(), &config.SubstitutionContext{}, - &composetypes.ServiceConfig{Image: exampleBaseImage}, "selected-stage", details, + composeBuildSource{ + service: &composetypes.ServiceConfig{Image: exampleBaseImage}, + buildTarget: "selected-stage", + resolved: details, + }, ) require.NoError(t, err) require.Equal(t, "selected-user", info.imageBuildInfo.User) diff --git a/pkg/devcontainer/compose_recreate_test.go b/pkg/devcontainer/compose_recreate_test.go index 64bbd7ae4d..827d5c4e2a 100644 --- a/pkg/devcontainer/compose_recreate_test.go +++ b/pkg/devcontainer/compose_recreate_test.go @@ -14,6 +14,22 @@ import ( "github.com/stretchr/testify/require" ) +func recreateTestProject(buildContext string) *composetypes.Project { + return &composetypes.Project{ + Name: "recreate-test", + Services: composetypes.Services{ + composeSecretTestServiceName: { + Name: composeSecretTestServiceName, + Image: "test:latest", + Build: &composetypes.BuildConfig{ + Context: buildContext, + Dockerfile: structuralDockerfile, + }, + }, + }, + } +} + func TestStartComposeContainerRecreateRefreshesOverrides(t *testing.T) { for _, reset := range []bool{false, true} { name := "recreate" @@ -55,21 +71,9 @@ func TestStartComposeContainerRecreateRefreshesOverrides(t *testing.T) { }, }, substitutionContext: &config.SubstitutionContext{}, - project: &composetypes.Project{ - Name: "recreate-test", - Services: composetypes.Services{ - composeSecretTestServiceName: { - Name: composeSecretTestServiceName, - Image: "test:latest", - Build: &composetypes.BuildConfig{ - Context: buildContext, - Dockerfile: structuralDockerfile, - }, - }, - }, - }, - composeHelper: &compose.ComposeHelper{}, - container: container, + project: recreateTestProject(buildContext), + composeHelper: &compose.ComposeHelper{}, + container: container, options: UpOptions{ CLIOptions: provider.CLIOptions{Recreate: true, Reset: reset}, }, diff --git a/pkg/devcontainer/compose_resume.go b/pkg/devcontainer/compose_resume.go index 5fc722307e..6f96ae75c2 100644 --- a/pkg/devcontainer/compose_resume.go +++ b/pkg/devcontainer/compose_resume.go @@ -9,6 +9,7 @@ import ( "github.com/devsy-org/devsy/pkg/compose" pkgconfig "github.com/devsy-org/devsy/pkg/config" "github.com/devsy-org/devsy/pkg/devcontainer/config" + "github.com/devsy-org/devsy/pkg/docker" "github.com/devsy-org/devsy/pkg/log" ) @@ -111,7 +112,11 @@ func (r *runner) startComposeInstance( args = r.onlyRunServices(append(args, "start"), params.runParams.parsedConfig) writer := log.Writer(log.LevelInfo) defer func() { _ = writer.Close() }() - if err := params.composeHelper.Run(ctx, args, nil, writer, writer); err != nil { + if err := params.composeHelper.Run( + ctx, + args, + docker.Streams{Stdout: writer, Stderr: writer}, + ); err != nil { return nil, fmt.Errorf("start existing compose workspace: %w", err) } started, err := params.composeHelper.FindDevContainer( diff --git a/pkg/devcontainer/compose_test.go b/pkg/devcontainer/compose_test.go index ea8cbf1d9e..a893f2b960 100644 --- a/pkg/devcontainer/compose_test.go +++ b/pkg/devcontainer/compose_test.go @@ -14,6 +14,21 @@ import ( "github.com/stretchr/testify/suite" ) +const ( + value2300 = "2.30.0" + workspaceValue = "workspace" + app = "app" + ghcrIoExampleSharedBaseLatest = "ghcr.io/example/shared-base:latest" + registryExampleComApp123 = "registry.example.com/app:1.2.3" + workspaceAppLatest = "workspace-app:latest" + dockerfileWithFeatures = "Dockerfile-with-features" + tmpContext = "/tmp/context" + devContainersTargetStage = "dev_containers_target_stage" + testService = "test-service" + workspaceDevcontainerFeaturesFolder = "/workspace/.devcontainer/features/folder" + workspaceValueValue = "/workspace" +) + const ( platformLinuxAMD64 = "linux/amd64" platformLinuxARM64 = "linux/arm64" @@ -31,21 +46,21 @@ type composeBuildImageNameTestCase struct { var composeBuildImageNameTests = []composeBuildImageNameTestCase{ { name: "keeps original image without features", - composeHelper: &compose.ComposeHelper{Version: "2.30.0"}, - projectName: "workspace", + composeHelper: &compose.ComposeHelper{Version: value2300}, + projectName: workspaceValue, service: &composetypes.ServiceConfig{ - Name: "app", - Image: "ghcr.io/example/shared-base:latest", + Name: app, + Image: ghcrIoExampleSharedBaseLatest, }, - want: "ghcr.io/example/shared-base:latest", + want: ghcrIoExampleSharedBaseLatest, }, { name: "uses workspace image for image backed features", - composeHelper: &compose.ComposeHelper{Version: "2.30.0"}, - projectName: "workspace", + composeHelper: &compose.ComposeHelper{Version: value2300}, + projectName: workspaceValue, service: &composetypes.ServiceConfig{ - Name: "app", - Image: "ghcr.io/example/shared-base:latest", + Name: app, + Image: ghcrIoExampleSharedBaseLatest, }, hasFeatures: true, want: "workspace-app", @@ -53,10 +68,10 @@ var composeBuildImageNameTests = []composeBuildImageNameTestCase{ { name: "uses compose version separator for image backed features", composeHelper: &compose.ComposeHelper{Version: "2.7.0"}, - projectName: "workspace", + projectName: workspaceValue, service: &composetypes.ServiceConfig{ - Name: "app", - Image: "ghcr.io/example/shared-base:latest", + Name: app, + Image: ghcrIoExampleSharedBaseLatest, }, hasFeatures: true, want: "workspace_app", @@ -66,34 +81,34 @@ var composeBuildImageNameTests = []composeBuildImageNameTestCase{ // declared image tag is used even with features, which could collide // with the upstream registry tag. Changing this would be intentional. name: "preserves build backed services with features", - composeHelper: &compose.ComposeHelper{Version: "2.30.0"}, - projectName: "workspace", + composeHelper: &compose.ComposeHelper{Version: value2300}, + projectName: workspaceValue, service: &composetypes.ServiceConfig{ - Name: "app", - Image: "ghcr.io/example/shared-base:latest", + Name: app, + Image: ghcrIoExampleSharedBaseLatest, Build: &composetypes.BuildConfig{Context: "."}, }, hasFeatures: true, - want: "ghcr.io/example/shared-base:latest", + want: ghcrIoExampleSharedBaseLatest, }, { name: "preserves build backed services without features", - composeHelper: &compose.ComposeHelper{Version: "2.30.0"}, - projectName: "workspace", + composeHelper: &compose.ComposeHelper{Version: value2300}, + projectName: workspaceValue, service: &composetypes.ServiceConfig{ - Name: "app", - Image: "ghcr.io/example/shared-base:latest", + Name: app, + Image: ghcrIoExampleSharedBaseLatest, Build: &composetypes.BuildConfig{Context: "."}, }, hasFeatures: false, - want: "ghcr.io/example/shared-base:latest", + want: ghcrIoExampleSharedBaseLatest, }, { name: "uses default image when compose image is empty", - composeHelper: &compose.ComposeHelper{Version: "2.30.0"}, - projectName: "workspace", + composeHelper: &compose.ComposeHelper{Version: value2300}, + projectName: workspaceValue, service: &composetypes.ServiceConfig{ - Name: "app", + Name: app, }, hasFeatures: true, want: "workspace-app", @@ -113,12 +128,12 @@ func (s *ComposeSuite) TestStripDigestFromImageRef() { { name: "digest reference", input: "registry.example.com/app:1.2.3@sha256:abcdef", - want: "registry.example.com/app:1.2.3", + want: registryExampleComApp123, }, { name: "no digest", - input: "registry.example.com/app:1.2.3", - want: "registry.example.com/app:1.2.3", + input: registryExampleComApp123, + want: registryExampleComApp123, }, { name: "digest without tag", @@ -154,26 +169,26 @@ func (s *ComposeSuite) TestCreateComposeServiceUsesBuildImageName() { r := &runner{} service := r.createComposeService(&composeServiceParams{ composeService: &composetypes.ServiceConfig{ - Name: "app", - Image: "ghcr.io/example/shared-base:latest", + Name: app, + Image: ghcrIoExampleSharedBaseLatest, Build: &composetypes.BuildConfig{Target: "original-target"}, }, - buildImageName: "workspace-app:latest", - dockerfilePathInContext: "Dockerfile-with-features", - buildContext: "/tmp/context", + buildImageName: workspaceAppLatest, + dockerfilePathInContext: dockerfileWithFeatures, + buildContext: tmpContext, featuresBuildInfo: &feature.BuildInfo{ - OverrideTarget: "dev_containers_target_stage", + OverrideTarget: devContainersTargetStage, BuildArgs: map[string]string{ "FEATURE_FLAG": "true", }, }, }) - s.Equal("workspace-app:latest", service.Image) + s.Equal(workspaceAppLatest, service.Image) s.Require().NotNil(service.Build) - s.Equal("dev_containers_target_stage", service.Build.Target) - s.Equal("Dockerfile-with-features", service.Build.Dockerfile) - s.Equal("/tmp/context", service.Build.Context) + s.Equal(devContainersTargetStage, service.Build.Target) + s.Equal(dockerfileWithFeatures, service.Build.Dockerfile) + s.Equal(tmpContext, service.Build.Context) s.Require().NotNil(service.Build.Args) s.requireBuildArgValue(service.Build.Args, "FEATURE_FLAG", "true") s.requireBuildArgValue(service.Build.Args, "BUILDKIT_INLINE_CACHE", "1") @@ -183,15 +198,15 @@ func (s *ComposeSuite) TestCreateComposeServiceCarriesPlatform() { r := &runner{} service := r.createComposeService(&composeServiceParams{ composeService: &composetypes.ServiceConfig{ - Name: "app", - Image: "ghcr.io/example/shared-base:latest", + Name: app, + Image: ghcrIoExampleSharedBaseLatest, Platform: platformLinuxAMD64, }, - buildImageName: "workspace-app:latest", - dockerfilePathInContext: "Dockerfile-with-features", - buildContext: "/tmp/context", + buildImageName: workspaceAppLatest, + dockerfilePathInContext: dockerfileWithFeatures, + buildContext: tmpContext, featuresBuildInfo: &feature.BuildInfo{ - OverrideTarget: "dev_containers_target_stage", + OverrideTarget: devContainersTargetStage, }, }) @@ -203,14 +218,14 @@ func (s *ComposeSuite) TestCreateComposeServiceOmitsPlatformWhenUnset() { r := &runner{} service := r.createComposeService(&composeServiceParams{ composeService: &composetypes.ServiceConfig{ - Name: "app", - Image: "ghcr.io/example/shared-base:latest", + Name: app, + Image: ghcrIoExampleSharedBaseLatest, }, - buildImageName: "workspace-app:latest", - dockerfilePathInContext: "Dockerfile-with-features", - buildContext: "/tmp/context", + buildImageName: workspaceAppLatest, + dockerfilePathInContext: dockerfileWithFeatures, + buildContext: tmpContext, featuresBuildInfo: &feature.BuildInfo{ - OverrideTarget: "dev_containers_target_stage", + OverrideTarget: devContainersTargetStage, }, }) @@ -222,13 +237,13 @@ func (s *ComposeSuite) TestCreateComposeServiceClonesBuildPlatforms() { r := &runner{} service := r.createComposeService(&composeServiceParams{ composeService: &composetypes.ServiceConfig{ - Name: "app", + Name: app, Build: &composetypes.BuildConfig{ Platforms: composetypes.StringList{platformLinuxAMD64, platformLinuxARM64}, }, }, - dockerfilePathInContext: "Dockerfile-with-features", - buildContext: "/tmp/context", + dockerfilePathInContext: dockerfileWithFeatures, + buildContext: tmpContext, featuresBuildInfo: &feature.BuildInfo{}, }) @@ -243,14 +258,14 @@ func (s *ComposeSuite) TestCreateComposeServiceClonesUnrelatedBuildFields() { r := &runner{} service := r.createComposeService(&composeServiceParams{ composeService: &composetypes.ServiceConfig{ - Name: "app", + Name: app, Build: &composetypes.BuildConfig{ ShmSize: 64 * 1024 * 1024, CacheFrom: composetypes.StringList{"type=registry,ref=example.com/cache"}, }, }, - dockerfilePathInContext: "Dockerfile-with-features", - buildContext: "/tmp/context", + dockerfilePathInContext: dockerfileWithFeatures, + buildContext: tmpContext, featuresBuildInfo: &feature.BuildInfo{}, }) @@ -287,7 +302,7 @@ func (s *PrepareBuildContextSuite) SetupTest() { func (s *PrepareBuildContextSuite) TestNoContextRelativePath() { result, err := s.runner.prepareBuildContext( - &composetypes.ServiceConfig{Name: "test-service"}, + &composetypes.ServiceConfig{Name: testService}, "/tmp/features/Dockerfile", "FROM alpine", &feature.BuildInfo{FeaturesFolder: "/tmp/features/folder"}, @@ -304,10 +319,10 @@ func (s *PrepareBuildContextSuite) TestNoContextRelativePath() { func (s *PrepareBuildContextSuite) TestNilBuildRelativePath() { result, err := s.runner.prepareBuildContext( - &composetypes.ServiceConfig{Name: "test-service", Build: nil}, + &composetypes.ServiceConfig{Name: testService, Build: nil}, "/workspace/.devcontainer/features/Dockerfile", "FROM alpine", - &feature.BuildInfo{FeaturesFolder: "/workspace/.devcontainer/features/folder"}, + &feature.BuildInfo{FeaturesFolder: workspaceDevcontainerFeaturesFolder}, ) s.NoError(err) @@ -324,14 +339,14 @@ func (s *PrepareBuildContextSuite) TestCustomBuildContext() { result, err := s.runner.prepareBuildContext( &composetypes.ServiceConfig{ - Name: "test-service", + Name: testService, Build: &composetypes.BuildConfig{ - Context: "/workspace", + Context: workspaceValueValue, }, }, "/workspace/.devcontainer/features/Dockerfile", dockerfileContent, - &feature.BuildInfo{FeaturesFolder: "/workspace/.devcontainer/features/folder"}, + &feature.BuildInfo{FeaturesFolder: workspaceDevcontainerFeaturesFolder}, ) s.NoError(err) @@ -340,7 +355,7 @@ func (s *PrepareBuildContextSuite) TestCustomBuildContext() { "dockerfilePathInContext should be relative", ) s.Equal(".devcontainer/features/Dockerfile", result.dockerfilePathInContext) - s.Equal("/workspace", result.context) + s.Equal(workspaceValueValue, result.context) s.Contains(result.dockerfileContent, "COPY ./.devcontainer/features/folder/") s.NotContains(result.dockerfileContent, "COPY ./"+config.DevsyContextFeatureFolder+"/") } @@ -351,12 +366,12 @@ func (s *PrepareBuildContextSuite) TestCustomBuildContextPreservesWhitespace() { result, err := s.runner.prepareBuildContext( &composetypes.ServiceConfig{ - Name: "test-service", - Build: &composetypes.BuildConfig{Context: "/workspace"}, + Name: testService, + Build: &composetypes.BuildConfig{Context: workspaceValueValue}, }, "/workspace/.devcontainer/features/Dockerfile", dockerfileContent, - &feature.BuildInfo{FeaturesFolder: "/workspace/.devcontainer/features/folder"}, + &feature.BuildInfo{FeaturesFolder: workspaceDevcontainerFeaturesFolder}, ) s.NoError(err) @@ -369,12 +384,12 @@ func (s *PrepareBuildContextSuite) TestCustomBuildContextNoReplacementNeeded() { result, err := s.runner.prepareBuildContext( &composetypes.ServiceConfig{ - Name: "test-service", - Build: &composetypes.BuildConfig{Context: "/workspace"}, + Name: testService, + Build: &composetypes.BuildConfig{Context: workspaceValueValue}, }, "/workspace/.devcontainer/features/Dockerfile", dockerfileContent, - &feature.BuildInfo{FeaturesFolder: "/workspace/.devcontainer/features/folder"}, + &feature.BuildInfo{FeaturesFolder: workspaceDevcontainerFeaturesFolder}, ) s.NoError(err) @@ -384,12 +399,12 @@ func (s *PrepareBuildContextSuite) TestCustomBuildContextNoReplacementNeeded() { func (s *PrepareBuildContextSuite) TestCustomBuildContextEmptyContext() { result, err := s.runner.prepareBuildContext( &composetypes.ServiceConfig{ - Name: "test-service", + Name: testService, Build: &composetypes.BuildConfig{Context: ""}, }, "/workspace/.devcontainer/features/Dockerfile", "FROM alpine", - &feature.BuildInfo{FeaturesFolder: "/workspace/.devcontainer/features/folder"}, + &feature.BuildInfo{FeaturesFolder: workspaceDevcontainerFeaturesFolder}, ) s.NoError(err) @@ -404,8 +419,8 @@ func TestPrepareBuildContextSuite(t *testing.T) { func TestValidateRunServices(t *testing.T) { project := &composetypes.Project{ Services: map[string]composetypes.ServiceConfig{ - "app": {Name: "app"}, - "db": {Name: "db"}, + app: {Name: app}, + "db": {Name: "db"}, }, } emptyProject := &composetypes.Project{Services: map[string]composetypes.ServiceConfig{}} @@ -418,10 +433,10 @@ func TestValidateRunServices(t *testing.T) { errContains string }{ {"empty runServices returns nil", nil, project, false, ""}, - {"valid services returns nil", []string{"app", "db"}, project, false, ""}, + {"valid services returns nil", []string{app, "db"}, project, false, ""}, {"invalid service returns error", []string{"nonexistent"}, project, true, "nonexistent"}, - {"mix of valid and invalid", []string{"app", "typo-svc", "bad"}, project, true, "typo-svc"}, - {"project with no services", []string{"app"}, emptyProject, true, "app"}, + {"mix of valid and invalid", []string{app, "typo-svc", "bad"}, project, true, "typo-svc"}, + {"project with no services", []string{app}, emptyProject, true, app}, } for _, tt := range tests { @@ -838,7 +853,7 @@ func TestComposeBuildArgs(t *testing.T) { t.Run(tt.name, func(t *testing.T) { args := composeBuildArgs(&composeBuildArgsParams{ projectName: "ws", - serviceName: "app", + serviceName: app, pull: tt.pull, noCache: tt.noCache, }) @@ -855,11 +870,11 @@ func TestComposeBuildArgs(t *testing.T) { func TestComposeBuildArgsIncludesCoreArgs(t *testing.T) { args := composeBuildArgs(&composeBuildArgsParams{ projectName: "ws", - serviceName: "app", + serviceName: app, overrideComposeFilePath: "/tmp/override.yml", }) - for _, want := range []string{composeProjectNameFlag, "ws", "build", "/tmp/override.yml"} { + for _, want := range []string{composeProjectNameFlag, "ws", overlayBuildField, "/tmp/override.yml"} { if !slices.Contains(args, want) { t.Errorf("expected %q in %v", want, args) } @@ -869,13 +884,13 @@ func TestComposeBuildArgsIncludesCoreArgs(t *testing.T) { func TestComposeBuildArgsRunServicesNoDuplicate(t *testing.T) { args := composeBuildArgs(&composeBuildArgsParams{ projectName: "ws", - serviceName: "app", - runServices: []string{"app", "db"}, + serviceName: app, + runServices: []string{app, "db"}, }) appCount := 0 for _, a := range args { - if a == "app" { + if a == app { appCount++ } } diff --git a/pkg/devcontainer/config.go b/pkg/devcontainer/config.go index 6c34e510ac..9b602dabf2 100644 --- a/pkg/devcontainer/config.go +++ b/pkg/devcontainer/config.go @@ -252,15 +252,6 @@ func (r *runner) rawConfigFromContainer() *config.DevContainerConfig { } } -// rawConfigFromCrane pulls the config from the image source via crane. -func (r *runner) rawConfigFromCrane( - options provider.CLIOptions, -) (*config.DevContainerConfig, error) { - return r.rawConfigFromCraneWithContext( - context.Background(), options, r.effectiveDevContainerSelection(options), - ) -} - func (r *runner) rawConfigFromCraneWithContext( ctx context.Context, options provider.CLIOptions, @@ -291,16 +282,6 @@ func (r *runner) rawConfigFromCraneWithContext( ) } -// rawConfigFromFilesystem discovers and parses the devcontainer.json under the -// workspace folder, falling back to an auto-detected default when none is found. -func (r *runner) rawConfigFromFilesystem( - options provider.CLIOptions, -) (*config.DevContainerConfig, error) { - return r.rawConfigFromFilesystemWithContext( - context.Background(), options, r.effectiveDevContainerSelection(options), - ) -} - func (r *runner) rawConfigFromFilesystemWithContext( ctx context.Context, options provider.CLIOptions, @@ -363,13 +344,6 @@ func workspaceMountFolderWarning(conf *config.DevContainerConfig) string { return "" } -func (r *runner) rawConfigFromSource( - source string, - options provider.CLIOptions, -) (*config.DevContainerConfig, error) { - return r.rawConfigFromSourceWithContext(context.Background(), source, options) -} - func (r *runner) rawConfigFromSourceWithContext( ctx context.Context, source string, diff --git a/pkg/devcontainer/config/config.go b/pkg/devcontainer/config/config.go index 06151f9e07..e065914916 100644 --- a/pkg/devcontainer/config/config.go +++ b/pkg/devcontainer/config/config.go @@ -61,34 +61,42 @@ type DevContainerConfigBase struct { // Features to add to the dev container. Features map[string]any `json:"features,omitempty"` - // Array consisting of the Feature id (without the semantic version) of Features in the order the user wants them to be installed. + // Array consisting of the Feature id (without the semantic version) of Features in the order the user wants them to be + // installed. OverrideFeatureInstallOrder []string `json:"overrideFeatureInstallOrder,omitempty"` - // Ports that are forwarded from the container to the local machine. Can be an integer port number, or a string of the format "host:port_number". + // Ports that are forwarded from the container to the local machine. Can be an integer port number, or a string of the + // format "host:port_number". ForwardPorts types.StrIntArray `json:"forwardPorts,omitempty"` // Set default properties that are applied when a specific port number is forwarded. PortsAttributes map[string]PortAttribute `json:"portsAttributes,omitempty"` - // Set default properties that are applied to all ports that don't get properties from the setting `remote.portsAttributes`. + // Set default properties that are applied to all ports that don't get properties from the setting + // `remote.portsAttributes`. OtherPortsAttributes *PortAttribute `json:"otherPortsAttributes,omitempty"` - // Controls whether on Linux the container's user should be updated with the local user's UID and GID. On by default when opening from a local folder. + // Controls whether on Linux the container's user should be updated with the local user's UID and GID. On by default + // when opening from a local folder. UpdateRemoteUserUID *bool `json:"updateRemoteUserUID,omitempty"` - // Remote environment variables to set for processes spawned in the container including lifecycle scripts and any remote editor/IDE server process. + // Remote environment variables to set for processes spawned in the container including lifecycle scripts and any + // remote editor/IDE server process. RemoteEnv map[string]*string `json:"remoteEnv,omitempty"` - // The username to use for spawning processes in the container including lifecycle scripts and any remote editor/IDE server process. The default is the same user as the container. + // The username to use for spawning processes in the container including lifecycle scripts and any remote editor/IDE + // server process. The default is the same user as the container. RemoteUser string `json:"remoteUser,omitempty"` - // A command to run locally before anything else. This command is run before "onCreateCommand". If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell. + // A command to run locally before anything else. This command is run before "onCreateCommand". If this is a single + // string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell. InitializeCommand types.LifecycleHook `json:"initializeCommand,omitempty"` // Action to take when the user disconnects from the container in their editor. The default is to stop the container. ShutdownAction string `json:"shutdownAction,omitempty"` - // The user command to wait for before continuing execution in the background while the UI is starting up. The default is "updateContentCommand". + // The user command to wait for before continuing execution in the background while the UI is starting up. The default + // is "updateContentCommand". WaitFor string `json:"waitFor,omitempty"` // User environment probe to run. The default is "loginInteractiveShell". @@ -104,7 +112,8 @@ type DevContainerConfigBase struct { WorkspaceFolder string `json:"workspaceFolder,omitempty"` // Deprecated: Use 'customizations/vscode/settings' instead - // Machine specific settings that should be copied into the container. These are only copied when connecting to the container for the first time, rebuilding the container then triggers it again. + // Machine specific settings that should be copied into the container. These are only copied when connecting to the + // container for the first time, rebuilding the container then triggers it again. Settings map[string]any `json:"settings,omitempty"` // Deprecated: Use 'customizations/vscode/extensions' instead @@ -127,27 +136,37 @@ type SecretConfig struct { } type DevContainerActions struct { - // A command to run when creating the container. This command is run after "initializeCommand" and before "updateContentCommand". If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell. + // A command to run when creating the container. This command is run after "initializeCommand" and before + // "updateContentCommand". If this is a single string, it will be run in a shell. If this is an array of strings, it + // will be run as a single command without shell. OnCreateCommand types.LifecycleHook `json:"onCreateCommand,omitempty"` - // A command to run when creating the container and rerun when the workspace content was updated while creating the container. - // This command is run after "onCreateCommand" and before "postCreateCommand". If this is a single string, it will be run in a shell. + // A command to run when creating the container and rerun when the workspace content was updated while creating the + // container. + // This command is run after "onCreateCommand" and before "postCreateCommand". If this is a single string, it will be + // run in a shell. // If this is an array of strings, it will be run as a single command without shell. UpdateContentCommand types.LifecycleHook `json:"updateContentCommand,omitempty"` - // A command to run after creating the container. This command is run after "updateContentCommand" and before "postStartCommand". - // If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell. + // A command to run after creating the container. This command is run after "updateContentCommand" and before + // "postStartCommand". + // If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single + // command without shell. PostCreateCommand types.LifecycleHook `json:"postCreateCommand,omitempty"` - // A command to run after starting the container. This command is run after "postCreateCommand" and before "postAttachCommand". - // If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell. + // A command to run after starting the container. This command is run after "postCreateCommand" and before + // "postAttachCommand". + // If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single + // command without shell. PostStartCommand types.LifecycleHook `json:"postStartCommand,omitempty"` // A command to run when attaching to the container. This command is run after "postStartCommand". - // If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell. + // If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single + // command without shell. PostAttachCommand types.LifecycleHook `json:"postAttachCommand,omitempty"` - // Tool-specific configuration. Each tool should use a JSON object subproperty with a unique name to group its customizations. + // Tool-specific configuration. Each tool should use a JSON object subproperty with a unique name to group its + // customizations. Customizations map[string]any `json:"customizations,omitempty"` } @@ -155,27 +174,37 @@ type UpdatedConfigProperties struct { // Entrypoint script that should fire at container start up. Entrypoints []string `json:"entrypoints,omitempty"` - // A command to run when creating the container. This command is run after "initializeCommand" and before "updateContentCommand". If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell. + // A command to run when creating the container. This command is run after "initializeCommand" and before + // "updateContentCommand". If this is a single string, it will be run in a shell. If this is an array of strings, it + // will be run as a single command without shell. OnCreateCommands []types.LifecycleHook `json:"onCreateCommand,omitempty"` - // A command to run when creating the container and rerun when the workspace content was updated while creating the container. - // This command is run after "onCreateCommand" and before "postCreateCommand". If this is a single string, it will be run in a shell. + // A command to run when creating the container and rerun when the workspace content was updated while creating the + // container. + // This command is run after "onCreateCommand" and before "postCreateCommand". If this is a single string, it will be + // run in a shell. // If this is an array of strings, it will be run as a single command without shell. UpdateContentCommands []types.LifecycleHook `json:"updateContentCommand,omitempty"` - // A command to run after creating the container. This command is run after "updateContentCommand" and before "postStartCommand". - // If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell. + // A command to run after creating the container. This command is run after "updateContentCommand" and before + // "postStartCommand". + // If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single + // command without shell. PostCreateCommands []types.LifecycleHook `json:"postCreateCommand,omitempty"` - // A command to run after starting the container. This command is run after "postCreateCommand" and before "postAttachCommand". - // If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell. + // A command to run after starting the container. This command is run after "postCreateCommand" and before + // "postAttachCommand". + // If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single + // command without shell. PostStartCommands []types.LifecycleHook `json:"postStartCommand,omitempty"` // A command to run when attaching to the container. This command is run after "postStartCommand". - // If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell. + // If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single + // command without shell. PostAttachCommands []types.LifecycleHook `json:"postAttachCommand,omitempty"` - // Tool-specific configuration. Each tool should use a JSON object subproperty with a unique name to group its customizations. + // Tool-specific configuration. Each tool should use a JSON object subproperty with a unique name to group its + // customizations. Customizations map[string][]any `json:"customizations,omitempty"` } @@ -183,7 +212,8 @@ type ComposeContainer struct { // The name of the docker-compose file(s) used to start the services. DockerComposeFile types.StrArray `json:"dockerComposeFile,omitempty"` - // The service you want to work on. This is considered the primary container for your dev environment which your editor will connect to. + // The service you want to work on. This is considered the primary container for your dev environment which your editor + // will connect to. Service string `json:"service,omitempty"` // An array of services that should be started and stopped. @@ -196,8 +226,10 @@ type ImageContainer struct { } type NonComposeBase struct { - // Application ports that are exposed by the container. This can be a single port or an array of ports. Each port can be a number or a string. - // A number is mapped to the same port on the host. A string is passed to Docker unchanged and can be used to map ports differently, + // Application ports that are exposed by the container. This can be a single port or an array of ports. Each port can + // be a number or a string. + // A number is mapped to the same port on the host. A string is passed to Docker unchanged and can be used to map ports + // differently, // e.g. "8000:8010". AppPort types.StrIntArray `json:"appPort,omitempty"` @@ -207,7 +239,8 @@ type NonComposeBase struct { // The user the container will be started with. The default is the user on the Docker image. ContainerUser string `json:"containerUser,omitempty"` - // Mounts points to set up when creating the container. See Docker's documentation for the --mount option for the supported syntax. + // Mounts points to set up when creating the container. See Docker's documentation for the --mount option for the + // supported syntax. Mounts []*Mount `json:"mounts,omitempty"` // Passes the --init flag when creating the dev container. @@ -231,10 +264,12 @@ type NonComposeBase struct { } type DockerfileContainer struct { - // The location of the Dockerfile that defines the contents of the container. The path is relative to the folder containing the `devcontainer.json` file. + // The location of the Dockerfile that defines the contents of the container. The path is relative to the folder + // containing the `devcontainer.json` file. Dockerfile string `json:"dockerFile,omitempty"` - // The location of the context folder for building the Docker image. The path is relative to the folder containing the `devcontainer.json` file. + // The location of the context folder for building the Docker image. The path is relative to the folder containing the + // `devcontainer.json` file. Context string `json:"context,omitempty"` // Docker build-related options. @@ -294,10 +329,12 @@ func (d DockerfileContainer) GetCacheFrom() types.StrArray { } type ConfigBuildOptions struct { - // The location of the Dockerfile that defines the contents of the container. The path is relative to the folder containing the `devcontainer.json` file. + // The location of the Dockerfile that defines the contents of the container. The path is relative to the folder + // containing the `devcontainer.json` file. Dockerfile string `json:"dockerfile,omitempty"` - // The location of the context folder for building the Docker image. The path is relative to the folder containing the `devcontainer.json` file. + // The location of the context folder for building the Docker image. The path is relative to the folder containing the + // `devcontainer.json` file. Context string `json:"context,omitempty"` // Target stage in a multi-stage build. @@ -413,7 +450,8 @@ type PortAttribute struct { // default=notify OnAutoForward string `json:"onAutoForward,omitempty"` - // Automatically prompt for elevation (if needed) when this port is forwarded. Elevate is required if the local port is a privileged port. + // Automatically prompt for elevation (if needed) when this port is forwarded. Elevate is required if the local port is + // a privileged port. ElevateIfNeeded bool `json:"elevateIfNeeded,omitempty"` // Label that will be shown in the UI for this port. diff --git a/pkg/devcontainer/config/envelope.go b/pkg/devcontainer/config/envelope.go index bf4f0e6ad7..b47f133f2a 100644 --- a/pkg/devcontainer/config/envelope.go +++ b/pkg/devcontainer/config/envelope.go @@ -29,6 +29,8 @@ type ResultEnvelope struct { URL string `json:"url,omitempty"` Warnings []string `json:"warnings,omitempty"` Recovery bool `json:"recovery,omitempty"` + // RunID names the retained setup history of this run. + RunID string `json:"runId,omitempty"` } type ErrorEnvelope struct { @@ -59,11 +61,19 @@ type StatusEnvelope struct { type TaskEnvelope struct { Kind string `json:"kind"` ID string `json:"id"` + // RunID names the retained setup history of the submitted run. + RunID string `json:"runId,omitempty"` } // WriteTaskJSON serializes a submitted task's ID as an NDJSON line to w. func WriteTaskJSON(w io.Writer, id string) error { - data, err := json.Marshal(TaskEnvelope{Kind: KindTask, ID: id}) + return WriteTaskRunJSON(w, id, "") +} + +// WriteTaskRunJSON is WriteTaskJSON with the run that records the task's +// setup history. +func WriteTaskRunJSON(w io.Writer, id, runID string) error { + data, err := json.Marshal(TaskEnvelope{Kind: KindTask, ID: id, RunID: runID}) if err != nil { return err } @@ -111,23 +121,11 @@ func ParseStatusLine(line string) (status.Event, bool) { if !strings.HasPrefix(trimmed, "{") { return status.Event{}, false } - var env StatusEnvelope - var fields map[string]json.RawMessage - if err := json.Unmarshal([]byte(trimmed), &fields); err != nil || !currentStatusFields(fields) { + if !hasCurrentStatusShape(trimmed) { return status.Event{}, false } - var errorMessage string - if raw, ok := fields["error"]; ok && json.Unmarshal(raw, &errorMessage) == nil { - return status.Event{}, false - } - if raw, ok := fields["error"]; ok && !currentStatusError(raw) { - return status.Event{}, false - } - if err := json.Unmarshal([]byte(trimmed), &env); err != nil || - env.Kind != KindStatus || env.SchemaVersion != 1 || env.Phase == "" || - !validState(env.State) || env.DurationMs < 0 || - env.DurationMs > int64(time.Duration(1<<63-1)/time.Millisecond) || - (env.State == status.StateFailed && env.Error == nil) { + var env StatusEnvelope + if err := json.Unmarshal([]byte(trimmed), &env); err != nil || !validStatusEnvelope(env) { return status.Event{}, false } return status.Event{ @@ -226,3 +224,28 @@ func redactContext(values map[string]string, redactor *secrets.Redactor) map[str } return redacted } + +// hasCurrentStatusShape reports whether the line has only current status +// fields, with a well-formed error object when one is present. +func hasCurrentStatusShape(trimmed string) bool { + var fields map[string]json.RawMessage + if err := json.Unmarshal([]byte(trimmed), &fields); err != nil || !currentStatusFields(fields) { + return false + } + var errorMessage string + if raw, ok := fields["error"]; ok && json.Unmarshal(raw, &errorMessage) == nil { + return false + } + if raw, ok := fields["error"]; ok && !currentStatusError(raw) { + return false + } + return true +} + +// validStatusEnvelope checks the values of a decoded status envelope. +func validStatusEnvelope(env StatusEnvelope) bool { + return env.Kind == KindStatus && env.SchemaVersion == 1 && env.Phase != "" && + validState(env.State) && env.DurationMs >= 0 && + env.DurationMs <= int64(time.Duration(1<<63-1)/time.Millisecond) && + (env.State != status.StateFailed || env.Error != nil) +} diff --git a/pkg/devcontainer/config/envelope_test.go b/pkg/devcontainer/config/envelope_test.go index f52d9892b5..6dea43e16a 100644 --- a/pkg/devcontainer/config/envelope_test.go +++ b/pkg/devcontainer/config/envelope_test.go @@ -13,72 +13,26 @@ import ( "github.com/devsy-org/devsy/pkg/status" ) +const ( + vscodeValue = "vscode" + workspacesProject = "/workspaces/project" + abc123 = "abc123" +) + const envelopeTestSecret = "status-secret" +// resultCase is one input of TestWriteResultJSON. +type resultCase struct { + name string + containerID string + user string + workdir string + url string + warnings []string +} + func TestWriteResultJSON(t *testing.T) { - tests := []struct { - name string - containerID string - user string - workdir string - url string - warnings []string - }{ - { - name: "typical values", - containerID: "abc123def456", - user: "vscode", - workdir: "/workspaces/project", - warnings: nil, - }, - { - name: "root user", - containerID: "sha256:abcdef1234567890", - user: "root", - workdir: "/workspaces/my-app", - warnings: nil, - }, - { - name: "empty strings", - containerID: "", - user: "", - workdir: "", - warnings: nil, - }, - { - name: "special characters in container ID", - containerID: "container:with/special-chars_123", - user: "dev-user", - workdir: "/workspaces/my project", - warnings: nil, - }, - { - name: "with warnings", - containerID: "abc123", - user: "vscode", - workdir: "/workspaces/project", - warnings: []string{ - "cpus: required 128, available 4", - "memory: required 256gb (274877906944 bytes), available 17179869184 bytes", - }, - }, - { - name: "with url", - containerID: "abc123", - user: "vscode", - workdir: "/workspaces/project", - url: "http://localhost:8080/?folder=/workspaces/project", - warnings: nil, - }, - { - name: "empty url omitted", - containerID: "abc123", - user: "vscode", - workdir: "/workspaces/project", - url: "", - warnings: nil, - }, - } + tests := resultCases() for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -94,63 +48,7 @@ func TestWriteResultJSON(t *testing.T) { t.Fatalf("WriteResultJSON returned error: %v", err) } - output := buf.Bytes() - if output[len(output)-1] != '\n' { - t.Fatal("output must be newline-terminated") - } - - var envelope ResultEnvelope - if err := json.Unmarshal(output, &envelope); err != nil { - t.Fatalf("output is not valid JSON: %v", err) - } - - if envelope.Outcome != "success" { - t.Errorf("outcome = %q, want %q", envelope.Outcome, "success") - } - if envelope.ContainerID != tt.containerID { - t.Errorf("containerId = %q, want %q", envelope.ContainerID, tt.containerID) - } - if envelope.RemoteUser != tt.user { - t.Errorf("remoteUser = %q, want %q", envelope.RemoteUser, tt.user) - } - if envelope.RemoteWorkspaceFolder != tt.workdir { - t.Errorf("remoteWorkspaceFolder = %q, want %q", - envelope.RemoteWorkspaceFolder, tt.workdir) - } - if envelope.URL != tt.url { - t.Errorf("url = %q, want %q", envelope.URL, tt.url) - } - if tt.url == "" && bytes.Contains(output, []byte(`"url"`)) { - t.Error("url field must be omitted when empty") - } - if tt.url != "" && !bytes.Contains(output, []byte(`"url"`)) { - t.Error("url field must be present when set") - } - if len(tt.warnings) == 0 { - if envelope.Warnings != nil { - t.Errorf("warnings = %v, want nil (omitempty)", envelope.Warnings) - } - if bytes.Contains(output, []byte(`"warnings"`)) { - t.Error("warnings field must be omitted when empty") - } - } - if len(tt.warnings) > 0 { - if len(envelope.Warnings) != len(tt.warnings) { - t.Fatalf( - "warnings length = %d, want %d", - len(envelope.Warnings), len(tt.warnings), - ) - } - for i, w := range tt.warnings { - if envelope.Warnings[i] != w { - t.Errorf("warnings[%d] = %q, want %q", i, envelope.Warnings[i], w) - } - } - } - - if bytes.Contains(output[:len(output)-1], []byte("\n")) { - t.Error("JSON must be single-line (no embedded newlines)") - } + assertResultEnvelope(t, tt, buf.Bytes()) }) } } @@ -529,3 +427,149 @@ func assertStatusEnvelopeFields(t *testing.T, got StatusEnvelope) { t.Fatalf("current status fields missing: %+v", got) } } + +// assertResultEnvelope checks one encoded result envelope against the values +// it was written from. +func assertResultEnvelope(t *testing.T, want resultCase, output []byte) { + t.Helper() + containerID, user, workdir, url, warnings := want.containerID, want.user, want.workdir, want.url, want.warnings + if output[len(output)-1] != '\n' { + t.Fatal("output must be newline-terminated") + } + + var envelope ResultEnvelope + if err := json.Unmarshal(output, &envelope); err != nil { + t.Fatalf("output is not valid JSON: %v", err) + } + + assertCoreFields( + t, + envelope, + coreFields{containerID: containerID, user: user, workdir: workdir, url: url}, + ) + assertOptionalField(t, output, "url", url != "") + assertWarnings(t, envelope.Warnings, warnings, output) + + if bytes.Contains(output[:len(output)-1], []byte("\n")) { + t.Error("JSON must be single-line (no embedded newlines)") + } +} + +// coreFields are the envelope fields every successful run reports. +type coreFields struct { + containerID, user, workdir, url string +} + +func assertCoreFields(t *testing.T, envelope ResultEnvelope, want coreFields) { + t.Helper() + if envelope.Outcome != "success" { + t.Errorf("outcome = %q, want %q", envelope.Outcome, "success") + } + if envelope.ContainerID != want.containerID { + t.Errorf("containerId = %q, want %q", envelope.ContainerID, want.containerID) + } + if envelope.RemoteUser != want.user { + t.Errorf("remoteUser = %q, want %q", envelope.RemoteUser, want.user) + } + if envelope.RemoteWorkspaceFolder != want.workdir { + t.Errorf( + "remoteWorkspaceFolder = %q, want %q", + envelope.RemoteWorkspaceFolder, + want.workdir, + ) + } + if envelope.URL != want.url { + t.Errorf("url = %q, want %q", envelope.URL, want.url) + } +} + +// assertOptionalField checks that the named field appears only when present. +func assertOptionalField(t *testing.T, output []byte, field string, present bool) { + t.Helper() + found := bytes.Contains(output, []byte(`"`+field+`"`)) + if !present && found { + t.Errorf("%s field must be omitted when empty", field) + } + if present && !found { + t.Errorf("%s field must be present when set", field) + } +} + +func assertWarnings(t *testing.T, got, want []string, output []byte) { + t.Helper() + if len(want) == 0 { + if got != nil { + t.Errorf("warnings = %v, want nil (omitempty)", got) + } + assertOptionalField(t, output, "warnings", false) + return + } + if len(got) != len(want) { + t.Fatalf("warnings length = %d, want %d", len(got), len(want)) + } + for i, w := range want { + if got[i] != w { + t.Errorf("warnings[%d] = %q, want %q", i, got[i], w) + } + } +} + +// resultCases lists the inputs of TestWriteResultJSON. +func resultCases() []resultCase { + return []resultCase{ + { + name: "typical values", + containerID: "abc123def456", + user: vscodeValue, + workdir: workspacesProject, + warnings: nil, + }, + { + name: "root user", + containerID: "sha256:abcdef1234567890", + user: "root", + workdir: "/workspaces/my-app", + warnings: nil, + }, + { + name: "empty strings", + containerID: "", + user: "", + workdir: "", + warnings: nil, + }, + { + name: "special characters in container ID", + containerID: "container:with/special-chars_123", + user: "dev-user", + workdir: "/workspaces/my project", + warnings: nil, + }, + { + name: "with warnings", + containerID: abc123, + user: vscodeValue, + workdir: workspacesProject, + warnings: []string{ + "cpus: required 128, available 4", + "memory: required 256gb (274877906944 bytes), available 17179869184 bytes", + }, + }, + { + name: "with url", + containerID: abc123, + user: vscodeValue, + workdir: workspacesProject, + url: "http://localhost:8080/?folder=/workspaces/project", + warnings: nil, + }, + { + name: "empty url omitted", + containerID: abc123, + user: vscodeValue, + workdir: workspacesProject, + url: "", + warnings: nil, + }, + } +} diff --git a/pkg/devcontainer/config/extends_oci.go b/pkg/devcontainer/config/extends_oci.go index 4458bd49fb..9cff98c3de 100644 --- a/pkg/devcontainer/config/extends_oci.go +++ b/pkg/devcontainer/config/extends_oci.go @@ -268,8 +268,7 @@ func isOCIExtendsTransientError(err error) bool { if err == nil { return false } - var terr *transport.Error - if errors.As(err, &terr) { + if terr, ok := errors.AsType[*transport.Error](err); ok { return terr.StatusCode >= http.StatusInternalServerError } return true diff --git a/pkg/devcontainer/config/feature.go b/pkg/devcontainer/config/feature.go index 12d60abb46..7637a2be1b 100644 --- a/pkg/devcontainer/config/feature.go +++ b/pkg/devcontainer/config/feature.go @@ -56,7 +56,8 @@ type FeatureSet struct { } type FeatureConfig struct { - // ID of the Feature. The id should be unique in the context of the repository/published package where the feature exists and must match the name of the directory where the devcontainer-feature.json resides. + // ID of the Feature. The id should be unique in the context of the repository/published package where the feature + // exists and must match the name of the directory where the devcontainer-feature.json resides. ID string `json:"id,omitempty"` // Display name of the Feature. @@ -65,19 +66,23 @@ type FeatureConfig struct { // The version of the Feature. Follows the semanatic versioning (semver) specification. Version string `json:"version,omitempty"` - // Description of the Feature. For the best appearance in an implementing tool, refrain from including markdown or HTML in the description. + // Description of the Feature. For the best appearance in an implementing tool, refrain from + // including markdown or HTML in the description. Description string `json:"description,omitempty"` // Entrypoint script that should fire at container start up. Entrypoint string `json:"entrypoint,omitempty"` - // Indicates that the Feature is deprecated, and will not receive any further updates/support. This property is intended to be used by the supporting tools for highlighting Feature deprecation. + // Indicates that the Feature is deprecated, and will not receive any further updates/support. This property is + // intended to be used by the supporting tools for highlighting Feature deprecation. Deprecated bool `json:"deprecated,omitempty"` - // Array of old IDs used to publish this Feature. The property is useful for renaming a currently published Feature within a single namespace. + // Array of old IDs used to publish this Feature. The property is useful for renaming a currently published Feature + // within a single namespace. LegacyIds []string `json:"legacyIds,omitempty"` - // Possible user-configurable options for this Feature. The selected options will be passed as environment variables when installing the Feature into the container. + // Possible user-configurable options for this Feature. The selected options will be passed as environment variables + // when installing the Feature into the container. Options map[string]FeatureConfigOption `json:"options,omitempty"` // URL to documentation for the Feature. @@ -101,10 +106,12 @@ type FeatureConfig struct { // Mounts a volume or bind mount into the container. Mounts []*Mount `json:"mounts,omitempty"` - // Array of ID's of Features that should execute before this one. Allows control for feature authors on soft dependencies between different Features. + // Array of ID's of Features that should execute before this one. Allows control for feature authors on soft + // dependencies between different Features. InstallsAfter []string `json:"installsAfter,omitempty"` - // The optional dependsOn property indicates a set of required, “hard” dependencies for a given Feature. Hard dependencies must be satisfied before this Feature is installed. + // The optional dependsOn property indicates a set of required, “hard” dependencies for a given Feature. Hard + // dependencies must be satisfied before this Feature is installed. DependsOn DependsOnField `json:"dependsOn,omitempty"` // Container environment variables. @@ -181,12 +188,15 @@ type FeatureConfigOption struct { // A description of the option displayed to the user by a supporting tool. Description string `json:"description,omitempty"` - // The type of the option. Can be 'boolean' or 'string'. Options of type 'string' should use the 'enum' or 'proposals' property to provide a list of allowed values. + // The type of the option. Can be 'boolean' or 'string'. Options of type 'string' should use the 'enum' or 'proposals' + // property to provide a list of allowed values. Type string `json:"type,omitempty"` - // Allowed values for this option. Unlike 'proposals', the user cannot provide a custom value not included in the 'enum' array. + // Allowed values for this option. Unlike 'proposals', the user cannot provide a custom value not included in the + // 'enum' array. Enum []string `json:"enum,omitempty"` - // Suggested values for this option. Unlike 'enum', the 'proposals' attribute indicates the installation script can handle arbitrary values provided by the user. + // Suggested values for this option. Unlike 'enum', the 'proposals' attribute indicates the installation script can + // handle arbitrary values provided by the user. Proposals []string `json:"proposals,omitempty"` } diff --git a/pkg/devcontainer/config/gpu_test.go b/pkg/devcontainer/config/gpu_test.go index 75db8df5f3..96ffe0f9e0 100644 --- a/pkg/devcontainer/config/gpu_test.go +++ b/pkg/devcontainer/config/gpu_test.go @@ -5,14 +5,21 @@ import ( "testing" ) +const ( + trueValue = "true" + falseValue = "false" + value8gb = "8gb" + value16gb = "16gb" +) + func TestGPURequirement_BoolAndString(t *testing.T) { tests := []struct { name string input string wantVal string }{ - {"true", `{"gpu": true}`, "true"}, - {"false", `{"gpu": false}`, "false"}, + {trueValue, `{"gpu": true}`, trueValue}, + {falseValue, `{"gpu": false}`, falseValue}, {"optional", `{"gpu": "optional"}`, "optional"}, } for _, tt := range tests { @@ -42,22 +49,22 @@ func TestGPURequirement_ObjectFormat(t *testing.T) { { "cores and memory", `{"gpu": {"cores": 4, "memory": "8gb"}}`, - "true", 4, "8gb", + trueValue, 4, value8gb, }, { "cores only", `{"gpu": {"cores": 2}}`, - "true", 2, "", + trueValue, 2, "", }, { "memory only", `{"gpu": {"memory": "16gb"}}`, - "true", 0, "16gb", + trueValue, 0, value16gb, }, { "empty object", `{"gpu": {}}`, - "true", 0, "", + trueValue, 0, "", }, } for _, tt := range tests { @@ -111,20 +118,28 @@ func TestShouldEnableGPU(t *testing.T) { wantWarning bool }{ {"nil GPU", &HostRequirements{}, false, false, false}, - {"true available", &HostRequirements{GPU: gpu("true")}, true, true, false}, - {"true unavailable", &HostRequirements{GPU: gpu("true")}, false, false, true}, - {"false", &HostRequirements{GPU: gpu("false")}, true, false, false}, + {"true available", &HostRequirements{GPU: gpu(trueValue)}, true, true, false}, + {"true unavailable", &HostRequirements{GPU: gpu(trueValue)}, false, false, true}, + {falseValue, &HostRequirements{GPU: gpu(falseValue)}, true, false, false}, {"optional available", &HostRequirements{GPU: gpu("optional")}, true, true, false}, {"optional unavailable", &HostRequirements{GPU: gpu("optional")}, false, false, false}, { "object available", - &HostRequirements{GPU: &GPURequirement{Value: "true", Cores: 4, GPUMemory: "8gb"}}, - true, true, false, + &HostRequirements{ + GPU: &GPURequirement{Value: trueValue, Cores: 4, GPUMemory: value8gb}, + }, + true, + true, + false, }, { "object unavailable", - &HostRequirements{GPU: &GPURequirement{Value: "true", Cores: 4, GPUMemory: "8gb"}}, - false, false, true, + &HostRequirements{ + GPU: &GPURequirement{Value: trueValue, Cores: 4, GPUMemory: value8gb}, + }, + false, + false, + true, }, } for _, tt := range tests { diff --git a/pkg/devcontainer/config/merge_test.go b/pkg/devcontainer/config/merge_test.go index f81438c9ea..db3af1b9cc 100644 --- a/pkg/devcontainer/config/merge_test.go +++ b/pkg/devcontainer/config/merge_test.go @@ -10,6 +10,13 @@ import ( "github.com/devsy-org/devsy/pkg/types" ) +const ( + value4gb = "4gb" + value1tb = "1tb" + value512mb = "512mb" + value1gb = "1gb" +) + const ( testPortRange = "3000-3002" testTouchHook = "touch /tmp/setup-test-marker" @@ -37,7 +44,7 @@ func TestMergeHostRequirements_AllNil(t *testing.T) { func TestMergeHostRequirements_SingleEntry(t *testing.T) { entries := []*ImageMetadata{ - hr(&HostRequirements{CPUs: 4, Memory: "8gb", Storage: "32gb", GPU: gpu(gpuTrue)}), + hr(&HostRequirements{CPUs: 4, Memory: value8gb, Storage: "32gb", GPU: gpu(gpuTrue)}), } got := mergeHostRequirements(entries) if got == nil { @@ -46,8 +53,8 @@ func TestMergeHostRequirements_SingleEntry(t *testing.T) { if got.CPUs != 4 { t.Errorf("CPUs = %d, want 4", got.CPUs) } - if got.Memory != "8gb" { - t.Errorf("Memory = %q, want %q", got.Memory, "8gb") + if got.Memory != value8gb { + t.Errorf("Memory = %q, want %q", got.Memory, value8gb) } if got.Storage != "32gb" { t.Errorf("Storage = %q, want %q", got.Storage, "32gb") @@ -71,35 +78,35 @@ func TestMergeHostRequirements_MaxCPUs(t *testing.T) { func TestMergeHostRequirements_MaxMemory(t *testing.T) { entries := []*ImageMetadata{ - hr(&HostRequirements{Memory: "4gb"}), - hr(&HostRequirements{Memory: "16gb"}), - hr(&HostRequirements{Memory: "8gb"}), + hr(&HostRequirements{Memory: value4gb}), + hr(&HostRequirements{Memory: value16gb}), + hr(&HostRequirements{Memory: value8gb}), } got := mergeHostRequirements(entries) - if got.Memory != "16gb" { - t.Errorf("Memory = %q, want %q", got.Memory, "16gb") + if got.Memory != value16gb { + t.Errorf("Memory = %q, want %q", got.Memory, value16gb) } } func TestMergeHostRequirements_MaxStorage(t *testing.T) { entries := []*ImageMetadata{ hr(&HostRequirements{Storage: "64gb"}), - hr(&HostRequirements{Storage: "1tb"}), + hr(&HostRequirements{Storage: value1tb}), } got := mergeHostRequirements(entries) - if got.Storage != "1tb" { - t.Errorf("Storage = %q, want %q", got.Storage, "1tb") + if got.Storage != value1tb { + t.Errorf("Storage = %q, want %q", got.Storage, value1tb) } } func TestMergeHostRequirements_MixedUnits(t *testing.T) { entries := []*ImageMetadata{ - hr(&HostRequirements{Memory: "512mb"}), - hr(&HostRequirements{Memory: "1gb"}), + hr(&HostRequirements{Memory: value512mb}), + hr(&HostRequirements{Memory: value1gb}), } got := mergeHostRequirements(entries) - if got.Memory != "1gb" { - t.Errorf("Memory = %q, want %q", got.Memory, "1gb") + if got.Memory != value1gb { + t.Errorf("Memory = %q, want %q", got.Memory, value1gb) } } @@ -149,17 +156,17 @@ func TestMergeHostRequirements_GPUEmptyPreservesValue(t *testing.T) { func TestMergeHostRequirements_MultiSource(t *testing.T) { entries := []*ImageMetadata{ - hr(&HostRequirements{CPUs: 2, Memory: "4gb"}), + hr(&HostRequirements{CPUs: 2, Memory: value4gb}), hr(&HostRequirements{CPUs: 4, Storage: "64gb", GPU: gpu(gpuOptional)}), - hr(&HostRequirements{Memory: "8gb", GPU: gpu(gpuTrue)}), + hr(&HostRequirements{Memory: value8gb, GPU: gpu(gpuTrue)}), hr(&HostRequirements{CPUs: 1, Storage: "128gb"}), } got := mergeHostRequirements(entries) if got.CPUs != 4 { t.Errorf("CPUs = %d, want 4", got.CPUs) } - if got.Memory != "8gb" { - t.Errorf("Memory = %q, want %q", got.Memory, "8gb") + if got.Memory != value8gb { + t.Errorf("Memory = %q, want %q", got.Memory, value8gb) } if got.Storage != "128gb" { t.Errorf("Storage = %q, want %q", got.Storage, "128gb") @@ -172,18 +179,18 @@ func TestMergeHostRequirements_MultiSource(t *testing.T) { func TestMergeHostRequirements_PartialFields(t *testing.T) { entries := []*ImageMetadata{ hr(&HostRequirements{CPUs: 8}), - hr(&HostRequirements{Memory: "16gb"}), - hr(&HostRequirements{Storage: "1tb"}), + hr(&HostRequirements{Memory: value16gb}), + hr(&HostRequirements{Storage: value1tb}), } got := mergeHostRequirements(entries) if got.CPUs != 8 { t.Errorf("CPUs = %d, want 8", got.CPUs) } - if got.Memory != "16gb" { - t.Errorf("Memory = %q, want %q", got.Memory, "16gb") + if got.Memory != value16gb { + t.Errorf("Memory = %q, want %q", got.Memory, value16gb) } - if got.Storage != "1tb" { - t.Errorf("Storage = %q, want %q", got.Storage, "1tb") + if got.Storage != value1tb { + t.Errorf("Storage = %q, want %q", got.Storage, value1tb) } } @@ -196,9 +203,9 @@ func TestParseByteSize(t *testing.T) { {"garbage", 0}, {"1024", 1024}, {"4kb", 4 * 1024}, - {"512mb", 512 * 1024 * 1024}, - {"8gb", 8 * 1024 * 1024 * 1024}, - {"1tb", 1024 * 1024 * 1024 * 1024}, + {value512mb, 512 * 1024 * 1024}, + {value8gb, 8 * 1024 * 1024 * 1024}, + {value1tb, 1024 * 1024 * 1024 * 1024}, {"1.5gb", uint64(1.5 * 1024 * 1024 * 1024)}, {" 16GB ", 16 * 1024 * 1024 * 1024}, } @@ -218,13 +225,13 @@ func TestMaxByteString(t *testing.T) { want string }{ {"", "", ""}, - {"", "8gb", "8gb"}, - {"8gb", "", "8gb"}, - {"4gb", "8gb", "8gb"}, - {"8gb", "4gb", "8gb"}, - {"512mb", "1gb", "1gb"}, - {"1gb", "512mb", "1gb"}, - {"1tb", "512gb", "1tb"}, + {"", value8gb, value8gb}, + {value8gb, "", value8gb}, + {value4gb, value8gb, value8gb}, + {value8gb, value4gb, value8gb}, + {value512mb, value1gb, value1gb}, + {value1gb, value512mb, value1gb}, + {value1tb, "512gb", value1tb}, } for _, tt := range tests { t.Run(tt.a+"_vs_"+tt.b, func(t *testing.T) { diff --git a/pkg/devcontainer/config/parse_test.go b/pkg/devcontainer/config/parse_test.go index a28a3d76af..e868b31f41 100644 --- a/pkg/devcontainer/config/parse_test.go +++ b/pkg/devcontainer/config/parse_test.go @@ -8,6 +8,10 @@ import ( "testing" ) +const ( + parseTestName = "test" +) + //nolint:cyclop // test function with multiple assertions func TestParseSecrets(t *testing.T) { tmpDir := t.TempDir() @@ -103,12 +107,12 @@ func TestSaveDevContainerJSON(t *testing.T) { args: args{ config: &DevContainerConfig{ ImageContainer: ImageContainer{ - Image: "test", + Image: parseTestName, }, }, }, wantErr: false, - wantJSON: `{"image":"test"}`, + wantJSON: `{"image":"` + parseTestName + `"}`, }, } for _, tt := range tests { @@ -151,7 +155,11 @@ func TestFindDevContainerConfigs(t *testing.T) { t.Fatal(err) } // #nosec G306 -- TODO Consider using a more secure permission setting and ownership if needed. - if err := os.WriteFile(fullPath, []byte(`{"name":"test"}`), 0o644); err != nil { + if err := os.WriteFile( + fullPath, + []byte(`{"name":"`+parseTestName+`"}`), + 0o644, + ); err != nil { t.Fatal(err) } } diff --git a/pkg/devcontainer/config/port_attribute_test.go b/pkg/devcontainer/config/port_attribute_test.go index 82a4a89ee3..372d2959df 100644 --- a/pkg/devcontainer/config/port_attribute_test.go +++ b/pkg/devcontainer/config/port_attribute_test.go @@ -5,6 +5,7 @@ import ( ) const ( + testPortRangeAttr = "8080-8090" labelThreeThousands = "Three-thousands" regexKeyThreeThousands = "^30\\d\\d$" ) @@ -26,7 +27,7 @@ func TestResolvePortAttribute_ExactMatch(t *testing.T) { func TestResolvePortAttribute_RangeMatch(t *testing.T) { attrs := map[string]PortAttribute{ - "8080-8090": {Label: "Dev servers", OnAutoForward: AutoForwardSilent}, + testPortRangeAttr: {Label: "Dev servers", OnAutoForward: AutoForwardSilent}, } got := ResolvePortAttribute(8085, attrs, nil) if got.Label != "Dev servers" { @@ -37,7 +38,7 @@ func TestResolvePortAttribute_RangeMatch(t *testing.T) { func TestResolvePortAttribute_RangeBoundaries(t *testing.T) { const rangeLabel = "Range" attrs := map[string]PortAttribute{ - "8080-8090": {Label: rangeLabel}, + testPortRangeAttr: {Label: rangeLabel}, } tests := []struct { port int @@ -247,7 +248,7 @@ func TestResolvePortAttribute_BareNumericKeyDoesNotSubstringMatchOtherPorts(t *t func TestResolvePortAttribute_RangeKeyDoesNotSubstringMatchOtherPorts(t *testing.T) { attrs := map[string]PortAttribute{ - "8080-8090": {Label: "should-not-leak"}, + testPortRangeAttr: {Label: "should-not-leak"}, } got := ResolvePortAttribute(180809, attrs, nil) @@ -255,7 +256,7 @@ func TestResolvePortAttribute_RangeKeyDoesNotSubstringMatchOtherPorts(t *testing t.Errorf( "Label = %q, want empty (range key %q must not substring-match a different port)", got.Label, - "8080-8090", + testPortRangeAttr, ) } } diff --git a/pkg/devcontainer/config/prebuild.go b/pkg/devcontainer/config/prebuild.go index 0b787e24ab..236a1c00cc 100644 --- a/pkg/devcontainer/config/prebuild.go +++ b/pkg/devcontainer/config/prebuild.go @@ -125,12 +125,15 @@ func readDockerignore(contextDir string, dockerfile string) ([]string, error) { dockerignorefilepath := dockerfile + ".dockerignore" if filepath.IsAbs(dockerignorefilepath) { + //nolint:gosec // path is a prebuild repository file chosen by the user f, err = os.Open(dockerignorefilepath) } else { + //nolint:gosec // path is a prebuild repository file chosen by the user f, err = os.Open(filepath.Join(contextDir, dockerignorefilepath)) } if os.IsNotExist(err) { dockerignorefilepath = ".dockerignore" + //nolint:gosec // path is a prebuild repository file chosen by the user f, err = os.Open(filepath.Join(contextDir, dockerignorefilepath)) if os.IsNotExist(err) { return ensureDockerIgnoreAndDockerFile(excludes, dockerfile, dockerignorefilepath), nil diff --git a/pkg/devcontainer/config/prebuild_test.go b/pkg/devcontainer/config/prebuild_test.go index 434f13cf19..9ca5d89efd 100644 --- a/pkg/devcontainer/config/prebuild_test.go +++ b/pkg/devcontainer/config/prebuild_test.go @@ -10,6 +10,12 @@ import ( "github.com/stretchr/testify/require" ) +const ( + prebuildTestAmd64 = "amd64" + prebuildTestArm64 = "arm64" + prebuildTestName = "test" +) + func TestNormalizeArchitecture(t *testing.T) { tests := []struct { name string @@ -20,32 +26,32 @@ func TestNormalizeArchitecture(t *testing.T) { { name: "extract from linux platform", platform: "linux/amd64", - architecture: "arm64", - want: "amd64", + architecture: prebuildTestArm64, + want: prebuildTestAmd64, }, { name: "extract from linux arm64", platform: "linux/arm64", - architecture: "amd64", - want: "arm64", + architecture: prebuildTestAmd64, + want: prebuildTestArm64, }, { name: "empty platform uses architecture", platform: "", - architecture: "amd64", - want: "amd64", + architecture: prebuildTestAmd64, + want: prebuildTestAmd64, }, { name: "non-linux platform uses architecture", platform: "windows/amd64", - architecture: "arm64", - want: "arm64", + architecture: prebuildTestArm64, + want: prebuildTestArm64, }, { name: "invalid platform format uses architecture", platform: "linux", - architecture: "amd64", - want: "amd64", + architecture: prebuildTestAmd64, + want: prebuildTestAmd64, }, } @@ -96,7 +102,7 @@ func TestNormalizeConfigForHash(t *testing.T) { t.Run("deterministic output", func(t *testing.T) { config := &DevContainerConfig{ DevContainerConfigBase: DevContainerConfigBase{ - Name: "test", + Name: prebuildTestName, }, } diff --git a/pkg/devcontainer/config/prepareprobe.go b/pkg/devcontainer/config/prepareprobe.go index fe037eed9b..916681f267 100644 --- a/pkg/devcontainer/config/prepareprobe.go +++ b/pkg/devcontainer/config/prepareprobe.go @@ -18,8 +18,8 @@ func PrepareCmdUser(cmd *exec.Cmd, userName string) error { if err != nil { return fmt.Errorf("lookup user %s: %w", userName, err) } - uid, _ := strconv.Atoi(u.Uid) - gid, _ := strconv.Atoi(u.Gid) + uid, _ := strconv.ParseUint(u.Uid, 10, 32) + gid, _ := strconv.ParseUint(u.Gid, 10, 32) cmd.Env = patchEnvVars(cmd.Environ(), map[string]string{ "HOME": u.HomeDir, "USER": u.Username, diff --git a/pkg/devcontainer/config/substitute.go b/pkg/devcontainer/config/substitute.go index 3019fe0ba4..386e5d97de 100644 --- a/pkg/devcontainer/config/substitute.go +++ b/pkg/devcontainer/config/substitute.go @@ -80,17 +80,13 @@ func Substitute(substitutionCtx *SubstitutionContext, config any, out any) error if substitutionCtx.ContainerWorkspaceFolder != "" { substitutionCtx.ContainerWorkspaceFolder = ResolveString( substitutionCtx.ContainerWorkspaceFolder, - func(match, variable string, args []string) string { - return replaceWithContext(isWindows, substitutionCtx, match, variable, args) - }, + replaceWithContext(isWindows, substitutionCtx), ) } // Two-pass substitution: pre-container fields get a restricted replacer // that preserves container-scoped variables as literals. - fullReplace := func(match, variable string, args []string) string { - return replaceWithContext(isWindows, substitutionCtx, match, variable, args) - } + fullReplace := replaceWithContext(isWindows, substitutionCtx) preFieldValues := extractPreContainerFields(newVal, fullReplace) // Full substitution for remaining fields. @@ -164,27 +160,25 @@ func replaceWithContainerEnv( } } -func replaceWithContext( - isWindows bool, - substitutionCtx *SubstitutionContext, - match, variable string, - args []string, -) string { - switch variable { - case "devcontainerId": - return valueOrMatch(substitutionCtx.DevContainerID, match) - case varLocalEnv: - return lookupValue(isWindows, substitutionCtx.Env, args, match) - case varLocalWorkspaceFolder: - return valueOrMatch(substitutionCtx.LocalWorkspaceFolder, match) - case "localWorkspaceFolderBasename": - return baseOrMatch(substitutionCtx.LocalWorkspaceFolder, match) - case "containerWorkspaceFolder": - return valueOrMatch(substitutionCtx.ContainerWorkspaceFolder, match) - case "containerWorkspaceFolderBasename": - return baseOrMatch(substitutionCtx.ContainerWorkspaceFolder, match) - default: - return match +//nolint:unparam // the value depends on the build platform; Windows builds pass true +func replaceWithContext(isWindows bool, substitutionCtx *SubstitutionContext) ReplaceFunction { + return func(match, variable string, args []string) string { + switch variable { + case "devcontainerId": + return valueOrMatch(substitutionCtx.DevContainerID, match) + case varLocalEnv: + return lookupValue(isWindows, substitutionCtx.Env, args, match) + case varLocalWorkspaceFolder: + return valueOrMatch(substitutionCtx.LocalWorkspaceFolder, match) + case "localWorkspaceFolderBasename": + return baseOrMatch(substitutionCtx.LocalWorkspaceFolder, match) + case "containerWorkspaceFolder": + return valueOrMatch(substitutionCtx.ContainerWorkspaceFolder, match) + case "containerWorkspaceFolderBasename": + return baseOrMatch(substitutionCtx.ContainerWorkspaceFolder, match) + default: + return match + } } } diff --git a/pkg/devcontainer/config/substitute_test.go b/pkg/devcontainer/config/substitute_test.go index 3a8f7bd388..5f369b3495 100644 --- a/pkg/devcontainer/config/substitute_test.go +++ b/pkg/devcontainer/config/substitute_test.go @@ -6,6 +6,11 @@ import ( "testing" ) +const ( + home = "HOME" + pathValue = "PATH" +) + func TestLookupValue(t *testing.T) { tests := []struct { name string @@ -15,35 +20,35 @@ func TestLookupValue(t *testing.T) { }{ { name: "var set no default", - env: map[string]string{"HOME": "/root"}, - args: []string{"HOME"}, - want: "/root", + env: map[string]string{home: testRootHome}, + args: []string{home}, + want: testRootHome, }, { name: "var unset no default", env: map[string]string{}, - args: []string{"MISSING"}, + args: []string{testMissingKey}, want: "", }, { name: "var unset simple default", env: map[string]string{}, - args: []string{"MISSING", "fallback"}, + args: []string{testMissingKey, "fallback"}, want: "fallback", }, { name: "var unset default with colons", env: map[string]string{}, args: []string{ - "MISSING", "http", + testMissingKey, "http", "//proxy.example.com", "8080", }, want: "http://proxy.example.com:8080", }, { name: "var set default ignored", - env: map[string]string{"VAR": "real"}, - args: []string{"VAR", "default"}, + env: map[string]string{testVarKey: "real"}, + args: []string{testVarKey, "default"}, want: "real", }, } @@ -65,11 +70,11 @@ func TestLookupValue(t *testing.T) { func TestReplaceWithContextUnknownVarPreservedAsLiteral(t *testing.T) { ctx := &SubstitutionContext{ - Env: map[string]string{"HOME": "/root"}, + Env: map[string]string{home: testRootHome}, } match := "${unknownVar}" - result := replaceWithContext( - false, ctx, match, "unknownVar", nil, + result := replaceWithContext(false, ctx)( + match, "unknownVar", nil, ) if result != match { t.Errorf( @@ -81,9 +86,9 @@ func TestReplaceWithContextUnknownVarPreservedAsLiteral(t *testing.T) { func TestReplaceWithContextPreservesContainerEnv(t *testing.T) { ctx := &SubstitutionContext{} - match := "${containerEnv:PATH}" - result := replaceWithContext( - false, ctx, match, containerEnvField, []string{testPATHKey}, + match := testContainerEnvPathVar + result := replaceWithContext(false, ctx)( + match, containerEnvField, []string{testPATHKey}, ) if result != match { t.Errorf( @@ -94,7 +99,7 @@ func TestReplaceWithContextPreservesContainerEnv(t *testing.T) { } func TestReplaceWithContainerEnvUnknownPreservedAsLiteral(t *testing.T) { - env := map[string]string{"PATH": "/usr/bin"} + env := map[string]string{pathValue: testUsrBin} match := "${unknownVar}" result := replaceWithContainerEnv( env, match, "unknownVar", nil, @@ -109,27 +114,27 @@ func TestReplaceWithContainerEnvUnknownPreservedAsLiteral(t *testing.T) { func TestReplaceWithContextLocalEnvResolves(t *testing.T) { ctx := &SubstitutionContext{ - Env: map[string]string{"HOME": "/root"}, + Env: map[string]string{home: testRootHome}, } match := "${localEnv:HOME}" - result := replaceWithContext( - false, ctx, match, "localEnv", []string{"HOME"}, + result := replaceWithContext(false, ctx)( + match, "localEnv", []string{home}, ) - if result != "/root" { + if result != testRootHome { t.Errorf( "expected localEnv to resolve to %q, got %q", - "/root", result, + testRootHome, result, ) } } func TestReplaceWithContextEnvPreservedAsLiteral(t *testing.T) { ctx := &SubstitutionContext{ - Env: map[string]string{"HOME": "/root"}, + Env: map[string]string{home: testRootHome}, } match := "${env:HOME}" - result := replaceWithContext( - false, ctx, match, "env", []string{"HOME"}, + result := replaceWithContext(false, ctx)( + match, "env", []string{home}, ) if result != match { t.Errorf( @@ -144,8 +149,8 @@ func TestReplaceWithContextEnvDefaultPreservedAsLiteral(t *testing.T) { Env: map[string]string{}, } match := "${env:MISSING:default}" - result := replaceWithContext( - false, ctx, match, "env", []string{"MISSING", "default"}, + result := replaceWithContext(false, ctx)( + match, "env", []string{testMissingKey, "default"}, ) if result != match { t.Errorf( @@ -173,6 +178,11 @@ func TestResolveStringDefaultWithColons(t *testing.T) { } const ( + testContainerEnvPathVar = "${containerEnv:PATH}" + testRootHome = "/root" + testUsrBin = "/usr/bin" + testMissingKey = "MISSING" + testVarKey = "VAR" testWorkspaceFolder = "/home/user/project" testContainerWorkspaceFolder = "/workspaces/project" testDevContainerID = "abc123" @@ -183,7 +193,7 @@ const ( testLOCKey = "LOC" testContainerWorkspaceFolderName = "containerWorkspaceFolder" testContainerWorkspaceFolderBasenameName = "containerWorkspaceFolderBasename" - testPATHKey = "PATH" + testPATHKey = pathValue testCaseResolvesCWF = "resolves containerWorkspaceFolder" testCaseResolvesCWFBasename = "resolves containerWorkspaceFolderBasename" testProjectName = "project" @@ -311,8 +321,8 @@ func TestSubstituteContainerEnvScoping(t *testing.T) { }, { name: "preserves containerEnv references", - input: map[string]string{"V": "${containerEnv:PATH}"}, - want: map[string]string{"V": "${containerEnv:PATH}"}, + input: map[string]string{"V": testContainerEnvPathVar}, + want: map[string]string{"V": testContainerEnvPathVar}, }, { name: "resolves local-scoped variables", @@ -403,11 +413,9 @@ func TestRestrictedReplacePreservesContainerVars(t *testing.T) { ctx := &SubstitutionContext{ ContainerWorkspaceFolder: testContainerWorkspaceFolder, LocalWorkspaceFolder: testWorkspaceFolder, - Env: map[string]string{"HOME": "/root"}, - } - fullReplace := func(match, variable string, args []string) string { - return replaceWithContext(false, ctx, match, variable, args) + Env: map[string]string{home: testRootHome}, } + fullReplace := replaceWithContext(false, ctx) restricted := restrictedReplace(fullReplace) tests := []struct { @@ -431,10 +439,10 @@ func TestRestrictedReplacePreservesContainerVars(t *testing.T) { }, { name: "preserves containerEnv", - match: "${containerEnv:PATH}", + match: testContainerEnvPathVar, variable: containerEnvField, args: []string{testPATHKey}, - want: "${containerEnv:PATH}", + want: testContainerEnvPathVar, }, { name: "resolves localWorkspaceFolder", @@ -446,8 +454,8 @@ func TestRestrictedReplacePreservesContainerVars(t *testing.T) { name: "resolves localEnv", match: "${localEnv:HOME}", variable: "localEnv", - args: []string{"HOME"}, - want: "/root", + args: []string{home}, + want: testRootHome, }, } diff --git a/pkg/devcontainer/config/userenvprobe.go b/pkg/devcontainer/config/userenvprobe.go index 0f61c9b926..77e4483d0b 100644 --- a/pkg/devcontainer/config/userenvprobe.go +++ b/pkg/devcontainer/config/userenvprobe.go @@ -86,6 +86,7 @@ func probeUserEnvWithFallback( preferredShell []string, userName string, ) map[string]string { + target := probeTarget{userEnvProbe, preferredShell, userName} log.Debugf( "running user env probe with shell %q, probe %q, user %q and command %q", strings.Join(preferredShell, " "), @@ -94,14 +95,7 @@ func probeUserEnvWithFallback( "cat /proc/self/environ", ) - probedEnv, err := doProbe( - ctx, - userEnvProbe, - preferredShell, - userName, - "cat /proc/self/environ", - '\x00', - ) + probedEnv, err := doProbe(ctx, target, "cat /proc/self/environ", '\x00') if err == nil { return probedEnv } @@ -114,14 +108,7 @@ func probeUserEnvWithFallback( "printenv", ) - newProbedEnv, newErr := doProbe( - ctx, - userEnvProbe, - preferredShell, - userName, - "printenv", - '\n', - ) + newProbedEnv, newErr := doProbe(ctx, target, "printenv", '\n') if newErr != nil { log.Warnf("failed to probe user environment variables: %v, %v", err, newErr) return probedEnv @@ -153,22 +140,30 @@ func parseProbeOutput(out []byte, sep byte) map[string]string { return retEnv } +type probeTarget struct { + userEnvProbe UserEnvProbe + preferredShell []string + userName string +} + func doProbe( ctx context.Context, - userEnvProbe UserEnvProbe, - preferredShell []string, - userName string, + target probeTarget, probeCmd string, sep byte, ) (map[string]string, error) { - args := preferredShell - args = append(args, getShellArgs(userEnvProbe, probeCmd)...) + args := target.preferredShell + args = append(args, getShellArgs(target.userEnvProbe, probeCmd)...) timeoutCtx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() - cmd := exec.CommandContext(timeoutCtx, args[0], args[1:]...) + //nolint:gosec // probe command comes from the devcontainer config by design + cmd := exec.CommandContext( + timeoutCtx, + args[0], + args[1:]...) //nolint:gosec // probe command comes from the devcontainer config by design - err := PrepareCmdUser(cmd, userName) + err := PrepareCmdUser(cmd, target.userName) if err != nil { return nil, fmt.Errorf("prepare probe: %w", err) } diff --git a/pkg/devcontainer/config/userenvprobe_test.go b/pkg/devcontainer/config/userenvprobe_test.go index a66c4293cb..70be6f4358 100644 --- a/pkg/devcontainer/config/userenvprobe_test.go +++ b/pkg/devcontainer/config/userenvprobe_test.go @@ -5,6 +5,13 @@ import ( "testing" ) +const ( + probeTestUsrBin = "/usr/bin" + probeTestVar = "VAR" + probeTestName = "test" + probeTestValue = "value" +) + //nolint:funlen,lll // It's just test input vectors. func TestParseProbeOutput(t *testing.T) { tests := []struct { @@ -18,9 +25,9 @@ func TestParseProbeOutput(t *testing.T) { output: []byte("PATH=/usr/bin\x00HOME=/home/user\x00USER=testuser\x00"), sep: '\x00', expected: map[string]string{ - "PATH": "/usr/bin", - "HOME": "/home/user", - "USER": "testuser", + pathValue: probeTestUsrBin, + home: "/home/user", + "USER": "testuser", }, }, { @@ -28,7 +35,7 @@ func TestParseProbeOutput(t *testing.T) { output: []byte("SIMPLE=value\x00COMPLEX=key=value=more\x00"), sep: '\x00', expected: map[string]string{ - "SIMPLE": "value", + "SIMPLE": probeTestValue, "COMPLEX": "key=value=more", }, }, @@ -40,7 +47,7 @@ func TestParseProbeOutput(t *testing.T) { sep: '\x00', expected: map[string]string{ "BASH_FUNC_scl%%": "() { if [ \"$1\" = \"load\" -o \"$1\" = \"unload\" ]; then\n eval \"module $@\";\n else\n /usr/bin/scl \"$@\";\n fi\n}", - "PATH": "/usr/bin", + pathValue: probeTestUsrBin, }, }, { @@ -58,7 +65,7 @@ func TestParseProbeOutput(t *testing.T) { sep: '\x00', expected: map[string]string{ "MULTILINE": "line1\nline2\nline3", - "SIMPLE": "test", + "SIMPLE": probeTestName, }, }, { @@ -73,7 +80,7 @@ func TestParseProbeOutput(t *testing.T) { sep: '\x00', expected: map[string]string{ "EMPTY": "", - "NONEMPTY": "value", + "NONEMPTY": probeTestValue, }, }, { @@ -81,7 +88,7 @@ func TestParseProbeOutput(t *testing.T) { output: []byte("INVALID\x00VALID=value\x00"), sep: '\x00', expected: map[string]string{ - "VALID": "value", + "VALID": probeTestValue, }, }, { @@ -89,9 +96,9 @@ func TestParseProbeOutput(t *testing.T) { output: []byte("PATH=/usr/bin\nHOME=/home/user\nUSER=testuser\n"), sep: '\n', expected: map[string]string{ - "PATH": "/usr/bin", - "HOME": "/home/user", - "USER": "testuser", + pathValue: probeTestUsrBin, + home: "/home/user", + "USER": "testuser", }, }, { @@ -110,7 +117,7 @@ func TestParseProbeOutput(t *testing.T) { output: []byte("VAR==value\x00"), sep: '\x00', expected: map[string]string{ - "VAR": "=value", + probeTestVar: "=value", }, }, { @@ -143,7 +150,7 @@ func TestParseProbeOutput(t *testing.T) { output: []byte("VAR=value\x00\x00\x00"), sep: '\x00', expected: map[string]string{ - "VAR": "value", + probeTestVar: probeTestValue, }, }, } diff --git a/pkg/devcontainer/config_overlay_recreation_test.go b/pkg/devcontainer/config_overlay_recreation_test.go index e9a0077056..5a7763994f 100644 --- a/pkg/devcontainer/config_overlay_recreation_test.go +++ b/pkg/devcontainer/config_overlay_recreation_test.go @@ -69,7 +69,7 @@ func structuralSignatureMutations() []struct { mutate: func(c *config.DevContainerConfig) { c.Dockerfile = "" }, }, { - name: "image", + name: imageValue, mutate: func(c *config.DevContainerConfig) { c.Image = exampleUpdatedImage }, }, { diff --git a/pkg/devcontainer/config_test.go b/pkg/devcontainer/config_test.go index 818195e3b4..8af07a0e20 100644 --- a/pkg/devcontainer/config_test.go +++ b/pkg/devcontainer/config_test.go @@ -11,6 +11,11 @@ import ( "github.com/stretchr/testify/suite" ) +const ( + testWorkspace = "test-workspace" + alpineLatest = "alpine:latest" +) + const ( testWorkspaceFolder = "/workspace" testDevContainerProfile = "max" @@ -35,7 +40,7 @@ func (s *SubstituteTestSuite) SetupTest() { localWorkspaceFolder: testWorkspaceFolder, workspaceConfig: &provider2.AgentWorkspaceInfo{ Workspace: &provider2.Workspace{ - ID: "test-workspace", + ID: testWorkspace, }, }, } @@ -122,7 +127,7 @@ func (s *SubstituteTestSuite) TestSubstitute_MultipleInitEnvVars() { func (s *SubstituteTestSuite) TestSubstitute_EmptyInitEnv() { rawConfig := &config.DevContainerConfig{ ImageContainer: config.ImageContainer{ - Image: "alpine:latest", + Image: alpineLatest, }, } options := provider2.CLIOptions{ @@ -132,13 +137,13 @@ func (s *SubstituteTestSuite) TestSubstitute_EmptyInitEnv() { result, _, err := s.runner.substitute(options, rawConfig) s.NoError(err) - s.Equal("alpine:latest", result.Config.Image) + s.Equal(alpineLatest, result.Config.Image) } func (s *SubstituteTestSuite) TestSubstitute_InitEnvInRemoteEnv() { rawConfig := &config.DevContainerConfig{ ImageContainer: config.ImageContainer{ - Image: "alpine:latest", + Image: alpineLatest, }, DevContainerConfigBase: config.DevContainerConfigBase{ RemoteEnv: map[string]*string{ @@ -175,7 +180,7 @@ func (s *SubstituteTestSuite) TestSubstitute_MissingVariable() { func (s *SubstituteTestSuite) TestSubstitute_AdditionalFeatures() { rawConfig := &config.DevContainerConfig{ - ImageContainer: config.ImageContainer{Image: "alpine:latest"}, + ImageContainer: config.ImageContainer{Image: alpineLatest}, } options := provider2.CLIOptions{ AdditionalFeatures: `{"ghcr.io/devcontainers/features/git:1": {}}`, @@ -189,7 +194,7 @@ func (s *SubstituteTestSuite) TestSubstitute_AdditionalFeatures() { func (s *SubstituteTestSuite) TestSubstitute_AdditionalFeaturesMergesWithExisting() { rawConfig := &config.DevContainerConfig{ - ImageContainer: config.ImageContainer{Image: "alpine:latest"}, + ImageContainer: config.ImageContainer{Image: alpineLatest}, DevContainerConfigBase: config.DevContainerConfigBase{ Features: map[string]any{ "ghcr.io/devcontainers/features/node:1": map[string]any{"version": "20"}, @@ -210,7 +215,7 @@ func (s *SubstituteTestSuite) TestSubstitute_AdditionalFeaturesMergesWithExistin func (s *SubstituteTestSuite) TestSubstitute_AdditionalFeaturesOverridesExisting() { rawConfig := &config.DevContainerConfig{ - ImageContainer: config.ImageContainer{Image: "alpine:latest"}, + ImageContainer: config.ImageContainer{Image: alpineLatest}, DevContainerConfigBase: config.DevContainerConfigBase{ Features: map[string]any{ "ghcr.io/devcontainers/features/node:1": map[string]any{"version": "18"}, @@ -232,7 +237,7 @@ func (s *SubstituteTestSuite) TestSubstitute_AdditionalFeaturesOverridesExisting func (s *SubstituteTestSuite) TestSubstitute_AdditionalFeaturesInvalidJSON() { rawConfig := &config.DevContainerConfig{ - ImageContainer: config.ImageContainer{Image: "alpine:latest"}, + ImageContainer: config.ImageContainer{Image: alpineLatest}, } options := provider2.CLIOptions{ AdditionalFeatures: `{invalid json`, @@ -246,7 +251,7 @@ func (s *SubstituteTestSuite) TestSubstitute_AdditionalFeaturesInvalidJSON() { func (s *SubstituteTestSuite) TestSubstitute_AdditionalFeaturesEmpty() { rawConfig := &config.DevContainerConfig{ - ImageContainer: config.ImageContainer{Image: "alpine:latest"}, + ImageContainer: config.ImageContainer{Image: alpineLatest}, } options := provider2.CLIOptions{ AdditionalFeatures: "", @@ -261,7 +266,7 @@ func (s *SubstituteTestSuite) TestSubstitute_AdditionalFeaturesEmpty() { func (s *SubstituteTestSuite) TestSubstitute_WorkspaceMountConsistencyApplied() { const testVal = "delegated" rawConfig := &config.DevContainerConfig{ - ImageContainer: config.ImageContainer{Image: "alpine:latest"}, + ImageContainer: config.ImageContainer{Image: alpineLatest}, } options := provider2.CLIOptions{ WorkspaceMountConsistency: testVal, @@ -276,7 +281,7 @@ func (s *SubstituteTestSuite) TestSubstitute_WorkspaceMountConsistencyApplied() func (s *SubstituteTestSuite) TestSubstitute_WorkspaceMountConsistencyReplacesExisting() { const testVal = "delegated" rawConfig := &config.DevContainerConfig{ - ImageContainer: config.ImageContainer{Image: "alpine:latest"}, + ImageContainer: config.ImageContainer{Image: alpineLatest}, NonComposeBase: config.NonComposeBase{ WorkspaceMount: ptr("type=bind,source=/src,target=/ws,consistency='consistent'"), }, @@ -294,7 +299,7 @@ func (s *SubstituteTestSuite) TestSubstitute_WorkspaceMountConsistencyReplacesEx func (s *SubstituteTestSuite) TestSubstitute_WorkspaceMountConsistencyEmpty() { rawConfig := &config.DevContainerConfig{ - ImageContainer: config.ImageContainer{Image: "alpine:latest"}, + ImageContainer: config.ImageContainer{Image: alpineLatest}, } options := provider2.CLIOptions{ WorkspaceMountConsistency: "", @@ -308,7 +313,7 @@ func (s *SubstituteTestSuite) TestSubstitute_WorkspaceMountConsistencyEmpty() { func (s *SubstituteTestSuite) TestSubstitute_CLIMountsAppended() { rawConfig := &config.DevContainerConfig{ - ImageContainer: config.ImageContainer{Image: "alpine:latest"}, + ImageContainer: config.ImageContainer{Image: alpineLatest}, } options := provider2.CLIOptions{ Mounts: []string{ @@ -331,7 +336,7 @@ func (s *SubstituteTestSuite) TestSubstitute_CLIMountsAppended() { func (s *SubstituteTestSuite) TestSubstitute_CLIMountsMergedWithExisting() { rawConfig := &config.DevContainerConfig{ - ImageContainer: config.ImageContainer{Image: "alpine:latest"}, + ImageContainer: config.ImageContainer{Image: alpineLatest}, NonComposeBase: config.NonComposeBase{ Mounts: []*config.Mount{ {Type: "bind", Source: "/existing", Target: "/existing-target"}, @@ -354,7 +359,7 @@ func (s *SubstituteTestSuite) TestSubstitute_CLIMountsMergedWithExisting() { func (s *SubstituteTestSuite) TestSubstitute_CLIMountsEmpty() { rawConfig := &config.DevContainerConfig{ - ImageContainer: config.ImageContainer{Image: "alpine:latest"}, + ImageContainer: config.ImageContainer{Image: alpineLatest}, NonComposeBase: config.NonComposeBase{ Mounts: []*config.Mount{ {Type: "bind", Source: "/existing", Target: "/existing-target"}, @@ -374,7 +379,7 @@ func (s *SubstituteTestSuite) TestSubstitute_LocalEnvInMountSource() { s.T().Setenv("HOME", "/home/test") rawConfig := &config.DevContainerConfig{ - ImageContainer: config.ImageContainer{Image: "alpine:latest"}, + ImageContainer: config.ImageContainer{Image: alpineLatest}, NonComposeBase: config.NonComposeBase{ Mounts: []*config.Mount{ { @@ -466,7 +471,7 @@ func newRunnerAt(folder string) *runner { id: "test-id", localWorkspaceFolder: folder, workspaceConfig: &provider2.AgentWorkspaceInfo{ - Workspace: &provider2.Workspace{ID: "test-workspace"}, + Workspace: &provider2.Workspace{ID: testWorkspace}, }, } } diff --git a/pkg/devcontainer/crane/run.go b/pkg/devcontainer/crane/run.go index 252fbf1ab1..ecdbdc8125 100644 --- a/pkg/devcontainer/crane/run.go +++ b/pkg/devcontainer/crane/run.go @@ -168,11 +168,16 @@ func storeFilesInDirectory(content *Content, path string) (string, error) { filePath := filepath.Join(path, filename) dir := filepath.Dir(filePath) - if err := os.MkdirAll(dir, os.ModePerm); err != nil { + if err := os.MkdirAll(dir, 0o750); err != nil { return "", err } - err := os.WriteFile(filePath, []byte(fileContent), os.ModePerm) + //nolint:gosec // feature scripts must stay executable + err := os.WriteFile( + filePath, + []byte(fileContent), + 0o755, + ) //nolint:gosec // feature scripts must stay executable if err != nil { _ = os.RemoveAll(path) return "", fmt.Errorf("failed to write file %s: %w", filename, err) diff --git a/pkg/devcontainer/delete_test.go b/pkg/devcontainer/delete_test.go index 53f74948f4..8a75463ef4 100644 --- a/pkg/devcontainer/delete_test.go +++ b/pkg/devcontainer/delete_test.go @@ -16,6 +16,10 @@ import ( "go.uber.org/zap/zaptest/observer" ) +const ( + exited = "exited" +) + const ( testContainerID = "container-abc" testStatusRunning = "running" @@ -47,7 +51,6 @@ func (m *mockDriver) DeleteDevContainer(_ context.Context, _ string) error { return m.deleteErr } -//nolint:revive // interface implementation requires 7 args func (m *mockDriver) CommandDevContainer(_ context.Context, _ *driver.CommandParams) error { return nil } @@ -73,7 +76,7 @@ func (m *mockDriver) GetDevContainerLogs( func newTestRunner(d driver.Driver) *runner { return &runner{ driver: d, - id: "test-workspace", + id: testWorkspace, workspaceConfig: &provider.AgentWorkspaceInfo{ Agent: provider.ProviderAgentConfig{ Driver: provider.CustomDriver, @@ -138,7 +141,7 @@ func TestDelete_StoppedContainer_SkipsStopAndDeletes(t *testing.T) { d := &mockDriver{ findResult: &config.ContainerDetails{ ID: testContainerID, - State: config.ContainerDetailsState{Status: "exited"}, + State: config.ContainerDetailsState{Status: exited}, Config: config.ContainerDetailsConfig{Labels: map[string]string{}}, }, } @@ -160,7 +163,7 @@ func TestDelete_DeleteError_ReturnsError(t *testing.T) { d := &mockDriver{ findResult: &config.ContainerDetails{ ID: testContainerID, - State: config.ContainerDetailsState{Status: "exited"}, + State: config.ContainerDetailsState{Status: exited}, Config: config.ContainerDetailsConfig{Labels: map[string]string{}}, }, deleteErr: fmt.Errorf("permission denied"), @@ -206,7 +209,7 @@ func TestDelete_RequiredFailure_StillRunsLeftoverSteps(t *testing.T) { d := &mockDriver{ findResult: &config.ContainerDetails{ ID: testContainerID, - State: config.ContainerDetailsState{Status: "exited"}, + State: config.ContainerDetailsState{Status: exited}, Config: config.ContainerDetailsConfig{Labels: map[string]string{}}, }, deleteErr: fmt.Errorf("daemon unreachable"), diff --git a/pkg/devcontainer/feature/extend.go b/pkg/devcontainer/feature/extend.go index 61f5a1e927..e887359b56 100644 --- a/pkg/devcontainer/feature/extend.go +++ b/pkg/devcontainer/feature/extend.go @@ -73,15 +73,8 @@ func GetExtendedBuildInfo(params *ExtendedBuildParams) (*ExtendedBuildInfo, erro } ctx := params.Ctx imageBuildInfo := params.ImageBuildInfo - target := params.Target devContainerConfig := params.DevContainerConfig - forceBuild := params.ForceBuild - secretOpts := params.SecretOpts - features, err := fetchFeatures( - executionCtx, - devContainerConfig.Config, forceBuild, secretOpts, - lockfileMode{write: true, frozen: params.FrozenLockfile, disabled: params.NoLockfile}, - ) + features, err := fetchFeatures(executionCtx, devContainerConfig.Config, params.fetchOptions()) if err != nil { return nil, fmt.Errorf("fetch features: %w", err) } @@ -116,7 +109,7 @@ func GetExtendedBuildInfo(params *ExtendedBuildParams) (*ExtendedBuildInfo, erro buildInfo, err := getFeatureBuildOptions( contextPath, &effectiveImageBuildInfo, - target, + params.Target, features, ) if err != nil { @@ -315,7 +308,7 @@ func ResolveFeatureOrderWithContext( if ctx == nil { ctx = context.Background() } - return fetchFeatures(ctx, devContainerConfig, false, nil, lockfileMode{}) + return fetchFeatures(ctx, devContainerConfig, featureFetchOptions{}) } func applyUserFallback(user, composeServiceUser, imageUser string) string { @@ -328,24 +321,41 @@ func applyUserFallback(user, composeServiceUser, imageUser string) string { return imageUser } +func (params *ExtendedBuildParams) fetchOptions() featureFetchOptions { + return featureFetchOptions{ + forceBuild: params.ForceBuild, + secretOpts: params.SecretOpts, + lockMode: lockfileMode{ + write: true, + frozen: params.FrozenLockfile, + disabled: params.NoLockfile, + }, + } +} + +// featureFetchOptions controls how fetchFeatures builds and locks the feature set. +type featureFetchOptions struct { + forceBuild bool + secretOpts *SecretOptions + lockMode lockfileMode +} + func fetchFeatures( ctx context.Context, devContainerConfig *config.DevContainerConfig, - forceBuild bool, - secretOpts *SecretOptions, - lockMode lockfileMode, + opts featureFetchOptions, ) ([]*config.FeatureSet, error) { if ctx == nil { ctx = context.Background() } - lock, err := prepareLock(devContainerConfig, lockMode) + lock, err := prepareLock(devContainerConfig, opts.lockMode) if err != nil { return nil, err } processor := &featureProcessor{ devContainerConfig: devContainerConfig, - forceBuild: forceBuild, - secretOpts: secretOpts, + forceBuild: opts.forceBuild, + secretOpts: opts.secretOpts, lock: lock, ctx: ctx, } @@ -370,7 +380,7 @@ func fetchFeatures( return nil, fmt.Errorf("failed to get sorted feature sets: %w", err) } - if err := lock.commit(devContainerConfig, lockMode); err != nil { + if err := lock.commit(devContainerConfig, opts.lockMode); err != nil { return nil, err } @@ -498,10 +508,12 @@ func (p *featureProcessor) resolveTarballFeature( log.Debugf("process feature: type=%s, id=%s", "url", id) _, pinnedIntegrity, _ := p.lock.pin(id) headers := config.GetDevsyCustomizations(p.devContainerConfig).FeatureDownloadHTTPHeaders - folder, integrity, err := processDirectTarFeature( - ctx, - id, headers, p.forceBuild, pinnedIntegrity, - ) + folder, integrity, err := processDirectTarFeature(ctx, directTarRequest{ + id: id, + httpHeaders: headers, + forceDownload: p.forceBuild, + pinnedIntegrity: pinnedIntegrity, + }) if err != nil { return nil, err } diff --git a/pkg/devcontainer/feature/features.go b/pkg/devcontainer/feature/features.go index a4bcc64d2c..bbed768bd3 100644 --- a/pkg/devcontainer/feature/features.go +++ b/pkg/devcontainer/feature/features.go @@ -598,15 +598,22 @@ func extractTarball(downloadFile, dest string) error { return nil } +// directTarRequest describes an HTTP(S) tarball feature to resolve. +type directTarRequest struct { + id string + httpHeaders map[string]string + forceDownload bool + pinnedIntegrity string +} + // processDirectTarFeature resolves an HTTP(S) tarball feature to a local folder // and returns its integrity digest (sha256: of the tarball). func processDirectTarFeature( ctx context.Context, - id string, - httpHeaders map[string]string, - forceDownload bool, - pinnedIntegrity string, + req directTarRequest, ) (folder, integrity string, err error) { + id, httpHeaders := req.id, req.httpHeaders + forceDownload, pinnedIntegrity := req.forceDownload, req.pinnedIntegrity log.Debugf("processing direct tar feature: featureId=%s, forceDownload=%v", id, forceDownload) downloadBase := id[strings.LastIndex(id, "/"):] diff --git a/pkg/devcontainer/feature/oci_retry.go b/pkg/devcontainer/feature/oci_retry.go index f88b8879d2..fed2e15767 100644 --- a/pkg/devcontainer/feature/oci_retry.go +++ b/pkg/devcontainer/feature/oci_retry.go @@ -21,8 +21,7 @@ func isTransientError(err error) bool { return false } - var terr *transport.Error - if errors.As(err, &terr) { + if terr, ok := errors.AsType[*transport.Error](err); ok { return terr.StatusCode >= http.StatusInternalServerError } diff --git a/pkg/devcontainer/graph/graph_test.go b/pkg/devcontainer/graph/graph_test.go index bd496014fc..d87ce3e053 100644 --- a/pkg/devcontainer/graph/graph_test.go +++ b/pkg/devcontainer/graph/graph_test.go @@ -7,6 +7,10 @@ import ( "github.com/stretchr/testify/suite" ) +const ( + testNode1 = "node1" +) + type GraphTestSuite struct { suite.Suite graph *Graph[string] @@ -17,22 +21,22 @@ func (suite *GraphTestSuite) SetupTest() { } func (suite *GraphTestSuite) TestAddNode() { - err := suite.graph.AddNode("node1", "data1") + err := suite.graph.AddNode(testNode1, "data1") suite.NoError(err) - suite.Equal("data1", suite.graph.nodes["node1"]) + suite.Equal("data1", suite.graph.nodes[testNode1]) // Test duplicate node error - err = suite.graph.AddNode("node1", "data2") + err = suite.graph.AddNode(testNode1, "data2") suite.Error(err) suite.Contains(err.Error(), "node1 already exists") - suite.Equal("data1", suite.graph.nodes["node1"]) + suite.Equal("data1", suite.graph.nodes[testNode1]) } func (suite *GraphTestSuite) TestAddNodes() { nodes := map[string]string{ - "node1": "data1", - "node2": "data2", - "node3": "data3", + testNode1: "data1", + "node2": "data2", + "node3": "data3", } err := suite.graph.AddNodes(nodes) @@ -44,13 +48,13 @@ func (suite *GraphTestSuite) TestAddNodes() { // Test duplicate node error duplicateNodes := map[string]string{ - "node1": "new_data1", - "node4": "data4", + testNode1: "new_data1", + "node4": "data4", } err = suite.graph.AddNodes(duplicateNodes) suite.Error(err) suite.Contains(err.Error(), "node1 already exists") - suite.Equal("data1", suite.graph.nodes["node1"]) + suite.Equal("data1", suite.graph.nodes[testNode1]) suite.NotContains(suite.graph.nodes, "node4") } @@ -194,9 +198,9 @@ func (suite *GraphTestSuite) TestUpdateNode() { { name: "update existing node", setup: func() { - suite.Require().NoError(suite.graph.AddNode("node1", "original")) + suite.Require().NoError(suite.graph.AddNode(testNode1, "original")) }, - nodeID: "node1", + nodeID: testNode1, newData: "updated", wantErr: false, }, diff --git a/pkg/devcontainer/helpers.go b/pkg/devcontainer/helpers.go index 27ea576523..cf3683f396 100644 --- a/pkg/devcontainer/helpers.go +++ b/pkg/devcontainer/helpers.go @@ -30,12 +30,18 @@ type gitRepoFindOptions struct { maxDepth int } +// FindOptions tunes how FindDevcontainerFiles searches a source. +type FindOptions struct { + MaxDepth int + StrictHostKeyChecking bool +} + func FindDevcontainerFiles( ctx context.Context, rawSource, tmpDirPath string, - maxDepth int, - strictHostKeyChecking bool, + opts FindOptions, ) (*GetWorkspaceConfigResult, error) { + maxDepth, strictHostKeyChecking := opts.MaxDepth, opts.StrictHostKeyChecking // local path isLocalPath, _ := file.IsLocalDir(rawSource) if isLocalPath { diff --git a/pkg/devcontainer/image_details_test.go b/pkg/devcontainer/image_details_test.go index 10e79d888a..5db2c9adb0 100644 --- a/pkg/devcontainer/image_details_test.go +++ b/pkg/devcontainer/image_details_test.go @@ -94,7 +94,7 @@ func TestImageDetailsScope(t *testing.T) { defaultSelection bool }{ { - "build", + overlayBuildField, func(c *config.DevContainerConfig, _ *UpOptions) { c.Build = &config.ConfigBuildOptions{} }, false, }, diff --git a/pkg/devcontainer/import_external_test.go b/pkg/devcontainer/import_external_test.go index fa815813b3..947a8b4c4d 100644 --- a/pkg/devcontainer/import_external_test.go +++ b/pkg/devcontainer/import_external_test.go @@ -94,7 +94,7 @@ func TestImportExternalDevContainer_BareFile(t *testing.T) { cfg, err := r.importExternalDevContainer(filepath.Join(external, "devcontainer.json")) require.NoError(t, err) assert.FileExists(t, filepath.Join(importedProfilePath(ws), "devcontainer.json")) - assert.Equal(t, "alpine", cfg.Image) + assert.Equal(t, alpine, cfg.Image) } func TestImportExternalDevContainer_WritesMarker(t *testing.T) { @@ -122,7 +122,7 @@ func TestImportExternalDevContainer_CollisionGetsSuffix(t *testing.T) { cfg, err := r.importExternalDevContainer(filepath.Join(external, "devcontainer.json")) require.NoError(t, err) - assert.Equal(t, "alpine", cfg.Image) + assert.Equal(t, alpine, cfg.Image) base := filepath.Base(filepath.Dir(cfg.Origin)) assert.True(t, strings.HasPrefix(base, importedProfileName+"_"), "expected a suffixed dir, got %q", base) @@ -165,7 +165,7 @@ func TestSaveSynthesizedConfig_AddsGitExclude(t *testing.T) { r := &runner{localWorkspaceFolder: ws} _, err := r.saveSynthesizedConfig(&config.DevContainerConfig{ - ImageContainer: config.ImageContainer{Image: "alpine"}, + ImageContainer: config.ImageContainer{Image: alpine}, }) require.NoError(t, err) diff --git a/pkg/devcontainer/initialize.go b/pkg/devcontainer/initialize.go index b1166cc225..9d6874962b 100644 --- a/pkg/devcontainer/initialize.go +++ b/pkg/devcontainer/initialize.go @@ -4,14 +4,17 @@ import ( "context" "errors" "fmt" + "io" "os" "os/exec" "runtime" "strings" "sync" + "time" "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/workspacelogs" ) // initializeCommand carries the shared execution state for the initializeCommand @@ -77,6 +80,7 @@ func hostShell() []string { // shell string; a multi-element command is executed argv-style. func (c *initializeCommand) run(ctx context.Context, name string, cmd []string) error { if len(cmd) == 0 { + newInitRecording(ctx, name).record(workspacelogs.InvocationFailed, "empty_command") return fmt.Errorf("initializeCommand %q is empty", name) } @@ -103,8 +107,99 @@ func (c *initializeCommand) run(ctx context.Context, name string, cmd []string) command.Stdout = stdout command.Stderr = stderr - if err := command.Run(); err != nil { + // Recording tees before the log writers filter by level. + rec := newInitRecording(ctx, name) + if rec != nil { + command.Stdout = io.MultiWriter(stdout, rec.stdout) + command.Stderr = io.MultiWriter(stderr, rec.stderr) + rec.record(workspacelogs.InvocationRunning, "") + } + + started := time.Now() + err := command.Run() + rec.finish(ctx, command.ProcessState, time.Since(started), err) + if err != nil { return fmt.Errorf("initializeCommand %q failed: %w", name, err) } return nil } + +// initRecording retains one initializeCommand sub-command's output and +// outcome. A nil value records nothing. +type initRecording struct { + rec *workspacelogs.Recorder + inv workspacelogs.Invocation + stdout, stderr io.WriteCloser +} + +func newInitRecording(ctx context.Context, name string) *initRecording { + if ctx == nil { + return nil + } + rec := workspacelogs.RecorderFrom(ctx) + if rec == nil { + return nil + } + id := workspacelogs.NewID() + + return &initRecording{ + rec: rec, + inv: workspacelogs.Invocation{ + ID: id, Phase: "lifecycle", Hook: "initializeCommand", Key: name, + }, + stdout: rec.Writer(id, workspacelogs.StreamStdout), + stderr: rec.Writer(id, workspacelogs.StreamStderr), + } +} + +func (r *initRecording) record(state workspacelogs.InvocationState, reason string) { + if r == nil { + return + } + inv := r.inv + inv.State = state + inv.Reason = reason + r.rec.Invocation(inv) +} + +// finish flushes output, then records the terminal state. +func (r *initRecording) finish( + ctx context.Context, + state *os.ProcessState, + elapsed time.Duration, + runErr error, +) { + if r == nil { + return + } + _ = r.stdout.Close() + _ = r.stderr.Close() + inv := r.inv + inv.Duration = elapsed + inv.State, inv.Reason = initOutcome(ctx, state, runErr) + if state != nil { + if sig, ok := strings.CutPrefix(state.String(), "signal: "); ok { + inv.Signal = sig + } else if code := state.ExitCode(); code >= 0 { + inv.ExitCode = &code + } + } + r.rec.Invocation(inv) +} + +func initOutcome( + ctx context.Context, + state *os.ProcessState, + runErr error, +) (workspacelogs.InvocationState, string) { + switch { + case runErr == nil: + return workspacelogs.InvocationSucceeded, "" + case ctx.Err() != nil: + return workspacelogs.InvocationCanceled, "" + case state == nil: + return workspacelogs.InvocationFailed, "start_failed" + default: + return workspacelogs.InvocationFailed, "" + } +} diff --git a/pkg/devcontainer/initialize_record_test.go b/pkg/devcontainer/initialize_record_test.go new file mode 100644 index 0000000000..d69e0c027b --- /dev/null +++ b/pkg/devcontainer/initialize_record_test.go @@ -0,0 +1,153 @@ +package devcontainer + +import ( + "context" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/devcontainer/config" + "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/types" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/zap/zapcore" +) + +func recordedInitContext(t *testing.T) (context.Context, func() []workspacelogs.Record) { + t.Helper() + store, err := workspacelogs.Open(workspacelogs.Options{Root: t.TempDir()}) + require.NoError(t, err) + m, err := store.CreateRun(workspacelogs.RunSpec{Context: recordTestContext, Command: "up"}) + require.NoError(t, err) + rec, err := workspacelogs.NewRecorder(workspacelogs.RecorderOptions{ + Backend: store, RunID: m.RunID, Location: workspacelogs.LocationHost, + }) + require.NoError(t, err) + + return workspacelogs.WithRecorder(context.Background(), rec), func() []workspacelogs.Record { + rec.Close(workspacelogs.ProducerTerminal, true) + resp, readErr := store.Read(workspacelogs.ReadRequest{RunID: m.RunID}) + require.NoError(t, readErr) + + return resp.Records + } +} + +func initConf(cmds map[string][]string) *config.DevContainerConfig { + conf := &config.DevContainerConfig{} + conf.InitializeCommand = types.LifecycleHook(cmds) + + return conf +} + +type initSummary struct { + states []workspacelogs.InvocationState + exitCode *int + reason string + stdout, stderr string +} + +func summarizeInit(recs []workspacelogs.Record) map[string]*initSummary { + byID := map[string]*initSummary{} + keys := map[string]string{} + for i := range recs { + r := &recs[i] + if r.InvocationID == "" { + continue + } + s := byID[r.InvocationID] + if s == nil { + s = &initSummary{} + byID[r.InvocationID] = s + } + switch r.Kind { + case workspacelogs.KindInvocation: + keys[r.InvocationID] = r.HookKey + s.states = append(s.states, r.State) + s.exitCode, s.reason = r.ExitCode, r.Reason + case workspacelogs.KindOutput: + if r.Stream == workspacelogs.StreamStderr { + s.stderr += r.Text + } else { + s.stdout += r.Text + } + } + } + out := map[string]*initSummary{} + for id, s := range byID { + out[keys[id]] = s + } + + return out +} + +func TestInitializeCommandRecordsOutputAtErrorLevel(t *testing.T) { + log.InitTestObserved(t, zapcore.ErrorLevel) + ctx, records := recordedInitContext(t) + conf := initConf(map[string][]string{ + "a": {"echo a-out; echo a-err >&2"}, + "b": {"echo b-out"}, + }) + + require.NoError(t, runInitializeCommand(ctx, t.TempDir(), conf, nil)) + + got := summarizeInit(records()) + require.Len(t, got, 2) + assert.Equal(t, "a-out\n", got["a"].stdout) + assert.Equal(t, "a-err\n", got["a"].stderr) + assert.Equal(t, "b-out\n", got["b"].stdout) + assert.Empty(t, got["b"].stderr) + for _, s := range got { + assert.Equal(t, []workspacelogs.InvocationState{ + workspacelogs.InvocationRunning, workspacelogs.InvocationSucceeded, + }, s.states) + } +} + +func TestInitializeCommandRecordsFailure(t *testing.T) { + ctx, records := recordedInitContext(t) + conf := initConf(map[string][]string{"": {"echo bad >&2; exit 5"}}) + + require.Error(t, runInitializeCommand(ctx, t.TempDir(), conf, nil)) + + s := summarizeInit(records())[""] + require.NotNil(t, s) + assert.Equal(t, workspacelogs.InvocationFailed, s.states[len(s.states)-1]) + require.NotNil(t, s.exitCode) + assert.Equal(t, 5, *s.exitCode) + assert.Equal(t, "bad\n", s.stderr) +} + +func TestInitializeCommandRecordsStartFailure(t *testing.T) { + ctx, records := recordedInitContext(t) + conf := initConf(map[string][]string{"": {"definitely-not-a-command-xyz", "x"}}) + + require.Error(t, runInitializeCommand(ctx, t.TempDir(), conf, nil)) + + s := summarizeInit(records())[""] + require.NotNil(t, s) + assert.Equal(t, workspacelogs.InvocationFailed, s.states[len(s.states)-1]) + assert.Equal(t, "start_failed", s.reason) + assert.Nil(t, s.exitCode) +} + +func TestInitializeCommandRecordsCancellation(t *testing.T) { + ctx, records := recordedInitContext(t) + ctx, cancel := context.WithCancel(ctx) + time.AfterFunc(300*time.Millisecond, cancel) + conf := initConf(map[string][]string{"": {"sleep 30"}}) + + require.Error(t, runInitializeCommand(ctx, t.TempDir(), conf, nil)) + + s := summarizeInit(records())[""] + require.NotNil(t, s) + assert.Equal(t, workspacelogs.InvocationCanceled, s.states[len(s.states)-1]) +} + +func TestInitializeCommandWithoutRecorderStillRuns(t *testing.T) { + conf := initConf(map[string][]string{"": {"echo ok"}}) + require.NoError(t, runInitializeCommand(context.Background(), t.TempDir(), conf, nil)) + conf = initConf(map[string][]string{"": {"exit 2"}}) + require.Error(t, runInitializeCommand(context.Background(), t.TempDir(), conf, nil)) +} diff --git a/pkg/devcontainer/prebuild.go b/pkg/devcontainer/prebuild.go index e77dee782b..9b115fffdb 100644 --- a/pkg/devcontainer/prebuild.go +++ b/pkg/devcontainer/prebuild.go @@ -13,17 +13,8 @@ import ( ) func (r *runner) Build(ctx context.Context, options provider.BuildOptions) (string, error) { - r.imageDetails = nil - r.imageDetailsImage = "" - if d, ok := r.driver.(driver.ImageDetailsDriver); ok { - if _, err := d.ImageDetails( - driver.ImageDetailsRequest{UnsupportedReason: "standalone workspace build"}, - ); err != nil { - return "", err - } - } - if r.imageBackend == nil { - return "", fmt.Errorf("building requires an image backend") + if err := r.prepareStandaloneBuild(); err != nil { + return "", err } substitutedConfig, substitutionContext, err := r.getSubstitutedConfig(options.CLIOptions) @@ -74,6 +65,22 @@ func (r *runner) Build(ctx context.Context, options provider.BuildOptions) (stri return prebuildImage, nil } +func (r *runner) prepareStandaloneBuild() error { + r.imageDetails = nil + r.imageDetailsImage = "" + if d, ok := r.driver.(driver.ImageDetailsDriver); ok { + if _, err := d.ImageDetails( + driver.ImageDetailsRequest{UnsupportedReason: "standalone workspace build"}, + ); err != nil { + return err + } + } + if r.imageBackend == nil { + return fmt.Errorf("building requires an image backend") + } + return nil +} + // determinePrebuildImage computes the prebuild image reference, defaulting an // empty PrebuildHash to "latest" to avoid an invalid reference format on push. func (r *runner) determinePrebuildImage( diff --git a/pkg/devcontainer/require_running_test.go b/pkg/devcontainer/require_running_test.go new file mode 100644 index 0000000000..e91d996635 --- /dev/null +++ b/pkg/devcontainer/require_running_test.go @@ -0,0 +1,70 @@ +package devcontainer + +import ( + "context" + "testing" + + "github.com/devsy-org/devsy/pkg/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// plainDriver does not advertise RequireRunning, like an older external runtime. +type plainDriver struct { + driver.Driver + commands int +} + +func (d *plainDriver) CommandDevContainer(context.Context, *driver.CommandParams) error { + d.commands++ + + return nil +} + +type capableDriver struct { + plainDriver + got *driver.CommandParams +} + +func (d *capableDriver) CommandDevContainer(_ context.Context, p *driver.CommandParams) error { + d.got = p + + return nil +} + +func (d *capableDriver) SupportsRequireRunning() bool { return true } + +const testCmd = "true" + +func TestCommandRequireRunningWithoutCapabilityNeverExecs(t *testing.T) { + d := &plainDriver{} + r := &runner{driver: d} + + err := r.Command(context.Background(), CommandParams{Command: testCmd, RequireRunning: true}) + + require.ErrorIs(t, err, ErrRequireRunningUnsupported) + assert.Zero(t, d.commands, "the ordinary command path must not be used as a fallback") +} + +func TestCommandDefaultsStillExecWithoutCapability(t *testing.T) { + d := &plainDriver{} + r := &runner{driver: d} + + require.NoError(t, r.Command(context.Background(), CommandParams{Command: testCmd})) + assert.Equal(t, 1, d.commands) +} + +func TestCommandPropagatesRequireRunning(t *testing.T) { + d := &capableDriver{} + r := &runner{driver: d, id: "ws"} + + err := r.Command(context.Background(), CommandParams{ + Command: testCmd, RawStdout: true, RequireRunning: true, + }) + + require.NoError(t, err) + require.NotNil(t, d.got) + assert.True(t, d.got.RequireRunning) + assert.True(t, d.got.RawStdout) + assert.Equal(t, "ws", d.got.WorkspaceID) +} diff --git a/pkg/devcontainer/run.go b/pkg/devcontainer/run.go index ef450ceb13..31b252cd92 100644 --- a/pkg/devcontainer/run.go +++ b/pkg/devcontainer/run.go @@ -2,6 +2,7 @@ package devcontainer import ( "context" + "errors" "fmt" "io" "time" @@ -15,6 +16,7 @@ import ( "github.com/devsy-org/devsy/pkg/log" "github.com/devsy-org/devsy/pkg/provider" "github.com/devsy-org/devsy/pkg/status" + "github.com/devsy-org/devsy/pkg/workspacelogs" ) // Runner drives the lifecycle of a single workspace's dev container. @@ -46,8 +48,15 @@ type CommandParams struct { Stderr io.Writer // RawStdout keeps binary protocols out of text redaction and its suffix buffer. RawStdout bool + // RequireRunning makes the call exec-only: it never starts or unpauses the + // workload and fails when the runtime cannot guarantee that. + RequireRunning bool } +// ErrRequireRunningUnsupported is returned when the runtime cannot enforce an +// exec-only call. +var ErrRequireRunningUnsupported = errors.New("runtime does not support exec-only commands") + // UpOptions configures a single Up invocation. type UpOptions struct { provider.CLIOptions @@ -160,21 +169,7 @@ func (r *runner) Up( timeout, ) - var substitutedConfig *config.SubstitutedConfig - var substitutionContext *config.SubstitutionContext - err := status.Run( - ctx, - reporter, - status.Operation{Phase: status.PhaseResolvingConfig}, - func(ctx context.Context) error { - var resolveErr error - substitutedConfig, substitutionContext, resolveErr = r.getSubstitutedConfigWithContext( - ctx, - options.CLIOptions, - ) - return resolveErr - }, - ) + substitutedConfig, substitutionContext, err := r.resolveUpConfig(ctx, reporter, options) if err != nil { return nil, err } @@ -186,49 +181,10 @@ func (r *runner) Up( } } - // Recovery skips initializeCommand: a failing host hook must not block the - // recovery container. - switch { - case options.Recovery: - status.Skip(reporter, status.PhaseInitializeCommand, "recovery mode") - case options.Platform.Enabled: - // Platform workspaces execute initialization remotely; this host-side - // phase is intentionally not run and must not appear successful. - status.Skip(reporter, status.PhaseInitializeCommand, "platform mode") - default: - err = status.Run( - ctx, - reporter, - status.Operation{Phase: status.PhaseInitializeCommand}, - func(ctx context.Context) error { - if err := r.runInitializeCommand( - ctx, - substitutedConfig.Config, - options, - ); err != nil { - return clierr.Recoverable(fmt.Errorf("initialize command: %w", err)) - } - return nil - }, - ) - if err != nil { - return nil, err - } - } - - if err := r.prepareImageDetails(substitutedConfig, options, defaultSelection); err != nil { + if err := r.runInitializePhase(ctx, reporter, substitutedConfig, options); err != nil { return nil, err } - if defaultSelection { - // Detection follows initialization so the hook can generate project files. - if err := r.resolveDefaultContainerConfig(substitutedConfig.Config, options); err != nil { - return nil, err - } - if err := r.checkOverlayRecreation(ctx, substitutedConfig, options); err != nil { - return nil, err - } - } - if err := r.validateOverlayUpAssets(ctx, substitutedConfig, options); err != nil { + if err := r.afterInitialize(ctx, substitutedConfig, options, defaultSelection); err != nil { return nil, err } params := &runContainerParams{ @@ -249,24 +205,24 @@ func (r *runner) Up( return dispatchErr }, ) - if result != nil { - result.RecoveryContainer = r.recovering - if selectionWarning != "" { - result.HostWarnings = append(result.HostWarnings, selectionWarning) - } - } + r.annotateUpResult(result, selectionWarning) return result, err } func (r *runner) Command(ctx context.Context, params CommandParams) error { + if params.RequireRunning && !driver.DriverSupportsRequireRunning(r.driver) { + return ErrRequireRunningUnsupported + } + return r.driver.CommandDevContainer(ctx, &driver.CommandParams{ - WorkspaceID: r.id, - User: params.User, - Command: params.Command, - Stdin: params.Stdin, - Stdout: params.Stdout, - Stderr: params.Stderr, - RawStdout: params.RawStdout, + WorkspaceID: r.id, + User: params.User, + Command: params.Command, + Stdin: params.Stdin, + Stdout: params.Stdout, + Stderr: params.Stderr, + RawStdout: params.RawStdout, + RequireRunning: params.RequireRunning, }) } @@ -282,6 +238,101 @@ func (r *runner) Logs(ctx context.Context, writer io.Writer) error { return r.driver.GetDevContainerLogs(ctx, r.id, writer, writer) } +func (r *runner) annotateUpResult(result *config.Result, selectionWarning string) { + if result == nil { + return + } + result.RecoveryContainer = r.recovering + if selectionWarning != "" { + result.HostWarnings = append(result.HostWarnings, selectionWarning) + } +} + +func (r *runner) afterInitialize( + ctx context.Context, + substitutedConfig *config.SubstitutedConfig, + options UpOptions, + defaultSelection bool, +) error { + if err := r.prepareImageDetails(substitutedConfig, options, defaultSelection); err != nil { + return err + } + if defaultSelection { + // Detection follows initialization so the hook can generate project files. + if err := r.resolveDefaultContainerConfig(substitutedConfig.Config, options); err != nil { + return err + } + if err := r.checkOverlayRecreation(ctx, substitutedConfig, options); err != nil { + return err + } + } + return r.validateOverlayUpAssets(ctx, substitutedConfig, options) +} + +// runInitializePhase skips initializeCommand in recovery and platform modes. +// Recovery skips it so a failing host hook cannot block the recovery container. +func (r *runner) runInitializePhase( + ctx context.Context, + reporter status.Reporter, + substitutedConfig *config.SubstitutedConfig, + options UpOptions, +) error { + switch { + case options.Recovery: + status.Skip(reporter, status.PhaseInitializeCommand, "recovery mode") + recordInitializeSkipped(ctx, "recovery_excluded") + return nil + case options.Platform.Enabled: + // Platform workspaces execute initialization remotely; this host-side + // phase is intentionally not run and must not appear successful. + status.Skip(reporter, status.PhaseInitializeCommand, "platform mode") + recordInitializeSkipped(ctx, "platform_excluded") + return nil + default: + return status.Run( + ctx, + reporter, + status.Operation{Phase: status.PhaseInitializeCommand}, + func(ctx context.Context) error { + if err := r.runInitializeCommand( + ctx, + substitutedConfig.Config, + options, + ); err != nil { + return clierr.Recoverable(fmt.Errorf("initialize command: %w", err)) + } + return nil + }, + ) + } +} + +func (r *runner) resolveUpConfig( + ctx context.Context, + reporter status.Reporter, + options UpOptions, +) (*config.SubstitutedConfig, *config.SubstitutionContext, error) { + var substitutedConfig *config.SubstitutedConfig + var substitutionContext *config.SubstitutionContext + err := status.Run( + ctx, + reporter, + status.Operation{Phase: status.PhaseResolvingConfig}, + func(ctx context.Context) error { + var resolveErr error + substitutedConfig, substitutionContext, resolveErr = r.getSubstitutedConfigWithContext( + ctx, + options.CLIOptions, + ) + return resolveErr + }, + ) + if err != nil { + return nil, nil, err + } + return substitutedConfig, substitutionContext, nil +} + // dispatchByConfigKind routes to the container implementation for the config's // kind (image/Dockerfile, compose, or default/auto-detected). func (r *runner) dispatchByConfigKind( @@ -306,6 +357,22 @@ func (r *runner) dispatchByConfigKind( } } +// recordInitializeSkipped keeps the reason the host-side initializeCommand did +// not run in the retained setup history. +func recordInitializeSkipped(ctx context.Context, reason string) { + rec := workspacelogs.RecorderFrom(ctx) + if rec == nil { + return + } + rec.Invocation(workspacelogs.Invocation{ + ID: workspacelogs.NewID(), + Phase: "lifecycle", + Hook: "initializeCommand", + State: workspacelogs.InvocationSkipped, + Reason: reason, + }) +} + // runInitializeCommand runs the host-side initializeCommand hook. The hook is // never executed in platform mode. func (r *runner) runInitializeCommand( diff --git a/pkg/devcontainer/run_record_test.go b/pkg/devcontainer/run_record_test.go new file mode 100644 index 0000000000..910b75243c --- /dev/null +++ b/pkg/devcontainer/run_record_test.go @@ -0,0 +1,40 @@ +package devcontainer + +import ( + "context" + "testing" + + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const recordTestContext = "default" + +func TestInitializeSkipIsRecordedWithItsReason(t *testing.T) { + store, err := workspacelogs.Open(workspacelogs.Options{Root: t.TempDir()}) + require.NoError(t, err) + session := workspacelogs.StartSessionIn(store, workspacelogs.SessionOptions{ + Run: workspacelogs.RunSpec{Context: recordTestContext, Command: "up"}, + Location: workspacelogs.LocationHost, + }) + require.NotNil(t, session) + + recordInitializeSkipped(session.Context(context.Background()), "recovery_excluded") + recordInitializeSkipped(context.Background(), "ignored without a recorder") + session.Finish(nil, true) + + resp, err := store.Read( + workspacelogs.ReadRequest{RunID: session.RunID, Hook: "initializeCommand"}, + ) + require.NoError(t, err) + var skipped []workspacelogs.Record + for _, rec := range resp.Records { + if rec.Kind == workspacelogs.KindInvocation { + skipped = append(skipped, rec) + } + } + require.Len(t, skipped, 1) + assert.Equal(t, workspacelogs.InvocationSkipped, skipped[0].State) + assert.Equal(t, "recovery_excluded", skipped[0].Reason) +} diff --git a/pkg/devcontainer/setup.go b/pkg/devcontainer/setup.go index 2fd5904122..daad4dea4f 100644 --- a/pkg/devcontainer/setup.go +++ b/pkg/devcontainer/setup.go @@ -109,8 +109,7 @@ func (r *runner) setupContainer( ctx, info.result, setupCommand, - params.secretsEnv, - params.secretsMount, + setupSecrets{env: params.secretsEnv, mount: params.secretsMount}, ) return executeErr }, @@ -409,6 +408,7 @@ func (r *runner) compressWorkspaceConfig( Source: r.workspaceConfig.Workspace.Source, Agent: r.workspaceConfig.Agent, ContentFolder: r.workspaceConfig.ContentFolder, + SetupRun: r.setupRunRef(cliOptions), } workspaceConfig.Agent.Kubernetes.ImageConfigFile = "" lifecycle, err := r.lifecycleOptions(mergedConfig, dockerless, imageEntrypoint) @@ -562,28 +562,36 @@ func (r *runner) isDebugMode() bool { return log.DebugEnabled() } +// setupSecrets are the secret env vars and mount paths passed to lifecycle commands. +type setupSecrets struct { + env, mount []string +} + func (r *runner) executeSetup( ctx context.Context, result *config.Result, setupCommand string, - secretsEnv, secretsMount []string, + secretOpts setupSecrets, ) (*config.Result, error) { + secretsEnv, secretsMount := secretOpts.env, secretOpts.mount + setupLogOptions := tunnelserver.SetupLogOptionsFrom(ctx) runSetupServer := func(ctx context.Context, stdin io.WriteCloser, stdout io.Reader) (*config.Result, error) { - return tunnelserver.RunSetupServer( - ctx, - stdout, - stdin, - r.workspaceConfig.Agent.InjectGitCredentials != stringFalse, - r.workspaceConfig.Agent.InjectDockerCredentials != stringFalse, - result, - tunnelserver.WithPlatformOptions(&r.workspaceConfig.CLIOptions.Platform), - tunnelserver.WithSecrets( - secretsEnv, - secretsMount, - ), - tunnelserver.WithGitToken(r.workspaceConfig.CLIOptions.GitToken), - tunnelserver.WithStatusReporter(r.reporter), - ) + return tunnelserver.RunSetupServer(ctx, tunnelserver.SetupSpec{ + Reader: stdout, + Writer: stdin, + AllowGitCredentials: r.workspaceConfig.Agent.InjectGitCredentials != stringFalse, + AllowDockerCredentials: r.workspaceConfig.Agent.InjectDockerCredentials != stringFalse, + SetupInfo: result, + Options: append([]tunnelserver.Option{ + tunnelserver.WithPlatformOptions(&r.workspaceConfig.CLIOptions.Platform), + tunnelserver.WithSecrets( + secretsEnv, + secretsMount, + ), + tunnelserver.WithGitToken(r.workspaceConfig.CLIOptions.GitToken), + tunnelserver.WithStatusReporter(r.reporter), + }, setupLogOptions...), + }) } sshTunnelArgv := r.buildSSHTunnelArgv() @@ -703,3 +711,20 @@ func filterWorkspaceMounts( return retMounts } + +// setupRunRef describes the run this setup records into, or nil when the CLI +// options name none. +func (r *runner) setupRunRef(cliOptions provider2.CLIOptions) *provider2.SetupRunRef { + ws := r.workspaceConfig.Workspace + if cliOptions.SetupRunID == "" || ws == nil { + return nil + } + + return &provider2.SetupRunRef{ + RunID: cliOptions.SetupRunID, + ParentProducerID: cliOptions.SetupParentProducerID, + Context: ws.Context, + WorkspaceUID: ws.UID, + WorkspaceID: ws.ID, + } +} diff --git a/pkg/devcontainer/setup/lifecyclehooks.go b/pkg/devcontainer/setup/lifecyclehooks.go index a41a4b71db..5cb5662ac8 100644 --- a/pkg/devcontainer/setup/lifecyclehooks.go +++ b/pkg/devcontainer/setup/lifecyclehooks.go @@ -11,6 +11,7 @@ import ( "path/filepath" "regexp" "slices" + "strconv" "strings" "sync" "time" @@ -21,6 +22,7 @@ import ( "github.com/devsy-org/devsy/pkg/secrets" "github.com/devsy-org/devsy/pkg/subprocess" "github.com/devsy-org/devsy/pkg/types" + "github.com/devsy-org/devsy/pkg/workspacelogs" ) // LifecyclePhase identifies a devcontainer lifecycle command. @@ -130,6 +132,12 @@ type hookRunParams struct { redactor *secrets.Redactor name string content string + // hook is the lifecycle property name recorded for retained history. + hook string + // index is the merged-config position of the command being run. + index int + // plan shares invocation IDs between planning and execution. + plan *hookPlan } // lifecycleEnv holds the resolved environment for running lifecycle hooks. @@ -235,6 +243,7 @@ func preAttachPhaseParams( commands: mc.OnCreateCommands, env: env, name: "onCreateCommands", + hook: string(PhaseOnCreate), content: cd.Created, }, }, @@ -245,6 +254,7 @@ func preAttachPhaseParams( commands: mc.UpdateContentCommands, env: env, name: "updateContentCommands", + hook: string(PhaseUpdateContent), content: updateContentMarker, }, }, @@ -255,6 +265,7 @@ func preAttachPhaseParams( commands: mc.PostCreateCommands, env: env, name: "postCreateCommands", + hook: string(PhasePostCreate), content: cd.Created, }, }, @@ -265,6 +276,7 @@ func preAttachPhaseParams( commands: mc.PostStartCommands, env: env, name: "postStartCommands", + hook: string(PhasePostStart), content: cd.State.StartedAt, }, }, @@ -321,8 +333,11 @@ func RunPreAttachHooks( created := setupInfo.ContainerDetails.Created all = insertDotfilesPhase(ctx, all, opts.Dotfiles, created) + recordExcludedPhases(ctx, all, opts) + // Remove skipped phases. all = filterSkippedPhases(all, opts.Skip) + planPhases(ctx, all, opts.Prebuild) if opts.Prebuild { return DeferredHooks{}, runPrebuildHooks(all) @@ -404,11 +419,14 @@ func insertDotfilesPhase( // runPrebuildHooks runs only onCreateCommand and updateContentCommand. func runPrebuildHooks(all []phaseHook) error { - for _, ph := range all { + for i, ph := range all { if ph.phase != PhaseOnCreate && ph.phase != PhaseUpdateContent { continue } if err := runPhaseHook(ph); err != nil { + recordNotReached(slices.DeleteFunc(slices.Clone(all[i+1:]), func(rest phaseHook) bool { + return prebuildExcluded(rest.phase) + })) return err } } @@ -425,18 +443,23 @@ func runWithWaitFor( waitFor LifecyclePhase, ) ([]phaseHook, error) { if waitFor == PhaseInitializeCommand { + for _, ph := range all { + ph.params.recordAll(workspacelogs.InvocationDeferred, "") + } return append([]phaseHook(nil), all...), nil } pastWaitFor := false var deferred []phaseHook - for _, ph := range all { + for i, ph := range all { if pastWaitFor { + ph.params.recordAll(workspacelogs.InvocationDeferred, "") deferred = append(deferred, ph) continue } if err := runPhaseHook(ph); err != nil { + recordNotReached(all[i+1:]) return nil, err } if ph.phase == waitFor { @@ -474,8 +497,9 @@ func (d DeferredHooks) Empty() bool { // Run executes all deferred hooks sequentially. func (d DeferredHooks) Run() error { - for _, ph := range d.hooks { + for i, ph := range d.hooks { if err := runPhaseHook(ph); err != nil { + recordNotReached(d.hooks[i+1:]) return err } } @@ -484,28 +508,42 @@ func (d DeferredHooks) Run() error { // RunPostAttachHooks runs postAttachCommand only. // These run after the IDE has been opened and can be long-running. +// PostAttachOptions carries the inputs of RunPostAttachHooks. +type PostAttachOptions struct { + SecretsEnv []string + SecretsMount []string + Skip bool +} + func RunPostAttachHooks( ctx context.Context, setupInfo *config.Result, - secretsEnv []string, - secretsMount []string, - skipPostAttach ...bool, + opts PostAttachOptions, ) error { + secretsEnv, secretsMount := opts.SecretsEnv, opts.SecretsMount + skipPostAttach := []bool{opts.Skip} + params := hookRunParams{ + ctx: ctx, + commands: setupInfo.MergedConfig.PostAttachCommands, + name: "postAttachCommands", + content: "", + hook: string(PhasePostAttach), + plan: &hookPlan{}, + } if len(skipPostAttach) > 0 && skipPostAttach[0] { log.Debugf("skipping postAttachCommand (--skip-post-attach set)") + params.recordSkipped(reasonExplicitSkip) return nil } - env := resolveLifecycleEnv(ctx, setupInfo) - redactor := mergeSecretsEnv(env.remoteEnv, secretsEnv, secretsMount) + params.env = resolveLifecycleEnv(ctx, setupInfo) + params.redactor = mergeSecretsEnv(params.env.remoteEnv, secretsEnv, secretsMount) + if len(params.commands) == 0 { + params.recordSkipped(reasonNoCommand) + return nil + } + params.recordAll(workspacelogs.InvocationPending, "") - return runHook(hookRunParams{ - ctx: ctx, - commands: setupInfo.MergedConfig.PostAttachCommands, - env: env, - redactor: redactor, - name: "postAttachCommands", - content: "", - }) + return runHook(params) } func runHook(p hookRunParams) error { @@ -513,9 +551,15 @@ func runHook(p hookRunParams) error { return nil } - if skip, err := shouldSkipHook(p.name, p.content); err != nil || skip { + skip, err := shouldSkipHook(p.name, p.content) + if err != nil { + p.recordAll(workspacelogs.InvocationFailed, reasonMarkerCheckFailed) return err } + if skip { + p.recordSkipped(reasonAlreadyAttempted) + return nil + } envArr := buildEnvArr(p.env.remoteEnv) return executeHookCommands(p.ctx, p, envArr) @@ -537,11 +581,13 @@ func buildEnvArr(remoteEnv map[string]string) []string { } func executeHookCommands(ctx context.Context, p hookRunParams, envArr []string) error { - for _, cmd := range p.commands { + for i, cmd := range p.commands { if len(cmd) == 0 { continue } + p.index = i if err := executeLifecycleHook(ctx, p, envArr, cmd); err != nil { + p.recordFrom(i+1, workspacelogs.InvocationSkipped, reasonNotReached) return err } } @@ -559,7 +605,7 @@ func executeLifecycleHook( ) error { if len(hook) <= 1 { for k, c := range hook { - return runSingleHookCommand(ctx, p, envArr, k, c) + return runSingleHookCommand(ctx, p, envArr, hookCommand{key: k, c: c}) } } @@ -573,7 +619,7 @@ func executeLifecycleHook( for k, c := range hook { go func() { defer wg.Done() - if err := runSingleHookCommand(ctx, p, envArr, k, c); err != nil { + if err := runSingleHookCommand(ctx, p, envArr, hookCommand{key: k, c: c}); err != nil { mu.Lock() errs = append(errs, fmt.Errorf("named command %q failed: %w", k, err)) mu.Unlock() @@ -585,35 +631,47 @@ func executeLifecycleHook( return errors.Join(errs...) } +// hookCommand is one named command of a lifecycle phase. +type hookCommand struct { + key string + c []string +} + func runSingleHookCommand( ctx context.Context, p hookRunParams, remoteEnvArr []string, - key string, c []string, + cmd hookCommand, ) error { + key, c := cmd.key, cmd.c log.Debugf( "running %s lifecycle hook: %s %s", p.name, key, p.redactor.Redact(strings.Join(c, " ")), ) + inv := p.invocation(ctx, key) currentUser, err := user.Current() if err != nil { + inv.record(workspacelogs.InvocationFailed, reasonUserLookupFailed) return err } if len(c) == 0 { log.Debugf("skipping empty command for lifecycle hook %s", p.name) + inv.record(workspacelogs.InvocationSkipped, reasonNoCommand) return nil } args := buildCommandArgs(c, p.env.remoteUser, currentUser.Username) resolvedPath, err := exec.LookPath(args[0]) if err != nil { + inv.record(workspacelogs.InvocationFailed, reasonCommandNotFound) return fmt.Errorf("command not found: %s: %w", args[0], err) } return executeAndCapture(ctx, executeCaptureOptions{ binary: resolvedPath, args: args, phaseName: p.name, key: key, command: c, dir: p.env.workspaceFolder, env: remoteEnvArr, redactor: p.redactor, + inv: inv, }) } @@ -621,22 +679,30 @@ type executeCaptureOptions struct { binary, phaseName, key, dir string args, command, env []string redactor *secrets.Redactor + inv invocationRef } func executeAndCapture(ctx context.Context, options executeCaptureOptions) error { if ctx == nil { ctx = context.Background() } + sink, closeSink := options.inv.sink() + options.inv.record(workspacelogs.InvocationRunning, "") result, err := subprocess.Run(ctx, options.binary, options.args[1:], subprocess.Options{ - Dir: options.dir, - Env: options.env, - Redactor: options.redactor, + Dir: options.dir, + Env: options.env, + Redactor: options.redactor, + Diagnostic: sink, }) + // Flush retained output before the terminal record so readers never see + // a finished invocation with output still pending. + closeSink() + options.inv.finish(ctx, result, err) if err != nil { // Include only the bounded, redacted tail in the returned error. This // gives normal users actionable diagnostics without replaying an // unbounded subprocess stream or requiring debug mode. - details := result.DiagnosticOutput() + details := joinDetails(result.DiagnosticOutput(), options.inv.hint()) if details != "" { return fmt.Errorf( "%s: command %q failed: %s: %w", @@ -727,3 +793,257 @@ func buildCommandArgs(c []string, remoteUser, currentUsername string) []string { } return c } + +// Skip and failure reasons recorded without command text or environment. +const ( + reasonNoCommand = "no_command_configured" + reasonExplicitSkip = "explicit_skip" + reasonAlreadyAttempted = "already_attempted" + reasonPrebuildExcluded = "prebuild_excluded" + reasonMarkerCheckFailed = "marker_check_failed" + reasonNotReached = "not_reached" + reasonUserLookupFailed = "user_lookup_failed" + reasonCommandNotFound = "command_not_found" + reasonStartFailed = "start_failed" + reasonSignaled = "signaled" + hookPhase = "lifecycle" +) + +type hookCmdKey struct { + index int + key string +} + +// hookPlan hands out one invocation ID per command so the planned, deferred +// and executed records of a command share an identity. +type hookPlan struct { + mu sync.Mutex + ids map[hookCmdKey]string +} + +func (pl *hookPlan) id(runID, hook string, index int, key string) string { + if pl == nil { + return workspacelogs.NewID() + } + if runID != "" { + return workspacelogs.DeterministicID(runID, hook, strconv.Itoa(index), key) + } + pl.mu.Lock() + defer pl.mu.Unlock() + k := hookCmdKey{index: index, key: key} + if id, ok := pl.ids[k]; ok { + return id + } + if pl.ids == nil { + pl.ids = map[hookCmdKey]string{} + } + id := workspacelogs.NewID() + pl.ids[k] = id + + return id +} + +// invocationRef records transitions of one hook command. The zero value +// records nothing. +type invocationRef struct { + rec *workspacelogs.Recorder + inv workspacelogs.Invocation +} + +func recorderFrom(ctx context.Context) *workspacelogs.Recorder { + if ctx == nil { + return nil + } + + return workspacelogs.RecorderFrom(ctx) +} + +func (p hookRunParams) invocation(ctx context.Context, key string) invocationRef { + rec := recorderFrom(ctx) + if rec == nil { + return invocationRef{} + } + index := p.index + + return invocationRef{rec: rec, inv: workspacelogs.Invocation{ + ID: p.plan.id( + rec.RunID(), + p.hook, + index, + key, + ), + Phase: hookPhase, + Hook: p.hook, + Index: &index, + Key: key, + }} +} + +// recordAll records state for every command of the phase. A phase with no +// commands has nothing to record. +func (p hookRunParams) recordAll(state workspacelogs.InvocationState, reason string) { + p.recordFrom(0, state, reason) +} + +// recordFrom records state for every command of the phase from index first on. +func (p hookRunParams) recordFrom(first int, state workspacelogs.InvocationState, reason string) { + if recorderFrom(p.ctx) == nil || p.hook == "" { + return + } + for i := first; i < len(p.commands); i++ { + for _, key := range slices.Sorted(maps.Keys(p.commands[i])) { + q := p + q.index = i + q.invocation(p.ctx, key).record(state, reason) + } + } +} + +// recordNotReached marks the commands of phases that never started because an +// earlier phase failed. +func recordNotReached(phases []phaseHook) { + for _, ph := range phases { + ph.params.recordFrom(0, workspacelogs.InvocationSkipped, reasonNotReached) + } +} + +// recordSkipped records every command of the phase as skipped. A phase with +// no commands records one skipped invocation with no index. +func (p hookRunParams) recordSkipped(reason string) { + rec := recorderFrom(p.ctx) + if rec == nil || p.hook == "" { + return + } + if len(p.commands) == 0 { + rec.Invocation(workspacelogs.Invocation{ + ID: workspacelogs.NewID(), Phase: hookPhase, Hook: p.hook, + State: workspacelogs.InvocationSkipped, Reason: reasonNoCommand, + }) + return + } + p.recordAll(workspacelogs.InvocationSkipped, reason) +} + +func (r invocationRef) record(state workspacelogs.InvocationState, reason string) { + if r.rec == nil { + return + } + inv := r.inv + inv.State = state + inv.Reason = reason + r.rec.Invocation(inv) +} + +// finish records the terminal state from the command result. +func (r invocationRef) finish(ctx context.Context, res subprocess.Result, err error) { + if r.rec == nil { + return + } + inv := r.inv + inv.Duration = res.Duration + inv.Signal = res.Signal + inv.State, inv.Reason = outcomeState(ctx, res, err) + if err == nil || res.ExitCode >= 0 { + code := res.ExitCode + inv.ExitCode = &code + } + r.rec.Invocation(inv) +} + +func outcomeState( + ctx context.Context, + res subprocess.Result, + err error, +) (workspacelogs.InvocationState, string) { + switch { + case err == nil: + return workspacelogs.InvocationSucceeded, "" + case ctx.Err() != nil: + return workspacelogs.InvocationCanceled, "" + case res.Signal != "": + return workspacelogs.InvocationFailed, reasonSignaled + case res.ExitCode < 0: + return workspacelogs.InvocationFailed, reasonStartFailed + default: + return workspacelogs.InvocationFailed, "" + } +} + +// sink returns the diagnostic destinations and a function that flushes them. +func (r invocationRef) sink() (subprocess.DiagnosticSink, func()) { + if r.rec == nil { + return subprocess.DiagnosticSink{}, func() {} + } + stdout := r.rec.Writer(r.inv.ID, workspacelogs.StreamStdout) + stderr := r.rec.Writer(r.inv.ID, workspacelogs.StreamStderr) + + return subprocess.DiagnosticSink{Stdout: stdout, Stderr: stderr}, func() { + _ = stdout.Close() + _ = stderr.Close() + } +} + +// hint tells the reader where retained output lives. It is empty when no run +// is being recorded. +func (r invocationRef) hint() string { + if r.rec == nil || r.rec.RunID() == "" { + return "" + } + hint := "full output: devsy workspace logs --source setup --run " + r.rec.RunID() + if r.inv.Hook != "" { + hint += " --hook " + r.inv.Hook + } + + return hint +} + +func joinDetails(output, hint string) string { + if hint == "" { + return output + } + if output == "" { + return hint + } + + return output + "\n" + hint +} + +// recordExcludedPhases records phases that will not run because of a skip flag +// or prebuild mode, or because they have no command. +func recordExcludedPhases(ctx context.Context, all []phaseHook, opts PreAttachOptions) { + skipped := map[LifecyclePhase]bool{ + PhasePostCreate: opts.Skip.PostCreate, + PhasePostStart: opts.Skip.PostStart, + PhasePostAttach: opts.Skip.PostAttach, + } + for _, ph := range all { + p := ph.params + p.ctx = ctx + switch { + case len(p.commands) == 0: + p.recordSkipped(reasonNoCommand) + case skipped[ph.phase]: + p.recordSkipped(reasonExplicitSkip) + case opts.Prebuild && prebuildExcluded(ph.phase): + p.recordSkipped(reasonPrebuildExcluded) + } + } +} + +func prebuildExcluded(phase LifecyclePhase) bool { + return phase != PhaseOnCreate && phase != PhaseUpdateContent +} + +// planPhases assigns the pending state to every command that may still run. +func planPhases(ctx context.Context, all []phaseHook, prebuild bool) { + for i := range all { + p := &all[i].params + p.plan = &hookPlan{} + if prebuild && prebuildExcluded(all[i].phase) { + continue + } + pc := *p + pc.ctx = ctx + pc.recordAll(workspacelogs.InvocationPending, "") + } +} diff --git a/pkg/devcontainer/setup/lifecyclehooks_record_test.go b/pkg/devcontainer/setup/lifecyclehooks_record_test.go new file mode 100644 index 0000000000..03c8d63869 --- /dev/null +++ b/pkg/devcontainer/setup/lifecyclehooks_record_test.go @@ -0,0 +1,431 @@ +package setup + +import ( + "context" + "fmt" + "maps" + "os/user" + "slices" + "strings" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/devcontainer/config" + "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/types" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/zap/zapcore" +) + +type recordedRun struct { + store *workspacelogs.Store + runID string + rec *workspacelogs.Recorder + ctx context.Context +} + +func newRecordedRun(t *testing.T) *recordedRun { + t.Helper() + store, err := workspacelogs.Open(workspacelogs.Options{Root: t.TempDir()}) + require.NoError(t, err) + m, err := store.CreateRun(workspacelogs.RunSpec{Context: "default", Command: "up"}) + require.NoError(t, err) + rec, err := workspacelogs.NewRecorder(workspacelogs.RecorderOptions{ + Backend: store, RunID: m.RunID, Location: workspacelogs.LocationContainer, + }) + require.NoError(t, err) + + return &recordedRun{ + store: store, runID: m.RunID, rec: rec, + ctx: workspacelogs.WithRecorder(context.Background(), rec), + } +} + +func (r *recordedRun) records(t *testing.T) []workspacelogs.Record { + t.Helper() + r.rec.Close(workspacelogs.ProducerTerminal, true) + resp, err := r.store.Read(workspacelogs.ReadRequest{ + RunID: r.runID, Limit: workspacelogs.MaxReadLimit, + }) + require.NoError(t, err) + + return resp.Records +} + +// invocation is the folded view of one invocation's records. +type invocation struct { + hook, key, reason string + index *int + states []workspacelogs.InvocationState + exitCode *int + stdout, stderr string +} + +func fold(recs []workspacelogs.Record) map[string]*invocation { + out := map[string]*invocation{} + for i := range recs { + r := &recs[i] + if r.InvocationID == "" { + continue + } + inv := out[r.InvocationID] + if inv == nil { + inv = &invocation{} + out[r.InvocationID] = inv + } + switch r.Kind { + case workspacelogs.KindInvocation: + inv.hook, inv.key, inv.index = r.Hook, r.HookKey, r.HookIndex + inv.reason, inv.exitCode = r.Reason, r.ExitCode + inv.states = append(inv.states, r.State) + case workspacelogs.KindOutput: + if r.Stream == workspacelogs.StreamStderr { + inv.stderr += r.Text + } else { + inv.stdout += r.Text + } + } + } + + return out +} + +func (inv *invocation) final() workspacelogs.InvocationState { + return inv.states[len(inv.states)-1] +} + +func byKey(t *testing.T, recs []workspacelogs.Record, hook string) map[string]*invocation { + t.Helper() + out := map[string]*invocation{} + for _, inv := range fold(recs) { + if inv.hook == hook { + slot := "-" + if inv.index != nil { + slot = fmt.Sprint(*inv.index) + } + out[slot+"/"+inv.key] = inv + } + } + + return out +} + +func testParams(t *testing.T, ctx context.Context, hooks ...types.LifecycleHook) hookRunParams { + t.Helper() + u, err := user.Current() + require.NoError(t, err) + + return hookRunParams{ + ctx: ctx, + commands: hooks, + env: lifecycleEnv{remoteUser: u.Username, workspaceFolder: t.TempDir()}, + name: "testHooks", + hook: "postCreateCommand", + plan: &hookPlan{}, + } +} + +func TestRecordsStringAndArgvCommands(t *testing.T) { + run := newRecordedRun(t) + p := testParams(t, run.ctx, + types.LifecycleHook{"": {"echo out; echo err >&2"}}, + types.LifecycleHook{"": {"sh", "-c", "echo argv-out"}}, + ) + + require.NoError(t, executeHookCommands(run.ctx, p, nil)) + + got := byKey(t, run.records(t), "postCreateCommand") + require.Len(t, got, 2) + assert.Equal(t, "out\n", got["0/"].stdout) + assert.Equal(t, "err\n", got["0/"].stderr) + assert.Equal(t, "argv-out\n", got["1/"].stdout) + for _, inv := range got { + assert.Equal(t, + []workspacelogs.InvocationState{ + workspacelogs.InvocationRunning, workspacelogs.InvocationSucceeded, + }, inv.states) + require.NotNil(t, inv.exitCode) + assert.Zero(t, *inv.exitCode) + } +} + +func TestRecordsParallelNamedCommandsKeepOwnOutput(t *testing.T) { + run := newRecordedRun(t) + p := testParams(t, run.ctx, types.LifecycleHook{ + "alpha": {"echo alpha-out"}, + "beta": {"echo beta-out; exit 4"}, + }) + + err := executeHookCommands(run.ctx, p, nil) + require.Error(t, err) + + got := byKey(t, run.records(t), "postCreateCommand") + require.Len(t, got, 2) + assert.Equal(t, "alpha-out\n", got["0/alpha"].stdout) + assert.Equal(t, "beta-out\n", got["0/beta"].stdout) + assert.Equal(t, workspacelogs.InvocationSucceeded, got["0/alpha"].final()) + assert.Equal(t, workspacelogs.InvocationFailed, got["0/beta"].final()) + require.NotNil(t, got["0/beta"].exitCode) + assert.Equal(t, 4, *got["0/beta"].exitCode) +} + +func TestRecordsFailureEvenWhenCallerIgnoresError(t *testing.T) { + run := newRecordedRun(t) + p := testParams(t, run.ctx, types.LifecycleHook{"": {"echo boom >&2; exit 3"}}) + + _ = executeHookCommands(run.ctx, p, nil) + + for _, inv := range byKey(t, run.records(t), "postCreateCommand") { + assert.Equal(t, workspacelogs.InvocationFailed, inv.final()) + assert.Equal(t, "boom\n", inv.stderr) + assert.Equal(t, 3, *inv.exitCode) + } +} + +func TestRecordsUnreachedCommandsAfterFailureAreSkipped(t *testing.T) { + run := newRecordedRun(t) + p := testParams(t, run.ctx, + types.LifecycleHook{"": {"exit 3"}}, + types.LifecycleHook{"": {neverRun}}, + types.LifecycleHook{"first": {neverRun}, "second": {neverRun}}, + ) + + require.Error(t, executeHookCommands(run.ctx, p, nil)) + + got := byKey(t, run.records(t), "postCreateCommand") + assert.Equal(t, workspacelogs.InvocationFailed, got["0/"].final()) + for _, slot := range []string{"1/", "2/first", "2/second"} { + require.Contains(t, got, slot) + assert.Equal(t, workspacelogs.InvocationSkipped, got[slot].final(), slot) + assert.Equal(t, reasonNotReached, got[slot].reason, slot) + } +} + +func TestRecordsLaterPhasesAreSkippedWhenAnEarlierPhaseFails(t *testing.T) { + run := newRecordedRun(t) + result := hooksResult(t, t.TempDir()) + result.MergedConfig.OnCreateCommands = []types.LifecycleHook{{"": {"exit 5"}}} + + _, err := RunPreAttachHooks(run.ctx, result, PreAttachOptions{}) + require.Error(t, err) + + recs := run.records(t) + assert.Equal(t, workspacelogs.InvocationFailed, byKey(t, recs, "onCreateCommand")["0/"].final()) + for _, hook := range []string{"postCreateCommand", "postStartCommand"} { + inv := byKey(t, recs, hook)["0/"] + require.NotNil(t, inv, hook) + assert.Equal(t, workspacelogs.InvocationSkipped, inv.final(), hook) + assert.Equal(t, reasonNotReached, inv.reason, hook) + } +} + +func TestRecordsSilentSuccessHasNoOutput(t *testing.T) { + run := newRecordedRun(t) + p := testParams(t, run.ctx, types.LifecycleHook{"": {"true"}}) + + require.NoError(t, executeHookCommands(run.ctx, p, nil)) + + got := byKey(t, run.records(t), "postCreateCommand") + require.Len(t, got, 1) + assert.Equal(t, workspacelogs.InvocationSucceeded, got["0/"].final()) + assert.Empty(t, got["0/"].stdout+got["0/"].stderr) +} + +func TestRecordsOutputAtQuietLogLevel(t *testing.T) { + log.InitTestObserved(t, zapcore.ErrorLevel) + run := newRecordedRun(t) + p := testParams(t, run.ctx, types.LifecycleHook{"": {"echo kept"}}) + + require.NoError(t, executeHookCommands(run.ctx, p, nil)) + + assert.Equal(t, "kept\n", byKey(t, run.records(t), "postCreateCommand")["0/"].stdout) +} + +func TestRecordsStartFailure(t *testing.T) { + run := newRecordedRun(t) + p := testParams(t, run.ctx, types.LifecycleHook{"": {"definitely-not-a-command-xyz", "arg"}}) + u, err := user.Current() + require.NoError(t, err) + p.env.remoteUser = u.Username + + require.Error(t, executeHookCommands(run.ctx, p, nil)) + + inv := byKey(t, run.records(t), "postCreateCommand")["0/"] + assert.Equal(t, workspacelogs.InvocationFailed, inv.final()) + assert.Equal(t, reasonCommandNotFound, inv.reason) + assert.Nil(t, inv.exitCode) +} + +func TestRecordsCancellation(t *testing.T) { + run := newRecordedRun(t) + ctx, cancel := context.WithCancel(run.ctx) + p := testParams(t, ctx, types.LifecycleHook{"": {"sleep 30"}}) + time.AfterFunc(300*time.Millisecond, cancel) + + require.Error(t, executeHookCommands(ctx, p, nil)) + + inv := byKey(t, run.records(t), "postCreateCommand")["0/"] + assert.Equal(t, workspacelogs.InvocationCanceled, inv.final()) +} + +func TestRecordsMarkerSkipIsAlreadyAttempted(t *testing.T) { + run := newRecordedRun(t) + p := testParams(t, run.ctx, types.LifecycleHook{"": {"true"}}) + p.name = "recordTestMarker" + workspacelogs.NewID() + p.content = "created-1" + if _, err := markerExists(p.name, p.content); err != nil { + t.Skipf("marker directory unavailable: %v", err) + } + + require.NoError(t, runHook(p)) + require.NoError(t, runHook(p)) + + inv := byKey(t, run.records(t), "postCreateCommand") + require.Len(t, inv, 1) + states := inv["0/"].states + assert.Equal(t, workspacelogs.InvocationSkipped, states[len(states)-1]) + assert.Equal(t, reasonAlreadyAttempted, inv["0/"].reason) +} + +func hooksResult(t *testing.T, dir string) *config.Result { + t.Helper() + u, err := user.Current() + require.NoError(t, err) + cmd := func(s string) []types.LifecycleHook { + return []types.LifecycleHook{{"": {s}}} + } + + return &config.Result{ + MergedConfig: &config.MergedDevContainerConfig{ + DevContainerConfigBase: config.DevContainerConfigBase{RemoteUser: u.Username}, + UpdatedConfigProperties: config.UpdatedConfigProperties{ + OnCreateCommands: cmd("echo created"), + PostCreateCommands: cmd("echo later"), + PostStartCommands: cmd("echo started"), + }, + }, + ContainerDetails: &config.ContainerDetails{State: config.ContainerDetailsState{}}, + SubstitutionContext: &config.SubstitutionContext{ContainerWorkspaceFolder: dir}, + } +} + +func TestRecordsPlanningSkipReasonsAndDeferred(t *testing.T) { + run := newRecordedRun(t) + result := hooksResult(t, t.TempDir()) + + deferred, err := RunPreAttachHooks(run.ctx, result, PreAttachOptions{ + Skip: SkipPhases{PostStart: true}, + }) + require.NoError(t, err) + require.NoError(t, deferred.Run()) + + recs := run.records(t) + create := byKey(t, recs, "onCreateCommand")["0/"] + assert.Equal(t, []workspacelogs.InvocationState{ + workspacelogs.InvocationPending, workspacelogs.InvocationRunning, + workspacelogs.InvocationSucceeded, + }, create.states) + assert.Equal(t, "created\n", create.stdout) + + content := byKey(t, recs, "updateContentCommand")["-/"] + require.NotNil(t, content) + assert.Equal(t, workspacelogs.InvocationSkipped, content.final()) + assert.Equal(t, reasonNoCommand, content.reason) + + // Same invocation ID before and after the background worker runs it. + post := byKey(t, recs, "postCreateCommand")["0/"] + assert.Equal(t, []workspacelogs.InvocationState{ + workspacelogs.InvocationPending, workspacelogs.InvocationDeferred, + workspacelogs.InvocationRunning, workspacelogs.InvocationSucceeded, + }, post.states) + + start := byKey(t, recs, "postStartCommand")["0/"] + assert.Equal(t, reasonExplicitSkip, start.reason) + assert.Empty(t, start.stdout) +} + +func TestRecordsPrebuildExclusion(t *testing.T) { + run := newRecordedRun(t) + result := hooksResult(t, t.TempDir()) + + _, err := RunPreAttachHooks(run.ctx, result, PreAttachOptions{Prebuild: true}) + require.NoError(t, err) + + recs := run.records(t) + post := byKey(t, recs, "postCreateCommand")["0/"] + assert.Equal(t, workspacelogs.InvocationSkipped, post.final()) + assert.Equal(t, reasonPrebuildExcluded, post.reason) + assert.Equal( + t, + workspacelogs.InvocationSucceeded, + byKey(t, recs, "onCreateCommand")["0/"].final(), + ) +} + +func TestRecordsPostAttachSkipAndRun(t *testing.T) { + skippedRun, ranRun := newRecordedRun(t), newRecordedRun(t) + result := hooksResult(t, t.TempDir()) + result.MergedConfig.PostAttachCommands = []types.LifecycleHook{{"": {"echo attached"}}} + + require.NoError(t, RunPostAttachHooks(skippedRun.ctx, result, PostAttachOptions{Skip: true})) + require.NoError(t, RunPostAttachHooks(ranRun.ctx, result, PostAttachOptions{})) + + var skipped, ran int + invocations := slices.Concat( + slices.Collect(maps.Values(fold(skippedRun.records(t)))), + slices.Collect(maps.Values(fold(ranRun.records(t)))), + ) + for _, inv := range invocations { + switch inv.final() { + case workspacelogs.InvocationSkipped: + skipped++ + assert.Equal(t, reasonExplicitSkip, inv.reason) + case workspacelogs.InvocationSucceeded: + ran++ + assert.Equal(t, "attached\n", inv.stdout) + } + } + assert.Equal(t, 1, skipped) + assert.Equal(t, 1, ran) +} + +func TestNoRecorderKeepsBehaviorAndErrorText(t *testing.T) { + failing := types.LifecycleHook{"": {"echo oops >&2; exit 2"}} + + plain := testParams(t, context.Background(), failing) + plainErr := executeHookCommands(context.Background(), plain, nil) + require.Error(t, plainErr) + assert.NotContains(t, plainErr.Error(), "full output") + assert.Contains(t, plainErr.Error(), "oops") + + run := newRecordedRun(t) + recorded := testParams(t, run.ctx, failing) + recErr := executeHookCommands(run.ctx, recorded, nil) + require.Error(t, recErr) + assert.Contains(t, recErr.Error(), "oops") + assert.Contains( + t, + recErr.Error(), + "--source setup --run "+run.runID+" --hook postCreateCommand", + ) + assert.True(t, strings.HasPrefix(recErr.Error(), "testHooks: command")) +} + +func TestHookPlanGivesSameIDToCommandAcrossProcesses(t *testing.T) { + runID := workspacelogs.NewID() + first := (&hookPlan{}).id(runID, "postCreateCommand", 0, "build") + second := (&hookPlan{}).id(runID, "postCreateCommand", 0, "build") + + assert.Equal(t, first, second) + assert.NotEqual(t, first, (&hookPlan{}).id(runID, "postCreateCommand", 1, "build")) + assert.NotEqual( + t, + first, + (&hookPlan{}).id(workspacelogs.NewID(), "postCreateCommand", 0, "build"), + ) + assert.True(t, workspacelogs.ValidID(first)) +} + +const neverRun = "echo never" diff --git a/pkg/devcontainer/setup/lifecyclehooks_test.go b/pkg/devcontainer/setup/lifecyclehooks_test.go index 325f89461a..d9248aef42 100644 --- a/pkg/devcontainer/setup/lifecyclehooks_test.go +++ b/pkg/devcontainer/setup/lifecyclehooks_test.go @@ -18,6 +18,16 @@ import ( "go.uber.org/zap/zapcore" ) +const ( + hello = "hello" +) + +const ( + testWorkspacePath = "/workspaces/test" + testEchoCmd = "echo" + testDotfilesRepo = "https://github.com/user/dotfiles" +) + type LifecycleHookTestSuite struct { suite.Suite } @@ -35,9 +45,9 @@ func (s *LifecycleHookTestSuite) TestArrayCommand() { currentUser, err := user.Current() s.Require().NoError(err) - c := []string{"echo", "hello", "world"} + c := []string{testEchoCmd, hello, "world"} args := buildCommandArgs(c, currentUser.Username, currentUser.Username) - assert.Equal(s.T(), []string{"echo", "hello", "world"}, args) + assert.Equal(s.T(), []string{testEchoCmd, hello, "world"}, args) } func (s *LifecycleHookTestSuite) TestArrayCommandWithShellWrapper() { @@ -62,7 +72,7 @@ func (s *LifecycleHookTestSuite) TestArrayCommandWithUserSwitch() { currentUser, err := user.Current() s.Require().NoError(err) - c := []string{"echo", "hello"} + c := []string{testEchoCmd, hello} args := buildCommandArgs(c, "otheruser", currentUser.Username) assert.Equal(s.T(), []string{"su", "otheruser", "-c", "echo hello"}, args) } @@ -95,7 +105,7 @@ func (s *LifecycleHookTestSuite) TestLifecycleHooksNoOpWithEmptyConfig() { State: config.ContainerDetailsState{}, }, SubstitutionContext: &config.SubstitutionContext{ - ContainerWorkspaceFolder: "/workspaces/test", + ContainerWorkspaceFolder: testWorkspacePath, }, } @@ -103,7 +113,7 @@ func (s *LifecycleHookTestSuite) TestLifecycleHooksNoOpWithEmptyConfig() { assert.NoError(s.T(), err) assert.True(s.T(), deferred.Empty()) - err = RunPostAttachHooks(ctx, result, nil, nil) + err = RunPostAttachHooks(ctx, result, PostAttachOptions{}) assert.NoError(s.T(), err) } @@ -126,7 +136,7 @@ func (s *LifecycleHookTestSuite) TestResolveLifecycleEnvIncludesSecrets() { State: config.ContainerDetailsState{}, }, SubstitutionContext: &config.SubstitutionContext{ - ContainerWorkspaceFolder: "/workspaces/test", + ContainerWorkspaceFolder: testWorkspacePath, }, } @@ -254,7 +264,7 @@ func (s *LifecycleHookTestSuite) TestPrebuildIgnoresWaitFor() { State: config.ContainerDetailsState{}, }, SubstitutionContext: &config.SubstitutionContext{ - ContainerWorkspaceFolder: "/workspaces/test", + ContainerWorkspaceFolder: testWorkspacePath, }, } @@ -418,7 +428,7 @@ func (s *LifecycleHookTestSuite) TestSingleStringCommandBackwardCompat() { err = executeHookCommands(context.Background(), p, envArr) assert.NoError(t, err) - assertFileContains(t, dir, "out.txt", "hello") + assertFileContains(t, dir, "out.txt", hello) } func (s *LifecycleHookTestSuite) TestSingleNamedCommandNoGoroutine() { @@ -472,7 +482,7 @@ func (s *LifecycleHookTestSuite) TestInsertDotfilesPhaseOrdering() { all := makeTestPhaseHooks() ctx := context.Background() - cfg := DotfilesConfig{Repository: "https://github.com/user/dotfiles"} + cfg := DotfilesConfig{Repository: testDotfilesRepo} result := insertDotfilesPhase(ctx, all, cfg, "test-created") // Should have 5 phases: onCreate, updateContent, postCreate, dotfiles, postStart @@ -503,7 +513,7 @@ func (s *LifecycleHookTestSuite) TestDotfilesPhaseHasRunFunc() { all := makeTestPhaseHooks() ctx := context.Background() - cfg := DotfilesConfig{Repository: "https://github.com/user/dotfiles"} + cfg := DotfilesConfig{Repository: testDotfilesRepo} result := insertDotfilesPhase(ctx, all, cfg, "") dotfilesHook := result[3] @@ -516,7 +526,7 @@ func (s *LifecycleHookTestSuite) TestRunWithWaitForDefaultSplitWithDotfiles() { all := makeTestPhaseHooks() ctx := context.Background() - cfg := DotfilesConfig{Repository: "https://github.com/user/dotfiles"} + cfg := DotfilesConfig{Repository: testDotfilesRepo} all = insertDotfilesPhase(ctx, all, cfg, "") deferred, err := runWithWaitFor(all, DefaultWaitFor) @@ -535,7 +545,7 @@ func (s *LifecycleHookTestSuite) TestRunWithWaitForPostCreateWithDotfiles() { all := makeTestPhaseHooks() ctx := context.Background() - cfg := DotfilesConfig{Repository: "https://github.com/user/dotfiles"} + cfg := DotfilesConfig{Repository: testDotfilesRepo} all = insertDotfilesPhase(ctx, all, cfg, "") deferred, err := runWithWaitFor(all, PhasePostCreate) @@ -551,7 +561,7 @@ func (s *LifecycleHookTestSuite) TestDeferredHooksNotEmptyWithDotfiles() { t := s.T() ctx := context.Background() - cfg := DotfilesConfig{Repository: "https://github.com/user/dotfiles"} + cfg := DotfilesConfig{Repository: testDotfilesRepo} all := insertDotfilesPhase(ctx, nil, cfg, "") d := DeferredHooks{hooks: all} @@ -560,7 +570,7 @@ func (s *LifecycleHookTestSuite) TestDeferredHooksNotEmptyWithDotfiles() { func (s *LifecycleHookTestSuite) TestPromoteDotfilesWaitForDefault() { t := s.T() - cfg := DotfilesConfig{Repository: "https://github.com/user/dotfiles"} + cfg := DotfilesConfig{Repository: testDotfilesRepo} // Default waitFor (updateContentCommand) should be promoted to PhaseDotfiles. result := promoteDotfilesWaitFor(DefaultWaitFor, cfg) @@ -569,7 +579,7 @@ func (s *LifecycleHookTestSuite) TestPromoteDotfilesWaitForDefault() { func (s *LifecycleHookTestSuite) TestPromoteDotfilesWaitForPostCreate() { t := s.T() - cfg := DotfilesConfig{Repository: "https://github.com/user/dotfiles"} + cfg := DotfilesConfig{Repository: testDotfilesRepo} // postCreateCommand is before dotfiles, so it should be promoted. result := promoteDotfilesWaitFor(PhasePostCreate, cfg) @@ -578,7 +588,7 @@ func (s *LifecycleHookTestSuite) TestPromoteDotfilesWaitForPostCreate() { func (s *LifecycleHookTestSuite) TestPromoteDotfilesWaitForPostStartNotPromoted() { t := s.T() - cfg := DotfilesConfig{Repository: "https://github.com/user/dotfiles"} + cfg := DotfilesConfig{Repository: testDotfilesRepo} // postStartCommand is after dotfiles, no promotion needed. result := promoteDotfilesWaitFor(PhasePostStart, cfg) @@ -587,7 +597,7 @@ func (s *LifecycleHookTestSuite) TestPromoteDotfilesWaitForPostStartNotPromoted( func (s *LifecycleHookTestSuite) TestPromoteDotfilesWaitForInitializeCommandNotPromoted() { t := s.T() - cfg := DotfilesConfig{Repository: "https://github.com/user/dotfiles"} + cfg := DotfilesConfig{Repository: testDotfilesRepo} // initializeCommand defers everything; dotfiles promotion must not override it. result := promoteDotfilesWaitFor(PhaseInitializeCommand, cfg) @@ -606,7 +616,7 @@ func (s *LifecycleHookTestSuite) TestPhaseHasCommandsTrue() { all := []phaseHook{ { phase: PhaseOnCreate, - params: hookRunParams{commands: []types.LifecycleHook{{"": {"echo", "hi"}}}}, + params: hookRunParams{commands: []types.LifecycleHook{{"": {testEchoCmd, "hi"}}}}, }, } assert.True(s.T(), phaseHasCommands(all, PhaseOnCreate)) @@ -725,7 +735,7 @@ func (s *LifecycleHookTestSuite) TestPostAttachHooksRunEveryTime() { // Run postAttachCommand multiple times - it must execute every time. for i := 1; i <= 3; i++ { - err := RunPostAttachHooks(context.Background(), result, nil, nil) + err := RunPostAttachHooks(context.Background(), result, PostAttachOptions{}) assert.NoError(t, err) content, readErr := os.ReadFile(counterFile) //nolint:gosec // test file from TempDir @@ -750,8 +760,8 @@ func (s *LifecycleHookTestSuite) TestPostCreateHookUsesOnceSemantics() { result := &config.Result{ MergedConfig: &config.MergedDevContainerConfig{ UpdatedConfigProperties: config.UpdatedConfigProperties{ - PostCreateCommands: []types.LifecycleHook{{"": {"echo", "hi"}}}, - PostStartCommands: []types.LifecycleHook{{"": {"echo", "hi"}}}, + PostCreateCommands: []types.LifecycleHook{{"": {testEchoCmd, "hi"}}}, + PostStartCommands: []types.LifecycleHook{{"": {testEchoCmd, "hi"}}}, }, }, ContainerDetails: &config.ContainerDetails{ diff --git a/pkg/devcontainer/setup/setup.go b/pkg/devcontainer/setup/setup.go index 89d9024d2e..1cd3d98e8f 100644 --- a/pkg/devcontainer/setup/setup.go +++ b/pkg/devcontainer/setup/setup.go @@ -26,6 +26,7 @@ import ( "github.com/devsy-org/devsy/pkg/log" "github.com/devsy-org/devsy/pkg/secrets" "github.com/devsy-org/devsy/pkg/sharedfile" + "github.com/devsy-org/devsy/pkg/workspacelogs" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" ) @@ -82,14 +83,7 @@ func SetupContainerPreAttach( return DeferredHooks{}, err } - if err := writeSecretFiles(cfg); err != nil { - return DeferredHooks{}, err - } - if err := writeSecretEnvironment( - cfg.SecretsEnv, - cfg.TerminalSecretEnvNames, - config.GetRemoteUser(cfg.SetupInfo), - ); err != nil { + if err := writeSecrets(cfg); err != nil { return DeferredHooks{}, err } @@ -120,13 +114,11 @@ func SetupContainerPreAttach( // Called after the IDE has been opened. func SetupContainerPostAttach(ctx context.Context, cfg *ContainerSetupConfig) error { log.Debugf("running post-attach lifecycle hooks") - if err := RunPostAttachHooks( - ctx, - cfg.SetupInfo, - cfg.SecretsEnv, - cfg.SecretsMount, - cfg.SkipPostAttach, - ); err != nil { + if err := RunPostAttachHooks(ctx, cfg.SetupInfo, PostAttachOptions{ + SecretsEnv: cfg.SecretsEnv, + SecretsMount: cfg.SecretsMount, + Skip: cfg.SkipPostAttach, + }); err != nil { return fmt.Errorf("lifecycle hooks post-attach: %w", err) } @@ -380,10 +372,6 @@ func secretMountPath(target string) (string, error) { return filepath.Join(config.SecretsMountDir, target), nil } -func writeResultFile(cfg *ContainerSetupConfig) error { - return writeResultFileWithContext(context.Background(), cfg) -} - func writeResultFileWithContext(ctx context.Context, cfg *ContainerSetupConfig) error { rawBytes, err := json.Marshal(cfg.SetupInfo) if err != nil { @@ -471,6 +459,17 @@ func setupWorkspaceOwnership(cfg *ContainerSetupConfig) error { return nil } +func writeSecrets(cfg *ContainerSetupConfig) error { + if err := writeSecretFiles(cfg); err != nil { + return err + } + return writeSecretEnvironment( + cfg.SecretsEnv, + cfg.TerminalSecretEnvNames, + config.GetRemoteUser(cfg.SetupInfo), + ) +} + func setupEnvironment(cfg *ContainerSetupConfig) error { log.Debugf("patching etc environment") @@ -540,14 +539,9 @@ func linkRootHome(setupInfo *config.Result) error { func chownWorkspace(setupInfo *config.Result, recursive bool) error { workspaceFolder := setupInfo.SubstitutionContext.ContainerWorkspaceFolder - present, err := workspaceFolderPresent(workspaceFolder) - if err != nil || !present { - return err - } - workspaceID := os.Getenv(pkgconfig.EnvWorkspaceID) - latched, err := workspaceChownLatched(workspaceID) - if err != nil || latched { + skip, err := skipWorkspaceChown(workspaceFolder, workspaceID) + if err != nil || skip { return err } @@ -566,6 +560,14 @@ func chownWorkspace(setupInfo *config.Result, recursive bool) error { return recordWorkspaceChown(workspaceID) } +func skipWorkspaceChown(workspaceFolder, workspaceID string) (bool, error) { + present, err := workspaceFolderPresent(workspaceFolder) + if err != nil || !present { + return true, err + } + return workspaceChownLatched(workspaceID) +} + // Compose services are not guaranteed a workspaceMount. func workspaceFolderPresent(workspaceFolder string) (bool, error) { _, err := os.Lstat(workspaceFolder) @@ -993,6 +995,18 @@ func containerDataDir() string { return writableContainerDataDirOnce() } +// WorkspaceLogsRoot returns the container-side spool for retained setup +// history, inside the already selected secure data directory, or empty when no +// secure directory is available. The caller opens it as a workspacelogs store. +func WorkspaceLogsRoot() string { + dir := containerDataDir() + if dir == "" { + return "" + } + + return filepath.Join(dir, workspacelogs.SpoolDirName) +} + func setupPlatformGitCredentials( ctx context.Context, userName string, diff --git a/pkg/devcontainer/setup_test.go b/pkg/devcontainer/setup_test.go index 72e3dec24d..09c0d500e5 100644 --- a/pkg/devcontainer/setup_test.go +++ b/pkg/devcontainer/setup_test.go @@ -26,6 +26,11 @@ import ( "go.uber.org/zap/zapcore" ) +const ( + stderrValue = "stderr" + none = "none" +) + const ( testDockerHostEnvKey = "DOCKER_HOST" testSetupSSHServerCommand = "ssh-server --stdio" @@ -109,7 +114,7 @@ func TestExecSetupSSHServer_SilentExecClosesStdin(t *testing.T) { } func TestExecSetupSSHServer_OutputDisarmsWatchdog(t *testing.T) { - for _, stream := range []string{"stdout", "stderr"} { + for _, stream := range []string{"stdout", stderrValue} { t.Run(stream, func(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) @@ -119,7 +124,7 @@ func TestExecSetupSSHServer_OutputDisarmsWatchdog(t *testing.T) { params *driver.CommandParams, ) error { output := params.Stdout - if stream == "stderr" { + if stream == stderrValue { output = params.Stderr } _, _ = io.WriteString(output, "SSH activity") @@ -322,7 +327,7 @@ func TestBuildResult_DefaultUserEnvProbeOverride(t *testing.T) { r := &runner{ workspaceConfig: &provider2.AgentWorkspaceInfo{ CLIOptions: provider2.CLIOptions{ - DefaultUserEnvProbe: "none", + DefaultUserEnvProbe: none, }, }, } @@ -338,8 +343,8 @@ func TestBuildResult_DefaultUserEnvProbeOverride(t *testing.T) { } result := r.buildResult(params) - if result.MergedConfig.UserEnvProbe != "none" { - t.Errorf("expected UserEnvProbe=%q, got %q", "none", result.MergedConfig.UserEnvProbe) + if result.MergedConfig.UserEnvProbe != none { + t.Errorf("expected UserEnvProbe=%q, got %q", none, result.MergedConfig.UserEnvProbe) } } @@ -468,13 +473,13 @@ func TestPostStartDeliverySkipsUnusedBinarySource(t *testing.T) { } func TestPostStartDeliveryRetainsArchitectureForBinarySource(t *testing.T) { - d := &architectureDriver{arch: "arm64"} + d := &architectureDriver{arch: arm64} r := newTestRunner(d) strategy := &capturePostStartDelivery{usesBinarySource: true} require.NoError(t, r.deliverPostStart(context.Background(), strategy)) assert.True(t, strategy.called) assert.Equal(t, 1, d.architectureCalls) - assert.Equal(t, "arm64", strategy.opts.Arch) + assert.Equal(t, arm64, strategy.opts.Arch) assert.NotNil(t, strategy.opts.BinarySource) strategy.called = false d.err = context.Canceled @@ -499,7 +504,7 @@ func TestPostStartDeliveryCustomAgentVersionPolicy(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Setenv(pkgconfig.EnvAgentBinary, tc.binaryPath) t.Setenv(pkgconfig.EnvAgentURL, tc.envURL) - r := newTestRunner(&architectureDriver{arch: "arm64"}) + r := newTestRunner(&architectureDriver{arch: arm64}) r.agentDownloadURL = tc.downloadURL strategy := &capturePostStartDelivery{usesBinarySource: true} require.NoError(t, r.deliverPostStart(context.Background(), strategy)) @@ -582,7 +587,7 @@ func TestBuildSSHTunnelArgvPreservesLiteralArguments(t *testing.T) { want = append(want, "--debug") } assert.Equal(t, want, r.buildSSHTunnelArgv()) - r.workspaceConfig.Workspace.IDE.Name = "none" + r.workspaceConfig.Workspace.IDE.Name = none want = []string{r.agentContainerPath(), "internal", "ssh-server", "--stdio"} if debug { want = append(want, "--debug") @@ -702,7 +707,7 @@ func TestExecSetupSSHServerArgvStartupSilenceClosesOwnedInput(t *testing.T) { } func TestExecSetupSSHServerArgvActivityRetainsCallerCancellation(t *testing.T) { - for _, output := range []string{"stdout", "stderr"} { + for _, output := range []string{"stdout", stderrValue} { t.Run(output, func(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond) defer cancel() @@ -714,7 +719,7 @@ func TestExecSetupSSHServerArgvActivityRetainsCallerCancellation(t *testing.T) { d.argv = func(ctx context.Context, _ string, _ []string, streams driver.Streams) error { defer close(done) writer := streams.Stdout - if output == "stderr" { + if output == stderrValue { writer = streams.Stderr } if _, err := io.WriteString(writer, "ready"); err != nil { diff --git a/pkg/devcontainer/single.go b/pkg/devcontainer/single.go index 5bfc93a0c8..68bec2816f 100644 --- a/pkg/devcontainer/single.go +++ b/pkg/devcontainer/single.go @@ -280,6 +280,16 @@ func (r *runner) resolveExistingContainer( return nil, err } + return r.completeExistingContainer(ctx, containerDetails, mergedConfig, p) +} + +func (r *runner) completeExistingContainer( + ctx context.Context, + containerDetails *config.ContainerDetails, + mergedConfig *config.MergedDevContainerConfig, + p *resolveParams, +) (*resolvedContainer, error) { + var err error // For non-managed containers with a workingDir, use it as the workspace folder. if p.parsedConfig.Config.ContainerID != "" && containerDetails.Config.WorkingDir != "" { p.substitutionContext.ContainerWorkspaceFolder = containerDetails.Config.WorkingDir @@ -418,28 +428,37 @@ func (r *runner) resolveNewContainer( return nil, fmt.Errorf("seed workspace volume: %w", seedErr) } - err = r.runContainer(ctx, p, mergedConfig, buildInfo, runOptions) + err = r.runContainer(ctx, p, containerLaunch{mergedConfig, buildInfo, runOptions}) if err != nil { return nil, fmt.Errorf("runner run container: %w", err) } + result := &resolvedContainer{mergedConfig: mergedConfig, hostWarnings: hostWarnings} + if err := r.finishNewContainer(ctx, result, buildInfo, imageEntrypoint); err != nil { + return nil, err + } + return result, nil +} + +func (r *runner) finishNewContainer( + ctx context.Context, + result *resolvedContainer, + buildInfo *config.BuildInfo, + imageEntrypoint bool, +) error { containerDetails, err := r.findRunningContainerOrFail(ctx, "creation") if err != nil { - return nil, err + return err } // New run options take precedence over metadata from an existing workspace volume. containerDetails.Dockerless = buildInfo.Dockerless != nil containerDetails.ImageEntrypoint = imageEntrypoint + result.details = containerDetails if w := r.lingerWarning(ctx); w != "" { - hostWarnings = append(hostWarnings, w) + result.hostWarnings = append(result.hostWarnings, w) } - - return &resolvedContainer{ - details: containerDetails, - mergedConfig: mergedConfig, - hostWarnings: hostWarnings, - }, nil + return nil } func (r *runner) attemptPreStartDelivery( @@ -449,29 +468,12 @@ func (r *runner) attemptPreStartDelivery( buildInfo *config.BuildInfo, ) *driver.RunOptions { strategy := r.newAgentDelivery() - switch strategy.Phase() { - case delivery.PhasePostStart: - log.Debug("agent delivery scheduled for post-start") - return nil - case delivery.PhasePreStart: - default: - log.Debugf( - "pre-start agent delivery skipped for unsupported phase %s", - strategy.Phase(), - ) + if !preStartPhaseSupported(strategy.Phase()) { return nil } - if _, local := strategy.(*delivery.LocalDockerDelivery); local { - for _, arg := range mergedConfig.RunArgs { - if arg == "--mount" || strings.HasPrefix(arg, "--mount=") || - arg == "--volume" || strings.HasPrefix(arg, "--volume=") || - arg == "--tmpfs" || strings.HasPrefix(arg, "--tmpfs=") || - arg == "--volumes-from" || strings.HasPrefix(arg, "--volumes-from=") || - arg == "-v" || strings.HasPrefix(arg, "-v=") || - (strings.HasPrefix(arg, "-v") && len(arg) > 2) { - return nil - } - } + if _, local := strategy.(*delivery.LocalDockerDelivery); local && + hasVolumeRunArg(mergedConfig.RunArgs) { + return nil } runOptions, err := r.buildRunOptionsForDelivery( @@ -495,6 +497,36 @@ func (r *runner) attemptPreStartDelivery( return runOptions } +func preStartPhaseSupported(phase delivery.DeliveryPhase) bool { + switch phase { + case delivery.PhasePreStart: + return true + case delivery.PhasePostStart: + log.Debug("agent delivery scheduled for post-start") + default: + log.Debugf("pre-start agent delivery skipped for unsupported phase %s", phase) + } + + return false +} + +var volumeRunArgFlags = []string{"--mount", "--volume", "--tmpfs", "--volumes-from", "-v"} + +func hasVolumeRunArg(args []string) bool { + for _, arg := range args { + if strings.HasPrefix(arg, "-v") && len(arg) > 2 { + return true + } + for _, flag := range volumeRunArgFlags { + if arg == flag || strings.HasPrefix(arg, flag+"=") { + return true + } + } + } + + return false +} + func (r *runner) lingerWarning(ctx context.Context) string { helperProvider, ok := r.driver.(driver.DockerHelperProvider) if !ok { @@ -698,6 +730,10 @@ func (r *runner) deleteForRecreate(ctx context.Context) error { r.overlayExisting = nil return nil } + return r.recreateByMode(ctx) +} + +func (r *runner) recreateByMode(ctx context.Context) error { switch driver.DriverRecreateMode(r.driver) { case driver.RecreateDelete: if err := r.Delete(ctx, DeleteOptions{}); err != nil { @@ -738,11 +774,13 @@ func (r *runner) injectDaemonEntrypoint( } data, err := agent.GetEncodedWorkspaceDaemonConfig( - platform, - r.workspaceConfig.Workspace, - p.substitutionContext, - mergedConfig, - lifecycle, + agent.WorkspaceDaemonInput{ + PlatformOptions: platform, + Workspace: r.workspaceConfig.Workspace, + SubstitutionContext: p.substitutionContext, + MergedConfig: mergedConfig, + Lifecycle: lifecycle, + }, ) if err != nil { return fmt.Errorf("build daemon config: %w", err) @@ -920,36 +958,21 @@ func (r *runner) seedWorkspaceVolume(ctx context.Context, p *resolveParams) erro }) } +type containerLaunch struct { + mergedConfig *config.MergedDevContainerConfig + buildInfo *config.BuildInfo + runOptions *driver.RunOptions +} + func (r *runner) runContainer( ctx context.Context, p *resolveParams, - mergedConfig *config.MergedDevContainerConfig, - buildInfo *config.BuildInfo, - runOptions *driver.RunOptions, + launch containerLaunch, ) error { - var err error - - if runOptions == nil && buildInfo.Dockerless != nil { - runOptions, err = r.getDockerlessRunOptions( - mergedConfig, - p.substitutionContext, - buildInfo, - len(p.options.SecretsMount) > 0, - ) - if err != nil { - return fmt.Errorf("build dockerless run options: %w", err) - } - } else if runOptions == nil { - // build run options - runOptions, err = r.getRunOptions( - mergedConfig, - p.substitutionContext, - buildInfo, - len(p.options.SecretsMount) > 0, - ) - if err != nil { - return fmt.Errorf("build run options: %w", err) - } + mergedConfig := launch.mergedConfig + runOptions, err := r.resolveLaunchRunOptions(p, launch) + if err != nil { + return err } runOptions.Labels = append( @@ -983,6 +1006,34 @@ func (r *runner) runContainer( return fmt.Errorf("driver does not support running a devcontainer") } +func (r *runner) resolveLaunchRunOptions( + p *resolveParams, + launch containerLaunch, +) (*driver.RunOptions, error) { + if launch.runOptions != nil { + return launch.runOptions, nil + } + hasSecrets := len(p.options.SecretsMount) > 0 + if launch.buildInfo.Dockerless != nil { + runOptions, err := r.getDockerlessRunOptions( + launch.mergedConfig, p.substitutionContext, launch.buildInfo, hasSecrets, + ) + if err != nil { + return nil, fmt.Errorf("build dockerless run options: %w", err) + } + + return runOptions, nil + } + runOptions, err := r.getRunOptions( + launch.mergedConfig, p.substitutionContext, launch.buildInfo, hasSecrets, + ) + if err != nil { + return nil, fmt.Errorf("build run options: %w", err) + } + + return runOptions, nil +} + // withResolvedUser returns a copy of parsedConfig carrying the effective user // identity from the merged config. remoteUser/containerUser/updateRemoteUserUID // often come from image metadata rather than the raw devcontainer.json, so the @@ -1066,14 +1117,7 @@ func (r *runner) getDockerlessRunOptions( image = r.workspaceConfig.Agent.Dockerless.Image } - // we need to add an extra mount here, because otherwise the build config might get lost - mounts := mergedConfig.Mounts - mounts = append(mounts, &config.Mount{ - Type: "volume", - Source: "dockerless-" + r.id, - Target: "/workspaces/.dockerless", - }) - mounts, err = r.withSecretsMount(mounts, secretsMountRequired) + mounts, err := r.dockerlessMounts(mergedConfig, secretsMountRequired) if err != nil { return nil, err } @@ -1111,6 +1155,31 @@ func (r *runner) getDockerlessRunOptions( }, nil } +func (r *runner) runLabels(marshalled []byte, imageUser string) []string { + labels := []string{ + metadata.ImageMetadataLabel + "=" + string(marshalled), + config.UserLabel + "=" + imageUser, + } + if r.recovering { + labels = append(labels, pkgconfig.DockerRecoveryLabel+"="+pkgconfig.LabelValueTrue) + } + return labels +} + +func (r *runner) dockerlessMounts( + mergedConfig *config.MergedDevContainerConfig, + secretsMountRequired bool, +) ([]*config.Mount, error) { + // we need to add an extra mount here, because otherwise the build config might get lost + mounts := mergedConfig.Mounts + mounts = append(mounts, &config.Mount{ + Type: "volume", + Source: "dockerless-" + r.id, + Target: "/workspaces/.dockerless", + }) + return r.withSecretsMount(mounts, secretsMountRequired) +} + func (r *runner) getRunOptions( mergedConfig *config.MergedDevContainerConfig, substitutionContext *config.SubstitutionContext, @@ -1134,13 +1203,7 @@ func (r *runner) getRunOptions( imageUser = buildInfo.ImageDetails.Config.User } - labels := []string{ - metadata.ImageMetadataLabel + "=" + string(marshalled), - config.UserLabel + "=" + imageUser, - } - if r.recovering { - labels = append(labels, pkgconfig.DockerRecoveryLabel+"="+pkgconfig.LabelValueTrue) - } + labels := r.runLabels(marshalled, imageUser) user := imageUser if mergedConfig.ContainerUser != "" { @@ -1367,15 +1430,20 @@ func (r *runner) applyDriverRecreateRequirement( containerUser = details.Config.Labels[config.UserLabel] } remoteUser := effectiveRemoteUser(merged, containerUser) - if preflight, ok := r.driver.(driver.ReusePreflightDriver); ok && - preflight.SupportsReusePreflight() { - if err := preflight.ReusePreflight(ctx, r.id, remoteUser); err != nil { - return err - } + if err := r.reusePreflight(ctx, remoteUser); err != nil { + return err } return r.scheduleDriverRecreation(details, remoteUser, p) } +func (r *runner) reusePreflight(ctx context.Context, remoteUser string) error { + preflight, ok := r.driver.(driver.ReusePreflightDriver) + if !ok || !preflight.SupportsReusePreflight() { + return nil + } + return preflight.ReusePreflight(ctx, r.id, remoteUser) +} + func (r *runner) scheduleDriverRecreation( details *config.ContainerDetails, remoteUser string, diff --git a/pkg/devcontainer/single_test.go b/pkg/devcontainer/single_test.go index b80611bf0c..7633164558 100644 --- a/pkg/devcontainer/single_test.go +++ b/pkg/devcontainer/single_test.go @@ -14,6 +14,11 @@ import ( "github.com/stretchr/testify/require" ) +const ( + vscode = "vscode" + nodeValue = "node" +) + const mountTypeVolume = "volume" type provisioningPreflightMockDriver struct { @@ -238,16 +243,16 @@ func TestWithResolvedUser(t *testing.T) { uid := true merged := &config.MergedDevContainerConfig{} - merged.RemoteUser = "vscode" - merged.ContainerUser = "node" + merged.RemoteUser = vscode + merged.ContainerUser = nodeValue merged.UpdateRemoteUserUID = &uid got := withResolvedUser(parsed, merged) - if got.RemoteUser != "vscode" { + if got.RemoteUser != vscode { t.Errorf("RemoteUser = %q, want vscode", got.RemoteUser) } - if got.ContainerUser != "node" { + if got.ContainerUser != nodeValue { t.Errorf("ContainerUser = %q, want node", got.ContainerUser) } if got.UpdateRemoteUserUID == nil || !*got.UpdateRemoteUserUID { @@ -511,7 +516,7 @@ func TestAutomaticMigrationPreflightFailurePreservesExisting(t *testing.T) { func TestEffectiveRemoteUser(t *testing.T) { for _, tt := range []struct{ remote, container, want string }{ - {"vscode", containerRootUser, "vscode"}, {"", "node", "node"}, {"", "", containerRootUser}, + {vscode, containerRootUser, vscode}, {"", nodeValue, nodeValue}, {"", "", containerRootUser}, } { cfg := &config.MergedDevContainerConfig{} cfg.RemoteUser = tt.remote @@ -523,30 +528,30 @@ func TestRunOptionsSeparateDeveloperIdentity(t *testing.T) { r := newTestRunner(&mockDriver{}) cfg := &config.MergedDevContainerConfig{} cfg.ContainerUser = containerRootUser - cfg.RemoteUser = "vscode" + cfg.RemoteUser = vscode info := &config.BuildInfo{ ImageName: "final-image", ImageMetadata: &config.ImageMetadataConfig{}, ImageDetails: &config.ImageDetails{}, } - info.ImageDetails.Config.User = "node" + info.ImageDetails.Config.User = nodeValue options, err := r.getRunOptions(cfg, &config.SubstitutionContext{}, info, false) require.NoError(t, err) require.Equal(t, containerRootUser, options.User) - require.Equal(t, "vscode", options.RemoteUser) - info.Dockerless = &config.BuildInfoDockerless{User: "node"} + require.Equal(t, vscode, options.RemoteUser) + info.Dockerless = &config.BuildInfoDockerless{User: nodeValue} options, err = r.getDockerlessRunOptions(cfg, &config.SubstitutionContext{}, info, false) require.NoError(t, err) require.Equal(t, containerRootUser, options.User) - require.Equal(t, "vscode", options.RemoteUser) + require.Equal(t, vscode, options.RemoteUser) require.True(t, options.Dockerless) } func TestMigrationUsesCurrentDeveloperIdentity(t *testing.T) { for _, tt := range []struct{ remote, container, image, want string }{ - {"new-user", containerRootUser, "node", "new-user"}, - {"", "node", "vscode", "node"}, - {"", "", "node", "node"}, + {"new-user", containerRootUser, nodeValue, "new-user"}, + {"", nodeValue, vscode, nodeValue}, + {"", "", nodeValue, nodeValue}, {"", "", "", containerRootUser}, } { d := &migrationMockDriver{ diff --git a/pkg/devcontainer/sshtunnel/sshtunnel.go b/pkg/devcontainer/sshtunnel/sshtunnel.go index 9ea49e8f3d..fff92500ea 100644 --- a/pkg/devcontainer/sshtunnel/sshtunnel.go +++ b/pkg/devcontainer/sshtunnel/sshtunnel.go @@ -77,8 +77,7 @@ func isExpectedError(err error) bool { if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { return true } - var exitErr *exec.ExitError - if errors.As(err, &exitErr) { + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { return exitErr != nil && !exitErr.Exited() } return false diff --git a/pkg/docker/boot_state_test.go b/pkg/docker/boot_state_test.go index cc965e81f1..fc27de3d28 100644 --- a/pkg/docker/boot_state_test.go +++ b/pkg/docker/boot_state_test.go @@ -7,70 +7,70 @@ import ( "github.com/stretchr/testify/assert" ) -func TestFailedBootSentinel(t *testing.T) { - tests := []struct { - name string - status config.ContainerStatus - graceElapsed bool - wantSentinel error - wantNil bool - }{ - { - "dead is terminal regardless of grace", - config.ContainerStatusDead, - false, - ErrContainerTerminal, - false, - }, - { - "dead is terminal even after grace", - config.ContainerStatusDead, - true, - ErrContainerTerminal, - false, - }, - { - "removing is terminal regardless of grace", - config.ContainerStatusRemoving, - false, - ErrContainerTerminal, - false, - }, - { - "removing is terminal even after grace", - config.ContainerStatusRemoving, - true, - ErrContainerTerminal, - false, - }, - {"exited before grace is still booting", config.ContainerStatusExited, false, nil, true}, - { - "exited after grace failed", - config.ContainerStatusExited, - true, - ErrContainerExited, - false, - }, - {"created before grace is still booting", config.ContainerStatusCreated, false, nil, true}, - { - "created after grace failed", - config.ContainerStatusCreated, - true, - ErrContainerExited, - false, - }, - {"paused is not a terminal boot state", config.ContainerStatusPaused, true, nil, true}, - { - "restarting is not a terminal boot state", - config.ContainerStatusRestarting, - false, - nil, - true, - }, - {"empty status is not a terminal boot state", "", true, nil, true}, - } +var failedBootSentinelCases = []struct { + name string + status config.ContainerStatus + graceElapsed bool + wantSentinel error + wantNil bool +}{ + { + "dead is terminal regardless of grace", + config.ContainerStatusDead, + false, + ErrContainerTerminal, + false, + }, + { + "dead is terminal even after grace", + config.ContainerStatusDead, + true, + ErrContainerTerminal, + false, + }, + { + "removing is terminal regardless of grace", + config.ContainerStatusRemoving, + false, + ErrContainerTerminal, + false, + }, + { + "removing is terminal even after grace", + config.ContainerStatusRemoving, + true, + ErrContainerTerminal, + false, + }, + {"exited before grace is still booting", config.ContainerStatusExited, false, nil, true}, + { + "exited after grace failed", + config.ContainerStatusExited, + true, + ErrContainerExited, + false, + }, + {"created before grace is still booting", config.ContainerStatusCreated, false, nil, true}, + { + "created after grace failed", + config.ContainerStatusCreated, + true, + ErrContainerExited, + false, + }, + {"paused is not a terminal boot state", config.ContainerStatusPaused, true, nil, true}, + { + "restarting is not a terminal boot state", + config.ContainerStatusRestarting, + false, + nil, + true, + }, + {"empty status is not a terminal boot state", "", true, nil, true}, +} - for _, tt := range tests { +func TestFailedBootSentinel(t *testing.T) { + for _, tt := range failedBootSentinelCases { t.Run(tt.name, func(t *testing.T) { got := failedBootSentinel(tt.status, tt.graceElapsed) if tt.wantNil { diff --git a/pkg/dockercredentials/dockercredentials.go b/pkg/dockercredentials/dockercredentials.go index 7cbb2e90a5..4a52ac84ac 100644 --- a/pkg/dockercredentials/dockercredentials.go +++ b/pkg/dockercredentials/dockercredentials.go @@ -76,16 +76,28 @@ func ConfigureCredentialsContainer(userName string, port int) error { configDir = filepath.Join(userHome, ".docker") } - return configureCredentials(userName, "#!/bin/sh", "/usr/local/bin", configDir, port) + return configureCredentials(userName, port, credentialLayout{ + shebang: "#!/bin/sh", + targetDir: "/usr/local/bin", + configDir: configDir, + }) } const AzureContainerRegistryUsername = "00000000-0000-0000-0000-000000000000" +// credentialLayout is where the credential helper is installed and where it reads config. +type credentialLayout struct { + shebang string + targetDir string + configDir string +} + func configureCredentials( - userName, shebang string, - targetDir, configDir string, + userName string, port int, + layout credentialLayout, ) error { + shebang, targetDir, configDir := layout.shebang, layout.targetDir, layout.configDir binaryPath, err := os.Executable() if err != nil { return err @@ -167,13 +179,11 @@ func ConfigureCredentialsDockerless(targetFolder string, port int) (string, erro return "", err } - err := configureCredentials( - "", - "#!/.dockerless/bin/sh", - dockerConfigDir, - dockerConfigDir, - port, - ) + err := configureCredentials("", port, credentialLayout{ + shebang: "#!/.dockerless/bin/sh", + targetDir: dockerConfigDir, + configDir: dockerConfigDir, + }) if err != nil { _ = os.RemoveAll(dockerConfigDir) return "", err @@ -202,7 +212,11 @@ func ConfigureCredentialsMachine(targetFolder string, port int) (string, error) return "", err } - err := configureCredentials("", "#!/bin/sh", dockerConfigDir, dockerConfigDir, port) + err := configureCredentials("", port, credentialLayout{ + shebang: "#!/bin/sh", + targetDir: dockerConfigDir, + configDir: dockerConfigDir, + }) if err != nil { _ = os.RemoveAll(dockerConfigDir) return "", err @@ -236,7 +250,20 @@ func preserveDockerConfig(dockerConfigDir string) error { return err } - // Copy config.json if it exists + if err := preserveConfigFile(srcDir, dockerConfigDir); err != nil { + return err + } + if err := preserveCliPluginsExtraDir( + srcDir, + filepath.Join(dockerConfigDir, config.ConfigFileName), + ); err != nil { + return err + } + + return preserveContexts(srcDir, dockerConfigDir) +} + +func preserveConfigFile(srcDir, dockerConfigDir string) error { srcConfigFile := filepath.Join(srcDir, config.ConfigFileName) if _, err := os.Stat(srcConfigFile); err == nil { destConfigFile := filepath.Join(dockerConfigDir, config.ConfigFileName) @@ -246,14 +273,10 @@ func preserveDockerConfig(dockerConfigDir string) error { } else if !os.IsNotExist(err) { return fmt.Errorf("inspect Docker config: %w", err) } - if err := preserveCliPluginsExtraDir( - srcDir, - filepath.Join(dockerConfigDir, config.ConfigFileName), - ); err != nil { - return err - } + return nil +} - // Copy contexts if they exist +func preserveContexts(srcDir, dockerConfigDir string) error { srcContextsDir := filepath.Join(srcDir, "contexts") if _, err := os.Stat(srcContextsDir); err == nil { destContextsDir := filepath.Join(dockerConfigDir, "contexts") @@ -263,7 +286,6 @@ func preserveDockerConfig(dockerConfigDir string) error { } else if !os.IsNotExist(err) { return fmt.Errorf("inspect Docker contexts: %w", err) } - return nil } diff --git a/pkg/dockercredentials/helper.go b/pkg/dockercredentials/helper.go index b2b62f8612..ed02fb32cc 100644 --- a/pkg/dockercredentials/helper.go +++ b/pkg/dockercredentials/helper.go @@ -137,6 +137,7 @@ func (h *Helper) getFromWorkspaceServer(serverURL string) (string, string, error client := &http.Client{Timeout: credentialsTimeout} endpoint := fmt.Sprintf("http://localhost:%s/docker-credentials", workspacePort) + //nolint:gosec // endpoint is always loopback; only the port comes from the agent environment resp, err := client.Post(endpoint, "application/json", bytes.NewReader(requestBody)) if err != nil { return "", "", err diff --git a/pkg/dockercredentials/helper_test.go b/pkg/dockercredentials/helper_test.go index 527d2ddcd2..f14b2f1e5e 100644 --- a/pkg/dockercredentials/helper_test.go +++ b/pkg/dockercredentials/helper_test.go @@ -12,6 +12,8 @@ import ( "github.com/stretchr/testify/suite" ) +const testRegistry = "docker.io" + type HelperTestSuite struct { suite.Suite } @@ -28,18 +30,20 @@ func (s *HelperTestSuite) TestGet_Success() { request := &Request{} err := json.NewDecoder(r.Body).Decode(request) s.NoError(err) - s.Equal("docker.io", request.ServerURL) - - _ = json.NewEncoder(w).Encode(&Credentials{ - ServerURL: "docker.io", - Username: "testuser", - Secret: "testpass", - }) + s.Equal(testRegistry, request.ServerURL) + + //nolint:gosec // test fixture credential, not a real secret + _ = json.NewEncoder(w). + Encode(&Credentials{ //nolint:gosec // test fixture credential, not a real secret + ServerURL: testRegistry, + Username: "testuser", + Secret: "testpass", //nolint:gosec // test fixture + }) })) defer server.Close() helper := NewHelper(parsePort(server.URL)) - username, secret, err := helper.Get("docker.io") + username, secret, err := helper.Get(testRegistry) s.NoError(err) s.Equal("testuser", username) @@ -53,7 +57,7 @@ func (s *HelperTestSuite) TestGet_EmptyCredentials() { defer server.Close() helper := NewHelper(parsePort(server.URL)) - username, secret, err := helper.Get("docker.io") + username, secret, err := helper.Get(testRegistry) s.NoError(err) s.Empty(username) @@ -67,7 +71,7 @@ func (s *HelperTestSuite) TestGet_ServerError() { defer server.Close() helper := NewHelper(parsePort(server.URL)) - username, secret, err := helper.Get("docker.io") + username, secret, err := helper.Get(testRegistry) s.NoError(err) s.Empty(username) @@ -90,13 +94,15 @@ func (s *HelperTestSuite) TestGet_WorkspaceServerFallback() { request := &Request{} err := json.NewDecoder(r.Body).Decode(request) s.NoError(err) - s.Equal("docker.io", request.ServerURL) - - _ = json.NewEncoder(w).Encode(&Credentials{ - ServerURL: "docker.io", - Username: "workspaceuser", - Secret: "workspacepass", - }) + s.Equal(testRegistry, request.ServerURL) + + //nolint:gosec // test fixture credential, not a real secret + _ = json.NewEncoder(w). + Encode(&Credentials{ //nolint:gosec // test fixture credential, not a real secret + ServerURL: testRegistry, + Username: "workspaceuser", + Secret: "workspacepass", //nolint:gosec // test fixture + }) }), ) defer workspaceServer.Close() @@ -105,7 +111,7 @@ func (s *HelperTestSuite) TestGet_WorkspaceServerFallback() { defer func() { _ = os.Unsetenv("DEVSY_WORKSPACE_CREDENTIALS_PORT") }() helper := NewHelper(parsePort(primaryServer.URL)) - username, secret, err := helper.Get("docker.io") + username, secret, err := helper.Get(testRegistry) s.NoError(err) s.Equal("workspaceuser", username) @@ -123,8 +129,8 @@ func (s *HelperTestSuite) TestList_Success() { s.Equal("", request.ServerURL) _ = json.NewEncoder(w).Encode(&ListResponse{Registries: map[string]string{ - "docker.io": "testuser", - "gcr.io": "gcpuser", + testRegistry: "testuser", + "gcr.io": "gcpuser", }}) })) defer server.Close() @@ -134,7 +140,7 @@ func (s *HelperTestSuite) TestList_Success() { s.NoError(err) s.Len(list, 2) - s.Equal("testuser", list["docker.io"]) + s.Equal("testuser", list[testRegistry]) } func (s *HelperTestSuite) TestList_Empty() { @@ -158,7 +164,7 @@ func (s *HelperTestSuite) TestAdd_NotSupported() { func (s *HelperTestSuite) TestDelete_NotSupported() { helper := NewHelper(0) - err := helper.Delete("docker.io") + err := helper.Delete(testRegistry) s.Error(err) } @@ -167,11 +173,11 @@ func (s *HelperTestSuite) TestSanitizeServerURL() { input string expected string }{ - {"https://docker.io", "docker.io"}, - {"http://docker.io", "docker.io"}, - {"docker.io/", "docker.io"}, - {"https://docker.io/", "docker.io"}, - {"docker.io", "docker.io"}, + {"https://docker.io", testRegistry}, + {"http://docker.io", testRegistry}, + {"docker.io/", testRegistry}, + {"https://docker.io/", testRegistry}, + {testRegistry, testRegistry}, } for _, tt := range tests { diff --git a/pkg/dockerfile/parse_test.go b/pkg/dockerfile/parse_test.go index 7a20b8d7d5..dd3b88d024 100644 --- a/pkg/dockerfile/parse_test.go +++ b/pkg/dockerfile/parse_test.go @@ -8,6 +8,14 @@ import ( "github.com/stretchr/testify/suite" ) +const ( + baseVersion = "BASE_VERSION" +) + +const ( + testBaseImageArg = "BASE_IMAGE" +) + //go:embed test_Dockerfile var testDockerFileContents string @@ -69,7 +77,10 @@ FROM ${BASE_IMAGE}:${BASE_VERSION}` baseImage := d.FindBaseImage(map[string]string{}, "") s.Equal("ubuntu:20.04", baseImage) - baseImage = d.FindBaseImage(map[string]string{"BASE_IMAGE": "debian", "BASE_VERSION": "11"}, "") + baseImage = d.FindBaseImage( + map[string]string{testBaseImageArg: "debian", baseVersion: "11"}, + "", + ) s.Equal("debian:11", baseImage) } @@ -81,7 +92,7 @@ FROM ${BASE_IMAGE}:${BASE_VERSION}` s.NoError(err) baseImage := d.FindBaseImage( - map[string]string{"BASE_IMAGE": "alpine", "BASE_VERSION": "3.18"}, + map[string]string{testBaseImageArg: "alpine", baseVersion: "3.18"}, "", ) s.Equal("alpine:3.18", baseImage) @@ -119,7 +130,10 @@ COPY --from=builder /app /app` baseImage = d.FindBaseImage(map[string]string{}, "builder") s.Equal("golang:1.21", baseImage) - baseImage = d.FindBaseImage(map[string]string{"BASE_IMAGE": "node", "BASE_VERSION": "18"}, "") + baseImage = d.FindBaseImage( + map[string]string{testBaseImageArg: "node", baseVersion: "18"}, + "", + ) s.Equal("node:18", baseImage) } diff --git a/pkg/dotfiles/dotfiles_test.go b/pkg/dotfiles/dotfiles_test.go index c619207e8c..9f0340be3c 100644 --- a/pkg/dotfiles/dotfiles_test.go +++ b/pkg/dotfiles/dotfiles_test.go @@ -6,6 +6,13 @@ import ( "github.com/stretchr/testify/assert" ) +const ( + fooBar = "FOO=bar" + foo = "FOO" + httpsGithubComUserDotfiles = "https://github.com/user/dotfiles" + repository = "--repository" +) + func TestExtractKeysFromEnvKeyValuePairs(t *testing.T) { tests := []struct { name string @@ -19,28 +26,28 @@ func TestExtractKeysFromEnvKeyValuePairs(t *testing.T) { }, { name: "single key-value pair", - input: []string{"FOO=bar"}, - expected: []string{"FOO"}, + input: []string{fooBar}, + expected: []string{foo}, }, { name: "multiple key-value pairs", - input: []string{"FOO=bar", "BAZ=qux"}, - expected: []string{"FOO", "BAZ"}, + input: []string{fooBar, "BAZ=qux"}, + expected: []string{foo, "BAZ"}, }, { name: "value contains equals sign", input: []string{"FOO=bar=baz"}, - expected: []string{"FOO"}, + expected: []string{foo}, }, { name: "entry without equals sign is skipped", - input: []string{"NOEQ", "FOO=bar"}, - expected: []string{"FOO"}, + input: []string{"NOEQ", fooBar}, + expected: []string{foo}, }, { name: "empty value", input: []string{"FOO="}, - expected: []string{"FOO"}, + expected: []string{foo}, }, } @@ -76,31 +83,31 @@ func TestBuildDotCmdAgentArguments(t *testing.T) { }{ { name: "basic repo only", - dotfilesRepo: "https://github.com/user/dotfiles", + dotfilesRepo: httpsGithubComUserDotfiles, expected: []string{ cmdInternal, cmdAgent, cmdWorkspace, cmdInstallDotfs, - "--repository", "https://github.com/user/dotfiles", + repository, httpsGithubComUserDotfiles, }, }, { name: "with script", - dotfilesRepo: "https://github.com/user/dotfiles", + dotfilesRepo: httpsGithubComUserDotfiles, dotfilesScript: "install.sh", expected: []string{ cmdInternal, cmdAgent, cmdWorkspace, cmdInstallDotfs, - "--repository", "https://github.com/user/dotfiles", + repository, httpsGithubComUserDotfiles, "--install-script", "install.sh", }, }, { name: "all options enabled", - dotfilesRepo: "https://github.com/user/dotfiles", + dotfilesRepo: httpsGithubComUserDotfiles, dotfilesScript: "setup.sh", strictHostKey: true, debug: true, expected: []string{ cmdInternal, cmdAgent, cmdWorkspace, cmdInstallDotfs, - "--repository", "https://github.com/user/dotfiles", + repository, httpsGithubComUserDotfiles, "--strict-host-key-checking", "--debug", "--install-script", "setup.sh", }, diff --git a/pkg/driver/apple/build.go b/pkg/driver/apple/build.go index a967ffe11a..b9d7163fb7 100644 --- a/pkg/driver/apple/build.go +++ b/pkg/driver/apple/build.go @@ -3,16 +3,20 @@ package apple import ( "bytes" "context" + "errors" "fmt" "io" + "os/exec" "sort" "strings" + "time" "github.com/devsy-org/devsy/pkg/apple" "github.com/devsy-org/devsy/pkg/devcontainer/build" "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/driver" "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/workspacelogs" ) func (d *appleDriver) BuildDevContainer( @@ -84,6 +88,7 @@ func (d *appleDriver) resolveExistingImage( } log.Debugf("found existing local image %s", imageName) + recordImageReused(ctx) return &config.BuildInfo{ ImageDetails: imageDetails, ImageMetadata: req.ExtendedBuildInfo.MetadataConfig, @@ -107,8 +112,14 @@ func (d *appleDriver) executeBuild( defer func() { _ = writer.Close() }() stderrBuf := &bytes.Buffer{} - streams := apple.Streams{Stdout: writer, Stderr: io.MultiWriter(writer, stderrBuf)} - if err := d.Apple.Run(ctx, args, streams); err != nil { + run := startBuildRun(ctx, platform) + streams := apple.Streams{ + Stdout: run.tee(writer, workspacelogs.StreamStdout), + Stderr: io.MultiWriter(run.tee(writer, workspacelogs.StreamStderr), stderrBuf), + } + err := d.Apple.Run(ctx, args, streams) + run.finish(ctx, err) + if err != nil { if stderrBuf.Len() > 0 { return fmt.Errorf( "failed to build image: %w: %s", @@ -162,3 +173,84 @@ func sortedKeys(m map[string]string) []string { sort.Strings(keys) return keys } + +const buildPhase = "build" + +// buildRun records one image build as an invocation in the run found in the +// context. A nil buildRun does nothing. +type buildRun struct { + rec *workspacelogs.Recorder + id string + started time.Time + writers []io.Closer +} + +func buildKey(platform string) string { + if platform == "" { + return "apple" + } + + return "apple " + platform +} + +func startBuildRun(ctx context.Context, platform string) *buildRun { + rec := workspacelogs.RecorderFrom(ctx) + if rec == nil { + return nil + } + b := &buildRun{rec: rec, id: workspacelogs.NewID(), started: time.Now()} + rec.Invocation(workspacelogs.Invocation{ + ID: b.id, Phase: buildPhase, Key: buildKey(platform), + State: workspacelogs.InvocationRunning, + }) + + return b +} + +func recordImageReused(ctx context.Context) { + rec := workspacelogs.RecorderFrom(ctx) + if rec == nil { + return + } + rec.Invocation(workspacelogs.Invocation{ + ID: workspacelogs.NewID(), Phase: buildPhase, Key: "apple", + State: workspacelogs.InvocationSkipped, Reason: "image_reused", + }) +} + +// tee records redacted output before forwarding to dst, so the text is kept +// regardless of the logger level. +func (b *buildRun) tee(dst io.Writer, stream workspacelogs.Stream) io.Writer { + if b == nil { + return dst + } + w := b.rec.Writer(b.id, stream) + b.writers = append(b.writers, w) + + return io.MultiWriter(w, dst) +} + +// finish flushes every writer, then records the terminal state for err. +func (b *buildRun) finish(ctx context.Context, err error) { + if b == nil { + return + } + for _, w := range b.writers { + _ = w.Close() + } + inv := workspacelogs.Invocation{ + ID: b.id, Phase: buildPhase, State: workspacelogs.InvocationSucceeded, + Duration: time.Since(b.started), + } + if err != nil { + inv.State = workspacelogs.InvocationFailed + if ctx.Err() != nil || errors.Is(err, context.Canceled) { + inv.State = workspacelogs.InvocationCanceled + } + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { + code := exitErr.ExitCode() + inv.ExitCode = &code + } + } + b.rec.Invocation(inv) +} diff --git a/pkg/driver/apple/build_record_test.go b/pkg/driver/apple/build_record_test.go new file mode 100644 index 0000000000..d3ddabdc20 --- /dev/null +++ b/pkg/driver/apple/build_record_test.go @@ -0,0 +1,165 @@ +package apple + +import ( + "context" + "errors" + "strings" + "testing" + + "github.com/devsy-org/devsy/pkg/apple" + "github.com/devsy-org/devsy/pkg/devcontainer/build" + "github.com/devsy-org/devsy/pkg/devcontainer/feature" + "github.com/devsy-org/devsy/pkg/driver" + "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/zap/zapcore" +) + +type streamingClient struct { + mockClient + stdout, stderr string + err error +} + +func (c *streamingClient) Run(_ context.Context, _ []string, s apple.Streams) error { + _, _ = s.Stdout.Write([]byte(c.stdout)) + _, _ = s.Stderr.Write([]byte(c.stderr)) + + return c.err +} + +func recordedContext(t *testing.T) (context.Context, *workspacelogs.Store, string) { + t.Helper() + store, err := workspacelogs.Open(workspacelogs.Options{Root: t.TempDir()}) + require.NoError(t, err) + m, err := store.CreateRun(workspacelogs.RunSpec{ + Context: "default", WorkspaceUID: "uid", Command: "up", + }) + require.NoError(t, err) + rec, err := workspacelogs.NewRecorder(workspacelogs.RecorderOptions{ + Backend: store, RunID: m.RunID, Location: workspacelogs.LocationProviderHost, + }) + require.NoError(t, err) + + return workspacelogs.WithRecorder(t.Context(), rec), store, m.RunID +} + +func readRecords(t *testing.T, store *workspacelogs.Store, runID string) []workspacelogs.Record { + t.Helper() + var all []workspacelogs.Record + req := workspacelogs.ReadRequest{RunID: runID} + for { + resp, err := store.Read(req) + require.NoError(t, err) + all = append(all, resp.Records...) + if !resp.HasMore { + return all + } + req.After = resp.NextCursor + } +} + +func outputText(recs []workspacelogs.Record, stream workspacelogs.Stream) string { + var b strings.Builder + for i := range recs { + if recs[i].Kind == workspacelogs.KindOutput && recs[i].Stream == stream { + b.WriteString(recs[i].Text) + } + } + + return b.String() +} + +func invocationRecords(recs []workspacelogs.Record) []workspacelogs.Record { + var out []workspacelogs.Record + for i := range recs { + if recs[i].Kind == workspacelogs.KindInvocation { + out = append(out, recs[i]) + } + } + + return out +} + +func TestExecuteBuildRetainsOutputAtWarnLevel(t *testing.T) { + logs := log.InitTestObserved(t, zapcore.WarnLevel) + ctx, store, runID := recordedContext(t) + d := &appleDriver{Apple: &streamingClient{stdout: "built layer\n", stderr: "warning text"}} + + err := d.executeBuild(ctx, &build.BuildOptions{Dockerfile: "Dockerfile"}, "linux/arm64") + + require.NoError(t, err) + assert.Empty(t, logs.All()) + recs := readRecords(t, store, runID) + assert.Equal(t, "built layer\n", outputText(recs, workspacelogs.StreamStdout)) + assert.Equal(t, "warning text", outputText(recs, workspacelogs.StreamStderr)) + inv := invocationRecords(recs) + require.Len(t, inv, 2) + assert.Equal(t, "apple linux/arm64", inv[0].HookKey) + assert.Equal(t, "build", inv[0].Phase) + assert.Equal(t, workspacelogs.InvocationSucceeded, inv[1].State) +} + +func TestExecuteBuildFailureKeepsErrorTail(t *testing.T) { + log.InitTestObserved(t, zapcore.WarnLevel) + ctx, store, runID := recordedContext(t) + cause := errors.New("exit status 2") + d := &appleDriver{Apple: &streamingClient{stderr: "no space left\n", err: cause}} + + err := d.executeBuild(ctx, &build.BuildOptions{}, "") + + require.ErrorIs(t, err, cause) + assert.EqualError(t, err, "failed to build image: exit status 2: no space left") + recs := readRecords(t, store, runID) + assert.Equal(t, "no space left\n", outputText(recs, workspacelogs.StreamStderr)) + inv := invocationRecords(recs) + require.Len(t, inv, 2) + assert.Equal(t, "apple", inv[0].HookKey) + assert.Equal(t, workspacelogs.InvocationFailed, inv[1].State) +} + +func TestExecuteBuildCanceledIsRecorded(t *testing.T) { + log.InitTestObserved(t, zapcore.WarnLevel) + ctx, store, runID := recordedContext(t) + ctx, cancel := context.WithCancel(ctx) + cancel() + d := &appleDriver{Apple: &streamingClient{err: context.Canceled}} + + err := d.executeBuild(ctx, &build.BuildOptions{}, "") + + require.ErrorIs(t, err, context.Canceled) + inv := invocationRecords(readRecords(t, store, runID)) + require.Len(t, inv, 2) + assert.Equal(t, workspacelogs.InvocationCanceled, inv[1].State) +} + +func TestExecuteBuildWithoutRecorderLogsOnly(t *testing.T) { + logs := log.InitTestObserved(t, zapcore.InfoLevel) + d := &appleDriver{Apple: &streamingClient{stdout: "plain\n"}} + + require.NoError(t, d.executeBuild(t.Context(), &build.BuildOptions{}, "")) + + var messages []string + for _, e := range logs.All() { + messages = append(messages, e.Message) + } + assert.Equal(t, []string{"plain"}, messages) +} + +func TestResolveExistingImageRecordsReuse(t *testing.T) { + ctx, store, runID := recordedContext(t) + d := &appleDriver{Apple: &mockClient{}} + + info, found := d.resolveExistingImage(ctx, "img", driver.BuildRequest{ + ExtendedBuildInfo: &feature.ExtendedBuildInfo{}, + }) + + require.True(t, found) + assert.Equal(t, "img", info.ImageName) + inv := invocationRecords(readRecords(t, store, runID)) + require.Len(t, inv, 1) + assert.Equal(t, workspacelogs.InvocationSkipped, inv[0].State) + assert.Equal(t, "image_reused", inv[0].Reason) +} diff --git a/pkg/driver/apple/lifecycle.go b/pkg/driver/apple/lifecycle.go index 47477d0369..10d8507214 100644 --- a/pkg/driver/apple/lifecycle.go +++ b/pkg/driver/apple/lifecycle.go @@ -22,7 +22,11 @@ func (d *appleDriver) CommandDevContainer( return fmt.Errorf("container not found") } - if err := d.ensureContainerRunning(ctx, container); err != nil { + if params.RequireRunning { + if container.State.Status != config.ContainerStatusRunning { + return fmt.Errorf("%w: container is %q", driver.ErrNotRunning, container.State.Status) + } + } else if err := d.ensureContainerRunning(ctx, container); err != nil { return err } @@ -39,6 +43,9 @@ func (d *appleDriver) CommandDevContainer( }) } +// SupportsRequireRunning reports that CommandDevContainer honors RequireRunning. +func (d *appleDriver) SupportsRequireRunning() bool { return true } + func (d *appleDriver) ensureContainerRunning( ctx context.Context, container *config.ContainerDetails, diff --git a/pkg/driver/apple/lifecycle_test.go b/pkg/driver/apple/lifecycle_test.go index a09ee793da..9dd8619a36 100644 --- a/pkg/driver/apple/lifecycle_test.go +++ b/pkg/driver/apple/lifecycle_test.go @@ -12,6 +12,10 @@ import ( "github.com/devsy-org/devsy/pkg/driver" ) +const ( + stopC1 = "stop:c1" +) + // Test image references shared across driver tests. const ( testImage = "img" @@ -138,7 +142,7 @@ func TestDeleteDevContainer_StopsThenRemovesInOrder(t *testing.T) { t.Fatalf("delete: %v", err) } // Order matters: Apple's delete requires the container be stopped first. - if want := []string{"stop:c1", "remove:c1"}; !slices.Equal(m.calls, want) { + if want := []string{stopC1, "remove:c1"}; !slices.Equal(m.calls, want) { t.Errorf("call order = %v, want %v", m.calls, want) } } @@ -150,7 +154,7 @@ func TestDeleteDevContainer_StopFailureStillRemoves(t *testing.T) { t.Fatalf("delete: %v", err) } // A stop failure is logged but delete is still attempted. - if want := []string{"stop:c1", "remove:c1"}; !slices.Equal(m.calls, want) { + if want := []string{stopC1, "remove:c1"}; !slices.Equal(m.calls, want) { t.Errorf("call order = %v, want %v", m.calls, want) } } @@ -280,7 +284,7 @@ func TestRequireDevContainer_LifecycleOperations(t *testing.T) { call: func(d *appleDriver) error { return d.StopDevContainer(context.Background(), "ws") }, - wantCall: "stop:c1", + wantCall: stopC1, }, { name: "GetDevContainerLogs", diff --git a/pkg/driver/apple/preflight_test.go b/pkg/driver/apple/preflight_test.go index 2089d69ce5..a9bb8f230d 100644 --- a/pkg/driver/apple/preflight_test.go +++ b/pkg/driver/apple/preflight_test.go @@ -12,8 +12,7 @@ func TestApplePreflightBinaryMissing(t *testing.T) { d := &appleDriver{command: "devsy-nonexistent-container-binary", Apple: &mockClient{}} err := d.Preflight(context.Background(), driver.PreflightOptions{}) - var perr *driver.PreflightError - if !errors.As(err, &perr) { + if _, ok := errors.AsType[*driver.PreflightError](err); !ok { t.Fatalf("expected *driver.PreflightError, got %v (%T)", err, err) } } @@ -32,8 +31,7 @@ func TestApplePreflightOptOutSystemDown(t *testing.T) { d := &appleDriver{command: "sh", Apple: m} err := d.Preflight(context.Background(), driver.PreflightOptions{DisableAutoStart: true}) - var perr *driver.PreflightError - if !errors.As(err, &perr) { + if _, ok := errors.AsType[*driver.PreflightError](err); !ok { t.Fatalf("expected *driver.PreflightError when opted out and system down, got %v", err) } } diff --git a/pkg/driver/apple/require_running_test.go b/pkg/driver/apple/require_running_test.go new file mode 100644 index 0000000000..26e853eed6 --- /dev/null +++ b/pkg/driver/apple/require_running_test.go @@ -0,0 +1,53 @@ +package apple + +import ( + "context" + "testing" + + "github.com/devsy-org/devsy/pkg/devcontainer/config" + "github.com/devsy-org/devsy/pkg/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + statusExited = "exited" + execUser = "root" +) + +func TestCommandDevContainerRequireRunningNeverStarts(t *testing.T) { + for _, status := range []config.ContainerStatus{statusExited, "created", "paused", "stopped"} { + t.Run(string(status), func(t *testing.T) { + m := &mockClient{found: &config.ContainerDetails{ + ID: "c2", State: config.ContainerDetailsState{Status: status}, + }} + d := &appleDriver{Apple: m} + + err := d.CommandDevContainer(context.Background(), &driver.CommandParams{ + WorkspaceID: "ws", User: execUser, Command: "true", RequireRunning: true, + }) + + require.ErrorIs(t, err, driver.ErrNotRunning) + assert.Empty(t, m.calls) + assert.Empty(t, m.ranArgs) + }) + } +} + +func TestCommandDevContainerRequireRunningExecsRunningContainer(t *testing.T) { + m := &mockClient{found: &config.ContainerDetails{ + ID: "c2", State: config.ContainerDetailsState{Status: config.ContainerStatusRunning}, + }} + d := &appleDriver{Apple: m} + + err := d.CommandDevContainer(context.Background(), &driver.CommandParams{ + WorkspaceID: "ws", User: execUser, Command: "true", RequireRunning: true, + }) + + require.NoError(t, err) + assert.Equal(t, []string{appleExec}, m.calls) +} + +func TestAppleDriverSupportsRequireRunning(t *testing.T) { + assert.True(t, driver.DriverSupportsRequireRunning(&appleDriver{})) +} diff --git a/pkg/driver/custom/custom.go b/pkg/driver/custom/custom.go index ff22b36838..83bc4abc89 100644 --- a/pkg/driver/custom/custom.go +++ b/pkg/driver/custom/custom.go @@ -253,36 +253,6 @@ type runCommandOptions struct { extraEnv []string } -func (c *customDriver) runCommand( - ctx context.Context, - runOpts runCommandOptions, -) error { - if len(runOpts.command) == 0 { - return nil - } - - log.Debugf("run %s driver command: %s", runOpts.name, strings.Join(runOpts.command, " ")) - environ, err := ToEnvironWithBinaries(ctx, c.workspaceInfo) - if err != nil { - return err - } - environ = append(environ, pkgconfig.EnvDevcontainerID+"="+runOpts.workspaceId) - environ = append(environ, runOpts.extraEnv...) - - if log.Underlying().Core().Enabled(zapcore.DebugLevel) { - environ = append(environ, pkgconfig.EnvDebug+"="+pkgconfig.BoolTrue) - } - - return clientimplementation.RunCommand(ctx, clientimplementation.RunCommandOptions{ - Command: runOpts.command, - Environ: environ, - Stdin: runOpts.stdin, - Stdout: runOpts.stdout, - Stderr: runOpts.stderr, - RawStdout: runOpts.rawStdout, - }) -} - func ToEnvironWithBinaries( ctx context.Context, workspace *provider.AgentWorkspaceInfo, @@ -323,3 +293,33 @@ func (d *customDriver) RequiresMountStreaming() bool { return true } func (d *customDriver) RecreateMode() driver.RecreateMode { return driver.RecreateStop } func (d *customDriver) RequiresWorkspaceChown() bool { return true } + +func (c *customDriver) runCommand( + ctx context.Context, + runOpts runCommandOptions, +) error { + if len(runOpts.command) == 0 { + return nil + } + + log.Debugf("run %s driver command: %s", runOpts.name, strings.Join(runOpts.command, " ")) + environ, err := ToEnvironWithBinaries(ctx, c.workspaceInfo) + if err != nil { + return err + } + environ = append(environ, pkgconfig.EnvDevcontainerID+"="+runOpts.workspaceId) + environ = append(environ, runOpts.extraEnv...) + + if log.Underlying().Core().Enabled(zapcore.DebugLevel) { + environ = append(environ, pkgconfig.EnvDebug+"="+pkgconfig.BoolTrue) + } + + return clientimplementation.RunCommand(ctx, clientimplementation.RunCommandOptions{ + Command: runOpts.command, + Environ: environ, + Stdin: runOpts.stdin, + Stdout: runOpts.stdout, + Stderr: runOpts.stderr, + RawStdout: runOpts.rawStdout, + }) +} diff --git a/pkg/driver/docker/build.go b/pkg/driver/docker/build.go index b7c29af5c1..3e625d8e45 100644 --- a/pkg/driver/docker/build.go +++ b/pkg/driver/docker/build.go @@ -17,6 +17,7 @@ import ( "github.com/devsy-org/devsy/pkg/driver" "github.com/devsy-org/devsy/pkg/log" "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/workspacelogs" ) func (d *dockerDriver) BuildDevContainer( @@ -64,6 +65,23 @@ type buildStrategy interface { name() string } +// buildStreams are the sinks a build writes to. Combined is for producers that +// already merge stdout and stderr. +type buildStreams struct { + stdout, stderr, combined io.Writer +} + +// streamedStrategy is implemented by strategies that keep their streams apart. +type streamedStrategy interface { + buildStreamed( + ctx context.Context, + streams buildStreams, + platform string, + options *build.BuildOptions, + ) error + stepKey() string +} + // dockerBuildxStrategy uses docker buildx build. type dockerBuildxStrategy struct { driver *dockerDriver @@ -74,6 +92,22 @@ func (s *dockerBuildxStrategy) build( writer io.Writer, platform string, options *build.BuildOptions, +) error { + return s.buildStreamed( + ctx, + buildStreams{stdout: writer, stderr: writer, combined: writer}, + platform, + options, + ) +} + +func (s *dockerBuildxStrategy) stepKey() string { return "docker-buildx" } + +func (s *dockerBuildxStrategy) buildStreamed( + ctx context.Context, + streams buildStreams, + platform string, + options *build.BuildOptions, ) error { runtime := s.driver.Docker.GetRuntime() args := buildDockerBuildxArgs(options, platform, runtime) @@ -84,12 +118,12 @@ func (s *dockerBuildxStrategy) build( args = append(args, secretArgs...) log.Debugf("running docker buildx build with args: %s", strings.Join(args, " ")) stderrBuf := &tailBuffer{limit: maxStderrDiagnostics} - multiWriter := io.MultiWriter(writer, stderrBuf) + multiWriter := io.MultiWriter(streams.stderr, stderrBuf) if err := s.driver.Docker.RunWithEnv( ctx, secretEnv, args, - docker.Streams{Stdout: writer, Stderr: multiWriter}, + docker.Streams{Stdout: streams.stdout, Stderr: multiWriter}, ); err != nil { if stderrBuf.Len() > 0 { return fmt.Errorf( @@ -259,6 +293,22 @@ func (s *buildkitStrategy) build( writer io.Writer, platform string, options *build.BuildOptions, +) error { + return s.buildStreamed( + ctx, + buildStreams{stdout: writer, stderr: writer, combined: writer}, + platform, + options, + ) +} + +func (s *buildkitStrategy) stepKey() string { return "buildkit" } + +func (s *buildkitStrategy) buildStreamed( + ctx context.Context, + streams buildStreams, + platform string, + options *build.BuildOptions, ) error { dockerClient, err := s.newDockerClient(ctx) if err != nil { @@ -272,13 +322,11 @@ func (s *buildkitStrategy) build( } defer func() { _ = buildKitClient.Close() }() - if err := buildkit.Build( - ctx, - buildKitClient, - writer, - platform, - options, - ); err != nil { + if err := buildkit.Build(ctx, buildKitClient, buildkit.BuildRequest{ + Writer: streams.combined, + Platform: platform, + Options: options, + }); err != nil { return fmt.Errorf("build: %w", err) } return nil @@ -370,6 +418,7 @@ func (r *imageResolver) tryResolve( } log.Debugf("found existing local image %s", req.imageName) + buildkit.RecordSkipped(ctx, "image", "image_reused") return &config.BuildInfo{ ImageDetails: imageDetails, ImageMetadata: req.extendedBuildInfo.MetadataConfig, @@ -465,12 +514,44 @@ func (d *dockerDriver) executeBuild( }) defer func() { _ = writer.Close() }() - if err := strategy.build(ctx, writer, req.Options.Platform, buildOptions); err != nil { + err := runStrategy(ctx, strategyRun{ + strategy: strategy, writer: writer, platform: req.Options.Platform, options: buildOptions, + }) + if err != nil { return fmt.Errorf("%s build: %w", strategy.name(), err) } return nil } +type strategyRun struct { + strategy buildStrategy + writer io.Writer + platform string + options *build.BuildOptions +} + +// runStrategy tees build output to the recorded run before the logger filters +// it. Without a recorder the strategy writes to the logger only. +func runStrategy(ctx context.Context, run strategyRun) error { + streamed, ok := run.strategy.(streamedStrategy) + if !ok { + return run.strategy.build(ctx, run.writer, run.platform, run.options) + } + key := streamed.stepKey() + if run.platform != "" { + key += " " + run.platform + } + step := buildkit.StartStep(ctx, key, run.options.BuildSecrets...) + err := streamed.buildStreamed(ctx, buildStreams{ + stdout: step.Tee(run.writer, workspacelogs.StreamStdout), + stderr: step.Tee(run.writer, workspacelogs.StreamStderr), + combined: step.Tee(run.writer, workspacelogs.StreamCombined), + }, run.platform, run.options) + step.Finish(ctx, err) + + return err +} + // createBuildInfo constructs the BuildInfo after a successful build. When pushing, // the image may not be available locally, so ImageDetails may be nil. func (d *dockerDriver) createBuildInfo( diff --git a/pkg/driver/docker/build_record_test.go b/pkg/driver/docker/build_record_test.go new file mode 100644 index 0000000000..8c7f914fb4 --- /dev/null +++ b/pkg/driver/docker/build_record_test.go @@ -0,0 +1,247 @@ +package docker + +import ( + "context" + "errors" + "io" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/devsy-org/devsy/pkg/devcontainer/build" + "github.com/devsy-org/devsy/pkg/docker" + "github.com/devsy-org/devsy/pkg/driver" + "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/zap/zapcore" +) + +func recordedContext(t *testing.T) (context.Context, *workspacelogs.Store, string) { + t.Helper() + store, err := workspacelogs.Open(workspacelogs.Options{Root: t.TempDir()}) + require.NoError(t, err) + m, err := store.CreateRun(workspacelogs.RunSpec{ + Context: "default", WorkspaceUID: "uid", Command: "up", + }) + require.NoError(t, err) + rec, err := workspacelogs.NewRecorder(workspacelogs.RecorderOptions{ + Backend: store, RunID: m.RunID, Location: workspacelogs.LocationProviderHost, + }) + require.NoError(t, err) + + return workspacelogs.WithRecorder(t.Context(), rec), store, m.RunID +} + +func readRecords(t *testing.T, store *workspacelogs.Store, runID string) []workspacelogs.Record { + t.Helper() + var all []workspacelogs.Record + req := workspacelogs.ReadRequest{RunID: runID} + for { + resp, err := store.Read(req) + require.NoError(t, err) + all = append(all, resp.Records...) + if !resp.HasMore { + return all + } + req.After = resp.NextCursor + } +} + +func outputText(recs []workspacelogs.Record, stream workspacelogs.Stream) string { + var b strings.Builder + for i := range recs { + if recs[i].Kind == workspacelogs.KindOutput && recs[i].Stream == stream { + b.WriteString(recs[i].Text) + } + } + + return b.String() +} + +func lastInvocation(t *testing.T, recs []workspacelogs.Record) workspacelogs.Record { + t.Helper() + var last *workspacelogs.Record + for i := range recs { + if recs[i].Kind == workspacelogs.KindInvocation { + last = &recs[i] + } + } + require.NotNil(t, last) + + return *last +} + +func firstInvocation(t *testing.T, recs []workspacelogs.Record) workspacelogs.Record { + t.Helper() + for i := range recs { + if recs[i].Kind == workspacelogs.KindInvocation { + return recs[i] + } + } + require.Fail(t, "no invocation recorded") + + return workspacelogs.Record{} +} + +const fakeKey = "fake" + +type streamedFake struct { + stdout, stderr, combined string + err error + key string +} + +func (s streamedFake) build(context.Context, io.Writer, string, *build.BuildOptions) error { + return errors.New("plain build must not be used") +} + +func (s streamedFake) name() string { return "fake build" } +func (s streamedFake) stepKey() string { return s.key } + +func (s streamedFake) buildStreamed( + _ context.Context, + streams buildStreams, + _ string, + _ *build.BuildOptions, +) error { + _, _ = streams.stdout.Write([]byte(s.stdout)) + _, _ = streams.stderr.Write([]byte(s.stderr)) + _, _ = streams.combined.Write([]byte(s.combined)) + + return s.err +} + +func TestExecuteBuildRetainsOutputAtWarnLevel(t *testing.T) { + logs := log.InitTestObserved(t, zapcore.WarnLevel) + ctx, store, runID := recordedContext(t) + + err := (&dockerDriver{}).executeBuild( + ctx, + streamedFake{stdout: "out line\n", stderr: "err line\n", combined: "merged", key: fakeKey}, + driver.BuildRequest{Options: provider.BuildOptions{Platform: "linux/arm64"}}, + &build.BuildOptions{}, + ) + + require.NoError(t, err) + assert.Empty(t, logs.All()) + recs := readRecords(t, store, runID) + assert.Equal(t, "out line\n", outputText(recs, workspacelogs.StreamStdout)) + assert.Equal(t, "err line\n", outputText(recs, workspacelogs.StreamStderr)) + assert.Equal(t, "merged", outputText(recs, workspacelogs.StreamCombined)) + inv := lastInvocation(t, recs) + assert.Equal(t, workspacelogs.InvocationSucceeded, inv.State) + start := firstInvocation(t, recs) + assert.Equal(t, workspacelogs.InvocationRunning, start.State) + assert.Equal(t, fakeKey+" linux/arm64", start.HookKey) + assert.Equal(t, "build", start.Phase) + assert.Empty(t, start.Hook) +} + +func TestExecuteBuildFailureKeepsErrorAndRecordsFailed(t *testing.T) { + log.InitTestObserved(t, zapcore.WarnLevel) + ctx, store, runID := recordedContext(t) + cause := errors.New("solve exploded") + + err := (&dockerDriver{}).executeBuild( + ctx, + streamedFake{stderr: "boom\n", err: cause, key: fakeKey}, + driver.BuildRequest{}, + &build.BuildOptions{}, + ) + + require.ErrorIs(t, err, cause) + assert.EqualError(t, err, "fake build build: solve exploded") + recs := readRecords(t, store, runID) + assert.Equal(t, "boom\n", outputText(recs, workspacelogs.StreamStderr)) + assert.Equal(t, workspacelogs.InvocationFailed, lastInvocation(t, recs).State) +} + +func TestExecuteBuildCanceledIsRecorded(t *testing.T) { + log.InitTestObserved(t, zapcore.WarnLevel) + ctx, store, runID := recordedContext(t) + ctx, cancel := context.WithCancel(ctx) + cancel() + + err := (&dockerDriver{}).executeBuild( + ctx, + streamedFake{err: context.Canceled, key: fakeKey}, + driver.BuildRequest{}, + &build.BuildOptions{}, + ) + + require.ErrorIs(t, err, context.Canceled) + assert.Equal( + t, + workspacelogs.InvocationCanceled, + lastInvocation(t, readRecords(t, store, runID)).State, + ) +} + +func TestExecuteBuildWithoutRecorderUsesLoggerOnly(t *testing.T) { + logs := log.InitTestObserved(t, zapcore.DebugLevel) + + err := (&dockerDriver{}).executeBuild( + t.Context(), + streamedFake{stdout: "out\n", stderr: "err\n", combined: "both\n"}, + driver.BuildRequest{}, + &build.BuildOptions{}, + ) + + require.NoError(t, err) + var messages []string + for _, e := range logs.All() { + messages = append(messages, e.Message) + } + assert.Equal(t, []string{"build with fake build", "out", "err", "both"}, messages) +} + +func writeBuildxScript(t *testing.T) string { + t.Helper() + path := filepath.Join(t.TempDir(), "docker-fake") + script := "#!/bin/sh\n" + + "case \"$1\" in\n" + + " --version) echo 'Docker version 24.0.7, build abc'; exit 0;;\n" + + "esac\n" + + "echo 'step output'\n" + + "echo 'step error' >&2\n" + + "exit 3\n" + //nolint:gosec // test helper script needs exec bit + require.NoError(t, os.WriteFile(path, []byte(script), 0o755)) + + return path +} + +func TestBuildxKeepsStreamsSeparateAndErrorTail(t *testing.T) { + log.InitTestObserved(t, zapcore.WarnLevel) + ctx, store, runID := recordedContext(t) + d := &dockerDriver{Docker: &docker.DockerHelper{DockerCommand: writeBuildxScript(t)}} + + err := d.executeBuild( + ctx, + &dockerBuildxStrategy{driver: d}, + driver.BuildRequest{}, + &build.BuildOptions{Dockerfile: "Dockerfile", Context: "."}, + ) + + require.Error(t, err) + assert.Contains(t, err.Error(), "failed to build image") + assert.Contains(t, err.Error(), "step error") + assert.NotContains(t, err.Error(), "step output") + recs := readRecords(t, store, runID) + assert.Equal(t, "step output\n", outputText(recs, workspacelogs.StreamStdout)) + assert.Equal(t, "step error\n", outputText(recs, workspacelogs.StreamStderr)) + inv := lastInvocation(t, recs) + assert.Equal(t, workspacelogs.InvocationFailed, inv.State) + assert.Equal(t, "docker-buildx", firstInvocation(t, recs).HookKey) + require.NotNil(t, inv.ExitCode) + assert.Equal(t, 3, *inv.ExitCode) +} + +func TestStrategiesReportStepKeys(t *testing.T) { + assert.Equal(t, "docker-buildx", (&dockerBuildxStrategy{}).stepKey()) + assert.Equal(t, "buildkit", (&buildkitStrategy{}).stepKey()) +} diff --git a/pkg/driver/docker/lifecycle.go b/pkg/driver/docker/lifecycle.go index 601486524f..981f6110eb 100644 --- a/pkg/driver/docker/lifecycle.go +++ b/pkg/driver/docker/lifecycle.go @@ -47,7 +47,11 @@ func (d *dockerDriver) CommandDevContainer( return err } - if err := d.ensureContainerRunning(ctx, container); err != nil { + if params.RequireRunning { + if container.State.Status != config.ContainerStatusRunning { + return fmt.Errorf("%w: container is %q", driver.ErrNotRunning, container.State.Status) + } + } else if err := d.ensureContainerRunning(ctx, container); err != nil { return err } @@ -67,6 +71,9 @@ func (d *dockerDriver) CommandDevContainer( return nil } +// SupportsRequireRunning reports that CommandDevContainer honors RequireRunning. +func (d *dockerDriver) SupportsRequireRunning() bool { return true } + // ensureContainerRunning checks the container's state and starts it if necessary. func (d *dockerDriver) ensureContainerRunning( ctx context.Context, diff --git a/pkg/driver/docker/require_running_test.go b/pkg/driver/docker/require_running_test.go new file mode 100644 index 0000000000..b6d3c49713 --- /dev/null +++ b/pkg/driver/docker/require_running_test.go @@ -0,0 +1,117 @@ +package docker + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/devsy-org/devsy/pkg/docker" + "github.com/devsy-org/devsy/pkg/driver" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// requireRunningDocker is a fake docker whose inspected state is fixed and whose +// calls are logged, so a test can prove nothing but inspect and exec ran. +func requireRunningDocker(t *testing.T, status, execBody string) (*dockerDriver, string) { + t.Helper() + dir := t.TempDir() + calls := filepath.Join(dir, "calls") + script := `#!/bin/sh +echo "$1" >> "` + calls + `" +case "$1" in + inspect) + echo '[{"ID":"c1","State":{"Status":"` + status + `"}}]' + ;; + exec) + ` + execBody + ` + ;; +esac +` + bin := filepath.Join(dir, "docker-fake") + require.NoError(t, os.WriteFile(bin, []byte(script), 0o755)) //nolint:gosec + + return &dockerDriver{Docker: &docker.DockerHelper{DockerCommand: bin, ContainerID: "c1"}}, calls +} + +const shellTrue = "true" + +func loggedCalls(t *testing.T, path string) []string { + t.Helper() + data, err := os.ReadFile(path) //nolint:gosec // test temp file + require.NoError(t, err) + + return strings.Fields(string(data)) +} + +func TestCommandDevContainerRequireRunningNeverStarts(t *testing.T) { + for _, status := range []string{"exited", "created", "paused", "restarting", "dead"} { + t.Run(status, func(t *testing.T) { + d, calls := requireRunningDocker(t, status, "") + + err := d.CommandDevContainer(context.Background(), &driver.CommandParams{ + User: rootUser, Command: shellTrue, RequireRunning: true, + }) + + require.ErrorIs(t, err, driver.ErrNotRunning) + for _, call := range loggedCalls(t, calls) { + assert.NotContains(t, []string{"start", "unpause", "restart", "exec"}, call) + } + }) + } +} + +func TestCommandDevContainerRequireRunningExecsRunningContainer(t *testing.T) { + d, calls := requireRunningDocker(t, "running", "echo ok") + + err := d.CommandDevContainer(context.Background(), &driver.CommandParams{ + User: rootUser, Command: shellTrue, RequireRunning: true, + }) + + require.NoError(t, err) + assert.Equal(t, []string{"inspect", "exec"}, loggedCalls(t, calls)) +} + +// A stop between inspect and exec surfaces as the exec error. It must not +// trigger a start or a second attempt. +func TestCommandDevContainerRequireRunningStopBetweenInspectAndExec(t *testing.T) { + d, calls := requireRunningDocker(t, "running", + `echo "container c1 is not running" >&2; exit 1`) + + err := d.CommandDevContainer(context.Background(), &driver.CommandParams{ + User: rootUser, Command: shellTrue, RequireRunning: true, + }) + + require.Error(t, err) + got := loggedCalls(t, calls) + assert.Equal(t, 1, countOf(got, "exec")) + assert.NotContains(t, got, "start") + assert.NotContains(t, got, "unpause") +} + +func countOf(list []string, want string) int { + n := 0 + for _, s := range list { + if s == want { + n++ + } + } + + return n +} + +func TestCommandDevContainerDefaultStillStartsStoppedContainer(t *testing.T) { + d, calls := requireRunningDocker(t, "exited", "echo ok") + + _ = d.CommandDevContainer(context.Background(), &driver.CommandParams{ + User: rootUser, Command: shellTrue, + }) + + assert.Contains(t, loggedCalls(t, calls), "start") +} + +func TestDockerDriverSupportsRequireRunning(t *testing.T) { + assert.True(t, driver.DriverSupportsRequireRunning(&dockerDriver{})) +} diff --git a/pkg/driver/external/internal/testfixture/main.go b/pkg/driver/external/internal/testfixture/main.go index ca2036febf..ab0e9110f1 100644 --- a/pkg/driver/external/internal/testfixture/main.go +++ b/pkg/driver/external/internal/testfixture/main.go @@ -138,23 +138,27 @@ func (f *fixture) Preflight( if f.mode != "blocked" && f.mode != "stream-block-child" { return f.Driver.Preflight(ctx, request) } + return blockPreflight(f.directory) +} + +func blockPreflight(directory string) (*runtimev1.PreflightResponse, error) { executable, err := os.Executable() if err != nil { return nil, err } // #nosec G204 -- Relaunches this trusted test fixture; no shell or PATH lookup. - child := exec.Command(executable, "child", f.directory) + child := exec.Command(executable, "child", directory) if err := child.Start(); err != nil { return nil, err } go func() { _ = child.Wait() }() for { - if _, err := os.Stat(filepath.Join(f.directory, "child-pids")); err == nil { + if _, err := os.Stat(filepath.Join(directory, "child-pids")); err == nil { break } time.Sleep(time.Millisecond) } - if err := os.WriteFile(filepath.Join(f.directory, "ready"), nil, 0o600); err != nil { + if err := os.WriteFile(filepath.Join(directory, "ready"), nil, 0o600); err != nil { return nil, err } // #nosec G118 -- Deliberately ignores cancellation to prove supervisor forced cleanup. diff --git a/pkg/driver/external/run_image_test.go b/pkg/driver/external/run_image_test.go index 384b893386..98c3ffb687 100644 --- a/pkg/driver/external/run_image_test.go +++ b/pkg/driver/external/run_image_test.go @@ -232,9 +232,10 @@ func (s *ImageRunSuite) TestNegotiatedCapabilities() { s.True(driver.DriverRequiresMountStreaming(h)) s.True(driver.DriverRequiresWorkspaceChown(h)) want := driver.RecreateStop - if mode == runtimev1.RecreateMode_RECREATE_MODE_DELETE { + switch mode { + case runtimev1.RecreateMode_RECREATE_MODE_DELETE: want = driver.RecreateDelete - } else if mode == runtimev1.RecreateMode_RECREATE_MODE_ON_RUN { + case runtimev1.RecreateMode_RECREATE_MODE_ON_RUN: want = driver.RecreateOnRun } s.Equal(want, driver.DriverRecreateMode(h)) diff --git a/pkg/driver/external/session.go b/pkg/driver/external/session.go index 05504dd5e5..0debee63a0 100644 --- a/pkg/driver/external/session.go +++ b/pkg/driver/external/session.go @@ -41,36 +41,7 @@ func (h *Host) call( stream: secrets.NewStreamingRedactor(h.redactor), } defer func() { diagnostic.close(); _ = output.Close() }() - owned := supervisor.Runner(supervisor.Options{ - SupervisorBinary: h.supervisorBinary, SupervisorArgs: h.supervisorArgs, - RuntimeBinary: binary, Args: h.config.External.Args, - Env: append(slices.Clone(h.environment), h.config.External.Binary+"="+binary), - }) - // Routine helper lifecycle diagnostics belong at trace; runtime stderr is - // preserved independently below, including unstructured backend failures. - frameworkLogLevel := hclog.Warn - if log.LevelString() == log.LevelTraceName { - frameworkLogLevel = hclog.Trace - } - client := hplugin.NewClient(&hplugin.ClientConfig{ - HandshakeConfig: sdkplugin.Handshake(), - VersionedPlugins: map[int]hplugin.PluginSet{ - sdkplugin.ProtocolVersion: sdkplugin.ClientPlugins(), - }, - AllowedProtocols: []hplugin.Protocol{hplugin.ProtocolGRPC}, - StartTimeout: h.timeout, - Logger: hclog.New(&hclog.LoggerOptions{ - Output: diagnostic, Level: frameworkLogLevel, - }), - Stderr: diagnostic, - RunnerFunc: func(logger hclog.Logger, command *exec.Cmd, socketDir string) (runner.Runner, error) { - process, err := owned(logger, command, socketDir) - if err != nil { - return nil, err - } - return &contextRunner{Runner: process, caller: ctx}, nil - }, - }) + client := h.newPluginClient(ctx, binary, diagnostic) defer client.Kill() started := time.Now() transport, err := client.Client() @@ -129,3 +100,40 @@ func (w *diagnosticWriter) close() { defer w.mu.Unlock() _, _ = io.WriteString(w.writer, w.stream.Flush()) } + +func (h *Host) newPluginClient( + ctx context.Context, + binary string, + diagnostic *diagnosticWriter, +) *hplugin.Client { + owned := supervisor.Runner(supervisor.Options{ + SupervisorBinary: h.supervisorBinary, SupervisorArgs: h.supervisorArgs, + RuntimeBinary: binary, Args: h.config.External.Args, + Env: append(slices.Clone(h.environment), h.config.External.Binary+"="+binary), + }) + // Routine helper lifecycle diagnostics belong at trace; runtime stderr is + // preserved independently below, including unstructured backend failures. + frameworkLogLevel := hclog.Warn + if log.LevelString() == log.LevelTraceName { + frameworkLogLevel = hclog.Trace + } + return hplugin.NewClient(&hplugin.ClientConfig{ + HandshakeConfig: sdkplugin.Handshake(), + VersionedPlugins: map[int]hplugin.PluginSet{ + sdkplugin.ProtocolVersion: sdkplugin.ClientPlugins(), + }, + AllowedProtocols: []hplugin.Protocol{hplugin.ProtocolGRPC}, + StartTimeout: h.timeout, + Logger: hclog.New(&hclog.LoggerOptions{ + Output: diagnostic, Level: frameworkLogLevel, + }), + Stderr: diagnostic, + RunnerFunc: func(logger hclog.Logger, command *exec.Cmd, socketDir string) (runner.Runner, error) { + process, err := owned(logger, command, socketDir) + if err != nil { + return nil, err + } + return &contextRunner{Runner: process, caller: ctx}, nil + }, + }) +} diff --git a/pkg/driver/kubernetes/client.go b/pkg/driver/kubernetes/client.go index 7f1a3932e2..821757645c 100644 --- a/pkg/driver/kubernetes/client.go +++ b/pkg/driver/kubernetes/client.go @@ -77,7 +77,7 @@ func (c *Client) Config() *rest.Config { } func (c *Client) FullLogs(ctx context.Context, namespace, pod, container string) ([]byte, error) { - logs, err := c.Logs(ctx, namespace, pod, container, true) + logs, err := c.Logs(ctx, logsTarget{namespace: namespace, pod: pod, container: container}, true) if err != nil { return nil, err } @@ -85,11 +85,13 @@ func (c *Client) FullLogs(ctx context.Context, namespace, pod, container string) return io.ReadAll(logs) } -func (c *Client) Logs( - ctx context.Context, - namespace, pod, container string, - follow bool, -) (io.ReadCloser, error) { +// logsTarget selects the container whose logs are read. +type logsTarget struct { + namespace, pod, container string +} + +func (c *Client) Logs(ctx context.Context, target logsTarget, follow bool) (io.ReadCloser, error) { + namespace, pod, container := target.namespace, target.pod, target.container return c.client.CoreV1().Pods(namespace).GetLogs(pod, &corev1.PodLogOptions{ Container: container, Follow: follow, diff --git a/pkg/driver/kubernetes/driver.go b/pkg/driver/kubernetes/driver.go index 045514940e..0abd5503f8 100644 --- a/pkg/driver/kubernetes/driver.go +++ b/pkg/driver/kubernetes/driver.go @@ -31,7 +31,8 @@ func NewKubernetesDriver( if err != nil { return nil, fmt.Errorf("failed to create kubernetes client: %w", err) } - // Namespace can be defined in many ways, we first check the kube config, then the provider options KUBERNETES_NAMESPACE, then failing that the default "devsy" + // Namespace can be defined in many ways, we first check the kube config, then the provider options + // KUBERNETES_NAMESPACE, then failing that the default "devsy" if namespace == "" || namespace == "default" || options.KubernetesNamespace != "" { log.Debugf("Using Explicit Kubernetes Namespace") namespace = options.KubernetesNamespace @@ -164,6 +165,10 @@ func (k *KubernetesDriver) CommandDevContainer( }) } +// SupportsRequireRunning reports that CommandDevContainer honors RequireRunning. +// It only execs into the existing pod, so it never starts anything. +func (k *KubernetesDriver) SupportsRequireRunning() bool { return true } + // CommandContainerArgv execs argv in the workspace pod's dev container, streaming // the given stdin/stdout/stderr. Unlike CommandDevContainer it takes explicit // argv rather than wrapping the command in sh -c. @@ -192,7 +197,11 @@ func (k *KubernetesDriver) GetDevContainerLogs( ) error { workspaceID = getID(workspaceID) - logs, err := k.client.Logs(ctx, k.namespace, workspaceID, DevContainerName, true) + logs, err := k.client.Logs(ctx, logsTarget{ + namespace: k.namespace, + pod: workspaceID, + container: DevContainerName, + }, true) if err != nil { return fmt.Errorf("get logs: %w", err) } @@ -205,6 +214,12 @@ func (k *KubernetesDriver) GetDevContainerLogs( return nil } +func (d *KubernetesDriver) RequiresMountStreaming() bool { return true } + +func (d *KubernetesDriver) RecreateMode() driver.RecreateMode { return driver.RecreateStop } + +func (d *KubernetesDriver) RequiresWorkspaceChown() bool { return true } + func (k *KubernetesDriver) getDevContainerPvc( ctx context.Context, id string, @@ -288,9 +303,3 @@ func (k *KubernetesDriver) deleteWorkspaceSecrets(ctx context.Context, workspace return nil } - -func (d *KubernetesDriver) RequiresMountStreaming() bool { return true } - -func (d *KubernetesDriver) RecreateMode() driver.RecreateMode { return driver.RecreateStop } - -func (d *KubernetesDriver) RequiresWorkspaceChown() bool { return true } diff --git a/pkg/driver/kubernetes/find.go b/pkg/driver/kubernetes/find.go index 0f63328126..f5b76c96f0 100644 --- a/pkg/driver/kubernetes/find.go +++ b/pkg/driver/kubernetes/find.go @@ -6,6 +6,7 @@ import ( "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/log" + corev1 "k8s.io/api/core/v1" ) func (k *KubernetesDriver) FindDevContainer( @@ -41,35 +42,48 @@ func (k *KubernetesDriver) FindDevContainer( if pod != nil { startedAt = pod.CreationTimestamp.String() } - // A volume without saved options still has to be found so stop and delete can clean it up. - var ( - dockerless, imageEntrypoint bool - topologyError string - labels = map[string]string{} - ) - if containerInfo.Options == nil { - topologyError = fmt.Sprintf("workspace volume %q has no saved run options", workspaceId) - } else { - var topologyErr error - dockerless, imageEntrypoint, topologyErr = observedPodTopology(pod, containerInfo.Options) - if topologyErr != nil { - topologyError = topologyErr.Error() - } - labels = config.ListToObject(containerInfo.Options.Labels) - } + topology := describeTopology(pod, workspaceId, containerInfo) return &config.ContainerDetails{ ID: pvc.Name, Created: pvc.CreationTimestamp.String(), - Dockerless: dockerless, - ImageEntrypoint: imageEntrypoint, - LifecycleTopologyError: topologyError, + Dockerless: topology.dockerless, + ImageEntrypoint: topology.imageEntrypoint, + LifecycleTopologyError: topology.err, State: config.ContainerDetailsState{ Status: status, StartedAt: startedAt, }, Config: config.ContainerDetailsConfig{ - Labels: labels, + Labels: topology.labels, }, }, nil } + +type topologyDescription struct { + dockerless, imageEntrypoint bool + err string + labels map[string]string +} + +// A volume without saved options still has to be found so stop and delete can clean it up. +func describeTopology( + pod *corev1.Pod, + workspaceId string, + containerInfo *DevContainerInfo, +) topologyDescription { + if containerInfo.Options == nil { + return topologyDescription{ + err: fmt.Sprintf("workspace volume %q has no saved run options", workspaceId), + labels: map[string]string{}, + } + } + result := topologyDescription{labels: config.ListToObject(containerInfo.Options.Labels)} + var topologyErr error + result.dockerless, result.imageEntrypoint, topologyErr = observedPodTopology( + pod, containerInfo.Options) + if topologyErr != nil { + result.err = topologyErr.Error() + } + return result +} diff --git a/pkg/driver/kubernetes/helper.go b/pkg/driver/kubernetes/helper.go index 37f118bfec..5a39ad1f7b 100644 --- a/pkg/driver/kubernetes/helper.go +++ b/pkg/driver/kubernetes/helper.go @@ -73,6 +73,7 @@ func getPodTemplate(manifest string) (*corev1.Pod, error) { err, ) } + //nolint:gosec // path is inside the kubeconfig directory body, err := os.ReadFile(p) if err != nil { return nil, fmt.Errorf( diff --git a/pkg/driver/kubernetes/pod_lifecycle.go b/pkg/driver/kubernetes/pod_lifecycle.go index 2965109c21..400145d4cc 100644 --- a/pkg/driver/kubernetes/pod_lifecycle.go +++ b/pkg/driver/kubernetes/pod_lifecycle.go @@ -164,7 +164,7 @@ func (k *KubernetesDriver) reconcileObservedPod( } log.Debugf("Pod %q already exists and nothing changed, skipping update", id) if classifyPod(pod) == podPending { - if _, err := k.waitPodRunning(ctx, id); err != nil { + if err := k.waitPodRunning(ctx, id); err != nil { return false, err } } diff --git a/pkg/driver/kubernetes/pod_lifecycle_test.go b/pkg/driver/kubernetes/pod_lifecycle_test.go index 9b8ce270db..bf540af146 100644 --- a/pkg/driver/kubernetes/pod_lifecycle_test.go +++ b/pkg/driver/kubernetes/pod_lifecycle_test.go @@ -30,6 +30,8 @@ import ( clienttesting "k8s.io/client-go/testing" ) +const testDiskSize = "1Gi" + const ( testNamespace = "devsy-test" testPodID = "devsy-ws" @@ -59,7 +61,7 @@ func newFakeCluster(t *testing.T, objects ...runtime.Object) *fakeCluster { namespace: testNamespace, options: &provider2.ProviderKubernetesDriverConfig{ PodTimeout: "2s", - DiskSize: "1Gi", + DiskSize: testDiskSize, }, agentConfig: &provider2.ProviderAgentConfig{}, }, @@ -233,7 +235,7 @@ func TestReconcileReusesPodWhenOnlyImageConfigFileChanged(t *testing.T) { phase: corev1.PodRunning, options: &provider2.ProviderKubernetesDriverConfig{ PodTimeout: "2s", - DiskSize: "1Gi", + DiskSize: testDiskSize, ImageConfigFile: "/old/image-config.json", }, devsy: &corev1.ContainerStatus{ @@ -250,7 +252,7 @@ func TestReconcileReusesPodWhenOnlyImageConfigFileChanged(t *testing.T) { func TestWithoutLocalOptionsDropsImageConfigFile(t *testing.T) { options := &provider2.ProviderKubernetesDriverConfig{ - DiskSize: "1Gi", + DiskSize: testDiskSize, ImageConfigFile: "/home/me/image-config.json", } @@ -258,23 +260,14 @@ func TestWithoutLocalOptionsDropsImageConfigFile(t *testing.T) { require.NoError(t, err) assert.NotContains(t, string(raw), "imageConfigFile") - assert.Contains(t, string(raw), "1Gi") + assert.Contains(t, string(raw), testDiskSize) assert.Equal(t, "/home/me/image-config.json", options.ImageConfigFile) } func TestReconcilePendingPodWaitsForReadiness(t *testing.T) { c := newFakeCluster(t) c.putPod(t, c.pod(t, podSpec{uid: "u1", phase: corev1.PodPending})) - observed := make(chan struct{}, 1) - c.cs.PrependReactor( - "get", podResourceName, func(clienttesting.Action) (bool, runtime.Object, error) { - select { - case observed <- struct{}{}: - default: - } - return false, nil, nil - }, - ) + observed := observePodGets(c) type result struct { reused bool err error @@ -294,12 +287,7 @@ func TestReconcilePendingPodWaitsForReadiness(t *testing.T) { t.Fatalf("reconcile returned before the pod was ready: %+v", got) case <-time.After(40 * time.Millisecond): } - running := c.pod(t, podSpec{uid: "u1", phase: corev1.PodRunning}) - running.Status.ContainerStatuses = []corev1.ContainerStatus{{ - Name: DevContainerName, Ready: true, - State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{}}, - }} - c.putPod(t, running) + c.putPod(t, readyRunningPod(t, c)) select { case got := <-done: require.NoError(t, got.err) @@ -309,6 +297,33 @@ func TestReconcilePendingPodWaitsForReadiness(t *testing.T) { } } +func observePodGets(c *fakeCluster) chan struct{} { + observed := make(chan struct{}, 1) + c.cs.PrependReactor( + "get", podResourceName, func(clienttesting.Action) (bool, runtime.Object, error) { + select { + case observed <- struct{}{}: + default: + } + + return false, nil, nil + }, + ) + + return observed +} + +func readyRunningPod(t *testing.T, c *fakeCluster) *corev1.Pod { + t.Helper() + running := c.pod(t, podSpec{uid: "u1", phase: corev1.PodRunning}) + running.Status.ContainerStatuses = []corev1.ContainerStatus{{ + Name: DevContainerName, Ready: true, + State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{}}, + }} + + return running +} + func TestReconcileTerminatingPodWaitsForDeletionThenAllowsCreate(t *testing.T) { c := newFakeCluster(t) c.putPod(t, c.pod(t, podSpec{uid: "u1", phase: corev1.PodRunning, deleting: true})) diff --git a/pkg/driver/kubernetes/preflight_test.go b/pkg/driver/kubernetes/preflight_test.go index d779c58a03..3faec7b60f 100644 --- a/pkg/driver/kubernetes/preflight_test.go +++ b/pkg/driver/kubernetes/preflight_test.go @@ -47,8 +47,7 @@ func TestKubernetesPreflightUnreachable(t *testing.T) { srv.Close() // nothing is listening on host anymore err := newTestDriver(t, host).Preflight(context.Background(), driver.PreflightOptions{}) - var perr *driver.PreflightError - if !errors.As(err, &perr) { + if _, ok := errors.AsType[*driver.PreflightError](err); !ok { t.Fatalf("expected *driver.PreflightError for unreachable cluster, got %v (%T)", err, err) } } diff --git a/pkg/driver/kubernetes/registry_test.go b/pkg/driver/kubernetes/registry_test.go index 42ae619af1..d6a901622a 100644 --- a/pkg/driver/kubernetes/registry_test.go +++ b/pkg/driver/kubernetes/registry_test.go @@ -2,6 +2,10 @@ package kubernetes import "testing" +const ( + shouldReturnOfficialDockerRegistry = "should return official docker registry" +) + func TestGetRegistryFromImageName(t *testing.T) { type args struct { imageName string @@ -13,7 +17,7 @@ func TestGetRegistryFromImageName(t *testing.T) { wantErr bool }{ { - name: "should return official docker registry", + name: shouldReturnOfficialDockerRegistry, args: args{ imageName: "docker.io/loftsh/devsy", }, @@ -21,7 +25,7 @@ func TestGetRegistryFromImageName(t *testing.T) { wantErr: false, }, { - name: "should return official docker registry", + name: shouldReturnOfficialDockerRegistry, args: args{ imageName: "hub.docker.com/loftsh/devsy", }, @@ -29,7 +33,7 @@ func TestGetRegistryFromImageName(t *testing.T) { wantErr: false, }, { - name: "should return official docker registry", + name: shouldReturnOfficialDockerRegistry, args: args{ imageName: "nginx:latest", }, diff --git a/pkg/driver/kubernetes/require_running_test.go b/pkg/driver/kubernetes/require_running_test.go new file mode 100644 index 0000000000..88c162eafd --- /dev/null +++ b/pkg/driver/kubernetes/require_running_test.go @@ -0,0 +1,14 @@ +package kubernetes + +import ( + "testing" + + "github.com/devsy-org/devsy/pkg/driver" + "github.com/stretchr/testify/assert" +) + +// The Kubernetes driver only execs into the existing pod, so exec-only calls +// cannot start anything. +func TestKubernetesDriverSupportsRequireRunning(t *testing.T) { + assert.True(t, driver.DriverSupportsRequireRunning(&KubernetesDriver{})) +} diff --git a/pkg/driver/kubernetes/run.go b/pkg/driver/kubernetes/run.go index c1bbceb4af..df9da2d9a1 100644 --- a/pkg/driver/kubernetes/run.go +++ b/pkg/driver/kubernetes/run.go @@ -169,21 +169,8 @@ func (k *KubernetesDriver) buildPod( options *driver.RunOptions, initialize bool, ) (*corev1.Pod, error) { - mount, err := k.resolveWorkspaceMount(options) - if err != nil { - return nil, err - } - - pod, err := loadPodTemplate(k.options.PodManifestTemplate) - if err != nil { - return nil, err - } - // Reject before any cluster object is created for this run. - timeout, err := lifecycleTimeout(options) + pod, mount, err := k.loadValidatedPod(options) if err != nil { - return nil, fmt.Errorf("read lifecycle policy: %w", err) - } - if err := validateNoExtraContainers(pod, timeout); err != nil { return nil, err } @@ -226,13 +213,40 @@ func (k *KubernetesDriver) buildPod( }); err != nil { return nil, err } + setTopologyAnnotations(pod, options) + + return pod, nil +} + +func (k *KubernetesDriver) loadValidatedPod( + options *driver.RunOptions, +) (*corev1.Pod, *config.Mount, error) { + mount, err := k.resolveWorkspaceMount(options) + if err != nil { + return nil, nil, err + } + + pod, err := loadPodTemplate(k.options.PodManifestTemplate) + if err != nil { + return nil, nil, err + } + // Reject before any cluster object is created for this run. + timeout, err := lifecycleTimeout(options) + if err != nil { + return nil, nil, fmt.Errorf("read lifecycle policy: %w", err) + } + if err := validateNoExtraContainers(pod, timeout); err != nil { + return nil, nil, err + } + return pod, mount, nil +} + +func setTopologyAnnotations(pod *corev1.Pod, options *driver.RunOptions) { if pod.Annotations == nil { pod.Annotations = map[string]string{} } pod.Annotations[DevsyDockerlessAnnotation] = strconv.FormatBool(options.Dockerless) pod.Annotations[DevsyImageEntrypointAnnotation] = strconv.FormatBool(options.ImageEntrypoint) - - return pod, nil } type podSpecInputs struct { @@ -589,7 +603,7 @@ func (k *KubernetesDriver) runPod( // wait for pod running log.Debugf("Waiting for DevContainer Pod %q to come up", id) - _, err = k.waitPodRunning(ctx, id) + err = k.waitPodRunning(ctx, id) if err != nil { return err } diff --git a/pkg/driver/kubernetes/wait.go b/pkg/driver/kubernetes/wait.go index e624dae93c..550d14304c 100644 --- a/pkg/driver/kubernetes/wait.go +++ b/pkg/driver/kubernetes/wait.go @@ -13,12 +13,12 @@ import ( "k8s.io/apimachinery/pkg/util/wait" ) -func (k *KubernetesDriver) waitPodRunning(ctx context.Context, id string) (*corev1.Pod, error) { +func (k *KubernetesDriver) waitPodRunning(ctx context.Context, id string) error { throttledLogger := throttledlogger.NewThrottledLogger(time.Second * 5) timeoutDuration, err := time.ParseDuration(k.options.PodTimeout) if err != nil { - return nil, fmt.Errorf("parse pod timeout: %w", err) + return fmt.Errorf("parse pod timeout: %w", err) } started := time.Now() @@ -37,7 +37,7 @@ func (k *KubernetesDriver) waitPodRunning(ctx context.Context, id string) (*core return false, fmt.Errorf("pod %q disappeared while waiting for it to start", id) } - return k.evaluatePodStatus(ctx, pod, &podStatusParams{ + return k.evaluatePodStatus(pod, &podStatusParams{ id: id, started: started, throttledLogger: throttledLogger, @@ -45,7 +45,7 @@ func (k *KubernetesDriver) waitPodRunning(ctx context.Context, id string) (*core }, ) - return pod, err + return err } type podStatusParams struct { @@ -55,7 +55,6 @@ type podStatusParams struct { } func (k *KubernetesDriver) evaluatePodStatus( - ctx context.Context, pod *corev1.Pod, params *podStatusParams, ) (bool, error) { @@ -83,7 +82,7 @@ func (k *KubernetesDriver) evaluatePodStatus( return false, err } - return k.checkContainerStatuses(ctx, pod, params.id, params.throttledLogger) + return k.checkContainerStatuses(pod, params.id, params.throttledLogger) } func podConditionMessage(pod *corev1.Pod, started time.Time) string { @@ -202,13 +201,12 @@ func initContainerReady( } func (k *KubernetesDriver) checkContainerStatuses( - ctx context.Context, pod *corev1.Pod, id string, throttledLogger *throttledlogger.ThrottledLogger, ) (bool, error) { for _, c := range pod.Status.ContainerStatuses { - proceed, err := k.checkContainerStatus(ctx, id, &c, throttledLogger) + proceed, err := k.checkContainerStatus(id, &c, throttledLogger) if err != nil || !proceed { return false, err } @@ -218,7 +216,6 @@ func (k *KubernetesDriver) checkContainerStatuses( } func (k *KubernetesDriver) checkContainerStatus( - ctx context.Context, id string, c *corev1.ContainerStatus, throttledLogger *throttledlogger.ThrottledLogger, diff --git a/pkg/driver/microsandbox/microsandbox.go b/pkg/driver/microsandbox/microsandbox.go index c58ee0e68d..11f54ed279 100644 --- a/pkg/driver/microsandbox/microsandbox.go +++ b/pkg/driver/microsandbox/microsandbox.go @@ -342,119 +342,6 @@ func (d *microsandboxDriver) ProvisioningPreflight(ctx context.Context) error { return nil } -func (d *microsandboxDriver) runFromOptions( - ctx context.Context, - workspaceID string, - options *driver.RunOptions, - hostReqs *config.HostRequirements, -) error { - if options == nil { - return fmt.Errorf( - "microsandbox driver requires run options (in-place reprovision is unsupported)", - ) - } - if options.Image == "" { - return fmt.Errorf("microsandbox driver requires an image to run") - } - if err := d.ProvisioningPreflight(ctx); err != nil { - return err - } - if options.AllowRecreate { - if err := d.recreationPreflight(ctx); err != nil { - return err - } - } - prepared, err := d.client.PrepareImage(ctx, options.Image, options.ImageBuilt) - if err != nil { - return fmt.Errorf("prepare microsandbox final image: %w", err) - } - defer prepared.cleanup() - owner, err := d.resolveWorkspaceOwner(ctx, options, prepared.image) - if err != nil { - return err - } - warnUnsupportedOptions(options) - if err := d.client.EnsureImage(ctx, prepared); err != nil { - return fmt.Errorf("ensure microsandbox final image: %w", err) - } - info, err := d.client.Find(ctx, sandboxName(workspaceID)) - if err != nil { - return err - } - if info != nil && !options.AllowRecreate { - return fmt.Errorf( - "microsandbox VM already exists; back up VM-local data and rerun with --recreate: " + - "replacement discards the VM root disk and cannot roll back after removal", - ) - } - spec := d.buildSpec(workspaceID, options, hostReqs, owner) - spec.AllowRecreate = options.AllowRecreate - spec.Image = prepared.reference - if err := d.client.PrepareVolumes(ctx, spec.Mounts); err != nil { - return fmt.Errorf("prepare microsandbox volumes: %w", err) - } - if err := d.client.Create( - ctx, - sandboxName(workspaceID), - spec, - ); err != nil { - return fmt.Errorf("create microsandbox VM: %w", err) - } - return nil -} - -// buildSpec resolves sizing from, in priority order, the operator-configured -// MICROSANDBOX_* defaults, then the devcontainer's hostRequirements, falling -// back to the microsandbox runtime default (zero) when neither is set. -func (d *microsandboxDriver) buildSpec( - workspaceID string, - options *driver.RunOptions, - hostReqs *config.HostRequirements, - owner *mountOwner, -) sandboxSpec { - labels := config.ListToObject(options.Labels) - if labels == nil { - labels = map[string]string{} - } - maps.Copy(labels, config.ListToObject(config.GetIDLabels(workspaceID, d.idLabels))) - labels[workspaceMountContractLabel] = d.workspaceMountContract() - if owner != nil { - labels[workspaceRemoteUserLabel] = workspaceRemoteUser(options) - } - if options.User != "" { - labels[userLabel] = options.User - } - memory := d.defaults.memory - if memory == 0 { - memory = hostRequirementMemoryMiB(hostReqs) - } - cpus := d.defaults.cpus - if cpus == 0 { - cpus = hostRequirementCPUs(hostReqs) - } - rootDiskGB := d.defaults.rootDiskGB - if rootDiskGB == 0 { - rootDiskGB = hostRequirementStorageGB(hostReqs) - } - return sandboxSpec{ - Image: options.Image, - User: options.User, - Entrypoint: options.Entrypoint, - Cmd: options.Cmd, - Memory: memory, - CPUs: cpus, - Env: options.Env, - Labels: labels, - Ephemeral: d.defaults.ephemeral, - IdleTimeout: d.defaults.idleTimeout, - Mounts: d.volumeMounts(options, owner), - MaxMemory: d.defaults.maxMemory, - MaxCPUs: d.defaults.maxCPUs, - BlockEgress: d.defaults.blockEgress, - RootDiskGB: rootDiskGB, - } -} - func warnUnsupportedOptions(options *driver.RunOptions) { var ignored []string if isPrivileged(options) { @@ -646,6 +533,158 @@ func (d *microsandboxDriver) RequiresRecreate( return false, "" } +// createVM prepares the volumes and creates the VM from the prepared image. +// vmCreate carries what createVM needs to build the sandbox. +type vmCreate struct { + workspaceID string + options *driver.RunOptions + hostReqs *config.HostRequirements + owner *mountOwner + prepared *preparedImage +} + +func (d *microsandboxDriver) createVM(ctx context.Context, req vmCreate) error { + workspaceID, options, prepared := req.workspaceID, req.options, req.prepared + spec := d.buildSpec(workspaceID, options, req.hostReqs, req.owner) + spec.AllowRecreate = options.AllowRecreate + spec.Image = prepared.reference + if err := d.client.PrepareVolumes(ctx, spec.Mounts); err != nil { + return fmt.Errorf("prepare microsandbox volumes: %w", err) + } + if err := d.client.Create(ctx, sandboxName(workspaceID), spec); err != nil { + return fmt.Errorf("create microsandbox VM: %w", err) + } + return nil +} + +// rejectExistingVM refuses to replace an existing VM unless recreation was requested. +func (d *microsandboxDriver) rejectExistingVM( + ctx context.Context, + workspaceID string, + options *driver.RunOptions, +) error { + info, err := d.client.Find(ctx, sandboxName(workspaceID)) + if err != nil { + return err + } + if info != nil && !options.AllowRecreate { + return fmt.Errorf( + "microsandbox VM already exists; back up VM-local data and rerun with --recreate: " + + "replacement discards the VM root disk and cannot roll back after removal", + ) + } + return nil +} + +// checkRunPreconditions rejects run options this driver cannot honour and runs +// the provisioning and recreation preflights. +func (d *microsandboxDriver) checkRunPreconditions( + ctx context.Context, + options *driver.RunOptions, +) error { + if options == nil { + return fmt.Errorf( + "microsandbox driver requires run options (in-place reprovision is unsupported)", + ) + } + if options.Image == "" { + return fmt.Errorf("microsandbox driver requires an image to run") + } + if err := d.ProvisioningPreflight(ctx); err != nil { + return err + } + if options.AllowRecreate { + return d.recreationPreflight(ctx) + } + return nil +} + +func (d *microsandboxDriver) runFromOptions( + ctx context.Context, + workspaceID string, + options *driver.RunOptions, + hostReqs *config.HostRequirements, +) error { + if err := d.checkRunPreconditions(ctx, options); err != nil { + return err + } + prepared, err := d.client.PrepareImage(ctx, options.Image, options.ImageBuilt) + if err != nil { + return fmt.Errorf("prepare microsandbox final image: %w", err) + } + defer prepared.cleanup() + owner, err := d.resolveWorkspaceOwner(ctx, options, prepared.image) + if err != nil { + return err + } + warnUnsupportedOptions(options) + if err := d.client.EnsureImage(ctx, prepared); err != nil { + return fmt.Errorf("ensure microsandbox final image: %w", err) + } + if err := d.rejectExistingVM(ctx, workspaceID, options); err != nil { + return err + } + return d.createVM(ctx, vmCreate{ + workspaceID: workspaceID, + options: options, + hostReqs: hostReqs, + owner: owner, + prepared: prepared, + }) +} + +// buildSpec resolves sizing from, in priority order, the operator-configured +// MICROSANDBOX_* defaults, then the devcontainer's hostRequirements, falling +// back to the microsandbox runtime default (zero) when neither is set. +func (d *microsandboxDriver) buildSpec( + workspaceID string, + options *driver.RunOptions, + hostReqs *config.HostRequirements, + owner *mountOwner, +) sandboxSpec { + labels := config.ListToObject(options.Labels) + if labels == nil { + labels = map[string]string{} + } + maps.Copy(labels, config.ListToObject(config.GetIDLabels(workspaceID, d.idLabels))) + labels[workspaceMountContractLabel] = d.workspaceMountContract() + if owner != nil { + labels[workspaceRemoteUserLabel] = workspaceRemoteUser(options) + } + if options.User != "" { + labels[userLabel] = options.User + } + memory := d.defaults.memory + if memory == 0 { + memory = hostRequirementMemoryMiB(hostReqs) + } + cpus := d.defaults.cpus + if cpus == 0 { + cpus = hostRequirementCPUs(hostReqs) + } + rootDiskGB := d.defaults.rootDiskGB + if rootDiskGB == 0 { + rootDiskGB = hostRequirementStorageGB(hostReqs) + } + return sandboxSpec{ + Image: options.Image, + User: options.User, + Entrypoint: options.Entrypoint, + Cmd: options.Cmd, + Memory: memory, + CPUs: cpus, + Env: options.Env, + Labels: labels, + Ephemeral: d.defaults.ephemeral, + IdleTimeout: d.defaults.idleTimeout, + Mounts: d.volumeMounts(options, owner), + MaxMemory: d.defaults.maxMemory, + MaxCPUs: d.defaults.maxCPUs, + BlockEgress: d.defaults.blockEgress, + RootDiskGB: rootDiskGB, + } +} + func (d *microsandboxDriver) workspaceMountContract() string { owner := "none" if d.workspaceMountPolicy.StatVirtualization != statVirtOff { diff --git a/pkg/driver/types.go b/pkg/driver/types.go index afc94f4249..133e1b87f7 100644 --- a/pkg/driver/types.go +++ b/pkg/driver/types.go @@ -2,6 +2,7 @@ package driver import ( "context" + "errors" "io" "github.com/devsy-org/devsy/pkg/devcontainer/config" @@ -168,6 +169,32 @@ type CommandParams struct { // RawStdout preserves protocol bytes; text commands keep stdout redacted. // Protocol consumers must not log the unredacted stream. RawStdout bool + // RequireRunning makes the call exec-only: it must not start, unpause or + // restart anything, and returns ErrNotRunning when the workload is down. + // Only drivers that implement RequireRunningDriver honor it. + RequireRunning bool +} + +// ErrNotRunning is returned by an exec-only call when the workload is not +// running. +var ErrNotRunning = errors.New("workspace is not running") + +// RequireRunningDriver is implemented by drivers that enforce +// CommandParams.RequireRunning. Absence means the flag would be ignored, so +// callers must not use it. +type RequireRunningDriver interface { + Driver + + SupportsRequireRunning() bool +} + +// DriverSupportsRequireRunning reports whether d enforces exec-only calls. +func DriverSupportsRequireRunning(d Driver) bool { + if rr, ok := d.(RequireRunningDriver); ok { + return rr.SupportsRequireRunning() + } + + return false } // Streams bundles the standard IO streams for an exec. diff --git a/pkg/extract/extract_test.go b/pkg/extract/extract_test.go index 402408fd82..535d366888 100644 --- a/pkg/extract/extract_test.go +++ b/pkg/extract/extract_test.go @@ -11,6 +11,11 @@ import ( "testing" ) +const ( + testWorld = "world" + testEvilLink = "evil-link" +) + type tarEntry struct { name string body string @@ -90,7 +95,7 @@ func writeTarEntry(t *testing.T, tw *tar.Writer, e tarEntry) { func TestExtract_NormalArchive(t *testing.T) { t.Parallel() buf := newTarGz(t, []tarEntry{ - {name: "hello.txt", body: "world"}, + {name: "hello.txt", body: testWorld}, }) dest := t.TempDir() @@ -103,8 +108,8 @@ func TestExtract_NormalArchive(t *testing.T) { if err != nil { t.Fatalf("read extracted file: %v", err) } - if string(content) != "world" { - t.Fatalf("got %q, want %q", string(content), "world") + if string(content) != testWorld { + t.Fatalf("got %q, want %q", string(content), testWorld) } } @@ -128,7 +133,7 @@ func TestExtract_SymlinkTraversalBlocked(t *testing.T) { t.Parallel() buf := newTarGz(t, []tarEntry{ { - name: "evil-link", + name: testEvilLink, linkTarget: etcPasswd, symlink: true, }, @@ -148,7 +153,7 @@ func TestExtract_HardLinkTraversalBlocked(t *testing.T) { t.Parallel() buf := newTarGz(t, []tarEntry{ { - name: "evil-link", + name: testEvilLink, linkTarget: etcPasswd, symlink: false, }, @@ -173,7 +178,7 @@ func TestExtract_HardLinkResolvesRelativeToDestinationNotCWD(t *testing.T) { } buf := newTarGz(t, []tarEntry{ - {name: "evil-link", linkTarget: "sentinel.txt"}, + {name: testEvilLink, linkTarget: "sentinel.txt"}, }) dest := t.TempDir() @@ -196,7 +201,7 @@ func TestExtract_HardLinkResolvesRelativeToDestinationNotCWD(t *testing.T) { func TestExtract_HardLinkResolvesFromArchiveRootWhenNested(t *testing.T) { t.Parallel() buf := newTarGz(t, []tarEntry{ - {name: testHardLinkRel, body: "world"}, + {name: testHardLinkRel, body: testWorld}, {name: "dir/sub/link", linkTarget: testHardLinkRel}, }) @@ -210,8 +215,8 @@ func TestExtract_HardLinkResolvesFromArchiveRootWhenNested(t *testing.T) { if err != nil { t.Fatalf("read hard link: %v", err) } - if string(content) != "world" { - t.Fatalf("hard link content = %q, want %q", content, "world") + if string(content) != testWorld { + t.Fatalf("hard link content = %q, want %q", content, testWorld) } if _, err := os.Stat(filepath.Join(dest, "dir", "sub", "dir", "file")); !os.IsNotExist(err) { @@ -283,7 +288,7 @@ func TestExtract_PreserveHeaderOwnershipUnprivilegedDegrades(t *testing.T) { t.Skip("running as root: permission errors cannot occur") } buf := newTarGz(t, []tarEntry{ - {name: "hello.txt", body: "world", uid: 0, gid: 0}, + {name: "hello.txt", body: testWorld, uid: 0, gid: 0}, }) dest := t.TempDir() @@ -295,7 +300,7 @@ func TestExtract_PreserveHeaderOwnershipUnprivilegedDegrades(t *testing.T) { if err != nil { t.Fatalf("read extracted file: %v", err) } - if string(content) != "world" { - t.Fatalf("got %q, want %q", string(content), "world") + if string(content) != testWorld { + t.Fatalf("got %q, want %q", string(content), testWorld) } } diff --git a/pkg/extract/zip.go b/pkg/extract/zip.go index f3d84244e3..7ed0bdd107 100644 --- a/pkg/extract/zip.go +++ b/pkg/extract/zip.go @@ -43,16 +43,17 @@ func unzipFile(f *zip.File, destination string) error { } if f.FileInfo().IsDir() { - if err := os.MkdirAll(filePath, os.ModePerm); err != nil { + if err := os.MkdirAll(filePath, 0o750); err != nil { return err } return nil } - if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil { + if err := os.MkdirAll(filepath.Dir(filePath), 0o750); err != nil { return err } + //nolint:gosec // filePath is confined to destination by the prefix check above destinationFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) if err != nil { return err diff --git a/pkg/git/runner.go b/pkg/git/runner.go index 2a5b7a2e8d..bdfdbe8882 100644 --- a/pkg/git/runner.go +++ b/pkg/git/runner.go @@ -73,13 +73,7 @@ func (execRunner) Run(ctx context.Context, opts RunOptions) (RunResult, error) { binary = binGit } if !command.Exists(binary) { - if binary == binGit { - return RunResult{}, &CommandError{Args: opts.Args, ExitCode: -1, Err: ErrGitNotFound} - } - return RunResult{}, &CommandError{ - Args: opts.Args, ExitCode: -1, - Err: fmt.Errorf("%q binary not found in PATH", binary), - } + return RunResult{}, missingBinaryError(binary, opts.Args) } cmd := exec.CommandContext( @@ -96,47 +90,72 @@ func (execRunner) Run(ctx context.Context, opts RunOptions) (RunResult, error) { stdoutRedactor = nil } outBuf := subprocess.NewBuffer(stdoutRedactor) - var streamOut *subprocess.StreamingRedactingWriter - if opts.Stdout != nil { - streamOut = &subprocess.StreamingRedactingWriter{ - Next: opts.Stdout, - Redactor: stdoutRedactor, - } - cmd.Stdout = io.MultiWriter(streamOut, outBuf) - } else { - cmd.Stdout = outBuf - } + streamOut := attachStream(&cmd.Stdout, outBuf, opts.Stdout, stdoutRedactor) errBuf := subprocess.NewBuffer(redactor) - var streamErr *subprocess.StreamingRedactingWriter - if opts.Stderr != nil { - streamErr = &subprocess.StreamingRedactingWriter{Next: opts.Stderr, Redactor: redactor} - cmd.Stderr = io.MultiWriter(streamErr, errBuf) - } else { - cmd.Stderr = errBuf - } + streamErr := attachStream(&cmd.Stderr, errBuf, opts.Stderr, redactor) err := cmd.Run() result := RunResult{Stdout: []byte(outBuf.String()), Stderr: []byte(errBuf.String())} flushErr := errors.Join(flushStream(streamOut), flushStream(streamErr)) - if err == nil && flushErr != nil { - return result, fmt.Errorf("flush git output: %w", flushErr) + + return result, finishRun(opts.Args, errBuf.String(), err, flushErr) +} + +// finishRun returns the command failure, with any flush failure joined to it. +// A flush failure alone is still an error because output may be missing. +func finishRun(args []string, stderr string, runErr, flushErr error) error { + if runErr == nil { + if flushErr != nil { + return fmt.Errorf("flush git output: %w", flushErr) + } + + return nil } - if err != nil && flushErr != nil { - err = errors.Join(err, flushErr) + if flushErr != nil { + runErr = errors.Join(runErr, flushErr) } - if err != nil { - cmdErr := &CommandError{ - Args: opts.Args, - ExitCode: -1, - Stderr: strings.TrimSpace(errBuf.String()), - Err: err, - } - if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { - cmdErr.ExitCode = exitErr.ExitCode() - } - return result, cmdErr + + return runFailure(args, stderr, runErr) +} + +func missingBinaryError(binary string, args []string) error { + if binary == binGit { + return &CommandError{Args: args, ExitCode: -1, Err: ErrGitNotFound} + } + return &CommandError{ + Args: args, ExitCode: -1, + Err: fmt.Errorf("%q binary not found in PATH", binary), + } +} + +// attachStream points *dst at the buffer and, when a live writer is given, +// also at a redacting stream to it. It returns the stream to flush, or nil. +func attachStream( + dst *io.Writer, + buf *subprocess.Buffer, + live io.Writer, + redactor *secrets.Redactor, +) *subprocess.StreamingRedactingWriter { + if live == nil { + *dst = buf + return nil + } + stream := &subprocess.StreamingRedactingWriter{Next: live, Redactor: redactor} + *dst = io.MultiWriter(stream, buf) + return stream +} + +func runFailure(args []string, stderr string, err error) *CommandError { + cmdErr := &CommandError{ + Args: args, + ExitCode: -1, + Stderr: strings.TrimSpace(stderr), + Err: err, + } + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { + cmdErr.ExitCode = exitErr.ExitCode() } - return result, nil + return cmdErr } func flushStream(stream *subprocess.StreamingRedactingWriter) error { diff --git a/pkg/gitsshsigning/helper.go b/pkg/gitsshsigning/helper.go index cbf1c2314e..638fd9beaf 100644 --- a/pkg/gitsshsigning/helper.go +++ b/pkg/gitsshsigning/helper.go @@ -87,7 +87,7 @@ func RemoveHelper(userName string) error { func createHelperScript() error { // we do it this way instead of os.Create because we need sudo - cmd := exec.Command( + cmd := exec.Command( //nolint:gosec // signing program comes from git config by design "sudo", "bash", "-c", @@ -139,7 +139,7 @@ func formatGitConfigValue(val string) string { } func readGitConfig(gitConfigPath string) (string, error) { - out, err := os.ReadFile(gitConfigPath) + out, err := os.ReadFile(gitConfigPath) //nolint:gosec // path is the user git config if err != nil && !os.IsNotExist(err) { return "", err } diff --git a/pkg/gitsshsigning/helper_test.go b/pkg/gitsshsigning/helper_test.go index 882e3b43f5..9e0208d7c6 100644 --- a/pkg/gitsshsigning/helper_test.go +++ b/pkg/gitsshsigning/helper_test.go @@ -12,6 +12,19 @@ import ( "github.com/stretchr/testify/suite" ) +const ( + userValue = "[user]" + gpg = "[gpg]" + commit = "[commit]" +) + +const ( + cfgUserName = "\tname = Test User" + cfgSignProgram = "\tprogram = devsy-ssh-signature" + cfgFormatSSH = "\tformat = ssh" + cfgGPGSign = "\tgpgsign = true" +) + type HelperTestSuite struct { suite.Suite } @@ -22,28 +35,28 @@ func TestHelperSuite(t *testing.T) { func (s *HelperTestSuite) TestRemoveSignatureHelper_PreservesUnrelatedGpgConfig() { input := strings.Join([]string{ - "[user]", "\tname = Test User", "\temail = test@example.com", - `[gpg "ssh"]`, "\tprogram = devsy-ssh-signature", - "[gpg]", "\tformat = ssh", "\tprogram = /usr/bin/gpg2", - "[commit]", "\tgpgsign = true", - "[user]", "\tsigningkey = /path/to/key", + userValue, cfgUserName, "\temail = test@example.com", + sectionHeaderGpgSSH, cfgSignProgram, + gpg, cfgFormatSSH, "\tprogram = /usr/bin/gpg2", + commit, cfgGPGSign, + userValue, "\tsigningkey = /path/to/key", }, "\n") result := removeSignatureHelper(input) assert.NotContains(s.T(), result, "devsy-ssh-signature") - assert.Contains(s.T(), result, "[user]") - assert.Contains(s.T(), result, "[commit]") + assert.Contains(s.T(), result, userValue) + assert.Contains(s.T(), result, commit) assert.Contains(s.T(), result, "program = /usr/bin/gpg2") assert.NotContains(s.T(), result, "format = ssh") } func (s *HelperTestSuite) TestRemoveSignatureHelper_RemovesDevsySections() { input := strings.Join([]string{ - "[user]", "\tname = Test User", - `[gpg "ssh"]`, "\tprogram = devsy-ssh-signature", - "[gpg]", "\tformat = ssh", - "[user]", "\tsigningkey = /path/to/key", + userValue, cfgUserName, + sectionHeaderGpgSSH, cfgSignProgram, + gpg, cfgFormatSSH, + userValue, "\tsigningkey = /path/to/key", }, "\n") result := removeSignatureHelper(input) @@ -63,30 +76,30 @@ func (s *HelperTestSuite) TestRemoveSignatureHelper_NoGpgSections() { func (s *HelperTestSuite) TestRemoveSignatureHelper_PreservesUserOwnedGpgSSHKeys() { input := strings.Join([]string{ - "[user]", "\tname = Test User", - `[gpg "ssh"]`, "\tprogram = devsy-ssh-signature", + userValue, cfgUserName, + sectionHeaderGpgSSH, cfgSignProgram, "\tallowedSignersFile = ~/.ssh/allowed_signers", - "[commit]", "\tgpgsign = true", + commit, cfgGPGSign, }, "\n") result := removeSignatureHelper(input) assert.NotContains(s.T(), result, "devsy-ssh-signature") - assert.Contains(s.T(), result, `[gpg "ssh"]`, + assert.Contains(s.T(), result, sectionHeaderGpgSSH, "section header should be preserved when user keys remain") assert.Contains(s.T(), result, "allowedSignersFile", "user-owned key should be preserved") - assert.Contains(s.T(), result, "[commit]") + assert.Contains(s.T(), result, commit) } func (s *HelperTestSuite) TestRemoveSignatureHelper_PreservesUserOwnedSigningKey() { // A [user] section with name + signingkey is user-owned; only the // devsy-appended [user] section (signingkey-only) should be dropped. input := strings.Join([]string{ - "[user]", "\tname = Test User", "\tsigningkey = /my/gpg-key", - `[gpg "ssh"]`, "\tprogram = devsy-ssh-signature", - "[gpg]", "\tformat = ssh", - "[user]", "\tsigningkey = /devsy/injected-key", + userValue, cfgUserName, "\tsigningkey = /my/gpg-key", + sectionHeaderGpgSSH, cfgSignProgram, + gpg, cfgFormatSSH, + userValue, "\tsigningkey = /devsy/injected-key", }, "\n") result := removeSignatureHelper(input) @@ -143,17 +156,17 @@ func TestUpdateGitConfig_DifferentKey(t *testing.T) { func (s *HelperTestSuite) TestRemoveSignatureHelper_DropsEmptyGpgSSHSection() { input := strings.Join([]string{ - "[user]", "\tname = Test User", - `[gpg "ssh"]`, "\tprogram = devsy-ssh-signature", - "[commit]", "\tgpgsign = true", + userValue, cfgUserName, + sectionHeaderGpgSSH, cfgSignProgram, + commit, cfgGPGSign, }, "\n") result := removeSignatureHelper(input) assert.NotContains(s.T(), result, "devsy-ssh-signature") - assert.NotContains(s.T(), result, `[gpg "ssh"]`, + assert.NotContains(s.T(), result, sectionHeaderGpgSSH, "empty section should be dropped entirely") - assert.Contains(s.T(), result, "[commit]") + assert.Contains(s.T(), result, commit) } func TestUpdateGitConfig_WindowsBackslashPathQuotingAndCleanRemoval(t *testing.T) { diff --git a/pkg/gpg/gpg_forwarding.go b/pkg/gpg/gpg_forwarding.go index 94014a46da..e0a73295c5 100644 --- a/pkg/gpg/gpg_forwarding.go +++ b/pkg/gpg/gpg_forwarding.go @@ -192,6 +192,7 @@ func (g *GPGConf) SetupRemoteSocketLink(ctx context.Context) error { if err := os.MkdirAll(filepath.Dir(link), 0o700); err != nil { return err } + //nolint:gosec // path is inside the GPG forwarding directory _ = os.Remove(link) if err := os.Symlink(g.SocketPath, link); err != nil { return err diff --git a/pkg/ide/fleet/fleet.go b/pkg/ide/fleet/fleet.go index e8b85a9d88..203da4456a 100644 --- a/pkg/ide/fleet/fleet.go +++ b/pkg/ide/fleet/fleet.go @@ -29,6 +29,9 @@ const ( DownloadArm64Option = "DOWNLOAD_ARM64" ) +const fleetDownloadBase = "https://download.jetbrains.com/product?" + + "code=FLL&release.type=preview&release.type=eap&platform=" + var fleetVersionRegex = regexp.MustCompile(`^[a-zA-Z0-9._-]+$`) var Options = ide.Options{ @@ -40,12 +43,12 @@ var Options = ide.Options{ DownloadArm64Option: { Name: DownloadArm64Option, Description: "The download url for the arm64 install script", - Default: "https://download.jetbrains.com/product?code=FLL&release.type=preview&release.type=eap&platform=linux_aarch64", + Default: fleetDownloadBase + "linux_aarch64", }, DownloadAmd64Option: { Name: DownloadAmd64Option, Description: "The download url for the amd64 install script", - Default: "https://download.jetbrains.com/product?code=FLL&release.type=preview&release.type=eap&platform=linux_x64", + Default: fleetDownloadBase + "linux_x64", }, } diff --git a/pkg/ide/jetbrains/clion.go b/pkg/ide/jetbrains/clion.go index 39d22d75ba..5ed39c5bd9 100644 --- a/pkg/ide/jetbrains/clion.go +++ b/pkg/ide/jetbrains/clion.go @@ -20,8 +20,7 @@ func NewCLionServer( CLionOptions, values, CLionProductCode, - CLionDownloadAmd64Template, - CLionDownloadArm64Template, + downloadTemplates{amd64: CLionDownloadAmd64Template, arm64: CLionDownloadArm64Template}, ) return newGenericServer(userName, &GenericOptions{ ID: "clion", diff --git a/pkg/ide/jetbrains/dataspell.go b/pkg/ide/jetbrains/dataspell.go index a62624d505..ef2e7734f6 100644 --- a/pkg/ide/jetbrains/dataspell.go +++ b/pkg/ide/jetbrains/dataspell.go @@ -20,8 +20,10 @@ func NewDataSpellServer( DataSpellOptions, values, DataSpellProductCode, - DataSpellDownloadAmd64Template, - DataSpellDownloadArm64Template, + downloadTemplates{ + amd64: DataSpellDownloadAmd64Template, + arm64: DataSpellDownloadArm64Template, + }, ) return newGenericServer(userName, &GenericOptions{ ID: "dataspell", diff --git a/pkg/ide/jetbrains/generic.go b/pkg/ide/jetbrains/generic.go index c96b387b14..950865f62b 100644 --- a/pkg/ide/jetbrains/generic.go +++ b/pkg/ide/jetbrains/generic.go @@ -56,12 +56,17 @@ func getLatestDownloadURL(code string, platform string) string { return fmt.Sprintf("https://download.jetbrains.com/product?code=%s&platform=%s", code, platform) } +// downloadTemplates are fmt templates for a versioned download URL. +type downloadTemplates struct { + amd64 string + arm64 string +} + func getDownloadURLs( options ide.Options, values map[string]config2.OptionValue, productCode string, - templateAmd64 string, - templateArm64 string, + templates downloadTemplates, ) (string, string) { version := options.GetValue(values, VersionOption) var amd64Download, arm64Download string @@ -71,11 +76,11 @@ func getDownloadURLs( } else { amd64Download = options.GetValue(values, DownloadAmd64Option) if amd64Download == "" { - amd64Download = fmt.Sprintf(templateAmd64, version) + amd64Download = fmt.Sprintf(templates.amd64, version) } arm64Download = options.GetValue(values, DownloadArm64Option) if arm64Download == "" { - arm64Download = fmt.Sprintf(templateArm64, version) + arm64Download = fmt.Sprintf(templates.arm64, version) } } @@ -188,7 +193,7 @@ func (o *GenericJetBrainsServer) installPlugins( log.Infof("installing JetBrains plugins: plugins=%v", plugins) - installPluginsCommand := exec.Command( + installPluginsCommand := exec.Command( //nolint:gosec // fixed JetBrains CLI with arguments built internally path.Join(targetLocation, "bin", "remote-dev-server.sh"), append( []string{"installPlugins", "--give-consent-to-use-third-party-plugins"}, @@ -230,6 +235,7 @@ func (o *GenericJetBrainsServer) getDirectory(baseFolder string) string { } func (o *GenericJetBrainsServer) extractArchive(fromPath string, toPath string) error { + //nolint:gosec // path is inside the JetBrains config directory file, err := os.Open(fromPath) if err != nil { return err @@ -240,7 +246,7 @@ func (o *GenericJetBrainsServer) extractArchive(fromPath string, toPath string) } func (o *GenericJetBrainsServer) download(targetFolder string) (string, error) { - err := os.MkdirAll(targetFolder, os.ModePerm) + err := os.MkdirAll(targetFolder, 0o750) if err != nil { return "", err } diff --git a/pkg/ide/jetbrains/generic_test.go b/pkg/ide/jetbrains/generic_test.go index 0b3c6afb3b..c4036e45a7 100644 --- a/pkg/ide/jetbrains/generic_test.go +++ b/pkg/ide/jetbrains/generic_test.go @@ -98,8 +98,10 @@ func TestGetDownloadURLs(t *testing.T) { GolandOptions, tt.values, GolandProductCode, - GolandDownloadAmd64Template, - GolandDownloadArm64Template, + downloadTemplates{ + amd64: GolandDownloadAmd64Template, + arm64: GolandDownloadArm64Template, + }, ) assert.Equal(t, tt.wantAmd64, amd64) assert.Equal(t, tt.wantArm64, arm64) diff --git a/pkg/ide/jetbrains/goland.go b/pkg/ide/jetbrains/goland.go index 9d0dc81fd7..7bc0f6f296 100644 --- a/pkg/ide/jetbrains/goland.go +++ b/pkg/ide/jetbrains/goland.go @@ -20,8 +20,7 @@ func NewGolandServer( GolandOptions, values, GolandProductCode, - GolandDownloadAmd64Template, - GolandDownloadArm64Template, + downloadTemplates{amd64: GolandDownloadAmd64Template, arm64: GolandDownloadArm64Template}, ) return newGenericServer(userName, &GenericOptions{ ID: "goland", diff --git a/pkg/ide/jetbrains/intellij.go b/pkg/ide/jetbrains/intellij.go index 302cb592bc..c1b4a1ddff 100644 --- a/pkg/ide/jetbrains/intellij.go +++ b/pkg/ide/jetbrains/intellij.go @@ -20,8 +20,10 @@ func NewIntellij( IntellijOptions, values, IntellijProductCode, - IntellijDownloadAmd64Template, - IntellijDownloadArm64Template, + downloadTemplates{ + amd64: IntellijDownloadAmd64Template, + arm64: IntellijDownloadArm64Template, + }, ) return newGenericServer(userName, &GenericOptions{ ID: "intellij", diff --git a/pkg/ide/jetbrains/phpstorm.go b/pkg/ide/jetbrains/phpstorm.go index f28a94d229..b483ffd65e 100644 --- a/pkg/ide/jetbrains/phpstorm.go +++ b/pkg/ide/jetbrains/phpstorm.go @@ -20,8 +20,10 @@ func NewPhpStorm( PhpStormOptions, values, PhpStormProductCode, - PhpStormDownloadAmd64Template, - PhpStormDownloadArm64Template, + downloadTemplates{ + amd64: PhpStormDownloadAmd64Template, + arm64: PhpStormDownloadArm64Template, + }, ) return newGenericServer(userName, &GenericOptions{ ID: "phpstorm", diff --git a/pkg/ide/jetbrains/pycharm.go b/pkg/ide/jetbrains/pycharm.go index 21d4176dc6..307b9f763d 100644 --- a/pkg/ide/jetbrains/pycharm.go +++ b/pkg/ide/jetbrains/pycharm.go @@ -20,8 +20,7 @@ func NewPyCharmServer( PyCharmOptions, values, PycharmProductCode, - PycharmDownloadAmd64Template, - PycharmDownloadArm64Template, + downloadTemplates{amd64: PycharmDownloadAmd64Template, arm64: PycharmDownloadArm64Template}, ) return newGenericServer(userName, &GenericOptions{ ID: "pycharm", diff --git a/pkg/ide/jetbrains/rider.go b/pkg/ide/jetbrains/rider.go index 1eab4a0e6c..6d583ccd4e 100644 --- a/pkg/ide/jetbrains/rider.go +++ b/pkg/ide/jetbrains/rider.go @@ -20,8 +20,7 @@ func NewRiderServer( RiderOptions, values, RiderProductCode, - RiderDownloadAmd64Template, - RiderDownloadArm64Template, + downloadTemplates{amd64: RiderDownloadAmd64Template, arm64: RiderDownloadArm64Template}, ) return newGenericServer(userName, &GenericOptions{ ID: "rider", diff --git a/pkg/ide/jetbrains/rubymine.go b/pkg/ide/jetbrains/rubymine.go index e62d949c56..abf12c5812 100644 --- a/pkg/ide/jetbrains/rubymine.go +++ b/pkg/ide/jetbrains/rubymine.go @@ -20,8 +20,10 @@ func NewRubyMineServer( RubyMineOptions, values, RubyMineProductCode, - RubyMineDownloadAmd64Template, - RubyMineDownloadArm64Template, + downloadTemplates{ + amd64: RubyMineDownloadAmd64Template, + arm64: RubyMineDownloadArm64Template, + }, ) return newGenericServer(userName, &GenericOptions{ ID: "rubymine", diff --git a/pkg/ide/jetbrains/rustrover.go b/pkg/ide/jetbrains/rustrover.go index a8d5161383..8aea755c51 100644 --- a/pkg/ide/jetbrains/rustrover.go +++ b/pkg/ide/jetbrains/rustrover.go @@ -20,8 +20,10 @@ func NewRustRoverServer( RustRoverOptions, values, RustRoverProductCode, - RustRoverDownloadAmd64Template, - RustRoverDownloadArm64Template, + downloadTemplates{ + amd64: RustRoverDownloadAmd64Template, + arm64: RustRoverDownloadArm64Template, + }, ) return newGenericServer(userName, &GenericOptions{ ID: "rustrover", diff --git a/pkg/ide/jetbrains/webstorm.go b/pkg/ide/jetbrains/webstorm.go index e359097e69..8833a49377 100644 --- a/pkg/ide/jetbrains/webstorm.go +++ b/pkg/ide/jetbrains/webstorm.go @@ -20,8 +20,10 @@ func NewWebStormServer( WebStormOptions, values, WebStormProductCode, - WebStormDownloadAmd64Template, - WebStormDownloadArm64Template, + downloadTemplates{ + amd64: WebStormDownloadAmd64Template, + arm64: WebStormDownloadArm64Template, + }, ) return newGenericServer(userName, &GenericOptions{ ID: "webstorm", diff --git a/pkg/ide/opener/browser_tunnel_state_unix_test.go b/pkg/ide/opener/browser_tunnel_state_unix_test.go index 5b4902f7b9..48eecb044d 100644 --- a/pkg/ide/opener/browser_tunnel_state_unix_test.go +++ b/pkg/ide/opener/browser_tunnel_state_unix_test.go @@ -10,6 +10,10 @@ import ( "time" ) +const ( + httpLocalhost10800 = "http://localhost:10800" +) + // spawnSleepHelper starts a long-running child process and registers cleanup // that kills + reaps it. Returns the spawned cmd. func spawnSleepHelper(t *testing.T) *exec.Cmd { @@ -188,7 +192,7 @@ func TestLoadLiveTunnelState_LiveMatch(t *testing.T) { want := TunnelState{ PID: pid, CreateTime: ct, - TargetURL: "http://localhost:10800", + TargetURL: httpLocalhost10800, Label: LabelVSCodeBrowser, } if err := WriteTunnelState("ctx", "ws", want); err != nil { @@ -248,7 +252,7 @@ func TestTryReuseExistingTunnel_DeadHelperReturnsFalse(t *testing.T) { state := TunnelState{ PID: 1, CreateTime: 1, - TargetURL: "http://localhost:10800", + TargetURL: httpLocalhost10800, Label: LabelVSCodeBrowser, } if err := WriteTunnelState("ctx", "ws", state); err != nil { diff --git a/pkg/ide/opener/browser_tunnel_test.go b/pkg/ide/opener/browser_tunnel_test.go index 81e410012d..6a35607ce0 100644 --- a/pkg/ide/opener/browser_tunnel_test.go +++ b/pkg/ide/opener/browser_tunnel_test.go @@ -16,6 +16,10 @@ import ( "github.com/stretchr/testify/assert" ) +const ( + httpLocalhost1234 = "http://localhost:1234" +) + // containsAdjacent reports whether flag appears immediately followed by // value in args, e.g. ["--user", "vscode"]. func containsAdjacent(args []string, flag, value string) bool { @@ -67,7 +71,7 @@ func TestBuildHelperArgs_Basic(t *testing.T) { func TestBuildHelperArgs_ForwardPorts(t *testing.T) { args := buildHelperArgs("ctx", "ws", tunnel.BrowserTunnelParams{ - TargetURL: "http://localhost:1234", + TargetURL: httpLocalhost1234, ForwardPorts: true, }, false) if !containsArg(args, "--forward-ports") { @@ -77,7 +81,7 @@ func TestBuildHelperArgs_ForwardPorts(t *testing.T) { func TestBuildHelperArgs_ExtraPorts(t *testing.T) { args := buildHelperArgs("ctx", "ws", tunnel.BrowserTunnelParams{ - TargetURL: "http://localhost:1234", + TargetURL: httpLocalhost1234, ExtraPorts: []string{"localhost:10800", "127.0.0.1:8443"}, }, false) if !containsAdjacent(args, "--extra-ports", "localhost:10800") { @@ -92,14 +96,14 @@ func TestBuildHelperArgs_ExtraPorts(t *testing.T) { // emitted iff openBrowser=true. func TestBuildHelperArgs_OpenBrowser(t *testing.T) { withFlag := buildHelperArgs("ctx", "ws", tunnel.BrowserTunnelParams{ - TargetURL: "http://localhost:1234", + TargetURL: httpLocalhost1234, }, true) if !containsArg(withFlag, "--open-browser") { t.Errorf("expected --open-browser in %v", withFlag) } withoutFlag := buildHelperArgs("ctx", "ws", tunnel.BrowserTunnelParams{ - TargetURL: "http://localhost:1234", + TargetURL: httpLocalhost1234, }, false) if containsArg(withoutFlag, "--open-browser") { t.Errorf("did not expect --open-browser in %v", withoutFlag) @@ -108,7 +112,7 @@ func TestBuildHelperArgs_OpenBrowser(t *testing.T) { func TestBuildHelperArgs_GPGAgentForwarding(t *testing.T) { withFlag := buildHelperArgs("ctx", "ws", tunnel.BrowserTunnelParams{ - TargetURL: "http://localhost:1234", + TargetURL: httpLocalhost1234, GPGAgentForwarding: true, }, false) if !containsArg(withFlag, "--ssh-gpg-forwarding=true") { @@ -116,7 +120,7 @@ func TestBuildHelperArgs_GPGAgentForwarding(t *testing.T) { } withoutFlag := buildHelperArgs("ctx", "ws", tunnel.BrowserTunnelParams{ - TargetURL: "http://localhost:1234", + TargetURL: httpLocalhost1234, GPGAgentForwarding: false, }, false) if containsArg(withoutFlag, "--ssh-gpg-forwarding=true") { @@ -127,7 +131,7 @@ func TestBuildHelperArgs_GPGAgentForwarding(t *testing.T) { func TestBuildHelperArgs_IncludesResolvedUser(t *testing.T) { args := buildHelperArgs("default", "my-workspace", tunnel.BrowserTunnelParams{ User: "vscode", - TargetURL: "http://localhost:1234", + TargetURL: httpLocalhost1234, }, false) joined := strings.Join(args, " ") @@ -151,7 +155,7 @@ func TestWriteReadTunnelState_RoundTrip(t *testing.T) { want := TunnelState{ PID: 12345, CreateTime: 67890, - TargetURL: "http://localhost:10800", + TargetURL: httpLocalhost10800, Label: LabelVSCodeBrowser, } if err := WriteTunnelState("ctx-test", "ws-test", want); err != nil { diff --git a/pkg/ide/opener/opener.go b/pkg/ide/opener/opener.go index 7e074819b1..835a00c3c4 100644 --- a/pkg/ide/opener/opener.go +++ b/pkg/ide/opener/opener.go @@ -147,11 +147,11 @@ func openDesktopIDE( return "", openJetBrains(ideName, ideOptions, params) case string(config.IDEZed): - return "", zed.Open( - ctx, ideOptions, params.User, - params.Result.SubstitutionContext.ContainerWorkspaceFolder, - params.Client.Workspace(), - ) + return "", zed.Open(ctx, ideOptions, zed.Target{ + UserName: params.User, + WorkspaceFolder: params.Result.SubstitutionContext.ContainerWorkspaceFolder, + WorkspaceID: params.Client.Workspace(), + }) default: return "", nil diff --git a/pkg/ide/openvscode/openvscode.go b/pkg/ide/openvscode/openvscode.go index 445a5f1596..aa29e133f4 100644 --- a/pkg/ide/openvscode/openvscode.go +++ b/pkg/ide/openvscode/openvscode.go @@ -20,8 +20,10 @@ import ( ) const ( - DownloadAmd64Template = "https://github.com/gitpod-io/openvscode-server/releases/download/openvscode-server-%s/openvscode-server-%s-linux-x64.tar.gz" - DownloadArm64Template = "https://github.com/gitpod-io/openvscode-server/releases/download/openvscode-server-%s/openvscode-server-%s-linux-arm64.tar.gz" + DownloadAmd64Template = "https://github.com/gitpod-io/openvscode-server/releases/download/" + + "openvscode-server-%s/openvscode-server-%s-linux-x64.tar.gz" + DownloadArm64Template = "https://github.com/gitpod-io/openvscode-server/releases/download/" + + "openvscode-server-%s/openvscode-server-%s-linux-arm64.tar.gz" ) const ( @@ -81,13 +83,19 @@ type OpenVSCodeServer struct { port string } -func NewOpenVSCodeServer( - extensions []string, - settings string, - userName string, - host, port string, - values map[string]config.OptionValue, -) *OpenVSCodeServer { +// ServerConfig describes the code-server to create. +type ServerConfig struct { + Extensions []string + Settings string + UserName string + Host string + Port string + Values map[string]config.OptionValue +} + +func NewOpenVSCodeServer(cfg ServerConfig) *OpenVSCodeServer { + extensions, settings, userName := cfg.Extensions, cfg.Settings, cfg.UserName + host, port, values := cfg.Host, cfg.Port, cfg.Values return &OpenVSCodeServer{ values: values, extensions: extensions, diff --git a/pkg/ide/openvscode/openvscode_test.go b/pkg/ide/openvscode/openvscode_test.go index 870ebbd487..dcce03c87a 100644 --- a/pkg/ide/openvscode/openvscode_test.go +++ b/pkg/ide/openvscode/openvscode_test.go @@ -9,7 +9,7 @@ import ( ) func TestGetReleaseURLDefaultVersion(t *testing.T) { - o := NewOpenVSCodeServer(nil, "", "", "", "", nil) + o := NewOpenVSCodeServer(ServerConfig{}) url := o.getReleaseUrl() wantVersion := Options[VersionOption].Default @@ -33,9 +33,9 @@ func TestGetReleaseURLDefaultVersion(t *testing.T) { } func TestGetReleaseURLVersionOverride(t *testing.T) { - o := NewOpenVSCodeServer(nil, "", "", "", "", map[string]config.OptionValue{ + o := NewOpenVSCodeServer(ServerConfig{Values: map[string]config.OptionValue{ VersionOption: {Value: "v1.99.0"}, - }) + }}) url := o.getReleaseUrl() if !strings.Contains(url, "v1.99.0") { t.Fatalf("expected url to honor VERSION override, got %q", url) @@ -48,9 +48,9 @@ func TestGetReleaseURLDownloadOverride(t *testing.T) { if runtime.GOARCH == "arm64" { opt = DownloadArm64Option } - o := NewOpenVSCodeServer(nil, "", "", "", "", map[string]config.OptionValue{ + o := NewOpenVSCodeServer(ServerConfig{Values: map[string]config.OptionValue{ opt: {Value: custom}, - }) + }}) if got := o.getReleaseUrl(); got != custom { t.Fatalf("expected explicit download url %q, got %q", custom, got) } diff --git a/pkg/ide/rstudio/rstudio.go b/pkg/ide/rstudio/rstudio.go index 4506f2ef22..fb7f13aef1 100644 --- a/pkg/ide/rstudio/rstudio.go +++ b/pkg/ide/rstudio/rstudio.go @@ -215,7 +215,8 @@ func downloadRStudioDeb(ubuntuCodename string) (string, error) { } func getDownloadURL(version, ubuntuCodename, architecture string) string { - return "https://rstudio.org/download/latest/" + version + "/server/" + ubuntuCodename + "/rstudio-server-latest-" + architecture + ".deb" + return "https://rstudio.org/download/latest/" + version + "/server/" + ubuntuCodename + + "/rstudio-server-latest-" + architecture + ".deb" } func download(targetFolder, downloadURL string) (string, error) { @@ -242,6 +243,7 @@ func download(targetFolder, downloadURL string) (string, error) { func installDeb(debPath string) error { log.Info("Installing deb") + //nolint:gosec // debPath is the file this process just downloaded into its own install folder out, err := exec.Command("gdebi", "--non-interactive", debPath).CombinedOutput() if err != nil { return fmt.Errorf("install deb: %w: %s", err, string(out)) @@ -270,7 +272,7 @@ func installDeb(debPath string) error { } func ensureConfigFolder(userName string) error { - err := os.MkdirAll(dataFolder, os.ModePerm) + err := os.MkdirAll(dataFolder, 0o750) if err != nil { return err } @@ -288,7 +290,8 @@ func setupSingleUserMode(configFolder, userName string) error { dbConf := fmt.Sprintf(`provider=sqlite directory=%s`, configFolder) dbConfPath := filepath.Join(configFolder, "dbconf.conf") - err := os.WriteFile(dbConfPath, []byte(dbConf), os.ModePerm) + //nolint:gosec // the server reads this config after dropping privileges to server-user + err := os.WriteFile(dbConfPath, []byte(dbConf), 0o644) if err != nil { return fmt.Errorf("save db conf: %w", err) } @@ -309,7 +312,8 @@ database-config-file=%s/dbconf.conf serverConfPath := filepath.Join(rstudioConfigFolder, "rserver.conf") // The RStudio installer automatically creates an empty file at destConfPath, let's try to remove that first _ = os.Remove(serverConfPath) - err = os.WriteFile(serverConfPath, []byte(rServerConf), os.ModePerm) + //nolint:gosec // the server reads this config after dropping privileges to server-user + err = os.WriteFile(serverConfPath, []byte(rServerConf), 0o644) if err != nil { return fmt.Errorf("save rserver conf: %w", err) } @@ -335,7 +339,7 @@ func setupPreferences(workspaceFolder, userName string) error { } prefsPath := filepath.Join(prefsDir, preferencesFile) - err = os.WriteFile(prefsPath, outPrefs, os.ModePerm) + err = os.WriteFile(prefsPath, outPrefs, 0o600) if err != nil { return fmt.Errorf("save preferences: %w", err) } diff --git a/pkg/ide/vscode/apk.go b/pkg/ide/vscode/apk.go index 6a19dfa47b..1af577ece7 100644 --- a/pkg/ide/vscode/apk.go +++ b/pkg/ide/vscode/apk.go @@ -38,6 +38,7 @@ func InstallAPKRequirements() { dependencies = append(dependencies, "curl") } + //nolint:gosec // dependencies holds only fixed package names out, err := exec.Command("sh", "-c", "apk update && apk add "+strings.Join(dependencies, " ")). CombinedOutput() if err != nil { diff --git a/pkg/ide/vscode/open.go b/pkg/ide/vscode/open.go index 8faba7529f..066798b990 100644 --- a/pkg/ide/vscode/open.go +++ b/pkg/ide/vscode/open.go @@ -23,6 +23,8 @@ import ( const listExtensionsArg = "--list-extensions" +const remoteSSHExtension = "ms-vscode-remote.remote-ssh" + const containersExtension = "ms-vscode-remote.remote-containers" const remoteFolderScheme = "vscode-remote" @@ -48,25 +50,25 @@ var openConfigs = map[Flavor]openConfig{ scheme: "vscode", cliName: "code", macAppPath: "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code", - sshExtension: "ms-vscode-remote.remote-ssh", + sshExtension: remoteSSHExtension, }, FlavorInsiders: { scheme: "vscode-insiders", cliName: "code-insiders", macAppPath: "/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin/code", - sshExtension: "ms-vscode-remote.remote-ssh", + sshExtension: remoteSSHExtension, }, FlavorCursor: { scheme: "cursor", cliName: "cursor", macAppPath: "/Applications/Cursor.app/Contents/Resources/app/bin/cursor", - sshExtension: "ms-vscode-remote.remote-ssh", + sshExtension: remoteSSHExtension, }, FlavorPositron: { - scheme: "positron", - cliName: "positron", + scheme: string(FlavorPositron), + cliName: string(FlavorPositron), macAppPath: "/Applications/Positron.app/Contents/Resources/app/bin/positron", - sshExtension: "ms-vscode-remote.remote-ssh", + sshExtension: remoteSSHExtension, }, FlavorCodium: { scheme: "codium", @@ -78,13 +80,13 @@ var openConfigs = map[Flavor]openConfig{ scheme: "windsurf", cliName: "windsurf", macAppPath: "/Applications/Windsurf.app/Contents/Resources/app/bin/windsurf", - sshExtension: "ms-vscode-remote.remote-ssh", + sshExtension: remoteSSHExtension, }, FlavorAntigravity: { scheme: "antigravity", cliName: "agy", macAppPath: "/Applications/Antigravity.app/Contents/Resources/app/bin/agy", - sshExtension: "ms-vscode-remote.remote-ssh", + sshExtension: remoteSSHExtension, }, FlavorBob: { scheme: "bob", diff --git a/pkg/ide/vscode/vscode.go b/pkg/ide/vscode/vscode.go index d4804094bb..b29622dbdb 100644 --- a/pkg/ide/vscode/vscode.go +++ b/pkg/ide/vscode/vscode.go @@ -62,7 +62,7 @@ var flavorConfigs = map[Flavor]flavorConfig{ FlavorStable: {"VS Code", ".vscode-server", "code-server"}, FlavorInsiders: {"VS Code Insiders", ".vscode-server-insiders", "code-server-insiders"}, FlavorCursor: {"Cursor", ".cursor-server", "cursor-server"}, - FlavorPositron: {"positron", ".positron-server", "positron-server"}, + FlavorPositron: {string(FlavorPositron), ".positron-server", "positron-server"}, FlavorCodium: {"VSCodium", ".vscodium-server", "codium-server"}, FlavorWindsurf: {"Windsurf", ".windsurf-server", "windsurf-server"}, FlavorAntigravity: {"Antigravity", ".antigravity-server", "antigravity-server"}, @@ -216,8 +216,10 @@ func (o *VsCodeServer) buildExtensionCommand(binPath, extension string) *exec.Cm if o.userName != "" { cmd := shellescape.QuoteCommand(append([]string{binPath}, args...)) + //nolint:gosec // cmd is shell-quoted above return exec.Command("su", o.userName, "-c", cmd) } + //nolint:gosec // binPath is resolved from the server install location return exec.Command(binPath, args...) } diff --git a/pkg/ide/zed/target.go b/pkg/ide/zed/target.go new file mode 100644 index 0000000000..7d0bfac1db --- /dev/null +++ b/pkg/ide/zed/target.go @@ -0,0 +1,8 @@ +package zed + +// Target names the workspace Zed connects to over SSH. +type Target struct { + UserName string + WorkspaceFolder string + WorkspaceID string +} diff --git a/pkg/ide/zed/zed.go b/pkg/ide/zed/zed.go index d7c906fbec..cfd26a0c29 100644 --- a/pkg/ide/zed/zed.go +++ b/pkg/ide/zed/zed.go @@ -15,8 +15,9 @@ import ( func Open( ctx context.Context, values map[string]config.OptionValue, - userName, workspaceFolder, workspaceID string, + target Target, ) error { + workspaceFolder, workspaceID := target.WorkspaceFolder, target.WorkspaceID log.Info("Opening Zed editor") if len(workspaceFolder) == 0 || workspaceFolder[0] != '/' { diff --git a/pkg/ide/zed/zed_linux.go b/pkg/ide/zed/zed_linux.go index 0b2ec0cad2..621d605b9b 100644 --- a/pkg/ide/zed/zed_linux.go +++ b/pkg/ide/zed/zed_linux.go @@ -15,8 +15,9 @@ import ( func Open( ctx context.Context, values map[string]config.OptionValue, - userName, workspaceFolder, workspaceID string, + target Target, ) error { + workspaceFolder, workspaceID := target.WorkspaceFolder, target.WorkspaceID log.Info("Opening Zed editor") if len(workspaceFolder) == 0 || workspaceFolder[0] != '/' { diff --git a/pkg/image/auth.go b/pkg/image/auth.go index 56a3b6d7fa..c64709f0b0 100644 --- a/pkg/image/auth.go +++ b/pkg/image/auth.go @@ -24,9 +24,10 @@ var ( ) ) +//nolint:gosec // token file path, not a credential const tokenFileLocation = "/var/run/secrets/kubernetes.io/serviceaccount/token" -// See https://github.com/kubernetes/kubernetes/blob/30ae12d018697d3c5f04e225b11f242f5310e097/pkg/serviceaccount/claims.go#L55 +// Claims follow the kubernetes service account token format. type privateClaims struct { jwt.RegisteredClaims Kubernetes kubernetesClaim `json:"kubernetes.io"` @@ -83,7 +84,8 @@ func GetKeychain(ctx context.Context) (authn.Keychain, error) { } keyChains = append(keyChains, authn.DefaultKeychain) - // Order matters here: We want to go through all of the cloud provider keychains before we hit the default keychain (docker config.json) + // Order matters here: We want to go through all of the cloud provider keychains before we hit the default keychain + // (docker config.json) return authn.NewMultiKeychain( keyChains..., ), nil diff --git a/pkg/inject/inject_test.go b/pkg/inject/inject_test.go index f0f73578de..7f6cdc53b1 100644 --- a/pkg/inject/inject_test.go +++ b/pkg/inject/inject_test.go @@ -837,14 +837,7 @@ func TestInjectScript_RealShellRejectsInvalidBinarySize(t *testing.T) { tmp := t.TempDir() installPath := filepath.Join(tmp, "bin", "agent") require.NoError(t, os.MkdirAll(filepath.Dir(installPath), 0o750)) - toolsDir := filepath.Join(tmp, "tools") - require.NoError(t, os.Mkdir(toolsDir, 0o700)) - curlPath := filepath.Join(toolsDir, "curl") - require.NoError(t, os.WriteFile(curlPath, []byte("#!/bin/sh\nexit 1\n"), 0o600)) - require.NoError(t, os.Chmod(curlPath, 0o700)) //nolint:gosec // fake curl must be executable - sleepPath := filepath.Join(toolsDir, "sleep") - require.NoError(t, os.WriteFile(sleepPath, []byte("#!/bin/sh\nexit 0\n"), 0o600)) - require.NoError(t, os.Chmod(sleepPath, 0o700)) //nolint:gosec // fake sleep must be executable + toolsDir := writeFakeTools(t, tmp) rendered, err := GenerateScript(Script, &Params{ Command: "echo should-not-run", @@ -856,62 +849,23 @@ func TestInjectScript_RealShellRejectsInvalidBinarySize(t *testing.T) { }) require.NoError(t, err) - stdinR, stdinW := io.Pipe() - stdoutR, stdoutW := io.Pipe() - stderrR, stderrW := io.Pipe() - cmd := exec.Command( //nolint:gosec // G204: script content is test-generated, not external input - "sh", - "-c", - rendered, - ) - cmd.Env = append( - os.Environ(), - "PATH="+toolsDir+string(os.PathListSeparator)+os.Getenv("PATH"), - ) - cmd.Stdin = stdinR - cmd.Stdout = stdoutW - cmd.Stderr = stderrW - require.NoError(t, cmd.Start()) - waitDone := make(chan struct{}) - t.Cleanup(func() { - _ = stdinW.Close() - _ = stdoutW.Close() - _ = stderrW.Close() - select { - case <-waitDone: - case <-time.After(5 * time.Second): - _ = cmd.Process.Kill() - <-waitDone - } - }) - go func() { - _ = cmd.Wait() - close(waitDone) - }() + sh := startShScript(t, rendered, toolsDir) - shPipe := shTestPipe{in: stdinW, out: stdoutR} - expectLine(t, shPipe.out, "ping") - requireWrite(t, shPipe.in, testPong) - if got := readLineFatal(t, shPipe.out); !strings.HasPrefix(got, "ARM-") { + expectLine(t, sh.pipe.out, "ping") + requireWrite(t, sh.pipe.in, testPong) + if got := readLineFatal(t, sh.pipe.out); !strings.HasPrefix(got, "ARM-") { t.Fatalf("expected ARM-..., got %q", got) } - requireWrite(t, shPipe.in, "1K\n") + requireWrite(t, sh.pipe.in, "1K\n") - if got := readLineFatal(t, stderrR); !strings.Contains(got, "Invalid binary size") { + if got := readLineFatal(t, sh.stderr); !strings.Contains(got, "Invalid binary size") { t.Fatalf("expected an invalid-size rejection on stderr, got %q", got) } _, statErr := os.Stat(installPath) require.True(t, os.IsNotExist(statErr), "invalid size must not install anything") - _ = stdinW.Close() - _ = stdoutW.Close() - _ = stderrW.Close() - select { - case <-waitDone: - case <-time.After(5 * time.Second): - t.Fatal("inject.sh did not finish the failed-download fallback") - } + sh.finish(t) } func requireWrite(t *testing.T, w io.Writer, s string) { @@ -937,3 +891,93 @@ func readDelimitedLine(r io.Reader) (string, error) { } } } + +// writeFakeTools puts a curl that always fails and a no-op sleep in a new +// directory under dir, so the script takes its download fallback path. +func writeFakeTools(t *testing.T, dir string) string { + t.Helper() + toolsDir := filepath.Join(dir, "tools") + require.NoError(t, os.Mkdir(toolsDir, 0o700)) + writeExecutable(t, filepath.Join(toolsDir, "curl"), "#!/bin/sh\nexit 1\n") + writeExecutable(t, filepath.Join(toolsDir, "sleep"), "#!/bin/sh\nexit 0\n") + + return toolsDir +} + +func writeExecutable(t *testing.T, path, content string) { + t.Helper() + require.NoError(t, os.WriteFile(path, []byte(content), 0o600)) + require.NoError(t, os.Chmod(path, 0o700)) //nolint:gosec // fake tool must be executable +} + +// shScript is a rendered inject script running under sh with its stdio piped +// to the test. +type shScript struct { + pipe shTestPipe + stderr *io.PipeReader + stdinW *io.PipeWriter + stdoutW *io.PipeWriter + stderrW *io.PipeWriter + waitDone chan struct{} +} + +func startShScript(t *testing.T, rendered, toolsDir string) *shScript { + t.Helper() + stdinR, stdinW := io.Pipe() + stdoutR, stdoutW := io.Pipe() + stderrR, stderrW := io.Pipe() + cmd := exec.Command( //nolint:gosec // G204: script content is test-generated, not external input + "sh", + "-c", + rendered, + ) + cmd.Env = append( + os.Environ(), + "PATH="+toolsDir+string(os.PathListSeparator)+os.Getenv("PATH"), + ) + cmd.Stdin = stdinR + cmd.Stdout = stdoutW + cmd.Stderr = stderrW + require.NoError(t, cmd.Start()) + + s := &shScript{ + pipe: shTestPipe{in: stdinW, out: stdoutR}, + stderr: stderrR, + stdinW: stdinW, + stdoutW: stdoutW, + stderrW: stderrW, + waitDone: make(chan struct{}), + } + t.Cleanup(func() { + s.closeStdio() + select { + case <-s.waitDone: + case <-time.After(5 * time.Second): + _ = cmd.Process.Kill() + <-s.waitDone + } + }) + go func() { + _ = cmd.Wait() + close(s.waitDone) + }() + + return s +} + +func (s *shScript) closeStdio() { + _ = s.stdinW.Close() + _ = s.stdoutW.Close() + _ = s.stderrW.Close() +} + +// finish closes the script's stdio and requires the script to exit. +func (s *shScript) finish(t *testing.T) { + t.Helper() + s.closeStdio() + select { + case <-s.waitDone: + case <-time.After(5 * time.Second): + t.Fatal("inject.sh did not finish the failed-download fallback") + } +} diff --git a/pkg/log/streamer.go b/pkg/log/streamer.go index 07108ee700..427caa18c0 100644 --- a/pkg/log/streamer.go +++ b/pkg/log/streamer.go @@ -151,13 +151,10 @@ func (s *JSONLogStreamer) capture(line string) { } s.mu.Lock() defer s.mu.Unlock() - for (s.captureLines > 0 && len(s.lastLines) >= s.captureLines) || - (s.captureBytes > 0 && len(s.lastLines) > 0 && - s.capturedBytes+1+len(line) > s.captureBytes) { + for s.overCaptureLimit(len(line)) { s.dropOldestLine() } - if s.captureBytes > 0 && len(s.lastLines) > 0 && - s.capturedBytes+1+len(line) > s.captureBytes { + if s.byteOverflow(len(line)) { line = line[len(line)-(s.captureBytes-s.capturedBytes-1):] } if len(s.lastLines) > 0 { @@ -167,6 +164,21 @@ func (s *JSONLogStreamer) capture(line string) { s.capturedBytes += len(line) } +// overCaptureLimit reports whether the oldest line must go before a new line of +// size n can be captured. +func (s *JSONLogStreamer) overCaptureLimit(n int) bool { + if s.captureLines > 0 && len(s.lastLines) >= s.captureLines { + return true + } + return s.captureBytes > 0 && len(s.lastLines) > 0 && s.capturedBytes+1+n > s.captureBytes +} + +// byteOverflow reports whether a new line of size n must be cut to fit the +// byte budget, after older lines were dropped. +func (s *JSONLogStreamer) byteOverflow(n int) bool { + return s.captureBytes > 0 && len(s.lastLines) > 0 && s.capturedBytes+1+n > s.captureBytes +} + func (s *JSONLogStreamer) dropOldestLine() { if len(s.lastLines) == 0 { return diff --git a/pkg/netstat/netstat_util.go b/pkg/netstat/netstat_util.go index 63ea794570..2e0fe33316 100644 --- a/pkg/netstat/netstat_util.go +++ b/pkg/netstat/netstat_util.go @@ -281,6 +281,7 @@ func extractProcInfo(sktab []SockTabEntry) { // callers degrade gracefully instead of aborting the entire port scan. All // other errors, including permission and parse failures, still propagate. func doNetstat(path string, fn AcceptFn) ([]SockTabEntry, error) { + //nolint:gosec // reads a kernel-provided status file f, err := os.Open(path) if err != nil { if os.IsNotExist(err) { diff --git a/pkg/open/open.go b/pkg/open/open.go index 690a6e3bde..ce82bd0338 100644 --- a/pkg/open/open.go +++ b/pkg/open/open.go @@ -53,7 +53,10 @@ func JLabDesktop(ctx context.Context, url string) error { } func jlabOpen(url string) error { - return exec.Command("jlab", url).Run() + //nolint:gosec // fixed jlab binary; url is passed as one argument + return exec.Command("jlab", url). + Run() + //nolint:gosec // fixed jlab binary; url is passed as one argument } func tryOpen(ctx context.Context, url string, fn func(string) error) error { diff --git a/pkg/options/resolve.go b/pkg/options/resolve.go index 3883c4dc00..6faf0ef92f 100644 --- a/pkg/options/resolve.go +++ b/pkg/options/resolve.go @@ -14,13 +14,20 @@ import ( "github.com/devsy-org/devsy/pkg/types" ) +// ResolveMachineRequest is the input to ResolveAndSaveOptionsMachine. +type ResolveMachineRequest struct { + DevConfig *config.Config + ProviderConfig *provider.ProviderConfig + OriginalMachine *provider.Machine + UserOptions map[string]string +} + func ResolveAndSaveOptionsMachine( ctx context.Context, - devConfig *config.Config, - providerConfig *provider.ProviderConfig, - originalMachine *provider.Machine, - userOptions map[string]string, + req ResolveMachineRequest, ) (*provider.Machine, error) { + devConfig, providerConfig := req.DevConfig, req.ProviderConfig + originalMachine, userOptions := req.OriginalMachine, req.UserOptions if originalMachine == nil { return nil, fmt.Errorf("originalMachine cannot be nil") } @@ -58,13 +65,12 @@ func ResolveAndSaveOptionsMachine( } // remove global options - filterResolvedOptions( - resolvedOptions, - beforeConfigOptions, - devConfig.ProviderOptions(providerConfig.Name), - providerConfig.Options, - userOptions, - ) + filterResolvedOptions(resolvedOptions, resolvedOptionScope{ + beforeConfigOptions: beforeConfigOptions, + providerValues: devConfig.ProviderOptions(providerConfig.Name), + providerOptions: providerConfig.Options, + userOptions: userOptions, + }) // save machine config if machine != nil { @@ -88,14 +94,21 @@ func saveMachineIfChanged( return provider.SaveMachineConfig(machine) } +// ResolveWorkspaceRequest carries the inputs of ResolveAndSaveOptionsWorkspace. +type ResolveWorkspaceRequest struct { + DevConfig *config.Config + ProviderConfig *provider.ProviderConfig + OriginalWorkspace *provider.Workspace + UserOptions map[string]string + Options []resolver.Option +} + func ResolveAndSaveOptionsWorkspace( ctx context.Context, - devConfig *config.Config, - providerConfig *provider.ProviderConfig, - originalWorkspace *provider.Workspace, - userOptions map[string]string, - options ...resolver.Option, + req ResolveWorkspaceRequest, ) (*provider.Workspace, error) { + devConfig, providerConfig, originalWorkspace := req.DevConfig, req.ProviderConfig, req.OriginalWorkspace + userOptions, options := req.UserOptions, req.Options if originalWorkspace == nil { return nil, fmt.Errorf("originalWorkspace cannot be nil") } @@ -134,13 +147,12 @@ func ResolveAndSaveOptionsWorkspace( } // remove global options - filterResolvedOptions( - resolvedOptions, - beforeConfigOptions, - devConfig.ProviderOptions(providerConfig.Name), - providerConfig.Options, - userOptions, - ) + filterResolvedOptions(resolvedOptions, resolvedOptionScope{ + beforeConfigOptions: beforeConfigOptions, + providerValues: devConfig.ProviderOptions(providerConfig.Name), + providerOptions: providerConfig.Options, + userOptions: userOptions, + }) // save workspace config workspace.Provider.Options = resolvedOptions @@ -154,15 +166,22 @@ func ResolveAndSaveOptionsWorkspace( return workspace, nil } +// ResolveOptionsRequest carries the inputs of ResolveOptions. +type ResolveOptionsRequest struct { + DevConfig *config.Config + ProviderConfig *provider.ProviderConfig + UserOptions map[string]string + SkipRequired bool + SkipSubOptions bool + SingleMachine *bool +} + func ResolveOptions( ctx context.Context, - devConfig *config.Config, - providerConfig *provider.ProviderConfig, - userOptions map[string]string, - skipRequired bool, - skipSubOptions bool, - singleMachine *bool, + req ResolveOptionsRequest, ) (*config.Config, error) { + devConfig, providerConfig, userOptions := req.DevConfig, req.ProviderConfig, req.UserOptions + skipRequired, skipSubOptions, singleMachine := req.SkipRequired, req.SkipSubOptions, req.SingleMachine if devConfig == nil { return nil, nil } @@ -482,30 +501,31 @@ func resolveAgentDownloadURL(devConfig *config.Config) string { return config.DefaultAgentDownloadURL() } +// resolvedOptionScope holds the option sources that decide which resolved +// options are removed. +type resolvedOptionScope struct { + beforeConfigOptions map[string]config.OptionValue + providerValues map[string]config.OptionValue + providerOptions map[string]*types.Option + userOptions map[string]string +} + func filterResolvedOptions( - resolvedOptions, beforeConfigOptions, providerValues map[string]config.OptionValue, - providerOptions map[string]*types.Option, - userOptions map[string]string, + resolvedOptions map[string]config.OptionValue, + scope resolvedOptionScope, ) { for k := range resolvedOptions { - if shouldRemoveResolvedOption(removeResolvedOptionParams{ - k: k, - beforeConfigOptions: beforeConfigOptions, - providerValues: providerValues, - providerOptions: providerOptions, - userOptions: userOptions, - }) { + if shouldRemoveResolvedOption( + removeResolvedOptionParams{k: k, resolvedOptionScope: scope}, + ) { delete(resolvedOptions, k) } } } type removeResolvedOptionParams struct { - k string - beforeConfigOptions map[string]config.OptionValue - providerValues map[string]config.OptionValue - providerOptions map[string]*types.Option - userOptions map[string]string + k string + resolvedOptionScope } func shouldRemoveResolvedOption(p removeResolvedOptionParams) bool { diff --git a/pkg/options/resolve_test.go b/pkg/options/resolve_test.go index 61ef45cc1f..1201d9c4a3 100644 --- a/pkg/options/resolve_test.go +++ b/pkg/options/resolve_test.go @@ -15,6 +15,10 @@ import ( "gotest.tools/assert" ) +const ( + trueValue = "true" +) + type testCase struct { Name string ProviderOptions map[string]*types.Option @@ -787,14 +791,14 @@ func TestResolveAgentAppleConfig(t *testing.T) { options := map[string]string{ "CONTAINER_PATH": "/opt/homebrew/bin/container", - "ROSETTA": "true", + "ROSETTA": trueValue, "VAL": "resolved", } resolveAgentAppleConfig(agentConfig, options) assert.Equal(t, "/opt/homebrew/bin/container", agentConfig.Apple.Path) - assert.Equal(t, types.StrBool("true"), agentConfig.Apple.Rosetta) + assert.Equal(t, types.StrBool(trueValue), agentConfig.Apple.Rosetta) assert.Equal(t, "resolved", agentConfig.Apple.Env["KEY"]) } @@ -813,9 +817,9 @@ func TestResolveAgentMicrosandboxConfig(t *testing.T) { options := map[string]string{ "MICROSANDBOX_MEMORY": "2048", "MICROSANDBOX_CPUS": "4", - "MICROSANDBOX_EPHEMERAL": "true", + "MICROSANDBOX_EPHEMERAL": trueValue, "MICROSANDBOX_MAX_MEMORY": "8192", - "MICROSANDBOX_BLOCK_EGRESS": "true", + "MICROSANDBOX_BLOCK_EGRESS": trueValue, "MICROSANDBOX_STORAGE": "32", "MICROSANDBOX_WORKSPACE_HOST_PERMISSIONS": "private", "MICROSANDBOX_WORKSPACE_STAT_VIRTUALIZATION": "relaxed", @@ -825,9 +829,9 @@ func TestResolveAgentMicrosandboxConfig(t *testing.T) { assert.Equal(t, "2048", agentConfig.Microsandbox.Memory) assert.Equal(t, "4", agentConfig.Microsandbox.CPUs) - assert.Equal(t, types.StrBool("true"), agentConfig.Microsandbox.Ephemeral) + assert.Equal(t, types.StrBool(trueValue), agentConfig.Microsandbox.Ephemeral) assert.Equal(t, "8192", agentConfig.Microsandbox.MaxMemory) - assert.Equal(t, types.StrBool("true"), agentConfig.Microsandbox.BlockEgress) + assert.Equal(t, types.StrBool(trueValue), agentConfig.Microsandbox.BlockEgress) assert.Equal(t, "32", agentConfig.Microsandbox.Storage) assert.Equal(t, "private", agentConfig.Microsandbox.WorkspaceHostPermissions) assert.Equal(t, "relaxed", agentConfig.Microsandbox.WorkspaceStatVirtualization) diff --git a/pkg/options/resolver/resolve_test.go b/pkg/options/resolver/resolve_test.go index 8694bf1a95..75d805cd9c 100644 --- a/pkg/options/resolver/resolve_test.go +++ b/pkg/options/resolver/resolve_test.go @@ -10,6 +10,12 @@ import ( "github.com/stretchr/testify/suite" ) +const ( + value1 = "value1" + value2 = "value2" + option1Value = "option1" +) + type ResolveTestSuite struct { suite.Suite resolver *Resolver @@ -28,12 +34,12 @@ func TestResolveTestSuite(t *testing.T) { } func (suite *ResolveTestSuite) TestResolveOptions_HandlesRemovedNodeWithDanglingEdge() { - option1 := &types.Option{Default: "value1"} - option2 := &types.Option{Default: "value2"} + option1 := &types.Option{Default: value1} + option2 := &types.Option{Default: value2} - suite.Require().NoError(suite.resolver.graph.AddNode("option1", option1)) + suite.Require().NoError(suite.resolver.graph.AddNode(option1Value, option1)) suite.Require().NoError(suite.resolver.graph.AddNode("option2", option2)) - suite.Require().NoError(suite.resolver.graph.AddEdge("option1", "option2")) + suite.Require().NoError(suite.resolver.graph.AddEdge(option1Value, "option2")) suite.Require().NoError(suite.resolver.graph.RemoveNode("option2")) result, err := suite.resolver.resolveOptions( @@ -42,16 +48,16 @@ func (suite *ResolveTestSuite) TestResolveOptions_HandlesRemovedNodeWithDangling ) suite.NoError(err) suite.Len(result, 1) - suite.Equal("value1", result["option1"].Value) + suite.Equal(value1, result[option1Value].Value) } func (suite *ResolveTestSuite) TestResolveOptions_WithDependencies() { - option1 := &types.Option{Default: "value1"} - option2 := &types.Option{Default: "value2"} + option1 := &types.Option{Default: value1} + option2 := &types.Option{Default: value2} - suite.Require().NoError(suite.resolver.graph.AddNode("option1", option1)) + suite.Require().NoError(suite.resolver.graph.AddNode(option1Value, option1)) suite.Require().NoError(suite.resolver.graph.AddNode("option2", option2)) - suite.Require().NoError(suite.resolver.graph.AddEdge("option1", "option2")) + suite.Require().NoError(suite.resolver.graph.AddEdge(option1Value, "option2")) result, err := suite.resolver.resolveOptions( context.Background(), @@ -59,15 +65,15 @@ func (suite *ResolveTestSuite) TestResolveOptions_WithDependencies() { ) suite.NoError(err) suite.Len(result, 2) - suite.Equal("value1", result["option1"].Value) - suite.Equal("value2", result["option2"].Value) + suite.Equal(value1, result[option1Value].Value) + suite.Equal(value2, result["option2"].Value) } func (suite *ResolveTestSuite) TestResolveOptions_MultipleNodes() { nodes := map[string]*types.Option{ - "option1": {Default: "value1"}, - "option2": {Default: "value2"}, - "option3": {Default: "value3"}, + option1Value: {Default: value1}, + "option2": {Default: value2}, + "option3": {Default: "value3"}, } suite.Require().NoError(suite.resolver.graph.AddNodes(nodes)) @@ -78,7 +84,7 @@ func (suite *ResolveTestSuite) TestResolveOptions_MultipleNodes() { ) suite.NoError(err) suite.Len(result, 3) - suite.Equal("value1", result["option1"].Value) - suite.Equal("value2", result["option2"].Value) + suite.Equal(value1, result[option1Value].Value) + suite.Equal(value2, result["option2"].Value) suite.Equal("value3", result["option3"].Value) } diff --git a/pkg/options/resolver/resolver_test.go b/pkg/options/resolver/resolver_test.go index a196766fff..37e5a58cfe 100644 --- a/pkg/options/resolver/resolver_test.go +++ b/pkg/options/resolver/resolver_test.go @@ -11,6 +11,11 @@ import ( "github.com/stretchr/testify/suite" ) +const ( + testOption = "Test option" + parentValue = "parent" +) + type ResolverTestSuite struct { suite.Suite resolver *Resolver @@ -26,7 +31,7 @@ func TestResolverSuite(t *testing.T) { func (suite *ResolverTestSuite) TestBasicFunctionality() { optionDefs := map[string]*types.Option{ - "option1": { + option1Value: { Description: "Test option 1", Default: "default1", }, @@ -37,7 +42,7 @@ func (suite *ResolverTestSuite) TestBasicFunctionality() { } optionValues := map[string]config.OptionValue{ - "option1": {Value: "value1"}, + option1Value: {Value: value1}, } resolved, newDefs, err := suite.resolver.Resolve( @@ -49,14 +54,14 @@ func (suite *ResolverTestSuite) TestBasicFunctionality() { suite.NoError(err) suite.NotNil(resolved) suite.NotNil(newDefs) - suite.Equal("value1", resolved["option1"].Value) + suite.Equal(value1, resolved[option1Value].Value) suite.Equal("default2", resolved["option2"].Value) } func (suite *ResolverTestSuite) TestGraphFunctionality() { optionDefs := map[string]*types.Option{ "test": { - Description: "Test option", + Description: testOption, Default: "test_default", }, } @@ -84,7 +89,7 @@ func (suite *ResolverTestSuite) TestResolveOptions_EmptyGraph() { func (suite *ResolverTestSuite) TestResolveOptions_SingleOption() { suite.resolver.graph = graph.NewGraph[*types.Option]() option := &types.Option{ - Description: "Test option", + Description: testOption, Default: "default_value", } suite.Require().NoError(suite.resolver.graph.AddNode("test_option", option)) @@ -104,7 +109,7 @@ func (suite *ResolverTestSuite) TestResolveOptions_UserProvidedValue() { suite.resolver.graph = graph.NewGraph[*types.Option]() option := &types.Option{ - Description: "Test option", + Description: testOption, Default: "default_value", } suite.Require().NoError(suite.resolver.graph.AddNode("test_option", option)) @@ -218,14 +223,14 @@ func (suite *ResolverTestSuite) TestResolveOptions_PreserveChildWhenParentUnchan suite.setupParentChildGraph() existingValues := map[string]config.OptionValue{ - "parent": {Value: "old_parent_value", UserProvided: true}, - "child": {Value: "old_child_value", UserProvided: false}, + parentValue: {Value: "old_parent_value", UserProvided: true}, + "child": {Value: "old_child_value", UserProvided: false}, } result, err := suite.resolver.resolveOptions(context.Background(), existingValues) suite.NoError(err) - suite.Equal("old_parent_value", result["parent"].Value) + suite.Equal("old_parent_value", result[parentValue].Value) suite.Equal("old_child_value", result["child"].Value) } @@ -233,14 +238,14 @@ func (suite *ResolverTestSuite) TestResolveOptions_PreserveUserProvidedChild() { suite.setupParentChildGraph() existingValues := map[string]config.OptionValue{ - "parent": {Value: "old_parent_value", UserProvided: true}, - "child": {Value: "user_child_value", UserProvided: true}, + parentValue: {Value: "old_parent_value", UserProvided: true}, + "child": {Value: "user_child_value", UserProvided: true}, } result, err := suite.resolver.resolveOptions(context.Background(), existingValues) suite.NoError(err) - suite.Equal("old_parent_value", result["parent"].Value) + suite.Equal("old_parent_value", result[parentValue].Value) suite.Equal("user_child_value", result["child"].Value) } @@ -282,7 +287,7 @@ func (suite *ResolverTestSuite) TestBasicResolverFunctionality() { resolver := New(map[string]string{}, map[string]string{}, WithResolveSubOptions()) optionDefs := map[string]*types.Option{ - "parent": { + parentValue: { Default: "parent_value", }, } @@ -291,7 +296,7 @@ func (suite *ResolverTestSuite) TestBasicResolverFunctionality() { resolved, _, err := resolver.Resolve(context.Background(), nil, optionDefs, optionValues) suite.NoError(err) - suite.Equal("parent_value", resolved["parent"].Value) + suite.Equal("parent_value", resolved[parentValue].Value) } func (suite *ResolverTestSuite) TestAddOptionsToGraph_MultipleCalls() { @@ -317,7 +322,7 @@ func (suite *ResolverTestSuite) setupParentChildGraph() { parentOption := &types.Option{Description: "Parent", Default: "new_parent_value"} childOption := &types.Option{Description: "Child", Default: "child_default"} - suite.Require().NoError(suite.resolver.graph.AddNode("parent", parentOption)) + suite.Require().NoError(suite.resolver.graph.AddNode(parentValue, parentOption)) suite.Require().NoError(suite.resolver.graph.AddNode("child", childOption)) - suite.Require().NoError(suite.resolver.graph.AddEdge("parent", "child")) + suite.Require().NoError(suite.resolver.graph.AddEdge(parentValue, "child")) } diff --git a/pkg/platform/client/client.go b/pkg/platform/client/client.go index 36e24e2438..804bbf6dde 100644 --- a/pkg/platform/client/client.go +++ b/pkg/platform/client/client.go @@ -194,17 +194,20 @@ func (c *client) Save() error { c.config.APIVersion = "storage.devsy.sh/v1" } - err := os.MkdirAll(filepath.Dir(c.configPath), 0o755) + err := os.MkdirAll(filepath.Dir(c.configPath), 0o750) if err != nil { return err } - out, err := json.Marshal(c.config) + //nolint:gosec // value is a config field or a version number, not a secret or overflow risk + out, err := json.Marshal( + c.config, + ) //nolint:gosec // config file is written with 0600 permissions if err != nil { return err } - return os.WriteFile(c.configPath, out, 0o660) + return os.WriteFile(c.configPath, out, 0o600) } func (c *client) ManagementConfig() (*rest.Config, error) { @@ -417,10 +420,8 @@ func (c *client) verifyConnection() error { return nil } - var urlError *url.Error - if errors.As(err, &urlError) { - var authErr x509.UnknownAuthorityError - if errors.As(urlError.Err, &authErr) { + if urlError, ok := errors.AsType[*url.Error](err); ok { + if _, ok := errors.AsType[x509.UnknownAuthorityError](urlError.Err); ok { return fmt.Errorf( "unsafe login endpoint %q, if you wish to login into an insecure devsy endpoint run with the '--insecure' flag", c.config.Host, @@ -456,7 +457,10 @@ func VerifyVersion(baseClient Client) error { return err } - if int(cliVersion.Major) > backendMajor { + //nolint:gosec // value is a config field or a version number, not a secret or overflow risk + if int( + cliVersion.Major, + ) > backendMajor { //nolint:gosec // major versions are small non-negative numbers return fmt.Errorf( "unsupported %[1]s version %[2]s. Downgrade your CLI to below v%[3]d.0.0 to support this version, "+ "as %[1]s v%[3]d.0.0 and newer versions are incompatible with v%[4]d.x.x", @@ -465,7 +469,10 @@ func VerifyVersion(baseClient Client) error { cliVersion.Major, backendMajor, ) - } else if int(cliVersion.Major) < backendMajor { + //nolint:gosec // value is a config field or a version number, not a secret or overflow risk + } else if int( + cliVersion.Major, + ) < backendMajor { //nolint:gosec // major versions are small non-negative numbers return fmt.Errorf( "unsupported %[1]s version %[2]s. Upgrade your CLI to v%[3]d.0.0 or above to support this version, "+ "as %[1]s v%[3]d.0.0 and newer versions are incompatible with v%[4]d.x.x", @@ -533,7 +540,7 @@ func GetRestConfig(host, token string, insecure bool) (*rest.Config, error) { } func startServer(redirectURI string, keyChannel chan keyStruct) *http.Server { - srv := &http.Server{Addr: ":25843"} + srv := &http.Server{Addr: ":25843", ReadHeaderTimeout: 10 * time.Second} http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { keys, ok := r.URL.Query()["key"] diff --git a/pkg/platform/config.go b/pkg/platform/config.go index 04c212717a..c660bce712 100644 --- a/pkg/platform/config.go +++ b/pkg/platform/config.go @@ -39,6 +39,7 @@ func ReadConfig(contextName string, providerName string) (*client.Config, error) return nil, err } + //nolint:gosec // path is the user devsy config content, err := os.ReadFile(configPath) if err != nil { return nil, err diff --git a/pkg/platform/form/form.go b/pkg/platform/form/form.go index 98ca992a9f..c00f84c655 100644 --- a/pkg/platform/form/form.go +++ b/pkg/platform/form/form.go @@ -23,11 +23,20 @@ import ( const ParameterTypeBoolean = "boolean" +// InstanceSpec describes the workspace instance a form creates. +type InstanceSpec struct { + ID string + UID string + Source string + Picture string +} + func CreateInstance( ctx context.Context, baseClient client.Client, - id, uid, source, picture string, + spec InstanceSpec, ) (*managementv1.DevsyWorkspaceInstance, error) { + id, uid, source, picture := spec.ID, spec.UID, spec.Source, spec.Picture instance, _, err := createInstance(ctx, baseClient, InstanceIdentity{ ID: id, UID: uid, diff --git a/pkg/platform/instance.go b/pkg/platform/instance.go index 5b4fea7324..da1b232a2b 100644 --- a/pkg/platform/instance.go +++ b/pkg/platform/instance.go @@ -199,8 +199,10 @@ func DialInstance( devsyURL += "?" + values.Encode() } + //nolint:gosec // platform instances commonly use self-signed certificates + tlsConfig := &tls.Config{InsecureSkipVerify: true} dialer := websocket.Dialer{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + TLSClientConfig: tlsConfig, Proxy: http.ProxyFromEnvironment, HandshakeTimeout: 45 * time.Second, } diff --git a/pkg/platform/kubeconfig.go b/pkg/platform/kubeconfig.go index 7feaa68bff..41bac9c8e9 100644 --- a/pkg/platform/kubeconfig.go +++ b/pkg/platform/kubeconfig.go @@ -20,7 +20,8 @@ import ( var configTTL time.Duration = time.Hour * 24 * 90 // 90 days -// NewInstanceKubeConfig creates a KubeConfig (clientcmdapi.Config) based for either a space instance or virtual cluster instance. +// NewInstanceKubeConfig creates a KubeConfig (clientcmdapi.Config) based for either a space instance or virtual cluster +// instance. // We return the config as byte slice to ensure correct handling and formatting through the `clientcmd` methods. func NewInstanceKubeConfig( ctx context.Context, diff --git a/pkg/platform/parameters/parameters.go b/pkg/platform/parameters/parameters.go index 0cf89eb89f..dd81280a00 100644 --- a/pkg/platform/parameters/parameters.go +++ b/pkg/platform/parameters/parameters.go @@ -192,23 +192,29 @@ func checkNumberBounds(num int, parameter storagev1.AppParameter) error { func GetDeepValue(parameters any, path string) any { current := parameters for segment := range strings.SplitSeq(path, ".") { - switch t := current.(type) { - case map[string]any: - val, ok := t[segment] - if !ok { - return nil - } - current = val - case []any: - index, err := strconv.Atoi(segment) - if err != nil || index < 0 || index >= len(t) { - return nil - } - current = t[index] - default: + next, ok := childValue(current, segment) + if !ok { return nil } + current = next } return current } + +// childValue returns the map key or list index segment of v. +func childValue(v any, segment string) (any, bool) { + switch t := v.(type) { + case map[string]any: + val, ok := t[segment] + return val, ok + case []any: + index, err := strconv.Atoi(segment) + if err != nil || index < 0 || index >= len(t) { + return nil, false + } + return t[index], true + default: + return nil, false + } +} diff --git a/pkg/platform/parameters/parameters_test.go b/pkg/platform/parameters/parameters_test.go index c5806b0fcb..a0a269ad8e 100644 --- a/pkg/platform/parameters/parameters_test.go +++ b/pkg/platform/parameters/parameters_test.go @@ -217,20 +217,28 @@ func TestVerifyNumberValue(t *testing.T) { }) } -func TestGetDeepValue(t *testing.T) { +const plainText = "plain" + +type deepValueCase struct { + name string + parameters any + path string + want any +} + +func deepValueCases() []deepValueCase { + return append(deepValuePathCases(), deepValueEdgeCases()...) +} + +func deepValuePathCases() []deepValueCase { nested := map[string]any{"a": map[string]any{"b": map[string]any{"c": 1}}} withItems := map[string]any{ "items": []any{"x", "y"}, "list": []any{map[string]any{varName: "first"}}, - "text": "plain", + "text": plainText, } - cases := []struct { - name string - parameters any - path string - want any - }{ + return []deepValueCase{ {name: "nil parameters yield nil", path: "a"}, { name: "top-level key returns its value", @@ -251,6 +259,16 @@ func TestGetDeepValue(t *testing.T) { path: "list.0.name", want: "first", }, + } +} + +func deepValueEdgeCases() []deepValueCase { + withItems := map[string]any{ + "items": []any{"x", "y"}, + "text": plainText, + } + + return []deepValueCase{ {name: "missing key yields nil", parameters: withItems, path: "absent"}, {name: "index past end of slice yields nil", parameters: withItems, path: "items.2"}, {name: "negative index yields nil", parameters: withItems, path: "items.-1"}, @@ -260,7 +278,7 @@ func TestGetDeepValue(t *testing.T) { path: "items.first", }, {name: "path through scalar yields nil", parameters: withItems, path: "text.length"}, - {name: "unsupported root type yields nil", parameters: "plain", path: "a"}, + {name: "unsupported root type yields nil", parameters: plainText, path: "a"}, { name: "empty path looks up the empty-string key", parameters: map[string]any{"": "blank"}, @@ -287,8 +305,10 @@ func TestGetDeepValue(t *testing.T) { want: "y", }, } +} - for _, tc := range cases { +func TestGetDeepValue(t *testing.T) { + for _, tc := range deepValueCases() { t.Run(tc.name, func(t *testing.T) { assert.DeepEqual(t, GetDeepValue(tc.parameters, tc.path), tc.want) }) diff --git a/pkg/platform/remotecommand/client.go b/pkg/platform/remotecommand/client.go index 4eb980258d..a2be3b74c2 100644 --- a/pkg/platform/remotecommand/client.go +++ b/pkg/platform/remotecommand/client.go @@ -9,13 +9,19 @@ import ( "github.com/gorilla/websocket" ) +// Streams are the local ends of an exec session. +type Streams struct { + Stdin io.Reader + Stdout io.Writer + Stderr io.Writer +} + func ExecuteConn( ctx context.Context, rawConn *websocket.Conn, - stdin io.Reader, - stdout io.Writer, - stderr io.Writer, + streams Streams, ) (int, error) { + stdin, stdout, stderr := streams.Stdin, streams.Stdout, streams.Stderr conn := NewWebsocketConn(rawConn) ctx, cancel := context.WithCancel(ctx) diff --git a/pkg/provider/dir.go b/pkg/provider/dir.go index 5ef9d88e33..95eb1c9be3 100644 --- a/pkg/provider/dir.go +++ b/pkg/provider/dir.go @@ -246,6 +246,7 @@ func LoadProviderConfig(context, provider string) (*ProviderConfig, error) { } providerFile := filepath.Join(providerDir, ProviderConfigFile) + //nolint:gosec // path is inside the provider state directory providerConfigBytes, err := os.ReadFile(providerFile) if err != nil { return nil, err diff --git a/pkg/provider/download.go b/pkg/provider/download.go index 9e45774111..f7add5319a 100644 --- a/pkg/provider/download.go +++ b/pkg/provider/download.go @@ -199,18 +199,15 @@ func isRetriableError(err error) bool { return false } - var httpErr *download.HTTPStatusError - if errors.As(err, &httpErr) { + if httpErr, ok := errors.AsType[*download.HTTPStatusError](err); ok { return isRetriableHTTPStatus(httpErr.StatusCode) } - var netErr net.Error - if errors.As(err, &netErr) { + if netErr, ok := errors.AsType[net.Error](err); ok { return netErr.Timeout() } - var opErr *net.OpError - if errors.As(err, &opErr) { + if _, ok := errors.AsType[*net.OpError](err); ok { return true } diff --git a/pkg/provider/resolve.go b/pkg/provider/resolve.go index 1a09e8377e..a740db4058 100644 --- a/pkg/provider/resolve.go +++ b/pkg/provider/resolve.go @@ -282,21 +282,26 @@ func GetProviderSource(src ProviderSource, configName string) string { case src.File != "": return src.File case src.Github != "": - // Canonicalize to github.com// so version helpers - // (classifyVersionSource, parseGitHubSourcePath) recognize it. - githubSource, githubTag := SplitSourceAndTag(src.Github) - if !strings.HasPrefix(githubSource, "github.com/") { - githubSource = "github.com/" + githubSource - } - _, rawTag := SplitSourceAndTag(src.Raw) - if rawTag != "" { - githubTag = rawTag - } - if githubTag != "" { - return githubSource + "@" + githubTag - } - return githubSource + return githubSourceString(src) default: return "" } } + +// githubSourceString canonicalizes a GitHub source to github.com// +// so version helpers (classifyVersionSource, parseGitHubSourcePath) recognize +// it, and applies the tag from Raw when there is one. +func githubSourceString(src ProviderSource) string { + githubSource, githubTag := SplitSourceAndTag(src.Github) + if !strings.HasPrefix(githubSource, "github.com/") { + githubSource = "github.com/" + githubSource + } + _, rawTag := SplitSourceAndTag(src.Raw) + if rawTag != "" { + githubTag = rawTag + } + if githubTag != "" { + return githubSource + "@" + githubTag + } + return githubSource +} diff --git a/pkg/provider/resolve_test.go b/pkg/provider/resolve_test.go index 81a697fd57..0b65155ad0 100644 --- a/pkg/provider/resolve_test.go +++ b/pkg/provider/resolve_test.go @@ -4,15 +4,23 @@ import ( "testing" ) -func TestGetProviderSource_GithubCanonicalization(t *testing.T) { - const githubPath = "devsy-org/devsy-provider-ssh" - const canonicalGithub = "github.com/" + githubPath +const ( + githubPath = "devsy-org/devsy-provider-ssh" + canonicalGithub = "github.com/" + githubPath +) + +type providerSourceCase struct { + name string + src ProviderSource + want string +} + +func githubCanonicalizationCases() []providerSourceCase { + return append(githubSourceCases(), otherSourceCases()...) +} - tests := []struct { - name string - src ProviderSource - want string - }{ +func githubSourceCases() []providerSourceCase { + return []providerSourceCase{ { name: "bare org/repo gets github.com prefix", src: ProviderSource{Github: githubPath}, @@ -62,6 +70,11 @@ func TestGetProviderSource_GithubCanonicalization(t *testing.T) { }, want: canonicalGithub, }, + } +} + +func otherSourceCases() []providerSourceCase { + return []providerSourceCase{ { name: "internal source returns raw", src: ProviderSource{Internal: true, Raw: "docker"}, @@ -78,8 +91,10 @@ func TestGetProviderSource_GithubCanonicalization(t *testing.T) { want: "/tmp/p.yaml", }, } +} - for _, tc := range tests { +func TestGetProviderSource_GithubCanonicalization(t *testing.T) { + for _, tc := range githubCanonicalizationCases() { t.Run(tc.name, func(t *testing.T) { got := GetProviderSource(tc.src, "config-name") if got != tc.want { diff --git a/pkg/provider/workspace.go b/pkg/provider/workspace.go index c5cf1a4b95..7570494566 100644 --- a/pkg/provider/workspace.go +++ b/pkg/provider/workspace.go @@ -190,6 +190,20 @@ type ContainerWorkspaceInfo struct { // Agent holds the agent info Agent ProviderAgentConfig `json:"agent"` + + // SetupRun names the retained setup history this container setup records + // into. Nil when no history is requested. + SetupRun *SetupRunRef `json:"setupRun,omitempty"` +} + +// SetupRunRef identifies a retained setup history run and the workspace +// generation it belongs to. It holds no secrets. +type SetupRunRef struct { + RunID string `json:"runId"` + ParentProducerID string `json:"parentProducerId,omitempty"` + Context string `json:"context"` + WorkspaceUID string `json:"workspaceUid,omitempty"` + WorkspaceID string `json:"workspaceId,omitempty"` } type AgentWorkspaceInfo struct { @@ -244,6 +258,13 @@ type CLIOptions struct { // Platform are the platform options Platform devsy.PlatformOptions `json:"platformOptions"` + // SetupRunID names the retained setup history this invocation records to. + // It is non-secret, set fresh for every invocation, and never reused from + // persisted state. + SetupRunID string `json:"setupRunId,omitempty"` + // SetupParentProducerID is the producer in the previous process hop. + SetupParentProducerID string `json:"setupParentProducerId,omitempty"` + // up options ID string `json:"id,omitempty"` Source string `json:"source,omitempty"` diff --git a/pkg/random/random.go b/pkg/random/random.go index 8569644a82..2b2a86b86e 100644 --- a/pkg/random/random.go +++ b/pkg/random/random.go @@ -1,6 +1,9 @@ package random -import "math/rand" +import ( + "crypto/rand" + "math/big" +) var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz") @@ -8,7 +11,7 @@ var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz") func String(length int) string { b := make([]rune, length) for i := range b { - b[i] = letterRunes[rand.Intn(len(letterRunes))] + b[i] = letterRunes[intn(len(letterRunes))] } return string(b) } @@ -17,5 +20,15 @@ func InRange(min, max int) int { if max <= min { return min } - return rand.Intn(max-min) + min + return intn(max-min) + min +} + +// intn returns a uniform value in [0, n). rand.Int never fails with the +// default reader since Go 1.24. +func intn(n int) int { + v, err := rand.Int(rand.Reader, big.NewInt(int64(n))) + if err != nil { + panic(err) + } + return int(v.Int64()) } diff --git a/pkg/secrets/atomicwrite.go b/pkg/secrets/atomicwrite.go index 0e2c7d6e6f..9e0579dcd7 100644 --- a/pkg/secrets/atomicwrite.go +++ b/pkg/secrets/atomicwrite.go @@ -8,20 +8,27 @@ import ( // atomicWriteFile writes via a same-dir temp file then rename, so a crash // mid-write cannot leave a truncated store file. -func atomicWriteFile(path string, data []byte, perm os.FileMode) error { +// Files are created 0600: the store holds secrets. +func atomicWriteFile(path string, data []byte) error { dir := filepath.Dir(path) - if err := os.MkdirAll(dir, 0o700); err != nil { + if err := os.MkdirAll( //nolint:gosec // directory is the internal secrets store location + dir, + 0o700, + ); err != nil { //nolint:gosec // path is the internal secrets store location return fmt.Errorf("create secrets dir: %w", err) } - tmp, err := os.CreateTemp(dir, filepath.Base(path)+".tmp-*") + tmp, err := os.CreateTemp( + dir, + filepath.Base(path)+".tmp-*", + ) //nolint:gosec // path is the internal secrets store location if err != nil { return fmt.Errorf("create temp secrets file: %w", err) } tmpName := tmp.Name() - defer func() { _ = os.Remove(tmpName) }() + defer func() { _ = os.Remove(tmpName) }() //nolint:gosec // temp file is created in the internal secrets store - if err := tmp.Chmod(perm); err != nil { + if err := tmp.Chmod(0o600); err != nil { _ = tmp.Close() return fmt.Errorf("chmod temp secrets file: %w", err) } @@ -36,7 +43,10 @@ func atomicWriteFile(path string, data []byte, perm os.FileMode) error { if err := tmp.Close(); err != nil { return fmt.Errorf("close temp secrets file: %w", err) } - if err := os.Rename(tmpName, path); err != nil { + if err := os.Rename( //nolint:gosec // rename within the internal secrets store + tmpName, + path, + ); err != nil { //nolint:gosec // path is the internal secrets store location return fmt.Errorf("replace secrets file: %w", err) } syncDir(dir) @@ -47,6 +57,7 @@ func atomicWriteFile(path string, data []byte, perm os.FileMode) error { // syncDir fsyncs a directory so a rename is durable across a crash. Best-effort: // some filesystems reject directory sync, and durability is not correctness. func syncDir(dir string) { + //nolint:gosec // path is inside the internal secrets store d, err := os.Open(dir) // #nosec G304 -- secrets config dir. if err != nil { return diff --git a/pkg/secrets/file_backend.go b/pkg/secrets/file_backend.go index 42906ab53b..68a47b398c 100644 --- a/pkg/secrets/file_backend.go +++ b/pkg/secrets/file_backend.go @@ -23,6 +23,7 @@ func newFileBackend(path string, key *fileKey) *fileBackend { } func (f *fileBackend) load() (map[string]string, error) { + //nolint:gosec // path is inside the internal secrets store raw, err := os.ReadFile(f.path) // #nosec G304 -- path derived from config dir. if err != nil { if os.IsNotExist(err) { @@ -71,7 +72,7 @@ func (f *fileBackend) store(values map[string]string) error { return fmt.Errorf("finalize encrypted secrets: %w", err) } - return atomicWriteFile(f.path, buf.Bytes(), 0o600) + return atomicWriteFile(f.path, buf.Bytes()) } func (f *fileBackend) set(key, value string) error { diff --git a/pkg/secrets/index.go b/pkg/secrets/index.go index 6472d7cc9f..da536d2507 100644 --- a/pkg/secrets/index.go +++ b/pkg/secrets/index.go @@ -30,6 +30,7 @@ type index struct { func loadIndex(path string) (*index, error) { idx := &index{path: path, data: indexData{Contexts: map[string]map[string]SecretMeta{}}} + //nolint:gosec // path is inside the internal secrets store raw, err := os.ReadFile(path) // #nosec G304 -- path derived from config dir. if err != nil { if os.IsNotExist(err) { @@ -120,7 +121,7 @@ func (i *index) save() error { return fmt.Errorf("marshal secrets index: %w", err) } - return atomicWriteFile(i.path, out, 0o600) + return atomicWriteFile(i.path, out) } func (i *index) put(meta SecretMeta) { diff --git a/pkg/secrets/lock.go b/pkg/secrets/lock.go index a5eeb8e6e1..2bf034fcd9 100644 --- a/pkg/secrets/lock.go +++ b/pkg/secrets/lock.go @@ -22,6 +22,7 @@ const ( // NOT reentrant: each call opens its own flock handle, so taking a lock while // one is already held on the same path in this process self-deadlocks. func acquireFlock(dir, name string) (func(), error) { + //nolint:gosec // lock file is inside the internal secrets store if err := os.MkdirAll(dir, 0o700); err != nil { return nil, fmt.Errorf("create secrets dir: %w", err) } diff --git a/pkg/secrets/rekey.go b/pkg/secrets/rekey.go index e462649950..f7d6180e74 100644 --- a/pkg/secrets/rekey.go +++ b/pkg/secrets/rekey.go @@ -109,7 +109,7 @@ func restoreRekeyFile(dir, backup, target string, existed bool) error { if err != nil { return fmt.Errorf("read rekey rollback backup: %w", err) } - return atomicWriteFile(filepath.Join(dir, target), raw, 0o600) + return atomicWriteFile(filepath.Join(dir, target), raw) } func removeRekeyFile(path string) error { @@ -230,7 +230,7 @@ func writeRekeyJournal(dir string, j rekeyJournal) error { if err != nil { return err } - if err := atomicWriteFile(filepath.Join(dir, rekeyJournalName), raw, 0o600); err != nil { + if err := atomicWriteFile(filepath.Join(dir, rekeyJournalName), raw); err != nil { return err } return syncRekeyDir(dir) @@ -247,7 +247,7 @@ func backupRekeyFile(dir, target, backup string) (bool, error) { if err != nil { return false, err } - return true, atomicWriteFile(filepath.Join(dir, backup), raw, 0o600) + return true, atomicWriteFile(filepath.Join(dir, backup), raw) } func validateRekeyJournal(j rekeyJournal) error { diff --git a/pkg/selfupdate/errors_test.go b/pkg/selfupdate/errors_test.go index 0e8d8b51b4..c3aba71018 100644 --- a/pkg/selfupdate/errors_test.go +++ b/pkg/selfupdate/errors_test.go @@ -20,8 +20,7 @@ func TestClassifyGitHubError_RateLimit(t *testing.T) { if !stderrs.Is(err, clierr.ErrRateLimited) { t.Fatalf("expected ErrRateLimited, got %v", err) } - var rlErr *github.RateLimitError - if !stderrs.As(err, &rlErr) { + if _, ok := stderrs.AsType[*github.RateLimitError](err); !ok { t.Fatal("original *github.RateLimitError should remain in the chain") } } diff --git a/pkg/shell/shell.go b/pkg/shell/shell.go index babf371648..6be8172baa 100644 --- a/pkg/shell/shell.go +++ b/pkg/shell/shell.go @@ -165,6 +165,7 @@ func getUserShell(userName string) (string, error) { if err != nil { return "", err } + //nolint:gosec // fixed binary; username resolved from the local user database output, err := exec.Command("getent", "passwd", currentUser.Username).Output() if err != nil { return "", err diff --git a/pkg/ssh/config.go b/pkg/ssh/config.go index 597cbeae6d..ecaebc35b1 100644 --- a/pkg/ssh/config.go +++ b/pkg/ssh/config.go @@ -454,6 +454,7 @@ func removeFromConfig(path, host string) (string, error) { func transformHostSection(path, host string, transform func(line string) string) (string, error) { var reader io.Reader + //nolint:gosec // path is the user ssh config f, err := os.Open(path) if err != nil { if !os.IsNotExist(err) { diff --git a/pkg/ssh/config_test.go b/pkg/ssh/config_test.go index c98c2b5f86..8f235f8e76 100644 --- a/pkg/ssh/config_test.go +++ b/pkg/ssh/config_test.go @@ -12,6 +12,11 @@ import ( "github.com/stretchr/testify/suite" ) +const ( + newhost = "newhost" + workspaceValue = "workspace" +) + type SSHConfigTestSuite struct { suite.Suite } @@ -650,10 +655,10 @@ func TestAddHostSectionSectionSyntaxVariants(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result, err := addHostSection(tt.config, testExecPath, addHostParams{ - host: "newhost", + host: newhost, user: testSSHUser, context: testSSHContext, - workspace: "workspace", + workspace: workspaceValue, }) assert.NoError(t, err) assert.True(t, strings.HasPrefix(result, "# Devsy Start newhost\nHost newhost")) @@ -672,16 +677,16 @@ Host existing.devsy "host *\n User alice\n\n"+existingBlock, testExecPath, addHostParams{ - host: "newhost", + host: newhost, user: testSSHUser, context: testSSHContext, - workspace: "workspace", + workspace: workspaceValue, }, ) assert.NoError(t, err) assert.Contains(t, result, existingBlock) - newStart := strings.Index(result, MarkerStartPrefix+"newhost") + newStart := strings.Index(result, MarkerStartPrefix+newhost) existingStart := strings.Index(result, MarkerStartPrefix+"existing.devsy") assert.NotEqual(t, -1, newStart) assert.NotEqual(t, -1, existingStart) @@ -696,10 +701,10 @@ func TestAddHostSectionInsertionBoundaries(t *testing.T) { assert.Equal(t, tt.insertionIndex, position) result, err := addHostSection(tt.config, testExecPath, addHostParams{ - host: "newhost", + host: newhost, user: testSSHUser, context: testSSHContext, - workspace: "workspace", + workspace: workspaceValue, }) assert.NoError(t, err) diff --git a/pkg/ssh/forward.go b/pkg/ssh/forward.go index 65efb05165..92b03c7d2a 100644 --- a/pkg/ssh/forward.go +++ b/pkg/ssh/forward.go @@ -27,23 +27,34 @@ type forwardTarget struct { type ForwardingFunction func(context.Context, net.Conn, *ssh.Client, forwardTarget) +// PortForwardOpts names both ends of a forwarded connection. For a reverse +// forward the local end is the one dialed and the remote end is the one listened on. +type PortForwardOpts struct { + LocalNetwork string + LocalAddr string + RemoteNetwork string + RemoteAddr string + ExitAfterTimeout time.Duration +} + func PortForward( ctx context.Context, client *ssh.Client, - localNetwork, localAddr, remoteNetwork, remoteAddr string, - exitAfterTimeout time.Duration, + opts PortForwardOpts, ) error { - listener, err := net.Listen(localNetwork, localAddr) + listener, err := net.Listen(opts.LocalNetwork, opts.LocalAddr) if err != nil { return err } defer func() { _ = listener.Close() }() - return portForwarding( - ctx, client, listener, - localAddr, remoteNetwork, remoteAddr, - exitAfterTimeout, forward, - ) + return portForwarding(ctx, client, listener, forwardRoute{ + srcAddr: opts.LocalAddr, + dstNetwork: opts.RemoteNetwork, + dstAddr: opts.RemoteAddr, + exitAfterTimeout: opts.ExitAfterTimeout, + forwardFn: forward, + }) } // ForwardOpts groups the parameters for PortForwardWithListener so callers @@ -66,29 +77,30 @@ func PortForwardWithListener( ) error { defer func() { _ = opts.Listener.Close() }() - return portForwarding( - ctx, client, opts.Listener, - opts.Listener.Addr().String(), "tcp", opts.RemoteAddr, - opts.ExitAfterTimeout, forward, - ) + return portForwarding(ctx, client, opts.Listener, forwardRoute{ + srcAddr: opts.Listener.Addr().String(), + dstNetwork: "tcp", + dstAddr: opts.RemoteAddr, + exitAfterTimeout: opts.ExitAfterTimeout, + forwardFn: forward, + }) } func ReversePortForward( ctx context.Context, client *ssh.Client, - remoteNetwork, remoteAddr, localNetwork, localAddr string, - exitAfterTimeout time.Duration, + opts PortForwardOpts, ) error { - listener, err := ReverseListen(client, remoteNetwork, remoteAddr) + listener, err := ReverseListen(client, opts.RemoteNetwork, opts.RemoteAddr) if err != nil { return err } return RunReverseForward(ctx, client, ReverseForwardOpts{ Listener: listener, - RemoteAddr: remoteAddr, - LocalNetwork: localNetwork, - LocalAddr: localAddr, - ExitAfterTimeout: exitAfterTimeout, + RemoteAddr: opts.RemoteAddr, + LocalNetwork: opts.LocalNetwork, + LocalAddr: opts.LocalAddr, + ExitAfterTimeout: opts.ExitAfterTimeout, }) } @@ -110,21 +122,34 @@ type ReverseForwardOpts struct { // ReverseListen, closing it on return. func RunReverseForward(ctx context.Context, client *ssh.Client, opts ReverseForwardOpts) error { defer func() { _ = opts.Listener.Close() }() - return portForwarding( - ctx, client, opts.Listener, - opts.RemoteAddr, opts.LocalNetwork, opts.LocalAddr, - opts.ExitAfterTimeout, reverseForward, - ) + return portForwarding(ctx, client, opts.Listener, forwardRoute{ + srcAddr: opts.RemoteAddr, + dstNetwork: opts.LocalNetwork, + dstAddr: opts.LocalAddr, + exitAfterTimeout: opts.ExitAfterTimeout, + forwardFn: reverseForward, + }) +} + +// forwardRoute describes where a forwarded connection comes from and goes to. +type forwardRoute struct { + srcAddr string + dstNetwork string + dstAddr string + exitAfterTimeout time.Duration + forwardFn ForwardingFunction } func portForwarding( ctx context.Context, client *ssh.Client, listener net.Listener, - srcAddr, dstNetwork, dstAddr string, - exitAfterTimeout time.Duration, - forwardFn ForwardingFunction, + route forwardRoute, ) error { + srcAddr, dstAddr := route.srcAddr, route.dstAddr + dstNetwork, forwardFn := route.dstNetwork, route.forwardFn + exitAfterTimeout := route.exitAfterTimeout + // Derive a child context so the idle-timeout handler can signal shutdown // with a typed cause (ErrIdleTimeout) without killing the whole process. fwdCtx, cancel := context.WithCancelCause(ctx) diff --git a/pkg/ssh/forward_test.go b/pkg/ssh/forward_test.go index 4271ea7928..628be547d4 100644 --- a/pkg/ssh/forward_test.go +++ b/pkg/ssh/forward_test.go @@ -14,6 +14,8 @@ import ( // noopForward is a ForwardingFunction that immediately closes the local // connection. This lets portForwarding exercise its idle/cancellation paths // without needing a real *ssh.Client. +const networkTCP = "tcp" + func noopForward(_ context.Context, localConn net.Conn, _ *ssh.Client, _ forwardTarget) { _ = localConn.Close() } @@ -32,11 +34,13 @@ func TestPortForwarding_IdleTimeoutReturnsErrIdleTimeout(t *testing.T) { errCh := make(chan error, 1) go func() { - errCh <- portForwarding( - ctx, nil, lis, - lis.Addr().String(), "tcp", "127.0.0.1:0", - 15*time.Millisecond, noopForward, - ) + errCh <- portForwarding(ctx, nil, lis, forwardRoute{ + srcAddr: lis.Addr().String(), + dstNetwork: networkTCP, + dstAddr: "127.0.0.1:0", + exitAfterTimeout: 15 * time.Millisecond, + forwardFn: noopForward, + }) }() // Open and close a connection so the connection counter starts the idle @@ -69,12 +73,14 @@ func TestPortForwarding_ParentCancelNotIdleTimeout(t *testing.T) { errCh := make(chan error, 1) go func() { - errCh <- portForwarding( - ctx, nil, lis, - lis.Addr().String(), "tcp", "127.0.0.1:0", + errCh <- portForwarding(ctx, nil, lis, forwardRoute{ + srcAddr: lis.Addr().String(), + dstNetwork: networkTCP, + dstAddr: "127.0.0.1:0", // Use a long idle timeout so it cannot fire during the test. - 10*time.Second, noopForward, - ) + exitAfterTimeout: 10 * time.Second, + forwardFn: noopForward, + }) }() // Cancel before any idle timer could be relevant. @@ -163,7 +169,12 @@ func TestPortForwarding_ReleasesListenerOnTransportDeath(t *testing.T) { done := make(chan error, 1) go func() { - done <- portForwarding(t.Context(), client, lis, addr, "tcp", "127.0.0.1:9", 0, forward) + done <- portForwarding(t.Context(), client, lis, forwardRoute{ + srcAddr: addr, + dstNetwork: networkTCP, + dstAddr: "127.0.0.1:9", + forwardFn: forward, + }) }() time.Sleep(100 * time.Millisecond) diff --git a/pkg/ssh/helper.go b/pkg/ssh/helper.go index ef4c7a6645..f14d4fd22b 100644 --- a/pkg/ssh/helper.go +++ b/pkg/ssh/helper.go @@ -35,7 +35,7 @@ func handleKeepAliveRequests(in <-chan *ssh.Request) <-chan *ssh.Request { func NewSSHPassClient(user, addr, password string) (*ssh.Client, error) { clientConfig := &ssh.ClientConfig{ Auth: []ssh.AuthMethod{}, - HostKeyCallback: ssh.InsecureIgnoreHostKey(), + HostKeyCallback: ssh.InsecureIgnoreHostKey(), //nolint:gosec // tunnelled, no known_hosts } clientConfig.Auth = append(clientConfig.Auth, ssh.Password(password)) @@ -233,7 +233,7 @@ func handshakeWithIdleTimeout( func ConfigFromKeyBytes(keyBytes []byte) (*ssh.ClientConfig, error) { clientConfig := &ssh.ClientConfig{ Auth: []ssh.AuthMethod{}, - HostKeyCallback: ssh.InsecureIgnoreHostKey(), + HostKeyCallback: ssh.InsecureIgnoreHostKey(), //nolint:gosec // tunnelled, no known_hosts } // key file authentication? @@ -366,8 +366,7 @@ func handleRunError(ctx context.Context, err error, command string) error { } // Check for exit errors with exit codes - var exitErr *ssh.ExitError - if errors.As(err, &exitErr) { + if exitErr, ok := errors.AsType[*ssh.ExitError](err); ok { exitCode := exitErr.ExitStatus() if isSignalInterrupt(exitCode) { diff --git a/pkg/ssh/keys.go b/pkg/ssh/keys.go index 8d790e28ed..0732798bda 100644 --- a/pkg/ssh/keys.go +++ b/pkg/ssh/keys.go @@ -161,6 +161,7 @@ func GetPrivateKeyRawBase(dir string) ([]byte, error) { } // read private key + //nolint:gosec // path is a user key file chosen by config out, err := os.ReadFile(privateKeyFile) if err != nil { return nil, fmt.Errorf("read private ssh key: %w", err) @@ -195,6 +196,7 @@ func GetHostKeyBase(dir string) (string, error) { } // read public key + //nolint:gosec // path is a user key file chosen by config out, err := os.ReadFile(hostKeyFile) if err != nil { return "", fmt.Errorf("read host ssh key: %w", err) @@ -236,6 +238,7 @@ func GetPublicKeyBase(dir string) (string, error) { } // read public key + //nolint:gosec // path is a user key file chosen by config out, err := os.ReadFile(publicKeyFile) if err != nil { return "", fmt.Errorf("read public ssh key: %w", err) diff --git a/pkg/ssh/server/agent.go b/pkg/ssh/server/agent.go index 87b699af04..90be05b284 100644 --- a/pkg/ssh/server/agent.go +++ b/pkg/ssh/server/agent.go @@ -39,7 +39,8 @@ func setupAgentListener(reuseSock string) (net.Listener, string, error) { } // Check if we should create a "shared" socket to be reused by clients - // used for browser tunnels such as openvscode, since the IDE itself doesn't create an SSH connection it uses a "backhaul" connection and uses the existing socket + // used for browser tunnels such as openvscode, since the IDE itself doesn't create an SSH connection it uses a + // "backhaul" connection and uses the existing socket dir := "" if reuseSock != "" { dir = filepath.Join(runtimeDir, fmt.Sprintf("auth-agent-%s", reuseSock)) diff --git a/pkg/ssh/server/exit.go b/pkg/ssh/server/exit.go index 0a1bfe9d16..294c895851 100644 --- a/pkg/ssh/server/exit.go +++ b/pkg/ssh/server/exit.go @@ -11,8 +11,7 @@ import ( func exitWithError(sess ssh.Session, err error) { if err != nil { - var exitError *exec.ExitError - if !errors.As(err, &exitError) { + if _, ok := errors.AsType[*exec.ExitError](err); !ok { log.Errorf("Exit error: %v", err) msg := strings.TrimPrefix(err.Error(), "exec: ") if _, err := sess.Stderr().Write([]byte(msg)); err != nil { @@ -33,8 +32,8 @@ func exitCode(err error) int { return 0 } - var exitErr *exec.ExitError - if !errors.As(err, &exitErr) { + exitErr, ok := errors.AsType[*exec.ExitError](err) + if !ok { return 1 } diff --git a/pkg/ssh/server/ssh.go b/pkg/ssh/server/ssh.go index c50267a596..74ec338605 100644 --- a/pkg/ssh/server/ssh.go +++ b/pkg/ssh/server/ssh.go @@ -155,13 +155,15 @@ type server struct { sshServer ssh.Server } -func NewServer( - addr string, - hostKey []byte, - keys []ssh.PublicKey, - workdir string, - reuseSock string, -) (Server, error) { +// ServerOptions configures NewServer. +type ServerOptions struct { + HostKey []byte + Keys []ssh.PublicKey + Workdir string + ReuseSock string +} + +func NewServer(addr string, opts ServerOptions) (Server, error) { sh, err := shell.GetShell("") if err != nil { return nil, err @@ -174,18 +176,18 @@ func NewServer( server := &server{ shell: sh, - workdir: workdir, - reuseSock: reuseSock, + workdir: opts.Workdir, + reuseSock: opts.ReuseSock, currentUser: currentUser.Username, sshServer: buildSSHServer(addr, currentUser.Username), } - if len(keys) > 0 { - server.sshServer.PublicKeyHandler = makePublicKeyHandler(keys) + if len(opts.Keys) > 0 { + server.sshServer.PublicKeyHandler = makePublicKeyHandler(opts.Keys) } - if len(hostKey) > 0 { - err = server.sshServer.SetOption(ssh.HostKeyPEM(hostKey)) + if len(opts.HostKey) > 0 { + err = server.sshServer.SetOption(ssh.HostKeyPEM(opts.HostKey)) if err != nil { return nil, err } diff --git a/pkg/ssh/server/ssh_container.go b/pkg/ssh/server/ssh_container.go index 754feb8d53..6af95c2a73 100644 --- a/pkg/ssh/server/ssh_container.go +++ b/pkg/ssh/server/ssh_container.go @@ -143,9 +143,11 @@ func (s *containerServer) getCommand(sess ssh.Session, isPty bool) (*exec.Cmd, e } if len(sess.RawCommand()) == 0 { + //nolint:gosec // command is built from fixed arguments by design cmd = exec.Command(shell[0], args...) } else { args = append(args, "-c", sess.RawCommand()) + //nolint:gosec // command is built from fixed arguments by design cmd = exec.Command(shell[0], args...) } diff --git a/pkg/ssh/ssh_add.go b/pkg/ssh/ssh_add.go index bbfb45d613..d9565e1084 100644 --- a/pkg/ssh/ssh_add.go +++ b/pkg/ssh/ssh_add.go @@ -30,7 +30,10 @@ func AddPrivateKeysToAgent(ctx context.Context) error { for _, privateKey := range privateKeys { timeoutCtx, cancel := context.WithTimeout(ctx, time.Second*2) log.Debugf("running ssh-add: privateKeyPath=%s", privateKey) - out, err := exec.CommandContext(timeoutCtx, "ssh-add", privateKey).CombinedOutput() + //nolint:gosec // key path comes from the ssh config + out, err := exec.CommandContext(timeoutCtx, "ssh-add", privateKey). + CombinedOutput() + //nolint:gosec // key path comes from the ssh config cancel() if err != nil { log.Debugf( @@ -63,6 +66,7 @@ func FindPrivateKeys() ([]string, error) { } keyPath := filepath.Join(sshDir, entry.Name()) + //nolint:gosec // path is the user key file out, err := os.ReadFile(keyPath) if err == nil { _, err = ssh.ParsePrivateKey(out) diff --git a/pkg/subprocess/capture.go b/pkg/subprocess/capture.go index df57ee83a6..65c2a9688d 100644 --- a/pkg/subprocess/capture.go +++ b/pkg/subprocess/capture.go @@ -31,8 +31,12 @@ type Options struct { // Stdout optionally receives complete, unredacted stdout for a protocol // consumer. It must not be a diagnostic log sink. Result capture remains // bounded and redacted independently. - Stdout io.Writer - Redactor *secrets.Redactor + Stdout io.Writer + // Diagnostic optionally receives redacted copies of each stream for + // retained history. Its writers are best effort: errors are ignored and + // never change the command result. Unlike Stdout it may be a log sink. + Diagnostic DiagnosticSink + Redactor *secrets.Redactor // SensitiveValues contains secret values that may appear in argv or in // subprocess output but are not represented by a sensitive environment // variable. Values are masked before capture and command rendering. @@ -42,6 +46,36 @@ type Options struct { OperationID string } +// DiagnosticSink holds optional per-stream destinations for redacted output. +type DiagnosticSink struct { + Stdout io.Writer + Stderr io.Writer +} + +// streams carries the optional destinations of one command run. +type streams struct { + stdout io.Writer + diagnostic DiagnosticSink +} + +// bestEffortWriter drops write errors so a failing sink cannot fail the +// command that produced the output. +type bestEffortWriter struct{ next io.Writer } + +func (w bestEffortWriter) Write(p []byte) (int, error) { + _, _ = w.next.Write(p) + return len(p), nil +} + +// diagnosticWriter wraps sink so it receives redacted text and reports its +// final suffix on flush. It returns nil when there is no sink. +func diagnosticWriter(sink io.Writer, redactor *secrets.Redactor) *StreamingRedactingWriter { + if sink == nil { + return nil + } + return &StreamingRedactingWriter{Next: bestEffortWriter{next: sink}, Redactor: redactor} +} + // RedactingWriter forwards output after applying redaction. It is useful when // a subprocess must stream diagnostics while retaining the same safety policy // as captured output. @@ -146,7 +180,7 @@ func (r Result) DiagnosticOutput() string { if !r.Truncated || output == "" { return output } - return output + "\n(additional output omitted; use --verbose for full command output)" + return output + "\n(additional output omitted)" } // Run executes binary with args and captures bounded, redacted output from @@ -167,7 +201,9 @@ func Run(ctx context.Context, binary string, args []string, options Options) (Re } cmd.Stdin = options.Stdin redactor := commandRedactor(options) - result, err := runCommand(cmd, redactor, options.Stdout) + result, err := runCommand(cmd, redactor, streams{ + stdout: options.Stdout, diagnostic: options.Diagnostic, + }) err = withContextError(ctx, err) result.OperationID = options.OperationID return result, err @@ -206,20 +242,19 @@ func withContextError(ctx context.Context, err error) error { // stdout and stderr capture. Existing command setup such as an explicit // environment, working directory, or stdin is preserved. func RunCommand(cmd *exec.Cmd, redactor *secrets.Redactor) (Result, error) { - return runCommand(cmd, redactor, nil) + return runCommand(cmd, redactor, streams{}) } -func runCommand(cmd *exec.Cmd, redactor *secrets.Redactor, stdout io.Writer) (Result, error) { +func runCommand(cmd *exec.Cmd, redactor *secrets.Redactor, dest streams) (Result, error) { if cmd == nil { return Result{}, errors.New("subprocess command is nil") } out := NewBuffer(redactor) errOut := NewBuffer(redactor) - cmd.Stdout = out - if stdout != nil { - cmd.Stdout = io.MultiWriter(out, stdout) - } - cmd.Stderr = errOut + outDiag := diagnosticWriter(dest.diagnostic.Stdout, redactor) + errDiag := diagnosticWriter(dest.diagnostic.Stderr, redactor) + cmd.Stdout = teeWriter(out, dest.stdout, outDiag) + cmd.Stderr = teeWriter(errOut, nil, errDiag) binary := cmd.Path args := []string(nil) if len(cmd.Args) > 0 { @@ -231,6 +266,10 @@ func runCommand(cmd *exec.Cmd, redactor *secrets.Redactor, stdout io.Writer) (Re started := time.Now() err := cmd.Run() + // Flush before publishing the outcome so trailing bytes are not lost on + // any exit path, including a failed start. + flushDiagnostic(outDiag) + flushDiagnostic(errDiag) result := Result{ Command: append([]string(nil), cmd.Args...), DisplayCommand: displayCommand(binary, args, redactor), @@ -251,6 +290,28 @@ func runCommand(cmd *exec.Cmd, redactor *secrets.Redactor, stdout io.Writer) (Re return result, nil } +// teeWriter returns a writer that feeds the bounded buffer first, then the +// optional functional and diagnostic destinations. +func teeWriter(buf *Buffer, functional io.Writer, diag *StreamingRedactingWriter) io.Writer { + writers := []io.Writer{buf} + if functional != nil { + writers = append(writers, functional) + } + if diag != nil { + writers = append(writers, diag) + } + if len(writers) == 1 { + return buf + } + return io.MultiWriter(writers...) +} + +func flushDiagnostic(w *StreamingRedactingWriter) { + if w != nil { + _ = w.Flush() + } +} + func processSignal(state *os.ProcessState) string { if state == nil { return "" diff --git a/pkg/subprocess/capture_test.go b/pkg/subprocess/capture_test.go index 31ee966dfb..d8a444baa0 100644 --- a/pkg/subprocess/capture_test.go +++ b/pkg/subprocess/capture_test.go @@ -266,8 +266,8 @@ func TestDiagnosticOutputAnnotatesTruncatedTail(t *testing.T) { if !strings.Contains(got, "warning\nlast useful line") { t.Fatalf("diagnostic output = %q, want stderr before stdout", got) } - if !strings.Contains(got, "additional output omitted") || !strings.Contains(got, "--verbose") { - t.Fatalf("diagnostic output = %q, want truncation guidance", got) + if !strings.Contains(got, "additional output omitted") || strings.Contains(got, "--verbose") { + t.Fatalf("diagnostic output = %q, want neutral truncation text", got) } } diff --git a/pkg/subprocess/diagnostic_test.go b/pkg/subprocess/diagnostic_test.go new file mode 100644 index 0000000000..b1f1041400 --- /dev/null +++ b/pkg/subprocess/diagnostic_test.go @@ -0,0 +1,137 @@ +package subprocess + +import ( + "bytes" + "context" + "errors" + "runtime" + "strings" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/secrets" +) + +type failingWriter struct{ calls int } + +func (w *failingWriter) Write([]byte) (int, error) { + w.calls++ + return 0, errors.New("sink unavailable") +} + +func skipWithoutShell(t *testing.T) { + t.Helper() + if runtime.GOOS == testWindows { + t.Skip("test command uses sh") + } +} + +func TestDiagnosticSinkReceivesFullOutputWhileResultStaysBounded(t *testing.T) { + skipWithoutShell(t) + lines := MaxCapturedLines * 3 + var out bytes.Buffer + result, err := Run(context.Background(), "sh", + []string{"-c", "i=0; while [ $i -lt 600 ]; do echo line-$i; i=$((i+1)); done"}, + Options{Diagnostic: DiagnosticSink{Stdout: &out}}) + if err != nil { + t.Fatal(err) + } + if got := strings.Count(out.String(), "\n"); got != lines { + t.Fatalf("sink received %d lines, want %d", got, lines) + } + if !strings.HasPrefix(out.String(), "line-0\n") { + t.Fatal("sink lost the head of the output") + } + if !result.Truncated || strings.Count(result.Stdout, "\n") > MaxCapturedLines { + t.Fatal("result must keep its bounded tail") + } +} + +func TestDiagnosticSinkKeepsStreamsSeparateAndFlushesFragment(t *testing.T) { + skipWithoutShell(t) + var out, errOut bytes.Buffer + _, err := Run(context.Background(), "sh", + []string{"-c", "printf 'to-out\\n'; printf 'to-err\\n' >&2; printf 'no-newline'"}, + Options{Diagnostic: DiagnosticSink{Stdout: &out, Stderr: &errOut}}) + if err != nil { + t.Fatal(err) + } + if out.String() != "to-out\nno-newline" { + t.Fatalf("stdout sink = %q", out.String()) + } + if errOut.String() != "to-err\n" { + t.Fatalf("stderr sink = %q", errOut.String()) + } +} + +func TestDiagnosticSinkMasksSecretSplitAcrossWrites(t *testing.T) { + skipWithoutShell(t) + var out bytes.Buffer + script := "printf 'key=DEVSY_SECRET_TEST_'; sleep 0.1; printf '846297 done\\n'" + _, err := Run(context.Background(), "sh", []string{"-c", script}, Options{ + Redactor: secrets.NewRedactor([]string{captureToken + captureTestSecret}), + Diagnostic: DiagnosticSink{Stdout: &out}, + }) + if err != nil { + t.Fatal(err) + } + if strings.Contains(out.String(), "846297") || !strings.Contains(out.String(), "done") { + t.Fatalf("sink output = %q, want secret masked and text kept", out.String()) + } +} + +func TestDiagnosticSinkFailureDoesNotChangeOutcome(t *testing.T) { + skipWithoutShell(t) + sink := &failingWriter{} + result, err := Run(context.Background(), "sh", []string{"-c", "echo hello"}, + Options{Diagnostic: DiagnosticSink{Stdout: sink}}) + if err != nil || result.ExitCode != 0 || result.Stdout != "hello\n" { + t.Fatalf("result = %+v, err = %v; a failing sink must not alter the command", result, err) + } + if sink.calls == 0 { + t.Fatal("sink was never written") + } +} + +func TestDiagnosticSinkPreservesExitCodeAndFunctionalStdout(t *testing.T) { + skipWithoutShell(t) + var protocol, sink bytes.Buffer + result, err := Run( + context.Background(), + "sh", + []string{"-c", "printf 'a\\nb'; exit 7"}, + Options{ + Stdout: &protocol, + Diagnostic: DiagnosticSink{Stdout: &sink}, + }, + ) + if err == nil || result.ExitCode != 7 { + t.Fatalf("result = %+v, err = %v, want exit 7", result, err) + } + if protocol.String() != "a\nb" || sink.String() != "a\nb" { + t.Fatalf("protocol = %q, sink = %q", protocol.String(), sink.String()) + } +} + +func TestDiagnosticSinkOnStartFailureAndCancellation(t *testing.T) { + var sink bytes.Buffer + opts := Options{Diagnostic: DiagnosticSink{Stdout: &sink, Stderr: &sink}} + if _, err := Run( + context.Background(), + "definitely-not-a-binary-846297", + nil, + opts, + ); err == nil { + t.Fatal("start failure should return an error") + } + skipWithoutShell(t) + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond) + defer cancel() + result, err := Run(ctx, "sh", []string{"-c", "echo before; sleep 5"}, opts) + if !errors.Is(err, context.DeadlineExceeded) || result.Signal == "" { + t.Fatalf("result = %+v, err = %v, want a canceled signaled command", result, err) + } + if !strings.Contains(sink.String(), "before") { + t.Fatalf("sink = %q, want output written before cancellation", sink.String()) + } +} diff --git a/pkg/table/table_test.go b/pkg/table/table_test.go index 0878f8c2c9..5897df1757 100644 --- a/pkg/table/table_test.go +++ b/pkg/table/table_test.go @@ -5,8 +5,13 @@ import ( "testing" ) +const ( + nameHeader = "Name" + ageHeader = "Age" +) + func TestRender(t *testing.T) { - headers := []string{"Name", "Age"} + headers := []string{nameHeader, ageHeader} rows := [][]string{ {"Alice", "30"}, {"Bob", "25"}, @@ -23,13 +28,13 @@ func TestRender(t *testing.T) { if !strings.Contains(out, "Name") { t.Errorf("expected output to contain header 'Name', got:\n%s", out) } - if !strings.Contains(out, "Age") { + if !strings.Contains(out, ageHeader) { t.Errorf("expected output to contain header 'Age', got:\n%s", out) } } func TestRenderEmpty(t *testing.T) { - headers := []string{"Name", "Age"} + headers := []string{nameHeader, ageHeader} rows := [][]string{} out := Render(headers, rows) @@ -41,7 +46,7 @@ func TestRenderEmpty(t *testing.T) { func TestMarkdown(t *testing.T) { got := Markdown( - []string{"Name", "Age"}, + []string{nameHeader, ageHeader}, [][]string{{"Alice", "30"}, {"Bob", "25"}}, ) want := "| Name | Age |\n| --- | --- |\n| Alice | 30 |\n| Bob | 25 |\n" diff --git a/pkg/task/runlogs.go b/pkg/task/runlogs.go new file mode 100644 index 0000000000..bf06904078 --- /dev/null +++ b/pkg/task/runlogs.go @@ -0,0 +1,36 @@ +package task + +import "github.com/devsy-org/devsy/pkg/workspacelogs" + +// runFailureContext returns the run identity to attach to a failed task's +// error context. Lookup problems are not task failures, so they yield whatever +// identity is known. +func runFailureContext(runID string) map[string]string { + if runID == "" { + return nil + } + store, err := workspacelogs.OpenDefault() + if err != nil { + return map[string]string{workspacelogs.ContextRunID: runID} + } + + return store.FailureContext(runID) +} + +// withRunContext adds run identity to an error context without replacing keys +// the failure already carries. +func withRunContext(base, run map[string]string) map[string]string { + if len(run) == 0 { + return base + } + if base == nil { + base = make(map[string]string, len(run)) + } + for k, v := range run { + if _, exists := base[k]; !exists { + base[k] = v + } + } + + return base +} diff --git a/pkg/task/runlogs_test.go b/pkg/task/runlogs_test.go new file mode 100644 index 0000000000..ca077ce6a5 --- /dev/null +++ b/pkg/task/runlogs_test.go @@ -0,0 +1,84 @@ +package task + +import ( + "errors" + "testing" + + "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func useTempHome(t *testing.T) { + t.Helper() + t.Setenv(config.EnvHome, t.TempDir()) + config.ResetPathManager() + t.Cleanup(config.ResetPathManager) +} + +func recordedRun(t *testing.T, failures int) string { + t.Helper() + store, err := workspacelogs.OpenDefault() + require.NoError(t, err) + m, err := store.CreateRun(workspacelogs.RunSpec{Context: "default", Command: "up"}) + require.NoError(t, err) + rec, err := workspacelogs.NewRecorder(workspacelogs.RecorderOptions{ + Backend: store, RunID: m.RunID, Location: workspacelogs.LocationContainer, + }) + require.NoError(t, err) + for range failures { + rec.Invocation(workspacelogs.Invocation{ + ID: workspacelogs.NewID(), + Hook: "postCreateCommand", + State: workspacelogs.InvocationFailed, + }) + } + rec.Close(workspacelogs.ProducerTerminal, true) + + return m.RunID +} + +func TestFailedTaskCarriesRunAndUniqueFailedInvocation(t *testing.T) { + useTempHome(t) + runID := recordedRun(t, 1) + store := newTestStore(t) + tk, err := store.Create(CreateOptions{Command: "up", WorkspaceID: "ws", RunID: runID}) + require.NoError(t, err) + + require.NoError(t, tk.Fail(errors.New("hook failed"))) + + state, err := store.Get(tk.ID()) + require.NoError(t, err) + assert.Equal(t, runID, state.RunID) + assert.Equal(t, runID, state.ErrorContext[workspacelogs.ContextRunID]) + assert.NotEmpty(t, state.ErrorContext[workspacelogs.ContextFailedInvocationID]) +} + +func TestFailedTaskOmitsInvocationWhenSeveralFailed(t *testing.T) { + useTempHome(t) + runID := recordedRun(t, 2) + store := newTestStore(t) + tk, err := store.Create(CreateOptions{Command: "up", RunID: runID}) + require.NoError(t, err) + + require.NoError(t, tk.Fail(errors.New("boom"))) + + state, err := store.Get(tk.ID()) + require.NoError(t, err) + assert.Equal(t, runID, state.ErrorContext[workspacelogs.ContextRunID]) + assert.NotContains(t, state.ErrorContext, workspacelogs.ContextFailedInvocationID) +} + +func TestFailedTaskWithoutRunKeepsContextUnchanged(t *testing.T) { + useTempHome(t) + store := newTestStore(t) + tk, err := store.Create(CreateOptions{Command: "up"}) + require.NoError(t, err) + + require.NoError(t, tk.Fail(errors.New("boom"))) + + state, err := store.Get(tk.ID()) + require.NoError(t, err) + assert.NotContains(t, state.ErrorContext, workspacelogs.ContextRunID) +} diff --git a/pkg/task/store.go b/pkg/task/store.go index e48151a84d..2d67b5562c 100644 --- a/pkg/task/store.go +++ b/pkg/task/store.go @@ -40,6 +40,7 @@ func NewStore() (*Store, error) { } func NewStoreAt(dir string) (*Store, error) { + //nolint:gosec // path is inside the internal task store if err := os.MkdirAll(dir, 0o750); err != nil { return nil, fmt.Errorf("create task dir: %w", err) } @@ -689,6 +690,7 @@ func (s *Store) write(state *State) error { return fmt.Errorf("create temp task file: %w", err) } tmpPath := tmp.Name() + //nolint:gosec // path is inside the internal task store defer func() { _ = os.Remove(tmpPath) }() if _, err := tmp.Write(data); err != nil { @@ -711,6 +713,7 @@ func (s *Store) write(state *State) error { // syncDir fsyncs a directory so a rename survives a crash. func syncDir(dir string) { + //nolint:gosec // path is inside the internal task store d, err := os.Open(dir) // #nosec G304 -- store's own task dir. if err != nil { return @@ -732,6 +735,7 @@ func (s *Store) create(opts CreateOptions, launch bool) (*Task, error) { ID: id, Command: opts.Command, WorkspaceID: opts.WorkspaceID, + RunID: opts.RunID, Status: StatusPending, StartedAt: now, UpdatedAt: now, diff --git a/pkg/task/task.go b/pkg/task/task.go index 4bc6da26e2..2fd3afd6da 100644 --- a/pkg/task/task.go +++ b/pkg/task/task.go @@ -46,9 +46,11 @@ func (s Status) Terminal() bool { // doing the work, distinct from whatever process is merely polling this // state. type State struct { - ID string `json:"id"` - Command string `json:"command,omitempty"` - WorkspaceID string `json:"workspaceId,omitempty"` + ID string `json:"id"` + Command string `json:"command,omitempty"` + WorkspaceID string `json:"workspaceId,omitempty"` + // RunID links the task to its retained setup history. + RunID string `json:"runId,omitempty"` Status Status `json:"status"` Phase string `json:"phase,omitempty"` Step string `json:"step,omitempty"` @@ -78,6 +80,8 @@ type State struct { type CreateOptions struct { Command string WorkspaceID string + // RunID is the retained-history run allocated before the task exists. + RunID string } // Task is a handle to a single background task, bound to the Store it was @@ -94,6 +98,17 @@ type Task struct { func (t *Task) ID() string { return t.id } +// RunID returns the retained-history run bound to the task, or empty when the +// task has none or its state cannot be read. +func (t *Task) RunID() string { + state, err := t.store.readState(t.id) + if err != nil { + return "" + } + + return state.RunID +} + func (s *State) Canceled() bool { return s != nil && s.Status == StatusFailed && s.ErrorCode == string(clierr.CodeCanceled) } @@ -527,6 +542,10 @@ func (t *Task) recordFailure(err error, failedLaunch bool) error { message = classified.Message } redactor := secrets.NewEnvironmentRedactor(os.Environ()) + var runContext map[string]string + if current, readErr := t.store.readState(t.id); readErr == nil { + runContext = runFailureContext(current.RunID) + } return t.store.update(t.id, func(s *State) { if failedLaunch { s.markLaunchCleanupPending() @@ -542,6 +561,7 @@ func (t *Task) recordFailure(err error, failedLaunch bool) error { s.ErrorHint = redactor.Redact(classified.Hint) s.ErrorContext = redactContext(classified.Context, redactor) } + s.ErrorContext = withRunContext(s.ErrorContext, runContext) }) } diff --git a/pkg/transport/lifecycle.go b/pkg/transport/lifecycle.go index 58c0128cb1..30868b82ef 100644 --- a/pkg/transport/lifecycle.go +++ b/pkg/transport/lifecycle.go @@ -208,8 +208,7 @@ func isExitMissingErr(err error) bool { if err == nil { return false } - var exitMissing *ssh.ExitMissingError - if errors.As(err, &exitMissing) { + if _, ok := errors.AsType[*ssh.ExitMissingError](err); ok { return true } return strings.Contains(err.Error(), "remote command exited without exit status or exit signal") diff --git a/pkg/tunnel/forwarder.go b/pkg/tunnel/forwarder.go index 9cbad3db59..708a3681a6 100644 --- a/pkg/tunnel/forwarder.go +++ b/pkg/tunnel/forwarder.go @@ -17,6 +17,10 @@ import ( "golang.org/x/crypto/ssh" ) +const ( + tcp = "tcp" +) + // PortAttributeResolver resolves port attributes for a given port string. type PortAttributeResolver func(port string) config2.PortAttribute @@ -75,15 +79,16 @@ func (f *forwarder) Forward(port string, _ netstat.PortForwardAttribute) error { logStartForwarding(port, attr) go func(port string) { - network := "tcp" + network := tcp err := devssh.PortForward( cancelCtx, f.sshClient, - network, - localAddr, - network, - "localhost:"+port, - 0, + devssh.PortForwardOpts{ + LocalNetwork: network, + LocalAddr: localAddr, + RemoteNetwork: network, + RemoteAddr: "localhost:" + port, + }, ) if err != nil { log.Errorf("Error port forwarding %s: %v", port, err) diff --git a/pkg/tunnel/services.go b/pkg/tunnel/services.go index 498663f973..2cf4f1a32b 100644 --- a/pkg/tunnel/services.go +++ b/pkg/tunnel/services.go @@ -107,18 +107,19 @@ func runTunnelServer(ctx context.Context, cancel context.CancelFunc, p tunnelSer defer cancel() defer func() { _ = p.stdoutReader.Close() }() defer func() { _ = p.stdinWriter.Close() }() - err := tunnelserver.RunServicesServer( - ctx, - p.stdoutReader, - p.stdinWriter, - p.opts.ConfigureGitCredentials, - p.opts.ConfigureDockerCredentials, - p.forwarder, - p.opts.Workspace, - tunnelserver.WithPlatformOptions(p.opts.PlatformOptions), - tunnelserver.WithAllowGitHubCredentials(p.opts.ConfigureGitHubCredentials), - tunnelserver.WithGitSSHSigningKey(p.gitSSHSigningKey), - ) + err := tunnelserver.RunServicesServer(ctx, tunnelserver.ServicesSpec{ + Reader: p.stdoutReader, + Writer: p.stdinWriter, + AllowGitCredentials: p.opts.ConfigureGitCredentials, + AllowDockerCredentials: p.opts.ConfigureDockerCredentials, + Forwarder: p.forwarder, + Workspace: p.opts.Workspace, + Options: []tunnelserver.Option{ + tunnelserver.WithPlatformOptions(p.opts.PlatformOptions), + tunnelserver.WithAllowGitHubCredentials(p.opts.ConfigureGitHubCredentials), + tunnelserver.WithGitSSHSigningKey(p.gitSSHSigningKey), + }, + }) if err != nil { p.errChan <- fmt.Errorf("run tunnel server: %w", err) } @@ -460,11 +461,12 @@ func forwardConfigPorts(ctx context.Context, p portForwardParams, result *config if err := devssh.PortForward( ctx, p.containerClient, - "tcp", - fmt.Sprintf("localhost:%d", portNumber), - "tcp", - fmt.Sprintf("%s:%d", host, portNumber), - 0, + devssh.PortForwardOpts{ + LocalNetwork: tcp, + LocalAddr: fmt.Sprintf("localhost:%d", portNumber), + RemoteNetwork: tcp, + RemoteAddr: fmt.Sprintf("%s:%d", host, portNumber), + }, ); err != nil { log.Errorf("error port forwarding %s: %v", port, err) } @@ -520,11 +522,13 @@ func forwardPort(ctx context.Context, p singlePortForwardParams) []string { err = devssh.PortForward( ctx, p.containerClient, - "tcp", - hostAddr, - "tcp", - containerAddr, - p.exitAfterTimeout, + devssh.PortForwardOpts{ + LocalNetwork: tcp, + LocalAddr: hostAddr, + RemoteNetwork: tcp, + RemoteAddr: containerAddr, + ExitAfterTimeout: p.exitAfterTimeout, + }, ) } if err != nil { diff --git a/pkg/types/time.go b/pkg/types/time.go index b76092c030..7f9a735399 100644 --- a/pkg/types/time.go +++ b/pkg/types/time.go @@ -44,12 +44,6 @@ func (t *Time) DeepCopyInto(out *Time) { *out = *t } -// Date returns the Time corresponding to the supplied parameters -// by wrapping time.Date. -func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time { - return Time{time.Date(year, month, day, hour, min, sec, nsec, loc)} -} - // Now returns the current local time. func Now() Time { return Time{time.Now()} diff --git a/pkg/util/homedir_test.go b/pkg/util/homedir_test.go index 060a624a5b..ab9cf787c6 100644 --- a/pkg/util/homedir_test.go +++ b/pkg/util/homedir_test.go @@ -12,6 +12,8 @@ import ( gotoolsassert "gotest.tools/assert" ) +const userProfileValue = "userProfile" + func TestUserHomeDir(t *testing.T) { // Remember to reset environment variables after the test origHome := os.Getenv("HOME") @@ -37,7 +39,7 @@ func TestUserHomeDir(t *testing.T) { Name: "both HOME and USERPROFILE are set", Input: input{ home: "home", - userProfile: "userProfile", + userProfile: userProfileValue, }, Expect: "home", }, @@ -48,9 +50,9 @@ func TestUserHomeDir(t *testing.T) { Name: "HOME is unset and USERPROFILE is set", Input: input{ home: "", - userProfile: "userProfile", + userProfile: userProfileValue, }, - Expect: "userProfile", + Expect: userProfileValue, }) } diff --git a/pkg/util/rand.go b/pkg/util/rand.go index 9e477778a0..253147c5de 100644 --- a/pkg/util/rand.go +++ b/pkg/util/rand.go @@ -1,7 +1,8 @@ package util import ( - "math/rand" + "crypto/rand" + "math/big" ) const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" @@ -9,7 +10,12 @@ const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" func RandStringBytes(n int) string { b := make([]byte, n) for i := range b { - b[i] = letterBytes[rand.Intn(len(letterBytes))] + // rand.Int never fails with the default reader since Go 1.24. + v, err := rand.Int(rand.Reader, big.NewInt(int64(len(letterBytes)))) + if err != nil { + panic(err) + } + b[i] = letterBytes[v.Int64()] } return string(b) } diff --git a/pkg/workspace/delete.go b/pkg/workspace/delete.go index 1bc6a5f966..f30a8ef7a2 100644 --- a/pkg/workspace/delete.go +++ b/pkg/workspace/delete.go @@ -52,7 +52,7 @@ func Delete(ctx context.Context, opts DeleteOptions) (string, error) { return "", err } - if !opts.Force && client.WorkspaceConfig().Imported { + if isImportedLocalDelete(opts, client) { return removeImportedLocalWorkspace(ctx, client, opts) } @@ -259,7 +259,7 @@ func handleDeleteLoadError( ) } - workspaceID := Exists(ctx, opts.DevsyConfig, opts.Args, "", opts.Owner) + workspaceID := Exists(ctx, opts.DevsyConfig, LookupOptions{Args: opts.Args, Owner: opts.Owner}) if workspaceID == "" { if opts.IgnoreNotFound { return "", nil @@ -529,3 +529,9 @@ func (opts DeleteOptions) quiesce(ctx context.Context, workspaceID string) error } return nil } + +// isImportedLocalDelete reports whether the delete only unlinks an imported +// local workspace, which a force delete never does. +func isImportedLocalDelete(opts DeleteOptions, client client2.BaseWorkspaceClient) bool { + return !opts.Force && client.WorkspaceConfig().Imported +} diff --git a/pkg/workspace/delete_test.go b/pkg/workspace/delete_test.go index 907f1fe5f5..35797f7c66 100644 --- a/pkg/workspace/delete_test.go +++ b/pkg/workspace/delete_test.go @@ -14,6 +14,10 @@ import ( "github.com/stretchr/testify/require" ) +const ( + chosen = "chosen" +) + type testQuiescer func(context.Context, string) error func (f testQuiescer) Quiesce(ctx context.Context, workspaceID string) error { @@ -86,8 +90,8 @@ func TestDeleteWithPreResolvedClientSkipsSelection(t *testing.T) { setupTestPathManager(t) fake := &preResolvedDeleteClient{ - workspaceID: "chosen", - config: &provider.Workspace{ID: "chosen", Context: testDefaultContext}, + workspaceID: chosen, + config: &provider.Workspace{ID: chosen, Context: testDefaultContext}, } devsyConfig := &config.Config{ DefaultContext: testDefaultContext, @@ -100,7 +104,7 @@ func TestDeleteWithPreResolvedClientSkipsSelection(t *testing.T) { Force: true, }) require.NoError(t, err) - require.Equal(t, "chosen", id) + require.Equal(t, chosen, id) require.True(t, fake.deleted) } @@ -129,12 +133,12 @@ func TestDeleteQuiescesPersistedUpTaskBeforeDeleting(t *testing.T) { store, err := task.NewStore() require.NoError(t, err) - tk, err := store.Create(task.CreateOptions{Command: "up", WorkspaceID: "chosen"}) + tk, err := store.Create(task.CreateOptions{Command: "up", WorkspaceID: chosen}) require.NoError(t, err) fake := &preResolvedDeleteClient{ - workspaceID: "chosen", - config: &provider.Workspace{ID: "chosen", Context: testDefaultContext}, + workspaceID: chosen, + config: &provider.Workspace{ID: chosen, Context: testDefaultContext}, } devsyConfig := &config.Config{ DefaultContext: testDefaultContext, @@ -147,7 +151,7 @@ func TestDeleteQuiescesPersistedUpTaskBeforeDeleting(t *testing.T) { Force: true, }) require.NoError(t, err) - require.Equal(t, "chosen", id) + require.Equal(t, chosen, id) require.True(t, fake.deleted) state, err := store.Get(tk.ID()) @@ -160,8 +164,8 @@ func TestDeleteBlockedWhenActiveUpTaskCannotBeQuiesced(t *testing.T) { setupTestPathManager(t) fake := &preResolvedDeleteClient{ - workspaceID: "chosen", - config: &provider.Workspace{ID: "chosen", Context: testDefaultContext}, + workspaceID: chosen, + config: &provider.Workspace{ID: chosen, Context: testDefaultContext}, } devsyConfig := &config.Config{ DefaultContext: testDefaultContext, diff --git a/pkg/workspace/exec.go b/pkg/workspace/exec.go index 5862025907..5123416b77 100644 --- a/pkg/workspace/exec.go +++ b/pkg/workspace/exec.go @@ -340,41 +340,46 @@ type containerRunFunc func( // execWithRunner builds the exec argument list and runs it via run, translating // a non-zero process exit into a returned exit code rather than an error. -func execWithRunner(ctx context.Context, req ExecRequest, run containerRunFunc) (int, error) { - execArgs := []string{"exec", "-i"} +// execFlags returns the docker exec options that precede the container ID. +func execFlags(req ExecRequest) []string { + args := []string{"exec", "-i"} if req.TTY { - execArgs = append(execArgs, "-t") + args = append(args, "-t") } for k, v := range req.Env { - execArgs = append(execArgs, "-e", k+"="+v) + args = append(args, "-e", k+"="+v) } if req.Workdir != "" { - execArgs = append(execArgs, "--workdir", req.Workdir) + args = append(args, "--workdir", req.Workdir) } if req.Target.User != "" { - execArgs = append(execArgs, "--user", req.Target.User) + args = append(args, "--user", req.Target.User) } - execArgs = append(execArgs, req.Target.ContainerID) - execArgs = append(execArgs, req.Argv...) - stdout := req.Stdout - if stdout == nil { - stdout = io.Discard - } - stderr := req.Stderr - if stderr == nil { - stderr = io.Discard + return args +} + +func discardIfNil(w io.Writer) io.Writer { + if w == nil { + return io.Discard } - err := run(ctx, execArgs, req.Stdin, stdout, stderr) + return w +} + +func execWithRunner(ctx context.Context, req ExecRequest, run containerRunFunc) (int, error) { + execArgs := execFlags(req) + execArgs = append(execArgs, req.Target.ContainerID) + execArgs = append(execArgs, req.Argv...) + + err := run(ctx, execArgs, req.Stdin, discardIfNil(req.Stdout), discardIfNil(req.Stderr)) if err == nil { return 0, nil } if ctxErr := ctx.Err(); ctxErr != nil && errors.Is(err, ctxErr) { return -1, fmt.Errorf("exec in container %s: %w", req.Target.ContainerID, err) } - var exitErr *exec.ExitError - if errors.As(err, &exitErr) { + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { return exitErr.ExitCode(), nil } return -1, fmt.Errorf("exec in container %s: %w", req.Target.ContainerID, err) diff --git a/pkg/workspace/id_test.go b/pkg/workspace/id_test.go index 3fea7f6e7d..b34438512d 100644 --- a/pkg/workspace/id_test.go +++ b/pkg/workspace/id_test.go @@ -4,6 +4,10 @@ import ( "testing" ) +const ( + devsy = "devsy" +) + // TestToID takes in a string which represents a workspace source and returns an appropriate // _human readable_ ID for it. // Note that these tests document the status quo and not the ideal state. @@ -22,22 +26,22 @@ func toIDTestCases() []struct { { name: "Simple URL, no @, parse repo name", input: "github.com/devsy-org/devsy", - want: "devsy", + want: devsy, }, { name: "URL with .git suffix", input: "github.com/devsy-org/devsy.git", - want: "devsy", + want: devsy, }, { name: "URL with .git suffix and https prefix", input: "https://github.com/devsy-org/devsy.git", - want: "devsy", + want: devsy, }, { name: "URL with trailing slash", input: "github.com/devsy-org/devsy/", - want: "devsy", + want: devsy, }, { name: "Bare string with no slash", @@ -47,7 +51,7 @@ func toIDTestCases() []struct { { name: "Local directory", input: "/home/loft/devsy", - want: "devsy", + want: devsy, }, { name: "Branch with valid characters", diff --git a/pkg/workspace/list.go b/pkg/workspace/list.go index 8fd64323ce..4bb0843ecd 100644 --- a/pkg/workspace/list.go +++ b/pkg/workspace/list.go @@ -237,12 +237,7 @@ func listPlatformWorkspacesForProvider( ctx context.Context, params listPlatformWorkspacesForProviderParams, ) ([]*providerpkg.Workspace, error) { - instances, err := listPlatformInstances(ctx, listPlatformInstancesParams{ - devsyConfig: params.devsyConfig, - provider: params.provider, - providerConfig: params.providerConfig, - owner: params.owner, - }) + instances, err := listPlatformInstances(ctx, listPlatformInstancesParams(params)) if err != nil { if log.DebugEnabled() { log.Warnf( diff --git a/pkg/workspace/provider_update.go b/pkg/workspace/provider_update.go index 08816f4c54..c4aa36008c 100644 --- a/pkg/workspace/provider_update.go +++ b/pkg/workspace/provider_update.go @@ -95,12 +95,10 @@ func applyProviderUpdate( return fmt.Errorf("find provider %s: %w", providerName, err) } originalSource := originalProvider.Config.Source - providerSource := provider2.GetProviderSource(originalSource, originalProvider.Config.Name) - sourceBase, _ := provider2.SplitSourceAndTag(providerSource) - if sourceBase == "" { - return fmt.Errorf("no provider source found %s", providerSource) + providerSource, err := sourceAtVersion(originalProvider, newVersion) + if err != nil { + return err } - providerSource = sourceBase + "@" + newVersion opLock, err := provider2.GetProviderOperationLock(devsyConfig.DefaultContext, providerName) if err != nil { @@ -116,24 +114,24 @@ func applyProviderUpdate( return err } - // Another command may have updated the provider while this one waited on - // the lock; never replace an equal or newer version with this one, and - // never undo a source change made in the meantime. - currentProvider, err := FindProvider(devsyConfig, providerName) + reason, err := currentSkipReason(devsyConfig, providerName, originalSource, newVersion) if err != nil { - return fmt.Errorf("find provider %s: %w", providerName, err) + return err } - if reason := providerUpdateSkipReason( - originalSource, - currentProvider.Config.Source, - currentProvider.Config.Version, - newVersion, - ); reason != "" { + if reason != "" { log.Infof("%s, skipping update: provider=%s", reason, providerName) return nil } - _, err = UpdateProvider(ctx, devsyConfig, providerName, providerSource) + return replaceProviderSource(ctx, devsyConfig, providerName, providerSource) +} + +func replaceProviderSource( + ctx context.Context, + devsyConfig *config.Config, + providerName, providerSource string, +) error { + _, err := UpdateProvider(ctx, devsyConfig, providerName, providerSource) if err != nil { return fmt.Errorf("update provider %s: %w", providerName, err) } @@ -142,6 +140,28 @@ func applyProviderUpdate( return nil } +// currentSkipReason re-reads the provider after the lock is taken. Another +// command may have updated it while this one waited. Never replace an equal or +// newer version with this one, and never undo a source change made in the meantime. +func currentSkipReason( + devsyConfig *config.Config, + providerName string, + originalSource provider2.ProviderSource, + newVersion string, +) (string, error) { + currentProvider, err := FindProvider(devsyConfig, providerName) + if err != nil { + return "", fmt.Errorf("find provider %s: %w", providerName, err) + } + + return providerUpdateSkipReason( + originalSource, + currentProvider.Config.Source, + currentProvider.Config.Version, + newVersion, + ), nil +} + func providerUpdateSkipReason( originalSource, currentSource provider2.ProviderSource, currentVersion, newVersion string, @@ -190,3 +210,13 @@ func GetProInstance( func shouldSkipProviderUpdate(isDevVersion, isInternal bool) bool { return isDevVersion || isInternal } + +// sourceAtVersion returns the provider source pinned to newVersion. +func sourceAtVersion(p *ProviderWithOptions, newVersion string) (string, error) { + providerSource := provider2.GetProviderSource(p.Config.Source, p.Config.Name) + sourceBase, _ := provider2.SplitSourceAndTag(providerSource) + if sourceBase == "" { + return "", fmt.Errorf("no provider source found %s", providerSource) + } + return sourceBase + "@" + newVersion, nil +} diff --git a/pkg/workspace/provider_update_test.go b/pkg/workspace/provider_update_test.go index e75e5fce58..49383154bb 100644 --- a/pkg/workspace/provider_update_test.go +++ b/pkg/workspace/provider_update_test.go @@ -8,6 +8,10 @@ import ( "github.com/stretchr/testify/assert" ) +const ( + v050 = "v0.5.0" +) + func TestClearInitialized(t *testing.T) { newConfig := func(providers map[string]*config.ProviderConfig) *config.Config { return &config.Config{ @@ -94,13 +98,13 @@ func TestProviderVersionNeedsUpdate(t *testing.T) { name, newVer, curVer string expected, expectErr bool }{ - {"same version", "v0.5.0", "v0.5.0", false, false}, - {"newer version", "v0.6.0", "v0.5.0", true, false}, - {"older version (downgrade)", "v0.4.0", "v0.5.0", true, false}, + {"same version", v050, v050, false, false}, + {"newer version", "v0.6.0", v050, true, false}, + {"older version (downgrade)", "v0.4.0", v050, true, false}, {"mixed v prefix", "v0.6.0", "0.5.0", true, false}, {"patch difference", "v1.2.4", "v1.2.3", true, false}, - {"invalid new version", "not-a-version", "v0.5.0", false, true}, - {"invalid current version", "v0.5.0", "not-a-version", false, true}, + {"invalid new version", "not-a-version", v050, false, true}, + {"invalid current version", v050, "not-a-version", false, true}, } for _, tt := range tests { diff --git a/pkg/workspace/quiesce.go b/pkg/workspace/quiesce.go index 9393f451d3..a34bd534cb 100644 --- a/pkg/workspace/quiesce.go +++ b/pkg/workspace/quiesce.go @@ -41,8 +41,7 @@ func quiesceUpTasks( var errs []error handled := 0 for _, state := range states { - if state.Status.Terminal() && !state.LaunchPending && !state.NeedsExitedWorkerCleanup() && - !state.NeedsCanceledWorkerCleanup() { + if !needsQuiesce(state) { continue } taskCtx, cancel := context.WithTimeout(ctx, upTaskCancellationTimeout) @@ -106,3 +105,13 @@ func (StoreUpTaskQuiescer) Quiesce(ctx context.Context, workspaceID string) erro func QuiesceUpTasksForWorkspace(workspaceID string) error { return StoreUpTaskQuiescer{}.Quiesce(context.Background(), workspaceID) } + +// needsQuiesce reports whether a task still holds resources that a later +// operation could collide with. A terminal task is skipped once its worker +// and launch cleanup are done. +func needsQuiesce(state *task.State) bool { + if !state.Status.Terminal() || state.LaunchPending { + return true + } + return state.NeedsExitedWorkerCleanup() || state.NeedsCanceledWorkerCleanup() +} diff --git a/pkg/workspace/workspace.go b/pkg/workspace/workspace.go index a2f9349474..d07482e9b7 100644 --- a/pkg/workspace/workspace.go +++ b/pkg/workspace/workspace.go @@ -257,19 +257,20 @@ func findWorkspaceByArgs(ctx context.Context, opts GetOptions) (*providerpkg.Wor if opts.LocalOnly { return findLocalWorkspace(opts.DevsyConfig, opts.Args, ""), nil } - return findWorkspace(ctx, opts.DevsyConfig, opts.Args, "", opts.Owner) + return findWorkspace(ctx, opts.DevsyConfig, LookupOptions{Args: opts.Args, Owner: opts.Owner}) } // Exists returns the ID of a matching workspace, or an empty string if none // exists. -func Exists( - ctx context.Context, - devsyConfig *config.Config, - args []string, - workspaceID string, - owner platform.OwnerFilter, -) string { - workspace, _ := findWorkspace(ctx, devsyConfig, args, workspaceID, owner) +// LookupOptions selects a workspace by its arguments, an explicit ID or the owner filter. +type LookupOptions struct { + Args []string + ID string + Owner platform.OwnerFilter +} + +func Exists(ctx context.Context, devsyConfig *config.Config, opts LookupOptions) string { + workspace, _ := findWorkspace(ctx, devsyConfig, opts) if workspace == nil { return "" } @@ -292,12 +293,16 @@ func resolveWorkspace( workspaceID := ToID(name) if params.DesiredID != "" { - if Exists(ctx, devsyConfig, nil, params.DesiredID, params.Owner) != "" { + if Exists( + ctx, + devsyConfig, + LookupOptions{ID: params.DesiredID, Owner: params.Owner}, + ) != "" { log.Debugf("workspace id already exists: desiredID=%s", params.DesiredID) return loadExistingWorkspace(devsyConfig, params.DesiredID, params.ChangeLastUsed) } workspaceID = params.DesiredID - } else if Exists(ctx, devsyConfig, nil, workspaceID, params.Owner) != "" { + } else if Exists(ctx, devsyConfig, LookupOptions{ID: workspaceID, Owner: params.Owner}) != "" { log.Debugf("workspace already exists: workspaceID=%s", workspaceID) return loadExistingWorkspace(devsyConfig, workspaceID, params.ChangeLastUsed) } @@ -340,7 +345,11 @@ func resolveWorkspaceWithoutArgs( params ResolveParams, ) (resolvedWorkspace, error) { if params.DesiredID != "" { - workspace, err := findWorkspace(ctx, devsyConfig, nil, params.DesiredID, params.Owner) + workspace, err := findWorkspace( + ctx, + devsyConfig, + LookupOptions{ID: params.DesiredID, Owner: params.Owner}, + ) if err != nil { return resolvedWorkspace{}, fmt.Errorf("find workspace: %w", err) } @@ -720,16 +729,14 @@ func findLocalWorkspace( func findWorkspace( ctx context.Context, devsyConfig *config.Config, - args []string, - workspaceID string, - owner platform.OwnerFilter, + opts LookupOptions, ) (*providerpkg.Workspace, error) { - workspaceID = ensureWorkspaceID(args, workspaceID) + workspaceID := ensureWorkspaceID(opts.Args, opts.ID) if workspaceID == "" { return nil, nil } - allWorkspaces, err := List(ctx, devsyConfig, false, owner) + allWorkspaces, err := List(ctx, devsyConfig, false, opts.Owner) if err != nil { return nil, fmt.Errorf("list workspaces: %w", err) } diff --git a/pkg/workspacelogs/ack.go b/pkg/workspacelogs/ack.go new file mode 100644 index 0000000000..6fdc86a634 --- /dev/null +++ b/pkg/workspacelogs/ack.go @@ -0,0 +1,189 @@ +package workspacelogs + +import ( + "encoding/json" + "errors" + "fmt" + "io/fs" + "os" + "path/filepath" + "sync" +) + +// Hop names one acknowledged position. A host that both receives and forwards +// keeps both, so durability at the next host is never inferred from durability +// in its own store. +type Hop string + +// Acknowledgement hops. +const ( + // HopInbound is the position persisted in this host's store for a producer + // that mirrors into it. + HopInbound Hop = "inbound" + // HopUpstream is the position the next host acknowledged for records this + // host sends on. + HopUpstream Hop = "upstream" +) + +const ackMaxBytes = 4 * 1024 + +// AckKey identifies one stored position. +type AckKey struct { + RunID string + ProducerID string + Hop Hop +} + +// AckState is the durable position of one producer on one hop. +type AckState struct { + SchemaVersion int `json:"schemaVersion"` + RunID string `json:"runId"` + ProducerID string `json:"producerId"` + Hop Hop `json:"hop"` + // Acked is the highest sequence the peer persisted or represented as a gap. + Acked uint64 `json:"acked"` + // Gapped is set once the peer reported sequences it will never hold. + Gapped bool `json:"gapped,omitempty"` + GapFirst uint64 `json:"gapFirst,omitempty"` + GapLast uint64 `json:"gapLast,omitempty"` +} + +// AckStore keeps acknowledged positions in small files beneath one private +// directory. Positions only move forward. +type AckStore struct { + dir string + mu sync.Mutex +} + +// NewAckStore creates dir (absolute) and returns the store. +func NewAckStore(dir string) (*AckStore, error) { + if !filepath.IsAbs(dir) { + return nil, fmt.Errorf("%w: ack directory must be absolute", ErrUnsafePath) + } + if err := os.MkdirAll(dir, dirPerm); err != nil { + return nil, fmt.Errorf("create ack directory: %w", err) + } + + return &AckStore{dir: dir}, nil +} + +// Load returns the stored position, or a zero position when none exists. +func (a *AckStore) Load(key AckKey) (AckState, error) { + a.mu.Lock() + defer a.mu.Unlock() + + return a.load(key) +} + +// Advance moves the acknowledged position forward. A lower value is ignored. +func (a *AckStore) Advance(key AckKey, acked uint64) error { + return a.update(key, func(st *AckState) { + st.Acked = max(st.Acked, acked) + }) +} + +// RecordGap stores that the peer will never hold first..last and moves the +// position past the gap so the sender stops retrying those bytes. +func (a *AckStore) RecordGap(key AckKey, first, last uint64) error { + return a.update(key, func(st *AckState) { + if !st.Gapped { + st.GapFirst = first + } + st.Gapped = true + st.GapLast = max(st.GapLast, last) + st.Acked = max(st.Acked, last) + }) +} + +func (a *AckStore) update(key AckKey, fn func(*AckState)) error { + a.mu.Lock() + defer a.mu.Unlock() + st, err := a.load(key) + if err != nil { + return err + } + fn(&st) + + return a.write(st) +} + +func (a *AckStore) path(key AckKey) (string, error) { + if err := requireID("runId", key.RunID); err != nil { + return "", err + } + if err := requireID("producerId", key.ProducerID); err != nil { + return "", err + } + if key.Hop != HopInbound && key.Hop != HopUpstream { + return "", fmt.Errorf("%w: hop", ErrInvalidRecord) + } + + return filepath.Join(a.dir, key.RunID+"."+key.ProducerID+"."+string(key.Hop)+".json"), nil +} + +func (a *AckStore) load(key AckKey) (AckState, error) { + zero := AckState{ + SchemaVersion: SchemaVersion, RunID: key.RunID, ProducerID: key.ProducerID, Hop: key.Hop, + } + path, err := a.path(key) + if err != nil { + return zero, err + } + data, err := readSmallFile(path) + if err != nil || data == nil { + return zero, err + } + var st AckState + if json.Unmarshal(data, &st) != nil || st.RunID != key.RunID || + st.ProducerID != key.ProducerID || st.Hop != key.Hop { + // A damaged position restarts from zero; the receiver deduplicates. + return zero, nil + } + + return st, nil +} + +// readSmallFile returns nil data for a missing or oversized file. +func readSmallFile(path string) ([]byte, error) { + if err := checkRegular(path); err != nil { + return nil, err + } + data, err := os.ReadFile(path) //nolint:gosec // path is built from validated IDs + if errors.Is(err, fs.ErrNotExist) || len(data) > ackMaxBytes { + return nil, nil + } + if err != nil { + return nil, fmt.Errorf("read ack: %w", err) + } + + return data, nil +} + +func (a *AckStore) write(st AckState) error { + path, err := a.path(AckKey{RunID: st.RunID, ProducerID: st.ProducerID, Hop: st.Hop}) + if err != nil { + return err + } + data, err := json.Marshal(st) + if err != nil { + return fmt.Errorf("encode ack: %w", err) + } + tmp, err := os.CreateTemp(a.dir, "ack-*.tmp") + if err != nil { + return fmt.Errorf("create ack temp file: %w", err) + } + _, werr := tmp.Write(data) + cerr := tmp.Close() + if err = errors.Join(werr, cerr); err != nil { + _ = os.Remove(tmp.Name()) + + return fmt.Errorf("write ack: %w", err) + } + if err = os.Rename(tmp.Name(), path); err != nil { + _ = os.Remove(tmp.Name()) + + return fmt.Errorf("replace ack: %w", err) + } + + return nil +} diff --git a/pkg/workspacelogs/append.go b/pkg/workspacelogs/append.go new file mode 100644 index 0000000000..6bc146d997 --- /dev/null +++ b/pkg/workspacelogs/append.go @@ -0,0 +1,364 @@ +package workspacelogs + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" +) + +// AppendResult reports what an Append call persisted. +type AppendResult struct { + // Accepted is the highest contiguous sequence persisted for the producer. + // Acknowledge this value, never a higher one. + Accepted uint64 + Written int + Duplicates int + Elided int +} + +// Append persists a batch of records from one producer. Records at or below +// the producer's last persisted sequence are duplicates and are skipped, so a +// retransmitted batch never repeats output. A batch that skips ahead of the +// next expected sequence returns ErrSequenceGap with Accepted unchanged. +func (s *Store) Append(records []Record) (AppendResult, error) { + if len(records) == 0 { + return AppendResult{}, nil + } + if err := validateBatch(records); err != nil { + return AppendResult{}, err + } + dir, err := s.prepareProducer(&records[0]) + if err != nil { + return AppendResult{}, err + } + + var ( + res AppendResult + applied []Record + ) + lockErr := withLock(filepath.Join(dir, appendLock), func() error { + var appendErr error + res, applied, appendErr = s.appendLocked(dir, records) + + return appendErr + }) + // Producer and capture records that reached disk still update the manifest + // when a later record in the batch failed. + if applyErr := s.applyToManifest(records[0].RunID, applied); lockErr == nil { + lockErr = applyErr + } + + return res, lockErr +} + +// prepareProducer makes sure the producer is registered (a mirrored producer +// may introduce itself) and returns its directory. +func (s *Store) prepareProducer(first *Record) (string, error) { + m, err := s.Manifest(first.RunID) + if err != nil { + return "", err + } + if m.producer(first.ProducerID) < 0 { + if err = s.registerFromFirstRecord(first); err != nil { + return "", err + } + } + if err = s.ensureDir(runsDirName, first.RunID, producersDir, first.ProducerID); err != nil { + return "", err + } + + return s.producerDir(first.RunID, first.ProducerID) +} + +func validateBatch(records []Record) error { + first := records[0] + for i := range records { + rec := &records[i] + if err := rec.Validate(); err != nil { + return err + } + if rec.RunID != first.RunID || rec.ProducerID != first.ProducerID { + return fmt.Errorf("%w: batch mixes runs or producers", ErrInvalidRecord) + } + if rec.Sequence != first.Sequence+uint64(i) { + return fmt.Errorf("%w: batch sequences are not contiguous", ErrInvalidRecord) + } + } + + return nil +} + +func (s *Store) appendLocked(dir string, records []Record) (AppendResult, []Record, error) { + t, err := s.loadTail(dir) + if err != nil { + return AppendResult{}, nil, err + } + runDir := filepath.Dir(filepath.Dir(dir)) + w := &batchWriter{dir: dir, t: t, runBytes: dirBytes(runDir)} + defer w.close() + + res := AppendResult{Accepted: t.lastSeq} + var applied []Record + for i := range records { + rec := records[i] + switch { + case rec.Sequence <= w.t.lastSeq: + res.Duplicates++ + + continue + case rec.Sequence != w.t.lastSeq+1: + return res, applied, fmt.Errorf( + "%w: expected %d, got %d", + ErrSequenceGap, + w.t.lastSeq+1, + rec.Sequence, + ) + } + stored, writeErr := w.write(rec) + if writeErr != nil { + return res, applied, writeErr + } + res.Accepted, res.Written = stored.Sequence, res.Written+1 + if stored.Elided { + res.Elided++ + } + applied = append(applied, stored) + } + s.rememberTail(dir, w.t) + + return res, applied, nil +} + +// batchWriter appends encoded lines to the producer's current segment. +type batchWriter struct { + dir string + t tail + runBytes int64 + f *os.File +} + +func (w *batchWriter) write(rec Record) (Record, error) { + line, stored, err := w.encode(rec) + if err != nil { + return Record{}, err + } + if err = w.openFor(stored.Sequence, int64(len(line))); err != nil { + return Record{}, err + } + if _, err = w.f.Write(line); err != nil { + return Record{}, fmt.Errorf("write record: %w", err) + } + w.t.size += int64(len(line)) + w.t.lastSeq = stored.Sequence + w.runBytes += int64(len(line)) + + return stored, nil +} + +// encode returns the stored line. Output that would push the run past its +// budget keeps its sequence and byte count but loses its text. +func (w *batchWriter) encode(rec Record) ([]byte, Record, error) { + if rec.Kind == KindOutput && rec.Bytes == 0 { + rec.Bytes = len(rec.Text) + } + line, err := marshalLine(rec) + if err != nil { + return nil, Record{}, err + } + if rec.Kind == KindOutput { + return w.fitOutput(line, rec) + } + if len(line) > MaxMetadataBytes { + return nil, Record{}, fmt.Errorf( + "%w: metadata record exceeds %d bytes", + ErrInvalidRecord, + MaxMetadataBytes, + ) + } + + return w.checkCapacity(line, rec) +} + +func (w *batchWriter) fitOutput(line []byte, rec Record) ([]byte, Record, error) { + if w.runBytes+int64(len(line)) > MaxRunBytes-metadataReserve { + rec.Text, rec.Elided = "", true + var err error + if line, err = marshalLine(rec); err != nil { + return nil, Record{}, err + } + if w.runBytes+int64(len(line)) > MaxRunBytes-finalStateReserve { + return nil, Record{}, ErrCapacity + } + } + + return w.checkCapacity(line, rec) +} + +func (w *batchWriter) checkCapacity(line []byte, rec Record) ([]byte, Record, error) { + if w.runBytes+int64(len(line)) > MaxRunBytes { + return nil, Record{}, ErrCapacity + } + + return line, rec, nil +} + +func marshalLine(rec Record) ([]byte, error) { + line, err := json.Marshal(rec) + if err != nil { + return nil, fmt.Errorf("encode record: %w", err) + } + + return append(line, '\n'), nil +} + +// openFor makes sure the open segment can take n more bytes, rolling to a new +// segment named after the record's sequence when it cannot. +func (w *batchWriter) openFor(seq uint64, n int64) error { + if w.f != nil && w.t.size+n <= MaxSegmentBytes { + return nil + } + if w.f == nil && w.t.name != "" && w.t.size+n <= MaxSegmentBytes { + return w.open(w.t.name) + } + w.close() + w.t.name, w.t.size = segmentName(seq), 0 + + return w.open(w.t.name) +} + +func (w *batchWriter) open(name string) error { + path := filepath.Join(w.dir, name) + if err := checkRegular(path); err != nil { + return err + } + //nolint:gosec // path is built from validated IDs and a generated segment name + f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, filePerm) + if err != nil { + return fmt.Errorf("open segment: %w", err) + } + w.f = f + + return nil +} + +func (w *batchWriter) close() { + if w.f != nil { + _ = w.f.Close() + w.f = nil + } +} + +// applyToManifest mirrors producer transitions and capture conditions into the +// run manifest so each store can decide completion. +func (s *Store) applyToManifest(runID string, applied []Record) error { + relevant := false + for i := range applied { + if applied[i].Kind == KindProducer || applied[i].Kind == KindCapture || applied[i].Elided { + relevant = true + } + } + if !relevant { + return nil + } + + return s.update(runID, func(m *RunManifest) error { + for i := range applied { + m.apply(&applied[i]) + } + + return nil + }) +} + +func (m *RunManifest) apply(rec *Record) { + switch { + case rec.Elided: + m.addCondition(ConditionCapacity) + case rec.Kind == KindCapture: + m.addCondition(rec.Condition) + case rec.Kind == KindProducer && rec.ChildProducerID != "": + m.applyChild(rec) + case rec.Kind == KindProducer: + m.applyOwn(rec) + } +} + +// applyOwn applies a producer's report about itself. +func (m *RunManifest) applyOwn(rec *Record) { + if i := m.producer(rec.ProducerID); i >= 0 && rec.ProducerState != "" { + m.Producers[i].advance(rec.ProducerState) + } + if i := m.producer(rec.ProducerID); i >= 0 && rec.DiscoverySealed { + m.Producers[i].Sealed = true + } + m.refreshDiscoverySealed() + if rec.ProducerState == ProducerUnavailable { + m.addCondition(ConditionSourceUnavailable) + } +} + +// applyChild registers a reserved child or moves a known child to the state the +// parent reports for it. Only the child's own parent may move it. +func (m *RunManifest) applyChild(rec *Record) { + switch i := m.producer(rec.ChildProducerID); { + case i >= 0: + if m.Producers[i].ParentProducerID == rec.ProducerID { + m.Producers[i].advance(rec.ProducerState) + } + case len(m.Producers) < MaxProducers: + m.Producers = append(m.Producers, ProducerRef{ + ProducerID: rec.ChildProducerID, ParentProducerID: rec.ProducerID, + State: rec.ProducerState, Location: rec.Location, + }) + default: + m.addCondition(ConditionProducerCap) + } + if rec.ProducerState == ProducerUnavailable { + m.addCondition(ConditionSourceUnavailable) + } +} + +func (p *ProducerRef) advance(next ProducerState) { + if next.rank() > p.State.rank() { + p.State = next + } +} + +func (s ProducerState) rank() int { + switch s { + case ProducerReserved: + return 1 + case ProducerRunning: + return 2 + case ProducerTerminal, ProducerUnavailable: + return 3 + default: + return 0 + } +} + +// registerFromFirstRecord lets a mirrored producer introduce itself: only the +// producer's own first record (sequence 1, state running or reserved) may. +func (s *Store) registerFromFirstRecord(rec *Record) error { + if rec.Kind != KindProducer || rec.Sequence != 1 || rec.ChildProducerID != "" { + return fmt.Errorf("%w: producer is not registered", ErrNotFound) + } + + return s.RegisterProducer(rec.RunID, ProducerRef{ + ProducerID: rec.ProducerID, ParentProducerID: rec.ParentProducerID, + State: ProducerRunning, Location: rec.Location, + }) +} + +func (m *RunManifest) refreshDiscoverySealed() { + if m.DiscoverySealed || len(m.Producers) == 0 { + return + } + for _, p := range m.Producers { + if p.ParentProducerID == "" && !p.Sealed { + return + } + } + m.DiscoverySealed = true +} diff --git a/pkg/workspacelogs/context.go b/pkg/workspacelogs/context.go new file mode 100644 index 0000000000..44184fdab8 --- /dev/null +++ b/pkg/workspacelogs/context.go @@ -0,0 +1,26 @@ +package workspacelogs + +import "context" + +type recorderKey struct{} + +// WithRecorder returns a context that carries rec so deep call sites can +// record without new parameters. A nil rec returns ctx unchanged. +func WithRecorder(ctx context.Context, rec *Recorder) context.Context { + if rec == nil { + return ctx + } + + return context.WithValue(ctx, recorderKey{}, rec) +} + +// RecorderFrom returns the recorder carried by ctx, or nil. Every Recorder +// method is safe to skip when this is nil. +func RecorderFrom(ctx context.Context) *Recorder { + rec, _ := ctx.Value(recorderKey{}).(*Recorder) + + return rec +} + +// RunID returns the run this recorder writes to. +func (r *Recorder) RunID() string { return r.opts.RunID } diff --git a/pkg/workspacelogs/context_test.go b/pkg/workspacelogs/context_test.go new file mode 100644 index 0000000000..9a7e8f3f5a --- /dev/null +++ b/pkg/workspacelogs/context_test.go @@ -0,0 +1,21 @@ +package workspacelogs + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRecorderTravelsInContext(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + rec := newTestRecorder(t, s, runID, nil) + + assert.Nil(t, RecorderFrom(context.Background())) + assert.Same(t, rec, RecorderFrom(WithRecorder(context.Background(), rec))) + assert.Equal(t, runID, rec.RunID()) + + base := context.Background() + assert.Equal(t, base, WithRecorder(base, nil), "a nil recorder leaves the context alone") +} diff --git a/pkg/workspacelogs/cursor.go b/pkg/workspacelogs/cursor.go new file mode 100644 index 0000000000..36d873be29 --- /dev/null +++ b/pkg/workspacelogs/cursor.go @@ -0,0 +1,85 @@ +package workspacelogs + +import ( + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "encoding/json" + "fmt" + "time" +) + +type cursorKind string + +const ( + cursorRun cursorKind = "run" + cursorIndex cursorKind = "index" +) + +// cursor is the decoded form of an opaque page token. A run cursor binds the +// run, the record filters and the last sequence read from each producer. An +// index cursor binds the selectors and the ordering position. Wall-clock time +// never decides what was already read. +type cursor struct { + Version int `json:"v"` + Kind cursorKind `json:"k"` + Run string `json:"r,omitempty"` + Filter string `json:"f,omitempty"` + Pos map[string]uint64 `json:"p,omitempty"` + // Gaps remembers that an earlier page reported a gap, so the last page of a + // read cannot claim completeness. + Gaps bool `json:"g,omitempty"` + Context string `json:"c,omitempty"` + UID string `json:"u,omitempty"` + At time.Time `json:"t,omitzero"` + ID string `json:"i,omitempty"` +} + +func (c *cursor) encode() (string, error) { + if len(c.Pos) > MaxProducers { + return "", fmt.Errorf("%w: too many producers", ErrInvalidCursor) + } + data, err := json.Marshal(c) + if err != nil { + return "", fmt.Errorf("encode cursor: %w", err) + } + token := base64.RawURLEncoding.EncodeToString(data) + if len(token) > MaxCursorBytes { + return "", fmt.Errorf("%w: cursor exceeds %d bytes", ErrInvalidCursor, MaxCursorBytes) + } + + return token, nil +} + +// decodeCursor parses token. An empty token is the start position. +func decodeCursor(token string, kind cursorKind) (*cursor, error) { + if token == "" { + return &cursor{Version: SchemaVersion, Kind: kind}, nil + } + if len(token) > MaxCursorBytes { + return nil, fmt.Errorf("%w: cursor exceeds %d bytes", ErrInvalidCursor, MaxCursorBytes) + } + data, err := base64.RawURLEncoding.DecodeString(token) + if err != nil { + return nil, fmt.Errorf("%w: not a cursor", ErrInvalidCursor) + } + var c cursor + if err = json.Unmarshal(data, &c); err != nil { + return nil, fmt.Errorf("%w: not a cursor", ErrInvalidCursor) + } + if c.Version != SchemaVersion || c.Kind != kind || len(c.Pos) > MaxProducers { + return nil, fmt.Errorf("%w: wrong version, kind or size", ErrInvalidCursor) + } + + return &c, nil +} + +// filterHash binds a cursor to the selectors it was produced under. +func filterHash(parts ...string) string { + h := sha256.New() + for _, p := range parts { + _, _ = fmt.Fprintf(h, "%d:%s;", len(p), p) + } + + return hex.EncodeToString(h.Sum(nil))[:16] +} diff --git a/pkg/workspacelogs/failed.go b/pkg/workspacelogs/failed.go new file mode 100644 index 0000000000..4d50bb115e --- /dev/null +++ b/pkg/workspacelogs/failed.go @@ -0,0 +1,148 @@ +package workspacelogs + +import "time" + +// Keys under which a failed command carries run identity in its error context. +const ( + ContextRunID = "runId" + ContextFailedInvocationID = "failedInvocationId" +) + +// FailedInvocations returns the IDs of invocations whose latest recorded state +// is failed, in order of first appearance. At most MaxProducers IDs are +// returned; callers only use an ID when exactly one invocation failed. +func (s *Store) FailedInvocations(runID string) ([]string, error) { + m, err := s.Manifest(runID) + if err != nil { + return nil, err + } + state, order, err := s.latestStates(m) + if err != nil { + return nil, err + } + var failed []string + for _, id := range order { + if state[id] == InvocationFailed && len(failed) < MaxProducers { + failed = append(failed, id) + } + } + + return failed, nil +} + +type transitionMark struct { + producerID string + sequence uint64 + rank int + time time.Time +} + +// supersedes reports whether next replaces prior. Sequences only compare +// inside one producer. Across producers a result is never replaced by an +// earlier phase, and equal phases fall back to record time. +func (next transitionMark) supersedes(prior transitionMark) bool { + if prior.producerID == next.producerID { + return next.sequence >= prior.sequence + } + if prior.rank != next.rank { + return next.rank > prior.rank + } + + return !next.time.Before(prior.time) +} + +func (s InvocationState) rank() int { + switch s { + case InvocationPending: + return 0 + case InvocationDeferred: + return 1 + case InvocationRunning: + return 2 + case InvocationSkipped: + return 3 + default: + return 4 + } +} + +func (s InvocationState) inFlight() bool { + return s == InvocationPending || s == InvocationDeferred || s == InvocationRunning +} + +type stateFold struct { + state map[string]InvocationState + latest map[string]transitionMark + order []string +} + +func (f *stateFold) add(producerID string, rec *Record) { + next := transitionMark{producerID, rec.Sequence, rec.State.rank(), rec.Time} + prior, seen := f.latest[rec.InvocationID] + if !seen { + f.order = append(f.order, rec.InvocationID) + } else if !next.supersedes(prior) { + return + } + f.latest[rec.InvocationID] = next + f.state[rec.InvocationID] = rec.State +} + +// latestStates returns each invocation's latest state and the order the +// invocations first appeared in. Once no producer can still write, a state +// that never reached a result is reported as unknown. +func (s *Store) latestStates(m *RunManifest) (map[string]InvocationState, []string, error) { + fold := &stateFold{ + state: map[string]InvocationState{}, + latest: map[string]transitionMark{}, + } + for _, p := range m.Producers { + err := s.eachRecord(m.RunID, p.ProducerID, 1, func(rec *Record) bool { + if rec != nil && rec.Kind == KindInvocation { + fold.add(p.ProducerID, rec) + } + + return true + }) + if err != nil { + return nil, nil, err + } + } + if !m.Active() { + for id, st := range fold.state { + if st.inFlight() { + fold.state[id] = InvocationUnknown + } + } + } + + return fold.state, fold.order, nil +} + +// FailureContext returns the error context that identifies a failed run: the +// run ID, plus the failed invocation when exactly one invocation failed. +// Lookup problems yield whatever identity is known. +func (s *Store) FailureContext(runID string) map[string]string { + if runID == "" { + return nil + } + values := map[string]string{ContextRunID: runID} + if s == nil { + return values + } + if failed, err := s.FailedInvocations(runID); err == nil && len(failed) == 1 { + values[ContextFailedInvocationID] = failed[0] + } + + return values +} + +// FailureContext is the Session form of Store.FailureContext. A nil Session +// has no history to point at. +func (s *Session) FailureContext() map[string]string { + if s == nil { + return nil + } + + return s.Store.FailureContext(s.RunID) +} diff --git a/pkg/workspacelogs/failed_test.go b/pkg/workspacelogs/failed_test.go new file mode 100644 index 0000000000..9cc6ecfbe3 --- /dev/null +++ b/pkg/workspacelogs/failed_test.go @@ -0,0 +1,54 @@ +package workspacelogs + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestFailedInvocationsKeepsExecutedResultOverLaterPlan(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + worker := newProducer(t, s, runID) + parent := newProducer(t, s, runID) + inv := NewID() + workerSeq := newSeq(runID, worker) + parentSeq := newSeq(runID, parent) + + mustAppend(t, s, workerSeq.invocation(inv, hookCreate, InvocationFailed)) + mustAppend(t, s, parentSeq.invocation(inv, hookCreate, InvocationDeferred)) + + failed, err := s.FailedInvocations(runID) + require.NoError(t, err) + assert.Equal(t, []string{inv}, failed) +} + +func TestLatestStatesReportsUnfinishedStepsAsUnknownOnceRunIsOver(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + started, planned, skipped := NewID(), NewID(), NewID() + mustAppend( + t, s, + seq.invocation(started, hookCreate, InvocationRunning), + seq.invocation(planned, hookCreate, InvocationPending), + seq.invocation(skipped, hookCreate, InvocationSkipped), + ) + + m, err := s.Manifest(runID) + require.NoError(t, err) + states, _, err := s.latestStates(m) + require.NoError(t, err) + assert.Equal(t, InvocationRunning, states[started], "a live run may still finish the step") + + mustAppend(t, s, seq.terminal(true)) + m, err = s.Manifest(runID) + require.NoError(t, err) + states, _, err = s.latestStates(m) + require.NoError(t, err) + assert.Equal(t, InvocationUnknown, states[started]) + assert.Equal(t, InvocationUnknown, states[planned]) + assert.Equal(t, InvocationSkipped, states[skipped]) +} diff --git a/pkg/workspacelogs/golden_test.go b/pkg/workspacelogs/golden_test.go new file mode 100644 index 0000000000..9da9fdc8c7 --- /dev/null +++ b/pkg/workspacelogs/golden_test.go @@ -0,0 +1,115 @@ +package workspacelogs + +import ( + "encoding/json" + "os" + "path/filepath" + "sort" + "testing" + + "github.com/stretchr/testify/require" +) + +const goldenDir = "../../desktop/src/main/__tests__/fixtures" + +// TestGoldenResponsesMatchDesktopFixtures keeps the JSON the Desktop parses in +// step with what the CLI emits. Regenerate with UPDATE_GOLDEN=1. +func TestGoldenResponsesMatchDesktopFixtures(t *testing.T) { + s, _ := newTestStore(t) + h := newHookRun(t, s) + read, err := s.Read(ReadRequest{RunID: h.runID}) + require.NoError(t, err) + index, err := s.ListRuns(IndexRequest{Context: testContext}) + require.NoError(t, err) + + for name, value := range map[string]any{ + "go-read-response.json": normalize(t, read), + "go-index-response.json": normalize(t, index), + } { + got, err := json.MarshalIndent(value, "", " ") + require.NoError(t, err) + path := filepath.Join(goldenDir, name) + if os.Getenv("UPDATE_GOLDEN") != "" { + require.NoError(t, os.WriteFile(path, append(got, '\n'), 0o600)) + } + want, err := os.ReadFile(path) //nolint:gosec // fixed test fixture path + require.NoError(t, err, "run with UPDATE_GOLDEN=1 to create the fixture") + require.JSONEq(t, string(want), string(got)) + } +} + +// normalize replaces random IDs and times with stable values so the fixture +// does not change between runs. +func normalize(t *testing.T, v any) any { + t.Helper() + raw, err := json.Marshal(v) + require.NoError(t, err) + var generic any + require.NoError(t, json.Unmarshal(raw, &generic)) + ids := map[string]string{} + + return stabilize(generic, ids) +} + +func stabilize(v any, ids map[string]string) any { + switch x := v.(type) { + case map[string]any: + return stabilizeMap(x, ids) + case []any: + for i, val := range x { + x[i] = stabilize(val, ids) + } + case string: + return stabilizeString(x, ids) + } + + return v +} + +func stabilizeMap(m map[string]any, ids map[string]string) map[string]any { + keys := make([]string, 0, len(m)) + for k := range m { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + if k == "nextCursor" { + m[k] = "opaque-cursor" + + continue + } + m[k] = stabilize(m[k], ids) + } + + return m +} + +func stabilizeString(s string, ids map[string]string) string { + if ValidID(s) { + if _, ok := ids[s]; !ok { + ids[s] = stableID(len(ids) + 1) + } + + return ids[s] + } + if len(s) > 10 && s[4] == '-' && s[10] == 'T' { + return "2026-10-30T12:00:00Z" + } + + return s +} + +func stableID(n int) string { + return "00000000-0000-4000-8000-" + padHex(n) +} + +func padHex(n int) string { + const digits = "0123456789abcdef" + out := []byte("000000000000") + for i := len(out) - 1; i >= 0 && n > 0; i-- { + out[i] = digits[n%16] + n /= 16 + } + + return string(out) +} diff --git a/pkg/workspacelogs/helpers_test.go b/pkg/workspacelogs/helpers_test.go new file mode 100644 index 0000000000..2d50cacbaf --- /dev/null +++ b/pkg/workspacelogs/helpers_test.go @@ -0,0 +1,162 @@ +package workspacelogs + +import ( + "os" + "path/filepath" + "strings" + "sync" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +const ( + testContext = "default" + hookCreate = "postCreateCommand" + uidA = "uid-a" +) + +type fakeClock struct { + mu sync.Mutex + now time.Time +} + +func (c *fakeClock) Now() time.Time { + c.mu.Lock() + defer c.mu.Unlock() + c.now = c.now.Add(time.Second) + + return c.now +} + +func (c *fakeClock) Advance(d time.Duration) { + c.mu.Lock() + defer c.mu.Unlock() + c.now = c.now.Add(d) +} + +func newClock() *fakeClock { + return &fakeClock{now: time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC)} +} + +func newTestStore(t *testing.T) (*Store, *fakeClock) { + t.Helper() + clock := newClock() + s, err := Open(Options{Root: t.TempDir(), Now: clock.Now}) + require.NoError(t, err) + + return s, clock +} + +func newRun(t *testing.T, s *Store, uid string) string { + t.Helper() + m, err := s.CreateRun(RunSpec{Context: testContext, WorkspaceUID: uid, Command: "up"}) + require.NoError(t, err) + + return m.RunID +} + +func newProducer(t *testing.T, s *Store, runID string) string { + t.Helper() + id := NewID() + require.NoError( + t, + s.RegisterProducer(runID, ProducerRef{ProducerID: id, State: ProducerRunning}), + ) + + return id +} + +// producerSeq numbers records for one test producer. +type producerSeq struct { + runID, producerID string + next uint64 +} + +func newSeq(runID, producerID string) *producerSeq { + return &producerSeq{runID: runID, producerID: producerID, next: 1} +} + +func (p *producerSeq) rec(r Record) Record { + r.SchemaVersion = SchemaVersion + r.RunID, r.ProducerID = p.runID, p.producerID + r.Sequence = p.next + r.Time = time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC) + p.next++ + + return r +} + +func (p *producerSeq) output(inv string, stream Stream, text string) Record { + return p.rec(Record{Kind: KindOutput, InvocationID: inv, Stream: stream, Text: text}) +} + +func (p *producerSeq) invocation(inv, hook string, state InvocationState) Record { + return p.rec( + Record{Kind: KindInvocation, InvocationID: inv, Hook: hook, State: state, Phase: "setup"}, + ) +} + +func (p *producerSeq) terminal(sealed bool) Record { + return p.rec( + Record{Kind: KindProducer, ProducerState: ProducerTerminal, DiscoverySealed: sealed}, + ) +} + +func mustAppend(t *testing.T, s *Store, recs ...Record) AppendResult { + t.Helper() + res, err := s.Append(recs) + require.NoError(t, err) + + return res +} + +// finishRun seals discovery and marks one producer terminal so the run is no +// longer active. +func finishRun(t *testing.T, s *Store, runID string) { + t.Helper() + pid := newProducer(t, s, runID) + mustAppend(t, s, newSeq(runID, pid).terminal(true)) +} + +func readAll(t *testing.T, s *Store, req ReadRequest) []Record { + t.Helper() + var all []Record + for { + resp, err := s.Read(req) + require.NoError(t, err) + all = append(all, resp.Records...) + if !resp.HasMore { + return all + } + req.After = resp.NextCursor + } +} + +func texts(recs []Record) string { + var b strings.Builder + for i := range recs { + if recs[i].Kind == KindOutput { + b.WriteString(recs[i].Text) + } + } + + return b.String() +} + +func readTree(t *testing.T, dir string) string { + t.Helper() + var b strings.Builder + require.NoError(t, filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error { + if err != nil || d.IsDir() { + return err + } + data, readErr := os.ReadFile(path) //nolint:gosec // test walks its own temp dir + b.Write(data) + + return readErr + })) + + return b.String() +} diff --git a/pkg/workspacelogs/import.go b/pkg/workspacelogs/import.go new file mode 100644 index 0000000000..2c8b97dd02 --- /dev/null +++ b/pkg/workspacelogs/import.go @@ -0,0 +1,268 @@ +package workspacelogs + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" +) + +func decodeReadResponse(data []byte) (*ReadResponse, error) { + var resp ReadResponse + if err := json.Unmarshal(data, &resp); err != nil { + return nil, fmt.Errorf("%w: decode response: %v", ErrInvalidRecord, err) + } + if resp.SchemaVersion != SchemaVersion { + return nil, fmt.Errorf("%w: schema version %d", ErrInvalidRecord, resp.SchemaVersion) + } + + return &resp, nil +} + +// LocalPositions returns the last committed sequence of every producer of the +// run. A refresh asks the remote side only for records after these positions. +func (s *Store) LocalPositions(runID string) (map[string]uint64, error) { + m, err := s.Manifest(runID) + if err != nil { + return nil, err + } + pos := make(map[string]uint64, len(m.Producers)) + for _, p := range m.Producers { + last, lastErr := s.lastCommitted(runID, p.ProducerID) + if lastErr != nil { + return nil, lastErr + } + pos[p.ProducerID] = last + } + + return pos, nil +} + +// lastCommitted scans the producer's last segment without repairing it, so a +// reader never writes. +func (s *Store) lastCommitted(runID, producerID string) (uint64, error) { + dir, err := s.producerDir(runID, producerID) + if err != nil { + return 0, err + } + segs, err := listSegments(dir) + if err != nil || len(segs) == 0 { + return 0, err + } + last := segs[len(segs)-1] + path := filepath.Join(dir, last.name) + if _, statErr := os.Lstat(path); statErr != nil { + return 0, fmt.Errorf("stat segment: %w", statErr) + } + seq := last.first - 1 + err = scanSegment(path, func(rec *Record) bool { + if rec != nil { + seq = max(seq, rec.Sequence) + } + + return true + }) + + return seq, err +} + +// PositionCursor returns an unfiltered run cursor that resumes after pos. +func PositionCursor(runID string, pos map[string]uint64) (string, error) { + return (&cursor{ + Version: SchemaVersion, Kind: cursorRun, Run: runID, + Filter: filterHash("", ""), Pos: pos, + }).encode() +} + +// ImportRequest is one validated remote page to merge into the local store. +type ImportRequest struct { + // RunID, Context and WorkspaceUID are the identity the caller expects. The + // response must match all of them. + RunID string + Context string + WorkspaceUID string + Response *ReadResponse +} + +// ImportResult reports what a merge changed. +type ImportResult struct { + Written int + Duplicates int + // Stalled lists producers whose records could not be merged past a gap or + // a malformed remote record. Their remaining records are not imported. + Stalled []string +} + +// ImportRemote merges a remote page into the local run. Records are +// deduplicated by producer and sequence. It rejects a response for another +// run, context or workspace generation, oversized pages and invalid records. +// Nothing is written when validation fails. +func (s *Store) ImportRemote(req ImportRequest) (ImportResult, error) { + m, err := s.Manifest(req.RunID) + if err != nil { + return ImportResult{}, err + } + if err = validateImport(m, req); err != nil { + return ImportResult{}, err + } + groups, order := groupByProducer(req.Response.Records) + var res ImportResult + for _, producerID := range order { + stalled, mergeErr := s.importProducer(m, producerID, groups[producerID], &res) + if mergeErr != nil { + return res, mergeErr + } + if stalled { + res.Stalled = append(res.Stalled, producerID) + } + } + + return res, nil +} + +func (s *Store) importProducer( + m *RunManifest, producerID string, records []Record, res *ImportResult, +) (bool, error) { + if i := m.producer(producerID); i >= 0 && m.Producers[i].Location != LocationContainer { + return s.stall(m.RunID, nil, ConditionMalformedRecord) + } + + return s.mergeProducer(m.RunID, producerID, records, res) +} + +func validateImport(m *RunManifest, req ImportRequest) error { + resp := req.Response + if resp == nil || resp.SchemaVersion != SchemaVersion { + return fmt.Errorf("%w: missing or unsupported response", ErrInvalidRecord) + } + if err := validateResponseIdentity(m, req); err != nil { + return err + } + if len(resp.Records) > MaxReadLimit { + return fmt.Errorf("%w: page has too many records", ErrInvalidRecord) + } + for i := range resp.Records { + if err := validateImportRecord(&resp.Records[i], req.RunID); err != nil { + return err + } + } + + return nil +} + +func validateResponseIdentity(m *RunManifest, req ImportRequest) error { + run := req.Response.Run + if run.RunID != req.RunID || run.Context != req.Context { + return fmt.Errorf("%w: response is for another run or context", ErrIdentityConflict) + } + if err := checkRunIdentity(m, req.Context, req.WorkspaceUID); err != nil { + return err + } + if run.WorkspaceUID != nil && *run.WorkspaceUID != req.WorkspaceUID { + return fmt.Errorf("%w: response is for another workspace", ErrIdentityConflict) + } + + return nil +} + +func validateImportRecord(rec *Record, runID string) error { + if rec.RunID != runID { + return fmt.Errorf("%w: record belongs to another run", ErrIdentityConflict) + } + if isSynthetic(rec) { + return nil + } + + return rec.Validate() +} + +// isSynthetic reports whether rec is a capture note built at read time (a +// retention gap or a malformed line). Stored records always carry a time. +func isSynthetic(rec *Record) bool { + return rec.Kind == KindCapture && rec.Time.IsZero() +} + +func groupByProducer(records []Record) (map[string][]Record, []string) { + groups := map[string][]Record{} + var order []string + for i := range records { + id := records[i].ProducerID + if _, seen := groups[id]; !seen { + order = append(order, id) + } + groups[id] = append(groups[id], records[i]) + } + + return groups, order +} + +// mergeProducer appends the contiguous stored prefix of one producer's +// records. A synthetic note ends the prefix because its sequence has no stored +// record to merge. +func (s *Store) mergeProducer( + runID, producerID string, records []Record, res *ImportResult, +) (bool, error) { + if err := requireID("producerId", producerID); err != nil { + return false, err + } + batch, cond := storedPrefix(records) + if err := s.ensureProducer(runID, producerID, records[0]); err != nil { + return s.stall(runID, err, ConditionSourceUnavailable) + } + if len(batch) > 0 { + out, err := s.Append(batch) + res.Written += out.Written + res.Duplicates += out.Duplicates + if err != nil { + return s.stallOnAppendError(runID, err) + } + } + if cond != "" { + return s.stall(runID, nil, cond) + } + + return false, nil +} + +func (s *Store) stallOnAppendError(runID string, err error) (bool, error) { + switch { + case errors.Is(err, ErrSequenceGap): + return s.stall(runID, err, ConditionRetentionGap) + case errors.Is(err, ErrCapacity): + return s.stall(runID, err, ConditionCapacity) + case errors.Is(err, ErrInvalidRecord): + return s.stall(runID, err, ConditionMalformedRecord) + default: + return false, err + } +} + +// storedPrefix returns the records before the first synthetic note and the +// condition that note stands for. +func storedPrefix(records []Record) ([]Record, Condition) { + for i := range records { + if isSynthetic(&records[i]) { + return records[:i], records[i].Condition + } + } + + return records, "" +} + +func (s *Store) ensureProducer(runID, producerID string, first Record) error { + ref := ProducerRef{ProducerID: producerID, State: ProducerRunning, Location: LocationContainer} + if first.Kind == KindProducer { + ref.ParentProducerID = first.ParentProducerID + } + + return s.RegisterProducer(runID, ref) +} + +func (s *Store) stall(runID string, cause error, c Condition) (bool, error) { + if err := s.AddCondition(runID, c); err != nil { + return true, errors.Join(cause, err) + } + + return true, nil +} diff --git a/pkg/workspacelogs/import_test.go b/pkg/workspacelogs/import_test.go new file mode 100644 index 0000000000..90be05f565 --- /dev/null +++ b/pkg/workspacelogs/import_test.go @@ -0,0 +1,203 @@ +package workspacelogs + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// remoteRun builds a spool with one finished producer and returns its first +// page as the remote side would send it. +func remotePage(t *testing.T) (*ReadResponse, string) { + t.Helper() + root, runID := spoolWithRun(t, uidA) + resp, err := ReadSpoolPage(SpoolSelector{ + Root: root, Context: testContext, WorkspaceUID: uidA, Read: ReadRequest{RunID: runID}, + }) + require.NoError(t, err) + + return resp, runID +} + +// localRunFor creates the local run that mirrors the remote one. +func localRunFor(t *testing.T, runID, uid string) *Store { + t.Helper() + s, _ := newTestStore(t) + _, err := s.CreateRun( + RunSpec{RunID: runID, Context: testContext, WorkspaceUID: uid, Command: "up"}, + ) + require.NoError(t, err) + + return s +} + +func importReq(runID, uid string, resp *ReadResponse) ImportRequest { + return ImportRequest{RunID: runID, Context: testContext, WorkspaceUID: uid, Response: resp} +} + +func TestImportRemoteMergesAndDeduplicates(t *testing.T) { + resp, runID := remotePage(t) + local := localRunFor(t, runID, uidA) + + res, err := local.ImportRemote(importReq(runID, uidA, resp)) + require.NoError(t, err) + require.Equal(t, len(resp.Records), res.Written) + require.Empty(t, res.Stalled) + + again, err := local.ImportRemote(importReq(runID, uidA, resp)) + require.NoError(t, err) + require.Zero(t, again.Written) + require.Equal(t, len(resp.Records), again.Duplicates) + + got := readAll(t, local, ReadRequest{RunID: runID}) + require.Equal(t, "hello\n", texts(got)) + m, err := local.Manifest(runID) + require.NoError(t, err) + require.True(t, m.Complete()) +} + +func TestImportRemoteRejectsMismatchedIdentity(t *testing.T) { + resp, runID := remotePage(t) + local := localRunFor(t, runID, uidA) + + cases := map[string]ImportRequest{ + "run": importReq(NewID(), uidA, resp), + "context": {RunID: runID, Context: otherContext, WorkspaceUID: uidA, Response: resp}, + "uid": importReq(runID, "uid-b", resp), + "nil": importReq(runID, uidA, nil), + } + for name, req := range cases { + _, err := local.ImportRemote(req) + require.Error(t, err, name) + } + require.Empty(t, readAll(t, local, ReadRequest{RunID: runID})) +} + +func TestImportRemoteRejectsForeignRecordAndOversizedPage(t *testing.T) { + resp, runID := remotePage(t) + local := localRunFor(t, runID, uidA) + + foreign := *resp + foreign.Records = append([]Record(nil), resp.Records...) + foreign.Records[0].RunID = NewID() + _, err := local.ImportRemote(importReq(runID, uidA, &foreign)) + require.ErrorIs(t, err, ErrIdentityConflict) + + big := *resp + big.Records = make([]Record, MaxReadLimit+1) + _, err = local.ImportRemote(importReq(runID, uidA, &big)) + require.ErrorIs(t, err, ErrInvalidRecord) + + bad := *resp + bad.Records = append([]Record(nil), resp.Records...) + bad.Records[1].Stream = "bogus" + _, err = local.ImportRemote(importReq(runID, uidA, &bad)) + require.ErrorIs(t, err, ErrInvalidRecord) + require.Empty(t, readAll(t, local, ReadRequest{RunID: runID})) +} + +func TestImportRemoteStallsAtRetentionGap(t *testing.T) { + resp, runID := remotePage(t) + local := localRunFor(t, runID, uidA) + + gapped := *resp + gapped.Records = []Record{ + resp.Records[0], + capture(Record{RunID: runID, ProducerID: resp.Records[0].ProducerID, Sequence: 2}, + ConditionRetentionGap, "sequences 2-3 are not retained"), + resp.Records[3], + } + res, err := local.ImportRemote(importReq(runID, uidA, &gapped)) + require.NoError(t, err) + require.Equal(t, 1, res.Written) + require.Equal(t, []string{resp.Records[0].ProducerID}, res.Stalled) + + m, err := local.Manifest(runID) + require.NoError(t, err) + require.Contains(t, m.Conditions, ConditionRetentionGap) + require.False(t, m.Complete()) +} + +func TestImportRemoteSkipAheadStalls(t *testing.T) { + resp, runID := remotePage(t) + local := localRunFor(t, runID, uidA) + + ahead := *resp + ahead.Records = resp.Records[2:] + res, err := local.ImportRemote(importReq(runID, uidA, &ahead)) + require.NoError(t, err) + require.Zero(t, res.Written) + require.Len(t, res.Stalled, 1) +} + +func TestImportRemoteSealedDiscoveryRejectsUnknownProducer(t *testing.T) { + resp, runID := remotePage(t) + local := localRunFor(t, runID, uidA) + require.NoError(t, local.SealDiscovery(runID)) + + res, err := local.ImportRemote(importReq(runID, uidA, resp)) + require.NoError(t, err) + require.Zero(t, res.Written) + require.Len(t, res.Stalled, 1) +} + +func TestLocalPositionsResumeAfterImport(t *testing.T) { + resp, runID := remotePage(t) + local := localRunFor(t, runID, uidA) + _, err := local.ImportRemote(importReq(runID, uidA, resp)) + require.NoError(t, err) + + pos, err := local.LocalPositions(runID) + require.NoError(t, err) + producerID := resp.Records[0].ProducerID + require.Equal(t, uint64(len(resp.Records)), pos[producerID]) + + token, err := PositionCursor(runID, pos) + require.NoError(t, err) + next, err := local.Read(ReadRequest{RunID: runID, After: token}) + require.NoError(t, err) + require.Empty(t, next.Records) +} + +func TestImportRemoteDoesNotBindUnboundRunToResponseUID(t *testing.T) { + resp, runID := remotePage(t) + local := localRunFor(t, runID, "") + + _, err := local.ImportRemote(importReq(runID, uidA, resp)) + require.NoError(t, err) + m, err := local.Manifest(runID) + require.NoError(t, err) + require.Nil(t, m.WorkspaceUID) +} + +func TestImportRemoteIgnoresRemoteClaimsOnLocalProducers(t *testing.T) { + resp, runID := remotePage(t) + local := localRunFor(t, runID, uidA) + producerID := resp.Records[0].ProducerID + require.NoError(t, local.RegisterProducer(runID, ProducerRef{ + ProducerID: producerID, State: ProducerRunning, Location: LocationHost, + })) + + res, err := local.ImportRemote(importReq(runID, uidA, resp)) + require.NoError(t, err) + assert.Zero(t, res.Written) + assert.Equal(t, []string{producerID}, res.Stalled) + m, err := local.Manifest(runID) + require.NoError(t, err) + assert.Equal(t, ProducerRunning, m.Producers[0].State) + assert.Contains(t, m.Conditions, ConditionMalformedRecord) +} + +func TestImportRemoteStallsOnMalformedProducerBatch(t *testing.T) { + resp, runID := remotePage(t) + local := localRunFor(t, runID, uidA) + resp.Records = append(resp.Records[:1:1], resp.Records[2:]...) + + res, err := local.ImportRemote(importReq(runID, uidA, resp)) + require.NoError(t, err, "a malformed remote batch stalls the producer, not the refresh") + assert.Len(t, res.Stalled, 1) + m, err := local.Manifest(runID) + require.NoError(t, err) + assert.Contains(t, m.Conditions, ConditionMalformedRecord) +} diff --git a/pkg/workspacelogs/mirror.go b/pkg/workspacelogs/mirror.go new file mode 100644 index 0000000000..6919ccd9f8 --- /dev/null +++ b/pkg/workspacelogs/mirror.go @@ -0,0 +1,353 @@ +package workspacelogs + +import ( + "encoding/json" + "errors" + "fmt" + "sync" +) + +// ErrSourceGap means the next records a sender needs are no longer retained +// at its own store, so mirroring cannot continue past them. +var ErrSourceGap = errors.New("source records are not retained") + +// StoreSource reads a producer's committed records back from a Store in +// bounded batches for mirroring. +type StoreSource struct { + store *Store +} + +// NewStoreSource returns a source over store. +func NewStoreSource(store *Store) *StoreSource { return &StoreSource{store: store} } + +// ReadAfter returns the contiguous records of one producer after sequence +// after, at most maxBytes when JSON encoded. It returns ErrSourceGap when the +// first record needed is missing or unreadable. +func (s *StoreSource) ReadAfter( + runID, producerID string, after uint64, maxBytes int, +) ([]Record, error) { + b := &batchCollector{producerID: producerID, after: after, maxBytes: maxBytes} + cursor := "" + for { + resp, err := s.store.Read(ReadRequest{RunID: runID, After: cursor, Limit: MaxReadLimit}) + if err != nil { + return nil, err + } + if b.collect(resp.Records) { + return gapOrBatch(b.out, b.broken) + } + if !resp.HasMore { + return b.out, nil + } + cursor = resp.NextCursor + } +} + +// batchCollector gathers one producer's contiguous records from read pages. +type batchCollector struct { + producerID string + after uint64 + maxBytes int + used int + out []Record + broken bool +} + +// collect adds the producer's records from one page. It returns true once the +// batch is full or the sequence breaks. +func (b *batchCollector) collect(page []Record) bool { + for i := range page { + rec := &page[i] + if rec.ProducerID == b.producerID && rec.Sequence > b.after && b.add(rec) { + return true + } + } + + return false +} + +func (b *batchCollector) add(rec *Record) bool { + // Records synthesized by the reader for holes carry no time. + if rec.Sequence != b.after+uint64(len(b.out))+1 || rec.Time.IsZero() { + b.broken = true + + return true + } + data, err := json.Marshal(rec) + if err != nil || (b.used+len(data) > b.maxBytes && len(b.out) > 0) { + return true + } + b.used += len(data) + b.out = append(b.out, *rec) + + return false +} + +func gapOrBatch(out []Record, broken bool) ([]Record, error) { + if broken && len(out) == 0 { + return nil, ErrSourceGap + } + + return out, nil +} + +// MirrorResult is the typed outcome of one batch. +type MirrorResult string + +// Batch outcomes. +const ( + // MirrorAccepted means records up to Accepted are persisted. Accepted can be + // lower than the batch's last sequence when the receiver was behind. + MirrorAccepted MirrorResult = "accepted" + // MirrorRetentionGap means the receiver will never hold the gap range. + MirrorRetentionGap MirrorResult = "retention_gap" + // MirrorRejected means the batch is invalid and must not be retried. + MirrorRejected MirrorResult = "rejected_invalid" +) + +// BatchRequest is one contiguous batch of a producer's encoded records. +type BatchRequest struct { + SchemaVersion int + RunID string + ProducerID string + FirstSequence uint64 + LastSequence uint64 + // Records holds JSON encoded Record values. + Records [][]byte +} + +// BatchResult is the receiver's answer. +type BatchResult struct { + Result MirrorResult + // Accepted is the highest contiguous sequence persisted for the producer. + Accepted uint64 + GapFirst uint64 + GapLast uint64 + Detail string +} + +// batchOverhead is the allowance for the request envelope around the records. +const batchOverhead = 1024 + +// SinkOptions configures a Sink. +type SinkOptions struct { + Store *Store + // Spec identifies the run this host expects. RunID is required; the run is + // created or joined at construction. + Spec RunSpec + // Acks persists the inbound position and retention gaps. Optional. + Acks *AckStore + // OnAccepted is called after records are persisted, so a forwarding host can + // queue them for the next hop. It must not block. + OnAccepted func(producerID string, accepted uint64) +} + +// Sink validates incoming batches against the expected run and persists them +// idempotently. It acknowledges only what reached its store or was recorded as +// a gap. +type Sink struct { + opts SinkOptions + + mu sync.Mutex + known map[string]bool +} + +// NewSink creates or joins the expected run and returns its sink. +func NewSink(opts SinkOptions) (*Sink, error) { + if opts.Store == nil || opts.Spec.RunID == "" { + return nil, fmt.Errorf("%w: sink needs a store and a run ID", ErrInvalidRecord) + } + if _, err := opts.Store.CreateRun(opts.Spec); err != nil { + return nil, err + } + + return &Sink{opts: opts, known: map[string]bool{}}, nil +} + +// RunID returns the run this sink accepts. +func (s *Sink) RunID() string { return s.opts.Spec.RunID } + +// Append validates and persists one batch. A non-nil error is a transient +// storage failure the sender may retry. +func (s *Sink) Append(req BatchRequest) (BatchResult, error) { + records, reject := s.decode(req) + if reject != "" { + return BatchResult{Result: MirrorRejected, Detail: reject}, nil + } + if gap, ok := s.excluded(req); ok { + return gap, nil + } + if res, done, err := s.register(req, &records[0]); done { + return res, err + } + res, err := s.opts.Store.Append(records) + + return s.outcome(req, res, err) +} + +// decode checks identity, sizes and record types. The second result is a +// rejection reason, empty when the batch is valid. +func (s *Sink) decode(req BatchRequest) ([]Record, string) { + if reason := s.checkHeader(req); reason != "" { + return nil, reason + } + total := batchOverhead + records := make([]Record, len(req.Records)) + for i, raw := range req.Records { + if total += len(raw); total > MaxPageBytes { + return nil, "batch exceeds page size" + } + if reason := decodeRecord(raw, req, i, &records[i]); reason != "" { + return nil, reason + } + } + + return records, "" +} + +func (s *Sink) checkHeader(req BatchRequest) string { + n := len(req.Records) + switch { + case req.SchemaVersion != SchemaVersion: + return "unsupported schema version" + case req.RunID != s.opts.Spec.RunID: + return "unexpected run" + case !ValidID(req.ProducerID): + return "invalid producer" + case n == 0 || req.FirstSequence == 0 || req.LastSequence < req.FirstSequence || + req.LastSequence-req.FirstSequence+1 != uint64(n): //nolint:gosec // n is a slice length + return "inconsistent sequence range" + } + + return "" +} + +func decodeRecord(raw []byte, req BatchRequest, i int, rec *Record) string { + if err := json.Unmarshal(raw, rec); err != nil { + return "undecodable record" + } + if rec.RunID != req.RunID || rec.ProducerID != req.ProducerID || + rec.Sequence != req.FirstSequence+uint64(i) { //nolint:gosec // i is a slice index + return "record identity does not match the batch" + } + if err := rec.Validate(); err != nil { + return err.Error() + } + if rec.Kind != KindOutput && len(raw) > MaxMetadataBytes { + return "metadata record is too large" + } + + return "" +} + +// excluded answers batches for a producer whose bytes the store already gave +// up on, so the sender stops retrying them. +func (s *Sink) excluded(req BatchRequest) (BatchResult, bool) { + if s.opts.Acks == nil { + return BatchResult{}, false + } + st, err := s.opts.Acks.Load( + AckKey{RunID: req.RunID, ProducerID: req.ProducerID, Hop: HopInbound}, + ) + if err != nil || !st.Gapped { + return BatchResult{}, false + } + first := max(req.FirstSequence, st.Acked+1) + if first > req.LastSequence { + return BatchResult{ + Result: MirrorRetentionGap, Accepted: st.Acked, + GapFirst: st.GapFirst, GapLast: st.GapLast, Detail: "producer excluded by retention", + }, true + } + + return BatchResult{ + Result: MirrorRetentionGap, Accepted: req.LastSequence, + GapFirst: first, GapLast: req.LastSequence, Detail: "producer excluded by retention", + }, true +} + +// register adds the producer to the run before its first batch. done reports +// that the batch is answered without appending. +func (s *Sink) register(req BatchRequest, first *Record) (BatchResult, bool, error) { + producerID := req.ProducerID + s.mu.Lock() + defer s.mu.Unlock() + if s.known[producerID] { + return BatchResult{}, false, nil + } + ref := ProducerRef{ProducerID: producerID, State: ProducerRunning, Location: first.Location} + if first.Kind == KindProducer { + ref.ParentProducerID = first.ParentProducerID + } + err := s.opts.Store.RegisterProducer(s.opts.Spec.RunID, ref) + switch { + case err == nil: + s.known[producerID] = true + + return BatchResult{}, false, nil + case errors.Is(err, ErrProducerCap): + res := s.gap(req, 0) + res.Detail = "producer limit reached" + + return res, true, nil + case errors.Is(err, ErrDiscoverySealed), errors.Is(err, ErrInvalidID): + return BatchResult{Result: MirrorRejected, Detail: err.Error()}, true, nil + default: + return BatchResult{}, true, err + } +} + +func (s *Sink) outcome(req BatchRequest, res AppendResult, err error) (BatchResult, error) { + switch { + case err == nil: + s.persisted(req, res.Accepted) + + return BatchResult{Result: MirrorAccepted, Accepted: res.Accepted}, nil + case errors.Is(err, ErrSequenceGap): + // Behind the batch: report what is held so the sender resends from there. + return BatchResult{Result: MirrorAccepted, Accepted: res.Accepted}, nil + case errors.Is(err, ErrCapacity): + return s.gap(req, res.Accepted), nil + case errors.Is(err, ErrInvalidRecord): + return BatchResult{Result: MirrorRejected, Accepted: res.Accepted, Detail: err.Error()}, nil + default: + s.persisted(req, res.Accepted) + + return BatchResult{}, err + } +} + +func (s *Sink) persisted(req BatchRequest, accepted uint64) { + if accepted == 0 { + return + } + if s.opts.Acks != nil { + _ = s.opts.Acks.Advance( + AckKey{RunID: req.RunID, ProducerID: req.ProducerID, Hop: HopInbound}, + accepted, + ) + } + if s.opts.OnAccepted != nil { + s.opts.OnAccepted(req.ProducerID, accepted) + } +} + +// gap records that the store cannot take the rest of the batch. Capture is +// marked partial and the position moves past the range. +func (s *Sink) gap(req BatchRequest, accepted uint64) BatchResult { + s.persisted(req, accepted) + first := max(req.FirstSequence, accepted+1) + _ = s.opts.Store.AddCondition(req.RunID, ConditionRetentionGap) + if s.opts.Acks != nil { + _ = s.opts.Acks.RecordGap( + AckKey{RunID: req.RunID, ProducerID: req.ProducerID, Hop: HopInbound}, + first, + req.LastSequence, + ) + } + + return BatchResult{ + Result: MirrorRetentionGap, Accepted: req.LastSequence, + GapFirst: first, GapLast: req.LastSequence, Detail: "retention excluded the batch", + } +} diff --git a/pkg/workspacelogs/mirror_test.go b/pkg/workspacelogs/mirror_test.go new file mode 100644 index 0000000000..b6a3111ce4 --- /dev/null +++ b/pkg/workspacelogs/mirror_test.go @@ -0,0 +1,311 @@ +package workspacelogs + +import ( + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +type sinkFixture struct { + sink *Sink + store *Store + acks *AckStore + runID string + acc map[string]uint64 +} + +func newSinkFixture(t *testing.T) *sinkFixture { + t.Helper() + store, _ := newTestStore(t) + acks, err := NewAckStore(filepath.Join(t.TempDir(), "acks")) + require.NoError(t, err) + f := &sinkFixture{store: store, acks: acks, runID: NewID(), acc: map[string]uint64{}} + f.sink, err = NewSink(SinkOptions{ + Store: store, Acks: acks, + Spec: RunSpec{RunID: f.runID, Context: testContext, Command: "up"}, + OnAccepted: func(p string, n uint64) { f.acc[p] = n }, + }) + require.NoError(t, err) + + return f +} + +func batchOf(runID, producerID string, recs ...Record) BatchRequest { + req := BatchRequest{ + SchemaVersion: SchemaVersion, RunID: runID, ProducerID: producerID, + FirstSequence: recs[0].Sequence, LastSequence: recs[len(recs)-1].Sequence, + } + for i := range recs { + data, _ := json.Marshal(recs[i]) + req.Records = append(req.Records, data) + } + + return req +} + +func TestSinkAcceptsAndDeduplicates(t *testing.T) { + f := newSinkFixture(t) + pid := NewID() + seq := newSeq(f.runID, pid) + inv := NewID() + req := batchOf(f.runID, pid, + seq.invocation(inv, hookCreate, InvocationRunning), + seq.output(inv, StreamStdout, "hello\n"), + ) + + for range 3 { + res, err := f.sink.Append(req) + require.NoError(t, err) + require.Equal(t, MirrorAccepted, res.Result) + require.Equal(t, uint64(2), res.Accepted) + } + require.Equal(t, "hello\n", texts(readAll(t, f.store, ReadRequest{RunID: f.runID}))) + require.Equal(t, uint64(2), f.acc[pid]) + st, err := f.acks.Load(AckKey{RunID: f.runID, ProducerID: pid, Hop: HopInbound}) + require.NoError(t, err) + require.Equal(t, uint64(2), st.Acked) +} + +func TestSinkRejectsInvalidBatches(t *testing.T) { + f := newSinkFixture(t) + pid, inv := NewID(), NewID() + good := func() []Record { + seq := newSeq(f.runID, pid) + + return []Record{seq.output(inv, StreamStdout, "x")} + } + tooBig := newSeq(f.runID, pid).output(inv, StreamStdout, strings.Repeat("a", MaxChunkBytes+1)) + badStream := newSeq(f.runID, pid).output(inv, Stream("tty"), "x") + badKind := newSeq(f.runID, pid).rec(Record{Kind: Kind("shell")}) + otherRun := newSeq(NewID(), pid).output(inv, StreamStdout, "x") + + cases := map[string]BatchRequest{ + "wrong run": batchOf(NewID(), pid, good()...), + "foreign rec": batchOf(f.runID, pid, otherRun), + "bad producer": batchOf(f.runID, "../etc", good()...), + "oversized": batchOf(f.runID, pid, tooBig), + "wrong stream": batchOf(f.runID, pid, badStream), + "wrong kind": batchOf(f.runID, pid, badKind), + "old schema": func() BatchRequest { r := batchOf(f.runID, pid, good()...); r.SchemaVersion = 0; return r }(), + "range mismtch": func() BatchRequest { r := batchOf(f.runID, pid, good()...); r.LastSequence = 9; return r }(), + "empty": {SchemaVersion: SchemaVersion, RunID: f.runID, ProducerID: pid}, + "garbage": { + SchemaVersion: SchemaVersion, + RunID: f.runID, + ProducerID: pid, + FirstSequence: 1, + LastSequence: 1, + Records: [][]byte{[]byte("{")}, + }, + } + for name, req := range cases { + res, err := f.sink.Append(req) + require.NoError(t, err, name) + require.Equal(t, MirrorRejected, res.Result, name) + } + require.Empty(t, f.acc) +} + +func TestSinkRejectsOversizedPage(t *testing.T) { + f := newSinkFixture(t) + pid, inv := NewID(), NewID() + seq := newSeq(f.runID, pid) + var recs []Record + for range 40 { + recs = append(recs, seq.output(inv, StreamStdout, strings.Repeat("a", MaxChunkBytes))) + } + res, err := f.sink.Append(batchOf(f.runID, pid, recs...)) + require.NoError(t, err) + require.Equal(t, MirrorRejected, res.Result) +} + +func TestSinkDoesNotAcknowledgeAheadOfPersisted(t *testing.T) { + f := newSinkFixture(t) + pid, inv := NewID(), NewID() + seq := newSeq(f.runID, pid) + first := seq.output(inv, StreamStdout, "a") + _ = seq.output(inv, StreamStdout, "b") + third := seq.output(inv, StreamStdout, "c") + + res, err := f.sink.Append(batchOf(f.runID, pid, first)) + require.NoError(t, err) + require.Equal(t, uint64(1), res.Accepted) + + // Sequence 2 is missing: the receiver must keep acknowledging 1. + res, err = f.sink.Append(batchOf(f.runID, pid, third)) + require.NoError(t, err) + require.Equal(t, MirrorAccepted, res.Result) + require.Equal(t, uint64(1), res.Accepted) + require.Equal(t, uint64(1), f.acc[pid]) + require.Equal(t, "a", texts(readAll(t, f.store, ReadRequest{RunID: f.runID}))) +} + +func TestSinkRepresentsRetentionGap(t *testing.T) { + f := newSinkFixture(t) + pid, inv := NewID(), NewID() + seq := newSeq(f.runID, pid) + res, err := f.sink.Append(batchOf(f.runID, pid, seq.output(inv, StreamStdout, "a"))) + require.NoError(t, err) + require.Equal(t, uint64(1), res.Accepted) + + // Fill the run so the store cannot take anything more. + junk := filepath.Join(f.store.root, runsDirName, f.runID, "junk") + require.NoError(t, os.WriteFile(junk, make([]byte, MaxRunBytes), 0o600)) + + res, err = f.sink.Append(batchOf(f.runID, pid, + seq.output(inv, StreamStdout, "b"), seq.output(inv, StreamStdout, "c"))) + require.NoError(t, err) + require.Equal(t, MirrorRetentionGap, res.Result) + require.Equal(t, uint64(2), res.GapFirst) + require.Equal(t, uint64(3), res.GapLast) + require.Equal(t, uint64(3), res.Accepted) + + m, err := f.store.Manifest(f.runID) + require.NoError(t, err) + require.Contains(t, m.Conditions, ConditionRetentionGap) + require.Equal(t, CapturePartial, m.CaptureState) + + // A later batch for the excluded producer is answered as a gap too. + res, err = f.sink.Append(batchOf(f.runID, pid, seq.output(inv, StreamStdout, "d"))) + require.NoError(t, err) + require.Equal(t, MirrorRetentionGap, res.Result) + require.Equal(t, uint64(4), res.GapLast) + require.Equal(t, "a", texts(readAll(t, f.store, ReadRequest{RunID: f.runID}))) +} + +func TestSinkRejectsLateProducerAfterSeal(t *testing.T) { + f := newSinkFixture(t) + require.NoError(t, f.store.SealDiscovery(f.runID)) + pid := NewID() + res, err := f.sink.Append(batchOf(f.runID, pid, newSeq(f.runID, pid).terminal(false))) + require.NoError(t, err) + require.Equal(t, MirrorRejected, res.Result) +} + +func TestAckStorePersistsForwardOnly(t *testing.T) { + dir := filepath.Join(t.TempDir(), "acks") + acks, err := NewAckStore(dir) + require.NoError(t, err) + run, pid := NewID(), NewID() + + require.NoError(t, acks.Advance(AckKey{RunID: run, ProducerID: pid, Hop: HopUpstream}, 7)) + require.NoError(t, acks.Advance(AckKey{RunID: run, ProducerID: pid, Hop: HopUpstream}, 3)) + require.NoError(t, acks.Advance(AckKey{RunID: run, ProducerID: pid, Hop: HopInbound}, 2)) + + reopened, err := NewAckStore(dir) + require.NoError(t, err) + up, err := reopened.Load(AckKey{RunID: run, ProducerID: pid, Hop: HopUpstream}) + require.NoError(t, err) + in, err := reopened.Load(AckKey{RunID: run, ProducerID: pid, Hop: HopInbound}) + require.NoError(t, err) + require.Equal(t, uint64(7), up.Acked) + require.Equal(t, uint64(2), in.Acked) + + _, err = reopened.Load(AckKey{RunID: "../x", ProducerID: pid, Hop: HopUpstream}) + require.ErrorIs(t, err, ErrInvalidID) + _, err = NewAckStore("relative") + require.ErrorIs(t, err, ErrUnsafePath) +} + +func TestStoreSourceReadsContiguousBatches(t *testing.T) { + store, _ := newTestStore(t) + runID := newRun(t, store, uidA) + pid := newProducer(t, store, runID) + other := newProducer(t, store, runID) + seq, otherSeq, inv := newSeq(runID, pid), newSeq(runID, other), NewID() + for range 5 { + mustAppend(t, store, seq.output(inv, StreamStdout, "line\n")) + mustAppend(t, store, otherSeq.output(inv, StreamStdout, "other\n")) + } + src := NewStoreSource(store) + + recs, err := src.ReadAfter(runID, pid, 2, MaxPageBytes) + require.NoError(t, err) + require.Len(t, recs, 3) + require.Equal(t, uint64(3), recs[0].Sequence) + + small, err := src.ReadAfter(runID, pid, 0, 1) + require.NoError(t, err) + require.Len(t, small, 1) + + none, err := src.ReadAfter(runID, pid, 5, MaxPageBytes) + require.NoError(t, err) + require.Empty(t, none) +} + +func TestStoreSourceReportsSourceGap(t *testing.T) { + store, _ := newTestStore(t) + runID := newRun(t, store, uidA) + pid := newProducer(t, store, runID) + seq, inv := newSeq(runID, pid), NewID() + mustAppend(t, store, seq.output(inv, StreamStdout, "a")) + dir, err := store.producerDir(runID, pid) + require.NoError(t, err) + segs, err := listSegments(dir) + require.NoError(t, err) + f, err := os.OpenFile( //nolint:gosec // test writes inside its own temp dir + filepath.Join(dir, segs[0].name), os.O_APPEND|os.O_WRONLY, 0o600) + require.NoError(t, err) + _, err = f.WriteString("not json\n") + require.NoError(t, err) + require.NoError(t, f.Close()) + + _, err = NewStoreSource(store).ReadAfter(runID, pid, 1, MaxPageBytes) + require.ErrorIs(t, err, ErrSourceGap) +} + +func TestSinkRejectsUnknownProducerAndCaptureValues(t *testing.T) { + f := newSinkFixture(t) + pid := NewID() + seq := newSeq(f.runID, pid) + + badState := seq.rec(Record{Kind: KindProducer, ProducerState: "done"}) + res, err := f.sink.Append(batchOf(f.runID, pid, badState)) + require.NoError(t, err) + require.Equal(t, MirrorRejected, res.Result) + + badCondition := seq.rec(Record{Kind: KindCapture, Condition: "made-up"}) + res, err = f.sink.Append(batchOf(f.runID, pid, badCondition)) + require.NoError(t, err) + require.Equal(t, MirrorRejected, res.Result) + + emptyCondition := seq.rec(Record{Kind: KindCapture}) + res, err = f.sink.Append(batchOf(f.runID, pid, emptyCondition)) + require.NoError(t, err) + require.Equal(t, MirrorRejected, res.Result) +} + +func TestSinkAnswersProducerCapForWholeBatch(t *testing.T) { + f := newSinkFixture(t) + for range MaxProducers { + pid := NewID() + inv := NewID() + res, err := f.sink.Append(batchOf(f.runID, pid, + newSeq(f.runID, pid).invocation(inv, hookCreate, InvocationRunning))) + require.NoError(t, err) + require.Equal(t, MirrorAccepted, res.Result) + } + + pid := NewID() + seq := newSeq(f.runID, pid) + inv := NewID() + req := batchOf(f.runID, pid, + seq.invocation(inv, hookCreate, InvocationRunning), + seq.output(inv, StreamStdout, "a\n"), + seq.output(inv, StreamStdout, "b\n"), + ) + res, err := f.sink.Append(req) + require.NoError(t, err) + require.Equal(t, MirrorRetentionGap, res.Result) + require.Equal(t, req.LastSequence, res.Accepted) + require.Equal(t, req.FirstSequence, res.GapFirst) + require.Equal(t, req.LastSequence, res.GapLast) + + again, err := f.sink.Append(req) + require.NoError(t, err) + require.Equal(t, MirrorRetentionGap, again.Result) +} diff --git a/pkg/workspacelogs/reader.go b/pkg/workspacelogs/reader.go new file mode 100644 index 0000000000..a166831fe0 --- /dev/null +++ b/pkg/workspacelogs/reader.go @@ -0,0 +1,506 @@ +package workspacelogs + +import ( + "encoding/json" + "fmt" + "maps" + "path/filepath" + "slices" +) + +// Page limits. +const ( + DefaultReadLimit = 500 + MaxReadLimit = 2000 + DefaultIndexLimit = 20 + MaxIndexLimit = 100 + + // envelopeReserve is page space kept for the response around the records. + envelopeReserve = 4 * 1024 +) + +// ReadRequest selects one page of a run. +type ReadRequest struct { + RunID string + Hook string + InvocationID string + // After is the NextCursor of the previous page. + After string + // Limit is the most records in the page. Zero means DefaultReadLimit. + Limit int + // MaxBytes caps the encoded page. Zero or less means MaxPageBytes. + MaxBytes int +} + +// SourceAvailability tells whether a producer's records can be read. +type SourceAvailability struct { + ProducerID string `json:"producerId"` + Available bool `json:"available"` + Reason string `json:"reason,omitempty"` +} + +// ReadResponse is one bounded page. +type ReadResponse struct { + SchemaVersion int `json:"schemaVersion"` + Run RunSummary `json:"run"` + Records []Record `json:"records"` + NextCursor string `json:"nextCursor,omitempty"` + HasMore bool `json:"hasMore"` + Availability []SourceAvailability `json:"availability,omitempty"` + Complete bool `json:"complete"` +} + +// Read returns the next page of a run. Output is read from disk in pages; +// the archive is never loaded whole. +func (s *Store) Read(req ReadRequest) (*ReadResponse, error) { + limit, maxBytes, err := readLimits(req) + if err != nil { + return nil, err + } + plan, err := s.prepareRead(req) + if err != nil { + return nil, err + } + pg := newPage(limit, maxBytes-envelopeReserve, plan.cur.Pos) + pg.gaps = plan.cur.Gaps + for _, p := range plan.m.Producers { + if err = s.readProducer(plan.m.RunID, p.ProducerID, plan.f, pg); err != nil { + return nil, err + } + if pg.full { + break + } + } + + return s.response(plan.m, plan.f, pg) +} + +type readPlan struct { + m *RunManifest + f *filter + cur *cursor +} + +// prepareRead resolves the run, filter and start position, and rejects a +// cursor bound to a different run or filter. +func (s *Store) prepareRead(req ReadRequest) (*readPlan, error) { + m, err := s.Manifest(req.RunID) + if err != nil { + return nil, err + } + f, err := s.newFilter(m, req) + if err != nil { + return nil, err + } + cur, err := decodeCursor(req.After, cursorRun) + if err != nil { + return nil, err + } + if req.After != "" && (cur.Run != req.RunID || cur.Filter != f.hash) { + return nil, fmt.Errorf( + "%w: cursor does not match the selected run or filters", + ErrInvalidCursor, + ) + } + + return &readPlan{m: m, f: f, cur: cur}, nil +} + +func readLimits(req ReadRequest) (limit, maxBytes int, err error) { + limit, maxBytes = req.Limit, req.MaxBytes + if limit == 0 { + limit = DefaultReadLimit + } + if maxBytes <= 0 { + maxBytes = MaxPageBytes + } + if limit < 0 || limit > MaxReadLimit { + return 0, 0, fmt.Errorf( + "%w: limit must be between 1 and %d", + ErrInvalidSelector, + MaxReadLimit, + ) + } + if maxBytes > MaxPageBytes || maxBytes <= envelopeReserve { + return 0, 0, fmt.Errorf("%w: invalid byte cap", ErrInvalidSelector) + } + + return limit, maxBytes, nil +} + +func (s *Store) response(m *RunManifest, f *filter, pg *page) (*ReadResponse, error) { + resp := &ReadResponse{ + SchemaVersion: SchemaVersion, + Run: m.Summary(), + Records: pg.records, + HasMore: pg.full, + Complete: m.Complete() && !pg.full && !pg.gaps, + } + if resp.Records == nil { + resp.Records = []Record{} + } + for _, p := range m.Producers { + a := SourceAvailability{ProducerID: p.ProducerID, Available: p.State != ProducerUnavailable} + if !a.Available { + a.Reason = string(ConditionSourceUnavailable) + } + resp.Availability = append(resp.Availability, a) + } + token, err := (&cursor{ + Version: SchemaVersion, Kind: cursorRun, Run: m.RunID, Filter: f.hash, Pos: pg.pos, Gaps: pg.gaps, + }).encode() + if err != nil { + return nil, err + } + resp.NextCursor = token + + return resp, nil +} + +// filter selects records by hook or invocation. Invocation membership is +// resolved up front from the invocation records so a resumed page filters +// the same way as the first. +type filter struct { + active bool + hash string + allowed map[string]bool +} + +func (s *Store) newFilter(m *RunManifest, req ReadRequest) (*filter, error) { + f := &filter{hash: filterHash(req.Hook, req.InvocationID)} + if req.Hook == "" && req.InvocationID == "" { + return f, nil + } + if err := validateFilterSelectors(req); err != nil { + return nil, err + } + hooks, err := s.invocationHooks(m) + if err != nil { + return nil, err + } + f.active, f.allowed = true, matchInvocations(hooks, req) + if req.InvocationID != "" && !f.allowed[req.InvocationID] { + return nil, fmt.Errorf("%w: invocation is not part of this run", ErrNotFound) + } + + return f, nil +} + +func validateFilterSelectors(req ReadRequest) error { + if req.Hook != "" && !ValidHook(req.Hook) { + return fmt.Errorf("%w: unknown hook %q", ErrInvalidSelector, req.Hook) + } + if req.InvocationID != "" { + return requireID("invocationId", req.InvocationID) + } + + return nil +} + +// matchInvocations returns the invocation IDs that satisfy every set selector. +func matchInvocations(hooks map[string]string, req ReadRequest) map[string]bool { + allowed := map[string]bool{} + for id, hook := range hooks { + if (req.Hook == "" || hook == req.Hook) && + (req.InvocationID == "" || id == req.InvocationID) { + allowed[id] = true + } + } + + return allowed +} + +func (f *filter) accept(rec *Record) bool { + if !f.active || rec.Kind == KindCapture { + return true + } + if rec.Kind == KindProducer { + return false + } + + return f.allowed[rec.InvocationID] +} + +// invocationHooks maps every invocation ID in the run to its hook name. +func (s *Store) invocationHooks(m *RunManifest) (map[string]string, error) { + hooks := map[string]string{} + for _, p := range m.Producers { + err := s.eachRecord(m.RunID, p.ProducerID, 1, func(rec *Record) bool { + if rec != nil && rec.Kind == KindInvocation { + hooks[rec.InvocationID] = rec.Hook + } + + return true + }) + if err != nil { + return nil, err + } + } + + return hooks, nil +} + +// eachRecord streams a producer's committed records from sequence from on. A +// nil record stands for a malformed committed line. +func (s *Store) eachRecord(runID, producerID string, from uint64, fn func(*Record) bool) error { + dir, err := s.producerDir(runID, producerID) + if err != nil { + return err + } + segs, err := listSegments(dir) + if err != nil { + return err + } + for _, seg := range segs[firstSegmentFor(segs, from):] { + stopped, scanErr := scanFrom(filepath.Join(dir, seg.name), seg.first, from, fn) + if scanErr != nil || stopped { + return scanErr + } + } + + return nil +} + +// firstSegmentFor returns the index of the segment holding sequence from. +func firstSegmentFor(segs []segment, from uint64) int { + start := 0 + for i, seg := range segs { + if seg.first <= from { + start = i + } + } + + return start +} + +// scanFrom passes fn the records of one segment at or after sequence from and +// reports whether fn stopped the scan. Lines are contiguous in a segment, so a +// malformed line's sequence is the one after the previous line. +func scanFrom(path string, first, from uint64, fn func(*Record) bool) (bool, error) { + stopped := false + implied := first + err := scanSegment(path, func(rec *Record) bool { + seq := implied + if rec != nil { + seq = rec.Sequence + } + implied = seq + 1 + if seq < from { + return true + } + stopped = !fn(rec) + + return !stopped + }) + + return stopped, err +} + +func (s *Store) readProducer(runID, producerID string, f *filter, pg *page) error { + return s.eachRecord(runID, producerID, pg.pos[producerID]+1, func(rec *Record) bool { + return pg.consume(runID, producerID, rec, f) + }) +} + +// page accumulates records for one response. +type page struct { + limit int + maxBytes int + used int + records []Record + pos map[string]uint64 + full bool + gaps bool +} + +func newPage(limit, maxBytes int, pos map[string]uint64) *page { + next := make(map[string]uint64, len(pos)) + maps.Copy(next, pos) + + return &page{limit: limit, maxBytes: maxBytes, pos: next} +} + +func (p *page) add(rec *Record) bool { + data, err := json.Marshal(rec) + if err != nil || len(p.records) >= p.limit || + (len(p.records) > 0 && p.used+len(data)+1 > p.maxBytes) { + p.full = true + + return false + } + p.used += len(data) + 1 + p.records = append(p.records, *rec) + + return true +} + +// consume adds one record, first reporting any missing sequence range, and +// advances the producer position. It returns false once the page is full. +func (p *page) consume(runID, producerID string, rec *Record, f *filter) bool { + next := p.pos[producerID] + 1 + if rec == nil { + return p.consumeMalformed(runID, producerID, next) + } + if rec.Sequence > next { + gap := capture( + Record{RunID: runID, ProducerID: producerID, Sequence: next}, + ConditionRetentionGap, + fmt.Sprintf("sequences %d-%d are not retained", next, rec.Sequence-1), + ) + if !p.add(&gap) { + return false + } + p.gaps = true + p.pos[producerID] = rec.Sequence - 1 + } + if f.accept(rec) && !p.add(rec) { + return false + } + p.pos[producerID] = rec.Sequence + + return true +} + +func (p *page) consumeMalformed(runID, producerID string, seq uint64) bool { + rec := capture(Record{RunID: runID, ProducerID: producerID, Sequence: seq}, + ConditionMalformedRecord, "a stored record could not be decoded") + if !p.add(&rec) { + return false + } + p.gaps = true + p.pos[producerID] = seq + + return true +} + +// capture turns where (run, producer, sequence) into a synthetic capture +// record. It is built at read time and never stored. +func capture(where Record, c Condition, detail string) Record { + where.SchemaVersion, where.Kind = SchemaVersion, KindCapture + where.Condition, where.Detail = c, detail + + return where +} + +// IndexRequest selects one page of the run index. +type IndexRequest struct { + Context string + // WorkspaceUID narrows the index to one workspace generation. + WorkspaceUID string + After string + // Limit is the most runs in the page. Zero means DefaultIndexLimit. + Limit int +} + +// IndexResponse is one page of runs, newest first by (createdAt, runId). +type IndexResponse struct { + SchemaVersion int `json:"schemaVersion"` + Runs []RunSummary `json:"runs"` + NextCursor string `json:"nextCursor,omitempty"` + HasMore bool `json:"hasMore"` +} + +// ListRuns returns retained runs for a context, including runs whose workspace +// no longer exists. +func (s *Store) ListRuns(req IndexRequest) (*IndexResponse, error) { + limit, cur, err := indexSelectors(req) + if err != nil { + return nil, err + } + var matches []*RunManifest + for _, r := range s.loadRuns() { + if inIndex(r.manifest, req) && (req.After == "" || olderThan(r.manifest, cur)) { + matches = append(matches, r.manifest) + } + } + + return indexPage(matches, req, limit) +} + +func indexLimit(req IndexRequest) (int, error) { + limit := req.Limit + if limit == 0 { + limit = DefaultIndexLimit + } + if req.Context == "" || limit < 0 || limit > MaxIndexLimit { + return 0, fmt.Errorf( + "%w: context and a limit up to %d are required", ErrInvalidSelector, MaxIndexLimit, + ) + } + + return limit, nil +} + +// indexSelectors validates the index request and decodes its cursor. +func indexSelectors(req IndexRequest) (int, *cursor, error) { + limit, err := indexLimit(req) + if err != nil { + return 0, nil, err + } + cur, err := decodeCursor(req.After, cursorIndex) + if err != nil { + return 0, nil, err + } + if req.After != "" && (cur.Context != req.Context || cur.UID != req.WorkspaceUID) { + return 0, nil, fmt.Errorf( + "%w: cursor does not match the selected context or workspace", + ErrInvalidCursor, + ) + } + + return limit, cur, nil +} + +func inIndex(m *RunManifest, req IndexRequest) bool { + if m.Context != req.Context { + return false + } + + return req.WorkspaceUID == "" || (m.WorkspaceUID != nil && *m.WorkspaceUID == req.WorkspaceUID) +} + +// olderThan reports whether m sorts after the cursor position. +func olderThan(m *RunManifest, cur *cursor) bool { + if c := m.CreatedAt.Compare(cur.At); c != 0 { + return c < 0 + } + + return m.RunID < cur.ID +} + +func indexPage(matches []*RunManifest, req IndexRequest, limit int) (*IndexResponse, error) { + slices.SortFunc(matches, func(a, b *RunManifest) int { + if c := b.CreatedAt.Compare(a.CreatedAt); c != 0 { + return c + } + if a.RunID > b.RunID { + return -1 + } + if a.RunID < b.RunID { + return 1 + } + + return 0 + }) + resp := &IndexResponse{SchemaVersion: SchemaVersion, Runs: []RunSummary{}} + if len(matches) > limit { + resp.HasMore = true + matches = matches[:limit] + } + for _, m := range matches { + resp.Runs = append(resp.Runs, m.Summary()) + } + if resp.HasMore { + last := matches[len(matches)-1] + token, err := (&cursor{ + Version: SchemaVersion, Kind: cursorIndex, Context: req.Context, UID: req.WorkspaceUID, + At: last.CreatedAt, ID: last.RunID, + }).encode() + if err != nil { + return nil, err + } + resp.NextCursor = token + } + + return resp, nil +} diff --git a/pkg/workspacelogs/reader_test.go b/pkg/workspacelogs/reader_test.go new file mode 100644 index 0000000000..a56708a13a --- /dev/null +++ b/pkg/workspacelogs/reader_test.go @@ -0,0 +1,376 @@ +package workspacelogs + +import ( + "os" + "path/filepath" + "strings" + "testing" + "unicode/utf8" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// hookRun builds a run with a silent postCreate, a noisy postStart and a +// second producer, all closed. +type hookRun struct { + runID, producerID, silent, noisy string +} + +func newHookRun(t *testing.T, s *Store) hookRun { + t.Helper() + runID := newRun(t, s, "uid") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + h := hookRun{runID: runID, producerID: pid, silent: NewID(), noisy: NewID()} + + mustAppend(t, s, + seq.invocation(h.silent, hookCreate, InvocationRunning), + seq.invocation(h.silent, hookCreate, InvocationSucceeded), + seq.invocation(h.noisy, "postStartCommand", InvocationRunning), + seq.output(h.noisy, StreamStdout, "out-1\n"), + seq.output(h.noisy, StreamStderr, "err-1\n"), + seq.output(h.noisy, StreamStdout, "out-2\n"), + seq.invocation(h.noisy, "postStartCommand", InvocationFailed), + seq.terminal(true), + ) + + return h +} + +func TestReadPagesByLimitWithoutDuplicates(t *testing.T) { + s, _ := newTestStore(t) + h := newHookRun(t, s) + + first, err := s.Read(ReadRequest{RunID: h.runID, Limit: 3}) + require.NoError(t, err) + assert.Len(t, first.Records, 3) + assert.True(t, first.HasMore) + assert.False(t, first.Complete, "complete only once every page is drained") + + all := readAll(t, s, ReadRequest{RunID: h.runID, Limit: 3}) + assert.Len(t, all, 8) + seen := map[uint64]bool{} + for i := range all { + assert.False(t, seen[all[i].Sequence], "sequence %d repeated", all[i].Sequence) + seen[all[i].Sequence] = true + } + + last, err := s.Read(ReadRequest{RunID: h.runID}) + require.NoError(t, err) + assert.True(t, last.Complete) + assert.False(t, last.HasMore) +} + +func TestReadHonorsByteCap(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + inv := NewID() + for range 40 { + mustAppend(t, s, seq.output(inv, StreamStdout, strings.Repeat("z", MaxChunkBytes))) + } + + resp, err := s.Read(ReadRequest{RunID: runID, MaxBytes: 32 * 1024}) + require.NoError(t, err) + assert.True(t, resp.HasMore) + assert.LessOrEqual(t, len(resp.Records), 4) + assert.Len( + t, + texts(readAll(t, s, ReadRequest{RunID: runID, MaxBytes: 32 * 1024})), + 40*MaxChunkBytes, + ) + + _, err = s.Read(ReadRequest{RunID: runID, MaxBytes: MaxPageBytes + 1}) + require.ErrorIs(t, err, ErrInvalidSelector) + _, err = s.Read(ReadRequest{RunID: runID, Limit: MaxReadLimit + 1}) + require.ErrorIs(t, err, ErrInvalidSelector) +} + +func TestReadFiltersByHookAndInvocation(t *testing.T) { + s, _ := newTestStore(t) + h := newHookRun(t, s) + + noisy := readAll(t, s, ReadRequest{RunID: h.runID, Hook: "postStartCommand"}) + assert.Equal(t, "out-1\nerr-1\nout-2\n", texts(noisy)) + for i := range noisy { + assert.NotEqual(t, KindProducer, noisy[i].Kind) + } + + silent := readAll(t, s, ReadRequest{RunID: h.runID, Hook: hookCreate}) + assert.Empty(t, texts(silent)) + require.NotEmpty(t, silent, "a silent hook still returns its transitions") + assert.Equal(t, InvocationSucceeded, silent[len(silent)-1].State) + + byID := readAll(t, s, ReadRequest{RunID: h.runID, InvocationID: h.noisy}) + assert.Equal(t, "out-1\nerr-1\nout-2\n", texts(byID)) +} + +func TestReadRejectsBadSelectors(t *testing.T) { + s, _ := newTestStore(t) + h := newHookRun(t, s) + other := newHookRun(t, s) + + _, err := s.Read(ReadRequest{RunID: h.runID, Hook: "rm -rf"}) + require.ErrorIs(t, err, ErrInvalidSelector) + _, err = s.Read(ReadRequest{RunID: h.runID, InvocationID: "x"}) + require.ErrorIs(t, err, ErrInvalidID) + _, err = s.Read(ReadRequest{RunID: h.runID, InvocationID: other.noisy}) + require.ErrorIs(t, err, ErrNotFound, "an invocation from another run is a lookup error") + _, err = s.Read(ReadRequest{RunID: "../x"}) + require.ErrorIs(t, err, ErrInvalidID) + _, err = s.Read(ReadRequest{RunID: NewID()}) + require.ErrorIs(t, err, ErrNotFound) +} + +func TestCursorBindsRunAndFilters(t *testing.T) { + s, _ := newTestStore(t) + h := newHookRun(t, s) + other := newHookRun(t, s) + + page, err := s.Read(ReadRequest{RunID: h.runID, Limit: 2}) + require.NoError(t, err) + + _, err = s.Read(ReadRequest{RunID: other.runID, After: page.NextCursor}) + require.ErrorIs(t, err, ErrInvalidCursor) + _, err = s.Read(ReadRequest{RunID: h.runID, Hook: "postStartCommand", After: page.NextCursor}) + require.ErrorIs(t, err, ErrInvalidCursor) + _, err = s.Read(ReadRequest{RunID: h.runID, After: "garbage"}) + require.ErrorIs(t, err, ErrInvalidCursor) + _, err = s.Read(ReadRequest{RunID: h.runID, After: strings.Repeat("a", MaxCursorBytes+1)}) + require.ErrorIs(t, err, ErrInvalidCursor) + + indexCursor := indexCursorFor(t, s) + _, err = s.Read(ReadRequest{RunID: h.runID, After: indexCursor}) + require.ErrorIs(t, err, ErrInvalidCursor, "an index cursor is not a run cursor") +} + +func indexCursorFor(t *testing.T, s *Store) string { + t.Helper() + resp, err := s.ListRuns(IndexRequest{Context: testContext, Limit: 1}) + require.NoError(t, err) + require.NotEmpty(t, resp.NextCursor) + + return resp.NextCursor +} + +func TestReadReportsRetentionGap(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + inv := NewID() + // Three segments of 60 records each, then drop the middle one. + chunk := strings.Repeat("g", MaxChunkBytes) + for range 190 { + mustAppend(t, s, seq.output(inv, StreamStdout, chunk)) + } + dir, err := s.producerDir(runID, pid) + require.NoError(t, err) + segs, err := listSegments(dir) + require.NoError(t, err) + require.GreaterOrEqual(t, len(segs), 3) + require.NoError(t, os.Remove(filepath.Join(dir, segs[1].name))) + + recs := readAll(t, s, ReadRequest{RunID: runID, Limit: MaxReadLimit}) + + var gap *Record + for i := range recs { + if recs[i].Condition == ConditionRetentionGap { + gap = &recs[i] + } + } + require.NotNil(t, gap) + assert.Equal(t, pid, gap.ProducerID, "the gap names the affected producer") + assert.Contains(t, gap.Detail, "are not retained") + + finishRun(t, s, runID) + req := ReadRequest{RunID: runID, Limit: MaxReadLimit} + for { + resp, readErr := s.Read(req) + require.NoError(t, readErr) + if !resp.HasMore { + assert.False( + t, + resp.Complete, + "a gap seen on an earlier page keeps the read incomplete", + ) + + break + } + req.After = resp.NextCursor + } +} + +func TestReadSurfacesMalformedCommittedRecord(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + inv := NewID() + good1 := marshalTestLine(t, seq.output(inv, StreamStdout, "good-1\n")) + bad := []byte("not json\n") + seq.next++ + good2 := marshalTestLine(t, seq.output(inv, StreamStdout, "good-2\n")) + + // Corrupt a committed line in the middle of the segment. + require.NoError(t, s.ensureDir(runsDirName, runID, producersDir, pid)) + dir, err := s.producerDir(runID, pid) + require.NoError(t, err) + content := append(append(good1, bad...), good2...) + require.NoError(t, os.WriteFile(filepath.Join(dir, segmentName(1)), content, filePerm)) + + recs := readAll(t, s, ReadRequest{RunID: runID, Limit: 1}) + + assert.Equal(t, "good-1\ngood-2\n", texts(recs)) + var malformed int + for i := range recs { + if recs[i].Condition == ConditionMalformedRecord { + malformed++ + } + } + assert.Equal(t, 1, malformed, "reported once, not on every page") +} + +func marshalTestLine(t *testing.T, rec Record) []byte { + t.Helper() + line, err := marshalLine(rec) + require.NoError(t, err) + + return line +} + +func TestReadPageResumesAcrossProducers(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + inv := NewID() + for _, label := range []string{"a", "b"} { + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + mustAppend( + t, + s, + seq.output(inv, StreamStdout, label+"1"), + seq.output(inv, StreamStdout, label+"2"), + ) + } + + assert.Equal(t, "a1a2b1b2", texts(readAll(t, s, ReadRequest{RunID: runID, Limit: 1}))) +} + +func TestListRunsOrdersAndIsolates(t *testing.T) { + s, _ := newTestStore(t) + var ids []string + for range 5 { + ids = append(ids, newRun(t, s, uidA)) + } + otherWorkspace := newRun(t, s, "uid-b") + _, err := s.CreateRun(RunSpec{Context: "elsewhere", WorkspaceUID: uidA, Command: "up"}) + require.NoError(t, err) + + var got []string + req := IndexRequest{Context: testContext, WorkspaceUID: uidA, Limit: 2} + for { + resp, listErr := s.ListRuns(req) + require.NoError(t, listErr) + for _, r := range resp.Runs { + got = append(got, r.RunID) + } + if !resp.HasMore { + break + } + req.After = resp.NextCursor + } + + assert.Equal(t, []string{ids[4], ids[3], ids[2], ids[1], ids[0]}, got, "newest first") + assert.NotContains(t, got, otherWorkspace) + + all, err := s.ListRuns(IndexRequest{Context: testContext, Limit: MaxIndexLimit}) + require.NoError(t, err) + assert.Len(t, all.Runs, 6, "no workspace argument lists the whole context") +} + +func TestListRunsRejectsChangedSelectorsAndLimits(t *testing.T) { + s, _ := newTestStore(t) + for range 3 { + newRun(t, s, uidA) + } + page, err := s.ListRuns(IndexRequest{Context: testContext, WorkspaceUID: uidA, Limit: 1}) + require.NoError(t, err) + require.NotEmpty(t, page.NextCursor) + + _, err = s.ListRuns( + IndexRequest{Context: testContext, WorkspaceUID: "uid-b", After: page.NextCursor}, + ) + require.ErrorIs(t, err, ErrInvalidCursor) + _, err = s.ListRuns( + IndexRequest{Context: "other", WorkspaceUID: uidA, After: page.NextCursor}, + ) + require.ErrorIs(t, err, ErrInvalidCursor) + _, err = s.ListRuns(IndexRequest{Context: testContext, Limit: MaxIndexLimit + 1}) + require.ErrorIs(t, err, ErrInvalidSelector) + _, err = s.ListRuns(IndexRequest{}) + require.ErrorIs(t, err, ErrInvalidSelector) + + runCursor, err := (&cursor{Version: SchemaVersion, Kind: cursorRun, Run: NewID()}).encode() + require.NoError(t, err) + _, err = s.ListRuns(IndexRequest{Context: testContext, After: runCursor}) + require.ErrorIs(t, err, ErrInvalidCursor, "a run cursor is not an index cursor") +} + +func TestRenamedWorkspaceKeepsRunAndNewUIDDoesNot(t *testing.T) { + s, _ := newTestStore(t) + m, err := s.CreateRun(RunSpec{ + Context: testContext, WorkspaceUID: "uid-old", WorkspaceIDAtStart: "app", Command: "up", + }) + require.NoError(t, err) + + byUID, err := s.ListRuns(IndexRequest{Context: testContext, WorkspaceUID: "uid-old"}) + require.NoError(t, err) + require.Len(t, byUID.Runs, 1, "lookup is by UID, so a rename does not lose the run") + assert.Equal(t, m.RunID, byUID.Runs[0].RunID) + + reused, err := s.ListRuns(IndexRequest{Context: testContext, WorkspaceUID: "uid-new"}) + require.NoError(t, err) + assert.Empty(t, reused.Runs, "a new workspace that reuses the name does not inherit old runs") +} + +func TestSplitTextKeepsUTF8AndTrailingFragment(t *testing.T) { + text := strings.Repeat("é", 10) + "tail" + chunks := SplitText(text, 5) + + assert.Equal(t, text, strings.Join(chunks, "")) + for _, c := range chunks { + assert.LessOrEqual(t, len(c), 5) + assert.True(t, utf8.ValidString(c), "chunk %q splits a rune", c) + } + assert.Equal(t, []string{"no newline"}, SplitText("no newline", MaxChunkBytes)) + assert.Empty(t, SplitText("", MaxChunkBytes)) +} + +func TestFailedInvocationsReportsLatestState(t *testing.T) { + s, _ := newTestStore(t) + h := newHookRun(t, s) + + failed, err := s.FailedInvocations(h.runID) + require.NoError(t, err) + assert.Equal(t, []string{h.noisy}, failed, "the silent hook succeeded and is not listed") + + _, err = s.FailedInvocations(NewID()) + require.ErrorIs(t, err, ErrNotFound) +} + +func TestReadReturnsFirstRecordLargerThanByteCap(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + inv := NewID() + mustAppend(t, s, seq.output(inv, StreamStdout, strings.Repeat("<", MaxChunkBytes))) + + resp, err := s.Read(ReadRequest{RunID: runID, MaxBytes: 16 * 1024}) + require.NoError(t, err) + assert.Len(t, resp.Records, 1) + assert.False(t, resp.HasMore) +} diff --git a/pkg/workspacelogs/recorder.go b/pkg/workspacelogs/recorder.go new file mode 100644 index 0000000000..ae9d6b99f3 --- /dev/null +++ b/pkg/workspacelogs/recorder.go @@ -0,0 +1,373 @@ +package workspacelogs + +import ( + "errors" + "io" + "strings" + "sync" + "time" + "unicode/utf8" + + "github.com/devsy-org/devsy/pkg/secrets" +) + +// Backend is the persistence a Recorder writes to. *Store implements it. +type Backend interface { + RegisterProducer(runID string, ref ProducerRef) error + Append(records []Record) (AppendResult, error) + AddCondition(runID string, c Condition) error +} + +// RecorderOptions configures a Recorder. +type RecorderOptions struct { + Backend Backend + RunID string + Location Location + // ProducerID uses a producer ID its parent reserved earlier. Empty + // generates one. + ProducerID string + // Resumed marks a worker that continues a run whose launching process + // already recorded the plan. Plan-time transitions are not repeated. + Resumed bool + // ParentProducerID links this producer to the one that launched it. + ParentProducerID string + // Redactor masks known secrets before anything is persisted. Nil keeps text + // as written. + Redactor *secrets.Redactor + // Now overrides the clock in tests. + Now func() time.Time +} + +// Invocation describes one hook or build step transition. +type Invocation struct { + ID string + Phase string + Hook string + Index *int + Key string + State InvocationState + Reason string + ExitCode *int + Signal string + Duration time.Duration +} + +// Recorder writes one producer's records. It assigns the producer sequence and +// never fails the caller: the first storage error disables the recorder, is +// kept for Err, and later calls become no-ops, except that Close still makes +// one best-effort attempt, so the recorded command's own error and exit +// semantics are untouched. +type Recorder struct { + opts RecorderOptions + producerID string + + mu sync.Mutex + seq uint64 + persisted uint64 + err error + disabled bool +} + +// NewRecorder registers a new producer in the run and records that it is +// running. A failure to register returns a recorder that is already disabled +// together with the error, so callers may ignore the recorder's state. +func NewRecorder(opts RecorderOptions) (*Recorder, error) { + if opts.Now == nil { + opts.Now = time.Now + } + id := opts.ProducerID + if !ValidID(id) { + id = NewID() + } + r := &Recorder{opts: opts, producerID: id} + err := opts.Backend.RegisterProducer(opts.RunID, ProducerRef{ + ProducerID: r.producerID, + ParentProducerID: opts.ParentProducerID, + State: ProducerRunning, + Location: opts.Location, + }) + if err != nil { + r.fail(err) + + return r, err + } + r.emit(&Record{ + Kind: KindProducer, ProducerState: ProducerRunning, + ParentProducerID: opts.ParentProducerID, Location: opts.Location, + }) + + return r, nil +} + +// ProducerID returns the identity of this producer. +func (r *Recorder) ProducerID() string { return r.producerID } + +// Err returns the first storage error, if any. +func (r *Recorder) Err() error { + r.mu.Lock() + defer r.mu.Unlock() + + return r.err +} + +// Invocation records a transition of one invocation, including silent ones. +func (r *Recorder) Invocation(inv Invocation) { + if r.opts.Resumed && repeatsPlan(inv) { + return + } + rec := &Record{ + Kind: KindInvocation, + InvocationID: inv.ID, + Phase: inv.Phase, + Hook: inv.Hook, + HookIndex: inv.Index, + HookKey: truncateDetail(inv.Key), + Location: r.opts.Location, + State: inv.State, + Reason: truncateDetail(inv.Reason), + ExitCode: inv.ExitCode, + Signal: inv.Signal, + } + if inv.Duration > 0 { + ms := inv.Duration.Milliseconds() + rec.DurationMillis = &ms + } + r.emit(rec) +} + +// Capture records a condition that makes the run's capture partial. +func (r *Recorder) Capture(c Condition, detail string, droppedBytes int64) { + r.emit(&Record{ + Kind: KindCapture, + Condition: c, + Detail: truncateDetail(detail), + DroppedBytes: droppedBytes, + }) +} + +// ReserveChild records that a child producer will be launched and returns its +// ID. Pass the ID to the child as RecorderOptions.ProducerID. Reserve every +// child before sealing discovery. +func (r *Recorder) ReserveChild(location Location) string { + id := NewID() + r.emit(&Record{ + Kind: KindProducer, + ChildProducerID: id, + ProducerState: ProducerReserved, + Location: location, + }) + + return id +} + +// MarkChild records a factual outcome for a reserved child, for example +// ProducerUnavailable when its launch was not observed. Reason is shown to +// readers. +func (r *Recorder) MarkChild(id string, state ProducerState, reason string) { + r.emit(&Record{ + Kind: KindProducer, + ChildProducerID: id, + ProducerState: state, + Detail: truncateDetail(reason), + }) +} + +// SealDiscovery records that this producer has reserved every child it will +// launch. +func (r *Recorder) SealDiscovery() { + r.emit(&Record{Kind: KindProducer, DiscoverySealed: true}) +} + +// Close records the producer's final state. Close any DiagnosticWriter first. +// A disabled recorder still makes one best-effort attempt so the producer does +// not stay running in the run. +func (r *Recorder) Close(state ProducerState, sealDiscovery bool) { + r.mu.Lock() + defer r.mu.Unlock() + rec := &Record{Kind: KindProducer, ProducerState: state, DiscoverySealed: sealDiscovery} + if r.persisted == 0 { + rec.ParentProducerID = r.opts.ParentProducerID + rec.Location = r.opts.Location + } + if err := r.appendLocked(rec); err != nil && !r.disabled { + r.failLocked(err) + } +} + +// Writer returns a writer for one invocation's stream. Each writer redacts on +// its own, so a secret split across writes is masked and streams do not mix. +func (r *Recorder) Writer(invocationID string, stream Stream) *DiagnosticWriter { + return r.WriterRedacting(invocationID, stream, nil) +} + +// WriterRedacting is Writer with extra values masked on top of the recorder's +// own redactor. +func (r *Recorder) WriterRedacting( + invocationID string, + stream Stream, + extra *secrets.Redactor, +) *DiagnosticWriter { + redactor := r.opts.Redactor + if extra != nil { + redactor = secrets.Combine(redactor, extra) + } + return &DiagnosticWriter{ + rec: r, invocationID: invocationID, stream: stream, + redactor: secrets.NewStreamingRedactor(redactor), + } +} + +func (r *Recorder) emit(rec *Record) { + r.mu.Lock() + defer r.mu.Unlock() + if r.disabled { + return + } + err := r.appendLocked(rec) + if err == nil { + return + } + if rec.Kind == KindOutput && errors.Is(err, ErrCapacity) { + _ = r.opts.Backend.AddCondition(r.opts.RunID, ConditionCapacity) + + return + } + r.failLocked(err) +} + +func (r *Recorder) appendLocked(rec *Record) error { + r.seq++ + rec.SchemaVersion = SchemaVersion + rec.RunID = r.opts.RunID + rec.ProducerID = r.producerID + rec.Sequence = r.seq + rec.Time = r.opts.Now().UTC() + res, err := r.opts.Backend.Append([]Record{*rec}) + r.persisted = max(r.persisted, res.Accepted) + if err != nil { + r.seq = r.persisted + } + + return err +} + +func (r *Recorder) fail(err error) { + r.mu.Lock() + defer r.mu.Unlock() + r.failLocked(err) +} + +func (r *Recorder) failLocked(err error) { + if r.err == nil { + r.err = err + // Best effort: the store may be the thing that failed. + _ = r.opts.Backend.AddCondition(r.opts.RunID, ConditionWriteError) + } + r.disabled = true +} + +// DiagnosticWriter adapts a Recorder to io.WriteCloser for one invocation +// stream. Write always reports success so a failing store cannot change the +// behavior of the process producing the output. +type DiagnosticWriter struct { + rec *Recorder + invocationID string + stream Stream + redactor *secrets.StreamingRedactor + + mu sync.Mutex + partial []byte + closed bool +} + +var _ io.WriteCloser = (*DiagnosticWriter)(nil) + +// Write redacts p and records it. +func (w *DiagnosticWriter) Write(p []byte) (int, error) { + w.mu.Lock() + defer w.mu.Unlock() + if w.closed { + return len(p), nil + } + data := append(w.partial, p...) //nolint:gocritic // partial is replaced below + cut := completeUTF8(data) + w.partial = append([]byte(nil), data[cut:]...) + w.record(w.redactor.RedactChunk(strings.ToValidUTF8(string(data[:cut]), "�"))) + + return len(p), nil +} + +// Close flushes buffered text, including a final fragment without a newline. +func (w *DiagnosticWriter) Close() error { + w.mu.Lock() + defer w.mu.Unlock() + if w.closed { + return nil + } + w.closed = true + tail := w.redactor.RedactChunk(strings.ToValidUTF8(string(w.partial), "�")) + w.partial = nil + w.record(tail + w.redactor.Flush()) + + return nil +} + +func (w *DiagnosticWriter) record(text string) { + for _, chunk := range SplitText(text, MaxChunkBytes) { + w.rec.emit(&Record{ + Kind: KindOutput, InvocationID: w.invocationID, Stream: w.stream, + Text: chunk, Bytes: len(chunk), + }) + } +} + +// completeUTF8 returns the length of the longest prefix of data that does not +// end inside a multi-byte sequence. +func completeUTF8(data []byte) int { + for back := 1; back <= utf8.UTFMax-1 && back <= len(data); back++ { + i := len(data) - back + if utf8.RuneStart(data[i]) { + if utf8.FullRune(data[i:]) { + return len(data) + } + + return i + } + } + + return len(data) +} + +func truncateDetail(s string) string { + const maxBytes = MaxDetailBytes + if len(s) <= maxBytes { + return s + } + cut := maxBytes + for cut > 0 && !utf8.RuneStart(s[cut]) { + cut-- + } + + return s[:cut] +} + +// planSkipReasons are skip reasons decided from configuration alone, so the +// process that planned the phase has already recorded them. Reasons that +// depend on state seen by a later process (a once-marker) are not listed. +var planSkipReasons = map[string]bool{ + "no_command_configured": true, + "explicit_skip": true, + "prebuild_excluded": true, + "recovery_excluded": true, +} + +func repeatsPlan(inv Invocation) bool { + switch inv.State { + case InvocationPending, InvocationDeferred: + return true + case InvocationSkipped: + return planSkipReasons[inv.Reason] + default: + return false + } +} diff --git a/pkg/workspacelogs/recorder_test.go b/pkg/workspacelogs/recorder_test.go new file mode 100644 index 0000000000..d130d0c7cd --- /dev/null +++ b/pkg/workspacelogs/recorder_test.go @@ -0,0 +1,415 @@ +package workspacelogs + +import ( + "errors" + "strings" + "testing" + + "github.com/devsy-org/devsy/pkg/secrets" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func newTestRecorder(t *testing.T, s *Store, runID string, redactor *secrets.Redactor) *Recorder { + t.Helper() + rec, err := NewRecorder(RecorderOptions{ + Backend: s, RunID: runID, Location: LocationContainer, Redactor: redactor, + }) + require.NoError(t, err) + + return rec +} + +func TestRecorderRoundTripsInvocationOutputAndExit(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + rec := newTestRecorder(t, s, runID, nil) + inv := NewID() + exit := 7 + idx := 0 + + rec.Invocation( + Invocation{ + ID: inv, + Phase: "setup", + Hook: hookCreate, + Index: &idx, + State: InvocationRunning, + }, + ) + out, errW := rec.Writer(inv, StreamStdout), rec.Writer(inv, StreamStderr) + _, _ = out.Write([]byte("hello ")) + _, _ = errW.Write([]byte("oops\n")) + _, _ = out.Write([]byte("world")) + require.NoError(t, out.Close()) + require.NoError(t, errW.Close()) + rec.Invocation( + Invocation{ID: inv, Hook: hookCreate, State: InvocationFailed, ExitCode: &exit}, + ) + rec.Close(ProducerTerminal, true) + require.NoError(t, rec.Err()) + + recs := readAll(t, s, ReadRequest{RunID: runID, Hook: hookCreate}) + var stdout, stderr string + var final *Record + for i := range recs { + switch { + case recs[i].Kind == KindOutput && recs[i].Stream == StreamStdout: + stdout += recs[i].Text + case recs[i].Kind == KindOutput && recs[i].Stream == StreamStderr: + stderr += recs[i].Text + case recs[i].Kind == KindInvocation: + final = &recs[i] + } + } + assert.Equal(t, "hello world", stdout, "a final fragment without a newline is kept") + assert.Equal(t, "oops\n", stderr) + require.NotNil(t, final) + assert.Equal(t, InvocationFailed, final.State) + require.NotNil(t, final.ExitCode) + assert.Equal(t, 7, *final.ExitCode) + + resp, err := s.Read(ReadRequest{RunID: runID}) + require.NoError(t, err) + assert.True(t, resp.Complete) +} + +func TestRecorderRedactsSecretsSplitAcrossWrites(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + redactor := secrets.NewRedactor([]string{"API_TOKEN=hunter2-secret-value"}) + rec := newTestRecorder(t, s, runID, redactor) + inv := NewID() + + w := rec.Writer(inv, StreamStdout) + for _, part := range []string{"token=hunter2-", "secret", "-value done\n"} { + _, err := w.Write([]byte(part)) + require.NoError(t, err) + } + require.NoError(t, w.Close()) + + got := texts(readAll(t, s, ReadRequest{RunID: runID})) + assert.NotContains(t, got, "hunter2") + assert.Contains(t, got, "token=") + assert.Contains(t, got, "done") + + raw := rawRunFiles(t, s, runID) + assert.NotContains(t, raw, "hunter2", "the secret is absent from files on disk") +} + +func TestRecorderKeepsStreamsIndependent(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + redactor := secrets.NewRedactor([]string{"API_TOKEN=abcdef-secret"}) + rec := newTestRecorder(t, s, runID, redactor) + inv := NewID() + out, errW := rec.Writer(inv, StreamStdout), rec.Writer(inv, StreamStderr) + + // Halves of the secret arrive on different streams: neither stream holds + // the whole value, so neither is masked, and neither half is swallowed. + _, _ = out.Write([]byte("abcdef-")) + _, _ = errW.Write([]byte("secret")) + require.NoError(t, out.Close()) + require.NoError(t, errW.Close()) + + got := texts(readAll(t, s, ReadRequest{RunID: runID})) + assert.Contains(t, got, "abcdef-") + assert.Contains(t, got, "secret") +} + +func TestRecorderSplitsMultibyteRunesAcrossWrites(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + rec := newTestRecorder(t, s, runID, nil) + inv := NewID() + w := rec.Writer(inv, StreamStdout) + rune3 := []byte("€") + + _, _ = w.Write(append([]byte("a"), rune3[:2]...)) + _, _ = w.Write(append(append([]byte{}, rune3[2:]...), 'b')) + require.NoError(t, w.Close()) + + assert.Equal(t, "a€b", texts(readAll(t, s, ReadRequest{RunID: runID}))) +} + +func TestRecorderChunksLargeWrites(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + rec := newTestRecorder(t, s, runID, nil) + inv := NewID() + big := strings.Repeat("q", MaxChunkBytes*3+10) + + w := rec.Writer(inv, StreamStdout) + _, _ = w.Write([]byte(big)) + require.NoError(t, w.Close()) + + recs := readAll(t, s, ReadRequest{RunID: runID}) + var outputs int + for i := range recs { + if recs[i].Kind == KindOutput { + outputs++ + assert.LessOrEqual(t, len(recs[i].Text), MaxChunkBytes) + } + } + assert.Equal(t, 4, outputs) + assert.Equal(t, big, texts(recs)) +} + +type failingBackend struct { + Backend + appendErr error + appends int + conditions []Condition +} + +func (f *failingBackend) Append(recs []Record) (AppendResult, error) { + f.appends++ + if f.appends > 1 { + return AppendResult{}, f.appendErr + } + + return f.Backend.Append(recs) +} + +func (f *failingBackend) AddCondition(runID string, c Condition) error { + f.conditions = append(f.conditions, c) + + return f.Backend.AddCondition(runID, c) +} + +func TestWriterNeverFailsWhenStorageDoes(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + backend := &failingBackend{Backend: s, appendErr: errors.New("disk full")} + rec, err := NewRecorder(RecorderOptions{Backend: backend, RunID: runID, Location: LocationHost}) + require.NoError(t, err) + w := rec.Writer(NewID(), StreamStdout) + + for range 5 { + n, writeErr := w.Write([]byte("still running\n")) + require.NoError(t, writeErr, "the producing process never sees a log error") + assert.Equal(t, len("still running\n"), n) + } + require.NoError(t, w.Close()) + + require.EqualError(t, rec.Err(), "disk full", "the first error is kept") + assert.Equal(t, 2, backend.appends, "the failed sink is disabled after its first error") + assert.Equal(t, []Condition{ConditionWriteError}, backend.conditions) + m, err := s.Manifest(runID) + require.NoError(t, err) + assert.Equal(t, CapturePartial, m.CaptureState) +} + +func TestNewRecorderReportsRegistrationFailureAndStaysUsable(t *testing.T) { + s, _ := newTestStore(t) + rec, err := NewRecorder(RecorderOptions{Backend: s, RunID: NewID()}) + + require.Error(t, err) + require.NotNil(t, rec) + w := rec.Writer(NewID(), StreamStdout) + n, writeErr := w.Write([]byte("ignored")) + require.NoError(t, writeErr) + assert.Equal(t, len("ignored"), n) + rec.Invocation(Invocation{ID: NewID(), State: InvocationRunning}) + rec.Close(ProducerTerminal, true) +} + +type flakyBackend struct { + Backend + failing map[int]bool + appends int +} + +func (f *flakyBackend) Append(recs []Record) (AppendResult, error) { + f.appends++ + if f.failing[f.appends] { + return AppendResult{}, errors.New("disk full") + } + + return f.Backend.Append(recs) +} + +func TestCloseStillRecordsTerminalAfterStorageError(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + backend := &flakyBackend{Backend: s, failing: map[int]bool{2: true}} + rec, err := NewRecorder(RecorderOptions{Backend: backend, RunID: runID, Location: LocationHost}) + require.NoError(t, err) + + rec.Invocation(Invocation{ID: NewID(), Hook: hookCreate, State: InvocationRunning}) + rec.Invocation(Invocation{ID: NewID(), Hook: hookCreate, State: InvocationRunning}) + require.Error(t, rec.Err()) + rec.Close(ProducerTerminal, true) + + m, err := s.Manifest(runID) + require.NoError(t, err) + require.Len(t, m.Producers, 1) + assert.Equal(t, ProducerTerminal, m.Producers[0].State) + assert.True(t, m.DiscoverySealed) + assert.False(t, m.Active()) + recs := readAll(t, s, ReadRequest{RunID: runID}) + require.Len(t, recs, 2) + assert.Equal(t, uint64(2), recs[1].Sequence, "terminal reuses the unstored sequence") +} + +func TestRecorderTruncatesOptionalDisplayFields(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + rec := newTestRecorder(t, s, runID, nil) + + rec.Invocation(Invocation{ + ID: NewID(), Hook: hookCreate, State: InvocationSkipped, + Reason: strings.Repeat("r", MaxDetailBytes*3), + }) + + require.NoError(t, rec.Err()) + for _, r := range readAll(t, s, ReadRequest{RunID: runID}) { + assert.LessOrEqual(t, len(r.Reason), MaxDetailBytes) + } +} + +func rawRunFiles(t *testing.T, s *Store, runID string) string { + t.Helper() + dir, err := s.runDir(runID) + require.NoError(t, err) + + return readTree(t, dir) +} + +func TestReservedChildIsKnownBeforeItStarts(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + parent := newTestRecorder(t, s, runID, nil) + + childID := parent.ReserveChild(LocationContainer) + parent.SealDiscovery() + parent.Close(ProducerTerminal, false) + + m, err := s.Manifest(runID) + require.NoError(t, err) + require.Len(t, m.Producers, 2) + assert.Equal(t, childID, m.Producers[1].ProducerID) + assert.Equal(t, ProducerReserved, m.Producers[1].State) + assert.True(t, m.DiscoverySealed) + assert.False(t, m.Complete(), "a reserved child that has not run keeps the run incomplete") + + child, err := NewRecorder(RecorderOptions{ + Backend: s, RunID: runID, ProducerID: childID, ParentProducerID: parent.ProducerID(), + }) + require.NoError(t, err) + assert.Equal(t, childID, child.ProducerID()) + child.Close(ProducerTerminal, false) + + m, err = s.Manifest(runID) + require.NoError(t, err) + assert.Len(t, m.Producers, 2, "the child reuses its reserved entry") + assert.True(t, m.Complete()) +} + +func TestUnobservedChildLaunchIsUnavailableNotEmptySuccess(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + parent := newTestRecorder(t, s, runID, nil) + childID := parent.ReserveChild(LocationContainer) + + parent.MarkChild(childID, ProducerUnavailable, "launch_not_observed") + parent.SealDiscovery() + parent.Close(ProducerTerminal, false) + + resp, err := s.Read(ReadRequest{RunID: runID}) + require.NoError(t, err) + assert.False(t, resp.Complete) + assert.Equal(t, CapturePartial, resp.Run.CaptureState) + var unavailable bool + for _, a := range resp.Availability { + unavailable = unavailable || (a.ProducerID == childID && !a.Available) + } + assert.True(t, unavailable) +} + +func TestMirroredProducerIntroducesItselfWithItsFirstRecord(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + pid := NewID() + seq := newSeq(runID, pid) + first := seq.rec( + Record{Kind: KindProducer, ProducerState: ProducerRunning, Location: LocationContainer}, + ) + + _, err := s.Append([]Record{seq.output(NewID(), StreamStdout, "no intro")}) + require.ErrorIs(t, err, ErrNotFound, "output cannot introduce a producer") + + seq.next = 1 + res, err := s.Append([]Record{first}) + require.NoError(t, err) + assert.Equal(t, uint64(1), res.Accepted) + m, err := s.Manifest(runID) + require.NoError(t, err) + assert.Equal(t, LocationContainer, m.Producers[0].Location) +} + +func TestResumedWorkerDoesNotRepeatThePlan(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + rec, err := NewRecorder(RecorderOptions{Backend: s, RunID: runID, Resumed: true}) + require.NoError(t, err) + inv := NewID() + + rec.Invocation(Invocation{ID: inv, Hook: hookCreate, State: InvocationPending}) + rec.Invocation(Invocation{ID: inv, Hook: hookCreate, State: InvocationDeferred}) + rec.Invocation( + Invocation{ + ID: NewID(), + Hook: hookCreate, + State: InvocationSkipped, + Reason: "no_command_configured", + }, + ) + rec.Invocation( + Invocation{ + ID: NewID(), + Hook: hookCreate, + State: InvocationSkipped, + Reason: "already_attempted", + }, + ) + rec.Invocation(Invocation{ID: inv, Hook: hookCreate, State: InvocationRunning}) + + var states []InvocationState + for _, r := range readAll(t, s, ReadRequest{RunID: runID}) { + if r.Kind == KindInvocation { + states = append(states, r.State) + } + } + assert.Equal(t, []InvocationState{InvocationSkipped, InvocationRunning}, states, + "the marker skip and real execution are kept, the plan is not repeated") +} + +func TestNoisyHookStillRecordsItsOutcomeAfterOutputBudget(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "uid") + rec := newTestRecorder(t, s, runID, nil) + inv := NewID() + rec.Invocation( + Invocation{ID: inv, Phase: "lifecycle", Hook: hookCreate, State: InvocationRunning}, + ) + out := rec.Writer(inv, StreamStdout) + chunk := strings.Repeat("n", MaxChunkBytes-1) + "\n" + for range (MaxRunBytes/MaxChunkBytes)*2 + 64 { + _, _ = out.Write([]byte(chunk)) + } + require.NoError(t, out.Close()) + exit := 3 + rec.Invocation(Invocation{ID: inv, Hook: hookCreate, State: InvocationFailed, ExitCode: &exit}) + rec.Close(ProducerTerminal, true) + + require.NoError(t, rec.Err()) + m, err := s.Manifest(runID) + require.NoError(t, err) + assert.Contains(t, m.Conditions, ConditionCapacity) + assert.Equal(t, ProducerTerminal, m.Producers[0].State) + recs := readAll(t, s, ReadRequest{RunID: runID, Hook: hookCreate}) + last := recs[len(recs)-1] + assert.Equal(t, KindInvocation, last.Kind) + assert.Equal(t, InvocationFailed, last.State) +} diff --git a/pkg/workspacelogs/retention.go b/pkg/workspacelogs/retention.go new file mode 100644 index 0000000000..25fcef8ffd --- /dev/null +++ b/pkg/workspacelogs/retention.go @@ -0,0 +1,198 @@ +package workspacelogs + +import ( + "io/fs" + "os" + "path/filepath" + "slices" + "time" + + "github.com/gofrs/flock" +) + +// Retention bounds. They count encoded bytes on disk, not text length. +const ( + // MaxSegmentBytes is the size at which a producer rolls to a new segment. + MaxSegmentBytes = 512 * 1024 + // MaxRunBytes is the retained data per run in one physical store. + MaxRunBytes = 10 * 1024 * 1024 + // MaxGenerationBytes is the retained data per workspace generation. + MaxGenerationBytes = 50 * 1024 * 1024 + // MaxCompletedRuns is the completed runs kept per workspace generation. + MaxCompletedRuns = 20 + // RetentionAge is how long a run is kept after its last update. + RetentionAge = 30 * 24 * time.Hour + // MaxStoreBytes is the total retained data per installation. + MaxStoreBytes = 512 * 1024 * 1024 + + // metadataReserve is the part of a run's budget only metadata may use, so a + // full output budget cannot hide the terminal state of a hook. + metadataReserve = 256 * 1024 + + finalStateReserve = metadataReserve / 2 +) + +type runInfo struct { + dir string + manifest *RunManifest + bytes int64 +} + +// generation is the retention grouping: one workspace UID inside a context. +// Runs not yet bound to a UID form their own per-context group. +func (r runInfo) generation() string { + uid := "" + if r.manifest.WorkspaceUID != nil { + uid = *r.manifest.WorkspaceUID + } + + return r.manifest.Context + "\x00" + uid +} + +// Prune removes expired and surplus completed runs. It never removes a run +// with an active writer, unless the run has not been updated within +// RetentionAge (a crashed writer). Another process pruning at the same time +// makes this a no-op. +func (s *Store) Prune() error { + l := flock.New(filepath.Join(s.root, pruneLock), flock.SetPermissions(filePerm)) + locked, err := l.TryLock() + if err != nil || !locked { + return err + } + defer func() { _ = l.Unlock() }() + + runs := s.scanRuns() + runs = s.pruneExpired(runs) + runs = s.pruneGenerations(runs) + s.pruneTotal(runs) + + return nil +} + +func (s *Store) scanRuns() []runInfo { + runs := s.loadRuns() + for i := range runs { + runs[i].bytes = dirBytes(runs[i].dir) + } + + return runs +} + +// loadRuns reads every valid run manifest, newest first. Directories that are +// not run IDs or have no readable manifest are skipped. +func (s *Store) loadRuns() []runInfo { + entries, err := os.ReadDir(filepath.Join(s.root, runsDirName)) + if err != nil { + return nil + } + var runs []runInfo + for _, e := range entries { + if !e.IsDir() || !ValidID(e.Name()) { + continue + } + dir := filepath.Join(s.root, runsDirName, e.Name()) + m, readErr := s.readManifest(dir) + if readErr != nil { + continue + } + runs = append(runs, runInfo{dir: dir, manifest: m}) + } + slices.SortFunc( + runs, + func(a, b runInfo) int { return b.manifest.CreatedAt.Compare(a.manifest.CreatedAt) }, + ) + + return runs +} + +// removable reports whether the run may be deleted now. +func (s *Store) removable(r runInfo) bool { + return !r.manifest.Active() || s.expired(r) +} + +func (s *Store) expired(r runInfo) bool { + return s.now().Sub(r.manifest.UpdatedAt) > RetentionAge +} + +func (s *Store) pruneExpired(runs []runInfo) []runInfo { + return slices.DeleteFunc(runs, func(r runInfo) bool { + return s.expired(r) && s.remove(r) + }) +} + +// pruneGenerations keeps the newest completed runs of each generation within +// the count and byte caps. Runs arrive newest first. +func (s *Store) pruneGenerations(runs []runInfo) []runInfo { + count := map[string]int{} + size := map[string]int64{} + + return slices.DeleteFunc(runs, func(r runInfo) bool { + key := r.generation() + size[key] += r.bytes + if r.manifest.Active() { + return false + } + count[key]++ + + return (count[key] > MaxCompletedRuns || size[key] > MaxGenerationBytes) && s.remove(r) + }) +} + +// pruneTotal removes the oldest removable runs until the store fits. +func (s *Store) pruneTotal(runs []runInfo) { + var total int64 + for _, r := range runs { + total += r.bytes + } + for i := len(runs) - 1; i >= 0 && total > MaxStoreBytes; i-- { + if s.removable(runs[i]) && s.remove(runs[i]) { + total -= runs[i].bytes + } + } +} + +func (s *Store) remove(r runInfo) bool { + info, err := os.Lstat(r.dir) + if err != nil || !info.IsDir() { + return false + } + + if os.RemoveAll(r.dir) != nil { + return false + } + s.removeAcks(r.manifest.RunID) + + return true +} + +func (s *Store) removeAcks(runID string) { + matches, err := filepath.Glob(filepath.Join(s.root, AckDirName, runID+".*.json")) + if err != nil { + return + } + for _, m := range matches { + _ = os.Remove(m) + } +} + +// storeFull reports whether the store has reached its total byte cap, so a new +// run starts with capacity already exhausted. +func (s *Store) storeFull() bool { + return dirBytes(filepath.Join(s.root, runsDirName)) >= MaxStoreBytes +} + +func dirBytes(dir string) int64 { + var total int64 + _ = filepath.WalkDir(dir, func(_ string, d fs.DirEntry, err error) error { + if err != nil || !d.Type().IsRegular() { + return nil //nolint:nilerr // best effort: unreadable entries do not count + } + if info, infoErr := d.Info(); infoErr == nil { + total += info.Size() + } + + return nil + }) + + return total +} diff --git a/pkg/workspacelogs/retention_test.go b/pkg/workspacelogs/retention_test.go new file mode 100644 index 0000000000..1579808d38 --- /dev/null +++ b/pkg/workspacelogs/retention_test.go @@ -0,0 +1,151 @@ +package workspacelogs + +import ( + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func runExists(t *testing.T, s *Store, runID string) bool { + t.Helper() + _, err := s.Manifest(runID) + if err == nil { + return true + } + require.ErrorIs(t, err, ErrNotFound) + + return false +} + +func TestPruneKeepsNewestCompletedRunsPerGeneration(t *testing.T) { + s, _ := newTestStore(t) + var old []string + for range MaxCompletedRuns + 3 { + id := newRun(t, s, uidA) + finishRun(t, s, id) + old = append(old, id) + } + other := newRun(t, s, "uid-b") + finishRun(t, s, other) + + require.NoError(t, s.Prune()) + + for i, id := range old { + assert.Equal(t, i >= 3, runExists(t, s, id), "run %d", i) + } + assert.True(t, runExists(t, s, other), "another generation is counted separately") +} + +func TestPruneNeverRemovesActiveRuns(t *testing.T) { + s, _ := newTestStore(t) + active := newRun(t, s, uidA) + newProducer(t, s, active) + for range MaxCompletedRuns + 2 { + finishRun(t, s, newRun(t, s, uidA)) + } + + require.NoError(t, s.Prune()) + + assert.True(t, runExists(t, s, active)) +} + +func TestPruneRemovesExpiredAndStaleActiveRuns(t *testing.T) { + s, clock := newTestStore(t) + done := newRun(t, s, uidA) + finishRun(t, s, done) + crashed := newRun(t, s, uidA) + newProducer(t, s, crashed) + + clock.Advance(RetentionAge + time.Hour) + fresh := newRun(t, s, uidA) + newProducer(t, s, fresh) + require.NoError(t, s.Prune()) + + assert.False(t, runExists(t, s, done), "completed run past retention age") + assert.False( + t, + runExists(t, s, crashed), + "a writer idle past retention age is treated as crashed", + ) + assert.True(t, runExists(t, s, fresh)) +} + +func TestPruneEnforcesGenerationByteCap(t *testing.T) { + s, _ := newTestStore(t) + chunk := strings.Repeat("b", MaxChunkBytes) + var ids []string + for range 7 { + id := newRun(t, s, uidA) + pid := newProducer(t, s, id) + seq := newSeq(id, pid) + inv := NewID() + for range MaxRunBytes/MaxChunkBytes - 40 { + mustAppend(t, s, seq.output(inv, StreamStdout, chunk)) + } + mustAppend(t, s, seq.terminal(true)) + ids = append(ids, id) + } + + require.NoError(t, s.Prune()) + + var kept int + for _, id := range ids { + if runExists(t, s, id) { + kept++ + } + } + assert.Less(t, kept, len(ids)) + assert.LessOrEqual(t, dirBytes(s.root), int64(MaxGenerationBytes)) + assert.True(t, runExists(t, s, ids[len(ids)-1]), "the newest run survives") +} + +func TestOpenPrunesExpiredRuns(t *testing.T) { + s, clock := newTestStore(t) + id := newRun(t, s, uidA) + finishRun(t, s, id) + + clock.Advance(RetentionAge + time.Hour) + reopened, err := Open(Options{Root: s.root, Now: clock.Now}) + require.NoError(t, err) + + assert.False(t, runExists(t, reopened, id)) +} + +func TestPruneIgnoresForeignEntries(t *testing.T) { + s, _ := newTestStore(t) + require.NoError(t, s.ensureDir(runsDirName, "not-a-run-id")) + outside := t.TempDir() + require.NoError(t, os.Symlink(outside, s.root+"/"+runsDirName+"/"+NewID())) + + require.NoError(t, s.Prune()) + + _, err := os.Stat(outside) + require.NoError(t, err, "pruning never follows a link out of the store") +} + +func TestPruneRemovesAcknowledgementFilesOfPrunedRuns(t *testing.T) { + s, _ := newTestStore(t) + acks, err := NewAckStore(filepath.Join(s.Root(), AckDirName)) + require.NoError(t, err) + var ids []string + for range MaxCompletedRuns + 1 { + id := newRun(t, s, uidA) + finishRun(t, s, id) + require.NoError(t, acks.Advance(AckKey{RunID: id, ProducerID: NewID(), Hop: HopInbound}, 3)) + ids = append(ids, id) + } + + require.NoError(t, s.Prune()) + + left, err := filepath.Glob(filepath.Join(s.Root(), AckDirName, ids[0]+".*.json")) + require.NoError(t, err) + assert.Empty(t, left) + kept, err := filepath.Glob(filepath.Join(s.Root(), AckDirName, ids[len(ids)-1]+".*.json")) + require.NoError(t, err) + assert.Len(t, kept, 1) +} diff --git a/pkg/workspacelogs/segment.go b/pkg/workspacelogs/segment.go new file mode 100644 index 0000000000..65739550c9 --- /dev/null +++ b/pkg/workspacelogs/segment.go @@ -0,0 +1,191 @@ +package workspacelogs + +import ( + "bufio" + "bytes" + "cmp" + "encoding/json" + "errors" + "fmt" + "io" + "io/fs" + "os" + "path/filepath" + "slices" + "strconv" + "strings" +) + +const ( + segmentPrefix = "seg-" + segmentSuffix = ".jsonl" + segmentDigits = 20 +) + +type segment struct { + name string + first uint64 +} + +func segmentName(first uint64) string { + return fmt.Sprintf("%s%0*d%s", segmentPrefix, segmentDigits, first, segmentSuffix) +} + +func parseSegmentName(name string) (uint64, bool) { + if !strings.HasPrefix(name, segmentPrefix) || !strings.HasSuffix(name, segmentSuffix) { + return 0, false + } + digits := strings.TrimSuffix(strings.TrimPrefix(name, segmentPrefix), segmentSuffix) + if len(digits) != segmentDigits { + return 0, false + } + first, err := strconv.ParseUint(digits, 10, 64) + + return first, err == nil +} + +// listSegments returns the producer's segments ordered by first sequence. +func listSegments(dir string) ([]segment, error) { + entries, err := os.ReadDir(dir) + if errors.Is(err, fs.ErrNotExist) { + return nil, nil + } + if err != nil { + return nil, fmt.Errorf("list segments: %w", err) + } + var segs []segment + for _, e := range entries { + if first, ok := parseSegmentName(e.Name()); ok && e.Type().IsRegular() { + segs = append(segs, segment{name: e.Name(), first: first}) + } + } + slices.SortFunc(segs, func(a, b segment) int { return cmp.Compare(a.first, b.first) }) + + return segs, nil +} + +// scanSegment calls fn for every committed line. A malformed committed line is +// reported as a nil record. A final line without a newline is not committed +// yet and is ignored. fn returns false to stop. +func scanSegment(path string, fn func(rec *Record) bool) error { + if err := checkRegular(path); err != nil { + return err + } + f, err := os.Open(path) //nolint:gosec // path is built from validated IDs + if err != nil { + return fmt.Errorf("open segment: %w", err) + } + defer func() { _ = f.Close() }() + + r := bufio.NewReader(f) + for { + line, readErr := r.ReadBytes('\n') + if readErr != nil { + if errors.Is(readErr, io.EOF) { + return nil + } + + return fmt.Errorf("read segment: %w", readErr) + } + if !fn(decodeLine(line)) { + return nil + } + } +} + +func decodeLine(line []byte) *Record { + var rec Record + if err := json.Unmarshal(bytes.TrimSpace(line), &rec); err != nil || rec.Validate() != nil { + return nil + } + + return &rec +} + +// repairTail truncates an incomplete trailing line left by a crashed writer and +// returns the resulting size. +func repairTail(path string) (int64, error) { + if err := checkRegular(path); err != nil { + return 0, err + } + f, err := os.OpenFile(path, os.O_RDWR, filePerm) //nolint:gosec // validated path + if err != nil { + return 0, fmt.Errorf("open segment: %w", err) + } + defer func() { _ = f.Close() }() + + data, err := io.ReadAll(f) + if err != nil { + return 0, fmt.Errorf("read segment: %w", err) + } + keep := int64(bytes.LastIndexByte(data, '\n') + 1) + if keep != int64(len(data)) { + if err = f.Truncate(keep); err != nil { + return 0, fmt.Errorf("truncate partial record: %w", err) + } + } + + return keep, nil +} + +type tail struct { + name string + size int64 + lastSeq uint64 +} + +// loadTail returns the position after the producer's last committed record. +func (s *Store) loadTail(dir string) (tail, error) { + segs, err := listSegments(dir) + if err != nil || len(segs) == 0 { + return tail{}, err + } + last := segs[len(segs)-1] + path := filepath.Join(dir, last.name) + if err = checkRegular(path); err != nil { + return tail{}, err + } + info, err := os.Stat(path) + if err != nil { + return tail{}, fmt.Errorf("stat segment: %w", err) + } + // Our own last write left the file at a known-good size; skip the repair + // and rescan unless another process or a crash changed it. + if cached, ok := s.cachedTail(dir, last.name, info.Size()); ok { + return cached, nil + } + + return rescanTail(path, last) +} + +func (s *Store) cachedTail(dir, name string, size int64) (tail, bool) { + s.mu.Lock() + defer s.mu.Unlock() + cached, ok := s.tails[dir] + + return cached, ok && cached.name == name && cached.size == size +} + +// rescanTail repairs a partial final line and finds the last sequence. +func rescanTail(path string, last segment) (tail, error) { + size, err := repairTail(path) + if err != nil { + return tail{}, err + } + t := tail{name: last.name, size: size, lastSeq: last.first - 1} + err = scanSegment(path, func(rec *Record) bool { + if rec != nil { + t.lastSeq = max(t.lastSeq, rec.Sequence) + } + + return true + }) + + return t, err +} + +func (s *Store) rememberTail(dir string, t tail) { + s.mu.Lock() + s.tails[dir] = t + s.mu.Unlock() +} diff --git a/pkg/workspacelogs/selectors.go b/pkg/workspacelogs/selectors.go new file mode 100644 index 0000000000..15f3a49b3d --- /dev/null +++ b/pkg/workspacelogs/selectors.go @@ -0,0 +1,58 @@ +package workspacelogs + +import ( + "bytes" + "fmt" + "regexp" + "sync" +) + +var cursorPattern = regexp.MustCompile(`^[A-Za-z0-9_-]*$`) + +// ValidateReadSelectors checks the values a caller may pass to a reader before +// any I/O. Every side of a refresh calls it again, so a hand-built command line +// cannot smuggle a path, a flag or an oversized cursor through. +func ValidateReadSelectors(req ReadRequest) error { + if err := requireID("runId", req.RunID); err != nil { + return err + } + if err := validateFilterSelectors(req); err != nil { + return err + } + if len(req.After) > MaxCursorBytes || !cursorPattern.MatchString(req.After) { + return fmt.Errorf("%w: malformed cursor", ErrInvalidCursor) + } + if req.Limit < 0 || req.Limit > MaxReadLimit { + return fmt.Errorf("%w: limit must be between 1 and %d", ErrInvalidSelector, MaxReadLimit) + } + + return nil +} + +// BoundedBuffer keeps at most Max bytes of what is written and drops the rest. +// Writes never fail, so it is safe as a diagnostics sink. +type BoundedBuffer struct { + Max int + + mu sync.Mutex + buf bytes.Buffer +} + +// Write keeps the head of the stream. +func (b *BoundedBuffer) Write(p []byte) (int, error) { + b.mu.Lock() + defer b.mu.Unlock() + if room := b.Max - b.buf.Len(); room > 0 { + b.buf.Write(p[:min(room, len(p))]) + } + + return len(p), nil +} + +// String returns the kept bytes. +func (b *BoundedBuffer) String() string { + b.mu.Lock() + defer b.mu.Unlock() + + return b.buf.String() +} diff --git a/pkg/workspacelogs/session.go b/pkg/workspacelogs/session.go new file mode 100644 index 0000000000..4e8ade70a7 --- /dev/null +++ b/pkg/workspacelogs/session.go @@ -0,0 +1,126 @@ +package workspacelogs + +import ( + "context" + "errors" + "time" + + "github.com/devsy-org/devsy/pkg/secrets" +) + +// SessionOptions describes the run an entry point records into. +type SessionOptions struct { + // RunID joins a run allocated earlier (a detached submission or a parent + // process). Empty allocates a new run. + RunID string + // ParentProducerID links this process's producer to the one that + // launched it. + ParentProducerID string + // ProducerID uses a producer ID reserved by the parent process. + ProducerID string + // Resumed marks a worker continuing a run whose plan the parent recorded. + Resumed bool + Run RunSpec + Location Location + Redactor *secrets.Redactor +} + +// Session is one process's handle on a run: the local store, the run, and a +// recorder for this process. A nil Session is valid and does nothing, so +// storage problems never change the command being recorded. +type Session struct { + Store *Store + Recorder *Recorder + RunID string + started time.Time +} + +// StartSession opens the default store, creates or joins the run and starts a +// recorder. It returns nil when history cannot be recorded. +func StartSession(opts SessionOptions) *Session { + store, err := OpenDefault() + if err != nil { + return nil + } + + return StartSessionIn(store, opts) +} + +// StartSessionIn is StartSession against an explicit store, for processes that +// keep their spool outside the default state directory. +func StartSessionIn(store *Store, opts SessionOptions) *Session { + opts.Run.RunID = opts.RunID + m, err := store.CreateRun(opts.Run) + if err != nil { + return nil + } + rec, err := NewRecorder(RecorderOptions{ + Backend: store, + RunID: m.RunID, + Location: opts.Location, + ParentProducerID: opts.ParentProducerID, + ProducerID: opts.ProducerID, + Resumed: opts.Resumed, + Redactor: opts.Redactor, + }) + if err != nil { + if opts.ParentProducerID == "" { + _ = store.SetCommandState(m.RunID, CommandFailed) + _ = store.SealDiscovery(m.RunID) + } + + return nil + } + + return &Session{Store: store, Recorder: rec, RunID: m.RunID, started: time.Now()} +} + +// Context returns ctx carrying this session's recorder. +func (s *Session) Context(ctx context.Context) context.Context { + if s == nil { + return ctx + } + + return WithRecorder(ctx, s.Recorder) +} + +// ProducerID returns this process's producer ID, or empty without a session. +func (s *Session) ProducerID() string { + if s == nil { + return "" + } + + return s.Recorder.ProducerID() +} + +// BindWorkspace binds the resolved workspace UID to the run. +func (s *Session) BindWorkspace(uid, idAtStart string) { + if s == nil || uid == "" { + return + } + _ = s.Store.BindWorkspace(s.RunID, uid, idAtStart) +} + +// Finish records the command outcome and closes this process's producer. Pass +// sealDiscovery only from the process that schedules no further producers. That +// process alone writes the command state. +func (s *Session) Finish(err error, sealDiscovery bool) { + if s == nil { + return + } + s.Recorder.Close(ProducerTerminal, sealDiscovery) + if sealDiscovery { + _ = s.Store.SetCommandState(s.RunID, commandStateFor(err)) + } +} + +func commandStateFor(err error) CommandState { + switch { + case err == nil: + return CommandSucceeded + case errors.Is(err, context.Canceled): + return CommandCanceled + default: + return CommandFailed + } +} diff --git a/pkg/workspacelogs/session_test.go b/pkg/workspacelogs/session_test.go new file mode 100644 index 0000000000..cf8b25a1cd --- /dev/null +++ b/pkg/workspacelogs/session_test.go @@ -0,0 +1,115 @@ +package workspacelogs + +import ( + "context" + "errors" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestSessionRecordsAndFinishesRun(t *testing.T) { + s, _ := newTestStore(t) + sess := StartSessionIn(s, SessionOptions{ + Run: RunSpec{Context: testContext, Command: "up", WorkspaceUID: "u"}, + Location: LocationHost, + }) + require.NotNil(t, sess) + + inv := NewID() + w := sess.Recorder.Writer(inv, StreamStdout) + _, _ = w.Write([]byte("hi")) + require.NoError(t, w.Close()) + ctx := sess.Context(context.Background()) + assert.Same(t, sess.Recorder, RecorderFrom(ctx)) + sess.Finish(nil, true) + + m, err := s.Manifest(sess.RunID) + require.NoError(t, err) + assert.Equal(t, CommandSucceeded, m.CommandState) + assert.Equal(t, CaptureComplete, m.CaptureState) +} + +func TestSessionJoinsRunAndRecordsFailure(t *testing.T) { + s, _ := newTestStore(t) + first := StartSessionIn(s, SessionOptions{Run: RunSpec{Context: testContext, Command: "up"}}) + require.NotNil(t, first) + + joined := StartSessionIn(s, SessionOptions{ + RunID: first.RunID, ParentProducerID: first.ProducerID(), + Run: RunSpec{Context: testContext, Command: "up"}, Location: LocationContainer, + }) + require.NotNil(t, joined) + assert.Equal(t, first.RunID, joined.RunID) + joined.BindWorkspace("uid-9", "ws") + joined.Finish(errors.New("boom"), false) + first.Finish(nil, true) + + m, err := s.Manifest(first.RunID) + require.NoError(t, err) + assert.Equal( + t, CommandSucceeded, m.CommandState, "only the sealing process writes the command state", + ) + require.NotNil(t, m.WorkspaceUID) + assert.Equal(t, "uid-9", *m.WorkspaceUID) + assert.Len(t, m.Producers, 2) + assert.Equal(t, first.ProducerID(), m.Producers[1].ParentProducerID) +} + +func TestNilSessionIsInert(t *testing.T) { + var sess *Session + ctx := context.Background() + + assert.Equal(t, ctx, sess.Context(ctx)) + assert.Empty(t, sess.ProducerID()) + sess.BindWorkspace("u", "w") + sess.Finish(errors.New("x"), true) +} + +func TestFinishMapsCancellation(t *testing.T) { + s, _ := newTestStore(t) + sess := StartSessionIn(s, SessionOptions{Run: RunSpec{Context: testContext, Command: "up"}}) + require.NotNil(t, sess) + + sess.Finish(context.Canceled, true) + + m, err := s.Manifest(sess.RunID) + require.NoError(t, err) + assert.Equal(t, CommandCanceled, m.CommandState) +} + +func TestChildFinishDoesNotWriteCommandState(t *testing.T) { + s, _ := newTestStore(t) + first := StartSessionIn(s, SessionOptions{Run: RunSpec{Context: testContext, Command: "up"}}) + require.NotNil(t, first) + joined := StartSessionIn(s, SessionOptions{ + RunID: first.RunID, ParentProducerID: first.ProducerID(), + Run: RunSpec{Context: testContext, Command: "up"}, Location: LocationContainer, + }) + require.NotNil(t, joined) + + joined.Finish(errors.New("boom"), false) + + m, err := s.Manifest(first.RunID) + require.NoError(t, err) + assert.Equal(t, CommandRunning, m.CommandState) +} + +func TestStartSessionFailsAndSealsRunWhenRecorderCannotStart(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + for range MaxProducers { + newProducer(t, s, runID) + } + + sess := StartSessionIn(s, SessionOptions{ + RunID: runID, Run: RunSpec{Context: testContext, Command: "up", WorkspaceUID: "u"}, + }) + + assert.Nil(t, sess) + m, err := s.Manifest(runID) + require.NoError(t, err) + assert.Equal(t, CommandFailed, m.CommandState) + assert.True(t, m.DiscoverySealed) +} diff --git a/pkg/workspacelogs/spool.go b/pkg/workspacelogs/spool.go new file mode 100644 index 0000000000..5bace6bb80 --- /dev/null +++ b/pkg/workspacelogs/spool.go @@ -0,0 +1,187 @@ +package workspacelogs + +import ( + "errors" + "fmt" + "io" + "io/fs" + "os" + "path/filepath" + "strings" + "time" + + "github.com/devsy-org/devsy/pkg/config" +) + +// SpoolDirName is the private subdirectory of a container data directory that +// holds the container-side spool. +const SpoolDirName = "workspace-logs" + +// Availability reasons shared by the readers on both sides of a refresh. +const ( + ReasonRefreshUnsupported = "refresh_unsupported" + ReasonSourceOffline = "source_offline" + ReasonRefreshFailed = "refresh_failed" + ReasonNoSpool = "no_spool" + + // unavailableMarker prefixes the stderr line that carries a typed reason. + unavailableMarker = "devsy-setup-logs-unavailable: " + // maxMarkerBytes bounds the reason text taken from a remote stderr. + maxMarkerBytes = 256 +) + +// FormatUnavailable returns the stderr line a reader prints before exiting +// non-zero, so the caller can tell unavailable history from a command error. +func FormatUnavailable(reason string) string { + return unavailableMarker + reason + "\n" +} + +// ParseUnavailable extracts a typed reason from remote stderr. It returns +// false when stderr carries no marker. +func ParseUnavailable(stderr string) (string, bool) { + _, rest, found := strings.Cut(stderr, unavailableMarker) + if !found { + return "", false + } + reason, _, _ := strings.Cut(rest, "\n") + reason = strings.TrimSpace(reason) + if reason == "" || len(reason) > maxMarkerBytes { + return "", false + } + + return reason, true +} + +// ResolveSpool returns the root of an existing container spool. It checks the +// primary and the non-root fallback data directories in that order and never +// creates, chmods or probes anything. ErrNotFound means no spool exists. +func ResolveSpool() (string, error) { + return resolveSpoolIn([]string{config.ContainerDataDir, config.ContainerDataDirFallback}) +} + +func resolveSpoolIn(dataDirs []string) (string, error) { + var unsafeErr error + for _, dir := range dataDirs { + root, err := validateSpoolRoot(dir) + switch { + case err == nil: + return root, nil + case errors.Is(err, ErrNotFound): + default: + unsafeErr = errors.Join(unsafeErr, err) + } + } + if unsafeErr != nil { + return "", unsafeErr + } + + return "", ErrNotFound +} + +// validateSpoolRoot checks dataDir and its spool subdirectory. Only the final +// components are inspected: the parent is a fixed system directory such as +// /tmp, which some systems expose as a symlink. +func validateSpoolRoot(dataDir string) (string, error) { + if err := checkOwnedDir(dataDir); err != nil { + return "", err + } + root := filepath.Join(dataDir, SpoolDirName) + if err := checkOwnedDir(root); err != nil { + return "", err + } + + return root, nil +} + +// checkOwnedDir accepts a real directory owned by the current user that is not +// writable by group or others. +func checkOwnedDir(dir string) error { + info, err := os.Lstat(dir) + if errors.Is(err, fs.ErrNotExist) { + return ErrNotFound + } + if err != nil { + return fmt.Errorf("%w: %v", ErrUnsafePath, err) + } + if !info.IsDir() { + return fmt.Errorf("%w: %s is not a directory", ErrUnsafePath, dir) + } + if info.Mode().Perm()&0o022 != 0 { + return fmt.Errorf("%w: %s is writable by others", ErrUnsafePath, dir) + } + if !ownedByCurrentUser(info) { + return fmt.Errorf("%w: %s has another owner", ErrUnsafePath, dir) + } + + return nil +} + +// openExisting returns a Store over an existing root without creating or +// pruning anything. +func openExisting(root string) (*Store, error) { + if !filepath.IsAbs(root) { + return nil, fmt.Errorf("%w: root must be absolute", ErrUnsafePath) + } + + return &Store{root: root, now: time.Now, tails: map[string]tail{}}, nil +} + +// SpoolSelector selects one page of a container-side read. +type SpoolSelector struct { + // Root is the validated spool root from ResolveSpool. + Root string + // Context and WorkspaceUID must match the stored run. An empty UID skips + // the UID check only when the run is not bound to one. + Context string + WorkspaceUID string + Read ReadRequest +} + +// ReadSpoolPage returns one bounded page of a spooled run. It validates the +// selectors again, so a caller cannot read a run of another context or +// workspace generation. +func ReadSpoolPage(sel SpoolSelector) (*ReadResponse, error) { + if err := requireID("runId", sel.Read.RunID); err != nil { + return nil, err + } + s, err := openExisting(sel.Root) + if err != nil { + return nil, err + } + m, err := s.Manifest(sel.Read.RunID) + if err != nil { + return nil, err + } + if err = checkRunIdentity(m, sel.Context, sel.WorkspaceUID); err != nil { + return nil, err + } + + return s.Read(sel.Read) +} + +// checkRunIdentity rejects a run that belongs to another context or workspace +// generation. A run bound to a UID needs the caller to name the same UID. +func checkRunIdentity(m *RunManifest, contextName, uid string) error { + if m.Context != contextName { + return fmt.Errorf("%w: run belongs to another context", ErrIdentityConflict) + } + if m.WorkspaceUID != nil && *m.WorkspaceUID != uid { + return fmt.Errorf("%w: run belongs to another workspace", ErrIdentityConflict) + } + + return nil +} + +// DecodeReadResponse decodes one remote page, refusing input larger than +// MaxPageBytes. +func DecodeReadResponse(r io.Reader) (*ReadResponse, error) { + data, err := io.ReadAll(io.LimitReader(r, MaxPageBytes+1)) + if err != nil { + return nil, fmt.Errorf("read response: %w", err) + } + if len(data) > MaxPageBytes { + return nil, fmt.Errorf("%w: response exceeds %d bytes", ErrInvalidRecord, MaxPageBytes) + } + + return decodeReadResponse(data) +} diff --git a/pkg/workspacelogs/spool_test.go b/pkg/workspacelogs/spool_test.go new file mode 100644 index 0000000000..21fb45d780 --- /dev/null +++ b/pkg/workspacelogs/spool_test.go @@ -0,0 +1,181 @@ +package workspacelogs + +import ( + "bytes" + "os" + "path/filepath" + "runtime" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +const otherContext = "other" + +func makeDataDir(t *testing.T, perm os.FileMode) string { + t.Helper() + dir := filepath.Join(t.TempDir(), "data") + require.NoError(t, os.Mkdir(dir, perm)) + require.NoError(t, os.Chmod(dir, perm)) + + return dir +} + +func makeSpool(t *testing.T, dataDir string) string { + t.Helper() + root := filepath.Join(dataDir, SpoolDirName) + require.NoError(t, os.Mkdir(root, 0o700)) + + return root +} + +func TestResolveSpoolFindsPrimaryThenFallback(t *testing.T) { + primary := makeDataDir(t, 0o755) + fallback := makeDataDir(t, 0o755) + fallbackRoot := makeSpool(t, fallback) + + got, err := resolveSpoolIn([]string{primary, fallback}) + require.NoError(t, err) + require.Equal(t, fallbackRoot, got) + + primaryRoot := makeSpool(t, primary) + got, err = resolveSpoolIn([]string{primary, fallback}) + require.NoError(t, err) + require.Equal(t, primaryRoot, got) +} + +func TestResolveSpoolDoesNotCreateAnything(t *testing.T) { + dir := makeDataDir(t, 0o755) + + _, err := resolveSpoolIn([]string{dir, filepath.Join(dir, "missing")}) + require.ErrorIs(t, err, ErrNotFound) + + entries, readErr := os.ReadDir(dir) + require.NoError(t, readErr) + require.Empty(t, entries) +} + +func TestResolveSpoolRejectsSymlinkAndWritableByOthers(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("permission bits and symlinks are not portable here") + } + dir := makeDataDir(t, 0o755) + real := t.TempDir() + require.NoError(t, os.Symlink(real, filepath.Join(dir, SpoolDirName))) + + _, err := resolveSpoolIn([]string{dir}) + require.ErrorIs(t, err, ErrUnsafePath) + + open := makeDataDir(t, 0o777) + makeSpool(t, open) + _, err = resolveSpoolIn([]string{open}) + require.ErrorIs(t, err, ErrUnsafePath) + + linkedData := filepath.Join(t.TempDir(), "link") + require.NoError(t, os.Symlink(real, linkedData)) + _, err = resolveSpoolIn([]string{linkedData}) + require.ErrorIs(t, err, ErrUnsafePath) +} + +func TestUnavailableMarkerRoundTrip(t *testing.T) { + reason, ok := ParseUnavailable("noise\n" + FormatUnavailable(ReasonSourceOffline) + "more") + require.True(t, ok) + require.Equal(t, ReasonSourceOffline, reason) + + _, ok = ParseUnavailable("plain error") + require.False(t, ok) + _, ok = ParseUnavailable(unavailableMarker + strings.Repeat("x", 400) + "\n") + require.False(t, ok) +} + +func spoolWithRun(t *testing.T, uid string) (root, runID string) { + t.Helper() + root = filepath.Join(t.TempDir(), SpoolDirName) + s, err := Open(Options{Root: root, Now: newClock().Now}) + require.NoError(t, err) + runID = newRun(t, s, uid) + producerID := newProducer(t, s, runID) + seq := newSeq(runID, producerID) + inv := NewID() + mustAppend( + t, + s, + seq.invocation(inv, hookCreate, InvocationRunning), + seq.output( + inv, + StreamStdout, + "hello\n", + ), + seq.invocation(inv, hookCreate, InvocationSucceeded), + seq.terminal(true), + ) + + return root, runID +} + +func TestReadSpoolPageValidatesIdentity(t *testing.T) { + root, runID := spoolWithRun(t, uidA) + sel := SpoolSelector{ + Root: root, Context: testContext, WorkspaceUID: uidA, + Read: ReadRequest{RunID: runID}, + } + + resp, err := ReadSpoolPage(sel) + require.NoError(t, err) + require.Equal(t, "hello\n", texts(resp.Records)) + + for name, mutate := range map[string]func(*SpoolSelector){ + "context": func(s *SpoolSelector) { s.Context = otherContext }, + "uid": func(s *SpoolSelector) { s.WorkspaceUID = "uid-b" }, + "empty uid": func(s *SpoolSelector) { + s.WorkspaceUID = "" + }, + "bad run": func(s *SpoolSelector) { s.Read.RunID = "../escape" }, + } { + bad := sel + mutate(&bad) + _, err = ReadSpoolPage(bad) + require.Error(t, err, name) + } +} + +func TestReadSpoolPageIsReadOnly(t *testing.T) { + root, runID := spoolWithRun(t, uidA) + before := readTree(t, root) + entriesBefore := countFiles(t, root) + + _, err := ReadSpoolPage(SpoolSelector{ + Root: root, Context: testContext, WorkspaceUID: uidA, Read: ReadRequest{RunID: runID}, + }) + require.NoError(t, err) + require.Equal(t, before, readTree(t, root)) + require.Equal(t, entriesBefore, countFiles(t, root)) +} + +func countFiles(t *testing.T, dir string) int { + t.Helper() + n := 0 + require.NoError(t, filepath.WalkDir(dir, func(_ string, _ os.DirEntry, err error) error { + n++ + + return err + })) + + return n +} + +func TestDecodeReadResponseRejectsOversizedAndWrongSchema(t *testing.T) { + _, err := DecodeReadResponse(bytes.NewReader(bytes.Repeat([]byte(" "), MaxPageBytes+1))) + require.ErrorIs(t, err, ErrInvalidRecord) + + _, err = DecodeReadResponse(strings.NewReader(`{"schemaVersion":2}`)) + require.ErrorIs(t, err, ErrInvalidRecord) + + _, err = DecodeReadResponse(strings.NewReader(`not json`)) + require.ErrorIs(t, err, ErrInvalidRecord) + + resp, err := DecodeReadResponse(strings.NewReader(`{"schemaVersion":1,"records":[]}`)) + require.NoError(t, err) + require.Empty(t, resp.Records) +} diff --git a/pkg/workspacelogs/spool_unix.go b/pkg/workspacelogs/spool_unix.go new file mode 100644 index 0000000000..70d1fcdf71 --- /dev/null +++ b/pkg/workspacelogs/spool_unix.go @@ -0,0 +1,15 @@ +//go:build !windows + +package workspacelogs + +import ( + "io/fs" + "os" + "syscall" +) + +func ownedByCurrentUser(info fs.FileInfo) bool { + st, ok := info.Sys().(*syscall.Stat_t) + + return ok && int(st.Uid) == os.Getuid() +} diff --git a/pkg/workspacelogs/spool_windows.go b/pkg/workspacelogs/spool_windows.go new file mode 100644 index 0000000000..463c8af7df --- /dev/null +++ b/pkg/workspacelogs/spool_windows.go @@ -0,0 +1,9 @@ +//go:build windows + +package workspacelogs + +import "io/fs" + +// Containers are Linux, so ownership is not meaningful here. Callers still get +// the symlink and permission checks. +func ownedByCurrentUser(fs.FileInfo) bool { return true } diff --git a/pkg/workspacelogs/store.go b/pkg/workspacelogs/store.go new file mode 100644 index 0000000000..f25198e207 --- /dev/null +++ b/pkg/workspacelogs/store.go @@ -0,0 +1,454 @@ +package workspacelogs + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io/fs" + "os" + "path/filepath" + "slices" + "sync" + "time" + + "github.com/devsy-org/devsy/pkg/config" + "github.com/gofrs/flock" +) + +const ( + // AckDirName is the directory beside the runs that holds mirror acknowledgements. + AckDirName = "mirror" + runsDirName = "runs" + producersDir = "producers" + manifestName = "manifest.json" + manifestLock = "manifest.lock" + appendLock = "append.lock" + pruneLock = "prune.lock" + lockTimeout = 15 * time.Second + lockRetry = 25 * time.Millisecond + dirPerm = 0o700 + filePerm = 0o600 + maxNameBytes = 256 +) + +// Options configures a Store. +type Options struct { + // Root is the absolute storage directory. + Root string + // Now overrides the clock in tests. + Now func() time.Time +} + +// Store is a file-backed archive of workspace run output. It is safe for use +// by several goroutines and several processes sharing one Root. +type Store struct { + root string + now func() time.Time + + mu sync.Mutex + tails map[string]tail +} + +// Open creates the storage root if needed and returns a Store. It also removes +// expired runs. +func Open(opts Options) (*Store, error) { + if !filepath.IsAbs(opts.Root) { + return nil, fmt.Errorf("%w: root must be absolute", ErrUnsafePath) + } + if opts.Now == nil { + opts.Now = time.Now + } + if err := os.MkdirAll(opts.Root, dirPerm); err != nil { + return nil, fmt.Errorf("create workspace logs root: %w", err) + } + s := &Store{root: opts.Root, now: opts.Now, tails: map[string]tail{}} + _ = s.Prune() + + return s, nil +} + +// OpenDefault opens the store at the PathManager workspace logs directory. +func OpenDefault() (*Store, error) { + root, err := config.DefaultPathManager().WorkspaceLogsDir() + if err != nil { + return nil, err + } + + return Open(Options{Root: root}) +} + +// Root returns the storage directory. +func (s *Store) Root() string { return s.root } + +// CreateRun creates a run, or joins the run named by spec.RunID. Joining +// requires the stored context (and workspace UID, when both are set) to match. +func (s *Store) CreateRun(spec RunSpec) (*RunManifest, error) { + if err := validateSpec(spec); err != nil { + return nil, err + } + if spec.RunID == "" { + spec.RunID = NewID() + } + dir, err := s.runDir(spec.RunID) + if err != nil { + return nil, err + } + if err = s.ensureDir(runsDirName, spec.RunID); err != nil { + return nil, err + } + var created *RunManifest + err = withLock(filepath.Join(dir, manifestLock), func() error { + var lockErr error + created, lockErr = s.createOrJoin(dir, spec) + + return lockErr + }) + if err != nil { + return nil, err + } + + return created, nil +} + +func joinConflict(m *RunManifest, spec RunSpec) error { + if m.Context != spec.Context { + return fmt.Errorf("%w: run belongs to another context", ErrIdentityConflict) + } + if m.WorkspaceUID != nil && spec.WorkspaceUID != "" && *m.WorkspaceUID != spec.WorkspaceUID { + return fmt.Errorf("%w: run belongs to another workspace", ErrIdentityConflict) + } + + return nil +} + +func validateSpec(spec RunSpec) error { + if spec.RunID != "" { + if err := requireID("runId", spec.RunID); err != nil { + return err + } + } + for name, v := range map[string]string{ + "context": spec.Context, "workspaceUid": spec.WorkspaceUID, + "workspaceId": spec.WorkspaceIDAtStart, "command": spec.Command, "taskId": spec.TaskID, + } { + if len(v) > maxNameBytes { + return fmt.Errorf("%w: %s is too long", ErrInvalidRecord, name) + } + } + if spec.Context == "" || spec.Command == "" { + return fmt.Errorf("%w: context and command are required", ErrInvalidRecord) + } + + return nil +} + +// Manifest returns a copy of the run manifest. +func (s *Store) Manifest(runID string) (*RunManifest, error) { + dir, err := s.runDir(runID) + if err != nil { + return nil, err + } + + return s.readManifest(dir) +} + +// BindWorkspace binds the resolved workspace UID to a provisional run. Binding +// the same UID again is a no-op; a different UID is rejected. +func (s *Store) BindWorkspace(runID, uid, idAtStart string) error { + if uid == "" || len(uid) > maxNameBytes { + return fmt.Errorf("%w: workspace UID", ErrInvalidRecord) + } + + return s.update(runID, func(m *RunManifest) error { + if m.WorkspaceUID != nil { + if *m.WorkspaceUID != uid { + return ErrIdentityConflict + } + + return nil + } + m.WorkspaceUID = &uid + if idAtStart != "" && len(idAtStart) <= maxNameBytes { + m.WorkspaceIDAtStart = idAtStart + } + + return nil + }) +} + +// SetTaskID associates a background task with a run created before the task +// existed. +func (s *Store) SetTaskID(runID, taskID string) error { + if len(taskID) > maxNameBytes { + return fmt.Errorf("%w: taskId is too long", ErrInvalidRecord) + } + + return s.update(runID, func(m *RunManifest) error { + m.TaskID = taskID + + return nil + }) +} + +// SetCommandState records the originating command's outcome. +func (s *Store) SetCommandState(runID string, state CommandState) error { + return s.update(runID, func(m *RunManifest) error { + m.CommandState = state + + return nil + }) +} + +// RegisterProducer reserves a producer in the run. Registering a known +// producer is a no-op. Overflowing MaxProducers marks capture partial and +// returns ErrProducerCap; execution is not affected. +func (s *Store) RegisterProducer(runID string, ref ProducerRef) error { + if err := requireID("producerId", ref.ProducerID); err != nil { + return err + } + if ref.State == "" { + ref.State = ProducerReserved + } + var capErr error + + err := s.update(runID, func(m *RunManifest) error { + if m.producer(ref.ProducerID) >= 0 { + return nil + } + if m.DiscoverySealed { + return ErrDiscoverySealed + } + if len(m.Producers) >= MaxProducers { + m.addCondition(ConditionProducerCap) + capErr = ErrProducerCap + + return nil + } + m.Producers = append(m.Producers, ref) + if m.CaptureState == CapturePending { + m.CaptureState = CaptureRecording + } + + return nil + }) + if err != nil { + return err + } + + return capErr +} + +// SealDiscovery closes producer discovery. Call it after every scheduler has +// recorded its child reservations or exclusions. +func (s *Store) SealDiscovery(runID string) error { + return s.update(runID, func(m *RunManifest) error { + m.DiscoverySealed = true + + return nil + }) +} + +// AddCondition records a capture condition and marks capture partial. +func (s *Store) AddCondition(runID string, c Condition) error { + return s.update(runID, func(m *RunManifest) error { + m.addCondition(c) + + return nil + }) +} + +func (m *RunManifest) addCondition(c Condition) { + if !slices.Contains(m.Conditions, c) { + m.Conditions = append(m.Conditions, c) + } + m.CaptureState = CapturePartial +} + +func (m *RunManifest) producer(id string) int { + return slices.IndexFunc(m.Producers, func(p ProducerRef) bool { return p.ProducerID == id }) +} + +// refreshCapture settles a clean run to complete once nothing more can arrive. +func (m *RunManifest) refreshCapture() { + if m.CaptureState == CaptureRecording && !m.Active() { + m.CaptureState = CaptureComplete + } +} + +func (s *Store) createOrJoin(dir string, spec RunSpec) (*RunManifest, error) { + existing, err := s.readManifest(dir) + if err == nil { + return existing, joinConflict(existing, spec) + } + if !errors.Is(err, ErrNotFound) { + return nil, err + } + now := s.now().UTC() + m := &RunManifest{ + SchemaVersion: SchemaVersion, + RunID: spec.RunID, + Context: spec.Context, + WorkspaceIDAtStart: spec.WorkspaceIDAtStart, + Command: spec.Command, + TaskID: spec.TaskID, + CreatedAt: now, + UpdatedAt: now, + CommandState: CommandRunning, + CaptureState: CapturePending, + } + if spec.WorkspaceUID != "" { + m.WorkspaceUID = &spec.WorkspaceUID + } + if s.storeFull() { + m.addCondition(ConditionCapacity) + } + + return m, s.writeManifest(dir, m) +} + +func (s *Store) update(runID string, fn func(*RunManifest) error) error { + dir, err := s.runDir(runID) + if err != nil { + return err + } + + return withLock(filepath.Join(dir, manifestLock), func() error { + m, readErr := s.readManifest(dir) + if readErr != nil { + return readErr + } + if fnErr := fn(m); fnErr != nil { + return fnErr + } + m.refreshCapture() + m.UpdatedAt = s.now().UTC() + + return s.writeManifest(dir, m) + }) +} + +func (s *Store) readManifest(dir string) (*RunManifest, error) { + path := filepath.Join(dir, manifestName) + if err := checkRegular(path); err != nil { + return nil, err + } + data, err := os.ReadFile(path) //nolint:gosec // path is built from a validated run ID + if errors.Is(err, fs.ErrNotExist) { + return nil, ErrNotFound + } + if err != nil { + return nil, fmt.Errorf("read run manifest: %w", err) + } + var m RunManifest + if err = json.Unmarshal(data, &m); err != nil { + return nil, fmt.Errorf("decode run manifest: %w", err) + } + + return &m, nil +} + +func (s *Store) writeManifest(dir string, m *RunManifest) error { + data, err := json.Marshal(m) + if err != nil { + return fmt.Errorf("encode run manifest: %w", err) + } + tmp, err := os.CreateTemp(dir, "manifest-*.tmp") + if err != nil { + return fmt.Errorf("create manifest temp file: %w", err) + } + _, werr := tmp.Write(data) + cerr := tmp.Close() + if err = errors.Join(werr, cerr); err != nil { + _ = os.Remove(tmp.Name()) + + return fmt.Errorf("write run manifest: %w", err) + } + if err = os.Rename(tmp.Name(), filepath.Join(dir, manifestName)); err != nil { + _ = os.Remove(tmp.Name()) + + return fmt.Errorf("replace run manifest: %w", err) + } + + return nil +} + +func (s *Store) runDir(runID string) (string, error) { + if err := requireID("runId", runID); err != nil { + return "", err + } + + return filepath.Join(s.root, runsDirName, runID), nil +} + +func (s *Store) producerDir(runID, producerID string) (string, error) { + dir, err := s.runDir(runID) + if err != nil { + return "", err + } + if err = requireID("producerId", producerID); err != nil { + return "", err + } + + return filepath.Join(dir, producersDir, producerID), nil +} + +// ensureDir creates each component beneath the root and rejects symlinks and +// non-directories. Components are validated IDs or fixed names, never raw +// caller selectors. +func (s *Store) ensureDir(parts ...string) error { + cur := s.root + for _, part := range parts { + cur = filepath.Join(cur, part) + info, err := os.Lstat(cur) + if errors.Is(err, fs.ErrNotExist) { + if err = os.Mkdir(cur, dirPerm); err != nil && !errors.Is(err, fs.ErrExist) { + return fmt.Errorf("create %s: %w", cur, err) + } + + continue + } + if err != nil { + return fmt.Errorf("stat %s: %w", cur, err) + } + if !info.IsDir() { + return fmt.Errorf("%w: %s is not a directory", ErrUnsafePath, cur) + } + } + + return nil +} + +// checkRegular rejects a path that exists but is a symlink or not a regular +// file. A missing path is fine. +func checkRegular(path string) error { + info, err := os.Lstat(path) + if errors.Is(err, fs.ErrNotExist) { + return nil + } + if err != nil { + return fmt.Errorf("stat %s: %w", path, err) + } + if !info.Mode().IsRegular() { + return fmt.Errorf("%w: %s is not a regular file", ErrUnsafePath, path) + } + + return nil +} + +func withLock(path string, fn func() error) error { + l := flock.New(path, flock.SetPermissions(filePerm)) + ctx, cancel := context.WithTimeout(context.Background(), lockTimeout) + defer cancel() + + locked, err := l.TryLockContext(ctx, lockRetry) + if err != nil { + return fmt.Errorf("lock %s: %w", filepath.Base(path), err) + } + if !locked { + return fmt.Errorf("lock %s: timed out", filepath.Base(path)) + } + defer func() { _ = l.Unlock() }() + + return fn() +} diff --git a/pkg/workspacelogs/store_test.go b/pkg/workspacelogs/store_test.go new file mode 100644 index 0000000000..a991098c19 --- /dev/null +++ b/pkg/workspacelogs/store_test.go @@ -0,0 +1,368 @@ +package workspacelogs + +import ( + "os" + "path/filepath" + "strings" + "sync" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestOpenRejectsRelativeRoot(t *testing.T) { + _, err := Open(Options{Root: "relative/path"}) + require.ErrorIs(t, err, ErrUnsafePath) +} + +func TestCreateRunValidatesAndJoins(t *testing.T) { + s, _ := newTestStore(t) + + _, err := s.CreateRun(RunSpec{Command: "up"}) + require.ErrorIs(t, err, ErrInvalidRecord) + _, err = s.CreateRun(RunSpec{RunID: "../../etc", Context: testContext, Command: "up"}) + require.ErrorIs(t, err, ErrInvalidID) + + first, err := s.CreateRun(RunSpec{Context: testContext, Command: "up", TaskID: "t1"}) + require.NoError(t, err) + assert.Nil(t, first.WorkspaceUID) + assert.Equal(t, CapturePending, first.CaptureState) + + joined, err := s.CreateRun(RunSpec{RunID: first.RunID, Context: testContext, Command: "up"}) + require.NoError(t, err) + assert.Equal(t, first.RunID, joined.RunID) + + _, err = s.CreateRun(RunSpec{RunID: first.RunID, Context: "other", Command: "up"}) + require.ErrorIs(t, err, ErrIdentityConflict) +} + +func TestBindWorkspaceOnce(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "") + + require.NoError(t, s.BindWorkspace(runID, "uid-1", "ws")) + require.NoError(t, s.BindWorkspace(runID, "uid-1", "ws"), "same UID is idempotent") + require.ErrorIs(t, s.BindWorkspace(runID, "uid-2", "ws"), ErrIdentityConflict) + + m, err := s.Manifest(runID) + require.NoError(t, err) + require.NotNil(t, m.WorkspaceUID) + assert.Equal(t, "uid-1", *m.WorkspaceUID) + assert.Equal(t, "ws", m.WorkspaceIDAtStart) +} + +func TestAppendRequiresRegisteredProducer(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + + _, err := s.Append([]Record{newSeq(runID, NewID()).output(NewID(), StreamStdout, "x")}) + require.ErrorIs(t, err, ErrNotFound) +} + +func TestAppendDeduplicatesRetransmission(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + inv := NewID() + batch := []Record{ + seq.invocation(inv, hookCreate, InvocationRunning), + seq.output(inv, StreamStdout, "one\n"), + seq.output(inv, StreamStderr, "two\n"), + } + + first := mustAppend(t, s, batch...) + again := mustAppend(t, s, batch...) + + assert.Equal(t, 3, first.Written) + assert.Equal(t, uint64(3), first.Accepted) + assert.Equal(t, 3, again.Duplicates) + assert.Zero(t, again.Written) + assert.Equal(t, uint64(3), again.Accepted) + assert.Equal(t, "one\ntwo\n", texts(readAll(t, s, ReadRequest{RunID: runID}))) +} + +func TestAppendRejectsGapWithoutAdvancing(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + inv := NewID() + mustAppend(t, s, seq.output(inv, StreamStdout, "a")) + + seq.next = 5 + res, err := s.Append([]Record{seq.output(inv, StreamStdout, "late")}) + + require.ErrorIs(t, err, ErrSequenceGap) + assert.Equal(t, uint64(1), res.Accepted) +} + +func TestAppendRejectsInvalidBatches(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + inv := NewID() + + tests := map[string][]Record{ + "oversized chunk": {seq.output(inv, StreamStdout, strings.Repeat("a", MaxChunkBytes+1))}, + "bad utf-8": {seq.output(inv, StreamStdout, "\xff\xfe")}, + "bad stream": {seq.output(inv, Stream("tty"), "x")}, + "unknown hook": {seq.invocation(inv, "evilCommand", InvocationRunning)}, + "bad invocation": {seq.invocation("not-a-uuid", hookCreate, InvocationRunning)}, + } + for name, batch := range tests { + t.Run(name, func(t *testing.T) { + _, err := s.Append(batch) + require.Error(t, err) + }) + } + + mixed := []Record{seq.output(inv, StreamStdout, "a"), seq.output(inv, StreamStdout, "b")} + mixed[1].ProducerID = NewID() + _, err := s.Append(mixed) + require.ErrorIs(t, err, ErrInvalidRecord) +} + +func TestAppendRollsSegmentsAndReadsAcrossThem(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + inv := NewID() + chunk := strings.Repeat("x", MaxChunkBytes) + + var want int + for range 90 { + mustAppend(t, s, seq.output(inv, StreamStdout, chunk)) + want += len(chunk) + } + + dir, err := s.producerDir(runID, pid) + require.NoError(t, err) + segs, err := listSegments(dir) + require.NoError(t, err) + assert.Greater(t, len(segs), 1, "output past 512 KiB rolls to a new segment") + assert.Len(t, texts(readAll(t, s, ReadRequest{RunID: runID, Limit: 17})), want) +} + +func TestAppendRepairsPartialFinalRecord(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + inv := NewID() + mustAppend(t, s, seq.output(inv, StreamStdout, "kept\n")) + + dir, err := s.producerDir(runID, pid) + require.NoError(t, err) + segs, err := listSegments(dir) + require.NoError(t, err) + //nolint:gosec // test appends to its own temp dir + f, err := os.OpenFile(filepath.Join(dir, segs[0].name), os.O_APPEND|os.O_WRONLY, 0) + require.NoError(t, err) + _, err = f.WriteString(`{"schemaVersion":1,"kind":"out`) + require.NoError(t, err) + require.NoError(t, f.Close()) + + assert.Equal(t, "kept\n", texts(readAll(t, s, ReadRequest{RunID: runID})), + "an uncommitted final line is not shown") + + // A fresh store instance models another process taking over the producer. + other, err := Open(Options{Root: s.root}) + require.NoError(t, err) + mustAppend(t, other, seq.output(inv, StreamStdout, "after\n")) + assert.Equal(t, "kept\nafter\n", texts(readAll(t, other, ReadRequest{RunID: runID}))) +} + +func TestConcurrentStoresAppendWithoutLoss(t *testing.T) { + root := t.TempDir() + a, err := Open(Options{Root: root}) + require.NoError(t, err) + b, err := Open(Options{Root: root}) + require.NoError(t, err) + runID := newRun(t, a, "u") + inv := NewID() + + var wg sync.WaitGroup + for _, st := range []*Store{a, b} { + pid := newProducer(t, st, runID) + seq := newSeq(runID, pid) + wg.Go(func() { + for range 40 { + _, appendErr := st.Append([]Record{seq.output(inv, StreamStdout, "line\n")}) + assert.NoError(t, appendErr) + } + }) + } + wg.Wait() + + m, err := a.Manifest(runID) + require.NoError(t, err) + assert.Len(t, m.Producers, 2) + assert.Equal(t, strings.Repeat("line\n", 80), texts(readAll(t, a, ReadRequest{RunID: runID}))) +} + +func TestStorageRejectsSymlinks(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + pid := newProducer(t, s, runID) + outside := t.TempDir() + + runDir, err := s.runDir(runID) + require.NoError(t, err) + require.NoError(t, os.RemoveAll(filepath.Join(runDir, producersDir))) + require.NoError(t, os.Symlink(outside, filepath.Join(runDir, producersDir))) + + _, err = s.Append([]Record{newSeq(runID, pid).output(NewID(), StreamStdout, "x")}) + require.ErrorIs(t, err, ErrUnsafePath) + entries, err := os.ReadDir(outside) + require.NoError(t, err) + assert.Empty(t, entries, "nothing is written through the link") +} + +func TestPermissionsArePrivate(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + pid := newProducer(t, s, runID) + mustAppend(t, s, newSeq(runID, pid).output(NewID(), StreamStdout, "x")) + + if filepath.Separator == '\\' { + t.Skip("POSIX permission bits do not apply") + } + require.NoError(t, filepath.WalkDir(s.root, func(path string, d os.DirEntry, err error) error { + require.NoError(t, err) + info, infoErr := d.Info() + require.NoError(t, infoErr) + if path == s.root { + return nil + } + if d.IsDir() { + assert.Equal(t, os.FileMode(dirPerm), info.Mode().Perm(), path) + } else { + assert.Equal(t, os.FileMode(filePerm), info.Mode().Perm(), path) + } + + return nil + })) +} + +func TestRunBudgetElidesOutputButKeepsSequenceAndMetadata(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + inv := NewID() + chunk := strings.Repeat("y", MaxChunkBytes) + + var elided int + for range MaxRunBytes/MaxChunkBytes + 50 { + elided += mustAppend(t, s, seq.output(inv, StreamStdout, chunk)).Elided + } + require.Positive(t, elided, "output past the budget is elided") + + mustAppend(t, s, seq.invocation(inv, hookCreate, InvocationFailed)) + mustAppend(t, s, seq.terminal(true)) + + m, err := s.Manifest(runID) + require.NoError(t, err) + assert.Equal(t, CapturePartial, m.CaptureState) + assert.Contains(t, m.Conditions, ConditionCapacity) + assert.LessOrEqual(t, dirBytes(filepath.Join(s.root, runsDirName)), int64(MaxRunBytes)) + + recs := readAll(t, s, ReadRequest{RunID: runID, Hook: hookCreate, Limit: MaxReadLimit}) + var failed bool + for i := range recs { + failed = failed || (recs[i].Kind == KindInvocation && recs[i].State == InvocationFailed) + } + assert.True(t, failed, "the terminal state survives a full output budget") +} + +func TestCompleteNeedsSealedTerminalAndCleanCapture(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + + resp, err := s.Read(ReadRequest{RunID: runID}) + require.NoError(t, err) + assert.False(t, resp.Complete, "a running producer is not complete") + + mustAppend(t, s, seq.terminal(false)) + resp, err = s.Read(ReadRequest{RunID: runID}) + require.NoError(t, err) + assert.False(t, resp.Complete, "unsealed discovery is not complete") + + require.NoError(t, s.SealDiscovery(runID)) + resp, err = s.Read(ReadRequest{RunID: runID}) + require.NoError(t, err) + assert.True(t, resp.Complete) + assert.Equal(t, CaptureComplete, resp.Run.CaptureState) + + require.ErrorIs( + t, + s.RegisterProducer(runID, ProducerRef{ProducerID: NewID()}), + ErrDiscoverySealed, + ) +} + +func TestProducerCapMarksCapturePartial(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + for range MaxProducers { + newProducer(t, s, runID) + } + + err := s.RegisterProducer(runID, ProducerRef{ProducerID: NewID()}) + + require.ErrorIs(t, err, ErrProducerCap) + m, err := s.Manifest(runID) + require.NoError(t, err) + assert.Len(t, m.Producers, MaxProducers) + assert.Equal(t, CapturePartial, m.CaptureState) +} + +func TestProducerStateNeverRegresses(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + pid := newProducer(t, s, runID) + seq := newSeq(runID, pid) + + mustAppend(t, s, seq.terminal(false)) + mustAppend(t, s, seq.rec(Record{Kind: KindProducer, ProducerState: ProducerRunning})) + + m, err := s.Manifest(runID) + require.NoError(t, err) + assert.Equal(t, ProducerTerminal, m.Producers[0].State) +} + +func TestOnlyParentMovesItsChild(t *testing.T) { + s, _ := newTestStore(t) + runID := newRun(t, s, "u") + parent := newProducer(t, s, runID) + stranger := newProducer(t, s, runID) + childID := NewID() + parentSeq := newSeq(runID, parent) + strangerSeq := newSeq(runID, stranger) + reserve := func(seq *producerSeq, state ProducerState) Record { + return seq.rec(Record{ + Kind: KindProducer, ChildProducerID: childID, ProducerState: state, + Location: LocationContainer, + }) + } + + mustAppend(t, s, reserve(parentSeq, ProducerReserved)) + mustAppend(t, s, reserve(strangerSeq, ProducerUnavailable)) + + m, err := s.Manifest(runID) + require.NoError(t, err) + child := m.Producers[m.producer(childID)] + assert.Equal(t, parent, child.ParentProducerID) + assert.Equal(t, ProducerReserved, child.State) + + mustAppend(t, s, reserve(parentSeq, ProducerUnavailable)) + m, err = s.Manifest(runID) + require.NoError(t, err) + assert.Equal(t, ProducerUnavailable, m.Producers[m.producer(childID)].State) +} diff --git a/pkg/workspacelogs/types.go b/pkg/workspacelogs/types.go new file mode 100644 index 0000000000..ba95adc472 --- /dev/null +++ b/pkg/workspacelogs/types.go @@ -0,0 +1,463 @@ +// Package workspacelogs retains bounded, redacted build and lifecycle output +// for workspace runs. It owns the on-disk record format, per-run manifests, +// append and read paths, retention, and typed cursors. It does not execute +// hooks or own a background service. +package workspacelogs + +import ( + "errors" + "fmt" + "regexp" + "strings" + "time" + "unicode/utf8" + + "github.com/google/uuid" +) + +// SchemaVersion is the version written to every manifest, record and response. +const SchemaVersion = 1 + +// Kind identifies the shape of a Record. +type Kind string + +// Record kinds. +const ( + KindProducer Kind = "producer" + KindInvocation Kind = "invocation" + KindOutput Kind = "output" + KindCapture Kind = "capture" +) + +// Stream names the origin of output text. +type Stream string + +// Output streams. StreamCombined is only for upstream producers that already +// merge stdout and stderr. +const ( + StreamStdout Stream = "stdout" + StreamStderr Stream = "stderr" + StreamCombined Stream = "combined" +) + +// InvocationState is the lifecycle state of one hook or build invocation. +type InvocationState string + +// Invocation states. +const ( + InvocationPending InvocationState = "pending" + InvocationDeferred InvocationState = "deferred" + InvocationRunning InvocationState = "running" + InvocationSucceeded InvocationState = "succeeded" + InvocationFailed InvocationState = "failed" + InvocationSkipped InvocationState = "skipped" + InvocationCanceled InvocationState = "canceled" + InvocationUnknown InvocationState = "completion_unknown" +) + +// ProducerState is the lifecycle state of one producer (a process or worker). +type ProducerState string + +// Producer states. +const ( + ProducerReserved ProducerState = "reserved" + ProducerRunning ProducerState = "running" + ProducerTerminal ProducerState = "terminal" + ProducerUnavailable ProducerState = "unavailable" +) + +// CommandState is the outcome of the command that started a run. It is +// separate from hook and capture completion. +type CommandState string + +// Command states. +const ( + CommandPending CommandState = "pending" + CommandRunning CommandState = "running" + CommandSucceeded CommandState = "succeeded" + CommandFailed CommandState = "failed" + CommandCanceled CommandState = "canceled" + CommandUnknown CommandState = "unknown" +) + +// CaptureState describes how much of a run's output is retained. +type CaptureState string + +// Capture states. +const ( + CapturePending CaptureState = "pending" + CaptureRecording CaptureState = "recording" + CaptureComplete CaptureState = "complete" + CapturePartial CaptureState = "partial" + CaptureUnavailable CaptureState = "unavailable" +) + +// Condition explains why capture is not complete. It never replaces the +// command outcome. +type Condition string + +// Capture conditions. +const ( + ConditionTruncated Condition = "truncated" + ConditionRetentionGap Condition = "retention_gap" + ConditionWriteError Condition = "write_error" + ConditionUnsupportedSource Condition = "unsupported_source" + ConditionSourceUnavailable Condition = "source_unavailable" + ConditionInterrupted Condition = "interrupted" + ConditionCapacity Condition = "capacity" + ConditionProducerCap Condition = "producer_cap" + ConditionMalformedRecord Condition = "malformed_record" +) + +// Location is where an invocation executed. +type Location string + +// Execution locations. +const ( + LocationHost Location = "host" + LocationProviderHost Location = "provider_host" + LocationContainer Location = "container" +) + +// Size bounds. The values are defaults for the first delivery. +const ( + // MaxChunkBytes is the largest output text in one record. + MaxChunkBytes = 8 * 1024 + // MaxPageBytes is the largest encoded read or transport page. + MaxPageBytes = 256 * 1024 + // MaxMetadataBytes is the largest encoded non-output record. + MaxMetadataBytes = 64 * 1024 + // MaxProducers is the most producers a run records. + MaxProducers = 64 + // MaxCursorBytes is the largest encoded cursor. + MaxCursorBytes = 8 * 1024 + // MaxDetailBytes truncates optional free-text fields. + MaxDetailBytes = 1024 +) + +// Errors returned by the package. +var ( + ErrInvalidID = errors.New("invalid identifier") + ErrInvalidRecord = errors.New("invalid record") + ErrNotFound = errors.New("not found") + ErrIdentityConflict = errors.New("workspace identity already bound") + ErrSequenceGap = errors.New("sequence gap") + ErrInvalidCursor = errors.New("invalid cursor") + ErrInvalidSelector = errors.New("invalid selector") + ErrUnsafePath = errors.New("unsafe storage path") + ErrCapacity = errors.New("storage capacity exhausted") + ErrProducerCap = errors.New("producer limit reached") + ErrDiscoverySealed = errors.New("producer discovery is sealed") +) + +// Record is one immutable entry in a producer's stream. Fields outside the +// record's Kind are omitted. +type Record struct { + SchemaVersion int `json:"schemaVersion"` + Kind Kind `json:"kind"` + RunID string `json:"runId"` + ProducerID string `json:"producerId"` + Sequence uint64 `json:"sequence"` + Time time.Time `json:"time"` + + // Producer and invocation transitions. + ParentProducerID string `json:"parentProducerId,omitempty"` + // ChildProducerID, on a producer record, reserves (or finalizes) a child + // producer the writer is about to launch. Mirrored stores learn about the + // child from this record, before any of its own records arrive. + ChildProducerID string `json:"childProducerId,omitempty"` + ProducerState ProducerState `json:"producerState,omitempty"` + DiscoverySealed bool `json:"discoverySealed,omitempty"` + InvocationID string `json:"invocationId,omitempty"` + Phase string `json:"phase,omitempty"` + Hook string `json:"hook,omitempty"` + HookIndex *int `json:"hookIndex,omitempty"` + HookKey string `json:"hookKey,omitempty"` + Location Location `json:"location,omitempty"` + State InvocationState `json:"state,omitempty"` + Reason string `json:"reason,omitempty"` + DurationMillis *int64 `json:"durationMillis,omitempty"` + ExitCode *int `json:"exitCode,omitempty"` + Signal string `json:"signal,omitempty"` + + // Output. + Stream Stream `json:"stream,omitempty"` + Text string `json:"text,omitempty"` + Bytes int `json:"bytes,omitempty"` + // Elided marks output whose text was dropped for capacity. Sequence and + // Bytes are kept so producers and readers stay contiguous. + Elided bool `json:"elided,omitempty"` + + // Capture conditions. + Condition Condition `json:"condition,omitempty"` + Detail string `json:"detail,omitempty"` + DroppedBytes int64 `json:"droppedBytes,omitempty"` +} + +var uuidPattern = regexp.MustCompile( + `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, +) + +// NewID returns a new random lower-case UUID for runs, producers and +// invocations. +func NewID() string { return uuid.NewString() } + +// ValidID reports whether id is a canonical lower-case UUID. Every identifier +// that reaches the filesystem passes this check first. +func ValidID(id string) bool { return uuidPattern.MatchString(id) } + +func requireID(name, id string) error { + if !ValidID(id) { + return fmt.Errorf("%w: %s", ErrInvalidID, name) + } + + return nil +} + +var validHooks = map[string]struct{}{ + "initializeCommand": {}, + "onCreateCommand": {}, + "updateContentCommand": {}, + "postCreateCommand": {}, + "postStartCommand": {}, + "postAttachCommand": {}, +} + +// ValidHook reports whether name is an exact lifecycle property name. +func ValidHook(name string) bool { + _, ok := validHooks[name] + + return ok +} + +// Validate checks structural bounds that do not depend on stored state. +func (r *Record) Validate() error { + if r.SchemaVersion != SchemaVersion { + return fmt.Errorf("%w: schema version %d", ErrInvalidRecord, r.SchemaVersion) + } + if err := requireID("runId", r.RunID); err != nil { + return err + } + if err := requireID("producerId", r.ProducerID); err != nil { + return err + } + if r.Sequence == 0 { + return fmt.Errorf("%w: sequence must start at 1", ErrInvalidRecord) + } + + return r.validateKind() +} + +func (r *Record) validateKind() error { + switch r.Kind { + case KindOutput: + return r.validateOutput() + case KindInvocation: + return r.validateInvocation() + case KindProducer: + return r.validateProducer() + case KindCapture: + return r.validateCapture() + default: + return fmt.Errorf("%w: kind %q", ErrInvalidRecord, r.Kind) + } +} + +func (r *Record) validateProducer() error { + switch r.ProducerState { + case "", ProducerReserved, ProducerRunning, ProducerTerminal, ProducerUnavailable: + default: + return fmt.Errorf("%w: producer state %q", ErrInvalidRecord, r.ProducerState) + } + if r.ChildProducerID != "" { + if err := requireID("childProducerId", r.ChildProducerID); err != nil { + return err + } + } + + return validLocation(r.Location) +} + +func (r *Record) validateCapture() error { + switch r.Condition { + case ConditionTruncated, ConditionRetentionGap, ConditionWriteError, + ConditionUnsupportedSource, ConditionSourceUnavailable, ConditionInterrupted, + ConditionCapacity, ConditionProducerCap, ConditionMalformedRecord: + return validLocation(r.Location) + default: + return fmt.Errorf("%w: condition %q", ErrInvalidRecord, r.Condition) + } +} + +func validLocation(l Location) error { + switch l { + case "", LocationHost, LocationProviderHost, LocationContainer: + return nil + default: + return fmt.Errorf("%w: location %q", ErrInvalidRecord, l) + } +} + +func (r *Record) validateOutput() error { + if err := requireID("invocationId", r.InvocationID); err != nil { + return err + } + switch r.Stream { + case StreamStdout, StreamStderr, StreamCombined: + default: + return fmt.Errorf("%w: stream %q", ErrInvalidRecord, r.Stream) + } + if len(r.Text) > MaxChunkBytes { + return fmt.Errorf("%w: output chunk exceeds %d bytes", ErrInvalidRecord, MaxChunkBytes) + } + if !utf8.ValidString(r.Text) { + return fmt.Errorf("%w: output is not valid UTF-8", ErrInvalidRecord) + } + + return nil +} + +func (r *Record) validateInvocation() error { + if err := requireID("invocationId", r.InvocationID); err != nil { + return err + } + if r.Hook != "" && !ValidHook(r.Hook) { + return fmt.Errorf("%w: hook %q", ErrInvalidRecord, r.Hook) + } + switch r.State { + case InvocationPending, InvocationDeferred, InvocationRunning, InvocationSucceeded, + InvocationFailed, InvocationSkipped, InvocationCanceled, InvocationUnknown: + return nil + default: + return fmt.Errorf("%w: invocation state %q", ErrInvalidRecord, r.State) + } +} + +// SplitText splits text into chunks of at most max bytes without breaking a +// UTF-8 sequence. A trailing fragment without a newline is kept. +func SplitText(text string, maxBytes int) []string { + if maxBytes <= 0 { + maxBytes = MaxChunkBytes + } + var chunks []string + for len(text) > maxBytes { + cut := maxBytes + for cut > 0 && !utf8.RuneStart(text[cut]) { + cut-- + } + if cut == 0 { + cut = maxBytes + } + chunks = append(chunks, text[:cut]) + text = text[cut:] + } + if text != "" { + chunks = append(chunks, text) + } + + return chunks +} + +// ProducerRef is a run manifest entry for one producer. +type ProducerRef struct { + ProducerID string `json:"producerId"` + ParentProducerID string `json:"parentProducerId,omitempty"` + State ProducerState `json:"state"` + Location Location `json:"location,omitempty"` + Sealed bool `json:"discoverySealed,omitempty"` +} + +// RunManifest is the per-run metadata file. +type RunManifest struct { + SchemaVersion int `json:"schemaVersion"` + RunID string `json:"runId"` + Context string `json:"context"` + WorkspaceUID *string `json:"workspaceUid"` + WorkspaceIDAtStart string `json:"workspaceIdAtStart,omitempty"` + Command string `json:"command"` + TaskID string `json:"taskId,omitempty"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` + CommandState CommandState `json:"commandState"` + CaptureState CaptureState `json:"captureState"` + Conditions []Condition `json:"conditions,omitempty"` + Producers []ProducerRef `json:"producers,omitempty"` + DiscoverySealed bool `json:"discoverySealed,omitempty"` +} + +// RunSpec describes a run at creation. WorkspaceUID may stay empty until the +// workspace is resolved and bound. +type RunSpec struct { + // RunID joins an existing run created by another process. Empty allocates + // a new run. + RunID string + Context string + WorkspaceUID string + WorkspaceIDAtStart string + Command string + TaskID string +} + +// RunSummary is the bounded run description returned by readers. +type RunSummary struct { + RunID string `json:"runId"` + Context string `json:"context"` + WorkspaceUID *string `json:"workspaceUid"` + WorkspaceIDAtStart string `json:"workspaceIdAtStart,omitempty"` + Command string `json:"command"` + TaskID string `json:"taskId,omitempty"` + CreatedAt time.Time `json:"createdAt"` + CommandState CommandState `json:"commandState"` + CaptureState CaptureState `json:"captureState"` + Conditions []Condition `json:"conditions,omitempty"` +} + +// Summary returns the bounded view of m. +func (m *RunManifest) Summary() RunSummary { + return RunSummary{ + RunID: m.RunID, + Context: m.Context, + WorkspaceUID: m.WorkspaceUID, + WorkspaceIDAtStart: m.WorkspaceIDAtStart, + Command: m.Command, + TaskID: m.TaskID, + CreatedAt: m.CreatedAt, + CommandState: m.CommandState, + CaptureState: m.CaptureState, + Conditions: m.Conditions, + } +} + +// Complete reports whether every selected record has been captured: discovery +// is sealed, every producer is terminal and capture has no gaps. +func (m *RunManifest) Complete() bool { + if !m.DiscoverySealed || m.CaptureState != CaptureComplete { + return false + } + for _, p := range m.Producers { + if p.State != ProducerTerminal { + return false + } + } + + return true +} + +// Active reports whether a writer may still append to the run: discovery is +// open or a producer has not reached a final state. +func (m *RunManifest) Active() bool { + if !m.DiscoverySealed { + return true + } + for _, p := range m.Producers { + if p.State == ProducerReserved || p.State == ProducerRunning { + return true + } + } + + return false +} + +func DeterministicID(parts ...string) string { + return uuid.NewSHA1(uuid.NameSpaceOID, []byte(strings.Join(parts, "\x00"))).String() +} diff --git a/podman_ci b/podman_ci new file mode 100755 index 0000000000..7dac79fbe9 Binary files /dev/null and b/podman_ci differ diff --git a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/continuous-integration.mdx b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/continuous-integration.mdx index 17c7ce8f8a..9866f9c574 100644 --- a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/continuous-integration.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/continuous-integration.mdx @@ -86,6 +86,17 @@ workspace command's success result JSON off stderr. Use `--result-format=json` when the caller needs the structured result; child stdout and stderr remain separate from Devsy diagnostics. +## Setup history after cleanup + +`devsy ci` deletes the workspace, but the history of its build and lifecycle hooks stays in the local store. Read it by run ID after the job ends, or from an artifact step that runs on the same machine: + +```sh +devsy workspace logs --source setup --list +devsy workspace logs --source setup --run RUN_ID --context CONTEXT --result-format=plain +``` + +Retention is bounded. See [Setup history](../troubleshooting/setup-history.mdx). The history covers the build and the lifecycle hooks. The stdout and stderr of your own command after `--` stay on their normal streams and are not archived. Hooks still running when the command ends are not waited for. + ## Pre-building and pushing images To pre-build and publish a devcontainer image for reuse as a build cache, use `devsy workspace build`: diff --git a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/create-a-workspace.mdx b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/create-a-workspace.mdx index a93de3ea05..2dc3fac296 100644 --- a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/create-a-workspace.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/create-a-workspace.mdx @@ -93,6 +93,23 @@ Only the `docker` provider supports this, and `--recreate` is rejected for these To change where Devsy keeps its data, pass `--home=PATH` or set `DEVSY_HOME`. This helps on WSL when a workspace tries to mount a Windows path instead of a path inside WSL. +## Read setup output + +Devsy keeps the output of the build and of each lifecycle hook, such as `postCreateCommand`, after `devsy workspace up` finishes. Find the run and read it: + +```sh +devsy workspace logs WORKSPACE_NAME --source setup --list +devsy workspace logs WORKSPACE_NAME --source setup --run RUN_ID --result-format=plain +``` + +The run ID is in the result of `devsy workspace up` and in the output of a detached creation task. `--list` shows it for earlier runs. + +- Hooks after `waitFor` (the default is `updateContentCommand`) can run after `up` returns. Their output is added to the same run. Add `--follow` to watch it. +- A hook skipped because its marker shows an earlier attempt is listed as skipped with the reason `already_attempted`. That does not mean the earlier attempt succeeded. Check the earlier run. +- `postAttachCommand` runs on each attach and is recorded as its own invocation each time. + +See [Troubleshooting](../troubleshooting/troubleshooting.mdx#find-the-right-log) for all options and [Setup history](../troubleshooting/setup-history.mdx) for limits and redaction. + ## .devsyignore Put a `.devsyignore` file in the root of a local workspace folder to leave files out of the upload when the provider streams the folder, as Kubernetes does. It uses [Docker ignore syntax](https://docs.docker.com/build/concepts/context/#dockerignore-files). Paths are relative to the folder, and the last matching rule wins. diff --git a/sites/docs-devsy-sh/content/docs/troubleshooting/meta.json b/sites/docs-devsy-sh/content/docs/troubleshooting/meta.json index dd359cb0db..cf9df68930 100644 --- a/sites/docs-devsy-sh/content/docs/troubleshooting/meta.json +++ b/sites/docs-devsy-sh/content/docs/troubleshooting/meta.json @@ -1,4 +1,4 @@ { "title": "Troubleshooting", - "pages": ["troubleshooting", "linux-troubleshooting"] + "pages": ["troubleshooting", "setup-history", "linux-troubleshooting"] } diff --git a/sites/docs-devsy-sh/content/docs/troubleshooting/setup-history.mdx b/sites/docs-devsy-sh/content/docs/troubleshooting/setup-history.mdx new file mode 100644 index 0000000000..321fb8e4d5 --- /dev/null +++ b/sites/docs-devsy-sh/content/docs/troubleshooting/setup-history.mdx @@ -0,0 +1,78 @@ +--- +title: Setup history +sidebar_label: Setup history +--- + +Devsy keeps bounded, redacted build and lifecycle output for workspace runs in a local store. Read it with `devsy workspace logs --source setup` or in the **Logs** tab of Devsy Desktop. This page covers what is kept, for how long, and when it is unavailable. For the commands, see [Troubleshooting](./troubleshooting.mdx#find-the-right-log). + +History covers runs that start after you install a release with this feature. It cannot recover output from earlier runs. + +## Retention and limits + +These are the defaults of this release. + +| Bound | Value | +| --- | --- | +| Output text per chunk | 8 KiB | +| Encoded read page | 256 KiB | +| One metadata record | 64 KiB | +| Producers per run | 64 | +| Encoded cursor | 8 KiB | +| Segment | 512 KiB | +| Retained data per run, per store | 10 MiB | +| Retained data per workspace generation, per store | 50 MiB | +| Completed runs per workspace generation | 20 | +| Retention age | 30 days | +| Total per store | 512 MiB | + +Devsy removes completed runs first when a bound is reached. It never removes the segment an active run is writing. + +When a run reaches its budget, Devsy stops storing more output for that capture but keeps reading the command's output so the command is not blocked. The run is marked truncated, and the read response reports a capture gap with the affected producer and missing sequence range. Output is partial in that case. + +A full budget never changes how a hook runs or its exit status. A hook that fails still reports its own exit code, and one that succeeds still succeeds. + +## Redaction + +Devsy redacts output before it is written to disk and before it is sent between machines. + +Masked: + +- Known secret environment values. +- Runtime and mount secrets. +- Build secrets. +- Credential patterns that Devsy already detects. + +Not stored: + +- Environment snapshots. +- Argument dumps of commands. +- Resolved configuration that contains secrets. +- Contents of secret files. + +Devsy cannot guarantee that it detects arbitrary values a script invents or transforms. A script that prints a decoded or reformatted secret can leak it into history. Do not print secrets in lifecycle commands. + +## Availability + +Each source of history reports whether it is available. An unavailable source is a typed status in the response, not a command failure. + +- A workspace whose agent is older than this feature, or whose runtime does not support read-only access, shows cached history only. Run `devsy workspace logs` without `--source setup` for container logs. +- History is stored on the machine where the run happened. Devsy does not share it between machines. +- Output is first written next to the process that produced it, then copied to the machine you ran the command on. If the remote copy is gone, for example because the container was destroyed, Devsy reports the records that were never copied as unavailable. If the local copy is also gone, nothing can be recovered. +- A response with `complete=false` and an availability reason is a successful read of partial history. + +To fetch missing remote records, Devsy makes one read-only attempt with a five-second limit. It does not start, unpause, rebuild or install anything. If the workspace is stopped, you get the cached records and an availability note. + +## Desktop + +Open the workspace and choose **Logs**. + +- The run selector lists retained runs. It opens the latest run first and keeps an older run you chose when a new operation starts. +- The step list shows each build and lifecycle step with its command, time, outcome and capture condition. +- Each step is in one of these states: `pending`, `deferred`, `running`, `succeeded`, `failed`, `skipped`, `canceled` or `completion unknown`. The recorded reason and exit code appear when known. A step that succeeded with zero bytes of output is shown differently from one whose output was never captured. +- The workspace being Running and a hook having succeeded are separate facts. +- **View logs** on a failure notification, the workspace operation view or the creation wizard opens the failed run, and the failing step when it can be identified. Successful steps offer **View output**. +- Output is shown as literal text with whitespace, blank lines and the stdout or stderr label preserved. JSON-looking or HTML-looking output is not interpreted. Scroll up to pause auto-scroll. +- A link to a known run still works after a failed creation or a deletion removed the workspace. That view is read-only. +- Use the export action to save a run. + +**Desktop diagnostic files** are listed below the history. They are Desktop's own logs. The diagnostic log settings control them only. They do not turn off retained lifecycle output. diff --git a/sites/docs-devsy-sh/content/docs/troubleshooting/troubleshooting.mdx b/sites/docs-devsy-sh/content/docs/troubleshooting/troubleshooting.mdx index 37970b1024..7e7f3ca977 100644 --- a/sites/docs-devsy-sh/content/docs/troubleshooting/troubleshooting.mdx +++ b/sites/docs-devsy-sh/content/docs/troubleshooting/troubleshooting.mdx @@ -45,6 +45,68 @@ Pass `--log-output json` to get errors as JSON with a stable `code` and a `messa Run with `--debug` to see the full error chain. +### Find the right log + +Devsy keeps four kinds of logs. Pick by the question you have. + +| Question | Source | Command | +| --- | --- | --- | +| What is the container or runtime printing now? | Container and runtime logs | `devsy workspace logs WORKSPACE_NAME` | +| What did the build and each lifecycle hook print, and how did each end? | Retained setup history | `devsy workspace logs WORKSPACE_NAME --source setup` | +| What state changes did the workspace go through? | Status events | `devsy workspace events` | +| What did a background task print? | Task logs | `devsy workspace task logs` | + +Reading logs never reruns lifecycle commands. It never starts, unpauses, rebuilds or installs anything, and it does not need a running workspace to show history that is already stored locally. + +### Read setup history + +List retained runs in the selected context, newest first. This works for removed workspaces too: + +```sh +devsy workspace logs my-workspace --source setup --list +``` + +Read the latest run, including output of hooks that succeeded: + +```sh +devsy workspace logs my-workspace --source setup --result-format=plain +``` + +Read one hook in one run. `RUN_ID` is the run UUID from `--list` or from the result of the command that created the run. It is not the workspace name or an `op-N` ID: + +```sh +devsy workspace logs my-workspace --source setup --run RUN_ID \ + --hook postCreateCommand --result-format=plain +``` + +Read one command from a hook that runs several named or parallel commands, using its invocation ID: + +```sh +devsy workspace logs my-workspace --source setup --run RUN_ID --invocation INVOCATION_ID +``` + +Follow a run, including hooks that keep running after `devsy workspace up` returns. Ctrl-C stops only the reader: + +```sh +devsy workspace logs my-workspace --source setup --run RUN_ID --follow +``` + +Read a run after a failed creation or CI cleanup removed the workspace. Without a workspace name you must give a run UUID: + +```sh +devsy workspace logs --source setup --run RUN_ID --context CONTEXT +``` + +Page through long output with `--limit` and `--after`. The default is 500 output records per page and the maximum is 2000. For `--list` the default is 20 runs and the maximum is 100. `--after` takes the opaque cursor from the previous page and must be used with the same selectors: + +```sh +devsy workspace logs my-workspace --source setup --run RUN_ID --limit 200 --after CURSOR +``` + +Use `--result-format=json` for a machine-readable response. Each invocation prints one bounded JSON response. With `--follow` it prints one JSON response per line. + +A hook that failed in the past is still a successful read, so the command exits zero and returns the hook's exit status as data. Invalid selectors and cursors, and unreadable local storage, are command errors. See [Setup history](./setup-history.mdx) for limits, redaction and when history is unavailable. + ### Hide live progress while keeping diagnostics Pass `--no-status` to hide Devsy's live progress messages. Diagnostics still @@ -52,8 +114,8 @@ follow `--quiet`, `--log-level`, and diagnostic format settings, and a command's stdout and stderr remain on their original streams. Existing event and task recording also continues when available. Hiding progress does not disable capture or history. The flag cannot recover lifecycle output that was not -retained when the command ran. See the [lifecycle transcript discussion](https://github.com/devsy-org/devsy/discussions/1724) -for related work on retaining lifecycle output. +retained when the command ran. `--quiet` also leaves capture on, and setup +history stays readable with both flags. ### VS Code-family editor launch diagnostics