2015-09-29 21:48:10 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2015-10-07 22:24:16 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-03-08 05:28:24 +00:00
|
|
|
"net"
|
2015-10-07 22:24:16 +00:00
|
|
|
"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"
|
|
|
|
|
2016-02-17 00:10:45 +00:00
|
|
|
"github.com/hashicorp/go-plugin"
|
2015-12-21 17:48:21 +00:00
|
|
|
"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"
|
2016-02-17 00:10:45 +00:00
|
|
|
"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"
|
2016-02-17 00:10:45 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/discover"
|
2016-04-09 22:38:42 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/fields"
|
2015-10-07 22:24:16 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-11-14 02:09:42 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-09-29 21:48:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-09-29 21:48:10 +00:00
|
|
|
)
|
|
|
|
|
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
|
2016-04-01 01:11:27 +00:00
|
|
|
|
|
|
|
// The key populated in the Node Attributes to indicate the presence of the
|
|
|
|
// Rkt driver
|
|
|
|
rktDriverAttr = "driver.rkt"
|
2015-12-22 05:15:37 +00:00
|
|
|
)
|
|
|
|
|
2015-09-29 21:48:10 +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-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 04:22:49 +00:00
|
|
|
type RktDriverConfig struct {
|
2016-03-08 05:28:24 +00:00
|
|
|
ImageName string `mapstructure:"image"`
|
|
|
|
Args []string `mapstructure:"args"`
|
|
|
|
DNSServers []string `mapstructure:"dns_servers"` // DNS Server for containers
|
|
|
|
DNSSearchDomains []string `mapstructure:"dns_search_domains"` // DNS Search domains for containers
|
2015-11-14 02:09:42 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 21:48:10 +00:00
|
|
|
// rktHandle is returned from Start/Open as a handle to the PID
|
|
|
|
type rktHandle struct {
|
2016-03-03 17:21:21 +00:00
|
|
|
pluginClient *plugin.Client
|
|
|
|
executorPid int
|
|
|
|
executor executor.Executor
|
|
|
|
allocDir *allocdir.AllocDir
|
|
|
|
logger *log.Logger
|
|
|
|
killTimeout time.Duration
|
|
|
|
maxKillTimeout time.Duration
|
|
|
|
waitCh chan *cstructs.WaitResult
|
|
|
|
doneCh chan struct{}
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// rktPID is a struct to map the pid running the process to the vm image on
|
|
|
|
// disk
|
|
|
|
type rktPID struct {
|
2016-03-03 17:21:21 +00:00
|
|
|
PluginConfig *PluginReattachConfig
|
|
|
|
AllocDir *allocdir.AllocDir
|
|
|
|
ExecutorPid int
|
|
|
|
KillTimeout time.Duration
|
|
|
|
MaxKillTimeout time.Duration
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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}
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 23:15:09 +00:00
|
|
|
// Validate is used to validate the driver configuration
|
2016-04-08 20:19:43 +00:00
|
|
|
func (d *RktDriver) Validate(config map[string]interface{}) error {
|
2016-04-09 22:38:42 +00:00
|
|
|
fd := &fields.FieldData{
|
|
|
|
Raw: config,
|
|
|
|
Schema: map[string]*fields.FieldSchema{
|
|
|
|
"image": &fields.FieldSchema{
|
|
|
|
Type: fields.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"args": &fields.FieldSchema{
|
|
|
|
Type: fields.TypeArray,
|
|
|
|
},
|
|
|
|
"dns_servers": &fields.FieldSchema{
|
|
|
|
Type: fields.TypeArray,
|
|
|
|
},
|
|
|
|
"dns_search_domains": &fields.FieldSchema{
|
|
|
|
Type: fields.TypeArray,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fd.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-08 20:19:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 21:48:10 +00:00
|
|
|
func (d *RktDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
|
2016-04-01 09:22:17 +00:00
|
|
|
// Get the current status so that we can log any debug messages only if the
|
|
|
|
// state changes
|
|
|
|
_, currentlyEnabled := node.Attributes[rktDriverAttr]
|
|
|
|
|
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 {
|
2016-04-01 09:22:17 +00:00
|
|
|
if currentlyEnabled {
|
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: must run as root user, disabling")
|
|
|
|
}
|
2016-04-01 01:11:27 +00:00
|
|
|
delete(node.Attributes, rktDriverAttr)
|
2015-10-07 22:24:16 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
outBytes, err := exec.Command("rkt", "version").Output()
|
|
|
|
if err != nil {
|
2016-04-01 01:11:27 +00:00
|
|
|
delete(node.Attributes, rktDriverAttr)
|
2015-10-07 22:24:16 +00:00
|
|
|
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 {
|
2016-04-01 01:11:27 +00:00
|
|
|
delete(node.Attributes, rktDriverAttr)
|
2015-10-07 22:24:16 +00:00
|
|
|
return false, fmt.Errorf("Unable to parse Rkt version string: %#v", rktMatches)
|
|
|
|
}
|
|
|
|
|
2016-04-01 01:11:27 +00:00
|
|
|
node.Attributes[rktDriverAttr] = "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)
|
2015-12-21 17:48:21 +00:00
|
|
|
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)
|
2016-04-01 01:11:27 +00:00
|
|
|
node.Attributes[rktDriverAttr] = "0"
|
2015-12-21 17:48:21 +00:00
|
|
|
}
|
2015-10-07 22:24:16 +00:00
|
|
|
return true, nil
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2015-11-14 02:09:42 +00:00
|
|
|
if err := mapstructure.WeakDecode(task.Config, &driverConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-09 17:52:44 +00:00
|
|
|
// Validate that the config is valid.
|
2015-11-14 02:09:42 +00:00
|
|
|
img := driverConfig.ImageName
|
|
|
|
if img == "" {
|
2015-10-12 21:37:56 +00:00
|
|
|
return nil, fmt.Errorf("Missing ACI image for rkt")
|
2015-10-09 17:52:44 +00:00
|
|
|
}
|
|
|
|
|
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"]
|
2016-02-05 21:28:41 +00:00
|
|
|
insecure := false
|
2015-12-21 10:10:37 +00:00
|
|
|
if trustCmd {
|
2015-10-12 21:37:56 +00:00
|
|
|
var outBuf, errBuf bytes.Buffer
|
2016-02-05 21:28:41 +00:00
|
|
|
cmd := exec.Command("rkt", "trust", "--skip-fingerprint-review=true", fmt.Sprintf("--prefix=%s", trustPrefix))
|
2015-10-12 21:37:56 +00:00
|
|
|
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.
|
2016-02-05 21:28:41 +00:00
|
|
|
insecure = true
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:22:49 +00:00
|
|
|
cmdArgs = append(cmdArgs, "run")
|
2016-03-02 19:28:50 +00:00
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--volume=%s,kind=host,source=%s", task.Name, ctx.AllocDir.SharedDir))
|
2016-02-05 21:28:41 +00:00
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--mount=volume=%s,target=%s", task.Name, ctx.AllocDir.SharedDir))
|
|
|
|
cmdArgs = append(cmdArgs, img)
|
|
|
|
if insecure == true {
|
2015-12-25 15:59:54 +00:00
|
|
|
cmdArgs = append(cmdArgs, "--insecure-options=all")
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// Inject environment 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
|
|
|
}
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// Check if the user has overridden 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
|
|
|
}
|
2015-10-09 17:52:44 +00:00
|
|
|
|
2015-12-21 06:06:45 +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")
|
|
|
|
}
|
2015-12-21 06:06:45 +00:00
|
|
|
|
|
|
|
// Add memory isolator
|
2015-12-22 18:11:22 +00:00
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--memory=%vM", int64(task.Resources.MemoryMB)*bytesToMB))
|
2015-12-21 06:06:45 +00:00
|
|
|
|
|
|
|
// Add CPU isolator
|
2015-12-21 10:10:37 +00:00
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--cpu=%vm", int64(task.Resources.CPU)))
|
2015-12-21 06:06:45 +00:00
|
|
|
|
2016-03-08 05:28:24 +00:00
|
|
|
// Add DNS servers
|
|
|
|
for _, ip := range driverConfig.DNSServers {
|
|
|
|
if err := net.ParseIP(ip); err == nil {
|
|
|
|
msg := fmt.Errorf("invalid ip address for container dns server %q", ip)
|
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: %v", msg)
|
|
|
|
return nil, msg
|
|
|
|
} else {
|
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--dns=%s", ip))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set DNS search domains
|
|
|
|
for _, domain := range driverConfig.DNSSearchDomains {
|
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("--dns-search=%s", domain))
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:52:44 +00:00
|
|
|
// Add user passed arguments.
|
2015-11-18 23:16:42 +00:00
|
|
|
if len(driverConfig.Args) != 0 {
|
2016-01-11 17:58:26 +00:00
|
|
|
parsed := d.taskEnv.ParseAndReplace(driverConfig.Args)
|
2015-10-09 17:52:44 +00:00
|
|
|
|
|
|
|
// Need to start arguments with "--"
|
|
|
|
if len(parsed) > 0 {
|
2015-12-21 10:10:37 +00:00
|
|
|
cmdArgs = append(cmdArgs, "--")
|
2015-10-09 17:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, arg := range parsed {
|
2015-12-21 10:10:37 +00:00
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("%v", arg))
|
2015-10-09 17:52:44 +00:00
|
|
|
}
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
2015-10-09 17:52:44 +00:00
|
|
|
|
2016-02-17 00:10:45 +00:00
|
|
|
bin, err := discover.NomadExecutable()
|
2015-10-09 19:14:56 +00:00
|
|
|
if err != nil {
|
2016-02-17 00:10:45 +00:00
|
|
|
return nil, fmt.Errorf("unable to find the nomad binary: %v", err)
|
2015-10-09 19:14:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 00:10:45 +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
|
|
|
}
|
|
|
|
|
2016-03-16 03:21:52 +00:00
|
|
|
execIntf, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
|
2016-02-17 00:10:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
executorCtx := &executor.ExecutorContext{
|
2016-03-24 22:39:10 +00:00
|
|
|
TaskEnv: d.taskEnv,
|
|
|
|
Driver: "rkt",
|
|
|
|
AllocDir: ctx.AllocDir,
|
|
|
|
AllocID: ctx.AllocID,
|
|
|
|
Task: task,
|
2016-02-17 00:10:45 +00:00
|
|
|
}
|
2015-10-09 19:14:56 +00:00
|
|
|
|
2016-03-16 02:22:40 +00:00
|
|
|
absPath, err := GetAbsolutePath("rkt")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-23 11:57:31 +00:00
|
|
|
ps, err := execIntf.LaunchCmd(&executor.ExecCommand{
|
|
|
|
Cmd: absPath,
|
|
|
|
Args: cmdArgs,
|
|
|
|
User: task.User,
|
|
|
|
}, executorCtx)
|
2016-02-17 00:10:45 +00:00
|
|
|
if err != nil {
|
|
|
|
pluginClient.Kill()
|
2016-03-19 19:18:10 +00:00
|
|
|
return nil, err
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
2015-10-09 17:52:44 +00:00
|
|
|
|
2016-02-17 00:10:45 +00:00
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: started ACI %q with: %v", img, cmdArgs)
|
2016-03-03 17:21:21 +00:00
|
|
|
maxKill := d.DriverContext.config.MaxKillTimeout
|
2015-10-07 22:24:16 +00:00
|
|
|
h := &rktHandle{
|
2016-03-03 17:21:21 +00:00
|
|
|
pluginClient: pluginClient,
|
2016-03-16 03:21:52 +00:00
|
|
|
executor: execIntf,
|
2016-03-03 17:21:21 +00:00
|
|
|
executorPid: ps.Pid,
|
|
|
|
allocDir: ctx.AllocDir,
|
|
|
|
logger: d.logger,
|
|
|
|
killTimeout: GetKillTimeout(task.KillTimeout, maxKill),
|
|
|
|
maxKillTimeout: maxKill,
|
|
|
|
doneCh: make(chan struct{}),
|
|
|
|
waitCh: make(chan *cstructs.WaitResult, 1),
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
2016-03-24 22:39:10 +00:00
|
|
|
if h.executor.SyncServices(consulContext(d.config, "")); err != nil {
|
2016-03-24 21:53:53 +00:00
|
|
|
h.logger.Printf("[ERR] driver.rkt: error registering services for task: %q: %v", task.Name, err)
|
2016-03-23 20:19:45 +00:00
|
|
|
}
|
2015-10-07 22:24:16 +00:00
|
|
|
go h.run()
|
|
|
|
return h, nil
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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:"))
|
2016-03-03 17:21:21 +00:00
|
|
|
id := &rktPID{}
|
|
|
|
if err := json.Unmarshal(pidBytes, id); err != nil {
|
2015-10-07 22:24:16 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse Rkt handle '%s': %v", handleID, err)
|
|
|
|
}
|
|
|
|
|
2016-02-17 00:10:45 +00:00
|
|
|
pluginConfig := &plugin.ClientConfig{
|
2016-03-03 17:21:21 +00:00
|
|
|
Reattach: id.PluginConfig.PluginConfig(),
|
2016-02-17 00:10:45 +00:00
|
|
|
}
|
2016-03-29 23:27:31 +00:00
|
|
|
exec, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
|
2016-02-17 00:10:45 +00:00
|
|
|
if err != nil {
|
|
|
|
d.logger.Println("[ERROR] driver.rkt: error connecting to plugin so destroying plugin pid and user pid")
|
2016-03-03 17:21:21 +00:00
|
|
|
if e := destroyPlugin(id.PluginConfig.Pid, id.ExecutorPid); e != nil {
|
2016-02-19 00:46:54 +00:00
|
|
|
d.logger.Printf("[ERROR] driver.rkt: error destroying plugin and executor pid: %v", e)
|
2016-02-17 00:10:45 +00:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("error connecting to plugin: %v", err)
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 05:05:02 +00:00
|
|
|
ver, _ := exec.Version()
|
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: version of executor: %v", ver.Version)
|
2015-10-07 22:24:16 +00:00
|
|
|
// Return a driver handle
|
|
|
|
h := &rktHandle{
|
2016-03-03 17:21:21 +00:00
|
|
|
pluginClient: pluginClient,
|
|
|
|
executorPid: id.ExecutorPid,
|
|
|
|
allocDir: id.AllocDir,
|
2016-03-29 23:27:31 +00:00
|
|
|
executor: exec,
|
2016-03-03 17:21:21 +00:00
|
|
|
logger: d.logger,
|
|
|
|
killTimeout: id.KillTimeout,
|
|
|
|
maxKillTimeout: id.MaxKillTimeout,
|
|
|
|
doneCh: make(chan struct{}),
|
|
|
|
waitCh: make(chan *cstructs.WaitResult, 1),
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
2016-03-24 22:39:10 +00:00
|
|
|
if h.executor.SyncServices(consulContext(d.config, "")); err != nil {
|
|
|
|
h.logger.Printf("[ERR] driver.rkt: error registering services: %v", err)
|
|
|
|
}
|
2015-10-07 22:24:16 +00:00
|
|
|
go h.run()
|
|
|
|
return h, nil
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *rktHandle) ID() string {
|
2015-10-07 22:24:16 +00:00
|
|
|
// Return a handle to the PID
|
|
|
|
pid := &rktPID{
|
2016-03-03 17:21:21 +00:00
|
|
|
PluginConfig: NewPluginReattachConfig(h.pluginClient.ReattachConfig()),
|
|
|
|
KillTimeout: h.killTimeout,
|
|
|
|
MaxKillTimeout: h.maxKillTimeout,
|
|
|
|
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))
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 06:07:13 +00:00
|
|
|
func (h *rktHandle) WaitCh() chan *cstructs.WaitResult {
|
2015-10-07 22:24:16 +00:00
|
|
|
return h.waitCh
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *rktHandle) Update(task *structs.Task) error {
|
2016-02-04 03:43:44 +00:00
|
|
|
// Store the updated kill timeout.
|
2016-03-03 17:21:21 +00:00
|
|
|
h.killTimeout = GetKillTimeout(task.KillTimeout, h.maxKillTimeout)
|
2016-03-17 09:53:31 +00:00
|
|
|
h.executor.UpdateTask(task)
|
2016-02-04 03:43:44 +00:00
|
|
|
|
2015-10-07 22:24:16 +00:00
|
|
|
// Update is not possible
|
|
|
|
return nil
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-02-17 00:10:45 +00:00
|
|
|
h.executor.ShutDown()
|
2015-10-07 22:24:16 +00:00
|
|
|
select {
|
|
|
|
case <-h.doneCh:
|
|
|
|
return nil
|
2015-12-23 00:10:30 +00:00
|
|
|
case <-time.After(h.killTimeout):
|
2016-02-17 00:10:45 +00:00
|
|
|
return h.executor.Exit()
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 23:06:01 +00:00
|
|
|
func (h *rktHandle) Stats() (*cstructs.TaskResourceUsage, error) {
|
|
|
|
return nil, fmt.Errorf("stats not implemented for rkt")
|
|
|
|
}
|
|
|
|
|
2015-09-29 21:48:10 +00:00
|
|
|
func (h *rktHandle) run() {
|
2016-02-17 00:10:45 +00:00
|
|
|
ps, err := h.executor.Wait()
|
2015-10-07 22:24:16 +00:00
|
|
|
close(h.doneCh)
|
2016-02-19 02:57:13 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 17:30:53 +00:00
|
|
|
h.waitCh <- cstructs.NewWaitResult(ps.ExitCode, 0, err)
|
2015-10-07 22:24:16 +00:00
|
|
|
close(h.waitCh)
|
2016-03-23 20:19:45 +00:00
|
|
|
// Remove services
|
|
|
|
if err := h.executor.DeregisterServices(); err != nil {
|
|
|
|
h.logger.Printf("[ERR] driver.rkt: failed to deregister services: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-03-18 19:04:11 +00:00
|
|
|
if err := h.executor.Exit(); err != nil {
|
|
|
|
h.logger.Printf("[ERR] driver.rkt: error killing executor: %v", err)
|
|
|
|
}
|
2016-02-17 00:10:45 +00:00
|
|
|
h.pluginClient.Kill()
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|