Skip to content

Commit

Permalink
Merge pull request #159 from unifi-poller/dn2_multi_controller
Browse files Browse the repository at this point in the history
v2: Multi Controller Support
  • Loading branch information
davidnewhall committed Dec 29, 2019
2 parents 4149441 + 971d7bc commit f5830d9
Show file tree
Hide file tree
Showing 53 changed files with 2,285 additions and 1,510 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -27,3 +27,4 @@ bitly_token
github_deploy_key
gpg.signing.key
.secret-files.tar
*.so
16 changes: 3 additions & 13 deletions .metadata.sh
Expand Up @@ -11,7 +11,7 @@ HBREPO="golift/homebrew-mugs"
MAINT="David Newhall II <david at sleepers dot pro>"
VENDOR="Go Lift <code at golift dot io>"
DESC="Polls a UniFi controller, exports metrics to InfluxDB and Prometheus"
GOLANGCI_LINT_ARGS="--enable-all -D gochecknoglobals -D dupl -D lll -D funlen -D wsl -e G402"
GOLANGCI_LINT_ARGS="--enable-all -D gochecknoglobals -D funlen -e G402 -D gochecknoinits"
# Example must exist at examples/$CONFIG_FILE.example
CONFIG_FILE="up.conf"
LICENSE="MIT"
Expand All @@ -25,30 +25,20 @@ export BINARY GHUSER HBREPO MAINT VENDOR DESC GOLANGCI_LINT_ARGS CONFIG_FILE LIC
# Fix the repo if it doesn't match the binary name.
# Provide a better URL if one exists.

# Used as go import path in docker and homebrew builds.
IMPORT_PATH="github.com/${GHUSER}/${BINARY}"
# Used for source links and wiki links.
SOURCE_URL="https://${IMPORT_PATH}"
SOURCE_URL="https://github.com/${GHUSER}/${BINARY}"
# Used for documentation links.
URL="${SOURCE_URL}"

# This parameter is passed in as -X to go build. Used to override the Version variable in a package.
# This makes a path like github.com/user/hello-world/helloworld.Version=1.3.3
# Name the Version-containing library the same as the github repo, without dashes.
VERSION_PATH="${IMPORT_PATH}/pkg/poller.Version"

# 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)"
BRANCH="$(git rev-parse --abbrev-ref HEAD || echo unknown)"
COMMIT="$(git rev-parse --short HEAD || echo 0)"

# Used by homebrew downloads.
#SOURCE_PATH=https://codeload.${IMPORT_PATH}/tar.gz/v${VERSION}
# This is a custom download path for homebrew formula.
SOURCE_PATH=https://golift.io/${BINARY}/archive/v${VERSION}.tar.gz

export IMPORT_PATH SOURCE_URL URL VERSION_PATH VVERSION VERSION ITERATION DATE BRANCH COMMIT SOURCE_PATH
export SOURCE_URL URL VVERSION VERSION ITERATION DATE COMMIT SOURCE_PATH
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -16,7 +16,7 @@ addons:
- gnupg
- expect
go:
- 1.12.x
- 1.13.x
services:
- docker
install:
Expand Down
139 changes: 0 additions & 139 deletions Gopkg.lock

This file was deleted.

9 changes: 0 additions & 9 deletions Gopkg.toml

This file was deleted.

