Skip to content
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

feat(lang): Add default conda pkg cache #705

Merged
merged 1 commit into from
Aug 2, 2022
Merged
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
28 changes: 20 additions & 8 deletions pkg/lang/ir/conda.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ func (g Graph) compileCondaPackages(root llb.State) llb.State {
}

cacheDir := "/opt/conda/pkgs"
// Create the cache directory to the container. see issue #582
root = g.CompileCacheDir(root, cacheDir)
// Refer to https://github.com/moby/buildkit/blob/31054718bf775bf32d1376fe1f3611985f837584/frontend/dockerfile/dockerfile2llb/convert_runmount.go#L46
cache := root.File(llb.Mkdir("/cache-conda",
0755, llb.WithParents(true), llb.WithUIDGID(g.uid, g.gid)),
llb.WithCustomName("[internal] setting conda cache mount permissions"))

// Compose the package install command.
var sb strings.Builder
Expand All @@ -69,20 +71,27 @@ func (g Graph) compileCondaPackages(root llb.State) llb.State {

cmd := sb.String()
root = llb.User("envd")(root)
// Refer to https://github.com/moby/buildkit/blob/31054718bf775bf32d1376fe1f3611985f837584/frontend/dockerfile/dockerfile2llb/convert_runmount.go#L46
cache := root.File(llb.Mkdir("/cache",
0755, llb.WithParents(true), llb.WithUIDGID(g.uid, g.gid)),
llb.WithCustomName("[internal] setting conda cache mount permissions"))

run := root.
Run(llb.Shlex(cmd), llb.WithCustomNamef("conda install %s",
strings.Join(g.CondaPackages, " ")))
run.AddMount(cacheDir, cache,
llb.AsPersistentCacheDir(g.CacheID(cacheDir), llb.CacheMountShared), llb.SourcePath("/cache"))
llb.AsPersistentCacheDir(g.CacheID(cacheDir), llb.CacheMountShared), llb.SourcePath("/cache-conda"))
return run.Root()
}

func (g Graph) setCondaENV(root llb.State) llb.State {
func (g Graph) compileCondaEnvironment(root llb.State) llb.State {
root = llb.User("envd")(root)

cacheDir := "/opt/conda/pkgs"
// Create the cache directory to the container. see issue #582
root = g.CompileCacheDir(root, cacheDir)

// Refer to https://github.com/moby/buildkit/blob/31054718bf775bf32d1376fe1f3611985f837584/frontend/dockerfile/dockerfile2llb/convert_runmount.go#L46
cache := root.File(llb.Mkdir("/cache-conda",
0755, llb.WithParents(true), llb.WithUIDGID(g.uid, g.gid)),
llb.WithCustomName("[internal] setting conda cache mount permissions"))

// Always init bash since we will use it to create jupyter notebook service.
run := root.Run(llb.Shlex("bash -c \"/opt/conda/bin/conda init bash\""), llb.WithCustomName("[internal] initialize conda bash environment"))

Expand All @@ -92,8 +101,11 @@ func (g Graph) setCondaENV(root llb.State) llb.State {
}
cmd := fmt.Sprintf(
"bash -c \"/opt/conda/bin/conda create -n envd python=%s\"", pythonVersion)

// Create a conda environment.
run = run.Run(llb.Shlex(cmd), llb.WithCustomName("[internal] create conda environment"))
run.AddMount(cacheDir, cache,
llb.AsPersistentCacheDir(g.CacheID(cacheDir), llb.CacheMountShared), llb.SourcePath("/cache-conda"))

switch g.Shell {
case shellBASH:
Expand Down
2 changes: 1 addition & 1 deletion pkg/lang/ir/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (g Graph) compilePython(aptStage llb.State) (llb.State, error) {
return llb.State{}, errors.Wrap(err, "failed to compile shell")
}

condaEnvStage := g.setCondaENV(shellStage)
condaEnvStage := g.compileCondaEnvironment(shellStage)

condaStage := llb.Diff(builtinSystemStage,
g.compileCondaPackages(condaEnvStage),
Expand Down