Update building stuff

- Add a Makefile with common targets (binary, test, validate, ..)
- Use docker to build it (isolated)

And update circleci to work with the new Makefile..

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2015-09-15 21:38:54 +02:00
parent fd835e1fcd
commit 0ea80582d2
14 changed files with 278 additions and 10 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
dist
gen.go

View file

@ -1,3 +1,3 @@
FROM scratch FROM scratch
ADD traefik / COPY dist/traefik_linux-386 /traefik
CMD ["/traefik"] ENTRYPOINT ["/traefik"]

48
Makefile Normal file
View file

@ -0,0 +1,48 @@
.PHONY: all
TRAEFIK_ENVS := \
-e OS_ARCH_ARG \
-e OS_PLATFORM_ARG \
-e TESTFLAGS
BIND_DIR := $(if $(DOCKER_HOST),,dist)
TRAEFIK_MOUNT := $(if $(BIND_DIR),-v "$(CURDIR)/$(BIND_DIR):/go/src/github.com/emilevauge/traefik/$(BIND_DIR)")
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
TRAEFIK_DEV_IMAGE := traefik-dev$(if $(GIT_BRANCH),:$(GIT_BRANCH))
TRAEFIK_IMAGE := $(if $(REPO),$(REPO),"emilevauge/traefik")
DOCKER_RUN_TRAEFIK := docker run $(if $(CIRCLECI),,--rm) -it $(TRAEFIK_ENVS) $(TRAEFIK_MOUNT) "$(TRAEFIK_DEV_IMAGE)"
default: binary
binary: build
$(DOCKER_RUN_TRAEFIK) ./script/make.sh generate binary
test: build
$(DOCKER_RUN_TRAEFIK) ./script/make.sh generate test-unit test-integration
test-unit: build
$(DOCKER_RUN_TRAEFIK) ./script/make.sh generate test-unit
test-integration: build
$(DOCKER_RUN_TRAEFIK) ./script/make.sh generate binary test-integration
validate: build
$(DOCKER_RUN_TRAEFIK) ./script/make.sh validate-gofmt
validate-gofmt: build
$(DOCKER_RUN_TRAEFIK) ./script/make.sh validate-gofmt
build: dist
docker build -t "$(TRAEFIK_DEV_IMAGE)" -f build.Dockerfile .
image: build
if ! [ -a dist/traefik_linux-386 ] ; \
then \
$(DOCKER_RUN_TRAEFIK) ./script/make.sh generate binary; \
fi;
docker build -t $(TRAEFIK_IMAGE) .
dist:
mkdir dist

27
build.Dockerfile Normal file
View file

@ -0,0 +1,27 @@
FROM golang:1.5
RUN go get github.com/mitchellh/gox
RUN go get github.com/tcnksm/ghr
# Install dependencies
RUN go get github.com/BurntSushi/toml \
&& go get github.com/BurntSushi/ty/fun
RUN go get github.com/mailgun/oxy/forward \
&& go get github.com/mailgun/oxy/roundrobin
RUN go get github.com/gorilla/handlers \
&& go get github.com/gorilla/mux
RUN go get github.com/cenkalti/backoff \
&& go get github.com/codegangsta/negroni \
&& go get github.com/op/go-logging \
&& go get github.com/elazarl/go-bindata-assetfs \
&& go get github.com/leekchan/gtf \
&& go get github.com/thoas/stats \
&& go get github.com/tylerb/graceful \
&& go get github.com/unrolled/render
RUN go get github.com/fsouza/go-dockerclient \
&& go get github.com/gambol99/go-marathon
RUN go get gopkg.in/fsnotify.v1 \
&& go get gopkg.in/alecthomas/kingpin.v2
WORKDIR /go/src/github.com/emilevauge/traefik
COPY . /go/src/github.com/emilevauge/traefik

View file

@ -6,15 +6,17 @@ machine:
dependencies: dependencies:
pre: pre:
- go generate
- CGO_ENABLED=0 go build -a -installsuffix nocgo .
- go get github.com/mitchellh/gox
- go get github.com/tcnksm/ghr - go get github.com/tcnksm/ghr
- gox -verbose -os "linux darwin windows" --output "dist/{{.Dir}}_{{.OS}}_{{.Arch}}" - make validate
override: override:
- make binary
test:
override:
- make test
post:
- echo $REPO - echo $REPO
- docker build -t ${REPO,,} . - make image
deployment: deployment:
github: github:

1
file_test.go Normal file
View file

@ -0,0 +1 @@
package main

33
script/.validate Normal file
View file

@ -0,0 +1,33 @@
#!/bin/bash
if [ -z "$VALIDATE_UPSTREAM" ]; then
# this is kind of an expensive check, so let's not do this twice if we
# are running more than one validate bundlescript
VALIDATE_REPO='https://github.com/emilevauge/traefik.git'
VALIDATE_BRANCH='master'
if [ "$TRAVIS" = 'true' -a "$TRAVIS_PULL_REQUEST" != 'false' ]; then
VALIDATE_REPO="https://github.com/${TRAVIS_REPO_SLUG}.git"
VALIDATE_BRANCH="${TRAVIS_BRANCH}"
fi
VALIDATE_HEAD="$(git rev-parse --verify HEAD)"
git fetch -q "$VALIDATE_REPO" "refs/heads/$VALIDATE_BRANCH"
VALIDATE_UPSTREAM="$(git rev-parse --verify FETCH_HEAD)"
VALIDATE_COMMIT_LOG="$VALIDATE_UPSTREAM..$VALIDATE_HEAD"
VALIDATE_COMMIT_DIFF="$VALIDATE_UPSTREAM...$VALIDATE_HEAD"
validate_diff() {
if [ "$VALIDATE_UPSTREAM" != "$VALIDATE_HEAD" ]; then
git diff "$VALIDATE_COMMIT_DIFF" "$@"
fi
}
validate_log() {
if [ "$VALIDATE_UPSTREAM" != "$VALIDATE_HEAD" ]; then
git log "$VALIDATE_COMMIT_LOG" "$@"
fi
}
fi

