8000 fix: allow setting MagicDir in Options by johnstcn · Pull Request #337 · coder/envbuilder · GitHub
[go: up one dir, main page]

Skip to content

fix: allow setting MagicDir in Options #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move MagicDir to internal/magicdir
  • Loading branch information
johnstcn committed Sep 9, 2024
commit b8eb67e51be7d7e46405464733bd618e687aa4ef
22 changes: 11 additions & 11 deletions envbuilder.go
Original file 10000 line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"time"

"github.com/coder/envbuilder/buildinfo"
"github.com/coder/envbuilder/constants"
"github.com/coder/envbuilder/git"
"github.com/coder/envbuilder/options"
"github.com/go-git/go-billy/v5"
Expand All @@ -36,6 +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/log"
"github.com/containerd/platforms"
"github.com/distribution/distribution/v3/configuration"
Expand Down Expand Up @@ -72,7 +72,7 @@ func Run(ctx context.Context, opts options.Options) error {
return fmt.Errorf("--cache-repo must be set when using --push-image")
}

magicDir := constants.MagicDirAt(opts.MagicDirBase)
magicDir := magicdir.At(opts.MagicDirBase)

// Default to the shell!
initArgs := []string{"-c", opts.InitScript}
Expand Down Expand Up @@ -341,12 +341,12 @@ func Run(ctx context.Context, opts options.Options) error {
if err := util.AddAllowedPathToDefaultIgnoreList(magicDir.Image()); err != nil {
return fmt.Errorf("add magic image file to ignore list: %w", err)
}
magicTempDir := constants.MagicDirAt(buildParams.BuildContext, constants.MagicTempDir)
magicTempDir := magicdir.At(buildParams.BuildContext, magicdir.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 += constants.MagicDirectives
buildParams.DockerfileContent += magicdir.Directives
// Copy the envbuilder binary into the build context.
// External callers will need to specify the path to the desired envbuilder binary.
envbuilderBinDest := filepath.Join(magicTempDir.Path(), "envbuilder")
Expand Down Expand Up @@ -861,7 +861,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 := constants.MagicDirAt(opts.MagicDirBase)
magicDir := magicdir.At(opts.MagicDirBase)

stageNumber := 0
startStage := func(format string, args ...any) func(format string, args ...any) {
Expand Down Expand Up @@ -1105,8 +1105,8 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
// envbuilder binary available used to build the image and we also need to
// add the magic directives to the Dockerfile content.
// MAGICDIR
buildParams.DockerfileContent += constants.MagicDirectives
magicTempDir := filepath.Join(buildParams.BuildContext, constants.MagicTempDir)
buildParams.DockerfileContent += magicdir.Directives
magicTempDir := filepath.Join(buildParams.BuildContext, magicdir.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 @@ -1417,11 +1417,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 := constants.MagicDir("")
// defaultMagicDir := magicdir.MagicDir("")
kanikoDir, ok := os.LookupEnv("KANIKO_DIR")
if !ok || strings.TrimSpace(kanikoDir) != constants.DefaultMagicDir.Path() {
if !ok || strings.TrimSpace(kanikoDir) != magicdir.Default.Path() {
if !force {
logger(log.LevelError, "KANIKO_DIR is not set to %s. Bailing!\n", constants.DefaultMagicDir.Path())
logger( 10000 log.LevelError, "KANIKO_DIR is not set to %s. Bailing!\n", magicdir.Default.Path())
logger(log.LevelError, "To bypass this check, set FORCE_SAFE=true.")
return errors.New("safety check failed")
}
Expand Down Expand Up @@ -1469,7 +1469,7 @@ func touchFile(fs billy.Filesystem, dst string, mode fs.FileMode) error {
return f.Close()
}

func initDockerConfigJSON(logf log.Func, magicDir constants.MagicDir, dockerConfigBase64 string) (func() error, error) {
func initDockerConfigJSON(logf log.Func, magicDir magicdir.MagicDir, dockerConfigBase64 string) (func() error, error) {
var cleanupOnce sync.Once
noop := func() error { return nil }
if dockerConfigBase64 == "" {
Expand Down
4 changes: 2 additions & 2 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"time"

"github.com/coder/envbuilder"
"github.com/coder/envbuilder/constants"
"github.com/coder/envbuilder/devcontainer/features"
"github.com/coder/envbuilder/internal/magicdir"
"github.com/coder/envbuilder/options"
"github.com/coder/envbuilder/testutil/gittest"
"github.com/coder/envbuilder/testutil/mwtest"
Expand Down Expand Up @@ -365,7 +365,7 @@ func TestBuildFromDockerfile(t *testing.T) {
require.Equal(t, "hello", strings.TrimSpace(output))

// Verify that the Docker configuration secret file is removed
configJSONContainerPath := constants.DefaultMagicDir.Join("config.json")
configJSONContainerPath := magicdir.Default.Join("config.json")
output = execContainer(t, ctr, "stat "+configJSONContainerPath)
require.Contains(t, output, "No such file or directory")
}
Expand Down
20 changes: 10 additions & 10 deletions constants/constants.go → internal/magicdir/magicdir.go
EDBE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package constants
package magicdir

import (
"fmt"
Expand All @@ -11,26 +11,26 @@ const (
// or images. This is intentionally unexported.
defaultMagicDirBase = "/.envbuilder"

// MagicTempDir is a directory inside the build context inside which
// TempDir is a directory inside the build context inside which
// we place files referenced by MagicDirectives.
MagicTempDir = ".envbuilder.tmp"
TempDir = ".envbuilder.tmp"
)

var (
// DefaultMagicDir is the default working directory for Envbuilder.
// Default is the default working directory for Envbuilder.
// This defaults to /.envbuilder. It should only be used when Envbuilder
// is known to be running as root inside a container.
DefaultMagicDir MagicDir
// MagicDirectives are directives automatically appended to Dockerfiles
Default MagicDir
// Directives are directives automatically appended to Dockerfiles
// when pushing the image. These directives allow the built image to be
// 're-used'.
MagicDirectives = fmt.Sprintf(`
Directives = fmt.Sprintf(`
COPY --chmod=0755 %[1]s/envbuilder %[2]s/bin/envbuilder
COPY --chmod=0644 %[1]s/image %[2]s/image
USER root
WORKDIR /
ENTRYPOINT ["%[2]s/bin/envbuilder"]
`, MagicTempDir, defaultMagicDirBase)
`, TempDir, defaultMagicDirBase)
)

// MagicDir is a working directory for envbuilder. It
Expand All @@ -39,8 +39,8 @@ type MagicDir struct {
base string
}

// MagicDirAt returns a MagicDir rooted at filepath.Join(paths...)
func MagicDirAt(paths ...string) MagicDir {
// At returns a MagicDir rooted at filepath.Join(paths...)
func At(paths ...string) MagicDir {
if len(paths) == 0 {
return MagicDir{}
}
Expand Down
38 changes: 38 additions & 0 deletions internal/magicdir/magicdir_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package magicdir

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_MagicDir(t *testing.T) {
t.Parallel()

t.Run("Default", func(t *testing.T) {
t.Parallel()
require.Equal(t, defaultMagicDirBase+"/foo", Default.Join("foo"))
require.Equal(t, defaultMagicDirBase, Default.Path())
require.Equal(t, defaultMagicDirBase+"/built", Default.Built())
require.Equal(t, defaultMagicDirBase+"/image", Default.Image())
})

t.Run("ZeroValue", func(t *testing.T) {
t.Parallel()
var md MagicDir
require.Equal(t, defaultMagicDirBase+"/foo", md.Join("foo"))
require.Equal(t, defaultMagicDirBase, md.Path())
require.Equal(t, defaultMagicDirBase+"/built", md.Built())
require.Equal(t, defaultMagicDirBase+"/image", md.Image())
})

t.Run("At", func(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
md := At(tmpDir)
require.Equal(t, tmpDir+"/foo", md.Join("foo"))
require.Equal(t, tmpDir, md.Path())
require.Equal(t, tmpDir+"/built", md.Built())
require.Equal(t, tmpDir+"/image", md.Image())
})
}
4 changes: 2 additions & 2 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"strings"

"github.com/coder/envbuilder/constants"
"github.com/coder/envbuilder/internal/magicdir"
"github.com/coder/envbuilder/log"
"github.com/coder/serpent"
"github.com/go-git/go-billy/v5"
Expand Down Expand Up @@ -464,7 +464,7 @@ func (o *Options) CLI() serpent.OptionSet {
Flag: "remote-repo-dir",
Env: WithEnvPrefix("REMOTE_REPO_DIR"),
Value: serpent.StringOf(&o.RemoteRepoDir),
Default: constants.DefaultMagicDir.Join("repo"),
Default: magicdir.Default.Join("repo"),
Hidden: true,
Description: "Specify the destination directory for the cloned repo when using remote repo build mode.",
},
Expand Down
0