-
Notifications
You must be signed in to change notification settings - Fork 5
/
opsmanager.go
228 lines (197 loc) · 7.84 KB
/
opsmanager.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
package cfbackup
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"path"
"github.com/pivotalservices/gtils/command"
ghttp "github.com/pivotalservices/gtils/http"
"github.com/pivotalservices/gtils/log"
"github.com/pivotalservices/gtils/osutils"
)
const (
OPSMGR_INSTALLATION_SETTINGS_FILENAME string = "installation.json"
OPSMGR_INSTALLATION_SETTINGS_POSTFIELD_NAME string = "installation[file]"
OPSMGR_INSTALLATION_ASSETS_FILENAME string = "installation.zip"
OPSMGR_INSTALLATION_ASSETS_POSTFIELD_NAME string = "installation[file]"
OPSMGR_DEPLOYMENTS_FILENAME string = "deployments.tar.gz"
OPSMGR_ENCRYPTIONKEY_FILENAME string = "cc_db_encryption_key.txt"
OPSMGR_BACKUP_DIR string = "opsmanager"
OPSMGR_DEPLOYMENTS_DIR string = "deployments"
OPSMGR_DEFAULT_USER string = "tempest"
OPSMGR_INSTALLATION_SETTINGS_URL string = "https://%s/api/installation_settings"
OPSMGR_INSTALLATION_ASSETS_URL string = "https://%s/api/installation_asset_collection"
OPSMGR_DEPLOYMENTS_FILE string = "/var/tempest/workspaces/default/deployments/bosh-deployments.yml"
)
type httpUploader func(conn ghttp.ConnAuth, paramName, filename string, fileRef io.Reader, params map[string]string) (res *http.Response, err error)
type httpRequestor interface {
Get(ghttp.HttpRequestEntity) ghttp.RequestAdaptor
Post(ghttp.HttpRequestEntity, io.Reader) ghttp.RequestAdaptor
Put(ghttp.HttpRequestEntity, io.Reader) ghttp.RequestAdaptor
}
// OpsManager contains the location and credentials of a Pivotal Ops Manager instance
type OpsManager struct {
BackupContext
Hostname string
Username string
Password string
TempestPassword string
DbEncryptionKey string
Executer command.Executer
LocalExecuter command.Executer
SettingsUploader httpUploader
AssetsUploader httpUploader
SettingsRequestor httpRequestor
AssetsRequestor httpRequestor
DeploymentDir string
OpsmanagerBackupDir string
Logger log.Logger
}
// NewOpsManager initializes an OpsManager instance
var NewOpsManager = func(hostname string, username string, password string, tempestpassword string, target string, logger log.Logger) (context *OpsManager, err error) {
var remoteExecuter command.Executer
if remoteExecuter, err = createExecuter(hostname, tempestpassword); err == nil {
settingsHttpRequestor := ghttp.NewHttpGateway()
settingsMultiHttpRequestor := ghttp.MultiPartUpload
assetsHttpRequestor := ghttp.NewHttpGateway()
assetsMultiHttpRequestor := ghttp.MultiPartUpload
context = &OpsManager{
SettingsUploader: settingsMultiHttpRequestor,
AssetsUploader: assetsMultiHttpRequestor,
SettingsRequestor: settingsHttpRequestor,
AssetsRequestor: assetsHttpRequestor,
DeploymentDir: path.Join(target, OPSMGR_BACKUP_DIR, OPSMGR_DEPLOYMENTS_DIR),
Hostname: hostname,
Username: username,
Password: password,
BackupContext: BackupContext{
TargetDir: target,
},
Executer: remoteExecuter,
LocalExecuter: command.NewLocalExecuter(),
OpsmanagerBackupDir: OPSMGR_BACKUP_DIR,
Logger: logger,
}
}
return
}
// Backup performs a backup of a Pivotal Ops Manager instance
func (context *OpsManager) Backup() (err error) {
if err = context.copyDeployments(); err == nil {
err = context.exportAndExtract()
}
return
}
// Restore performs a restore of a Pivotal Ops Manager instance
func (context *OpsManager) Restore() (err error) {
err = context.importInstallation()
return
}
func (context *OpsManager) importInstallation() (err error) {
defer func() {
if err == nil {
err = context.removeExistingDeploymentFiles()
}
}()
installSettingsUrl := fmt.Sprintf(OPSMGR_INSTALLATION_SETTINGS_URL, context.Hostname)
if err = context.importInstallationPart(installSettingsUrl, OPSMGR_INSTALLATION_SETTINGS_FILENAME, OPSMGR_INSTALLATION_SETTINGS_POSTFIELD_NAME, context.SettingsUploader); err == nil {
installAssetsUrl := fmt.Sprintf(OPSMGR_INSTALLATION_ASSETS_URL, context.Hostname)
err = context.importInstallationPart(installAssetsUrl, OPSMGR_INSTALLATION_ASSETS_FILENAME, OPSMGR_INSTALLATION_ASSETS_POSTFIELD_NAME, context.AssetsUploader)
}
return
}
func (context *OpsManager) removeExistingDeploymentFiles() (err error) {
var w bytes.Buffer
command := fmt.Sprintf("if [ -f %s ]; then sudo rm %s;fi", OPSMGR_DEPLOYMENTS_FILE, OPSMGR_DEPLOYMENTS_FILE)
err = context.Executer.Execute(&w, command)
return
}
func (context *OpsManager) importInstallationPart(url, filename, fieldname string, upload httpUploader) (err error) {
var (
fileRef io.Reader
)
filePath := path.Join(context.TargetDir, context.OpsmanagerBackupDir, filename)
if fileRef, err = os.Open(filePath); err == nil {
var res *http.Response
conn := ghttp.ConnAuth{
Url: url,
Username: context.Username,
Password: context.Password,
}
if res, err = upload(conn, fieldname, filename, fileRef, nil); err != nil {
err = fmt.Errorf(fmt.Sprintf("ERROR:%s - %v", err.Error(), res))
context.Logger.Debug("upload failed", log.Data{"err": err, "response": res})
}
}
return
}
func (context *OpsManager) exportAndExtract() (err error) {
if err = context.extract(); err == nil {
err = context.export()
}
return
}
func (context *OpsManager) export() (err error) {
if err = context.exportUrlToFile(OPSMGR_INSTALLATION_SETTINGS_URL, OPSMGR_INSTALLATION_SETTINGS_FILENAME); err == nil {
err = context.exportUrlToFile(OPSMGR_INSTALLATION_ASSETS_URL, OPSMGR_INSTALLATION_ASSETS_FILENAME)
}
return
}
func (context *OpsManager) exportUrlToFile(urlFormat string, filename string) (err error) {
var settingsFileRef *os.File
defer settingsFileRef.Close()
url := fmt.Sprintf(urlFormat, context.Hostname)
context.Logger.Debug("Exporting url '%s' to file '%s'", log.Data{"url": url, "filename": filename})
if settingsFileRef, err = osutils.SafeCreate(context.TargetDir, context.OpsmanagerBackupDir, filename); err == nil {
err = context.exportUrlToWriter(url, settingsFileRef, context.SettingsRequestor)
}
return
}
func (context *OpsManager) exportUrlToWriter(url string, dest io.Writer, requestor httpRequestor) (err error) {
resp, err := requestor.Get(ghttp.HttpRequestEntity{
Url: url,
Username: context.Username,
Password: context.Password,
ContentType: "application/octet-stream",
})()
if err == nil {
defer resp.Body.Close()
_, err = io.Copy(dest, resp.Body)
}
return
}
func (context *OpsManager) extract() (err error) {
var keyFileRef *os.File
defer keyFileRef.Close()
context.Logger.Debug("Extracting Ops Manager")
if keyFileRef, err = osutils.SafeCreate(context.TargetDir, context.OpsmanagerBackupDir, OPSMGR_ENCRYPTIONKEY_FILENAME); err == nil {
context.Logger.Debug("Extracting encryption key")
backupDir := path.Join(context.TargetDir, context.OpsmanagerBackupDir)
deployment := path.Join(backupDir, OPSMGR_DEPLOYMENTS_FILENAME)
cmd := "tar -xf " + deployment + " -C " + backupDir
context.Logger.Debug("Extracting : %s", log.Data{"command": cmd})
context.LocalExecuter.Execute(nil, cmd)
err = ExtractEncryptionKey(keyFileRef, context.DeploymentDir)
}
return
}
func (context *OpsManager) copyDeployments() (err error) {
var file *os.File
defer file.Close()
if file, err = osutils.SafeCreate(context.TargetDir, context.OpsmanagerBackupDir, OPSMGR_DEPLOYMENTS_FILENAME); err == nil {
command := "cd /var/tempest/workspaces/default && tar cz deployments"
err = context.Executer.Execute(file, command)
}
return
}
func createExecuter(hostname, tempestpassword string) (remoteExecuter command.Executer, err error) {
remoteExecuter, err = command.NewRemoteExecutor(command.SshConfig{
Username: OPSMGR_DEFAULT_USER,
Password: tempestpassword,
Host: hostname,
Port: 22,
})
return
}