packer-cn/builder/lxd/step_lxd_launch.go

51 lines
1.2 KiB
Go
Raw Normal View History

2016-05-26 23:21:55 -04:00
package lxd
import (
"fmt"
2017-09-05 17:09:15 -04:00
"github.com/hashicorp/packer/packer"
2016-05-26 23:21:55 -04:00
"github.com/mitchellh/multistep"
2016-05-30 19:13:59 -04:00
"time"
2016-05-26 23:21:55 -04:00
)
type stepLxdLaunch struct{}
func (s *stepLxdLaunch) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui)
name := config.ContainerName
image := config.Image
args := []string{
"launch", "--ephemeral=false", image, name,
2016-05-26 23:21:55 -04:00
}
ui.Say("Creating container...")
_, err := LXDCommand(args...)
if err != nil {
err := fmt.Errorf("Error creating container: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
2016-05-26 23:21:55 -04:00
}
2016-05-30 19:57:26 -04:00
// TODO: Should we check `lxc info <container>` for "Running"?
// We have to do this so /tmp doens't get cleared and lose our provisioner scripts.
time.Sleep(1 * time.Second)
2016-05-26 23:21:55 -04:00
return multistep.ActionContinue
}
func (s *stepLxdLaunch) Cleanup(state multistep.StateBag) {
config := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui)
args := []string{
"delete", "--force", config.ContainerName,
2016-05-26 23:21:55 -04:00
}
ui.Say("Unregistering and deleting deleting container...")
if _, err := LXDCommand(args...); err != nil {
2016-05-26 23:21:55 -04:00
ui.Error(fmt.Sprintf("Error deleting container: %s", err))
}
}