packer-cn/builder/lxd/step_publish.go

60 lines
1.4 KiB
Go
Raw Normal View History

2016-05-26 23:21:55 -04:00
package lxd
import (
"fmt"
2018-01-22 20:21:10 -05:00
"regexp"
2017-09-05 17:09:15 -04:00
"github.com/hashicorp/packer/packer"
2016-05-26 23:21:55 -04:00
"github.com/mitchellh/multistep"
)
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
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
}
2016-05-26 23:21:55 -04:00
publish_args := []string{
"publish", name, "--alias", config.OutputImage,
2016-05-26 23:21:55 -04:00
}
for k, v := range config.PublishProperties {
publish_args = append(publish_args, fmt.Sprintf("%s=%s", k, v))
}
2016-05-30 19:13:59 -04:00
ui.Say("Publishing container...")
stdoutString, err := LXDCommand(publish_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) {}