Browse Source

Merge pull request #461 from tomdee/port-mk-docker-opts

mk-docker-opts.sh: replace with busybox shell compatible version
Tom Denham 8 năm trước cách đây
mục cha
commit
d0087f54f8
4 tập tin đã thay đổi với 84 bổ sung11 xóa
  1. 1 0
      Makefile
  2. 18 11
      dist/mk-docker-opts.sh
  3. 61 0
      dist/mk-docker-opts_tests.sh
  4. 4 0
      dist/sample_subnet.env

+ 1 - 0
Makefile

@@ -20,6 +20,7 @@ artifacts/flanneld: $(shell find . -type f  -name '*.go')
 
 test:
 	go test -cover $(TEST_PACKAGES_EXPANDED)
+	cd dist; ./mk-docker-opts_tests.sh
 
 cover:
 	#A single package must be given - e.g. 'PACKAGES=pkg/ip make cover'

+ 18 - 11
dist/mk-docker-opts.sh

@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 
 usage() {
 	echo "$0 [-f FLANNEL-ENV-FILE] [-d DOCKER-ENV-FILE] [-i] [-c] [-m] [-k COMBINED-KEY]
@@ -11,7 +11,7 @@ OPTIONS:
 	-c	Output combined Docker options into DOCKER_OPTS var
 	-k	Set the combined options key to this value (default DOCKER_OPTS=)
 	-m	Do not output --ip-masq (useful for older Docker version)
-" >/dev/stderr 
+" >&2
 
 	exit 1
 }
@@ -23,7 +23,7 @@ indiv_opts=false
 combined_opts=false
 ipmasq=true
 
-while getopts "f:d:icmk:" opt; do
+while getopts "f:d:icmk:?h" opt; do
 	case $opt in
 		f)
 			flannel_env=$OPTARG
@@ -43,7 +43,7 @@ while getopts "f:d:icmk:" opt; do
 		k)
 			combined_opts_key=$OPTARG
 			;;
-		\?)
+		[\?h])
 			usage
 			;;
 	esac
@@ -55,7 +55,7 @@ if [ $indiv_opts = false ] && [ $combined_opts = false ]; then
 fi
 
 if [ -f "$flannel_env" ]; then
-	source $flannel_env
+	. $flannel_env
 fi
 
 if [ -n "$FLANNEL_SUBNET" ]; then
@@ -72,23 +72,30 @@ if [ -n "$FLANNEL_IPMASQ" ] && [ $ipmasq = true ] ; then
 	elif [ "$FLANNEL_IPMASQ" = false ] ; then
 		DOCKER_OPT_IPMASQ="--ip-masq=true"
 	else
-		echo "Invalid value of FLANNEL_IPMASQ: $FLANNEL_IPMASQ" > /dev/stderr
+		echo "Invalid value of FLANNEL_IPMASQ: $FLANNEL_IPMASQ" >&2
 		exit 1
 	fi
 fi
 
 eval docker_opts="\$${combined_opts_key}"
-docker_opts+=" "
+
+if [ "$docker_opts" ]; then
+	docker_opts="$docker_opts ";
+fi
 
 echo -n "" >$docker_env
-for opt in $(compgen -v DOCKER_OPT_); do
-	eval val=\$$opt
+
+for opt in $(set | grep "DOCKER_OPT_"); do
+
+	OPT_NAME=$(echo $opt | awk -F "=" '{print $1;}');
+	OPT_VALUE=$(eval echo "\$$OPT_NAME");
 
 	if [ "$indiv_opts" = true ]; then
-		echo "$opt=\"$val\"" >>$docker_env
+		echo "$OPT_NAME=\"$OPT_VALUE\"" >>$docker_env;
 	fi
 
-	docker_opts+="$val "
+	docker_opts="$docker_opts $OPT_VALUE";
+
 done
 
 if [ "$combined_opts" = true ]; then

+ 61 - 0
dist/mk-docker-opts_tests.sh

@@ -0,0 +1,61 @@
+#!/bin/bash
+set -e
+
+echo "### Dry run with input & output files set"
+echo "$ ./mk-docker-opts.sh -f ./sample_subnet.env -d here.txt"
+! read -d '' EXPECTED <<EOF 
+DOCKER_OPT_BIP="--bip=10.1.74.1/24"
+DOCKER_OPT_IPMASQ="--ip-masq=true"
+DOCKER_OPT_MTU="--mtu=1472"
+DOCKER_OPTS=" --bip=10.1.74.1/24 --ip-masq=true --mtu=1472"
+EOF
+./mk-docker-opts.sh -f ./sample_subnet.env -d here.txt
+diff -B -b here.txt <(echo -e "${EXPECTED}")
+echo
+
+
+echo "### Individual vars only (Note DOCKER_OPTS= is missing)"
+echo "$ ./mk-docker-opts.sh -f ./sample_subnet.env -d here.txt -i"
+! read -d '' EXPECTED <<EOF 
+DOCKER_OPT_BIP="--bip=10.1.74.1/24"
+DOCKER_OPT_IPMASQ="--ip-masq=true"
+DOCKER_OPT_MTU="--mtu=1472"
+EOF
+./mk-docker-opts.sh -f ./sample_subnet.env -d here.txt -i
+diff -B -b here.txt <(echo -e "${EXPECTED}")
+echo
+
+
+echo "### Combined vars only (Note DOCKER_OPT_* vars are missing)"
+echo "$ ./mk-docker-opts.sh -f ./sample_subnet.env -d here.txt -c"
+! read -d '' EXPECTED <<EOF 
+DOCKER_OPTS=" --bip=10.1.74.1/24 --ip-masq=true --mtu=1472"
+EOF
+./mk-docker-opts.sh -f ./sample_subnet.env -d here.txt -c
+diff -B -b here.txt <(echo -e "${EXPECTED}")
+echo
+
+
+echo "### Custom key test (Note DOCKER_OPTS= is substituted by CUSTOM_KEY=)"
+echo "$ ./mk-docker-opts.sh -f ./sample_subnet.env -d here.txt -k CUSTOM_KEY"
+! read -d '' EXPECTED <<EOF 
+DOCKER_OPT_BIP="--bip=10.1.74.1/24"
+DOCKER_OPT_IPMASQ="--ip-masq=true"
+DOCKER_OPT_MTU="--mtu=1472"
+CUSTOM_KEY=" --bip=10.1.74.1/24 --ip-masq=true --mtu=1472"
+EOF
+./mk-docker-opts.sh -f ./sample_subnet.env -d here.txt -k CUSTOM_KEY
+diff -B -b here.txt <(echo -e "${EXPECTED}")
+echo
+
+
+echo "### Ip-masq stripping test (Note DOCKER_OPT_IPMASQ and --ip-masq=true are missing)"
+echo "$ ./mk-docker-opts.sh -f ./sample_subnet.env -d here.txt -m"
+! read -d '' EXPECTED <<EOF 
+DOCKER_OPT_BIP="--bip=10.1.74.1/24"
+DOCKER_OPT_MTU="--mtu=1472"
+DOCKER_OPTS=" --bip=10.1.74.1/24 --mtu=1472"
+EOF
+./mk-docker-opts.sh -f ./sample_subnet.env -d here.txt -m
+diff -B -b here.txt <(echo -e "${EXPECTED}")
+

+ 4 - 0
dist/sample_subnet.env

@@ -0,0 +1,4 @@
+FLANNEL_NETWORK=10.1.0.0/16
+FLANNEL_SUBNET=10.1.74.1/24
+FLANNEL_MTU=1472
+FLANNEL_IPMASQ=false