28
script/binary Executable file
View file

@ -0,0 +1,28 @@
#!/bin/bash
set -e
if ! test -e gen.go; then
echo >&2 'error: generate must be run before binary'
false
fi
if [ -z "$1" ]; then
OS_PLATFORM_ARG=(-os="darwin linux windows")
else
OS_PLATFORM_ARG=($1)
fi
if [ -z "$2" ]; then
OS_ARCH_ARG=(-arch="386 amd64 arm")
else
OS_ARCH_ARG=($2)
fi
# Get rid of existing binaries
rm -f dist/traefik*
# Build binaries
CGO_ENABLED=0 go build -a -installsuffix nocgo .
gox "${OS_PLATFORM_ARG[@]}" "${OS_ARCH_ARG[@]}" \
-output="dist/traefik_{{.OS}}-{{.Arch}}"

4
script/generate Executable file
View file

@ -0,0 +1,4 @@
#!/bin/bash
set -e
go generate

29
script/make.sh Executable file
View file

@ -0,0 +1,29 @@
#!/usr/bin/env bash
set -e
# List of bundles to create when no argument is passed
DEFAULT_BUNDLES=(
validate-gofmt
binary
test-unit
test-integration
)
bundle() {
local bundle="$1"; shift
echo "---> Making bundle: $(basename "$bundle") (in $DEST)"
source "script/$bundle" "$@"
}
if [ $# -lt 1 ]; then
bundles=(${DEFAULT_BUNDLES[@]})
else
bundles=($@)
fi
for bundle in ${bundles[@]}; do
export DEST=.
ABS_DEST="$(cd "$DEST" && pwd -P)"
bundle "$bundle"
echo
done

8
script/test-integration Executable file
View file

@ -0,0 +1,8 @@
#!/bin/bash
export SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export DEST=.
TESTFLAGS="$TESTFLAGS"
#go test -v ./integration

56
script/test-unit Executable file
View file

@ -0,0 +1,56 @@
#!/bin/bash
set -e
if ! test -e gen.go; then
echo >&2 'error: generate must be run before binary'
false
fi
RED=$'\033[31m'
GREEN=$'\033[32m'
TEXTRESET=$'\033[0m' # reset the foreground colour
# This helper function walks the current directory looking for directories
# holding certain files ($1 parameter), and prints their paths on standard
# output, one per line.
find_dirs() {
find . -not \( \
\( \
-path './integration/*' \
-o -path './.git/*' \
\) \
-prune \
\) -name "$1" -print0 | xargs -0n1 dirname | sort -u
}
TESTFLAGS="-cover -coverprofile=cover.out ${TESTFLAGS}"
if [ -z "$TESTDIRS" ]; then
TESTDIRS=$(find_dirs '*_test.go')
fi
TESTS_FAILED=()
for dir in $TESTDIRS; do
echo '+ go test' $TESTFLAGS "${dir}"
go test ${TESTFLAGS} ${dir}
if [ $? != 0 ]; then
TESTS_FAILED+=("$dir")
echo
echo "${RED}Tests failed: $dir${TEXTRESET}"
sleep 1 # give it a second, so observers watching can take note
fi
done
echo
# if some tests fail, we want the bundlescript to fail, but we want to
# try running ALL the tests first, hence TESTS_FAILED
if [ "${#TESTS_FAILED[@]}" -gt 0 ]; then
echo "${RED}Test failures in: ${TESTS_FAILED[@]}${TEXTRESET}"
echo
false
else
echo "${GREEN}Test success${TEXTRESET}"
echo
true
fi

30
script/validate-gofmt Executable file
View file

@ -0,0 +1,30 @@
#!/bin/bash
source "$(dirname "$BASH_SOURCE")/.validate"
IFS=$'\n'
files=( $(validate_diff --diff-filter=ACMR --name-only -- '*.go' || true) )
unset IFS
badFiles=()
for f in "${files[@]}"; do
# we use "git show" here to validate that what's committed is formatted
if [ "$(git show "$VALIDATE_HEAD:$f" | gofmt -s -l)" ]; then
badFiles+=( "$f" )
fi
done
if [ ${#badFiles[@]} -eq 0 ]; then
echo 'Congratulations! All Go source files are properly formatted.'
else
{
echo "These files are not properly gofmt'd:"
for f in "${badFiles[@]}"; do
echo " - $f"
done
echo
echo 'Please reformat the above files using "gofmt -s -w" and commit the result.'
echo
} >&2
false
fi

View file

@ -3,6 +3,7 @@ package main
import ( import (
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/codegangsta/negroni" "github.com/codegangsta/negroni"
"github.com/emilevauge/traefik/middlewares"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/mailgun/oxy/forward" "github.com/mailgun/oxy/forward"
"github.com/mailgun/oxy/roundrobin" "github.com/mailgun/oxy/roundrobin"
@ -11,7 +12,6 @@ import (
"github.com/tylerb/graceful" "github.com/tylerb/graceful"
"github.com/unrolled/render" "github.com/unrolled/render"
"gopkg.in/alecthomas/kingpin.v2" "gopkg.in/alecthomas/kingpin.v2"
"./middlewares"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"