From 363181c64c3b9ce8631e8d3fb5c3b9ce46d63c18 Mon Sep 17 00:00:00 2001
From: Mark Hamzy <hamzy@us.ibm.com>
Date: Sat, 11 Nov 2017 14:05:09 -0600
Subject: [PATCH] fix_multi_arch tripleo-common

---
 tripleo_common/actions/baremetal.py   | 20 +++--------
 tripleo_common/actions/validations.py | 65 ++++++++++++++++++++++++-----------
 tripleo_common/utils/nodes.py         | 37 ++++++++++++++------
 workbooks/baremetal.yaml              | 16 +--------
 workbooks/validations.yaml            | 39 +++++----------------
 5 files changed, 86 insertions(+), 91 deletions(-)

diff --git a/tripleo_common/actions/baremetal.py b/tripleo_common/actions/baremetal.py
index dc73355..9fca802 100644
--- a/tripleo_common/actions/baremetal.py
+++ b/tripleo_common/actions/baremetal.py
@@ -36,21 +36,16 @@ class RegisterOrUpdateNodes(base.TripleOAction):
 
     :param nodes_json: list of nodes & attributes in json format
     :param remove: Should nodes not in the list be removed?
-    :param kernel_name: Glance ID of the kernel to use for the nodes.
-    :param ramdisk_name: Glance ID of the ramdisk to use for the nodes.
     :param instance_boot_option: Whether to set instances for booting from
                                  local hard drive (local) or network (netboot).
     :return: list of node objects representing the new nodes.
     """
 
-    def __init__(self, nodes_json, remove=False, kernel_name=None,
-                 ramdisk_name=None, instance_boot_option='local'):
+    def __init__(self, nodes_json, remove=False, instance_boot_option='local'):
         super(RegisterOrUpdateNodes, self).__init__()
         self.nodes_json = nodes_json
         self.remove = remove
         self.instance_boot_option = instance_boot_option
-        self.kernel_name = kernel_name
-        self.ramdisk_name = ramdisk_name
 
     def run(self, context):
         for node in self.nodes_json:
@@ -67,9 +62,7 @@ class RegisterOrUpdateNodes(base.TripleOAction):
                 self.nodes_json,
                 client=baremetal_client,
                 remove=self.remove,
-                glance_client=image_client,
-                kernel_name=self.kernel_name,
-                ramdisk_name=self.ramdisk_name)
+                glance_client=image_client)
         except Exception as err:
             LOG.exception("Error registering nodes with ironic.")
             return actions.Result(error=six.text_type(err))
@@ -100,18 +93,13 @@ class ConfigureBootAction(base.TripleOAction):
     """Configure kernel and ramdisk.
 
     :param node_uuid: an Ironic node UUID
