From 5237a5883942500cf354e24e425ea663118f7d98 Mon Sep 17 00:00:00 2001 From: Will Bollock Date: Fri, 2 Sep 2022 19:57:18 -0400 Subject: [PATCH 1/2] feat: add goreleaser --- .gitignore | 2 ++ .goreleaser.yaml | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 .goreleaser.yaml diff --git a/.gitignore b/.gitignore index f8eb9c5..6804791 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ # nagios_exporter specific devconfig.toml main + +dist/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..6f8696c --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,32 @@ +# This is an example .goreleaser.yml file with some sensible defaults. +# Make sure to check the documentation at https://goreleaser.com +before: + hooks: + # You may remove this if you don't use go modules. + - go mod tidy + # you may remove this if you don't need go generate + - go generate ./... +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin +archives: + - replacements: + darwin: Darwin + linux: Linux + windows: Windows + 386: i386 + amd64: x86_64 +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ incpatch .Version }}-next" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' From 0d83329091e3101865aaa88e0dddcabbb5709489 Mon Sep 17 00:00:00 2001 From: Will Bollock Date: Fri, 2 Sep 2022 19:57:35 -0400 Subject: [PATCH 2/2] feat: add nfpm settings and package configuration --- README.md | 26 +++++++ nfpm.yaml | 36 +++++++++ nfpm/etc/config.toml | 3 + nfpm/scripts/postinstall.sh | 76 +++++++++++++++++++ .../prometheus-nagios-exporter.service | 15 ++++ 5 files changed, 156 insertions(+) create mode 100644 nfpm.yaml create mode 100644 nfpm/etc/config.toml create mode 100644 nfpm/scripts/postinstall.sh create mode 100644 nfpm/systemd/prometheus-nagios-exporter.service diff --git a/README.md b/README.md index fcd618f..d78858a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,29 @@ # nagiosxi_exporter +## Build and Release Steps + +1. Build binaries with goreleaser: + +```bash +goreleaser build --snapshot --rm-dist +``` + +2. Use the resulting binaries in `./dist`, or create a deb/rpm packages with nfpm: + +```bash +# deb example - can substitute with rpm +nfpm package -p deb -t /tmp/ +``` + +3. Tag release and push: + +``` +git tag -a v0.1.0 -m "First release" +git push origin v0.1.0 +goreleaser release +``` + + ## Resources * [haproxy_expoter](https://github.com/prometheus/haproxy_exporter/blob/main/haproxy_exporter.go) @@ -8,3 +32,5 @@ * [mirth_exporter](https://github.com/teamzerolabs/mirth_channel_exporter) * [golang-json-api-client](https://blog.alexellis.io/golang-json-api-client/) * [jsonutils](https://github.com/bashtian/jsonutils) +* [goreleaser](https://github.com/goreleaser/goreleaser) +* [nfpm](https://github.com/goreleaser/nfpm) diff --git a/nfpm.yaml b/nfpm.yaml new file mode 100644 index 0000000..bac1aea --- /dev/null +++ b/nfpm.yaml @@ -0,0 +1,36 @@ +# nfpm example config file +# +# check https://nfpm.goreleaser.com/configuration for detailed usage +# +name: "prometheus-nagios-exporter" +arch: "amd64" +platform: "linux" +version: "v1.0.0" +section: "default" +priority: "extra" +replaces: [] +provides: [] +depends: [] +recommends: [] +suggests: [] +conflicts: [] +maintainer: "Will Bollock " +description: | + A Prometheus Nagios Exporter that provides information + on the current state of a Nagios application and configuration. +homepage: "https://github.com/wbollock/nagios_exporter" +license: "MIT" +# TODO - changelog? +#changelog: "changelog.yaml" +contents: +# provided by goreleaser +- src: ./dist/nagios_exporter_linux_amd64_v1/nagios_exporter + dst: /usr/local/bin/nagios_exporter +- src: ./nfpm/etc/config.toml + dst: /etc/nagios_exporter/config.toml + type: config +- src: ./nfpm/systemd/prometheus-nagios-exporter.service + dst: /etc/systemd/system/prometheus-nagios-exporter.service + type: config +scripts: + postinstall: ./nfpm/scripts/postinstall.sh diff --git a/nfpm/etc/config.toml b/nfpm/etc/config.toml new file mode 100644 index 0000000..2d2a1ce --- /dev/null +++ b/nfpm/etc/config.toml @@ -0,0 +1,3 @@ +# prometheus-nagios-exporter configuration + +APIKey = "" diff --git a/nfpm/scripts/postinstall.sh b/nfpm/scripts/postinstall.sh new file mode 100644 index 0000000..3b8986d --- /dev/null +++ b/nfpm/scripts/postinstall.sh @@ -0,0 +1,76 @@ +#!/bin/sh + +# Source: https://nfpm.goreleaser.com/tips/#example-multi-platform-post-install-script + +# Step 1, decide if we should use systemd or init/upstart +use_systemctl="True" +systemd_version=0 +if ! command -V systemctl >/dev/null 2>&1; then + use_systemctl="False" +else + systemd_version=$(systemctl --version | head -1 | awk '{ print $2}') +fi + +cleanup() { + # This is where you remove files that were not needed on this platform / system + if [ "${use_systemctl}" = "False" ]; then + rm -f /etc/systemd/system/prometheus-nagios-exporter.service + rm -f /etc/nagios_exporter/config.toml + else + rm -f /etc/chkconfig/prometheus-nagios-exporter + rm -f /etc/init.d/prometheus-nagios-exporter + fi +} + +cleanInstall() { + # Step 3 (clean install), enable the service in the proper way for this platform + if [ "${use_systemctl}" = "False" ]; then + if command -V chkconfig >/dev/null 2>&1; then + chkconfig --add prometheus-nagios-exporter + fi + + service prometheus-nagios-exporter restart ||: + else + # rhel/centos7 cannot use ExecStartPre=+ to specify the pre start should be run as root + # even if you want your service to run as non root. + if [ "${systemd_version}" -lt 231 ]; then + printf "\033[31m systemd version %s is less then 231, fixing the service file \033[0m\n" "${systemd_version}" + sed -i "s/=+/=/g" /etc/systemd/system/prometheus-nagios-exporter.service + fi + systemctl daemon-reload ||: + systemctl unmask prometheus-nagios-exporter ||: + systemctl preset prometheus-nagios-exporter ||: + systemctl enable prometheus-nagios-exporter ||: + systemctl restart prometheus-nagios-exporter ||: + fi +} + +# upgrade() { +# # Step 3(upgrade), do what you need +# # TODO - do I need upgrade steps? +# } + +# Step 2, check if this is a clean install or an upgrade +action="$1" +if [ "$1" = "configure" ] && [ -z "$2" ]; then + # Alpine linux does not pass args, and deb passes $1=configure + action="install" +elif [ "$1" = "configure" ] && [ -n "$2" ]; then + # deb passes $1=configure $2= + action="upgrade" +fi + +case "$action" in + "1" | "install") + cleanInstall + ;; + "2" | "upgrade") + upgrade + ;; + *) + cleanInstall + ;; +esac + +# Step 4, clean up unused files, yes you get a warning when you remove the package, but that is ok. +cleanup diff --git a/nfpm/systemd/prometheus-nagios-exporter.service b/nfpm/systemd/prometheus-nagios-exporter.service new file mode 100644 index 0000000..e46169d --- /dev/null +++ b/nfpm/systemd/prometheus-nagios-exporter.service @@ -0,0 +1,15 @@ +[Unit] +Description=Nagios Exporter +Documentation=https://github.com/wbollock/nagios_exporter +Wants=network-online.target nagios.service +After=network-online.target nagios.service + +[Service] +EnvironmentFile=-/etc/default/prometheus-nagios-exporter +User=root +Group=root +Type=simple +ExecStart=/usr/local/bin/nagios_exporter $ARGS + +[Install] +WantedBy=multi-user.target