-
Notifications
You must be signed in to change notification settings - Fork 9
/
makedisk.go
135 lines (109 loc) · 3.81 KB
/
makedisk.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package image
import (
"context"
"fmt"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/artifacts/registry"
"namespacelabs.dev/foundation/internal/build/binary"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/parsing/platform"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/module"
"namespacelabs.dev/foundation/std/pkggraph"
)
func newMakeDiskCmd() *cobra.Command {
var (
insecure bool
imageRef string
target string
size int64
envBound string
buildRef string
)
cmd := &cobra.Command{
Use: "make-disk",
Args: cobra.NoArgs,
RunE: fncobra.RunE(func(ctx context.Context, args []string) error {
root, err := module.FindRoot(ctx, ".")
if err != nil {
return err
}
env, err := cfg.LoadContext(root, envBound)
if err != nil {
return err
}
if size == 0 {
return fnerrors.New("size is required")
}
var platforms []specs.Platform
for _, plat := range []string{"linux/amd64", "linux/arm64"} {
p, err := platform.ParsePlatform(plat)
if err != nil {
return err
}
platforms = append(platforms, p)
}
imgid, err := oci.ParseImageID(imageRef)
if err != nil {
return err
}
image, err := makeImage(ctx, env, imgid, buildRef, insecure, platforms)
if err != nil {
return err
}
var imgwithplat []oci.ImageWithPlatform
for _, plat := range platforms {
plat := plat // Close plat.
x := binary.MakeDisk(compute.Transform("get-image", image, func(ctx context.Context, r oci.ResolvableImage) (oci.Image, error) {
return r.ImageForPlatform(plat)
}), target, size, true)
imgwithplat = append(imgwithplat, oci.ImageWithPlatform{
Image: oci.MakeNamedImage(platform.FormatPlatform(plat), x),
Platform: plat,
})
}
repository := registry.StaticRepository(nil, imgid.Repository, oci.RegistryAccess{InsecureRegistry: insecure})
published := oci.PublishResolvable(repository, oci.MakeImageIndex(imgwithplat...), nil)
res, err := compute.Get(ctx, published)
if err != nil {
return err
}
fmt.Fprintf(console.Stdout(ctx), "%s\n", res.Value.ImageRef())
return nil
}),
}
cmd.Flags().BoolVar(&insecure, "insecure", false, "Set to true to access registries over insecure communications.")
cmd.Flags().StringVar(&imageRef, "image", "", "The image to convert.")
cmd.Flags().StringVar(&buildRef, "build", "", "The image to build.")
cmd.Flags().StringVar(&target, "target", "disk.ext4.zstd", "The name of the disk.")
cmd.Flags().Int64Var(&size, "size", 0, "The size.")
cmd.Flags().StringVar(&envBound, "env", "dev", "The environment.")
return cmd
}
func makeImage(ctx context.Context, env cfg.Context, imgid oci.ImageID, buildRef string, insecure bool, platforms []specs.Platform) (compute.Computable[oci.ResolvableImage], error) {
if buildRef != "" {
pkgRef, err := schema.StrictParsePackageRef(buildRef)
if err != nil {
return nil, err
}
pl := parsing.NewPackageLoader(env)
bin, err := binary.Load(ctx, pl, env, pkgRef, binary.BuildImageOpts{
Platforms: platforms,
})
if err != nil {
return nil, err
}
return bin.Image(ctx, pkggraph.MakeSealedContext(env, pl.Seal()))
}
return oci.Prebuilt(imgid, oci.RegistryAccess{InsecureRegistry: insecure}), nil
}