Invoke ansible from writeable workdirs

In order to make it configurable via env/settings,
use writebale tmp paths for ansible runner. This also aligns the
way we call it for other places.

Change-Id: I64999f19b4ce2083f05e09c40d6b89c8d8ba2cdd
Related-bug: #1868063
Signed-off-by: Bogdan Dobrelya <bdobreli@redhat.com>
This commit is contained in:
Bogdan Dobrelya 2020-04-06 08:47:57 +02:00
parent 624a61f206
commit d9174e83fd
7 changed files with 116 additions and 62 deletions

View File

@ -285,12 +285,12 @@ class TestDeployOvercloud(fakes.TestDeployOvercloud):
'_validate_args')
@mock.patch('heatclient.common.template_utils.get_template_contents',
autospec=True)
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_tht_deploy(self, mock_tmpdir,
mock_get_template_contents,
mock_validate_args,
mock_breakpoints_cleanup,
mock_postconfig,
@mock.patch('tripleoclient.utils.makedirs')
def test_tht_deploy(self, mock_md, mock_tmpdir, mock_cd,
mock_get_template_contents, mock_validate_args,
mock_breakpoints_cleanup, mock_postconfig,
mock_invoke_plan_env_wf,
mock_get_undercloud_host_entry, mock_copy):
@ -346,7 +346,9 @@ class TestDeployOvercloud(fakes.TestDeployOvercloud):
'UndercloudHostsEntries':
['192.168.0.1 uc.ctlplane.localhost uc.ctlplane']}}
self.cmd.take_action(parsed_args)
mock_open_context = mock.mock_open()
with mock.patch('six.moves.builtins.open', mock_open_context):
self.cmd.take_action(parsed_args)
self.assertFalse(orchestration_client.stacks.create.called)

View File

