2018-01-12 19:06:03 -05:00
|
|
|
package classic
|
|
|
|
|
|
|
|
import (
|
2018-01-12 19:18:55 -05:00
|
|
|
"fmt"
|
2018-01-19 18:37:06 -05:00
|
|
|
"log"
|
2018-01-18 16:44:00 -05:00
|
|
|
"strings"
|
2018-01-12 19:18:55 -05:00
|
|
|
|
2018-01-16 19:55:39 -05:00
|
|
|
"github.com/hashicorp/go-oracle-terraform/compute"
|
2018-01-12 19:06:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
2018-01-12 19:18:55 -05:00
|
|
|
type stepCreateInstance struct{}
|
2018-01-12 19:06:03 -05:00
|
|
|
|
2018-01-16 19:55:39 -05:00
|
|
|
func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
// get variables from state
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-01-17 16:07:41 -05:00
|
|
|
ui.Say("Creating Instance...")
|
2018-01-17 19:25:38 -05:00
|
|
|
config := state.Get("config").(*Config)
|
2018-01-16 19:55:39 -05:00
|
|
|
client := state.Get("client").(*compute.ComputeClient)
|
2018-01-19 18:37:06 -05:00
|
|
|
|
|
|
|
// SSH KEY CONFIGURATION
|
|
|
|
|
|
|
|
// grab packer-generated key from statebag context.
|
2018-01-18 16:44:00 -05:00
|
|
|
sshPublicKey := strings.TrimSpace(state.Get("publicKey").(string))
|
|
|
|
|
2018-01-19 18:37:06 -05:00
|
|
|
// form API call to add key to compute cloud
|
2018-01-18 18:52:31 -05:00
|
|
|
sshKeyName := fmt.Sprintf("/Compute-%s/%s/packer_generated_key", config.IdentityDomain, config.Username)
|
2018-01-18 16:44:00 -05:00
|
|
|
|
|
|
|
sshKeysClient := client.SSHKeys()
|
|
|
|
sshKeysInput := compute.CreateSSHKeyInput{
|
|
|
|
Name: sshKeyName,
|
|
|
|
Key: sshPublicKey,
|
|
|
|
Enabled: true,
|
|
|
|
}
|
2018-01-19 18:37:06 -05:00
|
|
|
|
|
|
|
// Load the packer-generated SSH key into the Oracle Compute cloud.
|
2018-01-18 16:44:00 -05:00
|
|
|
keyInfo, err := sshKeysClient.CreateSSHKey(&sshKeysInput)
|
|
|
|
if err != nil {
|
2018-01-19 18:37:06 -05:00
|
|
|
// Key already exists; update key instead of creating it
|
2018-01-18 18:52:31 -05:00
|
|
|
if strings.Contains(err.Error(), "packer_generated_key already exists") {
|
2018-01-18 16:44:00 -05:00
|
|
|
updateKeysInput := compute.UpdateSSHKeyInput{
|
|
|
|
Name: sshKeyName,
|
|
|
|
Key: sshPublicKey,
|
|
|
|
Enabled: true,
|
|
|
|
}
|
|
|
|
keyInfo, err = sshKeysClient.UpdateSSHKey(&updateKeysInput)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Problem adding Public SSH key through Oracle's API: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 19:55:39 -05:00
|
|
|
|
2018-01-19 18:37:06 -05:00
|
|
|
// NETWORKING INFO CONFIGURATION
|
|
|
|
ipAddName := fmt.Sprintf("ipres_%s", config.ImageName)
|
|
|
|
log.Printf("MEGAN ipADDName is %s", ipAddName)
|
|
|
|
secListName := "Megan_packer_test"
|
|
|
|
|
|
|
|
netInfo := compute.NetworkingInfo{
|
|
|
|
Nat: []string{ipAddName},
|
|
|
|
SecLists: []string{secListName},
|
|
|
|
}
|
|
|
|
fmt.Sprintf("Megan netInfo is %#v", netInfo)
|
|
|
|
|
|
|
|
// INSTANCE LAUNCH
|
|
|
|
|
2018-01-16 19:55:39 -05:00
|
|
|
// get instances client
|
|
|
|
instanceClient := client.Instances()
|
|
|
|
|
|
|
|
// Instances Input
|
|
|
|
input := &compute.CreateInstanceInput{
|
2018-01-19 18:37:06 -05:00
|
|
|
Name: config.ImageName,
|
|
|
|
Shape: config.Shape,
|
|
|
|
ImageList: config.ImageList,
|
|
|
|
SSHKeys: []string{keyInfo.Name},
|
|
|
|
Networking: map[string]compute.NetworkingInfo{"eth0": netInfo},
|
2018-01-12 19:51:59 -05:00
|
|
|
}
|
2018-01-16 19:55:39 -05:00
|
|
|
|
|
|
|
instanceInfo, err := instanceClient.CreateInstance(input)
|
2018-01-12 19:18:55 -05:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Problem creating instance: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-01-16 19:55:39 -05:00
|
|
|
state.Put("instance_id", instanceInfo.ID)
|
2018-01-17 16:07:41 -05:00
|
|
|
ui.Say(fmt.Sprintf("Created instance (%s).", instanceInfo.ID))
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2018-01-12 19:18:55 -05:00
|
|
|
|
2018-01-17 16:07:41 -05:00
|
|
|
func (s *stepCreateInstance) Cleanup(state multistep.StateBag) {
|
2018-01-19 19:08:42 -05:00
|
|
|
// terminate instance
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
client := state.Get("client").(*compute.ComputeClient)
|
|
|
|
imID := state.Get("instance_id").(string)
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Terminating instance (%s)...", id))
|
|
|
|
|
|
|
|
instanceClient := client.Instances()
|
|
|
|
// Instances Input
|
|
|
|
input := &compute.DeleteInstanceInput{
|
|
|
|
Name: config.ImageName,
|
|
|
|
ID: imID,
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceInfo, 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 instance.")
|
|
|
|
return
|
2018-01-12 19:06:03 -05:00
|
|
|
}
|