diff options
author | Scott Moser <smoser@brickies.net> | 2016-08-19 13:58:52 -0400 |
---|---|---|
committer | Scott Moser <smoser@brickies.net> | 2016-08-22 12:58:33 -0400 |
commit | 685ffd49561bb92971f6b76e4690b86d7d6ecc0f (patch) | |
tree | 7f71a18d816476359435baeaa9334a95ffba1df1 /cloudinit | |
parent | 1f8b37e0d80534d2055ee2e888f5a7e36c4b98b4 (diff) | |
download | vyos-cloud-init-685ffd49561bb92971f6b76e4690b86d7d6ecc0f.tar.gz vyos-cloud-init-685ffd49561bb92971f6b76e4690b86d7d6ecc0f.zip |
Minor cleanups to atomic_helper and add unit tests.
Change atomic_helper.write_file to have same same signature as write_file.
Add some simple unit tests for atomic_helper.
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/atomic_helper.py | 20 | ||||
-rw-r--r-- | cloudinit/cmd/main.py | 12 | ||||
-rw-r--r-- | cloudinit/dhclient_hook.py | 4 |
3 files changed, 21 insertions, 15 deletions
diff --git a/cloudinit/atomic_helper.py b/cloudinit/atomic_helper.py index 15319f71..a3cfd942 100644 --- a/cloudinit/atomic_helper.py +++ b/cloudinit/atomic_helper.py @@ -5,21 +5,27 @@ import json import os import tempfile +_DEF_PERMS = 0o644 -def atomic_write_file(path, content, mode='w'): + +def write_file(filename, content, mode=_DEF_PERMS, omode="wb"): + # open filename in mode 'omode', write content, set permissions to 'mode' tf = None try: - tf = tempfile.NamedTemporaryFile(dir=os.path.dirname(path), - delete=False, mode=mode) + tf = tempfile.NamedTemporaryFile(dir=os.path.dirname(filename), + delete=False, mode=omode) tf.write(content) tf.close() - os.rename(tf.name, path) + os.chmod(tf.name, mode) + os.rename(tf.name, filename) except Exception as e: if tf is not None: os.unlink(tf.name) raise e -def atomic_write_json(path, data): - return atomic_write_file(path, json.dumps(data, indent=1, - sort_keys=True) + "\n") +def write_json(filename, data, mode=_DEF_PERMS): + # dump json representation of data to file filename. + return write_file( + filename, json.dumps(data, indent=1, sort_keys=True) + "\n", + omode="w", mode=mode) diff --git a/cloudinit/cmd/main.py b/cloudinit/cmd/main.py index ba22b168..83eb02c9 100644 --- a/cloudinit/cmd/main.py +++ b/cloudinit/cmd/main.py @@ -46,7 +46,7 @@ from cloudinit.reporting import events from cloudinit.settings import (PER_INSTANCE, PER_ALWAYS, PER_ONCE, CLOUD_CONFIG) -from cloudinit.atomic_helper import atomic_write_json +from cloudinit import atomic_helper from cloudinit.dhclient_hook import LogDhclient @@ -513,7 +513,7 @@ def status_wrapper(name, args, data_d=None, link_d=None): v1['stage'] = mode v1[mode]['start'] = time.time() - atomic_write_json(status_path, status) + atomic_helper.write_json(status_path, status) util.sym_link(os.path.relpath(status_path, link_d), status_link, force=True) @@ -536,7 +536,7 @@ def status_wrapper(name, args, data_d=None, link_d=None): v1[mode]['finished'] = time.time() v1['stage'] = None - atomic_write_json(status_path, status) + atomic_helper.write_json(status_path, status) if mode == "modules-final": # write the 'finished' file @@ -545,9 +545,9 @@ def status_wrapper(name, args, data_d=None, link_d=None): if v1[m]['errors']: errors.extend(v1[m].get('errors', [])) - atomic_write_json(result_path, - {'v1': {'datasource': v1['datasource'], - 'errors': errors}}) + atomic_helper.write_json( + result_path, {'v1': {'datasource': v1['datasource'], + 'errors': errors}}) util.sym_link(os.path.relpath(result_path, link_d), result_link, force=True) diff --git a/cloudinit/dhclient_hook.py b/cloudinit/dhclient_hook.py index 9dcbe39c..82cb1855 100644 --- a/cloudinit/dhclient_hook.py +++ b/cloudinit/dhclient_hook.py @@ -3,7 +3,7 @@ import os -from cloudinit.atomic_helper import atomic_write_json +from cloudinit import atomic_helper from cloudinit import log as logging from cloudinit import stages @@ -46,5 +46,5 @@ class LogDhclient(object): envs = os.environ if self.hook_file is None: return - atomic_write_json(self.hook_file, self.get_vals(envs)) + atomic_helper.write_json(self.hook_file, self.get_vals(envs)) LOG.debug("Wrote dhclient options in %s", self.hook_file) |