Bug 1290531 - Move image tag resolution to Python; r?dustin draft
authorGregory Szorc <gps@mozilla.com>
Fri, 29 Jul 2016 13:06:10 -0700
changeset 395035 79f38d98e9e731d66cd40448e340c8b7a3823912
parent 395034 d56e5fb1b16db4f8e6d9943f8e66314d1ac9cdb0
child 395036 77a2afa9021e7d609d353f5801baacca4e0980b6
push id24705
push userbmo:gps@mozilla.com
push dateMon, 01 Aug 2016 18:26:57 +0000
reviewersdustin
bugs1290531
milestone50.0a1
Bug 1290531 - Move image tag resolution to Python; r?dustin We already had code for resolving the image registry and tag. We refactored it slightly to be more useful then changed build.sh to accept the tag as an argument. At this point, build.sh is basically a wrapper around `docker`. But there's a special case for executing custom "build.sh" files we need to eliminate first... MozReview-Commit-ID: A9HVvxgCdG2
taskcluster/taskgraph/docker.py
taskcluster/taskgraph/util/docker.py
testing/docker/build.sh
--- a/taskcluster/taskgraph/docker.py
+++ b/taskcluster/taskgraph/docker.py
@@ -72,21 +72,31 @@ def build_image(name):
     """
     if not name:
         raise ValueError('must provide a Docker image name')
 
     image_dir = os.path.join(IMAGE_DIR, name)
     if not os.path.isdir(image_dir):
         raise Exception('image directory does not exist: %s' % image_dir)
 
+    tag = docker.docker_image(name, default_version='latest')
+
     docker_bin = which.which('docker')
 
     # Verify that Docker is working.
     try:
         subprocess.check_output([docker_bin, '--version'])
     except subprocess.CalledProcessError:
         raise Exception('Docker server is unresponsive. Run `docker ps` and '
                         'check that Docker is running')
 
-    args = [os.path.join(IMAGE_DIR, 'build.sh'), name]
+    args = [os.path.join(IMAGE_DIR, 'build.sh'), name, tag]
     res = subprocess.call(args, cwd=IMAGE_DIR)
     if res:
         raise Exception('error building image')
+
+    if tag.endswith(':latest'):
+        print('*' * 50)
+        print('WARNING: no VERSION file found in image directory.')
+        print('Image is not suitable for deploying/pushing.')
+        print('Create an image suitable for deploying/pushing by creating')
+        print('a VERSION file in the image directory.')
+        print('*' * 50)
--- a/taskcluster/taskgraph/util/docker.py
+++ b/taskcluster/taskgraph/util/docker.py
@@ -13,28 +13,34 @@ from mozpack.archive import (
 )
 
 
 GECKO = os.path.realpath(os.path.join(__file__, '..', '..', '..', '..'))
 DOCKER_ROOT = os.path.join(GECKO, 'testing', 'docker')
 ARTIFACT_URL = 'https://queue.taskcluster.net/v1/task/{}/artifacts/{}'
 
 
-def docker_image(name):
+def docker_image(name, default_version=None):
     '''Determine the docker image name, including repository and tag, from an
     in-tree docker file.'''
     try:
         with open(os.path.join(DOCKER_ROOT, name, 'REGISTRY')) as f:
             registry = f.read().strip()
     except IOError:
         with open(os.path.join(DOCKER_ROOT, 'REGISTRY')) as f:
             registry = f.read().strip()
 
-    with open(os.path.join(DOCKER_ROOT, name, 'VERSION')) as f:
-        version = f.read().strip()
+    try:
+        with open(os.path.join(DOCKER_ROOT, name, 'VERSION')) as f:
+            version = f.read().strip()
+    except IOError:
+        if not default_version:
+            raise
+
+        version = default_version
 
     return '{}/{}:{}'.format(registry, name, version)
 
 
 def generate_context_hash(topsrcdir, image_path, image_name):
     """Generates a sha256 hash for context directory used to build an image."""
 
     # It is a bit unfortunate we have to create a temp file here - it would
--- a/testing/docker/build.sh
+++ b/testing/docker/build.sh
@@ -1,79 +1,26 @@
 #! /bin/bash -e
 
 # This file is a wrapper around docker build with specific concerns around image
 # versions and registry deployment... It also attempts to detect any potential
 # missing dependencies and warns you about them.
 
 gecko_root="$( cd "$( dirname "${BASH_SOURCE[0]}" )/../.." && pwd )"
 
-usage() {
-  echo "Build a docker image (and tag it)"
-  echo
-  echo "$0 <image-name>"
-  echo
-  echo "  Images are defined in testing/docker/<image-name>."
-  echo "  For more see: $PWD/README.md"
-  echo
-}
-
-usage_err() {
-  echo $1
-  echo
-  usage
-  exit 1
-}
-
 build() {
   local image_name=$1
+  local tag=$2
   local folder="$gecko_root/testing/docker/$image_name"
-  local folder_reg="$folder/REGISTRY"
-  local folder_ver="$folder/VERSION"
-  local could_deploy=false
-
-  # Assume that if an image context directory does not contain a VERSION file then
-  # it is not suitable for deploying.  Default to using 'latest' as the tag and
-  # warn the user at the end.
-  if [ ! -f $folder_ver ]; then
-    echo "This image does not contain a VERSION file.  Will use 'latest' as the image version"
-    local tag="$image_name:latest"
-  else
-    local version=$(cat $folder_ver)
-    test -n "$version" || usage_err "$folder_ver is empty aborting..."
-
-    # Fallback to default registry if one is not in the folder...
-    if [ ! -f "$folder_reg" ]; then
-      folder_reg=$PWD/REGISTRY
-    fi
-
-    local registry=$(cat $folder_reg)
-    test -n "$registry" || usage_err "$folder_reg is empty aborting..."
-
-    local tag="$registry/$image_name:$version"
-    local could_deploy=true
-  fi
 
   if [ -f $folder/build.sh ]; then
     shift
     $folder/build.sh -t $tag $* || exit 1
   else
     # use --no-cache so that we always get the latest updates from yum
     # and use the latest version of system-setup.sh
     ( cd $folder/.. && docker build --no-cache -t $tag $image_name ) || exit 1
   fi
 
   echo "Success built $image_name and tagged with $tag"
-  if [ "$could_deploy" = true ]; then
-    echo "If deploying now you can run 'docker push $tag'"
-  else
-    echo "*****************************************************************"
-    echo "WARNING: No VERSION file was found in the image directory."
-    echo "Image has not been prepared for deploying at this time."
-    echo "However, the image can be run locally. To prepare to "
-    echo "push to a user account on a docker registry, tag the image "
-    echo "by running 'docker tag $tag [REGISTRYHOST/][USERNAME/]NAME[:TAG]"
-    echo "prior to running 'docker push'."
-    echo "*****************************************************************"
-  fi
 }
 
 build $*