github.com/ojongerius/docker@v1.11.2/hack/Jenkins/W2L/setup.sh (about)

     1  # Jenkins CI script for Windows to Linux CI.
     2  # Heavily modified by John Howard (@jhowardmsft) December 2015 to try to make it more reliable.
     3  set +xe
     4  SCRIPT_VER="Thu Feb 25 18:54:57 UTC 2016"
     5  
     6  # TODO to make (even) more resilient: 
     7  #  - Wait for daemon to be running before executing docker commands
     8  #  - Check if jq is installed
     9  #  - Make sure bash is v4.3 or later. Can't do until all Azure nodes on the latest version
    10  #  - Make sure we are not running as local system. Can't do until all Azure nodes are updated.
    11  #  - Error if docker versions are not equal. Can't do until all Azure nodes are updated
    12  #  - Error if go versions are not equal. Can't do until all Azure nodes are updated.
    13  #  - Error if running 32-bit posix tools. Probably can take from bash --version and check contains "x86_64"
    14  #  - Warn if the CI directory cannot be deleted afterwards. Otherwise turdlets are left behind
    15  #  - Use %systemdrive% ($SYSTEMDRIVE) rather than hard code to c: for TEMP
    16  #  - Consider cross builing the Windows binary and copy across. That's a bit of a heavy lift. Only reason
    17  #    for doing that is that it mirrors the actual release process for docker.exe which is cross-built.
    18  #    However, should absolutely not be a problem if built natively, so nit-picking.
    19  #  - Tidy up of images and containers. Either here, or in the teardown script.
    20  
    21  ec=0
    22  uniques=1
    23  echo INFO: Started at `date`. Script version $SCRIPT_VER
    24  
    25  
    26  # !README!
    27  # There are two daemons running on the remote Linux host:
    28  # 	- outer: specified by DOCKER_HOST, this is the daemon that will build and run the inner docker daemon
    29  #			from the sources matching the PR.
    30  #	- inner: runs on the host network, on a port number similar to that of DOCKER_HOST but the last two digits are inverted
    31  #			(2357 if DOCKER_HOST had port 2375; and 2367 if DOCKER_HOST had port 2376).
    32  #			The windows integration tests are run against this inner daemon.
    33  
    34  # get the ip, inner and outer ports.
    35  ip="${DOCKER_HOST#*://}"
    36  port_outer="${ip#*:}"
    37  # inner port is like outer port with last two digits inverted.
    38  port_inner=$(echo "$port_outer" | sed -E 's/(.)(.)$/\2\1/')
    39  ip="${ip%%:*}"
    40  
    41  echo "INFO: IP=$ip PORT_OUTER=$port_outer PORT_INNER=$port_inner"
    42  
    43  # If TLS is enabled
    44  if [ -n "$DOCKER_TLS_VERIFY" ]; then
    45  	protocol=https
    46  	if [ -z "$DOCKER_MACHINE_NAME" ]; then
    47  		ec=1
    48  		echo "ERROR: DOCKER_MACHINE_NAME is undefined"
    49  	fi
    50  	certs=$(echo ~/.docker/machine/machines/$DOCKER_MACHINE_NAME)
    51  	curlopts="--cacert $certs/ca.pem --cert $certs/cert.pem --key $certs/key.pem"
    52  	run_extra_args="-v tlscerts:/etc/docker"
    53  	daemon_extra_args="--tlsverify --tlscacert /etc/docker/ca.pem --tlscert /etc/docker/server.pem --tlskey /etc/docker/server-key.pem"
    54  else
    55  	protocol=http
    56  fi
    57  
    58  # Save for use by make.sh and scripts it invokes
    59  export MAIN_DOCKER_HOST="tcp://$ip:$port_inner"
    60  
    61  # Verify we can get the remote node to respond to _ping
    62  if [ $ec -eq 0 ]; then
    63  	reply=`curl -s $curlopts $protocol://$ip:$port_outer/_ping`
    64  	if [ "$reply" != "OK" ]; then
    65  		ec=1
    66  		echo "ERROR: Failed to get an 'OK' response from the docker daemon on the Linux node"
    67  		echo "       at $ip:$port_outer when called with an http request for '_ping'. This implies that"
    68  		echo "       either the daemon has crashed/is not running, or the Linux node is unavailable."
    69  		echo
    70  		echo "       A regular ping to the remote Linux node is below. It should reply. If not, the"
    71  		echo "       machine cannot be reached at all and may have crashed. If it does reply, it is"
    72  		echo "       likely a case of the Linux daemon not running or having crashed, which requires"
    73  		echo "       further investigation."
    74  		echo
    75  		echo "       Try re-running this CI job, or ask on #docker-dev or #docker-maintainers"
    76  		echo "       for someone to perform further diagnostics, or take this node out of rotation."
    77  		echo
    78  		ping $ip
    79  	else
    80  		echo "INFO: The Linux nodes outer daemon replied to a ping. Good!"
    81  	fi 
    82  fi
    83  
    84  # Get the version from the remote node. Note this may fail if jq is not installed.
    85  # That's probably worth checking to make sure, just in case.
    86  if [ $ec -eq 0 ]; then
    87  	remoteVersion=`curl -s $curlopts $protocol://$ip:$port_outer/version | jq -c '.Version'`
    88  	echo "INFO: Remote daemon is running docker version $remoteVersion"
    89  fi
    90  
    91  # Compare versions. We should really fail if result is no 1. Output at end of script.
    92  if [ $ec -eq 0 ]; then
    93  	uniques=`docker version | grep Version | /usr/bin/sort -u | wc -l`
    94  fi
    95  
    96  # Make sure we are in repo
    97  if [ $ec -eq 0 ]; then
    98  	if [ ! -d hack ]; then
    99  		echo "ERROR: Are you sure this is being launched from a the root of docker repository?"
   100  		echo "       If this is a Windows CI machine, it should be c:\jenkins\gopath\src\github.com\docker\docker."
   101                  echo "       Current directory is `pwd`"
   102  		ec=1
   103  	fi
   104  fi
   105  
   106  # Get the commit has and verify we have something
   107  if [ $ec -eq 0 ]; then
   108  	export COMMITHASH=$(git rev-parse --short HEAD)
   109  	echo INFO: Commmit hash is $COMMITHASH
   110  	if [ -z $COMMITHASH ]; then
   111  		echo "ERROR: Failed to get commit hash. Are you sure this is a docker repository?"
   112  		ec=1
   113  	fi
   114  fi
   115  
   116  # Redirect to a temporary location. Check is here for local runs from Jenkins machines just in case not
   117  # in the right directory where the repo is cloned. We also redirect TEMP to not use the environment
   118  # TEMP as when running as a standard user (not local system), it otherwise exposes a bug in posix tar which
   119  # will cause CI to fail from Windows to Linux. Obviously it's not best practice to ever run as local system...
   120  if [ $ec -eq 0 ]; then
   121  	export TEMP=/c/CI/CI-$COMMITHASH
   122  	export TMP=$TMP
   123  	/usr/bin/mkdir -p $TEMP  # Make sure Linux mkdir for -p
   124  fi
   125  
   126  # Tidy up time
   127  if [ $ec -eq 0 ]; then
   128  	echo INFO: Deleting pre-existing containers and images...
   129  	# Force remove all containers based on a previously built image with this commit
   130  	! docker rm -f $(docker ps -aq --filter "ancestor=docker:$COMMITHASH") &>/dev/null
   131  
   132  	# Force remove any container with this commithash as a name
   133  	! docker rm -f $(docker ps -aq --filter "name=docker-$COMMITHASH") &>/dev/null
   134  
   135  	# Force remove the image if it exists
   136  	! docker rmi -f "docker-$COMMITHASH" &>/dev/null
   137  	
   138  	# This SHOULD never happen, but just in case, also blow away any containers
   139  	# that might be around. 
   140  	! if [ ! `docker ps -aq | wc -l` -eq 0 ]; then
   141  		echo WARN: There were some leftover containers. Cleaning them up.
   142  		! docker rm -f $(docker ps -aq)
   143  	fi
   144  fi
   145  
   146  # Provide the docker version for debugging purposes. If these fail, game over. 
   147  # as the Linux box isn't responding for some reason.
   148  if [ $ec -eq 0 ]; then
   149  	echo INFO: Docker version and info of the outer daemon on the Linux node
   150  	echo
   151  	docker version
   152  	ec=$?
   153  	if [ 0 -ne $ec ]; then
   154  		echo "ERROR: The main linux daemon does not appear to be running. Has the Linux node crashed?"
   155  	fi
   156  	echo
   157  fi
   158  
   159  # Same as above, but docker info
   160  if [ $ec -eq 0 ]; then
   161  	echo
   162  	docker info
   163  	ec=$?
   164  	if [ 0 -ne $ec ]; then
   165  		echo "ERROR: The main linux daemon does not appear to be running. Has the Linux node crashed?"
   166  	fi
   167  	echo
   168  fi
   169  
   170  # build the daemon image
   171  if [ $ec -eq 0 ]; then
   172  	echo "INFO: Running docker build on Linux host at $DOCKER_HOST"
   173  	set -x
   174  	docker build --rm --force-rm -t "docker:$COMMITHASH" .
   175  	ec=$?
   176  	set +x
   177  	if [ 0 -ne $ec ]; then
   178  		echo "ERROR: docker build failed"
   179  	fi
   180  fi
   181  
   182  # Start the docker-in-docker daemon from the image we just built
   183  if [ $ec -eq 0 ]; then
   184  	echo "INFO: Starting build of a Linux daemon to test against, and starting it..."
   185  	set -x
   186  	# aufs in aufs is faster than vfs in aufs
   187  	docker run $run_extra_args -e DOCKER_GRAPHDRIVER=aufs --pid host --privileged -d --name "docker-$COMMITHASH" --net host "docker:$COMMITHASH" bash -c "echo 'INFO: Compiling' && date && hack/make.sh binary && echo 'INFO: Compile complete' && date && cp bundles/$(cat VERSION)/binary/docker /bin/docker && echo 'INFO: Starting daemon' && exec docker daemon -D -H tcp://0.0.0.0:$port_inner $daemon_extra_args"
   188  	ec=$?
   189  	set +x
   190  	if [ 0 -ne $ec ]; then
   191  	    	echo "ERROR: Failed to compile and start the linux daemon"
   192  	fi
   193  fi
   194  
   195  # Build locally.
   196  if [ $ec -eq 0 ]; then
   197  	echo "INFO: Starting local build of Windows binary..."
   198  	set -x
   199  	export TIMEOUT="5m"
   200  	export DOCKER_HOST="tcp://$ip:$port_inner"
   201  	export DOCKER_TEST_HOST="tcp://$ip:$port_inner"
   202  	unset DOCKER_CLIENTONLY
   203  	export DOCKER_REMOTE_DAEMON=1
   204  	hack/make.sh binary 
   205  	ec=$?
   206  	set +x
   207  	if [ 0 -ne $ec ]; then
   208  	    echo "ERROR: Build of binary on Windows failed"
   209  	fi
   210  fi
   211  
   212  # Make a local copy of the built binary and ensure that is first in our path
   213  if [ $ec -eq 0 ]; then
   214  	VERSION=$(< ./VERSION)
   215  	cp bundles/$VERSION/binary/docker.exe $TEMP
   216  	ec=$?
   217  	if [ 0 -ne $ec ]; then
   218  		echo "ERROR: Failed to copy built binary to $TEMP"
   219  	fi
   220  	export PATH=$TEMP:$PATH
   221  fi
   222  
   223  # Run the integration tests
   224  if [ $ec -eq 0 ]; then
   225  	echo "INFO: Running Integration tests..."
   226  	set -x
   227  	export DOCKER_TEST_TLS_VERIFY="$DOCKER_TLS_VERIFY"
   228  	export DOCKER_TEST_CERT_PATH="$DOCKER_CERT_PATH"
   229  	hack/make.sh test-integration-cli
   230  	ec=$?
   231  	set +x
   232  	if [ 0 -ne $ec ]; then
   233  		echo "ERROR: CLI test failed."
   234  		# Next line is useful, but very long winded if included
   235  		# docker -H=$MAIN_DOCKER_HOST logs "docker-$COMMITHASH"
   236  	fi
   237  fi
   238  
   239  # Tidy up any temporary files from the CI run
   240  if [ ! -z $COMMITHASH ]; then
   241  	rm -rf $TEMP
   242  fi
   243  
   244  # CI Integrity check - ensure we are using the same version of go as present in the Dockerfile
   245  GOVER_DOCKERFILE=`grep 'ENV GO_VERSION' Dockerfile | awk '{print $3}'`
   246  GOVER_INSTALLED=`go version | awk '{print $3}'`
   247  if [ "${GOVER_INSTALLED:2}" != "$GOVER_DOCKERFILE" ]; then
   248  	#ec=1  # Uncomment to make CI fail once all nodes are updated.
   249  	echo
   250  	echo "---------------------------------------------------------------------------"
   251  	echo "WARN: CI should be using go version $GOVER_DOCKERFILE, but is using ${GOVER_INSTALLED:2}"
   252  	echo "      Please ping #docker-maintainers on IRC to get this CI server updated."
   253  	echo "---------------------------------------------------------------------------"
   254  	echo
   255  fi
   256  
   257  # Check the Linux box is running a matching version of docker
   258  if [ "$uniques" -ne 1 ]; then
   259      ec=0  # Uncomment to make CI fail once all nodes are updated.
   260  	echo
   261  	echo "---------------------------------------------------------------------------"
   262  	echo "ERROR: This CI node is not running the same version of docker as the daemon."
   263  	echo "       This is a CI configuration issue."
   264  	echo "---------------------------------------------------------------------------"
   265  	echo
   266  fi
   267  
   268  # Tell the user how we did.
   269  if [ $ec -eq 0 ]; then
   270  	echo INFO: Completed successfully at `date`. 
   271  else
   272  	echo ERROR: Failed with exitcode $ec at `date`.
   273  fi
   274  exit $ec