-
Notifications
You must be signed in to change notification settings - Fork 786
/
delete_application.go
389 lines (337 loc) · 12.5 KB
/
delete_application.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
package cmd
import (
"fmt"
"io"
"os/user"
"strings"
"time"
"k8s.io/helm/pkg/proto/hapi/chart"
"github.com/jenkins-x/jx/pkg/prow"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/golang-jenkins"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/jenkins"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"github.com/jenkins-x/jx/pkg/util"
)
var (
deleteApplicationLong = templates.LongDesc(`
Deletes one or more Applications
Note that this command does not remove the underlying Git Repositories.
For that see the [jx delete repo](https://jenkins-x.io/commands/jx_delete_repo/) command.
`)
deleteApplicationExample = templates.Examples(`
# prompt for the available applications to delete
jx delete application
# delete a specific app
jx delete application cheese
`)
)
// DeleteApplicationOptions are the flags for this delete commands
type DeleteApplicationOptions struct {
CommonOptions
SelectAll bool
SelectFilter string
IgnoreEnvironments bool
NoMergePullRequest bool
Timeout string
PullRequestPollTime string
Org string
// calculated fields
TimeoutDuration *time.Duration
PullRequestPollDuration *time.Duration
// allow git to be configured externally before a PR is created
ConfigureGitCallback ConfigureGitFolderFn
}
// NewCmdDeleteApplication creates a command object for this command
func NewCmdDeleteApplication(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &DeleteApplicationOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "application",
Short: "Deletes one or more applications from Jenkins",
Long: deleteApplicationLong,
Example: deleteApplicationExample,
Aliases: []string{"applications"}, // FIXME - naming conflict with 'app'
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.IgnoreEnvironments, "no-env", "", false, "Do not remove the application from any of the Environments")
cmd.Flags().BoolVarP(&options.SelectAll, "all", "a", false, "Selects all the matched applications")
cmd.Flags().BoolVarP(&options.NoMergePullRequest, "no-merge", "", false, "Disables automatic merge of promote Pull Requests")
cmd.Flags().StringVarP(&options.SelectFilter, "filter", "f", "", "Filter the list of applications to those containing this text")
cmd.Flags().StringVarP(&options.Timeout, optionTimeout, "t", "1h", "The timeout to wait for the promotion to succeed in the underlying Environment. The command fails if the timeout is exceeded or the promotion does not complete")
cmd.Flags().StringVarP(&options.PullRequestPollTime, optionPullRequestPollTime, "", "20s", "Poll time when waiting for a Pull Request to merge")
cmd.Flags().StringVarP(&options.Org, "org", "o", "", "github organisation/project name that source code resides in")
cmd.Flags().BoolVarP(&options.BatchMode, "batch-mode", "b", false, "Run without being prompted. WARNING! You will not be asked to confirm deletions if you use this flag.")
return cmd
}
// Run implements this command
func (o *DeleteApplicationOptions) Run() error {
err := o.init()
if err != nil {
return errors.Wrap(err, "setting up context")
}
isProw, err := o.isProw()
if err != nil {
return errors.Wrap(err, "getting prow config")
}
repoService := kube.NewSourceRepositoryService(o.jxClient, o.currentNamespace)
var deletedApplications []string
if isProw {
deletedApplications, err = o.deleteProwApplication(repoService)
} else {
deletedApplications, err = o.deleteJenkinsApplication()
}
if err != nil {
return errors.Wrapf(err, "deleting application")
}
log.Infof("Deleted Application(s): %s\n", util.ColorInfo(strings.Join(deletedApplications, ",")))
return nil
}
func (o *DeleteApplicationOptions) deleteProwApplication(repoService kube.SourceRepoer) (deletedApplications []string, err error) {
envMap, _, err := kube.GetOrderedEnvironments(o.jxClient, "")
currentUser, err := user.Current()
if err != nil {
return deletedApplications, errors.Wrap(err, "getting current user")
}
kubeClient, ns, err := o.KubeClientAndDevNamespace()
if err != nil {
return deletedApplications, errors.Wrap(err, "getting kube client")
}
for _, applicationName := range o.Args {
for _, env := range envMap {
if env.Spec.Kind == v1.EnvironmentKindTypePermanent {
err = o.deleteApplicationFromEnvironment(env, applicationName, currentUser.Username)
if err != nil {
return deletedApplications, errors.Wrapf(err, "deleting application %s from environment %s", applicationName, env.Name)
}
}
}
if o.Org == "" {
// fetch the list of sourcerepositories
srList, err := repoService.ListSourceRepositories()
if err != nil {
return deletedApplications, fmt.Errorf("error in sourcerepository service %s", err.Error())
}
srObjects := []v1.SourceRepository{}
for sr := range srList.Items {
if srList.Items[sr].Spec.Repo == applicationName {
srObjects = append(srObjects, srList.Items[sr])
if len(srObjects) > 1 {
return deletedApplications, fmt.Errorf("application %s exists in multiple orgs, use --org to specify the app to delete", util.ColorInfo(applicationName))
}
}
}
if len(srObjects) == 0 {
return deletedApplications, fmt.Errorf("no sourcerepository object found, unable to delete %s", util.ColorInfo(applicationName))
}
// we only found a single sourceporistory resource, proceed
o.Org = srObjects[0].Spec.Org
}
repo := []string{o.Org + "/" + applicationName}
err = prow.DeleteApplication(kubeClient, repo, ns)
if err != nil {
return deletedApplications, errors.Wrapf(err, "deleting prow config for %s", applicationName)
}
deletedApplications = append(deletedApplications,applicationName)
err := repoService.DeleteSourceRepository(o.Org + "-" +applicationName)
if err != nil {
log.Warnf("Unable to find application metadata for %s to remove", applicationName)
}
}
return
}
func (o *DeleteApplicationOptions) deleteJenkinsApplication() (deletedApplications []string, err error) {
args := o.Args
jenk, err := o.JenkinsClient()
if err != nil {
return deletedApplications, err
}
jobs, err := jenkins.LoadAllJenkinsJobs(jenk)
if err != nil {
return deletedApplications, err
}
names := []string{}
m := map[string]*gojenkins.Job{}
for _, j := range jobs {
if jenkins.IsMultiBranchProject(j) {
name := j.FullName
names = append(names, name)
m[name] = j
}
}
if len(names) == 0 {
return deletedApplications, fmt.Errorf("There are no Applications in Jenkins")
}
if len(args) == 0 {
args, err = util.SelectNamesWithFilter(names, "Pick Applications to remove from Jenkins:", o.SelectAll, o.SelectFilter, "", o.In, o.Out, o.Err)
if err != nil {
return deletedApplications, err
}
if len(args) == 0 {
return deletedApplications, fmt.Errorf("No application was picked to be removed from Jenkins")
}
} else {
for _, arg := range args {
if util.StringArrayIndex(names, arg) < 0 {
return deletedApplications, util.InvalidArg(arg, names)
}
}
}
deleteMessage := strings.Join(args, ", ")
if !o.BatchMode {
if !util.Confirm("You are about to delete these Applications from Jenkins: "+deleteMessage, false, "The list of Applications names to be deleted from Jenkins", o.In, o.Out, o.Err) {
return deletedApplications, err
}
}
for _, name := range args {
job := m[name]
if job != nil {
err = o.deleteApplication(jenk, name, job)
if err != nil {
return deletedApplications, err
}
deletedApplications = append(deletedApplications, name)
}
}
return deletedApplications, err
}
func (o *DeleteApplicationOptions) deleteApplication(jenkinsClient gojenkins.JenkinsClient, name string, job *gojenkins.Job) error {
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
envMap, envNames, err := kube.GetOrderedEnvironments(jxClient, ns)
if err != nil {
return err
}
u, err := user.Current()
if err != nil {
return err
}
applicationName := o.applicationNameFromJenkinsJobName(name)
for _, envName := range envNames {
// TODO filter on environment names?
env := envMap[envName]
if env != nil && env.Spec.Kind == v1.EnvironmentKindTypePermanent {
err = o.deleteApplicationFromEnvironment(env, applicationName, u.Username)
if err != nil {
return err
}
}
}
// lets try delete the job from each environment first
return jenkinsClient.DeleteJob(*job)
}
func (o *DeleteApplicationOptions) applicationNameFromJenkinsJobName(name string) string {
path := strings.Split(name, "/")
return path[len(path)-1]
}
func (o *DeleteApplicationOptions) deleteApplicationFromEnvironment(env *v1.Environment, applicationName string, username string) error {
if env.Spec.Source.URL == "" {
return nil
}
log.Infof("Removing application %s from environment %s\n", applicationName, env.Spec.Label)
branchName := "delete-" + applicationName
title := "Delete application " + applicationName + " from this environment"
message := "The command `jx delete application` was run by " + username + " and it generated this Pull Request"
modifyChartFn := func(requirements *helm.Requirements, metadata *chart.Metadata, values map[string]interface{},
templates map[string]string, dir string) error {
requirements.RemoveApplication(applicationName)
return nil
}
info, err := o.createEnvironmentPullRequest(env, modifyChartFn, &branchName, &title, &message, nil,
o.ConfigureGitCallback)
if err != nil {
return err
}
duration := *o.TimeoutDuration
end := time.Now().Add(duration)
return o.waitForGitOpsPullRequest(env, info, end, duration)
}
func (o *DeleteApplicationOptions) waitForGitOpsPullRequest(env *v1.Environment, pullRequestInfo *gits.PullRequestInfo, end time.Time, duration time.Duration) error {
if pullRequestInfo != nil {
logMergeFailure := false
pr := pullRequestInfo.PullRequest
log.Infof("Waiting for pull request %s to merge\n", pr.URL)
for {
gitProvider := pullRequestInfo.GitProvider
err := gitProvider.UpdatePullRequestStatus(pr)
if err != nil {
return fmt.Errorf("Failed to query the Pull Request status for %s %s", pr.URL, err)
}
if pr.Merged != nil && *pr.Merged {
log.Infof("Request %s is merged!\n", util.ColorInfo(pr.URL))
return nil
} else {
if pr.IsClosed() {
log.Warnf("Pull Request %s is closed\n", util.ColorInfo(pr.URL))
return fmt.Errorf("Promotion failed as Pull Request %s is closed without merging", pr.URL)
}
// lets try merge if the status is good
status, err := gitProvider.PullRequestLastCommitStatus(pr)
if err != nil {
log.Warnf("Failed to query the Pull Request last commit status for %s ref %s %s\n", pr.URL, pr.LastCommitSha, err)
} else {
if status == "success" {
if !o.NoMergePullRequest {
err = gitProvider.MergePullRequest(pr, "jx promote automatically merged promotion PR")
if err != nil {
if !logMergeFailure {
logMergeFailure = true
log.Warnf("Failed to merge the Pull Request %s due to %s maybe I don't have karma?\n", pr.URL, err)
}
}
}
} else if status == "error" || status == "failure" {
return fmt.Errorf("Pull request %s last commit has status %s for ref %s", pr.URL, status, pr.LastCommitSha)
}
}
}
if time.Now().After(end) {
return fmt.Errorf("Timed out waiting for pull request %s to merge. Waited %s", pr.URL, duration.String())
}
time.Sleep(*o.PullRequestPollDuration)
}
}
return nil
}
func (o *DeleteApplicationOptions) init() error {
var err error
o.jxClient, o.currentNamespace, err = o.JXClientAndDevNamespace()
if err != nil {
return errors.Wrap(err, "getting jx client")
}
if o.PullRequestPollTime != "" {
duration, err := time.ParseDuration(o.PullRequestPollTime)
if err != nil {
return fmt.Errorf("Invalid duration format %s for option --%s: %s", o.PullRequestPollTime, optionPullRequestPollTime, err)
}
o.PullRequestPollDuration = &duration
}
if o.Timeout != "" {
duration, err := time.ParseDuration(o.Timeout)
if err != nil {
return fmt.Errorf("Invalid duration format %s for option --%s: %s", o.Timeout, optionTimeout, err)
}
o.TimeoutDuration = &duration
}
return nil
}