From dc5aab8654c8352833b45a86287d41059cc5e8a5 Mon Sep 17 00:00:00 2001 From: Wolfgang Welz <welzwo@gmail.com> Date: Tue, 30 Jul 2019 15:45:24 +0200 Subject: [PATCH] Feat: plugins can be disabled and enabled --- packages/node/node.go | 27 +++++++++++++++++++--- packages/node/parameters.go | 4 +++- packages/node/plugin.go | 13 +++++++++-- plugins/analysis/plugin.go | 2 +- plugins/autopeering/plugin.go | 2 +- plugins/bundleprocessor/plugin.go | 2 +- plugins/cli/plugin.go | 15 ++++++++---- plugins/dashboard/plugin.go | 2 +- plugins/gossip-on-solidification/plugin.go | 2 +- plugins/gossip/plugin.go | 2 +- plugins/gracefulshutdown/plugin.go | 2 +- plugins/metrics/plugin.go | 2 +- plugins/statusscreen-tps/plugin.go | 2 +- plugins/statusscreen/statusscreen.go | 2 +- plugins/tangle/plugin.go | 2 +- plugins/tipselection/plugin.go | 2 +- plugins/validator/plugin.go | 2 +- plugins/webapi-gtta/plugin.go | 2 +- plugins/webapi-spammer/plugin.go | 2 +- plugins/webapi/plugin.go | 2 +- plugins/zeromq/plugin.go | 2 +- 21 files changed, 66 insertions(+), 27 deletions(-) diff --git a/packages/node/node.go b/packages/node/node.go index 9f86209f..443da232 100644 --- a/packages/node/node.go +++ b/packages/node/node.go @@ -14,6 +14,7 @@ type Node struct { } var DisabledPlugins = make(map[string]bool) +var EnabledPlugins = make(map[string]bool) func Load(plugins ...*Plugin) *Node { node := &Node{ @@ -100,18 +101,38 @@ func (node *Node) LogFailure(pluginName string, message string) { } } +func getPluginKey(plugin *Plugin) string { + return strings.ToLower(strings.Replace(plugin.Name, " ", "", -1)) +} + +func isDisabled(plugin *Plugin) bool { + _, exists := DisabledPlugins[getPluginKey(plugin)] + + return exists +} + +func isEnabled(plugin *Plugin) bool { + _, exists := EnabledPlugins[getPluginKey(plugin)] + + return exists +} + func (node *Node) Load(plugins ...*Plugin) { if len(plugins) >= 1 { for _, plugin := range plugins { - if _, exists := DisabledPlugins[strings.ToLower(strings.Replace(plugin.Name, " ", "", -1))]; !exists { + status := plugin.Status + if (status == Enabled && !isDisabled(plugin)) || + (status == Disabled && isEnabled(plugin)) { + plugin.wg = node.wg plugin.Node = node plugin.Events.Configure.Trigger(plugin) + node.loadedPlugins = append(node.loadedPlugins, plugin) node.LogInfo("Node", "Loading Plugin: "+plugin.Name+" ... done") - - node.loadedPlugins = append(node.loadedPlugins, plugin) + } else { + node.LogInfo("Node", "Skipping Plugin: "+plugin.Name) } } } diff --git a/packages/node/parameters.go b/packages/node/parameters.go index d94f31e9..ee244e3d 100644 --- a/packages/node/parameters.go +++ b/packages/node/parameters.go @@ -3,6 +3,8 @@ package node import "github.com/iotaledger/goshimmer/packages/parameter" var ( - LOG_LEVEL = parameter.AddInt("NODE/LOG_LEVEL", LOG_LEVEL_INFO, "controls the log types that are shown") + LOG_LEVEL = parameter.AddInt("NODE/LOG_LEVEL", LOG_LEVEL_INFO, "controls the log types that are shown") + DISABLE_PLUGINS = parameter.AddString("NODE/DISABLE_PLUGINS", "", "a list of plugins that shall be disabled") + ENABLE_PLUGINS = parameter.AddString("NODE/ENABLE_PLUGINS", "", "a list of plugins that shall be enabled") ) diff --git a/packages/node/plugin.go b/packages/node/plugin.go index a98a22be..4e915d3a 100644 --- a/packages/node/plugin.go +++ b/packages/node/plugin.go @@ -6,16 +6,25 @@ import ( "github.com/iotaledger/goshimmer/packages/events" ) +const ( + Disabled = iota + Enabled +) + type Plugin struct { Node *Node Name string + Status int Events pluginEvents wg *sync.WaitGroup } -func NewPlugin(name string, callback Callback, callbacks ...Callback) *Plugin { +// Creates a new plugin with the given name, default status and callbacks. +// The last specified callback is the run callback, while all other callbacks are configure callbacks. +func NewPlugin(name string, status int, callback Callback, callbacks ...Callback) *Plugin { plugin := &Plugin{ - Name: name, + Name: name, + Status: status, Events: pluginEvents{ Configure: events.NewEvent(pluginCaller), Run: events.NewEvent(pluginCaller), diff --git a/plugins/analysis/plugin.go b/plugins/analysis/plugin.go index ee40ed44..44268dd0 100644 --- a/plugins/analysis/plugin.go +++ b/plugins/analysis/plugin.go @@ -9,7 +9,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/analysis/webinterface" ) -var PLUGIN = node.NewPlugin("Analysis", configure, run) +var PLUGIN = node.NewPlugin("Analysis", node.Enabled, configure, run) func configure(plugin *node.Plugin) { if *server.SERVER_PORT.Value != 0 { diff --git a/plugins/autopeering/plugin.go b/plugins/autopeering/plugin.go index c8633bff..9cac751d 100644 --- a/plugins/autopeering/plugin.go +++ b/plugins/autopeering/plugin.go @@ -16,7 +16,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/gossip" ) -var PLUGIN = node.NewPlugin("Auto Peering", configure, run) +var PLUGIN = node.NewPlugin("Auto Peering", node.Enabled, configure, run) func configure(plugin *node.Plugin) { saltmanager.Configure(plugin) diff --git a/plugins/bundleprocessor/plugin.go b/plugins/bundleprocessor/plugin.go index 02303b75..db1e1c28 100644 --- a/plugins/bundleprocessor/plugin.go +++ b/plugins/bundleprocessor/plugin.go @@ -9,7 +9,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/tangle" ) -var PLUGIN = node.NewPlugin("Bundle Processor", configure, run) +var PLUGIN = node.NewPlugin("Bundle Processor", node.Enabled, configure, run) func configure(plugin *node.Plugin) { tangle.Events.TransactionSolid.Attach(events.NewClosure(func(tx *value_transaction.ValueTransaction) { diff --git a/plugins/cli/plugin.go b/plugins/cli/plugin.go index 4e98e515..0c0158f0 100644 --- a/plugins/cli/plugin.go +++ b/plugins/cli/plugin.go @@ -48,12 +48,19 @@ func init() { flag.Usage = printUsage } +func parseParameters() { + for _, pluginName := range strings.Fields(*node.DISABLE_PLUGINS.Value) { + node.DisabledPlugins[strings.ToLower(pluginName)] = true + } + for _, pluginName := range strings.Fields(*node.ENABLE_PLUGINS.Value) { + node.EnabledPlugins[strings.ToLower(pluginName)] = true + } +} + func configure(ctx *node.Plugin) { flag.Parse() - for _, disabledPlugin := range strings.Fields(*node.DISABLE_PLUGINS.Value) { - node.DisabledPlugins[strings.ToLower(disabledPlugin)] = true - } + parseParameters() fmt.Println(" _____ _ _ ________ ______ ___ ___________ ") fmt.Println(" / ___| | | |_ _| \\/ || \\/ || ___| ___ \\") @@ -66,6 +73,6 @@ func configure(ctx *node.Plugin) { ctx.Node.LogInfo("Node", "Loading plugins ...") } -var PLUGIN = node.NewPlugin("CLI", configure, func(plugin *node.Plugin) { +var PLUGIN = node.NewPlugin("CLI", node.Enabled, configure, func(plugin *node.Plugin) { }) diff --git a/plugins/dashboard/plugin.go b/plugins/dashboard/plugin.go index 2d4f992d..09fb191d 100644 --- a/plugins/dashboard/plugin.go +++ b/plugins/dashboard/plugin.go @@ -10,7 +10,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/metrics" ) -var PLUGIN = node.NewPlugin("Dashboard", configure, run) +var PLUGIN = node.NewPlugin("Dashboard", node.Enabled, configure, run) func configure(plugin *node.Plugin) { http.HandleFunc("/dashboard", ServeHome) diff --git a/plugins/gossip-on-solidification/plugin.go b/plugins/gossip-on-solidification/plugin.go index 1c226800..bd040b9c 100644 --- a/plugins/gossip-on-solidification/plugin.go +++ b/plugins/gossip-on-solidification/plugin.go @@ -8,7 +8,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/tangle" ) -var PLUGIN = node.NewPlugin("Gossip On Solidification", func(plugin *node.Plugin) { +var PLUGIN = node.NewPlugin("Gossip On Solidification", node.Enabled, func(plugin *node.Plugin) { tangle.Events.TransactionSolid.Attach(events.NewClosure(func(tx *value_transaction.ValueTransaction) { gossip.SendTransaction(tx.MetaTransaction) })) diff --git a/plugins/gossip/plugin.go b/plugins/gossip/plugin.go index 0f02bd14..872e3cac 100644 --- a/plugins/gossip/plugin.go +++ b/plugins/gossip/plugin.go @@ -4,7 +4,7 @@ import ( "github.com/iotaledger/goshimmer/packages/node" ) -var PLUGIN = node.NewPlugin("Gossip", configure, run) +var PLUGIN = node.NewPlugin("Gossip", node.Enabled, configure, run) func configure(plugin *node.Plugin) { configureNeighbors(plugin) diff --git a/plugins/gracefulshutdown/plugin.go b/plugins/gracefulshutdown/plugin.go index ee3a074c..41da78c2 100644 --- a/plugins/gracefulshutdown/plugin.go +++ b/plugins/gracefulshutdown/plugin.go @@ -15,7 +15,7 @@ import ( // maximum amount of time to wait for background processes to terminate. After that the process is killed. const WAIT_TO_KILL_TIME_IN_SECONDS = 10 -var PLUGIN = node.NewPlugin("Graceful Shutdown", func(plugin *node.Plugin) { +var PLUGIN = node.NewPlugin("Graceful Shutdown", node.Enabled, func(plugin *node.Plugin) { gracefulStop := make(chan os.Signal) signal.Notify(gracefulStop, syscall.SIGTERM) diff --git a/plugins/metrics/plugin.go b/plugins/metrics/plugin.go index bcc9310e..290b548f 100644 --- a/plugins/metrics/plugin.go +++ b/plugins/metrics/plugin.go @@ -11,7 +11,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/gossip" ) -var PLUGIN = node.NewPlugin("Metrics", configure, run) +var PLUGIN = node.NewPlugin("Metrics", node.Enabled, configure, run) func configure(plugin *node.Plugin) { // increase received TPS counter whenever we receive a new transaction diff --git a/plugins/statusscreen-tps/plugin.go b/plugins/statusscreen-tps/plugin.go index 9b73392c..e91121a1 100644 --- a/plugins/statusscreen-tps/plugin.go +++ b/plugins/statusscreen-tps/plugin.go @@ -23,7 +23,7 @@ var receivedTps uint64 var solidTps uint64 -var PLUGIN = node.NewPlugin("Statusscreen TPS", func(plugin *node.Plugin) { +var PLUGIN = node.NewPlugin("Statusscreen TPS", node.Enabled, func(plugin *node.Plugin) { gossip.Events.ReceiveTransaction.Attach(events.NewClosure(func(_ *meta_transaction.MetaTransaction) { atomic.AddUint64(&receivedTpsCounter, 1) })) diff --git a/plugins/statusscreen/statusscreen.go b/plugins/statusscreen/statusscreen.go index 5c6d13e2..91a83d4b 100644 --- a/plugins/statusscreen/statusscreen.go +++ b/plugins/statusscreen/statusscreen.go @@ -118,4 +118,4 @@ func run(plugin *node.Plugin) { }) } -var PLUGIN = node.NewPlugin("Status Screen", configure, run) +var PLUGIN = node.NewPlugin("Status Screen", node.Enabled, configure, run) diff --git a/plugins/tangle/plugin.go b/plugins/tangle/plugin.go index db5d5135..f1efc83a 100644 --- a/plugins/tangle/plugin.go +++ b/plugins/tangle/plugin.go @@ -6,7 +6,7 @@ import ( // region plugin module setup ////////////////////////////////////////////////////////////////////////////////////////// -var PLUGIN = node.NewPlugin("Tangle", configure, run) +var PLUGIN = node.NewPlugin("Tangle", node.Enabled, configure, run) func configure(plugin *node.Plugin) { configureTransactionDatabase(plugin) diff --git a/plugins/tipselection/plugin.go b/plugins/tipselection/plugin.go index 7980a595..745c1dad 100644 --- a/plugins/tipselection/plugin.go +++ b/plugins/tipselection/plugin.go @@ -7,7 +7,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/tangle" ) -var PLUGIN = node.NewPlugin("Tipselection", configure, run) +var PLUGIN = node.NewPlugin("Tipselection", node.Enabled, configure, run) func configure(node *node.Plugin) { tangle.Events.TransactionSolid.Attach(events.NewClosure(func(transaction *value_transaction.ValueTransaction) { diff --git a/plugins/validator/plugin.go b/plugins/validator/plugin.go index dce2129b..10e80653 100644 --- a/plugins/validator/plugin.go +++ b/plugins/validator/plugin.go @@ -13,7 +13,7 @@ import ( . "github.com/iotaledger/iota.go/trinary" ) -var PLUGIN = node.NewPlugin("Validator", configure, run) +var PLUGIN = node.NewPlugin("Validator", node.Enabled, configure, run) // Creates bundle signature fragments and the corresponding address to validate against. // Each signature fragment after the first must go into its own meta transaction with value = 0. diff --git a/plugins/webapi-gtta/plugin.go b/plugins/webapi-gtta/plugin.go index 0ebad673..73ee6f22 100644 --- a/plugins/webapi-gtta/plugin.go +++ b/plugins/webapi-gtta/plugin.go @@ -11,7 +11,7 @@ import ( "github.com/labstack/echo" ) -var PLUGIN = node.NewPlugin("WebAPI GTTA Endpoint", func(plugin *node.Plugin) { +var PLUGIN = node.NewPlugin("WebAPI GTTA Endpoint", node.Enabled, func(plugin *node.Plugin) { webapi.AddEndpoint("getTransactionsToApprove", Handler) }) diff --git a/plugins/webapi-spammer/plugin.go b/plugins/webapi-spammer/plugin.go index 4823ac75..71ec701d 100644 --- a/plugins/webapi-spammer/plugin.go +++ b/plugins/webapi-spammer/plugin.go @@ -10,7 +10,7 @@ import ( "github.com/labstack/echo" ) -var PLUGIN = node.NewPlugin("Spammer", configure) +var PLUGIN = node.NewPlugin("Spammer", node.Enabled, configure) func configure(plugin *node.Plugin) { webapi.AddEndpoint("spammer", WebApiHandler) diff --git a/plugins/webapi/plugin.go b/plugins/webapi/plugin.go index f19c9e47..f9de1de5 100644 --- a/plugins/webapi/plugin.go +++ b/plugins/webapi/plugin.go @@ -10,7 +10,7 @@ import ( "github.com/labstack/echo" ) -var PLUGIN = node.NewPlugin("WebAPI", configure, run) +var PLUGIN = node.NewPlugin("WebAPI", node.Enabled, configure, run) var Server = echo.New() diff --git a/plugins/zeromq/plugin.go b/plugins/zeromq/plugin.go index 1b7e38af..50dfd046 100644 --- a/plugins/zeromq/plugin.go +++ b/plugins/zeromq/plugin.go @@ -12,7 +12,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/tangle" ) -var PLUGIN = node.NewPlugin("ZeroMQ", configure, run) +var PLUGIN = node.NewPlugin("ZeroMQ", node.Enabled, configure, run) var publisher *Publisher var emptyTag = strings.Repeat("9", 27) -- GitLab