Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 15 additions & 1 deletion e2e/language/rust_test.go → e2e/language/other_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
)

var _ = Describe("rust", Ordered, func() {
exampleName := "rust"
testcase := "e2e"

Describe("Should install rust lang successfully", func() {
exampleName := "rust"
e := e2e.NewExample(e2e.BuildContextDirWithName(exampleName), testcase)
BeforeAll(e.BuildImage(true))
BeforeEach(e.RunContainer())
Expand All @@ -35,4 +36,17 @@ var _ = Describe("rust", Ordered, func() {
})
AfterEach(e.DestroyContainer())
})

Describe("Should install golang successfully", func() {
exampleName := "golang"
e := e2e.NewExample(e2e.BuildContextDirWithName(exampleName), testcase)
BeforeAll(e.BuildImage(true))
BeforeEach(e.RunContainer())
It("Should have go installed", func() {
res, err := e.ExecRuntimeCommand("version")
Expect(err).To(BeNil())
Expect(res).To(ContainSubstring("go version"))
})
AfterEach(e.DestroyContainer())
})
})
12 changes: 12 additions & 0 deletions e2e/language/testdata/golang/build.envd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# syntax=v1


def build():
base(dev=True)
install.go()
shell("fish")
runtime.command(
commands={
"version": "go version",
}
)
8 changes: 8 additions & 0 deletions envd/api/v1/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ def rust(version: Optional[str] = None):
"""


def go(version: Optional[str] = "1.25.3"):
"""Install Go programming language.

Args:
version (Optional[str]): Go version, such as '1.25.3'.
"""


def apt_packages(name: Sequence[str] = ()):
"""Install package using the system package manager (apt on Ubuntu).

Expand Down
1 change: 1 addition & 0 deletions pkg/lang/frontend/starlark/v1/install/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
ruleRLang = "install.r_lang"
ruleJulia = "install.julia"
ruleRust = "install.rust"
ruleGo = "install.go"

