Fixes Docker image upload for master/rocky
[apex.git] / apex / builders / common_builder.py
index a5f301b..e0e7303 100644 (file)
@@ -14,8 +14,11 @@ import git
 import json
 import logging
 import os
+import platform
+import pprint
 import re
 import urllib.parse
+import yaml
 
 import apex.builders.overcloud_builder as oc_builder
 from apex import build_utils
@@ -25,10 +28,11 @@ from apex.common import utils
 from apex.virtual import utils as virt_utils
 
 
-def project_to_path(project):
+def project_to_path(project, patch=None):
     """
     Translates project to absolute file path to use in patching
     :param project: name of project
+    :param patch: the patch to applied to the project
     :return: File path
     """
     if project.startswith('openstack/'):
@@ -37,6 +41,15 @@ def project_to_path(project):
         return "/etc/puppet/modules/{}".format(project.replace('puppet-', ''))
     elif 'tripleo-heat-templates' in project:
         return "/usr/share/openstack-tripleo-heat-templates"
+    elif ('tripleo-common' in project and
+          build_utils.is_path_in_patch(patch, 'container-images/')):
+        # tripleo-common has python and another component to it
+        # here we detect if there is a change to the yaml component and if so
+        # treat it like it is not python. This has the caveat of if there
+        # is a patch to both python and yaml this will not work
+        # FIXME(trozet): add ability to split tripleo-common patches that
+        # modify both python and yaml
+        return "/usr/share/openstack-tripleo-common-containers/"
     else:
         # assume python.  python patches will apply to a project name subdir.
         # For example, python-tripleoclient patch will apply to the
@@ -157,7 +170,7 @@ def add_upstream_patches(patches, image, tmp_dir,
             branch = default_branch
         patch_diff = build_utils.get_patch(patch['change-id'],
                                            patch['project'], branch)
-        project_path = project_to_path(patch['project'])
+        project_path = project_to_path(patch['project'], patch_diff)
         # If docker tag and python we know this patch belongs on docker
         # container for a docker service. Therefore we build the dockerfile
         # and move the patch into the containers directory.  We also assume
@@ -248,3 +261,39 @@ def create_git_archive(repo_url, repo_name, tmp_dir,
         repo.archive(fh, prefix=prefix)
     logging.debug("Wrote archive file: {}".format(archive_path))
     return archive_path
+
+
+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):
+        return 'vpp'
+
+    return None
+
+
+def prepare_container_images(prep_file, branch='master', neutron_driver=None):
+    if not os.path.isfile(prep_file):
+        raise exc.ApexCommonBuilderException("Prep file does not exist: "
+                                             "{}".format(prep_file))
+    with open(prep_file) as fh:
+        data = yaml.safe_load(fh)
+    try:
+        p_set = data['parameter_defaults']['ContainerImagePrepare'][0]['set']
+        if neutron_driver:
+            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'
+
+    except KeyError:
+        logging.error("Invalid prep file format: {}".format(prep_file))
+        raise exc.ApexCommonBuilderException("Invalid format for prep file")
+
+    logging.debug("Writing new container prep file:\n{}".format(
+        pprint.pformat(data)))
+    with open(prep_file, 'w') as fh:
+        yaml.safe_dump(data, fh, default_flow_style=False)