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

     1  package lxd
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashicorp/packer/packer"
     6  	"github.com/mitchellh/multistep"
     7  	"time"
     8  )
     9  
    10  type stepLxdLaunch struct{}
    11  
    12  func (s *stepLxdLaunch) Run(state multistep.StateBag) multistep.StepAction {
    13  	config := state.Get("config").(*Config)
    14  	ui := state.Get("ui").(packer.Ui)
    15  
    16  	name := config.ContainerName
    17  	image := config.Image
    18  
    19  	args := []string{
    20  		"launch", "--ephemeral=false", image, name,
    21  	}
    22  
    23  	ui.Say("Creating container...")
    24  	_, err := LXDCommand(args...)
    25  	if err != nil {
    26  		err := fmt.Errorf("Error creating container: %s", err)
    27  		state.Put("error", err)
    28  		ui.Error(err.Error())
    29  		return multistep.ActionHalt
    30  	}
    31  	// TODO: Should we check `lxc info <container>` for "Running"?
    32  	// We have to do this so /tmp doens't get cleared and lose our provisioner scripts.
    33  	time.Sleep(1 * time.Second)
    34  
    35  	return multistep.ActionContinue
    36  }
    37  
    38  func (s *stepLxdLaunch) Cleanup(state multistep.StateBag) {
    39  	config := state.Get("config").(*Config)
    40  	ui := state.Get("ui").(packer.Ui)
    41  
    42  	args := []string{
    43  		"delete", "--force", config.ContainerName,
    44  	}
    45  
    46  	ui.Say("Unregistering and deleting deleting container...")
    47  	if _, err := LXDCommand(args...); err != nil {
    48  		ui.Error(fmt.Sprintf("Error deleting container: %s", err))
    49  	}
    50  }