forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
134 lines (112 loc) · 2.94 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package driver
import (
"fmt"
"runtime"
"syscall"
"time"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/executor"
"github.com/hashicorp/nomad/nomad/structs"
)
// ExecDriver fork/execs tasks using as many of the underlying OS's isolation
// features.
type ExecDriver struct {
DriverContext
}
// execHandle is returned from Start/Open as a handle to the PID
type execHandle struct {
cmd executor.Executor
waitCh chan error
doneCh chan struct{}
}
// NewExecDriver is used to create a new exec driver
func NewExecDriver(ctx *DriverContext) Driver {
return &ExecDriver{*ctx}
}
func (d *ExecDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
// Only enable if we are root when running on non-windows systems.
if runtime.GOOS != "windows" && syscall.Geteuid() != 0 {
d.logger.Printf("[DEBUG] driver.exec: must run as root user, disabling")
return false, nil
}
node.Attributes["driver.exec"] = "1"
return true, nil
}
func (d *ExecDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error) {
// Get the command
command, ok := task.Config["command"]
if !ok || command == "" {
return nil, fmt.Errorf("missing command for exec driver")
}
// Get the environment variables.
envVars := TaskEnvironmentVariables(ctx, task)
// Look for arguments
var args []string
if argRaw, ok := task.Config["args"]; ok {
args = append(args, argRaw)
}
// Setup the command
cmd := executor.Command(command, args...)
if err := cmd.Limit(task.Resources); err != nil {
return nil, fmt.Errorf("failed to constrain resources: %s", err)
}
// Populate environment variables
cmd.Command().Env = envVars.List()
if err := cmd.ConfigureTaskDir(d.taskName, ctx.AllocDir); err != nil {
return nil, fmt.Errorf("failed to configure task directory: %v", err)
}
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("failed to start command: %v", err)
}
// Return a driver handle
h := &execHandle{
cmd: cmd,
doneCh: make(chan struct{}),
waitCh: make(chan error, 1),
}
go h.run()
return h, nil
}
func (d *ExecDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
// Find the process
cmd, err := executor.OpenId(handleID)
if err != nil {
return nil, fmt.Errorf("failed to open ID %v: %v", handleID, err)
}
// Return a driver handle
h := &execHandle{
cmd: cmd,
doneCh: make(chan struct{}),
waitCh: make(chan error, 1),
}
go h.run()
return h, nil
}
func (h *execHandle) ID() string {
id, _ := h.cmd.ID()
return id
}
func (h *execHandle) WaitCh() chan error {
return h.waitCh
}
func (h *execHandle) Update(task *structs.Task) error {
// Update is not possible
return nil
}
func (h *execHandle) Kill() error {
h.cmd.Shutdown()
select {
case <-h.doneCh:
return nil
case <-time.After(5 * time.Second):
return h.cmd.ForceStop()
}
}
func (h *execHandle) run() {
err := h.cmd.Wait()
close(h.doneCh)
if err != nil {
h.waitCh <- err
}
close(h.waitCh)
}