Skip to content

Commit

Permalink
feat(lang): add daemon function to run daemon process in the container (
Browse files Browse the repository at this point in the history
#777)

* add daemon

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* change to list of list string

Signed-off-by: Keming <kemingyang@tensorchord.ai>

Signed-off-by: Keming <kemingyang@tensorchord.ai>
  • Loading branch information
kemingy committed Aug 16, 2022
1 parent 4d0b0c9 commit 7c2fed6
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 3 deletions.
8 changes: 8 additions & 0 deletions examples/python-basic/build.envd
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
def jupyter_lab():
install.python_packages(["jupyterlab"])
runtime.daemon(commands=[
["jupyter-lab", "--port", "8080"],
])


def build():
base(os="ubuntu20.04", language="python")
#config.pip_index(url = "https://pypi.tuna.tsinghua.edu.cn/simple")
Expand All @@ -8,3 +15,4 @@ def build():
runtime.command(commands={
"test": "ls /",
})
jupyter_lab()
2 changes: 1 addition & 1 deletion pkg/builder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func ImageConfigStr(labels map[string]string,
// DefaultPathEnvUnix is unix style list of directories to search for
// executables. Each directory is separated from the next by a colon
// ':' character .
const DefaultPathEnvUnix = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/conda/bin:/usr/local/julia/bin"
const DefaultPathEnvUnix = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/conda/bin:/opt/conda/envs/envd/bin/:/usr/local/julia/bin"

// DefaultPathEnvWindows is windows style list of directories to search for
// executables. Each directory is separated from the next by a colon
Expand Down
1 change: 1 addition & 0 deletions pkg/lang/frontend/starlark/runtime/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ package runtime

const (
ruleCommand = "runtime.command"
ruleDaemon = "runtime.daemon"
)
30 changes: 30 additions & 0 deletions pkg/lang/frontend/starlark/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var Module = &starlarkstruct.Module{
Name: "runtime",
Members: starlark.StringDict{
"command": starlark.NewBuiltin(ruleCommand, ruleFuncCommand),
"daemon": starlark.NewBuiltin(ruleDaemon, ruleFuncDaemon),
},
}

Expand Down Expand Up @@ -60,3 +61,32 @@ func ruleFuncCommand(thread *starlark.Thread, _ *starlark.Builtin,
ir.RuntimeCommands(commandsMap)
return starlark.None, nil
}

func ruleFuncDaemon(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var commands *starlark.List

if err := starlark.UnpackArgs(ruleDaemon, args, kwargs, "commands", &commands); err != nil {
return nil, err
}

commandList := [][]string{}
if commands != nil {
for i := 0; i < commands.Len(); i++ {
args, ok := commands.Index(i).(*starlark.List)
if !ok {
logger.Warnf("cannot parse %s into a list of string", commands.Index(i).String())
continue
}
argList := []string{}
for j := 0; j < args.Len(); j++ {
argList = append(argList, args.Index(j).(starlark.String).GoString())
}
commandList = append(commandList, argList)
}

logger.Debugf("rule `%s` is invoked, commands=%v", ruleDaemon, commandList)
ir.RuntimeDaemon(commandList)
}
return starlark.None, nil
}
8 changes: 6 additions & 2 deletions pkg/lang/ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,18 @@ wait -n`

// Generate jupyter and rstudio server commands.
var customCmd strings.Builder
workingDir := filepath.Join("/home/envd", filepath.Base(buildContextDir))
if g.RuntimeDaemon != nil {
for _, command := range g.RuntimeDaemon {
customCmd.WriteString(fmt.Sprintf("%s -c %s &\n", g.Shell, strings.Join(command, " ")))
}
}
if g.JupyterConfig != nil {
workingDir := filepath.Join("/home/envd", filepath.Base(buildContextDir))
jupyterCmd := g.generateJupyterCommand(workingDir)
customCmd.WriteString(strings.Join(jupyterCmd, " "))
customCmd.WriteString("\n")
}
if g.RStudioServerConfig != nil {
workingDir := filepath.Join("/home/envd", filepath.Base(buildContextDir))
rstudioCmd := g.generateRStudioCommand(workingDir)
customCmd.WriteString(strings.Join(rstudioCmd, " "))
customCmd.WriteString("\n")
Expand Down
4 changes: 4 additions & 0 deletions pkg/lang/ir/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,7 @@ func RuntimeCommands(commands map[string]string) {
DefaultGraph.RuntimeCommands[k] = v
}
}

func RuntimeDaemon(commands [][]string) {
DefaultGraph.RuntimeDaemon = append(DefaultGraph.RuntimeDaemon, commands...)
}
1 change: 1 addition & 0 deletions pkg/lang/ir/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Graph struct {
Mount []MountInfo
Entrypoint []string
RuntimeCommands map[string]string
RuntimeDaemon [][]string

*JupyterConfig
*GitConfig
Expand Down

0 comments on commit 7c2fed6

Please sign in to comment.