|
| 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 | + "fmt" |
| 20 | + "os" |
| 21 | + |
| 22 | + "github.com/coreos/coreos-assembler/internal/pkg/cmdrun" |
| 23 | + "github.com/coreos/coreos-assembler/internal/pkg/cosash" |
| 24 | + "github.com/spf13/cobra" |
| 25 | +) |
| 26 | + |
| 27 | +type BuildCImageOptions struct { |
| 28 | + authfile string |
| 29 | + initialize bool |
| 30 | +} |
| 31 | + |
| 32 | +var ( |
| 33 | + BuildCImageOpts BuildCImageOptions |
| 34 | + |
| 35 | + cmdBuildCImage = &cobra.Command{ |
| 36 | + Use: "build-cimage", |
| 37 | + Short: "cosa build-cimage [repository]", |
| 38 | + Args: cobra.ExactArgs(1), |
| 39 | + Long: "Initialize directory for ostree container image build", |
| 40 | + RunE: implRunBuildCImage, |
| 41 | + } |
| 42 | +) |
| 43 | + |
| 44 | +func init() { |
| 45 | + cmdBuildCImage.Flags().BoolVarP( |
| 46 | + &BuildCImageOpts.initialize, "initialize", "i", false, |
| 47 | + "Assume target image does not exist") |
| 48 | + cmdBuildCImage.Flags().StringVar( |
| 49 | + &BuildCImageOpts.authfile, "authfile", "", |
| 50 | + "Path to container authentication file") |
| 51 | +} |
| 52 | + |
| 53 | +func runBuildCImage(argv []string) error { |
| 54 | + cmdBuildCImage.SetArgs(argv) |
| 55 | + return cmdBuildCImage.Execute() |
| 56 | +} |
| 57 | + |
| 58 | +// This is a Go reipmlementation of pick_yaml_or_else_json() from cmdlib.sh |
| 59 | +func pickConfigFileYamlOrJson(name string, preferJson bool) (string, error) { |
| 60 | + jsonPath := fmt.Sprintf("src/config/%s.json", name) |
| 61 | + yamlPath := fmt.Sprintf("src/config/%s.yaml", name) |
| 62 | + if _, err := os.Stat(jsonPath); err != nil { |
| 63 | + if !os.IsNotExist(err) { |
| 64 | + return "", err |
| 65 | + } |
| 66 | + jsonPath = "" |
| 67 | + } |
| 68 | + if _, err := os.Stat(yamlPath); err != nil { |
| 69 | + if !os.IsNotExist(err) { |
| 70 | + return "", err |
| 71 | + } |
| 72 | + yamlPath = "" |
| 73 | + } |
| 74 | + if jsonPath != "" && yamlPath != "" { |
| 75 | + return "", fmt.Errorf("found both %s and %s", jsonPath, yamlPath) |
| 76 | + } |
| 77 | + if jsonPath != "" { |
| 78 | + return jsonPath, nil |
| 79 | + } |
| 80 | + return yamlPath, nil |
| 81 | +} |
| 82 | + |
| 83 | +func implRunBuildCImage(c *cobra.Command, args []string) error { |
| 84 | + if err := cmdrun.RunCmdSyncV("cosa", "build", "--prepare-only"); err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + csh, err := cosash.NewCosaSh() |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + basearch, err := csh.BaseArch() |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + repository := args[0] |
| 98 | + |
| 99 | + buildArgs := []string{"compose", "image", "--format", "registry", "--layer-repo", "tmp/repo"} |
| 100 | + if BuildCImageOpts.initialize { |
| 101 | + buildArgs = append(buildArgs, "--initialize") |
| 102 | + } |
| 103 | + if BuildCImageOpts.authfile != "" { |
| 104 | + buildArgs = append(buildArgs, "--authfile", BuildCImageOpts.authfile) |
| 105 | + } |
| 106 | + if _, err := os.Stat("tmp/cosa-transient"); err != nil { |
| 107 | + if !os.IsNotExist(err) { |
| 108 | + return err |
| 109 | + } |
| 110 | + cachedir := "cache/buildcimage-cache" |
| 111 | + if err := os.MkdirAll(cachedir, 0o755); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + buildArgs = append(buildArgs, "--cachedir", cachedir) |
| 115 | + } |
| 116 | + manifestLock, err := pickConfigFileYamlOrJson(fmt.Sprintf("manifest-lock.%s", basearch), true) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + manifestLockOverrides, err := pickConfigFileYamlOrJson("manifest-lock.overrides", false) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + manifestLockArchOverrides, err := pickConfigFileYamlOrJson(fmt.Sprintf("manifest-lock.overrides.%s", basearch), false) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + for _, lock := range []string{manifestLock, manifestLockOverrides, manifestLockArchOverrides} { |
| 129 | + if lock != "" { |
| 130 | + buildArgs = append(buildArgs, "--lockfile", lock) |
| 131 | + } |
| 132 | + } |
| 133 | + buildArgs = append(buildArgs, "src/config/manifest.yaml") |
| 134 | + buildArgs = append(buildArgs, repository) |
| 135 | + |
| 136 | + argv0 := "rpm-ostree" |
| 137 | + priv, err := csh.HasPrivileges() |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + if priv { |
| 142 | + argv0 = "sudo" |
| 143 | + buildArgs = append([]string{"rpm-ostree"}, buildArgs...) |
| 144 | + } else { |
| 145 | + return fmt.Errorf("this command currently requires the ability to create nested containers") |
| 146 | + } |
| 147 | + |
| 148 | + if err := cmdrun.RunCmdSyncV(argv0, buildArgs...); err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
0 commit comments