From 24e0115a225e3e7d1b79d4f9ad4476a6dbbdefc9 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 7 Jun 2019 12:19:17 -0700 Subject: [PATCH 01/11] 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 From 7c5ad6a8981d6b74cce756b3e4cab5a93b38b6f7 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 7 Jun 2019 13:45:19 -0700 Subject: [PATCH 02/11] Move builds into Makefile. --- integrations/promunifi/.gitignore | 12 +- integrations/promunifi/Makefile | 103 +++++++++++++++--- .../promunifi/scripts/build_manpages.sh | 16 --- .../promunifi/scripts/build_packages.sh | 79 -------------- 4 files changed, 95 insertions(+), 115 deletions(-) delete mode 100755 integrations/promunifi/scripts/build_manpages.sh delete mode 100755 integrations/promunifi/scripts/build_packages.sh diff --git a/integrations/promunifi/.gitignore b/integrations/promunifi/.gitignore index 9ad81108..bda3cba7 100644 --- a/integrations/promunifi/.gitignore +++ b/integrations/promunifi/.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/integrations/promunifi/Makefile b/integrations/promunifi/Makefile index 41906141..9747381a 100644 --- a/integrations/promunifi/Makefile +++ b/integrations/promunifi/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/integrations/promunifi/scripts/build_manpages.sh b/integrations/promunifi/scripts/build_manpages.sh deleted file mode 100755 index cf223e1b..00000000 --- a/integrations/promunifi/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/integrations/promunifi/scripts/build_packages.sh b/integrations/promunifi/scripts/build_packages.sh deleted file mode 100755 index fb6db411..00000000 --- a/integrations/promunifi/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 From e85d091743874be21e20cd0a8fa58b734b7f11ae Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 7 Jun 2019 13:54:26 -0700 Subject: [PATCH 03/11] Combined after install scripts. --- integrations/promunifi/.gitignore | 4 +-- integrations/promunifi/Makefile | 2 +- integrations/promunifi/examples/README.md | 2 +- .../promunifi/scripts/after-install-osx.sh | 19 ------------- .../promunifi/scripts/after-install.sh | 28 ++++++++++++++++--- 5 files changed, 28 insertions(+), 27 deletions(-) delete mode 100755 integrations/promunifi/scripts/after-install-osx.sh diff --git a/integrations/promunifi/.gitignore b/integrations/promunifi/.gitignore index bda3cba7..61d1785f 100644 --- a/integrations/promunifi/.gitignore +++ b/integrations/promunifi/.gitignore @@ -5,10 +5,10 @@ /unifi-poller*.deb /unifi-poller*.rpm /unifi-poller*.pkg +/unifi-poller.macos +/unifi-poller.linux /vendor .DS_Store *~ /package_build_* /release -/unifi-poller.macos -/unifi-poller.linux diff --git a/integrations/promunifi/Makefile b/integrations/promunifi/Makefile index 9747381a..0652c6af 100644 --- a/integrations/promunifi/Makefile +++ b/integrations/promunifi/Makefile @@ -82,7 +82,7 @@ $(BINARY)-$(VERSION).pkg: check_fpm package_build_osx --name $(BINARY) \ --version $(VERSION) \ --iteration $(ITERATION) \ - --after-install scripts/after-install-osx.sh \ + --after-install scripts/after-install.sh \ --osxpkg-identifier-prefix com.github.davidnewhall \ --license MIT \ --url $(URL) \ diff --git a/integrations/promunifi/examples/README.md b/integrations/promunifi/examples/README.md index 90fa3dd3..d9799785 100644 --- a/integrations/promunifi/examples/README.md +++ b/integrations/promunifi/examples/README.md @@ -1,6 +1,6 @@ # Grafana Dashboards & Examples -This folder contains 3 grafana dashboards to get you started with the new data pool. +This folder contains 4 grafana dashboards to get you started with the new data pool. Import these into Grafana to quickly visualize data from your devices. Created with Grafana 6.2. diff --git a/integrations/promunifi/scripts/after-install-osx.sh b/integrations/promunifi/scripts/after-install-osx.sh deleted file mode 100755 index bc2ad33c..00000000 --- a/integrations/promunifi/scripts/after-install-osx.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash - -# This file is used by osxpkg packages. FPM use. - -# Copy the config file into place if it does not exist. -if [ ! -f /usr/local/etc/unifi-poller/up.conf ] && [ -f /usr/local/etc/unifi-poller/up.conf.example ]; then - cp /usr/local/etc/unifi-poller/up.conf.example /usr/local/etc/unifi-poller/up.conf -fi - -# Allow admins to change the configuration and write logs. -chgrp -R admin /usr/local/etc/unifi-poller -chmod -R g+wr /usr/local/etc/unifi-poller - -# Make sure admins can write logs. -chgrp admin /usr/local/var/log -chmod g=rwx /usr/local/var/log - -# This starts it as root. no no no .... not sure how to fix that. -# launchctl load /Library/LaunchAgents/com.github.davidnewhall.unifi-poller.plist diff --git a/integrations/promunifi/scripts/after-install.sh b/integrations/promunifi/scripts/after-install.sh index 2fb3cd87..8842906b 100755 --- a/integrations/promunifi/scripts/after-install.sh +++ b/integrations/promunifi/scripts/after-install.sh @@ -1,7 +1,27 @@ #!/bin/bash -# This file is used by rpm and deb packages. FPM use. +# This file is used by deb, rpm and osx packages. +# FPM adds this as the after-install script. -systemctl daemon-reload -systemctl enable unifi-poller -systemctl restart unifi-poller +if [ "$(uname -s)" = "Darwin" ]; then + # Copy the config file into place if it does not exist. + if [ ! -f /usr/local/etc/unifi-poller/up.conf ] && [ -f /usr/local/etc/unifi-poller/up.conf.example ]; then + cp /usr/local/etc/unifi-poller/up.conf.example /usr/local/etc/unifi-poller/up.conf + fi + + # Allow admins to change the configuration and write logs. + chgrp -R admin /usr/local/etc/unifi-poller + chmod -R g+wr /usr/local/etc/unifi-poller + + # Make sure admins can write logs. + chgrp admin /usr/local/var/log + chmod g=rwx /usr/local/var/log + + # This starts it as root. no no no .... not sure how to fix that. + # launchctl load /Library/LaunchAgents/com.github.davidnewhall.unifi-poller.plist + +elif [ -x "/bin/systemctl" ]; then + /bin/systemctl daemon-reload + /bin/systemctl enable unifi-poller + /bin/systemctl restart unifi-poller +fi From 0c9431d09e52cb74a3484a6b1f26663509a363f3 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 7 Jun 2019 14:24:20 -0700 Subject: [PATCH 04/11] doc and path updates. --- integrations/promunifi/README.md | 16 ++++------------ .../promunifi/cmd/unifi-poller/README.md | 10 +++++----- .../promunifi/cmd/unifi-poller/config.go | 2 +- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/integrations/promunifi/README.md b/integrations/promunifi/README.md index df5a2333..cc89530e 100644 --- a/integrations/promunifi/README.md +++ b/integrations/promunifi/README.md @@ -37,12 +37,6 @@ for making this dashboard; it gave me a fantastic start to making my own. # What now... -- Better Linux support and testing - -I only, personally, run this on a Mac 10.13.something. I know others are using -Linux and it's working, but I need more feedback. Does the unit file work? Are -you able to stop and start the service? Does the Makefile do the right things? - - Are there other devices that need to be included? I have: switch, router, access point. Three total, and the type structs are @@ -55,7 +49,10 @@ Issue and lets discuss. - Better Installation instructions. If you're a nerd you can probably figure it out. I'd still like some pretty -pictures and maybe even a Twitch VOD. +pictures and maybe even a Twitch VOD. Update: The installation has been +simplified tremendously with the +[creation of binary packages](https://github.com/davidnewhall/unifi-poller/wiki/Package-Install). +More to come! - Radios, Frequencies, Interfaces, vAPs @@ -63,11 +60,6 @@ My access points only seem to have two radios, one interface and vAP per radio. I'm not sure if the graphs, as-is, provide enough insight into APs with other configurations. Help me figure that out? -- It possibly loses access to the controller at some point. - -I noticed metrics stop updating after a while. I think the new code will help -isolate why this happens. We may need to issue a reconnect and get a new cookie. - # What's it look like? Here's a picture of the Client dashboard. diff --git a/integrations/promunifi/cmd/unifi-poller/README.md b/integrations/promunifi/cmd/unifi-poller/README.md index 691b7de3..df977efb 100644 --- a/integrations/promunifi/cmd/unifi-poller/README.md +++ b/integrations/promunifi/cmd/unifi-poller/README.md @@ -1,9 +1,9 @@ -unifi-poller(1) -- Utility to poll Unifi Metrics and drop them into InfluxDB +unifi-poller(1) -- Utility to poll UniFi Controller Metrics and store them in InfluxDB === ## SYNOPSIS -`unifi-poller -c /usr/local/etc/unifi-poller.conf` +`unifi-poller -c /etc/unifi-poller.conf` ## DESCRIPTION @@ -26,7 +26,7 @@ unifi-poller(1) -- Utility to poll Unifi Metrics and drop them into InfluxDB ## CONFIGURATION -* Config File Default Location: /usr/local/etc/unifi-poller/up.conf +* Config File Default Location: /etc/unifi-poller/up.conf `Config File Parameters` @@ -94,10 +94,10 @@ Example Use: `1m`, `5h`, `100ms`, `17s`, `1s45ms`, `1m3s` * Garrett Bjerkhoel (original code) ~ 2016 * David Newhall II (rewritten) ~ 4/20/2018 +* David Newhall II (still going) ~ 6/7/2019 ## LOCATION * https://github.com/davidnewhall/unifi-poller -* /usr/local/bin/unifi-poller -* config-file: /usr/local/etc/unifi-poller/up.conf +* UniFi Library: https://github.com/golift/unifi * previously: https://github.com/dewski/unifi diff --git a/integrations/promunifi/cmd/unifi-poller/config.go b/integrations/promunifi/cmd/unifi-poller/config.go index 44b84be1..1b75860a 100644 --- a/integrations/promunifi/cmd/unifi-poller/config.go +++ b/integrations/promunifi/cmd/unifi-poller/config.go @@ -7,7 +7,7 @@ var Version = "development" const ( // App defaults in case they're missing from the config. - defaultConfFile = "/usr/local/etc/unifi-poller/up.conf" + defaultConfFile = "/etc/unifi-poller/up.conf" defaultInterval = 30 * time.Second defaultInfxDb = "unifi" defaultInfxUser = "unifi" From f0aa7db84a12f30b2c34068fbf30395cf2fb2f4c Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 7 Jun 2019 15:12:06 -0700 Subject: [PATCH 05/11] More cleanup. Move uninstall to Makefile. --- integrations/promunifi/Makefile | 55 ++++++++++++------- .../promunifi/cmd/unifi-poller/README.md | 4 +- .../promunifi/scripts/local_uninstall.sh | 36 ------------ 3 files changed, 38 insertions(+), 57 deletions(-) delete mode 100755 integrations/promunifi/scripts/local_uninstall.sh diff --git a/integrations/promunifi/Makefile b/integrations/promunifi/Makefile index 0652c6af..1eb693f8 100644 --- a/integrations/promunifi/Makefile +++ b/integrations/promunifi/Makefile @@ -9,7 +9,8 @@ ITERATION:=$(shell git rev-list --all --count) all: man build # Prepare a release. Called in Travis CI. -release: clean test rpm deb osxpkg +release: clean test $(BINARY)-$(VERSION)-$(ITERATION).x86_64.rpm $(BINARY)_$(VERSION)-$(ITERATION)_amd64.deb $(BINARY)-$(VERSION).pkg + # Prepareing a release! mkdir -p release gzip -9 $(BINARY).linux gzip -9 $(BINARY).macos @@ -18,6 +19,7 @@ release: clean test rpm deb osxpkg # Delete all build assets. clean: + # Cleaning up. rm -f $(BINARY){.macos,.linux,.1,}{,.gz} rm -f $(BINARY){_,-}*.{deb,rpm,pkg} rm -rf package_build_* release @@ -25,8 +27,8 @@ clean: # Build a man page from a markdown file using ronn. man: $(BINARY).1.gz $(BINARY).1.gz: + # Building man page. @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 @@ -37,15 +39,17 @@ $(BINARY): linux: $(BINARY).linux $(BINARY).linux: + # Building linux binary. GOOS=linux go build -o $(BINARY).linux -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE) macos: $(BINARY).macos $(BINARY).macos: + # Building darwin binary. GOOS=darwin go build -o $(BINARY).macos -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE) # Packages -rpm: $(BINARY)-$(VERSION)-$(ITERATION).x86_64.rpm +rpm: clean $(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 \ @@ -60,7 +64,7 @@ $(BINARY)-$(VERSION)-$(ITERATION).x86_64.rpm: check_fpm package_build_linux --description $(DESC) \ --chdir package_build_linux -deb: $(BINARY)_$(VERSION)-$(ITERATION)_amd64.deb +deb: clean $(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 \ @@ -75,7 +79,7 @@ $(BINARY)_$(VERSION)-$(ITERATION)_amd64.deb: check_fpm package_build_linux --description $(DESC) \ --chdir package_build_linux -osxpkg: $(BINARY)-$(VERSION).pkg +osxpkg: clean $(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 \ @@ -92,32 +96,29 @@ $(BINARY)-$(VERSION).pkg: check_fpm 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. + # Building package environment for macOS. + mkdir -p $@/usr/local/bin $@/usr/local/etc/$(BINARY) $@/Library/LaunchAgents + mkdir -p $@/usr/local/share/man/man1 $@/usr/local/share/doc/$(BINARY) $@/usr/local/var/log + # Copying 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) + # Building package environment for linux. + mkdir -p $@/usr/bin $@/etc/$(BINARY) $@/lib/systemd/system 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) + # Copying 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%" \ + cp examples/* $@/usr/share/doc/$(BINARY)/ + # Fixing the paths in the systemd unit file before copying it into the emv. + sed "s%ExecStart.*%ExecStart=/usr/bin/$(BINARY) --config=/etc/$(BINARY)/up.conf%" \ init/systemd/unifi-poller.service > $@/lib/systemd/system/$(BINARY).service check_fpm: @@ -127,8 +128,10 @@ check_fpm: # Run code tests and lint. test: lint + # Testing. go test -race -covermode=atomic $(PACKAGE) lint: + # Checking lint. golangci-lint run --enable-all -D gochecknoglobals # Install locally into /usr/local. Not recommended. @@ -136,8 +139,20 @@ install: man scripts/local_install.sh # If you installed with `make install` run `make uninstall` before installing a binary package. +# This will remove the package install from macOS, it will not remove a package install from Linux. uninstall: - scripts/local_uninstall.sh + [ -x /bin/systemctl ] && /bin/systemctl disable $(BINARY) || true + [ -x /bin/systemctl ] && /bin/systemctl stop $(BINARY) || true + [ -x /bin/launchctl ] && [ -f ~/Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist ] \ + && /bin/launchctl unload ~/Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist || true + [ -x /bin/launchctl ] && [ -f /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist ] \ + && /bin/launchctl unload /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist || true + rm -rf /usr/local/{etc,bin}/$(BINARY) /usr/local/share/man/man1/$(BINARY).1.gz + rm -f ~/Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist + rm -f /etc/systemd/system/$(BINARY).service + [ -x /bin/systemctl ] && /bin/systemctl --system daemon-reload || true + @[ -x /bin/launchctl ] && [ -f /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist ] \ + && echo " ==> Delete this file manually: sudo rm -f /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist" || true # Don't run this unless you're ready to debug untested vendored dependencies. deps: diff --git a/integrations/promunifi/cmd/unifi-poller/README.md b/integrations/promunifi/cmd/unifi-poller/README.md index df977efb..64bce18e 100644 --- a/integrations/promunifi/cmd/unifi-poller/README.md +++ b/integrations/promunifi/cmd/unifi-poller/README.md @@ -33,7 +33,9 @@ unifi-poller(1) -- Utility to poll UniFi Controller Metrics and store them in In `sites` default: ["default"] This list of strings should represent the names of sites on the unifi controller that will be polled for data. Pass `all` in the list to - poll all sites. + poll all sites. On startup, the application prints out all site names + found in the controller; they're cryptic, but they have the human-name + next to them. The cryptic names go into the config file `sites` list. `interval` default: 30s How often to poll the controller for updated client and device data. diff --git a/integrations/promunifi/scripts/local_uninstall.sh b/integrations/promunifi/scripts/local_uninstall.sh deleted file mode 100755 index fd5abce8..00000000 --- a/integrations/promunifi/scripts/local_uninstall.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash - -# This script removes a local installation of unifi-poller. -# Recommend using Makefile to invoke: make uninstall -# Supports Linux (systemd only) and macOS. - -BINARY=unifi-poller - -echo "Uninstall unifi-poller. You may need sudo on Linux. Do not use sudo on macOS." - -# Stopping the daemon -if [ -x /bin/systemctl ]; then - /bin/systemctl disable ${BINARY} - /bin/systemctl stop ${BINARY} -fi - -if [ -x /bin/launchctl ] && [ -f ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist ]; then - echo Unloading ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist - /bin/launchctl unload ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist || true -fi - -if [ -x /bin/launchctl ] && [ -f /Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist ]; then - echo Unloading /Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist - /bin/launchctl unload /Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist || true - echo "Delete this file manually: sudo rm -f /Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist" -fi - -# Deleting config file, binary, man page, launch agent or unit file. -rm -rf /usr/local/{etc,bin}/${BINARY} /usr/local/share/man/man1/${BINARY}.1.gz -rm -f ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist -rm -f /etc/systemd/system/${BINARY}.service - -# Making systemd happy by telling it to reload. -if [ -x /bin/systemctl ]; then - /bin/systemctl --system daemon-reload -fi From 72506b87ec824a4f9096e5da2bb01ad72f127846 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Fri, 7 Jun 2019 15:40:05 -0700 Subject: [PATCH 06/11] Deprecate make install --- integrations/promunifi/Makefile | 25 ++++++----- .../promunifi/examples/up.conf.example | 7 ++- .../init/systemd/unifi-poller.service | 2 +- .../promunifi/scripts/before-remove.sh | 6 ++- .../promunifi/scripts/local_install.sh | 44 ------------------- 5 files changed, 22 insertions(+), 62 deletions(-) delete mode 100755 integrations/promunifi/scripts/local_install.sh diff --git a/integrations/promunifi/Makefile b/integrations/promunifi/Makefile index 1eb693f8..c2c24cf9 100644 --- a/integrations/promunifi/Makefile +++ b/integrations/promunifi/Makefile @@ -104,22 +104,20 @@ package_build_osx: man macos cp *.1.gz $@/usr/local/share/man/man1 cp examples/*.conf.example $@/usr/local/etc/$(BINARY)/ cp examples/* $@/usr/local/share/doc/$(BINARY)/ - cp init/launchd/com.github.davidnewhall.unifi-poller.plist $@/Library/LaunchAgents/ + cp init/launchd/com.github.davidnewhall.$(BINARY).plist $@/Library/LaunchAgents/ # Build an environment that can be packaged for linux. package_build_linux: man linux # Building package environment for linux. mkdir -p $@/usr/bin $@/etc/$(BINARY) $@/lib/systemd/system mkdir -p $@/usr/share/man/man1 $@/usr/share/doc/$(BINARY) - # Copying the binary, config file and man page into the env. + # Copying the binary, config file, unit 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/up.conf.example $@/etc/$(BINARY)/up.conf cp examples/* $@/usr/share/doc/$(BINARY)/ - # Fixing the paths in the systemd unit file before copying it into the emv. - sed "s%ExecStart.*%ExecStart=/usr/bin/$(BINARY) --config=/etc/$(BINARY)/up.conf%" \ - init/systemd/unifi-poller.service > $@/lib/systemd/system/$(BINARY).service + cp init/systemd/$(BINARY).service $@/lib/systemd/system/ check_fpm: @fpm --version > /dev/null || (echo "FPM missing. Install FPM: https://fpm.readthedocs.io/en/latest/installing.html" && false) @@ -134,9 +132,14 @@ lint: # Checking lint. golangci-lint run --enable-all -D gochecknoglobals -# Install locally into /usr/local. Not recommended. -install: man - scripts/local_install.sh +# Deprecated. +install: + @echo - Local installation with the Makefile is no longer possible. + @echo If you wish to install the application manually, check out the wiki: \ + https://github.com/davidnewhall/unifi-poller/wiki/Installation + @echo - Otherwise, build and install a package: make rpm, make deb, make osxpkg + @echo See the Package Install wiki for more info: \ + https://github.com/davidnewhall/unifi-poller/wiki/Package-Install # If you installed with `make install` run `make uninstall` before installing a binary package. # This will remove the package install from macOS, it will not remove a package install from Linux. @@ -147,11 +150,11 @@ uninstall: && /bin/launchctl unload ~/Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist || true [ -x /bin/launchctl ] && [ -f /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist ] \ && /bin/launchctl unload /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist || true - rm -rf /usr/local/{etc,bin}/$(BINARY) /usr/local/share/man/man1/$(BINARY).1.gz + rm -rf /usr/local/{etc,bin,share/doc}/$(BINARY) rm -f ~/Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist - rm -f /etc/systemd/system/$(BINARY).service + rm -f /etc/systemd/system/$(BINARY).service /usr/local/share/man/man1/$(BINARY).1.gz [ -x /bin/systemctl ] && /bin/systemctl --system daemon-reload || true - @[ -x /bin/launchctl ] && [ -f /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist ] \ + @[ -f /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist ] \ && echo " ==> Delete this file manually: sudo rm -f /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist" || true # Don't run this unless you're ready to debug untested vendored dependencies. diff --git a/integrations/promunifi/examples/up.conf.example b/integrations/promunifi/examples/up.conf.example index c1c13b18..16a814fe 100644 --- a/integrations/promunifi/examples/up.conf.example +++ b/integrations/promunifi/examples/up.conf.example @@ -1,7 +1,6 @@ -# unifi-poller primary configuration file. # -# copy this file to: /usr/local/etc/unifi-poller/up.conf # -# commented lines are defaults, uncomment to change. # -########################################################## +# unifi-poller primary configuration file. # +# commented lines are defaults, uncomment to change. # +###################################################### # If the controller has more than one site, specify which sites to poll here. # If only one site, "default" is likely the correct name. diff --git a/integrations/promunifi/init/systemd/unifi-poller.service b/integrations/promunifi/init/systemd/unifi-poller.service index ba7d1bd7..ab5220ad 100644 --- a/integrations/promunifi/init/systemd/unifi-poller.service +++ b/integrations/promunifi/init/systemd/unifi-poller.service @@ -4,7 +4,7 @@ After=network.target Requires=network.target [Service] -ExecStart=/usr/local/bin/unifi-poller --config=/usr/local/etc/unifi-poller/up.conf +ExecStart=/usr/bin/unifi-poller --config=/etc/unifi-poller/up.conf Restart=always StandardOutput=syslog StandardError=syslog diff --git a/integrations/promunifi/scripts/before-remove.sh b/integrations/promunifi/scripts/before-remove.sh index 36ab63fe..83ae4004 100755 --- a/integrations/promunifi/scripts/before-remove.sh +++ b/integrations/promunifi/scripts/before-remove.sh @@ -6,5 +6,7 @@ if [ "$1" = "upgrade" ] || [ "$1" = "1" ] ; then exit 0 fi -systemctl stop unifi-poller -systemctl disable unifi-poller +if [ -x "/bin/systemctl" ]; then + /bin/systemctl stop unifi-poller + /bin/systemctl disable unifi-poller +fi diff --git a/integrations/promunifi/scripts/local_install.sh b/integrations/promunifi/scripts/local_install.sh deleted file mode 100755 index 74a082b0..00000000 --- a/integrations/promunifi/scripts/local_install.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/bash - -# This script creates a local installation of unifi-poller. -# Recommend using Makefile to invoke: make install -# Supports Linux (systemd only) and macOS. - -BINARY=unifi-poller - -echo "Installing unifi-poller. If you get errors, you may need sudo." - -# Install binary. -GOBIN=/usr/local/bin go install -ldflags "-w -s" ./... - -# Making config folders and installing man page. -mkdir -p /usr/local/etc/${BINARY} /usr/local/share/man/man1 -mv *.1.gz /usr/local/share/man/man1 - -# Installing config file, man page and launch agent or systemd unit file. -if [ ! -f /usr/local/etc/${BINARY}/up.conf ]; then - cp examples/up.conf.example /usr/local/etc/${BINARY}/up.conf -fi -if [ -d ~/Library/LaunchAgents ]; then - cp init/launchd/com.github.davidnewhall.${BINARY}.plist ~/Library/LaunchAgents -fi -if [ -d /etc/systemd/system ]; then - cp init/systemd/${BINARY}.service /etc/systemd/system -fi - -# Making systemd happy by telling it to reload. -if [ -x /bin/systemctl ]; then - /bin/systemctl --system daemon-reload - /bin/systemctl start unifi-poller - /bin/systemctl enable unifi-poller -fi - -echo "Installation Complete. Edit the config file @ /usr/local/etc/${BINARY}/up.conf" -echo "Then start the daemon with:" -if [ -d ~/Library/LaunchAgents ]; then - echo " launchctl load ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist" -fi -if [ -x /bin/systemctl ]; then - echo " sudo /bin/systemctl restart ${BINARY}" -fi -echo "Examine the log file at: /usr/local/var/log/${BINARY}.log (logs may go elsewhere on linux, check syslog)" From 87c9205b589d8661ef896b1457baf90847c64d28 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Sat, 8 Jun 2019 03:22:10 -0700 Subject: [PATCH 07/11] Run as nobody on macos. --- integrations/promunifi/Makefile | 7 ++++--- ...com.github.davidnewhall.unifi-poller.plist | 8 ++++++-- .../promunifi/scripts/after-install.sh | 19 +++++++++++-------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/integrations/promunifi/Makefile b/integrations/promunifi/Makefile index c2c24cf9..27c345f3 100644 --- a/integrations/promunifi/Makefile +++ b/integrations/promunifi/Makefile @@ -98,7 +98,7 @@ $(BINARY)-$(VERSION).pkg: check_fpm package_build_osx package_build_osx: man macos # Building package environment for macOS. mkdir -p $@/usr/local/bin $@/usr/local/etc/$(BINARY) $@/Library/LaunchAgents - mkdir -p $@/usr/local/share/man/man1 $@/usr/local/share/doc/$(BINARY) $@/usr/local/var/log + mkdir -p $@/usr/local/share/man/man1 $@/usr/local/share/doc/$(BINARY) $@/usr/local/var/log/unifi-poller # Copying 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 @@ -144,6 +144,7 @@ install: # If you installed with `make install` run `make uninstall` before installing a binary package. # This will remove the package install from macOS, it will not remove a package install from Linux. uninstall: + @echo " ==> You must run make uninstall as root on Linux. Recommend not running as root on macOS." [ -x /bin/systemctl ] && /bin/systemctl disable $(BINARY) || true [ -x /bin/systemctl ] && /bin/systemctl stop $(BINARY) || true [ -x /bin/launchctl ] && [ -f ~/Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist ] \ @@ -152,10 +153,10 @@ uninstall: && /bin/launchctl unload /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist || true rm -rf /usr/local/{etc,bin,share/doc}/$(BINARY) rm -f ~/Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist + rm -f /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist || true rm -f /etc/systemd/system/$(BINARY).service /usr/local/share/man/man1/$(BINARY).1.gz [ -x /bin/systemctl ] && /bin/systemctl --system daemon-reload || true - @[ -f /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist ] \ - && echo " ==> Delete this file manually: sudo rm -f /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist" || true + @[ -f /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist ] && echo " ==> Unload and delete this file manually:" && echo " sudo launchctl unload /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist" && echo " sudo rm -f /Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist" || true # Don't run this unless you're ready to debug untested vendored dependencies. deps: diff --git a/integrations/promunifi/init/launchd/com.github.davidnewhall.unifi-poller.plist b/integrations/promunifi/init/launchd/com.github.davidnewhall.unifi-poller.plist index d8a18294..cffcd1d9 100644 --- a/integrations/promunifi/init/launchd/com.github.davidnewhall.unifi-poller.plist +++ b/integrations/promunifi/init/launchd/com.github.davidnewhall.unifi-poller.plist @@ -15,8 +15,12 @@ KeepAlive StandardErrorPath - /usr/local/var/log/unifi-poller.log + /usr/local/var/log/unifi-poller/log StandardOutPath - /usr/local/var/log/unifi-poller.log + /usr/local/var/log/unifi-poller/log + UserName + nobody + GroupName + nobody diff --git a/integrations/promunifi/scripts/after-install.sh b/integrations/promunifi/scripts/after-install.sh index 8842906b..820eccc2 100755 --- a/integrations/promunifi/scripts/after-install.sh +++ b/integrations/promunifi/scripts/after-install.sh @@ -9,18 +9,21 @@ if [ "$(uname -s)" = "Darwin" ]; then cp /usr/local/etc/unifi-poller/up.conf.example /usr/local/etc/unifi-poller/up.conf fi - # Allow admins to change the configuration and write logs. - chgrp -R admin /usr/local/etc/unifi-poller - chmod -R g+wr /usr/local/etc/unifi-poller + # Allow admins to change the configuration and delete the docs. + chgrp -R admin /usr/local/etc/unifi-poller /usr/local/share/doc/unifi-poller + chmod -R g+wr /usr/local/etc/unifi-poller /usr/local/share/doc/unifi-poller - # Make sure admins can write logs. - chgrp admin /usr/local/var/log - chmod g=rwx /usr/local/var/log + # Make sure admins can delete logs. + chown -R nobody:admin /usr/local/var/log/unifi-poller + chmod 0775 /usr/local/var/log/unifi-poller + chmod -R g+rw /usr/local/var/log/unifi-poller - # This starts it as root. no no no .... not sure how to fix that. - # launchctl load /Library/LaunchAgents/com.github.davidnewhall.unifi-poller.plist + # Restart the service - this starts the application as user nobody. + launchctl unload /Library/LaunchAgents/com.github.davidnewhall.unifi-poller.plist + launchctl load /Library/LaunchAgents/com.github.davidnewhall.unifi-poller.plist elif [ -x "/bin/systemctl" ]; then + # Reload and restart - this starts the application as user nobody. /bin/systemctl daemon-reload /bin/systemctl enable unifi-poller /bin/systemctl restart unifi-poller From 3ee0eeb17c0fd4c57f6e06de3396de523697a835 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Sun, 9 Jun 2019 20:53:40 -0700 Subject: [PATCH 08/11] fix rpm build on macOS fixes #30 --- integrations/promunifi/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/integrations/promunifi/Makefile b/integrations/promunifi/Makefile index 27c345f3..91881816 100644 --- a/integrations/promunifi/Makefile +++ b/integrations/promunifi/Makefile @@ -54,6 +54,7 @@ $(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) \ + --rpm-os linux \ --version $(VERSION) \ --iteration $(ITERATION) \ --after-install scripts/after-install.sh \ From 1aa22236230b79c65fac1afbb73b19065def94b2 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Sun, 9 Jun 2019 21:01:11 -0700 Subject: [PATCH 09/11] fix typo --- integrations/promunifi/cmd/unifi-poller/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integrations/promunifi/cmd/unifi-poller/main.go b/integrations/promunifi/cmd/unifi-poller/main.go index ca26976d..066d5e48 100644 --- a/integrations/promunifi/cmd/unifi-poller/main.go +++ b/integrations/promunifi/cmd/unifi-poller/main.go @@ -139,7 +139,7 @@ func GetConfig(configFile string) (Config, error) { // PollUnifiController runs forever, polling and pushing. func (c *Config) PollUnifiController(controller *unifi.Unifi, infdb influx.Client) { - log.Println("[INFO] Everyting checks out! Beginning Poller Routine.") + log.Println("[INFO] Everything checks out! Beginning Poller Routine.") ticker := time.NewTicker(c.Interval.value) for range ticker.C { From 9710a6cb967fd8e8440e6798a320c6bc675cf1d6 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Sun, 9 Jun 2019 21:13:50 -0700 Subject: [PATCH 10/11] only count master for iteration --- integrations/promunifi/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integrations/promunifi/Makefile b/integrations/promunifi/Makefile index 91881816..3569d2d5 100644 --- a/integrations/promunifi/Makefile +++ b/integrations/promunifi/Makefile @@ -4,7 +4,7 @@ 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) +ITERATION:=$(shell git rev-list --count master) all: man build From 6551d6eb5d3d404e37bdbb0656eef0ffd370819d Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Sun, 9 Jun 2019 21:23:51 -0700 Subject: [PATCH 11/11] Update Makefile --- integrations/promunifi/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integrations/promunifi/Makefile b/integrations/promunifi/Makefile index 3569d2d5..25f3ad59 100644 --- a/integrations/promunifi/Makefile +++ b/integrations/promunifi/Makefile @@ -4,7 +4,7 @@ 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 --count master) +ITERATION:=$(shell git rev-list --count HEAD) all: man build