github.phpd.cn/hashicorp/packer@v1.3.2/builder/oracle/classic/step_create_instance.go (about) 1 package classic 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/hashicorp/go-oracle-terraform/compute" 8 "github.com/hashicorp/packer/helper/multistep" 9 "github.com/hashicorp/packer/packer" 10 ) 11 12 type stepCreateInstance struct{} 13 14 func (s *stepCreateInstance) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 15 // get variables from state 16 ui := state.Get("ui").(packer.Ui) 17 ui.Say("Creating Instance...") 18 19 config := state.Get("config").(*Config) 20 client := state.Get("client").(*compute.ComputeClient) 21 ipAddName := state.Get("ipres_name").(string) 22 secListName := state.Get("security_list").(string) 23 24 netInfo := compute.NetworkingInfo{ 25 Nat: []string{ipAddName}, 26 SecLists: []string{secListName}, 27 } 28 29 // get instances client 30 instanceClient := client.Instances() 31 32 // Instances Input 33 input := &compute.CreateInstanceInput{ 34 Name: config.ImageName, 35 Shape: config.Shape, 36 ImageList: config.SourceImageList, 37 Networking: map[string]compute.NetworkingInfo{"eth0": netInfo}, 38 Attributes: config.attribs, 39 } 40 if config.Comm.Type == "ssh" { 41 input.SSHKeys = []string{config.Comm.SSHKeyPairName} 42 } 43 44 instanceInfo, err := instanceClient.CreateInstance(input) 45 if err != nil { 46 err = fmt.Errorf("Problem creating instance: %s", err) 47 ui.Error(err.Error()) 48 state.Put("error", err) 49 return multistep.ActionHalt 50 } 51 52 state.Put("instance_info", instanceInfo) 53 state.Put("instance_id", instanceInfo.ID) 54 ui.Message(fmt.Sprintf("Created instance: %s.", instanceInfo.ID)) 55 return multistep.ActionContinue 56 } 57 58 func (s *stepCreateInstance) Cleanup(state multistep.StateBag) { 59 instanceID, ok := state.GetOk("instance_id") 60 if !ok { 61 return 62 } 63 64 // terminate instance 65 ui := state.Get("ui").(packer.Ui) 66 client := state.Get("client").(*compute.ComputeClient) 67 config := state.Get("config").(*Config) 68 69 ui.Say("Terminating source instance...") 70 71 instanceClient := client.Instances() 72 input := &compute.DeleteInstanceInput{ 73 Name: config.ImageName, 74 ID: instanceID.(string), 75 } 76 77 err := instanceClient.DeleteInstance(input) 78 if err != nil { 79 err = fmt.Errorf("Problem destroying instance: %s", err) 80 ui.Error(err.Error()) 81 state.Put("error", err) 82 return 83 } 84 // TODO wait for instance state to change to deleted? 85 ui.Say("Terminated instance.") 86 }