Attempting to fix NFS issues
[apex.git] / apex / builders / common_builder.py
index e0e7303..59af94c 100644 (file)
@@ -59,17 +59,19 @@ def project_to_path(project, patch=None):
         return "/usr/lib/python2.7/site-packages/"
 
 
-def project_to_docker_image(project):
+def project_to_docker_image(project, docker_url):
     """
     Translates OpenStack project to OOO services that are containerized
-    :param project: name of OpenStack project
+    :param project: short name of OpenStack project
     :return: List of OOO docker service names
     """
     # Fetch all docker containers in docker hub with tripleo and filter
     # based on project
-
+    logging.info("Checking for docker images matching project: {}".format(
+        project))
     hub_output = utils.open_webpage(
-        urllib.parse.urljoin(con.DOCKERHUB_OOO, '?page_size=1024'), timeout=10)
+        urllib.parse.urljoin(docker_url,
+                             '?page_size=1024'), timeout=10)
     try:
         results = json.loads(hub_output.decode())['results']
     except Exception as e:
@@ -84,12 +86,14 @@ def project_to_docker_image(project):
     for result in results:
         if result['name'].startswith("centos-binary-{}".format(project)):
             # add as docker image shortname (just service name)
+            logging.debug("Adding docker image {} for project {} for "
+                          "patching".format(result['name'], project))
             docker_images.append(result['name'].replace('centos-binary-', ''))
 
     return docker_images
 
 
-def is_patch_promoted(change, branch, docker_image=None):
+def is_patch_promoted(change, branch, docker_url, docker_image=None):
     """
     Checks to see if a patch that is in merged exists in either the docker
     container or the promoted tripleo images
@@ -122,8 +126,8 @@ def is_patch_promoted(change, branch, docker_image=None):
             return True
     else:
         # must be a docker patch, check docker tag modified time
-        docker_url = con.DOCKERHUB_OOO.replace('tripleomaster',
-                                               "tripleo{}".format(branch))
+        docker_url = docker_url.replace('tripleomaster',
+                                        "tripleo{}".format(branch))
         url_path = "{}/tags/{}".format(docker_image, con.DOCKER_TAG)
         docker_url = urllib.parse.urljoin(docker_url, url_path)
         logging.debug("docker url is: {}".format(docker_url))
@@ -176,10 +180,23 @@ def add_upstream_patches(patches, image, tmp_dir,
         # and move the patch into the containers directory.  We also assume
         # this builder call is for overcloud, because we do not support
         # undercloud containers
+        if platform.machine() == 'aarch64':
+            docker_url = con.DOCKERHUB_AARCH64
+        else:
+            docker_url = con.DOCKERHUB_OOO
         if docker_tag and 'python' in project_path:
             # Projects map to multiple THT services, need to check which
             # are supported
-            ooo_docker_services = project_to_docker_image(patch['project'])
+            project_short_name = os.path.basename(patch['project'])
+            ooo_docker_services = project_to_docker_image(project_short_name,
+                                                          docker_url)
+            if not ooo_docker_services:
+                logging.error("Did not find any matching docker containers "
+                              "for project: {}".format(project_short_name))
+                raise exc.ApexCommonBuilderException(
+                    'Unable to find docker services for python project in '
+                    'patch')
+            # Just use the first image to see if patch was promoted into it
             docker_img = ooo_docker_services[0]
         else:
             ooo_docker_services = []
@@ -189,28 +206,43 @@ def add_upstream_patches(patches, image, tmp_dir,
                                         patch['change-id'])
         patch_promoted = is_patch_promoted(change,
                                            branch.replace('stable/', ''),
+                                           docker_url,
                                            docker_img)
 
         if patch_diff and not patch_promoted:
             patch_file = "{}.patch".format(patch['change-id'])
+            patch_file_paths = []
             # If we found services, then we treat the patch like it applies to
             # docker only
             if ooo_docker_services:
                 os_version = default_branch.replace('stable/', '')
                 for service in ooo_docker_services:
                     docker_services = docker_services.union({service})
+                    # We need to go root to be able to install patch and then
+                    # switch back to previous user. Some containers that
+                    # have the same name as the project do not necessarily
+                    # contain the project code. For example
+                    # novajoin-notifier does not contain nova package code.
+                    # Therefore we must try to patch and unfortunately
+                    # ignore failures until we have a better way of checking
+                    # this
                     docker_cmds = [
                         "WORKDIR {}".format(project_path),
+                        "USER root",
+                        "ARG REAL_USER",
+                        "RUN yum -y install patch",
                         "ADD {} {}".format(patch_file, project_path),
-                        "RUN patch -p1 < {}".format(patch_file)
+                        "RUN patch -p1 < {} || echo "
+                        "'Patching failed'".format(patch_file),
+                        "USER $REAL_USER"
                     ]
                     src_img_uri = "{}:8787/tripleo{}/centos-binary-{}:" \
                                   "{}".format(uc_ip, os_version, service,
                                               docker_tag)
                     oc_builder.build_dockerfile(service, tmp_dir, docker_cmds,
                                                 src_img_uri)
-                patch_file_path = os.path.join(tmp_dir, 'containers',
-                                               patch_file)
+                    patch_file_paths.append(os.path.join(
+                        tmp_dir, "containers/{}".format(service), patch_file))
             else:
                 patch_file_path = os.path.join(tmp_dir, patch_file)
                 virt_ops.extend([
@@ -220,8 +252,10 @@ def add_upstream_patches(patches, image, tmp_dir,
                         project_path, patch_file)}])
                 logging.info("Adding patch {} to {}".format(patch_file,
                                                             image))
-            with open(patch_file_path, 'w') as fh:
-                fh.write(patch_diff)
+                patch_file_paths.append(patch_file_path)
+            for patch_fp in patch_file_paths:
+                with open(patch_fp, 'w') as fh:
+                    fh.write(patch_diff)
         else:
             logging.info("Ignoring patch:\n{}".format(patch))
     if len(virt_ops) > 1:
@@ -265,14 +299,15 @@ def create_git_archive(repo_url, repo_name, tmp_dir,
 
 def get_neutron_driver(ds_opts):
     sdn = ds_opts.get('sdn_controller', None)
-    for controllers in 'opendaylight', 'ovn':
-        if sdn == controllers:
-            return sdn
 
-    if ds_opts.get('vpp', False):
+    if sdn == 'opendaylight':
+        return 'odl'
+    elif sdn == 'ovn':
+        return sdn
+    elif ds_opts.get('vpp', False):
         return 'vpp'
-
-    return None
+    else:
+        return None
 
 
 def prepare_container_images(prep_file, branch='master', neutron_driver=None):
@@ -287,7 +322,8 @@ def prepare_container_images(prep_file, branch='master', neutron_driver=None):
             p_set['neutron_driver'] = neutron_driver
         p_set['namespace'] = "docker.io/tripleo{}".format(branch)
         if platform.machine() == 'aarch64':
-            p_set['ceph_tag'] = 'master-fafda7d-luminous-centos-7-aarch64'
+            p_set['namespace'] = "docker.io/armbandapex"
+            p_set['ceph_tag'] = 'v3.1.0-stable-3.1-luminous-centos-7-aarch64'
 
     except KeyError:
         logging.error("Invalid prep file format: {}".format(prep_file))