From 57a2265ebb641d617f7250e7790177172c4ced1b Mon Sep 17 00:00:00 2001 From: Clint Byrum Date: Fri, 15 Mar 2013 09:18:31 -0700 Subject: [PATCH] Atomically replace files --- os_config_applier/os_config_applier.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/os_config_applier/os_config_applier.py b/os_config_applier/os_config_applier.py index c9f8bb5..43c5815 100755 --- a/os_config_applier/os_config_applier.py +++ b/os_config_applier/os_config_applier.py @@ -10,6 +10,7 @@ from pystache.context import KeyNotFoundError from subprocess import Popen, PIPE from value_types import * from config_exception import * +from tempfile import NamedTemporaryFile TEMPLATES_DIR = os.environ.get('OS_CONFIG_APPLIER_TEMPLATES', '/opt/stack/os-config-applier/templates') @@ -37,7 +38,9 @@ def write_file(path, contents): logger.info("writing %s", path) d = os.path.dirname(path) os.path.exists(d) or os.makedirs(d) - with open(path, 'w') as f: f.write(contents) + with NamedTemporaryFile(dir=d, delete=False) as newfile: + newfile.write(contents) + os.rename(newfile.name, path) # return a map of filenames->filecontents def build_tree(templates, config):