Skip to content

Commit

Permalink
feat(lang): Support image in base (#595)
Browse files Browse the repository at this point in the history
feat(lang): Support base image customization

Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege authored Jul 13, 2022
1 parent b467279 commit 9b3fbe3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
5 changes: 5 additions & 0 deletions examples/custom-image/build.envd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def build():
base(language="python", image="python:3.8")
install.python_packages(name=[
"ormb",
])
11 changes: 6 additions & 5 deletions pkg/lang/frontend/starlark/universe/universe.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,21 @@ func RegisterBuildContext(buildContextDir string) {

func ruleFuncBase(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var os, language starlark.String
var os, language, image starlark.String

if err := starlark.UnpackArgs(ruleBase, args, kwargs,
"os?", &os, "language?", &language); err != nil {
"os?", &os, "language?", &language, "image?", &image); err != nil {
return nil, err
}

osStr := os.GoString()
langStr := language.GoString()
imageStr := image.GoString()

logger.Debugf("rule `%s` is invoked, os=%s, language=%s",
ruleBase, osStr, langStr)
logger.Debugf("rule `%s` is invoked, os=%s, language=%s, image=%s",
ruleBase, osStr, langStr, imageStr)

err := ir.Base(osStr, langStr)
err := ir.Base(osStr, langStr, imageStr)
return starlark.None, err
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/lang/ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ func (g Graph) ExposedPorts() (map[string]struct{}, error) {
}

func (g Graph) Entrypoint(buildContextDir string) ([]string, error) {
// Do not set entrypoint if the image is customized.
if g.Image != nil {
return []string{}, nil
}

ep := []string{
"tini",
"--",
Expand Down
5 changes: 4 additions & 1 deletion pkg/lang/ir/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/tensorchord/envd/pkg/lang/ir/parser"
)

func Base(os, language string) error {
func Base(os, language, image string) error {
l, version, err := parseLanguage(language)
if err != nil {
return err
Expand All @@ -31,6 +31,9 @@ func Base(os, language string) error {
Version: version,
}
DefaultGraph.OS = os
if image != "" {
DefaultGraph.Image = &image
}
return nil
}

Expand Down
14 changes: 9 additions & 5 deletions pkg/lang/ir/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ func (g *Graph) compileBase() llb.State {
logger.Debug("compile base image")

var base llb.State
if g.CUDA == nil && g.CUDNN == nil {
if g.Image != nil {
base = llb.Image(*g.Image)
} else if g.CUDA == nil && g.CUDNN == nil {
switch g.Language.Name {
case "r":
base = llb.Image("docker.io/tensorchord/r-base:4.2")
Expand Down Expand Up @@ -134,7 +136,7 @@ func (g *Graph) compileBase() llb.State {
llb.WithCustomName("[internal] set root home dir to /home/envd")).
Run(llb.Shlex("sed -i \"s/envd:x:1001/envd:x:0/g\" /etc/group"),
llb.WithCustomName("[internal] set envd group to 0 as root group"))
if g.Language.Name == "python" {
if g.Image == nil && g.Language.Name == "python" {
res = res.Run(llb.Shlex("chown -R root:root /opt/conda"),
llb.WithCustomName("[internal] configure user permissions"))
}
Expand All @@ -148,7 +150,7 @@ func (g *Graph) compileBase() llb.State {
llb.WithCustomName("[internal] add user envd to sudoers")).
Run(llb.Shlex("chown -R envd:envd /usr/local/lib"),
llb.WithCustomName("[internal] configure user permissions"))
if g.Language.Name == "python" {
if g.Image == nil && g.Language.Name == "python" {
res = res.Run(llb.Shlex("chown -R envd:envd /opt/conda"),
llb.WithCustomName("[internal] configure user permissions"))
}
Expand All @@ -157,15 +159,17 @@ func (g *Graph) compileBase() llb.State {
}

func (g Graph) copySSHKey(root llb.State) (llb.State, error) {
// TODO(gaocegege): Remove global var ssh image.
public := DefaultGraph.PublicKeyPath
bdat, err := os.ReadFile(public)
dat := strings.TrimSuffix(string(bdat), "\n")
if err != nil {
return llb.State{}, errors.Wrap(err, "Cannot read public SSH key")
}
run := root.
File(llb.Mkdir("/var/envd", 0755, llb.WithParents(true),
llb.WithUIDGID(g.uid, g.gid))).
File(llb.Mkfile(config.ContainerAuthorizedKeysPath,
0644, []byte(dat+" envd"), llb.WithUIDGID(g.uid, g.gid)), llb.WithCustomName("install ssh keys"))
0644, []byte(dat+" envd"), llb.WithUIDGID(g.uid, g.gid)),
llb.WithCustomName("install ssh keys"))
return run, nil
}
1 change: 1 addition & 0 deletions pkg/lang/ir/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Graph struct {

OS string
Language
Image *string

Shell string
CUDA *string
Expand Down

0 comments on commit 9b3fbe3

Please sign in to comment.