From 0675b9e8fa14f1820435f16d27e3db03997e8d6a Mon Sep 17 00:00:00 2001 From: Christian Adams Date: Wed, 6 May 2020 17:54:46 -0400 Subject: [PATCH 1/2] Make rsyslog.conf writes atomic - This writes the rsyslog.conf in a temporary dir, then replaces the original in one atomic operation. --- awx/main/utils/external_logging.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/awx/main/utils/external_logging.py b/awx/main/utils/external_logging.py index 8444b1cfbb..587ff0933b 100644 --- a/awx/main/utils/external_logging.py +++ b/awx/main/utils/external_logging.py @@ -1,5 +1,6 @@ import os - +import shutil +import tempfile import urllib.parse as urlparse from django.conf import settings @@ -112,6 +113,10 @@ def construct_rsyslog_conf_template(settings=settings): def reconfigure_rsyslog(): tmpl = construct_rsyslog_conf_template() - with open('/var/lib/awx/rsyslog/rsyslog.conf', 'w') as f: + # Write config to a temp file then move it to preserve atomicity + temp_dir = tempfile.TemporaryDirectory(prefix='rsyslog-conf-', dir='/tmp') + path = temp_dir.name + '/rsyslog.conf.temp' + with open(path, 'w') as f: f.write(tmpl + '\n') + shutil.move(path, '/var/lib/awx/rsyslog/rsyslog.conf') supervisor_service_command(command='restart', service='awx-rsyslogd') From 37125102abb12b555140f731850fd193831df6cf Mon Sep 17 00:00:00 2001 From: Christian Adams Date: Thu, 7 May 2020 12:24:34 -0400 Subject: [PATCH 2/2] Clean up rsyslog config temp dir - dir is cleaned up at end of 'with' context --- awx/main/utils/external_logging.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/awx/main/utils/external_logging.py b/awx/main/utils/external_logging.py index 587ff0933b..743e0f45ab 100644 --- a/awx/main/utils/external_logging.py +++ b/awx/main/utils/external_logging.py @@ -114,9 +114,9 @@ def construct_rsyslog_conf_template(settings=settings): def reconfigure_rsyslog(): tmpl = construct_rsyslog_conf_template() # Write config to a temp file then move it to preserve atomicity - temp_dir = tempfile.TemporaryDirectory(prefix='rsyslog-conf-', dir='/tmp') - path = temp_dir.name + '/rsyslog.conf.temp' - with open(path, 'w') as f: - f.write(tmpl + '\n') - shutil.move(path, '/var/lib/awx/rsyslog/rsyslog.conf') + with tempfile.TemporaryDirectory(prefix='rsyslog-conf-') as temp_dir: + path = temp_dir + '/rsyslog.conf.temp' + with open(path, 'w') as f: + f.write(tmpl + '\n') + shutil.move(path, '/var/lib/awx/rsyslog/rsyslog.conf') supervisor_service_command(command='restart', service='awx-rsyslogd')