From 029819b5d5cb51d5eaee8570faab26c46b2b28d8 Mon Sep 17 00:00:00 2001 From: Matt Stofko Date: Mon, 29 Oct 2018 14:52:12 -0700 Subject: [PATCH 1/3] Support formats other than OVA. --- .../amazon-import/post-processor.go | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/post-processor/amazon-import/post-processor.go b/post-processor/amazon-import/post-processor.go index c24b2f7ec..6be9772d7 100644 --- a/post-processor/amazon-import/post-processor.go +++ b/post-processor/amazon-import/post-processor.go @@ -35,6 +35,7 @@ type Config struct { Groups []string `mapstructure:"ami_groups"` LicenseType string `mapstructure:"license_type"` RoleName string `mapstructure:"role_name"` + Format string `mapstructure:"format"` ctx interpolate.Context } @@ -60,8 +61,12 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { } // Set defaults + if p.config.Format == "" { + p.config.Format = "ova" + } + if p.config.S3Key == "" { - p.config.S3Key = "packer-import-{{timestamp}}.ova" + p.config.S3Key = "packer-import-{{timestamp}}." + p.config.Format } errs := new(packer.MultiError) @@ -87,6 +92,11 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { } } + if !(p.config.Format == "ova" || p.config.Format == "raw" || p.config.Format == "vmdk" || p.config.Format == "vhd" || p.config.Format == "vhdx") { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("invalid format '%s'. Only 'ova', 'raw', 'vhd', 'vhdx', or 'vmdk' are allowed", p.config.Format)) + } + // Anything which flagged return back up the stack if len(errs.Errors) > 0 { return errs @@ -113,11 +123,11 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac } log.Printf("Rendered s3_key_name as %s", p.config.S3Key) - log.Println("Looking for OVA in artifact") + log.Println("Looking for image in artifact") // Locate the files output from the builder source := "" for _, path := range artifact.Files() { - if strings.HasSuffix(path, ".ova") { + if strings.HasSuffix(path, "."+p.config.Format) { source = path break } @@ -125,7 +135,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac // Hope we found something useful if source == "" { - return nil, false, fmt.Errorf("No OVA file found in artifact from builder") + return nil, false, fmt.Errorf("No %s image file found in artifact from builder", p.config.Format) } // open the source file @@ -137,7 +147,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac ui.Message(fmt.Sprintf("Uploading %s to s3://%s/%s", source, p.config.S3Bucket, p.config.S3Key)) - // Copy the OVA file into the S3 bucket specified + // Copy the image file into the S3 bucket specified uploader := s3manager.NewUploader(session) _, err = uploader.Upload(&s3manager.UploadInput{ Body: file, @@ -160,6 +170,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac params := &ec2.ImportImageInput{ DiskContainers: []*ec2.ImageDiskContainer{ { + Format: &p.config.Format, UserBucket: &ec2.UserBucket{ S3Bucket: &p.config.S3Bucket, S3Key: &p.config.S3Key, From c303a21390a3e18ab84af90a18652b657dedd46c Mon Sep 17 00:00:00 2001 From: Matt Stofko Date: Mon, 29 Oct 2018 14:54:42 -0700 Subject: [PATCH 2/3] Add documentation for 'format' parameter. --- website/source/docs/post-processors/amazon-import.html.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/website/source/docs/post-processors/amazon-import.html.md b/website/source/docs/post-processors/amazon-import.html.md index 1153cd6ff..b740fc838 100644 --- a/website/source/docs/post-processors/amazon-import.html.md +++ b/website/source/docs/post-processors/amazon-import.html.md @@ -65,6 +65,10 @@ Optional: provider whose API is compatible with aws EC2. Specify another endpoint like this `https://ec2.custom.endpoint.com`. +- `format` (string) - One of: `ova`, `raw`, `vhd`, `vhdx`, or `vmdk`. This specifies + the format of the source virtual machine image. The resulting artifact from the builder + is assumed to have a file extension matching the format. This defaults to `ova`. + - `license_type` (string) - The license type to be used for the Amazon Machine Image (AMI) after importing. Valid values: `AWS` or `BYOL` (default). For more details regarding licensing, see From b113518815fe7dd1b81e45b0cf1ac9ab03ebdcac Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Tue, 30 Oct 2018 10:06:31 -0700 Subject: [PATCH 3/3] Use switch for readability Co-Authored-By: c0sco <778087+c0sco@users.noreply.github.com> --- post-processor/amazon-import/post-processor.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/post-processor/amazon-import/post-processor.go b/post-processor/amazon-import/post-processor.go index 6be9772d7..e4df4ea01 100644 --- a/post-processor/amazon-import/post-processor.go +++ b/post-processor/amazon-import/post-processor.go @@ -92,7 +92,9 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { } } - if !(p.config.Format == "ova" || p.config.Format == "raw" || p.config.Format == "vmdk" || p.config.Format == "vhd" || p.config.Format == "vhdx") { + switch p.config.Format { + case "ova", "raw", "vmdk", "vhd", "vhdx": + default: errs = packer.MultiErrorAppend( errs, fmt.Errorf("invalid format '%s'. Only 'ova', 'raw', 'vhd', 'vhdx', or 'vmdk' are allowed", p.config.Format)) }