Browse code

Good-bye, ugly mega-Makefile. Docker can now be built with docker, with the help of a simple very simple shell script.

Solomon Hykes authored on 2013/08/07 13:00:50
Showing 3 changed files
... ...
@@ -3,6 +3,8 @@ docker-version 0.4.2
3 3
 from	ubuntu:12.04
4 4
 maintainer	Solomon Hykes <solomon@dotcloud.com>
5 5
 # Build dependencies
6
+run	echo 'deb http://archive.ubuntu.com/ubuntu precise main universe' > /etc/apt/sources.list
7
+run	apt-get update
6 8
 run	apt-get install -y -q curl
7 9
 run	apt-get install -y -q git
8 10
 # Install Go
... ...
@@ -11,24 +13,23 @@ env	PATH	/usr/local/go/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bi
11 11
 env	GOPATH	/go
12 12
 env	CGO_ENABLED 0
13 13
 run	cd /tmp && echo 'package main' > t.go && go test -a -i -v
14
+# Ubuntu stuff
15
+run	apt-get install -y -q ruby1.9.3 rubygems
16
+run	gem install fpm
17
+run	apt-get install -y -q reprepro
18
+# Install s3cmd 1.0.1 (earlier versions don't support env variables in the config)
19
+run	apt-get install -y -q python-pip
20
+run	pip install s3cmd
21
+run	/bin/echo -e '[default]\naccess_key=$AWS_ID\nsecret_key=$AWS_KEY\n' > /.s3cfg
14 22
 # Download dependencies
15 23
 run	PKG=github.com/kr/pty REV=27435c699;		 git clone http://$PKG /go/src/$PKG && cd /go/src/$PKG && git checkout -f $REV
16 24
 run	PKG=github.com/gorilla/context/ REV=708054d61e5; git clone http://$PKG /go/src/$PKG && cd /go/src/$PKG && git checkout -f $REV
17 25
 run	PKG=github.com/gorilla/mux/ REV=9b36453141c;	 git clone http://$PKG /go/src/$PKG && cd /go/src/$PKG && git checkout -f $REV
18 26
 run	PKG=github.com/dotcloud/tar/ REV=d06045a6d9;	 git clone http://$PKG /go/src/$PKG && cd /go/src/$PKG && git checkout -f $REV
19
-# Run dependencies
20
-run	apt-get install -y iptables
21
-# lxc requires updating ubuntu sources
22
-run	echo 'deb http://archive.ubuntu.com/ubuntu precise main universe' > /etc/apt/sources.list
23
-run	apt-get update
24
-run	apt-get install -y lxc
25
-run	apt-get install -y aufs-tools
26 27
 # Docker requires code.google.com/p/go.net/websocket
27 28
 run	apt-get install -y -q mercurial
28 29
 run	PKG=code.google.com/p/go.net REV=78ad7f42aa2e;	 hg clone https://$PKG /go/src/$PKG && cd /go/src/$PKG && hg checkout -r $REV
29 30
 # Upload docker source
30 31
 add	.       /go/src/github.com/dotcloud/docker
31 32
 # Build the binary
