open-nomad/client/driver/rkt.go

339 lines
9.9 KiB
Go
Raw Normal View History

package driver
import (
2015-10-07 22:24:16 +00:00
"bytes"
"encoding/json"
"fmt"
"log"
"os/exec"
2015-10-09 19:14:56 +00:00
"path/filepath"
2015-10-07 22:24:16 +00:00
"regexp"
"runtime"
"strings"
"syscall"
"time"
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/go-version"
2015-10-09 19:14:56 +00:00
"github.com/hashicorp/nomad/client/allocdir"
2015-10-07 22:24:16 +00:00
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/driver/executor"
2015-11-17 00:23:03 +00:00
cstructs "github.com/hashicorp/nomad/client/driver/structs"
2015-11-05 21:46:02 +00:00
"github.com/hashicorp/nomad/client/fingerprint"
"github.com/hashicorp/nomad/helper/discover"
2015-10-07 22:24:16 +00:00
"github.com/hashicorp/nomad/nomad/structs"
"github.com/mitchellh/mapstructure"
)
var (
2016-02-02 23:39:45 +00:00
reRktVersion = regexp.MustCompile(`rkt [vV]ersion[:]? (\d[.\d]+)`)
reAppcVersion = regexp.MustCompile(`appc [vV]ersion[:]? (\d[.\d]+)`)
)
2015-12-22 05:15:37 +00:00
const (
2015-12-22 18:11:22 +00:00
// minRktVersion is the earliest supported version of rkt. rkt added support
// for CPU and memory isolators in 0.14.0. We cannot support an earlier
// version to maintain an uniform interface across all drivers
minRktVersion = "0.14.0"
// bytesToMB is the conversion from bytes to megabytes.
bytesToMB = 1024 * 1024
2015-12-22 05:15:37 +00:00
)
// RktDriver is a driver for running images via Rkt
// We attempt to chose sane defaults for now, with more configuration available
// planned in the future
type RktDriver struct {
2015-10-07 22:24:16 +00:00
DriverContext
2015-11-05 21:46:02 +00:00
fingerprint.StaticFingerprinter
}
2015-11-14 04:22:49 +00:00
type RktDriverConfig struct {
ImageName string `mapstructure:"image"`
Args []string `mapstructure:"args"`
}
// rktHandle is returned from Start/Open as a handle to the PID
type rktHandle struct {
pluginClient *plugin.Client
executorPid int
executor executor.Executor
allocDir *allocdir.AllocDir
logger *log.Logger
killTimeout time.Duration
waitCh chan *cstructs.WaitResult
doneCh chan struct{}
}
// rktPID is a struct to map the pid running the process to the vm image on
// disk
type rktPID struct {
PluginConfig *PluginReattachConfig
AllocDir *allocdir.AllocDir
ExecutorPid int
KillTimeout time.Duration
}
// NewRktDriver is used to create a new exec driver
func NewRktDriver(ctx *DriverContext) Driver {
2015-11-05 21:46:02 +00:00
return &RktDriver{DriverContext: *ctx}
}
func (d *RktDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
2015-10-07 22:24:16 +00:00
// Only enable if we are root when running on non-windows systems.
if runtime.GOOS != "windows" && syscall.Geteuid() != 0 {
d.logger.Printf("[DEBUG] driver.rkt: must run as root user, disabling")
return false, nil
}
outBytes, err := exec.Command("rkt", "version").Output()
if err != nil {
return false, nil
}
out := strings.TrimSpace(string(outBytes))
rktMatches := reRktVersion.FindStringSubmatch(out)
2015-10-21 01:08:46 +00:00
appcMatches := reAppcVersion.FindStringSubmatch(out)
2015-10-07 22:24:16 +00:00
if len(rktMatches) != 2 || len(appcMatches) != 2 {
return false, fmt.Errorf("Unable to parse Rkt version string: %#v", rktMatches)
}
node.Attributes["driver.rkt"] = "1"
2015-10-21 01:08:46 +00:00
node.Attributes["driver.rkt.version"] = rktMatches[1]
2015-10-07 22:24:16 +00:00
node.Attributes["driver.rkt.appc.version"] = appcMatches[1]
2015-12-22 05:15:37 +00:00
minVersion, _ := version.NewVersion(minRktVersion)
currentVersion, _ := version.NewVersion(node.Attributes["driver.rkt.version"])
if currentVersion.LessThan(minVersion) {
// Do not allow rkt < 0.14.0
d.logger.Printf("[WARN] driver.rkt: please upgrade rkt to a version >= %s", minVersion)
node.Attributes["driver.rkt"] = "0"
}
2015-10-07 22:24:16 +00:00
return true, nil
}
// Run an existing Rkt image.
func (d *RktDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error) {
2015-11-14 04:22:49 +00:00
var driverConfig RktDriverConfig
if err := mapstructure.WeakDecode(task.Config, &driverConfig); err != nil {
return nil, err
}
// Validate that the config is valid.
img := driverConfig.ImageName
if img == "" {
return nil, fmt.Errorf("Missing ACI image for rkt")
}
2015-10-09 19:14:56 +00:00
// Get the tasks local directory.
taskName := d.DriverContext.taskName
taskDir, ok := ctx.AllocDir.TaskDirs[taskName]
if !ok {
return nil, fmt.Errorf("Could not find task directory for task: %v", d.DriverContext.taskName)
}
2015-12-22 18:23:29 +00:00
// Build the command.
var cmdArgs []string
2015-10-07 22:24:16 +00:00
// Add the given trust prefix
2015-12-21 10:10:37 +00:00
trustPrefix, trustCmd := task.Config["trust_prefix"]
insecure := false
2015-12-21 10:10:37 +00:00
if trustCmd {
var outBuf, errBuf bytes.Buffer
cmd := exec.Command("rkt", "trust", "--skip-fingerprint-review=true", fmt.Sprintf("--prefix=%s", trustPrefix))
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("Error running rkt trust: %s\n\nOutput: %s\n\nError: %s",
err, outBuf.String(), errBuf.String())
}
2015-12-21 10:10:37 +00:00
d.logger.Printf("[DEBUG] driver.rkt: added trust prefix: %q", trustPrefix)
2015-12-22 18:23:29 +00:00
} else {
// Disble signature verification if the trust command was not run.
insecure = true
}
local, ok := ctx.AllocDir.TaskDirs[task.Name]
if !ok {
return nil, fmt.Errorf("Failed to find task local directory: %v", task.Name)
}
cmdArgs = append(cmdArgs, "run")
cmdArgs = append(cmdArgs, fmt.Sprintf("--volume=%s,kind=host,source=%s", task.Name, local))
cmdArgs = append(cmdArgs, fmt.Sprintf("--mount=volume=%s,target=%s", task.Name, ctx.AllocDir.SharedDir))
cmdArgs = append(cmdArgs, img)
if insecure == true {
cmdArgs = append(cmdArgs, "--insecure-options=all")
2015-10-07 22:24:16 +00:00
}
// Inject enviornment variables
2016-01-11 17:58:26 +00:00
for k, v := range d.taskEnv.EnvMap() {
2015-12-21 10:10:37 +00:00
cmdArgs = append(cmdArgs, fmt.Sprintf("--set-env=%v=%v", k, v))
2015-10-07 22:24:16 +00:00
}
// Check if the user has overriden the exec command.
2015-12-21 10:10:37 +00:00
if execCmd, ok := task.Config["command"]; ok {
cmdArgs = append(cmdArgs, fmt.Sprintf("--exec=%v", execCmd))
2015-10-07 22:24:16 +00:00
}
if task.Resources.MemoryMB == 0 {
2015-12-21 06:09:11 +00:00
return nil, fmt.Errorf("Memory limit cannot be zero")
}
if task.Resources.CPU == 0 {
return nil, fmt.Errorf("CPU limit cannot be zero")
}
// Add memory isolator
2015-12-22 18:11:22 +00:00
cmdArgs = append(cmdArgs, fmt.Sprintf("--memory=%vM", int64(task.Resources.MemoryMB)*bytesToMB))
// Add CPU isolator
2015-12-21 10:10:37 +00:00
cmdArgs = append(cmdArgs, fmt.Sprintf("--cpu=%vm", int64(task.Resources.CPU)))
// Add user passed arguments.
if len(driverConfig.Args) != 0 {
2016-01-11 17:58:26 +00:00
parsed := d.taskEnv.ParseAndReplace(driverConfig.Args)
// Need to start arguments with "--"
if len(parsed) > 0 {
2015-12-21 10:10:37 +00:00
cmdArgs = append(cmdArgs, "--")
}
for _, arg := range parsed {
2015-12-21 10:10:37 +00:00
cmdArgs = append(cmdArgs, fmt.Sprintf("%v", arg))
}
2015-10-07 22:24:16 +00:00
}
bin, err := discover.NomadExecutable()
2015-10-09 19:14:56 +00:00
if err != nil {
return nil, fmt.Errorf("unable to find the nomad binary: %v", err)
2015-10-09 19:14:56 +00:00
}
pluginLogFile := filepath.Join(taskDir, fmt.Sprintf("%s-executor.out", task.Name))
pluginConfig := &plugin.ClientConfig{
Cmd: exec.Command(bin, "executor", pluginLogFile),
2015-10-09 19:14:56 +00:00
}
exec, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
if err != nil {
return nil, err
}
executorCtx := &executor.ExecutorContext{
TaskEnv: d.taskEnv,
AllocDir: ctx.AllocDir,
TaskName: task.Name,
TaskResources: task.Resources,
UnprivilegedUser: false,
LogConfig: task.LogConfig,
}
2015-10-09 19:14:56 +00:00
2016-02-17 00:37:50 +00:00
ps, err := exec.LaunchCmd(&executor.ExecCommand{Cmd: "rkt", Args: cmdArgs}, executorCtx)
if err != nil {
pluginClient.Kill()
return nil, fmt.Errorf("error starting process via the plugin: %v", err)
2015-10-07 22:24:16 +00:00
}
d.logger.Printf("[DEBUG] driver.rkt: started ACI %q with: %v", img, cmdArgs)
2015-10-07 22:24:16 +00:00
h := &rktHandle{
pluginClient: pluginClient,
executor: exec,
executorPid: ps.Pid,
logger: d.logger,
killTimeout: d.DriverContext.KillTimeout(task),
doneCh: make(chan struct{}),
waitCh: make(chan *cstructs.WaitResult, 1),
2015-10-07 22:24:16 +00:00
}
go h.run()
return h, nil
}
func (d *RktDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
2015-10-07 22:24:16 +00:00
// Parse the handle
pidBytes := []byte(strings.TrimPrefix(handleID, "Rkt:"))
qpid := &rktPID{}
if err := json.Unmarshal(pidBytes, qpid); err != nil {
return nil, fmt.Errorf("failed to parse Rkt handle '%s': %v", handleID, err)
}
pluginConfig := &plugin.ClientConfig{
Reattach: qpid.PluginConfig.PluginConfig(),
}
executor, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
if err != nil {
d.logger.Println("[ERROR] driver.rkt: error connecting to plugin so destroying plugin pid and user pid")
if e := destroyPlugin(qpid.PluginConfig.Pid, qpid.ExecutorPid); e != nil {
d.logger.Printf("[ERROR] driver.rkt: error destroying plugin and executor pid: %v", e)
}
return nil, fmt.Errorf("error connecting to plugin: %v", err)
2015-10-07 22:24:16 +00:00
}
// Return a driver handle
h := &rktHandle{
pluginClient: pluginClient,
executorPid: qpid.ExecutorPid,
allocDir: qpid.AllocDir,
executor: executor,
logger: d.logger,
killTimeout: qpid.KillTimeout,
doneCh: make(chan struct{}),
waitCh: make(chan *cstructs.WaitResult, 1),
2015-10-07 22:24:16 +00:00
}
go h.run()
return h, nil
}
func (h *rktHandle) ID() string {
2015-10-07 22:24:16 +00:00
// Return a handle to the PID
pid := &rktPID{
PluginConfig: NewPluginReattachConfig(h.pluginClient.ReattachConfig()),
KillTimeout: h.killTimeout,
ExecutorPid: h.executorPid,
AllocDir: h.allocDir,
2015-10-07 22:24:16 +00:00
}
data, err := json.Marshal(pid)
if err != nil {
h.logger.Printf("[ERR] driver.rkt: failed to marshal rkt PID to JSON: %s", err)
}
return fmt.Sprintf("Rkt:%s", string(data))
}
func (h *rktHandle) WaitCh() chan *cstructs.WaitResult {
2015-10-07 22:24:16 +00:00
return h.waitCh
}
func (h *rktHandle) Update(task *structs.Task) error {
// Store the updated kill timeout.
h.killTimeout = task.KillTimeout
h.executor.UpdateLogConfig(task.LogConfig)
2015-10-07 22:24:16 +00:00
// Update is not possible
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.
func (h *rktHandle) Kill() error {
h.executor.ShutDown()
2015-10-07 22:24:16 +00:00
select {
case <-h.doneCh:
return nil
case <-time.After(h.killTimeout):
return h.executor.Exit()
2015-10-07 22:24:16 +00:00
}
}
func (h *rktHandle) run() {
ps, err := h.executor.Wait()
2015-10-07 22:24:16 +00:00
close(h.doneCh)
if ps.ExitCode == 0 && err != nil {
if e := killProcess(h.executorPid); e != nil {
h.logger.Printf("[ERROR] driver.rkt: error killing user process: %v", e)
}
if e := h.allocDir.UnmountAll(); e != nil {
h.logger.Printf("[ERROR] driver.rkt: unmounting dev,proc and alloc dirs failed: %v", e)
}
}
h.waitCh <- &cstructs.WaitResult{ExitCode: ps.ExitCode, Signal: 0, Err: err}
2015-10-07 22:24:16 +00:00
close(h.waitCh)
h.pluginClient.Kill()
}