2016-05-26 23:21:55 -04:00
|
|
|
package lxd
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-05-26 23:21:55 -04:00
|
|
|
"fmt"
|
2018-03-20 15:30:46 -04:00
|
|
|
"strconv"
|
2018-01-22 18:32:33 -05:00
|
|
|
"time"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"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{}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
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
|
2018-03-20 15:30:46 -04:00
|
|
|
profile := fmt.Sprintf("--profile=%s", config.Profile)
|
2016-05-26 23:21:55 -04:00
|
|
|
|
2018-03-20 15:30:46 -04:00
|
|
|
launch_args := []string{
|
|
|
|
"launch", "--ephemeral=false", profile, image, name,
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range config.LaunchConfig {
|
|
|
|
launch_args = append(launch_args, fmt.Sprintf("--config %s=%s", k, v))
|
2016-05-26 23:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Creating container...")
|
2018-03-20 15:30:46 -04:00
|
|
|
_, err := LXDCommand(launch_args...)
|
2016-08-06 23:25:57 -04:00
|
|
|
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
|
|
|
}
|
2018-03-20 15:30:46 -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.
|
2018-03-20 15:30:46 -04:00
|
|
|
time.Sleep(time.Duration(sleep_seconds) * 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)
|
|
|
|
|
2018-03-20 15:30:46 -04:00
|
|
|
cleanup_args := []string{
|
2016-08-06 23:25:57 -04:00
|
|
|
"delete", "--force", config.ContainerName,
|
2016-05-26 23:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Unregistering and deleting deleting container...")
|
2018-03-20 15:30:46 -04:00
|
|
|
if _, err := LXDCommand(cleanup_args...); err != nil {
|
2016-05-26 23:21:55 -04:00
|
|
|
ui.Error(fmt.Sprintf("Error deleting container: %s", err))
|
|
|
|
}
|
|
|
|
}
|