|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/BurntSushi/toml" |
| 9 | + "github.com/alecthomas/kong" |
| 10 | +) |
| 11 | + |
| 12 | +var cli struct { |
| 13 | + Builder BuildersCmds `cmd:"" help:"Commands to modify builders"` |
| 14 | + Buildpacks BuildpackCmds `cmd:"" help:"Commands to modify buildpacks"` |
| 15 | +} |
| 16 | + |
| 17 | +type BuildersCmds struct { |
| 18 | + File string `short:"f" default:"builder.toml" help:"Path to builder.toml." type:"existingfile"` |
| 19 | + SetBuildpacks SetBuildpacksCmd `cmd:"" help:"Update the version of the renku buildpacks in a builder."` |
| 20 | + SetBuilder SetBuilderCmd `cmd:"" help:"Update the version of the builder image."` |
| 21 | + SetRunner SetRunnerCmd `cmd:"" help:"Update the version of the runner image."` |
| 22 | +} |
| 23 | + |
| 24 | +type BuildpackCmds struct { |
| 25 | + SetVersion SetBuildpackVersionCmd `cmd:"" help:"Update the version of all buildpacks image."` |
| 26 | +} |
| 27 | + |
| 28 | +type SetBuildpacksCmd struct { |
| 29 | + Version string `arg:"" help:"New version (tag) to set."` |
| 30 | +} |
| 31 | + |
| 32 | +type SetBuilderCmd struct { |
| 33 | + Version string `arg:"" help:"New version (tag) to set."` |
| 34 | +} |
| 35 | + |
| 36 | +type SetRunnerCmd struct { |
| 37 | + Version string `arg:"" help:"New version (tag) to set."` |
| 38 | +} |
| 39 | + |
| 40 | +type SetBuildpackVersionCmd struct { |
| 41 | + Version string `arg:"" help:"New version (tag) to set."` |
| 42 | +} |
| 43 | + |
| 44 | +func main() { |
| 45 | + ctx := kong.Parse(&cli, |
| 46 | + kong.Name("bpu"), |
| 47 | + kong.Description("Buildpack Utilities (bpu)"), |
| 48 | + kong.UsageOnError(), |
| 49 | + ) |
| 50 | + |
| 51 | + switch ctx.Command() { |
| 52 | + case "builder set-buildpacks <version>": |
| 53 | + cfg := mustLoadBuilder(cli.Builder.File) |
| 54 | + cli.Builder.SetBuildpacks.Run(cfg, cli.Builder.File) |
| 55 | + case "builder set-builder <version>": |
| 56 | + cfg := mustLoadBuilder(cli.Builder.File) |
| 57 | + cli.Builder.SetBuilder.Run(cfg, cli.Builder.File) |
| 58 | + case "builder set-runner <version>": |
| 59 | + cfg := mustLoadBuilder(cli.Builder.File) |
| 60 | + cli.Builder.SetRunner.Run(cfg, cli.Builder.File) |
| 61 | + case "buildpacks set-version <version>": |
| 62 | + cli.Buildpacks.SetVersion.Run() |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func (c *SetBuildpacksCmd) Run(cfg *BuilderConfig, path string) { |
| 67 | + changed := 0 |
| 68 | + buildpacks := getBuildpacks() |
| 69 | + |
| 70 | + for _, bp := range buildpacks { |
| 71 | + applyToBuildpacks(cfg.Buildpacks, fmt.Sprintf("../../buildpacks/%s", bp), c.Version, "[[buildpacks]]", &changed) |
| 72 | + } |
| 73 | + for _, bp := range buildpacks { |
| 74 | + applyToOrder(cfg.Order, fmt.Sprintf("renku/%s", bp), c.Version, "[[order]]", &changed) |
| 75 | + } |
| 76 | + |
| 77 | + if changed == 0 { |
| 78 | + fmt.Fprintf(os.Stderr, "warning: no entries matching found in builder at %q\n", path) |
| 79 | + os.Exit(1) |
| 80 | + } |
| 81 | + |
| 82 | + mustSaveBuilder(path, cfg) |
| 83 | + fmt.Printf("\n✓ Updated %d field(s) in %s\n", changed, path) |
| 84 | +} |
| 85 | + |
| 86 | +func (c *SetBuilderCmd) Run(cfg *BuilderConfig, path string) { |
| 87 | + oldImg := cfg.Build.Image |
| 88 | + newImg, _ := updateTag(oldImg, c.Version) |
| 89 | + cfg.Build.Image = newImg |
| 90 | + fmt.Printf("✓ Updated builder at %s %s -> %s\n", path, oldImg, newImg) |
| 91 | + mustSaveBuilder(path, cfg) |
| 92 | +} |
| 93 | + |
| 94 | +func (c *SetRunnerCmd) Run(cfg *BuilderConfig, path string) { |
| 95 | + for runImgInd, runImg := range cfg.Run.Images { |
| 96 | + oldImg := runImg.Image |
| 97 | + newImg, _ := updateTag(oldImg, c.Version) |
| 98 | + cfg.Run.Images[runImgInd].Image = newImg |
| 99 | + fmt.Printf("✓ Updated runner at %s with ind %d %s -> %s\n", path, runImgInd, oldImg, newImg) |
| 100 | + mustSaveBuilder(path, cfg) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func (c *SetBuildpackVersionCmd) Run() { |
| 105 | + bps := getBuildpacks() |
| 106 | + for _, bp := range bps { |
| 107 | + path := fmt.Sprintf("buildpacks/%s/buildpack.toml", bp) |
| 108 | + spec := mustLoadBuildpack(path) |
| 109 | + spec.Buildpack["version"] = c.Version |
| 110 | + fmt.Printf("Updated buildpack at %s to %s\n", path, c.Version) |
| 111 | + mustSaveBuildpack(path, spec) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// mutation helpers |
| 116 | + |
| 117 | +func applyToBuildpacks(bps []BuilderBuildpack, target, newVer, section string, changed *int) { |
| 118 | + for i := range bps { |
| 119 | + bp := &bps[i] |
| 120 | + if !matchesTarget(bp, target) { |
| 121 | + continue |
| 122 | + } |
| 123 | + if bp.Version != "" && bp.Version != newVer { |
| 124 | + fmt.Printf(" %s uri=%s version %q -> %q\n", section, target, bp.Version, newVer) |
| 125 | + bp.Version = newVer |
| 126 | + (*changed)++ |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func applyToOrder(orders []Order, target, newVer, section string, changed *int) { |
| 132 | + for oi := range orders { |
| 133 | + for gi := range orders[oi].Group { |
| 134 | + g := &orders[oi].Group[gi] |
| 135 | + if g.ID != target { |
| 136 | + continue |
| 137 | + } |
| 138 | + if g.Version != newVer { |
| 139 | + fmt.Printf(" %s id=%s version %q -> %q\n", section, g.ID, g.Version, newVer) |
| 140 | + g.Version = newVer |
| 141 | + (*changed)++ |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +// toml I/O |
| 148 | + |
| 149 | +func mustLoadBuilder(path string) *BuilderConfig { |
| 150 | + var cfg BuilderConfig |
| 151 | + if _, err := toml.DecodeFile(path, &cfg); err != nil { |
| 152 | + fmt.Fprintf(os.Stderr, "error: cannot parse %s: %v\n", path, err) |
| 153 | + os.Exit(1) |
| 154 | + } |
| 155 | + return &cfg |
| 156 | +} |
| 157 | + |
| 158 | +func mustSaveBuilder(path string, cfg *BuilderConfig) { |
| 159 | + f, err := os.Create(path) |
| 160 | + if err != nil { |
| 161 | + fmt.Fprintf(os.Stderr, "error: cannot write %s: %v\n", path, err) |
| 162 | + os.Exit(1) |
| 163 | + } |
| 164 | + defer func() { |
| 165 | + err := f.Close() |
| 166 | + fmt.Fprintf(os.Stderr, "error: cannot close file: %v\n", err) |
| 167 | + os.Exit(1) |
| 168 | + }() |
| 169 | + if err := toml.NewEncoder(f).Encode(cfg); err != nil { |
| 170 | + fmt.Fprintf(os.Stderr, "error: cannot encode toml: %v\n", err) |
| 171 | + os.Exit(1) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func mustLoadBuildpack(path string) *BuildpackSpec { |
| 176 | + var cfg BuildpackSpec |
| 177 | + _, err := toml.DecodeFile(path, &cfg) |
| 178 | + if err != nil { |
| 179 | + fmt.Fprintf(os.Stderr, "error: cannot parse %s: %v\n", path, err) |
| 180 | + os.Exit(1) |
| 181 | + } |
| 182 | + return &cfg |
| 183 | +} |
| 184 | + |
| 185 | +func mustSaveBuildpack(path string, cfg *BuildpackSpec) { |
| 186 | + f, err := os.Create(path) |
| 187 | + if err != nil { |
| 188 | + fmt.Fprintf(os.Stderr, "error: cannot write %s: %v\n", path, err) |
| 189 | + os.Exit(1) |
| 190 | + } |
| 191 | + defer func() { |
| 192 | + err := f.Close() |
| 193 | + fmt.Fprintf(os.Stderr, "error: cannot close file: %v\n", err) |
| 194 | + os.Exit(1) |
| 195 | + }() |
| 196 | + if err := toml.NewEncoder(f).Encode(cfg); err != nil { |
| 197 | + fmt.Fprintf(os.Stderr, "error: cannot encode toml: %v\n", err) |
| 198 | + os.Exit(1) |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +// utilities |
| 203 | + |
| 204 | +func matchesTarget(bp *BuilderBuildpack, target string) bool { |
| 205 | + return strings.Trim(bp.URI, " \n\t") == target || strings.Trim(bp.ID, " \n\t") == target |
| 206 | +} |
| 207 | + |
| 208 | +func updateTag(ref, newTag string) (string, bool) { |
| 209 | + i := strings.LastIndex(ref, ":") |
| 210 | + if i < 0 { |
| 211 | + return ref, false |
| 212 | + } |
| 213 | + if ref[i+1:] == newTag { |
| 214 | + return ref, false |
| 215 | + } |
| 216 | + return ref[:i+1] + newTag, true |
| 217 | +} |
| 218 | + |
| 219 | +func getBuildpacks() []string { |
| 220 | + buildpackDirsRaw, err := os.ReadDir("buildpacks") |
| 221 | + buildpackDirs := []string{} |
| 222 | + if err != nil { |
| 223 | + fmt.Fprintf(os.Stderr, "error: %v", err) |
| 224 | + } |
| 225 | + for _, e := range buildpackDirsRaw { |
| 226 | + if e.IsDir() { |
| 227 | + buildpackDirs = append(buildpackDirs, e.Name()) |
| 228 | + } |
| 229 | + } |
| 230 | + return buildpackDirs |
| 231 | +} |
0 commit comments