From f63543004b573c17b05a16c5144685e8c887ec7e Mon Sep 17 00:00:00 2001 From: Victor Castell Date: Sun, 6 Jun 2021 19:10:07 +0200 Subject: [PATCH 1/2] Convert shell plugin to internal plugin The purpose of this PR is to embed the shell plugin in the main dkron binary, that will facilitate creating a single binary with the most important executor, for easy deployment using a single binary. --- builtin/bins/dkron-executor-shell/main.go | 18 ------------- cmd/plugins.go | 18 ++++++++----- cmd/shell.go | 27 +++++++++++++++++++ .../shell}/shell.go | 2 +- .../shell}/shell_test.go | 2 +- .../shell}/shell_unix.go | 2 +- .../shell}/shell_windows.go | 2 +- 7 files changed, 43 insertions(+), 28 deletions(-) delete mode 100644 builtin/bins/dkron-executor-shell/main.go create mode 100644 cmd/shell.go rename {builtin/bins/dkron-executor-shell => plugin/shell}/shell.go (99%) rename {builtin/bins/dkron-executor-shell => plugin/shell}/shell_test.go (98%) rename {builtin/bins/dkron-executor-shell => plugin/shell}/shell_unix.go (97%) rename {builtin/bins/dkron-executor-shell => plugin/shell}/shell_windows.go (89%) diff --git a/builtin/bins/dkron-executor-shell/main.go b/builtin/bins/dkron-executor-shell/main.go deleted file mode 100644 index fb22bcc78..000000000 --- a/builtin/bins/dkron-executor-shell/main.go +++ /dev/null @@ -1,18 +0,0 @@ -package main - -import ( - dkplugin "github.com/distribworks/dkron/v3/plugin" - "github.com/hashicorp/go-plugin" -) - -func main() { - plugin.Serve(&plugin.ServeConfig{ - HandshakeConfig: dkplugin.Handshake, - Plugins: map[string]plugin.Plugin{ - "executor": &dkplugin.ExecutorPlugin{Executor: &Shell{}}, - }, - - // A non-nil value here enables gRPC serving for this plugin... - GRPCServer: plugin.DefaultGRPCServer, - }) -} diff --git a/cmd/plugins.go b/cmd/plugins.go index 772d99bb4..e72e17432 100644 --- a/cmd/plugins.go +++ b/cmd/plugins.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "os" "os/exec" "path" @@ -64,13 +65,12 @@ func (p *Plugins) DiscoverPlugins() error { } for _, file := range processors { - pluginName, ok := getPluginName(file) if !ok { continue } - raw, err := p.pluginFactory(file, dkplugin.ProcessorPluginName) + raw, err := p.pluginFactory(file, []string{}, dkplugin.ProcessorPluginName) if err != nil { return err } @@ -78,19 +78,24 @@ func (p *Plugins) DiscoverPlugins() error { } for _, file := range executors { - pluginName, ok := getPluginName(file) if !ok { continue } - raw, err := p.pluginFactory(file, dkplugin.ExecutorPluginName) + raw, err := p.pluginFactory(file, []string{}, dkplugin.ExecutorPluginName) if err != nil { return err } p.Executors[pluginName] = raw.(dkplugin.Executor) } + raw, err := p.pluginFactory(exePath, []string{"shell"}, dkplugin.ExecutorPluginName) + if err != nil { + return err + } + p.Executors["shell"] = raw.(dkplugin.Executor) + return nil } @@ -107,10 +112,11 @@ func getPluginName(file string) (string, bool) { return name, true } -func (p *Plugins) pluginFactory(path string, pluginType string) (interface{}, error) { +func (p *Plugins) pluginFactory(path string, args []string, pluginType string) (interface{}, error) { // Build the plugin client configuration and init the plugin var config plugin.ClientConfig - config.Cmd = exec.Command(path) + fmt.Println(path) + config.Cmd = exec.Command(path, args...) config.HandshakeConfig = dkplugin.Handshake config.Managed = true config.Plugins = dkplugin.PluginMap diff --git a/cmd/shell.go b/cmd/shell.go new file mode 100644 index 000000000..f02c5f5fb --- /dev/null +++ b/cmd/shell.go @@ -0,0 +1,27 @@ +package cmd + +import ( + dkplugin "github.com/distribworks/dkron/v3/plugin" + "github.com/distribworks/dkron/v3/plugin/shell" + "github.com/hashicorp/go-plugin" + "github.com/spf13/cobra" +) + +var shellCmd = &cobra.Command{ + Hidden: true, + Run: func(cmd *cobra.Command, args []string) { + plugin.Serve(&plugin.ServeConfig{ + HandshakeConfig: dkplugin.Handshake, + Plugins: map[string]plugin.Plugin{ + "executor": &dkplugin.ExecutorPlugin{Executor: &shell.Shell{}}, + }, + + // A non-nil value here enables gRPC serving for this plugin... + GRPCServer: plugin.DefaultGRPCServer, + }) + }, +} + +func init() { + dkronCmd.AddCommand(shellCmd) +} diff --git a/builtin/bins/dkron-executor-shell/shell.go b/plugin/shell/shell.go similarity index 99% rename from builtin/bins/dkron-executor-shell/shell.go rename to plugin/shell/shell.go index 3d4da4bef..d09cc90a6 100644 --- a/builtin/bins/dkron-executor-shell/shell.go +++ b/plugin/shell/shell.go @@ -1,4 +1,4 @@ -package main +package shell import ( "encoding/base64" diff --git a/builtin/bins/dkron-executor-shell/shell_test.go b/plugin/shell/shell_test.go similarity index 98% rename from builtin/bins/dkron-executor-shell/shell_test.go rename to plugin/shell/shell_test.go index 039077564..16c155d0c 100644 --- a/builtin/bins/dkron-executor-shell/shell_test.go +++ b/plugin/shell/shell_test.go @@ -1,4 +1,4 @@ -package main +package shell import ( "os" diff --git a/builtin/bins/dkron-executor-shell/shell_unix.go b/plugin/shell/shell_unix.go similarity index 97% rename from builtin/bins/dkron-executor-shell/shell_unix.go rename to plugin/shell/shell_unix.go index c3731163a..db11cc182 100644 --- a/builtin/bins/dkron-executor-shell/shell_unix.go +++ b/plugin/shell/shell_unix.go @@ -1,6 +1,6 @@ // +build !windows -package main +package shell import ( "os/exec" diff --git a/builtin/bins/dkron-executor-shell/shell_windows.go b/plugin/shell/shell_windows.go similarity index 89% rename from builtin/bins/dkron-executor-shell/shell_windows.go rename to plugin/shell/shell_windows.go index 417de4030..104dfbd16 100644 --- a/builtin/bins/dkron-executor-shell/shell_windows.go +++ b/plugin/shell/shell_windows.go @@ -1,6 +1,6 @@ // +build windows -package main +package shell import ( "os/exec" From 092e57901de072794b930e8212d50485676e489a Mon Sep 17 00:00:00 2001 From: Victor Castell Date: Tue, 8 Jun 2021 09:26:43 +0200 Subject: [PATCH 2/2] Remove debugging --- cmd/plugins.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/plugins.go b/cmd/plugins.go index e72e17432..fe4d2a777 100644 --- a/cmd/plugins.go +++ b/cmd/plugins.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "os" "os/exec" "path" @@ -115,7 +114,6 @@ func getPluginName(file string) (string, bool) { func (p *Plugins) pluginFactory(path string, args []string, pluginType string) (interface{}, error) { // Build the plugin client configuration and init the plugin var config plugin.ClientConfig - fmt.Println(path) config.Cmd = exec.Command(path, args...) config.HandshakeConfig = dkplugin.Handshake config.Managed = true