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

     1  package executor
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  
     7  	hclog "github.com/hashicorp/go-hclog"
     8  	plugin "github.com/hashicorp/go-plugin"
     9  
    10  	"github.com/hernad/nomad/plugins/base"
    11  )
    12  
    13  // Install a plugin cli handler to ease working with tests
    14  // and external plugins.
    15  // This init() must be initialized last in package required by the child plugin
    16  // process. It's recommended to avoid any other `init()` or inline any necessary calls
    17  // here. See eeaa95d commit message for more details.
    18  func init() {
    19  	if len(os.Args) > 1 && os.Args[1] == "executor" {
    20  		if len(os.Args) != 3 {
    21  			hclog.L().Error("json configuration not provided")
    22  			os.Exit(1)
    23  		}
    24  
    25  		config := os.Args[2]
    26  		var executorConfig ExecutorConfig
    27  		if err := json.Unmarshal([]byte(config), &executorConfig); err != nil {
    28  			os.Exit(1)
    29  		}
    30  
    31  		f, err := os.OpenFile(executorConfig.LogFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
    32  		if err != nil {
    33  			hclog.L().Error(err.Error())
    34  			os.Exit(1)
    35  		}
    36  
    37  		// Create the logger
    38  		logger := hclog.New(&hclog.LoggerOptions{
    39  			Level:      hclog.LevelFromString(executorConfig.LogLevel),
    40  			JSONFormat: true,
    41  			Output:     f,
    42  		})
    43  
    44  		plugin.Serve(&plugin.ServeConfig{
    45  			HandshakeConfig: base.Handshake,
    46  			Plugins: GetPluginMap(
    47  				logger,
    48  				executorConfig.FSIsolation,
    49  			),
    50  			GRPCServer: plugin.DefaultGRPCServer,
    51  			Logger:     logger,
    52  		})
    53  		os.Exit(0)
    54  	}
    55  }