From 6c528f0ff25053ec9f968a349ac9fdea295735b7 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 22 Oct 2015 14:16:01 -0400 Subject: [PATCH] Use gox for building --- .gitignore | 5 ++- Makefile | 30 ++++++++++++++-- scripts/build.sh | 90 ++++++++++++++++++++++++++++++---------------- scripts/dist.sh | 80 +++++++++++++++++------------------------ ui/Makefile | 14 ++------ ui/scripts/dist.sh | 37 +++++++++++++++++++ 6 files changed, 164 insertions(+), 92 deletions(-) create mode 100755 ui/scripts/dist.sh diff --git a/.gitignore b/.gitignore index a998ee8496..5fb798fabb 100644 --- a/.gitignore +++ b/.gitignore @@ -21,7 +21,7 @@ _cgo_export.* _testmain.go -/dist +/pkg *.exe *.test bin/ @@ -41,3 +41,6 @@ ui/dist/ website/.bundle website/vendor + +ui/.bundle +ui/vendor diff --git a/Makefile b/Makefile index 5ca763f040..29ade6fb76 100644 --- a/Makefile +++ b/Makefile @@ -2,11 +2,24 @@ DEPS = $(shell go list -f '{{range .TestImports}}{{.}} {{end}}' ./...) PACKAGES = $(shell go list ./...) VETARGS?=-asmdecl -atomic -bool -buildtags -copylocks -methods \ -nilfunc -printf -rangeloops -shift -structtags -unsafeptr +VERSION?=$(shell awk -F\" '/^const Version/ { print $$2; exit }' version.go) all: deps format @mkdir -p bin/ @bash --norc -i ./scripts/build.sh +# bin generates the releasable binaries +bin: generate + @sh -c "'$(CURDIR)/scripts/build.sh'" + +# dev creates binares for testing locally. There are put into ./bin and $GOPATH +dev: generate + @CONSUL_DEV=1 sh -c "'$(CURDIR)/scripts/build.sh'" + +# dist creates the binaries for distibution +dist: bin + @sh -c "'$(CURDIR)/scripts/dist.sh' $(VERSION)" + cov: gocov test ./... | gocov-html > /tmp/coverage.html open /tmp/coverage.html @@ -16,8 +29,14 @@ deps: @go get -d -v ./... $(DEPS) updatedeps: deps - @echo "--> Updating build dependencies" - @go get -d -f -u ./... $(DEPS) + go get -u github.com/mitchellh/gox + go get -u golang.org/x/tools/cmd/stringer + go list ./... \ + | xargs go list -f '{{join .Deps "\n"}}' \ + | grep -v github.com/hashicorp/consul \ + | grep -v '/internal/' \ + | sort -u \ + | xargs go get -f -u -v test: deps @./scripts/verify_no_uuid.sh @@ -46,10 +65,15 @@ vet: echo "and fix them if necessary before submitting the code for reviewal."; \ fi +# generate runs `go generate` to build the dynamically generated source files +generate: deps + find . -type f -name '.DS_Store' -delete + go generate ./... + web: ./scripts/website_run.sh web-push: ./scripts/website_push.sh -.PHONY: all cov deps integ test vet web web-push test-nodep +.PHONY: all bin dev dist cov deps integ test vet web web-push generate test-nodep diff --git a/scripts/build.sh b/scripts/build.sh index c39820c58e..3b72b4f640 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,6 +1,6 @@ -#!/usr/bin/env bash +#!/bin/bash # -# This script builds the application from source. +# This script builds the application from source for multiple platforms. set -e # Get the parent directory of where this script is. @@ -16,37 +16,67 @@ GIT_COMMIT=$(git rev-parse HEAD) GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true) GIT_DESCRIBE=$(git describe --tags) -# If we're building on Windows, specify an extension -EXTENSION="" -if [ "$(go env GOOS)" = "windows" ]; then - EXTENSION=".exe" -fi +# Determine the arch/os combos we're building for +XC_ARCH=${XC_ARCH:-"386 amd64 arm"} +XC_OS=${XC_OS:-"darwin freebsd linux netbsd openbsd windows"} -GOPATHSINGLE=${GOPATH%%:*} -if [ "$(go env GOOS)" = "windows" ]; then - GOPATHSINGLE=${GOPATH%%;*} -fi +# Install dependencies +echo "==> Getting dependencies..." +go get ./... -if [ "$(go env GOOS)" = "freebsd" ]; then - export CC="clang" -fi +# Delete the old dir +echo "==> Removing old directory..." +rm -f bin/* +rm -rf pkg/* +mkdir -p bin/ -# On OSX, we need to use an older target to ensure binaries are -# compatible with older linkers -if [ "$(go env GOOS)" = "darwin" ]; then - export MACOSX_DEPLOYMENT_TARGET=10.6 +# If its dev mode, only build for ourself +if [ "${CONSUL_DEV}x" != "x" ]; then + XC_OS=$(go env GOOS) + XC_ARCH=$(go env GOARCH) fi -# Install dependencies -echo "--> Installing dependencies to speed up builds..." -go get \ - -ldflags "${CGO_LDFLAGS}" \ - ./... - # Build! -echo "--> Building..." -go build \ - -ldflags "${CGO_LDFLAGS} -X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X main.GitDescribe=${GIT_DESCRIBE}" \ - -v \ - -o bin/consul${EXTENSION} -cp bin/consul${EXTENSION} "${GOPATHSINGLE}/bin" +echo "==> Building..." +gox \ + -os="${XC_OS}" \ + -arch="${XC_ARCH}" \ + -ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY} -X main.GitDescribe ${GIT_DESCRIBE}" \ + -output "pkg/{{.OS}}_{{.Arch}}/consul" \ + . + +# Move all the compiled things to the $GOPATH/bin +GOPATH=${GOPATH:-$(go env GOPATH)} +case $(uname) in + CYGWIN*) + GOPATH="$(cygpath $GOPATH)" + ;; +esac +OLDIFS=$IFS +IFS=: MAIN_GOPATH=($GOPATH) +IFS=$OLDIFS + +# Copy our OS/Arch to the bin/ directory +DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)" +for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do + cp ${F} bin/ + cp ${F} ${MAIN_GOPATH}/bin/ +done + +if [ "${CONSUL_DEV}x" = "x" ]; then + # Zip and copy to the dist dir + echo "==> Packaging..." + for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do + OSARCH=$(basename ${PLATFORM}) + echo "--> ${OSARCH}" + + pushd $PLATFORM >/dev/null 2>&1 + zip ../${OSARCH}.zip ./* + popd >/dev/null 2>&1 + done +fi + +# Done! +echo +echo "==> Results:" +ls -hl bin/ diff --git a/scripts/dist.sh b/scripts/dist.sh index 8f58d6d943..d1c2c4dda6 100755 --- a/scripts/dist.sh +++ b/scripts/dist.sh @@ -8,12 +8,6 @@ if [ -z $VERSION ]; then exit 1 fi -# Make sure we have a bintray API key -if [ -z $BINTRAY_API_KEY ]; then - echo "Please set your bintray API key in the BINTRAY_API_KEY env var." - exit 1 -fi - # Get the parent directory of where this script is. SOURCE="${BASH_SOURCE[0]}" while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done @@ -22,53 +16,45 @@ DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )" # Change into that dir because we expect that cd $DIR -# Zip all the files -rm -rf ./dist/pkg -mkdir -p ./dist/pkg -for FILENAME in $(find ./dist -mindepth 1 -maxdepth 1 -type f); do - FILENAME=$(basename $FILENAME) - EXTENSION="${FILENAME##*.}" - PLATFORM="${FILENAME%.*}" - - if [ "${EXTENSION}" != "exe" ]; then - EXTENSION="" - else - EXTENSION=".${EXTENSION}" - fi - - CONSULNAME="consul${EXTENSION}" - - pushd ./dist - - if [ "${FILENAME}" = "ui.zip" ]; then - cp ${FILENAME} ./pkg/${VERSION}_web_ui.zip - else - if [ "${EXTENSION}" = "" ]; then - chmod +x ${FILENAME} - fi +# Generate the tag +if [ -z $NOTAG ]; then + echo "==> Tagging..." + git commit --allow-empty -a --gpg-sign=348FFC4C -m "Release v$VERSION" + git tag -a -m "Version $VERSION" -s -u 348FFC4C "v${VERSION}" master +fi - cp ${FILENAME} ${CONSULNAME} - zip ./pkg/${VERSION}_${PLATFORM}.zip ${CONSULNAME} - rm ${CONSULNAME} - fi +# Generate the UI +if [ -z $NOUI ]; then + echo "==> Generating Consul UI..." + make -f ui/Makefile dist +fi - popd +# Zip all the files +rm -rf ./pkg/dist +mkdir -p ./pkg/dist +for FILENAME in $(find ./pkg -mindepth 1 -maxdepth 1 -type f); do + FILENAME=$(basename $FILENAME) + cp ./pkg/${FILENAME} ./pkg/dist/consul_${VERSION}_${FILENAME} done # Make the checksums -pushd ./dist/pkg -shasum -a256 * > ./${VERSION}_SHA256SUMS +pushd ./pkg/dist +shasum -a256 * > ./consul_${VERSION}_SHA256SUMS +if [ -z $NOSIGN ]; then + echo "==> Signing..." + gpg --default-key 348FFC4C --detach-sig ./consul_${VERSION}_SHA256SUMS +fi popd -# Upload -for ARCHIVE in ./dist/pkg/*; do - ARCHIVE_NAME=$(basename ${ARCHIVE}) +# # Upload +# for ARCHIVE in ./pkg/dist/*; do +# ARCHIVE_NAME=$(basename ${ARCHIVE}) - echo Uploading: $ARCHIVE_NAME - curl \ - -T ${ARCHIVE} \ - -umitchellh:${BINTRAY_API_KEY} \ - "https://api.bintray.com/content/mitchellh/consul/consul/${VERSION}/${ARCHIVE_NAME}" -done +# echo Uploading: $ARCHIVE_NAME +# curl \ +# -T ${ARCHIVE} \ +# -umitchellh:${BINTRAY_API_KEY} \ +# "https://api.bintray.com/content/mitchellh/consul/consul/${VERSION}/${ARCHIVE_NAME}" +# done -exit 0 +# exit 0 diff --git a/ui/Makefile b/ui/Makefile index 174a9c48cb..7bccbd7561 100644 --- a/ui/Makefile +++ b/ui/Makefile @@ -1,3 +1,5 @@ +ROOT:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) + server: python -m SimpleHTTPServer @@ -5,16 +7,6 @@ watch: sass styles:static --watch dist: - @echo clean dist - @rm -rf dist/index.html - @rm -rf dist/static - @echo "compile styles/*.scss" - @sass styles/base.scss static/base.css - @ruby scripts/compile.rb - cp -R ./static dist/static/ - cp index.html dist/index.html - sed -E -e "/ASSETS/,/\/ASSETS/ d" -ibak dist/index.html - sed -E -e "s#<\/body>##" -ibak dist/index.html - rm dist/index.htmlbak + @sh -c "'$(ROOT)/scripts/dist.sh'" .PHONY: server watch dist diff --git a/ui/scripts/dist.sh b/ui/scripts/dist.sh new file mode 100755 index 0000000000..be90b7f2ac --- /dev/null +++ b/ui/scripts/dist.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +set -e + +# Get the parent directory of where this script is. +SOURCE="${BASH_SOURCE[0]}" +while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done +DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )" + +# Change into that directory +cd "$DIR" + +# Generate the tag +DEPLOY="../pkg/web_ui" + +rm -rf $DEPLOY +mkdir -p $DEPLOY + +bundle check >/dev/null 2>&1 || bundle install +bundle exec sass styles/base.scss static/base.css + +bundle exec ruby scripts/compile.rb + +# Copy into deploy +shopt -s dotglob +cp -r $DIR/static/* $DEPLOY/ +cp index.html $DEPLOY/ + +# Magic scripting +sed -E -e "/ASSETS/,/\/ASSETS/ d" -ibak $DEPLOY/index.html +sed -E -e "s#<\/body>##" -ibak $DEPLOY/index.html + +# Remove the backup file from sed +rm $DEPLOY/index.htmlbak + +pushd $DEPLOY >/dev/null 2>&1 +zip ../web_ui.zip ./* +popd >/dev/null 2>&1