32
-run	cd /go/src/github.com/dotcloud/docker/docker && go install -ldflags "-X main.GITCOMMIT '??' -d -w"
33
-env	PATH	/usr/local/go/bin:/go/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
34
-cmd	["docker"]
33
+run	cd /go/src/github.com/dotcloud/docker && ./make.sh
35 34
deleted file mode 100644
... ...
@@ -1,95 +0,0 @@
1
-DOCKER_PACKAGE := github.com/dotcloud/docker
2
-RELEASE_VERSION := $(shell git tag | grep -E "v[0-9\.]+$$" | sort -nr | head -n 1)
3
-SRCRELEASE := docker-$(RELEASE_VERSION)
4
-BINRELEASE := docker-$(RELEASE_VERSION).tgz
5
-BUILD_SRC := build_src
6
-BUILD_PATH := ${BUILD_SRC}/src/${DOCKER_PACKAGE}
7
-
8
-GIT_ROOT := $(shell git rev-parse --show-toplevel)
9
-BUILD_DIR := $(CURDIR)/.gopath
10
-
11
-GOPATH ?= $(BUILD_DIR)
12
-export GOPATH
13
-
14
-GO_OPTIONS ?= -a -ldflags='-w -d'
15
-ifeq ($(VERBOSE), 1)
16
-GO_OPTIONS += -v
17
-endif
18
-
19
-GIT_COMMIT = $(shell git rev-parse --short HEAD)
20
-GIT_STATUS = $(shell test -n "`git status --porcelain`" && echo "+CHANGES")
21
-
22
-BUILD_OPTIONS = -a -ldflags "-X main.GITCOMMIT $(GIT_COMMIT)$(GIT_STATUS) -d -w"
23
-
24
-SRC_DIR := $(GOPATH)/src
25
-
26
-DOCKER_DIR := $(SRC_DIR)/$(DOCKER_PACKAGE)
27
-DOCKER_MAIN := $(DOCKER_DIR)/docker
28
-
29
-DOCKER_BIN_RELATIVE := bin/docker
30
-DOCKER_BIN := $(CURDIR)/$(DOCKER_BIN_RELATIVE)
31
-
32
-.PHONY: all clean test hack release srcrelease $(BINRELEASE) $(SRCRELEASE) $(DOCKER_BIN) $(DOCKER_DIR)
33
-
34
-all: $(DOCKER_BIN)
35
-
36
-$(DOCKER_BIN): $(DOCKER_DIR)
37
-	@mkdir -p  $(dir $@)
38
-	@(cd $(DOCKER_MAIN); CGO_ENABLED=0 go build $(GO_OPTIONS) $(BUILD_OPTIONS) -o $@)
39
-	@echo $(DOCKER_BIN_RELATIVE) is created.
40
-
41
-$(DOCKER_DIR):
42
-	@mkdir -p $(dir $@)
43
-	@if [ -h $@ ]; then rm -f $@; fi; ln -sf $(CURDIR)/ $@
44
-	@(cd $(DOCKER_MAIN); go get -d $(GO_OPTIONS))
45
-
46
-whichrelease:
47
-	echo $(RELEASE_VERSION)
48
-
49
-release: $(BINRELEASE)
50
-	s3cmd -P put $(BINRELEASE) s3://get.docker.io/builds/`uname -s`/`uname -m`/docker-$(RELEASE_VERSION).tgz
51
-	s3cmd -P put docker-latest.tgz s3://get.docker.io/builds/`uname -s`/`uname -m`/docker-latest.tgz
52
-	s3cmd -P put $(SRCRELEASE)/bin/docker s3://get.docker.io/builds/`uname -s`/`uname -m`/docker
53
-	echo $(RELEASE_VERSION) > latest ; s3cmd -P put latest s3://get.docker.io/latest ; rm latest
54
-
55
-srcrelease: $(SRCRELEASE)
56
-deps: $(DOCKER_DIR)
57
-
58
-# A clean checkout of $RELEASE_VERSION, with vendored dependencies
59
-$(SRCRELEASE):
60
-	rm -fr $(SRCRELEASE)
61
-	git clone $(GIT_ROOT) $(SRCRELEASE)
62
-	cd $(SRCRELEASE); git checkout -q $(RELEASE_VERSION)
63
-
64
-# A binary release ready to be uploaded to a mirror
65
-$(BINRELEASE): $(SRCRELEASE)
66
-	rm -f $(BINRELEASE)
67
-	cd $(SRCRELEASE); make; cp -R bin docker-$(RELEASE_VERSION); tar -f ../$(BINRELEASE) -zv -c docker-$(RELEASE_VERSION)
68
-	cd $(SRCRELEASE); cp -R bin docker-latest; tar -f ../docker-latest.tgz -zv -c docker-latest
69
-clean:
70
-	@rm -rf $(dir $(DOCKER_BIN))
71
-ifeq ($(GOPATH), $(BUILD_DIR))
72
-	@rm -rf $(BUILD_DIR)
73
-else ifneq ($(DOCKER_DIR), $(realpath $(DOCKER_DIR)))
74
-	@rm -f $(DOCKER_DIR)
75
-endif
76
-
77
-test:
78
-	# Copy docker source and dependencies for testing
79
-	rm -rf ${BUILD_SRC}; mkdir -p ${BUILD_PATH}
80
-	tar --exclude=${BUILD_SRC} -cz . | tar -xz -C ${BUILD_PATH}
81
-	GOPATH=${CURDIR}/${BUILD_SRC} go get -d
82
-	# Do the test
83
-	sudo -E GOPATH=${CURDIR}/${BUILD_SRC} CGO_ENABLED=0 go test ${GO_OPTIONS}
84
-
85
-testall: all
86
-	@(cd $(DOCKER_DIR); CGO_ENABLED=0 sudo -E go test ./... $(GO_OPTIONS))
87
-
88
-fmt:
89
-	@gofmt -s -l -w .
90
-
91
-hack:
92
-	cd $(CURDIR)/hack && vagrant up
93
-
94
-ssh-dev:
95
-	cd $(CURDIR)/hack && vagrant ssh
96 1
new file mode 100755
... ...
@@ -0,0 +1,100 @@
0
+#!/bin/sh
1
+
2
+# This script builds various binary artifacts from a checkout of the docker source code.
3
+#
4
+# Requirements:
5
+# - The current directory should be a checkout of the docker source code (http://github.com/dotcloud/docker). Whatever version is checked out will be built.
6
+# - The script is intented to be run as part of a docker build, as defined in the Dockerfile at the root of the source.
7
+# - If you don't call this script from the official Dockerfile, or a container built by the official Dockerfile, you're probably doing it wrong.
8
+# 
9
+
10
+set -e
11
+set -x
12
+
13
+VERSION=`cat ./VERSION`
14
+GIT_COMMIT=$(git rev-parse --short HEAD)
15
+GIT_CHANGES=$(test -n "`git status --porcelain`" && echo "+CHANGES")
16
+
17
+# "bundles" indicate the different types of build artifacts: static binary, ubuntu package, etc.
18
+
19
+# Build docker as a static binary file
20
+bundle_binary() {
21
+	mkdir -p bundles/$VERSION/binary
22
+	go build -o bundles/$VERSION/binary/docker-$VERSION -ldflags "-X main.GITCOMMIT $GIT_COMMIT$GIT_CHANGES -X main.VERSION $VERSION -d -w" ./docker
23
+}
24
+
25
+
26
+# Build docker's test suite as a collection of binary files (one per sub-package to test)
27
+bundle_test() {
28
+	mkdir -p bundles/$VERSION/test
29
+	for test_dir in `find_test_dirs`; do
30
+		test_binary=`
31
+			cd $test_dir
32
+			go test -c -v -ldflags "-X main.GITCOMMIT $GIT_COMMIT$GIT_CHANGES -X main.VERSION $VERSION -d -w" >&2
33
+			find . -maxdepth 1 -type f -name '*.test' -executable
34
+		`
35
+		cp $test_dir/$test_binary bundles/$VERSION/test/
36
+	done
37
+
38
+}
39
+
40
+# Build docker as an ubuntu package using FPM and REPREPRO (sue me).
41
+# bundle_binary must be called first.
42
+bundle_ubuntu() {
43
+	mkdir -p bundles/$VERSION/ubuntu
44
+
45
+	DIR=$(mktemp -d)
46
+
47
+	# Generate an upstart config file (ubuntu-specific)
48
+	mkdir -p $DIR/etc/init
49
+	cat > $DIR/etc/init/docker.conf <<EOF
50
+description     "Run docker"
51
+
52
+start on filesystem or runlevel [2345]
53
+stop on runlevel [!2345]
54
+
55
+respawn
56
+
57
+exec docker -d
58
+EOF
59
+
60
+	# Copy the binary
61
+	mkdir -p $DIR/usr/bin
62
+	cp bundles/$VERSION/binary/docker-$VERSION $DIR/usr/bin/docker
63
+
64
+	(
65
+		cd bundles/$VERSION/ubuntu
66
+		fpm -s dir -t deb -n lxc-docker -v $VERSION -a all --prefix / -C $DIR .
67
+	)
68
+	rm -fr $DIR
69
+
70
+
71
+	# Setup the APT repo
72
+	APTDIR=bundles/$VERSION/ubuntu/apt
73
+	mkdir -p $APTDIR/conf
74
+	cat > $APTDIR/conf/distributions <<EOF
75
+Codename: docker
76
+Components: main
77
+Architectures: amd64
78
+EOF
79
+
80
+	# Add the DEB package to the APT repo
81
+	DEBFILE=bundles/$VERSION/ubuntu/lxc-docker*.deb
82
+	reprepro -b $APTDIR includedeb docker $DEBFILE
83
+}
84
+
85
+
86
+# This helper function walks the current directory looking for directories holding Go test files,
87
+# and prints their paths on standard output, one per line.
88
+find_test_dirs() {
89
+	find . -name '*_test.go' | { while read f; do dirname $f; done; } | sort -u
90
+}
91
+
92
+
93
+main() {
94
+	bundle_binary
95
+	bundle_ubuntu
96
+	#bundle_test
97
+}
98
+
99
+main