forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promote.go
492 lines (454 loc) · 12.5 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
package cmd
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"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"
)
// 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
}
var (
promote_long = templates.LongDesc(`
Promotes a version of an application to an environment.
`)
promote_example = templates.Examples(`
# Promote a version of an application to staging
jx promote myapp --version 1.2.3 --env staging
`)
)
// 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)
},
}
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, "app", "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().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
}
return o.Promote(targetNS, env, true)
}
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 {
return err
}
environments := envs.Items
if len(environments) == 0 {
return fmt.Errorf("No Environments have been created yet in team %s. Please create some via 'jx create env'", team)
}
kube.SortEnvironments(environments)
for _, env := range environments {
if env.Spec.PromotionStrategy == v1.PromotionStrategyTypeAutomatic {
ns := env.Spec.Namespace
err = o.Promote(ns, &env, false)
if err != nil {
return err
}
err = o.WaitForPromotion(ns, &env)
if err != nil {
return err
}
}
}
return nil
}
func (o *PromoteOptions) Promote(targetNS string, env *v1.Environment, warnIfAuto bool) error {
app := o.Application
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))
}
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 err
}
if !flag {
return nil
}
}
if env != nil {
source := &env.Spec.Source
if source.URL != "" {
return o.PromoteViaPullRequest(env)
}
}
fullAppName := app
if o.LocalHelmRepoName != "" {
fullAppName = o.LocalHelmRepoName + "/" + app
}
// 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 err
}
}
releaseName := targetNS + "-" + app
if version != "" {
return o.runCommand("helm", "upgrade", "--install", "--namespace", targetNS, "--version", version, releaseName, fullAppName)
}
return o.runCommand("helm", "upgrade", "--install", "--namespace", targetNS, releaseName, fullAppName)
}
func (o *PromoteOptions) PromoteViaPullRequest(env *v1.Environment) 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")
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))
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
}
return answer, nil
}
func (options *PromoteOptions) WaitForPromotion(ns string, env *v1.Environment) error {
// TODO
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
}