2016-05-26 23:21:55 -04:00
|
|
|
package lxd
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-05-26 23:21:55 -04:00
|
|
|
"fmt"
|
2018-01-22 18:32:33 -05:00
|
|
|
"regexp"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
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
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepPublish) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-05-26 23:21:55 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2016-05-26 23:21:55 -04:00
|
|
|
|
|
|
|
name := config.ContainerName
|
2016-08-06 23:38:37 -04: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-26 23:21:55 -04:00
|
|
|
|
2016-08-06 23:38:37 -04:00
|
|
|
publish_args := []string{
|
|
|
|
"publish", name, "--alias", config.OutputImage,
|
2016-05-26 23:21:55 -04:00
|
|
|
}
|
|
|
|
|
2017-10-18 00:57:13 -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...")
|
2016-08-06 23:38:37 -04:00
|
|
|
stdoutString, err := LXDCommand(publish_args...)
|
2016-05-30 21:13:09 -04:00
|
|
|
if err != nil {
|
2016-08-06 23:25:57 -04:00
|
|
|
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) {}
|