master instance working

This commit is contained in:
Matthew Hooker 2018-09-05 22:44:31 -07:00
parent e271e88b49
commit 00ff187d77
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
11 changed files with 348 additions and 32 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/hashicorp/go-oracle-terraform/opc" "github.com/hashicorp/go-oracle-terraform/opc"
ocommon "github.com/hashicorp/packer/builder/oracle/common" ocommon "github.com/hashicorp/packer/builder/oracle/common"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/common/uuid"
"github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
@ -31,6 +32,16 @@ func (b *Builder) Prepare(rawConfig ...interface{}) ([]string, error) {
} }
b.config = config 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 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("hook", hook)
state.Put("ui", ui) state.Put("ui", ui)
state.Put("client", client) state.Put("client", client)
runID := uuid.TimeOrderedUUID()
var steps []multistep.Step var steps []multistep.Step
if b.config.PersistentVolumeSize != "" { if b.config.PersistentVolumeSize > 0 {
steps = []multistep.Step{ steps = []multistep.Step{
// TODO: make volume names UUIDs
&stepCreatePersistentVolume{ &stepCreatePersistentVolume{
volumeSize: b.config.PersistentVolumeSize, volumeSize: fmt.Sprintf("%d", b.config.PersistentVolumeSize),
volumeName: b.config.PersistentVolumeName, volumeName: fmt.Sprintf("master-storage_%s", runID),
latencyStorage: b.config.PersistentVolumeLatencyStorage,
sourceImageList: b.config.SourceImageList, 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 { } else {
// Build the steps // Build the steps

View File

@ -30,9 +30,7 @@ type Config struct {
// Image // Image
// PersistentVolumeSize lets us control the volume size by using persistent boot storage // PersistentVolumeSize lets us control the volume size by using persistent boot storage
PersistentVolumeSize string `mapstructure:"persistent_volume_size"` PersistentVolumeSize int `mapstructure:"persistent_volume_size"`
PersistentVolumeName string `mapstructure:"persistent_volume_name"`
PersistentVolumeLatencyStorage bool `mapstructure:"persistent_volume_latency_storage"`
ImageName string `mapstructure:"image_name"` ImageName string `mapstructure:"image_name"`
Shape string `mapstructure:"shape"` Shape string `mapstructure:"shape"`
@ -54,6 +52,10 @@ type Config struct {
ctx interpolate.Context 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) { func NewConfig(raws ...interface{}) (*Config, error) {
c := &Config{} c := &Config{}

View File

@ -28,8 +28,7 @@ func (s *stepAddKeysToAPI) Run(_ context.Context, state multistep.StateBag) mult
sshPublicKey := bytes.TrimSpace(config.Comm.SSHPublicKey) sshPublicKey := bytes.TrimSpace(config.Comm.SSHPublicKey)
// form API call to add key to compute cloud // form API call to add key to compute cloud
sshKeyName := fmt.Sprintf("/Compute-%s/%s/packer_generated_key_%s", sshKeyName := config.Identifier(fmt.Sprintf("packer_generated_key_%s", uuid.TimeOrderedUUID()))
config.IdentityDomain, config.Username, uuid.TimeOrderedUUID())
ui.Say(fmt.Sprintf("Creating temporary key: %s", sshKeyName)) ui.Say(fmt.Sprintf("Creating temporary key: %s", sshKeyName))

View File

@ -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) {}

View File

@ -36,6 +36,7 @@ func (s *stepCreateIPReservation) Run(_ context.Context, state multistep.StateBa
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
// TODO: state key prefixes for multiple hosts
state.Put("instance_ip", ipRes.IP) state.Put("instance_ip", ipRes.IP)
state.Put("ipres_name", ipresName) state.Put("ipres_name", ipresName)
return multistep.ActionContinue return multistep.ActionContinue

View File

@ -12,7 +12,7 @@ import (
type stepCreatePersistentVolume struct { type stepCreatePersistentVolume struct {
volumeSize string volumeSize string
volumeName string volumeName string
latencyStorage bool bootable bool
sourceImageList string sourceImageList string
} }
@ -22,26 +22,19 @@ func (s *stepCreatePersistentVolume) Run(_ context.Context, state multistep.Stat
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say("Creating Volume...") ui.Say("Creating Volume...")
var properties string
if s.latencyStorage {
properties = "/oracle/public/storage/latency"
} else {
properties = "/oracle/public/storage/default"
}
c := &compute.CreateStorageVolumeInput{ c := &compute.CreateStorageVolumeInput{
Name: s.volumeName, Name: s.volumeName,
Size: s.volumeSize, Size: s.volumeSize,
Properties: []string{properties},
ImageList: s.sourceImageList, ImageList: s.sourceImageList,
Bootable: true, Properties: []string{"/oracle/public/storage/default"},
Bootable: s.bootable,
} }
sc := client.StorageVolumes() sc := client.StorageVolumes()
cc, err := sc.CreateStorageVolume(c) cc, err := sc.CreateStorageVolume(c)
if err != nil { 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()) ui.Error(err.Error())
state.Put("error", err) state.Put("error", err)
return multistep.ActionHalt return multistep.ActionHalt
@ -54,4 +47,27 @@ func (s *stepCreatePersistentVolume) Run(_ context.Context, state multistep.Stat
} }
func (s *stepCreatePersistentVolume) Cleanup(state multistep.StateBag) { 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))
} }

View File

@ -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.")
}

View File

@ -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) {
}

View File

@ -50,8 +50,7 @@ func (s *stepListImages) Run(_ context.Context, state multistep.StateBag) multis
entriesClient := client.ImageListEntries() entriesClient := client.ImageListEntries()
entriesInput := compute.CreateImageListEntryInput{ entriesInput := compute.CreateImageListEntryInput{
Name: config.DestImageList, Name: config.DestImageList,
MachineImages: []string{fmt.Sprintf("/Compute-%s/%s/%s", MachineImages: []string{config.Identifier(snap.MachineImage)},
config.IdentityDomain, config.Username, snap.MachineImage)},
Version: version, Version: version,
} }
entryInfo, err := entriesClient.CreateImageListEntry(&entriesInput) entryInfo, err := entriesClient.CreateImageListEntry(&entriesInput)

View File

@ -29,12 +29,11 @@ func (s *stepSecurity) Run(_ context.Context, state multistep.StateBag) multiste
client := state.Get("client").(*compute.ComputeClient) client := state.Get("client").(*compute.ComputeClient)
runUUID := uuid.TimeOrderedUUID() 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) secListName := fmt.Sprintf("Packer_%s_Allow_%s_%s", commType, config.ImageName, runUUID)
secListClient := client.SecurityLists() secListClient := client.SecurityLists()
secListInput := compute.CreateSecurityListInput{ secListInput := compute.CreateSecurityListInput{
Description: fmt.Sprintf("Packer-generated security list to give packer %s access", commType), Description: fmt.Sprintf("Packer-generated security list to give packer %s access", commType),
Name: namePrefix + secListName, Name: config.Identifier(secListName),
} }
_, err := secListClient.CreateSecurityList(&secListInput) _, err := secListClient.CreateSecurityList(&secListInput)
if err != nil { if err != nil {
@ -78,8 +77,8 @@ func (s *stepSecurity) Run(_ context.Context, state multistep.StateBag) multiste
Action: "PERMIT", Action: "PERMIT",
Application: application, Application: application,
Description: "Packer-generated security rule to allow ssh/winrm", Description: "Packer-generated security rule to allow ssh/winrm",
DestinationList: "seclist:" + namePrefix + secListName, DestinationList: "seclist:" + config.Identifier(secListName),
Name: namePrefix + secRuleName, Name: config.Identifier(secRuleName),
SourceList: config.SSHSourceList, SourceList: config.SSHSourceList,
} }
@ -112,10 +111,9 @@ func (s *stepSecurity) Cleanup(state multistep.StateBag) {
ui.Say("Deleting temporary rules and lists...") ui.Say("Deleting temporary rules and lists...")
namePrefix := fmt.Sprintf("/Compute-%s/%s/", config.IdentityDomain, config.Username)
// delete security rules that Packer generated // delete security rules that Packer generated
secRulesClient := client.SecRules() secRulesClient := client.SecRules()
ruleInput := compute.DeleteSecRuleInput{Name: namePrefix + secRuleName.(string)} ruleInput := compute.DeleteSecRuleInput{Name: config.Identifier(secRuleName.(string))}
err := secRulesClient.DeleteSecRule(&ruleInput) err := secRulesClient.DeleteSecRule(&ruleInput)
if err != nil { if err != nil {
ui.Say(fmt.Sprintf("Error deleting the packer-generated security rule %s; "+ 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 // delete security list that Packer generated
secListClient := client.SecurityLists() secListClient := client.SecurityLists()
input := compute.DeleteSecurityListInput{Name: namePrefix + secListName.(string)} input := compute.DeleteSecurityListInput{Name: config.Identifier(secListName.(string))}
err = secListClient.DeleteSecurityList(&input) err = secListClient.DeleteSecurityList(&input)
if err != nil { if err != nil {
ui.Say(fmt.Sprintf("Error deleting the packer-generated security list %s; "+ 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() applicationClient := client.SecurityApplications()
deleteApplicationInput := compute.DeleteSecurityApplicationInput{ deleteApplicationInput := compute.DeleteSecurityApplicationInput{
Name: namePrefix + application.(string), Name: config.Identifier(application.(string)),
} }
err = applicationClient.DeleteSecurityApplication(&deleteApplicationInput) err = applicationClient.DeleteSecurityApplication(&deleteApplicationInput)
if err != nil { if err != nil {

View File

@ -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) {
}