2016-02-05 00:03:17 +00:00
|
|
|
package executor
|
2016-02-04 00:03:43 +00:00
|
|
|
|
|
|
|
import (
|
2017-04-21 23:20:37 +00:00
|
|
|
"context"
|
2016-02-04 00:03:43 +00:00
|
|
|
"fmt"
|
2018-04-17 17:43:04 +00:00
|
|
|
"io"
|
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"
|
|
|
|
|
2017-04-21 23:20:37 +00:00
|
|
|
"github.com/armon/circbuf"
|
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"
|
|
|
|
"github.com/shirou/gopsutil/process"
|
2016-02-04 00:03:43 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
|
|
|
"github.com/hashicorp/nomad/client/driver/env"
|
2016-02-19 22:01:07 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/logging"
|
2016-05-19 20:32:03 +00:00
|
|
|
"github.com/hashicorp/nomad/client/stats"
|
2016-06-17 20:23:30 +00:00
|
|
|
shelpers "github.com/hashicorp/nomad/helper/stats"
|
2016-02-04 00:03:43 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2016-06-12 03:15:50 +00:00
|
|
|
|
2018-04-17 17:43:04 +00:00
|
|
|
syslog "github.com/RackSec/srslog"
|
2018-04-19 18:16:28 +00:00
|
|
|
|
2016-06-12 03:15:50 +00:00
|
|
|
dstructs "github.com/hashicorp/nomad/client/driver/structs"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2016-02-04 00:03:43 +00:00
|
|
|
)
|
|
|
|
|
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
|
2018-05-31 22:21:36 +00:00
|
|
|
|
|
|
|
// processOutputCloseTolerance is the length of time we will wait for the
|
|
|
|
// launched process to close its stdout/stderr before we force close it. If
|
2018-05-31 22:29:55 +00:00
|
|
|
// data is written after this tolerance, we will not capture it.
|
2018-05-31 22:21:36 +00:00
|
|
|
processOutputCloseTolerance = 2 * time.Second
|
2016-05-18 05:11:25 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
2016-10-12 18:35:29 +00:00
|
|
|
SetContext(ctx *ExecutorContext) error
|
|
|
|
LaunchCmd(command *ExecCommand) (*ProcessState, error)
|
|
|
|
LaunchSyslogServer() (*SyslogServerState, error)
|
2016-03-17 09:53:31 +00:00
|
|
|
Wait() (*ProcessState, error)
|
|
|
|
ShutDown() error
|
|
|
|
Exit() error
|
|
|
|
UpdateLogConfig(logConfig *structs.LogConfig) error
|
|
|
|
UpdateTask(task *structs.Task) 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-10-10 18:46:27 +00:00
|
|
|
Signal(s os.Signal) error
|
2017-04-21 23:20:37 +00:00
|
|
|
Exec(deadline time.Time, cmd string, args []string) ([]byte, int, error)
|
2016-03-17 09:53:31 +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
|
2017-05-17 00:33:50 +00:00
|
|
|
TaskEnv *env.TaskEnv
|
2016-02-06 01:07:02 +00:00
|
|
|
|
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-12-03 01:04:07 +00:00
|
|
|
// TaskDir is the host path to the task's root
|
|
|
|
TaskDir string
|
|
|
|
|
|
|
|
// LogDir is the host path where logs should be written
|
|
|
|
LogDir string
|
2016-08-04 22:03:56 +00:00
|
|
|
|
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
|
|
|
|
2017-11-30 21:53:35 +00:00
|
|
|
// TaskKillSignal is an optional field which signal to kill the process
|
|
|
|
TaskKillSignal os.Signal
|
2017-11-21 22:27:52 +00:00
|
|
|
|
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
|
2018-05-25 22:25:23 +00:00
|
|
|
|
|
|
|
// Cgroup marks whether we put the process in a cgroup. Setting this field
|
|
|
|
// doesn't enforce resource limits. To enforce limits, set ResoruceLimits.
|
|
|
|
// Using the cgroup does allow more precise cleanup of processes.
|
|
|
|
Cgroup 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-06-12 03:15:50 +00:00
|
|
|
IsolationConfig *dstructs.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
|
|
|
}
|
|
|
|
|
2018-03-11 18:19:02 +00:00
|
|
|
// SyslogServerState holds the address and isolation information of a launched
|
2016-03-17 09:53:31 +00:00
|
|
|
// syslog server
|
|
|
|
type SyslogServerState struct {
|
2016-06-12 03:15:50 +00:00
|
|
|
IsolationConfig *dstructs.IsolationConfig
|
2016-03-17 09:53:31 +00:00
|
|
|
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-06-12 13:41:31 +00:00
|
|
|
pids map[int]*nomadPid
|
|
|
|
pidLock sync.RWMutex
|
|
|
|
exitState *ProcessState
|
|
|
|
processExited chan interface{}
|
|
|
|
fsIsolationEnforced bool
|
2016-03-17 09:53:31 +00:00
|
|
|
|
2018-05-24 22:54:28 +00:00
|
|
|
lre *logRotatorWrapper
|
|
|
|
lro *logRotatorWrapper
|
2016-03-17 09:53:31 +00:00
|
|
|
rotatorLock sync.Mutex
|
|
|
|
|
|
|
|
syslogServer *logging.SyslogServer
|
|
|
|
syslogChan chan *logging.SyslogMessage
|
|
|
|
|
2016-07-11 07:02:55 +00:00
|
|
|
resConCtx resourceContainerContext
|
2016-02-04 00:03:43 +00:00
|
|
|
|
2016-06-10 17:38:29 +00:00
|
|
|
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-06-17 20:23:30 +00:00
|
|
|
if err := shelpers.Init(); err != nil {
|
2017-03-14 19:56:31 +00:00
|
|
|
logger.Printf("[ERR] executor: unable to initialize stats: %v", err)
|
2016-06-17 20:23:30 +00:00
|
|
|
}
|
|
|
|
|
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-06-10 23:16:52 +00:00
|
|
|
pids: make(map[int]*nomadPid),
|
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) {
|
2017-02-01 00:43:57 +00:00
|
|
|
return &ExecutorVersion{Version: "1.1.0"}, nil
|
2016-03-29 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 18:35:29 +00:00
|
|
|
// SetContext is used to set the executors context and should be the first call
|
|
|
|
// after launching the executor.
|
|
|
|
func (e *UniversalExecutor) SetContext(ctx *ExecutorContext) error {
|
|
|
|
e.ctx = ctx
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-21 23:20:37 +00:00
|
|
|
// LaunchCmd launches the main process and returns its state. It also
|
|
|
|
// configures an applies isolation on certain platforms.
|
2016-10-12 18:35:29 +00:00
|
|
|
func (e *UniversalExecutor) LaunchCmd(command *ExecCommand) (*ProcessState, error) {
|
2018-03-21 18:53:58 +00:00
|
|
|
e.logger.Printf("[INFO] executor: launching command %v %v", command.Cmd, strings.Join(command.Args, " "))
|
2016-02-05 01:36:31 +00:00
|
|
|
|
2016-10-12 18:35:29 +00:00
|
|
|
// Ensure the context has been set first
|
|
|
|
if e.ctx == nil {
|
|
|
|
return nil, fmt.Errorf("SetContext must be called before launching a command")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// 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-12-03 01:04:07 +00:00
|
|
|
// set the task dir as the working directory for the command
|
|
|
|
e.cmd.Dir = e.ctx.TaskDir
|
2016-02-05 08:11:09 +00:00
|
|
|
|
2017-11-17 01:26:25 +00:00
|
|
|
// start command in separate process group
|
|
|
|
if err := e.setNewProcessGroup(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-07-10 07:12:59 +00:00
|
|
|
// configuring the chroot, resource container, and start the plugin
|
|
|
|
// process in the chroot.
|
2016-02-04 00:03:43 +00:00
|
|
|
if err := e.configureIsolation(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-10 07:12:59 +00:00
|
|
|
// Apply ourselves into the resource container. The executor MUST be in
|
|
|
|
// the resource container before the user task is started, otherwise we
|
|
|
|
// are subject to a fork attack in which a process escapes isolation by
|
|
|
|
// immediately forking.
|
2016-04-19 00:20:11 +00:00
|
|
|
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-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
|
|
|
}
|
2018-05-24 22:54:28 +00:00
|
|
|
e.cmd.Stdout = e.lro.processOutWriter
|
|
|
|
e.cmd.Stderr = e.lre.processOutWriter
|
2016-02-04 00:03:43 +00:00
|
|
|
|
2016-03-19 19:18:10 +00:00
|
|
|
// Look up the binary path and make it executable
|
2016-10-12 18:35:29 +00:00
|
|
|
absPath, err := e.lookupBin(e.ctx.TaskEnv.ReplaceEnv(command.Cmd))
|
2016-03-19 19:18:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
path := absPath
|
2016-06-12 13:41:31 +00:00
|
|
|
|
|
|
|
// Determine the path to run as it may have to be relative to the chroot.
|
|
|
|
if e.fsIsolationEnforced {
|
2016-12-03 01:04:07 +00:00
|
|
|
rel, err := filepath.Rel(e.ctx.TaskDir, path)
|
2016-03-19 19:18:10 +00:00
|
|
|
if err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
return nil, fmt.Errorf("failed to determine relative path base=%q target=%q: %v", e.ctx.TaskDir, path, err)
|
2016-03-19 19:18:10 +00:00
|
|
|
}
|
|
|
|
path = rel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the commands arguments
|
|
|
|
e.cmd.Path = path
|
2016-10-12 18:35:29 +00:00
|
|
|
e.cmd.Args = append([]string{e.cmd.Path}, e.ctx.TaskEnv.ParseAndReplace(command.Args)...)
|
2017-05-17 00:33:50 +00:00
|
|
|
e.cmd.Env = e.ctx.TaskEnv.List()
|
2016-03-19 19:18:10 +00:00
|
|
|
|
2016-04-19 00:20:11 +00:00
|
|
|
// Start the process
|
|
|
|
if err := e.cmd.Start(); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
return nil, fmt.Errorf("failed to start command path=%q --- args=%q: %v", path, e.cmd.Args, err)
|
2016-04-02 08:06:41 +00:00
|
|
|
}
|
2018-05-31 22:21:36 +00:00
|
|
|
|
|
|
|
// Close the files. This is copied from the os/exec package.
|
|
|
|
e.lro.processOutWriter.Close()
|
|
|
|
e.lre.processOutWriter.Close()
|
|
|
|
|
2016-05-11 19:56:47 +00:00
|
|
|
go e.collectPids()
|
2016-02-04 00:26:10 +00:00
|
|
|
go e.wait()
|
2016-07-11 07:02:55 +00:00
|
|
|
ic := e.resConCtx.getIsolationConfig()
|
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
|
|
|
}
|
|
|
|
|
2017-04-21 23:20:37 +00:00
|
|
|
// Exec a command inside a container for exec and java drivers.
|
|
|
|
func (e *UniversalExecutor) Exec(deadline time.Time, name string, args []string) ([]byte, int, error) {
|
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), deadline)
|
|
|
|
defer cancel()
|
2017-05-04 23:21:40 +00:00
|
|
|
return ExecScript(ctx, e.cmd.Dir, e.ctx.TaskEnv, e.cmd.SysProcAttr, name, args)
|
|
|
|
}
|
2017-04-21 23:20:37 +00:00
|
|
|
|
2017-05-04 23:21:40 +00:00
|
|
|
// ExecScript executes cmd with args and returns the output, exit code, and
|
|
|
|
// error. Output is truncated to client/driver/structs.CheckBufSize
|
2017-05-17 00:33:50 +00:00
|
|
|
func ExecScript(ctx context.Context, dir string, env *env.TaskEnv, attrs *syscall.SysProcAttr,
|
2017-05-04 23:21:40 +00:00
|
|
|
name string, args []string) ([]byte, int, error) {
|
|
|
|
name = env.ReplaceEnv(name)
|
|
|
|
cmd := exec.CommandContext(ctx, name, env.ParseAndReplace(args)...)
|
2017-04-21 23:20:37 +00:00
|
|
|
|
|
|
|
// Copy runtime environment from the main command
|
2017-05-04 23:21:40 +00:00
|
|
|
cmd.SysProcAttr = attrs
|
|
|
|
cmd.Dir = dir
|
2017-05-17 00:33:50 +00:00
|
|
|
cmd.Env = env.List()
|
2017-04-21 23:20:37 +00:00
|
|
|
|
|
|
|
// Capture output
|
|
|
|
buf, _ := circbuf.NewBuffer(int64(dstructs.CheckBufSize))
|
|
|
|
cmd.Stdout = buf
|
|
|
|
cmd.Stderr = buf
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
exitErr, ok := err.(*exec.ExitError)
|
|
|
|
if !ok {
|
|
|
|
// Non-exit error, return it and let the caller treat
|
|
|
|
// it as a critical failure
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some kind of error happened; default to critical
|
|
|
|
exitCode := 2
|
|
|
|
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
|
|
|
|
exitCode = status.ExitStatus()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't return the exitError as the caller only needs the
|
|
|
|
// output and code.
|
|
|
|
return buf.Bytes(), exitCode, nil
|
|
|
|
}
|
|
|
|
return buf.Bytes(), 0, nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-12-03 01:04:07 +00:00
|
|
|
lro, err := logging.NewFileRotator(e.ctx.LogDir, fmt.Sprintf("%v.stdout", e.ctx.Task.Name),
|
2016-03-17 09:53:31 +00:00
|
|
|
e.ctx.Task.LogConfig.MaxFiles, logFileSize, e.logger)
|
|
|
|
if err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
return fmt.Errorf("error creating new stdout log file for %q: %v", e.ctx.Task.Name, err)
|
2016-03-17 09:53:31 +00:00
|
|
|
}
|
2018-05-24 22:54:28 +00:00
|
|
|
|
2018-05-24 23:25:20 +00:00
|
|
|
r, err := NewLogRotatorWrapper(lro)
|
2018-05-24 22:54:28 +00:00
|
|
|
if err != nil {
|
2018-05-24 23:25:20 +00:00
|
|
|
return err
|
2018-05-24 22:54:28 +00:00
|
|
|
}
|
2018-05-24 23:25:20 +00:00
|
|
|
e.lro = r
|
2016-03-17 09:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if e.lre == nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
lre, err := logging.NewFileRotator(e.ctx.LogDir, fmt.Sprintf("%v.stderr", e.ctx.Task.Name),
|
2016-03-17 09:53:31 +00:00
|
|
|
e.ctx.Task.LogConfig.MaxFiles, logFileSize, e.logger)
|
|
|
|
if err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
return fmt.Errorf("error creating new stderr log file for %q: %v", e.ctx.Task.Name, err)
|
2016-03-17 09:53:31 +00:00
|
|
|
}
|
2018-05-24 22:54:28 +00:00
|
|
|
|
2018-05-24 23:25:20 +00:00
|
|
|
r, err := NewLogRotatorWrapper(lre)
|
2018-05-24 22:54:28 +00:00
|
|
|
if err != nil {
|
2018-05-24 23:25:20 +00:00
|
|
|
return err
|
2018-05-24 22:54:28 +00:00
|
|
|
}
|
2018-05-24 23:25:20 +00:00
|
|
|
e.lre = r
|
2016-03-17 09:53:31 +00:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
2018-05-24 22:54:28 +00:00
|
|
|
e.lro.rotatorWriter.MaxFiles = logConfig.MaxFiles
|
|
|
|
e.lro.rotatorWriter.FileSize = int64(logConfig.MaxFileSizeMB * 1024 * 1024)
|
2016-02-08 18:10:01 +00:00
|
|
|
|
|
|
|
if e.lre == nil {
|
|
|
|
return fmt.Errorf("log rotator for stderr doesn't exist")
|
|
|
|
}
|
2018-05-24 22:54:28 +00:00
|
|
|
e.lre.rotatorWriter.MaxFiles = logConfig.MaxFiles
|
|
|
|
e.lre.rotatorWriter.FileSize = int64(logConfig.MaxFileSizeMB * 1024 * 1024)
|
2016-02-08 18:10:01 +00:00
|
|
|
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-10-28 17:57:35 +00:00
|
|
|
e.rotatorLock.Lock()
|
|
|
|
if e.lro != nil && e.lre != nil {
|
|
|
|
fileSize := int64(task.LogConfig.MaxFileSizeMB * 1024 * 1024)
|
2018-05-24 22:54:28 +00:00
|
|
|
e.lro.rotatorWriter.MaxFiles = task.LogConfig.MaxFiles
|
|
|
|
e.lro.rotatorWriter.FileSize = fileSize
|
|
|
|
e.lre.rotatorWriter.MaxFiles = task.LogConfig.MaxFiles
|
|
|
|
e.lre.rotatorWriter.FileSize = fileSize
|
2016-10-28 17:57:35 +00:00
|
|
|
}
|
|
|
|
e.rotatorLock.Unlock()
|
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-07-11 07:02:55 +00:00
|
|
|
ic := e.resConCtx.getIsolationConfig()
|
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
|
|
|
}
|
2016-10-10 18:46:27 +00:00
|
|
|
|
|
|
|
e.lre.Close()
|
|
|
|
e.lro.Close()
|
|
|
|
|
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 {
|
2018-03-21 18:53:58 +00:00
|
|
|
e.logger.Printf("[WARN] executor: unexpected Cmd.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"
|
2017-11-23 10:57:26 +00:00
|
|
|
|
|
|
|
// noSuchProcessErr is the error message received when trying to kill a non
|
|
|
|
// existing process (e.g. when killing a process group).
|
|
|
|
noSuchProcessErr = "no such process"
|
2016-02-09 18:00:42 +00:00
|
|
|
)
|
|
|
|
|
2016-07-10 06:45:33 +00:00
|
|
|
// ClientCleanup is the cleanup routine that a Nomad Client uses to remove the
|
2018-03-11 18:41:26 +00:00
|
|
|
// remnants of a child UniversalExecutor.
|
2016-07-10 06:45:33 +00:00
|
|
|
func ClientCleanup(ic *dstructs.IsolationConfig, pid int) error {
|
|
|
|
return clientCleanup(ic, pid)
|
|
|
|
}
|
|
|
|
|
2016-07-10 07:12:59 +00:00
|
|
|
// Exit cleans up the alloc directory, destroys resource container 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()
|
|
|
|
}
|
2016-10-26 00:27:13 +00:00
|
|
|
|
|
|
|
if e.lre != nil {
|
|
|
|
e.lre.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.lro != nil {
|
|
|
|
e.lro.Close()
|
|
|
|
}
|
2016-03-18 19:04:11 +00:00
|
|
|
|
2016-04-19 20:48:02 +00:00
|
|
|
// If the executor did not launch a process, return.
|
|
|
|
if e.command == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-10 07:12:59 +00:00
|
|
|
// Prefer killing the process via the resource container.
|
2018-05-25 22:25:23 +00:00
|
|
|
if e.cmd.Process != nil && !(e.command.ResourceLimits || e.command.Cgroup) {
|
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)
|
2018-02-06 20:08:14 +00:00
|
|
|
} else if err := e.cleanupChildProcesses(proc); 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
|
|
|
|
2018-05-25 22:25:23 +00:00
|
|
|
if e.command.ResourceLimits || e.command.Cgroup {
|
2016-07-11 07:02:55 +00:00
|
|
|
if err := e.resConCtx.executorCleanup(); err != nil {
|
2016-02-05 18:49:54 +00:00
|
|
|
merr.Errors = append(merr.Errors, err)
|
|
|
|
}
|
2016-02-04 20:40:48 +00:00
|
|
|
}
|
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
|
|
|
}
|
2018-04-14 17:55:19 +00:00
|
|
|
return e.shutdownProcess(proc)
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2016-06-10 23:16:52 +00:00
|
|
|
pids := make(map[int]*nomadPid, len(e.pids))
|
|
|
|
for k, v := range e.pids {
|
|
|
|
pids[k] = v
|
|
|
|
}
|
2016-05-25 05:54:32 +00:00
|
|
|
e.pidLock.RUnlock()
|
2016-06-10 23:16:52 +00:00
|
|
|
for pid, np := range pids {
|
|
|
|
p, err := process.NewProcess(int32(pid))
|
2016-05-19 14:41:11 +00:00
|
|
|
if err != nil {
|
2017-01-09 19:21:51 +00:00
|
|
|
e.logger.Printf("[TRACE] executor: unable to create new process with pid: %v", 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 23:16:52 +00:00
|
|
|
cs.SystemMode = np.cpuStatsSys.Percent(cpuStats.System * float64(time.Second))
|
|
|
|
cs.UserMode = np.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 23:16:52 +00:00
|
|
|
cs.Percent = np.cpuStatsTotal.Percent(cpuStats.Total() * float64(time.Second))
|
2016-05-19 14:41:11 +00:00
|
|
|
}
|
2016-06-10 23:16:52 +00:00
|
|
|
stats[strconv.Itoa(pid)] = &cstructs.ResourceUsage{MemoryStats: ms, CpuStats: cs}
|
2016-05-19 14:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
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
|
2016-12-03 01:04:07 +00:00
|
|
|
local := filepath.Join(e.ctx.TaskDir, allocdir.TaskLocal, bin)
|
2016-03-19 19:18:10 +00:00
|
|
|
if _, err := os.Stat(local); err == nil {
|
|
|
|
return local, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check at the root of the task's directory
|
2016-12-03 01:04:07 +00:00
|
|
|
root := filepath.Join(e.ctx.TaskDir, bin)
|
2016-03-19 19:18:10 +00:00
|
|
|
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-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-06-10 23:40:52 +00:00
|
|
|
|
|
|
|
// Adding pids which are not being tracked
|
|
|
|
for pid, np := range pids {
|
|
|
|
if _, ok := e.pids[pid]; !ok {
|
|
|
|
e.pids[pid] = np
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Removing pids which are no longer present
|
|
|
|
for pid := range e.pids {
|
|
|
|
if _, ok := pids[pid]; !ok {
|
|
|
|
delete(e.pids, pid)
|
2016-06-10 23:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
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-06-10 23:40:52 +00:00
|
|
|
func (e *UniversalExecutor) scanPids(parentPid int, allPids []ps.Process) (map[int]*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{}{}
|
|
|
|
|
2016-09-27 23:57:26 +00:00
|
|
|
// A mapping of pids to their parent pids. It is used to build the process
|
|
|
|
// tree of the executing task
|
|
|
|
pidsRemaining := make(map[int]int, len(allPids))
|
|
|
|
for _, pid := range allPids {
|
|
|
|
pidsRemaining[pid.Pid()] = pid.PPid()
|
|
|
|
}
|
|
|
|
|
2016-05-25 21:58:32 +00:00
|
|
|
for {
|
|
|
|
// flag to indicate if we have found a match
|
|
|
|
foundNewPid := false
|
|
|
|
|
2016-09-27 23:57:26 +00:00
|
|
|
for pid, ppid := range pidsRemaining {
|
|
|
|
_, childPid := processFamily[ppid]
|
2016-05-25 21:58:32 +00:00
|
|
|
|
|
|
|
// checking if the pid is a child of any of the parents
|
2016-05-26 00:46:24 +00:00
|
|
|
if childPid {
|
2016-09-27 23:57:26 +00:00
|
|
|
processFamily[pid] = struct{}{}
|
|
|
|
delete(pidsRemaining, pid)
|
2016-05-25 21:58:32 +00:00
|
|
|
foundNewPid = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// not scanning anymore if we couldn't find a single match
|
|
|
|
if !foundNewPid {
|
|
|
|
break
|
2016-05-11 19:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-27 23:57:26 +00:00
|
|
|
|
2016-06-10 23:40:52 +00:00
|
|
|
res := make(map[int]*nomadPid)
|
2016-05-11 19:56:47 +00:00
|
|
|
for pid := range processFamily {
|
2016-06-10 23:16:52 +00:00
|
|
|
np := nomadPid{
|
2016-06-10 03:45:16 +00:00
|
|
|
pid: pid,
|
|
|
|
cpuStatsTotal: stats.NewCpuStats(),
|
|
|
|
cpuStatsUser: stats.NewCpuStats(),
|
|
|
|
cpuStatsSys: stats.NewCpuStats(),
|
2016-06-10 23:16:52 +00:00
|
|
|
}
|
2016-06-10 23:40:52 +00:00
|
|
|
res[pid] = &np
|
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-06-11 00:07:28 +00:00
|
|
|
TotalTicks: e.systemCpuStats.TicksConsumed(percent),
|
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,
|
|
|
|
}
|
|
|
|
}
|
2016-10-10 18:46:27 +00:00
|
|
|
|
|
|
|
// Signal sends the passed signal to the task
|
|
|
|
func (e *UniversalExecutor) Signal(s os.Signal) error {
|
|
|
|
if e.cmd.Process == nil {
|
|
|
|
return fmt.Errorf("Task not yet run")
|
|
|
|
}
|
|
|
|
|
2017-06-12 18:11:36 +00:00
|
|
|
e.logger.Printf("[DEBUG] executor: sending signal %s to PID %d", s, e.cmd.Process.Pid)
|
2016-10-10 18:46:27 +00:00
|
|
|
err := e.cmd.Process.Signal(s)
|
|
|
|
if err != nil {
|
2017-02-28 00:00:19 +00:00
|
|
|
e.logger.Printf("[ERR] executor: sending signal %v failed: %v", s, err)
|
2016-10-10 18:46:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-17 17:43:04 +00:00
|
|
|
|
|
|
|
func (e *UniversalExecutor) LaunchSyslogServer() (*SyslogServerState, error) {
|
|
|
|
// Ensure the context has been set first
|
|
|
|
if e.ctx == nil {
|
|
|
|
return nil, fmt.Errorf("SetContext must be called before launching the Syslog Server")
|
|
|
|
}
|
|
|
|
|
|
|
|
e.syslogChan = make(chan *logging.SyslogMessage, 2048)
|
|
|
|
l, err := e.getListener(e.ctx.PortLowerBound, e.ctx.PortUpperBound)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e.logger.Printf("[DEBUG] syslog-server: launching syslog server on addr: %v", l.Addr().String())
|
|
|
|
if err := e.configureLoggers(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
e.syslogServer = logging.NewSyslogServer(l, e.syslogChan, e.logger)
|
|
|
|
go e.syslogServer.Start()
|
2018-05-24 22:54:28 +00:00
|
|
|
go e.collectLogs(e.lre.rotatorWriter, e.lro.rotatorWriter)
|
2018-04-17 17:43:04 +00:00
|
|
|
syslogAddr := fmt.Sprintf("%s://%s", l.Addr().Network(), l.Addr().String())
|
|
|
|
return &SyslogServerState{Addr: syslogAddr}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UniversalExecutor) collectLogs(we io.Writer, wo io.Writer) {
|
|
|
|
for logParts := range e.syslogChan {
|
|
|
|
// If the severity of the log line is err then we write to stderr
|
|
|
|
// otherwise all messages go to stdout
|
|
|
|
if logParts.Severity == syslog.LOG_ERR {
|
2018-05-24 22:54:28 +00:00
|
|
|
we.Write(logParts.Message)
|
|
|
|
we.Write([]byte{'\n'})
|
2018-04-17 17:43:04 +00:00
|
|
|
} else {
|
2018-05-24 22:54:28 +00:00
|
|
|
wo.Write(logParts.Message)
|
|
|
|
wo.Write([]byte{'\n'})
|
2018-04-17 17:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-24 22:54:28 +00:00
|
|
|
|
|
|
|
// logRotatorWrapper wraps our log rotator and exposes a pipe that can feed the
|
|
|
|
// log rotator data. The processOutWriter should be attached to the process and
|
2018-05-24 23:25:20 +00:00
|
|
|
// data will be copied from the reader to the rotator.
|
2018-05-24 22:54:28 +00:00
|
|
|
type logRotatorWrapper struct {
|
2018-05-31 22:21:36 +00:00
|
|
|
processOutWriter *os.File
|
|
|
|
processOutReader *os.File
|
|
|
|
rotatorWriter *logging.FileRotator
|
|
|
|
hasFinishedCopied chan struct{}
|
2018-05-24 22:54:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 23:25:20 +00:00
|
|
|
// NewLogRotatorWrapper takes a rotator and returns a wrapper that has the
|
|
|
|
// processOutWriter to attach to the processes stdout or stderr.
|
|
|
|
func NewLogRotatorWrapper(rotator *logging.FileRotator) (*logRotatorWrapper, error) {
|
|
|
|
r, w, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create os.Pipe for extracting logs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wrap := &logRotatorWrapper{
|
2018-05-31 22:21:36 +00:00
|
|
|
processOutWriter: w,
|
|
|
|
processOutReader: r,
|
|
|
|
rotatorWriter: rotator,
|
|
|
|
hasFinishedCopied: make(chan struct{}, 1),
|
2018-05-24 23:25:20 +00:00
|
|
|
}
|
|
|
|
wrap.start()
|
|
|
|
return wrap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// start starts a go-routine that copies from the pipe into the rotator. This is
|
|
|
|
// called by the constructor and not the user of the wrapper.
|
|
|
|
func (l *logRotatorWrapper) start() {
|
2018-05-24 22:54:28 +00:00
|
|
|
go func() {
|
|
|
|
io.Copy(l.rotatorWriter, l.processOutReader)
|
|
|
|
l.processOutReader.Close() // in case io.Copy stopped due to write error
|
2018-05-31 22:21:36 +00:00
|
|
|
close(l.hasFinishedCopied)
|
2018-05-24 22:54:28 +00:00
|
|
|
}()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the rotator and the process writer to ensure that the Wait
|
|
|
|
// command exits.
|
|
|
|
func (l *logRotatorWrapper) Close() error {
|
2018-05-31 22:21:36 +00:00
|
|
|
// Wait up to the close tolerance before we force close
|
|
|
|
select {
|
|
|
|
case <-l.hasFinishedCopied:
|
|
|
|
case <-time.After(processOutputCloseTolerance):
|
|
|
|
}
|
|
|
|
err := l.processOutReader.Close()
|
2018-05-24 22:54:28 +00:00
|
|
|
l.rotatorWriter.Close()
|
2018-05-31 22:21:36 +00:00
|
|
|
return err
|
2018-05-24 22:54:28 +00:00
|
|
|
}
|