From 749a690b6cb05523017d5dd4111536d001907046 Mon Sep 17 00:00:00 2001 From: asandikci Date: Mon, 21 Aug 2023 16:45:11 +0300 Subject: [PATCH 01/14] =?UTF-8?q?neden=20=C3=A7al=C4=B1=C5=9Fm=C4=B1yon...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 5 +- Makefile | 49 +++++++++++++++++++ .../ahenkd-go.go => ahenk-go/main.go} | 2 +- cmd/ahenk-go/plugin-manager.go | 33 +++++++++++++ cmd/ahenk-go/plugin-opener.go | 25 ++++++++++ cmd/ahenkd-go/plugin-manager.go | 30 ------------ debian/changelog | 5 ++ debian/control | 27 ++++++++++ debian/copyright | 13 +++++ debian/rules | 10 ++++ {pkg/plugins => plugins}/resources/main.go | 17 ++++++- scripts/install.sh | 5 ++ 12 files changed, 186 insertions(+), 35 deletions(-) create mode 100644 Makefile rename cmd/{ahenkd-go/ahenkd-go.go => ahenk-go/main.go} (97%) create mode 100644 cmd/ahenk-go/plugin-manager.go create mode 100644 cmd/ahenk-go/plugin-opener.go delete mode 100644 cmd/ahenkd-go/plugin-manager.go create mode 100644 debian/changelog create mode 100644 debian/control create mode 100644 debian/copyright create mode 100644 debian/rules rename {pkg/plugins => plugins}/resources/main.go (91%) diff --git a/Dockerfile b/Dockerfile index 2599724..78b3cc2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,14 +13,15 @@ RUN apt-get update RUN apt-get upgrade -y ### INSTALL USEFUL UTILITIES ### -RUN apt-get install curl wget tree nano -y +RUN apt-get install curl wget tree nano procps -y ### INSTALL BUILD DEPENDENCIES ### -RUN apt-get install dh-golang -y +RUN apt-get install dh-golang dh-make -y RUN apt-get install dh-make-golang -y RUN apt-get install golang-github-sevlyar-go-daemon-dev -y RUN apt-get install golang-golang-x-exp-dev -y + ### CONFIGURATIONS ### # Enable UTF8 Encoding RUN echo "LC_ALL=en_US.UTF-8" >> /etc/environment diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b9f4c7a --- /dev/null +++ b/Makefile @@ -0,0 +1,49 @@ +REPO_NAME="ahenk-go" +REPO_LINK="https://git.aliberksandikci.com.tr/liderahenk/${REPO_NAME}" + +DAEMON_NAME="ahenk-go" +OLD_DAEMON_NAME="ahenkd-go" +PYTHON_DAEMON_NAME="ahenkd" + +CONF_DIR="/etc/ahenk-go/" +TEMP_DIR="$(mktemp -d)" +MAIN_DIR="${TEMP_DIR}/${REPO_NAME}/" + +info: + @echo "Made by Aliberk Sandıkçı - 2023" + @echo "preclean: for cleaning old files, configurations" + @echo "TODO test: Test go files" + @echo "install: Build and install ahenk-go" + @echo "TODO uninstall: Uninstall ahenk-go" + @echo "TODO clean: for postclean" + +preclean: + sudo rm -rf /usr/bin/$(DAEMON_NAME) + sudo rm -rf /usr/bin/${OLD_DAEMON_NAME} + sudo rm -rf /usr/bin/${PYTHON_DAEMON_NAME} + + @# TODO systemd control for both three process for docker + @# REVIEW are both killall and systemctl commands running? + @pgrep -x ${DAEMON_NAME} && (sudo killall "${DAEMON_NAME}" || sudo systemctl disable "${DAEMON_NAME}" || sudo systemctl stop "${DAEMON_NAME}") || echo "no ${DAEMON_NAME} found" + @pgrep -x ${OLD_DAEMON_NAME} && (sudo killall "${OLD_DAEMON_NAME}" || sudo systemctl disable "${OLD_DAEMON_NAME}" || sudo systemctl stop "${OLD_DAEMON_NAME}") || echo "no ${OLD_DAEMON_NAME} found" + + @# TODO + @# echo -e "Do you want to remove python implementation of ahenk if installed in system?" + # read -rp "(Y/N) " input + + @pgrep -x ${PYTHON_DAEMON_NAME} && (sudo killall "${PYTHON_DAEMON_NAME}" || sudo systemctl disable "${PYTHON_DAEMON_NAME}" || sudo systemctl stop "${PYTHON_DAEMON_NAME}") || echo "no ${PYTHON_DAEMON_NAME} found" + + sudo systemctl daemon-reload + sudo rm -rf ${CONF_DIR} + @echo -e "PRE-CLENING DONE\n" +test: + +install: + go build -o ${DESTDIR}/usr/bin/${REPO_NAME} ./cmd/ahenk-go/ + @sudo mkdir -p "${CONF_DIR}" + +uninstall: + + +clean: #postclean: + diff --git a/cmd/ahenkd-go/ahenkd-go.go b/cmd/ahenk-go/main.go similarity index 97% rename from cmd/ahenkd-go/ahenkd-go.go rename to cmd/ahenk-go/main.go index 76793d3..b729c50 100644 --- a/cmd/ahenkd-go/ahenkd-go.go +++ b/cmd/ahenk-go/main.go @@ -71,7 +71,7 @@ func main() { log.Print("STARTED AS NO-DAEMON") } } else { - log.Print("Please enter a valid option !") + panic("Please enter a valid option !") } PluginManager() log.Print("Plugin Manager Started Succesfully") diff --git a/cmd/ahenk-go/plugin-manager.go b/cmd/ahenk-go/plugin-manager.go new file mode 100644 index 0000000..14309f4 --- /dev/null +++ b/cmd/ahenk-go/plugin-manager.go @@ -0,0 +1,33 @@ +package main + +import "log" + +// type Greeter interface { +// Greet() +// } + +func PluginManager() { + // LoadPlugin("resources") + + // // 4. use the module + // greeter.Greet() + + // greeter.Myvar() + // // for { + // // logPlugin(greeter.AgentInfo()) + // // time.Sleep(30 * time.Second) + // // } + log.Print("plugin manager started succesfully") +} + +// func logPlugin(mp map[string]interface{}) { +// fmt.Printf("\nOs Info:\n") +// for i, v := range mp { +// fmt.Printf("%v: %v\n", i, v) +// } +// } + +// // TODO response to Lider +// // func createResponse() { + +// // } diff --git a/cmd/ahenk-go/plugin-opener.go b/cmd/ahenk-go/plugin-opener.go new file mode 100644 index 0000000..dd5c001 --- /dev/null +++ b/cmd/ahenk-go/plugin-opener.go @@ -0,0 +1,25 @@ +package main + +// // Load Plugin with plugin name and function name +// func LoadPlugin(plugName, funcName string) { +// plug, err := plugin.Open("../../plugins/resources/main.so") +// if err != nil { +// fmt.Println(err) +// os.Exit(1) +// } + +// symGreeter, err := plug.Lookup("Greeter") +// if err != nil { +// fmt.Println(err) +// os.Exit(1) +// } + +// var greeter Greeter +// greeter, ok := symGreeter.(Greeter) +// if !ok { +// fmt.Println("unexpected type from module symbol") +// os.Exit(1) +// } +// } + +// // NEXT move plugin-manager.go main here ! diff --git a/cmd/ahenkd-go/plugin-manager.go b/cmd/ahenkd-go/plugin-manager.go deleted file mode 100644 index 2b6a3d5..0000000 --- a/cmd/ahenkd-go/plugin-manager.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "ahenk-go/pkg/plugins/resources" - "fmt" - "log" - "time" -) - -func PluginManager() { - log.Print("Plugin Manager Started") - - for { - logPlugin(resources.AgentInfo()) - time.Sleep(30 * time.Second) - } - -} - -func logPlugin(mp map[string]interface{}) { - fmt.Printf("\nOs Info:\n") - for i, v := range mp { - fmt.Printf("%v: %v\n", i, v) - } -} - -// TODO response to Lider -// func createResponse() { - -// } diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..7900dfa --- /dev/null +++ b/debian/changelog @@ -0,0 +1,5 @@ +ahenk-go (0.0.1-1) UNRELEASED; urgency=low + + * Initial release + + -- Aliberk Sandıkçı Mon, 21 Aug 2023 13:05:24 +0000 \ No newline at end of file diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..4dd4456 --- /dev/null +++ b/debian/control @@ -0,0 +1,27 @@ +Source: ahenk-go +Maintainer: Aliberk Sandıkçı +Uploaders: Aliberk Sandıkçı +Section: utils +Testsuite: autopkgtest-pkg-go +Priority: optional +Build-Depends: debhelper-compat (= 13), + dh-golang, + golang-any, + golang-github-sevlyar-go-daemon-dev, + golang-golang-x-exp-dev +Standards-Version: 4.6.0 +Vcs-Browser: https://git.aliberksandikci.com.tr/Liderahenk/go-ahenk +Vcs-Git: https://git.aliberksandikci.com.tr/Liderahenk/go-ahenk.git +Homepage: https://liderahenk.org +Rules-Requires-Root: no +XS-Go-Import-Path: git.aliberksandikci.com.tr/Liderahenk/ahenk-go + +Package: ahenk-go +Architecture: any +Depends: ${misc:Depends}, + ${shlibs:Depends} +Built-Using: ${misc:Built-Using} +Description: The client side of the Lider Ahenk Project + Lider Ahenk is an open source project which provides solutions + to manage, monitor and audit unlimited number of different + systems and users on a network. diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..6fc10e9 --- /dev/null +++ b/debian/copyright @@ -0,0 +1,13 @@ +Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ +Upstream-Name: ahenk-go +Upstream-Contact: Aliberk Sandıkçı +Source: https://git.aliberksandikci.com.tr/Liderahenk/ahenk-go + +Files: * +Copyright: 2023 Aliberk Sandıkçı +License: LGPL-3.0+ + +Files: debian/* +Copyright: 2023 Aliberk Sandıkçı +License: LGPL-3.0+ +Comment: Debian packaging is licensed under the same terms as upstream diff --git a/debian/rules b/debian/rules new file mode 100644 index 0000000..954662b --- /dev/null +++ b/debian/rules @@ -0,0 +1,10 @@ +#!/usr/bin/make -f + +%: + dh $@ --builddirectory=_build --buildsystem=golang --with=golang + +override_dh_auto_install: + dh_auto_install -- --no-source + # dh_systemd_enable || true + # dh_systemd_start || true + dh_installsystemd diff --git a/pkg/plugins/resources/main.go b/plugins/resources/main.go similarity index 91% rename from pkg/plugins/resources/main.go rename to plugins/resources/main.go index c7cd9b6..491c766 100644 --- a/pkg/plugins/resources/main.go +++ b/plugins/resources/main.go @@ -1,10 +1,13 @@ -package resources +package main import ( "ahenk-go/pkg/osinfo" + "fmt" "runtime" ) +type greeting string + // return instant resource usage information func ResourceUsage() map[string]string { data := map[string]string{ @@ -27,7 +30,7 @@ func ResourceUsage() map[string]string { } // return general Agent information (that changes rarely) -func AgentInfo() map[string]interface{} { +func (g greeting) AgentInfo() map[string]interface{} { data := map[string]interface{}{ "System": runtime.GOOS, "Release": osinfo.GetKernelInfo()["Release"], // TODO 'agentVersion': self.get_agent_version(), @@ -67,3 +70,13 @@ func Info() map[string]string { // inf["machine_oriented"] = "false" return inf } + +func (g greeting) Greet() { + fmt.Println("Hello Universe") +} +func (g greeting) Myvar() { + fmt.Println("I am here") +} + +// this is exported +var Greeter greeting diff --git a/scripts/install.sh b/scripts/install.sh index 7051e3a..9bd5670 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -1,6 +1,11 @@ #!/bin/bash ### Ahenk Agent Local Installation Script +### IMPORTANT NOTICE ! +# Consider to use makefile for building or .deb file for installing package, +# using this scripts is not suggested anymore ! + +### --------------------- ### ### --> Prerequisites # git -- 2.30.2 From 746a918833a4688057ac6142c44df91bbc3e16f0 Mon Sep 17 00:00:00 2001 From: asandikci Date: Mon, 21 Aug 2023 17:22:59 +0300 Subject: [PATCH 02/14] =?UTF-8?q?Ali=20R=C4=B1za=20abi=20m=C3=BCsait=20olu?= =?UTF-8?q?nca=20yard=C4=B1m=20edebilir=20misin=F0=9F=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 21f5d44..241957c 100644 --- a/README.md +++ b/README.md @@ -2,28 +2,40 @@ Ahenk Agent for Liderahenk Central Management System - Go Implementation -You can find binary packages in [Releases](/releases) section or build from source code yourself. +## !!! Yardım Gerekli !!! +deb paketi yapmak, kurmak, güncellemek işlemleri arasında kayboldum. - +SORU-1 deb paketi paketlerken illa remote bir git reposundan kodu çekmek gerekir mi? (dh-make-golang böyle çalıştığı için soruyorum) -If you do not prefer to use binary packages you can also use installation scripts -Development Dependencies -```bash - go > 1.20 - git -``` +SORU-2 makefile, dpkg-buildpackage, dpkg-deb, gbp buildpackage, dh-make, debhelper komutları arasındaki fark nedir? Neden bunca farklı komut var ve bu kadar karışık... + +SORU-3 makefile dpkg-buildpackage kullanmadan önce dosyaları doğru yere yerleştirmek ve gerekli konfigrasyonları yapmak için mi? Eğer öyle ise doğru yer neresi, DESTDIR neresi olmalı ki dpkg-buildpackage .deb dosyasını oluştururken kuracağı paketleri doğru anlasın? + +SORU-4 systemd dosyalarını kurmak için makefile veya dpkg-buildpackage kullanarak yapabileceğim bir şey var mı? Yoksa git.pardus.org.tr > eta-pulse-config deposundaki örnek programdaki gibi `rules` dosyası içinden değişiklik yapmalıyım? Eğer evet ise yeni sürümlerde kaldırıldığı yazıyor, eta-pulse-config'deki gibi compat 9 sürümünü kullanmaya çalışınca dpkg-buildpacke şöyle error veriyor: `dh: warning: Compatibility levels before 10 are deprecated (level 9 in use)` ne yapmamı önerirsin(iz)? + +YAPMAK İSTEDİĞİM FARKLI AŞAMALAR: +1- debian paketleme için gerekli dosyaları (control,rules...) oluşturma + - manuel + - dh-make? +2- .deb paketleme. öyle ki oluşan bu deb paketi çalıştırıldığında cmd/ahenk-go içinde buildlenmiş dosyaları /usr/bin/ahenk-go'ya, ahenk-go.service dosyasını /lib/systemd/system altına .... koysun. + - dpkg-buildpackage? + - gbp buildpackage? +2.1- Ayrıca source code paketi oluşturma ve bu paketten tekrar .deb oluşturabilme + +3- .deb paketi kurma ve paketi sistemden kaldırma işlemleri. + - deb zaten kendi mi yapıyor bunu? + +4- koduma eklemeler yaptım ve tekrardan .deb paketi paketlemek istiyorum, güncellemeleri hangi komut ile veya hangi şekilde yapmalıyım? + +diğer branchların son durumu: +- main: en son 1 hafta önce denediğim dosyalar ve scripts/docker-build.sh son hâli (docker üzerinde denediğim build işleme, dh-make-golang ile) +- debian/sid: main'deki dosyaları buildlediğim, dh-make-golang [guide](https://people.debian.org/~stapelberg/2015/07/27/dh-make-golang.html)'i sonucu oluşturduğum branch + +özellikle bu guide üzerinden ve scripts/docker-build.sh üzerinden çalıştığım için bir hatam varsa buralarda olma ihtimali büyük +--- -Install with Script -```bash -wget -qO- https://git.aliberksandikci.com.tr/liderahenk/ahenk-go/raw/branch/main/scripts/install.sh | bash -``` ### Useful Links | Explanation | Link | -- 2.30.2 From cdafd63ddfd28f688c1e8d6d68da5b1b9ac14db6 Mon Sep 17 00:00:00 2001 From: asandikci Date: Mon, 21 Aug 2023 17:22:59 +0300 Subject: [PATCH 03/14] =?UTF-8?q?Ali=20R=C4=B1za=20abi=20m=C3=BCsait=20olu?= =?UTF-8?q?nca=20yard=C4=B1m=20edebilir=20misin=F0=9F=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 241957c..f584280 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ SORU-3 makefile dpkg-buildpackage kullanmadan önce dosyaları doğru yere yerle SORU-4 systemd dosyalarını kurmak için makefile veya dpkg-buildpackage kullanarak yapabileceğim bir şey var mı? Yoksa git.pardus.org.tr > eta-pulse-config deposundaki örnek programdaki gibi `rules` dosyası içinden değişiklik yapmalıyım? Eğer evet ise yeni sürümlerde kaldırıldığı yazıyor, eta-pulse-config'deki gibi compat 9 sürümünü kullanmaya çalışınca dpkg-buildpacke şöyle error veriyor: `dh: warning: Compatibility levels before 10 are deprecated (level 9 in use)` ne yapmamı önerirsin(iz)? YAPMAK İSTEDİĞİM FARKLI AŞAMALAR: +<<<<<<< HEAD 1- debian paketleme için gerekli dosyaları (control,rules...) oluşturma - manuel - dh-make? @@ -28,6 +29,21 @@ YAPMAK İSTEDİĞİM FARKLI AŞAMALAR: - deb zaten kendi mi yapıyor bunu? 4- koduma eklemeler yaptım ve tekrardan .deb paketi paketlemek istiyorum, güncellemeleri hangi komut ile veya hangi şekilde yapmalıyım? +======= +1. debian paketleme için gerekli dosyaları (control,rules...) oluşturma + - manuel + - dh-make? + +2. .deb paketleme. öyle ki oluşan bu deb paketi çalıştırıldığında cmd/ahenk-go içinde buildlenmiş dosyaları /usr/bin/ahenk-go'ya, ahenk-go.service dosyasını /lib/systemd/system altına .... koysun. + - dpkg-buildpackage? + - gbp buildpackage? + - 2.1. Ayrıca source code paketi oluşturma ve bu paketten tekrar .deb oluşturabilme + +3. .deb paketi kurma ve paketi sistemden kaldırma işlemleri. + - deb zaten kendi mi yapıyor bunu? + +4. koduma eklemeler yaptım ve tekrardan .deb paketi paketlemek istiyorum, güncellemeleri hangi komut ile veya hangi şekilde yapmalıyım? +>>>>>>> 417461b (Ali Rıza abi müsait olunca yardım edebilir misin😅) diğer branchların son durumu: - main: en son 1 hafta önce denediğim dosyalar ve scripts/docker-build.sh son hâli (docker üzerinde denediğim build işleme, dh-make-golang ile) -- 2.30.2 From f4091fc9aee0f55395ba42d273bce90695d048cf Mon Sep 17 00:00:00 2001 From: asandikci Date: Wed, 23 Aug 2023 22:49:17 +0300 Subject: [PATCH 04/14] fix: update package names --- cmd/ahenk-go/main.go | 3 ++- go.mod | 2 +- pkg/osinfo/main.go | 2 +- plugins/resources/main.go | 3 ++- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cmd/ahenk-go/main.go b/cmd/ahenk-go/main.go index b729c50..7fff023 100644 --- a/cmd/ahenk-go/main.go +++ b/cmd/ahenk-go/main.go @@ -1,11 +1,12 @@ package main import ( - "ahenk-go/pkg/utils" "log" "os" "syscall" + "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils" + "github.com/sevlyar/go-daemon" "golang.org/x/exp/slices" ) diff --git a/go.mod b/go.mod index 4875448..b004aba 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module ahenk-go +module git.aliberksandikci.com.tr/Liderahenk/ahenk-go go 1.20 diff --git a/pkg/osinfo/main.go b/pkg/osinfo/main.go index 931f398..3848ac3 100644 --- a/pkg/osinfo/main.go +++ b/pkg/osinfo/main.go @@ -1,9 +1,9 @@ package osinfo import ( - "ahenk-go/pkg/utils" "syscall" + "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils" "github.com/shirou/gopsutil/disk" ) diff --git a/plugins/resources/main.go b/plugins/resources/main.go index 491c766..ec9aadd 100644 --- a/plugins/resources/main.go +++ b/plugins/resources/main.go @@ -1,9 +1,10 @@ package main import ( - "ahenk-go/pkg/osinfo" "fmt" "runtime" + + "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/osinfo" ) type greeting string -- 2.30.2 From b257abb63fa747d830f0cdb2f7cdb9e43a248f13 Mon Sep 17 00:00:00 2001 From: asandikci Date: Wed, 23 Aug 2023 22:54:49 +0300 Subject: [PATCH 05/14] update deb packaging --- Makefile | 42 ++++++++++++++++++++++-------------------- debian/control | 3 ++- debian/rules | 6 ++---- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index b9f4c7a..b517440 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,22 @@ -REPO_NAME="ahenk-go" -REPO_LINK="https://git.aliberksandikci.com.tr/liderahenk/${REPO_NAME}" +REPO_NAME=ahenk-go +REPO_LINK=https://git.aliberksandikci.com.tr/liderahenk/${REPO_NAME} -DAEMON_NAME="ahenk-go" -OLD_DAEMON_NAME="ahenkd-go" -PYTHON_DAEMON_NAME="ahenkd" +DAEMON_NAME=ahenk-go +OLD_DAEMON_NAME=ahenkd-go +PYTHON_DAEMON_NAME=ahenkd -CONF_DIR="/etc/ahenk-go/" -TEMP_DIR="$(mktemp -d)" -MAIN_DIR="${TEMP_DIR}/${REPO_NAME}/" +DATA_DIR=/etc/ahenk-go/ +LIB_DIR=/usr/share/ahenk-go/ +PLUGIN_DIR=${LIB_DIR}/plugins/ +TEMP_DIR=$(mktemp -d) +MAIN_DIR=${TEMP_DIR}/${REPO_NAME}/ info: @echo "Made by Aliberk Sandıkçı - 2023" @echo "preclean: for cleaning old files, configurations" @echo "TODO test: Test go files" - @echo "install: Build and install ahenk-go" - @echo "TODO uninstall: Uninstall ahenk-go" - @echo "TODO clean: for postclean" + @echo "install: Build and install ahenk-go to DESTDIR" + @echo "uninstall: Uninstall ahenk-go from DESTDIR" preclean: sudo rm -rf /usr/bin/$(DAEMON_NAME) @@ -29,21 +30,22 @@ preclean: @# TODO @# echo -e "Do you want to remove python implementation of ahenk if installed in system?" - # read -rp "(Y/N) " input + @# read -rp "(Y/N) " input @pgrep -x ${PYTHON_DAEMON_NAME} && (sudo killall "${PYTHON_DAEMON_NAME}" || sudo systemctl disable "${PYTHON_DAEMON_NAME}" || sudo systemctl stop "${PYTHON_DAEMON_NAME}") || echo "no ${PYTHON_DAEMON_NAME} found" sudo systemctl daemon-reload - sudo rm -rf ${CONF_DIR} + sudo rm -rf ${DATA_DIR} @echo -e "PRE-CLENING DONE\n" test: - + @echo -e "Testing go files not implemented yet!" install: - go build -o ${DESTDIR}/usr/bin/${REPO_NAME} ./cmd/ahenk-go/ - @sudo mkdir -p "${CONF_DIR}" + sudo go build -o ${DESTDIR}/usr/bin/${REPO_NAME} ./cmd/ahenk-go/ + @sudo mkdir -p "${DESTDIR}/${LIB_DIR}" + @sudo mkdir -p "${DESTDIR}/${PLUGIN_DIR}" + + sudo go build -buildmode=plugin -o ${DESTDIR}/${PLUGIN_DIR}/resources.so ./plugins/resources + @sudo mkdir -p "${DESTDIR}/${DATA_DIR}" uninstall: - - -clean: #postclean: - + @sudo rm -rf ${DESTDIR}/usr/bin/${REPO_NAME} diff --git a/debian/control b/debian/control index 4dd4456..e1a75f9 100644 --- a/debian/control +++ b/debian/control @@ -4,7 +4,8 @@ Uploaders: Aliberk Sandıkçı Section: utils Testsuite: autopkgtest-pkg-go Priority: optional -Build-Depends: debhelper-compat (= 13), +Build-Depends: debhelper (>= 13), + debhelper-compat (= 10), dh-golang, golang-any, golang-github-sevlyar-go-daemon-dev, diff --git a/debian/rules b/debian/rules index 954662b..b61fdd4 100644 --- a/debian/rules +++ b/debian/rules @@ -1,10 +1,8 @@ #!/usr/bin/make -f %: - dh $@ --builddirectory=_build --buildsystem=golang --with=golang + dh $@ override_dh_auto_install: - dh_auto_install -- --no-source - # dh_systemd_enable || true - # dh_systemd_start || true + dh_auto_install dh_installsystemd -- 2.30.2 From e2b0ba4d179250762a907b6b7d9b04c15465374e Mon Sep 17 00:00:00 2001 From: asandikci Date: Wed, 23 Aug 2023 22:57:13 +0300 Subject: [PATCH 06/14] hallettim --- README.md | 49 +------------------------------------------------ 1 file changed, 1 insertion(+), 48 deletions(-) diff --git a/README.md b/README.md index f584280..fae5ea1 100644 --- a/README.md +++ b/README.md @@ -2,54 +2,7 @@ Ahenk Agent for Liderahenk Central Management System - Go Implementation -## !!! Yardım Gerekli !!! -deb paketi yapmak, kurmak, güncellemek işlemleri arasında kayboldum. - -ulaşmaya çalıştığım structure: ahenk-go dosyası /usr/bin/ahenk-go dizininde daemon olarak çalışacak, .service file doğru yere koyulacak, ayrıca -daha tam belirlemedim- farklı bir dizin içinde pluginler bulunacak. - -SORU-1 deb paketi paketlerken illa remote bir git reposundan kodu çekmek gerekir mi? (dh-make-golang böyle çalıştığı için soruyorum) - -SORU-2 makefile, dpkg-buildpackage, dpkg-deb, gbp buildpackage, dh-make, debhelper komutları arasındaki fark nedir? Neden bunca farklı komut var ve bu kadar karışık... - -SORU-3 makefile dpkg-buildpackage kullanmadan önce dosyaları doğru yere yerleştirmek ve gerekli konfigrasyonları yapmak için mi? Eğer öyle ise doğru yer neresi, DESTDIR neresi olmalı ki dpkg-buildpackage .deb dosyasını oluştururken kuracağı paketleri doğru anlasın? - -SORU-4 systemd dosyalarını kurmak için makefile veya dpkg-buildpackage kullanarak yapabileceğim bir şey var mı? Yoksa git.pardus.org.tr > eta-pulse-config deposundaki örnek programdaki gibi `rules` dosyası içinden değişiklik yapmalıyım? Eğer evet ise yeni sürümlerde kaldırıldığı yazıyor, eta-pulse-config'deki gibi compat 9 sürümünü kullanmaya çalışınca dpkg-buildpacke şöyle error veriyor: `dh: warning: Compatibility levels before 10 are deprecated (level 9 in use)` ne yapmamı önerirsin(iz)? - -YAPMAK İSTEDİĞİM FARKLI AŞAMALAR: -<<<<<<< HEAD -1- debian paketleme için gerekli dosyaları (control,rules...) oluşturma - - manuel - - dh-make? -2- .deb paketleme. öyle ki oluşan bu deb paketi çalıştırıldığında cmd/ahenk-go içinde buildlenmiş dosyaları /usr/bin/ahenk-go'ya, ahenk-go.service dosyasını /lib/systemd/system altına .... koysun. - - dpkg-buildpackage? - - gbp buildpackage? -2.1- Ayrıca source code paketi oluşturma ve bu paketten tekrar .deb oluşturabilme - -3- .deb paketi kurma ve paketi sistemden kaldırma işlemleri. - - deb zaten kendi mi yapıyor bunu? - -4- koduma eklemeler yaptım ve tekrardan .deb paketi paketlemek istiyorum, güncellemeleri hangi komut ile veya hangi şekilde yapmalıyım? -======= -1. debian paketleme için gerekli dosyaları (control,rules...) oluşturma - - manuel - - dh-make? - -2. .deb paketleme. öyle ki oluşan bu deb paketi çalıştırıldığında cmd/ahenk-go içinde buildlenmiş dosyaları /usr/bin/ahenk-go'ya, ahenk-go.service dosyasını /lib/systemd/system altına .... koysun. - - dpkg-buildpackage? - - gbp buildpackage? - - 2.1. Ayrıca source code paketi oluşturma ve bu paketten tekrar .deb oluşturabilme - -3. .deb paketi kurma ve paketi sistemden kaldırma işlemleri. - - deb zaten kendi mi yapıyor bunu? - -4. koduma eklemeler yaptım ve tekrardan .deb paketi paketlemek istiyorum, güncellemeleri hangi komut ile veya hangi şekilde yapmalıyım? ->>>>>>> 417461b (Ali Rıza abi müsait olunca yardım edebilir misin😅) - -diğer branchların son durumu: -- main: en son 1 hafta önce denediğim dosyalar ve scripts/docker-build.sh son hâli (docker üzerinde denediğim build işleme, dh-make-golang ile) -- debian/sid: main'deki dosyaları buildlediğim, dh-make-golang [guide](https://people.debian.org/~stapelberg/2015/07/27/dh-make-golang.html)'i sonucu oluşturduğum branch - -özellikle bu guide üzerinden ve scripts/docker-build.sh üzerinden çalıştığım için bir hatam varsa buralarda olma ihtimali büyük +Tamamdır hallettim sorunu. dpkg-buildpackage, makefile'ı çağırıyormuş. Anlayana kadar canım çıktı 😅 --- -- 2.30.2 From be2ec068160630a445031bac5f86d53b3601755a Mon Sep 17 00:00:00 2001 From: asandikci Date: Thu, 24 Aug 2023 15:20:43 +0300 Subject: [PATCH 07/14] update service --- debian/{ahenk-go.service => ahenkd-go.service} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename debian/{ahenk-go.service => ahenkd-go.service} (70%) diff --git a/debian/ahenk-go.service b/debian/ahenkd-go.service similarity index 70% rename from debian/ahenk-go.service rename to debian/ahenkd-go.service index 142509a..113bd46 100644 --- a/debian/ahenk-go.service +++ b/debian/ahenkd-go.service @@ -7,8 +7,8 @@ After=network.target network-online.target Type=forking User=root Group=root -ExecStart=/usr/bin/ahenkd-go start -ExecStop=/usr/bin/ahenkd-go stop +ExecStart=/usr/bin/ahenk-go start +ExecStop=/usr/bin/ahenk-go stop ExecReload=kill -HUP $MAINPID PIDFile=/run/ahenkd-go.pid Restart=always -- 2.30.2 From fc2feaf3af66341ea7ba64feea5fbad40ada8004 Mon Sep 17 00:00:00 2001 From: asandikci Date: Thu, 24 Aug 2023 15:26:05 +0300 Subject: [PATCH 08/14] change dockerfile - change base image to bitnami/minideb - remove unnecessary dependencies --- Dockerfile | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 78b3cc2..946ed76 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,24 +3,16 @@ # after cd ahenk-go # docker run -it -d --name CONT_NAME --mount type=bind,source=".",target=/ahenk-go/ IMAGE_NAME:VERSION "/bin/bash" - -ARG CODE_VERSION=sid -FROM debian:${CODE_VERSION} - -RUN su -c "echo 'deb http://ftp.tr.debian.org/debian sid main' >> /etc/apt/sources.list.d/sid.list" +FROM bitnami/minideb RUN apt-get update RUN apt-get upgrade -y ### INSTALL USEFUL UTILITIES ### -RUN apt-get install curl wget tree nano procps -y +RUN apt-get install curl wget tree nano procps lintian -y ### INSTALL BUILD DEPENDENCIES ### -RUN apt-get install dh-golang dh-make -y -RUN apt-get install dh-make-golang -y -RUN apt-get install golang-github-sevlyar-go-daemon-dev -y -RUN apt-get install golang-golang-x-exp-dev -y - +RUN apt-get install sudo dpkg-dev debhelper golang-any -y ### CONFIGURATIONS ### # Enable UTF8 Encoding -- 2.30.2 From efc07514b1bc64adec2d2e1b9568e713a83cd7be Mon Sep 17 00:00:00 2001 From: asandikci Date: Thu, 24 Aug 2023 15:26:23 +0300 Subject: [PATCH 09/14] update debian control file --- debian/control | 5 ----- 1 file changed, 5 deletions(-) diff --git a/debian/control b/debian/control index e1a75f9..00727f0 100644 --- a/debian/control +++ b/debian/control @@ -1,15 +1,10 @@ Source: ahenk-go Maintainer: Aliberk Sandıkçı -Uploaders: Aliberk Sandıkçı Section: utils Testsuite: autopkgtest-pkg-go Priority: optional Build-Depends: debhelper (>= 13), debhelper-compat (= 10), - dh-golang, - golang-any, - golang-github-sevlyar-go-daemon-dev, - golang-golang-x-exp-dev Standards-Version: 4.6.0 Vcs-Browser: https://git.aliberksandikci.com.tr/Liderahenk/go-ahenk Vcs-Git: https://git.aliberksandikci.com.tr/Liderahenk/go-ahenk.git -- 2.30.2 From 73c6ceac7fff41775ae53ac89a62b5cbda6fec70 Mon Sep 17 00:00:00 2001 From: asandikci Date: Thu, 24 Aug 2023 15:27:00 +0300 Subject: [PATCH 10/14] feat: low files when runnin as nodaemon --- cmd/ahenk-go/main.go | 25 +++++++++++++++++++++---- pkg/utils/main.go | 8 ++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/cmd/ahenk-go/main.go b/cmd/ahenk-go/main.go index 7fff023..b226d11 100644 --- a/cmd/ahenk-go/main.go +++ b/cmd/ahenk-go/main.go @@ -4,6 +4,7 @@ import ( "log" "os" "syscall" + "time" "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils" @@ -13,7 +14,10 @@ import ( const PidFile = "/run/ahenkd-go.pid" const ExecutablePath = "/usr/bin/ahenkd-go" -const ConfDir = "/etc/ahenk-go/" +const DataDir = "/etc/ahenk-go/" +const LogFile = DataDir + "ahenk.log" +const LibDir = "/usr/share/ahenk-go/" +const PluginDir = LibDir + "/plugins/" // FIXME there isn't any difference with Stop() function // TODO There can be a Start() function in it but start function doesnt work properly right now @@ -40,13 +44,13 @@ func main() { if len(os.Args) == 2 && slices.Contains([]string{"start", "stop", "restart", "nodaemon"}, os.Args[1]) { switch os.Args[1] { case "start": - utils.CreatePath(ConfDir) + utils.CreatePath(DataDir) cntxt := &daemon.Context{ PidFileName: PidFile, PidFilePerm: 0644, - LogFileName: ConfDir + "ahenkd.log", + LogFileName: LogFile, LogFilePerm: 0640, - WorkDir: ConfDir, + WorkDir: LibDir, Umask: 027, Args: []string{ExecutablePath, "start"}, } @@ -70,6 +74,19 @@ func main() { os.Exit(0) case "nodaemon": log.Print("STARTED AS NO-DAEMON") + + f := utils.OpenLogFile(LogFile) + defer f.Close() + log.SetOutput(f) + log.Printf("Log test") + + time.Sleep(10 * time.Second) + log.Print("Killed") + case "tmptest": + log.Print("TEMPORARY TEST") + + time.Sleep(3 * time.Second) + log.Print("Killed") } } else { panic("Please enter a valid option !") diff --git a/pkg/utils/main.go b/pkg/utils/main.go index 2475994..b39561f 100644 --- a/pkg/utils/main.go +++ b/pkg/utils/main.go @@ -47,3 +47,11 @@ func Check(err error) { panic(err) } } + +func OpenLogFile(path string) *os.File { + f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + if err != nil { + panic(err) + } + return f +} -- 2.30.2 From b3fcfc2f069f7a103fd3e711a00461e7e9585ae5 Mon Sep 17 00:00:00 2001 From: asandikci Date: Thu, 24 Aug 2023 17:44:01 +0300 Subject: [PATCH 11/14] feat: implement plugin structure - Plugin Manager - Plugin Opener - Code refactoring - Memory usage with gopsutil/mem --- cmd/ahenk-go/main.go | 55 +++++++++++++++++----------------- cmd/ahenk-go/plugin-manager.go | 47 +++++++++++++++-------------- cmd/ahenk-go/plugin-opener.go | 47 ++++++++++++++++------------- go.mod | 1 + go.sum | 2 ++ pkg/osinfo/main.go | 21 +++++++++++-- pkg/utils/main.go | 8 +++++ plugins/resources/main.go | 53 ++++++++++++++------------------ 8 files changed, 131 insertions(+), 103 deletions(-) diff --git a/cmd/ahenk-go/main.go b/cmd/ahenk-go/main.go index b226d11..72afee4 100644 --- a/cmd/ahenk-go/main.go +++ b/cmd/ahenk-go/main.go @@ -19,29 +19,9 @@ const LogFile = DataDir + "ahenk.log" const LibDir = "/usr/share/ahenk-go/" const PluginDir = LibDir + "/plugins/" -// FIXME there isn't any difference with Stop() function -// TODO There can be a Start() function in it but start function doesnt work properly right now -func Restart(pid, signal int) { - Stop(pid, signal) -} - -// Stop Ahenkd Daemon with a specific PID (running from second copy) -// do not forget to use also os.Exit() when using in ExecStop -func Stop(pid, signal int) { - log.Println("Stop Signal Caught") - - // FILLME what you want to do before stopping daemon? - - if err := syscall.Kill(pid, syscall.Signal(signal)); err == nil { - log.Printf("Ahenk Daemon with pid number %v Successfully stopped", pid) // TODO Also log to /etc/ahenk-go/ahenkd.log - } else { - log.Fatal(err) - } -} - // Main Function that starts daemon and controls arguments func main() { - if len(os.Args) == 2 && slices.Contains([]string{"start", "stop", "restart", "nodaemon"}, os.Args[1]) { + if len(os.Args) == 2 && slices.Contains([]string{"start", "stop", "restart", "nodaemon", "tmptest"}, os.Args[1]) { switch os.Args[1] { case "start": utils.CreatePath(DataDir) @@ -67,11 +47,9 @@ func main() { case "stop": i, _ := daemon.ReadPidFile(PidFile) Stop(i, 15) - os.Exit(0) case "restart": i, _ := daemon.ReadPidFile(PidFile) Restart(i, 15) - os.Exit(0) case "nodaemon": log.Print("STARTED AS NO-DAEMON") @@ -83,15 +61,36 @@ func main() { time.Sleep(10 * time.Second) log.Print("Killed") case "tmptest": - log.Print("TEMPORARY TEST") - - time.Sleep(3 * time.Second) - log.Print("Killed") + log.Print("TEMPORARY TEST STARTED, log files are NOT redirecting!") } } else { panic("Please enter a valid option !") } PluginManager() - log.Print("Plugin Manager Started Succesfully") // NEXT Make PluginManager async ! } + +// FIXME there isn't any difference with Stop() function +// TODO There can be a Start() function in it but start function doesnt work properly right now +// Restart ahenk daemon with a specific PID (running from second copy) +func Restart(pid, signal int) { + Stop(pid, signal) +} + +// Stop ahenk daemon with a specific PID (running from second copy) +func Stop(pid, signal int) { + log.Println("Stop Signal Caught") + + // FILLME what you want to do before stopping daemon? + + if err := syscall.Kill(pid, syscall.Signal(signal)); err == nil { + log.Printf("Ahenk Daemon with pid number %v Successfully stopped", pid) + f := utils.OpenLogFile(LogFile) + defer f.Close() + log.SetOutput(f) + log.Printf("Ahenk Daemon with pid number %v Successfully stopped", pid) + } else { + log.Fatal(err) + } + os.Exit(0) +} diff --git a/cmd/ahenk-go/plugin-manager.go b/cmd/ahenk-go/plugin-manager.go index 14309f4..7aa4bdf 100644 --- a/cmd/ahenk-go/plugin-manager.go +++ b/cmd/ahenk-go/plugin-manager.go @@ -1,31 +1,32 @@ package main -import "log" +import ( + "fmt" + "time" +) -// type Greeter interface { -// Greet() -// } - -func PluginManager() { - // LoadPlugin("resources") - - // // 4. use the module - // greeter.Greet() - - // greeter.Myvar() - // // for { - // // logPlugin(greeter.AgentInfo()) - // // time.Sleep(30 * time.Second) - // // } - log.Print("plugin manager started succesfully") +// plugins/resources +type Resources interface { + AgentInfo() map[string]interface{} } -// func logPlugin(mp map[string]interface{}) { -// fmt.Printf("\nOs Info:\n") -// for i, v := range mp { -// fmt.Printf("%v: %v\n", i, v) -// } -// } +// Loads Plugins and runs them. +// When you create a new plugin create a new interface and call this plugin in this function +func PluginManager() { + var resources Resources = LoadPlugin("resources").(Resources) + for { + logPlugin("AgentInfo", resources.AgentInfo()) + time.Sleep(30 * time.Second) + } +} + +// Logs plugin outputs. +func logPlugin(title string, mp map[string]interface{}) { + fmt.Printf("\n----- %v -----\n", title) + for i, v := range mp { + fmt.Printf("%v: %v\n", i, v) + } +} // // TODO response to Lider // // func createResponse() { diff --git a/cmd/ahenk-go/plugin-opener.go b/cmd/ahenk-go/plugin-opener.go index dd5c001..ba26b37 100644 --- a/cmd/ahenk-go/plugin-opener.go +++ b/cmd/ahenk-go/plugin-opener.go @@ -1,25 +1,32 @@ package main -// // Load Plugin with plugin name and function name -// func LoadPlugin(plugName, funcName string) { -// plug, err := plugin.Open("../../plugins/resources/main.so") -// if err != nil { -// fmt.Println(err) -// os.Exit(1) -// } +import ( + "fmt" + "os" + "plugin" -// symGreeter, err := plug.Lookup("Greeter") -// if err != nil { -// fmt.Println(err) -// os.Exit(1) -// } + "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils" +) -// var greeter Greeter -// greeter, ok := symGreeter.(Greeter) -// if !ok { -// fmt.Println("unexpected type from module symbol") -// os.Exit(1) -// } -// } +// Load Plugin placed in PluginDir, returns empty interface. +// 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) interface{} { -// // NEXT move plugin-manager.go main here ! + // TODO if error caugth try without relative path, this will be good for local testing + plug, err := plugin.Open(PluginDir + plugName + ".so") + utils.Check(err) + + // TODO also allow lookup another symbol other than PlugnameConnect + symGreeter, err := plug.Lookup(utils.FirstUpperEN(plugName) + "Connect") + utils.Check(err) + + var plugOut interface{} + plugOut, ok := symGreeter.(interface{}) + if !ok { + fmt.Println("unexpected type from module symbol") + os.Exit(1) + } + return plugOut +} diff --git a/go.mod b/go.mod index b004aba..ce2ce06 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 + golang.org/x/text v0.12.0 // indirect ) require ( diff --git a/go.sum b/go.sum index 5959980..fa11463 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,8 @@ golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/sys v0.10.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/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/osinfo/main.go b/pkg/osinfo/main.go index 3848ac3..292657d 100644 --- a/pkg/osinfo/main.go +++ b/pkg/osinfo/main.go @@ -5,6 +5,7 @@ import ( "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils" "github.com/shirou/gopsutil/disk" + "github.com/shirou/gopsutil/mem" ) const KB = uint64(1024) @@ -35,8 +36,10 @@ func GetDisks() []disk.PartitionStat { return parts } -// return disk usage as MiB -// TODO different function for all disks / a specified disk? +// return disk usage as GiB +// +// TODO different functions for all disks / a specified disk? +// FIXME Wrong Disk values for docker !!! (probably because counting also virtual mountpoints?) func GetDiskUsage() map[string]float64 { var totalSize, freeSize, usedSize uint64 for _, part := range GetDisks() { @@ -52,3 +55,17 @@ func GetDiskUsage() map[string]float64 { "used": utils.Byte2GiB(usedSize), } } + +// return memory usage as GiB +// +// TODO also implement swap usage +func GetMemoryUsage() map[string]float64 { + v, _ := mem.VirtualMemory() + return map[string]float64{ + "total": utils.Byte2GiB(v.Total), + "free": utils.Byte2GiB(v.Free), + "used": utils.Byte2GiB(v.Used), + "avaliable": utils.Byte2GiB(v.Available), + "cached": utils.Byte2GiB(v.Cached), + } +} diff --git a/pkg/utils/main.go b/pkg/utils/main.go index b39561f..70cb632 100644 --- a/pkg/utils/main.go +++ b/pkg/utils/main.go @@ -3,6 +3,9 @@ package utils import ( "log" "os" + + "golang.org/x/text/cases" + "golang.org/x/text/language" ) func Byte2String(arr []int8) string { @@ -55,3 +58,8 @@ func OpenLogFile(path string) *os.File { } return f } + +// Makes first character uppercase of given English string +func FirstUpperEN(str string) string { + return cases.Title(language.English).String(str) +} // TODO cases.NoLower vs cases.Compact ! diff --git a/plugins/resources/main.go b/plugins/resources/main.go index ec9aadd..84bc17d 100644 --- a/plugins/resources/main.go +++ b/plugins/resources/main.go @@ -1,13 +1,15 @@ package main import ( - "fmt" "runtime" "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/osinfo" ) -type greeting string +type plug string + +// exported plugin Symbol +var ResourcesConnect plug // return instant resource usage information func ResourceUsage() map[string]string { @@ -31,28 +33,33 @@ func ResourceUsage() map[string]string { } // return general Agent information (that changes rarely) -func (g greeting) AgentInfo() map[string]interface{} { +func (p plug) AgentInfo() map[string]interface{} { data := map[string]interface{}{ - "System": runtime.GOOS, "Release": osinfo.GetKernelInfo()["Release"], + "System": runtime.GOOS, "Kernel": osinfo.GetKernelInfo()["Release"], + "hostname": osinfo.GetKernelInfo()["Hostname"], + "osMachine": osinfo.GetKernelInfo()["Machine"], + "diskTotal": osinfo.GetDiskUsage()["total"], + "diskUsed": osinfo.GetDiskUsage()["used"], + "diskFree": osinfo.GetDiskUsage()["free"], + "diskUsage": osinfo.GetDiskUsage()["used"] / osinfo.GetDiskUsage()["total"], + "memoryTotal": osinfo.GetMemoryUsage()["total"], + "memoryFree": osinfo.GetMemoryUsage()["free"], + "memoryAvaliable": osinfo.GetMemoryUsage()["avaliable"], + "memoryUsed": osinfo.GetMemoryUsage()["used"], + "memoryCached": osinfo.GetMemoryUsage()["cached"], + "memoryUsage": (osinfo.GetMemoryUsage()["used"] + osinfo.GetMemoryUsage()["cached"]) / osinfo.GetMemoryUsage()["total"], //REVIEW just used/total ? + + // TODO is calling all functions one by one slow downs code? + // TODO 'agentVersion': self.get_agent_version(), - "hostname": osinfo.GetKernelInfo()["Hostname"], // TODO 'ipAddresses': str(self.Hardware.Network.ip_addresses()).replace('[', '').replace(']', ''), - // "osName": osinfo.GetKernelInfo()["Sysname"], // TODO is it necessary? - // "osNodeName": osinfo.GetKernelInfo()["NodeName"],// TODO is it necessary? - // "osVersion": osinfo.GetKernelInfo()["Version"], // TODO is it necessary? - "osMachine": osinfo.GetKernelInfo()["Machine"], - // TODO get distrubition name also (pardus,arch,debian,windows10 for example ...) + // TODO get distrubition name also (pardus,arch,debian,windows10 for example ...) // TODO 'macAddresses': str(self.Hardware.Network.mac_addresses()).replace('[', '').replace(']', ''), // TODO 'hardware.systemDefinitions': self.Hardware.system_definitions(), // TODO 'hardware.monitors': self.Hardware.monitors(), // TODO 'hardware.screens': self.Hardware.screens(), // TODO 'hardware.usbDevices': self.Hardware.usb_devices(), // TODO 'hardware.printers': self.Hardware.printers(), - "diskTotal": osinfo.GetDiskUsage()["total"], - "diskUsed": osinfo.GetDiskUsage()["used"], - "diskFree": osinfo.GetDiskUsage()["free"], - // TODO "diskUsage": osinfo.GetDiskUsage()["Usage"], - // TODO 'memory': self.Hardware.Memory.total(), // TODO 'Device': device, } return data @@ -61,23 +68,9 @@ func (g greeting) AgentInfo() map[string]interface{} { func Info() map[string]string { inf := make(map[string]string) inf["name"] = "resources" - inf["version"] = "0.0.1" + inf["version"] = "0.0.2" inf["support"] = "debian" inf["description"] = "Resource Usage Information and Controls" inf["developer"] = "asandikci@aliberksandikci.com.tr" - - // inf["task"] = "true" - // inf["user_oriented"] = "false" - // inf["machine_oriented"] = "false" return inf } - -func (g greeting) Greet() { - fmt.Println("Hello Universe") -} -func (g greeting) Myvar() { - fmt.Println("I am here") -} - -// this is exported -var Greeter greeting -- 2.30.2 From 96169913ddcbed8fad1fb5fe2bc2e25c93094fad Mon Sep 17 00:00:00 2001 From: asandikci Date: Thu, 24 Aug 2023 17:58:47 +0300 Subject: [PATCH 12/14] chore: move old scripts to scripts branch --- scripts/docker-build.sh | 92 ---------------------------------------- scripts/install.sh | 73 ------------------------------- scripts/local-build.sh | 3 -- scripts/local-install.sh | 67 ----------------------------- 4 files changed, 235 deletions(-) delete mode 100644 scripts/docker-build.sh delete mode 100644 scripts/install.sh delete mode 100644 scripts/local-build.sh delete mode 100644 scripts/local-install.sh diff --git a/scripts/docker-build.sh b/scripts/docker-build.sh deleted file mode 100644 index 3dc7bf0..0000000 --- a/scripts/docker-build.sh +++ /dev/null @@ -1,92 +0,0 @@ -#!/bin/bash - -# see https://people.debian.org/~stapelberg/2015/07/27/dh-make-golang.html - -GIT_HOST="git.aliberksandikci.com.tr" -GIT_ORG="liderahenk" -GIT_REPO="ahenk-go" -FULL_URL="https://$GIT_HOST/$GIT_ORG/$GIT_REPO" -DEV_NAME="Aliberk Sandıkçı" -GIT_MAIL="git@aliberksandikci.com.tr" - -UPLOAD_URL="127.0.0.1" -UPLOAD_PORT="8000" - -echo "Editing git configs..." -git config --global init.defaultBranch main -git config --global user.name "$DEV_NAME" -git config --global user.email $GIT_MAIL -git config --global color.status auto -git config --global color.branch auto -git config --global color.interactive auto -git config --global color.diff auto - -mkdir /build -cd /build || exit - -if [[ "$1" != "update" ]]; then - - dh-make-golang make -allow_unknown_hoster "$GIT_HOST/$GIT_ORG/$GIT_REPO" - - if [[ ! ("$1" != "notodo" || "$2" != "notodo") ]]; then - - # TODO Add option to disable writing all these files all the time (-s --skip) - nano "itp-$GIT_REPO.txt" - sendmail -t <"itp-$GIT_REPO.txt" - - cd $GIT_REPO || exit - grep --color=always -r TODO debian - echo -e "\nThese files needs review. Starting reviewing automatically in 10sec...\n" - sleep 10 - # TODO Allow users to escape from automatic review and manually do it while in this script - - while [[ $(grep -r TODO debian | wc --lines) -ne 0 ]]; do - nano "$(grep -r TODO debian | awk '{sub(/:.*/,"")} NR==1')" - - echo "Continuing from next file..." - sleep 1 - grep --color=always -r TODO debian - sleep 5 - - # TODO Allow user to reviewing remaining files, choosing between them, optional skipping, and waiting for an answer (continue? [Y/n]) - done - echo "ALL FILES DONE" - - dh_installsystemd - echo """ -#!/usr/bin/make -f - -%: - dh \$@ --builddirectory=_build --buildsystem=golang --with=golang --with-systemd -override_dh_auto_install: - dh_auto_install -- --no-source -""" >debian/rules - sleep 5 - # FIXME Not working! - - else - cd $GIT_REPO || exit - fi -else - gbp clone "$FULL_URL" - cd $GIT_REPO || exit - git checkout debian/sid - pwd - if [[ "$1" == "nmu" ]]; then - dch --nmu - else - dch - fi - echo "Succesfully Updated deb package information" -fi - -# TODO ask user to continue - -git add debian && git commit -a -m 'Initial packaging' - -gbp buildpackage - -cd .. -lintian -- *.changes - -echo -e "SOLVE LINTIAN ERRORS / WARNINGS\nAFTER THAT, PUSH REPO" diff --git a/scripts/install.sh b/scripts/install.sh deleted file mode 100644 index 9bd5670..0000000 --- a/scripts/install.sh +++ /dev/null @@ -1,73 +0,0 @@ -#!/bin/bash - -### Ahenk Agent Local Installation Script -### IMPORTANT NOTICE ! -# Consider to use makefile for building or .deb file for installing package, -# using this scripts is not suggested anymore ! - -### --------------------- ### - -### --> Prerequisites -# git -# go 1.20+ -# systemd - -# wget -qO- https://git.aliberksandikci.com.tr/liderahenk/ahenk-go/raw/branch/main/scripts/install.sh | bash - -### --> Variable Set -REPO_NAME="ahenk-go" -REPO_LINK="https://git.aliberksandikci.com.tr/liderahenk/${REPO_NAME}" -DAEMON_NAME="ahenk-go" -CONF_DIR="/etc/ahenk-go/" -TEMP_DIR="$(mktemp -d)" -MAIN_DIR="${TEMP_DIR}/${REPO_NAME}/" -echo -e "VARIABLES SET\n" - - -### --> Pre-Cleaning -sudo rm /usr/bin/${DAEMON_NAME} -sudo killall "$DAEMON_NAME" -sudo systemctl disable "$DAEMON_NAME" -sudo systemctl stop "$DAEMON_NAME" -sudo systemctl daemon-reload -sudo rm -rf "$CONF_DIR" -sudo mkdir -p "$CONF_DIR" -echo -e "PRE-CLENING DONE\n" - - -### --> Building -cd "$TEMP_DIR" || exit -echo "$TEMP_DIR" -git clone "$REPO_LINK" -cd "$MAIN_DIR" || exit - -# cmd/ahenkd-go -cd cmd/ahenkd-go || exit -sudo go build -o "/usr/bin/ahenkd-go" - -echo -e "BUILDING DONE\n" - -### --> Configurations -sudo touch "$CONF_DIR/user.info" -sudo cat << EOF | sudo tee -a "$CONF_DIR/user.info" > /dev/null -The current working directory is: $PWD -You are logged in as $(whoami) -$HOME $USER $SUDO_USER -EOF -sudo chown root:root "$CONF_DIR/user.info" -sudo chmod 640 "$CONF_DIR/user.info" -echo -e "CONFIGURATIONS DONE\n" - -### --> Installing -if [[ $(ps -p 1 | awk 'FNR == 2 {print $4} ') == "systemd" ]] -then - sudo mv "${MAIN_DIR}build/package/deb/${DAEMON_NAME}.service" /etc/systemd/system/ -fi -sudo systemctl daemon-reload -sudo systemctl enable "$DAEMON_NAME" -sudo systemctl start "$DAEMON_NAME" -echo -e "INSTALLING DONE\n" - -### --> Post-Cleaning -sudo rm -rf "$TEMP_DIR" -echo -e "POST-CLEANING DONE\n" \ No newline at end of file diff --git a/scripts/local-build.sh b/scripts/local-build.sh deleted file mode 100644 index 9c86f61..0000000 --- a/scripts/local-build.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -sudo go build -o "build/package/bin/ahenkd-go" cmd/ahenkd-go/ahenkd-go.go \ No newline at end of file diff --git a/scripts/local-install.sh b/scripts/local-install.sh deleted file mode 100644 index 17cd6e5..0000000 --- a/scripts/local-install.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/bash - -### Ahenk Agent Local Installation Script - -### --> Prerequisites -# go 1.20+ -# systemd - -# Change directory to main repo (cd ahenk-go) -# bash scripts/local-install.sh - -### --> Variable Set -REPO_NAME="ahenk-go" -DAEMON_NAME="ahenk-go" -CONF_DIR="/etc/ahenk-go/" -TEMP_DIR="$(mktemp -d)" -MAIN_DIR="${TEMP_DIR}/${REPO_NAME}/" -echo -e "VARIABLES SET\n" - - -### --> Pre-Cleaning -sudo rm /usr/bin/${DAEMON_NAME} -sudo killall "$DAEMON_NAME" -sudo systemctl disable "$DAEMON_NAME" -sudo systemctl stop "$DAEMON_NAME" -sudo systemctl daemon-reload -sudo rm -rf "$CONF_DIR" -sudo mkdir -p "$CONF_DIR" -echo -e "PRE-CLENING DONE\n" - - -### --> Building -cp -r ../ "$TEMP_DIR" -cd "$TEMP_DIR" || exit -echo "$TEMP_DIR" -cd "$MAIN_DIR" || exit - -# cmd/ahenkd-go -cd cmd/ahenkd-go || exit -sudo go build -o "/usr/bin/ahenkd-go" - -echo -e "BUILDING DONE\n" - -### --> Configurations -sudo touch "$CONF_DIR/user.info" -sudo cat << EOF | sudo tee -a "$CONF_DIR/user.info" > /dev/null -The current working directory is: $PWD -You are logged in as $(whoami) -$HOME $USER $SUDO_USER -EOF -sudo chown root:root "$CONF_DIR/user.info" -sudo chmod 640 "$CONF_DIR/user.info" -echo -e "CONFIGURATIONS DONE\n" - -### --> Installing -if [[ $(ps -p 1 | awk 'FNR == 2 {print $4} ') == "systemd" ]] -then - sudo mv "${MAIN_DIR}debian/${DAEMON_NAME}.service" /etc/systemd/system/ -fi -sudo systemctl daemon-reload -sudo systemctl enable "$DAEMON_NAME" -sudo systemctl start "$DAEMON_NAME" -echo -e "INSTALLING DONE\n" - -### --> Post-Cleaning -sudo rm -rf "$TEMP_DIR" -echo -e "POST-CLEANING DONE\n" \ No newline at end of file -- 2.30.2 From ac2374ca9215fe52be062ab12fe37caf645ba39a Mon Sep 17 00:00:00 2001 From: asandikci Date: Thu, 24 Aug 2023 19:10:48 +0300 Subject: [PATCH 13/14] update readme --- Dockerfile | 2 +- README.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 946ed76..7d19ff6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ RUN apt-get update RUN apt-get upgrade -y ### INSTALL USEFUL UTILITIES ### -RUN apt-get install curl wget tree nano procps lintian -y +RUN apt-get install git curl wget tree nano procps lintian -y ### INSTALL BUILD DEPENDENCIES ### RUN apt-get install sudo dpkg-dev debhelper golang-any -y diff --git a/README.md b/README.md index fae5ea1..6ee79c2 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,55 @@ # Ahenk Go -Ahenk Agent for Liderahenk Central Management System - Go Implementation +**Liderahenk** is an open source project which provides solutions to manage, monitor and audit unlimited number of different systems and users on a network. -Tamamdır hallettim sorunu. dpkg-buildpackage, makefile'ı çağırıyormuş. Anlayana kadar canım çıktı 😅 ---- +Ahenk-go is a Linux agent written in Go which enables Lider to manage & monitor clients remotely. + +### Creating Docker Development Environment (Optional) +1. Install and Create docker environment, [Quick Start](https://sulincix.github.io/sayfalar/html/docker-kullanimi.html) +2. Pull docker image +```sh +docker pull asandikci/godeb +``` +> Or alternatively build Dockerfile with `docker build -t godeb:latest` code for lower data usage + +3. Create container from image and attach to container, an example: +```sh +docker run -it -d --name build1 asandikci/godeb:latest "bin/bash" +docker attach build1 --detach-keys "ctrl-k" +``` +## Packaging +1. Install necessary packages (You dont need this step if you are using docker file) +```sh +sudo apt install sudo dpkg-dev debhelper golang-any +``` +2. Clone the repository and move to main directory +```sh +git clone https://git.aliberksandikci.com.tr/Liderahenk/ahenk-go/ +cd ahenk-go/ +``` +3. Build program and create binary/source packages +```sh +dpkg-buildpackage +``` +> Refer to Makefile for more info + +### Documentation +- See [Liderahenk/ahenk-docs](https://git.aliberksandikci.com.tr/Liderahenk/ahenk-docs/) for comprehensively prepared documentation. -### Useful Links -| Explanation | Link | -| ----------- | ---- | -| Ahenk-go documentation | [Liderahenk/ahenk-docs](https://git.aliberksandikci.com.tr/Liderahenk/ahenk-docs/) | -| Current Python Implementation of Ahenk | [Pardus-LiderAhenk/ahenk](https://github.com/Pardus-LiderAhenk/ahenk/) | +## Other Liderahenk Projects +- [Pardus-LiderAhenk/ahenk](https://github.com/Pardus-LiderAhenk/ahenk/): Current Python Implementation of Ahenk +- [Lider UI](https://github.com/Pardus-LiderAhenk/liderui): Lider Administration User Interface +- [Lider API](https://github.com/Pardus-LiderAhenk/liderapi): Lider API +- [Liderahenk-ansible](https://github.com/Pardus-LiderAhenk/liderahenk-ansible): Liderahenk setup with ansible +- [Ahenkdesk](https://github.com/Pardus-LiderAhenk/ahenkdesk): Ahenk User Interface +- See more in our [GitHub Page](https://github.com/Pardus-LiderAhenk) +## Changelog +See [changelog](./debian/changelog) to learn what have been changed between releases or refer to [Releases](./releases) Tab above + + +## License +Lider Ahenk and its sub projects are licensed under the [LGPL v3](./LICENSE). + -- 2.30.2 From a7df1fa687ec92eba66eb160909f85952ea79499 Mon Sep 17 00:00:00 2001 From: asandikci Date: Thu, 24 Aug 2023 21:06:05 +0300 Subject: [PATCH 14/14] push latest changes --- README.md | 26 ++++++------------- cmd/ahenk-go/main.go | 5 ---- cmd/ahenk-go/plugin-manager.go | 5 ++++ cmd/ahenk-go/plugin-opener.go | 4 +-- .../{ahenkd-go.service => ahenk-go.service} | 0 5 files changed, 15 insertions(+), 25 deletions(-) rename debian/{ahenkd-go.service => ahenk-go.service} (100%) diff --git a/README.md b/README.md index 6ee79c2..e18b188 100644 --- a/README.md +++ b/README.md @@ -4,19 +4,6 @@ Ahenk-go is a Linux agent written in Go which enables Lider to manage & monitor clients remotely. -### Creating Docker Development Environment (Optional) -1. Install and Create docker environment, [Quick Start](https://sulincix.github.io/sayfalar/html/docker-kullanimi.html) -2. Pull docker image -```sh -docker pull asandikci/godeb -``` -> Or alternatively build Dockerfile with `docker build -t godeb:latest` code for lower data usage - -3. Create container from image and attach to container, an example: -```sh -docker run -it -d --name build1 asandikci/godeb:latest "bin/bash" -docker attach build1 --detach-keys "ctrl-k" -``` ## Packaging 1. Install necessary packages (You dont need this step if you are using docker file) ```sh @@ -34,8 +21,9 @@ dpkg-buildpackage > Refer to Makefile for more info ### Documentation -- See [Liderahenk/ahenk-docs](https://git.aliberksandikci.com.tr/Liderahenk/ahenk-docs/) for comprehensively prepared documentation. - +- See comprehensively prepared [documentation](https://git.aliberksandikci.com.tr/Liderahenk/ahenk-docs/) +- See how to [setup development environment](https://git.aliberksandikci.com.tr/Liderahenk/ahenk-docs/src/branch/main/dev/environment.md) + ## Other Liderahenk Projects - [Pardus-LiderAhenk/ahenk](https://github.com/Pardus-LiderAhenk/ahenk/): Current Python Implementation of Ahenk @@ -47,9 +35,11 @@ dpkg-buildpackage ## Changelog -See [changelog](./debian/changelog) to learn what have been changed between releases or refer to [Releases](./releases) Tab above - +- [ahenk-docs/admin/changelog](https://git.aliberksandikci.com.tr/Liderahenk/ahenk-docs/src/branch/main/admin/changelog.md): changes between releases with easy-to-understand annotations +- [ahenk-docs/dev/changelog](https://git.aliberksandikci.com.tr/Liderahenk/ahenk-docs/src/branch/main/dev/changelog.md): detailed development changelog, includes all issues, feature implementations, breaking changes, code comments, todo etc. - **long** +- [debian/changelog](./debian/changelog): changes between debian releases + ## License -Lider Ahenk and its sub projects are licensed under the [LGPL v3](./LICENSE). +Liderahenk and its sub projects are licensed under the [LGPL v3](./LICENSE). diff --git a/cmd/ahenk-go/main.go b/cmd/ahenk-go/main.go index 72afee4..ac957e9 100644 --- a/cmd/ahenk-go/main.go +++ b/cmd/ahenk-go/main.go @@ -4,7 +4,6 @@ import ( "log" "os" "syscall" - "time" "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils" @@ -56,10 +55,6 @@ func main() { f := utils.OpenLogFile(LogFile) defer f.Close() log.SetOutput(f) - log.Printf("Log test") - - time.Sleep(10 * time.Second) - log.Print("Killed") case "tmptest": log.Print("TEMPORARY TEST STARTED, log files are NOT redirecting!") } diff --git a/cmd/ahenk-go/plugin-manager.go b/cmd/ahenk-go/plugin-manager.go index 7aa4bdf..26e85c3 100644 --- a/cmd/ahenk-go/plugin-manager.go +++ b/cmd/ahenk-go/plugin-manager.go @@ -10,10 +10,15 @@ type Resources interface { AgentInfo() map[string]interface{} } +// type NewPluginInterface interface { +// PluginMethod() returnType +// } + // Loads Plugins and runs them. // When you create a new plugin create a new interface and call this plugin in this function func PluginManager() { var resources Resources = LoadPlugin("resources").(Resources) + // var pluginName NewPluginInterface = LoadPlugin("pluginName").(NewPluginInterface) for { logPlugin("AgentInfo", resources.AgentInfo()) time.Sleep(30 * time.Second) diff --git a/cmd/ahenk-go/plugin-opener.go b/cmd/ahenk-go/plugin-opener.go index ba26b37..123422f 100644 --- a/cmd/ahenk-go/plugin-opener.go +++ b/cmd/ahenk-go/plugin-opener.go @@ -19,11 +19,11 @@ func LoadPlugin(plugName string) interface{} { utils.Check(err) // TODO also allow lookup another symbol other than PlugnameConnect - symGreeter, err := plug.Lookup(utils.FirstUpperEN(plugName) + "Connect") + symPlug, err := plug.Lookup(utils.FirstUpperEN(plugName) + "Connect") utils.Check(err) var plugOut interface{} - plugOut, ok := symGreeter.(interface{}) + plugOut, ok := symPlug.(interface{}) if !ok { fmt.Println("unexpected type from module symbol") os.Exit(1) diff --git a/debian/ahenkd-go.service b/debian/ahenk-go.service similarity index 100% rename from debian/ahenkd-go.service rename to debian/ahenk-go.service -- 2.30.2