2016-02-04 21:34:14 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-02-04 21:53:30 +00:00
|
|
|
"os"
|
2016-02-04 21:34:14 +00:00
|
|
|
|
2016-02-04 21:53:30 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2016-02-04 21:34:14 +00:00
|
|
|
"github.com/hashicorp/go-plugin"
|
2016-02-08 21:29:53 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2016-02-05 00:03:17 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/executor"
|
2016-02-04 21:34:14 +00:00
|
|
|
)
|
|
|
|
|
2016-02-05 18:49:54 +00:00
|
|
|
// createExecutor launches an executor plugin and returns an instance of the
|
|
|
|
// Executor interface
|
2016-02-08 21:29:53 +00:00
|
|
|
func createExecutor(config *plugin.ClientConfig, w io.Writer, clientConfig *config.Config) (executor.Executor, *plugin.Client, error) {
|
2016-02-05 00:03:17 +00:00
|
|
|
config.HandshakeConfig = HandshakeConfig
|
2016-02-05 01:36:31 +00:00
|
|
|
config.Plugins = GetPluginMap(w)
|
2016-02-08 21:29:53 +00:00
|
|
|
config.MaxPort = clientConfig.ClientMaxPort
|
|
|
|
config.MinPort = clientConfig.ClientMinPort
|
2016-02-09 00:27:31 +00:00
|
|
|
|
|
|
|
// setting the setsid of the plugin process so that it doesn't get signals sent to
|
|
|
|
// the nomad client.
|
2016-02-08 03:33:48 +00:00
|
|
|
if config.Cmd != nil {
|
|
|
|
isolateCommand(config.Cmd)
|
|
|
|
}
|
2016-02-09 00:27:31 +00:00
|
|
|
|
2016-02-04 21:34:14 +00:00
|
|
|
executorClient := plugin.NewClient(config)
|
|
|
|
rpcClient, err := executorClient.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("error creating rpc client for executor plugin: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := rpcClient.Dispense("executor")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unable to dispense the executor plugin: %v", err)
|
|
|
|
}
|
2016-02-05 00:03:17 +00:00
|
|
|
executorPlugin := raw.(executor.Executor)
|
2016-02-04 21:34:14 +00:00
|
|
|
return executorPlugin, executorClient, nil
|
|
|
|
}
|
2016-02-04 21:53:30 +00:00
|
|
|
|
2016-02-05 18:49:54 +00:00
|
|
|
// killProcess kills a process with the given pid
|
2016-02-04 21:53:30 +00:00
|
|
|
func killProcess(pid int) error {
|
|
|
|
proc, err := os.FindProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return proc.Kill()
|
|
|
|
}
|
|
|
|
|
2016-02-05 18:49:54 +00:00
|
|
|
// destroyPlugin kills the plugin with the given pid and also kills the user
|
|
|
|
// process
|
2016-02-04 21:53:30 +00:00
|
|
|
func destroyPlugin(pluginPid int, userPid int) error {
|
|
|
|
var merr error
|
|
|
|
if err := killProcess(pluginPid); err != nil {
|
|
|
|
merr = multierror.Append(merr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := killProcess(userPid); err != nil {
|
|
|
|
merr = multierror.Append(merr, err)
|
|
|
|
}
|
|
|
|
return merr
|
|
|
|
}
|