From da5d7163cbf894cca2ac2499c31149544d08cb28 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Thu, 30 May 2019 19:14:05 -0700 Subject: [PATCH 1/6] Update build procedures. Add rpm and deb package scripts. --- .gitignore | 3 + Makefile | 53 ++++++------------ README.md | 16 +----- examples/README.md | 8 +++ .../unifi-clients-dashboard.png | Bin .../unifi-clients-grafana-dash.json | 0 {images => examples}/unifi-uap-dashboard.png | Bin .../unifi-uap-grafana-dash.json | 0 .../unifi-usg-grafana-dash.json | 0 up.conf.example => examples/up.conf.example | 0 grafana-dashboards/README.md | 5 -- ...com.github.davidnewhall.unifi-poller.plist | 0 .../systemd/unifi-poller.service | 0 scripts/after-install.sh | 6 ++ scripts/before-remove.sh | 5 ++ {script => scripts}/build_manpages.sh | 1 + scripts/build_packages.sh | 39 +++++++++++++ scripts/local_install.sh | 42 ++++++++++++++ scripts/local_uninstall.sh | 27 +++++++++ 19 files changed, 150 insertions(+), 55 deletions(-) create mode 100644 examples/README.md rename {images => examples}/unifi-clients-dashboard.png (100%) rename {grafana-dashboards => examples}/unifi-clients-grafana-dash.json (100%) rename {images => examples}/unifi-uap-dashboard.png (100%) rename {grafana-dashboards => examples}/unifi-uap-grafana-dash.json (100%) rename {grafana-dashboards => examples}/unifi-usg-grafana-dash.json (100%) rename up.conf.example => examples/up.conf.example (100%) delete mode 100644 grafana-dashboards/README.md rename {startup => init}/launchd/com.github.davidnewhall.unifi-poller.plist (100%) rename {startup => init}/systemd/unifi-poller.service (100%) create mode 100755 scripts/after-install.sh create mode 100755 scripts/before-remove.sh rename {script => scripts}/build_manpages.sh (88%) create mode 100755 scripts/build_packages.sh create mode 100755 scripts/local_install.sh create mode 100755 scripts/local_uninstall.sh diff --git a/.gitignore b/.gitignore index 522d66e1..9e41de53 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ /unifi-poller /*.gz /*.1 +/*.deb +/*.rpm /vendor .DS_Store *~ +/package_build diff --git a/Makefile b/Makefile index d4fffac1..48149a89 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,11 @@ PACKAGES=`find ./cmd -mindepth 1 -maxdepth 1 -type d` BINARY=unifi-poller -all: clean man build +all: clean test man build clean: for p in $(PACKAGES); do rm -f `echo $${p}|cut -d/ -f3`{,.1,.1.gz}; done + rm -rf build unifi-poller_*.deb unifi-poller-*.rpm build: for p in $(PACKAGES); do go build -ldflags "-w -s" $${p}; done @@ -12,38 +13,6 @@ build: linux: for p in $(PACKAGES); do GOOS=linux go build -ldflags "-w -s" $${p}; done -install: man test build - @echo "If you get errors, you may need sudo." - # Install binary. 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. - test -f /usr/local/etc/$(BINARY)/up.conf || cp up.conf.example /usr/local/etc/$(BINARY)/up.conf - test -d ~/Library/LaunchAgents && cp startup/launchd/com.github.davidnewhall.$(BINARY).plist ~/Library/LaunchAgents || true - test -d /etc/systemd/system && cp startup/systemd/$(BINARY).service /etc/systemd/system || true - # Making systemd happy by telling it to reload. - test -x /bin/systemctl && /bin/systemctl --system daemon-reload || true - @echo - @echo "Installation Complete. Edit the config file @ /usr/local/etc/$(BINARY)/up.conf " - @echo "Then start the daemon with:" - @test -d ~/Library/LaunchAgents && echo " launchctl load ~/Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist" || true - @test -d /etc/systemd/system && echo " sudo /bin/systemctl start $(BINARY)" || true - @echo "Examine the log file at: /usr/local/var/log/$(BINARY).log (logs may go elsewhere on linux, check syslog)" - -uninstall: - @echo "If you get errors, you may need sudo." - # Stopping the daemon - test -x /bin/systemctl && /bin/systemctl stop $(BINARY) || true - test -x /bin/launchctl && /bin/launchctl unload ~/Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist || true - # 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. - test -x /bin/systemctl && /bin/systemctl --system daemon-reload || true - test: lint for p in $(PACKAGES) $(LIBRARYS); do go test -race -covermode=atomic $${p}; done @@ -54,8 +23,20 @@ lint: golint $(PACKAGES) go vet $(PACKAGES) -man: - script/build_manpages.sh ./ - deps: dep ensure -update + +man: + scripts/build_manpages.sh ./ + +rpm: all + scripts/build_packages.sh rpm + +deb: all + scripts/build_packages.sh deb + +install: all + scripts/local_install.sh + +uninstall: + scripts/local_uninstall.sh diff --git a/README.md b/README.md index 6621065c..6d5204b4 100644 --- a/README.md +++ b/README.md @@ -43,13 +43,6 @@ 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? -- I probably suck at InfluxDB. - -I don't know what should be a tag and what should be a field. I think -I did my best, but there's certainly room for improvements in both -the data input and the Grafana graphs (output). I'm always iterating, but -if you find a deficiency or something that can be improved, let me know. - - Are there other devices that need to be included? I have: switch, router, access point. Three total, and the type structs are @@ -64,11 +57,6 @@ Issue and lets discuss. If you're a nerd you can probably figure it out. I'd still like some pretty pictures and maybe even a Twitch VOD. -- Sanity Checking - -Did I actually graph the right data in the right way? Some validation would -be nice. - - Radios, Frequencies, Interfaces, vAPs My access points only seem to have two radios, one interface and vAP per radio. @@ -84,12 +72,12 @@ isolate why this happens. We may need to issue a reconnect and get a new cookie. Here's a picture of the Client dashboard. -![image](images/unifi-clients-dashboard.png?raw=true) +![image](examples/unifi-clients-dashboard.png?raw=true) Here's a picture of the UAP dashboard. This only shows one device, but you can select multiple to put specific stats side-by-side. -![image](images/unifi-uap-dashboard.png?raw=true) +![image](examples/unifi-uap-dashboard.png?raw=true) ## Copyright & License diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..6a66a486 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,8 @@ +# Grafana Dashboards & Examples + +This folder contains grafana dashboards to get you started with the new data pool. +Import these into Grafana to quickly visualize data from your devices. + +They may/do use a few plugins: Clock, Discrete, Singlestat, Table + +This folder also contains an example configuration file and some dashboard screenshots. diff --git a/images/unifi-clients-dashboard.png b/examples/unifi-clients-dashboard.png similarity index 100% rename from images/unifi-clients-dashboard.png rename to examples/unifi-clients-dashboard.png diff --git a/grafana-dashboards/unifi-clients-grafana-dash.json b/examples/unifi-clients-grafana-dash.json similarity index 100% rename from grafana-dashboards/unifi-clients-grafana-dash.json rename to examples/unifi-clients-grafana-dash.json diff --git a/images/unifi-uap-dashboard.png b/examples/unifi-uap-dashboard.png similarity index 100% rename from images/unifi-uap-dashboard.png rename to examples/unifi-uap-dashboard.png diff --git a/grafana-dashboards/unifi-uap-grafana-dash.json b/examples/unifi-uap-grafana-dash.json similarity index 100% rename from grafana-dashboards/unifi-uap-grafana-dash.json rename to examples/unifi-uap-grafana-dash.json diff --git a/grafana-dashboards/unifi-usg-grafana-dash.json b/examples/unifi-usg-grafana-dash.json similarity index 100% rename from grafana-dashboards/unifi-usg-grafana-dash.json rename to examples/unifi-usg-grafana-dash.json diff --git a/up.conf.example b/examples/up.conf.example similarity index 100% rename from up.conf.example rename to examples/up.conf.example diff --git a/grafana-dashboards/README.md b/grafana-dashboards/README.md deleted file mode 100644 index 565178b7..00000000 --- a/grafana-dashboards/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Grafana Dashboards - -Import these into Grafana to quickly visualize data from your devices. - -They may/do use a few plugins: Clock, Discrete, Singlestat, Table diff --git a/startup/launchd/com.github.davidnewhall.unifi-poller.plist b/init/launchd/com.github.davidnewhall.unifi-poller.plist similarity index 100% rename from startup/launchd/com.github.davidnewhall.unifi-poller.plist rename to init/launchd/com.github.davidnewhall.unifi-poller.plist diff --git a/startup/systemd/unifi-poller.service b/init/systemd/unifi-poller.service similarity index 100% rename from startup/systemd/unifi-poller.service rename to init/systemd/unifi-poller.service diff --git a/scripts/after-install.sh b/scripts/after-install.sh new file mode 100755 index 00000000..a59d5e10 --- /dev/null +++ b/scripts/after-install.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +# This file is used by rpm and deb packages. FPM use. + +systemctl daemon-reload +systemctl restart unifi-poller diff --git a/scripts/before-remove.sh b/scripts/before-remove.sh new file mode 100755 index 00000000..40ca0650 --- /dev/null +++ b/scripts/before-remove.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# This file is used by rpm and deb packages. FPM use. + +systemctl stop unifi-poller diff --git a/script/build_manpages.sh b/scripts/build_manpages.sh similarity index 88% rename from script/build_manpages.sh rename to scripts/build_manpages.sh index 19493dd9..cf223e1b 100755 --- a/script/build_manpages.sh +++ b/scripts/build_manpages.sh @@ -1,5 +1,6 @@ #!/bin/bash +# This file uses ronn to build a man page for unifi-poller. set -o pipefail OUTPUT=$1 diff --git a/scripts/build_packages.sh b/scripts/build_packages.sh new file mode 100755 index 00000000..9cfaa52a --- /dev/null +++ b/scripts/build_packages.sh @@ -0,0 +1,39 @@ +#!/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." + +# Make a build environment. +mkdir -p package_build/usr/bin package_build/etc/${BINARY} package_build/lib/systemd/system package_build/usr/share/man/man1 + +# Copy the binary, config file and man page into the env. +cp ${BINARY} package_build/usr/bin +cp *.1.gz package_build/usr/share/man/man1 +cp examples/up.conf.example package_build/etc/${BINARY}/up.conf + +# Fix 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 > package_build/lib/systemd/system/${BINARY}.service + +fpm -s dir -t ${OUTPUT} \ + -n ${BINARY} \ + -v ${VERSION} \ + --after-install scripts/after-install.sh \ + -C package_build diff --git a/scripts/local_install.sh b/scripts/local_install.sh new file mode 100755 index 00000000..05a4dbce --- /dev/null +++ b/scripts/local_install.sh @@ -0,0 +1,42 @@ +#!/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 +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 [ -d /etc/systemd/system ]; then + echo " sudo /bin/systemctl start ${BINARY}" +fi +echo "Examine the log file at: /usr/local/var/log/${BINARY}.log (logs may go elsewhere on linux, check syslog)" diff --git a/scripts/local_uninstall.sh b/scripts/local_uninstall.sh new file mode 100755 index 00000000..c814e208 --- /dev/null +++ b/scripts/local_uninstall.sh @@ -0,0 +1,27 @@ +#!/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. If you get errors, you may need sudo." + +# Stopping the daemon +if [ -x /bin/systemctl ]; then + /bin/systemctl stop ${BINARY} +fi +if [ -x /bin/launchctl ] && [ -f ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist ]; then + /bin/launchctl unload ~/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 41d2d201ac7463c0cf9478a74c64e5f790d578a9 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Thu, 30 May 2019 20:25:48 -0700 Subject: [PATCH 2/6] Build osx package too. --- .gitignore | 1 + Makefile | 38 ++++++++++-------- init/systemd/unifi-poller.service | 1 - ...ld_packages.sh => build_linux_packages.sh} | 20 ++++++---- scripts/build_osx_package.sh | 39 +++++++++++++++++++ 5 files changed, 75 insertions(+), 24 deletions(-) rename scripts/{build_packages.sh => build_linux_packages.sh} (60%) create mode 100755 scripts/build_osx_package.sh diff --git a/.gitignore b/.gitignore index 9e41de53..a8c71744 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ /*.1 /*.deb /*.rpm +/*.pkg /vendor .DS_Store *~ diff --git a/Makefile b/Makefile index 48149a89..0b560e33 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ all: clean test man build clean: for p in $(PACKAGES); do rm -f `echo $${p}|cut -d/ -f3`{,.1,.1.gz}; done - rm -rf build unifi-poller_*.deb unifi-poller-*.rpm + rm -rf build unifi-poller_*.deb unifi-poller-*.rpm unifi-poller-*.pkg build: for p in $(PACKAGES); do go build -ldflags "-w -s" $${p}; done @@ -13,9 +13,30 @@ build: linux: for p in $(PACKAGES); do GOOS=linux go build -ldflags "-w -s" $${p}; done +darwin: + for p in $(PACKAGES); do GOOS=darwin go build -ldflags "-w -s" $${p}; done + test: lint for p in $(PACKAGES) $(LIBRARYS); do go test -race -covermode=atomic $${p}; done +man: + scripts/build_manpages.sh ./ + +rpm: clean test man linux + scripts/build_linux_packages.sh rpm + +deb: clean test man linux + scripts/build_linux_packages.sh deb + +osxpkg: clean test man darwin + scripts/build_osx_package.sh + +install: all + scripts/local_install.sh + +uninstall: + scripts/local_uninstall.sh + lint: goimports -l $(PACKAGES) gofmt -l $(PACKAGES) @@ -25,18 +46,3 @@ lint: deps: dep ensure -update - -man: - scripts/build_manpages.sh ./ - -rpm: all - scripts/build_packages.sh rpm - -deb: all - scripts/build_packages.sh deb - -install: all - scripts/local_install.sh - -uninstall: - scripts/local_uninstall.sh diff --git a/init/systemd/unifi-poller.service b/init/systemd/unifi-poller.service index 4576bae8..ba7d1bd7 100644 --- a/init/systemd/unifi-poller.service +++ b/init/systemd/unifi-poller.service @@ -1,4 +1,3 @@ -# untested, feedback welcomed. [Unit] Description=Unifi Poller - Ubiquiti Metrics->InfluxDB After=network.target diff --git a/scripts/build_packages.sh b/scripts/build_linux_packages.sh similarity index 60% rename from scripts/build_packages.sh rename to scripts/build_linux_packages.sh index 9cfaa52a..18147867 100755 --- a/scripts/build_packages.sh +++ b/scripts/build_linux_packages.sh @@ -20,20 +20,26 @@ fi echo "Building '${OUTPUT}' package." +PREFIX= +BINFIX=/usr + # Make a build environment. -mkdir -p package_build/usr/bin package_build/etc/${BINARY} package_build/lib/systemd/system package_build/usr/share/man/man1 +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} package_build/usr/bin -cp *.1.gz package_build/usr/share/man/man1 -cp examples/up.conf.example package_build/etc/${BINARY}/up.conf +cp ${BINARY} package_build${BINFIX}/bin +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. -sed "s#ExecStart.*#ExecStart=/usr/bin/${BINARY} --config=/etc/${BINARY}/up.conf#" \ +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 fpm -s dir -t ${OUTPUT} \ - -n ${BINARY} \ - -v ${VERSION} \ + --name ${BINARY} \ + --version ${VERSION} \ --after-install scripts/after-install.sh \ + --before-remove scripts/before-remove.sh \ -C package_build diff --git a/scripts/build_osx_package.sh b/scripts/build_osx_package.sh new file mode 100755 index 00000000..adcbb562 --- /dev/null +++ b/scripts/build_osx_package.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +# This script builds a simple macos Installer pkg. Run by the Makefile. +# Use: `make osx` + +OUTPUT=$1 +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 'osxpkg' package." + +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 + +# Copy the binary, config file and man page into the env. +cp ${BINARY} package_build${BINFIX}/bin +cp *.1.gz package_build${BINFIX}/share/man/man1 +cp examples/up.conf.example package_build${PREFIX}/etc/${BINARY}/up.conf + +# 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 osxpkg \ + --name ${BINARY} \ + --version ${VERSION} \ + --osxpkg-identifier-prefix com.github.davidnewhall \ + -C package_build From b68c4e03ea63982d4d52c5e7548ab7e0e49bf787 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Thu, 30 May 2019 21:20:35 -0700 Subject: [PATCH 3/6] Deal with config file and logging path ownership. --- Makefile | 2 +- scripts/after-install-osx.sh | 15 +++++++++++++++ scripts/build_linux_packages.sh | 2 +- scripts/build_osx_package.sh | 6 ++++-- 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100755 scripts/after-install-osx.sh diff --git a/Makefile b/Makefile index 0b560e33..1c5e78be 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ all: clean test man build clean: for p in $(PACKAGES); do rm -f `echo $${p}|cut -d/ -f3`{,.1,.1.gz}; done - rm -rf build unifi-poller_*.deb unifi-poller-*.rpm unifi-poller-*.pkg + rm -rf package_build unifi-poller_*.deb unifi-poller-*.rpm unifi-poller-*.pkg build: for p in $(PACKAGES); do go build -ldflags "-w -s" $${p}; done diff --git a/scripts/after-install-osx.sh b/scripts/after-install-osx.sh new file mode 100755 index 00000000..28248634 --- /dev/null +++ b/scripts/after-install-osx.sh @@ -0,0 +1,15 @@ +#!/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 + +# 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/scripts/build_linux_packages.sh b/scripts/build_linux_packages.sh index 18147867..b579609c 100755 --- a/scripts/build_linux_packages.sh +++ b/scripts/build_linux_packages.sh @@ -42,4 +42,4 @@ fpm -s dir -t ${OUTPUT} \ --version ${VERSION} \ --after-install scripts/after-install.sh \ --before-remove scripts/before-remove.sh \ - -C package_build + --chdir package_build diff --git a/scripts/build_osx_package.sh b/scripts/build_osx_package.sh index adcbb562..a0af56f9 100755 --- a/scripts/build_osx_package.sh +++ b/scripts/build_osx_package.sh @@ -21,11 +21,12 @@ 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} package_build${BINFIX}/bin cp *.1.gz package_build${BINFIX}/share/man/man1 -cp examples/up.conf.example package_build${PREFIX}/etc/${BINARY}/up.conf +cp examples/up.conf.example package_build${PREFIX}/etc/${BINARY}/ # Copy in launch agent. mkdir -p package_build/Library/LaunchAgents @@ -35,5 +36,6 @@ cp init/launchd/com.github.davidnewhall.unifi-poller.plist package_build/Library fpm -s dir -t osxpkg \ --name ${BINARY} \ --version ${VERSION} \ + --after-install scripts/after-install-osx.sh \ --osxpkg-identifier-prefix com.github.davidnewhall \ - -C package_build + --chdir package_build From 4eddb527c8eafc7441ddb721537c315732018f26 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Thu, 30 May 2019 21:39:33 -0700 Subject: [PATCH 4/6] Support uninstall of pkg on os x w/ script. --- scripts/after-install-osx.sh | 4 ++++ scripts/local_uninstall.sh | 12 ++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/scripts/after-install-osx.sh b/scripts/after-install-osx.sh index 28248634..bc2ad33c 100755 --- a/scripts/after-install-osx.sh +++ b/scripts/after-install-osx.sh @@ -7,6 +7,10 @@ if [ ! -f /usr/local/etc/unifi-poller/up.conf ] && [ -f /usr/local/etc/unifi-pol 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 diff --git a/scripts/local_uninstall.sh b/scripts/local_uninstall.sh index c814e208..2c33db37 100755 --- a/scripts/local_uninstall.sh +++ b/scripts/local_uninstall.sh @@ -6,14 +6,22 @@ BINARY=unifi-poller -echo "Uninstall unifi-poller. If you get errors, you may need sudo." +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 stop ${BINARY} fi + if [ -x /bin/launchctl ] && [ -f ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist ]; then - /bin/launchctl unload ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist + 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. From cbbaaa4dccfa57d80722e8b67504e95c2177ec12 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Thu, 30 May 2019 22:24:54 -0700 Subject: [PATCH 5/6] A few tweaks. --- Makefile | 8 ++++---- scripts/after-install.sh | 1 + scripts/local_install.sh | 6 ++++-- scripts/local_uninstall.sh | 3 ++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 1c5e78be..595d9e59 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ PACKAGES=`find ./cmd -mindepth 1 -maxdepth 1 -type d` BINARY=unifi-poller -all: clean test man build +all: clean man build clean: for p in $(PACKAGES); do rm -f `echo $${p}|cut -d/ -f3`{,.1,.1.gz}; done @@ -22,13 +22,13 @@ test: lint man: scripts/build_manpages.sh ./ -rpm: clean test man linux +rpm: clean man linux scripts/build_linux_packages.sh rpm -deb: clean test man linux +deb: clean man linux scripts/build_linux_packages.sh deb -osxpkg: clean test man darwin +osxpkg: clean man darwin scripts/build_osx_package.sh install: all diff --git a/scripts/after-install.sh b/scripts/after-install.sh index a59d5e10..2fb3cd87 100755 --- a/scripts/after-install.sh +++ b/scripts/after-install.sh @@ -3,4 +3,5 @@ # This file is used by rpm and deb packages. FPM use. systemctl daemon-reload +systemctl enable unifi-poller systemctl restart unifi-poller diff --git a/scripts/local_install.sh b/scripts/local_install.sh index 05a4dbce..74a082b0 100755 --- a/scripts/local_install.sh +++ b/scripts/local_install.sh @@ -29,6 +29,8 @@ 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" @@ -36,7 +38,7 @@ echo "Then start the daemon with:" if [ -d ~/Library/LaunchAgents ]; then echo " launchctl load ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist" fi -if [ -d /etc/systemd/system ]; then - echo " sudo /bin/systemctl start ${BINARY}" +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)" diff --git a/scripts/local_uninstall.sh b/scripts/local_uninstall.sh index 2c33db37..fd5abce8 100755 --- a/scripts/local_uninstall.sh +++ b/scripts/local_uninstall.sh @@ -10,7 +10,8 @@ echo "Uninstall unifi-poller. You may need sudo on Linux. Do not use sudo on mac # Stopping the daemon if [ -x /bin/systemctl ]; then - /bin/systemctl stop ${BINARY} + /bin/systemctl disable ${BINARY} + /bin/systemctl stop ${BINARY} fi if [ -x /bin/launchctl ] && [ -f ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist ]; then From 814745bc34e40aacc1fe0eb33eb4e708027e0ad0 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Thu, 30 May 2019 23:01:06 -0700 Subject: [PATCH 6/6] disable service on remove --- scripts/before-remove.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/before-remove.sh b/scripts/before-remove.sh index 40ca0650..a1d4895e 100755 --- a/scripts/before-remove.sh +++ b/scripts/before-remove.sh @@ -3,3 +3,4 @@ # This file is used by rpm and deb packages. FPM use. systemctl stop unifi-poller +systemctl disable unifi-poller