@ -107,7 +107,9 @@ class TestOvercloudCreatePlan(utils.TestCommand):
self.swift.get_account = mock.MagicMock()
@mock.patch("tripleoclient.utils.run_ansible_playbook", autospec=True)
def test_create_default_plan(self, mock_run_playbook):
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_create_default_plan(self, mock_tmp, mock_cd, mock_run_playbook):
# Setup
arglist = ['overcast']
@ -124,6 +126,7 @@ class TestOvercloudCreatePlan(utils.TestCommand):
mock_run_playbook.assert_called_once_with(
'cli-create-deployment-plan.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": "overcast",
@ -135,7 +138,9 @@ class TestOvercloudCreatePlan(utils.TestCommand):
@mock.patch("tripleoclient.utils.run_ansible_playbook", autospec=True)
@mock.patch("tripleoclient.workflows.plan_management.tarball")
def test_create_custom_plan(self, mock_tarball,
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_create_custom_plan(self, mock_tmp, mock_cd, mock_tarball,
mock_run_playbook):
# Setup
@ -153,6 +158,7 @@ class TestOvercloudCreatePlan(utils.TestCommand):
mock_run_playbook.assert_called_once_with(
'cli-create-deployment-plan.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": "overcast",
@ -165,8 +171,10 @@ class TestOvercloudCreatePlan(utils.TestCommand):
@mock.patch("tripleoclient.utils.run_ansible_playbook", autospec=True)
@mock.patch("tripleoclient.workflows.plan_management.tarball")
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_create_custom_plan_plan_environment_file(
self, mock_tarball, mock_run_playbook):
self, mock_tmp, mock_cd, mock_tarball, mock_run_playbook):
# Setup
arglist = ['overcast', '--templates', '/fake/path',
'-p', 'the_plan_environment.yaml']
@ -190,6 +198,7 @@ class TestOvercloudCreatePlan(utils.TestCommand):
mock_run_playbook.assert_called_once_with(
'cli-create-deployment-plan.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": "overcast",
@ -202,8 +211,10 @@ class TestOvercloudCreatePlan(utils.TestCommand):
self.swift.get_account.assert_called_once()
@mock.patch("tripleoclient.utils.run_ansible_playbook", autospec=True)
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_create_default_plan_with_password_gen_disabled(
self, mock_run_playbook):
self, mock_tmp, mock_cd, mock_run_playbook):
# Setup
arglist = ['overcast', '--disable-password-generation']
@ -221,6 +232,7 @@ class TestOvercloudCreatePlan(utils.TestCommand):
mock_run_playbook.assert_called_once_with(
'cli-create-deployment-plan.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": "overcast",
@ -247,8 +259,10 @@ class TestOvercloudDeployPlan(utils.TestCommand):
@mock.patch("tripleoclient.utils.run_ansible_playbook", autospec=True)
@mock.patch('tripleoclient.utils.wait_for_stack_ready', autospec=True)
def test_overcloud_deploy_plan(self, mock_for_stack_ready,
mock_run_playbook):
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_overcloud_deploy_plan(self, mock_tmp, mock_cd,
mock_for_stack_ready, mock_run_playbook):
# Setup
arglist = ['--run-validations', 'overcast']
@ -271,6 +285,7 @@ class TestOvercloudDeployPlan(utils.TestCommand):
mock_run_playbook.assert_called_once_with(
'cli-deploy-deployment-plan.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": "overcast",

View File

@ -30,7 +30,9 @@ class TestDeleteOvercloud(deploy_fakes.TestDeployOvercloud):
self.cmd = overcloud_delete.DeleteOvercloud(self.app, None)
@mock.patch("tripleoclient.utils.run_ansible_playbook", autospec=True)
def test_plan_undeploy(self, mock_run_playbook):
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_plan_undeploy(self, mock_mkdir, mock_cd, mock_run_playbook):
arglist = ["overcast", "-y"]
verifylist = [
("stack", "overcast"),
@ -44,6 +46,7 @@ class TestDeleteOvercloud(deploy_fakes.TestDeployOvercloud):
mock_run_playbook.assert_called_once_with(
'cli-overcloud-delete.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"stack_name": "overcast",

View File

@ -32,7 +32,10 @@ class TestPlanCreationWorkflows(utils.TestCommand):
@mock.patch("tripleoclient.utils.run_ansible_playbook", autospec=True)
@mock.patch('tripleoclient.workflows.plan_management.tarball',
autospec=True)
def test_create_plan_from_templates_success(self, mock_tarball,
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_create_plan_from_templates_success(self, mock_tmp, mock_cd,
mock_tarball,
mock_run_playbook):
plan_management.create_plan_from_templates(
self.app.client_manager,
@ -43,6 +46,7 @@ class TestPlanCreationWorkflows(utils.TestCommand):
mock_run_playbook.assert_called_once_with(
'cli-create-deployment-plan.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": "test-overcloud",
@ -56,7 +60,10 @@ class TestPlanCreationWorkflows(utils.TestCommand):
@mock.patch('tripleoclient.utils.rel_or_abs_path')
@mock.patch('tripleoclient.workflows.plan_management.tarball',
autospec=True)
def test_create_plan_from_templates_roles_data(self, mock_tarball,
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_create_plan_from_templates_roles_data(self, mock_tmp, mock_cd,
mock_tarball,
mock_norm_path,
mock_run_playbook):
mock_open_context = mock.mock_open()
@ -71,6 +78,7 @@ class TestPlanCreationWorkflows(utils.TestCommand):
mock_run_playbook.assert_called_once_with(
'cli-create-deployment-plan.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": "test-overcloud",
@ -89,7 +97,10 @@ class TestPlanCreationWorkflows(utils.TestCommand):
@mock.patch("tripleoclient.utils.run_ansible_playbook", autospec=True)
@mock.patch('tripleoclient.workflows.plan_management.tarball',
autospec=True)
def test_create_plan_from_templates_plan_env_data(self, mock_tarball,
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_create_plan_from_templates_plan_env_data(self, mock_tmp, mock_cd,
mock_tarball,
mock_run_playbook):
mock_open_context = mock.mock_open()
with mock.patch('six.moves.builtins.open', mock_open_context):
@ -103,6 +114,7 @@ class TestPlanCreationWorkflows(utils.TestCommand):
mock_run_playbook.assert_called_once_with(
'cli-create-deployment-plan.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": "test-overcloud",
@ -121,7 +133,10 @@ class TestPlanCreationWorkflows(utils.TestCommand):
@mock.patch("tripleoclient.utils.run_ansible_playbook", autospec=True)
@mock.patch('tripleoclient.workflows.plan_management.tarball',
autospec=True)
def test_create_plan_from_templates_networks_data(self, mock_tarball,
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_create_plan_from_templates_networks_data(self, mock_tmp, mock_cd,
mock_tarball,
mock_run_playbook):
mock_open_context = mock.mock_open()
with mock.patch('six.moves.builtins.open', mock_open_context):
@ -135,6 +150,7 @@ class TestPlanCreationWorkflows(utils.TestCommand):
mock_run_playbook.assert_called_once_with(
'cli-create-deployment-plan.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": "test-overcloud",
@ -152,7 +168,10 @@ class TestPlanCreationWorkflows(utils.TestCommand):
@mock.patch("tripleoclient.utils.run_ansible_playbook", autospec=True)
@mock.patch('tripleoclient.workflows.plan_management.tarball',
autospec=True)
def test_create_plan_with_password_gen_disabled(self, mock_tarball,
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_create_plan_with_password_gen_disabled(self, mock_tmp, mock_cd,
mock_tarball,
mock_run_playbook):
plan_management.create_plan_from_templates(
self.app.client_manager,
@ -164,6 +183,7 @@ class TestPlanCreationWorkflows(utils.TestCommand):
mock_run_playbook.assert_called_once_with(
'cli-create-deployment-plan.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": "test-overcloud",
@ -211,8 +231,11 @@ class TestPlanUpdateWorkflows(base.TestCommand):
autospec=True)
@mock.patch('tripleo_common.utils.swift.empty_container',
autospec=True)
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_update_plan_from_templates_keep_env(
self, mock_empty_container, mock_tarball, mock_run_playbook):
self, mock_tmp, mock_cd, mock_empty_container, mock_tarball,
mock_run_playbook):
plan_management.update_plan_from_templates(
self.app.client_manager,
@ -246,6 +269,7 @@ class TestPlanUpdateWorkflows(base.TestCommand):
mock_run_playbook.assert_called_once_with(
'cli-update-deployment-plan.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": "test-overcloud",
@ -282,6 +306,7 @@ class TestPlanUpdateWorkflows(base.TestCommand):
mock_run_playbook.assert_called_once_with(
'cli-update-deployment-plan.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": "test-overcloud",
@ -300,9 +325,11 @@ class TestPlanUpdateWorkflows(base.TestCommand):
autospec=True)
@mock.patch('tripleo_common.utils.swift.empty_container',
autospec=True)
@mock.patch('os.chdir', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_update_plan_from_templates_recreate_env_missing_passwords(
self, mock_empty_container, mock_tarball, mock_yaml_safe_load,
mock_update_passwords, mock_run_playbook):
self, mock_tmp, mock_cd, mock_empty_container, mock_tarball,
mock_yaml_safe_load, mock_update_passwords, mock_run_playbook):
plan_management.update_plan_from_templates(
self.app.client_manager,
'test-overcloud',
@ -317,6 +344,7 @@ class TestPlanUpdateWorkflows(base.TestCommand):
mock_run_playbook.assert_called_once_with(
'cli-update-deployment-plan.yaml',
'undercloud,',
mock.ANY,
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": "test-overcloud",

View File

@ -58,15 +58,16 @@ class DeleteOvercloud(command.Command):
if not confirm:
raise oscexc.CommandError("Action not confirmed, exiting.")
utils.run_ansible_playbook(
"cli-overcloud-delete.yaml",
'undercloud,',
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
verbosity=utils.playbook_verbosity(self=self),
extra_vars={
"stack_name": parsed_args.stack
}
)
with utils.TempDirs() as tmp:
utils.run_ansible_playbook(
"cli-overcloud-delete.yaml",
'undercloud,',
workdir=tmp,
playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
verbosity=utils.playbook_verbosity(self=self),
extra_vars={
"stack_name": parsed_args.stack
}
)
print("Success.")

View File

@ -52,19 +52,20 @@ def deploy(container, run_validations, skip_deploy_identifier,
:param verbosity: Verbosity level
:type verbosity: Integer
"""
utils.run_ansible_playbook(
"cli-deploy-deployment-plan.yaml",
'undercloud,',
ANSIBLE_TRIPLEO_PLAYBOOKS,
verbosity=verbosity,
extra_vars={
"container": container,
"run_validations": run_validations,
"skip_deploy_identifier": skip_deploy_identifier,
"ansible_timeout": timeout,
}
)
with utils.TempDirs() as tmp:
utils.run_ansible_playbook(
"cli-deploy-deployment-plan.yaml",
'undercloud,',
workdir=tmp,
playbook_dir=ANSIBLE_TRIPLEO_PLAYBOOKS,
verbosity=verbosity,
extra_vars={
"container": container,
"run_validations": run_validations,
"skip_deploy_identifier": skip_deploy_identifier,
"ansible_timeout": timeout
}
)
print("Success.")

View File

@ -77,13 +77,15 @@ def create_deployment_plan(container, generate_passwords,
if plan_env_file:
extra_vars['plan_environment'] = plan_env_file
utils.run_ansible_playbook(
"cli-create-deployment-plan.yaml",
'undercloud,',
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars=extra_vars,
verbosity=verbosity_level
)
with utils.TempDirs() as tmp:
utils.run_ansible_playbook(
"cli-create-deployment-plan.yaml",
'undercloud,',
workdir=tmp,
playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars=extra_vars,
verbosity=verbosity_level
)
print("Success.")
@ -106,17 +108,19 @@ def delete_deployment_plan(clients, container):
def update_deployment_plan(clients, verbosity_level=0, **workflow_input):
utils.run_ansible_playbook(
"cli-update-deployment-plan.yaml",
'undercloud,',
constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": workflow_input['container'],
"validate": workflow_input['validate_stack'],
"generate_passwords": workflow_input["generate_passwords"],
},
verbosity=verbosity_level
)
with utils.TempDirs() as tmp:
utils.run_ansible_playbook(
"cli-update-deployment-plan.yaml",
'undercloud,',
workdir=tmp,
playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
extra_vars={
"container": workflow_input['container'],
"validate": workflow_input['validate_stack'],
"generate_passwords": workflow_input["generate_passwords"],
},
verbosity=verbosity_level
)
print("Success.")