github.phpd.cn/hashicorp/packer@v1.3.2/builder/lxd/step_publish.go (about) 1 package lxd 2 3 import ( 4 "context" 5 "fmt" 6 "regexp" 7 8 "github.com/hashicorp/packer/helper/multistep" 9 "github.com/hashicorp/packer/packer" 10 ) 11 12 type stepPublish struct{} 13 14 func (s *stepPublish) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 15 config := state.Get("config").(*Config) 16 ui := state.Get("ui").(packer.Ui) 17 18 name := config.ContainerName 19 stop_args := []string{ 20 // We created the container with "--ephemeral=false" so we know it is safe to stop. 21 "stop", name, 22 } 23 24 ui.Say("Stopping container...") 25 _, err := LXDCommand(stop_args...) 26 if err != nil { 27 err := fmt.Errorf("Error stopping container: %s", err) 28 state.Put("error", err) 29 ui.Error(err.Error()) 30 return multistep.ActionHalt 31 } 32 33 publish_args := []string{ 34 "publish", name, "--alias", config.OutputImage, 35 } 36 37 for k, v := range config.PublishProperties { 38 publish_args = append(publish_args, fmt.Sprintf("%s=%s", k, v)) 39 } 40 41 ui.Say("Publishing container...") 42 stdoutString, err := LXDCommand(publish_args...) 43 if err != nil { 44 err := fmt.Errorf("Error publishing container: %s", err) 45 state.Put("error", err) 46 ui.Error(err.Error()) 47 return multistep.ActionHalt 48 } 49 50 r := regexp.MustCompile("([0-9a-fA-F]+)$") 51 fingerprint := r.FindAllStringSubmatch(stdoutString, -1)[0][0] 52 53 ui.Say(fmt.Sprintf("Created image: %s", fingerprint)) 54 55 state.Put("imageFingerprint", fingerprint) 56 57 return multistep.ActionContinue 58 } 59 60 func (s *stepPublish) Cleanup(state multistep.StateBag) {}