Skip to content

Commit aba2f46

Browse files
authored
chore(envbuilder.go): rename the concept of a magicDir to workingDir (#388)
1 parent d14ab3d commit aba2f46

File tree

7 files changed

+102
-102
lines changed

7 files changed

+102
-102
lines changed

devcontainer/devcontainer_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/stretchr/testify/require"
2323
)
2424

25-
const magicDir = "/.envbuilder"
25+
const workingDir = "/.envbuilder"
2626

2727
func stubLookupEnv(string) (string, bool) {
2828
return "", false
@@ -98,7 +98,7 @@ func TestCompileWithFeatures(t *testing.T) {
9898
featureTwoDir := fmt.Sprintf("/.envbuilder/features/two-%x", featureTwoMD5[:4])
9999

100100
t.Run("WithoutBuildContexts", func(t *testing.T) {
101-
params, err := dc.Compile(fs, "", magicDir, "", "", false, stubLookupEnv)
101+
params, err := dc.Compile(fs, "", workingDir, "", "", false, stubLookupEnv)
102102
require.NoError(t, err)
103103

104104
require.Equal(t, `FROM localhost:5000/envbuilder-test-codercom-code-server:latest
@@ -116,7 +116,7 @@ USER 1000`, params.DockerfileContent)
116116
})
117117

118118
t.Run("WithBuildContexts", func(t *testing.T) {
119-
params, err := dc.Compile(fs, "", magicDir, "", "", true, stubLookupEnv)
119+
params, err := dc.Compile(fs, "", workingDir, "", "", true, stubLookupEnv)
120120
require.NoError(t, err)
121121

122122
registryHost := strings.TrimPrefix(registry, "http://")
@@ -155,10 +155,10 @@ func TestCompileDevContainer(t *testing.T) {
155155
dc := &devcontainer.Spec{
156156
Image: "localhost:5000/envbuilder-test-ubuntu:latest",
157157
}
158-
params, err := dc.Compile(fs, "", magicDir, "", "", false, stubLookupEnv)
158+
params, err := dc.Compile(fs, "", workingDir, "", "", false, stubLookupEnv)
159159
require.NoError(t, err)
160-
require.Equal(t, filepath.Join(magicDir, "Dockerfile"), params.DockerfilePath)
161-
require.Equal(t, magicDir, params.BuildContext)
160+
require.Equal(t, filepath.Join(workingDir, "Dockerfile"), params.DockerfilePath)
161+
require.Equal(t, workingDir, params.BuildContext)
162162
})
163163
t.Run("WithBuild", func(t *testing.T) {
164164
t.Parallel()
@@ -181,7 +181,7 @@ func TestCompileDevContainer(t *testing.T) {
181181
_, err = io.WriteString(file, "FROM localhost:5000/envbuilder-test-ubuntu:latest")
182182
require.NoError(t, err)
183183
_ = file.Close()
184-
params, err := dc.Compile(fs, dcDir, magicDir, "", "/var/workspace", false, stubLookupEnv)
184+
params, err := dc.Compile(fs, dcDir, workingDir, "", "/var/workspace", false, stubLookupEnv)
185185
require.NoError(t, err)
186186
require.Equal(t, "ARG1=value1", params.BuildArgs[0])
187187
require.Equal(t, "ARG2=workspace", params.BuildArgs[1])

envbuilder.go

+34-34
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
"github.com/GoogleContainerTools/kaniko/pkg/util"
3636
"github.com/coder/envbuilder/devcontainer"
3737
"github.com/coder/envbuilder/internal/ebutil"
38-
"github.com/coder/envbuilder/internal/magicdir"
38+
"github.com/coder/envbuilder/internal/workingdir"
3939
"github.com/coder/envbuilder/log"
4040
"github.com/containerd/platforms"
4141
"github.com/distribution/distribution/v3/configuration"
@@ -120,7 +120,7 @@ func Run(ctx context.Context, opts options.Options, preExec ...func()) error {
120120
func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) error {
121121
defer options.UnsetEnv()
122122

123-
magicDir := magicdir.At(opts.MagicDirBase)
123+
workingDir := workingdir.At(opts.MagicDirBase)
124124

125125
stageNumber := 0
126126
startStage := func(format string, args ...any) func(format string, args ...any) {
@@ -154,7 +154,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
154154

155155
opts.Logger(log.LevelInfo, "%s %s - Build development environments from repositories in a container", newColor(color.Bold).Sprintf("envbuilder"), buildinfo.Version())
156156

157-
cleanupDockerConfigJSON, err := initDockerConfigJSON(opts.Logger, magicDir, opts.DockerConfigBase64)
157+
cleanupDockerConfigJSON, err := initDockerConfigJSON(opts.Logger, workingDir, opts.DockerConfigBase64)
158158
if err != nil {
159159
return err
160160
}
@@ -168,8 +168,8 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
168168
ContainerEnv: make(map[string]string),
169169
RemoteEnv: make(map[string]string),
170170
}
171-
if fileExists(opts.Filesystem, magicDir.Image()) {
172-
if err = parseMagicImageFile(opts.Filesystem, magicDir.Image(), &runtimeData); err != nil {
171+
if fileExists(opts.Filesystem, workingDir.Image()) {
172+
if err = parseMagicImageFile(opts.Filesystem, workingDir.Image(), &runtimeData); err != nil {
173173
return fmt.Errorf("parse magic image file: %w", err)
174174
}
175175
runtimeData.Image = true
@@ -186,7 +186,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
186186
opts.ExportEnvFile = ""
187187
}
188188
}
189-
runtimeData.Built = fileExists(opts.Filesystem, magicDir.Built())
189+
runtimeData.Built = fileExists(opts.Filesystem, workingDir.Built())
190190

191191
buildTimeWorkspaceFolder := opts.WorkspaceFolder
192192
var fallbackErr error
@@ -233,7 +233,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
233233
if err != nil {
234234
return fmt.Errorf("git clone options: %w", err)
235235
}
236-
cloneOpts.Path = magicDir.Join("repo")
236+
cloneOpts.Path = workingDir.Join("repo")
237237

238238
endStage := startStage("📦 Remote repo build mode enabled, cloning %s to %s for build context...",
239239
newColor(color.FgCyan).Sprintf(opts.GitURL),
@@ -259,7 +259,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
259259

260260
if !runtimeData.Image {
261261
defaultBuildParams := func() (*devcontainer.Compiled, error) {
262-
dockerfile := magicDir.Join("Dockerfile")
262+
dockerfile := workingDir.Join("Dockerfile")
263263
file, err := opts.Filesystem.OpenFile(dockerfile, os.O_CREATE|os.O_WRONLY, 0o644)
264264
if err != nil {
265265
return nil, err
@@ -281,7 +281,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
281281
return &devcontainer.Compiled{
282282
DockerfilePath: dockerfile,
283283
DockerfileContent: content,
284-
BuildContext: magicDir.Path(),
284+
BuildContext: workingDir.Path(),
285285
}, nil
286286
}
287287

@@ -318,7 +318,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
318318
opts.Logger(log.LevelInfo, "No Dockerfile or image specified; falling back to the default image...")
319319
fallbackDockerfile = defaultParams.DockerfilePath
320320
}
321-
buildParams, err = devContainer.Compile(opts.Filesystem, devcontainerDir, magicDir.Path(), fallbackDockerfile, opts.WorkspaceFolder, false, os.LookupEnv)
321+
buildParams, err = devContainer.Compile(opts.Filesystem, devcontainerDir, workingDir.Path(), fallbackDockerfile, opts.WorkspaceFolder, false, os.LookupEnv)
322322
if err != nil {
323323
return fmt.Errorf("compile devcontainer.json: %w", err)
324324
}
@@ -393,7 +393,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
393393
// So we add them to the default ignore list. See:
394394
// https://github.com/GoogleContainerTools/kaniko/blob/63be4990ca5a60bdf06ddc4d10aa4eca0c0bc714/cmd/executor/cmd/root.go#L136
395395
ignorePaths := append([]string{
396-
magicDir.Path(),
396+
workingDir.Path(),
397397
opts.WorkspaceFolder,
398398
// See: https://github.com/coder/envbuilder/issues/37
399399
"/etc/resolv.conf",
@@ -421,18 +421,18 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
421421
if err := util.AddAllowedPathToDefaultIgnoreList(opts.BinaryPath); err != nil {
422422
return fmt.Errorf("add envbuilder binary to ignore list: %w", err)
423423
}
424-
if err := util.AddAllowedPathToDefaultIgnoreList(magicDir.Image()); err != nil {
424+
if err := util.AddAllowedPathToDefaultIgnoreList(workingDir.Image()); err != nil {
425425
return fmt.Errorf("add magic image file to ignore list: %w", err)
426426
}
427-
if err := util.AddAllowedPathToDefaultIgnoreList(magicDir.Features()); err != nil {
427+
if err := util.AddAllowedPathToDefaultIgnoreList(workingDir.Features()); err != nil {
428428
return fmt.Errorf("add features to ignore list: %w", err)
429429
}
430-
magicTempDir := magicdir.At(buildParams.BuildContext, magicdir.TempDir)
430+
magicTempDir := workingdir.At(buildParams.BuildContext, workingdir.TempDir)
431431
if err := opts.Filesystem.MkdirAll(magicTempDir.Path(), 0o755); err != nil {
432432
return fmt.Errorf("create magic temp dir in build context: %w", err)
433433
}
434434
// Add the magic directives that embed the binary into the built image.
435-
buildParams.DockerfileContent += magicdir.Directives
435+
buildParams.DockerfileContent += workingdir.Directives
436436

437437
envbuilderBinDest := filepath.Join(magicTempDir.Path(), "envbuilder")
438438
magicImageDest := magicTempDir.Image()
@@ -467,7 +467,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
467467
}
468468

469469
// temp move of all ro mounts
470-
tempRemountDest := magicDir.Join("mnt")
470+
tempRemountDest := workingDir.Join("mnt")
471471
// ignorePrefixes is a superset of ignorePaths that we pass to kaniko's
472472
// IgnoreList.
473473
ignorePrefixes := append([]string{"/dev", "/proc", "/sys"}, ignorePaths...)
@@ -845,7 +845,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
845845
// Create the magic file to indicate that this build
846846
// has already been ran before!
847847
if !runtimeData.Built {
848-
file, err := opts.Filesystem.Create(magicDir.Built())
848+
file, err := opts.Filesystem.Create(workingDir.Built())
849849
if err != nil {
850850
return fmt.Errorf("create magic file: %w", err)
851851
}
@@ -864,7 +864,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
864864
opts.Logger(log.LevelInfo, "=== Running the setup command %q as the root user...", opts.SetupScript)
865865

866866
envKey := "ENVBUILDER_ENV"
867-
envFile := magicDir.Join("environ")
867+
envFile := workingDir.Join("environ")
868868
file, err := opts.Filesystem.Create(envFile)
869869
if err != nil {
870870
return fmt.Errorf("create environ file: %w", err)
@@ -962,7 +962,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
962962
return nil, fmt.Errorf("--cache-repo must be set when using --get-cached-image")
963963
}
964964

965-
magicDir := magicdir.At(opts.MagicDirBase)
965+
workingDir := workingdir.At(opts.MagicDirBase)
966966

967967
stageNumber := 0
968968
startStage := func(format string, args ...any) func(format string, args ...any) {
@@ -978,7 +978,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
978978

979979
opts.Logger(log.LevelInfo, "%s %s - Build development environments from repositories in a container", newColor(color.Bold).Sprintf("envbuilder"), buildinfo.Version())
980980

981-
cleanupDockerConfigJSON, err := initDockerConfigJSON(opts.Logger, magicDir, opts.DockerConfigBase64)
981+
cleanupDockerConfigJSON, err := initDockerConfigJSON(opts.Logger, workingDir, opts.DockerConfigBase64)
982982
if err != nil {
983983
return nil, err
984984
}
@@ -1031,7 +1031,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
10311031
if err != nil {
10321032
return nil, fmt.Errorf("git clone options: %w", err)
10331033
}
1034-
cloneOpts.Path = magicDir.Join("repo")
1034+
cloneOpts.Path = workingDir.Join("repo")
10351035

10361036
endStage := startStage("📦 Remote repo build mode enabled, cloning %s to %s for build context...",
10371037
newColor(color.FgCyan).Sprintf(opts.GitURL),
@@ -1056,7 +1056,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
10561056
}
10571057

10581058
defaultBuildParams := func() (*devcontainer.Compiled, error) {
1059-
dockerfile := magicDir.Join("Dockerfile")
1059+
dockerfile := workingDir.Join("Dockerfile")
10601060
file, err := opts.Filesystem.OpenFile(dockerfile, os.O_CREATE|os.O_WRONLY, 0o644)
10611061
if err != nil {
10621062
return nil, err
@@ -1078,7 +1078,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
10781078
return &devcontainer.Compiled{
10791079
DockerfilePath: dockerfile,
10801080
DockerfileContent: content,
1081-
BuildContext: magicDir.Path(),
1081+
BuildContext: workingDir.Path(),
10821082
}, nil
10831083
}
10841084

@@ -1118,7 +1118,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
11181118
opts.Logger(log.LevelInfo, "No Dockerfile or image specified; falling back to the default image...")
11191119
fallbackDockerfile = defaultParams.DockerfilePath
11201120
}
1121-
buildParams, err = devContainer.Compile(opts.Filesystem, devcontainerDir, magicDir.Path(), fallbackDockerfile, opts.WorkspaceFolder, false, os.LookupEnv)
1121+
buildParams, err = devContainer.Compile(opts.Filesystem, devcontainerDir, workingDir.Path(), fallbackDockerfile, opts.WorkspaceFolder, false, os.LookupEnv)
11221122
if err != nil {
11231123
return nil, fmt.Errorf("compile devcontainer.json: %w", err)
11241124
}
@@ -1184,7 +1184,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
11841184
// So we add them to the default ignore list. See:
11851185
// https://github.com/GoogleContainerTools/kaniko/blob/63be4990ca5a60bdf06ddc4d10aa4eca0c0bc714/cmd/executor/cmd/root.go#L136
11861186
ignorePaths := append([]string{
1187-
magicDir.Path(),
1187+
workingDir.Path(),
11881188
opts.WorkspaceFolder,
11891189
// See: https://github.com/coder/envbuilder/issues/37
11901190
"/etc/resolv.conf",
@@ -1207,10 +1207,10 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
12071207
// build via executor.RunCacheProbe we need to have the *exact* copy of the
12081208
// envbuilder binary available used to build the image and we also need to
12091209
// add the magic directives to the Dockerfile content.
1210-
// MAGICDIR
1211-
buildParams.DockerfileContent += magicdir.Directives
1210+
// WORKINGDIR
1211+
buildParams.DockerfileContent += workingdir.Directives
12121212

1213-
magicTempDir := filepath.Join(buildParams.BuildContext, magicdir.TempDir)
1213+
magicTempDir := filepath.Join(buildParams.BuildContext, workingdir.TempDir)
12141214
if err := opts.Filesystem.MkdirAll(magicTempDir, 0o755); err != nil {
12151215
return nil, fmt.Errorf("create magic temp dir in build context: %w", err)
12161216
}
@@ -1546,11 +1546,11 @@ func maybeDeleteFilesystem(logger log.Func, force bool) error {
15461546
// We always expect the magic directory to be set to the default, signifying that
15471547
// the user is running envbuilder in a container.
15481548
// If this is set to anything else we should bail out to prevent accidental data loss.
1549-
// defaultMagicDir := magicdir.MagicDir("")
1549+
// defaultWorkingDir := workingdir.WorkingDir("")
15501550
kanikoDir, ok := os.LookupEnv("KANIKO_DIR")
1551-
if !ok || strings.TrimSpace(kanikoDir) != magicdir.Default.Path() {
1551+
if !ok || strings.TrimSpace(kanikoDir) != workingdir.Default.Path() {
15521552
if !force {
1553-
logger(log.LevelError, "KANIKO_DIR is not set to %s. Bailing!\n", magicdir.Default.Path())
1553+
logger(log.LevelError, "KANIKO_DIR is not set to %s. Bailing!\n", workingdir.Default.Path())
15541554
logger(log.LevelError, "To bypass this check, set FORCE_SAFE=true.")
15551555
return errors.New("safety check failed")
15561556
}
@@ -1627,13 +1627,13 @@ func parseMagicImageFile(fs billy.Filesystem, path string, v any) error {
16271627
return nil
16281628
}
16291629

1630-
func initDockerConfigJSON(logf log.Func, magicDir magicdir.MagicDir, dockerConfigBase64 string) (func() error, error) {
1630+
func initDockerConfigJSON(logf log.Func, workingDir workingdir.WorkingDir, dockerConfigBase64 string) (func() error, error) {
16311631
var cleanupOnce sync.Once
16321632
noop := func() error { return nil }
16331633
if dockerConfigBase64 == "" {
16341634
return noop, nil
16351635
}
1636-
cfgPath := magicDir.Join("config.json")
1636+
cfgPath := workingDir.Join("config.json")
16371637
decoded, err := base64.StdEncoding.DecodeString(dockerConfigBase64)
16381638
if err != nil {
16391639
return noop, fmt.Errorf("decode docker config: %w", err)
@@ -1656,7 +1656,7 @@ func initDockerConfigJSON(logf log.Func, magicDir magicdir.MagicDir, dockerConfi
16561656
}
16571657
logf(log.LevelInfo, "Wrote Docker config JSON to %s", cfgPath)
16581658
oldDockerConfig := os.Getenv("DOCKER_CONFIG")
1659-
_ = os.Setenv("DOCKER_CONFIG", magicDir.Path())
1659+
_ = os.Setenv("DOCKER_CONFIG", workingDir.Path())
16601660
newDockerConfig := os.Getenv("DOCKER_CONFIG")
16611661
logf(log.LevelInfo, "Set DOCKER_CONFIG to %s", newDockerConfig)
16621662
cleanup := func() error {

integration/integration_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"github.com/coder/coder/v2/codersdk/agentsdk"
2828
"github.com/coder/envbuilder"
2929
"github.com/coder/envbuilder/devcontainer/features"
30-
"github.com/coder/envbuilder/internal/magicdir"
30+
"github.com/coder/envbuilder/internal/workingdir"
3131
"github.com/coder/envbuilder/options"
3232
"github.com/coder/envbuilder/testutil/gittest"
3333
"github.com/coder/envbuilder/testutil/mwtest"
@@ -502,7 +502,7 @@ func TestBuildFromDockerfile(t *testing.T) {
502502
require.Equal(t, "hello", strings.TrimSpace(output))
503503

504504
// Verify that the Docker configuration secret file is removed
505-
configJSONContainerPath := magicdir.Default.Join("config.json")
505+
configJSONContainerPath := workingdir.Default.Join("config.json")
506506
output = execContainer(t, ctr, "stat "+configJSONContainerPath)
507507
require.Contains(t, output, "No such file or directory")
508508
}

internal/magicdir/magicdir_internal_test.go

-38
This file was deleted.

0 commit comments

Comments
 (0)