From 1268373105fdfc348d68a0d8a4bbdf7556f0175b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 8 Aug 2013 15:27:12 -0700 Subject: [PATCH] builder/amazon/instance: switch to new template stuff --- builder/amazon/instance/builder.go | 43 ++++++++++++++++++- builder/amazon/instance/step_bundle_volume.go | 31 +++++++------ builder/amazon/instance/step_register_ami.go | 20 +-------- builder/amazon/instance/step_upload_bundle.go | 16 ++++--- 4 files changed, 69 insertions(+), 41 deletions(-) diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index c0a5da986..75e869e3d 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -37,6 +37,8 @@ type Config struct { X509CertPath string `mapstructure:"x509_cert_path"` X509KeyPath string `mapstructure:"x509_key_path"` X509UploadPath string `mapstructure:"x509_upload_path"` + + tpl *common.Template } type Builder struct { @@ -50,6 +52,11 @@ func (b *Builder) Prepare(raws ...interface{}) error { return err } + b.config.tpl, err = common.NewTemplate() + if err != nil { + return err + } + if b.config.BundleDestination == "" { b.config.BundleDestination = "/tmp" } @@ -87,8 +94,40 @@ func (b *Builder) Prepare(raws ...interface{}) error { // Accumulate any errors errs := common.CheckUnusedConfig(md) - errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare()...) - errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare()...) + errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(b.config.tpl)...) + errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(b.config.tpl)...) + + validates := map[string]*string{ + "bundle_prefix": &b.config.BundlePrefix, + "bundle_upload_command": &b.config.BundleUploadCommand, + "bundle_vol_command": &b.config.BundleVolCommand, + } + + for n, ptr := range validates { + if err := b.config.tpl.Validate(*ptr); err != nil { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("Error parsing %s: %s", n, err)) + } + } + + templates := map[string]*string{ + "account_id": &b.config.AccountId, + "ami_name": &b.config.AMIName, + "bundle_destination": &b.config.BundleDestination, + "s3_bucket": &b.config.S3Bucket, + "x509_cert_path": &b.config.X509CertPath, + "x509_key_path": &b.config.X509KeyPath, + "x509_upload_path": &b.config.X509UploadPath, + } + + for n, ptr := range templates { + var err error + *ptr, err = b.config.tpl.Process(*ptr, nil) + if err != nil { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("Error processing %s: %s", n, err)) + } + } if b.config.AccountId == "" { errs = packer.MultiErrorAppend(errs, errors.New("account_id is required")) diff --git a/builder/amazon/instance/step_bundle_volume.go b/builder/amazon/instance/step_bundle_volume.go index f5da54dde..cd8b38f25 100644 --- a/builder/amazon/instance/step_bundle_volume.go +++ b/builder/amazon/instance/step_bundle_volume.go @@ -1,13 +1,11 @@ package instance import ( - "bytes" "fmt" "github.com/mitchellh/goamz/ec2" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" "strconv" - "text/template" "time" ) @@ -55,29 +53,36 @@ func (s *StepBundleVolume) Run(state map[string]interface{}) multistep.StepActio } // Bundle the volume - var bundlePrefix bytes.Buffer - prefixTData := bundlePrefixData{ + var err error + config.BundlePrefix, err = config.tpl.Process(config.BundlePrefix, bundlePrefixData{ CreateTime: strconv.FormatInt(time.Now().UTC().Unix(), 10), + }) + if err != nil { + err := fmt.Errorf("Error processing bundle prefix: %s", err) + state["error"] = err + ui.Error(err.Error()) + return multistep.ActionHalt } - t := template.Must(template.New("bundlePrefix").Parse(config.BundlePrefix)) - t.Execute(&bundlePrefix, prefixTData) - var bundleCmd bytes.Buffer - tData := bundleCmdData{ + config.BundleVolCommand, err = config.tpl.Process(config.BundleVolCommand, bundleCmdData{ AccountId: config.AccountId, Architecture: instance.Architecture, CertPath: x509RemoteCertPath, Destination: config.BundleDestination, KeyPath: x509RemoteKeyPath, - Prefix: bundlePrefix.String(), + Prefix: config.BundlePrefix, PrivatePath: config.X509UploadPath, + }) + if err != nil { + err := fmt.Errorf("Error processing bundle volume command: %s", err) + state["error"] = err + ui.Error(err.Error()) + return multistep.ActionHalt } - t = template.Must(template.New("bundleCmd").Parse(config.BundleVolCommand)) - t.Execute(&bundleCmd, tData) ui.Say("Bundling the volume...") cmd = new(packer.RemoteCmd) - cmd.Command = bundleCmd.String() + cmd.Command = config.BundleVolCommand if err := cmd.StartWithUi(comm, ui); err != nil { state["error"] = fmt.Errorf("Error bundling volume: %s", err) ui.Error(state["error"].(error).Error()) @@ -93,7 +98,7 @@ func (s *StepBundleVolume) Run(state map[string]interface{}) multistep.StepActio } // Store the manifest path - manifestName := bundlePrefix.String() + ".manifest.xml" + manifestName := config.BundlePrefix + ".manifest.xml" state["manifest_name"] = manifestName state["manifest_path"] = fmt.Sprintf( "%s/%s", config.BundleDestination, manifestName) diff --git a/builder/amazon/instance/step_register_ami.go b/builder/amazon/instance/step_register_ami.go index 1accf7e48..38bb319ad 100644 --- a/builder/amazon/instance/step_register_ami.go +++ b/builder/amazon/instance/step_register_ami.go @@ -1,21 +1,13 @@ package instance import ( - "bytes" "fmt" "github.com/mitchellh/goamz/ec2" "github.com/mitchellh/multistep" awscommon "github.com/mitchellh/packer/builder/amazon/common" "github.com/mitchellh/packer/packer" - "strconv" - "text/template" - "time" ) -type amiNameData struct { - CreateTime string -} - type StepRegisterAMI struct{} func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction { @@ -24,20 +16,10 @@ func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction manifestPath := state["remote_manifest_path"].(string) ui := state["ui"].(packer.Ui) - // Parse the name of the AMI - amiNameBuf := new(bytes.Buffer) - tData := amiNameData{ - strconv.FormatInt(time.Now().UTC().Unix(), 10), - } - - t := template.Must(template.New("ami").Parse(config.AMIName)) - t.Execute(amiNameBuf, tData) - amiName := amiNameBuf.String() - ui.Say("Registering the AMI...") registerOpts := &ec2.RegisterImage{ ImageLocation: manifestPath, - Name: amiName, + Name: config.AMIName, } registerResp, err := ec2conn.RegisterImage(registerOpts) diff --git a/builder/amazon/instance/step_upload_bundle.go b/builder/amazon/instance/step_upload_bundle.go index 8c8a9fdb5..842a0c07b 100644 --- a/builder/amazon/instance/step_upload_bundle.go +++ b/builder/amazon/instance/step_upload_bundle.go @@ -1,11 +1,9 @@ package instance import ( - "bytes" "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" - "text/template" ) type uploadCmdData struct { @@ -25,19 +23,23 @@ func (s *StepUploadBundle) Run(state map[string]interface{}) multistep.StepActio manifestPath := state["manifest_path"].(string) ui := state["ui"].(packer.Ui) - var uploadCmd bytes.Buffer - tData := uploadCmdData{ + var err error + config.BundleUploadCommand, err = config.tpl.Process(config.BundleUploadCommand, uploadCmdData{ AccessKey: config.AccessKey, BucketName: config.S3Bucket, BundleDirectory: config.BundleDestination, ManifestPath: manifestPath, SecretKey: config.SecretKey, + }) + if err != nil { + err := fmt.Errorf("Error processing bundle upload command: %s", err) + state["error"] = err + ui.Error(err.Error()) + return multistep.ActionHalt } - t := template.Must(template.New("uploadCmd").Parse(config.BundleUploadCommand)) - t.Execute(&uploadCmd, tData) ui.Say("Uploading the bundle...") - cmd := &packer.RemoteCmd{Command: uploadCmd.String()} + cmd := &packer.RemoteCmd{Command: config.BundleUploadCommand} if err := cmd.StartWithUi(comm, ui); err != nil { state["error"] = fmt.Errorf("Error uploading volume: %s", err) ui.Error(state["error"].(error).Error())