Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <[email protected]>
  • Loading branch information
gaocegege committed Dec 7, 2022
1 parent f862017 commit ee8eed4
Show file tree
Hide file tree
Showing 74 changed files with 4,534 additions and 459 deletions.
20 changes: 5 additions & 15 deletions examples/python-basic/build.envd
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
def build():
base(os="ubuntu20.04", language="python")
# config.pip_index(url = "https://pypi.tuna.tsinghua.edu.cn/simple")
install.python_packages(
[
"via",
]
)
io.copy(host_path="./build.envd", envd_path="/")
runtime.command(
commands={
"test": "ls /",
}
)
runtime.environ(env={"ENVD_MODE": "DEV"})
config.jupyter()
base(dev=True)
install.conda()
install.python()
install.python_packages(name=["ipython"])
install.apt_packages(name=["ripgrep"])
2 changes: 1 addition & 1 deletion pkg/app/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

"github.com/tensorchord/envd/pkg/envd"
"github.com/tensorchord/envd/pkg/home"
v1 "github.com/tensorchord/envd/pkg/lang/ir/v1"
v1 "github.com/tensorchord/envd/pkg/lang/ir/v0"
"github.com/tensorchord/envd/pkg/ssh"
sshconfig "github.com/tensorchord/envd/pkg/ssh/config"
"github.com/tensorchord/envd/pkg/types"
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/tensorchord/envd/pkg/builder"
"github.com/tensorchord/envd/pkg/envd"
"github.com/tensorchord/envd/pkg/home"
v1 "github.com/tensorchord/envd/pkg/lang/ir/v1"
v1 "github.com/tensorchord/envd/pkg/lang/ir/v0"
"github.com/tensorchord/envd/pkg/ssh"
"github.com/tensorchord/envd/pkg/util/fileutil"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/tensorchord/envd/pkg/app/telemetry"
"github.com/tensorchord/envd/pkg/envd"
"github.com/tensorchord/envd/pkg/home"
v1 "github.com/tensorchord/envd/pkg/lang/ir/v1"
v1 "github.com/tensorchord/envd/pkg/lang/ir/v0"
sshconfig "github.com/tensorchord/envd/pkg/ssh/config"
"github.com/tensorchord/envd/pkg/types"
)
Expand Down
8 changes: 5 additions & 3 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import (
"github.com/tensorchord/envd/pkg/docker"
"github.com/tensorchord/envd/pkg/flag"
"github.com/tensorchord/envd/pkg/home"
starlark "github.com/tensorchord/envd/pkg/lang/frontend/starlark/v1"
"github.com/tensorchord/envd/pkg/lang/frontend/starlark"
starlarkv1 "github.com/tensorchord/envd/pkg/lang/frontend/starlark/v1"
"github.com/tensorchord/envd/pkg/lang/ir"
v1 "github.com/tensorchord/envd/pkg/lang/ir/v1"
"github.com/tensorchord/envd/pkg/progress/progresswriter"
Expand Down Expand Up @@ -139,7 +140,7 @@ func New(ctx context.Context, opt Options) (Builder, error) {
}
b.Client = cli

b.Interpreter = starlark.NewInterpreter(opt.BuildContextDir)
b.Interpreter = starlarkv1.NewInterpreter(opt.BuildContextDir)
return b, nil
}

