diff --git a/.metadata.sh b/.metadata.sh index 2c375d2d..bc4a04c8 100755 --- a/.metadata.sh +++ b/.metadata.sh @@ -5,8 +5,6 @@ BINARY="unifi-poller" # github username GHUSER="davidnewhall" -# docker hub username -DHUSER="golift" # Github repo containing homebrew formula repo. HBREPO="golift/homebrew-mugs" MAINT="David Newhall II " @@ -16,10 +14,15 @@ GOLANGCI_LINT_ARGS="--enable-all -D gochecknoglobals" # Example must exist at examples/$CONFIG_FILE.example CONFIG_FILE="up.conf" LICENSE="MIT" +# FORMULA is either 'service' or 'tool'. Services run as a daemon, tools do not. +# This affects the homebrew formula (launchd) and linux packages (systemd). +FORMULA="service" -export BINARY GHUSER DHUSER HBREPO MAINT VENDOR DESC GOLANGCI_LINT_ARGS CONFIG_FILE LICENSE +export BINARY GHUSER HBREPO MAINT VENDOR DESC GOLANGCI_LINT_ARGS CONFIG_FILE LICENSE FORMULA # The rest is mostly automatic. +# Fix the repo if it doesn't match the binary name. +# Provide a better URL if one exists. GHREPO="${GHUSER}/${BINARY}" URL="https://github.com/${GHREPO}" @@ -30,7 +33,7 @@ URL="https://github.com/${GHREPO}" VERSION_PATH="github.com/${GHREPO}/$(echo ${BINARY} | tr -d -- -).Version" # Dynamic. Recommend not changing. -VERSION="$(git tag -l --merged | tail -n1 | tr -d v || echo development)" +VERSION="$(git tag -l --merged | tail -n1 | 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)" diff --git a/.travis.yml b/.travis.yml index 48f30a81..4382a921 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,18 +25,24 @@ install: #- curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest - rvm install 2.0.0 - rvm 2.0.0 do gem install --no-document fpm -- make docker +before_script: + - source .metadata.sh + - make vendor script: + # Test Go and Docker. + - make test + - make docker # Test built docker image. -- docker run golift/unifi-poller:local 2>&1 | grep -Eq "Loading Configuration File[:] /etc/unifi-poller/up.conf" -# test and build everything -- rvm 2.0.0 do make release + - docker run $BINARY -v 2>&1 | grep -Eq "^$BINARY v$VERSION" + # Build everything + - rvm 2.0.0 do make release after_success: # Display Release Folder - ls -l release/ - | - declare -r SSH_FILE="$(mktemp -u $HOME/.ssh/XXXXX)" - echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config + mkdir -p $HOME/.ssh + declare -r SSH_FILE="$(mktemp -u $HOME/.ssh/XXXXX)" + echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> $HOME/.ssh/config # Get deploy key for golift/homebrew-mugs. openssl aes-256-cbc -K $encrypted_9f3147001275_key -iv $encrypted_9f3147001275_iv -in ".travis/github_deploy_key.enc" -out "$SSH_FILE" -d chmod 600 "$SSH_FILE" \ @@ -44,7 +50,7 @@ after_success: "Host github.com" \ " IdentityFile $SSH_FILE" \ " StrictHostKeyChecking no" \ - " LogLevel ERROR" >> ~/.ssh/config + " LogLevel ERROR" >> $HOME/.ssh/config deploy: - provider: releases api_key: @@ -54,14 +60,8 @@ deploy: file_glob: true file: release/* on: - repo: davidnewhall/unifi-poller tags: true - provider: script script: scripts/formula-deploy.sh on: tags: true - repo: davidnewhall/unifi-poller -env: - global: - - GH_USER_NAME=unifi-auto-releaser - - GH_USER_EMAIL=unifi@auto.releaser diff --git a/Makefile b/Makefile index 26ca8664..c6a82e75 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ -# This Makefile is written as generic as possible. +# This# This Makefile is written as generic as possible. # Setting the variables in .metadata.sh and creating the paths in the repo makes this work. # # Suck in our application information. -IGNORE := $(shell bash -c "source .metadata.sh ; env | sed 's/=/:=/;s/^/export /' > .metadata.make") +IGNORED:=$(shell bash -c "source .metadata.sh ; env | sed 's/=/:=/;s/^/export /' > .metadata.make") # md2roff turns markdown into man files and html files. MD2ROFF_BIN=github.com/github/hub/md2roff-bin @@ -23,12 +23,31 @@ endif # rpm is wierd and changes - to _ in versions. RPMVERSION:=$(shell echo $(VERSION) | tr -- - _) +PACKAGE_SCRIPTS= +ifeq ($(FORMULA),service) + PACKAGE_SCRIPTS=--after-install scripts/after-install.sh --before-remove scripts/before-remove.sh +endif + +define PACKAGE_ARGS +$(PACKAGE_SCRIPTS) \ +--name $(BINARY) \ +--deb-no-default-config-files \ +--rpm-os linux \ +--iteration $(ITERATION) \ +--license $(LICENSE) \ +--url $(URL) \ +--maintainer "$(MAINT)" \ +--vendor "$(VENDOR)" \ +--description "$(DESC)" \ +--config-files "/etc/$(BINARY)/$(CONFIG_FILE)" +endef + # Makefile targets follow. all: build # Prepare a release. Called in Travis CI. -release: clean vendor test macos arm windows linux_packages +release: clean macos arm windows linux_packages # Prepareing a release! mkdir -p $@ mv $(BINARY).*.macos $(BINARY).*.linux $@/ @@ -42,7 +61,7 @@ release: clean vendor test macos arm windows linux_packages clean: # Cleaning up. rm -f $(BINARY) $(BINARY).*.{macos,linux,exe}{,.gz,.zip} $(BINARY).1{,.gz} $(BINARY).rb - rm -f $(BINARY){_,-}*.{deb,rpm} v*.tar.gz.sha256 .metadata.make + rm -f $(BINARY){_,-}*.{deb,rpm} v*.tar.gz.sha256 examples/MANUAL .metadata.make rm -f cmd/$(BINARY)/README{,.html} README{,.html} ./$(BINARY)_manual.html rm -rf package_build_* release @@ -111,161 +130,42 @@ linux_packages: rpm deb rpm386 deb386 debarm rpmarm debarmhf rpmarmhf 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)'." - fpm -s dir -t rpm \ - --architecture x86_64 \ - --name $(BINARY) \ - --rpm-os linux \ - --version $(RPMVERSION) \ - --iteration $(ITERATION) \ - --after-install scripts/after-install.sh \ - --before-remove scripts/before-remove.sh \ - --license $(LICENSE) \ - --url $(URL) \ - --maintainer "$(MAINT)" \ - --description "$(DESC)" \ - --config-files "/etc/$(BINARY)/$(CONFIG_FILE)" \ - --chdir $< + fpm -s dir -t rpm $(PACKAGE_ARGS) -a x86_64 -v $(RPMVERSION) -C $< deb: $(BINARY)_$(VERSION)-$(ITERATION)_amd64.deb $(BINARY)_$(VERSION)-$(ITERATION)_amd64.deb: package_build_linux check_fpm @echo "Building 'deb' package for $(BINARY) version '$(VERSION)-$(ITERATION)'." - fpm -s dir -t deb \ - --architecture amd64 \ - --name $(BINARY) \ - --deb-no-default-config-files \ - --version $(VERSION) \ - --iteration $(ITERATION) \ - --after-install scripts/after-install.sh \ - --before-remove scripts/before-remove.sh \ - --license $(LICENSE) \ - --url $(URL) \ - --maintainer "$(MAINT)" \ - --description "$(DESC)" \ - --config-files "/etc/$(BINARY)/$(CONFIG_FILE)" \ - --chdir $< + fpm -s dir -t deb $(PACKAGE_ARGS) -a amd64 -v $(VERSION) -C $< rpm386: $(BINARY)-$(RPMVERSION)-$(ITERATION).i386.rpm $(BINARY)-$(RPMVERSION)-$(ITERATION).i386.rpm: package_build_linux_386 check_fpm @echo "Building 32-bit 'rpm' package for $(BINARY) version '$(RPMVERSION)-$(ITERATION)'." - fpm -s dir -t rpm \ - --architecture i386 \ - --name $(BINARY) \ - --rpm-os linux \ - --version $(RPMVERSION) \ - --iteration $(ITERATION) \ - --after-install scripts/after-install.sh \ - --before-remove scripts/before-remove.sh \ - --license $(LICENSE) \ - --url $(URL) \ - --maintainer "$(MAINT)" \ - --description "$(DESC)" \ - --config-files "/etc/$(BINARY)/$(CONFIG_FILE)" \ - --chdir $< + fpm -s dir -t rpm $(PACKAGE_ARGS) -a i386 -v $(RPMVERSION) -C $< deb386: $(BINARY)_$(VERSION)-$(ITERATION)_i386.deb $(BINARY)_$(VERSION)-$(ITERATION)_i386.deb: package_build_linux_386 check_fpm @echo "Building 32-bit 'deb' package for $(BINARY) version '$(VERSION)-$(ITERATION)'." - fpm -s dir -t deb \ - --architecture i386 \ - --name $(BINARY) \ - --deb-no-default-config-files \ - --version $(VERSION) \ - --iteration $(ITERATION) \ - --after-install scripts/after-install.sh \ - --before-remove scripts/before-remove.sh \ - --license $(LICENSE) \ - --url $(URL) \ - --maintainer "$(MAINT)" \ - --description "$(DESC)" \ - --config-files "/etc/$(BINARY)/$(CONFIG_FILE)" \ - --chdir $< + fpm -s dir -t deb $(PACKAGE_ARGS) -a i386 -v $(VERSION) -C $< rpmarm: $(BINARY)-$(RPMVERSION)-$(ITERATION).arm64.rpm $(BINARY)-$(RPMVERSION)-$(ITERATION).arm64.rpm: package_build_linux_arm64 check_fpm @echo "Building 64-bit ARM8 'rpm' package for $(BINARY) version '$(RPMVERSION)-$(ITERATION)'." - fpm -s dir -t rpm \ - --architecture arm64 \ - --name $(BINARY) \ - --rpm-os linux \ - --version $(RPMVERSION) \ - --iteration $(ITERATION) \ - --after-install scripts/after-install.sh \ - --before-remove scripts/before-remove.sh \ - --license $(LICENSE) \ - --url $(URL) \ - --maintainer "$(MAINT)" \ - --description "$(DESC)" \ - --config-files "/etc/$(BINARY)/$(CONFIG_FILE)" \ - --chdir $< + fpm -s dir -t rpm $(PACKAGE_ARGS) -a arm64 -v $(RPMVERSION) -C $< debarm: $(BINARY)_$(VERSION)-$(ITERATION)_arm64.deb $(BINARY)_$(VERSION)-$(ITERATION)_arm64.deb: package_build_linux_arm64 check_fpm @echo "Building 64-bit ARM8 'deb' package for $(BINARY) version '$(VERSION)-$(ITERATION)'." - fpm -s dir -t deb \ - --architecture arm64 \ - --name $(BINARY) \ - --deb-no-default-config-files \ - --version $(VERSION) \ - --iteration $(ITERATION) \ - --after-install scripts/after-install.sh \ - --before-remove scripts/before-remove.sh \ - --license $(LICENSE) \ - --url $(URL) \ - --maintainer "$(MAINT)" \ - --description "$(DESC)" \ - --config-files "/etc/$(BINARY)/$(CONFIG_FILE)" \ - --chdir $< + fpm -s dir -t deb $(PACKAGE_ARGS) -a arm64 -v $(VERSION) -C $< rpmarmhf: $(BINARY)-$(RPMVERSION)-$(ITERATION).armhf.rpm $(BINARY)-$(RPMVERSION)-$(ITERATION).armhf.rpm: package_build_linux_armhf check_fpm @echo "Building 32-bit ARM6/7 HF 'rpm' package for $(BINARY) version '$(RPMVERSION)-$(ITERATION)'." - fpm -s dir -t rpm \ - --architecture armhf \ - --name $(BINARY) \ - --rpm-os linux \ - --version $(RPMVERSION) \ - --iteration $(ITERATION) \ - --after-install scripts/after-install.sh \ - --before-remove scripts/before-remove.sh \ - --license $(LICENSE) \ - --url $(URL) \ - --maintainer "$(MAINT)" \ - --description "$(DESC)" \ - --config-files "/etc/$(BINARY)/$(CONFIG_FILE)" \ - --chdir $< + fpm -s dir -t rpm $(PACKAGE_ARGS) -a armhf -v $(RPMVERSION) -C $< debarmhf: $(BINARY)_$(VERSION)-$(ITERATION)_armhf.deb $(BINARY)_$(VERSION)-$(ITERATION)_armhf.deb: package_build_linux_armhf check_fpm @echo "Building 32-bit ARM6/7 HF 'deb' package for $(BINARY) version '$(VERSION)-$(ITERATION)'." - fpm -s dir -t deb \ - --architecture armhf \ - --name $(BINARY) \ - --deb-no-default-config-files \ - --version $(VERSION) \ - --iteration $(ITERATION) \ - --after-install scripts/after-install.sh \ - --before-remove scripts/before-remove.sh \ - --license $(LICENSE) \ - --url $(URL) \ - --maintainer "$(MAINT)" \ - --description "$(DESC)" \ - --config-files "/etc/$(BINARY)/$(CONFIG_FILE)" \ - --chdir $< - -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 "GHREPO=$(GHREPO)" \ - --build-arg "CONFIG_FILE=$(CONFIG_FILE)" \ - --tag $(DHUSER)/$(BINARY):local . + fpm -s dir -t deb $(PACKAGE_ARGS) -a armhf -v $(VERSION) -C $< # Build an environment that can be packaged for linux. package_build_linux: readme man linux @@ -277,8 +177,8 @@ package_build_linux: readme man linux cp examples/$(CONFIG_FILE).example $@/etc/$(BINARY)/ cp examples/$(CONFIG_FILE).example $@/etc/$(BINARY)/$(CONFIG_FILE) cp LICENSE *.html examples/*?.?* $@/usr/share/doc/$(BINARY)/ - [ ! -f init/systemd/template.unit.service ] || mkdir -p $@/lib/systemd/system - [ ! -f init/systemd/template.unit.service ] || \ + [ "$(FORMULA)" != "service" ] || mkdir -p $@/lib/systemd/system + [ "$(FORMULA)" != "service" ] || \ sed -e "s/{{BINARY}}/$(BINARY)/g" -e "s/{{DESC}}/$(DESC)/g" \ init/systemd/template.unit.service > $@/lib/systemd/system/$(BINARY).service @@ -300,6 +200,21 @@ package_build_linux_armhf: package_build_linux armhf check_fpm: @fpm --version > /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 "GHREPO=$(GHREPO)" \ + --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. @@ -317,7 +232,7 @@ $(BINARY).rb: v$(VERSION).tar.gz.sha256 -e "s%{{GHREPO}}%$(GHREPO)%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 + init/homebrew/$(FORMULA).rb.tmpl | tee $(BINARY).rb # Extras @@ -344,9 +259,9 @@ deps: install: man readme $(BINARY) @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: $(URL)/wiki/Installation + @echo If you wish to install the application manually on Linux, check out the wiki: https://github.com/$(GHREPO)/wiki/Installation @echo - Otherwise, build and install a package: make rpm -or- make deb - @echo See the Package Install wiki for more info: $(URL)/wiki/Package-Install + @echo See the Package Install wiki for more info: https://github.com/$(GHREPO)/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) diff --git a/init/homebrew/formula.rb.tmpl b/init/homebrew/service.rb.tmpl similarity index 100% rename from init/homebrew/formula.rb.tmpl rename to init/homebrew/service.rb.tmpl diff --git a/init/systemd/template.unit.service b/init/systemd/template.unit.service index a471a3ef..468669be 100644 --- a/init/systemd/template.unit.service +++ b/init/systemd/template.unit.service @@ -9,7 +9,7 @@ Requires=network.target ExecStart=/usr/bin/{{BINARY}} $DAEMON_OPTS EnvironmentFile=-/etc/default/{{BINARY}} EnvironmentFile=-/etc/sysconfig/{{BINARY}} -Restart=on-error +Restart=always StandardOutput=syslog StandardError=syslog SyslogIdentifier={{BINARY}} diff --git a/scripts/formula-deploy.sh b/scripts/formula-deploy.sh index 66704bd7..52b6df18 100755 --- a/scripts/formula-deploy.sh +++ b/scripts/formula-deploy.sh @@ -3,6 +3,7 @@ # Deploys a new homebrew formula file to golift/homebrew-tap. # 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. source .metadata.sh @@ -16,6 +17,7 @@ git clone git@github.com:${HBREPO}.git homebrew_release_repo cp ${BINARY}.rb homebrew_release_repo/Formula pushd homebrew_release_repo -git commit -m "Update ${BINARY} on Release: v${VERSION}-${ITERATION}" Formula/${BINARY}.rb +git add Formula/${BINARY}.rb +git commit -m "Update ${BINARY} on Release: v${VERSION}-${ITERATION}" git push popd