forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_extensions_repository.go
298 lines (268 loc) · 8.69 KB
/
update_extensions_repository.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
package cmd
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pborman/uuid"
"github.com/pkg/errors"
"github.com/stoewer/go-strcase"
"github.com/ghodss/yaml"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// CreateExtensionsRepositoryOptions the flags for running create cluster
type UpdateExtensionsRepositoryOptions struct {
UpdateExtensionsOptions
Flags InitFlags
InputFile string
OutputFile string
}
type UpdateExtensionsRepositoryFlags struct {
}
type ExtensionsRepositoryLock struct {
Version int `json:"version"`
Extensions []ExtensionRepositoryLock `json:"extensions"`
}
type ExtensionRepositoryLock struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
UUID string `json:"uuid"`
Version string `json:"version"`
Description string `json:"description"`
When []string `json:"when,omitempty"`
Given string `json:"given,omitempty"`
Type string `json:"type,omitempty"`
Script string `json:"script"`
Parameters []struct {
Name string `json:"name"`
Description string `json:"description"`
DefaultValue string `json:"defaultValue"`
} `json:"parameters,omitempty"`
}
type ExtensionsRepositoryConstraints struct {
Extensions []struct {
Extension string `json:"extension"`
Version string `json:"version"`
} `json:"extensions"`
}
type ExtensionDefinitions struct {
Version string `json:"version,omitempty"`
Extensions []struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
UUID string `json:"uuid"`
Description string `json:"description"`
When []string `json:"when,omitempty"`
Given string `json:"given,omitempty"`
Type string `json:"type,omitempty"`
ScriptFile string `json:"scriptFile,omitempty"`
Parameters []struct {
Name string `json:"name"`
Description string `json:"description"`
DefaultValue string `json:"defaultValue"`
} `json:"parameters"`
} `json:"extensions"`
}
var (
updateExtensionsRepositoryLong = templates.LongDesc(`
This command updates the jenkins-x-extensions-repository.lock.yaml file from a jenkins-x-extensions-repository.yaml file
`)
updateExtensionsRepositoryExample = templates.Examples(`
# Updates a file called jenkins-x-extensions-repository.lock.yaml from a file called jenkins-x-extensions-repository.yaml in the same directory
jx update extensions repository
# Allows the input and output file to specified
jx update extensions repository -i my-repo.yaml -o my-repo.lock.yaml
`)
)
// NewCmdGet creates a command object for the generic "init" action, which
// installs the dependencies required to run the jenkins-x platform on a Kubernetes cluster.
func NewCmdUpdateExtensionsRepository(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &UpdateExtensionsRepositoryOptions{
UpdateExtensionsOptions: UpdateExtensionsOptions{
UpdateOptions: UpdateOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
},
}
cmd := &cobra.Command{
Use: "repository",
Short: "Updates an extension repository",
Long: fmt.Sprintf(updateExtensionsRepositoryLong),
Example: updateExtensionsRepositoryExample,
Run: func(cmd2 *cobra.Command, args []string) {
options.Cmd = cmd2
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.InputFile, "input-file", "i", "jenkins-x-extensions-repository.yaml", "The input file to read to generate the .lock file")
cmd.Flags().StringVarP(&options.OutputFile, "output-file", "o", "jenkins-x-extensions-repository.lock.yaml", "The output .lock file")
return cmd
}
func (o *UpdateExtensionsRepositoryOptions) Run() error {
constraints := ExtensionsRepositoryConstraints{}
err := constraints.LoadFromFile(o.InputFile)
if err != nil {
return err
}
oldLock := ExtensionsRepositoryLock{}
err = oldLock.LoadFromFile(o.OutputFile)
if err != nil {
return err
}
oldVersion := oldLock.Version
oldLockNameMap := make(map[string]ExtensionRepositoryLock, 0)
newLock := ExtensionsRepositoryLock{
Extensions: make([]ExtensionRepositoryLock, 0),
}
newLock.Version = oldVersion + 1
for _, c := range constraints.Extensions {
if strings.HasPrefix(c.Extension, "github.com") {
s := strings.Split(c.Extension, "/")
if len(s) != 3 {
errors.New(fmt.Sprintf("Cannot parse extension path %s", util.ColorInfo(c.Extension)))
}
org := s[1]
repo := s[2]
version := c.Version
if version == "latest" {
version, err = util.GetLatestVersionStringFromGitHub(org, repo)
if err != nil {
return err
}
}
definitionsUrl := fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/v%s", org, repo, version)
definitionsFileUrl := fmt.Sprintf("%s/jenkins-x-extension-definitions.yaml", definitionsUrl)
extensionDefinitions := ExtensionDefinitions{}
extensionDefinitions.LoadFromURL(definitionsFileUrl, c.Extension, c.Version)
for _, ed := range extensionDefinitions.Extensions {
// It's best practice to assign a UUID to an extension, but if it doesn't have one we
// try to give it the one it had last
UUID := ed.UUID
if UUID == "" {
// Lazy initialize the oldLockNameMap
if len(oldLockNameMap) == 0 {
for _, l := range oldLock.Extensions {
oldLockNameMap[fmt.Sprintf("%s.%s", l.Namespace, l.Name)] = l
}
}
UUID = oldLockNameMap[fmt.Sprintf("%s.%s", ed.Namespace, ed.Name)].UUID
}
// If the UUID is still empty, generate one
if UUID == "" {
UUID = uuid.New()
}
scriptFile := ed.ScriptFile
if scriptFile == "" {
scriptFile = fmt.Sprintf("%s.sh", strings.ToLower(strcase.SnakeCase(ed.Name)))
}
script, err := o.LoadAsStringFromURL(fmt.Sprintf("%s/%s", definitionsUrl, scriptFile))
if err != nil {
return err
}
eLock := ExtensionRepositoryLock{
Name: ed.Name,
Namespace: ed.Namespace,
Version: version,
UUID: UUID,
Description: ed.Description,
Parameters: ed.Parameters,
When: ed.When,
Given: ed.Given,
Type: ed.Type,
Script: script,
}
newLock.Extensions = append(newLock.Extensions, eLock)
}
} else {
return errors.New(fmt.Sprintf("Only github.com is supported, use a format like %s", util.ColorInfo("github.com/jenkins-x/ext-jacoco")))
}
}
bytes, err := yaml.Marshal(newLock)
if err != nil {
return err
}
log.Printf("Updating extensions repository from %s to %s. Changes are %s\n", util.ColorInfo(oldLock.Version), util.ColorInfo(newLock.Version), util.ColorInfo("Unknown"))
err = ioutil.WriteFile(o.OutputFile, bytes, 0755)
if err != nil {
return err
}
return nil
}
func (o *UpdateExtensionsRepositoryOptions) LoadAsStringFromURL(url string) (result string, err error) {
httpClient := &http.Client{Timeout: 10 * time.Second}
resp, err := httpClient.Get(fmt.Sprintf("%s?version=%d", url, time.Now().UnixNano()/int64(time.Millisecond)))
defer resp.Body.Close()
if err != nil {
return "", err
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(bytes), nil
}
func (constraints *ExtensionsRepositoryConstraints) LoadFromFile(inputFile string) (err error) {
path, err := filepath.Abs(inputFile)
if err != nil {
return err
}
y, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = yaml.Unmarshal(y, constraints)
if err != nil {
return err
}
return nil
}
func (lock *ExtensionsRepositoryLock) LoadFromFile(inputFile string) (err error) {
path, err := filepath.Abs(inputFile)
if err != nil {
return err
}
y, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = yaml.Unmarshal(y, lock)
if err != nil {
return err
}
return nil
}
func (lock *ExtensionDefinitions) LoadFromURL(definitionsUrl string, extension string, version string) (err error) {
httpClient := &http.Client{Timeout: 10 * time.Second}
resp, err := httpClient.Get(fmt.Sprintf("%s?version=%d", definitionsUrl, time.Now().UnixNano()/int64(time.Millisecond)))
if resp.StatusCode < 200 || resp.StatusCode > 299 {
log.Printf("Unable to find Extension Definitions at %s for %s with version %s", util.ColorWarning(definitionsUrl), util.ColorWarning(extension), util.ColorWarning(version))
return nil
}
if err != nil {
return err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = yaml.Unmarshal(bytes, lock)
if err != nil {
return err
}
return nil
}