-
Notifications
You must be signed in to change notification settings - Fork 59
/
main.go
326 lines (312 loc) · 9.36 KB
/
main.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
import (
"context"
"encoding/json"
"fmt"
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/containerd/containerd/archive"
vendor "github.com/ktock/container2wasm"
"github.com/ktock/container2wasm/version"
"github.com/urfave/cli"
)
const defaultOutputFile = "out.wasm"
func main() {
app := cli.NewApp()
app.Name = "c2w"
app.Version = version.Version
app.Usage = "container to wasm converter"
app.UsageText = fmt.Sprintf("%s [options] image-name [output file]", app.Name)
app.ArgsUsage = "image-name [output-file]"
var flags []cli.Flag
app.Flags = append([]cli.Flag{
cli.StringFlag{
Name: "assets",
Usage: "Custom location of build assets.",
},
cli.StringFlag{
Name: "dockerfile",
Usage: "Custom location of Dockerfile (default: embedded to this command)", // TODO: allow to show it
},
cli.StringFlag{
Name: "builder",
Usage: "Bulider command to use",
Value: "docker",
},
cli.StringFlag{
Name: "target-arch",
Usage: "target architecture of the source image to use",
Value: "amd64",
},
cli.StringSliceFlag{
Name: "build-arg",
Usage: "Additional build arguments",
},
cli.BoolFlag{
Name: "to-js",
Usage: "output JS files runnable on the browsers using emscripten",
},
cli.BoolFlag{
Name: "debug-image",
Usage: "Enable debug print in the output image",
},
cli.BoolFlag{
Name: "show-dockerfile",
Usage: "Show default Dockerfile",
},
cli.BoolFlag{
Name: "legacy",
Usage: "Use \"docker build\" instead of buildx (no support for assets flag)",
},
cli.BoolFlag{
Name: "external-bundle",
Usage: "Do not embed container image to the Wasm image but mount it during runtime",
},
}, flags...)
app.Action = rootAction
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
}
func rootAction(clicontext *cli.Context) error {
if clicontext.Bool("show-dockerfile") {
fmt.Printf("%s", vendor.Dockerfile)
fmt.Printf("\n")
return nil
}
arg1 := clicontext.Args().First()
if arg1 == "" {
if clicontext.Bool("external-bundle") {
return fmt.Errorf("specify output image name")
} else {
return fmt.Errorf("specify image name")
}
}
var outputPath string
if clicontext.Bool("external-bundle") {
outputPath = arg1
if clicontext.Args().Get(1) != "" {
return fmt.Errorf("command receives only 1 arg (output image path) with external-bundle")
}
} else {
outputPath = clicontext.Args().Get(1)
}
builderPath, err := exec.LookPath(clicontext.String("builder"))
if err != nil {
return err
}
legacy := false
if err := exec.Command(builderPath, "buildx").Run(); err != nil {
log.Printf("buildx unavailable. falling back to the normal builder.\n")
legacy = true
}
if clicontext.Bool("legacy") {
legacy = true
}
destDir, destFile := ".", defaultOutputFile
if clicontext.Bool("to-js") {
destFile = ""
}
if outputPath != "" {
d, f := filepath.Split(outputPath)
destDir, err = filepath.Abs(d)
if err != nil {
return err
}
if f != "" {
destFile = f
}
}
if clicontext.Bool("to-js") && destFile != "" {
return fmt.Errorf("output destination must be a slash-terminated directory path when using \"to-js\" option")
}
if a := clicontext.String("assets"); a != "" && legacy {
return fmt.Errorf("\"assets\" unsupported on docker build as of now; install docker buildx instead")
}
srcImgName := arg1
tmpdir, err := os.MkdirTemp("", "container2wasm")
if err != nil {
return err
}
defer os.RemoveAll(tmpdir)
srcImgPath := filepath.Join(tmpdir, "img")
if err := os.Mkdir(srcImgPath, 0755); err != nil {
return err
}
if !clicontext.Bool("external-bundle") {
if err := prepareSourceImg(builderPath, srcImgName, srcImgPath, clicontext.String("target-arch")); err != nil {
return fmt.Errorf("failed to prepare image: %w", err)
}
}
if legacy {
return buildWithLegacyBuilder(builderPath, srcImgPath, destDir, destFile, clicontext)
}
return build(builderPath, srcImgPath, destDir, destFile, clicontext)
}
func build(builderPath string, srcImgPath string, destDir, destFile string, clicontext *cli.Context) error {
buildxArgs := []string{
"buildx", "build", "--progress=plain",
"--build-arg", fmt.Sprintf("TARGETARCH=%s", clicontext.String("target-arch")),
"--build-arg", fmt.Sprintf("TARGETPLATFORM=linux/%s", clicontext.String("target-arch")),
"--platform=linux/amd64",
}
var dockerfilePath string
if o := clicontext.String("dockerfile"); o != "" {
dockerfilePath = o
} else {
f, err := os.CreateTemp("", "container2wasm")
if err != nil {
return err
}
defer os.Remove(f.Name())
if _, err := f.Write(vendor.Dockerfile); err != nil {
return err
}
dockerfilePath = f.Name()
}
buildxArgs = append(buildxArgs, "-f", dockerfilePath)
if o := clicontext.String("assets"); o != "" {
buildxArgs = append(buildxArgs, "--build-context", fmt.Sprintf("assets=%s", o))
}
if clicontext.Bool("to-js") {
buildxArgs = append(buildxArgs,
"--target=js",
"--build-arg", "OPTIMIZATION_MODE=native",
)
}
buildxArgs = append(buildxArgs, "--output", fmt.Sprintf("type=local,dest=%s", destDir))
if destFile != "" {
buildxArgs = append(buildxArgs, "--build-arg", fmt.Sprintf("OUTPUT_NAME=%s", destFile))
}
linuxLogLevel, initDebug := 0, false
if clicontext.Bool("debug-image") {
linuxLogLevel, initDebug = 7, true
}
buildxArgs = append(buildxArgs,
"--build-arg", fmt.Sprintf("LINUX_LOGLEVEL=%d", linuxLogLevel),
"--build-arg", fmt.Sprintf("INIT_DEBUG=%v", initDebug),
)
if clicontext.Bool("external-bundle") {
buildxArgs = append(buildxArgs, "--build-arg", "EXTERNAL_BUNDLE=true")
}
for _, a := range clicontext.StringSlice("build-arg") {
buildxArgs = append(buildxArgs, "--build-arg", a)
}
buildxArgs = append(buildxArgs, srcImgPath)
log.Printf("buildx args: %+v\n", buildxArgs)
cmd := exec.Command(builderPath, buildxArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func buildWithLegacyBuilder(builderPath string, srcImgPath, destDir, destFile string, clicontext *cli.Context) error {
buildArgs := []string{
"build", "--progress=plain",
"--platform=linux/amd64",
"--build-arg", fmt.Sprintf("TARGETARCH=%s", clicontext.String("target-arch")),
}
var dockerfilePath string
if o := clicontext.String("dockerfile"); o != "" {
dockerfilePath = o
} else {
f, err := os.CreateTemp("", "container2wasm")
if err != nil {
return err
}
defer os.Remove(f.Name())
if _, err := f.Write(vendor.Dockerfile); err != nil {
return err
}
dockerfilePath = f.Name()
}
buildArgs = append(buildArgs, "-f", dockerfilePath)
if clicontext.Bool("to-js") {
buildArgs = append(buildArgs,
"--target=js",
"--build-arg", "OPTIMIZATION_MODE=native",
)
}
buildArgs = append(buildArgs, "--output", fmt.Sprintf("type=local,dest=%s", destDir))
if destFile != "" {
buildArgs = append(buildArgs, "--build-arg", fmt.Sprintf("OUTPUT_NAME=%s", destFile))
}
linuxLogLevel, initDebug := 0, false
if clicontext.Bool("debug-image") {
linuxLogLevel, initDebug = 7, true
}
buildArgs = append(buildArgs,
"--build-arg", fmt.Sprintf("LINUX_LOGLEVEL=%d", linuxLogLevel),
"--build-arg", fmt.Sprintf("INIT_DEBUG=%v", initDebug),
)
if clicontext.Bool("external-bundle") {
buildArgs = append(buildArgs, "--build-arg", "EXTERNAL_BUNDLE=true")
}
for _, a := range clicontext.StringSlice("build-arg") {
buildArgs = append(buildArgs, "--build-arg", a)
}
buildArgs = append(buildArgs, srcImgPath)
log.Printf("build args: %+v\n", buildArgs)
cmd := exec.Command(builderPath, buildArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func prepareSourceImg(builderPath, imgName, tmpdir, targetarch string) error {
log.Printf("saving %q to %q\n", imgName, tmpdir)
// TODO: check architecture
needsPull := false
if idata, err := exec.Command(builderPath, "image", "inspect", imgName).Output(); err != nil {
needsPull = true
} else if targetarch != "" {
inspectData := make([]map[string]interface{}, 1)
if err := json.Unmarshal(idata, &inspectData); err != nil {
return err
}
if a := inspectData[0]["Architecture"]; a != targetarch {
log.Printf("unexpected archtecture %v (target: %v). Try \"--target-arch\" when specifying an architecture.\n", a, targetarch)
needsPull = true
}
}
if needsPull {
args := []string{"pull"}
if targetarch != "" {
args = append(args, "--platform=linux/"+targetarch)
}
args = append(args, imgName)
log.Printf("cannot get image %q locally; pulling it from the registry...\n", imgName)
pullCmd := exec.Command(builderPath, args...)
pullCmd.Stdout = os.Stdout
pullCmd.Stderr = os.Stderr
if err := pullCmd.Run(); err != nil {
return fmt.Errorf("failed to pull the image. Try \"--target-arch\" when specifying an architecture: %w", err)
}
}
saveCmd := exec.Command(builderPath, "save", imgName)
outR, err := saveCmd.StdoutPipe()
if err != nil {
return err
}
defer outR.Close()
saveCmd.Stderr = os.Stderr
if err := saveCmd.Start(); err != nil {
return err
}
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
if _, err := archive.Apply(ctx, tmpdir, outR, archive.WithNoSameOwner()); err != nil {
return err
}
if err := saveCmd.Wait(); err != nil {
return err
}
// update timestamp so that BuildKit can prioritize them over cached files
now := time.Now().Local()
return filepath.Walk(tmpdir, func(p string, info fs.FileInfo, err error) error {
return os.Chtimes(p, now, now)
})
}