2015-09-12 00:12:48 +00:00
|
|
|
// Package exec is used to invoke child processes across various platforms to
|
|
|
|
// provide the following features:
|
2015-09-11 19:35:03 +00:00
|
|
|
//
|
|
|
|
// - Least privilege
|
|
|
|
// - Resource constraints
|
|
|
|
// - Process isolation
|
|
|
|
//
|
2015-09-12 00:12:48 +00:00
|
|
|
// A "platform" may be defined as coarsely as "Windows" or as specifically as
|
|
|
|
// "linux 3.20 with systemd". This allows Nomad to use best-effort, best-
|
|
|
|
// available capabilities of each platform to provide resource constraints,
|
|
|
|
// process isolation, and security features, or otherwise take advantage of
|
|
|
|
// features that are unique to that platform.
|
|
|
|
//
|
2015-09-16 01:54:55 +00:00
|
|
|
// The `semantics of any particular instance are left up to the implementation.
|
2015-09-12 00:12:48 +00:00
|
|
|
// However, these should be completely transparent to the calling context. In
|
|
|
|
// other words, the Java driver should be able to call exec for any platform and
|
|
|
|
// just work.
|
2015-09-15 21:03:03 +00:00
|
|
|
package executor
|
2015-09-11 19:35:03 +00:00
|
|
|
|
2015-09-12 00:12:48 +00:00
|
|
|
import (
|
2015-09-24 08:11:52 +00:00
|
|
|
"fmt"
|
2015-09-12 00:12:48 +00:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2015-09-11 19:35:03 +00:00
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2015-09-12 00:12:48 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
2015-09-11 19:35:03 +00:00
|
|
|
|
2015-09-24 08:11:52 +00:00
|
|
|
var errNoResources = fmt.Errorf("No resources are associated with this task")
|
|
|
|
|
2015-09-12 00:12:48 +00:00
|
|
|
// Executor is an interface that any platform- or capability-specific exec
|
|
|
|
// wrapper must implement. You should not need to implement a Java executor.
|
|
|
|
// Rather, you would implement a cgroups executor that the Java driver will use.
|
2015-09-11 19:35:03 +00:00
|
|
|
type Executor interface {
|
|
|
|
// Limit must be called before Start and restricts the amount of resources
|
2015-09-14 22:57:21 +00:00
|
|
|
// the process can use. Note that an error may be returned ONLY IF the
|
|
|
|
// executor implements resource limiting. Otherwise Limit is ignored.
|
2015-09-15 20:11:56 +00:00
|
|
|
Limit(*structs.Resources) error
|
2015-09-11 19:35:03 +00:00
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
// ConfigureTaskDir must be called before Start and ensures that the tasks
|
|
|
|
// directory is properly configured.
|
|
|
|
ConfigureTaskDir(taskName string, alloc *allocdir.AllocDir) error
|
|
|
|
|
2015-09-11 19:35:03 +00:00
|
|
|
// Start the process. This may wrap the actual process in another command,
|
2015-09-12 00:12:48 +00:00
|
|
|
// depending on the capabilities in this environment. Errors that arise from
|
2015-09-16 01:54:55 +00:00
|
|
|
// Limits or Runas may bubble through Start()
|
2015-09-11 19:35:03 +00:00
|
|
|
Start() error
|
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
// Open should be called to restore a previous execution. This might be needed if
|
|
|
|
// nomad is restarted.
|
|
|
|
Open(string) error
|
2015-09-12 00:12:48 +00:00
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
// Wait waits till the user's command is completed.
|
2015-09-15 20:11:56 +00:00
|
|
|
Wait() error
|
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
// Returns a handle that is executor specific for use in reopening.
|
|
|
|
ID() (string, error)
|
2015-09-15 20:11:56 +00:00
|
|
|
|
2015-09-11 19:35:03 +00:00
|
|
|
// Shutdown should use a graceful stop mechanism so the application can
|
|
|
|
// perform checkpointing or cleanup, if such a mechanism is available.
|
2015-09-12 00:12:48 +00:00
|
|
|
// If such a mechanism is not available, Shutdown() should call ForceStop().
|
2015-09-11 19:35:03 +00:00
|
|
|
Shutdown() error
|
|
|
|
|
|
|
|
// ForceStop will terminate the process without waiting for cleanup. Every
|
|
|
|
// implementations must provide this.
|
|
|
|
ForceStop() error
|
2015-09-12 00:12:48 +00:00
|
|
|
|
2015-09-16 01:54:55 +00:00
|
|
|
// Command provides access the underlying Cmd struct in case the Executor
|
|
|
|
// interface doesn't expose the functionality you need.
|
2015-09-12 00:12:48 +00:00
|
|
|
Command() *cmd
|
2015-09-11 19:35:03 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 00:12:48 +00:00
|
|
|
// Command is a mirror of exec.Command that returns a platform-specific Executor
|
|
|
|
func Command(name string, arg ...string) Executor {
|
2015-09-16 01:54:55 +00:00
|
|
|
executor := NewExecutor()
|
2015-09-12 00:12:48 +00:00
|
|
|
cmd := executor.Command()
|
|
|
|
cmd.Path = name
|
|
|
|
cmd.Args = append([]string{name}, arg...)
|
|
|
|
|
|
|
|
if filepath.Base(name) == name {
|
|
|
|
if lp, err := exec.LookPath(name); err != nil {
|
|
|
|
// cmd.lookPathErr = err
|
|
|
|
} else {
|
|
|
|
cmd.Path = lp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return executor
|
|
|
|
}
|
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
// OpenId is similar to executor.Command but will attempt to reopen with the
|
|
|
|
// passed ID.
|
|
|
|
func OpenId(id string) (Executor, error) {
|
2015-09-16 01:54:55 +00:00
|
|
|
executor := NewExecutor()
|
2015-09-20 01:47:04 +00:00
|
|
|
err := executor.Open(id)
|
2015-09-15 20:11:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return executor, nil
|
2015-09-12 00:12:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 01:54:55 +00:00
|
|
|
// Cmd is an extension of exec.Cmd that incorporates functionality for
|
|
|
|
// re-attaching to processes, dropping priviledges, etc., based on platform-
|
|
|
|
// specific implementations.
|
|
|
|
type cmd struct {
|
|
|
|
exec.Cmd
|
2015-09-15 23:40:40 +00:00
|
|
|
|
2015-09-16 01:54:55 +00:00
|
|
|
// Resources is used to limit CPU and RAM used by the process, by way of
|
|
|
|
// cgroups or a similar mechanism.
|
|
|
|
Resources structs.Resources
|
2015-09-15 23:40:40 +00:00
|
|
|
|
2015-09-16 01:54:55 +00:00
|
|
|
// RunAs may be a username or Uid. The implementation will decide how to use it.
|
|
|
|
RunAs string
|
2015-09-15 23:40:40 +00:00
|
|
|
}
|