diff --git a/.gitignore b/.gitignore index 63b2d1c3..9ce67d3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,18 +1,23 @@ -/up.conf +# Change this line. /unifi-poller -/unifi-poller*.gz -/unifi-poller*.txz -/unifi-poller*.zip -/unifi-poller*.1 -/unifi-poller*.deb -/unifi-poller*.rpm -/unifi-poller*.arm -/unifi-poller*.exe -/unifi-poller*.macos -/unifi-poller*.freebsd -/unifi-poller*.bsd -/unifi-poller*.linux -/unifi-poller.rb +/init/macos/*.app/Contents/MacOS/* +# The rest is probably fine. +/rsrc*.syso +/*.log +/*.conf +/*.gz +/*.zip +/*.upx +/unpackerr*.1 +/*.deb +/*.rpm +/*.txz +/*.*.arm +/*.*.exe +/*.*.macos +/*.*.linux +/*.*.freebsd +/*.rb *.sha256 /vendor .DS_Store @@ -23,11 +28,10 @@ MANUAL MANUAL.html README README.html -/unifi-poller_manual.html -/homebrew_release_repo -/.metadata.make -bitly_token -github_deploy_key -gpg.signing.key -.secret-files.tar -*.so +/*_manual.html +/homebrew-mugs +.secret*files.tar +github_deploy_key* +.metadata.make +/after-install-rendered.sh +/before-remove-rendered.sh diff --git a/.travis.yml b/.travis.yml index 9d927dbe..ecdfcdd1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,13 +15,14 @@ addons: - debsigs - gnupg - expect + - upx go: -- 1.14.x +- 1.15.x services: - docker install: - - rvm install 2.3.0 - - rvm 2.3.0 do gem install --no-document fpm + - rvm install 2.6.0 + - rvm 2.6.0 do gem install --no-document fpm # download super-linter: golangci-lint # - curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest before_script: @@ -31,15 +32,15 @@ before_script: - tar -xf .secret-files.tar - gpg --import gpg.signing.key - rm -f gpg.signing.key .secret-files.tar - - source .metadata.sh + - source settings.sh script: # Test Go and Docker. # - make test - make docker # Test built docker image. - - docker run $BINARY -v 2>&1 | grep -Eq "^$BINARY v$VERSION" + - docker run $BINARY -v 2>&1 | grep -Eq "^$BINARY, version $VERSION" # Build everything - - rvm 2.3.0 do make release + - rvm 2.6.0 do make release after_success: # Display Release Folder - ls -l release/ diff --git a/Makefile b/Makefile index 20e5122a..19db5d40 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,18 @@ # This Makefile is written as generic as possible. -# Setting the variables in .metadata.sh and creating the paths in the repo makes this work. +# Setting the variables in settings.sh and creating the paths in the repo makes this work. # See more: https://github.com/golift/application-builder # Suck in our application information. -IGNORED:=$(shell bash -c "source .metadata.sh ; env | sed 's/=/:=/;s/^/export /' > .metadata.make") +IGNORED:=$(shell bash -c "source settings.sh ; env | grep -v BASH_FUNC | sed 's/=/:=/;s/^/export /' > .metadata.make") # md2roff turns markdown into man files and html files. -MD2ROFF_BIN=github.com/github/hub/md2roff-bin +MD2ROFF_BIN=github.com/davidnewhall/md2roff + +# rsrc adds an ico file to a Windows exe file. +RSRC_BIN=github.com/akavel/rsrc + +# If upx is available, use it to compress the binaries. +UPXPATH=$(shell which upx) # Travis CI passes the version in. Local builds get it from the current git tag. ifeq ($(VERSION),) @@ -20,13 +26,15 @@ else ITERATION:=$(_ITERATION) endif + # rpm is wierd and changes - to _ in versions. RPMVERSION:=$(shell echo $(VERSION) | tr -- - _) +# used for freebsd packages. BINARYU:=$(shell echo $(BINARY) | tr -- - _) PACKAGE_SCRIPTS= ifeq ($(FORMULA),service) - PACKAGE_SCRIPTS=--after-install scripts/after-install.sh --before-remove scripts/before-remove.sh + PACKAGE_SCRIPTS=--after-install after-install-rendered.sh --before-remove before-remove-rendered.sh endif define PACKAGE_ARGS @@ -36,118 +44,159 @@ $(PACKAGE_SCRIPTS) \ --rpm-os linux \ --iteration $(ITERATION) \ --license $(LICENSE) \ ---url $(URL) \ +--url $(SOURCE_URL) \ --maintainer "$(MAINT)" \ --vendor "$(VENDOR)" \ --description "$(DESC)" \ --config-files "/etc/$(BINARY)/$(CONFIG_FILE)" \ ---freebsd-origin "$(BINARY)/$(BINARY)" +--freebsd-origin "$(SOURCE_URL)" endef PLUGINS:=$(patsubst plugins/%/main.go,%,$(wildcard plugins/*/main.go)) -VERSION_LDFLAGS:= \ - -X github.com/prometheus/common/version.Branch=$(BRANCH) \ - -X github.com/prometheus/common/version.BuildDate=$(DATE) \ - -X github.com/prometheus/common/version.Revision=$(COMMIT) \ - -X github.com/prometheus/common/version.Version=$(VERSION)-$(ITERATION) +VERSION_LDFLAGS:= -X \"$(VERSION_PATH).Branch=$(BRANCH) ($(COMMIT))\" \ + -X \"$(VERSION_PATH).BuildDate=$(DATE)\" \ + -X \"$(VERSION_PATH).BuildUser=$(shell whoami)\" \ + -X \"$(VERSION_PATH).Revision=$(ITERATION)\" \ + -X \"$(VERSION_PATH).Version=$(VERSION)\" # Makefile targets follow. all: clean build +#################### +##### Releases ##### +#################### + # Prepare a release. Called in Travis CI. -release: clean macos windows linux_packages freebsd_packages +release: clean linux_packages freebsd_packages windows # Prepareing a release! mkdir -p $@ - mv $(BINARY).*.macos $(BINARY).*.linux $(BINARY).*.freebsd $@/ + mv $(BINARY).*.linux $(BINARY).*.freebsd $@/ gzip -9r $@/ - for i in $(BINARY)*.exe; do zip -9qm $@/$$i.zip $$i;done + for i in $(BINARY)*.exe ; do zip -9qj $@/$$i.zip $$i examples/*.example *.html; rm -f $$i;done mv *.rpm *.deb *.txz $@/ # Generating File Hashes openssl dgst -r -sha256 $@/* | sed 's#release/##' | tee $@/checksums.sha256.txt +dmg: clean macapp + mkdir -p release + hdiutil create release/$(MACAPP).dmg -srcfolder $(MACAPP).app -ov + openssl dgst -r -sha256 release/* | sed 's#release/##' | tee release/dmg_checksum.sha256.txt # Delete all build assets. clean: - # Cleaning up. - rm -f $(BINARY) $(BINARY).*.{macos,freebsd,linux,exe}{,.gz,.zip} $(BINARY).1{,.gz} $(BINARY).rb + rm -f $(BINARY) $(BINARY).*.{macos,freebsd,linux,exe,upx}{,.gz,.zip} $(BINARY).1{,.gz} $(BINARY).rb rm -f $(BINARY){_,-}*.{deb,rpm,txz} v*.tar.gz.sha256 examples/MANUAL .metadata.make - rm -f cmd/$(BINARY)/README{,.html} README{,.html} ./$(BINARY)_manual.html - rm -rf package_build_* release + rm -f cmd/$(BINARY)/README{,.html} README{,.html} ./$(BINARY)_manual.html rsrc.syso $(MACAPP).app.zip + rm -rf package_build_* release after-install-rendered.sh before-remove-rendered.sh $(MACAPP).app + +#################### +##### Sidecars ##### +#################### # Build a man page from a markdown file using md2roff. # This also turns the repo readme into an html file. # md2roff is needed to build the man file and html pages from the READMEs. man: $(BINARY).1.gz -$(BINARY).1.gz: +$(BINARY).1.gz: md2roff # Building man page. Build dependency first: md2roff - go run $(MD2ROFF_BIN) --manual $(BINARY) --version $(VERSION) --date "$(DATE)" examples/MANUAL.md + $(shell go env GOPATH)/bin/md2roff --manual $(BINARY) --version $(VERSION) --date "$(DATE)" examples/MANUAL.md gzip -9nc examples/MANUAL > $@ mv examples/MANUAL.html $(BINARY)_manual.html +md2roff: $(shell go env GOPATH)/bin/md2roff +$(shell go env GOPATH)/bin/md2roff: + cd /tmp ; go get $(MD2ROFF_BIN) ; go install $(MD2ROFF_BIN) + # TODO: provide a template that adds the date to the built html file. readme: README.html -README.html: +README.html: md2roff # This turns README.md into README.html - go run $(MD2ROFF_BIN) --manual $(BINARY) --version $(VERSION) --date "$(DATE)" README.md + $(shell go env GOPATH)/bin/md2roff --manual $(BINARY) --version $(VERSION) --date "$(DATE)" README.md -# Binaries +rsrc: rsrc.syso +rsrc.syso: init/windows/application.ico init/windows/manifest.xml $(shell go env GOPATH)/bin/rsrc + $(shell go env GOPATH)/bin/rsrc -ico init/windows/application.ico -manifest init/windows/manifest.xml +$(shell go env GOPATH)/bin/rsrc: + cd /tmp ; go get $(RSRC_BIN) ; go install $(RSRC_BIN) + +#################### +##### Binaries ##### +#################### build: $(BINARY) $(BINARY): main.go - go build -o $(BINARY) -ldflags "-w -s $(VERSION_LDFLAGS)" + go build -o $(BINARY) -ldflags "-w -s $(VERSION_LDFLAGS) $(EXTRA_LDFLAGS) " + [ -z "$(UPXPATH)" ] || $(UPXPATH) -q9 $@ linux: $(BINARY).amd64.linux $(BINARY).amd64.linux: main.go # Building linux 64-bit x86 binary. - GOOS=linux GOARCH=amd64 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS)" + GOOS=linux GOARCH=amd64 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS) $(EXTRA_LDFLAGS) " + [ -z "$(UPXPATH)" ] || $(UPXPATH) -q9 $@ linux386: $(BINARY).i386.linux $(BINARY).i386.linux: main.go # Building linux 32-bit x86 binary. - GOOS=linux GOARCH=386 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS)" + GOOS=linux GOARCH=386 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS) $(EXTRA_LDFLAGS) " + [ -z "$(UPXPATH)" ] || $(UPXPATH) -q9 $@ arm: arm64 armhf arm64: $(BINARY).arm64.linux $(BINARY).arm64.linux: main.go # Building linux 64-bit ARM binary. - GOOS=linux GOARCH=arm64 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS)" + GOOS=linux GOARCH=arm64 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS) $(EXTRA_LDFLAGS) " + [ -z "$(UPXPATH)" ] || $(UPXPATH) -q9 $@ armhf: $(BINARY).armhf.linux $(BINARY).armhf.linux: main.go # Building linux 32-bit ARM binary. - GOOS=linux GOARCH=arm GOARM=6 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS)" + GOOS=linux GOARCH=arm GOARM=6 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS) $(EXTRA_LDFLAGS) " + [ -z "$(UPXPATH)" ] || $(UPXPATH) -q9 $@ macos: $(BINARY).amd64.macos $(BINARY).amd64.macos: main.go - GOOS=darwin GOARCH=amd64 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS)" + # Building darwin 64-bit x86 binary. + GOOS=darwin GOARCH=amd64 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS) $(EXTRA_LDFLAGS) " + [ -z "$(UPXPATH)" ] || $(UPXPATH) -q9 $@ freebsd: $(BINARY).amd64.freebsd $(BINARY).amd64.freebsd: main.go - GOOS=freebsd GOARCH=amd64 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS)" + GOOS=freebsd GOARCH=amd64 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS) $(EXTRA_LDFLAGS) " freebsd386: $(BINARY).i386.freebsd $(BINARY).i386.freebsd: main.go - GOOS=freebsd GOARCH=386 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS)" + GOOS=freebsd GOARCH=386 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS) $(EXTRA_LDFLAGS) " + [ -z "$(UPXPATH)" ] || $(UPXPATH) -q9 $@ || true freebsdarm: $(BINARY).armhf.freebsd $(BINARY).armhf.freebsd: main.go - GOOS=freebsd GOARCH=arm go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS)" + GOOS=freebsd GOARCH=arm go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS) $(EXTRA_LDFLAGS) " exe: $(BINARY).amd64.exe windows: $(BINARY).amd64.exe -$(BINARY).amd64.exe: main.go +$(BINARY).amd64.exe: rsrc.syso main.go # Building windows 64-bit x86 binary. - GOOS=windows GOARCH=amd64 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS)" + GOOS=windows GOARCH=amd64 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS) $(EXTRA_LDFLAGS) $(WINDOWS_LDFLAGS)" + [ -z "$(UPXPATH)" ] || $(UPXPATH) -q9 $@ -# Packages +#################### +##### Packages ##### +#################### linux_packages: rpm deb rpm386 deb386 debarm rpmarm debarmhf rpmarmhf freebsd_packages: freebsd_pkg freebsd386_pkg freebsdarm_pkg +macapp: $(MACAPP).app +$(MACAPP).app: macos + @[ "$(MACAPP)" != "" ] || (echo "Must set 'MACAPP' in settings.sh!" && exit 1) + mkdir -p init/macos/$(MACAPP).app/Contents/MacOS + cp $(BINARY).amd64.macos init/macos/$(MACAPP).app/Contents/MacOS/$(MACAPP) + cp -rp init/macos/$(MACAPP).app $(MACAPP).app + rpm: $(BINARY)-$(RPMVERSION)-$(ITERATION).x86_64.rpm $(BINARY)-$(RPMVERSION)-$(ITERATION).x86_64.rpm: package_build_linux check_fpm @echo "Building 'rpm' package for $(BINARY) version '$(RPMVERSION)-$(ITERATION)'." @@ -212,13 +261,12 @@ $(BINARY)-$(VERSION)_$(ITERATION).armhf.txz: package_build_freebsd_arm check_fpm fpm -s dir -t freebsd $(PACKAGE_ARGS) -a arm -v $(VERSION) -p $(BINARY)-$(VERSION)_$(ITERATION).armhf.txz -C $< # Build an environment that can be packaged for linux. -package_build_linux: readme man plugins_linux_amd64 linux +package_build_linux: readme man plugins_linux_amd64 after-install-rendered.sh before-remove-rendered.sh linux # Building package environment for linux. - mkdir -p $@/usr/bin $@/etc/$(BINARY) $@/usr/share/man/man1 $@/usr/share/doc/$(BINARY) $@/usr/lib/$(BINARY)/web + mkdir -p $@/usr/bin $@/etc/$(BINARY) $@/usr/share/man/man1 $@/usr/share/doc/$(BINARY) $@/usr/lib/$(BINARY) # Copying the binary, config file, unit file, and man page into the env. cp $(BINARY).amd64.linux $@/usr/bin/$(BINARY) cp *.1.gz $@/usr/share/man/man1 - cp -r init/webserver/* $@/usr/lib/$(BINARY)/web/ rm -f $@/usr/lib/$(BINARY)/*.so [ ! -f *amd64.so ] || cp *amd64.so $@/usr/lib/$(BINARY)/ cp examples/$(CONFIG_FILE).example $@/etc/$(BINARY)/ @@ -229,6 +277,12 @@ package_build_linux: readme man plugins_linux_amd64 linux sed -e "s/{{BINARY}}/$(BINARY)/g" -e "s/{{DESC}}/$(DESC)/g" \ init/systemd/template.unit.service > $@/lib/systemd/system/$(BINARY).service +after-install-rendered.sh: + sed -e "s/{{BINARY}}/$(BINARY)/g" scripts/after-install.sh > after-install-rendered.sh + +before-remove-rendered.sh: + sed -e "s/{{BINARY}}/$(BINARY)/g" scripts/before-remove.sh > before-remove-rendered.sh + package_build_linux_386: package_build_linux linux386 mkdir -p $@ cp -r $ /dev/null || (echo "FPM missing. Install FPM: https://fpm.readthedocs.io/en/latest/installing.html" && false) -docker: - docker build -f init/docker/Dockerfile \ - --build-arg "BUILD_DATE=$(DATE)" \ - --build-arg "COMMIT=$(COMMIT)" \ - --build-arg "VERSION=$(VERSION)-$(ITERATION)" \ - --build-arg "LICENSE=$(LICENSE)" \ - --build-arg "DESC=$(DESC)" \ - --build-arg "URL=$(URL)" \ - --build-arg "VENDOR=$(VENDOR)" \ - --build-arg "AUTHOR=$(MAINT)" \ - --build-arg "BINARY=$(BINARY)" \ - --build-arg "SOURCE_URL=$(SOURCE_URL)" \ - --build-arg "CONFIG_FILE=$(CONFIG_FILE)" \ - --tag $(BINARY) . - -# This builds a Homebrew formula file that can be used to install this app from source. -# The source used comes from the released version on GitHub. This will not work with local source. -# This target is used by Travis CI to update the released Forumla when a new tag is created. -formula: $(BINARY).rb -v$(VERSION).tar.gz.sha256: - # Calculate the SHA from the Github source file. - curl -sL $(URL)/archive/v$(VERSION).tar.gz | openssl dgst -r -sha256 | tee $@ -$(BINARY).rb: v$(VERSION).tar.gz.sha256 init/homebrew/$(FORMULA).rb.tmpl - # Creating formula from template using sed. - sed -e "s/{{Version}}/$(VERSION)/g" \ - -e "s/{{Iter}}/$(ITERATION)/g" \ - -e "s/{{SHA256}}/$(shell head -c64 $<)/g" \ - -e "s/{{Desc}}/$(DESC)/g" \ - -e "s%{{URL}}%$(URL)%g" \ - -e "s%{{SOURCE_PATH}}%$(SOURCE_PATH)%g" \ - -e "s%{{SOURCE_URL}}%$(SOURCE_URL)%g" \ - -e "s%{{CONFIG_FILE}}%$(CONFIG_FILE)%g" \ - -e "s%{{Class}}%$(shell echo $(BINARY) | perl -pe 's/(?:\b|-)(\p{Ll})/\u$$1/g')%g" \ - init/homebrew/$(FORMULA).rb.tmpl | tee $(BINARY).rb - # That perl line turns hello-world into HelloWorld, etc. +################## +##### Extras ##### +################## plugins: $(patsubst %,%.so,$(PLUGINS)) $(patsubst %,%.so,$(PLUGINS)): @@ -324,44 +345,84 @@ plugins_darwin: $(patsubst %,%.darwin.so,$(PLUGINS)) $(patsubst %,%.darwin.so,$(PLUGINS)): GOOS=darwin go build -o $@ -ldflags "$(VERSION_LDFLAGS)" -buildmode=plugin ./plugins/$(patsubst %.darwin.so,%,$@) -# Extras - # Run code tests and lint. -test: +test: lint # Testing. go test -race -covermode=atomic ./... +lint: # Checking lint. - golangci-lint run $(GOLANGCI_LINT_ARGS) + GOOS=linux $(shell go env GOPATH)/bin/golangci-lint run $(GOLANGCI_LINT_ARGS) + GOOS=freebsd $(shell go env GOPATH)/bin/golangci-lint run $(GOLANGCI_LINT_ARGS) + GOOS=windows $(shell go env GOPATH)/bin/golangci-lint run $(GOLANGCI_LINT_ARGS) +# Mockgen and bindata are examples. +# Your `go generate` may require other tools; add them! -# Don't run this unless you're ready to debug untested vendored dependencies. -deps: - go get -u github.com/unifi-poller/unifi - go get -u github.com/unifi-poller/influxunifi - go get -u github.com/unifi-poller/promunifi - go get -u github.com/unifi-poller/lokiunifi - go get -u github.com/unifi-poller/webserver - go get -u github.com/unifi-poller/inputunifi - go get -u github.com/unifi-poller/poller +mockgen: $(shell go env GOPATH)/bin/mockgen +$(shell go env GOPATH)/bin/mockgen: + cd /tmp ; go get github.com/golang/mock/mockgen ; go install github.com/golang/mock/mockgen +bindata: $(shell go env GOPATH)/bin/go-bindata +$(shell go env GOPATH)/bin/go-bindata: + cd /tmp ; go get -u github.com/go-bindata/go-bindata/... ; go install github.com/go-bindata/go-bindata -# Homebrew stuff. macOS only. +generate: mockgen bindata + go generate ./... + +################## +##### Docker ##### +################## + +docker: + docker build -f init/docker/Dockerfile \ + --build-arg "BUILD_DATE=$(DATE)" \ + --build-arg "COMMIT=$(COMMIT)" \ + --build-arg "VERSION=$(VERSION)-$(ITERATION)" \ + --build-arg "LICENSE=$(LICENSE)" \ + --build-arg "DESC=$(DESC)" \ + --build-arg "VENDOR=$(VENDOR)" \ + --build-arg "AUTHOR=$(MAINT)" \ + --build-arg "BINARY=$(BINARY)" \ + --build-arg "SOURCE_URL=$(SOURCE_URL)" \ + --build-arg "CONFIG_FILE=$(CONFIG_FILE)" \ + --tag $(BINARY) . + +#################### +##### Homebrew ##### +#################### + +# This builds a Homebrew formula file that can be used to install this app from source. +# The source used comes from the released version on GitHub. This will not work with local source. +# This target is used by Travis CI to update the released Forumla when a new tag is created. +formula: $(BINARY).rb +v$(VERSION).tar.gz.sha256: + # Calculate the SHA from the Github source file. + curl -sL $(SOURCE_URL)/archive/v$(VERSION).tar.gz | openssl dgst -r -sha256 | tee $@ +$(BINARY).rb: v$(VERSION).tar.gz.sha256 init/homebrew/$(FORMULA).rb.tmpl + # Creating formula from template using sed. + sed -e "s/{{Version}}/$(VERSION)/g" \ + -e "s/{{Iter}}/$(ITERATION)/g" \ + -e "s/{{SHA256}}/$(shell head -c64 $<)/g" \ + -e "s/{{Desc}}/$(DESC)/g" \ + -e "s%{{SOURCE_URL}}%$(SOURCE_URL)%g" \ + -e "s%{{SOURCE_PATH}}%$(SOURCE_PATH)%g" \ + -e "s%{{CONFIG_FILE}}%$(CONFIG_FILE)%g" \ + -e "s%{{Class}}%$(shell echo $(BINARY) | perl -pe 's/(?:\b|-)(\p{Ll})/\u$$1/g')%g" \ + init/homebrew/$(FORMULA).rb.tmpl | tee $(BINARY).rb + # That perl line turns hello-world into HelloWorld, etc. # Used for Homebrew only. Other distros can create packages. install: man readme $(BINARY) plugins_darwin - @echo - Done Building! - + @echo - Done Building - @echo - Local installation with the Makefile is only supported on macOS. - @echo If you wish to install the application manually on Linux, check out the wiki: https://$(SOURCE_URL)/wiki/Installation @echo - Otherwise, build and install a package: make rpm -or- make deb - @echo See the Package Install wiki for more info: https://$(SOURCE_URL)/wiki/Package-Install @[ "$(shell uname)" = "Darwin" ] || (echo "Unable to continue, not a Mac." && false) @[ "$(PREFIX)" != "" ] || (echo "Unable to continue, PREFIX not set. Use: make install PREFIX=/usr/local ETC=/usr/local/etc" && false) @[ "$(ETC)" != "" ] || (echo "Unable to continue, ETC not set. Use: make install PREFIX=/usr/local ETC=/usr/local/etc" && false) # Copying the binary, config file, unit file, and man page into the env. - /usr/bin/install -m 0755 -d $(PREFIX)/bin $(PREFIX)/share/man/man1 $(ETC)/$(BINARY) $(PREFIX)/share/doc/$(BINARY) $(PREFIX)/lib/$(BINARY)/web + /usr/bin/install -m 0755 -d $(PREFIX)/bin $(PREFIX)/share/man/man1 $(ETC)/$(BINARY) $(PREFIX)/share/doc/$(BINARY) $(PREFIX)/lib/$(BINARY) /usr/bin/install -m 0755 -cp $(BINARY) $(PREFIX)/bin/$(BINARY) - /usr/bin/install -m 0644 -cp $(BINARY).1.gz $(PREFIX)/share/man/man1/ + /usr/bin/install -m 0644 -cp $(BINARY).1.gz $(PREFIX)/share/man/man1 /usr/bin/install -m 0644 -cp examples/$(CONFIG_FILE).example $(ETC)/$(BINARY)/ - /usr/bin/install -m 0644 -cp init/webserver/* $(PREFIX)/lib/$(BINARY)/web/ [ -f $(ETC)/$(BINARY)/$(CONFIG_FILE) ] || /usr/bin/install -m 0644 -cp examples/$(CONFIG_FILE).example $(ETC)/$(BINARY)/$(CONFIG_FILE) /usr/bin/install -m 0644 -cp LICENSE *.html examples/* $(PREFIX)/share/doc/$(BINARY)/ diff --git a/examples/up.conf.example b/examples/up.conf.example index 4f3bef3d..850d5da8 100644 --- a/examples/up.conf.example +++ b/examples/up.conf.example @@ -117,7 +117,7 @@ # Password for UniFi controller user (above). # If the password provided here begins with file:// then the password is read in from # the file path that follows the file:// prefix. ex: file:///etc/unifi/password.file - # ex: file:///etc/unifi/passwd.file + # ex: file:///etc/unifi/passwd.file, windows: file://C:\\UserData\\Unifi\\Passwd.txt pass = "unifipoller" # If the controller has more than one site, specify which sites to poll here. @@ -127,7 +127,7 @@ # Enable collection of site data. This data powers the Network Sites dashboard. # It's not valuable to everyone and setting this to false will save resources. - #save_sites = true + save_sites = true # Hash, with md5, client names and MAC addresses. This attempts to protect # personally identifiable information. Most users won't want to enable this. diff --git a/init/bsd/freebsd.rc.d b/init/bsd/freebsd.rc.d index acfc6990..13eda4af 100755 --- a/init/bsd/freebsd.rc.d +++ b/init/bsd/freebsd.rc.d @@ -12,7 +12,7 @@ name="{{BINARYU}}" real_name="{{BINARY}}" rcvar="{{BINARYU}}_enable" {{BINARYU}}_command="/usr/local/bin/${real_name}" -{{BINARYU}}_user="nobody" +{{BINARYU}}_user="{{BINARY}}" {{BINARYU}}_config="/usr/local/etc/${real_name}/{{CONFIG_FILE}}" pidfile="/var/run/${real_name}/pid" diff --git a/init/buildinfo.sh b/init/buildinfo.sh new file mode 100644 index 00000000..465618ec --- /dev/null +++ b/init/buildinfo.sh @@ -0,0 +1,22 @@ +# This file is read in by settings.sh. +# These values are not generally user configurable and this file is overwritten on upgrades. +# Override values in here by setting them in settings.sh; do not change this file. +########## + +VENDOR="Go Lift " + +# Dynamic. Recommend not changing. +VVERSION=$(git describe --abbrev=0 --tags $(git rev-list --tags --max-count=1) 2>/dev/null) +VERSION="$(echo $VVERSION | tr -d v | grep -E '^\S+$' || echo development)" +# This produces a 0 in some envirnoments (like Homebrew), but it's only used for packages. +ITERATION=$(git rev-list --count --all || echo 0) +DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)" +COMMIT="$(git rev-parse --short HEAD || echo 0)" + +GIT_BRANCH="$(git rev-parse --abbrev-ref HEAD || echo unknown)" +BRANCH="${TRAVIS_BRANCH:-${GIT_BRANCH}}" + +# Defines docker manifest/build types. +BUILDS="linux:armhf:arm linux:arm64:arm64 linux:amd64:amd64 linux:i386:386" + +export VENDOR VVERSION VERSION ITERATION DATE BRANCH COMMIT BUILDS diff --git a/init/docker/Dockerfile b/init/docker/Dockerfile index 48bd8749..999ebf08 100644 --- a/init/docker/Dockerfile +++ b/init/docker/Dockerfile @@ -10,7 +10,7 @@ ARG COMMIT=0 ARG VERSION=unknown ARG BINARY=application-builder -FROM golang:stretch as builder +FROM golang:latest as builder ARG ARCH ARG OS ARG BINARY @@ -19,7 +19,9 @@ RUN mkdir -p $GOPATH/pkg/mod $GOPATH/bin $GOPATH/src /${BINARY} COPY . /${BINARY} WORKDIR /${BINARY} -RUN CGO_ENABLED=0 make ${BINARY}.${ARCH}.${OS} +RUN apt update && \ + apt install -y upx && \ + CGO_ENABLED=0 make ${BINARY}.${ARCH}.${OS} FROM scratch ARG ARCH @@ -30,7 +32,6 @@ ARG VERSION ARG LICENSE=MIT ARG BINARY ARG SOURCE_URL=http://github.com/golift/application-builder -ARG URL=http://github.com/golift/application-builder ARG DESC=application-builder ARG VENDOR=golift ARG AUTHOR=golift @@ -41,7 +42,7 @@ LABEL org.opencontainers.image.created="${BUILD_DATE}" \ org.opencontainers.image.title="${BINARY}" \ org.opencontainers.image.documentation="${SOURCE_URL}/wiki/Docker" \ org.opencontainers.image.description="${DESC}" \ - org.opencontainers.image.url="${URL}" \ + org.opencontainers.image.url="${SOURCE_URL}" \ org.opencontainers.image.revision="${COMMIT}" \ org.opencontainers.image.source="${SOURCE_URL}" \ org.opencontainers.image.vendor="${VENDOR}" \ diff --git a/init/docker/hooks/README.md b/init/docker/hooks/README.md deleted file mode 100644 index f01d91f7..00000000 --- a/init/docker/hooks/README.md +++ /dev/null @@ -1,28 +0,0 @@ -# Application Builder - -[https://github.com/golift/application-builder](https://github.com/golift/application-builder) - -## Docker Build Hooks - -The files in this folder are used by Docker Cloud to automate image builds. -Do not edit these files. - -If you want to build, maintain and push multi-architecture Docker images, you may -follow the example provided here. All of the hooks are generic, and will work with -any build. Two environment variables must be passed in from Docker Cloud config. - -1. `BUILDS` must be set to the builds you're trying to perform. This repo is currently set to: - - `linux:armhf:arm linux:arm64:arm64 linux:amd64:amd64 linux:i386:386` - - The format is `os:name:arch`. - - `os` and `name` are passed into the Dockerfile. - - `os`, `arch` are passed into `docker manifest annotate`. - - This does not yet work with an OS other than `linux`. -1. Set `DOCKER_CLI_EXPERIMENTAL` to `enabled`. Not optional. - -Keep the build simple; see screenshot. This only supports one build tag, but it creates many more. - -![UniFi Poller Docker Cloud Build Rules](https://raw.githubusercontent.com/wiki/unifi-poller/unifi-poller/images/unifi-poller-build-rules.png "UniFi Poller Docker Cloud Build Rules") - -The fancy source tag is `/^v((\d+\.\d+)(?:\.\d+)?)$/` and it allows you to capture -the minor version without patch-level in `{\2}`. I no longer use `{\2}` in my build. -[See how it works here](https://regex101.com/r/fzt6ki/1). diff --git a/init/docker/hooks/build b/init/docker/hooks/build index 2fcdfc4d..85bc7125 100755 --- a/init/docker/hooks/build +++ b/init/docker/hooks/build @@ -9,7 +9,7 @@ set -e -o pipefail # This always run local to the Dockerfile folder, so the path is ../.. pushd ../.. -source .metadata.sh +source settings.sh # Build each configured image from Docker Cloud. for build in $BUILDS; do @@ -24,7 +24,6 @@ for build in $BUILDS; do --build-arg "VERSION=${VERSION}-${ITERATION}" \ --build-arg "LICENSE=${LICENSE}" \ --build-arg "DESC=${DESC}" \ - --build-arg "URL=${URL}" \ --build-arg "VENDOR=${VENDOR}" \ --build-arg "AUTHOR=${MAINT}" \ --build-arg "BINARY=${BINARY}" \ diff --git a/init/docker/hooks/push b/init/docker/hooks/push index f3f695da..26f7c907 100755 --- a/init/docker/hooks/push +++ b/init/docker/hooks/push @@ -4,13 +4,11 @@ # This is part of Application Builder. # https://github.com/golift/application-builder -#set -e -o pipefail - pushd ../.. -source .metadata.sh +source settings.sh popd -if [ "$TAGS" == "" ]; then +if [ "$BUILDS" != "" ]; then TAGS=$DOCKER_TAG fi @@ -26,14 +24,14 @@ for build in $BUILDS; do done echo "Annotating Images: ${IMAGES}" -# Build all the Docker tags if the source branch is a release and not a branch. +# Build all the Docker tags if the source branch is a release and not a branch or pre-release. if [ "v$VERSION" = "$SOURCE_BRANCH" ]; then - TAGS="$VERSION $(echo $VERSION | cut -d. -f1,2) $(echo $VERSION | cut -d. -f1)" + TAGS="$VERSION" echo $SOURCE_BRANCH | grep -q -- - if [ "$?" = "1" ]; then # tag does not contain a dash, so assume it's a prod tag. - TAGS="$TAGS latest stable" + TAGS="$TAGS latest stable $(echo $VERSION | cut -d. -f1,2) $(echo $VERSION | cut -d. -f1)" fi fi @@ -43,7 +41,7 @@ echo "Version: $VERSION, Source: $SOURCE_BRANCH, Building tags: ${TAGS}" for tag in $TAGS; do docker manifest create --amend ${DOCKER_REPO}:${tag} $IMAGES for build in $BUILDS; do - # os:name:arch:variant, ie linux:amd64:amd64: (no variant is ok) + # os:name:arch, ie linux:amd64:amd64 os=$(echo $build | cut -d: -f1) name=$(echo $build | cut -d: -f2) arch=$(echo $build | cut -d: -f3) diff --git a/init/homebrew/service.rb.tmpl b/init/homebrew/service.rb.tmpl index 00921281..3e4c588e 100644 --- a/init/homebrew/service.rb.tmpl +++ b/init/homebrew/service.rb.tmpl @@ -1,6 +1,7 @@ -# macOS Homebrew Formula Template. Built by Makefile: `make fomula` +# Homebrew Formula Template. Built by Makefile: `make fomula` # This is part of Application Builder. # https://github.com/golift/application-builder +# This file is used when FORMULA is set to 'service'. class {{Class}} < Formula desc "{{Desc}}" homepage "{{URL}}" @@ -9,6 +10,7 @@ class {{Class}} < Formula head "{{SOURCE_URL}}" depends_on "go" => :build + depends_on "upx" => :build def install bin_path = buildpath/"#{name}" diff --git a/init/launchd/README.md b/init/launchd/README.md deleted file mode 100644 index d5a6e8ae..00000000 --- a/init/launchd/README.md +++ /dev/null @@ -1,2 +0,0 @@ -This file isn't used by the build or for any packages. The homebrew launchd is -in the [homebrew](../homebrew) folder. This macOS launchd file is for reference only. diff --git a/init/launchd/com.github.davidnewhall.unifi-poller.plist b/init/launchd/com.github.davidnewhall.unifi-poller.plist deleted file mode 100644 index c5370da0..00000000 --- a/init/launchd/com.github.davidnewhall.unifi-poller.plist +++ /dev/null @@ -1,22 +0,0 @@ - - - - - Label - com.github.unifi-poller.unifi-poller - ProgramArguments - - /usr/local/bin/unifi-poller - -c - /usr/local/etc/unifi-poller/up.conf - - RunAtLoad - - KeepAlive - - StandardErrorPath - /usr/local/var/log/unifi-poller.log - StandardOutPath - /usr/local/var/log/unifi-poller.log - - diff --git a/init/systemd/template.unit.service b/init/systemd/template.unit.service index 15ae80c8..0612b55f 100644 --- a/init/systemd/template.unit.service +++ b/init/systemd/template.unit.service @@ -1,6 +1,7 @@ -# Linux Systemd service unit for {{BINARY}}. +# Systemd service unit for {{BINARY}}. # This is part of Application Builder. # https://github.com/golift/application-builder +# This file is installed when FORMULA is set to 'service'. [Unit] Description={{BINARY}} - {{DESC}} @@ -17,7 +18,12 @@ StandardOutput=syslog StandardError=syslog SyslogIdentifier={{BINARY}} Type=simple -User=nobody +WorkingDirectory=/tmp + +# These should be set correctly for your environment. +UMask=0002 +User={{BINARY}} +Group={{BINARY}} [Install] WantedBy=multi-user.target diff --git a/init/windows/application.ico b/init/windows/application.ico new file mode 100644 index 00000000..a3e38bae Binary files /dev/null and b/init/windows/application.ico differ diff --git a/init/windows/manifest.xml b/init/windows/manifest.xml new file mode 100644 index 00000000..9a97fd5f --- /dev/null +++ b/init/windows/manifest.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + PerMonitorV2, PerMonitor + True + + + + + + + + + + + + + + + + + diff --git a/scripts/after-install.sh b/scripts/after-install.sh old mode 100755 new mode 100644 index a465fdf4..d06888c9 --- a/scripts/after-install.sh +++ b/scripts/after-install.sh @@ -1,14 +1,29 @@ #!/bin/sh -# This file is used by txz, deb and rpm packages. -# FPM adds this as the after-install script to all packages. +# This file is used by deb, rpm and BSD packages. +# FPM adds this as the after-install script. +# Edit this file as needed for your application. +# This file is only installed if FORMULA is set to service. + +OS="$(uname -s)" + +if [ "${OS}" = "Linux" ]; then + # Make a user and group for this app, but only if it does not already exist. + id {{BINARY}} >/dev/null 2>&1 || \ + useradd --system --user-group --no-create-home --home-dir /tmp --shell /bin/false {{BINARY}} +elif [ "${OS}" = "OpenBSD" ]; then + id {{BINARY}} >/dev/null 2>&1 || \ + useradd -g =uid -d /tmp -s /bin/false {{BINARY}} +elif [ "${OS}" = "FreeBSD" ]; then + id {{BINARY}} >/dev/null 2>&1 || \ + pw useradd {{BINARY}} -d /tmp -w no -s /bin/false +else + echo "Unknown OS: ${OS}, please add system user {{BINARY}} manually." +fi if [ -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 -elif [ -x /usr/sbin/service ]; then - # Do not start or restart on freebsd. That's "bad practice." - /usr/sbin/service unifi-poller enabled || /usr/sbin/service unifi-poller enable + /bin/systemctl enable {{BINARY}} + /bin/systemctl restart {{BINARY}} fi diff --git a/scripts/before-remove.sh b/scripts/before-remove.sh old mode 100755 new mode 100644 index a6f2fce3..589bf0bb --- a/scripts/before-remove.sh +++ b/scripts/before-remove.sh @@ -1,15 +1,14 @@ -#!/bin/sh +#!/bin/bash -# This file is used by txz, rpm and deb packages. FPM use. +# This file is used by rpm and deb packages. FPM use. +# Edit this file as needed for your application. +# This file is only installed if FORMULA is set to service. if [ "$1" = "upgrade" ] || [ "$1" = "1" ] ; then exit 0 fi if [ -x "/bin/systemctl" ]; then - /bin/systemctl stop unifi-poller - /bin/systemctl disable unifi-poller -elif [ -x /usr/sbin/service ]; then - /usr/sbin/service unifi-poller stop - /usr/sbin/service unifi-poller disable + /bin/systemctl stop {{BINARY}} + /bin/systemctl disable {{BINARY}} fi diff --git a/scripts/formula-deploy.sh b/scripts/formula-deploy.sh old mode 100755 new mode 100644 index b93d92e5..22b6f579 --- a/scripts/formula-deploy.sh +++ b/scripts/formula-deploy.sh @@ -3,10 +3,9 @@ # Deploys a new homebrew formula file to a github homebrew formula repo: $HBREPO # Requires SSH credentials in ssh-agent to work. # Run by Travis-CI when a new release is created on GitHub. -# Do not edit this file. It's part of application-builder. -# https://github.com/golift/application-builder +# Do not edit this file. -source .metadata.sh +source settings.sh make ${BINARY}.rb @@ -16,6 +15,23 @@ git config --global user.name "${BINARY}-auto-releaser" rm -rf homebrew_release_repo git clone git@github.com:${HBREPO}.git homebrew_release_repo +# If a bitly token file exists, we'll use that to shorten the link (and allow download counting). +if [ -f "bitly_token" ]; then + API=https://api-ssl.bitly.com/v4/bitlinks + # Request payload. In single quotes with double quotes escaped. :see_no_evil: + JSON='{\"domain\": \"bit.ly\",\"title\": \"${BINARY}.v${VERSION}-${ITERATION}.tgz\", \ + \"tags\": [\"${BINARY}\"], \"long_url\": \"${SOURCE_PATH}\"}' + # Request with headers and data. Using bash -c to hide token from bash -x in travis logs. + OUT=$(bash -c "curl -s -X POST -H 'Content-type: application/json' ${API} -H \"\$( ${BINARY}.rb.new + if [ "$?" = "0" ] && [ "$LINK" != "null?v=v${VERSION}" ] && [ "$LINK" != "?v=v${VERSION}" ]; then + mv ${BINARY}.rb.new ${BINARY}.rb + fi +fi + cp ${BINARY}.rb homebrew_release_repo/Formula pushd homebrew_release_repo git add Formula/${BINARY}.rb diff --git a/scripts/install.sh b/scripts/install.sh old mode 100755 new mode 100644 index 03a65226..9337b922 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -1,11 +1,11 @@ -#!/bin/sh - -# This is a quick and dirty script to install the latest Linux package. +#!/bin/bash +# +# This is a quick and dirty script to install the latest package. # # Use it like this, pick curl or wget: (sudo is optional) # ---- -# curl -s https://raw.githubusercontent.com/unifi-poller/unifi-poller/master/scripts/install.sh | sudo sh -# wget -qO- https://raw.githubusercontent.com/unifi-poller/unifi-poller/master/scripts/install.sh | sudo sh +# curl -sL https://raw.githubusercontent.com/unifi-poller/unifi-poller/main/scripts/install.sh | sudo bash +# wget -qO- https://raw.githubusercontent.com/unifi-poller/unifi-poller/main/scripts/install.sh | sudo bash # ---- # # - If you're on RedHat/CentOS/Fedora, installs the latest rpm package. @@ -15,15 +15,18 @@ # This is part of application-builder. # https://github.com/golift/application-builder +# Set the repo name correctly. REPO=unifi-poller/unifi-poller BREW=golift/mugs/unifi-poller + +# Nothing else needs to be changed. Unless you're fixing things! + LATEST=https://api.github.com/repos/${REPO}/releases/latest ISSUES=https://github.com/${REPO}/issues/new ARCH=$(uname -m) OS=$(uname -s) P=" ==>" -# Nothing else needs to be changed. Unless you're fixing things! echo "<-------------------------------------------------->" if [ "$OS" = "Darwin" ]; then @@ -108,7 +111,7 @@ INSTALLER="rpm -Uvh" if [ "$FILE" = "deb" ]; then INSTALLER="dpkg --force-confdef --force-confold --install" elif [ "$FILE" = "txz" ]; then - INSTALLER="pkg install" + INSTALLER="pkg install --yes" fi FILE=$(basename ${URL}) diff --git a/.metadata.sh b/settings.sh similarity index 53% rename from .metadata.sh rename to settings.sh index de3e34bc..1e8f264f 100755 --- a/.metadata.sh +++ b/settings.sh @@ -2,6 +2,9 @@ # This file is parsed and sourced by the Makefile, Docker and Homebrew builds. # Powered by Application Builder: https://github.com/golift/application-builder +# Bring in dynamic repo/pull/source info. +source $(dirname "${BASH_SOURCE[0]}")/init/buildinfo.sh + # Must match the repo name. BINARY="unifi-poller" # Github repo containing homebrew formula repo. @@ -17,32 +20,13 @@ LICENSE="MIT" # This affects the homebrew formula (launchd) and linux packages (systemd). FORMULA="service" -# Defines docker manifest/build types. -BUILDS="linux:armhf:arm linux:arm64:arm64 linux:amd64:amd64 linux:i386:386" - -export BINARY HBREPO MAINT VENDOR DESC GOLANGCI_LINT_ARGS CONFIG_FILE LICENSE FORMULA BUILDS - -# The rest is mostly automatic. -# Fix the repo if it doesn't match the binary name. -# Provide a better URL if one exists. - # Used for source links and wiki links. SOURCE_URL="https://github.com/${BINARY}/${BINARY}" -# Used for documentation links. -URL="${SOURCE_URL}" -# Dynamic. Recommend not changing. -VVERSION=$(git describe --abbrev=0 --tags $(git rev-list --tags --max-count=1)) -VERSION="$(echo $VVERSION | tr -d v | grep -E '^\S+$' || echo development)" -# This produces a 0 in some envirnoments (like Homebrew), but it's only used for packages. -ITERATION=$(git rev-list --count --all || echo 0) -DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)" -COMMIT="$(git rev-parse --short HEAD || echo 0)" - -GIT_BRANCH="$(git rev-parse --abbrev-ref HEAD || echo unknown)" -BRANCH="${TRAVIS_BRANCH:-${GIT_BRANCH}}" +VERSION_PATH="golift.io/version" # This is a custom download path for homebrew formula. SOURCE_PATH=https://golift.io/${BINARY}/archive/v${VERSION}.tar.gz -export SOURCE_URL URL VVERSION VERSION ITERATION DATE BRANCH COMMIT SOURCE_PATH +export BINARY HBREPO MAINT VENDOR DESC GOLANGCI_LINT_ARGS CONFIG_FILE +export LICENSE FORMULA SOURCE_URL VERSION_PATH SOURCE_PATH