2016-05-26 23:21:55 -04:00
|
|
|
package lxd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
2016-05-30 19:57:26 -04:00
|
|
|
commands := [][]string{
|
|
|
|
{"lxc", "stop", "--force", name},
|
|
|
|
{"lxc", "publish", name, "--alias", config.OutputImage},
|
2016-05-26 23:21:55 -04:00
|
|
|
}
|
|
|
|
|
2016-05-30 19:13:59 -04:00
|
|
|
ui.Say("Publishing container...")
|
2016-05-26 23:21:55 -04:00
|
|
|
for _, command := range commands {
|
|
|
|
err := s.SudoCommand(command...)
|
|
|
|
if err != nil {
|
2016-05-30 19:57:26 -04:00
|
|
|
err := fmt.Errorf("Error publishing container: %s", err)
|
2016-05-26 23:21:55 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2016-05-30 19:13:59 -04:00
|
|
|
func (s *stepPublish) Cleanup(state multistep.StateBag) {}
|
2016-05-26 23:21:55 -04:00
|
|
|
|
2016-05-30 19:13:59 -04:00
|
|
|
func (s *stepPublish) SudoCommand(args ...string) error {
|
2016-05-26 23:21:55 -04:00
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
|
|
|
|
log.Printf("Executing sudo command: %#v", args)
|
|
|
|
cmd := exec.Command("sudo", args...)
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
|
|
|
|
stdoutString := strings.TrimSpace(stdout.String())
|
|
|
|
stderrString := strings.TrimSpace(stderr.String())
|
|
|
|
|
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
|
|
|
err = fmt.Errorf("Sudo command error: %s", stderrString)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("stdout: %s", stdoutString)
|
|
|
|
log.Printf("stderr: %s", stderrString)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|