-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
480 lines (412 loc) · 14.3 KB
/
plugin.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Package plasmactlmeta implements meta launchr plugin
package plasmactlmeta
import (
"bytes"
"errors"
"fmt"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"github.com/launchrctl/keyring"
"github.com/launchrctl/launchr"
"github.com/spf13/cobra"
)
func init() {
launchr.RegisterPlugin(&Plugin{})
}
var tplAddCredentials = "execute '%s login --url=%s' to add credentials to keyring" //nolint G101
// Plugin is launchr plugin providing meta action.
type Plugin struct {
k keyring.Keyring
app launchr.App
}
// PluginInfo implements launchr.Plugin interface.
func (p *Plugin) PluginInfo() launchr.PluginInfo {
return launchr.PluginInfo{
Weight: 1337,
}
}
// OnAppInit implements launchr.Plugin interface.
func (p *Plugin) OnAppInit(app launchr.App) error {
app.GetService(&p.k)
p.app = app
return nil
}
type metaOptions struct {
bin string
verboseCount int
keyringPassphrase string
override string
clean bool
last bool
ci bool
skipBump bool
}
// CobraAddCommands implements launchr.CobraPlugin interface to provide meta functionality.
func (p *Plugin) CobraAddCommands(rootCmd *launchr.Command) error {
options := metaOptions{
// Retrieve current bin name from args to use in consequent commands.
bin: os.Args[0],
}
metaCmd := &launchr.Command{
Use: "meta [flags] environment tags",
Short: "Executes bump + compose + sync + package + publish + deploy",
Aliases: []string{"deliver"},
Args: cobra.MatchAll(cobra.ExactArgs(2), cobra.OnlyValidArgs),
RunE: func(cmd *launchr.Command, args []string) error {
// Don't show usage help on a runtime error.
cmd.SilenceUsage = true
err := ensureKeyringPassphraseSet(cmd, &options)
if err != nil {
return err
}
verboseCount, err := cmd.Flags().GetCount("verbose")
if err != nil {
return err
}
options.verboseCount = verboseCount
return p.meta(args[0], args[1], options)
},
}
metaCmd.SetArgs([]string{"environment", "tags"})
metaCmd.Flags().StringVar(&options.override, "override", "", "Bump --sync override option")
metaCmd.Flags().BoolVar(&options.clean, "clean", false, "Clean flag for local compose command")
metaCmd.Flags().BoolVar(&options.last, "last", false, "Last flag for local bump command")
metaCmd.Flags().BoolVar(&options.ci, "ci", false, "Execute all commands and deploy in CI")
metaCmd.Flags().BoolVar(&options.skipBump, "skip-bump", false, "Skip execution of local bump command")
rootCmd.AddCommand(metaCmd)
return nil
}
func ensureKeyringPassphraseSet(cmd *launchr.Command, options *metaOptions) error {
keyringPassphrase, err := cmd.Flags().GetString("keyring-passphrase")
if err != nil {
return fmt.Errorf("error while getting keyring-passphrase option value: %w", err)
}
if keyringPassphrase == "" {
askPass := keyring.AskPassWithTerminal{}
var passphrase string
var errGet error
passphrase, errGet = askPass.GetPass()
if errGet != nil {
return errGet
}
keyringPassphrase = passphrase
err = cmd.Flags().Set("keyring-passphrase", keyringPassphrase)
if err != nil {
return err
}
}
options.keyringPassphrase = keyringPassphrase
return nil
}
func (p *Plugin) createCommand(command string, args ...string) *exec.Cmd {
streams := p.app.Streams()
cmd := exec.Command(command, args...)
cmd.Stdout = streams.Out()
cmd.Stderr = streams.Err()
// Set directly because it's a file. Exec command has special treatment for [*os.File] input.
cmd.Stdin = os.Stdin
return cmd
}
func (p *Plugin) meta(environment, tags string, options metaOptions) error {
var commonArgs []string
verbosity := ""
if options.verboseCount > 0 {
verbosity = "-" + strings.Repeat("v", options.verboseCount)
commonArgs = append(commonArgs, verbosity)
launchr.Log().Debug("verbosity level", "level", verbosity)
}
launchr.Log().Info("arguments", "environment", environment, "tags", tags)
var username string
var password string
if options.ci {
launchr.Term().Info().Println("Starting CI build")
gitlabDomain := "https://projects.skilld.cloud"
launchr.Term().Info().Printfln("Getting %s credentials from keyring", gitlabDomain)
ci, save, err := getCredentials(gitlabDomain, username, password, p.k)
if err != nil {
return err
}
launchr.Term().Printfln("URL: %s", ci.URL)
launchr.Term().Printfln("Username: %s", ci.Username)
username := ci.Username
password := ci.Password
comparisonRef := ""
if options.override != "" {
comparisonRef = options.override
launchr.Term().Printfln("Comparison artifact override: %s", comparisonRef)
}
// Get OAuth token
accessToken, err := getOAuthToken(gitlabDomain, username, password)
if err != nil {
return fmt.Errorf("failed to get OAuth token: %w", err)
}
// Save gitlab credentials to keyiring once we are sure that they are correct (after 1st successful api reuuest)
if save {
err = p.k.Save()
launchr.Log().Debug("saving credentials to keyring", "url", gitlabDomain)
if err != nil {
launchr.Log().Error("error during saving keyring file", "error", err)
}
}
// Get branch name
branchName, err := getBranchName()
if err != nil {
return fmt.Errorf("failed to get branch name: %w", err)
}
// Get repo name
repoName, err := getRepoName()
if err != nil {
return fmt.Errorf("failed to get repo name: %w", err)
}
// Get project ID
projectID, err := getProjectID(gitlabDomain, username, password, accessToken, repoName)
if err != nil {
return fmt.Errorf("failed to get ID of project %q: %w", repoName, err)
}
// Trigger pipeline
pipelineID, err := triggerPipeline(gitlabDomain, username, password, accessToken, projectID, branchName, environment, tags, comparisonRef)
if err != nil {
return fmt.Errorf("failed to trigger pipeline: %w", err)
}
// Get all jobs in the pipeline
jobs, err := getJobsInPipeline(gitlabDomain, username, password, accessToken, projectID, pipelineID)
if err != nil {
return fmt.Errorf("failed to retrieve jobs in pipeline: %w", err)
}
// Find the target job ID
var targetJobID int
for _, job := range jobs {
if job.Name == targetJobName {
targetJobID = job.ID
break
}
}
if targetJobID == 0 {
return fmt.Errorf("no %s job found in pipeline", targetJobName)
}
// Trigger the manual job
err = triggerManualJob(gitlabDomain, username, password, accessToken, projectID, targetJobID, pipelineID)
if err != nil {
return fmt.Errorf("failed to trigger manual job: %w", err)
}
} else {
launchr.Term().Info().Println("Starting local build")
// Check if provided keyring pw is correct, since it will be used for multiple commands
// Check if publish command credentials are available in keyring and correct as stdin will not be available in goroutine
artifactsRepositoryDomain := "https://repositories.skilld.cloud"
var accessibilityCode int
if isURLAccessible("http://repositories.interaction.svc.skilld:8081", &accessibilityCode) {
artifactsRepositoryDomain = "http://repositories.interaction.svc.skilld:8081"
}
launchr.Term().Println("Checking keyring...")
keyringEntryName := "Artifacts repository"
err := validateCredentials(artifactsRepositoryDomain, options.bin, p.k, keyringEntryName)
if err != nil {
return err
}
// Appending --keyring-passphrase to commands
keyringCmd := func(cmd *exec.Cmd) *exec.Cmd {
if options.keyringPassphrase != "" {
cmd.Args = append(cmd.Args, "--keyring-passphrase", options.keyringPassphrase)
}
return cmd
}
// Commands executed sequentially
if !options.skipBump {
bumpArgs := []string{"bump"}
if options.last {
bumpArgs = append(bumpArgs, "--last")
}
bumpArgs = append(bumpArgs, commonArgs...)
bumpCmd := keyringCmd(p.createCommand(options.bin, bumpArgs...))
launchr.Term().Println(sanitizeString(bumpCmd.String(), options.keyringPassphrase))
_ = bumpCmd.Run() //nolint
launchr.Term().Println()
} else {
launchr.Term().Info().Println("--skip-bump option detected: Skipping bump execution")
}
composeArgs := []string{"compose", "--skip-not-versioned", "--conflicts-verbosity"}
if options.clean {
composeArgs = append(composeArgs, "--clean")
}
composeArgs = append(composeArgs, commonArgs...)
composeCmd := keyringCmd(p.createCommand(options.bin, composeArgs...))
launchr.Term().Println(sanitizeString(composeCmd.String(), options.keyringPassphrase))
composeErr := composeCmd.Run()
if composeErr != nil {
return handleCmdErr(composeErr, "compose error")
}
launchr.Term().Println()
bumpSyncArgs := []string{"bump", "--sync"}
if options.override != "" {
bumpSyncArgs = append(bumpSyncArgs, "--override", options.override)
}
bumpSyncArgs = append(bumpSyncArgs, commonArgs...)
syncCmd := keyringCmd(p.createCommand(options.bin, bumpSyncArgs...))
launchr.Term().Println(sanitizeString(syncCmd.String(), options.keyringPassphrase))
syncErr := syncCmd.Run()
if syncErr != nil {
return handleCmdErr(syncErr, "sync error")
}
// Commands executed in parallel
var packageStdOut bytes.Buffer
var packageStdErr bytes.Buffer
var packageErr error
var publishStdOut bytes.Buffer
var publishStdErr bytes.Buffer
var publishErr error
launchr.Term().Println()
wg := &sync.WaitGroup{}
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
packageCmdArgs := append([]string{"package"}, commonArgs...)
packageCmd := p.createCommand(options.bin, packageCmdArgs...)
packageCmd.Stdout = &packageStdOut
packageCmd.Stderr = &packageStdErr
packageCmd.Stdin = nil // Any interaction will prevent waitgroup to finish and thus stuck before print of stdout
// cli.Println(sanitizeString(packageCmd.String(), options.keyringPassphrase)) // TODO: Find a way to prevent it to fill deploy stdin
packageErr = packageCmd.Run()
if packageErr != nil {
publishErr = handleCmdErr(packageErr, "package error")
return
}
publishCmdArgs := append([]string{"publish"}, commonArgs...)
publishCmd := keyringCmd(p.createCommand(options.bin, publishCmdArgs...))
publishCmd.Stdout = &publishStdOut
publishCmd.Stderr = &publishStdErr
publishCmd.Stdin = nil // Any interaction will prevent waitgroup to finish and thus stuck before print of stdout
// cli.Println(sanitizeString(publishCmd.String(), keyringPassphrase)) // TODO: Debug why it appears during deploy command stdout
publishErr = publishCmd.Run()
if publishErr != nil {
publishErr = handleCmdErr(publishErr, "publish error")
return
}
}(wg)
var deployErr error
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
deployCmdArgs := []string{"platform:deploy"}
deployCmdArgs = append(deployCmdArgs, environment)
deployCmdArgs = append(deployCmdArgs, tags)
if verbosity != "" {
deployCmdArgs = append(deployCmdArgs, "--debug")
}
deployCmd := keyringCmd(p.createCommand(options.bin, deployCmdArgs...))
launchr.Term().Println(sanitizeString(deployCmd.String(), options.keyringPassphrase))
deployErr = deployCmd.Run()
if deployErr != nil {
deployErr = handleCmdErr(deployErr, "deploy error")
return
}
}(wg)
wg.Wait()
if packageStdOut.Len() > 0 {
launchr.Term().Println()
launchr.Term().Println("package stdout:")
launchr.Term().Println(packageStdOut.String())
}
if packageStdErr.Len() > 0 {
launchr.Term().Println("package stderr:")
launchr.Term().Println(packageStdErr.String())
}
if publishStdOut.Len() > 0 {
launchr.Term().Println()
launchr.Term().Println("publish stdout:")
launchr.Term().Println(publishStdOut.String())
}
if publishStdErr.Len() > 0 {
launchr.Term().Println("publish stderr:")
launchr.Term().Println(publishStdErr.String())
}
// Return all error messages, the first error code will be used as a result.
errJoin := errors.Join(packageErr, publishErr, deployErr)
if errJoin != nil {
return errJoin
}
}
return nil
}
func handleCmdErr(cmdErr error, msg string) error {
var exitErr *exec.ExitError
if errors.As(cmdErr, &exitErr) {
return launchr.NewExitError(exitErr.ExitCode(), msg)
}
return cmdErr
}
func validateCredentials(url, plasmaBinary string, k keyring.Keyring, keyringEntryName string) error {
if !k.Exists() {
launchr.Term().Error().Println("Keyring doesn't exist")
return fmt.Errorf(tplAddCredentials, plasmaBinary, url)
}
ci, err := k.GetForURL(url)
if len(ci.URL) != 0 && len(ci.Username) != 0 && len(ci.Password) != 0 {
launchr.Term().Success().Println("Keyring was unlocked successfully: %s credentials were found", keyringEntryName)
}
if err != nil {
if errors.Is(err, keyring.ErrEmptyPass) {
return err
} else if errors.Is(err, keyring.ErrNotFound) {
launchr.Term().Success().Println("Keyring was unlocked successfully: %s credentials were not found", keyringEntryName)
return fmt.Errorf(tplAddCredentials, plasmaBinary, url)
} else if !errors.Is(err, keyring.ErrNotFound) {
launchr.Log().Error("error", "error", err)
return errors.New("the keyring is malformed or wrong passphrase provided")
}
}
return nil
}
func getCredentials(url, username, password string, k keyring.Keyring) (keyring.CredentialsItem, bool, error) {
ci, err := k.GetForURL(url)
save := false
if err != nil {
if errors.Is(err, keyring.ErrEmptyPass) {
return ci, false, err
} else if !errors.Is(err, keyring.ErrNotFound) {
launchr.Log().Error("error", "error", err)
return ci, false, errors.New("the keyring is malformed or wrong passphrase provided")
}
ci = keyring.CredentialsItem{}
ci.URL = url
ci.Username = username
ci.Password = password
if ci.Username == "" || ci.Password == "" {
if ci.URL != "" {
launchr.Term().Info().Printfln("Please add login and password for URL - %s", ci.URL)
}
err = keyring.RequestCredentialsFromTty(&ci)
if err != nil {
return ci, false, err
}
}
err = k.AddItem(ci)
if err != nil {
return ci, false, err
}
save = true
}
return ci, save, nil
}
func isURLAccessible(url string, code *int) bool {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return false
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
*code = resp.StatusCode
return resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices
}
func sanitizeString(command string, passphrase string) string {
if passphrase != "" {
return strings.ReplaceAll(command, passphrase, "[masked]")
}
return command
}