From 24e0115a225e3e7d1b79d4f9ad4476a6dbbdefc9 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 7 Jun 2019 12:19:17 -0700 Subject: [PATCH] Makefile and build pipeline updates. --- integrations/promunifi/Makefile | 77 ++++++++++-------- .../examples/unifi-usw-grafana-dash.json | 4 +- .../promunifi/scripts/build_linux_packages.sh | 52 ------------ .../promunifi/scripts/build_osx_package.sh | 46 ----------- .../promunifi/scripts/build_packages.sh | 79 +++++++++++++++++++ 5 files changed, 125 insertions(+), 133 deletions(-) delete mode 100755 integrations/promunifi/scripts/build_linux_packages.sh delete mode 100755 integrations/promunifi/scripts/build_osx_package.sh create mode 100755 integrations/promunifi/scripts/build_packages.sh diff --git a/integrations/promunifi/Makefile b/integrations/promunifi/Makefile index 7898ba48..41906141 100644 --- a/integrations/promunifi/Makefile +++ b/integrations/promunifi/Makefile @@ -1,53 +1,64 @@ -PACKAGE=./cmd/unifi-poller -BINARY=unifi-poller -VERSION=`git tag -l --merged | tail -n1` +BINARY:=unifi-poller +PACKAGE:=./cmd/$(BINARY) +VERSION:=$(shell git tag -l --merged | tail -n1 | tr -d v) +ITERATION:=$(shell git rev-list --all --count) -all: man unifi-poller +all: man build # Prepare a release. Called in Travis CI. release: clean test man linux macos rpm deb osxpkg mkdir -p release - gzip -9k unifi-poller.linux - gzip -9k unifi-poller.macos - mv unifi-poller.macos.gz unifi-poller.linux.gz release/ - cp *.rpm *.deb *.pkg release/ + gzip -9 $(BINARY).linux + gzip -9 $(BINARY).macos + mv $(BINARY).macos.gz $(BINARY).linux.gz release/ + mv *.rpm *.deb *.pkg release/ +# Delete all build assets. clean: - rm -f `echo $(PACKAGE)|cut -d/ -f3`{.macos,.linux,.1,}{,.gz} - rm -f `echo $(PACKAGE)|cut -d/ -f3`{_,-}*.{deb,rpm,pkg} + rm -f $(BINARY){.macos,.linux,.1,}{,.gz} + rm -f $(BINARY){_,-}*.{deb,rpm,pkg} rm -rf package_build release -build: unifi-poller -unifi-poller: - go build -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE) +# Build a man page from a markdown file using ronn. +man: $(BINARY).1.gz +$(BINARY).1.gz: + scripts/build_manpages.sh ./ -linux: unifi-poller.linux -unifi-poller.linux: - GOOS=linux go build -o unifi-poller.linux -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE) +# Binaries -macos: unifi-poller.macos -unifi-poller.macos: - GOOS=darwin go build -o unifi-poller.macos -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE) +build: $(BINARY) +$(BINARY): + go build -o $(BINARY) -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE) + +linux: $(BINARY).linux +$(BINARY).linux: + GOOS=linux go build -o $(BINARY).linux -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE) + +macos: $(BINARY).macos +$(BINARY).macos: + GOOS=darwin go build -o $(BINARY).macos -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE) + +# Packages + +rpm: man linux $(BINARY)-$(VERSION)-$(ITERATION).x86_64.rpm +$(BINARY)-$(VERSION)-$(ITERATION).x86_64.rpm: + scripts/build_packages.sh rpm "$(VERSION)" "$(ITERATION)" + +deb: man linux $(BINARY)_$(VERSION)-$(ITERATION)_amd64.deb +$(BINARY)_$(VERSION)-$(ITERATION)_amd64.deb: + scripts/build_packages.sh deb "$(VERSION)" "$(ITERATION)" + +osxpkg: man macos $(BINARY)-$(VERSION).pkg +$(BINARY)-$(VERSION).pkg: + scripts/build_packages.sh osxpkg "$(VERSION)" "$(ITERATION)" + +# Extras test: lint go test -race -covermode=atomic $(PACKAGE) - lint: golangci-lint run --enable-all -D gochecknoglobals -man: unifi-poller.1.gz -unifi-poller.1.gz: - scripts/build_manpages.sh ./ - -rpm: man linux - scripts/build_linux_packages.sh rpm - -deb: man linux - scripts/build_linux_packages.sh deb - -osxpkg: man macos - scripts/build_osx_package.sh - install: man scripts/local_install.sh diff --git a/integrations/promunifi/examples/unifi-usw-grafana-dash.json b/integrations/promunifi/examples/unifi-usw-grafana-dash.json index f221b4ab..a40c3ce2 100644 --- a/integrations/promunifi/examples/unifi-usw-grafana-dash.json +++ b/integrations/promunifi/examples/unifi-usw-grafana-dash.json @@ -53,7 +53,7 @@ "gnetId": null, "graphTooltip": 1, "id": null, - "iteration": 1559898667753, + "iteration": 1559898956053, "links": [ { "icon": "external link", @@ -3410,5 +3410,5 @@ "timezone": "", "title": "UniFi USW Insights", "uid": "HIKZ98GZz", - "version": 85 + "version": 87 } \ No newline at end of file diff --git a/integrations/promunifi/scripts/build_linux_packages.sh b/integrations/promunifi/scripts/build_linux_packages.sh deleted file mode 100755 index 9a6c5512..00000000 --- a/integrations/promunifi/scripts/build_linux_packages.sh +++ /dev/null @@ -1,52 +0,0 @@ -#!/bin/bash - -# This script builds a deb or rpm package. Run by the Makefile. -# Use: `make rpm` or `make deb` - -OUTPUT=$1 -BINARY=unifi-poller -VERSION=$(git tag -l --merged | tail -n1 | tr -d v) - -if [ "$OUTPUT" != "deb" ] && [ "$OUTPUT" != "rpm" ]; then - echo "first argument must be 'deb' or 'rpm'" - exit 1 -fi - -fpm -h > /dev/null 2>&1 -if [ "$?" != "0" ]; then - echo "fpm missing. Install fpm: https://fpm.readthedocs.io/en/latest/installing.html" - exit 1 -fi - -echo "Building '${OUTPUT}' package for ${BINARY} version ${VERSION}." - -# eh, don't change these. -PREFIX= -BINFIX=/usr - -# Make a build environment. -rm -rf package_build -mkdir -p package_build${BINFIX}/bin package_build${PREFIX}/etc/${BINARY} package_build${BINFIX}/share/man/man1 - -# Copy the binary, config file and man page into the env. -cp ${BINARY}.linux package_build${BINFIX}/bin/${BINARY} -cp *.1.gz package_build${BINFIX}/share/man/man1 -cp examples/up.conf.example package_build${PREFIX}/etc/${BINARY}/up.conf - -# Fix the paths in the systemd unit file before copying it into the emv. -mkdir -p package_build/lib/systemd/system -sed "s#ExecStart.*#ExecStart=${BINFIX}/bin/${BINARY} --config=${PREFIX}/etc/${BINARY}/up.conf#" \ - init/systemd/unifi-poller.service > package_build/lib/systemd/system/${BINARY}.service - -# Make a package. -fpm -s dir -t ${OUTPUT} \ - --name ${BINARY} \ - --version ${VERSION} \ - --iteration $(git rev-list --all --count) \ - --after-install scripts/after-install.sh \ - --before-remove scripts/before-remove.sh \ - --license MIT \ - --url 'https://github.com/davidnewhall/unifi-poller' \ - --maintainer 'david at sleepers dot pro' \ - --description 'This daemon polls a Unifi controller at a short interval and stores the collected metric data in an Influx Database.' \ - --chdir package_build diff --git a/integrations/promunifi/scripts/build_osx_package.sh b/integrations/promunifi/scripts/build_osx_package.sh deleted file mode 100755 index 08d1a57b..00000000 --- a/integrations/promunifi/scripts/build_osx_package.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash - -# This script builds a simple macos Installer pkg. Run by the Makefile. -# Use: `make osxpkg` - -OUTPUT=osxpkg -BINARY=unifi-poller -VERSION=$(git tag -l --merged | tail -n1 | tr -d v) - -fpm -h > /dev/null 2>&1 -if [ "$?" != "0" ]; then - echo "fpm missing. Install fpm: https://fpm.readthedocs.io/en/latest/installing.html" - exit 1 -fi - -echo "Building '${OUTPUT}' package for ${BINARY} version ${VERSION}." - -PREFIX=/usr/local -BINFIX=/usr/local - -# Make a build environment. -rm -rf package_build -mkdir -p package_build${BINFIX}/bin package_build${PREFIX}/etc/${BINARY} package_build${BINFIX}/share/man/man1 -mkdir -p package_build${PREFIX}/var/log - -# Copy the binary, config file and man page into the env. -cp ${BINARY}.macos package_build${BINFIX}/bin/${BINARY} -cp *.1.gz package_build${BINFIX}/share/man/man1 -cp examples/up.conf.example package_build${PREFIX}/etc/${BINARY}/ - -# Copy in launch agent. -mkdir -p package_build/Library/LaunchAgents -cp init/launchd/com.github.davidnewhall.unifi-poller.plist package_build/Library/LaunchAgents/ - -# Make a package. -fpm -s dir -t ${OUTPUT} \ - --name ${BINARY} \ - --version ${VERSION} \ - --iteration $(git rev-list --all --count) \ - --after-install scripts/after-install-osx.sh \ - --osxpkg-identifier-prefix com.github.davidnewhall \ - --license MIT \ - --maintainer 'david at sleepers dot pro' \ - --url 'https://github.com/davidnewhall/unifi-poller' \ - --description 'This daemon polls a Unifi controller at a short interval and stores the collected metric data in an Influx Database.' \ - --chdir package_build diff --git a/integrations/promunifi/scripts/build_packages.sh b/integrations/promunifi/scripts/build_packages.sh new file mode 100755 index 00000000..fb6db411 --- /dev/null +++ b/integrations/promunifi/scripts/build_packages.sh @@ -0,0 +1,79 @@ +#!/bin/bash + +# This script builds a deb, rpm or osx package. Run by the Makefile. +# Use: `make rpm`, `make deb`, or `make osxpkg` + +set -e -o pipefail + +BINARY=unifi-poller +OUTPUT=$1 +VERSION=$2 +ITERATION=$3 +[ "$VERSION" != "" ] || VERSION=$(git tag -l --merged | tail -n1 | tr -d v) +[ "$ITERATION" != "" ] || ITERATION=$(git rev-list --all --count) + +if [ "$OUTPUT" != "deb" ] && [ "$OUTPUT" != "rpm" ] && [ "$OUTPUT" != "osxpkg" ]; then + echo "first argument must be 'deb' or 'rpm' or 'osxpkg'" + exit 1 +fi + +fpm -h > /dev/null 2>&1 +if [ "$?" != "0" ]; then + echo "Package Build Failure. FPM missing. Install FPM: https://fpm.readthedocs.io/en/latest/installing.html" + exit 1 +fi + +echo "Building '${OUTPUT}' package for ${BINARY} version '${VERSION}-${ITERATION}'." + +# These paths work on Linux. Suggest not changing. +PREFIX= +BINFIX=/usr +UNAME=linux +AFTER=scripts/after-install.sh +if [ "$OUTPUT" = "osxpkg" ]; then + # These paths work on OSX. Do not change. + PREFIX=/usr/local + BINFIX=/usr/local + UNAME=macos + AFTER=scripts/after-install-osx.sh +fi + +# Make a build environment. +rm -rf package_build +mkdir -p package_build${BINFIX}/bin package_build${PREFIX}/etc/${BINARY} +mkdir -p package_build${BINFIX}/share/man/man1 package_build${BINFIX}/share/doc/unifi-poller + +# Copy the binary, config file and man page into the env. +cp ${BINARY}.${UNAME} package_build${BINFIX}/bin/${BINARY} +cp *.1.gz package_build${BINFIX}/share/man/man1 +cp examples/*.conf.example package_build${PREFIX}/etc/${BINARY}/ +cp examples/* package_build${BINFIX}/share/doc/unifi-poller + +# Copy startup file. Different for osx vs linux. +if [ "$UNAME" = "linux" ]; then + cp examples/up.conf.example package_build${PREFIX}/etc/${BINARY}/up.conf + # Fix the paths in the systemd unit file before copying it into the emv. + mkdir -p package_build/lib/systemd/system + sed "s#ExecStart.*#ExecStart=${BINFIX}/bin/${BINARY} --config=${PREFIX}/etc/${BINARY}/up.conf#" \ + init/systemd/unifi-poller.service > package_build/lib/systemd/system/${BINARY}.service + +else # macos + # Sometimes the log folder is missing on osx. Create it. + mkdir -p package_build${PREFIX}/var/log + mkdir -p package_build/Library/LaunchAgents + cp init/launchd/com.github.davidnewhall.unifi-poller.plist package_build/Library/LaunchAgents/ +fi + +# Make a package. +fpm -s dir -t ${OUTPUT} \ + --name ${BINARY} \ + --version ${VERSION} \ + --iteration ${ITERATION} \ + --after-install ${AFTER} \ + --before-remove scripts/before-remove.sh \ + --osxpkg-identifier-prefix com.github.davidnewhall \ + --license MIT \ + --url 'https://github.com/davidnewhall/unifi-poller' \ + --maintainer 'david at sleepers dot pro' \ + --description 'This daemon polls a Unifi controller at a short interval and stores the collected metric data in an Influx Database.' \ + --chdir package_build