packer-cn/builder/lxd/step_lxd_launch.go

69 lines
1.8 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"
2018-03-23 18:46:39 -04:00
"log"
"strconv"
"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(ctx 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
profile := fmt.Sprintf("--profile=%s", config.Profile)
2016-05-26 23:21:55 -04:00
launch_args := []string{
"launch", "--ephemeral=false", profile, image, name,
}
for k, v := range config.LaunchConfig {
launch_args = append(launch_args, "--config", fmt.Sprintf("%s=%s", k, v))
2016-05-26 23:21:55 -04:00
}
ui.Say("Creating container...")
_, err := LXDCommand(launch_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
}
sleep_seconds, err := strconv.Atoi(config.InitSleep)
if err != nil {
err := fmt.Errorf("Error parsing InitSleep into int: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
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-26 23:21:55 -04:00
2018-03-23 18:46:39 -04:00
time.Sleep(time.Duration(sleep_seconds) * time.Second)
log.Printf("Sleeping for %d seconds...", sleep_seconds)
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)
cleanup_args := []string{
"delete", "--force", config.ContainerName,
2016-05-26 23:21:55 -04:00
}
ui.Say("Unregistering and deleting deleting container...")
if _, err := LXDCommand(cleanup_args...); err != nil {
2016-05-26 23:21:55 -04:00
ui.Error(fmt.Sprintf("Error deleting container: %s", err))
}
}