Expand Down Expand Up @@ -225,8 +226,9 @@ func (b generalBuilder) imageConfig(ctx context.Context) (string, error) {
b.logger.Debugf("final entrypoint: {%s}\n", ep)

env := b.graph.GetEnviron()
user := b.graph.GetUser()

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

mockbuildkitd "github.com/tensorchord/envd/pkg/buildkitd/mock"
"github.com/tensorchord/envd/pkg/home"
mockstarlark "github.com/tensorchord/envd/pkg/lang/frontend/starlark/v1/mock"
v1 "github.com/tensorchord/envd/pkg/lang/ir/v1"
mockstarlark "github.com/tensorchord/envd/pkg/lang/frontend/starlark/v0/mock"
v1 "github.com/tensorchord/envd/pkg/lang/ir/v0"
"github.com/tensorchord/envd/pkg/progress/compileui"
compileuimock "github.com/tensorchord/envd/pkg/progress/compileui/mock"
"github.com/tensorchord/envd/pkg/progress/progresswriter"
Expand Down
4 changes: 2 additions & 2 deletions pkg/builder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ const (
)

func ImageConfigStr(labels map[string]string, ports map[string]struct{},
entrypoint []string, env []string) (string, error) {
entrypoint []string, env []string, user string) (string, error) {
pl := platforms.Normalize(platforms.DefaultSpec())
img := v1.Image{
Config: v1.ImageConfig{
Labels: labels,
User: "envd",
User: user,
WorkingDir: "/",
Env: env,
ExposedPorts: ports,
Expand Down
6 changes: 6 additions & 0 deletions pkg/lang/frontend/starlark/interpreter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package starlark

type Interpreter interface {
Eval(script string) (interface{}, error)
ExecFile(filename string, funcname string) (interface{}, error)
}
20 changes: 20 additions & 0 deletions pkg/lang/frontend/starlark/v0/builtin/builtin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 builtin

const (
// BuildContextDir is the name of the directory that contains the build context.
BuildContextDir = "_build_context_dir"
)
228 changes: 228 additions & 0 deletions pkg/lang/frontend/starlark/v0/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// 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 config

import (
"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"

ir "github.com/tensorchord/envd/pkg/lang/ir/v0"
"github.com/tensorchord/envd/pkg/util/starlarkutil"
)

var (
logger = logrus.WithField("frontend", "starlark")
)

var Module = &starlarkstruct.Module{
Name: "config",
Members: starlark.StringDict{
"apt_source": starlark.NewBuiltin(ruleUbuntuAptSource, ruleFuncUbuntuAptSource),
"gpu": starlark.NewBuiltin(ruleGPU, ruleFuncGPU),
"jupyter": starlark.NewBuiltin(ruleJupyter, ruleFuncJupyter),
"cran_mirror": starlark.NewBuiltin(
ruleCRANMirror, ruleFuncCRANMirror),
"pip_index": starlark.NewBuiltin(
rulePyPIIndex, ruleFuncPyPIIndex),
"conda_channel": starlark.NewBuiltin(
ruleCondaChannel, ruleFuncCondaChannel),
"julia_pkg_server": starlark.NewBuiltin(
ruleJuliaPackageServer, ruleFuncJuliaPackageServer),
"rstudio_server": starlark.NewBuiltin(ruleRStudioServer, ruleFuncRStudioServer),
"entrypoint": starlark.NewBuiltin(ruleEntrypoint, ruleFuncEntrypoint),
"repo": starlark.NewBuiltin(ruleRepo, ruleFuncRepo),
},
}

func ruleFuncGPU(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var numGPUs starlark.Int

if err := starlark.UnpackArgs(ruleGPU, args, kwargs,
"count?", &numGPUs); err != nil {
return nil, err
}

numGPUsInt, ok := numGPUs.Int64()
if ok {
ir.GPU(int(numGPUsInt))
logger.Debugf("Using %d GPUs", int(numGPUsInt))
} else {
logger.Debugf("Failed to convert gpu count to int64")
}
return starlark.None, nil
}

func ruleFuncJupyter(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var token starlark.String
var port starlark.Int

if err := starlark.UnpackArgs(ruleJupyter, args, kwargs,
"token?", &token, "port?", &port); err != nil {
return nil, err
}

pwdStr := token.GoString()

portInt, ok := port.Int64()
if !ok {
return nil, errors.New("port must be an integer")
}
logger.Debugf("rule `%s` is invoked, password=%s, port=%d",
ruleJupyter, pwdStr, portInt)
if err := ir.Jupyter(pwdStr, portInt); err != nil {
return nil, err
}

return starlark.None, nil
}

func ruleFuncPyPIIndex(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var url, extraURL starlark.String

if err := starlark.UnpackArgs(rulePyPIIndex, args, kwargs,
"url?", &url, "extra_url?", &extraURL); err != nil {
return nil, err
}

indexStr := url.GoString()
extraIndexStr := extraURL.GoString()

logger.Debugf("rule `%s` is invoked, index=%s, extraIndex=%s",
rulePyPIIndex, indexStr, extraIndexStr)
if err := ir.PyPIIndex(indexStr, extraIndexStr); err != nil {
return nil, err
}

return starlark.None, nil
}

func ruleFuncCRANMirror(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var url starlark.String

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

urlStr := url.GoString()

logger.Debugf("rule `%s` is invoked, url=%s", ruleCRANMirror, urlStr)
if err := ir.CRANMirror(urlStr); err != nil {
return nil, err
}
return starlark.None, nil
}

func ruleFuncJuliaPackageServer(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var url starlark.String

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

urlStr := url.GoString()

logger.Debugf("rule `%s` is invoked, url=%s", ruleJuliaPackageServer, urlStr)
if err := ir.JuliaPackageServer(urlStr); err != nil {
return nil, err
}
return starlark.None, nil
}

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

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

sourceStr := source.GoString()

logger.Debugf("rule `%s` is invoked, source=%s", ruleUbuntuAptSource, sourceStr)
if err := ir.UbuntuAPT(sourceStr); err != nil {
return nil, err
}

return starlark.None, nil
}

func ruleFuncRStudioServer(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if err := ir.RStudioServer(); err != nil {
return nil, err
}

return starlark.None, nil
}

func ruleFuncCondaChannel(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var channel string
var useMamba bool

if err := starlark.UnpackArgs(ruleCondaChannel, args, kwargs,
"channel?", &channel, "use_mamba?", &useMamba); err != nil {
return nil, err
}

logger.Debugf("rule `%s` is invoked, channel=%s, use_mamba=%t\n",
ruleCondaChannel, channel, useMamba)
if err := ir.CondaChannel(channel, useMamba); err != nil {
return nil, err
}

return starlark.None, nil
}

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

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

argList, err := starlarkutil.ToStringSlice(argv)
if err != nil {
return nil, err
}

logger.Debugf("user defined entrypoints: {%s}\n", argList)
ir.Entrypoint(argList)
return starlark.None, nil
}

func ruleFuncRepo(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var url, description string

if err := starlark.UnpackArgs(ruleRepo, args, kwargs, "url", &url, "description?", &description); err != nil {
return nil, err
}

logger.Debugf("repo info: url=%s, description=%s", url, description)
ir.Repo(url, description)
return starlark.None, nil
}
28 changes: 28 additions & 0 deletions pkg/lang/frontend/starlark/v0/config/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 config

const (
ruleUbuntuAptSource = "config.apt_source"
rulePyPIIndex = "config.pip_index"
ruleCRANMirror = "config.cran_mirror"
ruleJupyter = "config.jupyter"
ruleCondaChannel = "config.conda_channel"
ruleGPU = "config.gpu"
ruleJuliaPackageServer = "config.julia_pkg_server"
ruleRStudioServer = "config.rstudio_server"
ruleEntrypoint = "config.entrypoint"
ruleRepo = "config.repo"
)
Loading

0 comments on commit ee8eed4

Please sign in to comment.