Merge pull request #5726 from hashicorp/b-plugins-via-init

Use init() to handle plugin invocation
This commit is contained in:
Mahmood Ali
2019-06-18 21:09:03 -04:00
committed by GitHub
10 changed files with 129 additions and 173 deletions

View File

@@ -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,

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}