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 daemon function to run daemon process in the container #777

Merged
merged 2 commits into from
Aug 16, 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
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"
gaocegege marked this conversation as resolved.
Show resolved Hide resolved

// 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