-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
884 lines (806 loc) · 27.5 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/go-git/go-git/v5"
"github.com/rancher/charts-build-scripts/pkg/auto"
"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/images"
"github.com/rancher/charts-build-scripts/pkg/lifecycle"
"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"
// defaultBranchVersionEnvironmentVariable is the default environment variable that indicates the branch version to compare against
defaultBranchVersionEnvironmentVariable = "BRANCH_VERSION"
// defaultBranchEnvironmentVariable is the default environment variable that indicates the branch
defaultBranchEnvironmentVariable = "BRANCH"
// defaultForkEnvironmentVariable is the default environment variable that indicates the fork URL
defaultForkEnvironmentVariable = "FORK"
// defaultChartVersionEnvironmentVariable is the default environment variable that indicates the version to release
defaultChartVersionEnvironmentVariable = "CHART_VERSION"
// defaultGHTokenEnvironmentVariable is the default environment variable that indicates the Github Auth token
defaultGHTokenEnvironmentVariable = "GH_TOKEN"
// defaultPRNumberEnvironmentVariable is the default environment variable that indicates the PR number
defaultPRNumberEnvironmentVariable = "PR_NUMBER"
)
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
// 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
// ForkURL represents the fork URL configured as a remote in your local git repository
ForkURL = ""
// ChartVersion of the chart to release
ChartVersion = ""
// Branch repsents the branch to compare against
Branch = ""
// PullRequest represents the Pull Request identifying number
PullRequest = ""
// GithubToken represents the Github Auth token
GithubToken string
)
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"
// Flags
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: `Usage:
./bin/charts-build-scripts <some_command> --chart="chart-name"
CHART=<chart_name> make <some_command>
A chart you would like to run the scripts on. Can include version.
Default Environment Variable:
`,
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,
}
branchVersionFlag := cli.StringFlag{
Name: "branch-version",
Usage: `Usage:
./bin/charts-build-scripts <command> --branch-version="x.y"
BRANCH_VERSION="x.y" make <command>
The branch version line to compare against.
Available inputs: (2.5; 2.6; 2.7; 2.8; 2.9; 2.10; 2.11; 2.12).
Default Environment Variable:
`,
Required: true,
EnvVar: defaultBranchVersionEnvironmentVariable,
}
forkFlag := cli.StringFlag{
Name: "fork",
Usage: `Usage:
./bin/charts-build-scripts <command> --fork="<fork-URL>"
FORK="<fork-URL>" make <command>
Your fork URL configured as a remote in your local git repository.
`,
Required: true,
Destination: &ForkURL,
EnvVar: defaultForkEnvironmentVariable,
}
chartVersionFlag := cli.StringFlag{
Name: "version",
Usage: `Usage:
./bin/charts-build-scripts <command> --version="<chart_version>"
CHART_VERSION="<chart_version>" make <command>
Target version of chart to release.
`,
Required: true,
Destination: &ChartVersion,
EnvVar: defaultChartVersionEnvironmentVariable,
}
branchFlag := cli.StringFlag{
Name: "branch,b",
Usage: `Usage:
./bin/charts-build-scripts <command> --branch="release-v2.y" OR
BRANCH="dev-v2.y" make <command>
Available branches: (release-v2.8; dev-v2.9; release-v2.10.)
`,
Required: true,
EnvVar: defaultBranchEnvironmentVariable,
Destination: &Branch,
}
localModeFlag := cli.BoolFlag{
Name: "local,l",
Usage: "Only perform local validation of the contents of assets and charts",
Required: false,
Destination: &LocalMode,
}
remoteModeFlag := cli.BoolFlag{
Name: "remote,r",
Usage: "Only perform upstream validation of the contents of assets and charts",
Required: false,
Destination: &RemoteMode,
}
ghTokenFlag := cli.StringFlag{
Name: "gh_token",
Usage: `Usage:
./bin/charts-build-scripts <command> --gh_token="********"
GH_TOKEN="*********" make <command>
Github Auth Token provided by Github Actions job
`,
Required: true,
EnvVar: defaultGHTokenEnvironmentVariable,
Destination: &GithubToken,
}
prNumberFlag := cli.StringFlag{
Name: "pr_number",
Usage: `Usage:
./bin/charts-build-scripts <command> --pr_number="****"
PR_NUMBER="****" make <command>
Pull Request identifying number provided by Github Actions job
`,
Required: true,
EnvVar: defaultPRNumberEnvironmentVariable,
Destination: &PullRequest,
}
skipFlag := cli.BoolFlag{
Name: "skip",
Usage: "Skip the execution and return success",
}
// Commands
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,
Flags: []cli.Flag{},
},
{
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, localModeFlag, remoteModeFlag},
},
{
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{
// TODO: verify if this is the correct way to pass the variables
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",
},
},
},
{
Name: "check-images",
Usage: "Checks all container images used in the charts repository",
Action: checkImages,
},
{
Name: "check-rc",
Usage: "Checks if there are any images with RC tags or charts with RC versions in the charts repository",
Action: checkRCTagsAndVersions,
},
{
Name: "icon",
Usage: "Download the chart icon locally and use it",
Action: downloadIcon,
Flags: []cli.Flag{packageFlag, configFlag, cacheFlag},
},
{
Name: "lifecycle-status",
Usage: `Print the status of the current assets and charts based on the branch version and chart version according to the lifecycle rules.
Saves the logs in the logs/ directory.`,
Action: lifecycleStatus,
Flags: []cli.Flag{branchVersionFlag, chartFlag},
},
{
Name: "auto-forward-port",
Usage: `Execute the forward-port script to forward port a chart or all to the production branch.
The charts to be forward ported are listed in the result of lifecycle-status command.
It is advised to run make lifecycle-status before running this command.
At the end of the execution, the script will create a PR with the changes to each chart.
At the end of the execution, the script will save the logs in the logs directory,
with all assets versions and branches that were pushed to the upstream repository.
`,
Action: autoForwardPort,
Flags: []cli.Flag{branchVersionFlag, chartFlag, forkFlag},
},
{
Name: "release",
Usage: `Execute the release script to release a chart to the production branch.
`,
Action: release,
Flags: []cli.Flag{branchVersionFlag, chartFlag, chartVersionFlag, forkFlag},
},
{
Name: "validate-release-charts",
Usage: `Check charts to release in PR.
`,
Action: validateRelease,
Flags: []cli.Flag{branchFlag, ghTokenFlag, prNumberFlag, skipFlag},
},
{
Name: "compare-index-files",
Usage: `Compare the index.yaml between github repository and charts.rancher.io.
`,
Action: compareIndexFiles,
Flags: []cli.Flag{branchFlag},
},
{
Name: "chart-bump",
Usage: `Generate a new chart bump PR.
`,
Action: chartBump,
Before: setupCache,
Flags: []cli.Flag{packageFlag, branchFlag},
},
}
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 p.Auto == false {
if err := p.GenerateCharts(chartsScriptOptions.OmitBuildMetadataOnExport); err != nil {
logrus.Fatal(err)
}
} else {
repoRoot := getRepoRoot()
bump, err := auto.SetupBump(repoRoot, p.Name, "dev-v2.10", chartsScriptOptions)
if err != nil {
fmt.Printf("failed to initialize the chart bump: %s", err.Error())
bump.Pkg.Clean()
os.Exit(1)
}
if err := bump.BumpChart(); err != nil {
fmt.Printf("failed to bump the chart: %s", err.Error())
bump.Pkg.Clean()
os.Exit(1)
}
}
}
}
func downloadIcon(c *cli.Context) {
packages := getPackages()
if len(packages) == 0 {
logrus.Infof("No packages found.")
return
}
for _, p := range packages {
err := p.DownloadIcon()
if 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(defaultChartsScriptOptionsFile)
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
}
func checkImages(c *cli.Context) {
if err := images.CheckImages(); err != nil {
logrus.Fatal(err)
}
}
func checkRCTagsAndVersions(c *cli.Context) {
// Grab all images that contain RC tags
rcImageTagMap := images.CheckRCTags()
// Grab all chart versions that contain RC tags
rcChartVersionMap := charts.CheckRCCharts()
// If there are any charts that contains RC version or images that contains RC tags
// log them and return an error
if len(rcChartVersionMap) > 0 || len(rcImageTagMap) > 0 {
logrus.Errorf("found images with RC tags: %v", rcImageTagMap)
logrus.Errorf("found charts with RC version: %v", rcChartVersionMap)
logrus.Fatal("RC check has failed")
}
logrus.Info("RC check has succeeded")
}
func lifecycleStatus(c *cli.Context) {
// Initialize dependencies with branch-version and current chart
logrus.Info("Initializing dependencies for lifecycle-status")
rootFs := filesystem.GetFilesystem(getRepoRoot())
lifeCycleDep, err := lifecycle.InitDependencies(rootFs, c.String("branch-version"), CurrentChart)
if err != nil {
logrus.Fatalf("encountered error while initializing dependencies: %s", err)
}
// Execute lifecycle status check and save the logs
logrus.Info("Checking lifecycle status and saving logs")
_, err = lifeCycleDep.CheckLifecycleStatusAndSave(CurrentChart)
if err != nil {
logrus.Fatalf("Failed to check lifecycle status: %s", err)
}
}
func autoForwardPort(c *cli.Context) {
if ForkURL == "" {
logrus.Fatal("FORK environment variable must be set to run auto-forward-port")
}
// Initialize dependencies with branch-version and current chart
logrus.Info("Initializing dependencies for auto-forward-port")
rootFs := filesystem.GetFilesystem(getRepoRoot())
lifeCycleDep, err := lifecycle.InitDependencies(rootFs, c.String("branch-version"), CurrentChart)
if err != nil {
logrus.Fatalf("encountered error while initializing dependencies: %v", err)
}
// Execute lifecycle status check and save the logs
logrus.Info("Checking lifecycle status and saving logs")
status, err := lifeCycleDep.CheckLifecycleStatusAndSave(CurrentChart)
if err != nil {
logrus.Fatalf("Failed to check lifecycle status: %v", err)
}
// Execute forward port with loaded information from status
logrus.Info("Preparing forward port data")
fp, err := auto.CreateForwardPortStructure(lifeCycleDep, status.AssetsToBeForwardPorted, ForkURL)
if err != nil {
logrus.Fatalf("Failed to prepare forward port: %v", err)
}
logrus.Info("Starting forward port execution")
err = fp.ExecuteForwardPort(CurrentChart)
if err != nil {
logrus.Fatalf("Failed to execute forward port: %v", err)
}
}
func release(c *cli.Context) {
if ForkURL == "" {
logrus.Fatal("FORK environment variable must be set to run release cmd")
}
if CurrentChart == "" {
logrus.Fatal("CHART environment variable must be set to run release cmd")
}
rootFs := filesystem.GetFilesystem(getRepoRoot())
dependencies, err := lifecycle.InitDependencies(rootFs, c.String("branch-version"), CurrentChart)
if err != nil {
logrus.Fatalf("encountered error while initializing dependencies: %v", err)
}
status, err := lifecycle.LoadState(rootFs)
if err != nil {
logrus.Fatalf("could not load state; please run lifecycle-status before this command: %v", err)
}
release, err := auto.InitRelease(dependencies, status, ChartVersion, CurrentChart, ForkURL)
if err != nil {
logrus.Fatalf("failed to initialize release: %v", err)
}
if err := release.PullAsset(); err != nil {
logrus.Fatalf("failed to execute release: %v", err)
}
// Unzip Assets: ASSET=<chart>/<chart>-<version.tgz make unzip
CurrentAsset = release.Chart + "/" + release.AssetTgz
unzipAssets(c)
// update release.yaml
if err := release.UpdateReleaseYaml(); err != nil {
logrus.Fatalf("failed to update release.yaml: %v", err)
}
// make index
createOrUpdateIndex(c)
}
func validateRelease(c *cli.Context) {
if c.Bool("skip") {
fmt.Println("skipping execution...")
return
}
if GithubToken == "" {
fmt.Println("GH_TOKEN environment variable must be set to run validate-release-charts")
os.Exit(1)
}
if PullRequest == "" {
fmt.Println("PR_NUMBER environment variable must be set to run validate-release-charts")
os.Exit(1)
}
if Branch == "" {
fmt.Println("BRANCH environment variable must be set to run validate-release-charts")
os.Exit(1)
}
rootFs := filesystem.GetFilesystem(getRepoRoot())
if !strings.HasPrefix(Branch, "release-v") {
fmt.Println("Branch must be in the format release-v2.x")
os.Exit(1)
}
dependencies, err := lifecycle.InitDependencies(rootFs, strings.TrimPrefix(Branch, "release-v"), "")
if err != nil {
fmt.Printf("encountered error while initializing d: %v \n", err)
os.Exit(1)
}
if err := auto.ValidatePullRequest(GithubToken, PullRequest, dependencies); err != nil {
fmt.Printf("failed to validate pull request: %v \n", err)
os.Exit(1)
}
}
func compareIndexFiles(c *cli.Context) {
if Branch == "" {
fmt.Println("BRANCH environment variable must be set to run validate-release-charts")
os.Exit(1)
}
rootFs := filesystem.GetFilesystem(getRepoRoot())
if err := auto.CompareIndexFiles(rootFs); err != nil {
fmt.Printf("failed to compare index files: %v \n", err)
os.Exit(1)
}
fmt.Println("index.yaml files are the same at git repository and charts.rancher.io")
}
func chartBump(c *cli.Context) {
if CurrentPackage == "" {
fmt.Println("CurrentPackage environment variable must be set")
os.Exit(1)
}
if Branch == "" {
fmt.Println("Branch environment variable must be set")
os.Exit(1)
}
repoRoot := getRepoRoot()
chartsScriptOptions := parseScriptOptions()
bump, err := auto.SetupBump(repoRoot, CurrentPackage, Branch, chartsScriptOptions)
if err != nil {
fmt.Printf("failed to initialize the chart bump: %s", err.Error())
bump.Pkg.Clean()
os.Exit(1)
}
if err := bump.BumpChart(); err != nil {
fmt.Printf("failed to bump the chart: %s", err.Error())
bump.Pkg.Clean()
os.Exit(1)
}
}