Skip to content

Commit

Permalink
Merge pull request #2 from wbollock/feat/goreleaser
Browse files Browse the repository at this point in the history
feat: goreleaser; nfpm
  • Loading branch information
wbollock authored Sep 2, 2022
2 parents 82deafe + 0d83329 commit 6bf4d0f
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
# nagios_exporter specific
devconfig.toml
main

dist/
32 changes: 32 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -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:'
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
36 changes: 36 additions & 0 deletions nfpm.yaml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"
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
3 changes: 3 additions & 0 deletions nfpm/etc/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# prometheus-nagios-exporter configuration

APIKey = ""
76 changes: 76 additions & 0 deletions nfpm/scripts/postinstall.sh
Original file line number Diff line number Diff line change
@@ -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=<current version>
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
15 changes: 15 additions & 0 deletions nfpm/systemd/prometheus-nagios-exporter.service
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6bf4d0f

Please sign in to comment.