forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxsDeploy.go
490 lines (401 loc) · 12.1 KB
/
xsDeploy.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
481
482
483
484
485
486
487
488
489
490
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/pkg/errors"
"io"
"io/ioutil"
"os"
"regexp"
"strings"
"sync"
"text/template"
)
// DeployMode ...
type DeployMode int
const (
// NoDeploy ...
NoDeploy DeployMode = iota
//Deploy ...
Deploy DeployMode = iota
//BGDeploy ...
BGDeploy DeployMode = iota
)
// ValueOfMode ...
func ValueOfMode(str string) (DeployMode, error) {
switch str {
case "NONE":
return NoDeploy, nil
case "DEPLOY":
return Deploy, nil
case "BG_DEPLOY":
return BGDeploy, nil
default:
return NoDeploy, errors.New(fmt.Sprintf("Unknown DeployMode: '%s'", str))
}
}
// String ...
func (m DeployMode) String() string {
return [...]string{
"NONE",
"DEPLOY",
"BG_DEPLOY",
}[m]
}
// Action ...
type Action int
const (
//None ...
None Action = iota
//Resume ...
Resume Action = iota
//Abort ...
Abort Action = iota
//Retry ...
Retry Action = iota
)
// ValueOfAction ...
func ValueOfAction(str string) (Action, error) {
switch str {
case "NONE":
return None, nil
case "RESUME":
return Resume, nil
case "ABORT":
return Abort, nil
case "RETRY":
return Retry, nil
default:
return None, errors.New(fmt.Sprintf("Unknown Action: '%s'", str))
}
}
// String ...
func (a Action) String() string {
return [...]string{
"NONE",
"RESUME",
"ABORT",
"RETRY",
}[a]
}
const loginScript = `#!/bin/bash
xs login -a {{.APIURL}} -u {{.Username}} -p '{{.Password}}' -o {{.Org}} -s {{.Space}} {{.LoginOpts}}
`
const logoutScript = `#!/bin/bash
xs logout`
const deployScript = `#!/bin/bash
xs {{.Mode}} {{.MtaPath}} {{.DeployOpts}}`
const completeScript = `#!/bin/bash
xs {{.Mode.GetDeployCommand}} -i {{.OperationID}} -a {{.Action.GetAction}}
`
func xsDeploy(config xsDeployOptions, telemetryData *telemetry.CustomData, piperEnvironment *xsDeployCommonPipelineEnvironment) {
c := command.Command{}
fileUtils := piperutils.Files{}
err := runXsDeploy(config, piperEnvironment, &c, fileUtils, os.Remove, os.Stdout)
if err != nil {
log.Entry().
WithError(err).
Fatal("failed to execute xs deployment")
}
}
func runXsDeploy(XsDeployOptions xsDeployOptions, piperEnvironment *xsDeployCommonPipelineEnvironment, s command.ShellRunner,
fileUtils piperutils.FileUtils,
fRemove func(string) error,
stdout io.Writer) error {
mode, err := ValueOfMode(XsDeployOptions.Mode)
if err != nil {
return errors.Wrapf(err, "Extracting mode failed: '%s'", XsDeployOptions.Mode)
}
if mode == NoDeploy {
log.Entry().Infof("Deployment skipped intentionally. Deploy mode '%s'", mode.String())
return nil
}
action, err := ValueOfAction(XsDeployOptions.Action)
if err != nil {
return errors.Wrapf(err, "Extracting action failed: '%s'", XsDeployOptions.Action)
}
if mode == Deploy && action != None {
return errors.New(fmt.Sprintf("Cannot perform action '%s' in mode '%s'. Only action '%s' is allowed.", action, mode, None))
}
log.Entry().Debugf("Mode: '%s', Action: '%s'", mode, action)
performLogin := mode == Deploy || (mode == BGDeploy && !(action == Resume || action == Abort))
performLogout := mode == Deploy || (mode == BGDeploy && action != None)
log.Entry().Debugf("performLogin: %t, performLogout: %t", performLogin, performLogout)
{
exists, e := fileUtils.FileExists(XsDeployOptions.MtaPath)
if e != nil {
return e
}
if action == None && !exists {
return errors.New(fmt.Sprintf("Deployable '%s' does not exist", XsDeployOptions.MtaPath))
}
}
if action != None && len(XsDeployOptions.OperationID) == 0 {
return errors.New(fmt.Sprintf("OperationID was not provided. This is required for action '%s'.", action))
}
prOut, pwOut := io.Pipe()
prErr, pwErr := io.Pipe()
s.Stdout(pwOut)
s.Stderr(pwErr)
var e, o string
var wg sync.WaitGroup
wg.Add(2)
go func() {
buf := new(bytes.Buffer)
r := io.TeeReader(prOut, os.Stderr)
io.Copy(buf, r)
o = buf.String()
wg.Done()
}()
go func() {
buf := new(bytes.Buffer)
r := io.TeeReader(prErr, os.Stderr)
io.Copy(buf, r)
e = buf.String()
wg.Done()
}()
var loginErr error
xsSessionFile := ".xsconfig"
if len(XsDeployOptions.XsSessionFile) > 0 {
xsSessionFile = XsDeployOptions.XsSessionFile
}
if performLogin {
loginErr = xsLogin(XsDeployOptions, s)
if loginErr == nil {
err = copyFileFromHomeToPwd(xsSessionFile, fileUtils)
}
}
if loginErr == nil && err == nil {
{
exists, e := fileUtils.FileExists(xsSessionFile)
if e != nil {
return e
}
if !exists {
return fmt.Errorf("xs session file does not exist (%s)", xsSessionFile)
}
}
copyFileFromPwdToHome(xsSessionFile, fileUtils)
switch action {
case Resume, Abort, Retry:
err = complete(mode, action, XsDeployOptions.OperationID, s)
default:
err = deploy(mode, XsDeployOptions, s)
}
}
if loginErr == nil && (performLogout || err != nil) {
if logoutErr := xsLogout(XsDeployOptions, s); logoutErr != nil {
if err == nil {
err = logoutErr
}
} else {
// we delete the xs session file from workspace. From home directory it is deleted by the
// xs command itself.
if e := fRemove(xsSessionFile); e != nil {
err = e
}
log.Entry().Debugf("xs session file '%s' has been deleted from workspace", xsSessionFile)
}
} else {
if loginErr != nil {
log.Entry().Info("Logout skipped since login did not succeed.")
} else if !performLogout {
log.Entry().Info("Logout skipped in order to be able to resume or abort later")
}
}
if err == nil {
err = loginErr
}
if err != nil {
if e := handleLog(fmt.Sprintf("%s/%s", os.Getenv("HOME"), ".xs_logs")); e != nil {
log.Entry().Warningf("Cannot provide the logs: %s", e.Error())
}
}
pwOut.Close()
pwErr.Close()
wg.Wait()
if err == nil && (mode == BGDeploy && action == None) {
piperEnvironment.operationID = retrieveOperationID(o, XsDeployOptions.OperationIDLogPattern)
if len(piperEnvironment.operationID) == 0 && err == nil {
err = errors.New("No operationID found")
}
XsDeployOptions.OperationID = piperEnvironment.operationID // for backward compatibility as long as we render that struc to stdout (printStatus)
}
if err != nil {
log.Entry().Errorf("An error occurred. Stdout from underlying process: >>%s<<. Stderr from underlying process: >>%s<<", o, e)
}
if e := printStatus(XsDeployOptions, stdout); e != nil {
if err == nil {
err = e
}
}
return err
}
func printStatus(XsDeployOptions xsDeployOptions, stdout io.Writer) error {
XsDeployOptionsCopy := XsDeployOptions
XsDeployOptionsCopy.Password = ""
var e error
if b, e := json.Marshal(XsDeployOptionsCopy); e == nil {
fmt.Fprintln(stdout, string(b))
}
return e
}
func handleLog(logDir string) error {
if _, e := os.Stat(logDir); !os.IsNotExist(e) {
log.Entry().Warningf(fmt.Sprintf("Here are the logs (%s):", logDir))
logFiles, e := ioutil.ReadDir(logDir)
if e != nil {
return e
}
if len(logFiles) == 0 {
log.Entry().Warningf("Cannot provide xs logs. No log files found inside '%s'.", logDir)
}
for _, logFile := range logFiles {
buf := make([]byte, 32*1024)
log.Entry().Infof("File: '%s'", logFile.Name())
if f, e := os.Open(fmt.Sprintf("%s/%s", logDir, logFile.Name())); e == nil {
for {
if n, e := f.Read(buf); e != nil {
if e == io.EOF {
break
}
return e
} else {
os.Stderr.WriteString(string(buf[:n]))
}
}
} else {
return e
}
}
} else {
log.Entry().Warningf("Cannot provide xs logs. Log directory '%s' does not exist.", logDir)
}
return nil
}
func retrieveOperationID(deployLog, pattern string) string {
re := regexp.MustCompile(pattern)
lines := strings.Split(deployLog, "\n")
var operationID string
for _, line := range lines {
matched := re.FindStringSubmatch(line)
if len(matched) >= 1 {
operationID = matched[1]
break
}
}
if len(operationID) > 0 {
log.Entry().Infof("Operation identifier: '%s'", operationID)
} else {
log.Entry().Infof("No operation identifier found in >>>>%s<<<<.", deployLog)
}
return operationID
}
func xsLogin(XsDeployOptions xsDeployOptions, s command.ShellRunner) error {
log.Entry().Debugf("Performing xs login. api-url: '%s', org: '%s', space: '%s'",
XsDeployOptions.APIURL, XsDeployOptions.Org, XsDeployOptions.Space)
if e := executeCmd("login", loginScript, XsDeployOptions, s); e != nil {
log.Entry().Errorf("xs login failed: %s", e.Error())
return e
}
log.Entry().Infof("xs login has been performed. api-url: '%s', org: '%s', space: '%s'",
XsDeployOptions.APIURL, XsDeployOptions.Org, XsDeployOptions.Space)
return nil
}
func xsLogout(XsDeployOptions xsDeployOptions, s command.ShellRunner) error {
log.Entry().Debug("Performing xs logout.")
if e := executeCmd("logout", logoutScript, XsDeployOptions, s); e != nil {
return e
}
log.Entry().Info("xs logout has been performed")
return nil
}
func deploy(mode DeployMode, XsDeployOptions xsDeployOptions, s command.ShellRunner) error {
deployCommand, err := mode.GetDeployCommand()
if err != nil {
return err
}
type deployProperties struct {
xsDeployOptions
Mode string
}
log.Entry().Infof("Performing xs %s.", deployCommand)
if e := executeCmd("deploy", deployScript, deployProperties{xsDeployOptions: XsDeployOptions, Mode: deployCommand}, s); e != nil {
return e
}
log.Entry().Infof("xs %s performed.", deployCommand)
return nil
}
func complete(mode DeployMode, action Action, operationID string, s command.ShellRunner) error {
log.Entry().Debugf("Performing xs %s", action)
type completeProperties struct {
xsDeployOptions
Mode DeployMode
Action Action
OperationID string
}
CompleteProperties := completeProperties{Mode: mode, Action: action, OperationID: operationID}
if e := executeCmd("complete", completeScript, CompleteProperties, s); e != nil {
return e
}
return nil
}
func executeCmd(templateID string, commandPattern string, properties interface{}, s command.ShellRunner) error {
tmpl, e := template.New(templateID).Parse(commandPattern)
if e != nil {
return e
}
var script bytes.Buffer
tmpl.Execute(&script, properties)
if e := s.RunShell("/bin/bash", script.String()); e != nil {
return e
}
return nil
}
func copyFileFromHomeToPwd(xsSessionFile string, fileUtils piperutils.FileUtils) error {
src, dest := fmt.Sprintf("%s/%s", os.Getenv("HOME"), xsSessionFile), fmt.Sprintf("%s", xsSessionFile)
log.Entry().Debugf("Copying xs session file from home directory ('%s') to workspace ('%s')", src, dest)
if _, err := fileUtils.Copy(src, dest); err != nil {
return errors.Wrapf(err, "Cannot copy xssession file from home directory ('%s') to workspace ('%s')", src, dest)
}
log.Entry().Debugf("xs session file copied from home directory ('%s') to workspace ('%s')", src, dest)
return nil
}
func copyFileFromPwdToHome(xsSessionFile string, fileUtils piperutils.FileUtils) error {
//
// We rely on running inside a docker container which is discarded after a single use.
// In general it is not a good idea to update files in the build users home directory in case
// we are on an infrastructure which is used not only for single builds since updates at that level
// affects also other builds.
//
src, dest := fmt.Sprintf("%s", xsSessionFile), fmt.Sprintf("%s/%s", os.Getenv("HOME"), xsSessionFile)
log.Entry().Debugf("Copying xs session file from workspace ('%s') to home directory ('%s')", src, dest)
if _, err := fileUtils.Copy(src, dest); err != nil {
return errors.Wrapf(err, "Cannot copy xssession file from workspace ('%s') to home directory ('%s')", src, dest)
}
log.Entry().Debugf("xs session file copied from workspace ('%s') to home directory ('%s')", src, dest)
return nil
}
//GetAction ...
func (a Action) GetAction() (string, error) {
switch a {
case Resume, Abort, Retry:
return strings.ToLower(a.String()), nil
}
return "", errors.New(fmt.Sprintf("Invalid deploy mode: '%s'.", a))
}
//GetDeployCommand ...
func (m DeployMode) GetDeployCommand() (string, error) {
switch m {
case Deploy:
return "deploy", nil
case BGDeploy:
return "bg-deploy", nil
}
return "", errors.New(fmt.Sprintf("Invalid deploy mode: '%s'.", m))
}