46 changes: 32 additions & 14 deletions Makefile
Expand Up @@ -8,6 +8,7 @@ IGNORED:=$(shell bash -c "source .metadata.sh ; env | sed 's/=/:=/;s/^/export /'
# md2roff turns markdown into man files and html files.
MD2ROFF_BIN=github.com/github/hub/md2roff-bin


# Travis CI passes the version in. Local builds get it from the current git tag.
ifeq ($(VERSION),)
include .metadata.make
Expand Down Expand Up @@ -42,11 +43,13 @@ $(PACKAGE_SCRIPTS) \
--config-files "/etc/$(BINARY)/$(CONFIG_FILE)"
endef

PLUGINS:=$(patsubst plugins/%/main.go,%,$(wildcard plugins/*/main.go))

VERSION_LDFLAGS:= \
-X $(IMPORT_PATH)/vendor/github.com/prometheus/common/version.Branch=$(BRANCH) \
-X $(IMPORT_PATH)/vendor/github.com/prometheus/common/version.BuildDate=$(DATE) \
-X $(IMPORT_PATH)/vendor/github.com/prometheus/common/version.Revision=$(COMMIT) \
-X $(VERSION_PATH)=$(VERSION)-$(ITERATION)
-X github.com/prometheus/common/version.Branch=$(TRAVIS_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)

# Makefile targets follow.

Expand Down Expand Up @@ -183,12 +186,14 @@ $(BINARY)_$(VERSION)-$(ITERATION)_armhf.deb: package_build_linux_armhf check_fpm
[ "$(SIGNING_KEY)" == "" ] || expect -c "spawn debsigs --default-key="$(SIGNING_KEY)" --sign=origin $(BINARY)_$(VERSION)-$(ITERATION)_armhf.deb; expect -exact \"Enter passphrase: \"; send \"$(PRIVATE_KEY)\r\"; expect eof"

# Build an environment that can be packaged for linux.
package_build_linux: readme man linux
package_build_linux: readme man plugins_linux_amd64 linux
# Building package environment for linux.
mkdir -p $@/usr/bin $@/etc/$(BINARY) $@/usr/share/man/man1 $@/usr/share/doc/$(BINARY)
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
rm -f $@/usr/lib/$(BINARY)/*.so
cp *amd64.so $@/usr/lib/$(BINARY)/
cp examples/$(CONFIG_FILE).example $@/etc/$(BINARY)/
cp examples/$(CONFIG_FILE).example $@/etc/$(BINARY)/$(CONFIG_FILE)
cp LICENSE *.html examples/*?.?* $@/usr/share/doc/$(BINARY)/
Expand Down Expand Up @@ -226,7 +231,6 @@ docker:
--build-arg "VENDOR=$(VENDOR)" \
--build-arg "AUTHOR=$(MAINT)" \
--build-arg "BINARY=$(BINARY)" \
--build-arg "IMPORT_PATH=$(IMPORT_PATH)" \
--build-arg "SOURCE_URL=$(SOURCE_URL)" \
--build-arg "CONFIG_FILE=$(CONFIG_FILE)" \
--tag $(BINARY) .
Expand All @@ -245,14 +249,26 @@ $(BINARY).rb: v$(VERSION).tar.gz.sha256 init/homebrew/$(FORMULA).rb.tmpl
-e "s/{{SHA256}}/$(shell head -c64 $<)/g" \
-e "s/{{Desc}}/$(DESC)/g" \
-e "s%{{URL}}%$(URL)%g" \
-e "s%{{IMPORT_PATH}}%$(IMPORT_PATH)%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.

plugins: $(patsubst %,%.so,$(PLUGINS))
$(patsubst %,%.so,$(PLUGINS)):
go build -o $@ -ldflags "$(VERSION_LDFLAGS)" -buildmode=plugin ./plugins/$(patsubst %.so,%,$@)

linux_plugins: plugins_linux_amd64 plugins_linux_i386 plugins_linux_arm64 plugins_linux_armhf
plugins_linux_amd64: $(patsubst %,%.linux_amd64.so,$(PLUGINS))
$(patsubst %,%.linux_amd64.so,$(PLUGINS)):
GOOS=linux GOARCH=amd64 go build -o $@ -ldflags "$(VERSION_LDFLAGS)" -buildmode=plugin ./plugins/$(patsubst %.linux_amd64.so,%,$@)

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.
Expand All @@ -265,17 +281,18 @@ lint:

# This is safe; recommended even.
dep: vendor
vendor: Gopkg.*
dep ensure --vendor-only
vendor: go.mod go.sum
go mod vendor

# Don't run this unless you're ready to debug untested vendored dependencies.
deps:
dep ensure --update
deps: update vendor
update:
go get -u -d

# Homebrew stuff. macOS only.

# Used for Homebrew only. Other distros can create packages.
install: man readme $(BINARY)
install: man readme $(BINARY) plugins_darwin
@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
Expand All @@ -285,8 +302,9 @@ install: man readme $(BINARY)
@[ "$(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)
/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 0755 -cp *darwin.so $(PREFIX)/lib/$(BINARY)/
/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)/
[ -f $(ETC)/$(BINARY)/$(CONFIG_FILE) ] || /usr/bin/install -m 0644 -cp examples/$(CONFIG_FILE).example $(ETC)/$(BINARY)/$(CONFIG_FILE)
Expand Down

0 comments on commit f5830d9

Please sign in to comment.