|
| 1 | +// Package brokerpaks is used for downloading brokerpaks and associated resources for upgrade tests |
| 2 | +package brokerpaks |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "regexp" |
| 10 | +) |
| 11 | + |
| 12 | +const brokerpak = "cloudfoundry/csb-brokerpak-azure" |
| 13 | + |
| 14 | +// DownloadBrokerpak will download the brokerpak of the specified |
| 15 | +// version and return the directory where it has been placed. |
| 16 | +// The download is skipped if it has previously been downloaded. |
| 17 | +// Includes downloading the corresponding broker and ".envrc" file |
| 18 | +func DownloadBrokerpak(version, dir string) string { |
| 19 | + // Brokerpak |
| 20 | + basename := fmt.Sprintf("azure-services-%s.brokerpak", version) |
| 21 | + uri := fmt.Sprintf("https://github.com/%s/releases/download/%s/%s", brokerpak, version, basename) |
| 22 | + downloadUnlessCached(dir, basename, uri) |
| 23 | + |
| 24 | + // ".envrc" file |
| 25 | + envrcURI := fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/.envrc", brokerpak, version) |
| 26 | + downloadUnlessCached(dir, ".envrc", envrcURI) |
| 27 | + |
| 28 | + // broker |
| 29 | + brokerVersion := readBrokerVersion(version) |
| 30 | + brokerURI := fmt.Sprintf("https://github.com/cloudfoundry/cloud-service-broker/releases/download/%s/cloud-service-broker.linux", brokerVersion) |
| 31 | + downloadUnlessCached(dir, "cloud-service-broker", brokerURI) |
| 32 | + if err := os.Chmod(filepath.Join(dir, "cloud-service-broker"), 0777); err != nil { |
| 33 | + panic(err) |
| 34 | + } |
| 35 | + |
| 36 | + return dir |
| 37 | +} |
| 38 | + |
| 39 | +// readBrokerVersion will use the specified brokerpak version to determine the corresponding broker version |
| 40 | +func readBrokerVersion(version string) string { |
| 41 | + body := newClient().get(fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/go.mod", brokerpak, version), "text/plain") |
| 42 | + defer body.Close() |
| 43 | + data := must(io.ReadAll(body)) |
| 44 | + |
| 45 | + matches := regexp.MustCompile(`(?m)^\s*github\.com/cloudfoundry/cloud-service-broker\s+(\S+)\s*$`).FindSubmatch(data) |
| 46 | + if len(matches) != 2 { |
| 47 | + panic(fmt.Sprintf("Could not extract CSB version from go.mod file: %q", data)) |
| 48 | + } |
| 49 | + |
| 50 | + brokerVersion := string(matches[1]) |
| 51 | + fmt.Printf("Brokerpak version %q uses broker version %q\n", version, brokerVersion) |
| 52 | + return brokerVersion |
| 53 | +} |
| 54 | + |
| 55 | +// downloadUnlessCached will download a file to a known location, unless it's already there |
| 56 | +func downloadUnlessCached(dir, basename, uri string) { |
| 57 | + target := filepath.Join(dir, basename) |
| 58 | + |
| 59 | + _, err := os.Stat(target) |
| 60 | + switch err { |
| 61 | + case nil: |
| 62 | + fmt.Printf("Found %q cached at %q.\n", uri, target) |
| 63 | + default: |
| 64 | + fmt.Printf("Downloading %q to %q.\n", uri, target) |
| 65 | + newClient().download(target, uri) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// TargetDir will determine the target directory for a version and make sure that it exists |
| 70 | +func TargetDir(version string) string { |
| 71 | + pwd, err := os.Getwd() |
| 72 | + if err != nil { |
| 73 | + panic(err) |
| 74 | + } |
| 75 | + |
| 76 | + dir := filepath.Join(pwd, "versions", version) |
| 77 | + if err := os.MkdirAll(dir, 0777); err != nil { |
| 78 | + panic(err) |
| 79 | + } |
| 80 | + |
| 81 | + return dir |
| 82 | +} |
0 commit comments