Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Makefile and Dockerfiles to speed up builds #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.dockerignore
out
dockerfiles
make
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cmd/docker-machine-driver-kvm/docker-machine-driver-kvm
*.sw*
docker-machine-driver-kvm-*
out
41 changes: 23 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
PREFIX=docker-machine-driver-kvm
MACHINE_VERSION=v0.10.0
GO_VERSION=1.8.1
export PREFIX=docker-machine-driver-kvm
export GO_VERSION ?= 1.8.1
export MACHINE_VERSION ?= v0.10.0
export GIT_ROOT=$(shell git rev-parse --show-toplevel)
DESCRIBE=$(shell git describe --tags)

TARGETS=$(addprefix $(PREFIX)-, alpine3.4 alpine3.5 ubuntu14.04 ubuntu16.04 centos7)
TARGETS=$(addprefix out/$(PREFIX)-, alpine3.4 alpine3.5 ubuntu14.04 ubuntu16.04 centos7 opensuse42.2)

build: $(TARGETS)

$(PREFIX)-%: Dockerfile.%
docker rmi -f $@ >/dev/null 2>&1 || true
docker rm -f $@-extract > /dev/null 2>&1 || true
echo "Building binaries for $@"
docker build --build-arg "MACHINE_VERSION=$(MACHINE_VERSION)" --build-arg "GO_VERSION=$(GO_VERSION)" -t $@ -f $< .
docker create --name $@-extract $@ sh
docker cp $@-extract:/go/bin/docker-machine-driver-kvm ./
mv ./docker-machine-driver-kvm ./$@
docker rm $@-extract || true
docker rmi $@ || true
$(PREFIX)-base\:%: dockerfiles/Dockerfile.%
"$(GIT_ROOT)/make/base" $@

Dockerfile.%.build:
@echo "FROM $(PREFIX)-base:$*" >> $@
@echo "ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/go/bin" >> $@
@echo "ENV GOPATH /go" >> $@
@echo "COPY . /go/src/github.com/dhiltgen/docker-machine-kvm" >> $@

out/$(PREFIX)-%: $(PREFIX)-base\:% Dockerfile.%.build
"$(GIT_ROOT)/make/build" $<

clean:
rm -f ./$(PREFIX)-*
rm -f ./out/$(PREFIX)-*

clean-bases:
docker rmi $(docker images -q $(PREFIX)-base)

release: build
@echo "Paste the following into the release page on github and upload the binaries..."
@echo ""
@for bin in $(PREFIX)-* ; do \
@for bin in out/$(PREFIX)-* ; do \
bin=$$(basename "$$bin"); \
target=$$(echo $${bin} | cut -f5- -d-) ; \
md5=$$(md5sum $${bin}) ; \
md5=$$(md5sum out/$${bin}) ; \
echo "* $${target} - md5: $${md5}" ; \
echo '```' ; \
echo " curl -L https://github.com/dhiltgen/docker-machine-kvm/releases/download/$(DESCRIBE)/$${bin} > /usr/local/bin/$(PREFIX) \\ " ; \
echo " curl -L https://github.com/dhiltgen/docker-machine-kvm/releases/download/$(DESCRIBE)/$${bin} > /usr/local/bin/$(PREFIX)" ; \
echo " chmod +x /usr/local/bin/$(PREFIX)" ; \
echo '```' ; \
done
Expand Down
8 changes: 2 additions & 6 deletions Dockerfile.alpine3.4 → dockerfiles/Dockerfile.alpine3.4
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ FROM alpine:3.4
MAINTAINER Daniel Hiltgen <[email protected]>

ARG MACHINE_VERSION
# Uncomment the following line to build with docker engine < 1.13
# ARG GO_VERSION
ENV GOPATH /go

RUN apk -v add --update libvirt-dev curl go git musl-dev gcc
RUN git clone --branch ${MACHINE_VERSION} https://github.com/docker/machine.git /go/src/github.com/docker/machine

COPY . /go/src/github.com/dhiltgen/docker-machine-kvm
WORKDIR /go/src/github.com/dhiltgen/docker-machine-kvm
RUN go get -v -d ./...

RUN go install -v ./cmd/docker-machine-driver-kvm
8 changes: 2 additions & 6 deletions Dockerfile.alpine3.5 → dockerfiles/Dockerfile.alpine3.5
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ FROM alpine:3.5
MAINTAINER Daniel Hiltgen <[email protected]>

ARG MACHINE_VERSION
# Uncomment the following line to build with docker engine < 1.13
# ARG GO_VERSION
ENV GOPATH /go

RUN apk -v add --update libvirt-dev curl go git musl-dev gcc
RUN git clone --branch ${MACHINE_VERSION} https://github.com/docker/machine.git /go/src/github.com/docker/machine

COPY . /go/src/github.com/dhiltgen/docker-machine-kvm
WORKDIR /go/src/github.com/dhiltgen/docker-machine-kvm
RUN go get -v -d ./...

RUN go install -v ./cmd/docker-machine-driver-kvm
7 changes: 0 additions & 7 deletions Dockerfile.centos7 → dockerfiles/Dockerfile.centos7
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,4 @@ ENV GOPATH /go

RUN yum install -y libvirt-devel curl git gcc
RUN curl -sSL https://storage.googleapis.com/golang/go${GO_VERSION}.linux-amd64.tar.gz | tar -C /usr/local -xzf -
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/go/bin
RUN git clone --branch ${MACHINE_VERSION} https://github.com/docker/machine.git /go/src/github.com/docker/machine

COPY . /go/src/github.com/dhiltgen/docker-machine-kvm
WORKDIR /go/src/github.com/dhiltgen/docker-machine-kvm
RUN go get -v -d ./...

