|
| 1 | +// See usage below |
| 2 | +package main |
| 3 | + |
| 4 | +// build-cimage is a wrapper for `rpm-ostree compose image`; for more |
| 5 | +// on that, see https://coreos.github.io/rpm-ostree/container/ |
| 6 | +// |
| 7 | +// A key motivation here is to sever the dependency on S3 (and meta.json) for |
| 8 | +// our container image builds. As part of the ostree native container work, |
| 9 | +// the core of CoreOS becomes a container image. Our disk images |
| 10 | +// have always been derivatives of the container, and this is pushing things |
| 11 | +// farther in that direction. |
| 12 | +// See https://github.com/coreos/coreos-assembler/issues/2685 |
| 13 | +// |
| 14 | +// This command is opinionated on reading and writing to a remote registry, |
| 15 | +// whereas the underlying `rpm-ostree compose image` defaults to |
| 16 | +// an ociarchive. |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "io/ioutil" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/coreos/coreos-assembler/internal/pkg/cmdrun" |
| 25 | + "github.com/coreos/coreos-assembler/internal/pkg/cosash" |
| 26 | + "github.com/spf13/cobra" |
| 27 | +) |
| 28 | + |
| 29 | +const initConfigPath = "src/config.json" |
| 30 | +const defaultManifest = "src/config/manifest.yaml" |
| 31 | + |
| 32 | +type BuildCImageOptions struct { |
| 33 | + authfile string |
| 34 | + initialize bool |
| 35 | +} |
| 36 | + |
| 37 | +var ( |
| 38 | + BuildCImageOpts BuildCImageOptions |
| 39 | + |
| 40 | + cmdBuildCImage = &cobra.Command{ |
| 41 | + Use: "build-cimage", |
| 42 | + Short: "cosa build-cimage [repository]", |
| 43 | + Args: cobra.ExactArgs(1), |
| 44 | + Long: "Initialize directory for ostree container image build", |
| 45 | + RunE: implRunBuildCImage, |
| 46 | + } |
| 47 | +) |
| 48 | + |
| 49 | +func init() { |
| 50 | + cmdBuildCImage.Flags().BoolVarP( |
| 51 | + &BuildCImageOpts.initialize, "initialize", "i", false, |
| 52 | + "Assume target image does not exist") |
| 53 | + cmdBuildCImage.Flags().StringVar( |
| 54 | + &BuildCImageOpts.authfile, "authfile", "", |
| 55 | + "Path to container authentication file") |
| 56 | +} |
| 57 | + |
| 58 | +func runBuildCImage(argv []string) error { |
| 59 | + cmdBuildCImage.SetArgs(argv) |
| 60 | + return cmdBuildCImage.Execute() |
| 61 | +} |
| 62 | + |
| 63 | +// This is a Go reipmlementation of pick_yaml_or_else_json() from cmdlib.sh |
| 64 | +func pickConfigFileYamlOrJson(name string, preferJson bool) (string, error) { |
| 65 | + jsonPath := fmt.Sprintf("src/config/%s.json", name) |
| 66 | + yamlPath := fmt.Sprintf("src/config/%s.yaml", name) |
| 67 | + if _, err := os.Stat(jsonPath); err != nil { |
| 68 | + if !os.IsNotExist(err) { |
| 69 | + return "", err |
| 70 | + } |
| 71 | + jsonPath = "" |
| 72 | + } |
| 73 | + if _, err := os.Stat(yamlPath); err != nil { |
| 74 | + if !os.IsNotExist(err) { |
| 75 | + return "", err |
| 76 | + } |
| 77 | + yamlPath = "" |
| 78 | + } |
| 79 | + if jsonPath != "" && yamlPath != "" { |
| 80 | + return "", fmt.Errorf("found both %s and %s", jsonPath, yamlPath) |
| 81 | + } |
| 82 | + if jsonPath != "" { |
| 83 | + return jsonPath, nil |
| 84 | + } |
| 85 | + return yamlPath, nil |
| 86 | +} |
| 87 | + |
| 88 | +type configVariant struct { |
| 89 | + Variant string `json:"coreos-assembler.config-variant"` |
| 90 | +} |
| 91 | + |
| 92 | +func getVariant() (string, error) { |
| 93 | + contents, err := ioutil.ReadFile(initConfigPath) |
| 94 | + if err != nil { |
| 95 | + if !os.IsNotExist(err) { |
| 96 | + return "", err |
| 97 | + } |
| 98 | + contents = []byte{} |
| 99 | + } |
| 100 | + |
| 101 | + var variantData configVariant |
| 102 | + if err := json.Unmarshal(contents, &variantData); err != nil { |
| 103 | + return "", fmt.Errorf("parsing %s: %w", initConfigPath, err) |
| 104 | + } |
| 105 | + |
| 106 | + return variantData.Variant, nil |
| 107 | +} |
| 108 | + |
| 109 | +func implRunBuildCImage(c *cobra.Command, args []string) error { |
| 110 | + if err := cmdrun.RunCmdSyncV("cosa", "build", "--prepare-only"); err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + csh, err := cosash.NewCosaSh() |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + basearch, err := csh.BaseArch() |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + variant, err := getVariant() |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + manifest := defaultManifest |
| 128 | + if variant != "" { |
| 129 | + manifest = fmt.Sprintf("src/config/manifest-%s.yaml", variant) |
| 130 | + } |
| 131 | + |
| 132 | + repository := args[0] |
| 133 | + |
| 134 | + buildArgs := []string{"compose", "image", "--format", "registry", "--layer-repo", "tmp/repo"} |
| 135 | + if BuildCImageOpts.initialize { |
| 136 | + buildArgs = append(buildArgs, "--initialize") |
| 137 | + } |
| 138 | + if BuildCImageOpts.authfile != "" { |
| 139 | + buildArgs = append(buildArgs, "--authfile", BuildCImageOpts.authfile) |
| 140 | + } |
| 141 | + if _, err := os.Stat("tmp/cosa-transient"); err != nil { |
| 142 | + if !os.IsNotExist(err) { |
| 143 | + return err |
| 144 | + } |
| 145 | + cachedir := "cache/buildcimage-cache" |
| 146 | + if err := os.MkdirAll(cachedir, 0o755); err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + buildArgs = append(buildArgs, "--cachedir", cachedir) |
| 150 | + } |
| 151 | + manifestLock, err := pickConfigFileYamlOrJson(fmt.Sprintf("manifest-lock.%s", basearch), true) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + manifestLockOverrides, err := pickConfigFileYamlOrJson("manifest-lock.overrides", false) |
| 156 | + if err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + manifestLockArchOverrides, err := pickConfigFileYamlOrJson(fmt.Sprintf("manifest-lock.overrides.%s", basearch), false) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + for _, lock := range []string{manifestLock, manifestLockOverrides, manifestLockArchOverrides} { |
| 164 | + if lock != "" { |
| 165 | + buildArgs = append(buildArgs, "--lockfile", lock) |
| 166 | + } |
| 167 | + } |
| 168 | + buildArgs = append(buildArgs, manifest) |
| 169 | + buildArgs = append(buildArgs, repository) |
| 170 | + |
| 171 | + argv0 := "rpm-ostree" |
| 172 | + priv, err := csh.HasPrivileges() |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + if priv { |
| 177 | + argv0 = "sudo" |
| 178 | + buildArgs = append([]string{"rpm-ostree"}, buildArgs...) |
| 179 | + } else { |
| 180 | + return fmt.Errorf("this command currently requires the ability to create nested containers") |
| 181 | + } |
| 182 | + |
| 183 | + if err := cmdrun.RunCmdSyncV(argv0, buildArgs...); err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
0 commit comments