From 01f5fd25aaee64a215dec087539041fb5d36ac6a Mon Sep 17 00:00:00 2001
From: Carlos Camacho <ccamacho@redhat.com>
Date: Fri, 27 Apr 2018 16:03:33 +0200
Subject: [PATCH] Add --stack to update, upgrade and ffwd-upgrade 'run' CLI.

We need to be able to define the stack name if it
is different than 'overcloud', for the ansible inventory
used with the upgrade playbooks.

Change-Id: I0499eabc5c6f15cb32b2471c86245578c71a2a60
Closes-Bug: 1767379
---
 tripleoclient/tests/test_utils.py          | 29 ++++++++++++++++++++++
 tripleoclient/utils.py                     |  5 +++-
 tripleoclient/v1/overcloud_ffwd_upgrade.py | 11 +++++++-
 tripleoclient/v1/overcloud_update.py       | 11 +++++++-
 tripleoclient/v1/overcloud_upgrade.py      | 11 +++++++-
 5 files changed, 63 insertions(+), 4 deletions(-)

diff --git a/tripleoclient/tests/test_utils.py b/tripleoclient/tests/test_utils.py
index efdb987bd..e0cfc7af9 100644
--- a/tripleoclient/tests/test_utils.py
+++ b/tripleoclient/tests/test_utils.py
@@ -738,3 +738,32 @@ class ProcessMultipleEnvironments(TestCase):
 
         mock_yaml_dump.assert_has_calls([mock.call(rewritten_env,
                                         default_flow_style=False)])
+
+
+class GetTripleoAnsibleInventory(TestCase):
+
+    def setUp(self):
+        super(GetTripleoAnsibleInventory, self).setUp()
+        self.inventory_file = ''
+        self.ssh_user = 'heat_admin'
+        self.stack = 'foo-overcloud'
+
+    @mock.patch('tripleoclient.utils.get_tripleo_ansible_inventory',
+                autospec=True)
+    def test_get_tripleo_ansible_inventory(self, mock_inventory):
+
+        with mock.patch('os.path.exists') as mock_exists:
+            mock_exists.return_value = True
+
+            self.cmd = utils.get_tripleo_ansible_inventory(
+                inventory_file=self.inventory_file,
+                ssh_user=self.ssh_user,
+                stack=self.stack)
+
+            self.cmd.take_action()
+
+            mock_inventory.assert_called_once_with(
+                inventory_file='',
+                ssh_user='heat_admin',
+                stack='foo-overcloud'
+            )
diff --git a/tripleoclient/utils.py b/tripleoclient/utils.py
index fe852b7a2..3245bb3cf 100644
--- a/tripleoclient/utils.py
+++ b/tripleoclient/utils.py
@@ -816,13 +816,16 @@ def load_environment_directories(directories):
     return environments
 
 
-def get_tripleo_ansible_inventory(inventory_file='', ssh_user='heat-admin'):
+def get_tripleo_ansible_inventory(inventory_file='',
+                                  ssh_user='heat-admin',
+                                  stack='overcloud'):
     if not inventory_file:
         inventory_file = '%s/%s' % (os.path.expanduser('~'),
                                     'tripleo-ansible-inventory.yaml')
         try:
             processutils.execute(
                 '/usr/bin/tripleo-ansible-inventory',
+                '--stack', stack,
                 '--ansible_ssh_user', ssh_user,
                 '--static-yaml-inventory', inventory_file)
         except processutils.ProcessExecutionError as e:
diff --git a/tripleoclient/v1/overcloud_ffwd_upgrade.py b/tripleoclient/v1/overcloud_ffwd_upgrade.py
index 8a1c78019..f1161f110 100644
--- a/tripleoclient/v1/overcloud_ffwd_upgrade.py
+++ b/tripleoclient/v1/overcloud_ffwd_upgrade.py
@@ -15,6 +15,7 @@
 import logging
 
 from osc_lib.i18n import _
+from osc_lib import utils
 
 from tripleoclient import command
 from tripleoclient import constants
@@ -112,14 +113,22 @@ class FFWDUpgradeRun(command.Command):
                             help=_("The ssh user name for connecting to "
                                    "the overcloud nodes.")
                             )
