2016-03-24 07:13:30 +00:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
2016-05-08 07:13:28 +00:00
|
|
|
"fmt"
|
2016-03-24 07:13:30 +00:00
|
|
|
"log"
|
2016-05-08 07:13:28 +00:00
|
|
|
"os/exec"
|
2016-03-24 23:15:22 +00:00
|
|
|
"sync"
|
2016-05-08 07:13:28 +00:00
|
|
|
"syscall"
|
2016-03-24 07:13:30 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/armon/circbuf"
|
|
|
|
docker "github.com/fsouza/go-dockerclient"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/driver/structs"
|
|
|
|
)
|
|
|
|
|
2016-03-24 23:15:22 +00:00
|
|
|
var (
|
|
|
|
// We store the client globally to cache the connection to the docker daemon.
|
|
|
|
createClient sync.Once
|
|
|
|
client *docker.Client
|
|
|
|
)
|
|
|
|
|
2016-05-05 17:01:38 +00:00
|
|
|
const (
|
2016-05-05 17:45:02 +00:00
|
|
|
// The default check timeout
|
2016-05-05 17:01:38 +00:00
|
|
|
defaultCheckTimeout = 30 * time.Second
|
|
|
|
)
|
|
|
|
|
2016-03-25 02:30:02 +00:00
|
|
|
// DockerScriptCheck runs nagios compatible scripts in a docker container and
|
|
|
|
// provides the check result
|
2016-03-24 07:13:30 +00:00
|
|
|
type DockerScriptCheck struct {
|
2016-05-05 17:45:02 +00:00
|
|
|
id string // id of the check
|
|
|
|
interval time.Duration // interval of the check
|
|
|
|
timeout time.Duration // timeout of the check
|
|
|
|
containerID string // container id in which the check will be invoked
|
2016-03-24 07:13:30 +00:00
|
|
|
logger *log.Logger
|
2016-05-05 17:45:02 +00:00
|
|
|
cmd string // check command
|
|
|
|
args []string // check command arguments
|
2016-03-24 23:15:22 +00:00
|
|
|
|
2016-05-05 17:45:02 +00:00
|
|
|
dockerEndpoint string // docker endpoint
|
|
|
|
tlsCert string // path to tls certificate
|
|
|
|
tlsCa string // path to tls ca
|
|
|
|
tlsKey string // path to tls key
|
2016-03-24 23:15:22 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 02:30:02 +00:00
|
|
|
// dockerClient creates the client to interact with the docker daemon
|
2016-03-24 23:15:22 +00:00
|
|
|
func (d *DockerScriptCheck) dockerClient() (*docker.Client, error) {
|
|
|
|
if client != nil {
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
createClient.Do(func() {
|
|
|
|
if d.dockerEndpoint != "" {
|
|
|
|
if d.tlsCert+d.tlsKey+d.tlsCa != "" {
|
2016-03-25 02:00:24 +00:00
|
|
|
d.logger.Printf("[DEBUG] executor.checks: using TLS client connection to %s", d.dockerEndpoint)
|
2016-03-24 23:15:22 +00:00
|
|
|
client, err = docker.NewTLSClient(d.dockerEndpoint, d.tlsCert, d.tlsKey, d.tlsCa)
|
|
|
|
} else {
|
2016-03-25 02:00:24 +00:00
|
|
|
d.logger.Printf("[DEBUG] executor.checks: using standard client connection to %s", d.dockerEndpoint)
|
2016-03-24 23:15:22 +00:00
|
|
|
client, err = docker.NewClient(d.dockerEndpoint)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-25 02:00:24 +00:00
|
|
|
d.logger.Println("[DEBUG] executor.checks: using client connection initialized from environment")
|
2016-03-24 23:15:22 +00:00
|
|
|
client, err = docker.NewClientFromEnv()
|
|
|
|
})
|
|
|
|
return client, err
|
2016-03-24 07:13:30 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 02:30:02 +00:00
|
|
|
// Run runs a script check inside a docker container
|
2016-03-24 07:13:30 +00:00
|
|
|
func (d *DockerScriptCheck) Run() *cstructs.CheckResult {
|
2016-03-24 23:15:22 +00:00
|
|
|
var (
|
|
|
|
exec *docker.Exec
|
|
|
|
err error
|
|
|
|
execRes *docker.ExecInspect
|
|
|
|
time = time.Now()
|
|
|
|
)
|
|
|
|
|
|
|
|
if client, err = d.dockerClient(); err != nil {
|
|
|
|
return &cstructs.CheckResult{Err: err}
|
|
|
|
}
|
|
|
|
client = client
|
2016-03-24 07:13:30 +00:00
|
|
|
execOpts := docker.CreateExecOptions{
|
|
|
|
AttachStdin: false,
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
Tty: false,
|
2016-03-24 17:06:40 +00:00
|
|
|
Cmd: append([]string{d.cmd}, d.args...),
|
2016-03-24 07:13:30 +00:00
|
|
|
Container: d.containerID,
|
|
|
|
}
|
2016-03-24 23:15:22 +00:00
|
|
|
if exec, err = client.CreateExec(execOpts); err != nil {
|
2016-03-24 07:13:30 +00:00
|
|
|
return &cstructs.CheckResult{Err: err}
|
|
|
|
}
|
|
|
|
|
|
|
|
output, _ := circbuf.NewBuffer(int64(cstructs.CheckBufSize))
|
|
|
|
startOpts := docker.StartExecOptions{
|
|
|
|
Detach: false,
|
|
|
|
Tty: false,
|
|
|
|
OutputStream: output,
|
|
|
|
ErrorStream: output,
|
|
|
|
}
|
|
|
|
|
2016-03-24 23:15:22 +00:00
|
|
|
if err = client.StartExec(exec.ID, startOpts); err != nil {
|
2016-03-24 07:13:30 +00:00
|
|
|
return &cstructs.CheckResult{Err: err}
|
|
|
|
}
|
2016-03-24 23:15:22 +00:00
|
|
|
if execRes, err = client.InspectExec(exec.ID); err != nil {
|
2016-03-24 07:13:30 +00:00
|
|
|
return &cstructs.CheckResult{Err: err}
|
|
|
|
}
|
|
|
|
return &cstructs.CheckResult{
|
|
|
|
ExitCode: execRes.ExitCode,
|
|
|
|
Output: string(output.Bytes()),
|
|
|
|
Timestamp: time,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 02:30:02 +00:00
|
|
|
// ID returns the check id
|
2016-03-24 07:13:30 +00:00
|
|
|
func (d *DockerScriptCheck) ID() string {
|
|
|
|
return d.id
|
|
|
|
}
|
|
|
|
|
2016-03-25 02:30:02 +00:00
|
|
|
// Interval returns the interval at which the check has to run
|
2016-03-25 02:00:24 +00:00
|
|
|
func (d *DockerScriptCheck) Interval() time.Duration {
|
|
|
|
return d.interval
|
|
|
|
}
|
|
|
|
|
2016-05-05 17:01:38 +00:00
|
|
|
// Timeout returns the duration after which a check is timed out.
|
|
|
|
func (d *DockerScriptCheck) Timeout() time.Duration {
|
2016-05-05 17:45:02 +00:00
|
|
|
if d.timeout == 0 {
|
|
|
|
return defaultCheckTimeout
|
|
|
|
}
|
2016-05-05 17:01:38 +00:00
|
|
|
return d.timeout
|
|
|
|
}
|
|
|
|
|
2016-03-25 02:30:02 +00:00
|
|
|
// ExecScriptCheck runs a nagios compatible script and returns the check result
|
2016-03-24 07:13:30 +00:00
|
|
|
type ExecScriptCheck struct {
|
2016-05-05 17:45:02 +00:00
|
|
|
id string // id of the script check
|
|
|
|
interval time.Duration // interval at which the check is invoked
|
|
|
|
timeout time.Duration // timeout duration of the check
|
|
|
|
cmd string // command of the check
|
|
|
|
args []string // args passed to the check
|
|
|
|
taskDir string // the root directory of the check
|
|
|
|
|
|
|
|
FSIsolation bool // indicates whether the check has to be run within a chroot
|
2016-03-24 07:13:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-08 07:13:28 +00:00
|
|
|
// Run runs an exec script check
|
|
|
|
func (e *ExecScriptCheck) Run() *cstructs.CheckResult {
|
|
|
|
buf, _ := circbuf.NewBuffer(int64(cstructs.CheckBufSize))
|
|
|
|
cmd := exec.Command(e.cmd, e.args...)
|
|
|
|
cmd.Stdout = buf
|
|
|
|
cmd.Stderr = buf
|
|
|
|
e.setChroot(cmd)
|
|
|
|
ts := time.Now()
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return &cstructs.CheckResult{Err: err}
|
|
|
|
}
|
|
|
|
errCh := make(chan error, 2)
|
|
|
|
go func() {
|
|
|
|
errCh <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
endTime := time.Now()
|
|
|
|
if err == nil {
|
|
|
|
return &cstructs.CheckResult{
|
|
|
|
ExitCode: 0,
|
|
|
|
Output: string(buf.Bytes()),
|
|
|
|
Timestamp: ts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exitCode := 1
|
|
|
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
|
|
|
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
|
|
|
|
exitCode = status.ExitStatus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &cstructs.CheckResult{
|
|
|
|
ExitCode: exitCode,
|
|
|
|
Output: string(buf.Bytes()),
|
|
|
|
Timestamp: ts,
|
|
|
|
Duration: endTime.Sub(ts),
|
|
|
|
}
|
|
|
|
case <-time.After(e.Timeout()):
|
|
|
|
errCh <- fmt.Errorf("timed out after waiting 30s")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-08 18:46:20 +00:00
|
|
|
// ID returns the check id
|
|
|
|
func (e *ExecScriptCheck) ID() string {
|
|
|
|
return e.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interval returns the interval at which the check has to run
|
|
|
|
func (e *ExecScriptCheck) Interval() time.Duration {
|
|
|
|
return e.interval
|
|
|
|
}
|
|
|
|
|
2016-05-05 17:01:38 +00:00
|
|
|
// Timeout returns the duration after which a check is timed out.
|
|
|
|
func (e *ExecScriptCheck) Timeout() time.Duration {
|
2016-05-05 17:45:02 +00:00
|
|
|
if e.timeout == 0 {
|
|
|
|
return defaultCheckTimeout
|
|
|
|
}
|
2016-05-05 17:01:38 +00:00
|
|
|
return e.timeout
|
|
|
|
}
|