-
Notifications
You must be signed in to change notification settings - Fork 18
/
rollback.go
104 lines (95 loc) · 2.73 KB
/
rollback.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
package main
import (
"fmt"
"log"
"os"
"sort"
"strings"
)
func lsRollbackVersions(servers []*Server, verbose bool) {
for _, s := range servers {
log.Println("# ====================================")
log.Println("#", s.String())
s.initPathes()
releases := s.retrieveAllReleases()
for _, r := range releases {
log.Println(r)
if !verbose {
continue
}
session := s.getSession()
output, err := session.CombinedOutput(fmt.Sprintf("cat %s/harp/%s/releases/%s/harp-build.info", s.Home, cfg.App.Name, r))
if err != nil {
log.Printf("echo $HOME on %s error: %s\n%s\n", s, err, output)
os.Exit(1)
}
session.Close()
info := strings.Replace(string(output), "\n", "\n\t", -1)
log.Println("\t" + info[:len(info)-2])
}
}
}
func rollback(servers []*Server, version string) {
for _, s := range servers {
log.Printf("%s rollback start\n", s.String())
s.initPathes()
session := s.getSession()
if option.debug {
// log.Println(s.retrieveRollbackScript())
cmd := fmt.Sprintf("cat %s/harp/%s/rollback.sh", s.Home, cfg.App.Name)
output, err := session.CombinedOutput(cmd)
if err != nil {
log.Printf("failed to run %s\n", cmd)
os.Exit(1)
}
fmt.Println(string(output))
}
// TODO: should return error when release does not exist
output, err := session.CombinedOutput(fmt.Sprintf("harp_composer=%s %s/harp/%s/rollback.sh %s", retrieveAuthor(), s.Home, cfg.App.Name, version))
if err != nil {
log.Printf("rollback on %s error: %s\n%s\n", s, err, output)
os.Exit(1)
}
session.Close()
if strings.TrimSpace(string(output)) != "" {
log.Print(string(output))
}
log.Printf("%s rollback done\n", s.String())
}
}
func (s *Server) trimOldReleases() {
s.initPathes()
releases := s.retrieveAllReleases()
if len(releases) <= cfg.RollbackCount {
return
}
for _, release := range releases[:len(releases)-cfg.RollbackCount] {
session := s.getSession()
script := fmt.Sprintf("rm -rf %s/harp/%s/releases/%s", s.Home, cfg.App.Name, release)
if option.debug {
log.Printf("%s: %s\n", s, script)
}
output, err := session.CombinedOutput(script)
if err != nil {
exitf("failed to exec %s: %s %s", script, output, err)
}
}
}
func (s *Server) retrieveAllReleases() []string {
s.initPathes()
session := s.getSession()
rawReleases, err := session.CombinedOutput(fmt.Sprintf(`ls -1 %s/harp/%s/releases`, s.Home, cfg.App.Name))
if err != nil {
exitf("failed to exec ls -l: %s %s", rawReleases, err)
}
releases := strings.Split(string(rawReleases), "\n")
var newReleases []string
for i := range releases {
if r := strings.TrimSpace(releases[i]); r != "" {
newReleases = append(newReleases, r)
}
}
releases = newReleases
sort.Sort(sort.StringSlice(releases))
return releases
}