-
Notifications
You must be signed in to change notification settings - Fork 21
/
aws_importer.go
524 lines (453 loc) · 16.4 KB
/
aws_importer.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// Copyright 2020 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package importer
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"cloud.google.com/go/storage"
daisy "github.com/GoogleCloudPlatform/compute-daisy"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/dustin/go-humanize"
"google.golang.org/api/option"
htransport "google.golang.org/api/transport/http"
"github.com/GoogleCloudPlatform/compute-image-import/cli_tools/common/domain"
"github.com/GoogleCloudPlatform/compute-image-import/cli_tools/common/utils/compute"
"github.com/GoogleCloudPlatform/compute-image-import/cli_tools/common/utils/logging"
"github.com/GoogleCloudPlatform/compute-image-import/cli_tools/common/utils/param"
pathutils "github.com/GoogleCloudPlatform/compute-image-import/cli_tools/common/utils/path"
storageutils "github.com/GoogleCloudPlatform/compute-image-import/cli_tools/common/utils/storage"
)
const (
downloadBufSize = "100MB"
downloadBufNum = 3
uploadBufSize = "200MB"
logPrefix = "[onestep-import-image-aws]"
)
// awsImporter is responsible for importing image from AWS.
type awsImporter struct {
args *awsImportArguments
gcsClient domain.StorageClientInterface
ctx context.Context
oauth string
paramPopulator param.Populator
timeoutChan chan struct{}
uploader *uploader
// AWS clients for SDK
ec2Client ec2iface.EC2API
s3Client s3iface.S3API
// Impl of the functions
exportAWSImageFn func() error
monitorAWSExportImageTaskFn func() error
getAWSFileSizeFn func() error
copyFromS3ToGCSFn func() (string, error)
transferFileFn func() error
getUploaderFn func() *uploader
importImageFn func() error
cleanUpFn func()
}
// newAWSImporter creates an new awsImporter instance.
// Automatically populating dependencies, such as compute/storage clients.
func newAWSImporter(oauth string, timeoutChan chan struct{}, args *awsImportArguments) (*awsImporter, error) {
ctx := context.Background()
client, err := createGCSClient(ctx, oauth)
if err != nil {
return nil, err
}
computeClient, err := param.CreateComputeClient(&ctx, oauth, args.gcsComputeEndpoint)
if err != nil {
return nil, err
}
metadataGCE := &compute.MetadataGCE{}
paramPopulator := param.NewPopulator(
param.NewNetworkResolver(computeClient),
metadataGCE,
client,
storageutils.NewResourceLocationRetriever(metadataGCE, computeClient),
storageutils.NewScratchBucketCreator(ctx, client),
param.NewMachineSeriesDetector(computeClient),
)
awsSession, err := createAWSSession(args.region, args.accessKeyID, args.secretAccessKey, args.sessionToken)
if err != nil {
return nil, err
}
importer := &awsImporter{
args: args,
gcsClient: client,
s3Client: s3.New(awsSession),
ec2Client: ec2.New(awsSession),
ctx: ctx,
oauth: oauth,
paramPopulator: paramPopulator,
timeoutChan: timeoutChan,
}
return importer, nil
}
// createGCSClient creates a new GCS client.
func createGCSClient(ctx context.Context, oauth string) (domain.StorageClientInterface, error) {
log.SetPrefix(logPrefix + " ")
logger := logging.NewToolLogger(logPrefix)
baseTransport := &http.Transport{
DisableKeepAlives: false,
MaxIdleConns: 0,
MaxIdleConnsPerHost: 1000,
MaxConnsPerHost: 0,
IdleConnTimeout: 60 * time.Second,
ResponseHeaderTimeout: 5 * time.Second,
TLSHandshakeTimeout: 5 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
// Create a new transport with baseTransport. The default scope for Cloud Storage client is ScopeFullControl.
transport, err := htransport.NewTransport(ctx, baseTransport, option.WithCredentialsFile(oauth), option.WithScopes(storage.ScopeFullControl))
if err != nil {
return nil, daisy.Errf("failed to create Cloud Storage client: %v", err)
}
storage, err := storageutils.NewStorageClient(ctx, logger, option.WithHTTPClient(&http.Client{Transport: transport}))
if err != nil {
return nil, daisy.Errf("failed to create Cloud Storage client: %v", err)
}
return storage, nil
}
// createAWSSession creates a new Session for AWS SDK.
func createAWSSession(region, accessKeyID, secretAccessKey, sessionToken string) (*session.Session, error) {
session, err := session.NewSession(&aws.Config{
Region: aws.String(region),
Credentials: credentials.NewStaticCredentials(
accessKeyID,
secretAccessKey,
sessionToken),
})
if err != nil {
return nil, daisy.Errf("failed to create AWS session: %v", err)
}
return session, nil
}
// run runs the aws importer to import AMI.
func (importer *awsImporter) run(importArgs *OneStepImportArguments) error {
needsExport := importer.args.isExportRequired()
startTime := time.Now()
//1. validate AWS args
err := importer.args.validateAndPopulate(importer.paramPopulator)
if err != nil {
return err
}
// 2. export AMI to AWS S3 if user did not specify an exported AMI path.
if needsExport {
log.Println("Starting to export image ...")
err = importer.exportAWSImage()
if err != nil {
return err
}
}
if err := importer.getAWSFileSize(); err != nil {
return err
}
// 3. copy from S3 to GCS
log.Println("Starting to copy ...")
gcsFilePath, err := importer.copyFromS3ToGCS()
if err != nil {
return err
}
// 4. run image import
log.Println("Starting to import image ...")
err = importer.importImage(importArgs, startTime, gcsFilePath)
if err != nil {
return err
}
log.Println("Image import from AWS finished successfully!")
// 5. clean up temporary image files created in AWS and GCS
log.Println("Cleaning up ...")
importer.cleanUp(gcsFilePath, needsExport)
return nil
}
// cleanUp deletes temporary files created during image import, and closes GCS client.
func (importer *awsImporter) cleanUp(gcsFilePath string, shouldDeleteS3File bool) {
if importer.cleanUpFn != nil {
importer.cleanUpFn()
return
}
err := importer.gcsClient.DeleteGcsPath(gcsFilePath)
if err != nil {
log.Printf("Could not delete image file %v: %v. "+
"To avoid incurring charges to your billing account, "+
"you must manually delete the file from the storage location.\n", gcsFilePath, err.Error())
}
importer.gcsClient.Close()
// Only delete s3 file if the file is not pased
if shouldDeleteS3File {
log.Printf("Deleting %v.\n", importer.args.sourceFilePath)
_, err = importer.s3Client.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(importer.args.exportBucket),
Key: aws.String(importer.args.exportKey),
})
if err != nil {
log.Printf("Could not delete image file %v: %v. "+
"To avoid incurring charges to your billing account, "+
"you must manually delete the file from the storage location.\n", importer.args.sourceFilePath, err.Error())
}
}
}
// importImage updates importArgs to contain the image source file and updated timeout duration.
// It runs image import to import from gcsFilePath to Compute Engine.
func (importer *awsImporter) importImage(importArgs *OneStepImportArguments, startTime time.Time, gcsFilePath string) error {
if importer.importImageFn != nil {
return importer.importImageFn()
}
// update source file flag to copied GCS destination
importArgs.SourceFile = gcsFilePath
// adjust timeout to pass into image import
importArgs.Timeout = importArgs.Timeout - time.Since(startTime)
if importArgs.Timeout <= 0 {
return daisy.Errf("timeout exceeded")
}
// add label to indicate the image import is run from onestep import
if importArgs.Labels == nil {
importArgs.Labels = make(map[string]string)
}
importArgs.Labels["onestep-image-import"] = "aws"
err := runImageImport(importArgs)
if err != nil {
log.Printf("Failed to import image. "+
"The image file is copied to Cloud Storage, located at %v.\n", gcsFilePath)
return err
}
return nil
}
// getAWSFileSize gets the size of the file to copy from S3 to GCS.
func (importer *awsImporter) getAWSFileSize() error {
if importer.getAWSFileSizeFn != nil {
return importer.getAWSFileSizeFn()
}
resp, err := importer.s3Client.HeadObject(&s3.HeadObjectInput{
Bucket: aws.String(importer.args.exportBucket),
Key: aws.String(importer.args.exportKey),
})
if err != nil {
return daisy.Errf("failed to get file size: %v", err)
}
fileSize := aws.Int64Value(resp.ContentLength)
if fileSize <= 0 {
return daisy.Errf("file is empty")
}
importer.args.exportFileSize = fileSize
return nil
}
// getExportImageTask creates a awsExportImageTasks from an ec2.ExportImageTask
func getExportImageTask(task *ec2.ExportImageTask) (string, string, string) {
return aws.StringValue(task.Status), aws.StringValue(task.StatusMessage), aws.StringValue(task.Progress)
}
// exportAWSImage calls 'aws ec2 export-image' command to export AMI to S3
func (importer *awsImporter) exportAWSImage() error {
if importer.exportAWSImageFn != nil {
return importer.exportAWSImageFn()
}
// 1. send export request
s3ExportLocation := &ec2.ExportTaskS3LocationRequest{
S3Bucket: aws.String(importer.args.exportBucket),
S3Prefix: aws.String(importer.args.exportKey),
}
resp, err := importer.ec2Client.ExportImage(&ec2.ExportImageInput{
DiskImageFormat: aws.String("VMDK"),
ImageId: aws.String(importer.args.amiID),
S3ExportLocation: s3ExportLocation,
})
if err != nil {
return daisy.Errf("failed to begin export AWS image: %v", err)
}
// 2. get export task id from response
taskID := aws.StringValue(resp.ExportImageTaskId)
if taskID == "" {
return daisy.Errf("empty task id returned")
}
// 3. monitor export task progress
err = importer.monitorAWSExportImageTask(taskID)
if err != nil {
importer.cancelAWSExportImageTask(taskID)
return err
}
// 4. set exported file data
importer.args.exportKey = fmt.Sprintf("%v%v.vmdk", importer.args.exportFolder, taskID)
importer.args.sourceFilePath = fmt.Sprintf("s3://%v/%v", importer.args.exportBucket, importer.args.exportKey)
log.Printf("Image export location is %v.\n", importer.args.sourceFilePath)
return nil
}
// monitorAWSExportImageTask monitors the progress of the AWS export image task.
// It calls AWS SDK to get the progress of taskID, and outputs status if task is active or completed.
// Else, there is an error with the export image task, and the error is returned.
func (importer *awsImporter) monitorAWSExportImageTask(taskID string) error {
if importer.monitorAWSExportImageTaskFn != nil {
return importer.monitorAWSExportImageTaskFn()
}
describeExportInput := &ec2.DescribeExportImageTasksInput{
ExportImageTaskIds: []*string{aws.String(taskID)},
}
var taskStatus, taskStatusMessage, taskProgress string
for {
output, err := importer.ec2Client.DescribeExportImageTasks(describeExportInput)
if err != nil {
return daisy.Errf("failed to get export task status: %v", err)
}
if len(output.ExportImageTasks) != 1 {
return daisy.Errf("failed to get export task status: unexpected response")
}
taskStatus, taskStatusMessage, taskProgress = getExportImageTask(output.ExportImageTasks[0])
if taskStatus != "active" {
if taskStatus != "completed" {
return daisy.Errf("AWS export task wasn't completed successfully")
}
log.Println("AWS export task is completed!")
break
}
log.Printf("AWS export task status: %v, status message: %v, "+
"progress: %v.\n", taskStatus, taskStatusMessage, taskProgress)
select {
case <-importer.timeoutChan:
return daisy.Errf("timeout exceeded during image export")
default:
// Did not timeout, continue to check task status.
}
time.Sleep(time.Second * 10)
}
return nil
}
// cancelAWSExportImageTask cancels the AWS export task.
func (importer *awsImporter) cancelAWSExportImageTask(taskID string) {
log.Printf("Cancelling export task ...")
importer.ec2Client.CancelExportTask(&ec2.CancelExportTaskInput{ExportTaskId: aws.String(taskID)})
}
// copyFromS3ToGCS copies VMDK file from S3 to GCS.
func (importer *awsImporter) copyFromS3ToGCS() (string, error) {
if importer.copyFromS3ToGCSFn != nil {
return importer.copyFromS3ToGCSFn()
}
start := time.Now()
// 1. get GCS path as copy destination.
gcsFilePath := pathutils.JoinURL(importer.args.gcsScratchBucket,
fmt.Sprintf("onestep-image-import-aws-%v.vmdk", pathutils.RandString(5)))
log.Printf("Copying %v to %v.\n", importer.args.sourceFilePath, gcsFilePath)
// 2. create a new folder for local buffer
path := filepath.Join(filepath.Dir(importer.args.executablePath), fmt.Sprint("upload", pathutils.RandString(5)))
err := os.Mkdir(path, 0755)
if err != nil {
return "", daisy.ToDError(err)
}
defer os.RemoveAll(path)
// 3. get writer
bs, err := humanize.ParseBytes(uploadBufSize)
if err != nil {
return "", daisy.ToDError(err)
}
bkt, obj, err := storageutils.GetGCSObjectPathElements(gcsFilePath)
if err != nil {
return "", err
}
workers := int64(runtime.NumCPU())
writer := storageutils.NewBufferedWriter(importer.ctx, int64(bs), workers, createGCSClient, importer.oauth, path, bkt, obj, logPrefix)
// 4. Transfer file from S3 to GCS
if err := importer.transferFile(writer); err != nil {
return gcsFilePath, err
}
log.Printf("Successfully copied to %v in %v.\n", gcsFilePath, time.Since(start))
return gcsFilePath, nil
}
func (importer *awsImporter) getUploader(writer io.WriteCloser) *uploader {
if importer.getUploaderFn != nil {
return importer.getUploaderFn()
}
return &uploader{
readerChan: make(chan io.ReadCloser, downloadBufNum),
writer: writer,
totalUploaded: 0,
totalFileSize: importer.args.exportFileSize,
uploadErrChan: make(chan error),
}
}
// transferFile downloads S3 file and uploads to GCS concurrently.
func (importer *awsImporter) transferFile(writer io.WriteCloser) error {
if importer.transferFileFn != nil {
return importer.transferFileFn()
}
// 1. Set up download size and get number of chunks to download
output, err := humanize.ParseBytes(downloadBufSize)
if err != nil {
return daisy.ToDError(err)
}
readSize := int64(output)
// Take ceiling to get number of chunks to download.
readers := (importer.args.exportFileSize-1)/readSize + 1
// Set up download retry delay interval
delayTime := []int{1, 2, 4, 8, 8}
maxRetryTimes := len(delayTime)
// 2. Set up upload info
importer.uploader = importer.getUploader(writer)
importer.uploader.Add(1)
go importer.uploader.uploadFile()
// 3. Range download
for i := int64(0); i < readers; i++ {
startRange := i * readSize
endRange := startRange + readSize - 1
for retryAttempt := 0; ; retryAttempt++ {
res, err := importer.s3Client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(importer.args.exportBucket),
Key: aws.String(importer.args.exportKey),
Range: aws.String(fmt.Sprintf("bytes=%v-%v", startRange, endRange)),
})
if err != nil {
if retryAttempt >= maxRetryTimes {
return daisy.Errf("error in downloading from %v: %v", importer.args.sourceFilePath, err)
}
time.Sleep(time.Duration(delayTime[retryAttempt]) * time.Second)
continue
}
importer.uploader.readerChan <- res.Body
break
}
// Stop downloading as soon as one of the upload fails.
select {
case err := <-importer.uploader.uploadErrChan:
importer.uploader.cleanup()
return err
default:
// No error, continue to download.
}
// Stop downloading if timeout exceeded.
select {
case <-importer.timeoutChan:
importer.uploader.cleanup()
return daisy.Errf("timeout exceeded during transfer file")
default:
// Did not timeout, continue to download.
}
}
// All file chunks are downloaded, wait for upload to finish.
close(importer.uploader.readerChan)
importer.uploader.Wait()
err = importer.uploader.writer.Close()
if err != nil {
return daisy.ToDError(err)
}
return nil
}