From 456b10e0742fefc65b30aa1cd6d2eed12c05d06e Mon Sep 17 00:00:00 2001
From: Mark Goddard <mark@stackhpc.com>
Date: Sat, 1 Apr 2017 09:23:14 +0100
Subject: [PATCH] Allow specification of container images to build

Also adds a command to pull container images
---
 ansible/container-image-build.yml |  9 ++++++++
 kayobe/cli/commands.py            | 34 ++++++++++++++++++++++++++++++-
 kayobe/utils.py                   |  7 ++++++-
 setup.py                          |  4 +++-
 4 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/ansible/container-image-build.yml b/ansible/container-image-build.yml
index c7a87a601..ac690a346 100644
--- a/ansible/container-image-build.yml
+++ b/ansible/container-image-build.yml
@@ -4,9 +4,18 @@
   vars:
     # Set this to True to push images to the registry when built.
     push_images: False
+    # Set this variable to a space-separated list of regexes to override the
+    # default set of images.
+    container_image_regexes: ""
     kolla_venv: "{{ ansible_env['PWD'] }}/kolla-venv"
     kolla_build_log_path: "/var/log/kolla-build.log"
   tasks:
+    - name: Set the container image sets to build if images regexes specified
+      set_fact:
+        container_image_sets:
+          - regexes: "{{ container_image_regexes }}"
+      when: "{{ container_image_regexes != '' }}"
+
     - name: Display the regexes for container images that will be built
       debug:
         msg: >
diff --git a/kayobe/cli/commands.py b/kayobe/cli/commands.py
index 83c524d00..d6a573f2e 100644
--- a/kayobe/cli/commands.py
+++ b/kayobe/cli/commands.py
@@ -209,6 +209,9 @@ class SeedContainerImageBuild(KayobeAnsibleMixin, Command):
         group.add_argument("--push", action="store_true",
                            help="whether to push images to a registry after "
                                 "building")
+        group.add_argument("regex", nargs='*',
+                           help="regular expression matching names of images "
+                                "to build. Builds all images if unspecified")
         return parser
 
     def take_action(self, parsed_args):
@@ -216,6 +219,9 @@ class SeedContainerImageBuild(KayobeAnsibleMixin, Command):
         playbooks = _build_playbook_list(
             "kolla-build", "container-image-build")
         extra_vars = {"push_images": parsed_args.push}
+        if parsed_args.regex:
+            regexes = " ".join(parsed_args.regex)
+            extra_vars["container_image_regexes"] = regexes
         ansible.run_playbooks(parsed_args, playbooks, limit="seed",
                               extra_vars=extra_vars)
 
@@ -302,7 +308,7 @@ class OvercloudServiceDeploy(KollaAnsibleMixin, KayobeAnsibleMixin, Command):
         self.app.LOG.debug("Deploying overcloud services")
         playbooks = _build_playbook_list("kolla-openstack", "swift-setup")
         ansible.run_playbooks(parsed_args, playbooks)
-        for command in ["pull", "prechecks", "deploy"]:
+        for command in ["prechecks", "deploy"]:
             kolla_ansible.run_overcloud(parsed_args, command)
         # FIXME: Fudge to work around incorrect configuration path.
         extra_vars = {"node_config_directory": parsed_args.kolla_config_path}
@@ -310,6 +316,14 @@ class OvercloudServiceDeploy(KollaAnsibleMixin, KayobeAnsibleMixin, Command):
                                     extra_vars=extra_vars)
 
 
+class OvercloudContainerImagePull(KollaAnsibleMixin, Command):
+    """Pull the overcloud container images from a registry."""
+
+    def take_action(self, parsed_args):
+        self.app.LOG.debug("Pulling overcloud container images")
+        kolla_ansible.run_overcloud(parsed_args, "pull")
+
+
 class OvercloudContainerImageBuild(KayobeAnsibleMixin, Command):
     """Build the overcloud container images."""
 
@@ -320,6 +334,9 @@ class OvercloudContainerImageBuild(KayobeAnsibleMixin, Command):
         group.add_argument("--push", action="store_true",
                            help="whether to push images to a registry after "
                                 "building")
+        group.add_argument("regex", nargs='*',
+                           help="regular expression matching names of images "
+                                "to build. Builds all images if unspecified")
         return parser
 
     def take_action(self, parsed_args):
@@ -327,5 +344,20 @@ class OvercloudContainerImageBuild(KayobeAnsibleMixin, Command):
         playbooks = _build_playbook_list(
             "kolla-build", "container-image-build")
         extra_vars = {"push_images": parsed_args.push}
+        if parsed_args.regex:
+            regexes = " ".join(parsed_args.regex)
+            extra_vars["container_image_regexes"] = regexes
         ansible.run_playbooks(parsed_args, playbooks, limit="controllers",
                               extra_vars=extra_vars)
+
+
+class OvercloudPostConfigure(KayobeAnsibleMixin, Command):
+    """Perform post-deployment configuration."""
+
+    def take_action(self, parsed_args):
+        self.app.LOG.debug("Performing post-deployment configuration")
+        playbooks = _build_playbook_list(
+            "ipa-images", "overcloud-introspection-rules",
+            "overcloud-introspection-rules-dell-lldp-workaround",
+            "provision-net")
+        ansible.run_playbooks(parsed_args, playbooks)
diff --git a/kayobe/utils.py b/kayobe/utils.py
index 1f3b2fc7f..edcc63630 100644
--- a/kayobe/utils.py
+++ b/kayobe/utils.py
@@ -1,5 +1,6 @@
 import logging
 import os
+import six
 import subprocess
 import sys
 
@@ -83,5 +84,9 @@ def run_command(cmd, quiet=False, **kwargs):
     if quiet:
         kwargs["stdout"] = subprocess.PIPE
         kwargs["stderr"] = subprocess.PIPE
-    LOG.debug("Running command: %s", " ".join(cmd))
+    if isinstance(cmd, six.string_types):
+        cmd_string = cmd
+    else:
+        cmd_string = " ".join(cmd)
+    LOG.debug("Running command: %s", cmd_string)
     subprocess.check_call(cmd, **kwargs)
diff --git a/setup.py b/setup.py
index 85b0b9752..3d5b0f6c8 100644
--- a/setup.py
+++ b/setup.py
@@ -40,11 +40,13 @@ setup(
             'configuration_dump = kayobe.cli.commands:ConfigurationDump',
             'kolla_ansible_run = kayobe.cli.commands:KollaAnsibleRun',
             'overcloud_container_image_build = kayobe.cli.commands:OvercloudContainerImageBuild',
+            'overcloud_container_image_pull = kayobe.cli.commands:OvercloudContainerImagePull',
             'overcloud_deprovision = kayobe.cli.commands:OvercloudDeprovision',
             'overcloud_host_configure = kayobe.cli.commands:OvercloudHostConfigure',
             'overcloud_inventory_discover = kayobe.cli.commands:OvercloudInventoryDiscover',
-            'overcloud_service_deploy = kayobe.cli.commands:OvercloudServiceDeploy',
+            'overcloud_post_configure = kayobe.cli.commands:OvercloudPostConfigure',
             'overcloud_provision = kayobe.cli.commands:OvercloudProvision',
+            'overcloud_service_deploy = kayobe.cli.commands:OvercloudServiceDeploy',
             'physical_network_configure = kayobe.cli.commands:PhysicalNetworkConfigure',
             'playbook_run = kayobe.cli.commands:PlaybookRun',
             'seed_container_image_build = kayobe.cli.commands:SeedContainerImageBuild',