-
Notifications
You must be signed in to change notification settings - Fork 786
/
step_helm.go
230 lines (203 loc) · 5.8 KB
/
step_helm.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
package cmd
import (
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"os"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
)
const (
PROW_JOB_ID = "PROW_JOB_ID"
REPO_OWNER = "REPO_OWNER"
REPO_NAME = "REPO_NAME"
PULL_PULL_SHA = "PULL_PULL_SHA"
)
// StepHelmOptions contains the command line flags
type StepHelmOptions struct {
StepOptions
Dir string
https bool
GitProvider string
}
// NewCmdStepHelm Steps a command object for the "step" command
func NewCmdStepHelm(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &StepHelmOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "helm",
Short: "helm [command]",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.AddCommand(NewCmdStepHelmApply(f, in, out, errOut))
cmd.AddCommand(NewCmdStepHelmBuild(f, in, out, errOut))
cmd.AddCommand(NewCmdStepHelmEnv(f, in, out, errOut))
cmd.AddCommand(NewCmdStepHelmInstall(f, in, out, errOut))
cmd.AddCommand(NewCmdStepHelmList(f, in, out, errOut))
cmd.AddCommand(NewCmdStepHelmRelease(f, in, out, errOut))
cmd.AddCommand(NewCmdStepHelmVersion(f, in, out, errOut))
return cmd
}
// Run implements this command
func (o *StepHelmOptions) Run() error {
return o.Cmd.Help()
}
func (o *StepHelmOptions) addStepHelmFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.Dir, "dir", "d", ".", "The directory containing the helm chart to apply")
cmd.Flags().BoolVarP(&o.https, "clone-https", "", true, "Clone the environment Git repo over https rather than ssh which uses `git@foo/bar.git`")
cmd.Flags().StringVarP(&o.GitProvider, "git-provider", "", "github.com", "The Git provider for the environment Git repository")
}
func (o *StepHelmOptions) findStagingRepoIds() ([]string, error) {
answer := []string{}
files, err := filepath.Glob(statingRepositoryProperties)
if err != nil {
return answer, err
}
for _, f := range files {
data, err := ioutil.ReadFile(f)
if err != nil {
return answer, fmt.Errorf("Failed to load file %s: %s", f, err)
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, statingRepositoryIdPrefix) {
id := strings.TrimSpace(strings.TrimPrefix(line, statingRepositoryIdPrefix))
if id != "" {
answer = append(answer, id)
}
}
}
}
return answer, nil
}
func (o *StepHelmOptions) dropRepositories(repoIds []string, message string) error {
var answer error
for _, repoId := range repoIds {
err := o.dropRepository(repoId, message)
if err != nil {
log.Warnf("Failed to drop repository %s: %s\n", util.ColorInfo(repoIds), util.ColorError(err))
if answer == nil {
answer = err
}
}
}
return answer
}
func (o *StepHelmOptions) dropRepository(repoId string, message string) error {
if repoId == "" {
return nil
}
log.Infof("Dropping helm release repository %s\n", util.ColorInfo(repoId))
err := o.RunCommand("mvn",
"org.sonatype.plugins:helm-staging-maven-plugin:1.6.5:rc-drop",
"-DserverId=oss-sonatype-staging",
"-DhelmUrl=https://oss.sonatype.org",
"-DstagingRepositoryId="+repoId,
"-Ddescription=\""+message+"\" -DstagingProgressTimeoutMinutes=60")
if err != nil {
log.Warnf("Failed to drop repository %s due to: %s\n", repoId, err)
} else {
log.Infof("Dropped repository %s\n", util.ColorInfo(repoId))
}
return err
}
func (o *StepHelmOptions) releaseRepository(repoId string) error {
if repoId == "" {
return nil
}
log.Infof("Releasing helm release repository %s\n", util.ColorInfo(repoId))
options := o
err := options.RunCommand("mvn",
"org.sonatype.plugins:helm-staging-maven-plugin:1.6.5:rc-release",
"-DserverId=oss-sonatype-staging",
"-DhelmUrl=https://oss.sonatype.org",
"-DstagingRepositoryId="+repoId,
"-Ddescription=\"Next release is ready\" -DstagingProgressTimeoutMinutes=60")
if err != nil {
log.Infof("Failed to release repository %s due to: %s\n", repoId, err)
} else {
log.Infof("Released repository %s\n", util.ColorInfo(repoId))
}
return err
}
func (o *StepHelmOptions) cloneProwPullRequest(dir, gitProvider string) (string, error) {
stepOpts := StepOptions{
CommonOptions: o.CommonOptions,
}
gitOpts := StepGitCredentialsOptions{
StepOptions: stepOpts,
}
err := gitOpts.Run()
if err != nil {
return "", fmt.Errorf("failed to create Git credentials file: %v", err)
}
org := os.Getenv(REPO_OWNER)
if org == "" {
return "", fmt.Errorf("no %s env var found", REPO_OWNER)
}
repo := os.Getenv(REPO_NAME)
if org == "" {
return "", fmt.Errorf("no %s env var found", REPO_NAME)
}
var gitURL string
if o.https {
gitURL = fmt.Sprintf("https://%s/%s/%s.git", gitProvider, org, repo)
} else {
gitURL = fmt.Sprintf("git@%s:%s/%s.git", gitProvider, org, repo)
}
err = o.RunCommand("git", "clone", gitURL)
if err != nil {
return "", err
}
prCommit := os.Getenv(PULL_PULL_SHA)
if org == "" {
return "", fmt.Errorf("no %s env var found", PULL_PULL_SHA)
}
if dir != "" {
dir = filepath.Join(dir, repo)
} else {
pwd, err := os.Getwd()
if err != nil {
return "", err
}
dir = filepath.Join(pwd, repo)
}
err = o.runCommandFromDir(dir, "git", "checkout", prCommit)
if err != nil {
return "", err
}
err = o.RunCommand("cd", repo)
if err != nil {
return "", err
}
err = o.runCommandFromDir(dir, "git", "checkout", "-b", "pr")
if err != nil {
return "", err
}
exists, err := util.FileExists(filepath.Join(dir, "env"))
if err != nil {
return "", err
}
if exists {
dir = filepath.Join(dir, "env")
}
return dir, nil
}