-    :param kernel_name: Glance name of the kernel to use for the nodes.
-    :param ramdisk_name: Glance name of the ramdisk to use for the nodes.
     :param instance_boot_option: Whether to set instances for booting from
                                  local hard drive (local) or network (netboot).
     """
 
-    def __init__(self, node_uuid, kernel_name='bm-deploy-kernel',
-                 ramdisk_name='bm-deploy-ramdisk', instance_boot_option=None):
+    def __init__(self, node_uuid, instance_boot_option=None):
         super(ConfigureBootAction, self).__init__()
         self.node_uuid = node_uuid
-        self.kernel_name = kernel_name
-        self.ramdisk_name = ramdisk_name
         self.instance_boot_option = instance_boot_option
 
     def run(self, context):
@@ -136,6 +124,8 @@ class ConfigureBootAction(base.TripleOAction):
                     'boot_option', self.instance_boot_option or 'local')
             capabilities = nodes.dict_to_capabilities(capabilities)
 
+            raise exception.Timeout('MARK: TODO: ConfigureBootAction: node = %s, capabilities = %s' % (node, node.properties.get('capabilities', {}),))
+
             baremetal_client.node.update(node.uuid, [
                 {
                     'op': 'add',
diff --git a/tripleo_common/actions/validations.py b/tripleo_common/actions/validations.py
index 017f99d..8f16a30 100644
--- a/tripleo_common/actions/validations.py
+++ b/tripleo_common/actions/validations.py
@@ -141,22 +141,20 @@ class CheckBootImagesAction(base.TripleOAction):
 
     # TODO(bcrochet): The validation actions are temporary. This logic should
     #                 move to the tripleo-validations project eventually.
-    def __init__(self, images,
-                 deploy_kernel_name=constants.DEFAULT_DEPLOY_KERNEL_NAME,
-                 deploy_ramdisk_name=constants.DEFAULT_DEPLOY_RAMDISK_NAME):
+    def __init__(self, images):
         super(CheckBootImagesAction, self).__init__()
         self.images = images
-        self.deploy_kernel_name = deploy_kernel_name
-        self.deploy_ramdisk_name = deploy_ramdisk_name
 
     def run(self, context):
         messages = []
-        kernel_id = self._check_for_image(self.deploy_kernel_name, messages)
-        ramdisk_id = self._check_for_image(self.deploy_ramdisk_name, messages)
+
+        self._check_for_image("x86_64-bm-deploy-kernel", messages)
+        self._check_for_image("ppc64le-bm-deploy-kernel", messages)
+
+        self._check_for_image("x86_64-bm-deploy-ramdisk", messages)
+        self._check_for_image("ppc64le-bm-deploy-ramdisk", messages)
 
         return_value = {
-            'kernel_id': kernel_id,
-            'ramdisk_id': ramdisk_id,
             'errors': messages,
             'warnings': []
         }
@@ -181,10 +179,6 @@ class CheckBootImagesAction(base.TripleOAction):
             messages.append(multiple_message.format(name))
         elif len(found_images) == 0:
             messages.append(missing_message.format(name))
-        else:
-            image_id = found_images[0]
-
-        return image_id
 
 
 class CheckFlavorsAction(base.TripleOAction):
@@ -265,31 +259,62 @@ class CheckNodeBootConfigurationAction(base.TripleOAction):
 
     # TODO(bcrochet): The validation actions are temporary. This logic should
     #                 move to the tripleo-validations project eventually.
-    def __init__(self, node, kernel_id, ramdisk_id):
+    def __init__(self, node, images):
         super(CheckNodeBootConfigurationAction, self).__init__()
 
         self.node = node
-        self.kernel_id = kernel_id
-        self.ramdisk_id = ramdisk_id
+        self.images = images
+
+    def _check_for_image(self, name):
+        found_images = [item['id'] for item in self.images
+                        if item['name'] == name]
+        if len(found_images) == 1:
+            return found_images[0]
+        else:
+            return None
 
     def run(self, context):
         warnings = []
         errors = []
+
+        cpu_arch = None
+        if self.node is None:
+            errors.append("self.node is None ")
+        elif 'properties' in self.node:
+            if 'cpu_arch' in self.node['properties']:
+                cpu_arch = self.node['properties']['cpu_arch']
+            else:
+                errors.append("cpu_arch not in properties (%s) " % self.node['properties'])
+        else:
+            errors.append("properties not in node (%s)" % self.node)
+
+        kernel_name = 'bm-deploy-kernel'
+        ramdisk_name = 'bm-deploy-ramdisk'
+        if cpu_arch is not None:
+            kernel_name = cpu_arch+'-bm-deploy-kernel'
+            ramdisk_name = cpu_arch+'-bm-deploy-ramdisk'
+        kernel_id = self._check_for_image(kernel_name)
+        ramdisk_id = self._check_for_image(ramdisk_name)
+        if kernel_id is None:
+            errors.append("kernel_id = %s, kernel_name = %s" % (kernel_id,kernel_name,))
+        if ramdisk_id is None:
+            errors.append("ramdisk_id = %s, ramdisk_name= %s" % (ramdisk_id,ramdisk_name,))
+
         message = ("Node {uuid} has an incorrectly configured "
                    "{property}. Expected \"{expected}\" but got "
                    "\"{actual}\".")
-        if self.node['driver_info'].get('deploy_ramdisk') != self.ramdisk_id:
+        if self.node['driver_info'].get('deploy_ramdisk') != ramdisk_id:
             errors.append(message.format(
                 uuid=self.node['uuid'],
                 property='driver_info/deploy_ramdisk',
-                expected=self.ramdisk_id,
+                expected=ramdisk_id,
                 actual=self.node['driver_info'].get('deploy_ramdisk')
             ))
-        if self.node['driver_info'].get('deploy_kernel') != self.kernel_id:
+        if self.node['driver_info'].get('deploy_kernel') != kernel_id:
             errors.append(message.format(
                 uuid=self.node['uuid'],
                 property='driver_info/deploy_kernel',
-                expected=self.kernel_id,
+                expected=kernel_id,
                 actual=self.node['driver_info'].get('deploy_kernel')
             ))
         capabilities = nodeutils.capabilities_to_dict(
diff --git a/tripleo_common/utils/nodes.py b/tripleo_common/utils/nodes.py
index d160a00..6b10dd6 100644
--- a/tripleo_common/utils/nodes.py
+++ b/tripleo_common/utils/nodes.py
@@ -23,6 +23,9 @@ from oslo_concurrency import processutils
 from tripleo_common import exception
 from tripleo_common.utils import glance
 
+from glanceclient.v2.client import Client as real_glance_client
+from glanceclient import exc as exceptions
+
 LOG = logging.getLogger(__name__)
 
 _KNOWN_INTERFACE_FIELDS = [
@@ -449,8 +452,7 @@ def _clean_up_extra_nodes(seen, client, remove=False):
             LOG.debug('Extra registered node %s found.' % node)
 
 
-def register_all_nodes(nodes_list, client, remove=False, glance_client=None,
-                       kernel_name=None, ramdisk_name=None):
+def register_all_nodes(nodes_list, client, remove=False, glance_client=None):
     """Register all nodes in nodes_list in the baremetal service.
 
     :param nodes_list: The list of nodes to register.
