packer-cn/builder/oracle/classic/step_create_instance.go

82 lines
2.1 KiB
Go
Raw Normal View History

2018-01-12 19:06:03 -05:00
package classic
import (
2018-01-12 19:18:55 -05:00
"fmt"
"log"
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-22 19:54:09 -05:00
keyName := state.Get("key_name").(string)
ipAddName := fmt.Sprintf("ipres_%s", config.ImageName)
2018-01-22 19:54:09 -05:00
secListName := state.Get("security_list").(string)
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{
Name: config.ImageName,
Shape: config.Shape,
ImageList: config.ImageList,
2018-01-22 19:54:09 -05:00
SSHKeys: []string{keyName},
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)
2018-01-19 19:13:36 -05:00
config := state.Get("config").(*Config)
2018-01-19 19:08:42 -05:00
imID := state.Get("instance_id").(string)
2018-01-19 19:13:36 -05:00
ui.Say(fmt.Sprintf("Terminating instance (%s)...", imID))
2018-01-19 19:08:42 -05:00
instanceClient := client.Instances()
input := &compute.DeleteInstanceInput{
Name: config.ImageName,
ID: imID,
}
2018-01-22 19:54:09 -05:00
log.Printf("instance destroy input is %#v", input)
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
}
// TODO wait for instance state to change to deleted?
ui.Say("Terminated instance.")
return
2018-01-12 19:06:03 -05:00
}