From b4ed8b4af60b0e4b7efd7303d72c453cb3e2b177 Mon Sep 17 00:00:00 2001 From: asandikci Date: Mon, 28 Aug 2023 10:42:02 +0300 Subject: [PATCH] plugin feature enhancements - Added tmptest plugin for temporary testing - added plugin info to `checkPlugin()` function - added paramaters to `PluginManager` --- Makefile | 1 + cmd/ahenk-go/main.go | 3 +++ cmd/ahenk-go/plugin-manager.go | 36 +++++++++++++++++++++++++--------- go.mod | 1 + go.sum | 2 ++ plugins/resources/main.go | 2 +- plugins/tmptest/main.go | 30 ++++++++++++++++++++++++++++ 7 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 plugins/tmptest/main.go diff --git a/Makefile b/Makefile index b517440..522ae2a 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,7 @@ install: @sudo mkdir -p "${DESTDIR}/${PLUGIN_DIR}" sudo go build -buildmode=plugin -o ${DESTDIR}/${PLUGIN_DIR}/resources.so ./plugins/resources + sudo go build -buildmode=plugin -o ${DESTDIR}/${PLUGIN_DIR}/tmptest.so ./plugins/tmptest @sudo mkdir -p "${DESTDIR}/${DATA_DIR}" uninstall: diff --git a/cmd/ahenk-go/main.go b/cmd/ahenk-go/main.go index cb71627..efd34d5 100644 --- a/cmd/ahenk-go/main.go +++ b/cmd/ahenk-go/main.go @@ -55,8 +55,11 @@ func main() { f := utils.OpenLogFile(LogFile) defer f.Close() log.SetOutput(f) + // TODO Also pipe fmt.Print* commands + case "tmptest": log.Print("TEMPORARY TEST STARTED, log files are NOT redirecting!") + PluginManager("tmptest") } } else { panic("Please enter a valid option !") diff --git a/cmd/ahenk-go/plugin-manager.go b/cmd/ahenk-go/plugin-manager.go index 843a05c..ae95946 100644 --- a/cmd/ahenk-go/plugin-manager.go +++ b/cmd/ahenk-go/plugin-manager.go @@ -1,32 +1,49 @@ package main import ( - "fmt" "log" "time" ) +// General Functions/Methods that each plugin has +type PlugGeneral interface { + Info() map[string]string +} + // plugins/resources type Resources interface { AgentInfo() map[string]interface{} } -// FILLME creating new plugin interface template +// plugins/resources +type TmpTest interface { + TmpTest() +} + +// FILLME creating new plugin interface, template // type NewPluginInterface interface { // PluginMethod() returnType // } // Loads Plugins and runs them concurrently. // When you create a new plugin create a new interface and call this plugin in this function -func PluginManager() { +func PluginManager(params ...string) { chanPlug := make(chan interface{}) go LoadPlugin("resources", chanPlug) res, ok := <-chanPlug var resources Resources = res.(Resources) - checkPlugin(res, ok) + checkPlugin(resources, ok) - // FILLME Loading new plugin template + if len(params) > 0 && params[0] == "tmptest" { + go LoadPlugin("tmptest", chanPlug) + res, ok := <-chanPlug + var tmptest TmpTest = res.(TmpTest) + checkPlugin(res, ok) + tmptest.TmpTest() + } + + // FILLME Loading new plugin, template // go LoadPlugin("pluginName", chanPlug) // res, ok = <-chanPlug // var pluginName NewPluginInterface = res.(NewPluginInterface) @@ -36,7 +53,7 @@ func PluginManager() { for { go logPlugin("AgentInfo", resources.AgentInfo()) - // FILLME Running/Logout a plugin template + // FILLME Running/Log out a plugin, template // go logPlugin("InfoAboutFunction", pluginName.Function() ) time.Sleep(30 * time.Second) @@ -45,9 +62,9 @@ func PluginManager() { // Logs plugin outputs. func logPlugin(title string, mp map[string]interface{}) { - fmt.Printf("\n----- %v -----\n", title) + log.Printf("\n----- %v -----\n", title) for i, v := range mp { - fmt.Printf("%v: %v\n", i, v) + log.Printf("%v: %v\n", i, v) } } @@ -57,7 +74,8 @@ func checkPlugin(plugVal interface{}, status bool) { if plugVal == nil { log.Fatal("Plugin loaded but there is no value!") } else { - log.Println("Plugin loaded and ready to use") + plugInfo := plugVal.(PlugGeneral).Info() + log.Printf("Plugin \"%v\" loaded and ready to use, version \"%v\" ", plugInfo["name"], plugInfo["version"]) } } else { if plugVal == nil { diff --git a/go.mod b/go.mod index ce2ce06..12aad50 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( require ( github.com/go-ole/go-ole v1.2.6 // indirect github.com/yusufpapurcu/wmi v1.2.3 // indirect + github.com/zcalusic/sysinfo v1.0.1 // indirect golang.org/x/text v0.12.0 // indirect ) diff --git a/go.sum b/go.sum index fa11463..3c67789 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,8 @@ github.com/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7Am github.com/tklauser/numcpus v0.6.0/go.mod h1:FEZLMke0lhOUG6w2JadTzp0a+Nl8PF/GFkQ5UVIcaL4= github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw= github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +github.com/zcalusic/sysinfo v1.0.1 h1:cVh8q3codjh43AGRTa54dJ2Zq+qPejv8n2VWpxKViwc= +github.com/zcalusic/sysinfo v1.0.1/go.mod h1:LxwKwtQdbTIQc65drhjQzYzt0o7jfB80LrrZm7SWn8o= golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI= golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/plugins/resources/main.go b/plugins/resources/main.go index 03fb7fe..2dee806 100644 --- a/plugins/resources/main.go +++ b/plugins/resources/main.go @@ -65,7 +65,7 @@ func (p plug) AgentInfo() map[string]interface{} { return data } -func Info() map[string]string { +func (p plug) Info() map[string]string { inf := make(map[string]string) inf["name"] = "resources" inf["version"] = "0.0.2" diff --git a/plugins/tmptest/main.go b/plugins/tmptest/main.go new file mode 100644 index 0000000..97e0d4c --- /dev/null +++ b/plugins/tmptest/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "log" + + "github.com/zcalusic/sysinfo" +) + +type plug string + +// exported plugin Symbol +var TmptestConnect plug + +func (p plug) TmpTest() { + var si sysinfo.SysInfo + + si.GetSysInfo() + + log.Println(si) +} + +func (p plug) Info() map[string]string { + inf := make(map[string]string) + inf["name"] = "tmptest" + inf["version"] = "0.0.1" + inf["support"] = "debian" + inf["description"] = "Temporary testing" + inf["developer"] = "asandikci@aliberksandikci.com.tr" + return inf +}