-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmd_export_swift.go
253 lines (211 loc) · 7.47 KB
/
cmd_export_swift.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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/jtopjian/limbo/lib"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
// cmdExportSwift defines a cli command to export an LXD resource to Swift.
var cmdExportSwift = cli.Command{
Name: "swift",
Usage: "Swift Driver",
Action: actionExportSwift,
Category: "export",
}
func init() {
cmdExportSwift.Flags = append(cmdExportSwift.Flags, lxdFlags...)
cmdExportSwift.Flags = append(cmdExportSwift.Flags, swiftFlags...)
cmdExportSwift.Flags = append(cmdExportSwift.Flags, openStackFlags...)
cmdExportSwift.Flags = append(cmdExportSwift.Flags, cryptFlags...)
}
// actionExportSwift implements the actions to export an LXD resource
// and upload it to Swift.
func actionExportSwift(ctx *cli.Context) error {
log := logrus.New()
if ctx.GlobalBool("debug") {
log.Level = logrus.DebugLevel
}
// A name is required.
lxdContainerName := ctx.String("name")
if lxdContainerName == "" {
return fmt.Errorf("must specify --name")
}
log.Debugf("Source name is: %s", lxdContainerName)
// A storage container name is required.
storageContainerName := ctx.String("storage-container")
if storageContainerName == "" {
return fmt.Errorf("must specify --storage-container")
}
log.Debugf("Storage container name is: %s", storageContainerName)
// Set some other variables.
lxdResourceType := ctx.String("type")
log.Debugf("LXD resource type is: %s", lxdResourceType)
localTmpDir := ctx.String("tmpdir")
log.Debugf("Local tmpdir is: %s", localTmpDir)
lxdConfigDirectory := ctx.String("lxd-config-directory")
log.Debugf("LXD config directory is: %s", lxdConfigDirectory)
stopLXDContainer := ctx.Bool("stop")
log.Debugf("Stop container if it's running: %t", stopLXDContainer)
createStorageContainer := ctx.Bool("create-storage-container")
log.Debugf("Create storage container if it doesn't exist: %t", createStorageContainer)
archive := ctx.Bool("archive")
log.Debugf("Images will be archived: %t", archive)
// Because the exported image might be large, save it locally temporarily
// instead of in memory.
log.Debugf("Creating tmpdir %s", localTmpDir)
tmpDir, err := ioutil.TempDir(localTmpDir, "limbo")
if err != nil {
return fmt.Errorf("Unable to create temp directory: %s", err)
}
defer os.RemoveAll(tmpDir)
// Create an LXD client.
lxdConfig, err := newLXDConfig(lxdConfigDirectory)
if err != nil {
return fmt.Errorf("Unable to get LXD configuration: %s", err)
}
// Determine the LXD remote name and resource name.
remote, ctName, err := lxdConfig.Config.ParseRemote(lxdContainerName)
if err != nil {
return fmt.Errorf("Unable to parse LXD remote: %s", err)
}
lxdConfig.RemoteName = remote
log.Debugf("LXD Remote: %s", remote)
log.Debugf("LXD Container name: %s", ctName)
// Create a connection to the LXD container server API.
log.Debugf("Creating connection to LXD container server %s", remote)
lxdServer, err := lxdConfig.GetContainerServer()
if err != nil {
return fmt.Errorf("Unable to connect to LXD Server: %s", err)
}
// Get a Swift client.
log.Debug("Creating swift client")
swiftClient, err := newSwiftClient(ctx)
if err != nil {
return fmt.Errorf("Unable to create swift client: %s", err)
}
// See if the destination storage container exists.
// Configure the container to archive, if requested.
log.Debug("Configuring Swift container")
err = lib.SwiftCreateContainer(swiftClient, storageContainerName, createStorageContainer, archive)
if err != nil {
return err
}
var lxdFingerprint string
if lxdResourceType == "container" {
// First publish the LXD container.
// This converts a container to an image.
publishOpts := lib.LXDPublishOpts{
Name: ctName,
Stop: stopLXDContainer,
CompressionAlgorithm: ctx.String("compression"),
}
if len(ctx.StringSlice("property")) > 0 {
properties := map[string]string{}
for _, v := range ctx.StringSlice("property") {
if strings.Contains(v, "=") {
v := strings.Split(v, "=")
properties[v[0]] = v[1]
}
}
publishOpts.Properties = &properties
}
log.Debugf("LXD PublishOpts: %#v", publishOpts)
log.Infof("Creating an image from %s", ctName)
publishResult, err := lib.LXDPublishImage(lxdConfig, publishOpts)
if err != nil {
return fmt.Errorf("Unable to publish image: %s", err)
}
log.Debugf("LXD PublishResult: %#v", publishResult)
lxdFingerprint = publishResult.Fingerprint
// Delete the newly created image when the job is done.
defer func() {
op, err := lxdServer.DeleteImage(publishResult.Fingerprint)
if err != nil {
panic(err)
}
if err := op.Wait(); err != nil {
panic(err)
}
}()
}
// If lxdFingerprint doesn't have a value, that means an existing container
// wasn't exported and we might be dealing with an image. Try to get the
// fingerprint of the image.
if lxdFingerprint == "" {
lxdImage, err := lxdConfig.GetImageServer()
if err != nil {
return fmt.Errorf("Unable to connect to LXD image API: %s", err)
}
result, _, err := lxdImage.GetImageAlias(ctName)
if result == nil {
lxdFingerprint = ctName
}
lxdFingerprint = result.Target
}
// Download the image locally.
downloadOpts := lib.LXDDownloadOpts{
Name: ctName,
TmpDir: tmpDir,
Fingerprint: lxdFingerprint,
}
log.Debugf("LXD downloadOpts: %#v", downloadOpts)
log.Infof("Downloading image of %s", ctName)
downloadResult, err := lib.LXDDownloadImage(lxdConfig, downloadOpts)
if err != nil {
return fmt.Errorf("Unable to download lxd image: %s", err)
}
log.Debugf("LXD downloadResult: %#v", downloadResult)
// Encrypt the file, if requested.
if ctx.Bool("encrypt") {
log.Infof("Encrypting %s", downloadResult.MetaFilename)
err = lib.Encrypt(downloadResult.MetaFilename, ctx.String("pass"))
if err != nil {
return fmt.Errorf("Unable to encrypt %s: %s", downloadResult.MetaFilename, err)
}
}
// Upload the image to Swift.
objectName := ctx.String("object-name")
if objectName == "" {
objectName = ctName
}
// First upload the meta file.
uploadOpts := lib.SwiftUploadOpts{
ObjectName: objectName,
SourceName: downloadResult.MetaFilename,
StorageContainer: storageContainerName,
}
log.Debugf("Swift uploadOpts: %#v", uploadOpts)
log.Infof("Uploading %s to Swift container %s as %s",
ctName, uploadOpts.StorageContainer, uploadOpts.ObjectName)
uploadResult, err := lib.SwiftUploadObject(swiftClient, uploadOpts)
if err != nil {
return fmt.Errorf("Unable to upload meta file to swift: %s", err)
}
log.Debugf("Upload result headers: %#v", uploadResult.Headers)
// Then upload the rootfs file, if it exists.
if downloadResult.RootfsFilename != "" {
// Encrypt the file, if requested.
if ctx.Bool("encrypt") {
log.Infof("Encrypting %s", downloadResult.RootfsFilename)
err = lib.Encrypt(downloadResult.RootfsFilename, ctx.String("pass"))
if err != nil {
return fmt.Errorf("Unable to encrypt %s: %s", downloadResult.RootfsFilename, err)
}
}
uploadOpts.SourceName = downloadResult.RootfsFilename
uploadOpts.ObjectName = objectName + ".root"
log.Debugf("Swift uploadOpts: %#v", uploadOpts)
log.Infof("Uploading %s rootfs to Swift container %s as %s",
ctName, uploadOpts.StorageContainer, uploadOpts.ObjectName)
uploadResult, err = lib.SwiftUploadObject(swiftClient, uploadOpts)
if err != nil {
return fmt.Errorf("Unable to upload meta file to swift: %s", err)
}
log.Debugf("Upload result headers: %#v", uploadResult.Headers)
}
log.Infof("Successfully exported %s", ctName)
return nil
}