Skip to content

Conversation

@Itxaka
Copy link

@Itxaka Itxaka commented Jul 2, 2021

This patch adds the possibility of importing and image to amazon by
using the import snapshot api, which has lower requirements than the
image import.

It resuses the current post-process method but diverges once we need to
import the image. The image import is untouched, but the new snapshot
import does something similar to image. It uploads the artifact to s3,
then imports it as snapshot and finally registers the snapshot as a new
ami.

Closes #103

Signed-off-by: Itxaka igarcia@suse.com

This patch adds the possibility of importing and image to amazon by
using the import snapshot api, which has lower requirements than the
image import.

It resuses the current post-process method but diverges once we need to
import the image. The image import is untouched, but the new snapshot
import does something similar to image. It uploads the artifact to s3,
then imports it as snapshot and finally registers the snapshot as a new
ami.

Signed-off-by: Itxaka <igarcia@suse.com>
@Itxaka Itxaka requested a review from a team as a code owner July 2, 2021 15:46
@hashicorp-cla
Copy link

hashicorp-cla commented Jul 2, 2021

CLA assistant check
All committers have signed the CLA.

@Itxaka
Copy link
Author

Itxaka commented Jul 2, 2021

Missing:

  • docs
  • testing the import image manually to see if I broke it (probably!)
  • adding some extra options for the snapshot part (i.e. Architecture, boot mode, virtualization type)

Done:

  • checked that I can generate an AMI from an artifact by going the snapshot route

@Itxaka
Copy link
Author

Itxaka commented Jul 2, 2021

@sylviamoss Just the first pass, before I go further, I think this is a good step. Will await any comments before continuing. Have a nice weekend!

Copy link

@sylviamoss sylviamoss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good so far! I make some small suggestions. I haven't tested it though.

So, my understanding is that importing from a snapshot is an easier option to register an AMI as it has fewer requirements to do so, right? In the end, the post-processor will produce two artifacts then, a snapshot and an AMI.
We need to make sure to document the advantages here, so people won't get confused about which option they should go for it.

Comment on lines +86 to +88
if p.config.FromSnapshotDeviceName == "" {
p.config.FromSnapshotDeviceName = "/dev/sda"
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about moving this to inside the FromSnapshot condition? For better readability of the fact that this is only used and the default only matters if FromSnapshot is true.

})

