Browse code

Adjust "hack/make/.detect-daemon-osarch" to be the source of truth for "platform detection"

Instead of being split between three files, let's let `hack/make/.detect-daemon-osarch` be our single source of truth for multiarch detection/vars. Not only does it make it slightly easier to make sure we change everything properly when these bits have to change, but it also makes it so that all bits of `hack/make.sh` (especially `hack/make/.ensure-frozen-images`) work properly outside the context of the `Makefile` on all platforms.

Signed-off-by: Andrew "Tianon" Page <admwiggin@gmail.com>

Tianon Gravi authored on 2016/03/11 12:19:33
Showing 3 changed files
... ...
@@ -1,30 +1,8 @@
1 1
 .PHONY: all binary build cross default docs docs-build docs-shell shell test test-docker-py test-integration-cli test-unit validate
2 2
 
3 3
 # get OS/Arch of docker engine
4
-DOCKER_OSARCH := $(shell bash -c 'source hack/make/.detect-daemon-osarch && echo $${DOCKER_ENGINE_OSARCH:+$$DOCKER_CLIENT_OSARCH}')
5
-# default for linux/amd64 and others
6
-DOCKERFILE := Dockerfile
7
-# switch to different Dockerfile for linux/arm
8
-ifeq ($(DOCKER_OSARCH), linux/arm)
9
-	DOCKERFILE := Dockerfile.armhf
10
-else
11
-ifeq ($(DOCKER_OSARCH), linux/arm64)
12
-	DOCKERFILE := Dockerfile.aarch64
13
-else
14
-ifeq ($(DOCKER_OSARCH), linux/ppc64le)
15
-	DOCKERFILE := Dockerfile.ppc64le
16
-else
17
-ifeq ($(DOCKER_OSARCH), linux/s390x)
18
-	DOCKERFILE := Dockerfile.s390x
19
-else
20
-ifeq ($(DOCKER_OSARCH), windows/amd64)
21
-	DOCKERFILE := Dockerfile.windows
22
-endif
23
-endif
24
-endif
25
-endif
26
-endif
27
-export DOCKERFILE
4
+DOCKER_OSARCH := $(shell bash -c 'source hack/make/.detect-daemon-osarch && echo $${DOCKER_ENGINE_OSARCH:-$$DOCKER_CLIENT_OSARCH}')
5
+DOCKERFILE := $(shell bash -c 'source hack/make/.detect-daemon-osarch && echo $${DOCKERFILE}')
28 6
 
29 7
 # env vars passed through directly to Docker's build scripts
30 8
 # to allow things like `make DOCKER_CLIENTONLY=1 binary` easily
... ...
@@ -37,7 +15,6 @@ DOCKER_ENVS := \
37 37
 	-e DOCKER_CLIENTONLY \
38 38
 	-e DOCKER_DEBUG \
39 39
 	-e DOCKER_EXPERIMENTAL \
40
-	-e DOCKERFILE \
41 40
 	-e DOCKER_GRAPHDRIVER \
42 41
 	-e DOCKER_INCREMENTAL_BINARY \
43 42
 	-e DOCKER_REMAP_ROOT \
... ...
@@ -1,34 +1,63 @@
1 1
 #!/bin/bash
2 2
 set -e
3 3
 
4
+docker-version-osarch() {
5
+	local target="$1" # "Client" or "Server"
6
+	local fmtStr="{{.${target}.Os}}/{{.${target}.Arch}}"
7
+	if docker version -f "$fmtStr" 2>/dev/null; then
8
+		# if "docker version -f" works, let's just use that!
9
+		return
10
+	fi
11
+	docker version | awk '
12
+		$1 ~ /^(Client|Server):$/ { section = 0 }
13
+		$1 == "'"$target"':" { section = 1; next }
14
+		section && $1 == "OS/Arch:" { print $2 }
15
+	'
16
+}
17
+
4 18
 # Retrieve OS/ARCH of docker daemon, eg. linux/amd64
5
-export DOCKER_ENGINE_OSARCH="$(docker version | awk '
6
-	$1 == "Client:" { server = 0; next }
7
-	$1 == "Server:" { server = 1; next }
8
-	server && $1 == "OS/Arch:" { print $2 }
9
-')"
19
+export DOCKER_ENGINE_OSARCH="$(docker-version-osarch 'Server')"
10 20
 export DOCKER_ENGINE_GOOS="${DOCKER_ENGINE_OSARCH%/*}"
11 21
 export DOCKER_ENGINE_GOARCH="${DOCKER_ENGINE_OSARCH##*/}"
12 22
 DOCKER_ENGINE_GOARCH=${DOCKER_ENGINE_GOARCH:=amd64}
13 23
 
14 24
 # and the client, just in case
