From 36c1e81609dc9886c93368ffada1f6c3e78f67d2 Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Mon, 7 Mar 2022 12:21:01 +0100 Subject: [PATCH 1/5] Rename main source to fit binary name --- metric-store.go => cc-metric-store.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename metric-store.go => cc-metric-store.go (100%) diff --git a/metric-store.go b/cc-metric-store.go similarity index 100% rename from metric-store.go rename to cc-metric-store.go From d546b0e7d158e804b13010beecf79ea5eab58d3d Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Mon, 7 Mar 2022 12:21:20 +0100 Subject: [PATCH 2/5] Move systemd service file to scripts subfolder --- cc-metric-store.service => scripts/cc-metric-store.service | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename cc-metric-store.service => scripts/cc-metric-store.service (100%) diff --git a/cc-metric-store.service b/scripts/cc-metric-store.service similarity index 100% rename from cc-metric-store.service rename to scripts/cc-metric-store.service From 7a5a611d8e51e7698b595d90172dd9cd7bdbde16 Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Mon, 7 Mar 2022 12:21:59 +0100 Subject: [PATCH 3/5] Add additional files for systemd and init system --- scripts/cc-metric-store.config | 17 ++++ scripts/cc-metric-store.init | 141 +++++++++++++++++++++++++++++++ scripts/cc-metric-store.sysusers | 2 + 3 files changed, 160 insertions(+) create mode 100644 scripts/cc-metric-store.config create mode 100644 scripts/cc-metric-store.init create mode 100644 scripts/cc-metric-store.sysusers diff --git a/scripts/cc-metric-store.config b/scripts/cc-metric-store.config new file mode 100644 index 0000000..feb0740 --- /dev/null +++ b/scripts/cc-metric-store.config @@ -0,0 +1,17 @@ +CC_USER=clustercockpit + +CC_GROUP=clustercockpit + +CC_HOME=/tmp + +LOG_DIR=/var/log + +DATA_DIR=/var/run/cc-metric-store + +MAX_OPEN_FILES=10000 + +CONF_DIR=/etc/cc-metric-store + +CONF_FILE=/etc/cc-metric-store/cc-metric-store.json + +RESTART_ON_UPGRADE=true diff --git a/scripts/cc-metric-store.init b/scripts/cc-metric-store.init new file mode 100644 index 0000000..80cab6f --- /dev/null +++ b/scripts/cc-metric-store.init @@ -0,0 +1,141 @@ +#! /usr/bin/env bash + +# chkconfig: 2345 80 05 +# description: ClusterCockpit metric store +# processname: cc-metric-store +# config: /etc/default/cc-metric-store +# pidfile: /var/run/cc-metric-store.pid + +### BEGIN INIT INFO +# Provides: cc-metric-store +# Required-Start: $all +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Start ClusterCockpit metric store at boot time +### END INIT INFO + + +PATH=/bin:/usr/bin:/sbin:/usr/sbin +NAME=cc-metric-store +DESC="ClusterCockpit metric store" +DEFAULT=/etc/default/${NAME}.json + +CC_USER=clustercockpit +CC_GROUP=clustercockpit +CONF_DIR=/etc/cc-metric-store +PID_FILE=/var/run/$NAME.pid +DAEMON=/usr/sbin/$NAME +CONF_FILE=${CONF_DIR}/cc-metric-store.json + +umask 0027 + +if [ ! -x $DAEMON ]; then + echo "Program not installed or not executable" + exit 5 +fi + +. /lib/lsb/init-functions + +if [ -r /etc/default/rcS ]; then + . /etc/default/rcS +fi + +# overwrite settings from default file +if [ -f "$DEFAULT" ]; then + . "$DEFAULT" +fi + +CC_OPTS="--config=${CONF_FILE}" + +function checkUser() { + if [ `id -u` -ne 0 ]; then + echo "You need root privileges to run this script" + exit 4 + fi +} + +case "$1" in + start) + checkUser + log_daemon_msg "Starting $DESC" + + pid=`pidofproc -p $PID_FILE $NAME` + if [ -n "$pid" ] ; then + log_begin_msg "Already running." + log_end_msg 0 + exit 0 + fi + + # Prepare environment + touch "$PID_FILE" && chown "$CC_USER":"$CC_GROUP" "$PID_FILE" + + if [ -n "$MAX_OPEN_FILES" ]; then + ulimit -n $MAX_OPEN_FILES + fi + + # Start Daemon + start-stop-daemon --start -b --chdir "$WORK_DIR" --user "$CC_USER" -c "$CC_USER" --pidfile "$PID_FILE" --exec $DAEMON -- $DAEMON_OPTS + return=$? + if [ $return -eq 0 ] + then + sleep 1 + + # check if pid file has been written to + if ! [[ -s $PID_FILE ]]; then + log_end_msg 1 + exit 1 + fi + + i=0 + timeout=10 + # Wait for the process to be properly started before exiting + until { cat "$PID_FILE" | xargs kill -0; } >/dev/null 2>&1 + do + sleep 1 + i=$(($i + 1)) + if [ $i -gt $timeout ]; then + log_end_msg 1 + exit 1 + fi + done + fi + log_end_msg $return + ;; + stop) + checkUser + log_daemon_msg "Stopping $DESC" + + if [ -f "$PID_FILE" ]; then + start-stop-daemon --stop --pidfile "$PID_FILE" \ + --user "$CC_USER" \ + --retry=TERM/20/KILL/5 >/dev/null + if [ $? -eq 1 ]; then + log_progress_msg "$DESC is not running but pid file exists, cleaning up" + elif [ $? -eq 3 ]; then + PID="`cat $PID_FILE`" + log_failure_msg "Failed to stop $DESC (pid $PID)" + exit 1 + fi + rm -f "$PID_FILE" + else + log_progress_msg "(not running)" + fi + log_end_msg 0 + ;; + status) + status_of_proc -p $PID_FILE $NAME $NAME && exit 0 || exit $? + ;; + restart|force-reload) + if [ -f "$PID_FILE" ]; then + $0 stop + sleep 1 + fi + $0 start + ;; + *) + log_success_msg "Usage: $0 {start|stop|restart|force-reload|status}" + exit 3 + ;; +esac + diff --git a/scripts/cc-metric-store.sysusers b/scripts/cc-metric-store.sysusers new file mode 100644 index 0000000..6ce3700 --- /dev/null +++ b/scripts/cc-metric-store.sysusers @@ -0,0 +1,2 @@ +#Type Name ID GECOS Home directory Shell +u clustercockpit - "User for ClusterCockpit" /run/cc-metric-collector /sbin/nologin From 3ec4dd7f693be15cbef9847a3b5dd66472f8a889 Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Mon, 7 Mar 2022 12:22:14 +0100 Subject: [PATCH 4/5] Add spec file for RPM building --- scripts/cc-metric-store.spec | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 scripts/cc-metric-store.spec diff --git a/scripts/cc-metric-store.spec b/scripts/cc-metric-store.spec new file mode 100644 index 0000000..6f07955 --- /dev/null +++ b/scripts/cc-metric-store.spec @@ -0,0 +1,62 @@ +Name: cc-metric-store +Version: %{VERS} +Release: 1%{?dist} +Summary: In-memory metric database from the ClusterCockpit suite + +License: MIT +Source0: %{name}-%{version}.tar.gz + +BuildRequires: go-toolset +BuildRequires: systemd-rpm-macros + +Provides: %{name} = %{version} + +%description +In-memory metric database from the ClusterCockpit suite + +%global debug_package %{nil} + +%prep +%autosetup + + +%build +make + + +%install +# Install cc-metric-store +make PREFIX=%{buildroot} BINDIR=%%{_sbindir} install +# Integrate into system +install -Dpm 0644 scripts/%{name}.service %{buildroot}%{_unitdir}/%{name}.service +install -Dpm 0600 scripts/%{name}.config %{buildroot}%{_sysconfdir}/default/%{name} +install -Dpm 0644 scripts/%{name}.sysusers %{buildroot}%{_sysusersdir}/%{name}.conf + + +%check +# go test should be here... :) + +%pre +%sysusers_create_package scripts/%{name}.sysusers + +%post +%systemd_post %{name}.service + +%preun +%systemd_preun %{name}.service + +%files +# Binary +%attr(-,clustercockpit,clustercockpit) %{_sbindir}/%{name} +# Config +%dir %{_sysconfdir}/%{name} +%attr(0600,clustercockpit,clustercockpit) %config(noreplace) %{_sysconfdir}/%{name}/%{name}.json +# Systemd +%{_unitdir}/%{name}.service +%{_sysconfdir}/default/%{name} +%{_sysusersdir}/%{name}.conf + +%changelog +* Mon Mar 07 2022 Thomas Gruber - 0.1 +- Initial metric store implementation + From bbf49e1b21afe0277790f8ec84cda148c3f41119 Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Mon, 7 Mar 2022 12:22:22 +0100 Subject: [PATCH 5/5] Add Makefile --- Makefile | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e38bd2a --- /dev/null +++ b/Makefile @@ -0,0 +1,81 @@ + +APP = cc-metric-store +GOSRC_APP := cc-metric-store.go +GOSRC_FILES := api.go \ + memstore.go \ + archive.go \ + debug.go \ + float.go \ + lineprotocol.go \ + selector.go \ + stats.go + + + +BINDIR ?= bin + + +.PHONY: all +all: $(APP) + +$(APP): $(GOSRC) + go get + go build -o $(APP) $(GOSRC_APP) $(GOSRC_FILES) + +install: $(APP) + @WORKSPACE=$(PREFIX) + @if [ -z "$${WORKSPACE}" ]; then exit 1; fi + @mkdir --parents --verbose $${WORKSPACE}/usr/$(BINDIR) + @install -Dpm 755 $(APP) $${WORKSPACE}/usr/$(BINDIR)/$(APP) + @install -Dpm 600 config.json $${WORKSPACE}/$(APP)/$(APP).conf + +.PHONY: clean +.ONESHELL: +clean: + rm -f $(APP) + +.PHONY: fmt +fmt: + go fmt $(GOSRC_APP) + +# Examine Go source code and reports suspicious constructs +.PHONY: vet +vet: + go vet ./... + +# Run linter for the Go programming language. +# Using static analysis, it finds bugs and performance issues, offers simplifications, and enforces style rules +.PHONY: staticcheck +staticcheck: + go install honnef.co/go/tools/cmd/staticcheck@latest + $$(go env GOPATH)/bin/staticcheck ./... + +.ONESHELL: +.PHONY: RPM +RPM: scripts/cc-metric-collector.spec + @WORKSPACE="$${PWD}" + @SPECFILE="$${WORKSPACE}/scripts/cc-metric-store.spec" + # Setup RPM build tree + @eval $$(rpm --eval "ARCH='%{_arch}' RPMDIR='%{_rpmdir}' SOURCEDIR='%{_sourcedir}' SPECDIR='%{_specdir}' SRPMDIR='%{_srcrpmdir}' BUILDDIR='%{_builddir}'") + @mkdir --parents --verbose "$${RPMDIR}" "$${SOURCEDIR}" "$${SPECDIR}" "$${SRPMDIR}" "$${BUILDDIR}" + # Create source tarball + @COMMITISH="HEAD" + @VERS=$$(git describe --tags $${COMMITISH}) + @VERS=$${VERS#v} + @VERS=$$(echo $$VERS | sed -e s+'-'+'_'+g) + @eval $$(rpmspec --query --queryformat "NAME='%{name}' VERSION='%{version}' RELEASE='%{release}' NVR='%{NVR}' NVRA='%{NVRA}'" --define="VERS $${VERS}" "$${SPECFILE}") + @PREFIX="$${NAME}-$${VERSION}" + @FORMAT="tar.gz" + @SRCFILE="$${SOURCEDIR}/$${PREFIX}.$${FORMAT}" + @git archive --verbose --format "$${FORMAT}" --prefix="$${PREFIX}/" --output="$${SRCFILE}" $${COMMITISH} + # Build RPM and SRPM + @rpmbuild -ba --define="VERS $${VERS}" --rmsource --clean "$${SPECFILE}" + # Report RPMs and SRPMs when in GitHub Workflow + @if [[ "$${GITHUB_ACTIONS}" == true ]]; then + @ RPMFILE="$${RPMDIR}/$${ARCH}/$${NVRA}.rpm" + @ SRPMFILE="$${SRPMDIR}/$${NVR}.src.rpm" + @ echo "RPM: $${RPMFILE}" + @ echo "SRPM: $${SRPMFILE}" + @ echo "::set-output name=SRPM::$${SRPMFILE}" + @ echo "::set-output name=RPM::$${RPMFILE}" + @fi