github.phpd.cn/hashicorp/packer@v1.3.2/builder/lxd/step_lxd_launch.go (about) 1 package lxd 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 "strconv" 8 "time" 9 10 "github.com/hashicorp/packer/helper/multistep" 11 "github.com/hashicorp/packer/packer" 12 ) 13 14 type stepLxdLaunch struct{} 15 16 func (s *stepLxdLaunch) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 17 config := state.Get("config").(*Config) 18 ui := state.Get("ui").(packer.Ui) 19 20 name := config.ContainerName 21 image := config.Image 22 profile := fmt.Sprintf("--profile=%s", config.Profile) 23 24 launch_args := []string{ 25 "launch", "--ephemeral=false", profile, image, name, 26 } 27 28 for k, v := range config.LaunchConfig { 29 launch_args = append(launch_args, "--config", fmt.Sprintf("%s=%s", k, v)) 30 } 31 32 ui.Say("Creating container...") 33 _, err := LXDCommand(launch_args...) 34 if err != nil { 35 err := fmt.Errorf("Error creating container: %s", err) 36 state.Put("error", err) 37 ui.Error(err.Error()) 38 return multistep.ActionHalt 39 } 40 sleep_seconds, err := strconv.Atoi(config.InitSleep) 41 if err != nil { 42 err := fmt.Errorf("Error parsing InitSleep into int: %s", err) 43 state.Put("error", err) 44 ui.Error(err.Error()) 45 return multistep.ActionHalt 46 } 47 48 // TODO: Should we check `lxc info <container>` for "Running"? 49 // We have to do this so /tmp doesn't get cleared and lose our provisioner scripts. 50 51 time.Sleep(time.Duration(sleep_seconds) * time.Second) 52 log.Printf("Sleeping for %d seconds...", sleep_seconds) 53 return multistep.ActionContinue 54 } 55 56 func (s *stepLxdLaunch) Cleanup(state multistep.StateBag) { 57 config := state.Get("config").(*Config) 58 ui := state.Get("ui").(packer.Ui) 59 60 cleanup_args := []string{ 61 "delete", "--force", config.ContainerName, 62 } 63 64 ui.Say("Unregistering and deleting deleting container...") 65 if _, err := LXDCommand(cleanup_args...); err != nil { 66 ui.Error(fmt.Sprintf("Error deleting container: %s", err)) 67 } 68 }