Skip to content

Commit

Permalink
feat(v1): support copy from an image
Browse files Browse the repository at this point in the history
Signed-off-by: Keming <kemingyang@tensorchord.ai>
  • Loading branch information
kemingy committed Aug 14, 2023
1 parent c7bdb3e commit d9e4e76
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
15 changes: 12 additions & 3 deletions envd/api/v1/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@
from typing import Optional


def copy(host_path: str, envd_path: str):
def copy(source: str, target: str, image: Optional[str]):
"""Copy from host path to container path (build time)
Args:
host_path (str): source path in the host machine
envd_path (str): destination path in the envd container
source (str): source path in the host machine or in the ``image``
target (str): destination path in the envd container
image(Optional[str]): image name, if not specified, will use the host
Examples:
```
# copy from host to container
io.copy(source='main.py', target='/home/envd/')
# copy from image to container
io.copy(source='/bin/micromamba', target='/usr/local/bin/micromamba', image='mambaorg/micromamba:1.0.0')
```
"""


Expand Down
2 changes: 1 addition & 1 deletion examples/python-basic/build.envd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def build():
"via",
]
)
io.copy(host_path="./build.envd", envd_path="/")
io.copy("./build.envd", "/")
runtime.command(
commands={
"test": "ls /",
Expand Down
13 changes: 5 additions & 8 deletions pkg/lang/frontend/starlark/v1/io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,16 @@ var Module = &starlarkstruct.Module{

func ruleFuncCopy(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var source, destination starlark.String
var source, destination, image string

if err := starlark.UnpackArgs(ruleCopy, args, kwargs,
"host_path?", &source, "envd_path?", &destination); err != nil {
"source", &source, "target", &destination, "image?", &image); err != nil {
return nil, err
}

sourceStr := source.GoString()
destinationStr := destination.GoString()

logger.Debugf("rule `%s` is invoked, src=%s, dest=%s\n",
ruleCopy, sourceStr, destinationStr)
ir.Copy(sourceStr, destinationStr)
logger.Debugf("rule `%s` is invoked, src=%s, dest=%s, image=%s\n",
ruleCopy, source, destination, image)
ir.Copy(source, destination, image)

return starlark.None, nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/lang/ir/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type RuntimeGraph struct {
type CopyInfo struct {
Source string
Destination string
Image string
}

type MountInfo struct {
Expand Down
3 changes: 2 additions & 1 deletion pkg/lang/ir/v1/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,13 @@ func CondaPackage(deps []string, channel []string, envFile string) error {
return nil
}

func Copy(src, dest string) {
func Copy(src, dest, image string) {
g := DefaultGraph.(*generalGraph)

g.Copy = append(g.Copy, ir.CopyInfo{
Source: src,
Destination: dest,
Image: image,
})
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/lang/ir/v1/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,14 @@ func (g generalGraph) compileCopy(root llb.State) llb.State {
result := root
// Compose the copy command.
for _, c := range g.Copy {
var from llb.State
if c.Image == "" {
from = llb.Local(flag.FlagBuildContext)
} else {
from = llb.Image(c.Image)
}
result = result.File(llb.Copy(
llb.Local(flag.FlagBuildContext), c.Source, c.Destination,
from, c.Source, c.Destination,
&llb.CopyInfo{CreateDestPath: true},
llb.WithUIDGID(g.uid, g.gid)))
}
Expand Down

0 comments on commit d9e4e76

Please sign in to comment.