Skip to content

add support for starting envbuilder from built image #292

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ var (
// to skip building when a container is restarting.
// e.g. docker stop -> docker start
MagicFile = filepath.Join(MagicDir, "built")

// MagicImage is a file that is created in the image when
// envbuilder has already been run. This is used to skip
// building when a container is starting.
MagicImage = filepath.Join(MagicDir, "image")
)
56 changes: 43 additions & 13 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/coder/envbuilder/constants"
"github.com/coder/envbuilder/git"
"github.com/coder/envbuilder/options"
"github.com/go-git/go-billy/v5"

"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/GoogleContainerTools/kaniko/pkg/creds"
Expand Down Expand Up @@ -321,6 +322,10 @@ func Run(ctx context.Context, opts options.Options) error {
// In order to allow 'resuming' envbuilder, embed the binary into the image
// if it is being pushed
if opts.PushImage {
// Add exception to the ignore list for the magic image file.
if err := util.AddAllowedPathToDefaultIgnoreList(constants.MagicImage); err != nil {
return xerrors.Errorf("add exe path to ignore list: %w", err)
}
exePath, err := os.Executable()
if err != nil {
return xerrors.Errorf("get exe path: %w", err)
Expand All @@ -331,13 +336,18 @@ func Run(ctx context.Context, opts options.Options) error {
}
// Copy the envbuilder binary into the build context.
buildParams.DockerfileContent += fmt.Sprintf(`
COPY --chmod=0755 %s %s
COPY --chmod=0644 %[1]s %[1]s
COPY --chmod=0755 %[2]s %[2]s
USER root
WORKDIR /
ENTRYPOINT [%q]`, exePath, exePath, exePath)
dst := filepath.Join(buildParams.BuildContext, exePath)
if err := copyFile(exePath, dst); err != nil {
return xerrors.Errorf("copy running binary to build context: %w", err)
ENTRYPOINT [%[2]q]`, constants.MagicImage, exePath)
magicDst := filepath.Join(buildParams.BuildContext, constants.MagicImage)
if err := touchFile(opts.Filesystem, magicDst); err != nil {
return xerrors.Errorf("touch file in build context: %w", err)
}
exeDst := filepath.Join(buildParams.BuildContext, exePath)
if err := copyFile(opts.Filesystem, exePath, exeDst); err != nil {
return xerrors.Errorf("copy file to build context: %w", err)
}
}

Expand All @@ -362,8 +372,9 @@ ENTRYPOINT [%q]`, exePath, exePath, exePath)
stderrWriter, closeStderr := log.Writer(opts.Logger)
defer closeStderr()
build := func() (v1.Image, error) {
_, err := opts.Filesystem.Stat(constants.MagicFile)
if err == nil && opts.SkipRebuild {
_, alreadyBuiltErr := opts.Filesystem.Stat(constants.MagicFile)
_, isImageErr := opts.Filesystem.Stat(constants.MagicImage)
if (alreadyBuiltErr == nil && opts.SkipRebuild) || isImageErr == nil {
endStage := startStage("🏗️ Skipping build because of cache...")
imageRef, err := devcontainer.ImageFromDockerfile(buildParams.DockerfileContent)
if err != nil {
Expand Down Expand Up @@ -1371,24 +1382,43 @@ func maybeDeleteFilesystem(logger log.Func, force bool) error {
return util.DeleteFilesystem()
}

func copyFile(src, dst string) error {
content, err := os.ReadFile(src)
func copyFile(fs billy.Filesystem, src, dst string) error {
from, err := fs.Open(src)
if err != nil {
return fmt.Errorf("read file failed: %w", err)
return fmt.Errorf("open file failed: %w", err)
}
defer from.Close()

err = os.MkdirAll(filepath.Dir(dst), 0o755)
err = fs.MkdirAll(filepath.Dir(dst), 0o755)
if err != nil {
return fmt.Errorf("mkdir all failed: %w", err)
}

err = os.WriteFile(dst, content, 0o644)
to, err := fs.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return fmt.Errorf("open file failed: %w", err)
}
defer to.Close()

_, err = io.Copy(to, from)
if err != nil {
return fmt.Errorf("write file failed: %w", err)
return fmt.Errorf("copy file failed: %w", err)
}
if err := to.Close(); err != nil {
return fmt.Errorf("close file failed: %w", err)
}

return nil
}

func touchFile(fs billy.Filesystem, name string) error {
f, err := fs.Create(name)
if err != nil {
return xerrors.Errorf("create file failed: %w", err)
}
return f.Close()
}

func initCABundle(sslCertBase64 string) ([]byte, error) {
if sslCertBase64 == "" {
return []byte{}, nil
Expand Down