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