Merge
This commit is contained in:
parent
bb9f2aa66c
commit
a5940ef25e
|
@ -27,7 +27,7 @@ func ParseAndReplace(args string, env map[string]string) ([]string, error) {
|
||||||
|
|
||||||
replaced := make([]string, len(parsed))
|
replaced := make([]string, len(parsed))
|
||||||
for i, arg := range parsed {
|
for i, arg := range parsed {
|
||||||
replaced[i] = replaceEnv(arg, env)
|
replaced[i] = ReplaceEnv(arg, env)
|
||||||
}
|
}
|
||||||
|
|
||||||
return replaced, nil
|
return replaced, nil
|
||||||
|
@ -36,7 +36,7 @@ func ParseAndReplace(args string, env map[string]string) ([]string, error) {
|
||||||
// replaceEnv takes an arg and replaces all occurences of environment variables.
|
// replaceEnv takes an arg and replaces all occurences of environment variables.
|
||||||
// If the variable is found in the passed map it is replaced, otherwise the
|
// If the variable is found in the passed map it is replaced, otherwise the
|
||||||
// original string is returned.
|
// original string is returned.
|
||||||
func replaceEnv(arg string, env map[string]string) string {
|
func ReplaceEnv(arg string, env map[string]string) string {
|
||||||
return envRe.ReplaceAllStringFunc(arg, func(arg string) string {
|
return envRe.ReplaceAllStringFunc(arg, func(arg string) string {
|
||||||
stripped := arg[1:]
|
stripped := arg[1:]
|
||||||
if stripped[0] == '{' {
|
if stripped[0] == '{' {
|
||||||
|
|
|
@ -59,14 +59,7 @@ func (e *BasicExecutor) Start() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedPath, err := args.ParseAndReplace(e.cmd.Path, envVars.Map())
|
e.cmd.Path = args.ReplaceEnv(e.cmd.Path, envVars.Map())
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
} else if len(parsedPath) != 1 {
|
|
||||||
return fmt.Errorf("couldn't properly parse command path: %v", e.cmd.Path)
|
|
||||||
}
|
|
||||||
|
|
||||||
e.cmd.Path = parsedPath[0]
|
|
||||||
combined := strings.Join(e.cmd.Args, " ")
|
combined := strings.Join(e.cmd.Args, " ")
|
||||||
parsed, err := args.ParseAndReplace(combined, envVars.Map())
|
parsed, err := args.ParseAndReplace(combined, envVars.Map())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -165,14 +165,7 @@ func (e *LinuxExecutor) Start() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedPath, err := args.ParseAndReplace(e.cmd.Path, envVars.Map())
|
e.cmd.Path = args.ReplaceEnv(e.cmd.Path, envVars.Map())
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
} else if len(parsedPath) != 1 {
|
|
||||||
return fmt.Errorf("couldn't properly parse command path: %v", e.cmd.Path)
|
|
||||||
}
|
|
||||||
e.cmd.Path = parsedPath[0]
|
|
||||||
|
|
||||||
combined := strings.Join(e.cmd.Args, " ")
|
combined := strings.Join(e.cmd.Args, " ")
|
||||||
parsed, err := args.ParseAndReplace(combined, envVars.Map())
|
parsed, err := args.ParseAndReplace(combined, envVars.Map())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,11 +1,7 @@
|
||||||
package driver
|
package driver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -16,6 +12,7 @@ import (
|
||||||
|
|
||||||
"github.com/hashicorp/nomad/client/allocdir"
|
"github.com/hashicorp/nomad/client/allocdir"
|
||||||
"github.com/hashicorp/nomad/client/config"
|
"github.com/hashicorp/nomad/client/config"
|
||||||
|
"github.com/hashicorp/nomad/client/driver/executor"
|
||||||
"github.com/hashicorp/nomad/client/fingerprint"
|
"github.com/hashicorp/nomad/client/fingerprint"
|
||||||
"github.com/hashicorp/nomad/client/getter"
|
"github.com/hashicorp/nomad/client/getter"
|
||||||
"github.com/hashicorp/nomad/nomad/structs"
|
"github.com/hashicorp/nomad/nomad/structs"
|
||||||
|
@ -35,19 +32,11 @@ type QemuDriver struct {
|
||||||
|
|
||||||
// qemuHandle is returned from Start/Open as a handle to the PID
|
// qemuHandle is returned from Start/Open as a handle to the PID
|
||||||
type qemuHandle struct {
|
type qemuHandle struct {
|
||||||
proc *os.Process
|
cmd executor.Executor
|
||||||
vmID string
|
|
||||||
waitCh chan error
|
waitCh chan error
|
||||||
doneCh chan struct{}
|
doneCh chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// qemuPID is a struct to map the pid running the process to the vm image on
|
|
||||||
// disk
|
|
||||||
type qemuPID struct {
|
|
||||||
Pid int
|
|
||||||
VmID string
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewQemuDriver is used to create a new exec driver
|
// NewQemuDriver is used to create a new exec driver
|
||||||
func NewQemuDriver(ctx *DriverContext) Driver {
|
func NewQemuDriver(ctx *DriverContext) Driver {
|
||||||
return &QemuDriver{DriverContext: *ctx}
|
return &QemuDriver{DriverContext: *ctx}
|
||||||
|
@ -184,25 +173,25 @@ func (d *QemuDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start Qemu
|
// Setup the command
|
||||||
var outBuf, errBuf bytes.Buffer
|
cmd := executor.Command(args[0], args[1:]...)
|
||||||
cmd := exec.Command(args[0], args[1:]...)
|
if err := cmd.Limit(task.Resources); err != nil {
|
||||||
cmd.Stdout = &outBuf
|
return nil, fmt.Errorf("failed to constrain resources: %s", err)
|
||||||
cmd.Stderr = &errBuf
|
}
|
||||||
|
|
||||||
|
if err := cmd.ConfigureTaskDir(d.taskName, ctx.AllocDir); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to configure task directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
d.logger.Printf("[DEBUG] Starting QemuVM command: %q", strings.Join(args, " "))
|
d.logger.Printf("[DEBUG] Starting QemuVM command: %q", strings.Join(args, " "))
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf("failed to start command: %v", err)
|
||||||
"Error running QEMU: %s\n\nOutput: %s\n\nError: %s",
|
|
||||||
err, outBuf.String(), errBuf.String())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
d.logger.Printf("[INFO] Started new QemuVM: %s", vmID)
|
d.logger.Printf("[INFO] Started new QemuVM: %s", vmID)
|
||||||
|
|
||||||
// Create and Return Handle
|
// Create and Return Handle
|
||||||
h := &qemuHandle{
|
h := &qemuHandle{
|
||||||
proc: cmd.Process,
|
cmd: cmd,
|
||||||
vmID: vmPath,
|
|
||||||
doneCh: make(chan struct{}),
|
doneCh: make(chan struct{}),
|
||||||
waitCh: make(chan error, 1),
|
waitCh: make(chan error, 1),
|
||||||
}
|
}
|
||||||
|
@ -212,42 +201,25 @@ func (d *QemuDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *QemuDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
|
func (d *QemuDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
|
||||||
// Parse the handle
|
|
||||||
pidBytes := []byte(strings.TrimPrefix(handleID, "QEMU:"))
|
|
||||||
qpid := &qemuPID{}
|
|
||||||
if err := json.Unmarshal(pidBytes, qpid); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse Qemu handle '%s': %v", handleID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find the process
|
// Find the process
|
||||||
proc, err := os.FindProcess(qpid.Pid)
|
cmd, err := executor.OpenId(handleID)
|
||||||
if proc == nil || err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to find Qemu PID %d: %v", qpid.Pid, err)
|
return nil, fmt.Errorf("failed to open ID %v: %v", handleID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return a driver handle
|
// Return a driver handle
|
||||||
h := &qemuHandle{
|
h := &execHandle{
|
||||||
proc: proc,
|
cmd: cmd,
|
||||||
vmID: qpid.VmID,
|
|
||||||
doneCh: make(chan struct{}),
|
doneCh: make(chan struct{}),
|
||||||
waitCh: make(chan error, 1),
|
waitCh: make(chan error, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
go h.run()
|
go h.run()
|
||||||
return h, nil
|
return h, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *qemuHandle) ID() string {
|
func (h *qemuHandle) ID() string {
|
||||||
// Return a handle to the PID
|
id, _ := h.cmd.ID()
|
||||||
pid := &qemuPID{
|
return id
|
||||||
Pid: h.proc.Pid,
|
|
||||||
VmID: h.vmID,
|
|
||||||
}
|
|
||||||
data, err := json.Marshal(pid)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("[ERR] failed to marshal Qemu PID to JSON: %s", err)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("QEMU:%s", string(data))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *qemuHandle) WaitCh() chan error {
|
func (h *qemuHandle) WaitCh() chan error {
|
||||||
|
@ -259,28 +231,23 @@ func (h *qemuHandle) Update(task *structs.Task) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kill is used to terminate the task. We send an Interrupt
|
|
||||||
// and then provide a 5 second grace period before doing a Kill.
|
|
||||||
//
|
|
||||||
// TODO: allow a 'shutdown_command' that can be executed over a ssh connection
|
// TODO: allow a 'shutdown_command' that can be executed over a ssh connection
|
||||||
// to the VM
|
// to the VM
|
||||||
func (h *qemuHandle) Kill() error {
|
func (h *qemuHandle) Kill() error {
|
||||||
h.proc.Signal(os.Interrupt)
|
h.cmd.Shutdown()
|
||||||
select {
|
select {
|
||||||
case <-h.doneCh:
|
case <-h.doneCh:
|
||||||
return nil
|
return nil
|
||||||
case <-time.After(5 * time.Second):
|
case <-time.After(5 * time.Second):
|
||||||
return h.proc.Kill()
|
return h.cmd.ForceStop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *qemuHandle) run() {
|
func (h *qemuHandle) run() {
|
||||||
ps, err := h.proc.Wait()
|
err := h.cmd.Wait()
|
||||||
close(h.doneCh)
|
close(h.doneCh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.waitCh <- err
|
h.waitCh <- err
|
||||||
} else if !ps.Success() {
|
|
||||||
h.waitCh <- fmt.Errorf("task exited with error")
|
|
||||||
}
|
}
|
||||||
close(h.waitCh)
|
close(h.waitCh)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package driver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/nomad/client/config"
|
"github.com/hashicorp/nomad/client/config"
|
||||||
|
@ -11,21 +10,6 @@ import (
|
||||||
ctestutils "github.com/hashicorp/nomad/client/testutil"
|
ctestutils "github.com/hashicorp/nomad/client/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestQemuDriver_Handle(t *testing.T) {
|
|
||||||
h := &qemuHandle{
|
|
||||||
proc: &os.Process{Pid: 123},
|
|
||||||
vmID: "vmid",
|
|
||||||
doneCh: make(chan struct{}),
|
|
||||||
waitCh: make(chan error, 1),
|
|
||||||
}
|
|
||||||
|
|
||||||
actual := h.ID()
|
|
||||||
expected := `QEMU:{"Pid":123,"VmID":"vmid"}`
|
|
||||||
if actual != expected {
|
|
||||||
t.Errorf("Expected `%s`, found `%s`", expected, actual)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The fingerprinter test should always pass, even if QEMU is not installed.
|
// The fingerprinter test should always pass, even if QEMU is not installed.
|
||||||
func TestQemuDriver_Fingerprint(t *testing.T) {
|
func TestQemuDriver_Fingerprint(t *testing.T) {
|
||||||
ctestutils.QemuCompatible(t)
|
ctestutils.QemuCompatible(t)
|
||||||
|
@ -48,7 +32,7 @@ func TestQemuDriver_Fingerprint(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQemuDriver_Start(t *testing.T) {
|
func TestQemuDriver_StartOpen_Wait(t *testing.T) {
|
||||||
ctestutils.QemuCompatible(t)
|
ctestutils.QemuCompatible(t)
|
||||||
// TODO: use test server to load from a fixture
|
// TODO: use test server to load from a fixture
|
||||||
task := &structs.Task{
|
task := &structs.Task{
|
||||||
|
@ -60,6 +44,7 @@ func TestQemuDriver_Start(t *testing.T) {
|
||||||
"guest_ports": "22,8080",
|
"guest_ports": "22,8080",
|
||||||
},
|
},
|
||||||
Resources: &structs.Resources{
|
Resources: &structs.Resources{
|
||||||
|
CPU: 500,
|
||||||
MemoryMB: 512,
|
MemoryMB: 512,
|
||||||
Networks: []*structs.NetworkResource{
|
Networks: []*structs.NetworkResource{
|
||||||
&structs.NetworkResource{
|
&structs.NetworkResource{
|
||||||
|
|
Loading…
Reference in a new issue