packer-cn/builder/lxd/step_lxd_launch.go

53 lines
1.2 KiB
Go
Raw Normal View History

2016-05-26 23:21:55 -04:00
package lxd
import (
"context"
2016-05-26 23:21:55 -04:00
"fmt"
"time"
"github.com/hashicorp/packer/helper/multistep"
2017-09-05 17:09:15 -04:00
"github.com/hashicorp/packer/packer"
2016-05-26 23:21:55 -04:00
)
type stepLxdLaunch struct{}
func (s *stepLxdLaunch) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
2016-05-26 23:21:55 -04:00
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"?
2018-03-13 03:39:58 -04:00
// We have to do this so /tmp doesn't get cleared and lose our provisioner scripts.
2016-05-30 19:57:26 -04:00
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))
}
}