#!/bin/sh

# Build a "FROM scratch" docker image based on some
# statically compiled binaries. Usage example:
#
# $ scripts/build-golang-docker-image.sh \
#   	--project "dvol" \
#   	--source-files "dvol.go cmd/dvol-docker-plugin/dvol-docker-plugin.go" \
#   	--binaries "dvol dvol-docker-plugin" \
#   	--tag "golang"

set -e

# TODO Make this actually do proper argument parsing.
PROJECT=$2
SOURCE_FILES=$4
BINARIES=$6
TAG=$8

# Statically compile the binaries
for SOURCE_FILE in $SOURCE_FILES; do
    # XXX Need to make sure this is go 1.5 to avoid bug in
    # older versions of docker.
    CGO_ENABLED=0 GOOS=linux godep go build -a -ldflags '-s' ${SOURCE_FILE}
done

# Copy them into the build directory
for BINARY in $BINARIES; do
    mv ${PROJECT} ${PROJECT}-build/
done

# Copy the dockerfile
cp Dockerfile.${PROJECT} ${PROJECT}-build/Dockerfile

# Build the docker image in a constrained context.
mkdir -p ${PROJECT}-build
cd ${PROJECT}-build
docker build -t clusterhq/${PROJECT}:${TAG} .
cd ..

# Clean up
rm -rf ${PROJECT}-build/

#!/bin/sh

# Build a "FROM scratch" docker image based on some
# statically compiled binaries. Usage example:
#
# $ scripts/build-golang-docker-image.sh \
#   	--project "dvol" \
#   	--source-files "dvol.go cmd/dvol-docker-plugin/dvol-docker-plugin.go" \
#   	--binaries "dvol dvol-docker-plugin" \
#   	--tag "golang"

set -xe

# TODO Make this actually do proper argument parsing.
PROJECT=$2
SOURCE_FILES=$4
BINARIES=$6
TAG=$8

# Statically compile the binaries
for SOURCE_FILE in $SOURCE_FILES; do
    # XXX Need to make sure this is go 1.5 to avoid bug in
    # older versions of docker.
    CGO_ENABLED=0 GOOS=linux godep go build -a -ldflags '-s' ${SOURCE_FILE}
done

mkdir -p ${PROJECT}-build
# Copy them into the build directory
for BINARY in $BINARIES; do
    mv ${PROJECT} ${PROJECT}-build/
done

# Copy the dockerfile
cp Dockerfile.${PROJECT} ${PROJECT}-build/Dockerfile

# Build the docker image in a constrained context.
cd ${PROJECT}-build
docker build -t clusterhq/${PROJECT}:${TAG} .
cd ..

# Clean up
rm -rf ${PROJECT}-build/

Make the directory before you use it.
Make the directory before you use it.
