[lxd] avoid extra container start/stop and race

Before we couldn't be sure if we were a permanent container or not.
Now we explicitly pass this on the command line so we don't depend on the
extra logic in `lxc publish --force` for ephemeral handling.

This means we avoid restarting the container after we publish
since we tear it down right away anyhow.

Likewise, there was sometimes a race which prevented the deletion
while the container was in a boot stage.
This commit is contained in:
Chris Lundquist 2016-08-07 03:38:37 +00:00 committed by Megan Marsh
parent e29f06fe1c
commit 68bb72380a
2 changed files with 17 additions and 6 deletions

View File

@ -17,7 +17,7 @@ func (s *stepLxdLaunch) Run(state multistep.StateBag) multistep.StepAction {
image := config.Image
args := []string{
"launch", image, name,
"launch", "--ephemeral=false", image, name,
}
ui.Say("Creating container...")

View File

@ -14,15 +14,26 @@ func (s *stepPublish) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
name := config.ContainerName
stop_args := []string{
// We created the container with "--ephemeral=false" so we know it is safe to stop.
"stop", name,
}
args := []string{
// If we use `lxc stop <container>`, an ephemeral container would die forever.
// `lxc publish` has special logic to handle this case.
"publish", "--force", name, "--alias", config.OutputImage,
ui.Say("Stopping container...")
_, err := LXDCommand(stop_args...)
if err != nil {
err := fmt.Errorf("Error stopping container: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
publish_args := []string{
"publish", name, "--alias", config.OutputImage,
}
ui.Say("Publishing container...")
stdoutString, err := LXDCommand(args...)
stdoutString, err := LXDCommand(publish_args...)
if err != nil {
err := fmt.Errorf("Error publishing container: %s", err)
state.Put("error", err)