Ensure the ansible log file always has a correct UID

This change will ensure that the owner of the ansible.log file is
always set to the executing user, even when the tripleoclient is
invoked using sudo.

> The chagne will pull the UID and should the log file exist it
  will set the UID to the calling user accordingly. In the event
  of an upgrade, or command rerun, the client will ensure we're
  cleaning updating the file UID without user intevention; this
  makes the change transparent to the user and fixes a potential
  UX problem.

Closes-Bug: #1895318
Change-Id: If620fb3026c7420f37b924aa99d62f29a11730a9
Signed-off-by: Kevin Carter <kecarter@redhat.com>
This commit is contained in:
Kevin Carter 2020-09-14 11:35:47 -05:00 committed by Kevin Carter (cloudnull)
parent 4c7df58d9b
commit 7d9e41589a
1 changed files with 11 additions and 2 deletions

View File

@ -575,8 +575,9 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
env['TRIPLEO_PLAN_NAME'] = plan
get_uid = int(os.getenv('SUDO_UID', os.getuid()))
try:
user_pwd = pwd.getpwuid(int(os.getenv('SUDO_UID', os.getuid())))
user_pwd = pwd.getpwuid(get_uid)
except TypeError:
home = constants.CLOUD_HOME_DIR
else:
@ -673,7 +674,15 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
f.write('{} $@\n'.format(' '.join(runner_config.command)))
os.chmod(command_path, 0o750)
status, rc = runner.run()
try:
status, rc = runner.run()
finally:
# NOTE(cloudnull): After a playbook executes, ensure the log
# file, if it exists, was created with
# appropriate ownership.
_log_path = r_opts['envvars']['ANSIBLE_LOG_PATH']
if os.path.isfile(_log_path):
os.chown(_log_path, get_uid, -1)
if rc != 0:
err_msg = (