From be91c99d297886f612900f48254e7ed14fd9d326 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Mon, 22 Oct 2018 16:39:04 -0700 Subject: [PATCH] warn about dangling segments. The client offers no way to bulk delete keys, so for now let's just warn users. The issue is tracked upstream here: https://github.com/hashicorp/go-oracle-terraform/issues/172 --- builder/oracle/classic/builder.go | 16 ++++++++-------- builder/oracle/classic/step_attach_volume.go | 16 ++++++++-------- .../oracle/classic/step_create_pv_builder.go | 10 +++++----- .../oracle/classic/step_create_pv_master.go | 10 +++++----- builder/oracle/classic/step_upload_image.go | 18 ++++++++++++++---- 5 files changed, 40 insertions(+), 30 deletions(-) diff --git a/builder/oracle/classic/builder.go b/builder/oracle/classic/builder.go index 5cbc07f40..d744d00bf 100644 --- a/builder/oracle/classic/builder.go +++ b/builder/oracle/classic/builder.go @@ -97,8 +97,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &stepAddKeysToAPI{}, &stepSecurity{}, &stepCreatePVMaster{ - name: fmt.Sprintf("master-instance_%s", runID), - volumeName: fmt.Sprintf("master-storage_%s", runID), + Name: fmt.Sprintf("master-instance_%s", runID), + VolumeName: fmt.Sprintf("master-storage_%s", runID), }, &communicator.StepConnect{ Config: &b.config.Comm, @@ -108,13 +108,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &common.StepProvision{}, &stepTerminatePVMaster{}, &stepCreatePVBuilder{ - name: fmt.Sprintf("builder-instance_%s", runID), - builderVolumeName: fmt.Sprintf("builder-storage_%s", runID), + Name: fmt.Sprintf("builder-instance_%s", runID), + BuilderVolumeName: fmt.Sprintf("builder-storage_%s", runID), }, &stepAttachVolume{ - volumeName: fmt.Sprintf("master-storage_%s", runID), - index: 2, - instanceInfoKey: "builder_instance_info", + VolumeName: fmt.Sprintf("master-storage_%s", runID), + Index: 2, + InstanceInfoKey: "builder_instance_info", }, &communicator.StepConnect{ Config: &builderCommConfig, @@ -122,7 +122,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe SSHConfig: b.config.Comm.SSHConfigFunc(), }, &stepUploadImage{ - uploadImageCommand: b.config.BuilderUploadImageCommand, + UploadImageCommand: b.config.BuilderUploadImageCommand, }, &stepCreateImage{}, &stepListImages{}, diff --git a/builder/oracle/classic/step_attach_volume.go b/builder/oracle/classic/step_attach_volume.go index c4d06f0ef..83576cd03 100644 --- a/builder/oracle/classic/step_attach_volume.go +++ b/builder/oracle/classic/step_attach_volume.go @@ -10,21 +10,21 @@ import ( ) type stepAttachVolume struct { - index int - volumeName string - instanceInfoKey string + Index int + VolumeName string + InstanceInfoKey string } func (s *stepAttachVolume) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { client := state.Get("client").(*compute.ComputeClient) ui := state.Get("ui").(packer.Ui) - instanceInfo := state.Get(s.instanceInfoKey).(*compute.InstanceInfo) + instanceInfo := state.Get(s.InstanceInfoKey).(*compute.InstanceInfo) saClient := client.StorageAttachments() saInput := &compute.CreateStorageAttachmentInput{ - Index: s.index, + Index: s.Index, InstanceName: instanceInfo.Name + "/" + instanceInfo.ID, - StorageVolumeName: s.volumeName, + StorageVolumeName: s.VolumeName, } sa, err := saClient.CreateStorageAttachment(saInput) @@ -35,14 +35,14 @@ func (s *stepAttachVolume) Run(_ context.Context, state multistep.StateBag) mult return multistep.ActionHalt } - state.Put(s.instanceInfoKey+"/attachment", sa) + state.Put(s.InstanceInfoKey+"/attachment", sa) ui.Message("Volume attached to instance.") return multistep.ActionContinue } func (s *stepAttachVolume) Cleanup(state multistep.StateBag) { - sa, ok := state.GetOk(s.instanceInfoKey + "/attachment") + sa, ok := state.GetOk(s.InstanceInfoKey + "/attachment") if !ok { return } diff --git a/builder/oracle/classic/step_create_pv_builder.go b/builder/oracle/classic/step_create_pv_builder.go index 30f695a33..f0cab1061 100644 --- a/builder/oracle/classic/step_create_pv_builder.go +++ b/builder/oracle/classic/step_create_pv_builder.go @@ -11,8 +11,8 @@ import ( ) type stepCreatePVBuilder struct { - name string - builderVolumeName string + Name string + BuilderVolumeName string } func (s *stepCreatePVBuilder) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { @@ -30,7 +30,7 @@ func (s *stepCreatePVBuilder) Run(_ context.Context, state multistep.StateBag) m // Instances Input input := &compute.CreateInstanceInput{ - Name: s.name, + Name: s.Name, Shape: config.Shape, Networking: map[string]compute.NetworkingInfo{ "eth0": compute.NetworkingInfo{ @@ -40,7 +40,7 @@ func (s *stepCreatePVBuilder) Run(_ context.Context, state multistep.StateBag) m }, Storage: []compute.StorageAttachmentInput{ { - Volume: s.builderVolumeName, + Volume: s.BuilderVolumeName, Index: 1, }, }, @@ -80,7 +80,7 @@ func (s *stepCreatePVBuilder) Cleanup(state multistep.StateBag) { instanceClient := client.Instances() input := &compute.DeleteInstanceInput{ - Name: s.name, + Name: s.Name, ID: instanceID.(string), } diff --git a/builder/oracle/classic/step_create_pv_master.go b/builder/oracle/classic/step_create_pv_master.go index cffca9e55..082f23b50 100644 --- a/builder/oracle/classic/step_create_pv_master.go +++ b/builder/oracle/classic/step_create_pv_master.go @@ -10,8 +10,8 @@ import ( ) type stepCreatePVMaster struct { - name string - volumeName string + Name string + VolumeName string } func (s *stepCreatePVMaster) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { @@ -29,7 +29,7 @@ func (s *stepCreatePVMaster) Run(_ context.Context, state multistep.StateBag) mu // Instances Input input := &compute.CreateInstanceInput{ - Name: s.name, + Name: s.Name, Shape: config.Shape, Networking: map[string]compute.NetworkingInfo{ "eth0": compute.NetworkingInfo{ @@ -39,7 +39,7 @@ func (s *stepCreatePVMaster) Run(_ context.Context, state multistep.StateBag) mu }, Storage: []compute.StorageAttachmentInput{ { - Volume: s.volumeName, + Volume: s.VolumeName, Index: 1, }, }, @@ -81,7 +81,7 @@ func (s *stepCreatePVMaster) Cleanup(state multistep.StateBag) { instanceClient := client.Instances() input := &compute.DeleteInstanceInput{ - Name: s.name, + Name: s.Name, ID: instanceID.(string), } diff --git a/builder/oracle/classic/step_upload_image.go b/builder/oracle/classic/step_upload_image.go index 74c93b969..f9cde7082 100644 --- a/builder/oracle/classic/step_upload_image.go +++ b/builder/oracle/classic/step_upload_image.go @@ -3,6 +3,7 @@ package classic import ( "context" "fmt" + "log" "strings" "github.com/hashicorp/packer/helper/multistep" @@ -11,7 +12,8 @@ import ( ) type stepUploadImage struct { - uploadImageCommand string + UploadImageCommand string + segmentPath string } type uploadCmdData struct { @@ -30,15 +32,16 @@ func (s *stepUploadImage) Run(_ context.Context, state multistep.StateBag) multi imageFile := fmt.Sprintf("%s.tar.gz", config.ImageName) state.Put("image_file", imageFile) + s.segmentPath = fmt.Sprintf("compute_images_segments/%s/_segment_/%s", imageFile, runID) config.ctx.Data = uploadCmdData{ Username: config.Username, Password: config.Password, AccountID: config.IdentityDomain, ImageFile: imageFile, - SegmentPath: fmt.Sprintf("compute_images_segments/%s/_segment_/%s", imageFile, runID), + SegmentPath: s.segmentPath, } - uploadImageCmd, err := interpolate.Render(s.uploadImageCommand, &config.ctx) + uploadImageCmd, err := interpolate.Render(s.UploadImageCommand, &config.ctx) if err != nil { err := fmt.Errorf("Error processing image upload command: %s", err) state.Put("error", err) @@ -80,4 +83,11 @@ func (s *stepUploadImage) Run(_ context.Context, state multistep.StateBag) multi return multistep.ActionContinue } -func (s *stepUploadImage) Cleanup(state multistep.StateBag) {} +func (s *stepUploadImage) Cleanup(state multistep.StateBag) { + _, cancelled := state.GetOk(multistep.StateCancelled) + _, halted := state.GetOk(multistep.StateHalted) + if !cancelled && !halted { + return + } + log.Printf("Some segments may need to be manually cleaned at '%s'", s.segmentPath) +}