From a5c076851b834ee47dfbaa15ec82d5df20948a3d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 3 Mar 2015 23:14:18 -0800 Subject: [PATCH] scripts --- .gitignore | 4 +++ Makefile | 61 ++++++++++++++++++++++++++++++++++ command/version.go | 1 - scripts/build.sh | 81 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/dist.sh | 49 ++++++++++++++++++++++++++++ 5 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 Makefile create mode 100755 scripts/build.sh create mode 100755 scripts/dist.sh diff --git a/.gitignore b/.gitignore index daf913b1b3..611e303644 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,7 @@ _testmain.go *.exe *.test *.prof + +# Other dirs +bin/ +pkg/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..0285d23b07 --- /dev/null +++ b/Makefile @@ -0,0 +1,61 @@ +TEST?=./... +VETARGS?=-asmdecl -atomic -bool -buildtags -copylocks -methods -nilfunc -printf -rangeloops -shift -structtags -unsafeptr + +default: test + +# bin generates the releaseable binaries for Vault +bin: generate + @sh -c "'$(CURDIR)/scripts/build.sh'" + +# dev creates binaries for testing Vault locally. These are put +# into ./bin/ as well as $GOPATH/bin +dev: generate + @TF_DEV=1 sh -c "'$(CURDIR)/scripts/build.sh'" + +# test runs the unit tests and vets the code +test: generate + TF_ACC= go test $(TEST) $(TESTARGS) -timeout=30s -parallel=4 + @$(MAKE) vet + +# testrace runs the race checker +testrace: generate + TF_ACC= go test -race $(TEST) $(TESTARGS) + +# updatedeps installs all the dependencies that Vault needs to run +# and build. +updatedeps: + 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/vault \ + | sort -u \ + | xargs go get -f -u -v + +cover: + @go tool cover 2>/dev/null; if [ $$? -eq 3 ]; then \ + go get -u golang.org/x/tools/cmd/cover; \ + fi + go test $(TEST) -coverprofile=coverage.out + go tool cover -html=coverage.out + rm coverage.out + +# vet runs the Go source code static analysis tool `vet` to find +# any common errors. +vet: + @go tool vet 2>/dev/null ; if [ $$? -eq 3 ]; then \ + go get golang.org/x/tools/cmd/vet; \ + fi + @echo "go tool vet $(VETARGS) ." + @go tool vet $(VETARGS) . ; if [ $$? -eq 1 ]; then \ + echo ""; \ + echo "Vet found suspicious constructs. Please check the reported constructs"; \ + 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: + go generate ./... + +.PHONY: bin default generate test updatedeps vet diff --git a/command/version.go b/command/version.go index 097e3e3563..e529fa13cc 100644 --- a/command/version.go +++ b/command/version.go @@ -20,7 +20,6 @@ func (c *VersionCommand) Help() string { } func (c *VersionCommand) Run(_ []string) int { - var versionString bytes.Buffer fmt.Fprintf(&versionString, "Vault v%s", c.Version) diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000000..d390b3e588 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,81 @@ +#!/bin/bash +# +# This script builds the application from source for multiple platforms. +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" + +# Get the git commit +GIT_COMMIT=$(git rev-parse HEAD) +GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true) + +# Determine the arch/os combos we're building for +XC_ARCH=${XC_ARCH:-"386 amd64 arm"} +XC_OS=${XC_OS:-linux darwin windows freebsd openbsd} + +# Install dependencies +echo "==> Getting dependencies..." +go get ./... + +# Delete the old dir +echo "==> Removing old directory..." +rm -f bin/* +rm -rf pkg/* +mkdir -p bin/ + +# If its dev mode, only build for ourself +if [ "${TF_DEV}x" != "x" ]; then + XC_OS=$(go env GOOS) + XC_ARCH=$(go env GOARCH) +fi + +# Build! +echo "==> Building..." +gox \ + -os="${XC_OS}" \ + -arch="${XC_ARCH}" \ + -ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \ + -output "pkg/{{.OS}}_{{.Arch}}/vault" \ + . + +# 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 [ "${TF_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 new file mode 100755 index 0000000000..039bcb53cc --- /dev/null +++ b/scripts/dist.sh @@ -0,0 +1,49 @@ +#!/bin/bash +set -e + +# Get the version from the command line +VERSION=$1 +if [ -z $VERSION ]; then + echo "Please specify a version." + 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 +DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )" + +# Change into that dir because we expect that +cd $DIR + +# 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/vault_${VERSION}_${FILENAME} +done + +# Make the checksums +pushd ./pkg/dist +shasum -a256 * > ./vault_${VERSION}_SHA256SUMS +popd + +# 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/vault/vault/${VERSION}/${ARCHIVE_NAME}" +done + +exit 0