Support formats other than OVA.

This commit is contained in:
Matt Stofko 2018-10-29 14:52:12 -07:00
parent a1d6c7c916
commit 029819b5d5
1 changed files with 16 additions and 5 deletions

View File

@ -35,6 +35,7 @@ type Config struct {
Groups []string `mapstructure:"ami_groups"` Groups []string `mapstructure:"ami_groups"`
LicenseType string `mapstructure:"license_type"` LicenseType string `mapstructure:"license_type"`
RoleName string `mapstructure:"role_name"` RoleName string `mapstructure:"role_name"`
Format string `mapstructure:"format"`
ctx interpolate.Context ctx interpolate.Context
} }
@ -60,8 +61,12 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
} }
// Set defaults // Set defaults
if p.config.Format == "" {
p.config.Format = "ova"
}
if p.config.S3Key == "" { if p.config.S3Key == "" {
p.config.S3Key = "packer-import-{{timestamp}}.ova" p.config.S3Key = "packer-import-{{timestamp}}." + p.config.Format
} }
errs := new(packer.MultiError) 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 // Anything which flagged return back up the stack
if len(errs.Errors) > 0 { if len(errs.Errors) > 0 {
return errs 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.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 // Locate the files output from the builder
source := "" source := ""
for _, path := range artifact.Files() { for _, path := range artifact.Files() {
if strings.HasSuffix(path, ".ova") { if strings.HasSuffix(path, "."+p.config.Format) {
source = path source = path
break break
} }
@ -125,7 +135,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
// Hope we found something useful // Hope we found something useful
if source == "" { 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 // 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)) 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) uploader := s3manager.NewUploader(session)
_, err = uploader.Upload(&s3manager.UploadInput{ _, err = uploader.Upload(&s3manager.UploadInput{
Body: file, Body: file,
@ -160,6 +170,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
params := &ec2.ImportImageInput{ params := &ec2.ImportImageInput{
DiskContainers: []*ec2.ImageDiskContainer{ DiskContainers: []*ec2.ImageDiskContainer{
{ {
Format: &p.config.Format,
UserBucket: &ec2.UserBucket{ UserBucket: &ec2.UserBucket{
S3Bucket: &p.config.S3Bucket, S3Bucket: &p.config.S3Bucket,
S3Key: &p.config.S3Key, S3Key: &p.config.S3Key,