Move run command to utils

The run command in the deploy can be reused. It would be better in utils
rather than buried in the tripleo deploy command.

Change-Id: Ia40660c4d2cfbc8a5c7ff3a2a21e82abd8e3ecd3
This commit is contained in:
Alex Schultz 2018-05-01 09:39:50 -06:00
parent f335b9e040
commit fd36cf4f8e
3 changed files with 30 additions and 19 deletions

View File

@ -254,8 +254,8 @@ class TestDeployUndercloud(TestPluginV1):
autospec=True)
@mock.patch('tripleoclient.v1.tripleo_deploy.Deploy.'
'_update_passwords_env', autospec=True)
@mock.patch('tripleoclient.v1.tripleo_deploy.Deploy.'
'_run_and_log_output', autospec=True)
@mock.patch('tripleoclient.utils.'
'run_command_and_log', autospec=True)
@mock.patch('tempfile.mkdtemp', autospec=True, return_value='/twd')
@mock.patch('shutil.copytree', autospec=True)
def test_setup_heat_environments(self,
@ -328,15 +328,15 @@ class TestDeployUndercloud(TestPluginV1):
mock_inventory.write_static_inventory.assert_called_once_with(
fake_output_dir + '/inventory.yaml', extra_vars)
@mock.patch('tripleoclient.v1.tripleo_deploy.Deploy.'
'_run_and_log_output', autospec=True)
@mock.patch('tripleoclient.utils.'
'run_command_and_log', autospec=True)
@mock.patch('os.chdir')
@mock.patch('os.execvp')
def test_launch_ansible(self, mock_execvp, mock_chdir, mock_run):
self.cmd._launch_ansible('/tmp')
mock_chdir.assert_called_once()
mock_run.assert_called_once_with(self.cmd, [
mock_run.assert_called_once_with(self.cmd.log, [
'ansible-playbook', '-i', '/tmp/inventory.yaml',
'deploy_steps_playbook.yaml', '-e', 'role_name=Undercloud',
'-e', 'deploy_server_id=undercloud', '-e',

View File

@ -1033,3 +1033,26 @@ def bulk_symlink(log, src, dst, tmpd='/tmp'):
raise
finally:
shutil.rmtree(tmp, ignore_errors=True)
def run_command_and_log(log, cmd, cwd=None):
"""Run command and log output
:param log: logger instance for logging
:type log: Logger
:param cmd: command in list form
:param cmd: List
:param cwd: current worknig directory for execution
:param cmd: String
"""
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=False,
bufsize=1, cwd=cwd)
for line in iter(proc.stdout.readline, b''):
# TODO(aschultz): this should probably goto a log file
log.warning(line.rstrip())
proc.stdout.close()
return proc.wait()

View File

@ -22,7 +22,6 @@ import pwd
import re
import shutil
import six
import subprocess
import sys
import tarfile
import tempfile
@ -166,17 +165,6 @@ class Deploy(command.Command):
constants.PUPPET_MODULES,
constants.PUPPET_BASE)
def _run_and_log_output(self, cmd, cwd=None):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=False,
bufsize=1, cwd=cwd)
for line in iter(proc.stdout.readline, b''):
# TODO(aschultz): this should probably goto a log file
self.log.warning(line.rstrip())
proc.stdout.close()
return proc.wait()
def _update_passwords_env(self, output_dir, passwords=None):
pw_file = os.path.join(output_dir, 'tripleo-undercloud-passwords.yaml')
undercloud_pw_file = os.path.join(output_dir,
@ -372,7 +360,7 @@ class Deploy(command.Command):
'tools/process-templates.py')
args = ['python', process_templates, '--roles-data',
parsed_args.roles_file, '--output-dir', self.tht_render]
if self._run_and_log_output(args, cwd=self.tht_render) != 0:
if utils.run_command_and_log(self.log, args, cwd=self.tht_render) != 0:
# TODO(aschultz): improve error messaging
raise exceptions.DeploymentError("Problems generating templates.")
@ -538,7 +526,7 @@ class Deploy(command.Command):
'-e', 'deploy_server_id=undercloud', '-e',
'bootstrap_server_id=undercloud']
self.log.debug('Running Ansible: %s' % (' '.join(cmd)))
return self._run_and_log_output(cmd)
return utils.run_command_and_log(self.log, cmd)
def get_parser(self, prog_name):
parser = argparse.ArgumentParser(