@@ -465,17 +467,32 @@ def register_all_nodes(nodes_list, client, remove=False, glance_client=None,
     LOG.debug('Registering all nodes.')
     node_map = _populate_node_mapping(client)
 
-    glance_ids = {'kernel': None, 'ramdisk': None}
-    if kernel_name and ramdisk_name:
-        glance_ids = glance.create_or_find_kernel_and_ramdisk(
-            glance_client, kernel_name, ramdisk_name)
+    def find_image(name, format):
+        if not isinstance(glance_client, real_glance_client):
+            raise exceptions.NotFound("MARK1")
+
+        images = glance_client.images.list(name=name,
+                                           disk_format=format)
+        for img in images:
+            if img['name'] == name and img['disk_format'] == format:
+                return img['id']
+        return None
 
     seen = []
     for node in nodes_list:
-        if glance_ids['kernel'] and 'kernel_id' not in node:
-            node['kernel_id'] = glance_ids['kernel']
-        if glance_ids['ramdisk'] and 'ramdisk_id' not in node:
-            node['ramdisk_id'] = glance_ids['ramdisk']
+
+        kernel_id = find_image(name=node['arch']+'-bm-deploy-kernel',
+                               format='aki')
+        if not kernel_id:
+            raise exceptions.NotFound("No image found for "+kernel_name)
+
+        ramdisk_id = find_image(name=node['arch']+'-bm-deploy-ramdisk',
+                                format='ari')
+        if not ramdisk_id:
+            raise exceptions.NotFound("No image found for "+ramdisk_name)
+
+        node['kernel_id'] = kernel_id
+        node['ramdisk_id'] = ramdisk_id
 
         node = _update_or_register_ironic_node(node, node_map, client=client)
         seen.append(node)
diff --git a/workbooks/baremetal.yaml b/workbooks/baremetal.yaml
index 34b3f58..4ebff4f 100644
--- a/workbooks/baremetal.yaml
+++ b/workbooks/baremetal.yaml
@@ -147,8 +147,6 @@ workflows:
      - nodes_json
      - remove: False
      - queue_name: tripleo
-     - kernel_name: null
-     - ramdisk_name: null
      - instance_boot_option: local
      - initial_state: manageable
 
@@ -181,8 +179,6 @@ workflows:
         input:
            nodes_json: <% $.nodes_json %>
            remove: <% $.remove %>
-           kernel_name: <% $.kernel_name %>
-           ramdisk_name: <% $.ramdisk_name %>
            instance_boot_option: <% $.instance_boot_option %>
         publish:
           registered_nodes: <% task(register_or_update_nodes).result %>
@@ -700,8 +696,6 @@ workflows:
     input:
       - node_uuids
       - queue_name: tripleo
-      - kernel_name: bm-deploy-kernel
-      - ramdisk_name: bm-deploy-ramdisk
       - instance_boot_option: null
       - root_device: null
       - root_device_minimum_size: 4
@@ -716,7 +710,7 @@ workflows:
         on-success: configure_root_device
         on-error: set_status_failed_configure_boot
         with-items: node_uuid in <% $.node_uuids %>
-        action: tripleo.baremetal.configure_boot node_uuid=<% $.node_uuid %> kernel_name=<% $.kernel_name %> ramdisk_name=<% $.ramdisk_name %> instance_boot_option=<% $.instance_boot_option %>
+        action: tripleo.baremetal.configure_boot node_uuid=<% $.node_uuid %> instance_boot_option=<% $.instance_boot_option %>
 
       configure_root_device:
         on-success: send_message
@@ -759,8 +753,6 @@ workflows:
 
     input:
       - queue_name: tripleo
-      - kernel_name: 'bm-deploy-kernel'
-      - ramdisk_name: 'bm-deploy-ramdisk'
       - instance_boot_option: null
       - root_device: null
       - root_device_minimum_size: 4
@@ -785,8 +777,6 @@ workflows:
         input:
           node_uuids: <% $.managed_nodes %>
           queue_name: <% $.queue_name %>
-          kernel_name: <% $.kernel_name %>
-          ramdisk_name: <% $.ramdisk_name %>
           instance_boot_option: <% $.instance_boot_option %>
           root_device: <% $.root_device %>
           root_device_minimum_size: <% $.root_device_minimum_size %>
@@ -1174,8 +1164,6 @@ workflows:
       - ip_addresses
       - credentials
       - ports: [623]
-      - kernel_name: null
-      - ramdisk_name: null
       - instance_boot_option: local
       - initial_state: manageable
       - queue_name: tripleo
@@ -1207,8 +1195,6 @@ workflows:
         workflow: tripleo.baremetal.v1.register_or_update
         input:
           nodes_json: <% $.nodes_json %>
-          kernel_name: <% $.kernel_name %>
-          ramdisk_name: <% $.ramdisk_name %>
           instance_boot_option: <% $.instance_boot_option %>
           initial_state: <% $.initial_state %>
         on-success: send_message
diff --git a/workbooks/validations.yaml b/workbooks/validations.yaml
index 2ada12e..f327a1a 100644
--- a/workbooks/validations.yaml
+++ b/workbooks/validations.yaml
@@ -273,15 +273,11 @@ workflows:
 
   check_boot_images:
     input:
-      - deploy_kernel_name: 'bm-deploy-kernel'
-      - deploy_ramdisk_name: 'bm-deploy-ramdisk'
       - run_validations: true
       - queue_name: tripleo
     output:
       errors: <% $.errors %>
       warnings: <% $.warnings %>
-      kernel_id: <% $.kernel_id %>
-      ramdisk_id: <% $.ramdisk_id %>
     tags:
       - tripleo-common-managed
     tasks:
@@ -300,18 +296,12 @@ workflows:
         action: tripleo.validations.check_boot_images
         input:
           images: <% $.images %>
-          deploy_kernel_name: <% $.deploy_kernel_name %>
-          deploy_ramdisk_name: <% $.deploy_ramdisk_name %>
         on-success: send_message
         on-error: fail_check_images
         publish:
-          kernel_id: <% task().result.kernel_id %>
-          ramdisk_id: <% task().result.ramdisk_id %>
           warnings: <% task().result.warnings %>
           errors: <% task().result.errors %>
         publish-on-error:
-          kernel_id: <% task().result.kernel_id %>
-          ramdisk_id: <% task().result.ramdisk_id %>
           warnings: <% task().result.warnings %>
           errors: <% task().result.errors %>
 
@@ -333,8 +323,6 @@ workflows:
                 status: <% $.get('status', 'SUCCESS') %>
                 message: <% $.get('message', '') %>
                 execution: <% execution() %>
-                kernel_id: <% $.kernel_id %>
-                ramdisk_id: <% $.ramdisk_id %>
                 errors: <% $.errors %>
                 warnings: <% $.warnings %>
         on-success:
@@ -400,8 +388,6 @@ workflows:
 
   check_ironic_boot_configuration:
     input:
-      - kernel_id: null
-      - ramdisk_id: null
       - run_validations: true
       - queue_name: tripleo
     output:
@@ -419,7 +405,7 @@ workflows:
 
       get_ironic_nodes:
         action: ironic.node_list
-        on-success: check_node_boot_configuration
+        on-success: get_images
         on-error: failed_get_ironic_nodes
         input:
           provision_state: available
@@ -434,12 +420,17 @@ workflows:
           status: FAILED
           message: <% task(get_ironic_nodes).result %>
 
+      get_images:
+        action: glance.images_list
+        on-success: check_node_boot_configuration
+        publish:
+          images: <% task(get_images).result %>
+
       check_node_boot_configuration:
         action: tripleo.validations.check_node_boot_configuration
         input:
           node: <% $.node %>
-          kernel_id: <% $.kernel_id %>
-          ramdisk_id: <% $.ramdisk_id %>
+          images: <% $.images %>
         with-items: node in <% $.nodes %>
         on-success: send_message
         on-error: fail_check_node_boot_configuration
@@ -672,8 +663,6 @@ workflows:
 
   check_pre_deployment_validations:
     input:
-      - deploy_kernel_name: 'bm-deploy-kernel'
-      - deploy_ramdisk_name: 'bm-deploy-ramdisk'
       - roles_info: {}
       - stack_id: overcloud
       - parameters: {}
@@ -684,8 +673,6 @@ workflows:
     output:
       errors: <% $.errors %>
       warnings: <% $.warnings %>
-      kernel_id: <% $.kernel_id %>
-      ramdisk_id: <% $.ramdisk_id %>
       flavors: <% $.flavors %>
       statistics: <% $.statistics %>
     tags:
@@ -700,20 +687,14 @@ workflows:
       check_boot_images:
         workflow: check_boot_images
         input:
-          deploy_kernel_name: <% $.deploy_kernel_name %>
-          deploy_ramdisk_name: <% $.deploy_ramdisk_name %>
           run_validations: <% $.run_validations %>
           queue_name: <% $.queue_name %>
         publish:
           errors: <% $.errors + task().result.errors %>
           warnings: <% $.warnings + task().result.warnings %>
-          kernel_id: <% task().result.kernel_id %>
-          ramdisk_id: <% task().result.ramdisk_id %>
         publish-on-error:
           errors: <% $.errors + task().result.errors %>
           warnings: <% $.warnings + task().result.warnings %>
-          kernel_id: <% task().result.kernel_id %>
-          ramdisk_id: <% task().result.ramdisk_id %>
           status: FAILED
         on-success: collect_flavors
         on-error: collect_flavors
@@ -739,8 +720,6 @@ workflows:
       check_ironic_boot_configuration:
         workflow: check_ironic_boot_configuration
         input:
-          kernel_id: <% $.kernel_id %>
-          ramdisk_id: <% $.ramdisk_id %>
           run_validations: <% $.run_validations %>
           queue_name: <% $.queue_name %>
         publish:
@@ -801,8 +780,6 @@ workflows:
                 status: <% $.get('status', 'SUCCESS') %>
                 message: <% $.get('message', '') %>
                 execution: <% execution() %>
-                kernel_id: <% $.kernel_id %>
-                ramdisk_id: <% $.ramdisk_id %>
                 flavors: <% $.flavors %>
                 statistics: <% $.statistics %>
                 errors: <% $.errors %>
-- 
2.13.6

