diff --git a/.vscode/settings.json b/.vscode/settings.json index d34daa0..b874021 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,78 @@ { - "go.buildFlags": ["-tags=linux"], + // Go + "go.buildFlags": [ + "-tags=linux" // change this to "-tags=windows" when working for a windows feature + ], + "go.inlayHints.assignVariableTypes": true, // see variable types inline + "go.diagnostic.vulncheck": "Imports", // ? + // ------------------------------------------- + // Anchors + "commentAnchors.tags.list": [ + { + "tag": "FILLME", + "iconColor": "default", + "highlightColor": "#A8C993", + "scope": "workspace" + }, + { + "tag": "NEXT", + "iconColor": "default", + "highlightColor": "#12449f", + "scope": "file" + }, + { + "tag": "ANCHOR", + "iconColor": "default", + "highlightColor": "#A8C023", + "scope": "file" + }, + { + "tag": "TODO", + "iconColor": "blue", + "highlightColor": "#3ea8ff", + "scope": "workspace" + }, + { + "tag": "FIXME", + "iconColor": "red", + "highlightColor": "#F44336", + "scope": "workspace" + }, + { + "tag": "STUB", + "iconColor": "purple", + "highlightColor": "#BA68C8", + "scope": "file" + }, + { + "tag": "NOTE", + "iconColor": "orange", + "highlightColor": "#FFB300", + "scope": "file" + }, + { + "tag": "REVIEW", + "iconColor": "green", + "highlightColor": "#64DD17", + "scope": "workspace" + }, + { + "tag": "SECTION", + "iconColor": "blurple", + "highlightColor": "#896afc", + "scope": "workspace", + "behavior": "region" + }, + { + "tag": "LINK", + "iconColor": "#2ecc71", + "highlightColor": "#2ecc71", + "scope": "workspace", + "behavior": "link" + }, + ], + // LOCALIZATION + "editor.unicodeHighlight.allowedLocales": { + "tr": true + }, } \ No newline at end of file diff --git a/Makefile b/Makefile index a84fa1e..67bf1a2 100644 --- a/Makefile +++ b/Makefile @@ -49,10 +49,18 @@ install: @sudo mkdir -p "${DESTDIR}/${DATA_DIR}" linux_goloader_install: +# NEXT sudo go build -o ${DESTDIR}/usr/bin/${REPO_NAME} ./cmd/ahenk-go/ - + + @echo "linux_goloader_install option also removes all datadir!" + @sudo rm -rf "${DESTDIR}/${DATA_DIR}" + @sudo mkdir -p "${DESTDIR}/${DATA_DIR}" sudo cp -r ./plugins "${DESTDIR}/${DATA_DIR}/" + sudo cp -r ./pkg "${DESTDIR}/${DATA_DIR}/" + sudo cp go.mod "${DESTDIR}/${DATA_DIR}/" + sudo cp go.sum "${DESTDIR}/${DATA_DIR}/" + sudo cp version "${DESTDIR}/${DATA_DIR}/" windows_install: diff --git a/cmd/ahenk-go/ahenk-go.go b/cmd/ahenk-go/ahenk-go.go index 4bfdf17..e2ce317 100644 --- a/cmd/ahenk-go/ahenk-go.go +++ b/cmd/ahenk-go/ahenk-go.go @@ -4,9 +4,10 @@ import ( "log" "os" "os/user" + "time" + "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/cmd/plugin" "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/confdir" - "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/pluginmanager" "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils" "github.com/sevlyar/go-daemon" @@ -20,6 +21,7 @@ func main() { switch os.Args[1] { case "start": utils.CreatePath(confdir.Paths.Data) + utils.CreatePath(confdir.Paths.Logs) cntxt := &daemon.Context{ PidFileName: confdir.Paths.Pid, PidFilePerm: 0644, @@ -55,12 +57,16 @@ func main() { case "tmptest": log.Print("TEMPORARY TEST STARTED, log files are NOT redirecting!") - plugFunctions := map[string][]string{ - "tmptest": {"TmpTest"}, - "resources": {"AhenkInfo", "ResourceUsage"}, - } - pluginmanager.StartPlugins(plugFunctions) + plugFunctions := []string{"tmptest", "resources"} + plugin.ConnectPlugins(plugFunctions) + plugin.Tmptest.TmpTest() + plugin.LogPlugin("Agent Info", plugin.Resources.AgentInfo(), true) + plugin.LogPlugin("Resource Usage", plugin.Resources.ResourceUsage(), true) + + // comment these if you want to run usual code after tmptest + log.Println("exiting before making usual code") + os.Exit(0) } } else { panic("Please enter a valid option !") @@ -73,9 +79,6 @@ func main() { if current.Uid != "0" { log.Fatal("Ahenk-go requires superuser privilege") } - - // plugFunctions := map[string][]string{ - // "resources": {"AhenkInfo", "ResourceUsage"}, - // } - // pluginmanager.StartPlugins(plugFunctions) + time.Sleep(5 * time.Second) + log.Println("bye") } diff --git a/cmd/plugin/check.go b/cmd/plugin/check.go new file mode 100644 index 0000000..bfcd4a9 --- /dev/null +++ b/cmd/plugin/check.go @@ -0,0 +1,3 @@ +package plugin + +// NEXT diff --git a/pkg/pluginmanager/jitconf_linux.go b/cmd/plugin/jitconf_linux.go similarity index 96% rename from pkg/pluginmanager/jitconf_linux.go rename to cmd/plugin/jitconf_linux.go index 2690bc4..9afa4b1 100644 --- a/pkg/pluginmanager/jitconf_linux.go +++ b/cmd/plugin/jitconf_linux.go @@ -1,6 +1,6 @@ //go:build linux && !windows -package pluginmanager +package plugin import "github.com/eh-steve/goloader/jit" diff --git a/pkg/pluginmanager/jitconf_windows.go b/cmd/plugin/jitconf_windows.go similarity index 96% rename from pkg/pluginmanager/jitconf_windows.go rename to cmd/plugin/jitconf_windows.go index a18aa18..43b355d 100644 --- a/pkg/pluginmanager/jitconf_windows.go +++ b/cmd/plugin/jitconf_windows.go @@ -1,6 +1,6 @@ //go:build windows -package pluginmanager +package plugin import "github.com/eh-steve/goloader/jit" diff --git a/cmd/plugin/load.go b/cmd/plugin/load.go new file mode 100644 index 0000000..2cb2b9a --- /dev/null +++ b/cmd/plugin/load.go @@ -0,0 +1,29 @@ +package plugin + +import ( + "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/confdir" + "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils" + "github.com/eh-steve/goloader/jit" +) + +// Load Plugin placed in confdir.Paths.Plugins, returns empty interface with channel +// +// Do not forget to cast in plugin manager +func LoadPlugin(plugName string, conf jit.BuildConfig, chn chan interface{}) { + + // TODO if error caugth try without relative path, this will be good for local testing + loadable, err := jit.BuildGoPackage(conf, confdir.Paths.Plugins+plugName+"/") + utils.Check(err) + + module, err := loadable.Load() + utils.Check(err) + plugSymbols := module.SymbolsByPkg[loadable.ImportPath] + + // TODO also allow lookup another symbol other than PlugnameConnect + plugOut := plugSymbols[(utils.FirstUpperEN(plugName) + "Connect")] + if plugOut == nil { + panic("There is no exported symbol in " + plugName) + } + + chn <- plugOut +} diff --git a/cmd/plugin/manager.go b/cmd/plugin/manager.go new file mode 100644 index 0000000..3c17568 --- /dev/null +++ b/cmd/plugin/manager.go @@ -0,0 +1,106 @@ +package plugin // pluginmanager + +import ( + "encoding/json" + "log" + + "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils" +) + +// General Functions/Methods that each plugin has +type PlugGeneral interface { + // Get Plugin Info + Info() map[string]interface{} +} + +// plugins/resources +type ResourcesI interface { + // Get Agent Info, see plugins/resources for more information + AgentInfo() map[string]interface{} // LINK plugins/resources/main.go#AgentInfo + + // Get Resource Usage, see plugins/resources for more information + ResourceUsage() map[string]interface{} // LINK plugins/resources/main.go#ResourceUsage +} + +// plugins/tmptest +type TmpTestI interface { + // Run temporary tests + TmpTest() // LINK plugins/tmptest/main.go#TmpTest +} + +// FILLME[epic=new-plugin-template] +// type NewPluginInterfaceI interface { +// // explanation +// PluginFunction() returnType // link plugins/pluginname/file.go#PluginFunction +// } + +var Tmptest TmpTestI +var Resources ResourcesI + +// FILLME[epic=new-plugin-template] +// var pluginname NewPluginInterfaceI + +// Connect plugins to main code +// +// Gets plugin names as string slice +func ConnectPlugins(params []string) { + + conf := getJITConf() + chanPlug := make(chan interface{}) + + for _, v := range params { + go LoadPlugin(v, conf, chanPlug) + plug, ok := <-chanPlug + + switch v { + case "tmptest": + Tmptest = plug.(TmpTestI) + checkPlugin(Tmptest, ok) + case "resources": + Resources = plug.(ResourcesI) + checkPlugin(Resources, ok) + + // FILLME[epic=new-plugin-template] + // case "pluginname": + // Pluginname = plug.(NewPluginInterfaceI) + // checkPlugin(Pluginname, ok) + } + } +} + +// Logs plugin outputs. +func LogPlugin(title string, mp map[string]interface{}, toJson bool) { + log.Printf("\n----- %v -----\n", title) + if toJson { + data, err := json.MarshalIndent(&mp, "", " ") + utils.Check(err) + log.Println(string(data)) + } else { + for i, v := range mp { + log.Printf("%v: %v\n", i, v) + } + } +} + +// Checks plugin status +func checkPlugin(plugVal interface{}, status bool) { + if status { + if plugVal == nil { + log.Fatal("Plugin loaded but there is no value!") + } else { + plugInfo := plugVal.(PlugGeneral).Info() + log.Printf("Plugin \"%v\" loaded and ready to use with version \"%v\" ", plugInfo["name"], plugInfo["version"]) + } + } else { + if plugVal == nil { + log.Fatal("Plugin closed and there is no value! ") + } else { + log.Fatal("Plugin closed or there is an error") + } + } +} + +// // TODO response to Lider +// func createResponse() { + +// } diff --git a/cmd/plugin/refresh.go b/cmd/plugin/refresh.go new file mode 100644 index 0000000..bfcd4a9 --- /dev/null +++ b/cmd/plugin/refresh.go @@ -0,0 +1,3 @@ +package plugin + +// NEXT diff --git a/cmd/plugin/unload.go b/cmd/plugin/unload.go new file mode 100644 index 0000000..bfcd4a9 --- /dev/null +++ b/cmd/plugin/unload.go @@ -0,0 +1,3 @@ +package plugin + +// NEXT diff --git a/go.mod b/go.mod index 9901325..39b9a17 100644 --- a/go.mod +++ b/go.mod @@ -1,17 +1,17 @@ module git.aliberksandikci.com.tr/Liderahenk/ahenk-go -go 1.20 +go 1.21 require ( github.com/sevlyar/go-daemon v0.1.6 github.com/zcalusic/sysinfo v1.0.1 - golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b - golang.org/x/text v0.12.0 + golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 + golang.org/x/text v0.13.0 ) require ( - github.com/eh-steve/goloader v0.0.0-20230730231803-5c95d7a5f4e2 // indirect - github.com/go-ole/go-ole v1.2.6 // indirect + github.com/eh-steve/goloader v0.0.0-20230731163325-b789213e8550 // indirect + github.com/go-ole/go-ole v1.3.0 // indirect github.com/stretchr/testify v1.8.4 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect @@ -22,5 +22,5 @@ require ( github.com/eh-steve/goloader/jit v0.0.0-20230731163325-b789213e8550 github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect github.com/shirou/gopsutil v3.21.11+incompatible // direct - golang.org/x/sys v0.11.0 // direct + golang.org/x/sys v0.12.0 // direct ) diff --git a/go.sum b/go.sum index bab0633..a183255 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,22 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/eh-steve/goloader v0.0.0-20230730231803-5c95d7a5f4e2 h1:0mZ2Y8x4dhPfm9eOlbzBx31YSLaECdPvKEBrL5Hc0YE= -github.com/eh-steve/goloader v0.0.0-20230730231803-5c95d7a5f4e2/go.mod h1:k7xs3CUwCvOU9aw851I6AEb6ZzZJ3nos5dZ6A/2ewM0= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/eh-steve/goloader v0.0.0-20230731163325-b789213e8550 h1:8yXPOoUPeKZmC3QfqUY0wkerRf5OQ8YeCgSodrcC8J8= +github.com/eh-steve/goloader v0.0.0-20230731163325-b789213e8550/go.mod h1:k7xs3CUwCvOU9aw851I6AEb6ZzZJ3nos5dZ6A/2ewM0= github.com/eh-steve/goloader/jit v0.0.0-20230731163325-b789213e8550 h1:clY7T47fFZdFcILcu7uksS7FStRaKMssCYGW0l/AoMo= github.com/eh-steve/goloader/jit v0.0.0-20230731163325-b789213e8550/go.mod h1:p18VKcPYO8oWGYcQt/K5EGIGqak0ZT5HwVirGpUGZBg= -github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= +github.com/eh-steve/goloader/jit/testdata v0.0.0-20230730231803-5c95d7a5f4e2 h1:pulprUBu3Rncam2xFKzaUTf8SlbaoOHtNocYBSbvprI= +github.com/eh-steve/goloader/jit/testdata v0.0.0-20230730231803-5c95d7a5f4e2/go.mod h1:GyUY1mfY7NCdoVRo5H8+kmhLa/xE418ZcjfDfgwicXw= +github.com/eh-steve/goloader/unload v0.0.0-20230730231803-5c95d7a5f4e2 h1:XBYe9gRPYpfxBRO9kBeep/FZrbeIuxPn5d6dun2oLQU= +github.com/eh-steve/goloader/unload v0.0.0-20230730231803-5c95d7a5f4e2/go.mod h1:bA/aiceMvuZh45HAA/xzWJKT4E9EXqzmuPsB9yT/D/M= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= +github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA= github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8= +github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= +github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sevlyar/go-daemon v0.1.6 h1:EUh1MDjEM4BI109Jign0EaknA2izkOyi0LV3ro3QQGs= github.com/sevlyar/go-daemon v0.1.6/go.mod h1:6dJpPatBT9eUwM5VCw9Bt6CdX9Tk6UWvhW3MebLDRKE= github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= @@ -22,12 +31,15 @@ github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFi 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/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ= +golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= -golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/go.work.sum b/go.work.sum deleted file mode 100644 index 3aa05fa..0000000 --- a/go.work.sum +++ /dev/null @@ -1,4 +0,0 @@ -github.com/eh-steve/goloader/jit/testdata v0.0.0-20230730231803-5c95d7a5f4e2/go.mod h1:GyUY1mfY7NCdoVRo5H8+kmhLa/xE418ZcjfDfgwicXw= -github.com/eh-steve/goloader/unload v0.0.0-20230730231803-5c95d7a5f4e2/go.mod h1:bA/aiceMvuZh45HAA/xzWJKT4E9EXqzmuPsB9yT/D/M= -github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/pkg/confdir/confdir.go b/pkg/confdir/confdir.go index 247dc0d..32ff6f2 100644 --- a/pkg/confdir/confdir.go +++ b/pkg/confdir/confdir.go @@ -7,6 +7,7 @@ type Path struct { Plugins string `json:"Plugins"` Logs string `json:"Logs"` Pid string `json:"pid"` + Version string `json:"version"` } var Paths Path diff --git a/pkg/confdir/confdir_linux.go b/pkg/confdir/confdir_linux.go index 8f97ba9..230279a 100644 --- a/pkg/confdir/confdir_linux.go +++ b/pkg/confdir/confdir_linux.go @@ -12,5 +12,6 @@ func getPaths() Path { path.Plugins = path.Data + "plugins/" path.Logs = path.Data + "logs/" path.Pid = "/run/ahenk-go.pid" + path.Version = path.Data + "version" return path } diff --git a/pkg/confdir/go.mod b/pkg/confdir/go.mod deleted file mode 100644 index 654335c..0000000 --- a/pkg/confdir/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/confdir - -go 1.21.0 diff --git a/pkg/pluginmanager/go.mod b/pkg/pluginmanager/go.mod deleted file mode 100644 index 2d66bb9..0000000 --- a/pkg/pluginmanager/go.mod +++ /dev/null @@ -1,7 +0,0 @@ -module git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/pluginmanager - -go 1.21.0 - -require github.com/eh-steve/goloader/jit v0.0.0-20230731163325-b789213e8550 - -require github.com/eh-steve/goloader v0.0.0-20230730231803-5c95d7a5f4e2 // indirect diff --git a/pkg/pluginmanager/go.sum b/pkg/pluginmanager/go.sum deleted file mode 100644 index 7b3dda3..0000000 --- a/pkg/pluginmanager/go.sum +++ /dev/null @@ -1,4 +0,0 @@ -github.com/eh-steve/goloader v0.0.0-20230730231803-5c95d7a5f4e2 h1:0mZ2Y8x4dhPfm9eOlbzBx31YSLaECdPvKEBrL5Hc0YE= -github.com/eh-steve/goloader v0.0.0-20230730231803-5c95d7a5f4e2/go.mod h1:k7xs3CUwCvOU9aw851I6AEb6ZzZJ3nos5dZ6A/2ewM0= -github.com/eh-steve/goloader/jit v0.0.0-20230731163325-b789213e8550 h1:clY7T47fFZdFcILcu7uksS7FStRaKMssCYGW0l/AoMo= -github.com/eh-steve/goloader/jit v0.0.0-20230731163325-b789213e8550/go.mod h1:p18VKcPYO8oWGYcQt/K5EGIGqak0ZT5HwVirGpUGZBg= diff --git a/pkg/pluginmanager/pluginmanager.go b/pkg/pluginmanager/pluginmanager.go deleted file mode 100644 index 9c77b3d..0000000 --- a/pkg/pluginmanager/pluginmanager.go +++ /dev/null @@ -1,132 +0,0 @@ -package pluginmanager - -import "fmt" - -// General Functions/Methods that each plugin has -type PlugGeneral interface { - Info() (interface{}, error) -} - -// plugins/resources -type Resources interface { - AgentInfo() (interface{}, error) - ResourceUsage() (interface{}, error) -} - -type TmpTest interface { - TmpTest() (interface{}, error) -} - -type anny any - -// Start All plugins -func StartPlugins(params map[string][]string) { - // GeneralFunctions := []string{"Info"} - - conf := getJITConf() - chanPlug := make(chan PluginStruct) - - // var myint TmpTest - - // for i, v := range params { - // v := append(v, GeneralFunctions...) - // go LoadPlugin2(i, v, conf, chanPlug) - // } - - go LoadPlugin3("tmptest", conf, chanPlug) - a, ok := <-chanPlug - var an anny = a - fmt.Println(a, ok) - fmt.Println(an) - var aa []func() (interface{}, error) - for _, v := range a.Functions { - aa = append(aa, v) - } - fmt.Println(aa) - var loo interface{} = aa - fmt.Println(loo.(TmpTest)) -} - -// -// ------------------------- -// OLD PLUGIN IMPLEMENTATION -// ------------------------- -// - -// // 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(params ...string) { -// chanPlug := make(chan interface{}) - -// go LoadPlugin("resources", chanPlug) -// res, ok := <-chanPlug -// var resources Resources = res.(Resources) -// checkPlugin(resources, ok) - -// 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) -// // checkPlugin(res, ok) - -// // Run plugins concurrently and log out -// go logPlugin("AgentInfo", resources.AgentInfo(), true) -// for { -// go logPlugin("ResourceUsage", resources.ResourceUsage(), true) - -// // FILLME Running/Log out a plugin, template -// // go logPlugin("InfoAboutFunction", pluginName.Function(), true) - -// time.Sleep(30 * time.Second) -// } -// } - -// // Logs plugin outputs. -// func logPlugin(title string, mp map[string]interface{}, toJson bool) { -// log.Printf("\n----- %v -----\n", title) -// if toJson { -// data, err := json.MarshalIndent(&mp, "", " ") -// utils.Check(err) -// log.Println(string(data)) -// } else { -// for i, v := range mp { -// log.Printf("%v: %v\n", i, v) -// } -// } -// } - -// // Checks plugin status -// func checkPlugin(plugVal interface{}, status bool) { -// if status { -// if plugVal == nil { -// log.Fatal("Plugin loaded but there is no value!") -// } else { -// plugInfo, _ := plugVal.(PlugGeneral).Info() -// log.Printf("Plugin \"%v\" loaded and ready to use, version \"%v\" ", plugInfo["name"], plugInfo["version"]) -// } -// } else { -// if plugVal == nil { -// log.Fatal("Plugin closed and there is no value! ") -// } else { -// log.Fatal("Plugin closed or there is an error") -// } -// } -// } - -// // // TODO response to Lider -// // // func createResponse() { - -// // // } diff --git a/pkg/pluginmanager/pluginopener.go b/pkg/pluginmanager/pluginopener.go deleted file mode 100644 index 2516c85..0000000 --- a/pkg/pluginmanager/pluginopener.go +++ /dev/null @@ -1,140 +0,0 @@ -package pluginmanager - -import ( - "fmt" - "os" - "plugin" - - "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/confdir" - "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils" - "github.com/eh-steve/goloader/jit" -) - -// type mytype func() (interface{}, error) - -type PluginStruct struct { - PluginFunction map[string]interface{} - Functions map[string](func() (interface{}, error)) -} - -func LoadPlugin3(plugName string, conf jit.BuildConfig, chn chan PluginStruct) { - loadable, err := jit.BuildGoPackage(conf, confdir.Paths.Plugins+plugName+"/") - utils.Check(err) - module, err := loadable.Load() - utils.Check(err) - plugSymbols := module.SymbolsByPkg[loadable.ImportPath] - - // fmt.Println(*deneme, *deneme2) - - // var empt interface{} = plugSymbols - // a = empt.(TmpTest) - - // var a TmpTest - // jit.RegisterTypes(plugSymbols["TmpTest"]) - var plugMan PluginStruct - plugMan.PluginFunction = plugSymbols - fmt.Println(plugMan.PluginFunction) - plugMan.Functions = make(map[string](func() (interface{}, error))) - for i, v := range plugMan.PluginFunction { - switch f := v.(type) { - case func([]byte) (interface{}, error): - result, err := f([]byte(`{"k":"v"}`)) - if err != nil { - panic(err) - } - fmt.Println(result) - case func() (interface{}, error): - fmt.Println(i, 1) - plugMan.Functions[i] = f - default: - fmt.Println(f) - panic("Function signature was not what was expected") - } - } - fmt.Println(plugMan.Functions) - for i, o := range plugMan.Functions { - // fmt.Println(i, o) - fmt.Println(i, &o) - // fmt.Println(i, *o) - } - // plugMan.Functions["TmpTest"]() - fmt.Println(1) - chn <- plugMan - // var empt mytype = plugSymbols["TmpTest"].(mytype) - - // a := empt.(TmpTest) - // fmt.Println(empt) - - // switch f := plugSymbols["MyFunc"].(type) { - // case func([]byte) (interface{}, error): - // result, err := f([]byte(`{"k":"v"}`)) - // if err != nil { - // panic(err) - // } - // fmt.Println(result) - // default: - // fmt.Println(f) - // panic("Function signature was not what was expected") - // } - - // chn <- deneme -} - -// func LoadPlugin2(plugName string, plugFunction []string, conf jit.BuildConfig, chn chan interface{}) { - -// loadable, err := jit.BuildGoPackage(conf, confdir.Paths.Plugins+plugName+"/") -// utils.Check(err) - -// module, err := loadable.Load() - -// plugSymbols := module.SymbolsByPkg[loadable.ImportPath] -// utils.Check(err) - -// for i, v := range plugFunction { -// var f interface{} = plugSymbols - -// } - -// // switch f := plugSymbols["MyFunc"].(type) { -// // case func([]byte) (interface{}, error): -// // result, err := f([]byte(`{"k":"v"}`)) -// // if err != nil { -// // panic(err) -// // } -// // fmt.Println(result) -// // default: -// // fmt.Println(f) -// // panic("Function signature was not what was expected") -// // } - -// // chn <- -// } - -// -// ------------------------- -// OLD PLUGIN IMPLEMENTATION -// ------------------------- -// - -// Load Plugin placed in PluginDir, returns empty interface with channel -// Do not forget to cast in plugin manager -// -// Give Plugin Name as argument and be sure you compiled plugins with `-buildmode=plugin` to PluginDir as `pluginname.so` -func LoadPlugin(plugName string, chn chan interface{}) { - - // TODO if error caugth try without relative path, this will be good for local testing - plug, err := plugin.Open(confdir.Paths.Plugins + plugName + ".so") - utils.Check(err) - - // TODO also allow lookup another symbol other than PlugnameConnect - symPlug, err := plug.Lookup(utils.FirstUpperEN(plugName) + "Connect") - utils.Check(err) - - var plugOut interface{} - plugOut, ok := symPlug.(interface{}) - if !ok { - fmt.Println("unexpected type from module symbol") - os.Exit(1) - } - chn <- plugOut -} diff --git a/plugins/resources/go.mod b/plugins/resources/go.mod deleted file mode 100644 index 137add4..0000000 --- a/plugins/resources/go.mod +++ /dev/null @@ -1,19 +0,0 @@ -module git.aliberksandikci.com.tr/Liderahenk/ahenk-go/plugins/resources - -go 1.21.0 - -require git.aliberksandikci.com.tr/Liderahenk/ahenk-go v0.0.0-20230831165035-c1bc30d83082 - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/go-ole/go-ole v1.2.6 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/shirou/gopsutil v3.21.11+incompatible // indirect - github.com/tklauser/go-sysconf v0.3.12 // indirect - github.com/tklauser/numcpus v0.6.1 // indirect - github.com/yusufpapurcu/wmi v1.2.3 // indirect - github.com/zcalusic/sysinfo v1.0.1 // indirect - golang.org/x/sys v0.11.0 // indirect - golang.org/x/text v0.12.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/plugins/resources/go.sum b/plugins/resources/go.sum deleted file mode 100644 index 98031b2..0000000 --- a/plugins/resources/go.sum +++ /dev/null @@ -1,29 +0,0 @@ -git.aliberksandikci.com.tr/Liderahenk/ahenk-go v0.0.0-20230831165035-c1bc30d83082 h1:xSuQQVwNcORPGt23VKivLIhOipz0hx9e5evxxiyV/No= -git.aliberksandikci.com.tr/Liderahenk/ahenk-go v0.0.0-20230831165035-c1bc30d83082/go.mod h1:CsfyC8E2IhrkmUf84/GvIDaofgSxtYdw3lojgQ8YMYc= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= -github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= -github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU= -github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI= -github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk= -github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY= -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/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= -golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= -golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/plugins/resources/info.go b/plugins/resources/info.go index d6c6574..794b0c1 100644 --- a/plugins/resources/info.go +++ b/plugins/resources/info.go @@ -1,14 +1,14 @@ package resources -func Info() (interface{}, error) { +func (p plug) Info() map[string]interface{} { return map[string]interface{}{ "name": "resources", "version": "0.1.0", "support": map[string]interface{}{ - "linux": "debian", + "linux": []string{"debian", "pardus23"}, "windows": "10", }, "description": "Resource Usage Information and Controls", "developer": "asandikci@aliberksandikci.com.tr", - }, nil + } } diff --git a/plugins/resources/main.go b/plugins/resources/main.go index e3a0f0d..dbc8f54 100644 --- a/plugins/resources/main.go +++ b/plugins/resources/main.go @@ -1,20 +1,28 @@ package resources import ( + "os" "runtime" + "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/confdir" "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/osinfo" + "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils" ) +type plug string + +// exported plugin Symbol +var ResourcesConnect plug + // return instant resource usage information -func ResourceUsage() (interface{}, error) { +func (p plug) ResourceUsage() map[string]interface{} { // ANCHOR[id=ResourceUsage] var system osinfo.System system.GetSystemInfo() - data := map[string]interface{}{ + return map[string]interface{}{ // CPU Information "CPU Physical Core Count": system.CPU.Cores, - // "CPU Logical Core Count": system.CPU.Logical_core_count, // TODO + "CPU Logical Core Count": system.CPU.Threads, // "CPU Actual Hz": system.CPU.ActualHz, // TODO // "CPU Advertised Hz": system.CPU.Hz_advertised, // TODO // "Processor": system.CPU.Brand, // TODO @@ -28,35 +36,32 @@ func ResourceUsage() (interface{}, error) { "Usage Disk": system.Disk.Used, "Device": system.Disk.Devices, } - - // TODO see https://github.com/Pardus-LiderAhenk/ahenk/blob/master/src/plugins/resource-usage/resource_info_fetcher.py - return data, nil + // see https://github.com/Pardus-LiderAhenk/ahenk/blob/master/src/plugins/resource-usage/resource_info_fetcher.py } // return general Agent system information // // these values changes rarely, see ResourceUsage() function for instant resource usage information -func AgentInfo() (interface{}, error) { +func (p plug) AgentInfo() map[string]interface{} { // ANCHOR[id=AgentInfo] var system osinfo.System system.GetSystemInfo() + ver, err := os.ReadFile(confdir.Paths.Version) + utils.Check(err) // Common data - data := map[string]interface{}{ - "System": runtime.GOOS, - "DiskSpaceTotal": system.Disk.Total, - "MemoryTotal": system.Memory.Total, - // TODO "AhenkVersion": get Ahenk self version here - + return map[string]interface{}{ + "System": runtime.GOOS, + "AhenkVersion": string(ver), + "Hostname": system.Node.Hostname, + "KernelVersion": system.Kernel.Version, + "KernelRelease": system.Kernel.Release, "Name": system.OS.Name, "Distribution": system.OS.Distro, "Arch": system.OS.Arch, "Version": system.OS.Version, - "Hostname": system.Node.Hostname, - "KernelVersion": system.Kernel.Version, - "KernelRelease": system.Kernel.Release, - } - // REVIEW is calling all functions one by one slow downs code? - // TODO see https://github.com/Pardus-LiderAhenk/ahenk/blob/master/src/plugins/resource-usage/agent_info.py - return data, nil + "DiskSpaceTotal": system.Disk.Total, + "MemoryTotal": system.Memory.Total, + } + // see https://github.com/Pardus-LiderAhenk/ahenk/blob/master/src/plugins/resource-usage/agent_info.py } diff --git a/plugins/tmptest/go.mod b/plugins/tmptest/go.mod deleted file mode 100644 index 1543b3d..0000000 --- a/plugins/tmptest/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module git.aliberksandikci.com.tr/Liderahenk/ahenk-go/plugins/tmptest - -go 1.21.0 diff --git a/version b/version new file mode 100644 index 0000000..7bcd0e3 --- /dev/null +++ b/version @@ -0,0 +1 @@ +0.0.2 \ No newline at end of file