Skip to content

Commit

Permalink
feat: add envd init (#514)
Browse files Browse the repository at this point in the history
* add envd init

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* add julia

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>
  • Loading branch information
VoVAllen committed Jun 27, 2022
1 parent c0ea0ef commit fde448a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func New() EnvdApp {
CommandBuild,
CommandDestroy,
CommandGet,
CommandInit,
CommandPause,
CommandPrune,
CommandResume,
Expand Down
79 changes: 79 additions & 0 deletions pkg/app/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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 app

import (
"embed"
"io/ioutil"
"strings"

"github.com/cockroachdb/errors"
"github.com/tensorchord/envd/pkg/util/fileutil"
cli "github.com/urfave/cli/v2"
)

//go:embed template
var templatef embed.FS

var CommandInit = &cli.Command{
Name: "init",
Aliases: []string{"i"},
Usage: "Initializes the current directory with the build.envd file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "lang",
Usage: "language usage. Support Python, R",
Aliases: []string{"l"},
Required: true,
},
},
Action: initCommand,
}

func isValidLang(lang string) bool {
switch lang {
case
"python",
"r",
"julia":
return true
}
return false
}

func initCommand(clicontext *cli.Context) error {
lang := strings.ToLower(clicontext.String("lang"))
if !isValidLang(lang) {
return errors.Errorf("invalid language %s", lang)
}

exists, err := fileutil.FileExists("build.envd")
if err != nil {
return err
}
if exists {
return errors.Errorf("build.envd already exists")
}

buildEnvdContent, err := templatef.ReadFile("template/" + lang + ".envd")
if err != nil {
return err
}
err = ioutil.WriteFile("build.envd", buildEnvdContent, 0644)
if err != nil {
return errors.Wrapf(err, "Failed to create build.envd")
}
return nil
}
13 changes: 13 additions & 0 deletions pkg/app/template/julia.envd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def build():
# Use ubuntu20.04 as base image and install julia
base(os="ubuntu20.04", language="julia")
# Uncomment line below to enable Pypi mirror
# config.julia_pkg_server(url="https://mirrors.tuna.tsinghua.edu.cn/julia")

# Add the packages you are using here
install.julia_packages([
"Example"
])

# Select the shell environment you like
shell("zsh")
14 changes: 14 additions & 0 deletions pkg/app/template/python.envd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def build():
# Use ubuntu20.04 as base image and install python
base(os="ubuntu20.04", language="python3")
# Uncomment line below to enable Pypi mirror
# config.pip_index(url = "https://pypi.tuna.tsinghua.edu.cn/simple")

# Add the packages you are using here
install.python_packages(["numpy"])

# Select the shell environment you like
shell("zsh")

# Setup jupyter notebook
config.jupyter(password="", port=8888)
12 changes: 12 additions & 0 deletions pkg/app/template/r.envd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def build():
# Use ubuntu20.04 as base image and install r
base(os="ubuntu20.04", language="r")

# Add the packages you are using here
install.r_packages([
"remotes",
"rlang",
])

# Select the shell environment you like
shell("zsh")

0 comments on commit fde448a

Please sign in to comment.