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: use horust as the dev container supervisor #1051

Merged
merged 8 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 9 additions & 0 deletions base-images/horust.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM rust:bullseye as builder

RUN git clone https://github.com/FedericoPonzi/Horust.git /app --depth=1
kemingy marked this conversation as resolved.
Show resolved Hide resolved
WORKDIR /app
RUN cargo build --release

FROM scratch
COPY --from=builder /app/target/release/horust .
ENTRYPOINT [ "./horust" ]
48 changes: 6 additions & 42 deletions pkg/lang/ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/cockroachdb/errors"
"github.com/moby/buildkit/client/llb"
Expand Down Expand Up @@ -201,48 +200,12 @@ func (g Graph) DefaultCacheImporter() (*string, error) {
return &res, nil
}

func (g Graph) GetEntrypoint(buildContextDir string) ([]string, error) {
func (g *Graph) GetEntrypoint(buildContextDir string) ([]string, error) {
if g.Image != nil {
return g.Entrypoint, nil
}

ep := []string{
"tini",
"--",
"bash",
"-c",
}

template := `set -euo pipefail
/var/envd/bin/envd-sshd --port %d --shell %s &
%s
wait -n`

// Generate jupyter and rstudio server commands.
var customCmd strings.Builder
workingDir := fileutil.EnvdHomeDir(filepath.Base(buildContextDir))
if g.RuntimeDaemon != nil {
for _, command := range g.RuntimeDaemon {
customCmd.WriteString(fmt.Sprintf("%s &\n", strings.Join(command, " ")))
}
}
if g.JupyterConfig != nil {
jupyterCmd := g.generateJupyterCommand(workingDir)
customCmd.WriteString(strings.Join(jupyterCmd, " "))
customCmd.WriteString("\n")
}
if g.RStudioServerConfig != nil {
rstudioCmd := g.generateRStudioCommand(workingDir)
customCmd.WriteString(strings.Join(rstudioCmd, " "))
customCmd.WriteString("\n")
}

cmd := fmt.Sprintf(template,
config.SSHPortInContainer, g.Shell, customCmd.String())
ep = append(ep, cmd)

logrus.WithField("entrypoint", ep).Debug("generate entrypoint")
return ep, nil
g.RuntimeEnviron["ENVD_WORKDIR"] = fileutil.EnvdHomeDir(filepath.Base(buildContextDir))
kemingy marked this conversation as resolved.
Show resolved Hide resolved
return []string{"horust"}, nil
}

func (g Graph) Compile(uid, gid int) (llb.State, error) {
Expand Down Expand Up @@ -297,7 +260,8 @@ func (g Graph) Compile(uid, gid int) (llb.State, error) {
// TODO(gaocegege): Support order-based exec.
run := g.compileRun(copy)
git := g.compileGit(run)
finalStage := g.compileUserOwn(git)
user := g.compileUserOwn(git)
entrypoint := g.compileEntrypoint(user)
g.Writer.Finish()
return finalStage, nil
return entrypoint, nil
}
10 changes: 10 additions & 0 deletions pkg/lang/ir/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (g Graph) generateJupyterCommand(workingDir string) []string {
g.JupyterConfig.Token = "''"
}

// get from env if not set
if len(workingDir) == 0 {
workingDir = "${ENVD_WORKDIR}"
}

cmd := []string{
"python3", "-m", "notebook",
"--ip", "0.0.0.0", "--notebook-dir", workingDir,
Expand All @@ -99,6 +104,11 @@ func (g Graph) generateRStudioCommand(workingDir string) []string {
return nil
}

// get from env if not set
// if len(workingDir) == 0 {
gaocegege marked this conversation as resolved.
Show resolved Hide resolved
// workingDir = "${ENVD_WORKDIR}"
// }

return []string{
// TODO(gaocegege): Remove root permission here.
"sudo",
Expand Down
2 changes: 2 additions & 0 deletions pkg/lang/ir/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ disabled = false

func (g *Graph) compileShell(root llb.State) (llb.State, error) {
if g.Shell == shellZSH {
g.RuntimeEnviron["SHELL"] = "/usr/bin/zsh"
return g.compileZSH(root)
}
g.RuntimeEnviron["SHELL"] = "/usr/bin/bash"
return root, nil
}

Expand Down
82 changes: 82 additions & 0 deletions pkg/lang/ir/supervisor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2022 The envd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ir

import (
"fmt"
"path/filepath"
"strings"

"github.com/moby/buildkit/client/llb"

"github.com/tensorchord/envd/pkg/config"
"github.com/tensorchord/envd/pkg/types"
)

const (
horustTemplate = `
name = "%[1]s"
command = "%[2]s"
kemingy marked this conversation as resolved.
Show resolved Hide resolved
stdout = "/var/logs/%[1]s_stdout.log"
stderr = "/var/logs/%[1]s_stderr.log"
user = "${USER}"
working-directory = "${ENVD_WORKDIR}"

[environment]
keep-env = true
re-export = [ "PATH", "SHELL", "USER", "ENVD_WORKDIR" ]

[restart]
strategy = "on-failure"
backoff = "1s"
attempts = 5

[termination]
wait = "5s"
`
)

func (g Graph) addNewProcess(root llb.State, name, command string) llb.State {
template := fmt.Sprintf(horustTemplate, name, command)
filename := filepath.Join(types.HorustServiceDir, fmt.Sprintf("%s.toml", name))
supervisor := root.File(llb.Mkfile(filename, 0644, []byte(template), llb.WithUIDGID(g.uid, g.gid)))
return supervisor
}

func (g Graph) compileEntrypoint(root llb.State) llb.State {
if g.Image != nil {
return root
}
cmd := fmt.Sprintf("/var/envd/bin/envd-sshd --port %d --shell %s", config.SSHPortInContainer, g.Shell)
entrypoint := g.addNewProcess(root, "sshd", cmd)

if g.RuntimeDaemon != nil {
for i, command := range g.RuntimeDaemon {
entrypoint = g.addNewProcess(entrypoint, fmt.Sprintf("daemon_%d", i), fmt.Sprintf("%s &\n", strings.Join(command, " ")))
}
}

if g.JupyterConfig != nil {
jupyterCmd := g.generateJupyterCommand("")
entrypoint = g.addNewProcess(entrypoint, "jupyter", strings.Join(jupyterCmd, " "))
}

if g.RStudioServerConfig != nil {
rstudioCmd := g.generateRStudioCommand("")
entrypoint = g.addNewProcess(entrypoint, "rstudio", strings.Join(rstudioCmd, " "))
}

return entrypoint
}
12 changes: 11 additions & 1 deletion pkg/lang/ir/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,17 @@ func (g *Graph) compileBase() (llb.State, error) {
if err != nil {
return llb.State{}, errors.Wrap(err, "failed to install conda")
}
return g.compileSshd(condaStage), nil
supervisor := g.installHorust(condaStage)
return g.compileSshd(supervisor), nil
}

func (g Graph) installHorust(root llb.State) llb.State {
horust := root.
File(llb.Copy(llb.Image(types.HorustImage), "/", "/usr/local/bin", llb.WithUIDGID(g.uid, g.gid)),
llb.WithCustomName("[internal] install horust")).
File(llb.Mkdir(types.HorustServiceDir, 0755, llb.WithParents(true), llb.WithUIDGID(g.uid, g.gid))).
File(llb.Mkdir(types.HorustLogDir, 0755, llb.WithParents(true), llb.WithUIDGID(g.uid, g.gid)))
return horust
}

func (g Graph) copySSHKey(root llb.State) (llb.State, error) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/lang/ir/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import (
// compileUserOwn chown related directories
func (g *Graph) compileUserOwn(root llb.State) llb.State {
if g.Image != nil || g.uid == 0 {
g.RuntimeEnviron["USER"] = "root"
return root
}
g.RuntimeEnviron["USER"] = "envd"
if len(g.UserDirectories) == 0 {
return root.User("envd")
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/types/envd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ const DefaultPathEnvUnix = "/opt/conda/envs/envd/bin:/opt/conda/bin:/home/envd/.
// ';' character .
const DefaultPathEnvWindows = system.DefaultPathEnvWindows

const PythonBaseImage = "ubuntu:20.04"
const (
PythonBaseImage = "ubuntu:20.04"
// supervisor
HorustImage = "kemingy/horust:latest"
HorustServiceDir = "/etc/horust/services"
HorustLogDir = "/var/logs"
)

var EnvdSshdImage = fmt.Sprintf(
"tensorchord/envd-sshd-from-scratch:%s",
Expand Down Expand Up @@ -71,7 +77,6 @@ var BaseAptPackage = []string{
"curl",
"openssh-client",
"git",
"tini",
"sudo",
"vim",
"zsh",
Expand Down