diff --git a/.gitignore b/.gitignore index 9ad81108..bda3cba7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,14 +1,14 @@ /up.conf /unifi-poller -/*.gz -/*.1 -/*.deb -/*.rpm -/*.pkg +/unifi-poller*.gz +/unifi-poller*.1 +/unifi-poller*.deb +/unifi-poller*.rpm +/unifi-poller*.pkg /vendor .DS_Store *~ -/package_build +/package_build_* /release /unifi-poller.macos /unifi-poller.linux diff --git a/Makefile b/Makefile index 41906141..9747381a 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,7 @@ BINARY:=unifi-poller +URL=https://github.com/davidnewhall/unifi-poller +MAINT="david at sleepers dot pro" +DESC="This daemon polls a Unifi controller at a short interval and stores the collected metric data in an Influx Database." PACKAGE:=./cmd/$(BINARY) VERSION:=$(shell git tag -l --merged | tail -n1 | tr -d v) ITERATION:=$(shell git rev-list --all --count) @@ -6,23 +9,25 @@ ITERATION:=$(shell git rev-list --all --count) all: man build # Prepare a release. Called in Travis CI. -release: clean test man linux macos rpm deb osxpkg +release: clean test rpm deb osxpkg mkdir -p release gzip -9 $(BINARY).linux gzip -9 $(BINARY).macos - mv $(BINARY).macos.gz $(BINARY).linux.gz release/ - mv *.rpm *.deb *.pkg release/ + mv $(BINARY)-$(VERSION)-$(ITERATION).x86_64.rpm $(BINARY)_$(VERSION)-$(ITERATION)_amd64.deb \ + $(BINARY)-$(VERSION).pkg $(BINARY).macos.gz $(BINARY).linux.gz release/ # Delete all build assets. clean: rm -f $(BINARY){.macos,.linux,.1,}{,.gz} rm -f $(BINARY){_,-}*.{deb,rpm,pkg} - rm -rf package_build release + rm -rf package_build_* release # Build a man page from a markdown file using ronn. man: $(BINARY).1.gz $(BINARY).1.gz: - scripts/build_manpages.sh ./ + @ronn --version > /dev/null || (echo "Ronn missing. Install ronn: $(URL)/wiki/Ronn" && false) + @echo "Creating Man Page: $(PACKAGE)/README.md -> $(BINARY).1.gz" + ronn < "$(PACKAGE)/README.md" | gzip -9 > "$(BINARY).1.gz" # Binaries @@ -40,30 +45,100 @@ $(BINARY).macos: # Packages -rpm: man linux $(BINARY)-$(VERSION)-$(ITERATION).x86_64.rpm -$(BINARY)-$(VERSION)-$(ITERATION).x86_64.rpm: - scripts/build_packages.sh rpm "$(VERSION)" "$(ITERATION)" +rpm: $(BINARY)-$(VERSION)-$(ITERATION).x86_64.rpm +$(BINARY)-$(VERSION)-$(ITERATION).x86_64.rpm: check_fpm package_build_linux + @echo "Building 'rpm' package for $(BINARY) version '$(VERSION)-$(ITERATION)'." + fpm -s dir -t rpm \ + --name $(BINARY) \ + --version $(VERSION) \ + --iteration $(ITERATION) \ + --after-install scripts/after-install.sh \ + --before-remove scripts/before-remove.sh \ + --license MIT \ + --url $(URL) \ + --maintainer $(MAINT) \ + --description $(DESC) \ + --chdir package_build_linux -deb: man linux $(BINARY)_$(VERSION)-$(ITERATION)_amd64.deb -$(BINARY)_$(VERSION)-$(ITERATION)_amd64.deb: - scripts/build_packages.sh deb "$(VERSION)" "$(ITERATION)" +deb: $(BINARY)_$(VERSION)-$(ITERATION)_amd64.deb +$(BINARY)_$(VERSION)-$(ITERATION)_amd64.deb: check_fpm package_build_linux + @echo "Building 'deb' package for $(BINARY) version '$(VERSION)-$(ITERATION)'." + fpm -s dir -t deb \ + --name $(BINARY) \ + --version $(VERSION) \ + --iteration $(ITERATION) \ + --after-install scripts/after-install.sh \ + --before-remove scripts/before-remove.sh \ + --license MIT \ + --url $(URL) \ + --maintainer $(MAINT) \ + --description $(DESC) \ + --chdir package_build_linux -osxpkg: man macos $(BINARY)-$(VERSION).pkg -$(BINARY)-$(VERSION).pkg: - scripts/build_packages.sh osxpkg "$(VERSION)" "$(ITERATION)" +osxpkg: $(BINARY)-$(VERSION).pkg +$(BINARY)-$(VERSION).pkg: check_fpm package_build_osx + @echo "Building 'osx' package for $(BINARY) version '$(VERSION)-$(ITERATION)'." + fpm -s dir -t osxpkg \ + --name $(BINARY) \ + --version $(VERSION) \ + --iteration $(ITERATION) \ + --after-install scripts/after-install-osx.sh \ + --osxpkg-identifier-prefix com.github.davidnewhall \ + --license MIT \ + --url $(URL) \ + --maintainer $(MAINT) \ + --description $(DESC) \ + --chdir package_build_osx + +# OSX packages use /usr/local because Apple doesn't allow writing many other places. +package_build_osx: man macos + # Build package environment for macOS. + mkdir -p $@/usr/local/bin $@/usr/local/etc/$(BINARY) + mkdir -p $@/usr/local/share/man/man1 $@/usr/local/share/doc/$(BINARY) + # Copy the binary, config file and man page into the env. + cp $(BINARY).macos $@/usr/local/bin/$(BINARY) + cp *.1.gz $@/usr/local/share/man/man1 + cp examples/*.conf.example $@/usr/local/etc/$(BINARY)/ + cp examples/* $@/usr/local/share/doc/$(BINARY)/ + mkdir -p $@/usr/local/var/log + mkdir -p $@/Library/LaunchAgents + cp init/launchd/com.github.davidnewhall.unifi-poller.plist $@/Library/LaunchAgents/ + +# Build an environment that can be packaged for linux. +package_build_linux: man linux + # Build package environment for linux. + mkdir -p $@/usr/bin $@/etc/$(BINARY) + mkdir -p $@/usr/share/man/man1 $@/usr/share/doc/$(BINARY) + # Copy the binary, config file and man page into the env. + cp ${BINARY}.linux $@/usr/bin/$(BINARY) + cp *.1.gz $@/usr/share/man/man1 + cp examples/*.conf.example $@/etc/$(BINARY)/ + cp examples/* $@/usr/share/doc/$(BINARY)/ + cp examples/up.conf.example $@/etc/$(BINARY)/up.conf + # Fix the paths in the systemd unit file before copying it into the emv. + mkdir -p $@/lib/systemd/system + sed "s%ExecStart.*%ExecStart=/usr/bin/$(BINARY) --config=/etc/${BINARY}/up.conf%" \ + init/systemd/unifi-poller.service > $@/lib/systemd/system/$(BINARY).service + +check_fpm: + @fpm --version > /dev/null || (echo "FPM missing. Install FPM: https://fpm.readthedocs.io/en/latest/installing.html" && false) # Extras +# Run code tests and lint. test: lint go test -race -covermode=atomic $(PACKAGE) lint: golangci-lint run --enable-all -D gochecknoglobals +# Install locally into /usr/local. Not recommended. install: man scripts/local_install.sh +# If you installed with `make install` run `make uninstall` before installing a binary package. uninstall: scripts/local_uninstall.sh +# Don't run this unless you're ready to debug untested vendored dependencies. deps: dep ensure -update diff --git a/scripts/build_manpages.sh b/scripts/build_manpages.sh deleted file mode 100755 index cf223e1b..00000000 --- a/scripts/build_manpages.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -# This file uses ronn to build a man page for unifi-poller. -set -o pipefail - -OUTPUT=$1 - -# This requires the installation of `ronn`: sudo gem install ronn -for f in cmd/*/README.md;do - # Strtip off cmd/ then strip off README to get the man-file name. - PKGNOCMD="${f#cmd/}" - PKG="${PKGNOCMD%/README.md}" - echo "Creating Man Page: ${f} -> ${OUTPUT}${PKG}.1.gz" - ronn < "$f" | gzip -9 > "${OUTPUT}${PKG}.1.gz" || \ - echo "If this produces an error. Install ronn; something like: sudo gem install ronn" -done diff --git a/scripts/build_packages.sh b/scripts/build_packages.sh deleted file mode 100755 index fb6db411..00000000 --- a/scripts/build_packages.sh +++ /dev/null @@ -1,79 +0,0 @@ -#!/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