-
Notifications
You must be signed in to change notification settings - Fork 787
/
upgrade_platform.go
425 lines (357 loc) · 12.9 KB
/
upgrade_platform.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
package cmd
import (
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/cloud"
"github.com/jenkins-x/jx/pkg/config"
configio "github.com/jenkins-x/jx/pkg/io"
"io"
"io/ioutil"
"strings"
"gopkg.in/AlecAivazis/survey.v1"
"github.com/jenkins-x/jx/pkg/kube"
"fmt"
"os"
"path/filepath"
"github.com/ghodss/yaml"
do_not_use "gopkg.in/yaml.v2"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
core_v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
upgrade_platform_long = templates.LongDesc(`
Upgrades the Jenkins X platform if there is a newer release
`)
upgrade_platform_example = templates.Examples(`
# Upgrades the Jenkins X platform
jx upgrade platform
`)
)
// UpgradePlatformOptions the options for the create spring command
type UpgradePlatformOptions struct {
InstallOptions
Version string
ReleaseName string
Chart string
Namespace string
Set string
AlwaysUpgrade bool
UpdateSecrets bool
InstallFlags InstallFlags
}
// NewCmdUpgradePlatform defines the command
func NewCmdUpgradePlatform(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &UpgradePlatformOptions{
InstallOptions: InstallOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "platform",
Short: "Upgrades the Jenkins X platform if there is a new release available",
Aliases: []string{"install"},
Long: upgrade_platform_long,
Example: upgrade_platform_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Namespace, "namespace", "", "", "The Namespace to promote to.")
cmd.Flags().StringVarP(&options.ReleaseName, "name", "n", "jenkins-x", "The release name.")
cmd.Flags().StringVarP(&options.Chart, "chart", "c", "jenkins-x/jenkins-x-platform", "The Chart to upgrade.")
cmd.Flags().StringVarP(&options.Version, "version", "v", "", "The specific platform version to upgrade to.")
cmd.Flags().StringVarP(&options.Set, "set", "s", "", "The helm parameters to pass in while upgrading, separated by comma, e.g. key1=val1,key2=val2.")
cmd.Flags().BoolVarP(&options.AlwaysUpgrade, "always-upgrade", "", false, "If set to true, jx will upgrade platform Helm chart even if requested version is already installed.")
cmd.Flags().BoolVarP(&options.Flags.CleanupTempFiles, "cleanup-temp-files", "", true, "Cleans up any temporary values.yaml used by helm install [default true].")
cmd.Flags().BoolVarP(&options.UpdateSecrets, "update-secrets", "", false, "Regenerate adminSecrets.yaml on upgrade")
options.addCommonFlags(cmd)
options.InstallFlags.addCloudEnvOptions(cmd)
return cmd
}
// Run implements the command
func (o *UpgradePlatformOptions) Run() error {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
configStore := configio.NewFileStore()
targetVersion := o.Version
err := o.Helm().UpdateRepo()
if err != nil {
return err
}
apisClient, err := o.ApiExtensionsClient()
if err != nil {
return errors.Wrap(err, "failed to create the API extensions client")
}
kube.RegisterAllCRDs(apisClient)
if err != nil {
return err
}
ns := o.Namespace
if ns == "" {
_, ns, err = o.JXClientAndDevNamespace()
if err != nil {
return err
}
}
settings, err := o.TeamSettings()
if err != nil {
return err
}
if "" == settings.KubeProvider {
log.Warnf("Unable to determine provider from team settings")
provider := ""
prompt := &survey.Select{
Message: "Select the kube provider:",
Options: cloud.KubernetesProviders,
Default: "",
}
survey.AskOne(prompt, &provider, nil, surveyOpts)
err = o.ModifyDevEnvironment(func(env *v1.Environment) error {
settings = &env.Spec.TeamSettings
settings.KubeProvider = provider
return nil
})
if err != nil {
return errors.Wrap(err, "failed to create the API extensions client")
}
}
log.Infof("Using provider '%s' from team settings\n", util.ColorInfo(settings.KubeProvider))
wrkDir := ""
if targetVersion == "" {
io := &InstallOptions{}
io.CommonOptions = o.CommonOptions
io.Flags = o.InstallFlags
versionsDir, err := io.cloneJXVersionsRepo(o.Flags.VersionsRepository)
if err != nil {
return err
}
targetVersion, err = LoadVersionFromCloudEnvironmentsDir(versionsDir, configStore)
if err != nil {
return err
}
}
releases, err := o.Helm().StatusReleases(ns)
if err != nil {
return errors.Wrap(err, "list charts releases")
}
var currentVersion string
for name, rel := range releases {
if name == "jenkins-x" {
currentVersion = rel.Version
}
}
if currentVersion == "" {
return errors.New("Jenkins X platform helm chart is not installed.")
}
helmConfig := &o.CreateEnvOptions.HelmValuesConfig
exposeController := helmConfig.ExposeController
if exposeController != nil && exposeController.Config.Domain == "" {
helmConfig.ExposeController.Config.Domain = o.InitOptions.Flags.Domain
}
// clone the environments repo
if wrkDir == "" {
wrkDir, err = o.cloneJXCloudEnvironmentsRepo()
if err != nil {
return errors.Wrap(err, "failed to clone the jx cloud environments repo")
}
}
makefileDir := filepath.Join(wrkDir, fmt.Sprintf("env-%s", strings.ToLower(settings.KubeProvider)))
if _, err := os.Stat(wrkDir); os.IsNotExist(err) {
return fmt.Errorf("cloud environment dir %s not found", makefileDir)
}
// create a temporary file that's used to pass current git creds to helm in order to create a secret for pipelines to tag releases
dir, err := util.ConfigDir()
if err != nil {
return errors.Wrap(err, "failed to create a temporary config dir for Git credentials")
}
// file locations
adminSecretsFileName := filepath.Join(dir, AdminSecretsFile)
configFileName := filepath.Join(dir, ExtraValuesFile)
cloudEnvironmentValuesLocation := filepath.Join(makefileDir, CloudEnvValuesFile)
cloudEnvironmentSecretsLocation := filepath.Join(makefileDir, CloudEnvSecretsFile)
cloudEnvironmentSopsLocation := filepath.Join(makefileDir, CloudEnvSopsConfigFile)
client, err := o.KubeClient()
if err != nil {
return err
}
secretResources := client.CoreV1().Secrets(ns)
oldSecret, err := secretResources.Get(JXInstallConfig, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, "failed to get the jx-install-config secret")
}
if oldSecret == nil {
return errors.Wrap(err, "secret jx-install-config doesn't exist, aborting")
}
if targetVersion != currentVersion {
log.Infof("Upgrading platform from version %s to version %s\n", util.ColorInfo(currentVersion), util.ColorInfo(targetVersion))
} else if o.AlwaysUpgrade {
log.Infof("Rerunning platform version %s\n", util.ColorInfo(targetVersion))
} else {
log.Infof("Already installed platform version %s. Skipping upgrade process.\n", util.ColorInfo(targetVersion))
return nil
}
err = o.removeFileIfExists(adminSecretsFileName)
if err != nil {
return errors.Wrapf(err, "unable to remove %s if exist", adminSecretsFileName)
}
err = o.removeFileIfExists(configFileName)
if err != nil {
return errors.Wrapf(err, "unable to remove %s if exist", configFileName)
}
log.Infof("Creating %s from %s\n", util.ColorInfo(adminSecretsFileName), util.ColorInfo(JXInstallConfig))
err = ioutil.WriteFile(adminSecretsFileName, oldSecret.Data[AdminSecretsFile], 0644)
if err != nil {
return errors.Wrapf(err, "failed to write the config file %s", adminSecretsFileName)
}
o.Debugf("%s from %s is %s\n", AdminSecretsFile, JXInstallConfig, oldSecret.Data[AdminSecretsFile])
if o.UpdateSecrets {
// load admin secrets service from adminSecretsFileName
err = o.AdminSecretsService.NewAdminSecretsConfigFromSecret(adminSecretsFileName)
if err != nil {
return errors.Wrap(err, "failed to create the admin secret config service from the secrets file")
}
o.AdminSecretsService.NewMavenSettingsXML()
adminSecrets := &o.AdminSecretsService.Secrets
o.Debugf("Rewriting secrets file to %s\n", util.ColorInfo(adminSecretsFileName))
err = configStore.WriteObject(adminSecretsFileName, adminSecrets)
if err != nil {
return errors.Wrapf(err, "writing the admin secrets in the secrets file '%s'", adminSecretsFileName)
}
}
log.Infof("Creating %s from %s\n", util.ColorInfo(configFileName), util.ColorInfo(JXInstallConfig))
err = ioutil.WriteFile(configFileName, oldSecret.Data[ExtraValuesFile], 0644)
if err != nil {
return errors.Wrapf(err, "failed to write the config file %s", configFileName)
}
sopsFileExists, err := util.FileExists(cloudEnvironmentSopsLocation)
if err != nil {
return errors.Wrap(err, "failed to look for "+cloudEnvironmentSopsLocation)
}
if sopsFileExists {
log.Infof("Attempting to decrypt secrets file %s\n", util.ColorInfo(cloudEnvironmentSecretsLocation))
// need to decrypt secrets now
err = o.Helm().DecryptSecrets(cloudEnvironmentSecretsLocation)
if err != nil {
return errors.Wrap(err, "failed to decrypt "+cloudEnvironmentSecretsLocation)
}
cloudEnvironmentSecretsDecryptedLocation := filepath.Join(makefileDir, CloudEnvSecretsFile+".dec")
decryptedSecretsFile, err := util.FileExists(cloudEnvironmentSecretsDecryptedLocation)
if err != nil {
return errors.Wrap(err, "failed to look for "+cloudEnvironmentSecretsDecryptedLocation)
}
if decryptedSecretsFile {
log.Infof("Successfully decrypted %s\n", util.ColorInfo(cloudEnvironmentSecretsDecryptedLocation))
cloudEnvironmentSecretsLocation = cloudEnvironmentSecretsDecryptedLocation
}
}
invalidFormat, err := o.checkAdminSecretsForInvalidFormat(adminSecretsFileName)
if err != nil {
return errors.Wrap(err, "unable to check adminSecrets.yaml file for invalid format")
}
if invalidFormat {
log.Warnf("We have detected that the %s file has an invalid format", adminSecretsFileName)
confirm := false
prompt := &survey.Confirm{
Message: fmt.Sprintf("Would you like to repair the file?"),
Default: true,
}
survey.AskOne(prompt, &confirm, nil, surveyOpts)
if confirm {
err = o.repairAdminSecrets(adminSecretsFileName)
if err != nil {
return errors.Wrap(err, "unable to repair adminSecrets.yaml")
}
} else {
log.Error("Aborting upgrade due to invalid adminSecrets.yaml")
return nil
}
}
valueFiles := []string{cloudEnvironmentValuesLocation, configFileName, adminSecretsFileName, cloudEnvironmentSecretsLocation}
valueFiles, err = helm.AppendMyValues(valueFiles)
if err != nil {
return errors.Wrap(err, "failed to append the myvalues.yaml file")
}
values := []string{}
if o.Set != "" {
sets := strings.Split(o.Set, ",")
values = append(values, sets...)
}
for _, v := range valueFiles {
o.Debugf("Adding values file %s\n", util.ColorInfo(v))
}
err = o.Helm().UpgradeChart(o.Chart, o.ReleaseName, ns, targetVersion, false, -1, false, false, values,
valueFiles, "", "", "")
if err != nil {
return errors.Wrap(err, "unable to upgrade helm chart")
}
if o.Flags.CleanupTempFiles {
err = o.removeFileIfExists(configFileName)
if err != nil {
return errors.Wrap(err, "failed to cleanup the config file")
}
err = o.removeFileIfExists(adminSecretsFileName)
if err != nil {
return errors.Wrap(err, "failed to cleanup the admin secrets file")
}
}
return nil
}
func (o *UpgradePlatformOptions) removeFileIfExists(fileName string) error {
fileNameExists, err := util.FileExists(fileName)
if err != nil {
return errors.Wrapf(err, "unable to determine if %s exist", fileName)
}
if fileNameExists {
o.Debugf("Removing values file %s\n", util.ColorInfo(fileName))
err = os.Remove(fileName)
if err != nil {
return errors.Wrapf(err, "failed to remove %s", fileName)
}
}
return nil
}
func (o *UpgradePlatformOptions) checkAdminSecretsForInvalidFormat(fileName string) (bool, error) {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return false, errors.Wrap(err, "unable to read file")
}
return strings.Contains(string(data), "mavensettingsxml"), nil
}
func (o *UpgradePlatformOptions) repairAdminSecrets(fileName string) error {
admin := config.AdminSecretsConfig{}
data, err := ioutil.ReadFile(fileName)
if err != nil {
return errors.Wrap(err, "unable to read file")
}
err = do_not_use.Unmarshal([]byte(data), &admin)
if err != nil {
return errors.Wrap(err, "unable to unmarshall secrets")
}
// use the correct yaml library to persist
y, err := yaml.Marshal(admin)
if err != nil {
return errors.Wrapf(err, "unable to marshal object to yaml: %v", admin)
}
err = ioutil.WriteFile(fileName, y, util.DefaultWritePermissions)
if err != nil {
return errors.Wrapf(err, "unable to write secrets to file %s", fileName)
}
_, err = o.ModifySecret(JXInstallConfig, func(secret *core_v1.Secret) error {
secret.Data[AdminSecretsFile] = y
return nil
})
return nil
}