-
Notifications
You must be signed in to change notification settings - Fork 160
/
system.go
248 lines (221 loc) · 7.74 KB
/
system.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// 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 (
_ "embed"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/cockroachdb/errors"
"github.com/moby/buildkit/client/llb"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/tensorchord/envd/pkg/config"
"github.com/tensorchord/envd/pkg/flag"
"github.com/tensorchord/envd/pkg/types"
"github.com/tensorchord/envd/pkg/version"
)
func (g Graph) compileUbuntuAPT(root llb.State) llb.State {
if g.UbuntuAPTSource != nil {
logrus.WithField("source", *g.UbuntuAPTSource).Debug("using custom APT source")
aptSource := llb.Scratch().
File(llb.Mkdir(filepath.Dir(aptSourceFilePath), 0755, llb.WithParents(true)),
llb.WithCustomName("[internal] setting apt source")).
File(llb.Mkfile(aptSourceFilePath, 0644, []byte(*g.UbuntuAPTSource)),
llb.WithCustomName("[internal] setting apt source"))
return llb.Merge([]llb.State{root, aptSource},
llb.WithCustomName("[internal] setting apt source"))
}
return root
}
func (g Graph) compileRun(root llb.State) llb.State {
if len(g.Exec) == 0 {
return root
}
workingDir := g.getWorkingDir()
stage := root
for _, execGroup := range g.Exec {
var sb strings.Builder
sb.WriteString("set -euo pipefail\n")
for _, c := range execGroup {
sb.WriteString(c + "\n")
}
cmdStr := fmt.Sprintf("/usr/bin/bash -c '%s'", sb.String())
logrus.WithField("command", cmdStr).Debug("compile run command")
// Mount the build context into the build process.
// TODO(gaocegege): Maybe we should make it readonly,
// but these cases then cannot be supported:
// run(commands=["git clone xx.git"])
stage = stage.
Run(llb.Shlex(cmdStr),
llb.AddEnv("PATH", types.DefaultPathEnvUnix),
llb.Dir(workingDir),
llb.AddMount(workingDir, llb.Local(flag.FlagBuildContext))).Root()
}
return stage
}
func (g Graph) compileCopy(root llb.State) llb.State {
if len(g.Copy) == 0 {
return root
}
result := root
// Compose the copy command.
for _, c := range g.Copy {
result = result.File(llb.Copy(
llb.Local(flag.FlagBuildContext), c.Source, c.Destination,
llb.WithUIDGID(g.uid, g.gid)))
}
return result
}
func (g *Graph) compileCUDAPackages(org string) llb.State {
return g.preparePythonBase(llb.Image(fmt.Sprintf(
"docker.io/%s:%s-cudnn%s-devel-%s",
org, *g.CUDA, g.CUDNN, g.OS)))
}
func (g Graph) compileSystemPackages(root llb.State) llb.State {
if len(g.SystemPackages) == 0 {
logrus.Debug("skip the apt since system package is not specified")
return root
}
// Compose the package install command.
var sb strings.Builder
sb.WriteString("sudo apt-get update && sudo apt-get install -y --no-install-recommends")
for _, pkg := range g.SystemPackages {
sb.WriteString(fmt.Sprintf(" %s", pkg))
}
cacheDir := "/var/cache/apt"
cacheLibDir := "/var/lib/apt"
run := root.Run(llb.Shlex(fmt.Sprintf("bash -c \"%s\"", sb.String())),
llb.WithCustomNamef("apt-get install %s",
strings.Join(g.SystemPackages, " ")))
run.AddMount(cacheDir, llb.Scratch(),
llb.AsPersistentCacheDir(g.CacheID(cacheDir), llb.CacheMountShared))
run.AddMount(cacheLibDir, llb.Scratch(),
llb.AsPersistentCacheDir(g.CacheID(cacheLibDir), llb.CacheMountShared))
return run.Root()
}
// nolint:unparam
func (g *Graph) compileExtraSource(root llb.State) (llb.State, error) {
if len(g.HTTP) == 0 {
return root, nil
}
inputs := []llb.State{}
for _, httpInfo := range g.HTTP {
src := llb.HTTP(
httpInfo.URL,
llb.Checksum(httpInfo.Checksum),
llb.Filename(httpInfo.Filename),
llb.Chown(g.uid, g.gid),
)
inputs = append(inputs, llb.Scratch().File(
llb.Copy(src, "/", g.getExtraSourceDir(), &llb.CopyInfo{CreateDestPath: true}),
))
}
inputs = append(inputs, root)
return llb.Merge(inputs, llb.WithCustomName("[internal] build source layers")), nil
}
func (g *Graph) preparePythonBase(root llb.State) llb.State {
for _, env := range types.BaseEnvironment {
root = root.AddEnv(env.Name, env.Value)
}
// apt packages
var sb strings.Builder
sb.WriteString("apt-get update && apt-get install -y apt-utils && ")
sb.WriteString("apt-get install -y --no-install-recommends --no-install-suggests --fix-missing ")
sb.WriteString(strings.Join(types.BaseAptPackage, " "))
sb.WriteString("&& rm -rf /var/lib/apt/lists/* ")
// shell prompt
sb.WriteString("&& curl --proto '=https' --tlsv1.2 -sSf https://starship.rs/install.sh | sh -s -- -y")
run := root.Run(llb.Shlex(fmt.Sprintf("bash -c \"%s\"", sb.String())),
llb.WithCustomName("[internal] install system packages"))
return run.Root()
}
func (g Graph) compileSshd(root llb.State) llb.State {
sshd := root.File(llb.Copy(
llb.Image(types.EnvdSshdImage), "/usr/bin/envd-sshd", "/var/envd/bin/envd-sshd",
&llb.CopyInfo{CreateDestPath: true}),
llb.WithCustomName(fmt.Sprintf("[internal] add envd-sshd from %s", types.EnvdSshdImage)))
return sshd
}
func (g *Graph) compileBase() (llb.State, error) {
logger := logrus.WithFields(logrus.Fields{
"os": g.OS,
"language": g.Language.Name,
})
if g.Language.Version != nil {
logger = logger.WithField("version", *g.Language.Version)
}
logger.Debug("compile base image")
var base llb.State
org := viper.GetString(flag.FlagDockerOrganization)
v := version.GetVersionForImageTag()
// Do not update user permission in the base image.
if g.Image != nil {
logger.WithField("image", *g.Image).Debugf("using custom base image")
return llb.Image(*g.Image), nil
} else if g.CUDA == nil {
switch g.Language.Name {
case "r":
base = llb.Image(fmt.Sprintf("docker.io/%s/r-base:4.2-envd-%s", org, v))
// r-base image already has GID 1000.
// It is a trick, we actually use GID 1000
if g.gid == 1000 {
g.gid = 1001
}
if g.uid == 1000 {
g.uid = 1001
}
case "python":
// TODO(keming) use user input `base(os="")`
base = g.preparePythonBase(llb.Image(types.PythonBaseImage))
case "julia":
base = llb.Image(fmt.Sprintf(
"docker.io/%s/julia:1.8rc1-ubuntu20.04-envd-%s", org, v))
}
} else {
base = g.compileCUDAPackages("nvidia/cuda")
}
base = g.compileUserGroup(base)
// Install conda first.
condaStage, err := g.installConda(base)
if err != nil {
return llb.State{}, errors.Wrap(err, "failed to install conda")
}
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) {
public := DefaultGraph.PublicKeyPath
bdat, err := os.ReadFile(public)
dat := strings.TrimSuffix(string(bdat), "\n")
if err != nil {
return llb.State{}, errors.Wrap(err, "Cannot read public SSH key")
}
run := root.
File(llb.Mkdir("/var/envd", 0755, llb.WithParents(true),
llb.WithUIDGID(g.uid, g.gid))).
File(llb.Mkfile(config.ContainerAuthorizedKeysPath,
0644, []byte(dat+" envd"), llb.WithUIDGID(g.uid, g.gid)),
llb.WithCustomName("install ssh keys"))
return run, nil
}