github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/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  		keyName := state.Get("key_name").(string)
    42  		input.SSHKeys = []string{keyName}
    43  	}
    44  
    45  	instanceInfo, err := instanceClient.CreateInstance(input)
    46  	if err != nil {
    47  		err = fmt.Errorf("Problem creating instance: %s", err)
    48  		ui.Error(err.Error())
    49  		state.Put("error", err)
    50  		return multistep.ActionHalt
    51  	}
    52  
    53  	state.Put("instance_info", instanceInfo)
    54  	state.Put("instance_id", instanceInfo.ID)
    55  	ui.Message(fmt.Sprintf("Created instance: %s.", instanceInfo.ID))
    56  	return multistep.ActionContinue
    57  }
    58  
    59  func (s *stepCreateInstance) Cleanup(state multistep.StateBag) {
    60  	instanceID, ok := state.GetOk("instance_id")
    61  	if !ok {
    62  		return
    63  	}
    64  
    65  	// terminate instance
    66  	ui := state.Get("ui").(packer.Ui)
    67  	client := state.Get("client").(*compute.ComputeClient)
    68  	config := state.Get("config").(*Config)
    69  
    70  	ui.Say("Terminating source instance...")
    71  
    72  	instanceClient := client.Instances()
    73  	input := &compute.DeleteInstanceInput{
    74  		Name: config.ImageName,
    75  		ID:   instanceID.(string),
    76  	}
    77  
    78  	err := instanceClient.DeleteInstance(input)
    79  	if err != nil {
    80  		err = fmt.Errorf("Problem destroying instance: %s", err)
    81  		ui.Error(err.Error())
    82  		state.Put("error", err)
    83  		return
    84  	}
    85  	// TODO wait for instance state to change to deleted?
    86  	ui.Say("Terminated instance.")
    87  }