forked from rancher/charts-build-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
481 lines (447 loc) · 15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/go-git/go-git/v5"
"github.com/rancher/charts-build-scripts/pkg/charts"
"github.com/rancher/charts-build-scripts/pkg/filesystem"
"github.com/rancher/charts-build-scripts/pkg/helm"
"github.com/rancher/charts-build-scripts/pkg/options"
"github.com/rancher/charts-build-scripts/pkg/path"
"github.com/rancher/charts-build-scripts/pkg/puller"
"github.com/rancher/charts-build-scripts/pkg/regsync"
"github.com/rancher/charts-build-scripts/pkg/repository"
"github.com/rancher/charts-build-scripts/pkg/standardize"
"github.com/rancher/charts-build-scripts/pkg/update"
"github.com/rancher/charts-build-scripts/pkg/validate"
"github.com/rancher/charts-build-scripts/pkg/zip"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
)
const (
// DefaultChartsScriptOptionsFile is the default path to look a file containing options for the charts scripts to use for this branch
DefaultChartsScriptOptionsFile = "configuration.yaml"
// DefaultPackageEnvironmentVariable is the default environment variable for picking a specific package
DefaultPackageEnvironmentVariable = "PACKAGE"
// DefaultChartEnvironmentVariable is the default environment variable for picking a specific chart
DefaultChartEnvironmentVariable = "CHART"
// DefaultAssetEnvironmentVariable is the default environment variable for picking a specific asset
DefaultAssetEnvironmentVariable = "ASSET"
// DefaultPorcelainEnvironmentVariable is the default environment variable that indicates whether we should run on porcelain mode
DefaultPorcelainEnvironmentVariable = "PORCELAIN"
// DefaultCacheEnvironmentVariable is the default environment variable that indicates that a cache should be used on pulls to remotes
DefaultCacheEnvironmentVariable = "USE_CACHE"
)
var (
// Version represents the current version of the chart build scripts
Version = "v0.0.0-dev"
// GitCommit represents the latest commit when building this script
GitCommit = "HEAD"
// ChartsScriptOptionsFile represents a name of a file that contains options for the charts script to use for this branch
ChartsScriptOptionsFile string
// GithubToken represents the Github Auth token; currently not used
GithubToken string
// CurrentPackage represents the specific package to apply the scripts to
CurrentPackage string
// CurrentChart represents a specific chart to apply the scripts to. Also accepts a specific version.
CurrentChart string
// CurrentAsset represents a specific asset to apply the scripts to. Also accepts a specific archive.
CurrentAsset string
// PorcelainMode indicates that the output of the scripts should be in an easy-to-parse format for scripts
PorcelainMode bool
// LocalMode indicates that only local validation should be run
LocalMode bool
// RemoteMode indicates that only remote validation should be run
RemoteMode bool
// CacheMode indicates that caching should be used on all remotely pulled resources
CacheMode = false
)
func main() {
if len(os.Getenv("DEBUG")) > 0 {
logrus.SetLevel(logrus.DebugLevel)
}
app := cli.NewApp()
app.Name = "charts-build-scripts"
app.Version = fmt.Sprintf("%s (%s)", Version, GitCommit)
app.Usage = "Build scripts used to maintain patches on Helm charts forked from other repositories"
configFlag := cli.StringFlag{
Name: "config",
Usage: "A configuration file with additional options for allowing this branch to interact with other branches",
TakesFile: true,
Destination: &ChartsScriptOptionsFile,
Value: DefaultChartsScriptOptionsFile,
}
packageFlag := cli.StringFlag{
Name: "package,p",
Usage: "A package you would like to run the scripts on",
Required: false,
Destination: &CurrentPackage,
EnvVar: DefaultPackageEnvironmentVariable,
}
chartFlag := cli.StringFlag{
Name: "chart,c",
Usage: "A chart you would like to run the scripts on. Can include version.",
Required: false,
Destination: &CurrentChart,
EnvVar: DefaultChartEnvironmentVariable,
}
assetFlag := cli.StringFlag{
Name: "asset,a",
Usage: "An asset you would like to run the scripts on. Can directly point to archive.",
Required: false,
Destination: &CurrentAsset,
EnvVar: DefaultAssetEnvironmentVariable,
}
porcelainFlag := cli.BoolFlag{
Name: "porcelain",
Usage: "Print the output of the command in a easy-to-parse format for scripts",
Required: false,
Destination: &PorcelainMode,
EnvVar: DefaultPorcelainEnvironmentVariable,
}
cacheFlag := cli.BoolFlag{
Name: "useCache",
Usage: "Experimental: use a cache to speed up scripts",
Required: false,
Destination: &CacheMode,
EnvVar: DefaultCacheEnvironmentVariable,
}
app.Commands = []cli.Command{
{
Name: "list",
Usage: "Print a list of all packages tracked in the current repository",
Action: listPackages,
Flags: []cli.Flag{packageFlag, porcelainFlag},
},
{
Name: "prepare",
Usage: "Pull in the chart specified from upstream to the charts directory and apply any patch files",
Action: prepareCharts,
Before: setupCache,
Flags: []cli.Flag{packageFlag, cacheFlag},
},
{
Name: "patch",
Usage: "Apply a patch between the upstream chart and the current state of the chart in the charts directory",
Action: generatePatch,
Before: setupCache,
Flags: []cli.Flag{packageFlag, cacheFlag},
},
{
Name: "charts",
Usage: "Create a local chart archive of your finalized chart for testing",
Action: generateCharts,
Before: setupCache,
Flags: []cli.Flag{packageFlag, configFlag, cacheFlag},
},
{
Name: "regsync",
Usage: "Create a regsync config file containing all images used for the particular Rancher version",
Action: generateRegSyncConfigFile,
},
{
Name: "index",
Usage: "Create or update the existing Helm index.yaml at the repository root",
Action: createOrUpdateIndex,
},
{
Name: "zip",
Usage: "Take the contents of a chart under charts/ and rezip the asset if it has been changed",
Action: zipCharts,
Flags: []cli.Flag{chartFlag},
},
{
Name: "unzip",
Usage: "Take the contents of an asset under assets/ and unzip the chart",
Action: unzipAssets,
Flags: []cli.Flag{assetFlag},
},
{
Name: "clean",
Usage: "Clean up your current repository to get it ready for a PR",
Action: cleanRepo,
Flags: []cli.Flag{packageFlag},
},
{
Name: "clean-cache",
Usage: "Experimental: Clean cache",
Action: cleanCache,
},
{
Name: "validate",
Usage: "Run validation to ensure that contents of assets and charts won't overwrite released charts",
Action: validateRepo,
Flags: []cli.Flag{packageFlag, configFlag, cli.BoolFlag{
Name: "local,l",
Usage: "Only perform local validation of the contents of assets and charts",
Required: false,
Destination: &LocalMode,
}, cli.BoolFlag{
Name: "remote,r",
Usage: "Only perform upstream validation of the contents of assets and charts",
Required: false,
Destination: &RemoteMode,
},
},
},
{
Name: "standardize",
Usage: "Standardize a Helm repository to the expected assets, charts, and index.yaml structure of these scripts",
Action: standardizeRepo,
Flags: []cli.Flag{packageFlag, configFlag},
},
{
Usage: "Updates the current directory by applying the configuration.yaml on upstream Go templates to pull in the most up-to-date docs, scripts, etc.",
Name: "template",
Action: createOrUpdateTemplate,
Flags: []cli.Flag{
configFlag,
cli.StringFlag{
Name: "repositoryUrl,r",
Required: false,
Destination: &update.ChartsBuildScriptsRepositoryURL,
Value: "https://github.com/rancher/charts-build-scripts.git",
},
cli.StringFlag{
Name: "branch,b",
Required: false,
Destination: &update.ChartsBuildScriptsRepositoryBranch,
Value: "master",
},
},
},
}
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}
func listPackages(c *cli.Context) {
repoRoot := getRepoRoot()
packageList, err := charts.ListPackages(repoRoot, CurrentPackage)
if err != nil {
logrus.Fatal(err)
}
if PorcelainMode {
fmt.Println(strings.Join(packageList, " "))
return
}
logrus.Infof("Found the following packages: %v", packageList)
}
func prepareCharts(c *cli.Context) {
packages := getPackages()
if len(packages) == 0 {
logrus.Fatal("Could not find any packages in packages/")
}
for _, p := range packages {
if err := p.Prepare(); err != nil {
logrus.Fatal(err)
}
}
}
func generatePatch(c *cli.Context) {
packages := getPackages()
if len(packages) == 0 {
logrus.Infof("No packages found.")
return
}
if len(packages) != 1 {
packageNames := make([]string, len(packages))
for i, pkg := range packages {
packageNames[i] = pkg.Name
}
logrus.Fatalf(
"PACKAGE=\"%s\" must be set to point to exactly one package. Currently found the following packages: %s",
CurrentPackage, packageNames,
)
}
if err := packages[0].GeneratePatch(); err != nil {
logrus.Fatal(err)
}
}
func generateCharts(c *cli.Context) {
packages := getPackages()
if len(packages) == 0 {
logrus.Infof("No packages found.")
return
}
chartsScriptOptions := parseScriptOptions()
for _, p := range packages {
if err := p.GenerateCharts(chartsScriptOptions.OmitBuildMetadataOnExport); err != nil {
logrus.Fatal(err)
}
}
}
func generateRegSyncConfigFile(c *cli.Context) {
if err := regsync.GenerateConfigFile(); err != nil {
logrus.Fatal(err)
}
}
func createOrUpdateIndex(c *cli.Context) {
repoRoot := getRepoRoot()
if err := helm.CreateOrUpdateHelmIndex(filesystem.GetFilesystem(repoRoot)); err != nil {
logrus.Fatal(err)
}
}
func zipCharts(c *cli.Context) {
repoRoot := getRepoRoot()
if err := zip.ArchiveCharts(repoRoot, CurrentChart); err != nil {
logrus.Fatal(err)
}
createOrUpdateIndex(c)
}
func unzipAssets(c *cli.Context) {
repoRoot := getRepoRoot()
if err := zip.DumpAssets(repoRoot, CurrentAsset); err != nil {
logrus.Fatal(err)
}
createOrUpdateIndex(c)
}
func cleanRepo(c *cli.Context) {
packages := getPackages()
if len(packages) == 0 {
logrus.Infof("No packages found.")
return
}
for _, p := range packages {
if err := p.Clean(); err != nil {
logrus.Fatal(err)
}
}
}
func validateRepo(c *cli.Context) {
if LocalMode && RemoteMode {
logrus.Fatalf("cannot specify both local and remote validation")
}
CurrentPackage = "" // Validate always runs on all packages
chartsScriptOptions := parseScriptOptions()
logrus.Infof("Checking if Git is clean")
_, _, status := getGitInfo()
if !status.IsClean() {
logrus.Warnf("Git is not clean:\n%s", status)
logrus.Fatal("Repository must be clean to run validation")
}
if RemoteMode {
logrus.Infof("Running remote validation only, skipping generating charts locally")
} else {
logrus.Infof("Generating charts")
generateCharts(c)
logrus.Infof("Checking if Git is clean after generating charts")
_, _, status = getGitInfo()
if !status.IsClean() {
logrus.Warnf("Generated charts produced the following changes in Git.\n%s", status)
logrus.Fatalf("Please commit these changes and run validation again.")
}
logrus.Infof("Successfully validated that current charts and assets are up to date.")
}
if chartsScriptOptions.ValidateOptions != nil {
if LocalMode {
logrus.Infof("Running local validation only, skipping pulling upstream")
} else {
repoRoot := getRepoRoot()
repoFs := filesystem.GetFilesystem(repoRoot)
releaseOptions, err := options.LoadReleaseOptionsFromFile(repoFs, "release.yaml")
if err != nil {
logrus.Fatalf("Unable to unmarshall release.yaml: %s", err)
}
u := chartsScriptOptions.ValidateOptions.UpstreamOptions
branch := chartsScriptOptions.ValidateOptions.Branch
logrus.Infof("Performing upstream validation against repository %s at branch %s", u.URL, branch)
compareGeneratedAssetsResponse, err := validate.CompareGeneratedAssets(repoFs, u, branch, releaseOptions)
if err != nil {
logrus.Fatal(err)
}
if !compareGeneratedAssetsResponse.PassedValidation() {
// Output charts that have been modified
compareGeneratedAssetsResponse.LogDiscrepancies()
logrus.Infof("Dumping release.yaml tracking changes that have been introduced")
if err := compareGeneratedAssetsResponse.DumpReleaseYaml(repoFs); err != nil {
logrus.Errorf("Unable to dump newly generated release.yaml: %s", err)
}
logrus.Infof("Updating index.yaml")
if err := helm.CreateOrUpdateHelmIndex(repoFs); err != nil {
logrus.Fatal(err)
}
logrus.Fatalf("Validation against upstream repository %s at branch %s failed.", u.URL, branch)
}
}
}
logrus.Info("Zipping charts to ensure that contents of assets, charts, and index.yaml are in sync.")
zipCharts(c)
logrus.Info("Doing a final check to ensure Git is clean")
_, _, status = getGitInfo()
if !status.IsClean() {
logrus.Warnf("Git is not clean:\n%s", status)
logrus.Fatal("Repository must be clean to pass validation")
}
logrus.Info("Successfully validated current repository!")
}
func standardizeRepo(c *cli.Context) {
repoRoot := getRepoRoot()
repoFs := filesystem.GetFilesystem(repoRoot)
if err := standardize.RestructureChartsAndAssets(repoFs); err != nil {
logrus.Fatal(err)
}
}
func createOrUpdateTemplate(c *cli.Context) {
repoRoot := getRepoRoot()
repoFs := filesystem.GetFilesystem(repoRoot)
chartsScriptOptions := parseScriptOptions()
if err := update.ApplyUpstreamTemplate(repoFs, *chartsScriptOptions); err != nil {
logrus.Fatalf("Failed to update repository based on upstream template: %s", err)
}
logrus.Infof("Successfully updated repository based on upstream template.")
}
func setupCache(c *cli.Context) error {
return puller.InitRootCache(CacheMode, path.DefaultCachePath)
}
func cleanCache(c *cli.Context) {
if err := puller.CleanRootCache(path.DefaultCachePath); err != nil {
logrus.Fatal(err)
}
}
func parseScriptOptions() *options.ChartsScriptOptions {
configYaml, err := ioutil.ReadFile(ChartsScriptOptionsFile)
if err != nil {
logrus.Fatalf("Unable to find configuration file: %s", err)
}
chartsScriptOptions := options.ChartsScriptOptions{}
if err := yaml.UnmarshalStrict(configYaml, &chartsScriptOptions); err != nil {
logrus.Fatalf("Unable to unmarshall configuration file: %s", err)
}
return &chartsScriptOptions
}
func getRepoRoot() string {
repoRoot, err := os.Getwd()
if err != nil {
logrus.Fatalf("Unable to get current working directory: %s", err)
}
return repoRoot
}
func getPackages() []*charts.Package {
repoRoot := getRepoRoot()
packages, err := charts.GetPackages(repoRoot, CurrentPackage)
if err != nil {
logrus.Fatal(err)
}
return packages
}
func getGitInfo() (*git.Repository, *git.Worktree, git.Status) {
repoRoot := getRepoRoot()
repo, err := repository.GetRepo(repoRoot)
if err != nil {
logrus.Fatal(err)
}
// Check if git is clean
wt, err := repo.Worktree()
if err != nil {
logrus.Fatal(err)
}
status, err := wt.Status()
if err != nil {
logrus.Fatal(err)
}
return repo, wt, status
}