-
Notifications
You must be signed in to change notification settings - Fork 786
/
upgrade_platform.go
295 lines (255 loc) · 9.34 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
package cmd
import (
"io"
"io/ioutil"
"strings"
"fmt"
"os"
"path/filepath"
"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"
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
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")
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]")
options.addCommonFlags(cmd)
options.InstallFlags.addCloudEnvOptions(cmd)
return cmd
}
// Run implements the command
func (o *UpgradePlatformOptions) Run() error {
targetVersion := o.Version
err := o.Helm().UpdateRepo()
if err != nil {
return err
}
ns := o.Namespace
if ns == "" {
_, ns, err = o.JXClientAndDevNamespace()
if err != nil {
return err
}
}
wrkDir := ""
if targetVersion == "" {
io := &InstallOptions{}
io.CommonOptions = o.CommonOptions
io.Flags = o.InstallFlags
wrkDir, err = io.cloneJXCloudEnvironmentsRepo()
if err != nil {
return err
}
targetVersion, err = LoadVersionFromCloudEnvironmentsDir(wrkDir)
if err != nil {
return err
}
}
// Current version
var currentVersion string
output, err := o.Helm().ListCharts()
if err != nil {
log.Warnf("Failed to find helm installs: %s\n", err)
return err
} else {
for _, line := range strings.Split(output, "\n") {
fields := strings.Split(line, "\t")
if len(fields) > 4 && strings.TrimSpace(fields[0]) == "jenkins-x" {
for _, f := range fields[4:] {
f = strings.TrimSpace(f)
if strings.HasPrefix(f, jxChartPrefix) {
currentVersion = strings.TrimPrefix(f, jxChartPrefix)
}
}
}
}
}
if currentVersion == "" {
return errors.New("Jenkins X platform helm chart is not installed.")
}
settings, err := o.TeamSettings()
if err != nil {
return err
}
log.Infof("Using provider %s from team settings\n", util.ColorInfo(settings.KubeProvider))
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")
}
secretsFileName := filepath.Join(dir, GitSecretsFile)
adminSecretsFileName := filepath.Join(dir, AdminSecretsFile)
configFileName := filepath.Join(dir, ExtraValuesFile)
secretResources := o.KubeClientCached.CoreV1().Secrets(ns)
oldSecret, err := secretResources.Get(JXInstallConfig, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, "failed to get the jx secret resource")
}
if oldSecret == nil {
return errors.Wrap(err, "old secret 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
}
cloudEnvironmentValuesLocation := filepath.Join(makefileDir, CloudEnvValuesFile)
cloudEnvironmentSecretsLocation := filepath.Join(makefileDir, CloudEnvSecretsFile)
cloudEnvironmentSopsLocation := filepath.Join(makefileDir, CloudEnvSopsConfigFile)
secretsFileNameExists, err := util.FileExists(secretsFileName)
if err != nil {
return errors.Wrapf(err, "unable to determine if %s exist", secretsFileName)
}
if !secretsFileNameExists {
log.Infof("Creating %s from %s", util.ColorInfo(secretsFileName), util.ColorInfo(JXInstallConfig))
err = ioutil.WriteFile(secretsFileName, oldSecret.Data[GitSecretsFile], 0644)
if err != nil {
return errors.Wrapf(err, "failed to write the config file %s", secretsFileName)
}
}
adminSecretsFileNameExists, err := util.FileExists(adminSecretsFileName)
if err != nil {
return errors.Wrapf(err, "unable to determine if %s exist", adminSecretsFileName)
}
if !adminSecretsFileNameExists {
log.Infof("Creating %s from %s", 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)
}
}
configFileNameExists, err := util.FileExists(configFileName)
if err != nil {
return errors.Wrapf(err, "unable to determine if %s exist", configFileName)
}
if !configFileNameExists {
log.Infof("Creating %s from %s", 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
}
}
valueFiles := []string{cloudEnvironmentValuesLocation, secretsFileName, adminSecretsFileName, configFileName, 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 != "" {
values = append(values, o.Set)
}
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, nil, false, false, values, valueFiles)
if err != nil {
return errors.Wrap(err, "unable to upgrade helm chart")
}
if o.Flags.CleanupTempFiles {
err = os.Remove(secretsFileName)
if err != nil {
return errors.Wrap(err, "failed to cleanup the secrets file")
}
if !configFileNameExists {
err = os.Remove(configFileName)
if err != nil {
return errors.Wrap(err, "failed to cleanup the config file")
}
}
}
return nil
}