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