2016-02-05 00:03:17 +00:00
|
|
|
package executor
|
2016-02-04 00:03:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-03-17 09:53:31 +00:00
|
|
|
"io/ioutil"
|
2016-02-04 00:03:43 +00:00
|
|
|
"log"
|
2016-03-17 09:53:31 +00:00
|
|
|
"net"
|
2016-02-04 00:03:43 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-03-19 19:18:10 +00:00
|
|
|
"path/filepath"
|
2016-02-04 00:03:43 +00:00
|
|
|
"runtime"
|
2016-05-21 09:05:08 +00:00
|
|
|
"strconv"
|
2016-02-05 18:49:54 +00:00
|
|
|
"strings"
|
2016-02-04 00:03:43 +00:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2016-02-05 18:49:54 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2016-05-11 19:56:47 +00:00
|
|
|
"github.com/mitchellh/go-ps"
|
2016-02-04 00:03:43 +00:00
|
|
|
cgroupConfig "github.com/opencontainers/runc/libcontainer/configs"
|
2016-05-11 19:56:47 +00:00
|
|
|
"github.com/shirou/gopsutil/process"
|
2016-02-04 00:03:43 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2016-03-23 19:19:19 +00:00
|
|
|
"github.com/hashicorp/nomad/client/consul"
|
2016-02-04 00:03:43 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/env"
|
2016-02-19 22:01:07 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/logging"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/driver/structs"
|
2016-05-19 20:32:03 +00:00
|
|
|
"github.com/hashicorp/nomad/client/stats"
|
2016-02-04 00:03:43 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2016-05-18 05:11:25 +00:00
|
|
|
const (
|
2016-05-25 05:54:32 +00:00
|
|
|
// pidScanInterval is the interval at which the executor scans the process
|
|
|
|
// tree for finding out the pids that the executor and it's child processes
|
|
|
|
// have forked
|
2016-05-18 05:11:25 +00:00
|
|
|
pidScanInterval = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
2016-06-10 02:45:41 +00:00
|
|
|
var (
|
|
|
|
// The statistics the basic executor exposes
|
|
|
|
ExecutorBasicMeasuredMemStats = []string{"RSS", "Swap"}
|
2016-06-10 17:38:29 +00:00
|
|
|
ExecutorBasicMeasuredCpuStats = []string{"System Mode", "User Mode", "Percent"}
|
2016-06-10 02:45:41 +00:00
|
|
|
)
|
|
|
|
|
2016-03-17 09:53:31 +00:00
|
|
|
// Executor is the interface which allows a driver to launch and supervise
|
|
|
|
// a process
|
|
|
|
type Executor interface {
|
|
|
|
LaunchCmd(command *ExecCommand, ctx *ExecutorContext) (*ProcessState, error)
|
|
|
|
LaunchSyslogServer(ctx *ExecutorContext) (*SyslogServerState, error)
|
|
|
|
Wait() (*ProcessState, error)
|
|
|
|
ShutDown() error
|
|
|
|
Exit() error
|
|
|
|
UpdateLogConfig(logConfig *structs.LogConfig) error
|
|
|
|
UpdateTask(task *structs.Task) error
|
2016-03-24 22:39:10 +00:00
|
|
|
SyncServices(ctx *ConsulContext) error
|
2016-03-23 19:19:19 +00:00
|
|
|
DeregisterServices() error
|
2016-03-30 05:05:02 +00:00
|
|
|
Version() (*ExecutorVersion, error)
|
2016-04-28 23:06:01 +00:00
|
|
|
Stats() (*cstructs.TaskResourceUsage, error)
|
2016-03-17 09:53:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 22:39:10 +00:00
|
|
|
// ConsulContext holds context to configure the consul client and run checks
|
|
|
|
type ConsulContext struct {
|
|
|
|
// ConsulConfig is the configuration used to create a consul client
|
2016-05-21 21:27:21 +00:00
|
|
|
ConsulConfig *consul.AgentConfig
|
2016-03-24 22:39:10 +00:00
|
|
|
|
|
|
|
// ContainerID is the ID of the container
|
|
|
|
ContainerID string
|
2016-03-24 23:15:22 +00:00
|
|
|
|
|
|
|
// TLSCert is the cert which docker client uses while interactng with the docker
|
|
|
|
// daemon over TLS
|
|
|
|
TLSCert string
|
|
|
|
|
|
|
|
// TLSCa is the CA which the docker client uses while interacting with the docker
|
|
|
|
// daeemon over TLS
|
|
|
|
TLSCa string
|
|
|
|
|
2016-03-25 23:56:40 +00:00
|
|
|
// TLSKey is the TLS key which the docker client uses while interacting with
|
|
|
|
// the docker daemon
|
2016-03-24 23:15:22 +00:00
|
|
|
TLSKey string
|
|
|
|
|
|
|
|
// DockerEndpoint is the endpoint of the docker daemon
|
|
|
|
DockerEndpoint string
|
2016-03-24 22:39:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 18:49:54 +00:00
|
|
|
// ExecutorContext holds context to configure the command user
|
|
|
|
// wants to run and isolate it
|
2016-02-04 00:03:43 +00:00
|
|
|
type ExecutorContext struct {
|
2016-02-06 01:07:02 +00:00
|
|
|
// TaskEnv holds information about the environment of a Task
|
|
|
|
TaskEnv *env.TaskEnvironment
|
|
|
|
|
|
|
|
// AllocDir is the handle to do operations on the alloc dir of
|
|
|
|
// the task
|
|
|
|
AllocDir *allocdir.AllocDir
|
|
|
|
|
2016-03-17 09:53:31 +00:00
|
|
|
// Task is the task whose executor is being launched
|
|
|
|
Task *structs.Task
|
2016-02-06 01:07:02 +00:00
|
|
|
|
2016-03-24 02:02:29 +00:00
|
|
|
// AllocID is the allocation id to which the task belongs
|
2016-03-24 00:35:29 +00:00
|
|
|
AllocID string
|
|
|
|
|
2016-03-24 17:06:40 +00:00
|
|
|
// Driver is the name of the driver that invoked the executor
|
|
|
|
Driver string
|
|
|
|
|
2016-03-17 09:53:31 +00:00
|
|
|
// PortUpperBound is the upper bound of the ports that we can use to start
|
|
|
|
// the syslog server
|
|
|
|
PortUpperBound uint
|
2016-02-06 01:07:02 +00:00
|
|
|
|
2016-03-17 09:53:31 +00:00
|
|
|
// PortLowerBound is the lower bound of the ports that we can use to start
|
|
|
|
// the syslog server
|
|
|
|
PortLowerBound uint
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 09:53:31 +00:00
|
|
|
// ExecCommand holds the user command, args, and other isolation related
|
|
|
|
// settings.
|
2016-02-04 00:03:43 +00:00
|
|
|
type ExecCommand struct {
|
2016-03-17 09:53:31 +00:00
|
|
|
// Cmd is the command that the user wants to run.
|
|
|
|
Cmd string
|
|
|
|
|
|
|
|
// Args is the args of the command that the user wants to run.
|
2016-02-04 00:03:43 +00:00
|
|
|
Args []string
|
2016-03-17 09:53:31 +00:00
|
|
|
|
|
|
|
// FSIsolation determines whether the command would be run in a chroot.
|
|
|
|
FSIsolation bool
|
|
|
|
|
|
|
|
// User is the user which the executor uses to run the command.
|
|
|
|
User string
|
|
|
|
|
|
|
|
// ResourceLimits determines whether resource limits are enforced by the
|
|
|
|
// executor.
|
|
|
|
ResourceLimits bool
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 18:49:54 +00:00
|
|
|
// ProcessState holds information about the state of a user process.
|
2016-02-04 00:03:43 +00:00
|
|
|
type ProcessState struct {
|
2016-02-08 18:05:39 +00:00
|
|
|
Pid int
|
|
|
|
ExitCode int
|
|
|
|
Signal int
|
2016-02-19 22:01:07 +00:00
|
|
|
IsolationConfig *cstructs.IsolationConfig
|
2016-02-08 18:05:39 +00:00
|
|
|
Time time.Time
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 05:54:32 +00:00
|
|
|
// nomadPid holds a pid and it's cpu percentage calculator
|
|
|
|
type nomadPid struct {
|
2016-06-10 03:06:32 +00:00
|
|
|
pid int
|
|
|
|
cpuStatsTotal *stats.CpuStats
|
|
|
|
cpuStatsUser *stats.CpuStats
|
|
|
|
cpuStatsSys *stats.CpuStats
|
2016-05-20 09:05:48 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 09:53:31 +00:00
|
|
|
// SyslogServerState holds the address and islation information of a launched
|
|
|
|
// syslog server
|
|
|
|
type SyslogServerState struct {
|
|
|
|
IsolationConfig *cstructs.IsolationConfig
|
|
|
|
Addr string
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 05:05:02 +00:00
|
|
|
// ExecutorVersion is the version of the executor
|
|
|
|
type ExecutorVersion struct {
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *ExecutorVersion) GoString() string {
|
|
|
|
return v.Version
|
|
|
|
}
|
|
|
|
|
2016-02-05 00:18:10 +00:00
|
|
|
// UniversalExecutor is an implementation of the Executor which launches and
|
|
|
|
// supervises processes. In addition to process supervision it provides resource
|
|
|
|
// and file system isolation
|
2016-02-04 00:03:43 +00:00
|
|
|
type UniversalExecutor struct {
|
2016-03-17 09:53:31 +00:00
|
|
|
cmd exec.Cmd
|
|
|
|
ctx *ExecutorContext
|
|
|
|
command *ExecCommand
|
2016-02-04 00:03:43 +00:00
|
|
|
|
2016-05-25 05:54:32 +00:00
|
|
|
pids []*nomadPid
|
|
|
|
pidLock sync.RWMutex
|
2016-02-04 00:26:10 +00:00
|
|
|
taskDir string
|
|
|
|
exitState *ProcessState
|
|
|
|
processExited chan interface{}
|
2016-03-17 09:53:31 +00:00
|
|
|
|
|
|
|
lre *logging.FileRotator
|
|
|
|
lro *logging.FileRotator
|
|
|
|
rotatorLock sync.Mutex
|
|
|
|
|
|
|
|
syslogServer *logging.SyslogServer
|
|
|
|
syslogChan chan *logging.SyslogMessage
|
|
|
|
|
2016-04-02 08:06:41 +00:00
|
|
|
groups *cgroupConfig.Cgroup
|
|
|
|
cgPaths map[string]string
|
|
|
|
cgLock sync.Mutex
|
2016-02-04 00:03:43 +00:00
|
|
|
|
2016-06-10 17:38:29 +00:00
|
|
|
consulService *consul.ConsulService
|
|
|
|
consulCtx *ConsulContext
|
|
|
|
totalCpuStats *stats.CpuStats
|
|
|
|
userCpuStats *stats.CpuStats
|
|
|
|
systemCpuStats *stats.CpuStats
|
|
|
|
logger *log.Logger
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 00:18:10 +00:00
|
|
|
// NewExecutor returns an Executor
|
2016-02-04 00:03:43 +00:00
|
|
|
func NewExecutor(logger *log.Logger) Executor {
|
2016-05-19 20:32:03 +00:00
|
|
|
exec := &UniversalExecutor{
|
2016-06-10 17:38:29 +00:00
|
|
|
logger: logger,
|
|
|
|
processExited: make(chan interface{}),
|
|
|
|
totalCpuStats: stats.NewCpuStats(),
|
|
|
|
userCpuStats: stats.NewCpuStats(),
|
|
|
|
systemCpuStats: stats.NewCpuStats(),
|
2016-05-19 20:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return exec
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 23:27:31 +00:00
|
|
|
// Version returns the api version of the executor
|
2016-03-30 05:05:02 +00:00
|
|
|
func (e *UniversalExecutor) Version() (*ExecutorVersion, error) {
|
|
|
|
return &ExecutorVersion{Version: "1.0.0"}, nil
|
2016-03-29 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 00:18:10 +00:00
|
|
|
// LaunchCmd launches a process and returns it's state. It also configures an
|
|
|
|
// applies isolation on certain platforms.
|
2016-02-04 00:03:43 +00:00
|
|
|
func (e *UniversalExecutor) LaunchCmd(command *ExecCommand, ctx *ExecutorContext) (*ProcessState, error) {
|
2016-02-17 01:13:19 +00:00
|
|
|
e.logger.Printf("[DEBUG] executor: launching command %v %v", command.Cmd, strings.Join(command.Args, " "))
|
2016-02-05 01:36:31 +00:00
|
|
|
|
2016-02-04 00:03:43 +00:00
|
|
|
e.ctx = ctx
|
2016-03-17 09:53:31 +00:00
|
|
|
e.command = command
|
2016-02-04 19:51:43 +00:00
|
|
|
|
2016-02-05 08:11:09 +00:00
|
|
|
// configuring the task dir
|
2016-02-04 00:03:43 +00:00
|
|
|
if err := e.configureTaskDir(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-05 08:11:09 +00:00
|
|
|
|
2016-02-06 01:40:06 +00:00
|
|
|
// configuring the chroot, cgroup and enters the plugin process in the
|
|
|
|
// chroot
|
2016-02-04 00:03:43 +00:00
|
|
|
if err := e.configureIsolation(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-02-05 08:11:09 +00:00
|
|
|
// setting the user of the process
|
2016-03-17 09:53:31 +00:00
|
|
|
if command.User != "" {
|
2016-03-23 11:57:31 +00:00
|
|
|
e.logger.Printf("[DEBUG] executor: running command as %s", command.User)
|
2016-03-17 09:53:31 +00:00
|
|
|
if err := e.runAs(command.User); err != nil {
|
2016-02-04 00:09:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 09:53:31 +00:00
|
|
|
// Setup the loggers
|
|
|
|
if err := e.configureLoggers(); err != nil {
|
|
|
|
return nil, err
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
2016-03-17 09:53:31 +00:00
|
|
|
e.cmd.Stdout = e.lro
|
|
|
|
e.cmd.Stderr = e.lre
|
2016-02-04 00:03:43 +00:00
|
|
|
|
2016-02-05 18:49:54 +00:00
|
|
|
e.ctx.TaskEnv.Build()
|
2016-03-16 02:22:40 +00:00
|
|
|
|
2016-03-19 19:18:10 +00:00
|
|
|
// Look up the binary path and make it executable
|
|
|
|
absPath, err := e.lookupBin(ctx.TaskEnv.ReplaceEnv(command.Cmd))
|
|
|
|
if err != nil {
|
2016-03-16 02:22:40 +00:00
|
|
|
return nil, err
|
2016-02-04 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 19:18:10 +00:00
|
|
|
if err := e.makeExecutable(absPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the path to run as it may have to be relative to the chroot.
|
|
|
|
path := absPath
|
2016-03-19 19:39:15 +00:00
|
|
|
if e.command.FSIsolation {
|
2016-03-19 19:18:10 +00:00
|
|
|
rel, err := filepath.Rel(e.taskDir, absPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path = rel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the commands arguments
|
|
|
|
e.cmd.Path = path
|
|
|
|
e.cmd.Args = append([]string{path}, ctx.TaskEnv.ParseAndReplace(command.Args)...)
|
|
|
|
e.cmd.Env = ctx.TaskEnv.EnvList()
|
|
|
|
|
2016-04-19 00:20:11 +00:00
|
|
|
// Apply ourselves into the cgroup. The executor MUST be in the cgroup
|
|
|
|
// before the user task is started, otherwise we are subject to a fork
|
|
|
|
// attack in which a process escapes isolation by immediately forking.
|
|
|
|
if err := e.applyLimits(os.Getpid()); err != nil {
|
2016-03-19 19:18:10 +00:00
|
|
|
return nil, err
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
2016-04-19 00:20:11 +00:00
|
|
|
|
|
|
|
// Start the process
|
|
|
|
if err := e.cmd.Start(); err != nil {
|
2016-04-02 08:06:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-11 19:56:47 +00:00
|
|
|
go e.collectPids()
|
2016-02-04 00:26:10 +00:00
|
|
|
go e.wait()
|
2016-04-02 08:06:41 +00:00
|
|
|
ic := &cstructs.IsolationConfig{Cgroup: e.groups, CgroupPaths: e.cgPaths}
|
2016-02-08 21:48:26 +00:00
|
|
|
return &ProcessState{Pid: e.cmd.Process.Pid, ExitCode: -1, IsolationConfig: ic, Time: time.Now()}, nil
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 09:53:31 +00:00
|
|
|
// configureLoggers sets up the standard out/error file rotators
|
|
|
|
func (e *UniversalExecutor) configureLoggers() error {
|
|
|
|
e.rotatorLock.Lock()
|
|
|
|
defer e.rotatorLock.Unlock()
|
|
|
|
|
|
|
|
logFileSize := int64(e.ctx.Task.LogConfig.MaxFileSizeMB * 1024 * 1024)
|
|
|
|
if e.lro == nil {
|
|
|
|
lro, err := logging.NewFileRotator(e.ctx.AllocDir.LogDir(), fmt.Sprintf("%v.stdout", e.ctx.Task.Name),
|
|
|
|
e.ctx.Task.LogConfig.MaxFiles, logFileSize, e.logger)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
e.lro = lro
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.lre == nil {
|
|
|
|
lre, err := logging.NewFileRotator(e.ctx.AllocDir.LogDir(), fmt.Sprintf("%v.stderr", e.ctx.Task.Name),
|
|
|
|
e.ctx.Task.LogConfig.MaxFiles, logFileSize, e.logger)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
e.lre = lre
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-05 00:18:10 +00:00
|
|
|
// Wait waits until a process has exited and returns it's exitcode and errors
|
2016-02-04 00:03:43 +00:00
|
|
|
func (e *UniversalExecutor) Wait() (*ProcessState, error) {
|
2016-02-04 00:26:10 +00:00
|
|
|
<-e.processExited
|
|
|
|
return e.exitState, nil
|
|
|
|
}
|
|
|
|
|
2016-03-18 22:04:15 +00:00
|
|
|
// COMPAT: prior to Nomad 0.3.2, UpdateTask didn't exist.
|
2016-02-10 16:13:08 +00:00
|
|
|
// UpdateLogConfig updates the log configuration
|
2016-02-08 18:10:01 +00:00
|
|
|
func (e *UniversalExecutor) UpdateLogConfig(logConfig *structs.LogConfig) error {
|
2016-03-17 09:53:31 +00:00
|
|
|
e.ctx.Task.LogConfig = logConfig
|
2016-02-08 18:10:01 +00:00
|
|
|
if e.lro == nil {
|
|
|
|
return fmt.Errorf("log rotator for stdout doesn't exist")
|
|
|
|
}
|
|
|
|
e.lro.MaxFiles = logConfig.MaxFiles
|
|
|
|
e.lro.FileSize = int64(logConfig.MaxFileSizeMB * 1024 * 1024)
|
|
|
|
|
|
|
|
if e.lre == nil {
|
|
|
|
return fmt.Errorf("log rotator for stderr doesn't exist")
|
|
|
|
}
|
|
|
|
e.lre.MaxFiles = logConfig.MaxFiles
|
|
|
|
e.lre.FileSize = int64(logConfig.MaxFileSizeMB * 1024 * 1024)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-17 09:53:31 +00:00
|
|
|
func (e *UniversalExecutor) UpdateTask(task *structs.Task) error {
|
|
|
|
e.ctx.Task = task
|
2016-03-18 22:04:15 +00:00
|
|
|
|
|
|
|
// Updating Log Config
|
2016-03-17 09:53:31 +00:00
|
|
|
fileSize := int64(task.LogConfig.MaxFileSizeMB * 1024 * 1024)
|
|
|
|
e.lro.MaxFiles = task.LogConfig.MaxFiles
|
|
|
|
e.lro.FileSize = fileSize
|
|
|
|
e.lre.MaxFiles = task.LogConfig.MaxFiles
|
|
|
|
e.lre.FileSize = fileSize
|
2016-03-23 21:34:43 +00:00
|
|
|
|
|
|
|
// Re-syncing task with consul service
|
|
|
|
if e.consulService != nil {
|
2016-04-12 05:55:19 +00:00
|
|
|
if err := e.consulService.SyncServices(task.Services); err != nil {
|
2016-03-24 02:20:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-03-23 21:34:43 +00:00
|
|
|
}
|
2016-03-24 02:20:08 +00:00
|
|
|
return nil
|
2016-03-17 09:53:31 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 00:26:10 +00:00
|
|
|
func (e *UniversalExecutor) wait() {
|
2016-02-04 18:21:33 +00:00
|
|
|
defer close(e.processExited)
|
2016-02-04 00:03:43 +00:00
|
|
|
err := e.cmd.Wait()
|
2016-04-02 19:36:35 +00:00
|
|
|
ic := &cstructs.IsolationConfig{Cgroup: e.groups, CgroupPaths: e.cgPaths}
|
2016-02-04 00:03:43 +00:00
|
|
|
if err == nil {
|
2016-04-02 19:36:35 +00:00
|
|
|
e.exitState = &ProcessState{Pid: 0, ExitCode: 0, IsolationConfig: ic, Time: time.Now()}
|
2016-02-04 00:26:10 +00:00
|
|
|
return
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
exitCode := 1
|
2016-04-01 20:28:20 +00:00
|
|
|
var signal int
|
2016-02-04 00:03:43 +00:00
|
|
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
|
|
|
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
|
|
|
|
exitCode = status.ExitStatus()
|
2016-04-01 20:28:20 +00:00
|
|
|
if status.Signaled() {
|
2016-05-08 07:13:28 +00:00
|
|
|
// bash(1) uses the lower 7 bits of a uint8
|
|
|
|
// to indicate normal program failure (see
|
|
|
|
// <sysexits.h>). If a process terminates due
|
|
|
|
// to a signal, encode the signal number to
|
|
|
|
// indicate which signal caused the process
|
|
|
|
// to terminate. Mirror this exit code
|
|
|
|
// encoding scheme.
|
|
|
|
const exitSignalBase = 128
|
2016-04-01 20:28:20 +00:00
|
|
|
signal = int(status.Signal())
|
2016-05-08 07:13:28 +00:00
|
|
|
exitCode = exitSignalBase + signal
|
2016-04-01 20:28:20 +00:00
|
|
|
}
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
2016-04-19 22:54:21 +00:00
|
|
|
} else {
|
|
|
|
e.logger.Printf("[DEBUG] executor: unexpected Wait() error type: %v", err)
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
2016-04-19 22:54:21 +00:00
|
|
|
|
2016-04-02 19:36:35 +00:00
|
|
|
e.exitState = &ProcessState{Pid: 0, ExitCode: exitCode, Signal: signal, IsolationConfig: ic, Time: time.Now()}
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-09 18:00:42 +00:00
|
|
|
var (
|
|
|
|
// finishedErr is the error message received when trying to kill and already
|
|
|
|
// exited process.
|
|
|
|
finishedErr = "os: process already finished"
|
|
|
|
)
|
|
|
|
|
2016-02-05 00:18:10 +00:00
|
|
|
// Exit cleans up the alloc directory, destroys cgroups and kills the user
|
|
|
|
// process
|
2016-02-04 00:03:43 +00:00
|
|
|
func (e *UniversalExecutor) Exit() error {
|
2016-02-05 18:49:54 +00:00
|
|
|
var merr multierror.Error
|
2016-03-18 19:04:11 +00:00
|
|
|
if e.syslogServer != nil {
|
|
|
|
e.syslogServer.Shutdown()
|
|
|
|
}
|
|
|
|
e.lre.Close()
|
|
|
|
e.lro.Close()
|
|
|
|
|
2016-04-19 20:48:02 +00:00
|
|
|
// If the executor did not launch a process, return.
|
|
|
|
if e.command == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefer killing the process via cgroups.
|
|
|
|
if e.cmd.Process != nil && !e.command.ResourceLimits {
|
2016-02-05 18:49:54 +00:00
|
|
|
proc, err := os.FindProcess(e.cmd.Process.Pid)
|
|
|
|
if err != nil {
|
2016-02-24 23:57:58 +00:00
|
|
|
e.logger.Printf("[ERR] executor: can't find process with pid: %v, err: %v",
|
2016-02-08 19:56:48 +00:00
|
|
|
e.cmd.Process.Pid, err)
|
2016-02-09 18:00:42 +00:00
|
|
|
} else if err := proc.Kill(); err != nil && err.Error() != finishedErr {
|
2016-02-08 19:56:48 +00:00
|
|
|
merr.Errors = append(merr.Errors,
|
|
|
|
fmt.Errorf("can't kill process with pid: %v, err: %v", e.cmd.Process.Pid, err))
|
2016-02-05 18:49:54 +00:00
|
|
|
}
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
2016-02-05 18:49:54 +00:00
|
|
|
|
2016-04-19 20:48:02 +00:00
|
|
|
if e.command.ResourceLimits {
|
2016-03-17 09:53:31 +00:00
|
|
|
e.cgLock.Lock()
|
2016-04-19 00:20:11 +00:00
|
|
|
if err := DestroyCgroup(e.groups, e.cgPaths, os.Getpid()); err != nil {
|
2016-02-05 18:49:54 +00:00
|
|
|
merr.Errors = append(merr.Errors, err)
|
|
|
|
}
|
2016-03-17 09:53:31 +00:00
|
|
|
e.cgLock.Unlock()
|
2016-02-04 20:40:48 +00:00
|
|
|
}
|
2016-04-19 20:48:02 +00:00
|
|
|
|
|
|
|
if e.command.FSIsolation {
|
|
|
|
if err := e.removeChrootMounts(); err != nil {
|
|
|
|
merr.Errors = append(merr.Errors, err)
|
|
|
|
}
|
|
|
|
}
|
2016-02-05 18:49:54 +00:00
|
|
|
return merr.ErrorOrNil()
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 00:18:10 +00:00
|
|
|
// Shutdown sends an interrupt signal to the user process
|
2016-02-04 00:03:43 +00:00
|
|
|
func (e *UniversalExecutor) ShutDown() error {
|
2016-02-04 21:22:38 +00:00
|
|
|
if e.cmd.Process == nil {
|
|
|
|
return fmt.Errorf("executor.shutdown error: no process found")
|
|
|
|
}
|
2016-02-04 00:03:43 +00:00
|
|
|
proc, err := os.FindProcess(e.cmd.Process.Pid)
|
|
|
|
if err != nil {
|
2016-04-19 20:48:02 +00:00
|
|
|
return fmt.Errorf("executor.shutdown failed to find process: %v", err)
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
if runtime.GOOS == "windows" {
|
2016-04-19 20:48:02 +00:00
|
|
|
if err := proc.Kill(); err != nil && err.Error() != finishedErr {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
2016-04-19 20:48:02 +00:00
|
|
|
if err = proc.Signal(os.Interrupt); err != nil && err.Error() != finishedErr {
|
2016-02-04 20:40:48 +00:00
|
|
|
return fmt.Errorf("executor.shutdown error: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 14:41:11 +00:00
|
|
|
// SyncServices syncs the services of the task that the executor is running with
|
|
|
|
// Consul
|
2016-03-24 22:39:10 +00:00
|
|
|
func (e *UniversalExecutor) SyncServices(ctx *ConsulContext) error {
|
2016-03-24 01:08:32 +00:00
|
|
|
e.logger.Printf("[INFO] executor: registering services")
|
2016-03-24 22:39:10 +00:00
|
|
|
e.consulCtx = ctx
|
2016-03-23 19:19:19 +00:00
|
|
|
if e.consulService == nil {
|
2016-04-12 05:55:19 +00:00
|
|
|
cs, err := consul.NewConsulService(ctx.ConsulConfig, e.logger)
|
2016-03-23 19:19:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-24 17:06:40 +00:00
|
|
|
cs.SetDelegatedChecks(e.createCheckMap(), e.createCheck)
|
2016-05-14 07:43:25 +00:00
|
|
|
cs.SetServiceIdentifier(consul.GenerateServiceIdentifier(e.ctx.AllocID, e.ctx.Task.Name))
|
2016-04-12 05:55:19 +00:00
|
|
|
cs.SetAddrFinder(e.ctx.Task.FindHostAndPortFor)
|
2016-03-23 19:19:19 +00:00
|
|
|
e.consulService = cs
|
|
|
|
}
|
2016-03-26 19:49:49 +00:00
|
|
|
if e.ctx != nil {
|
|
|
|
e.interpolateServices(e.ctx.Task)
|
|
|
|
}
|
2016-04-12 05:55:19 +00:00
|
|
|
err := e.consulService.SyncServices(e.ctx.Task.Services)
|
2016-03-24 01:13:13 +00:00
|
|
|
go e.consulService.PeriodicSync()
|
2016-03-23 19:59:22 +00:00
|
|
|
return err
|
2016-03-23 19:19:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 14:41:11 +00:00
|
|
|
// DeregisterServices removes the services of the task that the executor is
|
|
|
|
// running from Consul
|
2016-03-23 19:19:19 +00:00
|
|
|
func (e *UniversalExecutor) DeregisterServices() error {
|
2016-03-24 02:20:08 +00:00
|
|
|
e.logger.Printf("[INFO] executor: de-registering services and shutting down consul service")
|
2016-03-23 19:19:19 +00:00
|
|
|
if e.consulService != nil {
|
|
|
|
return e.consulService.Shutdown()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-25 05:54:32 +00:00
|
|
|
// pidStats returns the resource usage stats per pid
|
|
|
|
func (e *UniversalExecutor) pidStats() (map[string]*cstructs.ResourceUsage, error) {
|
2016-05-21 09:05:08 +00:00
|
|
|
stats := make(map[string]*cstructs.ResourceUsage)
|
2016-05-25 05:54:32 +00:00
|
|
|
e.pidLock.RLock()
|
|
|
|
pids := make([]*nomadPid, len(e.pids))
|
|
|
|
copy(pids, e.pids)
|
|
|
|
e.pidLock.RUnlock()
|
2016-05-31 23:09:05 +00:00
|
|
|
for _, pid := range pids {
|
2016-05-20 09:05:48 +00:00
|
|
|
p, err := process.NewProcess(int32(pid.pid))
|
2016-05-19 14:41:11 +00:00
|
|
|
if err != nil {
|
2016-05-20 09:05:48 +00:00
|
|
|
e.logger.Printf("[DEBUG] executor: unable to create new process with pid: %v", pid.pid)
|
2016-05-19 14:41:11 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-05-20 09:05:48 +00:00
|
|
|
ms := &cstructs.MemoryStats{}
|
|
|
|
if memInfo, err := p.MemoryInfo(); err == nil {
|
|
|
|
ms.RSS = memInfo.RSS
|
|
|
|
ms.Swap = memInfo.Swap
|
2016-06-10 02:45:41 +00:00
|
|
|
ms.Measured = ExecutorBasicMeasuredMemStats
|
2016-05-19 14:41:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 07:49:17 +00:00
|
|
|
cs := &cstructs.CpuStats{}
|
2016-05-20 09:05:48 +00:00
|
|
|
if cpuStats, err := p.Times(); err == nil {
|
2016-06-10 18:52:15 +00:00
|
|
|
cs.SystemMode = pid.cpuStatsSys.Percent(cpuStats.System * float64(time.Second))
|
|
|
|
cs.UserMode = pid.cpuStatsUser.Percent(cpuStats.User * float64(time.Second))
|
2016-06-10 02:45:41 +00:00
|
|
|
cs.Measured = ExecutorBasicMeasuredCpuStats
|
2016-05-20 09:05:48 +00:00
|
|
|
|
|
|
|
// calculate cpu usage percent
|
2016-06-10 18:52:15 +00:00
|
|
|
cs.Percent = pid.cpuStatsTotal.Percent(cpuStats.Total() * float64(time.Second))
|
2016-05-19 14:41:11 +00:00
|
|
|
}
|
2016-05-27 21:15:51 +00:00
|
|
|
stats[strconv.Itoa(pid.pid)] = &cstructs.ResourceUsage{MemoryStats: ms, CpuStats: cs}
|
2016-05-19 14:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
2016-02-08 19:56:48 +00:00
|
|
|
// configureTaskDir sets the task dir in the executor
|
2016-02-04 00:03:43 +00:00
|
|
|
func (e *UniversalExecutor) configureTaskDir() error {
|
2016-03-17 09:53:31 +00:00
|
|
|
taskDir, ok := e.ctx.AllocDir.TaskDirs[e.ctx.Task.Name]
|
2016-02-04 00:03:43 +00:00
|
|
|
e.taskDir = taskDir
|
|
|
|
if !ok {
|
2016-03-17 09:53:31 +00:00
|
|
|
return fmt.Errorf("couldn't find task directory for task %v", e.ctx.Task.Name)
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
e.cmd.Dir = taskDir
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-16 02:22:40 +00:00
|
|
|
|
2016-03-19 19:18:10 +00:00
|
|
|
// lookupBin looks for path to the binary to run by looking for the binary in
|
|
|
|
// the following locations, in-order: task/local/, task/, based on host $PATH.
|
|
|
|
// The return path is absolute.
|
|
|
|
func (e *UniversalExecutor) lookupBin(bin string) (string, error) {
|
|
|
|
// Check in the local directory
|
|
|
|
local := filepath.Join(e.taskDir, allocdir.TaskLocal, bin)
|
|
|
|
if _, err := os.Stat(local); err == nil {
|
|
|
|
return local, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check at the root of the task's directory
|
|
|
|
root := filepath.Join(e.taskDir, bin)
|
|
|
|
if _, err := os.Stat(root); err == nil {
|
|
|
|
return root, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the $PATH
|
|
|
|
if host, err := exec.LookPath(bin); err == nil {
|
|
|
|
return host, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("binary %q could not be found", bin)
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeExecutable makes the given file executable for root,group,others.
|
|
|
|
func (e *UniversalExecutor) makeExecutable(binPath string) error {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-16 02:22:40 +00:00
|
|
|
fi, err := os.Stat(binPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("binary %q does not exist", binPath)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("specified binary is invalid: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is not executable, make it so.
|
|
|
|
perm := fi.Mode().Perm()
|
|
|
|
req := os.FileMode(0555)
|
|
|
|
if perm&req != req {
|
|
|
|
if err := os.Chmod(binPath, perm|req); err != nil {
|
|
|
|
return fmt.Errorf("error making %q executable: %s", binPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-17 09:53:31 +00:00
|
|
|
|
|
|
|
// getFreePort returns a free port ready to be listened on between upper and
|
|
|
|
// lower bounds
|
|
|
|
func (e *UniversalExecutor) getListener(lowerBound uint, upperBound uint) (net.Listener, error) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return e.listenerTCP(lowerBound, upperBound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.listenerUnix()
|
|
|
|
}
|
|
|
|
|
|
|
|
// listenerTCP creates a TCP listener using an unused port between an upper and
|
|
|
|
// lower bound
|
|
|
|
func (e *UniversalExecutor) listenerTCP(lowerBound uint, upperBound uint) (net.Listener, error) {
|
|
|
|
for i := lowerBound; i <= upperBound; i++ {
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:%v", i))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
l, err := net.ListenTCP("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("No free port found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// listenerUnix creates a Unix domain socket
|
|
|
|
func (e *UniversalExecutor) listenerUnix() (net.Listener, error) {
|
|
|
|
f, err := ioutil.TempFile("", "plugin")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path := f.Name()
|
|
|
|
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return net.Listen("unix", path)
|
|
|
|
}
|
2016-03-24 17:06:40 +00:00
|
|
|
|
2016-03-25 02:34:22 +00:00
|
|
|
// createCheckMap creates a map of checks that the executor will handle on it's
|
|
|
|
// own
|
2016-03-24 17:06:40 +00:00
|
|
|
func (e *UniversalExecutor) createCheckMap() map[string]struct{} {
|
|
|
|
checks := map[string]struct{}{
|
|
|
|
"script": struct{}{},
|
|
|
|
}
|
|
|
|
return checks
|
|
|
|
}
|
|
|
|
|
2016-03-25 02:34:22 +00:00
|
|
|
// createCheck creates NomadCheck from a ServiceCheck
|
2016-03-24 20:05:08 +00:00
|
|
|
func (e *UniversalExecutor) createCheck(check *structs.ServiceCheck, checkID string) (consul.Check, error) {
|
2016-03-24 17:06:40 +00:00
|
|
|
if check.Type == structs.ServiceCheckScript && e.ctx.Driver == "docker" {
|
|
|
|
return &DockerScriptCheck{
|
|
|
|
id: checkID,
|
2016-03-25 02:00:24 +00:00
|
|
|
interval: check.Interval,
|
2016-05-05 17:01:38 +00:00
|
|
|
timeout: check.Timeout,
|
2016-03-24 22:39:10 +00:00
|
|
|
containerID: e.consulCtx.ContainerID,
|
2016-03-24 17:06:40 +00:00
|
|
|
logger: e.logger,
|
2016-03-28 21:05:12 +00:00
|
|
|
cmd: check.Command,
|
2016-03-24 17:06:40 +00:00
|
|
|
args: check.Args,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:27:01 +00:00
|
|
|
if check.Type == structs.ServiceCheckScript && (e.ctx.Driver == "exec" ||
|
|
|
|
e.ctx.Driver == "raw_exec" || e.ctx.Driver == "java") {
|
2016-05-09 10:35:19 +00:00
|
|
|
return &ExecScriptCheck{
|
|
|
|
id: checkID,
|
|
|
|
interval: check.Interval,
|
|
|
|
timeout: check.Timeout,
|
|
|
|
cmd: check.Command,
|
|
|
|
args: check.Args,
|
|
|
|
taskDir: e.taskDir,
|
|
|
|
FSIsolation: e.command.FSIsolation,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
2016-03-24 17:06:40 +00:00
|
|
|
return nil, fmt.Errorf("couldn't create check for %v", check.Name)
|
|
|
|
}
|
2016-03-26 19:49:49 +00:00
|
|
|
|
2016-03-28 19:25:16 +00:00
|
|
|
// interpolateServices interpolates tags in a service and checks with values from the
|
2016-03-26 19:51:53 +00:00
|
|
|
// task's environment.
|
2016-03-26 19:49:49 +00:00
|
|
|
func (e *UniversalExecutor) interpolateServices(task *structs.Task) {
|
|
|
|
e.ctx.TaskEnv.Build()
|
|
|
|
for _, service := range task.Services {
|
2016-03-28 19:25:16 +00:00
|
|
|
for _, check := range service.Checks {
|
|
|
|
if check.Type == structs.ServiceCheckScript {
|
2016-03-28 21:36:51 +00:00
|
|
|
check.Name = e.ctx.TaskEnv.ReplaceEnv(check.Name)
|
|
|
|
check.Command = e.ctx.TaskEnv.ReplaceEnv(check.Command)
|
2016-03-28 19:25:16 +00:00
|
|
|
check.Args = e.ctx.TaskEnv.ParseAndReplace(check.Args)
|
2016-03-28 21:36:51 +00:00
|
|
|
check.Path = e.ctx.TaskEnv.ReplaceEnv(check.Path)
|
|
|
|
check.Protocol = e.ctx.TaskEnv.ReplaceEnv(check.Protocol)
|
2016-03-28 19:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-28 21:36:51 +00:00
|
|
|
service.Name = e.ctx.TaskEnv.ReplaceEnv(service.Name)
|
2016-03-26 19:49:49 +00:00
|
|
|
service.Tags = e.ctx.TaskEnv.ParseAndReplace(service.Tags)
|
|
|
|
}
|
|
|
|
}
|
2016-05-11 19:56:47 +00:00
|
|
|
|
2016-05-19 14:41:11 +00:00
|
|
|
// collectPids collects the pids of the child processes that the executor is
|
|
|
|
// running every 5 seconds
|
2016-05-11 19:56:47 +00:00
|
|
|
func (e *UniversalExecutor) collectPids() {
|
2016-05-26 17:46:38 +00:00
|
|
|
// Fire the timer right away when the executor starts from there on the pids
|
|
|
|
// are collected every scan interval
|
|
|
|
timer := time.NewTimer(0)
|
2016-05-26 22:12:48 +00:00
|
|
|
defer timer.Stop()
|
2016-05-11 19:56:47 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
pids, err := e.getAllPids()
|
|
|
|
if err != nil {
|
|
|
|
e.logger.Printf("[DEBUG] executor: error collecting pids: %v", err)
|
|
|
|
}
|
2016-05-25 05:54:32 +00:00
|
|
|
e.pidLock.Lock()
|
2016-05-11 19:56:47 +00:00
|
|
|
e.pids = pids
|
2016-05-25 05:54:32 +00:00
|
|
|
e.pidLock.Unlock()
|
|
|
|
timer.Reset(pidScanInterval)
|
2016-05-11 19:56:47 +00:00
|
|
|
case <-e.processExited:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 14:41:11 +00:00
|
|
|
// scanPids scans all the pids on the machine running the current executor and
|
|
|
|
// returns the child processes of the executor.
|
2016-05-25 21:58:32 +00:00
|
|
|
func (e *UniversalExecutor) scanPids(parentPid int, allPids []ps.Process) ([]*nomadPid, error) {
|
2016-05-11 19:56:47 +00:00
|
|
|
processFamily := make(map[int]struct{})
|
2016-05-25 21:58:32 +00:00
|
|
|
processFamily[parentPid] = struct{}{}
|
|
|
|
|
|
|
|
// A buffer for holding pids which haven't matched with any parent pid
|
|
|
|
var pidsRemaining []ps.Process
|
|
|
|
for {
|
|
|
|
// flag to indicate if we have found a match
|
|
|
|
foundNewPid := false
|
|
|
|
|
|
|
|
for _, pid := range allPids {
|
|
|
|
_, childPid := processFamily[pid.PPid()]
|
|
|
|
|
|
|
|
// checking if the pid is a child of any of the parents
|
2016-05-26 00:46:24 +00:00
|
|
|
if childPid {
|
2016-05-25 21:58:32 +00:00
|
|
|
processFamily[pid.Pid()] = struct{}{}
|
|
|
|
foundNewPid = true
|
|
|
|
} else {
|
|
|
|
// if it is not, then we add the pid to the buffer
|
|
|
|
pidsRemaining = append(pidsRemaining, pid)
|
|
|
|
}
|
|
|
|
// scan only the pids which are left in the buffer
|
|
|
|
allPids = pidsRemaining
|
|
|
|
}
|
|
|
|
|
|
|
|
// not scanning anymore if we couldn't find a single match
|
|
|
|
if !foundNewPid {
|
|
|
|
break
|
2016-05-11 19:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-25 05:54:32 +00:00
|
|
|
res := make([]*nomadPid, 0, len(processFamily))
|
2016-05-11 19:56:47 +00:00
|
|
|
for pid := range processFamily {
|
2016-06-10 03:45:16 +00:00
|
|
|
res = append(res, &nomadPid{
|
|
|
|
pid: pid,
|
|
|
|
cpuStatsTotal: stats.NewCpuStats(),
|
|
|
|
cpuStatsUser: stats.NewCpuStats(),
|
|
|
|
cpuStatsSys: stats.NewCpuStats(),
|
|
|
|
})
|
2016-05-11 19:56:47 +00:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2016-05-26 07:53:41 +00:00
|
|
|
|
|
|
|
// aggregatedResourceUsage aggregates the resource usage of all the pids and
|
|
|
|
// returns a TaskResourceUsage data point
|
|
|
|
func (e *UniversalExecutor) aggregatedResourceUsage(pidStats map[string]*cstructs.ResourceUsage) *cstructs.TaskResourceUsage {
|
2016-05-27 21:15:51 +00:00
|
|
|
ts := time.Now().UTC().UnixNano()
|
2016-05-26 07:53:41 +00:00
|
|
|
var (
|
|
|
|
systemModeCPU, userModeCPU, percent float64
|
|
|
|
totalRSS, totalSwap uint64
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, pidStat := range pidStats {
|
|
|
|
systemModeCPU += pidStat.CpuStats.SystemMode
|
|
|
|
userModeCPU += pidStat.CpuStats.UserMode
|
|
|
|
percent += pidStat.CpuStats.Percent
|
|
|
|
|
|
|
|
totalRSS += pidStat.MemoryStats.RSS
|
|
|
|
totalSwap += pidStat.MemoryStats.Swap
|
|
|
|
}
|
|
|
|
|
|
|
|
totalCPU := &cstructs.CpuStats{
|
|
|
|
SystemMode: systemModeCPU,
|
|
|
|
UserMode: userModeCPU,
|
|
|
|
Percent: percent,
|
2016-06-10 17:38:29 +00:00
|
|
|
Measured: ExecutorBasicMeasuredCpuStats,
|
2016-05-26 07:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
totalMemory := &cstructs.MemoryStats{
|
2016-06-10 02:45:41 +00:00
|
|
|
RSS: totalRSS,
|
|
|
|
Swap: totalSwap,
|
2016-06-10 17:38:29 +00:00
|
|
|
Measured: ExecutorBasicMeasuredMemStats,
|
2016-05-26 07:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resourceUsage := cstructs.ResourceUsage{
|
|
|
|
MemoryStats: totalMemory,
|
|
|
|
CpuStats: totalCPU,
|
|
|
|
}
|
|
|
|
return &cstructs.TaskResourceUsage{
|
|
|
|
ResourceUsage: &resourceUsage,
|
|
|
|
Timestamp: ts,
|
|
|
|
Pids: pidStats,
|
|
|
|
}
|
|
|
|
}
|