Merge "Use playbook for config-download export"

This commit is contained in:
Zuul 2020-06-13 04:19:37 +00:00 committed by Gerrit Code Review
commit 4b9a490419
3 changed files with 31 additions and 103 deletions

View File

@ -14,6 +14,7 @@ import mock
from osc_lib.tests import utils from osc_lib.tests import utils
from tripleoclient.tests import fakes
from tripleoclient.v1 import overcloud_config from tripleoclient.v1 import overcloud_config
@ -21,20 +22,12 @@ class TestOvercloudConfig(utils.TestCommand):
def setUp(self): def setUp(self):
super(TestOvercloudConfig, self).setUp() super(TestOvercloudConfig, self).setUp()
self.cmd = overcloud_config.DownloadConfig(self.app, None) self.cmd = overcloud_config.DownloadConfig(self.app, None)
self.app.client_manager.workflow_engine = mock.Mock()
self.app.client_manager.orchestration = mock.Mock() self.app.client_manager.orchestration = mock.Mock()
self.workflow = self.app.client_manager.workflow_engine self.app.options = fakes.FakeOptions()
@mock.patch('tripleoclient.v1.overcloud_config.processutils.execute') @mock.patch("tripleoclient.utils.run_ansible_playbook", autospec=True)
@mock.patch('tripleoclient.v1.overcloud_config.open') def test_overcloud_download_config(self, mock_playbook):
@mock.patch('tripleoclient.v1.overcloud_config.request')
@mock.patch('shutil.rmtree')
@mock.patch('tripleoclient.workflows.deployment.config_download_export')
def test_overcloud_download_config(
self, mock_config, mock_rmtree, mock_request,
mock_open, mock_execute):
arglist = ['--name', 'overcloud', '--config-dir', '/tmp'] arglist = ['--name', 'overcloud', '--config-dir', '/tmp']
verifylist = [ verifylist = [
('name', 'overcloud'), ('name', 'overcloud'),
@ -44,20 +37,15 @@ class TestOvercloudConfig(utils.TestCommand):
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args) self.cmd.take_action(parsed_args)
mock_config.assert_called_once_with( mock_playbook.assert_called_once_with(
self.app.client_manager, plan='overcloud', config_type=None) extra_vars={'plan': 'overcloud', 'config_dir': '/tmp',
mock_rmtree.assert_not_called() 'preserve_config': True},
mock_open.assert_called() inventory='localhost,', playbook='cli-config-download-export.yaml',
mock_request.urlopen.assert_called() playbook_dir='/usr/share/ansible/tripleo-playbooks',
verbosity=3, workdir=mock.ANY)
@mock.patch('tripleoclient.v1.overcloud_config.processutils.execute') @mock.patch("tripleoclient.utils.run_ansible_playbook", autospec=True)
@mock.patch('tripleoclient.v1.overcloud_config.open') def test_overcloud_download_config_no_preserve(self, mock_playbook):
@mock.patch('tripleoclient.v1.overcloud_config.request')
@mock.patch('shutil.rmtree')
@mock.patch('tripleoclient.workflows.deployment.config_download_export')
def test_overcloud_download_config_no_preserve(
self, mock_config, mock_rmtree, mock_request,
mock_open, mock_execute):
arglist = ['--name', 'overcloud', '--config-dir', '/tmp', arglist = ['--name', 'overcloud', '--config-dir', '/tmp',
'--no-preserve-config'] '--no-preserve-config']
verifylist = [ verifylist = [
@ -68,8 +56,9 @@ class TestOvercloudConfig(utils.TestCommand):
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args) self.cmd.take_action(parsed_args)
mock_config.assert_called_once_with( mock_playbook.assert_called_once_with(
self.app.client_manager, plan='overcloud', config_type=None) extra_vars={'plan': 'overcloud', 'config_dir': '/tmp',
mock_rmtree.assert_called() 'preserve_config': False},
mock_open.assert_called() inventory='localhost,', playbook='cli-config-download-export.yaml',
mock_request.urlopen.assert_called() playbook_dir='/usr/share/ansible/tripleo-playbooks',
verbosity=3, workdir=mock.ANY)

View File

@ -12,16 +12,12 @@
import logging import logging
import os import os
import shutil
from six.moves.urllib import request
from osc_lib.i18n import _ from osc_lib.i18n import _
from oslo_concurrency import processutils
from tripleoclient import command from tripleoclient import command
from tripleoclient import constants from tripleoclient import constants
from tripleoclient import utils from tripleoclient import utils
from tripleoclient.workflows import deployment
class DownloadConfig(command.Command): class DownloadConfig(command.Command):
@ -69,20 +65,6 @@ class DownloadConfig(command.Command):
) )
return parser return parser
def create_config_dir(self, config_dir, preserve_config_dir=True):
# Create config directory
if os.path.exists(config_dir) and preserve_config_dir is False:
try:
self.log.info("Directory %s already exists, removing"
% config_dir)
shutil.rmtree(config_dir)
except OSError as e:
message = 'Failed to remove: %s, error: %s' % (config_dir,
str(e))
raise OSError(message)
utils.makedirs(config_dir)
def take_action(self, parsed_args): def take_action(self, parsed_args):
self.log.debug("take_action(%s)" % parsed_args) self.log.debug("take_action(%s)" % parsed_args)
@ -90,29 +72,20 @@ class DownloadConfig(command.Command):
config_dir = parsed_args.config_dir config_dir = parsed_args.config_dir
config_type = parsed_args.config_type config_type = parsed_args.config_type
preserve_config_dir = parsed_args.preserve_config_dir preserve_config_dir = parsed_args.preserve_config_dir
self.create_config_dir(config_dir, preserve_config_dir) extra_vars = {'plan': name,
'config_dir': config_dir,
'preserve_config': preserve_config_dir}
if config_type:
extra_vars['config_type'] = config_type
# Get config with utils.TempDirs() as tmp:
print("Starting config-download export...") utils.run_ansible_playbook(
tempurl = deployment.config_download_export( playbook='cli-config-download-export.yaml',
self.app.client_manager, inventory='localhost,',
plan=name, workdir=tmp,
config_type=config_type playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
) verbosity=utils.playbook_verbosity(self=self),
print("Finished config-download export.") extra_vars=extra_vars)
self.log.debug("config-download tempurl: %s" % tempurl)
f = request.urlopen(tempurl)
tarball_contents = f.read()
f.close()
tarball_name = "%s-config.tar.gz" % name
tarball_path = os.path.join(config_dir, tarball_name)
with open(tarball_path, 'wb') as f:
f.write(tarball_contents)
print("Extracting config-download...")
cmd = ['/usr/bin/tar', '-C', config_dir, '-xf', tarball_path]
processutils.execute(*cmd)
print("The TripleO configuration has been successfully generated " print("The TripleO configuration has been successfully generated "
"into: {0}".format(config_dir)) "into: {0}".format(config_dir))