RUN go install -v ./cmd/docker-machine-driver-kvm
12 changes: 12 additions & 0 deletions dockerfiles/Dockerfile.opensuse42.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM opensuse:42.2

LABEL maintainer "Hart Simha <[email protected]>"
RUN zypper -n in libvirt-devel curl git gcc tar && \
git clone https://github.com/docker/machine.git /go/src/github.com/docker/machine

ARG GO_VERSION
RUN curl -sSL https://storage.googleapis.com/golang/go${GO_VERSION}.linux-amd64.tar.gz | tar -C /usr/local -xzf -

ARG MACHINE_VERSION
WORKDIR /go/src/github.com/docker/machine
RUN git checkout $MACHINE_VERSION
7 changes: 0 additions & 7 deletions Dockerfile.ubuntu14.04 → dockerfiles/Dockerfile.ubuntu14.04
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,4 @@ ENV GOPATH /go

RUN apt-get update && apt-get install -y libvirt-dev curl git gcc
RUN curl -sSL https://storage.googleapis.com/golang/go${GO_VERSION}.linux-amd64.tar.gz | tar -C /usr/local -xzf -
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/go/bin
RUN git clone --branch ${MACHINE_VERSION} https://github.com/docker/machine.git /go/src/github.com/docker/machine

COPY . /go/src/github.com/dhiltgen/docker-machine-kvm
WORKDIR /go/src/github.com/dhiltgen/docker-machine-kvm
RUN go get -v -d ./...

RUN go install -v ./cmd/docker-machine-driver-kvm
7 changes: 0 additions & 7 deletions Dockerfile.ubuntu16.04 → dockerfiles/Dockerfile.ubuntu16.04
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,4 @@ ENV GOPATH /go

RUN apt-get update && apt-get install -y libvirt-dev curl git gcc
RUN curl -sSL https://storage.googleapis.com/golang/go${GO_VERSION}.linux-amd64.tar.gz | tar -C /usr/local -xzf -
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/go/bin
RUN git clone --branch ${MACHINE_VERSION} https://github.com/docker/machine.git /go/src/github.com/docker/machine

COPY . /go/src/github.com/dhiltgen/docker-machine-kvm
WORKDIR /go/src/github.com/dhiltgen/docker-machine-kvm
RUN go get -v -d ./...

RUN go install -v ./cmd/docker-machine-driver-kvm
32 changes: 32 additions & 0 deletions make/base
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env sh

# This script creates the platform-specific base image used as the FROM base for constructing
# the image which will be used to generate the docker-machine-driver-kvm-$platform binary.
# It expects the base image name (taking the form of $PREFIX-base:$PLATFORM) to be passed as an argument
set -e
GO_VERSION_ARG=GO_VERSION=${GO_VERSION:-1.8.1}
MACHINE_VERSION_ARG=MACHINE_VERSION=${MACHINE_VERSION:-v0.10.0}
GIT_ROOT=${GIT_ROOT:-$(git rev-parse --show-toplevel)}

# Extract values 'BASE_IMAGE_NAME:PLATFORM' from the image name argument
BASE_IMAGE_NAME=${1%:*}
PLATFORM=${1#*:}

# If the base image for this platform, go version, and machine version is not already present, build it.
# Labels are used to track the GO_VERSION and MACHINE_VERSION
if [ -z "$(docker images -q $@ -f label=$GO_VERSION_ARG -f label=$MACHINE_VERSION_ARG)" ]; then
# If an image with this BASE_IMAGE_NAME:PLATFORM already exists, store its ID for deletion later
PREVIOUS_IMAGE=$(docker images -q $@)
# Build the new image before deleting any previous one, to reuse common layers
docker build \
--label $GO_VERSION_ARG --build-arg $GO_VERSION_ARG \
--label $MACHINE_VERSION_ARG --build-arg $MACHINE_VERSION_ARG \
-t $@ \
-f "${GIT_ROOT}/dockerfiles/Dockerfile.${PLATFORM}" \
"${GIT_ROOT}"
if [ -n "$PREVIOUS_IMAGE" ]; then
docker rmi $PREVIOUS_IMAGE
fi
else
echo "Image '$@' with labels $GO_VERSION_ARG $MACHINE_VERSION_ARG already exists"
fi
24 changes: 24 additions & 0 deletions make/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env sh

# This script builds the docker-machine-driver-kvm binary by first building a temporary
# image with the relevant docker-machine-kvm code inside, and PATH and GO_PATH set. The binary
# is then built in a container from this image and into 'out/'. Finally, the temporary
# image and container (with suffixes '-build' and '-extract) are deleted

set -e
PREFIX=${PREFIX:-docker-machine-driver-kvm}
GIT_ROOT=${GIT_ROOT:-$(git rev-parse --show-toplevel)}

BASE_IMAGE=$@
PLATFORM=${BASE_IMAGE#*:}
BUILD_TARGET=$PREFIX-$PLATFORM
BUILD_IMAGE=$PREFIX-build:$PLATFORM

echo "Building compilation image for out/$PREFIX-$PLATFORM"
docker build -t $BUILD_IMAGE -f "${GIT_ROOT}/Dockerfile.$PLATFORM.build" "${GIT_ROOT}"

echo "Building binaries for out/$PREFIX-$PLATFORM"
docker run --name $BUILD_TARGET-extract $BUILD_IMAGE sh -c "cd /go/src/github.com/dhiltgen/docker-machine-kvm && go get -v -d ./... && go install -v ./cmd/docker-machine-driver-kvm"
docker cp $BUILD_TARGET-extract:/go/bin/docker-machine-driver-kvm out/$BUILD_TARGET
docker rm $BUILD_TARGET-extract
docker rmi $BUILD_IMAGE
Empty file added out/.gitkeep
Empty file.