github.com/hernad/nomad@v1.6.112/drivers/nix/_executor/utils.go (about)

     1  package executor
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  
     9  	"github.com/golang/protobuf/ptypes"
    10  	hclog "github.com/hashicorp/go-hclog"
    11  	plugin "github.com/hashicorp/go-plugin"
    12  	"github.com/hernad/nomad/drivers/shared/executor/proto"
    13  	"github.com/hernad/nomad/plugins/base"
    14  )
    15  
    16  const (
    17  	// ExecutorDefaultMaxPort is the default max port used by the executor for
    18  	// searching for an available port
    19  	ExecutorDefaultMaxPort = 14512
    20  
    21  	// ExecutorDefaultMinPort is the default min port used by the executor for
    22  	// searching for an available port
    23  	ExecutorDefaultMinPort = 14000
    24  )
    25  
    26  // CreateExecutor launches an executor plugin and returns an instance of the
    27  // Executor interface
    28  func CreateExecutor(logger hclog.Logger, driverConfig *base.ClientDriverConfig,
    29  	executorConfig *ExecutorConfig) (Executor, *plugin.Client, error) {
    30  
    31  	c, err := json.Marshal(executorConfig)
    32  	if err != nil {
    33  		return nil, nil, fmt.Errorf("unable to create executor config: %v", err)
    34  	}
    35  	bin, err := os.Executable()
    36  	if err != nil {
    37  		return nil, nil, fmt.Errorf("unable to find the nomad binary: %v", err)
    38  	}
    39  
    40  	p := &ExecutorPlugin{
    41  		logger:      logger,
    42  		fsIsolation: executorConfig.FSIsolation,
    43  	}
    44  
    45  	config := &plugin.ClientConfig{
    46  		HandshakeConfig:  base.Handshake,
    47  		Plugins:          map[string]plugin.Plugin{"executor": p},
    48  		Cmd:              exec.Command(bin, "executor", string(c)),
    49  		AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
    50  		Logger:           logger.Named("executor"),
    51  	}
    52  
    53  	if driverConfig != nil {
    54  		config.MaxPort = driverConfig.ClientMaxPort
    55  		config.MinPort = driverConfig.ClientMinPort
    56  	} else {
    57  		config.MaxPort = ExecutorDefaultMaxPort
    58  		config.MinPort = ExecutorDefaultMinPort
    59  	}
    60  
    61  	// setting the setsid of the plugin process so that it doesn't get signals sent to
    62  	// the nomad client.
    63  	if config.Cmd != nil {
    64  		isolateCommand(config.Cmd)
    65  	}
    66  
    67  	return newExecutorClient(config, logger)
    68  }
    69  
    70  // ReattachToExecutor launches a plugin with a given plugin config
    71  func ReattachToExecutor(reattachConfig *plugin.ReattachConfig, logger hclog.Logger) (Executor, *plugin.Client, error) {
    72  	config := &plugin.ClientConfig{
    73  		HandshakeConfig:  base.Handshake,
    74  		Reattach:         reattachConfig,
    75  		Plugins:          GetPluginMap(logger, false),
    76  		AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
    77  		Logger:           logger.Named("executor"),
    78  	}
    79  
    80  	return newExecutorClient(config, logger)
    81  }
    82  
    83  func newExecutorClient(config *plugin.ClientConfig, logger hclog.Logger) (Executor, *plugin.Client, error) {
    84  	executorClient := plugin.NewClient(config)
    85  	rpcClient, err := executorClient.Client()
    86  	if err != nil {
    87  		return nil, nil, fmt.Errorf("error creating rpc client for executor plugin: %v", err)
    88  	}
    89  
    90  	raw, err := rpcClient.Dispense("executor")
    91  	if err != nil {
    92  		return nil, nil, fmt.Errorf("unable to dispense the executor plugin: %v", err)
    93  	}
    94  	executorPlugin, ok := raw.(Executor)
    95  	if !ok {
    96  		return nil, nil, fmt.Errorf("unexpected executor rpc type: %T", raw)
    97  	}
    98  	return executorPlugin, executorClient, nil
    99  }
   100  
   101  func processStateToProto(ps *ProcessState) (*proto.ProcessState, error) {
   102  	timestamp, err := ptypes.TimestampProto(ps.Time)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  	pb := &proto.ProcessState{
   107  		Pid:      int32(ps.Pid),
   108  		ExitCode: int32(ps.ExitCode),
   109  		Signal:   int32(ps.Signal),
   110  		Time:     timestamp,
   111  	}
   112  
   113  	return pb, nil
   114  }
   115  
   116  func processStateFromProto(pb *proto.ProcessState) (*ProcessState, error) {
   117  	timestamp, err := ptypes.Timestamp(pb.Time)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	return &ProcessState{
   123  		Pid:      int(pb.Pid),
   124  		ExitCode: int(pb.ExitCode),
   125  		Signal:   int(pb.Signal),
   126  		Time:     timestamp,
   127  	}, nil
   128  }
   129  
   130  // IsolationMode returns the namespace isolation mode as determined from agent
   131  // plugin configuration and task driver configuration. The task configuration
   132  // takes precedence, if it is configured.
   133  func IsolationMode(plugin, task string) string {
   134  	if task != "" {
   135  		return task
   136  	}
   137  	return plugin
   138  }