Skip to content

Commit

Permalink
chore(envbuilder.go): rename the concept of a magicDir to workingDir (#…
Browse files Browse the repository at this point in the history
…388)

(cherry picked from commit aba2f46)
  • Loading branch information
SasSwart authored and mafredri committed Oct 30, 2024
1 parent 7756b14 commit 179f3c8
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 102 deletions.
14 changes: 7 additions & 7 deletions devcontainer/devcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/stretchr/testify/require"
)

const magicDir = "/.envbuilder"
const workingDir = "/.envbuilder"

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

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

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

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

registryHost := strings.TrimPrefix(registry, "http://")
Expand Down Expand Up @@ -155,10 +155,10 @@ func TestCompileDevContainer(t *testing.T) {
dc := &devcontainer.Spec{
Image: "localhost:5000/envbuilder-test-ubuntu:latest",
}
params, err := dc.Compile(fs, "", magicDir, "", "", false, stubLookupEnv)
params, err := dc.Compile(fs, "", workingDir, "", "", false, stubLookupEnv)
require.NoError(t, err)
require.Equal(t, filepath.Join(magicDir, "Dockerfile"), params.DockerfilePath)
require.Equal(t, magicDir, params.BuildContext)
require.Equal(t, filepath.Join(workingDir, "Dockerfile"), params.DockerfilePath)
require.Equal(t, workingDir, params.BuildContext)
})
t.Run("WithBuild", func(t *testing.T) {
t.Parallel()
Expand All @@ -181,7 +181,7 @@ func TestCompileDevContainer(t *testing.T) {
_, err = io.WriteString(file, "FROM localhost:5000/envbuilder-test-ubuntu:latest")
require.NoError(t, err)
_ = file.Close()
params, err := dc.Compile(fs, dcDir, magicDir, "", "/var/workspace", false, stubLookupEnv)
params, err := dc.Compile(fs, dcDir, workingDir, "", "/var/workspace", false, stubLookupEnv)
require.NoError(t, err)
require.Equal(t, "ARG1=value1", params.BuildArgs[0])
require.Equal(t, "ARG2=workspace", params.BuildArgs[1])
Expand Down
68 changes: 34 additions & 34 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/coder/envbuilder/devcontainer"
"github.com/coder/envbuilder/internal/ebutil"
"github.com/coder/envbuilder/internal/magicdir"
"github.com/coder/envbuilder/internal/workingdir"
"github.com/coder/envbuilder/log"
"github.com/containerd/platforms"
"github.com/distribution/distribution/v3/configuration"
Expand Down Expand Up @@ -120,7 +120,7 @@ func Run(ctx context.Context, opts options.Options, preExec ...func()) error {
func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) error {
defer options.UnsetEnv()

magicDir := magicdir.At(opts.MagicDirBase)
workingDir := workingdir.At(opts.MagicDirBase)

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

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

cleanupDockerConfigJSON, err := initDockerConfigJSON(opts.Logger, magicDir, opts.DockerConfigBase64)
cleanupDockerConfigJSON, err := initDockerConfigJSON(opts.Logger, workingDir, opts.DockerConfigBase64)
if err != nil {
return err
}
Expand All @@ -168,8 +168,8 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
ContainerEnv: make(map[string]string),
RemoteEnv: make(map[string]string),
}
if fileExists(opts.Filesystem, magicDir.Image()) {
if err = parseMagicImageFile(opts.Filesystem, magicDir.Image(), &runtimeData); err != nil {
if fileExists(opts.Filesystem, workingDir.Image()) {
if err = parseMagicImageFile(opts.Filesystem, workingDir.Image(), &runtimeData); err != nil {
return fmt.Errorf("parse magic image file: %w", err)
}
runtimeData.Image = true
Expand All @@ -186,7 +186,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
opts.ExportEnvFile = ""
}
}
runtimeData.Built = fileExists(opts.Filesystem, magicDir.Built())
runtimeData.Built = fileExists(opts.Filesystem, workingDir.Built())

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

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

if !runtimeData.Image {
defaultBuildParams := func() (*devcontainer.Compiled, error) {
dockerfile := magicDir.Join("Dockerfile")
dockerfile := workingDir.Join("Dockerfile")
file, err := opts.Filesystem.OpenFile(dockerfile, os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return nil, err
Expand All @@ -281,7 +281,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
return &devcontainer.Compiled{
DockerfilePath: dockerfile,
DockerfileContent: content,
BuildContext: magicDir.Path(),
BuildContext: workingDir.Path(),
}, nil
}

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

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

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

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

magicDir := magicdir.At(opts.MagicDirBase)
workingDir := workingdir.At(opts.MagicDirBase)

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

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

cleanupDockerConfigJSON, err := initDockerConfigJSON(opts.Logger, magicDir, opts.DockerConfigBase64)
cleanupDockerConfigJSON, err := initDockerConfigJSON(opts.Logger, workingDir, opts.DockerConfigBase64)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1031,7 +1031,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
if err != nil {
return nil, fmt.Errorf("git clone options: %w", err)
}
cloneOpts.Path = magicDir.Join("repo")
cloneOpts.Path = workingDir.Join("repo")

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

defaultBuildParams := func() (*devcontainer.Compiled, error) {
dockerfile := magicDir.Join("Dockerfile")
dockerfile := workingDir.Join("Dockerfile")
file, err := opts.Filesystem.OpenFile(dockerfile, os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return nil, err
Expand All @@ -1078,7 +1078,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
return &devcontainer.Compiled{
DockerfilePath: dockerfile,
DockerfileContent: content,
BuildContext: magicDir.Path(),
BuildContext: workingDir.Path(),
}, nil
}

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

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

func initDockerConfigJSON(logf log.Func, magicDir magicdir.MagicDir, dockerConfigBase64 string) (func() error, error) {
func initDockerConfigJSON(logf log.Func, workingDir workingdir.WorkingDir, dockerConfigBase64 string) (func() error, error) {
var cleanupOnce sync.Once
noop := func() error { return nil }
if dockerConfigBase64 == "" {
return noop, nil
}
cfgPath := magicDir.Join("config.json")
cfgPath := workingDir.Join("config.json")
decoded, err := base64.StdEncoding.DecodeString(dockerConfigBase64)
if err != nil {
return noop, fmt.Errorf("decode docker config: %w", err)
Expand All @@ -1656,7 +1656,7 @@ func initDockerConfigJSON(logf log.Func, magicDir magicdir.MagicDir, dockerConfi
}
logf(log.LevelInfo, "Wrote Docker config JSON to %s", cfgPath)
oldDockerConfig := os.Getenv("DOCKER_CONFIG")
_ = os.Setenv("DOCKER_CONFIG", magicDir.Path())
_ = os.Setenv("DOCKER_CONFIG", workingDir.Path())
newDockerConfig := os.Getenv("DOCKER_CONFIG")
logf(log.LevelInfo, "Set DOCKER_CONFIG to %s", newDockerConfig)
cleanup := func() error {
Expand Down
4 changes: 2 additions & 2 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/coder/coder/v2/codersdk/agentsdk"
"github.com/coder/envbuilder"
"github.com/coder/envbuilder/devcontainer/features"
"github.com/coder/envbuilder/internal/magicdir"
"github.com/coder/envbuilder/internal/workingdir"
"github.com/coder/envbuilder/options"
"github.com/coder/envbuilder/testutil/gittest"
"github.com/coder/envbuilder/testutil/mwtest"
Expand Down Expand Up @@ -502,7 +502,7 @@ func TestBuildFromDockerfile(t *testing.T) {
require.Equal(t, "hello", strings.TrimSpace(output))

// Verify that the Docker configuration secret file is removed
configJSONContainerPath := magicdir.Default.Join("config.json")
configJSONContainerPath := workingdir.Default.Join("config.json")
output = execContainer(t, ctr, "stat "+configJSONContainerPath)
require.Contains(t, output, "No such file or directory")
}
Expand Down
38 changes: 0 additions & 38 deletions internal/magicdir/magicdir_internal_test.go

This file was deleted.

Loading

0 comments on commit 179f3c8

Please sign in to comment.