-
Notifications
You must be signed in to change notification settings - Fork 19
/
backup.go
215 lines (179 loc) · 3.93 KB
/
backup.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/codegangsta/cli"
gconfig "github.com/litl/galaxy/config"
"github.com/litl/galaxy/log"
"github.com/litl/galaxy/utils"
)
type backupData struct {
Time time.Time
Apps []*appCfg
}
// Serialized backup format
type appCfg struct {
Name string
Version string
Env map[string]string
Ports map[string]string
}
// Backup app config to a file or STDOUT
func appBackup(c *cli.Context) {
initStore(c)
env := utils.GalaxyEnv(c)
if env == "" {
log.Fatal("ERROR: env is required. Pass --env or set GALAXY_ENV")
}
backup := &backupData{
Time: time.Now(),
}
toBackup := c.Args()
if len(toBackup) == 0 {
appList, err := configStore.ListApps(env)
if err != nil {
log.Fatalf("ERROR: %s\n", err)
}
for _, app := range appList {
toBackup = append(toBackup, app.Name())
}
}
errCount := 0
for _, app := range toBackup {
data, err := getAppBackup(app, env)
if err != nil {
// log errors and continue
log.Errorf("ERROR: %s [%s]", err, app)
errCount++
continue
}
backup.Apps = append(backup.Apps, data)
}
if errCount > 0 {
fmt.Printf("WARNING: backup completed with %d errors\n", errCount)
defer os.Exit(errCount)
}
j, err := json.MarshalIndent(backup, "", " ")
if err != nil {
log.Fatal(err)
}
fileName := c.String("file")
if fileName != "" {
if err := ioutil.WriteFile(fileName, j, 0666); err != nil {
log.Fatal(err)
}
return
}
os.Stdout.Write(j)
}
func getAppBackup(app, env string) (*appCfg, error) {
svcCfg, err := configStore.GetApp(app, env)
if err != nil {
return nil, err
}
if svcCfg == nil {
return nil, fmt.Errorf("app not found")
}
backup := &appCfg{
Name: app,
Version: svcCfg.Version(),
Env: svcCfg.Env(),
}
return backup, nil
}
// restore an app's config from backup
func appRestore(c *cli.Context) {
initStore(c)
var err error
var rawBackup []byte
fileName := c.String("file")
if fileName != "" {
rawBackup, err = ioutil.ReadFile(fileName)
if err != nil {
log.Fatal(err)
}
} else {
log.Println("Reading backup from STDIN")
rawBackup, err = ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
}
backup := &backupData{}
if err := json.Unmarshal(rawBackup, backup); err != nil {
log.Fatal(err)
}
fmt.Println("Found backup from ", backup.Time)
var toRestore []*appCfg
if apps := c.Args(); len(apps) > 0 {
for _, app := range apps {
found := false
for _, bkup := range backup.Apps {
if bkup.Name == app {
toRestore = append(toRestore, bkup)
found = true
break
}
}
if !found {
log.Fatalf("no backup found for '%s'\n", app)
}
}
} else {
toRestore = backup.Apps
}
// check for conflicts
// NOTE: there is still a race here if an app is created after this check
if !c.Bool("force") {
needForce := false
for _, bkup := range toRestore {
exists, err := configStore.AppExists(bkup.Name, utils.GalaxyEnv(c))
if err != nil {
log.Fatal(err)
}
if exists {
log.Warnf("Cannot restore over existing app '%s'", bkup.Name)
needForce = true
}
}
if needForce {
log.Fatal("Use -force to overwrite")
}
}
loggedErr := false
for _, bkup := range toRestore {
if err := restoreApp(bkup, utils.GalaxyEnv(c)); err != nil {
log.Errorf("%s", err)
loggedErr = true
}
}
if loggedErr {
// This is mostly to give a non-zero exit status
log.Fatal("Error occured during restore")
}
}
func restoreApp(bkup *appCfg, env string) error {
fmt.Println("restoring", bkup.Name)
var svcCfg gconfig.App
exists, err := configStore.AppExists(bkup.Name, env)
if err != nil {
return err
}
if exists {
svcCfg, err = configStore.GetApp(bkup.Name, env)
if err != nil {
return err
}
}
if svcCfg == nil {
svcCfg = configStore.NewAppConfig(bkup.Name, bkup.Version)
}
for k, v := range bkup.Env {
svcCfg.EnvSet(k, v)
}
_, err = configStore.UpdateApp(svcCfg, env)
return err
}