-
Notifications
You must be signed in to change notification settings - Fork 51
/
upgrade.go
94 lines (83 loc) · 2.69 KB
/
upgrade.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
package upgrade
import (
"github.com/jenkins-x-plugins/jx-gitops/pkg/cmd/helmfile/resolve"
kptupdate "github.com/jenkins-x-plugins/jx-gitops/pkg/cmd/kpt/update"
"github.com/jenkins-x-plugins/jx-gitops/pkg/plugins"
"github.com/jenkins-x-plugins/jx-gitops/pkg/tfupgrade"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// ShowOptions the options for viewing running PRs
type Options struct {
kptupdate.Options
HelmfileResolve resolve.Options
TerraformUpgrade tfupgrade.Options
}
// NewCmdUpgrade creates a command object
func NewCmdUpgrade() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "upgrade",
Aliases: []string{"update"},
Short: "Upgrades the GitOps git repository with the latest configuration and versions the Version Stream",
Run: func(cmd *cobra.Command, args []string) {
err := o.Run()
helper.CheckErr(err)
},
}
o.Options.AddFlags(cmd)
o.HelmfileResolve.AddFlags(cmd, "")
return cmd, o
}
// Run implements the command
func (o *Options) Run() error {
log.Logger().Infof("upgrading local source code from the version stream using kpt...\n\n")
err := o.Options.Run()
if err != nil {
return errors.Wrapf(err, "failed to update source using kpt")
}
exists, err := o.HelmfileResolve.HasHelmfile()
if err != nil {
return errors.Wrapf(err, "failed to check for helmfile")
}
if exists {
err = o.doHelmfileUpgrade()
if err != nil {
return errors.Wrapf(err, "failed to resolve helmfile")
}
}
return o.doTerraformUpgrade(err)
}
func (o *Options) doHelmfileUpgrade() error {
log.Logger().Infof("\nnow checking the chart versions in %s\n\n", termcolor.ColorInfo("helmfile.yaml"))
var err error
if o.HelmfileResolve.HelmBinary == "" {
o.HelmfileResolve.HelmBinary, err = plugins.GetHelmBinary(plugins.HelmVersion)
if err != nil {
return errors.Wrapf(err, "failed to download helm binary")
}
log.Logger().Infof("using helm binary %s to verify chart repositories", termcolor.ColorInfo(o.HelmfileResolve.HelmBinary))
}
o.HelmfileResolve.UpdateMode = true
err = o.HelmfileResolve.Run()
if err != nil {
return errors.Wrapf(err, "failed to update the helmfile versions")
}
return nil
}
func (o *Options) doTerraformUpgrade(err error) error {
if o.Options.Dir != "" {
o.TerraformUpgrade.Dir = o.Options.Dir
}
if o.HelmfileResolve.VersionStreamDir != "" {
o.TerraformUpgrade.VersionStreamDir = o.HelmfileResolve.VersionStreamDir
}
err = o.TerraformUpgrade.Run()
if err != nil {
return errors.Wrapf(err, "failed to upgrade terraform git repository versions")
}
return nil
}