diff --git a/builder/oracle/classic/builder.go b/builder/oracle/classic/builder.go index 7f8148196..6d21dd117 100644 --- a/builder/oracle/classic/builder.go +++ b/builder/oracle/classic/builder.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/go-oracle-terraform/opc" ocommon "github.com/hashicorp/packer/builder/oracle/common" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/uuid" "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" @@ -31,6 +32,16 @@ func (b *Builder) Prepare(rawConfig ...interface{}) ([]string, error) { } b.config = config + var errs *packer.MultiError + + if b.config.PersistentVolumeSize > 0 && b.config.Comm.Type != "ssh" { + errs = packer.MultiErrorAppend(errs, + fmt.Errorf("Persistent storage volumes are only supported on unix, and must use the ssh communicator.")) + } + + if errs != nil && len(errs.Errors) > 0 { + return nil, errs + } return nil, nil } @@ -59,16 +70,52 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe state.Put("hook", hook) state.Put("ui", ui) state.Put("client", client) + runID := uuid.TimeOrderedUUID() var steps []multistep.Step - if b.config.PersistentVolumeSize != "" { + if b.config.PersistentVolumeSize > 0 { steps = []multistep.Step{ + // TODO: make volume names UUIDs &stepCreatePersistentVolume{ - volumeSize: b.config.PersistentVolumeSize, - volumeName: b.config.PersistentVolumeName, - latencyStorage: b.config.PersistentVolumeLatencyStorage, + volumeSize: fmt.Sprintf("%d", b.config.PersistentVolumeSize), + volumeName: fmt.Sprintf("master-storage_%s", runID), sourceImageList: b.config.SourceImageList, + bootable: true, }, + &stepCreatePersistentVolume{ + volumeSize: fmt.Sprintf("%d", b.config.PersistentVolumeSize*2), + volumeName: fmt.Sprintf("builder-storage_%s", runID), + }, + &ocommon.StepKeyPair{ + Debug: b.config.PackerDebug, + Comm: &b.config.Comm, + DebugKeyPath: fmt.Sprintf("oci_classic_%s.pem", b.config.PackerBuildName), + }, + &stepCreateIPReservation{}, + &stepAddKeysToAPI{}, + &stepSecurity{}, + &stepCreatePVMaster{ + name: fmt.Sprintf("master-instance_%s", runID), + volumeName: fmt.Sprintf("master-storage_%s", runID), + }, + &communicator.StepConnect{ + Config: &b.config.Comm, + Host: ocommon.CommHost, + SSHConfig: b.config.Comm.SSHConfigFunc(), + }, + &common.StepProvision{}, + &stepTerminatePVMaster{}, + &stepCreatePVBuilder{ + name: fmt.Sprintf("builder-instance_%s", runID), + masterVolumeName: fmt.Sprintf("master-storage_%s", runID), + builderVolumeName: fmt.Sprintf("builder-storage_%s", runID), + }, + &communicator.StepConnect{ + Config: &b.config.Comm, + Host: ocommon.CommHost, + SSHConfig: b.config.Comm.SSHConfigFunc(), + }, + &stepCreateImage{}, } } else { // Build the steps diff --git a/builder/oracle/classic/config.go b/builder/oracle/classic/config.go index c31d5f4c1..b15ea0de2 100644 --- a/builder/oracle/classic/config.go +++ b/builder/oracle/classic/config.go @@ -30,9 +30,7 @@ type Config struct { // 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"` + PersistentVolumeSize int `mapstructure:"persistent_volume_size"` ImageName string `mapstructure:"image_name"` Shape string `mapstructure:"shape"` @@ -54,6 +52,10 @@ type Config struct { ctx interpolate.Context } +func (c *Config) Identifier(s string) string { + return fmt.Sprintf("/Compute-%s/%s/%s", c.IdentityDomain, c.Username, s) +} + func NewConfig(raws ...interface{}) (*Config, error) { c := &Config{} diff --git a/builder/oracle/classic/step_add_keys.go b/builder/oracle/classic/step_add_keys.go index f1f14df80..739a2c790 100644 --- a/builder/oracle/classic/step_add_keys.go +++ b/builder/oracle/classic/step_add_keys.go @@ -28,8 +28,7 @@ func (s *stepAddKeysToAPI) Run(_ context.Context, state multistep.StateBag) mult sshPublicKey := bytes.TrimSpace(config.Comm.SSHPublicKey) // form API call to add key to compute cloud - sshKeyName := fmt.Sprintf("/Compute-%s/%s/packer_generated_key_%s", - config.IdentityDomain, config.Username, uuid.TimeOrderedUUID()) + sshKeyName := config.Identifier(fmt.Sprintf("packer_generated_key_%s", uuid.TimeOrderedUUID())) ui.Say(fmt.Sprintf("Creating temporary key: %s", sshKeyName)) diff --git a/builder/oracle/classic/step_create_image.go b/builder/oracle/classic/step_create_image.go new file mode 100644 index 000000000..13375c47b --- /dev/null +++ b/builder/oracle/classic/step_create_image.go @@ -0,0 +1,45 @@ +package classic + +import ( + "context" + + "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer" +) + +type stepCreateImage struct{} + +func (s *stepCreateImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { + //hook := state.Get("hook").(packer.Hook) + ui := state.Get("ui").(packer.Ui) + comm := state.Get("communicator").(packer.Communicator) + commands := []string{ + "mkdir ./builder", + "sudo mkfs -t ext3 /dev/xvdb", + "sudo mount /dev/xvdb ./builder", + "sudo chown opc:opc ./builder", + "cd ./builder", + "sudo dd if=/dev/xvdc bs=8M status=progress | cp --sparse=always /dev/stdin diskimage.raw", + "tar czSf ./diskimage.tar.gz ./diskimage.raw", + } + for _, c := range commands { + cmd := packer.RemoteCmd{ + Command: c, + } + cmd.StartWithUi(comm, ui) + } + // comm.Start(" + + /* + // Provision + log.Println("Running the provision hook") + if err := hook.Run(packer.HookProvision, ui, comm, nil); err != nil { + state.Put("error", err) + return multistep.ActionHalt + } + */ + + return multistep.ActionContinue +} + +func (s *stepCreateImage) Cleanup(state multistep.StateBag) {} diff --git a/builder/oracle/classic/step_create_ip_reservation.go b/builder/oracle/classic/step_create_ip_reservation.go index 5d400e746..82a447aab 100644 --- a/builder/oracle/classic/step_create_ip_reservation.go +++ b/builder/oracle/classic/step_create_ip_reservation.go @@ -36,6 +36,7 @@ func (s *stepCreateIPReservation) Run(_ context.Context, state multistep.StateBa ui.Error(err.Error()) return multistep.ActionHalt } + // TODO: state key prefixes for multiple hosts state.Put("instance_ip", ipRes.IP) state.Put("ipres_name", ipresName) return multistep.ActionContinue diff --git a/builder/oracle/classic/step_create_persistent_volume.go b/builder/oracle/classic/step_create_persistent_volume.go index 3377b9a22..3d2ba7d65 100644 --- a/builder/oracle/classic/step_create_persistent_volume.go +++ b/builder/oracle/classic/step_create_persistent_volume.go @@ -12,7 +12,7 @@ import ( type stepCreatePersistentVolume struct { volumeSize string volumeName string - latencyStorage bool + bootable bool sourceImageList string } @@ -22,26 +22,19 @@ func (s *stepCreatePersistentVolume) Run(_ context.Context, state multistep.Stat 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, + Properties: []string{"/oracle/public/storage/default"}, + Bootable: s.bootable, } sc := client.StorageVolumes() cc, err := sc.CreateStorageVolume(c) if err != nil { - err = fmt.Errorf("Error creating persistent volume: %s", err) + err = fmt.Errorf("Error creating persistent storage volume: %s", err) ui.Error(err.Error()) state.Put("error", err) return multistep.ActionHalt @@ -54,4 +47,27 @@ func (s *stepCreatePersistentVolume) Run(_ context.Context, state multistep.Stat } func (s *stepCreatePersistentVolume) Cleanup(state multistep.StateBag) { + _, cancelled := state.GetOk(multistep.StateCancelled) + _, halted := state.GetOk(multistep.StateHalted) + if !cancelled && !halted { + return + } + + client := state.Get("client").(*compute.ComputeClient) + + ui := state.Get("ui").(packer.Ui) + ui.Say("Cleaning up Volume...") + + c := &compute.DeleteStorageVolumeInput{ + Name: s.volumeName, + } + + sc := client.StorageVolumes() + + if err := sc.DeleteStorageVolume(c); err != nil { + ui.Error(fmt.Sprintf("Error cleaning up persistent storage volume: %s", err)) + return + } + + ui.Message(fmt.Sprintf("Deleted volume: %s", s.volumeName)) } diff --git a/builder/oracle/classic/step_create_pv_builder.go b/builder/oracle/classic/step_create_pv_builder.go new file mode 100644 index 000000000..27348a077 --- /dev/null +++ b/builder/oracle/classic/step_create_pv_builder.go @@ -0,0 +1,98 @@ +package classic + +import ( + "context" + "fmt" + + "github.com/hashicorp/go-oracle-terraform/compute" + "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer" +) + +type stepCreatePVBuilder struct { + name string + masterVolumeName string + builderVolumeName string +} + +func (s *stepCreatePVBuilder) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { + // get variables from state + ui := state.Get("ui").(packer.Ui) + ui.Say("Creating builder instance...") + + config := state.Get("config").(*Config) + client := state.Get("client").(*compute.ComputeClient) + ipAddName := state.Get("ipres_name").(string) + secListName := state.Get("security_list").(string) + + // get instances client + instanceClient := client.Instances() + + // Instances Input + input := &compute.CreateInstanceInput{ + Name: s.name, + Shape: config.Shape, + Networking: map[string]compute.NetworkingInfo{ + "eth0": compute.NetworkingInfo{ + Nat: []string{ipAddName}, + SecLists: []string{secListName}, + }, + }, + Storage: []compute.StorageAttachmentInput{ + { + Volume: s.builderVolumeName, + Index: 1, + }, + { + Volume: s.masterVolumeName, + Index: 2, + }, + }, + ImageList: config.SourceImageList, + Attributes: config.attribs, + SSHKeys: []string{config.Comm.SSHKeyPairName}, + } + + instanceInfo, err := instanceClient.CreateInstance(input) + if err != nil { + err = fmt.Errorf("Problem creating instance: %s", err) + ui.Error(err.Error()) + state.Put("error", err) + return multistep.ActionHalt + } + + state.Put("builder_instance_info", instanceInfo) + state.Put("builder_instance_id", instanceInfo.ID) + ui.Message(fmt.Sprintf("Created builder instance: %s.", instanceInfo.ID)) + return multistep.ActionContinue +} + +func (s *stepCreatePVBuilder) Cleanup(state multistep.StateBag) { + instanceID, ok := state.GetOk("builder_instance_id") + if !ok { + return + } + + // terminate instance + ui := state.Get("ui").(packer.Ui) + client := state.Get("client").(*compute.ComputeClient) + config := state.Get("config").(*Config) + + ui.Say("Terminating builder instance...") + + instanceClient := client.Instances() + input := &compute.DeleteInstanceInput{ + Name: config.ImageName, + ID: instanceID.(string), + } + + err := instanceClient.DeleteInstance(input) + if err != nil { + err = fmt.Errorf("Problem destroying instance: %s", err) + ui.Error(err.Error()) + state.Put("error", err) + return + } + // TODO wait for instance state to change to deleted? + ui.Say("Terminated builder instance.") +} diff --git a/builder/oracle/classic/step_create_pv_master.go b/builder/oracle/classic/step_create_pv_master.go new file mode 100644 index 000000000..57390253e --- /dev/null +++ b/builder/oracle/classic/step_create_pv_master.go @@ -0,0 +1,66 @@ +package classic + +import ( + "context" + "fmt" + + "github.com/hashicorp/go-oracle-terraform/compute" + "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer" +) + +type stepCreatePVMaster struct { + name string + volumeName string +} + +func (s *stepCreatePVMaster) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { + // get variables from state + ui := state.Get("ui").(packer.Ui) + ui.Say("Creating master instance...") + + config := state.Get("config").(*Config) + client := state.Get("client").(*compute.ComputeClient) + ipAddName := state.Get("ipres_name").(string) + secListName := state.Get("security_list").(string) + + // get instances client + instanceClient := client.Instances() + + // Instances Input + input := &compute.CreateInstanceInput{ + Name: s.name, + Shape: config.Shape, + Networking: map[string]compute.NetworkingInfo{ + "eth0": compute.NetworkingInfo{ + Nat: []string{ipAddName}, + SecLists: []string{secListName}, + }, + }, + Storage: []compute.StorageAttachmentInput{ + { + Volume: s.volumeName, + Index: 1, + }, + }, + BootOrder: []int{1}, + Attributes: config.attribs, + SSHKeys: []string{config.Comm.SSHKeyPairName}, + } + + instanceInfo, err := instanceClient.CreateInstance(input) + if err != nil { + err = fmt.Errorf("Problem creating instance: %s", err) + ui.Error(err.Error()) + state.Put("error", err) + return multistep.ActionHalt + } + + state.Put("master_instance_info", instanceInfo) + state.Put("master_instance_id", instanceInfo.ID) + ui.Message(fmt.Sprintf("Created master instance: %s.", instanceInfo.ID)) + return multistep.ActionContinue +} + +func (s *stepCreatePVMaster) Cleanup(state multistep.StateBag) { +} diff --git a/builder/oracle/classic/step_list_images.go b/builder/oracle/classic/step_list_images.go index a99612c14..1326bb480 100644 --- a/builder/oracle/classic/step_list_images.go +++ b/builder/oracle/classic/step_list_images.go @@ -49,10 +49,9 @@ func (s *stepListImages) Run(_ context.Context, state multistep.StateBag) multis version := len(imList.Entries) + 1 entriesClient := client.ImageListEntries() entriesInput := compute.CreateImageListEntryInput{ - Name: config.DestImageList, - MachineImages: []string{fmt.Sprintf("/Compute-%s/%s/%s", - config.IdentityDomain, config.Username, snap.MachineImage)}, - Version: version, + Name: config.DestImageList, + MachineImages: []string{config.Identifier(snap.MachineImage)}, + Version: version, } entryInfo, err := entriesClient.CreateImageListEntry(&entriesInput) if err != nil { diff --git a/builder/oracle/classic/step_security.go b/builder/oracle/classic/step_security.go index a5b04418b..f44702992 100644 --- a/builder/oracle/classic/step_security.go +++ b/builder/oracle/classic/step_security.go @@ -29,12 +29,11 @@ func (s *stepSecurity) Run(_ context.Context, state multistep.StateBag) multiste client := state.Get("client").(*compute.ComputeClient) runUUID := uuid.TimeOrderedUUID() - namePrefix := fmt.Sprintf("/Compute-%s/%s/", config.IdentityDomain, config.Username) secListName := fmt.Sprintf("Packer_%s_Allow_%s_%s", commType, config.ImageName, runUUID) secListClient := client.SecurityLists() secListInput := compute.CreateSecurityListInput{ Description: fmt.Sprintf("Packer-generated security list to give packer %s access", commType), - Name: namePrefix + secListName, + Name: config.Identifier(secListName), } _, err := secListClient.CreateSecurityList(&secListInput) if err != nil { @@ -78,8 +77,8 @@ func (s *stepSecurity) Run(_ context.Context, state multistep.StateBag) multiste Action: "PERMIT", Application: application, Description: "Packer-generated security rule to allow ssh/winrm", - DestinationList: "seclist:" + namePrefix + secListName, - Name: namePrefix + secRuleName, + DestinationList: "seclist:" + config.Identifier(secListName), + Name: config.Identifier(secRuleName), SourceList: config.SSHSourceList, } @@ -112,10 +111,9 @@ func (s *stepSecurity) Cleanup(state multistep.StateBag) { ui.Say("Deleting temporary rules and lists...") - namePrefix := fmt.Sprintf("/Compute-%s/%s/", config.IdentityDomain, config.Username) // delete security rules that Packer generated secRulesClient := client.SecRules() - ruleInput := compute.DeleteSecRuleInput{Name: namePrefix + secRuleName.(string)} + ruleInput := compute.DeleteSecRuleInput{Name: config.Identifier(secRuleName.(string))} err := secRulesClient.DeleteSecRule(&ruleInput) if err != nil { ui.Say(fmt.Sprintf("Error deleting the packer-generated security rule %s; "+ @@ -124,7 +122,7 @@ func (s *stepSecurity) Cleanup(state multistep.StateBag) { // delete security list that Packer generated secListClient := client.SecurityLists() - input := compute.DeleteSecurityListInput{Name: namePrefix + secListName.(string)} + input := compute.DeleteSecurityListInput{Name: config.Identifier(secListName.(string))} err = secListClient.DeleteSecurityList(&input) if err != nil { ui.Say(fmt.Sprintf("Error deleting the packer-generated security list %s; "+ @@ -140,7 +138,7 @@ func (s *stepSecurity) Cleanup(state multistep.StateBag) { } applicationClient := client.SecurityApplications() deleteApplicationInput := compute.DeleteSecurityApplicationInput{ - Name: namePrefix + application.(string), + Name: config.Identifier(application.(string)), } err = applicationClient.DeleteSecurityApplication(&deleteApplicationInput) if err != nil { diff --git a/builder/oracle/classic/step_terminate_pv_master.go b/builder/oracle/classic/step_terminate_pv_master.go new file mode 100644 index 000000000..59d4cbc99 --- /dev/null +++ b/builder/oracle/classic/step_terminate_pv_master.go @@ -0,0 +1,45 @@ +package classic + +import ( + "context" + "fmt" + + "github.com/hashicorp/go-oracle-terraform/compute" + "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer" +) + +type stepTerminatePVMaster struct { +} + +func (s *stepTerminatePVMaster) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { + // get variables from state + ui := state.Get("ui").(packer.Ui) + ui.Say("Deleting master Instance...") + + client := state.Get("client").(*compute.ComputeClient) + instanceInfo := state.Get("master_instance_info").(*compute.InstanceInfo) + + // get instances client + instanceClient := client.Instances() + + // Instances Input + input := &compute.DeleteInstanceInput{ + Name: instanceInfo.Name, + ID: instanceInfo.ID, + } + + err := instanceClient.DeleteInstance(input) + if err != nil { + err = fmt.Errorf("Problem creating instance: %s", err) + ui.Error(err.Error()) + state.Put("error", err) + return multistep.ActionHalt + } + + ui.Message(fmt.Sprintf("Deleted master instance: %s.", instanceInfo.ID)) + return multistep.ActionContinue +} + +func (s *stepTerminatePVMaster) Cleanup(state multistep.StateBag) { +}