package lxd import ( "fmt" "github.com/hashicorp/packer/packer" "github.com/mitchellh/multistep" "regexp" ) type stepPublish struct{} func (s *stepPublish) Run(state multistep.StateBag) multistep.StepAction { config := state.Get("config").(*Config) 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 } publish_args := []string{ "publish", name, "--alias", config.OutputImage, } ui.Say("Publishing container...") stdoutString, err := LXDCommand(publish_args...) if err != nil { err := fmt.Errorf("Error publishing container: %s", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } r := regexp.MustCompile("([0-9a-fA-F]+)$") fingerprint := r.FindAllStringSubmatch(stdoutString, -1)[0][0] ui.Say(fmt.Sprintf("Created image: %s", fingerprint)) state.Put("imageFingerprint", fingerprint) return multistep.ActionContinue } func (s *stepPublish) Cleanup(state multistep.StateBag) {}