-
Notifications
You must be signed in to change notification settings - Fork 5
/
elasticruntime.go
355 lines (315 loc) · 9.48 KB
/
elasticruntime.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
package cfbackup
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path"
. "github.com/pivotalservices/gtils/http"
"github.com/pivotalservices/gtils/log"
"github.com/pivotalservices/gtils/osutils"
)
const (
ER_DEFAULT_SYSTEM_USER = "vcap"
ER_DIRECTOR_INFO_URL = "https://%s:25555/info"
ER_BACKUP_DIR = "elasticruntime"
ER_VMS_URL = "https://%s:25555/deployments/%s/vms"
ER_DIRECTOR = "DirectorInfo"
ER_CONSOLE = "ConsoledbInfo"
ER_UAA = "UaadbInfo"
ER_CC = "CcdbInfo"
ER_MYSQL = "MysqldbInfo"
ER_NFS = "NfsInfo"
ER_BACKUP_FILE_FORMAT = "%s.backup"
ER_INVALID_DIRECTOR_CREDS_MSG = "invalid director credentials"
ER_NO_PERSISTENCE_ARCHIVES = "there are no persistence stores in the list"
ER_FILE_DOES_NOT_EXIST = "file does not exist"
ER_DB_BACKUP_FAILURE = "failed to backup database"
)
const (
IMPORT_ARCHIVE = iota
EXPORT_ARCHIVE
)
var (
ER_ERROR_DIRECTOR_CREDS = errors.New(ER_INVALID_DIRECTOR_CREDS_MSG)
ER_ERROR_EMPTY_DB_LIST = errors.New(ER_NO_PERSISTENCE_ARCHIVES)
ER_ERROR_INVALID_PATH = &os.PathError{Err: errors.New(ER_FILE_DOES_NOT_EXIST)}
ER_DB_BACKUP = errors.New(ER_DB_BACKUP_FAILURE)
)
// ElasticRuntime contains information about a Pivotal Elastic Runtime deployment
type ElasticRuntime struct {
JsonFile string
SystemsInfo map[string]SystemDump
PersistentSystems []SystemDump
HttpGateway HttpGateway
InstallationName string
BackupContext
Logger log.Logger
}
type CCJob struct {
Job string
Index int
}
// NewElasticRuntime initializes an ElasticRuntime intance
var NewElasticRuntime = func(jsonFile string, target string, logger log.Logger) *ElasticRuntime {
var (
uaadbInfo *PgInfo = &PgInfo{
SystemInfo: SystemInfo{
Product: "cf",
Component: "uaadb",
Identity: "root",
},
Database: "uaa",
}
consoledbInfo *PgInfo = &PgInfo{
SystemInfo: SystemInfo{
Product: "cf",
Component: "consoledb",
Identity: "root",
},
Database: "console",
}
ccdbInfo *PgInfo = &PgInfo{
SystemInfo: SystemInfo{
Product: "cf",
Component: "ccdb",
Identity: "admin",
},
Database: "ccdb",
}
mysqldbInfo *MysqlInfo = &MysqlInfo{
SystemInfo: SystemInfo{
Product: "cf",
Component: "mysql",
Identity: "root",
},
Database: "mysql",
}
directorInfo *SystemInfo = &SystemInfo{
Product: "microbosh",
Component: "director",
Identity: "director",
}
nfsInfo *NfsInfo = &NfsInfo{
SystemInfo: SystemInfo{
Product: "cf",
Component: "nfs_server",
Identity: "vcap",
},
}
)
context := &ElasticRuntime{
JsonFile: jsonFile,
BackupContext: BackupContext{
TargetDir: target,
},
SystemsInfo: map[string]SystemDump{
ER_DIRECTOR: directorInfo,
ER_CONSOLE: consoledbInfo,
ER_UAA: uaadbInfo,
ER_CC: ccdbInfo,
ER_MYSQL: mysqldbInfo,
ER_NFS: nfsInfo,
},
PersistentSystems: []SystemDump{
consoledbInfo,
uaadbInfo,
ccdbInfo,
nfsInfo,
mysqldbInfo,
},
Logger: logger,
}
return context
}
// Backup performs a backup of a Pivotal Elastic Runtime deployment
func (context *ElasticRuntime) Backup() (err error) {
return context.backupRestore(EXPORT_ARCHIVE)
}
// Restore performs a restore of a Pivotal Elastic Runtime deployment
func (context *ElasticRuntime) Restore() (err error) {
err = context.backupRestore(IMPORT_ARCHIVE)
return
}
func (context *ElasticRuntime) backupRestore(action int) (err error) {
var (
ccJobs []CCJob
)
if err = context.ReadAllUserCredentials(); err == nil && context.directorCredentialsValid() {
context.Logger.Debug("Retrieving All CC VMs")
manifest, erro := context.getManifest()
if err != nil {
return erro
}
if ccJobs, err = context.getAllCloudControllerVMs(); err == nil {
directorInfo := context.SystemsInfo[ER_DIRECTOR]
cloudController := NewCloudController(directorInfo.Get(SD_IP), directorInfo.Get(SD_USER), directorInfo.Get(SD_PASS), context.InstallationName, manifest, ccJobs)
context.Logger.Debug("Setting up CC jobs")
defer cloudController.Start()
cloudController.Stop()
}
context.Logger.Debug("Running db action")
if len(context.PersistentSystems) > 0 {
err = context.RunDbAction(context.PersistentSystems, action)
if err != nil {
context.Logger.Error("Error backing up db", err)
err = ER_DB_BACKUP
}
} else {
err = ER_ERROR_EMPTY_DB_LIST
}
} else if err == nil {
err = ER_ERROR_DIRECTOR_CREDS
}
return
}
func (context *ElasticRuntime) getAllCloudControllerVMs() (ccvms []CCJob, err error) {
context.Logger.Debug("Entering getAllCloudControllerVMs() function")
directorInfo := context.SystemsInfo[ER_DIRECTOR]
connectionURL := fmt.Sprintf(ER_VMS_URL, directorInfo.Get(SD_IP), context.InstallationName)
context.Logger.Debug("getAllCloudControllerVMs() function", log.Data{"connectionURL": connectionURL, "directorInfo": directorInfo})
gateway := context.HttpGateway
if gateway == nil {
gateway = NewHttpGateway()
}
context.Logger.Debug("Retrieving CC vms")
if resp, err := gateway.Get(HttpRequestEntity{
Url: connectionURL,
Username: directorInfo.Get(SD_USER),
Password: directorInfo.Get(SD_PASS),
ContentType: "application/json",
})(); err == nil {
var jsonObj []VMObject
context.Logger.Debug("Unmarshalling CC vms")
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err = json.Unmarshal(body, &jsonObj); err == nil {
ccvms, err = GetCCVMs(jsonObj)
}
}
return
}
func (context *ElasticRuntime) RunDbAction(dbInfoList []SystemDump, action int) (err error) {
for _, info := range dbInfoList {
if err = info.Error(); err == nil {
err = context.readWriterArchive(info, context.TargetDir, action)
context.Logger.Debug("backed up db", log.Data{"info": info})
} else {
break
}
}
return
}
func (context *ElasticRuntime) getReadWriter(fpath string, action int) (rw io.ReadWriter, err error) {
switch action {
case IMPORT_ARCHIVE:
var exists bool
if exists, err = osutils.Exists(fpath); exists && err == nil {
rw, err = os.Open(fpath)
} else {
var pathError os.PathError
pathError = *ER_ERROR_INVALID_PATH
pathError.Path = fpath
err = &pathError
}
case EXPORT_ARCHIVE:
rw, err = osutils.SafeCreate(fpath)
}
return
}
func (context *ElasticRuntime) readWriterArchive(dbInfo SystemDump, databaseDir string, action int) (err error) {
var (
archivefile io.ReadWriter
)
filename := fmt.Sprintf(ER_BACKUP_FILE_FORMAT, dbInfo.Get(SD_COMPONENT))
filepath := path.Join(databaseDir, filename)
if archivefile, err = context.getReadWriter(filepath, action); err == nil {
err = context.importExport(archivefile, dbInfo, action)
}
return
}
func (context *ElasticRuntime) importExport(rw io.ReadWriter, s SystemDump, action int) (err error) {
var pb PersistanceBackup
if pb, err = s.GetPersistanceBackup(); err == nil {
switch action {
case IMPORT_ARCHIVE:
context.Logger.Debug("we are doing something here now")
err = pb.Import(rw)
case EXPORT_ARCHIVE:
context.Logger.Info("Dumping database to file")
err = pb.Dump(rw)
}
}
return
}
func (context *ElasticRuntime) ReadAllUserCredentials() (err error) {
var (
fileRef *os.File
jsonObj InstallationCompareObject
)
defer fileRef.Close()
if fileRef, err = os.Open(context.JsonFile); err == nil {
if jsonObj, err = ReadAndUnmarshal(fileRef); err == nil {
err = context.assignCredentialsAndInstallationName(jsonObj)
}
}
return
}
func (context *ElasticRuntime) assignCredentialsAndInstallationName(jsonObj InstallationCompareObject) (err error) {
if err = context.assignCredentials(jsonObj); err == nil {
context.InstallationName, err = GetDeploymentName(jsonObj)
}
return
}
func (context *ElasticRuntime) assignCredentials(jsonObj InstallationCompareObject) (err error) {
for name, sysInfo := range context.SystemsInfo {
var (
ip string
pass string
vpass string
)
sysInfo.Set(SD_VCAPUSER, ER_DEFAULT_SYSTEM_USER)
sysInfo.Set(SD_USER, sysInfo.Get(SD_IDENTITY))
if ip, pass, err = GetPasswordAndIP(jsonObj, sysInfo.Get(SD_PRODUCT), sysInfo.Get(SD_COMPONENT), sysInfo.Get(SD_IDENTITY)); err == nil {
sysInfo.Set(SD_IP, ip)
sysInfo.Set(SD_PASS, pass)
_, vpass, err = GetPasswordAndIP(jsonObj, sysInfo.Get(SD_PRODUCT), sysInfo.Get(SD_COMPONENT), sysInfo.Get(SD_VCAPUSER))
sysInfo.Set(SD_VCAPPASS, vpass)
context.SystemsInfo[name] = sysInfo
}
}
return
}
func (context *ElasticRuntime) directorCredentialsValid() (ok bool) {
var directorInfo SystemDump
if directorInfo, ok = context.SystemsInfo[ER_DIRECTOR]; ok {
connectionURL := fmt.Sprintf(ER_DIRECTOR_INFO_URL, directorInfo.Get(SD_IP))
gateway := context.HttpGateway
if gateway == nil {
gateway = NewHttpGateway()
}
_, err := gateway.Get(HttpRequestEntity{
Url: connectionURL,
Username: directorInfo.Get(SD_USER),
Password: directorInfo.Get(SD_PASS),
ContentType: "application/json",
})()
ok = (err == nil)
}
return
}
func (context *ElasticRuntime) getManifest() (manifest string, err error) {
directorInfo, _ := context.SystemsInfo[ER_DIRECTOR]
director := NewDirector(directorInfo.Get(SD_IP), directorInfo.Get(SD_USER), directorInfo.Get(SD_PASS), 25555)
mfs, err := director.GetDeploymentManifest(context.InstallationName)
if err != nil {
return
}
data, err := ioutil.ReadAll(mfs)
if err != nil {
return
}
return string(data), nil
}