packer-cn/builder/lxd/step_publish.go

44 lines
1.1 KiB
Go
Raw Normal View History

2016-05-26 23:21:55 -04:00
package lxd
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
2016-05-30 21:13:09 -04:00
"regexp"
2016-05-26 23:21:55 -04:00
)
2016-05-30 19:13:59 -04:00
type stepPublish struct{}
2016-05-26 23:21:55 -04:00
2016-05-30 19:13:59 -04:00
func (s *stepPublish) Run(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
2016-05-30 21:13:09 -04:00
args := []string{
2016-05-31 19:53:50 -04:00
// 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,
2016-05-26 23:21:55 -04:00
}
2016-05-30 19:13:59 -04:00
ui.Say("Publishing container...")
stdoutString, err := LXDCommand(args...)
2016-05-30 21:13:09 -04:00
if err != nil {
err := fmt.Errorf("Error publishing container: %s", err)
2016-05-30 21:13:09 -04:00
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
2016-05-26 23:21:55 -04:00
}
2016-05-30 21:13:09 -04:00
func (s *stepPublish) Cleanup(state multistep.StateBag) {}