From 68bb72380aae3a72afcbc2900f4fd177ee56e59e Mon Sep 17 00:00:00 2001 From: Chris Lundquist Date: Sun, 7 Aug 2016 03:38:37 +0000 Subject: [PATCH] [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. --- builder/lxd/step_lxd_launch.go | 2 +- builder/lxd/step_publish.go | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/builder/lxd/step_lxd_launch.go b/builder/lxd/step_lxd_launch.go index 0cc0b80ac..cd51c34f2 100644 --- a/builder/lxd/step_lxd_launch.go +++ b/builder/lxd/step_lxd_launch.go @@ -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...") diff --git a/builder/lxd/step_publish.go b/builder/lxd/step_publish.go index cf3c98bb3..ecc547d92 100644 --- a/builder/lxd/step_publish.go +++ b/builder/lxd/step_publish.go @@ -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, + } + + 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 + } - args := []string{ - // If we use `lxc stop `, an ephemeral container would die forever. - // `lxc publish` has special logic to handle this case. - "publish", "--force", name, "--alias", config.OutputImage, + 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)