/
deploy.go
715 lines (675 loc) · 16.8 KB
/
deploy.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
// Copyright 2017 tsuru-client authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package client
import (
"archive/tar"
"bufio"
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/tsuru/gnuflag"
tsuruapp "github.com/tsuru/tsuru/app"
"github.com/tsuru/tsuru/cmd"
tsuruIo "github.com/tsuru/tsuru/io"
"github.com/tsuru/tsuru/safe"
)
type deployList []tsuruapp.DeployData
func (dl deployList) Len() int {
return len(dl)
}
func (dl deployList) Swap(i, j int) {
dl[i], dl[j] = dl[j], dl[i]
}
func (dl deployList) Less(i, j int) bool {
return dl[i].Timestamp.Before(dl[j].Timestamp)
}
type AppDeployList struct {
cmd.GuessingCommand
}
func (c *AppDeployList) Info() *cmd.Info {
return &cmd.Info{
Name: "app-deploy-list",
Usage: "app-deploy-list [-a/--app <appname>]",
Desc: "List information about deploys for an application.",
}
}
func (c *AppDeployList) Run(context *cmd.Context, client *cmd.Client) error {
appName, err := c.Guess()
if err != nil {
return err
}
url, err := cmd.GetURL(fmt.Sprintf("/deploys?app=%s&limit=10", appName))
if err != nil {
return err
}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
response, err := client.Do(request)
if err != nil {
return err
}
if response.StatusCode == http.StatusNoContent {
fmt.Fprintf(context.Stdout, "App %s has no deploy.\n", appName)
return nil
}
defer response.Body.Close()
result, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
var deploys []tsuruapp.DeployData
err = json.Unmarshal(result, &deploys)
if err != nil {
return err
}
sort.Sort(sort.Reverse(deployList(deploys)))
table := cmd.NewTable()
table.Headers = cmd.Row([]string{"Image (Rollback)", "Origin", "User", "Date (Duration)", "Error"})
for _, deploy := range deploys {
timestamp := deploy.Timestamp.Local().Format(time.Stamp)
seconds := deploy.Duration / time.Second
minutes := seconds / 60
seconds = seconds % 60
if deploy.Origin == "git" {
if len(deploy.Commit) > 7 {
deploy.Commit = deploy.Commit[:7]
}
deploy.Origin = fmt.Sprintf("git (%s)", deploy.Commit)
}
timestamp = fmt.Sprintf("%s (%02d:%02d)", timestamp, minutes, seconds)
if deploy.CanRollback {
deploy.Image += " (*)"
}
rowData := []string{deploy.Image, deploy.Origin, deploy.User, timestamp, deploy.Error}
if deploy.Error != "" {
for i, el := range rowData {
if el != "" {
rowData[i] = cmd.Colorfy(el, "red", "", "")
}
}
}
table.LineSeparator = true
table.AddRow(cmd.Row(rowData))
}
context.Stdout.Write(table.Bytes())
return nil
}
type AppDeploy struct {
cmd.GuessingCommand
image string
message string
fs *gnuflag.FlagSet
}
func (c *AppDeploy) Flags() *gnuflag.FlagSet {
if c.fs == nil {
c.fs = c.GuessingCommand.Flags()
image := "The image to deploy in app"
c.fs.StringVar(&c.image, "image", "", image)
c.fs.StringVar(&c.image, "i", "", image)
message := "A message describing this deploy"
c.fs.StringVar(&c.message, "message", "", message)
c.fs.StringVar(&c.message, "m", "", message)
}
return c.fs
}
func (c *AppDeploy) Info() *cmd.Info {
desc := `Deploys set of files and/or directories to tsuru server. Some examples of
calls are:
::
$ tsuru app-deploy .
$ tsuru app-deploy myfile.jar Procfile
$ tsuru app-deploy mysite
$ tsuru app-deploy -i http://registry.mysite.com:5000/image-name
`
return &cmd.Info{
Name: "app-deploy",
Usage: "app-deploy [-a/--app <appname>] [-i/--image <image_url>] [-m/--message <message>] <file-or-dir-1> [file-or-dir-2] ... [file-or-dir-n]",
Desc: desc,
MinArgs: 0,
}
}
type safeWriter struct {
mu sync.Mutex
w io.Writer
}
func (w *safeWriter) Write(p []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.w.Write(p)
}
func (c *AppDeploy) Run(context *cmd.Context, client *cmd.Client) error {
context.RawOutput()
if c.image == "" && len(context.Args) == 0 {
return errors.New("You should provide at least one file or a docker image to deploy.\n")
}
if c.image != "" && len(context.Args) > 0 {
return errors.New("You can't deploy files and docker image at the same time.\n")
}
appName, err := c.Guess()
if err != nil {
return err
}
u, err := cmd.GetURL("/apps/" + appName)
if err != nil {
return err
}
request, err := http.NewRequest("GET", u, nil)
if err != nil {
return err
}
_, err = client.Do(request)
if err != nil {
return err
}
origin := "app-deploy"
if c.image != "" {
origin = "image"
}
values := url.Values{}
values.Set("origin", origin)
if c.message != "" {
values.Set("message", c.message)
}
u, err = cmd.GetURL(fmt.Sprintf("/apps/%s/deploy", appName))
if err != nil {
return err
}
body := safe.NewBuffer(nil)
request, err = http.NewRequest("POST", u, body)
if err != nil {
return err
}
buf := safe.NewBuffer(nil)
stream := tsuruIo.NewStreamWriter(context.Stdout, nil)
safeStdout := &safeWriter{w: &tsuruIo.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(stream)}}
respBody := firstWriter{Writer: io.MultiWriter(safeStdout, buf)}
if c.image != "" {
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
values.Set("image", c.image)
_, err = body.WriteString(values.Encode())
if err != nil {
return err
}
fmt.Fprint(context.Stdout, "Deploying image...")
} else {
writer := multipart.NewWriter(body)
for k := range values {
writer.WriteField(k, values.Get(k))
}
var file io.Writer
file, err = writer.CreateFormFile("file", "archive.tar.gz")
if err != nil {
return err
}
ignoreSet := make(map[string]struct{})
ignorePatterns, _ := readTsuruIgnore()
for _, pattern := range ignorePatterns {
ignSet, errProc := processTsuruIgnore(pattern, context.Args...)
if errProc != nil {
return errProc
}
for k, v := range ignSet {
ignoreSet[k] = v
}
}
err = targz(context, file, ignoreSet, context.Args...)
if err != nil {
return err
}
writer.Close()
request.Header.Set("Content-Type", "multipart/form-data; boundary="+writer.Boundary())
fullSize := float64(body.Len())
megabyte := 1024.0 * 1024.0
fmt.Fprintf(context.Stdout, "Uploading files (%0.2fMB)... ", fullSize/megabyte)
count := 0
go func() {
t0 := time.Now()
lastTransferred := 0.0
for buf.Len() == 0 {
remaining := body.Len()
transferred := fullSize - float64(remaining)
speed := ((transferred - lastTransferred) / megabyte) / (float64(time.Since(t0)) / float64(time.Second))
t0 = time.Now()
lastTransferred = transferred
percent := (transferred / fullSize) * 100.0
fmt.Fprintf(safeStdout, "\rUploading files (%0.2fMB)... %0.2f%%", fullSize/megabyte, percent)
if remaining > 0 {
fmt.Fprintf(safeStdout, " (%0.2fMB/s)", speed)
}
if remaining == 0 && buf.Len() == 0 {
fmt.Fprintf(safeStdout, " Processing%s", strings.Repeat(".", count))
count++
}
time.Sleep(2e9)
}
}()
}
resp, err := client.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(&respBody, resp.Body)
if err != nil {
return err
}
if strings.HasSuffix(buf.String(), "\nOK\n") {
return nil
}
return cmd.ErrAbortCommand
}
func processTsuruIgnore(pattern string, paths ...string) (map[string]struct{}, error) {
ignoreSet := make(map[string]struct{})
old, err := os.Getwd()
if err != nil {
return nil, err
}
defer os.Chdir(old)
for _, dirPath := range paths {
fi, err := os.Stat(dirPath)
if err != nil {
return nil, err
}
if !fi.IsDir() {
dirPath = filepath.Dir(dirPath)
}
errCh := os.Chdir(dirPath)
if errCh != nil {
return nil, errCh
}
wd, err := os.Getwd()
if err != nil {
return nil, err
}
matchedPaths, err := filepath.Glob(pattern)
if err != nil {
return nil, err
}
for _, v := range matchedPaths {
if dirPath == "." {
ignoreSet[filepath.Join(wd, v)] = struct{}{}
continue
}
ignoreSet[filepath.Join(dirPath, v)] = struct{}{}
}
dir, err := os.Open(dirPath)
if err != nil {
return nil, err
}
fis, err := dir.Readdir(0)
if err != nil {
return nil, err
}
dir.Close()
for _, f := range fis {
if f.IsDir() {
glob, err := processTsuruIgnore(pattern, filepath.Join(wd, f.Name()))
if err != nil {
return nil, err
}
for k, v := range glob {
ignoreSet[k] = v
}
}
}
}
return ignoreSet, nil
}
func readTsuruIgnore() ([]string, error) {
file, err := os.Open(".tsuruignore")
if err != nil {
return nil, err
}
defer file.Close()
patterns := []string{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
patterns = append(patterns, scanner.Text())
}
return patterns, nil
}
func targz(ctx *cmd.Context, destination io.Writer, ignoreSet map[string]struct{}, filepaths ...string) error {
var buf bytes.Buffer
tarWriter := tar.NewWriter(&buf)
for _, path := range filepaths {
if path == ".." {
fmt.Fprintf(ctx.Stderr, "Warning: skipping %q", path)
continue
}
fi, err := os.Lstat(path)
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}
fiName := filepath.Join(wd, fi.Name())
if _, inSet := ignoreSet[fiName]; inSet {
continue
}
if fi.IsDir() {
if len(filepaths) == 1 && path != "." {
return singleDir(ctx, destination, path, ignoreSet)
}
err = addDir(tarWriter, path, ignoreSet)
} else {
err = addFile(tarWriter, path)
}
if err != nil {
return err
}
}
err := tarWriter.Close()
if err != nil {
return err
}
gzipWriter := gzip.NewWriter(destination)
defer gzipWriter.Close()
_, err = io.Copy(gzipWriter, &buf)
return err
}
func singleDir(ctx *cmd.Context, destination io.Writer, path string, ignoreSet map[string]struct{}) error {
old, err := os.Getwd()
if err != nil {
return err
}
defer os.Chdir(old)
err = os.Chdir(path)
if err != nil {
return err
}
return targz(ctx, destination, ignoreSet, ".")
}
func addDir(writer *tar.Writer, dirpath string, ignoreSet map[string]struct{}) error {
dir, err := os.Open(dirpath)
if err != nil {
return err
}
defer dir.Close()
fi, err := dir.Stat()
if err != nil {
return err
}
header, err := tar.FileInfoHeader(fi, "")
if err != nil {
return err
}
header.Name = dirpath
err = writer.WriteHeader(header)
if err != nil {
return err
}
fis, err := dir.Readdir(0)
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}
for _, fi := range fis {
fiName := filepath.Join(wd, fi.Name())
if dirpath != "." {
fiName = filepath.Join(wd, dirpath, fi.Name())
}
if _, existSet := ignoreSet[fiName]; existSet {
continue
}
if fi.IsDir() {
err = addDir(writer, path.Join(dirpath, fi.Name()), ignoreSet)
} else {
err = addFile(writer, path.Join(dirpath, fi.Name()))
}
if err != nil {
return err
}
}
return nil
}
func addFile(writer *tar.Writer, filepath string) error {
f, err := os.Open(filepath)
if err != nil {
return err
}
defer f.Close()
fi, err := os.Lstat(filepath)
if err != nil {
return err
}
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
var target string
target, err = os.Readlink(filepath)
if err != nil {
return err
}
return addSymlink(writer, filepath, target)
}
header, err := tar.FileInfoHeader(fi, "")
if err != nil {
return err
}
header.Name = filepath
err = writer.WriteHeader(header)
if err != nil {
return err
}
n, err := io.Copy(writer, f)
if err != nil {
return err
}
if n != fi.Size() {
return io.ErrShortWrite
}
return nil
}
func addSymlink(writer *tar.Writer, symlink, target string) error {
fi, err := os.Lstat(symlink)
if err != nil {
return err
}
header, err := tar.FileInfoHeader(fi, "")
if err != nil {
return err
}
header.Name = symlink
header.Linkname = target
return writer.WriteHeader(header)
}
type firstWriter struct {
io.Writer
once sync.Once
}
func (w *firstWriter) Write(p []byte) (int, error) {
w.once.Do(func() {
w.Writer.Write([]byte(" ok\n"))
})
return w.Writer.Write(p)
}
type AppDeployRollback struct {
cmd.GuessingCommand
cmd.ConfirmationCommand
fs *gnuflag.FlagSet
}
func (c *AppDeployRollback) Flags() *gnuflag.FlagSet {
if c.fs == nil {
c.fs = cmd.MergeFlagSet(
c.GuessingCommand.Flags(),
c.ConfirmationCommand.Flags(),
)
}
return c.fs
}
func (c *AppDeployRollback) Info() *cmd.Info {
desc := "Deploys an existing image for an app. You can list available images with `tsuru app-deploy-list`."
return &cmd.Info{
Name: "app-deploy-rollback",
Usage: "app-deploy-rollback [-a/--app appname] [-y/--assume-yes] <image-name>",
Desc: desc,
MinArgs: 1,
MaxArgs: 1,
}
}
func (c *AppDeployRollback) Run(context *cmd.Context, client *cmd.Client) error {
context.RawOutput()
appName, err := c.Guess()
if err != nil {
return err
}
imgName := context.Args[0]
if !c.Confirm(context, fmt.Sprintf("Are you sure you want to rollback app %q to image %q?", appName, imgName)) {
return nil
}
u, err := cmd.GetURL(fmt.Sprintf("/apps/%s/deploy/rollback", appName))
if err != nil {
return err
}
v := url.Values{}
v.Set("origin", "rollback")
v.Set("image", imgName)
request, err := http.NewRequest("POST", u, strings.NewReader(v.Encode()))
if err != nil {
return err
}
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
w := tsuruIo.NewStreamWriter(context.Stdout, nil)
for n := int64(1); n > 0 && err == nil; n, err = io.Copy(w, response.Body) {
}
if err != nil {
return err
}
unparsed := w.Remaining()
if len(unparsed) > 0 {
return fmt.Errorf("unparsed message error: %s", string(unparsed))
}
return nil
}
type AppDeployRebuild struct {
cmd.GuessingCommand
}
func (c *AppDeployRebuild) Info() *cmd.Info {
desc := "Rebuild and deploy the last app image."
return &cmd.Info{
Name: "app-deploy-rebuild",
Usage: "app-deploy-rebuild [-a/--app appname]",
Desc: desc,
MinArgs: 0,
MaxArgs: 0,
}
}
func (c *AppDeployRebuild) Run(context *cmd.Context, client *cmd.Client) error {
context.RawOutput()
appName, err := c.Guess()
if err != nil {
return err
}
u, err := cmd.GetURLVersion("1.3", fmt.Sprintf("/apps/%s/deploy/rebuild", appName))
if err != nil {
return err
}
v := url.Values{}
v.Set("origin", "rebuild")
request, err := http.NewRequest("POST", u, strings.NewReader(v.Encode()))
if err != nil {
return err
}
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
w := tsuruIo.NewStreamWriter(context.Stdout, nil)
for n := int64(1); n > 0 && err == nil; n, err = io.Copy(w, response.Body) {
}
if err != nil {
return err
}
unparsed := w.Remaining()
if len(unparsed) > 0 {
return fmt.Errorf("unparsed message error: %s", string(unparsed))
}
return nil
}
type AppDeployRollbackUpdate struct {
cmd.GuessingCommand
image string
reason string
disable bool
fs *gnuflag.FlagSet
}
func (c *AppDeployRollbackUpdate) Info() *cmd.Info {
desc := `Locks an existing image of an app. You can list images with "tsuru app-deploy-list -a <appName>"
::
The [-i/--image] flag is the name of an app image.
The [-d/--disable] flag disables an image rollback. The default behavior (omitting this flag) is to enable it.
The [-r/--reason] flag lets the user tell why this action was needed.
`
return &cmd.Info{
Name: "app-deploy-rollback-update",
Usage: "app-deploy-rollback-update [-a/--app appName] [-i/--image imageName] [-d/--disable] [-r/--reason reason]",
Desc: desc,
MinArgs: 0,
MaxArgs: 0,
}
}
func (c *AppDeployRollbackUpdate) Flags() *gnuflag.FlagSet {
if c.fs == nil {
c.fs = c.GuessingCommand.Flags()
image := "The image name for an app version"
c.fs.StringVar(&c.image, "image", "", image)
c.fs.StringVar(&c.image, "i", "", image)
reason := "The reason why the rollback has to be disabled"
c.fs.StringVar(&c.reason, "reason", "", reason)
c.fs.StringVar(&c.reason, "r", "", reason)
disable := "Enables or disables the rollback on a specific image version"
c.fs.BoolVar(&c.disable, "disable", false, disable)
c.fs.BoolVar(&c.disable, "d", false, disable)
}
return c.fs
}
func (c *AppDeployRollbackUpdate) Run(context *cmd.Context, client *cmd.Client) error {
appName, err := c.Guess()
if err != nil {
return err
}
u, err := cmd.GetURL(fmt.Sprintf("/apps/%s/deploy/rollback/update", appName))
if err != nil {
return err
}
v := url.Values{}
v.Set("image", c.image)
v.Set("reason", c.reason)
v.Set("origin", "rollback")
v.Set("disable", strconv.FormatBool(c.disable))
request, err := http.NewRequest(http.MethodPut, u, strings.NewReader(v.Encode()))
if err != nil {
return err
}
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
_, err = client.Do(request)
return err
}