2016-05-27 03:21:55 +00:00
|
|
|
package lxd
|
|
|
|
|
|
|
|
import (
|
2018-01-22 15:32:33 -08:00
|
|
|
"context"
|
2016-05-27 03:21:55 +00:00
|
|
|
"fmt"
|
2018-01-22 15:32:33 -08:00
|
|
|
"regexp"
|
|
|
|
|
2018-01-19 16:18:44 -08:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-09-05 14:09:15 -07:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-05-27 03:21:55 +00:00
|
|
|
)
|
|
|
|
|
2016-05-30 23:13:59 +00:00
|
|
|
type stepPublish struct{}
|
2016-05-27 03:21:55 +00:00
|
|
|
|
2019-03-29 16:50:02 +01:00
|
|
|
func (s *stepPublish) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-05-27 03:21:55 +00:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
name := config.ContainerName
|
2016-08-07 03:38:37 +00:00
|
|
|
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-27 03:21:55 +00:00
|
|
|
|
2016-08-07 03:38:37 +00:00
|
|
|
publish_args := []string{
|
|
|
|
"publish", name, "--alias", config.OutputImage,
|
2016-05-27 03:21:55 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 04:57:13 +00:00
|
|
|
for k, v := range config.PublishProperties {
|
|
|
|
publish_args = append(publish_args, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
|
2016-05-30 23:13:59 +00:00
|
|
|
ui.Say("Publishing container...")
|
2016-08-07 03:38:37 +00:00
|
|
|
stdoutString, err := LXDCommand(publish_args...)
|
2016-05-31 01:13:09 +00:00
|
|
|
if err != nil {
|
2016-08-07 03:25:57 +00:00
|
|
|
err := fmt.Errorf("Error publishing container: %s", err)
|
2016-05-31 01:13:09 +00: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-27 03:21:55 +00:00
|
|
|
}
|
2016-05-31 01:13:09 +00:00
|
|
|
|
|
|
|
func (s *stepPublish) Cleanup(state multistep.StateBag) {}
|