+        parser.add_argument('--stack', dest='stack',
+                            help=_('Name or ID of heat stack '
+                                   '(default=Env: OVERCLOUD_STACK_NAME)'),
+                            default=utils.env('OVERCLOUD_STACK_NAME',
+                                              default='overcloud')
+                            )
         return parser
 
     def take_action(self, parsed_args):
         self.log.debug("take_action(%s)" % parsed_args)
         clients = self.app.client_manager
+        stack = parsed_args.stack
+
         # Run ansible:
         inventory = oooutils.get_tripleo_ansible_inventory(
-            parsed_args.static_inventory)
+            parsed_args.static_inventory, stack)
         # Don't expost limit_hosts. We need this on the whole overcloud.
         limit_hosts = ''
         oooutils.run_update_ansible_action(
diff --git a/tripleoclient/v1/overcloud_update.py b/tripleoclient/v1/overcloud_update.py
index 75858311d..34aba921b 100644
--- a/tripleoclient/v1/overcloud_update.py
+++ b/tripleoclient/v1/overcloud_update.py
@@ -16,6 +16,7 @@
 import logging
 
 from osc_lib.i18n import _
+from osc_lib import utils
 
 from tripleoclient import command
 from tripleoclient import constants
@@ -138,11 +139,19 @@ class UpdateRun(command.Command):
                                    'generated in '
                                    '~/tripleo-ansible-inventory.yaml')
                             )
+        parser.add_argument('--stack', dest='stack',
+                            help=_('Name or ID of heat stack '
+                                   '(default=Env: OVERCLOUD_STACK_NAME)'),
+                            default=utils.env('OVERCLOUD_STACK_NAME',
+                                              default='overcloud')
+                            )
+
         return parser
 
     def take_action(self, parsed_args):
         self.log.debug("take_action(%s)" % parsed_args)
         clients = self.app.client_manager
+        stack = parsed_args.stack
 
         # Run ansible:
         nodes = parsed_args.nodes
@@ -151,7 +160,7 @@ class UpdateRun(command.Command):
             nodes = None
         playbook = parsed_args.playbook
         inventory = oooutils.get_tripleo_ansible_inventory(
-            parsed_args.static_inventory, parsed_args.ssh_user)
+            parsed_args.static_inventory, parsed_args.ssh_user, stack)
         oooutils.run_update_ansible_action(self.log, clients, nodes, inventory,
                                            playbook, constants.UPDATE_QUEUE,
                                            constants.MINOR_UPDATE_PLAYBOOKS,
diff --git a/tripleoclient/v1/overcloud_upgrade.py b/tripleoclient/v1/overcloud_upgrade.py
index cfe11471e..110fd155d 100644
--- a/tripleoclient/v1/overcloud_upgrade.py
+++ b/tripleoclient/v1/overcloud_upgrade.py
@@ -15,6 +15,7 @@
 import logging
 
 from osc_lib.i18n import _
+from osc_lib import utils
 
 from tripleoclient import command
 from tripleoclient import constants
@@ -170,6 +171,12 @@ class UpgradeRun(command.Command):
                                    'upgrade and some services cannot be '
                                    'started. ')
                             )
+        parser.add_argument('--stack', dest='stack',
+                            help=_('Name or ID of heat stack '
+                                   '(default=Env: OVERCLOUD_STACK_NAME)'),
+                            default=utils.env('OVERCLOUD_STACK_NAME',
+                                              default='overcloud'))
+
         return parser
 
     def _validate_skip_tags(self, skip_tags):
@@ -185,13 +192,15 @@ class UpgradeRun(command.Command):
     def take_action(self, parsed_args):
         self.log.debug("take_action(%s)" % parsed_args)
         clients = self.app.client_manager
+        stack = parsed_args.stack
+
         # Run ansible:
         roles = parsed_args.roles
         nodes = parsed_args.nodes
         limit_hosts = roles or nodes
         playbook = parsed_args.playbook
         inventory = oooutils.get_tripleo_ansible_inventory(
-            parsed_args.static_inventory, parsed_args.ssh_user)
+            parsed_args.static_inventory, parsed_args.ssh_user, stack)
         skip_tags = self._validate_skip_tags(parsed_args.skip_tags)
         oooutils.run_update_ansible_action(self.log, clients, limit_hosts,
                                            inventory, playbook,