From f84419de0d99acfd430beb4377ba1f942ce3fa16 Mon Sep 17 00:00:00 2001 From: Melora Hugues Date: Tue, 3 Dec 2024 14:11:19 +0100 Subject: [PATCH] Add tools to build debian package --- Makefile | 25 ++++++++++++++++--- debian/sys-exporter/control | 5 ++++ .../sys-exporter/etc/sys-exporter/config.yml | 7 ++++++ .../etc/systemd/system/sys-exporter.service | 16 ++++++++++++ debian/sys-exporter/postinst | 4 +++ debian/tools/Dockerfile | 16 ++++++++++++ debian/tools/build.sh | 17 +++++++++++++ 7 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 debian/sys-exporter/control create mode 100644 debian/sys-exporter/etc/sys-exporter/config.yml create mode 100644 debian/sys-exporter/etc/systemd/system/sys-exporter.service create mode 100644 debian/sys-exporter/postinst create mode 100644 debian/tools/Dockerfile create mode 100755 debian/tools/build.sh diff --git a/Makefile b/Makefile index 0bfc66a..24e6b71 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,34 @@ .PHONY: all client server +BUILDDIR?=./build +VERSION?=$(shell echo "$$(date +'%Y%m%d')-$$(git rev-parse --short=8 HEAD)") + all: client server clean: - rm -rf ./build + rm -rf $(BUILDDIR) client: - go build -o ./build/client ./cmd/client + go build -o $(BUILDDIR)/client ./cmd/client server: - go build -o ./build/server ./cmd/server + go build -o $(BUILDDIR)/server ./cmd/server test: go test -v ./... + +debpkg_server_prepare: server + rm -rf $(BUILDDIR)/debpkg + mkdir -p $(BUILDDIR)/debpkg/usr/bin $(BUILDDIR)/debpkg/DEBIAN $(BUILDDIR)/debpkg/etc/systemd/system $(BUILDDIR)/debpkg/etc/sys-exporter + install -m 755 -o root -g root $(BUILDDIR)/server $(BUILDDIR)/debpkg/usr/bin/ + install -m 644 -o root -g root debian/sys-exporter/etc/systemd/system/sys-exporter.service $(BUILDDIR)/debpkg/etc/systemd/system/ + install -m 644 -o root -g root debian/sys-exporter/etc/sys-exporter/config.yml $(BUILDDIR)/debpkg/etc/sys-exporter/ + install -m 644 -o root -g root debian/sys-exporter/control $(BUILDDIR)/debpkg/DEBIAN/control + install -m 755 -o root -g root debian/sys-exporter/postinst $(BUILDDIR)/debpkg/DEBIAN/postinst + +debpkg_server: debpkg_server_prepare + dpkg-deb --build $(BUILDDIR)/debpkg/ $(BUILDDIR)/sys-exporter.deb + +debpkg_server_docker: server + docker build -t debian-builder ./debian/tools + docker run -t -e PACKAGE_VERSION="$(VERSION)" -v $(BUILDDIR):/build/ -v ./debian:/debian debian-builder diff --git a/debian/sys-exporter/control b/debian/sys-exporter/control new file mode 100644 index 0000000..a2b5513 --- /dev/null +++ b/debian/sys-exporter/control @@ -0,0 +1,5 @@ +Package: sys-exporter +Version: ${PACKAGE_VERSION} +Architecture: amd64 +Maintainer: Melora Hugues +Description: System information exporter diff --git a/debian/sys-exporter/etc/sys-exporter/config.yml b/debian/sys-exporter/etc/sys-exporter/config.yml new file mode 100644 index 0000000..724e37d --- /dev/null +++ b/debian/sys-exporter/etc/sys-exporter/config.yml @@ -0,0 +1,7 @@ +logging: + level: info +collectors: + - system.loadavg + - system.meminfo + - services.systemd + - system.uptime diff --git a/debian/sys-exporter/etc/systemd/system/sys-exporter.service b/debian/sys-exporter/etc/systemd/system/sys-exporter.service new file mode 100644 index 0000000..7c974d3 --- /dev/null +++ b/debian/sys-exporter/etc/systemd/system/sys-exporter.service @@ -0,0 +1,16 @@ +[Unit] +Description=System information exporter daemon +After=network-online.target +Wants=network-online.target + +StartLimitIntervalSec=0 + +[Service] +Type=simple +ExecStart=/usr/bin/sys-exporter +TimeoutStartSec=10 +Restart=on-failure +RestartSec=60s + +[Install] +RequiredBy=multi-user.target diff --git a/debian/sys-exporter/postinst b/debian/sys-exporter/postinst new file mode 100644 index 0000000..de9f0db --- /dev/null +++ b/debian/sys-exporter/postinst @@ -0,0 +1,4 @@ +#!/bin/bash + +systemctl enable sys-exporter.service +systemctl start sys-exporter.service diff --git a/debian/tools/Dockerfile b/debian/tools/Dockerfile new file mode 100644 index 0000000..8857c8e --- /dev/null +++ b/debian/tools/Dockerfile @@ -0,0 +1,16 @@ +FROM debian:bookworm + +RUN set -ex \ + && sed -i -- 's/Types: deb/Types: deb deb-src/g' /etc/apt/sources.list.d/debian.sources \ + && apt-get update \ + && apt-get install -y --no-install-recommends \ + build-essential \ + cdbs \ + devscripts \ + equivs \ + fakeroot \ + && apt-get clean \ + && rm -rf /tmp/* /var/tmp/* + +COPY build.sh /root/ +ENTRYPOINT [ "/root/build.sh" ] diff --git a/debian/tools/build.sh b/debian/tools/build.sh new file mode 100755 index 0000000..815eef1 --- /dev/null +++ b/debian/tools/build.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +set -o errexit +set -o pipefail +set -o nounset + +rm -rf /build/debpkg +mkdir -p /build/debpkg/usr/bin /build/debpkg/DEBIAN /build/debpkg/etc/systemd/system /build/debpkg/etc/sys-exporter +install -m 755 -o root -g root /build/server /build/debpkg/usr/bin/ +install -m 644 -o root -g root /debian/sys-exporter/etc/systemd/system/sys-exporter.service /build/debpkg/etc/systemd/system/ +install -m 644 -o root -g root /debian/sys-exporter/etc/sys-exporter/config.yml /build/debpkg/etc/sys-exporter/ +install -m 644 -o root -g root /debian/sys-exporter/control /build/debpkg/DEBIAN/control +install -m 755 -o root -g root /debian/sys-exporter/postinst /build/debpkg/DEBIAN/postinst + +envsubst < /debian/sys-exporter/control > /build/debpkg/DEBIAN/control + +dpkg-deb --build /build/debpkg/ /build/sys-exporter.deb