if err != nil {
return "", fmt.Errorf("Failed to start import from s3://%s/%s: %s", p.config.S3Bucket, p.config.S3Key, err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To differentiate the error messages:

Suggested change
return "", fmt.Errorf("Failed to start import from s3://%s/%s: %s", p.config.S3Bucket, p.config.S3Key, err)
return "", fmt.Errorf("Failed to start snapshot import from s3://%s/%s: %s", p.config.S3Bucket, p.config.S3Key, err)


importTaskId := importStart.ImportTaskId

ui.Message(fmt.Sprintf("Started import of s3://%s/%s, task id %s", p.config.S3Bucket, p.config.S3Key, *importTaskId))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ui.Message(fmt.Sprintf("Started import of s3://%s/%s, task id %s", p.config.S3Bucket, p.config.S3Key, *importTaskId))
ui.Message(fmt.Sprintf("Started snapshot import of s3://%s/%s, task id %s", p.config.S3Bucket, p.config.S3Key, *importTaskId))

Comment on lines +466 to +477
importResult, err2 = ec2conn.DescribeImportSnapshotTasks(&ec2.DescribeImportSnapshotTasksInput{
ImportTaskIds: []*string{
importTaskId,
},
})

statusMessage := "Error retrieving status message"

if err2 == nil {
statusMessage = *importResult.ImportSnapshotTasks[0].SnapshotTaskDetail.StatusMessage
}
return "", fmt.Errorf("Import task %s failed with status message: %s, error: %s", *importTaskId, statusMessage, err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplifying this a little bit:

Suggested change
importResult, err2 = ec2conn.DescribeImportSnapshotTasks(&ec2.DescribeImportSnapshotTasksInput{
ImportTaskIds: []*string{
importTaskId,
},
})
statusMessage := "Error retrieving status message"
if err2 == nil {
statusMessage = *importResult.ImportSnapshotTasks[0].SnapshotTaskDetail.StatusMessage
}
return "", fmt.Errorf("Import task %s failed with status message: %s, error: %s", *importTaskId, statusMessage, err)
if importResult, describeErr := ec2conn.DescribeImportSnapshotTasks(&ec2.DescribeImportSnapshotTasksInput{
ImportTaskIds: []*string{
importTaskId,
},
}); describeErr != nil {
return "", fmt.Errorf("Import task %s failed with status message: %s, error: %s", *importTaskId, *importResult.ImportSnapshotTasks[0].SnapshotTaskDetail.StatusMessage, err)
}
return "", fmt.Errorf("Import task %s failed with status message: Error retrieving status message, error: %s", *importTaskId, err)


func (p *PostProcessor) importSnapshot(ui packersdk.Ui, ctx context.Context, ec2conn *ec2.EC2) (string, error) {
var err error
var err2 error

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the code simplification I proposed below this can be removed

Suggested change
var err2 error

})

if err != nil {
return "", fmt.Errorf("Failed to find import task %s: %s", *importTaskId, err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return "", fmt.Errorf("Failed to find import task %s: %s", *importTaskId, err)
return "", fmt.Errorf("Failed to find import snapshot task %s: %s", *importTaskId, err)

// Check it was actually completed
if *importResult.ImportSnapshotTasks[0].SnapshotTaskDetail.Status != "completed" {
// The most useful error message is from the job itself
return "", fmt.Errorf("Import task %s failed: %s", *importTaskId, *importResult.ImportSnapshotTasks[0].SnapshotTaskDetail.StatusMessage)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return "", fmt.Errorf("Import task %s failed: %s", *importTaskId, *importResult.ImportSnapshotTasks[0].SnapshotTaskDetail.StatusMessage)
return "", fmt.Errorf("Import snapshot task %s failed: %s", *importTaskId, *importResult.ImportSnapshotTasks[0].SnapshotTaskDetail.StatusMessage)

return "", fmt.Errorf("Import task %s failed: %s", *importTaskId, *importResult.ImportSnapshotTasks[0].SnapshotTaskDetail.StatusMessage)
}

ui.Message(fmt.Sprintf("Import task %s complete", *importTaskId))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ui.Message(fmt.Sprintf("Import task %s complete", *importTaskId))
ui.Message(fmt.Sprintf("Import snapshot task %s completed", *importTaskId))

@Itxaka
Copy link
Author

Itxaka commented Jul 12, 2021

Thanks for the comments! Not stale, Im just a bit strapped on time :)

@Itxaka
Copy link
Author

Itxaka commented Jul 15, 2021

Currently leaving for vacation, will pick this up once Im back in August!

@sylviamoss
Copy link

Sounds good!

@Itxaka
Copy link
Author

Itxaka commented Oct 18, 2021

unfortunately I wont have any more time to dedicate to this, sorry about that @sylviamoss :/

@Itxaka Itxaka closed this Oct 18, 2021
@breisig
Copy link

breisig commented Aug 7, 2022

Why was this never merged? This feature is required especially when attempting to upload Linux distro that aren't supported during vmimport [Example: Rocky Linux 9]

@Itxaka
Copy link
Author

Itxaka commented Aug 7, 2022

See above, my bandwidth to test and continue developing this was zero, so I could not continue on it.

Good thing the code is in there, feel free to pick it up and with the comments from Silvia send a PR to have this merged 😀

@Itxaka Itxaka deleted the snapshot-import branch September 14, 2023 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

amazon-import -> Import as snapshot?

5 participants