github.com/bigcommerce/nomad@v0.9.3-bc/command/executor_plugin.go (about)

     1  package command
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"strings"
     7  
     8  	hclog "github.com/hashicorp/go-hclog"
     9  	log "github.com/hashicorp/go-hclog"
    10  	plugin "github.com/hashicorp/go-plugin"
    11  
    12  	"github.com/hashicorp/nomad/drivers/shared/executor"
    13  	"github.com/hashicorp/nomad/plugins/base"
    14  )
    15  
    16  type ExecutorPluginCommand struct {
    17  	Meta
    18  }
    19  
    20  func (e *ExecutorPluginCommand) Help() string {
    21  	helpText := `
    22  	This is a command used by Nomad internally to launch an executor plugin"
    23  	`
    24  	return strings.TrimSpace(helpText)
    25  }
    26  
    27  func (e *ExecutorPluginCommand) Synopsis() string {
    28  	return "internal - launch an executor plugin"
    29  }
    30  
    31  func (e *ExecutorPluginCommand) Run(args []string) int {
    32  	if len(args) != 1 {
    33  		e.Ui.Error("json configuration not provided")
    34  		return 1
    35  	}
    36  
    37  	config := args[0]
    38  	var executorConfig executor.ExecutorConfig
    39  	if err := json.Unmarshal([]byte(config), &executorConfig); err != nil {
    40  		return 1
    41  	}
    42  
    43  	f, err := os.OpenFile(executorConfig.LogFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
    44  	if err != nil {
    45  		e.Ui.Error(err.Error())
    46  		return 1
    47  	}
    48  
    49  	// Create the logger
    50  	logger := log.New(&log.LoggerOptions{
    51  		Level:      hclog.LevelFromString(executorConfig.LogLevel),
    52  		JSONFormat: true,
    53  		Output:     f,
    54  	})
    55  
    56  	plugin.Serve(&plugin.ServeConfig{
    57  		HandshakeConfig: base.Handshake,
    58  		Plugins: executor.GetPluginMap(
    59  			logger,
    60  			executorConfig.FSIsolation,
    61  		),
    62  		GRPCServer: plugin.DefaultGRPCServer,
    63  		Logger:     logger,
    64  	})
    65  	return 0
    66  }