Skip to content

Commit

Permalink
feat: inherit the workdir from base image for non-dev env (#1743)
Browse files Browse the repository at this point in the history
Signed-off-by: Keming <[email protected]>
  • Loading branch information
kemingy authored Aug 15, 2023
1 parent a4fe97a commit 3bcce05
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ func (b generalBuilder) imageConfig(ctx context.Context) (string, error) {
env := b.graph.GetEnviron()
user := b.graph.GetUser()
platform := b.graph.GetPlatform()
workingDir := b.graph.GetWorkingDir()

data, err := ImageConfigStr(labels, ports, ep, env, user, platform)
data, err := ImageConfigStr(labels, ports, ep, env, user, workingDir, platform)
if err != nil {
return "", errors.Wrap(err, "failed to get image config")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/builder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ const (
)

func ImageConfigStr(labels map[string]string, ports map[string]struct{},
entrypoint []string, env []string, user string, platform *ocispecs.Platform) (string, error) {
entrypoint []string, env []string, user, workingDir string, platform *ocispecs.Platform) (string, error) {
img := ocispecs.Image{
Config: ocispecs.ImageConfig{
Labels: labels,
User: user,
WorkingDir: "/",
WorkingDir: workingDir,
Env: env,
ExposedPorts: ports,
Entrypoint: entrypoint,
Expand Down
1 change: 1 addition & 0 deletions pkg/lang/ir/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ type graphVisitor interface {
GetRuntimeCommands() map[string]string
GetUser() string
GetPlatform() *ocispecs.Platform
GetWorkingDir() string
}
5 changes: 5 additions & 0 deletions pkg/lang/ir/v0/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func NewGraph() ir.Graph {
CondaConfig: conda,
RuntimeGraph: runtimeGraph,
Platform: &ocispecs.Platform{},
WorkingDir: "/",
}
}

Expand Down Expand Up @@ -112,6 +113,10 @@ func (g generalGraph) GetPlatform() *ocispecs.Platform {
return g.Platform
}

func (g generalGraph) GetWorkingDir() string {
return g.WorkingDir
}

func (g *generalGraph) Compile(ctx context.Context, envPath string, pub string, platform *ocispecs.Platform, progressMode string) (*llb.Definition, error) {
w, err := compileui.New(ctx, os.Stdout, progressMode)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/lang/ir/v0/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ func (g *generalGraph) compileBase() (llb.State, error) {
base = g.compileCUDAPackages("nvidia/cuda")
}

g.WorkingDir = g.getWorkingDir()

// Install conda first.
condaStage := g.installConda(base)
supervisor := g.installHorust(condaStage)
Expand Down
3 changes: 3 additions & 0 deletions pkg/lang/ir/v0/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ type generalGraph struct {
EnvironmentName string
// EnvironmentPath is the full path of this environment.
EnvironmentPath string
// WorkingDir is the working directory of this environment.
// This only affect the `WorkingDir` in the image config.
WorkingDir string

ir.RuntimeGraph

Expand Down
5 changes: 5 additions & 0 deletions pkg/lang/ir/v1/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func NewGraph() ir.Graph {
Shell: shellBASH,
RuntimeGraph: runtimeGraph,
Platform: &ocispecs.Platform{},
WorkingDir: "/",
}
}

Expand Down Expand Up @@ -109,6 +110,10 @@ func (g generalGraph) GetPlatform() *ocispecs.Platform {
return g.Platform
}

func (g generalGraph) GetWorkingDir() string {
return g.WorkingDir
}

func (g *generalGraph) Compile(ctx context.Context, envPath string, pub string, platform *ocispecs.Platform, progressMode string) (*llb.Definition, error) {
w, err := compileui.New(ctx, os.Stdout, progressMode)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/lang/ir/v1/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ func (g generalGraph) compilePyPIPackages(root llb.State) llb.State {
if g.RequirementsFile != nil {
logrus.WithField("file", *g.RequirementsFile).
Debug("Configure pip install requirements statements")
root = root.Dir(g.getWorkingDir())
dependencies, safeToCopy := g.IsRequirementsFileSafeToCopyContent()
logrus.WithField("safeToCopy", safeToCopy).WithField("dependencies", dependencies).
Debug("Is requirements file safe to copy")
Expand All @@ -143,7 +142,7 @@ func (g generalGraph) compilePyPIPackages(root llb.State) llb.State {
llb.WithCustomNamef("[internal] pip install from %s with %s", *g.RequirementsFile, strings.Join(dependencies, " ")),
).Root()
} else {
run := root.
run := root.Dir(g.getWorkingDir()).
Run(llb.Shlexf("python -m pip install -r %s", *g.RequirementsFile),
llb.WithCustomNamef("[internal] pip install -r %s", *g.RequirementsFile))
run.AddMount(cacheDir, cache,
Expand Down
18 changes: 18 additions & 0 deletions pkg/lang/ir/v1/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,22 @@ func (g *generalGraph) compileLanguage(root llb.State) (llb.State, error) {
langs := []llb.State{}
lang := root
var err error

if g.DisableMergeOp {
for _, language := range g.Languages {
switch language.Name {
case "python":
root, err = g.installPython(root)
case "r":
rSrc := g.compileRLang(root)
root = g.installRLang(rSrc)
case "julia":
root = g.installJulia(root)
}
}
return root, err
}

for _, language := range g.Languages {
switch language.Name {
case "python":
Expand Down Expand Up @@ -396,9 +412,11 @@ func (g *generalGraph) compileBaseImage() (llb.State, error) {
g.Entrypoint = config.Entrypoint
}
g.User = config.User
g.WorkingDir = config.WorkingDir
} else {
// for dev mode, we will create an `envd` user
g.User = ""
g.WorkingDir = g.getWorkingDir()
}
return base, nil
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/lang/ir/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ type generalGraph struct {
EnvironmentName string
// EnvironmentPath is the full path of this environment.
EnvironmentPath string
// WorkingDir is the working directory of this environment.
// This only affect the `WorkingDir` in the image config.
WorkingDir string

// (v1) disable `merge` op for `moby` builder
// check https://github.com/tensorchord/envd/issues/1693
Expand Down

0 comments on commit 3bcce05

Please sign in to comment.