-
Notifications
You must be signed in to change notification settings - Fork 786
/
upgrade_apps.go
199 lines (171 loc) · 5.46 KB
/
upgrade_apps.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
package cmd
import (
"fmt"
"github.com/jenkins-x/jx/pkg/apps"
"github.com/jenkins-x/jx/pkg/io/secrets"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/pkg/environments"
jenkinsv1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
)
var (
upgradeAppsLong = templates.LongDesc(`
Upgrades Apps to newer releases
`)
upgradeAppsExample = templates.Examples(`
# Upgrade all apps
jx upgrade apps
# Upgrade a specific app
jx upgrade app cheese
`)
)
// UpgradeAppsOptions the options for the create spring command
type UpgradeAppsOptions struct {
AddOptions
GitOps bool
DevEnv *jenkinsv1.Environment
Repo string
Alias string
Username string
Password string
ReleaseName string
HelmUpdate bool
AskAll bool
Version string
All bool
Namespace string
Set []string
// allow git to be configured externally before a PR is created
ConfigureGitCallback environments.ConfigureGitFn
InstallFlags InstallFlags
}
// NewCmdUpgradeApps defines the command
func NewCmdUpgradeApps(commonOpts *CommonOptions) *cobra.Command {
o := &UpgradeAppsOptions{
AddOptions: AddOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "apps",
Short: "Upgrades any Apps to the latest release",
Aliases: []string{"app"},
Long: upgradeAppsLong,
Example: upgradeAppsExample,
Run: func(cmd *cobra.Command, args []string) {
o.Cmd = cmd
o.Args = args
err := o.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&o.Version, "username", "", "",
"The username for the repository")
cmd.Flags().StringVarP(&o.Version, "password", "", "",
"The password for the repository")
cmd.Flags().StringVarP(&o.Repo, "repository", "", "",
"The repository from which the app should be installed")
cmd.Flags().StringVarP(&o.Alias, "alias", "", "", "An alias to use for the app [--gitops]")
cmd.Flags().StringVarP(&o.Version, "version", "v", "",
"The chart version to install [--gitops]")
cmd.Flags().StringVarP(&o.Namespace, "namespace", "", "", "The Namespace to promote to [--no-gitops]")
cmd.Flags().StringArrayVarP(&o.Set, "set", "s", []string{},
"The Helm parameters to pass in while upgrading [--no-gitops]")
cmd.Flags().BoolVarP(&o.HelmUpdate, optionHelmUpdate, "", true,
"Should we run helm update first to ensure we use the latest version (available when NOT using GitOps for your dev environment)")
cmd.Flags().StringVarP(&o.ReleaseName, optionRelease, "r", "",
"The chart release name (by default the name of the app, available when NOT using GitOps for your dev environment)")
cmd.Flags().BoolVarP(&o.AskAll, "ask-all", "", false, "Ask all configuration questions. "+
"By default existing answers are reused automatically.")
return cmd
}
// Run implements the command
func (o *UpgradeAppsOptions) Run() error {
o.GitOps, o.DevEnv = o.GetDevEnv()
if o.Repo == "" {
o.Repo = o.DevEnv.Spec.TeamSettings.AppsRepository
}
opts := apps.InstallOptions{
In: o.In,
DevEnv: o.DevEnv,
Verbose: o.Verbose,
Err: o.Err,
Out: o.Out,
GitOps: o.GitOps,
BatchMode: o.BatchMode,
Helmer: o.Helm(),
}
if o.GitOps {
msg := "Unable to specify --%s when using GitOps for your dev environment"
if o.Namespace != "" {
return util.InvalidOptionf(optionNamespace, o.ReleaseName, msg, optionNamespace)
}
if len(o.Set) > 0 {
return util.InvalidOptionf(optionSet, o.ReleaseName, msg, optionSet)
}
if o.GetSecretsLocation() != secrets.VaultLocationKind {
return fmt.Errorf("cannot install apps without a vault when using GitOps for your dev environment")
}
if !o.HelmUpdate {
return util.InvalidOptionf(optionHelmUpdate, o.HelmUpdate, msg, optionHelmUpdate)
}
environmentsDir, err := o.EnvironmentsDir()
if err != nil {
return errors.Wrapf(err, "getting environments dir")
}
opts.EnvironmentsDir = environmentsDir
gitProvider, _, err := o.createGitProviderForURLWithoutKind(o.DevEnv.Spec.Source.URL)
if err != nil {
return errors.Wrapf(err, "creating git provider for %s", o.DevEnv.Spec.Source.URL)
}
opts.GitProvider = gitProvider
opts.ConfigureGitFn = o.ConfigureGitCallback
opts.Gitter = o.Git()
}
if !o.GitOps {
msg := "Unable to specify --%s when NOT using GitOps for your dev environment"
if o.Alias != "" {
return util.InvalidOptionf(optionAlias, o.ReleaseName, msg, optionAlias)
}
if o.Version != "" {
return util.InvalidOptionf(optionVersion, o.ReleaseName, msg, optionVersion)
}
jxClient, _, err := o.JXClientAndDevNamespace()
if err != nil {
return errors.Wrapf(err, "getting jx client")
}
kubeClient, _, err := o.KubeClientAndDevNamespace()
if err != nil {
return errors.Wrapf(err, "getting kubeClient")
}
opts.Namespace = o.Namespace
opts.KubeClient = kubeClient
opts.JxClient = jxClient
opts.InstallTimeout = defaultInstallTimeout
}
if o.GetSecretsLocation() == secrets.VaultLocationKind {
teamName, _, err := o.TeamAndEnvironmentNames()
if err != nil {
return err
}
opts.TeamName = teamName
client, err := o.SystemVaultClient("")
if err != nil {
return err
}
opts.VaultClient = &client
}
app := ""
if len(o.Args) > 1 {
return o.Cmd.Help()
} else if len(o.Args) == 1 {
app = o.Args[0]
}
var version string
if o.Version != "" {
version = o.Version
}
return opts.UpgradeApp(app, version, o.Repo, o.Username, o.Password, o.ReleaseName, o.Alias, o.HelmUpdate, o.AskAll)
}