forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promote.go
663 lines (609 loc) · 17.8 KB
/
promote.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
package cmd
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/blang/semver"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
)
const (
optionEnvironment = "environment"
optionApplication = "app"
optionTimeout = "timeout"
optionPullRequestPollTime = "pull-request-poll-time"
)
// PromoteOptions containers the CLI options
type PromoteOptions struct {
CommonOptions
Namespace string
Environment string
Application string
Version string
LocalHelmRepoName string
HelmRepositoryURL string
Preview bool
NoHelmUpdate bool
AllAutomatic bool
Timeout string
PullRequestPollTime string
TimeoutDuration *time.Duration
PullRequestPollDuration *time.Duration
}
type ReleaseInfo struct {
ReleaseName string
FullAppName string
Version string
PullRequestInfo *ReleasePullRequestInfo
}
type ReleasePullRequestInfo struct {
GitProvider gits.GitProvider
PullRequest *gits.GitPullRequest
PullRequestArguments *gits.GitPullRequestArguments
}
var (
promote_long = templates.LongDesc(`
Promotes a version of an application to an environment.
`)
promote_example = templates.Examples(`
# Promote a version of the current application to staging
# discovering the application name from the source code
jx promote --version 1.2.3 --env staging
# Promote a version of the myapp application to production
jx promote myapp --version 1.2.3 --env prod
`)
)
// NewCmdPromote creates the new command for: jx get prompt
func NewCmdPromote(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &PromoteOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "promote [application]",
Short: "Promotes a version of an application to an environment",
Long: promote_long,
Example: promote_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
cmdutil.CheckErr(err)
},
}
options.addCommonFlags(cmd)
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "The Namespace to promote to")
cmd.Flags().StringVarP(&options.Environment, optionEnvironment, "e", "", "The Environment to promote to")
cmd.Flags().StringVarP(&options.Application, optionApplication, "a", "", "The Application to promote")
cmd.Flags().StringVarP(&options.Version, "version", "v", "", "The Version to promote")
cmd.Flags().StringVarP(&options.LocalHelmRepoName, "helm-repo-name", "r", kube.LocalHelmRepoName, "The name of the helm repository that contains the app")
cmd.Flags().StringVarP(&options.HelmRepositoryURL, "helm-repo-url", "u", helm.DefaultHelmRepositoryURL, "The Helm Repository URL to use for the App")
cmd.Flags().StringVarP(&options.Timeout, optionTimeout, "t", "", "The timeout to wait for the promotion to succeed in the underlying Environment. The command fails if the timeout is exceeded or the promotion does not complete")
cmd.Flags().StringVarP(&options.PullRequestPollTime, optionPullRequestPollTime, "", "20s", "Poll time when waiting for a Pull Request to merge")
cmd.Flags().BoolVarP(&options.Preview, "preview", "p", false, "Whether to create a new Preview environment for the app")
cmd.Flags().BoolVarP(&options.NoHelmUpdate, "no-helm-update", "", false, "Allows the 'helm repo update' command if you are sure your local helm cache is up to date with the version you wish to promote")
cmd.Flags().BoolVarP(&options.AllAutomatic, "all-auto", "", false, "Promote to all automatic environments in order")
return cmd
}
// Run implements this command
func (o *PromoteOptions) Run() error {
app := o.Application
if app == "" {
args := o.Args
if len(args) == 0 {
var err error
app, err = o.discoverAppName()
if err != nil {
return err
}
} else {
app = args[0]
}
}
o.Application = app
if o.AllAutomatic {
return o.PromoteAllAutomatic()
}
targetNS, env, err := o.GetTargetNamespace(o.Namespace, o.Environment)
if err != nil {
return err
}
if o.PullRequestPollTime != "" {
duration, err := time.ParseDuration(o.PullRequestPollTime)
if err != nil {
return fmt.Errorf("Invalid duration format %s for option --%s: %s", o.PullRequestPollTime, optionPullRequestPollTime, err)
}
o.PullRequestPollDuration = &duration
}
if o.Timeout != "" {
duration, err := time.ParseDuration(o.Timeout)
if err != nil {
return fmt.Errorf("Invalid duration format %s for option --%s: %s", o.Timeout, optionTimeout, err)
}
o.TimeoutDuration = &duration
}
_, err = o.Promote(targetNS, env, true)
return err
}
func (o *PromoteOptions) PromoteAllAutomatic() error {
kubeClient, currentNs, err := o.KubeClient()
if err != nil {
return err
}
team, _, err := kube.GetDevNamespace(kubeClient, currentNs)
if err != nil {
return err
}
jxClient, _, err := o.JXClient()
if err != nil {
return err
}
envs, err := jxClient.JenkinsV1().Environments(team).List(metav1.ListOptions{})
if err != nil {
o.warnf("No Environments found: %s/n", err)
return nil
}
environments := envs.Items
if len(environments) == 0 {
o.warnf("No Environments have been created yet in team %s. Please create some via 'jx create env'\n", team)
return nil
}
kube.SortEnvironments(environments)
for _, env := range environments {
if env.Spec.PromotionStrategy == v1.PromotionStrategyTypeAutomatic {
ns := env.Spec.Namespace
releaseInfo, err := o.Promote(ns, &env, false)
if err != nil {
return err
}
err = o.WaitForPromotion(ns, &env, releaseInfo)
if err != nil {
return err
}
}
}
return nil
}
func (o *PromoteOptions) Promote(targetNS string, env *v1.Environment, warnIfAuto bool) (*ReleaseInfo, error) {
app := o.Application
if app == "" {
o.warnf("No application name could be detected so cannot promote via Helm. If the detection of the helm chart name is not working consider adding it with the --%s argument on the 'jx promomote' command\n", optionApplication)
return nil, nil
}
version := o.Version
info := util.ColorInfo
if version == "" {
o.Printf("Promoting latest version of app %s to namespace %s\n", info(app), info(targetNS))
} else {
o.Printf("Promoting app %s version %s to namespace %s\n", info(app), info(version), info(targetNS))
}
fullAppName := app
if o.LocalHelmRepoName != "" {
fullAppName = o.LocalHelmRepoName + "/" + app
}
releaseName := targetNS + "-" + app
releaseInfo := &ReleaseInfo{
ReleaseName: releaseName,
FullAppName: fullAppName,
Version: version,
}
if warnIfAuto && env != nil && env.Spec.PromotionStrategy == v1.PromotionStrategyTypeAutomatic {
o.Printf("%s", util.ColorWarning("WARNING: The Environment %s is setup to promote automatically as part of the CI / CD Pipelines.\n\n", env.Name))
confirm := &survey.Confirm{
Message: "Do you wish to promote anyway? :",
Default: false,
}
flag := false
err := survey.AskOne(confirm, &flag, nil)
if err != nil {
return releaseInfo, err
}
if !flag {
return releaseInfo, nil
}
}
if env != nil {
source := &env.Spec.Source
if source.URL != "" {
err := o.PromoteViaPullRequest(env, releaseInfo)
return releaseInfo, err
}
}
err := o.verifyHelmConfigured()
if err != nil {
return releaseInfo, err
}
// lets do a helm update to ensure we can find the latest version
if !o.NoHelmUpdate {
o.Printf("Updating the helm repositories to ensure we can find the latest versions...")
err = o.runCommand("helm", "repo", "update")
if err != nil {
return releaseInfo, err
}
}
if version != "" {
err = o.runCommand("helm", "upgrade", "--install", "--namespace", targetNS, "--version", version, releaseName, fullAppName)
} else {
err = o.runCommand("helm", "upgrade", "--install", "--namespace", targetNS, releaseName, fullAppName)
}
return releaseInfo, err
}
func (o *PromoteOptions) PromoteViaPullRequest(env *v1.Environment, releaseInfo *ReleaseInfo) error {
source := &env.Spec.Source
gitURL := source.URL
if gitURL == "" {
return fmt.Errorf("No source git URL")
}
gitInfo, err := gits.ParseGitURL(gitURL)
if err != nil {
return err
}
environmentsDir, err := util.EnvironmentsDir()
if err != nil {
return err
}
dir := filepath.Join(environmentsDir, gitInfo.Organisation, gitInfo.Name)
// now lets clone the fork and push it...
exists, err := util.FileExists(dir)
if err != nil {
return err
}
app := o.Application
version := o.Version
versionName := version
if versionName == "" {
versionName = "latest"
}
branchName := gits.ConvertToValidBranchName("promote-" + app + "-" + versionName)
base := source.Ref
if base == "" {
base = "master"
}
if exists {
// lets check the git remote URL is setup correctly
err = gits.SetRemoteURL(dir, "origin", gitURL)
if err != nil {
return err
}
err = gits.GitCmd(dir, "stash")
if err != nil {
return err
}
err = gits.GitCmd(dir, "checkout", base)
if err != nil {
return err
}
err = gits.GitCmd(dir, "pull")
if err != nil {
return err
}
} else {
err := os.MkdirAll(dir, DefaultWritePermissions)
if err != nil {
return fmt.Errorf("Failed to create directory %s due to %s", dir, err)
}
err = gits.GitClone(gitURL, dir)
if err != nil {
return err
}
if base != "master" {
err = gits.GitCmd(dir, "checkout", base)
if err != nil {
return err
}
}
// TODO lets fork if required???
/*
pushGitURL, err := gits.GitCreatePushURL(gitURL, details.User)
if err != nil {
return err
}
err = gits.GitCmd(dir, "remote", "add", "upstream", forkEnvGitURL)
if err != nil {
return err
}
err = gits.GitCmd(dir, "remote", "add", "origin", pushGitURL)
if err != nil {
return err
}
err = gits.GitCmd(dir, "push", "-u", "origin", "master")
if err != nil {
return err
}
*/
}
branchNames, err := gits.GitGetRemoteBranchNames(dir, "remotes/origin/")
if err != nil {
return fmt.Errorf("Failed to load remote branch names: %s", err)
}
o.Printf("Found remote branch names %s\n", strings.Join(branchNames, ", "))
if util.StringArrayIndex(branchNames, branchName) >= 0 {
// lets append a UUID as the branch name already exists
branchName += "-" + string(uuid.NewUUID())
}
err = gits.GitCmd(dir, "branch", branchName)
if err != nil {
return err
}
err = gits.GitCmd(dir, "checkout", branchName)
if err != nil {
return err
}
requirementsFile, err := helm.FindRequirementsFileName(dir)
if err != nil {
return err
}
requirements, err := helm.LoadRequirementsFile(requirementsFile)
if err != nil {
return err
}
if version == "" {
version, err = o.findLatestVersion(app)
if err != nil {
return err
}
}
requirements.SetAppVersion(app, version, o.HelmRepositoryURL)
err = helm.SaveRequirementsFile(requirementsFile, requirements)
err = gits.GitCmd(dir, "add", "*", "*/*")
if err != nil {
return err
}
changed, err := gits.HasChanges(dir)
if err != nil {
return err
}
if !changed {
o.Printf("%s\n", util.ColorWarning("No changes made to the GitOps Environment source code. Must be already on version!"))
return nil
}
message := fmt.Sprintf("Promote %s to version %s", app, versionName)
err = gits.GitCommit(dir, message)
if err != nil {
return err
}
err = gits.GitPush(dir)
if err != nil {
return err
}
authConfigSvc, err := o.Factory.CreateGitAuthConfigService()
if err != nil {
return err
}
provider, err := gitInfo.PickOrCreateProvider(authConfigSvc, "user name to submit the Pull Request", o.BatchMode)
if err != nil {
return err
}
gha := &gits.GitPullRequestArguments{
Owner: gitInfo.Organisation,
Repo: gitInfo.Name,
Title: app + " to " + versionName,
Body: message,
Base: base,
Head: branchName,
}
pr, err := provider.CreatePullRequest(gha)
if err != nil {
return err
}
o.Printf("Created Pull Request: %s\n\n", util.ColorInfo(pr.URL))
releaseInfo.PullRequestInfo = &ReleasePullRequestInfo{
GitProvider: provider,
PullRequest: pr,
PullRequestArguments: gha,
}
return nil
}
func (o *PromoteOptions) GetTargetNamespace(ns string, env string) (string, *v1.Environment, error) {
kubeClient, currentNs, err := o.KubeClient()
if err != nil {
return "", nil, err
}
team, _, err := kube.GetDevNamespace(kubeClient, currentNs)
if err != nil {
return "", nil, err
}
jxClient, _, err := o.JXClient()
if err != nil {
return "", nil, err
}
m, envNames, err := kube.GetEnvironments(jxClient, team)
if err != nil {
return "", nil, err
}
if len(envNames) == 0 {
return "", nil, fmt.Errorf("No Environments have been created yet in team %s. Please create some via 'jx create env'", team)
}
var envResource *v1.Environment
targetNS := currentNs
if env != "" {
envResource = m[env]
if envResource == nil {
return "", nil, util.InvalidOption(optionEnvironment, env, envNames)
}
targetNS = envResource.Spec.Namespace
if targetNS == "" {
return "", nil, fmt.Errorf("Environment %s does not have a namspace associated with it!", env)
}
} else if ns != "" {
targetNS = ns
}
labels := map[string]string{}
annotations := map[string]string{}
err = kube.EnsureNamespaceCreated(kubeClient, targetNS, labels, annotations)
if err != nil {
return "", nil, err
}
return targetNS, envResource, nil
}
func (options *PromoteOptions) discoverAppName() (string, error) {
answer := ""
dir, err := os.Getwd()
if err != nil {
return answer, err
}
root, gitConf, err := gits.FindGitConfigDir(dir)
if err != nil {
return answer, err
}
if root != "" {
url, err := gits.DiscoverRemoteGitURL(gitConf)
if err != nil {
return answer, err
}
gitInfo, err := gits.ParseGitURL(url)
if err != nil {
return answer, err
}
answer = gitInfo.Name
}
if answer == "" {
// lets try find the chart file
chartFile := filepath.Join(dir, "Chart.yaml")
exists, err := util.FileExists(chartFile)
if err != nil {
return answer, err
}
if !exists {
// lets try find all the chart files
files, err := filepath.Glob("*/Chart.yaml")
if err != nil {
return answer, err
}
if len(files) > 0 {
chartFile = files[0]
} else {
files, err = filepath.Glob("*/*/Chart.yaml")
if err != nil {
return answer, err
}
if len(files) > 0 {
chartFile = files[0]
}
}
}
if chartFile != "" {
return helm.LoadChartName(chartFile)
}
}
return answer, nil
}
func (o *PromoteOptions) WaitForPromotion(ns string, env *v1.Environment, releaseInfo *ReleaseInfo) error {
if o.TimeoutDuration == nil {
o.Printf("No --%s option specified on the 'jx promote' command so not waiting for the promotion to succeed\n", optionTimeout)
return nil
}
if o.PullRequestPollDuration == nil {
o.Printf("No --%s option specified on the 'jx promote' command so not waiting for the promotion to succeed\n", optionPullRequestPollTime)
return nil
}
pullRequestInfo := releaseInfo.PullRequestInfo
if pullRequestInfo != nil {
err := o.waitForGitOpsPullRequest(ns, env, releaseInfo)
if err != nil {
return err
}
}
return nil
}
// waitForGitOpsPullRequest waits for a github pull request to merge for the promotion
func (o *PromoteOptions) waitForGitOpsPullRequest(ns string, env *v1.Environment, releaseInfo *ReleaseInfo) error {
duration := *o.TimeoutDuration
pullRequestInfo := releaseInfo.PullRequestInfo
if pullRequestInfo != nil {
for {
end := time.Now().Add(duration)
pr := pullRequestInfo.PullRequest
err := pullRequestInfo.GitProvider.UpdatePullRequestStatus(pr)
if err != nil {
return fmt.Errorf("Failed to query the Pull Request status for %s %s", pr.URL, err)
}
if pr.Merged != nil && *pr.Merged {
o.Printf("Pull Request %s is merged\n", util.ColorInfo(pr.URL))
return nil
}
if pr.IsClosed() {
o.warnf("Pull Request %s is closed\n", util.ColorInfo(pr.URL))
return fmt.Errorf("Promotion failed as Pull Request %s is closed without merging", pr.URL)
}
if time.Now().After(end) {
return fmt.Errorf("Timed out waiting for pull request %s to merge. Waited %s", pr.URL, duration.String())
}
time.Sleep(*o.PullRequestPollDuration)
}
}
return nil
}
func (o *PromoteOptions) findLatestVersion(app string) (string, error) {
output, err := o.getCommandOutput("", "helm", "search", app, "--versions")
if err != nil {
return "", err
}
var maxSemVer *semver.Version
maxString := ""
for i, line := range strings.Split(output, "\n") {
if i == 0 {
continue
}
fields := strings.Fields(line)
if len(fields) > 1 {
v := fields[1]
if v != "" {
sv, err := semver.Parse(v)
if err != nil {
o.warnf("Invalid semantic version: %s %s\n", v, err)
} else {
if maxSemVer == nil || maxSemVer.Compare(sv) > 0 {
maxSemVer = &sv
}
}
if maxString == "" || strings.Compare(v, maxString) > 0 {
maxString = v
}
}
}
}
if maxSemVer != nil {
return maxSemVer.String(), nil
}
if maxString == "" {
return "", fmt.Errorf("Could not find a version of app %s in the helm repositories", app)
}
return maxString, nil
}
func (o *PromoteOptions) verifyHelmConfigured() error {
helmHomeDir := filepath.Join(util.HomeDir(), ".helm")
exists, err := util.FileExists(helmHomeDir)
if err != nil {
return err
}
if !exists {
o.Printf("No helm home dir at %s so lets initialise helm client\n", helmHomeDir)
err = o.runCommand("helm", "init", "--client-only")
if err != nil {
return err
}
}
f := o.Factory
_, ns, _ := f.CreateClient()
if err != nil {
return err
}
// lets add the releases chart
return o.registerLocalHelmRepo(o.LocalHelmRepoName, ns)
}