packer-cn/builder/lxd/step_publish.go

44 lines
1.1 KiB
Go

package lxd
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"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
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("Publishing container...")
stdoutString, err := LXDCommand(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) {}