View File

@ -20,7 +20,6 @@ from heatclient.common import event_utils
from heatclient import exc as heat_exc from heatclient import exc as heat_exc
from openstackclient import shell from openstackclient import shell
from swiftclient import exceptions as swiftexceptions from swiftclient import exceptions as swiftexceptions
from tripleo_common.actions import config
from tripleo_common.utils import swift as swift_utils from tripleo_common.utils import swift as swift_utils
from tripleoclient.constants import ANSIBLE_TRIPLEO_PLAYBOOKS from tripleoclient.constants import ANSIBLE_TRIPLEO_PLAYBOOKS
@ -475,39 +474,6 @@ def config_download(log, clients, stack, ssh_network='ctlplane',
) )
def config_download_export(clients, plan, config_type):
"""Export a given config.
:param clients: application client object.
:type clients: Object
:param plan: Plan name.
:type plan: String
:param config_type: List of config type options.
:type config_type: List
:returns: string
"""
context = clients.tripleoclient.create_mistral_context()
container_config = '{}-config'.format(plan)
config.GetOvercloudConfig(
container=plan,
config_type=config_type,
container_config=container_config
).run(context=context)
print(
'Config Download export complete for {}. Creating temp URL.'.format(
plan
)
)
return swift_utils.get_temp_url(
clients.tripleoclient.object_store, container=container_config,
object_name='{}.tar.gz'.format(container_config))
def get_horizon_url(stack, verbosity=0): def get_horizon_url(stack, verbosity=0):
"""Return horizon URL string. """Return horizon URL string.