/
engine.go
227 lines (196 loc) · 6.38 KB
/
engine.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
package mage
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"dagger.io/dagger"
"github.com/containerd/containerd/platforms"
"github.com/magefile/mage/mg"
"golang.org/x/mod/semver"
"github.com/dagger/dagger/ci/mage/sdk"
"github.com/dagger/dagger/ci/mage/util"
"github.com/dagger/dagger/engine/distconsts"
)
type Engine mg.Namespace
var (
publishedEnginePlatforms = []string{"linux/amd64", "linux/arm64"}
publishedGPUEnginePlatforms = []string{"linux/amd64"}
)
// Connect tests a connection to a Dagger Engine
func (t Engine) Connect(ctx context.Context) error {
c, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
if err != nil {
return err
}
return c.Close()
}
// Lint lints the engine
func (t Engine) Lint(ctx context.Context) error {
return util.DaggerCall(ctx, "engine", "lint")
}
// Lint lints the engine
func (t Engine) Scan(ctx context.Context) error {
return util.DaggerCall(ctx, "engine", "scan")
}
// Publish builds and pushes Engine OCI image to a container registry
func (t Engine) Publish(ctx context.Context, version string) error {
var commonArgs []string
if v, ok := os.LookupEnv("DAGGER_ENGINE_IMAGE"); ok {
commonArgs = append(commonArgs, "--image="+v)
}
if v, ok := os.LookupEnv("DAGGER_ENGINE_IMAGE_REGISTRY"); ok {
commonArgs = append(commonArgs, "--registry="+v)
}
if v, ok := os.LookupEnv("DAGGER_ENGINE_IMAGE_USERNAME"); ok {
commonArgs = append(commonArgs, "--registry-username="+v)
}
if _, ok := os.LookupEnv("DAGGER_ENGINE_IMAGE_PASSWORD"); ok {
commonArgs = append(commonArgs, "--registry-password=env:DAGGER_ENGINE_IMAGE_PASSWORD")
}
args := []string{"--version=" + version, "engine", "publish"}
args = append(args, commonArgs...)
for _, p := range publishedEnginePlatforms {
args = append(args, "--platform="+p)
}
err := util.DaggerCall(ctx, args...)
if err != nil {
return err
}
args = []string{"--version=" + version, "engine", "with-gpusupport", "publish"}
args = append(args, commonArgs...)
for _, p := range publishedGPUEnginePlatforms {
args = append(args, "--platform="+p)
}
err = util.DaggerCall(ctx, args...)
if err != nil {
return err
}
if semver.IsValid(version) {
sdks := sdk.All{}
if err := sdks.Bump(ctx, version); err != nil {
return err
}
} else {
fmt.Println("skipping image bump in SDKs")
}
return nil
}
// Verify that all arches for the Engine can be built. Just do a local export to avoid setting up
// a registry
func (t Engine) TestPublish(ctx context.Context) error {
err := util.DaggerCall(ctx, "engine", "test-publish")
if err != nil {
return err
}
err = util.DaggerCall(ctx, "engine", "with-gpusupport", "test-publish")
if err != nil {
return err
}
return nil
}
// Test runs Engine tests
func (t Engine) Test(ctx context.Context) error {
return t.test(ctx, "all")
}
// TestRace runs Engine tests with go race detector enabled
func (t Engine) TestRace(ctx context.Context) error {
return t.test(ctx, "all", "--race=true")
}
// TestImportant runs Engine Container+Module tests, which give good basic coverage
// of functionality w/out having to run everything
func (t Engine) TestImportant(ctx context.Context) error {
return t.test(ctx, "important", "--race=true")
}
func (t Engine) test(ctx context.Context, additional ...string) error {
args := []string{"test"}
if cfg, ok := os.LookupEnv("_EXPERIMENTAL_DAGGER_CACHE_CONFIG"); ok {
args = append(args, "with-cache", "--config="+cfg)
}
args = append(args, additional...)
return util.DaggerCall(ctx, args...)
}
// Dev builds and starts an Engine & CLI from local source code
func (t Engine) Dev(ctx context.Context) error {
gpuSupport := os.Getenv(util.GPUSupportEnvName) != ""
trace := os.Getenv(util.TraceEnvName) != ""
args := []string{"engine"}
if gpuSupport {
args = append(args, "with-gpusupport")
}
if trace {
args = append(args, "with-trace")
}
tarPath := "./bin/engine.tar"
args = append(args, "container", "export", "--path="+tarPath)
args = append(args, "--forced-compression="+string(dagger.Gzip)) // use gzip to avoid incompatibility w/ older docker versions
err := util.DaggerCall(ctx, args...)
if err != nil {
return err
}
volumeName := util.EngineContainerName
imageName := fmt.Sprintf("localhost/%s:latest", util.EngineContainerName)
// #nosec
loadCmd := exec.CommandContext(ctx, "docker", "load", "-i", tarPath)
output, err := loadCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("docker load failed: %w: %s", err, output)
}
_, imageID, ok := strings.Cut(string(output), "Loaded image ID: sha256:")
if !ok {
_, imageID, ok = strings.Cut(string(output), "Loaded image: sha256:") // podman
if !ok {
return fmt.Errorf("unexpected output from docker load: %s", output)
}
}
imageID = strings.TrimSpace(imageID)
if output, err := exec.CommandContext(ctx, "docker",
"tag",
imageID,
imageName,
).CombinedOutput(); err != nil {
return fmt.Errorf("docker tag %s %s: %w: %s", imageID, imageName, err, output)
}
//nolint:gosec
if output, err := exec.CommandContext(ctx, "docker",
"rm",
"-fv",
util.EngineContainerName,
).CombinedOutput(); err != nil {
return fmt.Errorf("docker rm: %w: %s", err, output)
}
runArgs := []string{
"run",
"-d",
}
// Make all GPUs visible to the engine container if the GPU support flag is set:
if gpuSupport {
runArgs = append(runArgs, []string{"--gpus", "all"}...)
}
runArgs = append(runArgs, []string{
"-e", util.CacheConfigEnvName,
"-e", "DAGGER_CLOUD_TOKEN",
"-e", "DAGGER_CLOUD_URL",
"-e", util.GPUSupportEnvName,
"-v", volumeName + ":" + distconsts.EngineDefaultStateDir,
"-p", "6060:6060",
"--name", util.EngineContainerName,
"--privileged",
}...)
runArgs = append(runArgs, imageName, "--debug", "--debugaddr=0.0.0.0:6060")
if output, err := exec.CommandContext(ctx, "docker", runArgs...).CombinedOutput(); err != nil {
return fmt.Errorf("docker run: %w: %s", err, output)
}
// build the CLI and export locally so it can be used to connect to the Engine
binDest := filepath.Join(os.Getenv("DAGGER_SRC_ROOT"), "bin", "dagger")
_ = os.Remove(binDest) // HACK(vito): avoid 'text file busy'.
err = util.DaggerCall(ctx, "cli", "file", "--platform="+platforms.DefaultString(), "export", "--path="+binDest)
if err != nil {
return err
}
fmt.Println("export _EXPERIMENTAL_DAGGER_CLI_BIN=" + binDest)
fmt.Println("export _EXPERIMENTAL_DAGGER_RUNNER_HOST=docker-container://" + util.EngineContainerName)
return nil
}