|
|
|
|
@ -1,13 +1,9 @@
|
|
|
|
|
package lxc
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
|
@ -30,7 +26,9 @@ func (s *stepLxcCreate) Run(_ context.Context, state multistep.StateBag) multist
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commands := make([][]string, 3)
|
|
|
|
|
commands[0] = append(config.EnvVars, "lxc-create")
|
|
|
|
|
commands[0] = append(commands[0], "env")
|
|
|
|
|
commands[0] = append(commands[0], config.EnvVars...)
|
|
|
|
|
commands[0] = append(commands[0], "lxc-create")
|
|
|
|
|
commands[0] = append(commands[0], config.CreateOptions...)
|
|
|
|
|
commands[0] = append(commands[0], []string{"-n", name, "-t", config.Name, "--"}...)
|
|
|
|
|
commands[0] = append(commands[0], config.Parameters...)
|
|
|
|
|
@ -42,8 +40,7 @@ func (s *stepLxcCreate) Run(_ context.Context, state multistep.StateBag) multist
|
|
|
|
|
|
|
|
|
|
ui.Say("Creating container...")
|
|
|
|
|
for _, command := range commands {
|
|
|
|
|
log.Printf("Executing sudo command: %#v", command)
|
|
|
|
|
err := s.SudoCommand(command...)
|
|
|
|
|
err := RunCommand(command...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
err := fmt.Errorf("Error creating container: %s", err)
|
|
|
|
|
state.Put("error", err)
|
|
|
|
|
@ -66,29 +63,7 @@ func (s *stepLxcCreate) Cleanup(state multistep.StateBag) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ui.Say("Unregistering and deleting virtual machine...")
|
|
|
|
|
if err := s.SudoCommand(command...); err != nil {
|
|
|
|
|
if err := RunCommand(command...); err != nil {
|
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting virtual machine: %s", err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *stepLxcCreate) SudoCommand(args ...string) error {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|