Skip to content

Commit f776d41

Browse files
authored
Polish and use native workspaces in executors mode (#768)
This PR cleans up the src side of things for SSBC: - Ensures to have a separate cache directory for each src running, so they don't collide while writing to them - Ensures to have a tmp directory set within the executor workspace so it can be retained for inspection - Removes useless API call to Sourcegraph to retrieve it's version - Supports native repos cloned into the workspace - Ensures no env var is used for cache key generation, those are not set on the server and it would cause cache key mismatches - And most importantly, it doesn't require an access token anymore, so it's much safer, especially in less-isolated executors! Closes https://github.com/sourcegraph/sourcegraph/issues/37079 Closes https://github.com/sourcegraph/sourcegraph/issues/36597 Closes https://github.com/sourcegraph/sourcegraph/issues/36187
1 parent 8503b35 commit f776d41

23 files changed

Lines changed: 430 additions & 216 deletions

cmd/src/batch_apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Examples:
3232
`
3333

3434
flagSet := flag.NewFlagSet("apply", flag.ExitOnError)
35-
flags := newBatchExecuteFlags(flagSet, false, batchDefaultCacheDir(), batchDefaultTempDirPrefix())
35+
flags := newBatchExecuteFlags(flagSet, batchDefaultCacheDir(), batchDefaultTempDirPrefix())
3636

3737
handler := func(args []string) error {
3838
if err := flagSet.Parse(args); err != nil {

cmd/src/batch_common.go

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"github.com/sourcegraph/src-cli/internal/batches"
2626
"github.com/sourcegraph/src-cli/internal/batches/executor"
2727
"github.com/sourcegraph/src-cli/internal/batches/graphql"
28+
"github.com/sourcegraph/src-cli/internal/batches/log"
29+
"github.com/sourcegraph/src-cli/internal/batches/repozip"
2830
"github.com/sourcegraph/src-cli/internal/batches/service"
2931
"github.com/sourcegraph/src-cli/internal/batches/ui"
3032
"github.com/sourcegraph/src-cli/internal/batches/workspace"
@@ -86,30 +88,31 @@ type batchExecuteFlags struct {
8688
textOnly bool
8789
}
8890

89-
func newBatchExecuteFlags(flagSet *flag.FlagSet, workspaceExecution bool, cacheDir, tempDir string) *batchExecuteFlags {
91+
func newBatchExecuteFlags(flagSet *flag.FlagSet, cacheDir, tempDir string) *batchExecuteFlags {
9092
caf := &batchExecuteFlags{
9193
batchExecutionFlags: newBatchExecutionFlags(flagSet),
9294
}
9395

94-
if !workspaceExecution {
95-
flagSet.BoolVar(
96-
&caf.textOnly, "text-only", false,
97-
"INTERNAL USE ONLY. EXPERIMENTAL. Switches off the TUI to only print JSON lines.",
98-
)
99-
flagSet.BoolVar(
100-
&caf.apply, "apply", false,
101-
"Ignored.",
102-
)
103-
flagSet.BoolVar(
104-
&caf.keepLogs, "keep-logs", false,
105-
"Retain logs after executing steps.",
106-
)
107-
}
96+
flagSet.BoolVar(
97+
&caf.textOnly, "text-only", false,
98+
"INTERNAL USE ONLY. EXPERIMENTAL. Switches off the TUI to only print JSON lines.",
99+
)
100+
101+
flagSet.BoolVar(
102+
&caf.apply, "apply", false,
103+
"Ignored.",
104+
)
105+
106+
flagSet.BoolVar(
107+
&caf.keepLogs, "keep-logs", false,
108+
"Retain logs after executing steps.",
109+
)
108110

109111
flagSet.StringVar(
110112
&caf.cacheDir, "cache", cacheDir,
111113
"Directory for caching results and repository archives.",
112114
)
115+
113116
flagSet.StringVar(
114117
&caf.tempDir, "tmp", tempDir,
115118
"Directory for storing temporary data, such as log files. Default is /tmp. Can also be set with environment variable SRC_BATCH_TMP_DIR; if both are set, this flag will be used and not the environment variable.",
@@ -124,14 +127,17 @@ func newBatchExecuteFlags(flagSet *flag.FlagSet, workspaceExecution bool, cacheD
124127
&caf.parallelism, "j", runtime.GOMAXPROCS(0),
125128
"The maximum number of parallel jobs. Default is GOMAXPROCS.",
126129
)
130+
127131
flagSet.DurationVar(
128132
&caf.timeout, "timeout", 60*time.Minute,
129133
"The maximum duration a single batch spec step can take.",
130134
)
135+
131136
flagSet.BoolVar(
132137
&caf.cleanArchives, "clean-archives", true,
133138
"If true, deletes downloaded repository archives after executing batch spec steps.",
134139
)
140+
135141
flagSet.BoolVar(
136142
&caf.skipErrors, "skip-errors", false,
137143
"If true, errors encountered while executing steps in a repository won't stop the execution of the batch spec but only cause that repository to be skipped.",
@@ -316,14 +322,15 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
316322
ui.PreparingContainerImagesSuccess()
317323

318324
ui.DeterminingWorkspaceCreatorType()
319-
workspaceCreator = workspace.NewCreator(ctx, opts.flags.workspace, opts.flags.cacheDir, opts.flags.tempDir, images)
320-
if workspaceCreator.Type() == workspace.CreatorTypeVolume {
325+
var typ workspace.CreatorType
326+
workspaceCreator, typ = workspace.NewCreator(ctx, opts.flags.workspace, opts.flags.cacheDir, opts.flags.tempDir, images)
327+
if typ == workspace.CreatorTypeVolume {
321328
_, err = svc.EnsureImage(ctx, workspace.DockerVolumeWorkspaceImage)
322329
if err != nil {
323330
return err
324331
}
325332
}
326-
ui.DeterminingWorkspaceCreatorTypeSuccess(workspaceCreator.Type())
333+
ui.DeterminingWorkspaceCreatorTypeSuccess(typ)
327334
}
328335

329336
ui.ResolvingRepositories()
@@ -347,19 +354,22 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
347354
}
348355
ui.DeterminingWorkspacesSuccess(len(workspaces))
349356

357+
archiveRegistry := repozip.NewArchiveRegistry(opts.client, opts.flags.cacheDir, opts.flags.cleanArchives)
358+
350359
// EXECUTION OF TASKS
351-
coord := svc.NewCoordinator(executor.NewCoordinatorOpts{
352-
Creator: workspaceCreator,
353-
CacheDir: opts.flags.cacheDir,
354-
Cache: executor.NewDiskCache(opts.flags.cacheDir),
355-
SkipErrors: opts.flags.skipErrors,
356-
CleanArchives: opts.flags.cleanArchives,
357-
Parallelism: opts.flags.parallelism,
358-
Timeout: opts.flags.timeout,
359-
KeepLogs: opts.flags.keepLogs,
360-
TempDir: opts.flags.tempDir,
361-
AllowPathMounts: true,
362-
})
360+
coord := svc.NewCoordinator(
361+
archiveRegistry,
362+
log.NewDiskManager(opts.flags.tempDir, opts.flags.keepLogs),
363+
executor.NewCoordinatorOpts{
364+
Creator: workspaceCreator,
365+
Cache: executor.NewDiskCache(opts.flags.cacheDir),
366+
Parallelism: opts.flags.parallelism,
367+
Timeout: opts.flags.timeout,
368+
TempDir: opts.flags.tempDir,
369+
GlobalEnv: os.Environ(),
370+
AllowPathMounts: true,
371+
},
372+
)
363373

364374
ui.CheckingCache()
365375
tasks := svc.BuildTasks(

0 commit comments

Comments
 (0)