15
-export DOCKER_CLIENT_OSARCH="$(docker version | awk '
16
-	$1 == "Client:" { client = 1; next }
17
-	$1 == "Server:" { client = 0; next }
18
-	client && $1 == "OS/Arch:" { print $2 }
19
-')"
25
+export DOCKER_CLIENT_OSARCH="$(docker-version-osarch 'Client')"
26
+export DOCKER_CLIENT_GOOS="${DOCKER_CLIENT_OSARCH%/*}"
27
+export DOCKER_CLIENT_GOARCH="${DOCKER_CLIENT_OSARCH##*/}"
28
+DOCKER_CLIENT_GOARCH=${DOCKER_CLIENT_GOARCH:=amd64}
20 29
 
21 30
 # Retrieve the architecture used in contrib/builder/(deb|rpm)/$PACKAGE_ARCH/
22
-PACKAGE_ARCH="amd64"
23
-case "$DOCKER_ENGINE_OSARCH" in
24
-	linux/arm)
31
+PACKAGE_ARCH='amd64'
32
+case "${DOCKER_ENGINE_GOARCH:-$DOCKER_CLIENT_GOARCH}" in
33
+	arm)
25 34
 		PACKAGE_ARCH='armhf'
26 35
 		;;
27
-	linux/ppc64le)
28
-		PACKAGE_ARCH='ppc64le'
36
+	arm64)
37
+		PACKAGE_ARCH='aarch64'
38
+		;;
39
+	amd64|ppc64le|s390x)
40
+		PACKAGE_ARCH="${DOCKER_ENGINE_GOARCH:-$DOCKER_CLIENT_GOARCH}"
29 41
 		;;
30
-	linux/s390x)
31
-		PACKAGE_ARCH='s390x'
42
+	*)
43
+		echo >&2 "warning: not sure how to convert '$DOCKER_ENGINE_GOARCH' to a 'Docker' arch, assuming '$PACKAGE_ARCH'"
32 44
 		;;
33 45
 esac
34 46
 export PACKAGE_ARCH
47
+
48
+DOCKERFILE='Dockerfile'
49
+TEST_IMAGE_NAMESPACE=
50
+case "$PACKAGE_ARCH" in
51
+	amd64)
52
+		case "${DOCKER_ENGINE_GOOS:-$DOCKER_CLIENT_GOOS}" in
53
+			windows)
54
+				DOCKERFILE='Dockerfile.windows'
55
+				;;
56
+		esac
57
+		;;
58
+	*)
59
+		DOCKERFILE="Dockerfile.$PACKAGE_ARCH"
60
+		TEST_IMAGE_NAMESPACE="$PACKAGE_ARCH"
61
+		;;
62
+esac
63
+export DOCKERFILE TEST_IMAGE_NAMESPACE
... ...
@@ -9,25 +9,9 @@ images=(
9 9
 	hello-world:latest
10 10
 )
11 11
 
12
-imagePrefix=
13
-case "$DOCKER_ENGINE_OSARCH" in
14
-	linux/arm)
15
-		imagePrefix='armhf'
16
-		;;
17
-	linux/arm64)
18
-		imagePrefix='aarch64'
19
-		;;
20
-	linux/ppc64le)
21
-		imagePrefix='ppc64le'
22
-		;;
23
-	linux/s390x)
24
-		imagePrefix='s390x'
25
-		;;
26
-esac
27
-
28
-if [ "$imagePrefix" ]; then
12
+if [ "$TEST_IMAGE_NAMESPACE" ]; then
29 13
 	for (( i = 0; i < ${#images[@]}; i++ )); do
30
-		images[$i]="$imagePrefix/${images[$i]}"
14
+		images[$i]="$TEST_IMAGE_NAMESPACE/${images[$i]}"
31 15
 	done
32 16
 fi
33 17
 
... ...
@@ -58,7 +42,7 @@ if ! docker inspect "${images[@]}" &> /dev/null; then
58 58
 					inCont = 0;
59 59
 				}
60 60
 			}
61
-		' "${DOCKERFILE:=Dockerfile}" | sh -x
61
+		' "$DOCKERFILE" | sh -x
62 62
 		# Do not use a subshell for the following command. Windows to Linux CI
63 63
 		# runs bash 3.x so will not trap an error in a subshell.
64 64
 		# http://stackoverflow.com/questions/22630363/how-does-set-e-work-with-subshells
... ...
@@ -66,9 +50,9 @@ if ! docker inspect "${images[@]}" &> /dev/null; then
66 66
 	fi
67 67
 fi
68 68
 
69
-if [ "$imagePrefix" ]; then
69
+if [ "$TEST_IMAGE_NAMESPACE" ]; then
70 70
 	for image in "${images[@]}"; do
71
-		target="${image#$imagePrefix/}"
71
+		target="${image#$TEST_IMAGE_NAMESPACE/}"
72 72
 		if [ "$target" != "$image" ]; then
73 73
 			# tag images to ensure that all integrations work with the defined image names
74 74
 			docker tag "$image" "$target"