Commit 2788ccc6 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Update scripts to latest HashiCorp style

parent db906582
NO_COLOR=\033[0m TEST?=./...
OK_COLOR=\033[32;01m
ERROR_COLOR=\033[31;01m
WARN_COLOR=\033[33;01m
DEPS = $(go list -f '{{range .TestImports}}{{.}} {{end}}' ./...)
UNAME := $(shell uname -s)
ifeq ($(UNAME),Darwin)
ECHO=echo
else
ECHO=/bin/echo -e
endif
all: deps default: test
@mkdir -p bin/
@$(ECHO) "$(OK_COLOR)==> Building$(NO_COLOR)"
@bash --norc -i ./scripts/devcompile.sh
deps: bin:
@$(ECHO) "$(OK_COLOR)==> Installing dependencies$(NO_COLOR)" @sh -c "$(CURDIR)/scripts/build.sh"
@go get -d -v ./...
@go get github.com/mitchellh/gox
@echo $(DEPS) | xargs -n1 go get -d
updatedeps: dev:
@$(ECHO) "$(OK_COLOR)==> Updating all dependencies$(NO_COLOR)" @TF_DEV=1 sh -c "$(CURDIR)/scripts/build.sh"
@go get -d -v -u ./...
@echo $(DEPS) | xargs -n1 go get -d -u
clean: test:
@rm -rf bin/ local/ pkg/ src/ website/.sass-cache website/build go test $(TEST) $(TESTARGS) -timeout=10s
format: testrace:
go fmt ./... go test -race $(TEST) $(TESTARGS)
test: deps updatedeps:
@$(ECHO) "$(OK_COLOR)==> Testing Packer...$(NO_COLOR)" go get -u -v ./...
go test ./...
.PHONY: all clean deps format test updatedeps .PHONY: bin default test updatedeps
...@@ -78,40 +78,44 @@ http://www.packer.io/docs ...@@ -78,40 +78,44 @@ http://www.packer.io/docs
## Developing Packer ## Developing Packer
If you wish to work on Packer itself, you'll first need [Go](http://golang.org) If you wish to work on Packer itself or any of its built-in providers,
installed (version 1.2+ is _required_). Make sure you have Go properly installed, you'll first need [Go](http://www.golang.org) installed (version 1.2+ is
including setting up your [GOPATH](http://golang.org/doc/code.html#GOPATH). _required_). Make sure Go is properly installed, including setting up
a [GOPATH](http://golang.org/doc/code.html#GOPATH).
For some additional dependencies, Go needs [Mercurial](http://mercurial.selenic.com/) Next, install the following software packages, which are needed for some dependencies:
and [Bazaar](http://bazaar.canonical.com/en/) to be installed.
Packer itself doesn't require these, but a dependency of a dependency does.
You'll also need [`gox`](https://github.com/mitchellh/gox) - [Bazaar](http://bazaar.canonical.com/en/)
to compile packer. You can install that with: - [Git](http://git-scm.com/)
- [Mercurial](http://mercurial.selenic.com/)
``` Then, install [Gox](https://github.com/mitchellh/gox), which is used
$ go get -u github.com/mitchellh/gox as a compilation tool on top of Go:
```
Next, clone this repository into `$GOPATH/src/github.com/mitchellh/packer` and $ go get -u github.com/mitchellh/gox
then just type `make`. In a few moments, you'll have a working `packer` executable:
``` Next, clone this repository into `$GOPATH/src/github.com/mitchellh/packer`.
$ make Install the necessary dependencies by running `make updatedeps` and then just
... type `make`. This will compile some more dependencies and then run the tests. If
$ bin/packer this exits with exit status 0, then everything is working!
...
``` $ make updatedeps
...
$ make
...
If you need to cross-compile Packer for other platforms, take a look at To compile a development version of Packer and the built-in plugins,
`scripts/dist.sh`. run `make dev`. This will put Packer binaries in the `bin` folder:
You can run tests by typing `make test`. $ make dev
...
$ bin/packer
...
This will run tests for Packer core along with all the core builders and commands and such that come with Packer.
If you make any changes to the code, run `make format` in order to automatically If you're developing a specific package, you can run tests for just that
format the code according to Go standards. package by specifying the `TEST` variable. For example below, only
`packer` package tests will be run.
When new dependencies are added to packer you can use `make updatedeps` to $ make test TEST=./packer
get the latest and subsequently use `make` to compile and generate the `packer` binary. ...
#!/bin/bash #!/bin/bash
# #
# This script compiles Packer for various platforms (specified by the # This script builds the application from source for multiple platforms.
# PACKER_OS and PACKER_ARCH environmental variables).
set -e set -e
NO_COLOR="\x1b[0m"
OK_COLOR="\x1b[32;01m"
ERROR_COLOR="\x1b[31;01m"
WARN_COLOR="\x1b[33;01m"
# Get the parent directory of where this script is. # Get the parent directory of where this script is.
SOURCE="${BASH_SOURCE[0]}" SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
...@@ -25,28 +19,59 @@ GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true) ...@@ -25,28 +19,59 @@ GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)
XC_ARCH=${XC_ARCH:-"386 amd64 arm"} XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
XC_OS=${XC_OS:-linux darwin windows freebsd openbsd} XC_OS=${XC_OS:-linux darwin windows freebsd openbsd}
# Make sure that if we're killed, we kill all our subprocseses # Install dependencies
trap "kill 0" SIGINT SIGTERM EXIT echo "==> Getting dependencies..."
echo -e "${OK_COLOR}==> Installing dependencies to speed up builds...${NO_COLOR}"
go get ./... go get ./...
echo -e "${OK_COLOR}==> Beginning compile...${NO_COLOR}" # Delete the old dir
rm -rf pkg/ 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 \ gox \
-os="${XC_OS}" \ -os="${XC_OS}" \
-arch="${XC_ARCH}" \ -arch="${XC_ARCH}" \
-ldflags "-X github.com/mitchellh/packer/packer.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \ -ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \
-output "pkg/{{.OS}}_{{.Arch}}/packer-{{.Dir}}" \ -output "pkg/{{.OS}}_{{.Arch}}/packer-{{.Dir}}" \
./... ./...
# Make sure "packer-packer" is renamed properly # Make sure "packer-packer" is renamed properly
for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
set +e set +e
mv ${PLATFORM}/packer-packer*.exe ${PLATFORM}/packer.exe 2>/dev/null mv ${PLATFORM}/packer-packer ${PLATFORM}/packer 2>/dev/null
mv ${PLATFORM}/packer-packer* ${PLATFORM}/packer 2>/dev/null mv ${PLATFORM}/packer-packer ${PLATFORM}/packer 2>/dev/null
set -e set -e
done done
# Reset signal trapping to avoid "Terminated: 15" at the end # Copy our OS/Arch to the bin/ directory
trap - SIGINT SIGTERM EXIT 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/
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/
#!/bin/bash
#
# This script only builds the application from source.
set -e
NO_COLOR="\x1b[0m"
OK_COLOR="\x1b[32;01m"
ERROR_COLOR="\x1b[31;01m"
WARN_COLOR="\x1b[33;01m"
# http://stackoverflow.com/questions/4023830/bash-how-compare-two-strings-in-version-format
verify_go () {
if [[ $1 == $2 ]]; then
return 0
fi
if [[ "$2" == "devel" ]]; then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++)); do
if [[ -z ${ver2[i]} ]]; then
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]})); then
echo -e "${ERROR_COLOR}==> Required Go version $1 not installed. Found $2 instead"
exit 1
fi
done
}
GO_MINIMUM_VERSION=1.2
GO_INSTALLED_VERSION=$(go version | cut -d ' ' -f 3)
GO_INSTALLED_VERSION=${GO_INSTALLED_VERSION#"go"}
echo -e "${OK_COLOR}==> Verifying Go"
verify_go $GO_MINIMUM_VERSION $GO_INSTALLED_VERSION
# 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
# Compile the thing
export XC_ARCH=$(go env GOARCH)
export XC_OS=$(go env GOOS)
./scripts/compile.sh
# Move all the compiled things to the PATH
case $(uname) in
CYGWIN*)
GOPATH="$(cygpath $GOPATH)"
;;
esac
IFS=: MAIN_GOPATH=( $GOPATH )
cp pkg/${XC_OS}_${XC_ARCH}/* ${MAIN_GOPATH}/bin
cp pkg/${XC_OS}_${XC_ARCH}/* ./bin
#!/bin/bash #!/bin/bash
set -e 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. # Get the parent directory of where this script is.
SOURCE="${BASH_SOURCE[0]}" SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
...@@ -9,59 +22,28 @@ DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )" ...@@ -9,59 +22,28 @@ DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
# Change into that dir because we expect that # Change into that dir because we expect that
cd $DIR cd $DIR
# Determine the version that we're building based on the contents # Zip all the files
# of packer/version.go. rm -rf ./pkg/dist
VERSION=$(grep "const Version " packer/version.go | sed -E 's/.*"(.+)"$/\1/')
VERSIONDIR="${VERSION}"
PREVERSION=$(grep "const VersionPrerelease " packer/version.go | sed -E 's/.*"(.*)"$/\1/')
if [ ! -z $PREVERSION ]; then
PREVERSION="${PREVERSION}.$(date -u +%s)"
VERSIONDIR="${VERSIONDIR}-${PREVERSION}"
fi
# This function waits for all background tasks to complete
waitAll() {
RESULT=0
for job in `jobs -p`; do
wait $job
if [ $? -ne 0 ]; then
RESULT=1
fi
done
if [ $RESULT -ne 0 ]; then
exit $RESULT
fi
}
# Compile the main project
./scripts/compile.sh
# Make sure that if we're killed, we kill all our subprocseses
trap "kill 0" SIGINT SIGTERM EXIT
# Zip all the packages
mkdir -p ./pkg/dist mkdir -p ./pkg/dist
for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do for FILENAME in $(find ./pkg -mindepth 1 -maxdepth 1 -type f); do
PLATFORM_NAME=$(basename ${PLATFORM}) FILENAME=$(basename $FILENAME)
ARCHIVE_NAME="${VERSIONDIR}_${PLATFORM_NAME}" cp ./pkg/${FILENAME} ./pkg/dist/packer_${VERSION}_${FILENAME}
if [ $PLATFORM_NAME = "dist" ]; then
continue
fi
(
pushd ${PLATFORM}
zip ${DIR}/pkg/dist/${ARCHIVE_NAME}.zip ./*
popd
) &
done done
waitAll
# Make the checksums # Make the checksums
pushd ./pkg/dist pushd ./pkg/dist
shasum -a256 * > ./${VERSIONDIR}_SHA256SUMS shasum -a256 * > ./packer_${VERSION}_SHA256SUMS
popd 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/packer/packer/${VERSION}/${ARCHIVE_NAME}"
done
exit 0 exit 0
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment