-
Notifications
You must be signed in to change notification settings - Fork 785
/
gitops.go
149 lines (133 loc) · 4.44 KB
/
gitops.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
package apps
import (
"fmt"
"os"
"path/filepath"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/util"
"k8s.io/helm/pkg/proto/hapi/chart"
"github.com/jenkins-x/jx/pkg/environments"
"github.com/jenkins-x/jx/pkg/log"
"github.com/pkg/errors"
)
// GitOpsOptions is the options used for Git Operations for apps
type GitOpsOptions struct {
*InstallOptions
}
// AddApp adds the app with version rooted in dir from the repository. An alias can be specified.
func (o *GitOpsOptions) AddApp(app string, dir string, version string, repository string, alias string) error {
details := environments.PullRequestDetails{
BranchName: "add-app-" + app + "-" + version,
Title: fmt.Sprintf("Add %s %s", app, version),
Message: fmt.Sprintf("Add app %s %s", app, version),
}
options := environments.EnvironmentPullRequestOptions{
ConfigGitFn: o.ConfigureGitFn,
Gitter: o.Gitter,
ModifyChartFn: environments.CreateAddRequirementFn(app, alias, version,
repository, o.valuesFiles, dir, o.Verbose, o.Helmer),
GitProvider: o.GitProvider,
}
info, err := options.Create(o.DevEnv, o.EnvironmentsDir, &details, nil)
if err != nil {
return errors.Wrapf(err, "creating pr for %s", app)
}
log.Infof("Added app via Pull Request %s\n", info.PullRequest.URL)
return nil
}
// UpgradeApp upgrades the app (or all apps if empty) to a version (
// or latest if empty) from a repository with username and password.
// If one app is being upgraded an alias can be specified.
func (o *GitOpsOptions) UpgradeApp(app string, version string, repository string, username string, password string,
alias string, interrogateChartFunc func(dir string, existing map[string]interface{}) (*ChartDetails,
error)) error {
all := true
details := environments.PullRequestDetails{}
if app != "" {
all = false
if version == "" {
version = "latest"
}
details.BranchName = fmt.Sprintf("upgrade-app-%s-%s", app, version)
} else {
details.BranchName = fmt.Sprintf("upgrade-all-apps")
details.Title = fmt.Sprintf("Upgrade all apps")
details.Message = fmt.Sprintf("Upgrade all apps:\n")
}
var interrogateCleanup func()
defer func() {
if interrogateCleanup != nil {
interrogateCleanup()
}
}()
inspectChartFunc := func(chartDir string, values map[string]interface{}) error {
chartDetails, err := interrogateChartFunc(chartDir, values)
interrogateCleanup = chartDetails.Cleanup
if err != nil {
return errors.Wrapf(err, "asking questions for %s", chartDir)
}
return nil
}
options := environments.EnvironmentPullRequestOptions{
ConfigGitFn: o.ConfigureGitFn,
Gitter: o.Gitter,
ModifyChartFn: environments.CreateUpgradeRequirementsFn(all, app, alias, version, username, password,
o.Helmer, inspectChartFunc, o.Verbose, o.valuesFiles),
GitProvider: o.GitProvider,
}
_, err := options.Create(o.DevEnv, o.EnvironmentsDir, &details, nil)
if err != nil {
return err
}
return nil
}
// DeleteApp deletes the app with alias
func (o *GitOpsOptions) DeleteApp(app string, alias string) error {
modifyChartFn := func(requirements *helm.Requirements, metadata *chart.Metadata, values map[string]interface{},
templates map[string]string, dir string, details *environments.PullRequestDetails) error {
// See if the app already exists in requirements
found := false
for i, d := range requirements.Dependencies {
if d.Name == app && d.Alias == alias {
found = true
requirements.Dependencies[i] = nil
}
}
// If app not found, add it
if !found {
a := app
if alias != "" {
a = fmt.Sprintf("%s with alias %s", a, alias)
}
return fmt.Errorf("unable to delete app %s as not installed", app)
}
if info, err := os.Stat(filepath.Join(dir, app)); err == nil {
if info.IsDir() {
err := util.DeleteFile(info.Name())
if err != nil {
return err
}
} else {
log.Warnf("Not removing %s for %s because it is not a directory", info.Name(), app)
}
}
return nil
}
details := environments.PullRequestDetails{
BranchName: "delete-app-" + app,
Title: fmt.Sprintf("Delete %s", app),
Message: fmt.Sprintf("Delete app %s", app),
}
options := environments.EnvironmentPullRequestOptions{
ConfigGitFn: o.ConfigureGitFn,
Gitter: o.Gitter,
ModifyChartFn: modifyChartFn,
GitProvider: o.GitProvider,
}
info, err := options.Create(o.DevEnv, o.EnvironmentsDir, &details, nil)
if err != nil {
return err
}
log.Infof("Delete app via Pull Request %s\n", info.PullRequest.URL)
return nil
}