From 8e7c2796fca886c3e0f20d7b6c922778c9cd4ba7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 31 Aug 2013 13:03:13 -0700 Subject: [PATCH] builder/amazon/instance: new multistep API --- builder/amazon/instance/builder.go | 16 ++++----- builder/amazon/instance/step_bundle_volume.go | 36 +++++++++---------- builder/amazon/instance/step_register_ami.go | 20 +++++------ builder/amazon/instance/step_upload_bundle.go | 32 ++++++++--------- .../amazon/instance/step_upload_x509_cert.go | 22 ++++++------ 5 files changed, 63 insertions(+), 63 deletions(-) diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 6d1ce15e6..2fd5b0664 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -176,11 +176,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ec2conn := ec2.New(auth, region) // Setup the state bag and initial state for the steps - state := make(map[string]interface{}) - state["config"] = &b.config - state["ec2"] = ec2conn - state["hook"] = hook - state["ui"] = ui + state := new(multistep.BasicStateBag) + state.Put("config", &b.config) + state.Put("ec2", ec2conn) + state.Put("hook", hook) + state.Put("ui", ui) // Build the steps steps := []multistep.Step{ @@ -242,18 +242,18 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe b.runner.Run(state) // If there was an error, return that - if rawErr, ok := state["error"]; ok { + if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) } // If there are no AMIs, then just return - if _, ok := state["amis"]; !ok { + if _, ok := state.GetOk("amis"); !ok { return nil, nil } // Build the artifact and return it artifact := &awscommon.Artifact{ - Amis: state["amis"].(map[string]string), + Amis: state.Get("amis").(map[string]string), BuilderIdValue: BuilderId, Conn: ec2conn, } diff --git a/builder/amazon/instance/step_bundle_volume.go b/builder/amazon/instance/step_bundle_volume.go index 4e690bf88..4dfbd4fc4 100644 --- a/builder/amazon/instance/step_bundle_volume.go +++ b/builder/amazon/instance/step_bundle_volume.go @@ -19,13 +19,13 @@ type bundleCmdData struct { type StepBundleVolume struct{} -func (s *StepBundleVolume) Run(state map[string]interface{}) multistep.StepAction { - comm := state["communicator"].(packer.Communicator) - config := state["config"].(*Config) - instance := state["instance"].(*ec2.Instance) - ui := state["ui"].(packer.Ui) - x509RemoteCertPath := state["x509RemoteCertPath"].(string) - x509RemoteKeyPath := state["x509RemoteKeyPath"].(string) +func (s *StepBundleVolume) Run(state multistep.StateBag) multistep.StepAction { + comm := state.Get("communicator").(packer.Communicator) + config := state.Get("config").(*Config) + instance := state.Get("instance").(*ec2.Instance) + ui := state.Get("ui").(packer.Ui) + x509RemoteCertPath := state.Get("x509RemoteCertPath").(string) + x509RemoteKeyPath := state.Get("x509RemoteKeyPath").(string) // Bundle the volume var err error @@ -40,7 +40,7 @@ func (s *StepBundleVolume) Run(state map[string]interface{}) multistep.StepActio }) if err != nil { err := fmt.Errorf("Error processing bundle volume command: %s", err) - state["error"] = err + state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } @@ -49,26 +49,26 @@ func (s *StepBundleVolume) Run(state map[string]interface{}) multistep.StepActio cmd := new(packer.RemoteCmd) 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()) + state.Put("error", fmt.Errorf("Error bundling volume: %s", err)) + ui.Error(state.Get("error").(error).Error()) return multistep.ActionHalt } if cmd.ExitStatus != 0 { - state["error"] = fmt.Errorf( - "Volume bundling failed. Please see the output above for more\n" + - "details on what went wrong.") - ui.Error(state["error"].(error).Error()) + state.Put("error", fmt.Errorf( + "Volume bundling failed. Please see the output above for more\n"+ + "details on what went wrong.")) + ui.Error(state.Get("error").(error).Error()) return multistep.ActionHalt } // Store the manifest path manifestName := config.BundlePrefix + ".manifest.xml" - state["manifest_name"] = manifestName - state["manifest_path"] = fmt.Sprintf( - "%s/%s", config.BundleDestination, manifestName) + state.Put("manifest_name", manifestName) + state.Put("manifest_path", fmt.Sprintf( + "%s/%s", config.BundleDestination, manifestName)) return multistep.ActionContinue } -func (s *StepBundleVolume) Cleanup(map[string]interface{}) {} +func (s *StepBundleVolume) Cleanup(multistep.StateBag) {} diff --git a/builder/amazon/instance/step_register_ami.go b/builder/amazon/instance/step_register_ami.go index 1237e6f81..729c020d3 100644 --- a/builder/amazon/instance/step_register_ami.go +++ b/builder/amazon/instance/step_register_ami.go @@ -10,11 +10,11 @@ import ( type StepRegisterAMI struct{} -func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction { - config := state["config"].(*Config) - ec2conn := state["ec2"].(*ec2.EC2) - manifestPath := state["remote_manifest_path"].(string) - ui := state["ui"].(packer.Ui) +func (s *StepRegisterAMI) Run(state multistep.StateBag) multistep.StepAction { + config := state.Get("config").(*Config) + ec2conn := state.Get("ec2").(*ec2.EC2) + manifestPath := state.Get("remote_manifest_path").(string) + ui := state.Get("ui").(packer.Ui) ui.Say("Registering the AMI...") registerOpts := &ec2.RegisterImage{ @@ -25,8 +25,8 @@ func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction registerResp, err := ec2conn.RegisterImage(registerOpts) if err != nil { - state["error"] = fmt.Errorf("Error registering AMI: %s", err) - ui.Error(state["error"].(error).Error()) + state.Put("error", fmt.Errorf("Error registering AMI: %s", err)) + ui.Error(state.Get("error").(error).Error()) return multistep.ActionHalt } @@ -34,13 +34,13 @@ func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction ui.Say(fmt.Sprintf("AMI: %s", registerResp.ImageId)) amis := make(map[string]string) amis[ec2conn.Region.Name] = registerResp.ImageId - state["amis"] = amis + state.Put("amis", amis) // Wait for the image to become ready ui.Say("Waiting for AMI to become ready...") if err := awscommon.WaitForAMI(ec2conn, registerResp.ImageId); err != nil { err := fmt.Errorf("Error waiting for AMI: %s", err) - state["error"] = err + state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } @@ -48,4 +48,4 @@ func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction return multistep.ActionContinue } -func (s *StepRegisterAMI) Cleanup(map[string]interface{}) {} +func (s *StepRegisterAMI) Cleanup(multistep.StateBag) {} diff --git a/builder/amazon/instance/step_upload_bundle.go b/builder/amazon/instance/step_upload_bundle.go index 842a0c07b..e002ff43d 100644 --- a/builder/amazon/instance/step_upload_bundle.go +++ b/builder/amazon/instance/step_upload_bundle.go @@ -16,12 +16,12 @@ type uploadCmdData struct { type StepUploadBundle struct{} -func (s *StepUploadBundle) Run(state map[string]interface{}) multistep.StepAction { - comm := state["communicator"].(packer.Communicator) - config := state["config"].(*Config) - manifestName := state["manifest_name"].(string) - manifestPath := state["manifest_path"].(string) - ui := state["ui"].(packer.Ui) +func (s *StepUploadBundle) Run(state multistep.StateBag) multistep.StepAction { + comm := state.Get("communicator").(packer.Communicator) + config := state.Get("config").(*Config) + manifestName := state.Get("manifest_name").(string) + manifestPath := state.Get("manifest_path").(string) + ui := state.Get("ui").(packer.Ui) var err error config.BundleUploadCommand, err = config.tpl.Process(config.BundleUploadCommand, uploadCmdData{ @@ -33,7 +33,7 @@ func (s *StepUploadBundle) Run(state map[string]interface{}) multistep.StepActio }) if err != nil { err := fmt.Errorf("Error processing bundle upload command: %s", err) - state["error"] = err + state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } @@ -41,23 +41,23 @@ func (s *StepUploadBundle) Run(state map[string]interface{}) multistep.StepActio ui.Say("Uploading the bundle...") 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()) + state.Put("error", fmt.Errorf("Error uploading volume: %s", err)) + ui.Error(state.Get("error").(error).Error()) return multistep.ActionHalt } if cmd.ExitStatus != 0 { - state["error"] = fmt.Errorf( - "Bundle upload failed. Please see the output above for more\n" + - "details on what went wrong.") - ui.Error(state["error"].(error).Error()) + state.Put("error", fmt.Errorf( + "Bundle upload failed. Please see the output above for more\n"+ + "details on what went wrong.")) + ui.Error(state.Get("error").(error).Error()) return multistep.ActionHalt } - state["remote_manifest_path"] = fmt.Sprintf( - "%s/%s", config.S3Bucket, manifestName) + state.Put("remote_manifest_path", fmt.Sprintf( + "%s/%s", config.S3Bucket, manifestName)) return multistep.ActionContinue } -func (s *StepUploadBundle) Cleanup(state map[string]interface{}) {} +func (s *StepUploadBundle) Cleanup(state multistep.StateBag) {} diff --git a/builder/amazon/instance/step_upload_x509_cert.go b/builder/amazon/instance/step_upload_x509_cert.go index 5660bc228..d62ea8797 100644 --- a/builder/amazon/instance/step_upload_x509_cert.go +++ b/builder/amazon/instance/step_upload_x509_cert.go @@ -9,34 +9,34 @@ import ( type StepUploadX509Cert struct{} -func (s *StepUploadX509Cert) Run(state map[string]interface{}) multistep.StepAction { - comm := state["communicator"].(packer.Communicator) - config := state["config"].(*Config) - ui := state["ui"].(packer.Ui) +func (s *StepUploadX509Cert) Run(state multistep.StateBag) multistep.StepAction { + comm := state.Get("communicator").(packer.Communicator) + config := state.Get("config").(*Config) + ui := state.Get("ui").(packer.Ui) x509RemoteCertPath := config.X509UploadPath + "/cert.pem" x509RemoteKeyPath := config.X509UploadPath + "/key.pem" ui.Say("Uploading X509 Certificate...") if err := s.uploadSingle(comm, x509RemoteCertPath, config.X509CertPath); err != nil { - state["error"] = fmt.Errorf("Error uploading X509 cert: %s", err) - ui.Error(state["error"].(error).Error()) + state.Put("error", fmt.Errorf("Error uploading X509 cert: %s", err)) + ui.Error(state.Get("error").(error).Error()) return multistep.ActionHalt } if err := s.uploadSingle(comm, x509RemoteKeyPath, config.X509KeyPath); err != nil { - state["error"] = fmt.Errorf("Error uploading X509 cert: %s", err) - ui.Error(state["error"].(error).Error()) + state.Put("error", fmt.Errorf("Error uploading X509 cert: %s", err)) + ui.Error(state.Get("error").(error).Error()) return multistep.ActionHalt } - state["x509RemoteCertPath"] = x509RemoteCertPath - state["x509RemoteKeyPath"] = x509RemoteKeyPath + state.Put("x509RemoteCertPath", x509RemoteCertPath) + state.Put("x509RemoteKeyPath", x509RemoteKeyPath) return multistep.ActionContinue } -func (s *StepUploadX509Cert) Cleanup(map[string]interface{}) {} +func (s *StepUploadX509Cert) Cleanup(multistep.StateBag) {} func (s *StepUploadX509Cert) uploadSingle(comm packer.Communicator, dst, src string) error { f, err := os.Open(src)