From e271e88b49b88460b53ad9ca7041ac47a8ee81b9 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Wed, 5 Sep 2018 15:01:14 -0700 Subject: [PATCH] oracle-classic variable volume size WIP --- builder/oracle/classic/builder.go | 62 +++++++++++-------- builder/oracle/classic/config.go | 5 ++ .../classic/step_create_persistent_volume.go | 57 +++++++++++++++++ 3 files changed, 99 insertions(+), 25 deletions(-) create mode 100644 builder/oracle/classic/step_create_persistent_volume.go diff --git a/builder/oracle/classic/builder.go b/builder/oracle/classic/builder.go index b0bfeef4f..7f8148196 100644 --- a/builder/oracle/classic/builder.go +++ b/builder/oracle/classic/builder.go @@ -60,31 +60,43 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe state.Put("ui", ui) state.Put("client", client) - // Build the steps - steps := []multistep.Step{ - &ocommon.StepKeyPair{ - Debug: b.config.PackerDebug, - Comm: &b.config.Comm, - DebugKeyPath: fmt.Sprintf("oci_classic_%s.pem", b.config.PackerBuildName), - }, - &stepCreateIPReservation{}, - &stepAddKeysToAPI{}, - &stepSecurity{}, - &stepCreateInstance{}, - &communicator.StepConnect{ - Config: &b.config.Comm, - Host: ocommon.CommHost, - SSHConfig: b.config.Comm.SSHConfigFunc(), - }, - &common.StepProvision{}, - &common.StepCleanupTempKeys{ - Comm: &b.config.Comm, - }, - &common.StepCleanupTempKeys{ - Comm: &b.config.Comm, - }, - &stepSnapshot{}, - &stepListImages{}, + var steps []multistep.Step + if b.config.PersistentVolumeSize != "" { + steps = []multistep.Step{ + &stepCreatePersistentVolume{ + volumeSize: b.config.PersistentVolumeSize, + volumeName: b.config.PersistentVolumeName, + latencyStorage: b.config.PersistentVolumeLatencyStorage, + sourceImageList: b.config.SourceImageList, + }, + } + } else { + // Build the steps + steps = []multistep.Step{ + &ocommon.StepKeyPair{ + Debug: b.config.PackerDebug, + Comm: &b.config.Comm, + DebugKeyPath: fmt.Sprintf("oci_classic_%s.pem", b.config.PackerBuildName), + }, + &stepCreateIPReservation{}, + &stepAddKeysToAPI{}, + &stepSecurity{}, + &stepCreateInstance{}, + &communicator.StepConnect{ + Config: &b.config.Comm, + Host: ocommon.CommHost, + SSHConfig: b.config.Comm.SSHConfigFunc(), + }, + &common.StepProvision{}, + &common.StepCleanupTempKeys{ + Comm: &b.config.Comm, + }, + &common.StepCleanupTempKeys{ + Comm: &b.config.Comm, + }, + &stepSnapshot{}, + &stepListImages{}, + } } // Run the steps diff --git a/builder/oracle/classic/config.go b/builder/oracle/classic/config.go index da3a76460..c31d5f4c1 100644 --- a/builder/oracle/classic/config.go +++ b/builder/oracle/classic/config.go @@ -29,6 +29,11 @@ type Config struct { apiEndpointURL *url.URL // Image + // PersistentVolumeSize lets us control the volume size by using persistent boot storage + PersistentVolumeSize string `mapstructure:"persistent_volume_size"` + PersistentVolumeName string `mapstructure:"persistent_volume_name"` + PersistentVolumeLatencyStorage bool `mapstructure:"persistent_volume_latency_storage"` + ImageName string `mapstructure:"image_name"` Shape string `mapstructure:"shape"` SourceImageList string `mapstructure:"source_image_list"` diff --git a/builder/oracle/classic/step_create_persistent_volume.go b/builder/oracle/classic/step_create_persistent_volume.go new file mode 100644 index 000000000..3377b9a22 --- /dev/null +++ b/builder/oracle/classic/step_create_persistent_volume.go @@ -0,0 +1,57 @@ +package classic + +import ( + "context" + "fmt" + + "github.com/hashicorp/go-oracle-terraform/compute" + "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer" +) + +type stepCreatePersistentVolume struct { + volumeSize string + volumeName string + latencyStorage bool + sourceImageList string +} + +func (s *stepCreatePersistentVolume) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { + client := state.Get("client").(*compute.ComputeClient) + + ui := state.Get("ui").(packer.Ui) + ui.Say("Creating Volume...") + + var properties string + if s.latencyStorage { + properties = "/oracle/public/storage/latency" + } else { + properties = "/oracle/public/storage/default" + } + + c := &compute.CreateStorageVolumeInput{ + Name: s.volumeName, + Size: s.volumeSize, + Properties: []string{properties}, + ImageList: s.sourceImageList, + Bootable: true, + } + + sc := client.StorageVolumes() + cc, err := sc.CreateStorageVolume(c) + + if err != nil { + err = fmt.Errorf("Error creating persistent volume: %s", err) + ui.Error(err.Error()) + state.Put("error", err) + return multistep.ActionHalt + } + + //TODO: wait to become available + + ui.Message(fmt.Sprintf("Created volume: %s", cc.Name)) + return multistep.ActionContinue +} + +func (s *stepCreatePersistentVolume) Cleanup(state multistep.StateBag) { +}