|
| 1 | +package pulp |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "slices" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/samber/lo" |
| 13 | + "gopkg.in/yaml.v3" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + pulpReposURL = "https://gitlab.com/api/v4/projects/75880111/repository/files/repos%2Frunner.yaml/raw" |
| 18 | +) |
| 19 | + |
| 20 | +type ( |
| 21 | + pulpRepository struct { |
| 22 | + Path string `yaml:"path"` |
| 23 | + } |
| 24 | + |
| 25 | + pulpRepositories struct { |
| 26 | + Deb []pulpRepository `yaml:"deb"` |
| 27 | + Rpm []pulpRepository `yaml:"rpm"` |
| 28 | + } |
| 29 | + |
| 30 | + pulpRelease struct { |
| 31 | + Repositories pulpRepositories `yaml:"repositories"` |
| 32 | + } |
| 33 | + |
| 34 | + pulpRepos struct { |
| 35 | + Stable pulpRelease `yaml:"gitlab-runner"` |
| 36 | + Unstable pulpRelease `yaml:"unstable"` |
| 37 | + } |
| 38 | + |
| 39 | + pulpConfig struct { |
| 40 | + Runner pulpRepos `yaml:"runner"` |
| 41 | + } |
| 42 | +) |
| 43 | + |
| 44 | +var ( |
| 45 | + dists = []string{"rpm", "deb"} |
| 46 | + branches = []string{"stable", "unstable"} |
| 47 | +) |
| 48 | + |
| 49 | +func Releases(dist, branch string) ([]string, error) { |
| 50 | + if !slices.Contains(dists, dist) { |
| 51 | + return nil, fmt.Errorf("unsupported package type %q", dist) |
| 52 | + } |
| 53 | + |
| 54 | + if !slices.Contains(branches, branch) { |
| 55 | + return nil, fmt.Errorf("unsupported branch %q", branch) |
| 56 | + } |
| 57 | + |
| 58 | + tokenType, tokenValue, err := getToken() |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + config, err := getPulpRunnerConfig(tokenType, tokenValue) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + return releasesForDistBranch(dist, branch, config), nil |
| 69 | +} |
| 70 | + |
| 71 | +func firstEnv(envs ...string) (string, string, bool) { |
| 72 | + for _, env := range envs { |
| 73 | + if val, ok := os.LookupEnv(env); ok { |
| 74 | + return env, val, true |
| 75 | + } |
| 76 | + } |
| 77 | + return "", "", false |
| 78 | +} |
| 79 | + |
| 80 | +func getToken() (string, string, error) { |
| 81 | + tokenType, tokenValue, ok := firstEnv("CI_JOB_TOKEN", "PRIVATE_TOKEN") |
| 82 | + if !ok { |
| 83 | + return "", "", errors.New("required 'CI_JOB_TOKEN' or 'PRIVATE_TOKEN' variable missing") |
| 84 | + } |
| 85 | + |
| 86 | + if tokenValue == "" { |
| 87 | + return "", "", fmt.Errorf("%s cannot be empty", tokenType) |
| 88 | + } |
| 89 | + |
| 90 | + tokenType = strings.ReplaceAll(tokenType, "_", "-") |
| 91 | + |
| 92 | + return tokenType, tokenValue, nil |
| 93 | +} |
| 94 | + |
| 95 | +func releasesForDistBranch(dist, branch string, config *pulpConfig) []string { |
| 96 | + var release pulpRelease |
| 97 | + switch branch { |
| 98 | + case "stable": |
| 99 | + release = config.Runner.Stable |
| 100 | + case "unstable": |
| 101 | + release = config.Runner.Unstable |
| 102 | + } |
| 103 | + |
| 104 | + var repos []pulpRepository |
| 105 | + switch dist { |
| 106 | + case "deb": |
| 107 | + repos = release.Repositories.Deb |
| 108 | + case "rpm": |
| 109 | + repos = release.Repositories.Rpm |
| 110 | + } |
| 111 | + |
| 112 | + return lo.Map(repos, func(repo pulpRepository, _ int) string { |
| 113 | + return repo.Path |
| 114 | + }) |
| 115 | +} |
| 116 | + |
| 117 | +// The full Pulp runner repo config file can be enjoyed at |
| 118 | +// https://gitlab.com/gitlab-org/build/pulp-repository-automation/-/blob/main/repos/runner.yaml?ref_type=heads |
| 119 | +func getPulpRunnerConfig(tokenType, tokenValue string) (*pulpConfig, error) { |
| 120 | + req, err := http.NewRequest("GET", pulpReposURL, nil) |
| 121 | + if err != nil { |
| 122 | + return nil, fmt.Errorf("failed to create request for url %q: %w", pulpReposURL, err) |
| 123 | + } |
| 124 | + req.Header.Add(tokenType, tokenValue) |
| 125 | + |
| 126 | + client := http.Client{Timeout: 20 * time.Second} |
| 127 | + resp, err := client.Do(req) |
| 128 | + if err != nil { |
| 129 | + return nil, fmt.Errorf("failed to get url %q: %w", pulpReposURL, err) |
| 130 | + } |
| 131 | + defer resp.Body.Close() |
| 132 | + |
| 133 | + if resp.StatusCode != http.StatusOK { |
| 134 | + return nil, fmt.Errorf("got unexpected response status code: %s", resp.Status) |
| 135 | + } |
| 136 | + |
| 137 | + result := pulpConfig{} |
| 138 | + if err := yaml.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 139 | + return nil, fmt.Errorf("failed to decode response body: %w", err) |
| 140 | + } |
| 141 | + |
| 142 | + return &result, nil |
| 143 | +} |
0 commit comments