// packages
ruleSystemPackage = "install.apt_packages"
Expand Down
14 changes: 14 additions & 0 deletions pkg/lang/frontend/starlark/v1/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var Module = &starlarkstruct.Module{
"r_lang": starlark.NewBuiltin(ruleRLang, ruleFuncRLang),
"julia": starlark.NewBuiltin(ruleJulia, ruleFuncJulia),
"rust": starlark.NewBuiltin(ruleRust, ruleFuncRust),
"go": starlark.NewBuiltin(ruleGo, ruleFuncGo),
// packages
"apt_packages": starlark.NewBuiltin(ruleSystemPackage, ruleFuncSystemPackage),
"python_packages": starlark.NewBuiltin(rulePyPIPackage, ruleFuncPyPIPackage),
Expand Down Expand Up @@ -138,6 +139,19 @@ func ruleFuncRust(thread *starlark.Thread, _ *starlark.Builtin,
return starlark.None, nil
}

func ruleFuncGo(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
logger.Debugf("rule `%s` is invoked", ruleGo)

var version string
if err := starlark.UnpackArgs(ruleGo, args, kwargs, "version?", &version); err != nil {
return nil, err
}

ir.Golang(version)
return starlark.None, nil
}

func ruleFuncPyPIPackage(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var name *starlark.List
Expand Down
48 changes: 48 additions & 0 deletions pkg/lang/ir/v1/golang.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2025 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 v1

import (
"github.com/moby/buildkit/client/llb"
)

const (
golangDefaultVersion = "1.25.3"
golangFilePath = "/tmp/golang.linux.tar.gz"
golangHomeBin = "/usr/local/go/bin"
)

func (g *generalGraph) installGolang(root llb.State, version *string) llb.State {
goVersion := golangDefaultVersion
if version != nil {
goVersion = *version
}

base := llb.Image(builderImage)
builder := base.Run(
llb.Shlexf(`sh -c "wget -qO %s https://go.dev/dl/go%s.linux-$(uname -m | sed -e 's/x86_64/amd64/' -e 's/aarch64/arm64/').tar.gz"`, golangFilePath, goVersion),
llb.WithCustomNamef("[internal] download go %s", goVersion),
).Root()

root = root.File(
llb.Copy(builder, golangFilePath, golangFilePath),
llb.WithCustomNamef("[internal] prepare go %s", goVersion),
).Run(
llb.Shlexf(`sh -c "tar -C /usr/local -xzf %[1]s && rm %[1]s"`, golangFilePath),
llb.WithCustomNamef("[internal] install go %s", goVersion),
).Root()
g.RuntimeEnvPaths = append(g.RuntimeEnvPaths, golangHomeBin)
return root
}
10 changes: 10 additions & 0 deletions pkg/lang/ir/v1/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ func Rust(version string) {
g.Languages = append(g.Languages, rust)
}

func Golang(version string) {
g := DefaultGraph.(*generalGraph)

golang := ir.Language{Name: "go"}
if len(version) > 0 {
golang.Version = &version
}
g.Languages = append(g.Languages, golang)
}

func PyPIPackage(deps []string, requirementsFile string, wheels []string) error {
g := DefaultGraph.(*generalGraph)

Expand Down
21 changes: 10 additions & 11 deletions pkg/lang/ir/v1/rust.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,31 @@ import (
)

const (
RustDefaultVersion = "latest"
RustUpInitFilePath = "/tmp/rustup-init.sh"
CargoHomeDir = "/opt/rust"
CargoHomeBin = "/opt/rust/bin"
rustUpInitFilePath = "/tmp/rustup-init.sh"
cargoHomeDir = "/opt/rust"
cargoHomeBin = "/opt/rust/bin"
)

func (g *generalGraph) installRust(root llb.State, version *string) llb.State {
base := llb.Image(builderImage)
builder := base.Run(
llb.Shlexf(`sh -c "curl --proto '=https' --tlsv1.2 -sSf -o %s https://sh.rustup.rs"`, RustUpInitFilePath),
llb.Shlexf(`sh -c "curl --proto '=https' --tlsv1.2 -sSf -o %s https://sh.rustup.rs"`, rustUpInitFilePath),
llb.WithCustomName("[internal] download rustup-init.sh"),
).Root()
root = root.File(
llb.Copy(builder, RustUpInitFilePath, RustUpInitFilePath),
llb.Copy(builder, rustUpInitFilePath, rustUpInitFilePath),
llb.WithCustomName("[internal] copy the rustup-init.sh"),
)
if version != nil {
root = root.AddEnv("RUSTUP_VERSION", *version)
}
root = root.AddEnv("CARGO_HOME", CargoHomeDir).File(
llb.Mkdir(CargoHomeDir, 0755, llb.WithParents(true), llb.WithUIDGID(g.uid, g.gid)),
llb.WithCustomNamef("[internal] create cargo dir: %s", CargoHomeDir),
root = root.AddEnv("CARGO_HOME", cargoHomeDir).File(
llb.Mkdir(cargoHomeDir, 0755, llb.WithParents(true), llb.WithUIDGID(g.uid, g.gid)),
llb.WithCustomNamef("[internal] create cargo dir: %s", cargoHomeDir),
).Run(
llb.Shlexf(`sh -c "sh %[1]s -y -q --no-modify-path && rm %[1]s"`, RustUpInitFilePath),
llb.Shlexf(`sh -c "sh %[1]s -y -q --no-modify-path && rm %[1]s"`, rustUpInitFilePath),
llb.WithCustomName("[internal] install rust"),
).Root()
g.RuntimeEnvPaths = append(g.RuntimeEnvPaths, CargoHomeBin)
g.RuntimeEnvPaths = append(g.RuntimeEnvPaths, cargoHomeBin)
return root
}
2 changes: 2 additions & 0 deletions pkg/lang/ir/v1/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ func (g *generalGraph) compileLanguage(root llb.State) (llb.State, error) {
root = g.installJulia(root)
case "rust":
root = g.installRust(root, language.Version)
case "go":
root = g.installGolang(root, language.Version)
}
}
return root, err
Expand Down
Loading