2018-01-12 19:06:03 -05:00
|
|
|
package classic
|
|
|
|
|
|
|
|
import (
|
2018-01-25 17:43:55 -05:00
|
|
|
"context"
|
2018-01-12 19:18:55 -05:00
|
|
|
"fmt"
|
|
|
|
|
2018-01-16 19:55:39 -05:00
|
|
|
"github.com/hashicorp/go-oracle-terraform/compute"
|
2018-01-25 17:45:09 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-12 19:06:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
2018-01-12 19:18:55 -05:00
|
|
|
type stepCreateInstance struct{}
|
2018-01-12 19:06:03 -05:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-01-16 19:55:39 -05:00
|
|
|
// get variables from state
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-01-17 16:07:41 -05:00
|
|
|
ui.Say("Creating Instance...")
|
2018-01-26 16:12:35 -05:00
|
|
|
|
2018-01-17 19:25:38 -05:00
|
|
|
config := state.Get("config").(*Config)
|
2018-10-24 17:08:11 -04:00
|
|
|
client := state.Get("client").(*compute.Client)
|
2018-01-26 16:12:35 -05:00
|
|
|
ipAddName := state.Get("ipres_name").(string)
|
2018-01-22 19:54:09 -05:00
|
|
|
secListName := state.Get("security_list").(string)
|
2018-01-19 18:37:06 -05:00
|
|
|
|
|
|
|
netInfo := compute.NetworkingInfo{
|
|
|
|
Nat: []string{ipAddName},
|
|
|
|
SecLists: []string{secListName},
|
|
|
|
}
|
|
|
|
|
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,
|
2018-01-29 19:50:53 -05:00
|
|
|
ImageList: config.SourceImageList,
|
2018-10-24 17:40:32 -04:00
|
|
|
Entry: config.SourceImageListEntry,
|
2018-01-19 18:37:06 -05:00
|
|
|
Networking: map[string]compute.NetworkingInfo{"eth0": netInfo},
|
2018-02-08 17:12:39 -05:00
|
|
|
Attributes: config.attribs,
|
2018-01-12 19:51:59 -05:00
|
|
|
}
|
2018-02-08 17:55:04 -05:00
|
|
|
if config.Comm.Type == "ssh" {
|
2018-08-29 08:01:55 -04:00
|
|
|
input.SSHKeys = []string{config.Comm.SSHKeyPairName}
|
2018-02-08 17:55:04 -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-02-08 17:55:04 -05:00
|
|
|
state.Put("instance_info", instanceInfo)
|
2018-01-16 19:55:39 -05:00
|
|
|
state.Put("instance_id", instanceInfo.ID)
|
2018-01-26 16:43:19 -05:00
|
|
|
ui.Message(fmt.Sprintf("Created instance: %s.", instanceInfo.ID))
|
2018-01-17 16:07:41 -05:00
|
|
|
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-04-02 14:56:11 -04:00
|
|
|
instanceID, ok := state.GetOk("instance_id")
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-19 19:08:42 -05:00
|
|
|
// terminate instance
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-10-24 17:08:11 -04:00
|
|
|
client := state.Get("client").(*compute.Client)
|
2018-01-19 19:13:36 -05:00
|
|
|
config := state.Get("config").(*Config)
|
2018-01-19 19:08:42 -05:00
|
|
|
|
2018-01-26 16:43:19 -05:00
|
|
|
ui.Say("Terminating source instance...")
|
2018-01-19 19:08:42 -05:00
|
|
|
|
|
|
|
instanceClient := client.Instances()
|
|
|
|
input := &compute.DeleteInstanceInput{
|
|
|
|
Name: config.ImageName,
|
2018-04-02 14:56:11 -04:00
|
|
|
ID: instanceID.(string),
|
2018-01-19 19:08:42 -05:00
|
|
|
}
|
|
|
|
|
2018-01-19 19:13:36 -05:00
|
|
|
err := instanceClient.DeleteInstance(input)
|
2018-01-19 19:08:42 -05:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Problem destroying instance: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ui.Say("Terminated instance.")
|
2018-01-12 19:06:03 -05:00
|
|
|
}
|