From 872a3e782c7f0342088d8f37c37c47a056b1e46e Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Tue, 9 Jul 2019 00:47:11 -0700 Subject: [PATCH] more auto --- .metadata.sh | 17 +++++++----- Makefile | 37 +++++++++++++++---------- init/docker/Dockerfile | 43 ++++++++++++++---------------- init/docker/hooks/build | 4 ++- init/homebrew/unifi-poller.rb.tmpl | 30 ++++++++++----------- scripts/formula-deploy.sh | 18 ++++--------- scripts/install.sh | 2 +- 7 files changed, 76 insertions(+), 75 deletions(-) diff --git a/.metadata.sh b/.metadata.sh index ce87c779..261460aa 100755 --- a/.metadata.sh +++ b/.metadata.sh @@ -1,6 +1,8 @@ # Each line must have an export clause. # This file is parsed and sourced by the Makefile, Docker and Homebrew builds. -TITLE="UniFi Poller" + +# Must match the repo name. +BINARY="unifi-poller" # github username GHUSER="davidnewhall" # docker hub username @@ -9,19 +11,21 @@ MAINT="David Newhall II " VENDOR="Go Lift" DESC="Polls a UniFi controller and stores metrics in InfluxDB" GOLANGCI_LINT_ARGS="--enable-all -D gochecknoglobals" +# Example must exist at examples/$CONFIG_FILE.example CONFIG_FILE="up.conf" LICENSE="MIT" -# The rest if mostly automatic. -BINARY="$(basename $(git rev-parse --show-toplevel))" -[ "$BINARY" != "" ] || BINARY="$(basename $(pwd))" +export BINARY GHUSER DHUSER MAINT VENDOR DESC GOLANGCI_LINT_ARGS CONFIG_FILE LICENSE + +# The rest is mostly automatic. + GHREPO="${GHUSER}/${BINARY}" URL="https://github.com/${GHREPO}" # 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/davidnewhall/unifi-poller/unifipoller.Version=1.3.3 # Name the Version-containing library the same as the github repo, without dashes. -VERSION_PATH="github.com/${GHUSER}/${BINARY}/$(echo ${BINARY} | tr -d -- -).Version" +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)" @@ -30,5 +34,4 @@ 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)" -export TITLE GHUSER DHUSER MAINT VENDOR DESC CONFIG_FILE LICENSE GOLANGCI_LINT_ARGS -export BINARY GHREPO URL VERSION_PATH VERSION ITERATION DATE COMMIT +export GHREPO URL VERSION_PATH VERSION ITERATION DATE COMMIT diff --git a/Makefile b/Makefile index 2cb69819..6791d8b5 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,8 @@ ifeq ($(VERSION),) include .metadata.make else # Preserve the passed-in version & iteration (homebrew). - _VERSION:=$(VERSION) - _ITERATION:=$(ITERATION) + _VERSION:=$(VERSION) + _ITERATION:=$(ITERATION) include .metadata.make VERSION:=$(_VERSION) ITERATION:=$(_ITERATION) @@ -42,7 +42,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 + rm -f $(BINARY){_,-}*.{deb,rpm} v*.tar.gz.sha256 .metadata.make rm -f cmd/$(BINARY)/README{,.html} README{,.html} ./$(BINARY)_manual.html rm -rf package_build_* release @@ -254,15 +254,17 @@ $(BINARY)_$(VERSION)-$(ITERATION)_armhf.deb: package_build_linux_armhf check_fpm 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 "TITLE=${TITLE}" \ - --build-arg "DESC=${DESC}" \ - --build-arg "URL=${URL}" \ - --build-arg "VENDOR=${VENDOR}" \ - --build-arg "AUTHOR=${MAINT}" \ + --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 . # Build an environment that can be packaged for linux. @@ -306,8 +308,15 @@ v$(VERSION).tar.gz.sha256: curl -sL $(URL)/archive/v$(VERSION).tar.gz | openssl dgst -r -sha256 | tee $@ $(BINARY).rb: v$(VERSION).tar.gz.sha256 # Creating formula from template using sed. - sed "s/{{Version}}/$(VERSION)/g;s/{{SHA256}}/`head -c64 $<`/g;s/{{Desc}}/$(DESC)/g;s%{{URL}}%$(URL)%g" init/homebrew/$(BINARY).rb.tmpl | tee $(BINARY).rb - + 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%{{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/$(BINARY).rb.tmpl | tee $(BINARY).rb # Extras # Run code tests and lint. diff --git a/init/docker/Dockerfile b/init/docker/Dockerfile index dc00d8b4..b1f14ad4 100644 --- a/init/docker/Dockerfile +++ b/init/docker/Dockerfile @@ -6,31 +6,26 @@ ARG ARCH=amd64 ARG OS=linux ARG BUILD_DATE=0 ARG COMMIT=0 -ARG VERSION=development +ARG VERSION=unknown +ARG BINARY=application-builder +ARG GHREPO=golift/application-builder FROM golang:stretch as builder ARG ARCH ARG OS +ARG BINARY +ARG GHREPO -RUN mkdir -p $GOPATH/pkg/mod $GOPATH/bin $GOPATH/src/github.com/davidnewhall/unifi-poller +RUN mkdir -p $GOPATH/pkg/mod $GOPATH/bin $GOPATH/src/github.com/${GHREPO} RUN apt-get update \ && apt-get install -y curl \ && curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh -COPY . $GOPATH/src/github.com/davidnewhall/unifi-poller -WORKDIR $GOPATH/src/github.com/davidnewhall/unifi-poller +COPY . $GOPATH/src/github.com/${GHREPO} +WORKDIR $GOPATH/src/github.com/${GHREPO} RUN dep ensure --vendor-only \ - && CGO_ENABLED=0 make unifi-poller.${ARCH}.${OS} - -# -# creating container for run -# to use this container use the following command: -# -# docker run -d -v /your/config/up.conf:/etc/unifi-poller/up.conf golift/unifi-poller -# -# by using "-e UNIFI_PASSWORD=your-secret-pasword" you can avoid this configuration in the config file -# + && CGO_ENABLED=0 make ${BINARY}.${ARCH}.${OS} FROM scratch ARG ARCH @@ -39,28 +34,30 @@ ARG BUILD_DATE ARG COMMIT ARG VERSION ARG LICENSE=MIT -ARG TITLE=unifi-poller -ARG URL=http://github.com/davidnewhall/unifi-poller -ARG DESC=unifi-poller +ARG BINARY +ARG GHREPO +ARG URL=http://github.com/golift/application-builder +ARG DESC=application-builder ARG VENDOR=golift ARG AUTHOR=golift +ARG CONFIG_FILE=config.conf # Build-time metadata as defined at https://github.com/opencontainers/image-spec/blob/master/annotations.md LABEL org.opencontainers.image.created="${BUILD_DATE}" \ - org.opencontainers.image.title="${TITLE}" \ + org.opencontainers.image.title="${BINARY}" \ org.opencontainers.image.documentation="${URL}/wiki/Docker" \ org.opencontainers.image.description="${DESC}" \ org.opencontainers.image.url="${URL}" \ org.opencontainers.image.revision="${COMMIT}" \ - org.opencontainers.image.source="${URL}" \ + org.opencontainers.image.source="https://github.com/${GHREPO}" \ org.opencontainers.image.vendor="${VENDOR}" \ org.opencontainers.image.authors="${AUTHOR}" \ org.opencontainers.image.architecture="${OS} ${ARCH}" \ org.opencontainers.image.licenses="${LICENSE}" \ org.opencontainers.image.version="${VERSION}" -COPY --from=builder /go/src/github.com/davidnewhall/unifi-poller/unifi-poller.${ARCH}.${OS} /unifi-poller -COPY --from=builder /go/src/github.com/davidnewhall/unifi-poller/examples/up.conf.example /etc/unifi-poller/up.conf +COPY --from=builder /go/src/github.com/${GHREPO}/${BINARY}.${ARCH}.${OS} /image +COPY --from=builder /go/src/github.com/${GHREPO}/examples/${CONFIG_FILE}.example /etc/${BINARY}/${CONFIG_FILE} -VOLUME [ "/etc/unifi-poller" ] -ENTRYPOINT [ "/unifi-poller" ] +VOLUME [ "/etc/${BINARY}" ] +ENTRYPOINT [ "/image" ] diff --git a/init/docker/hooks/build b/init/docker/hooks/build index 5053d4c3..01be1ca9 100755 --- a/init/docker/hooks/build +++ b/init/docker/hooks/build @@ -21,11 +21,13 @@ for build in $BUILDS; do --build-arg "COMMIT=${COMMIT}" \ --build-arg "VERSION=${VERSION}-${ITERATION}" \ --build-arg "LICENSE=${LICENSE}" \ - --build-arg "TITLE=${TITLE}" \ --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 "${IMAGE_NAME}_${os}_${name}" \ --file ${DOCKERFILE_PATH} . done diff --git a/init/homebrew/unifi-poller.rb.tmpl b/init/homebrew/unifi-poller.rb.tmpl index 1bfda369..c9731742 100644 --- a/init/homebrew/unifi-poller.rb.tmpl +++ b/init/homebrew/unifi-poller.rb.tmpl @@ -1,5 +1,5 @@ # Homebrew Formula Template. Built by Makefile: `make fomula` -class UnifiPoller < Formula +class {{Class}} < Formula desc "{{Desc}}" homepage "{{URL}}" url "{{URL}}/archive/v{{Version}}.tar.gz" @@ -12,27 +12,25 @@ class UnifiPoller < Formula def install ENV["GOPATH"] = buildpath - bin_path = buildpath/"src/github.com/davidnewhall/unifi-poller" + bin_path = buildpath/"src/github.com/{{GHREPO}}" # Copy all files from their current location (GOPATH root) - # to $GOPATH/src/github.com/davidnewhall/unifi-poller - bin_path.install Dir["*"] + # to $GOPATH/src/github.com/{{GHREPO}} + bin_path.install Dir["*",".??*"] cd bin_path do system "dep", "ensure", "--vendor-only" - system "make", "install", "VERSION=#{version}", "PREFIX=#{prefix}", "ETC=#{etc}" + system "make", "install", "VERSION=#{version}", "ITERATION={{Iter}}", "PREFIX=#{prefix}", "ETC=#{etc}" # If this fails, the user gets a nice big warning about write permissions on their # #{var}/log folder. The alternative could be letting the app silently fail # to start when it cannot write logs. This is better. Fix perms; reinstall. - touch("#{var}/log/unifi-poller.log") + touch("#{var}/log/#{name}.log") end end def caveats <<-EOS - This application will not work until the config file has authentication - information for a UniFi Controller and an Influx Database. Edit the config - file at #{etc}/unifi-poller/up.conf then start the application with - brew services start unifi-poller ~ log file: #{var}/log/unifi-poller.log - The manual explains the config file options: man unifi-poller + Edit the config file at #{etc}/#{name}/{{CONFIG_FILE}} then start #{name} with + brew services start #{name} ~ log file: #{var}/log/#{name}.log + The manual explains the config file options: man #{name} EOS end @@ -48,24 +46,24 @@ class UnifiPoller < Formula #{plist_name} ProgramArguments - #{bin}/unifi-poller + #{bin}/#{name} -c - #{etc}/unifi-poller/up.conf + #{etc}/#{name}/{{CONFIG_FILE}} RunAtLoad KeepAlive StandardErrorPath - #{var}/log/unifi-poller.log + #{var}/log/#{name}.log StandardOutPath - #{var}/log/unifi-poller.log + #{var}/log/#{name}.log EOS end test do - assert_match "unifi-poller v#{version}", shell_output("#{bin}/unifi-poller -v 2>&1", 2) + assert_match "#{name} v#{version}", shell_output("#{bin}/#{name} -v 2>&1", 2) end end diff --git a/scripts/formula-deploy.sh b/scripts/formula-deploy.sh index 0b5affae..980d2e2b 100755 --- a/scripts/formula-deploy.sh +++ b/scripts/formula-deploy.sh @@ -4,26 +4,18 @@ # Requires SSH credentials in ssh-agent to work. # Run by Travis-CI when a new release is created on GitHub. -source ../.metadata.sh +source .metadata.sh -if [ -z "$VERSION" ]; then - VERSION=$TRAVIS_TAG -fi -VERSION=$(echo $VERSION|tr -d v) +make ${BINARY}.rb -make ${BINARY}.rb VERSION=$VERSION - -if [ -z "$VERSION" ]; then - VERSION=$(grep -E '^\s+url\s+"' ${BINARY}.rb | cut -d/ -f7 | cut -d. -f1,2,3) -fi - -rm -rf homebrew-mugs git config --global user.email "${BINARY}@auto.releaser" git config --global user.name "${BINARY}-auto-releaser" + +rm -rf homebrew-mugs git clone git@github.com:golift/homebrew-mugs.git cp ${BINARY}.rb homebrew-mugs/Formula pushd homebrew-mugs -git commit -m "Update ${BINARY} on Release: ${VERSION}-${ITERATION}" Formula/${BINARY}.rb +git commit -m "Update ${BINARY} on Release: v${VERSION}-${ITERATION}" Formula/${BINARY}.rb git push popd diff --git a/scripts/install.sh b/scripts/install.sh index 5b260028..caea43a6 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -4,7 +4,7 @@ # # Use it like this: (sudo is optional) # === -# curl https://raw.githubusercontent.com/davidnewhall/unifi-poller/master/scripts/install.sh | sudo bash +# curl https://raw.githubusercontent.com/this/repo/master/scripts/install.sh | sudo bash # === # If you're on redhat, this installs the latest rpm. If you're on Debian, it installs the latest deb package. #