|
| 1 | +package auto |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/rancher/charts-build-scripts/pkg/filesystem" |
| 10 | + "github.com/rancher/charts-build-scripts/pkg/git" |
| 11 | + "github.com/rancher/charts-build-scripts/pkg/lifecycle" |
| 12 | + "github.com/rancher/charts-build-scripts/pkg/path" |
| 13 | + "gopkg.in/yaml.v3" |
| 14 | +) |
| 15 | + |
| 16 | +// Release holds necessary metadata to release a chart version |
| 17 | +type Release struct { |
| 18 | + git *git.Git |
| 19 | + VR *lifecycle.VersionRules |
| 20 | + AssetTgz string |
| 21 | + AssetPath string |
| 22 | + ChartVersion string |
| 23 | + Chart string |
| 24 | + ReleaseYamlPath string |
| 25 | + ForkRemoteURL string |
| 26 | +} |
| 27 | + |
| 28 | +// InitRelease will create the Release struct with access to the necessary dependencies. |
| 29 | +func InitRelease(d *lifecycle.Dependencies, s *lifecycle.Status, v, c, f string) (*Release, error) { |
| 30 | + r := &Release{ |
| 31 | + git: d.Git, |
| 32 | + VR: d.VR, |
| 33 | + ChartVersion: v, |
| 34 | + Chart: c, |
| 35 | + ForkRemoteURL: f, |
| 36 | + } |
| 37 | + |
| 38 | + var ok bool |
| 39 | + var assetVersions []lifecycle.Asset |
| 40 | + |
| 41 | + assetVersions, ok = s.AssetsToBeReleased[r.Chart] |
| 42 | + if !ok { |
| 43 | + assetVersions, ok = s.AssetsToBeForwardPorted[r.Chart] |
| 44 | + if !ok { |
| 45 | + return nil, errors.New("no asset version to release for chart:" + r.Chart) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + var assetVersion string |
| 50 | + for _, version := range assetVersions { |
| 51 | + if version.Version == r.ChartVersion { |
| 52 | + assetVersion = version.Version |
| 53 | + break |
| 54 | + } |
| 55 | + } |
| 56 | + if assetVersion == "" { |
| 57 | + return nil, errors.New("no asset version to release for chart:" + r.Chart + " version:" + r.ChartVersion) |
| 58 | + } |
| 59 | + |
| 60 | + r.AssetPath, r.AssetTgz = mountAssetVersionPath(r.Chart, assetVersion) |
| 61 | + |
| 62 | + // Check again if the asset was already released in the local repository |
| 63 | + if err := checkAssetReleased(r.AssetPath); err != nil { |
| 64 | + return nil, fmt.Errorf("failed to check for chart:%s ; err: %w", r.Chart, err) |
| 65 | + } |
| 66 | + |
| 67 | + // Check if we have a release.yaml file in the expected path |
| 68 | + if exist, err := filesystem.PathExists(d.RootFs, path.RepositoryReleaseYaml); err != nil || !exist { |
| 69 | + return nil, errors.New("release.yaml not found") |
| 70 | + } |
| 71 | + |
| 72 | + r.ReleaseYamlPath = filesystem.GetAbsPath(d.RootFs, path.RepositoryReleaseYaml) |
| 73 | + |
| 74 | + return r, nil |
| 75 | +} |
| 76 | + |
| 77 | +// PullAsset will execute the release porting for a chart in the repository |
| 78 | +func (r *Release) PullAsset() error { |
| 79 | + if err := r.git.FetchBranch(r.VR.DevBranch); err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + if err := r.git.CheckFileExists(r.AssetPath, r.VR.DevBranch); err != nil { |
| 84 | + return fmt.Errorf("asset version not found in dev branch: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + if err := r.git.CheckoutFile(r.VR.DevBranch, r.AssetPath); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + return r.git.ResetHEAD() |
| 92 | +} |
| 93 | + |
| 94 | +func checkAssetReleased(chartVersion string) error { |
| 95 | + if _, err := os.Stat(chartVersion); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +// mountAssetVersionPath returns the asset path and asset tgz name for a given chart and version. |
| 103 | +// example: assets/longhorn/longhorn-100.0.0+up0.0.0.tgz |
| 104 | +func mountAssetVersionPath(chart, version string) (string, string) { |
| 105 | + assetTgz := chart + "-" + version + ".tgz" |
| 106 | + assetPath := "assets/" + chart + "/" + assetTgz |
| 107 | + return assetPath, assetTgz |
| 108 | +} |
| 109 | + |
| 110 | +func (r *Release) readReleaseYaml() (map[string][]string, error) { |
| 111 | + var releaseVersions = make(map[string][]string, 0) |
| 112 | + |
| 113 | + file, err := os.Open(r.ReleaseYamlPath) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + defer file.Close() |
| 118 | + |
| 119 | + decoder := yaml.NewDecoder(file) |
| 120 | + if err := decoder.Decode(&releaseVersions); err != nil { |
| 121 | + if err == io.EOF { |
| 122 | + // Handle EOF error gracefully |
| 123 | + return releaseVersions, nil |
| 124 | + } |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + |
| 128 | + return releaseVersions, nil |
| 129 | +} |
| 130 | + |
| 131 | +// UpdateReleaseYaml reads and parse the release.yaml file to a struct, appends the new version and writes it back to the file. |
| 132 | +func (r *Release) UpdateReleaseYaml() error { |
| 133 | + releaseVersions, err := r.readReleaseYaml() |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + // Append new version and remove duplicates if any |
| 139 | + releaseVersions[r.Chart] = append(releaseVersions[r.Chart], r.ChartVersion) |
| 140 | + releaseVersions[r.Chart] = removeDuplicates(releaseVersions[r.Chart]) |
| 141 | + |
| 142 | + // Since we opened and read the file before we can truncate it. |
| 143 | + outputFile, err := os.Create(r.ReleaseYamlPath) |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + defer outputFile.Close() |
| 148 | + |
| 149 | + encoder := yaml.NewEncoder(outputFile) |
| 150 | + encoder.SetIndent(2) |
| 151 | + if err := encoder.Encode(releaseVersions); err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + |
| 155 | + return nil |
| 156 | +} |
| 157 | + |
| 158 | +// removeDuplicates takes a slice of strings and returns a new slice with duplicates removed. |
| 159 | +func removeDuplicates(slice []string) []string { |
| 160 | + seen := make(map[string]struct{}) // map to keep track of seen strings |
| 161 | + var result []string // slice to hold the results |
| 162 | + |
| 163 | + for _, val := range slice { |
| 164 | + if _, ok := seen[val]; !ok { |
| 165 | + seen[val] = struct{}{} // mark string as seen |
| 166 | + result = append(result, val) // append to result if not seen before |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return result |
| 171 | +} |
0 commit comments