mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
Merge pull request #5726 from hashicorp/b-plugins-via-init
Use init() to handle plugin invocation
This commit is contained in:
@@ -2,12 +2,12 @@ package logmon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
"github.com/hashicorp/nomad/client/logmon/proto"
|
||||
"github.com/hashicorp/nomad/helper/discover"
|
||||
"github.com/hashicorp/nomad/plugins/base"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
// TODO: Integrate with base plugin loader
|
||||
func LaunchLogMon(logger hclog.Logger, reattachConfig *plugin.ReattachConfig) (LogMon, *plugin.Client, error) {
|
||||
logger = logger.Named("logmon")
|
||||
bin, err := discover.NomadExecutable()
|
||||
bin, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
33
client/logmon/z_logmon_cmd.go
Normal file
33
client/logmon/z_logmon_cmd.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package logmon
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
"github.com/hashicorp/nomad/plugins/base"
|
||||
)
|
||||
|
||||
// Install a plugin cli handler to ease working with tests
|
||||
// and external plugins.
|
||||
// This init() must be initialized last in package required by the child plugin
|
||||
// process. It's recommended to avoid any other `init()` or inline any necessary calls
|
||||
// here. See eeaa95d commit message for more details.
|
||||
func init() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "logmon" {
|
||||
logger := hclog.New(&hclog.LoggerOptions{
|
||||
Level: hclog.Trace,
|
||||
JSONFormat: true,
|
||||
Name: "logmon",
|
||||
})
|
||||
plugin.Serve(&plugin.ServeConfig{
|
||||
HandshakeConfig: base.Handshake,
|
||||
Plugins: map[string]plugin.Plugin{
|
||||
"logmon": NewPlugin(NewLogMon(logger)),
|
||||
},
|
||||
GRPCServer: plugin.DefaultGRPCServer,
|
||||
Logger: logger,
|
||||
})
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/hashicorp/nomad/command/agent"
|
||||
"github.com/hashicorp/nomad/drivers/docker/docklog"
|
||||
"github.com/hashicorp/nomad/version"
|
||||
colorable "github.com/mattn/go-colorable"
|
||||
"github.com/mitchellh/cli"
|
||||
@@ -237,11 +236,6 @@ func Commands(metaPtr *Meta, agentUi cli.Ui) map[string]cli.CommandFactory {
|
||||
Meta: meta,
|
||||
}, nil
|
||||
},
|
||||
docklog.PluginName: func() (cli.Command, error) {
|
||||
return &DockerLoggerPluginCommand{
|
||||
Meta: meta,
|
||||
}, nil
|
||||
},
|
||||
"eval": func() (cli.Command, error) {
|
||||
return &EvalCommand{
|
||||
Meta: meta,
|
||||
@@ -262,11 +256,6 @@ func Commands(metaPtr *Meta, agentUi cli.Ui) map[string]cli.CommandFactory {
|
||||
Meta: meta,
|
||||
}, nil
|
||||
},
|
||||
"executor": func() (cli.Command, error) {
|
||||
return &ExecutorPluginCommand{
|
||||
Meta: meta,
|
||||
}, nil
|
||||
},
|
||||
"fs": func() (cli.Command, error) {
|
||||
return &AllocFSCommand{
|
||||
Meta: meta,
|
||||
@@ -372,11 +361,6 @@ func Commands(metaPtr *Meta, agentUi cli.Ui) map[string]cli.CommandFactory {
|
||||
Meta: meta,
|
||||
}, nil
|
||||
},
|
||||
"logmon": func() (cli.Command, error) {
|
||||
return &LogMonPluginCommand{
|
||||
Meta: meta,
|
||||
}, nil
|
||||
},
|
||||
"logs": func() (cli.Command, error) {
|
||||
return &AllocLogsCommand{
|
||||
Meta: meta,
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
log "github.com/hashicorp/go-hclog"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
"github.com/hashicorp/nomad/drivers/docker/docklog"
|
||||
"github.com/hashicorp/nomad/plugins/base"
|
||||
)
|
||||
|
||||
type DockerLoggerPluginCommand struct {
|
||||
Meta
|
||||
}
|
||||
|
||||
func (e *DockerLoggerPluginCommand) Help() string {
|
||||
helpText := `
|
||||
This is a command used by Nomad internally to launch the docker logger process"
|
||||
`
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
||||
func (e *DockerLoggerPluginCommand) Synopsis() string {
|
||||
return "internal - launch a docker logger plugin"
|
||||
}
|
||||
|
||||
func (e *DockerLoggerPluginCommand) Run(args []string) int {
|
||||
logger := log.New(&log.LoggerOptions{
|
||||
Level: log.Trace,
|
||||
JSONFormat: true,
|
||||
Name: docklog.PluginName,
|
||||
})
|
||||
|
||||
plugin.Serve(&plugin.ServeConfig{
|
||||
HandshakeConfig: base.Handshake,
|
||||
Plugins: map[string]plugin.Plugin{
|
||||
docklog.PluginName: docklog.NewPlugin(docklog.NewDockerLogger(logger)),
|
||||
},
|
||||
GRPCServer: plugin.DefaultGRPCServer,
|
||||
Logger: logger,
|
||||
})
|
||||
return 0
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
log "github.com/hashicorp/go-hclog"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
|
||||
"github.com/hashicorp/nomad/drivers/shared/executor"
|
||||
"github.com/hashicorp/nomad/plugins/base"
|
||||
)
|
||||
|
||||
type ExecutorPluginCommand struct {
|
||||
Meta
|
||||
}
|
||||
|
||||
func (e *ExecutorPluginCommand) Help() string {
|
||||
helpText := `
|
||||
This is a command used by Nomad internally to launch an executor plugin"
|
||||
`
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
||||
func (e *ExecutorPluginCommand) Synopsis() string {
|
||||
return "internal - launch an executor plugin"
|
||||
}
|
||||
|
||||
func (e *ExecutorPluginCommand) Run(args []string) int {
|
||||
if len(args) != 1 {
|
||||
e.Ui.Error("json configuration not provided")
|
||||
return 1
|
||||
}
|
||||
|
||||
config := args[0]
|
||||
var executorConfig executor.ExecutorConfig
|
||||
if err := json.Unmarshal([]byte(config), &executorConfig); err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(executorConfig.LogFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
e.Ui.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
// Create the logger
|
||||
logger := log.New(&log.LoggerOptions{
|
||||
Level: hclog.LevelFromString(executorConfig.LogLevel),
|
||||
JSONFormat: true,
|
||||
Output: f,
|
||||
})
|
||||
|
||||
plugin.Serve(&plugin.ServeConfig{
|
||||
HandshakeConfig: base.Handshake,
|
||||
Plugins: executor.GetPluginMap(
|
||||
logger,
|
||||
executorConfig.FSIsolation,
|
||||
),
|
||||
GRPCServer: plugin.DefaultGRPCServer,
|
||||
Logger: logger,
|
||||
})
|
||||
return 0
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
"github.com/hashicorp/nomad/client/logmon"
|
||||
"github.com/hashicorp/nomad/plugins/base"
|
||||
)
|
||||
|
||||
type LogMonPluginCommand struct {
|
||||
Meta
|
||||
}
|
||||
|
||||
func (e *LogMonPluginCommand) Help() string {
|
||||
helpText := `
|
||||
This is a command used by Nomad internally to launch the logmon process"
|
||||
`
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
||||
func (e *LogMonPluginCommand) Synopsis() string {
|
||||
return "internal - launch a logmon plugin"
|
||||
}
|
||||
|
||||
func (e *LogMonPluginCommand) Run(args []string) int {
|
||||
logger := hclog.New(&hclog.LoggerOptions{
|
||||
Level: hclog.Trace,
|
||||
JSONFormat: true,
|
||||
Name: "logmon",
|
||||
})
|
||||
plugin.Serve(&plugin.ServeConfig{
|
||||
HandshakeConfig: base.Handshake,
|
||||
Plugins: map[string]plugin.Plugin{
|
||||
"logmon": logmon.NewPlugin(logmon.NewLogMon(logger)),
|
||||
},
|
||||
GRPCServer: plugin.DefaultGRPCServer,
|
||||
Logger: logger,
|
||||
})
|
||||
return 0
|
||||
}
|
||||
@@ -2,12 +2,12 @@ package docklog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
"github.com/hashicorp/nomad/drivers/docker/docklog/proto"
|
||||
"github.com/hashicorp/nomad/helper/discover"
|
||||
"github.com/hashicorp/nomad/plugins/base"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
@@ -17,7 +17,7 @@ const PluginName = "docker_logger"
|
||||
// LaunchDockerLogger launches an instance of DockerLogger
|
||||
func LaunchDockerLogger(logger hclog.Logger) (DockerLogger, *plugin.Client, error) {
|
||||
logger = logger.Named(PluginName)
|
||||
bin, err := discover.NomadExecutable()
|
||||
bin, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
34
drivers/docker/docklog/z_docker_logger_cmd.go
Normal file
34
drivers/docker/docklog/z_docker_logger_cmd.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package docklog
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
log "github.com/hashicorp/go-hclog"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
"github.com/hashicorp/nomad/plugins/base"
|
||||
)
|
||||
|
||||
// Install a plugin cli handler to ease working with tests
|
||||
// and external plugins.
|
||||
// This init() must be initialized last in package required by the child plugin
|
||||
// process. It's recommended to avoid any other `init()` or inline any necessary calls
|
||||
// here. See eeaa95d commit message for more details.
|
||||
func init() {
|
||||
if len(os.Args) > 1 && os.Args[1] == PluginName {
|
||||
logger := log.New(&log.LoggerOptions{
|
||||
Level: log.Trace,
|
||||
JSONFormat: true,
|
||||
Name: PluginName,
|
||||
})
|
||||
|
||||
plugin.Serve(&plugin.ServeConfig{
|
||||
HandshakeConfig: base.Handshake,
|
||||
Plugins: map[string]plugin.Plugin{
|
||||
PluginName: NewPlugin(NewDockerLogger(logger)),
|
||||
},
|
||||
GRPCServer: plugin.DefaultGRPCServer,
|
||||
Logger: logger,
|
||||
})
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,13 @@ package executor
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
"github.com/hashicorp/nomad/drivers/shared/executor/proto"
|
||||
"github.com/hashicorp/nomad/helper/discover"
|
||||
"github.com/hashicorp/nomad/plugins/base"
|
||||
)
|
||||
|
||||
@@ -32,7 +32,7 @@ func CreateExecutor(logger hclog.Logger, driverConfig *base.ClientDriverConfig,
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("unable to create executor config: %v", err)
|
||||
}
|
||||
bin, err := discover.NomadExecutable()
|
||||
bin, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("unable to find the nomad binary: %v", err)
|
||||
}
|
||||
|
||||
56
drivers/shared/executor/z_executor_cmd.go
Normal file
56
drivers/shared/executor/z_executor_cmd.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
log "github.com/hashicorp/go-hclog"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
|
||||
"github.com/hashicorp/nomad/plugins/base"
|
||||
)
|
||||
|
||||
// Install a plugin cli handler to ease working with tests
|
||||
// and external plugins.
|
||||
// This init() must be initialized last in package required by the child plugin
|
||||
// process. It's recommended to avoid any other `init()` or inline any necessary calls
|
||||
// here. See eeaa95d commit message for more details.
|
||||
func init() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "executor" {
|
||||
if len(os.Args) != 3 {
|
||||
hclog.L().Error("json configuration not provided")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
config := os.Args[2]
|
||||
var executorConfig ExecutorConfig
|
||||
if err := json.Unmarshal([]byte(config), &executorConfig); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(executorConfig.LogFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
hclog.L().Error(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Create the logger
|
||||
logger := log.New(&log.LoggerOptions{
|
||||
Level: hclog.LevelFromString(executorConfig.LogLevel),
|
||||
JSONFormat: true,
|
||||
Output: f,
|
||||
})
|
||||
|
||||
plugin.Serve(&plugin.ServeConfig{
|
||||
HandshakeConfig: base.Handshake,
|
||||
Plugins: GetPluginMap(
|
||||
logger,
|
||||
executorConfig.FSIsolation,
|
||||
),
|
||||
GRPCServer: plugin.DefaultGRPCServer,
|
||||
Logger: logger,
|
||||
})
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user