github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/triton/step_create_source_machine.go (about)

     1  package triton
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/hashicorp/packer/packer"
     8  	"github.com/mitchellh/multistep"
     9  )
    10  
    11  // StepCreateSourceMachine creates an machine with the specified attributes
    12  // and waits for it to become available for provisioners.
    13  type StepCreateSourceMachine struct{}
    14  
    15  func (s *StepCreateSourceMachine) Run(state multistep.StateBag) multistep.StepAction {
    16  	config := state.Get("config").(Config)
    17  	driver := state.Get("driver").(Driver)
    18  	ui := state.Get("ui").(packer.Ui)
    19  
    20  	ui.Say("Creating source machine...")
    21  
    22  	machineId, err := driver.CreateMachine(config)
    23  	if err != nil {
    24  		state.Put("error", fmt.Errorf("Problem creating source machine: %s", err))
    25  		return multistep.ActionHalt
    26  	}
    27  
    28  	ui.Say("Waiting for source machine to become available...")
    29  	err = driver.WaitForMachineState(machineId, "running", 10*time.Minute)
    30  	if err != nil {
    31  		state.Put("error", fmt.Errorf("Problem waiting for source machine to become available: %s", err))
    32  		return multistep.ActionHalt
    33  	}
    34  
    35  	state.Put("machine", machineId)
    36  
    37  	return multistep.ActionContinue
    38  }
    39  
    40  func (s *StepCreateSourceMachine) Cleanup(state multistep.StateBag) {
    41  	driver := state.Get("driver").(Driver)
    42  	ui := state.Get("ui").(packer.Ui)
    43  
    44  	machineIdRaw, ok := state.GetOk("machine")
    45  	if ok && machineIdRaw.(string) != "" {
    46  		machineId := machineIdRaw.(string)
    47  		ui.Say(fmt.Sprintf("Stopping source machine (%s)...", machineId))
    48  		err := driver.StopMachine(machineId)
    49  		if err != nil {
    50  			state.Put("error", fmt.Errorf("Problem stopping source machine: %s", err))
    51  			return
    52  		}
    53  
    54  		ui.Say(fmt.Sprintf("Waiting for source machine to stop (%s)...", machineId))
    55  		err = driver.WaitForMachineState(machineId, "stopped", 10*time.Minute)
    56  		if err != nil {
    57  			state.Put("error", fmt.Errorf("Problem waiting for source machine to stop: %s", err))
    58  			return
    59  		}
    60  
    61  		ui.Say(fmt.Sprintf("Deleting source machine (%s)...", machineId))
    62  		err = driver.DeleteMachine(machineId)
    63  		if err != nil {
    64  			state.Put("error", fmt.Errorf("Problem deleting source machine: %s", err))
    65  			return
    66  		}
    67  
    68  		ui.Say(fmt.Sprintf("Waiting for source machine to be destroyed (%s)...", machineId))
    69  		err = driver.WaitForMachineDeletion(machineId, 10*time.Minute)
    70  		if err != nil {
    71  			state.Put("error", fmt.Errorf("Problem waiting for source machine to be deleted: %s", err))
    72  			return
    73  		}
    74  	}
    75  }