summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cloudinit/config/cc_write_files.py7
-rw-r--r--tests/unittests/test_handler/test_handler_write_files.py12
2 files changed, 18 insertions, 1 deletions
diff --git a/cloudinit/config/cc_write_files.py b/cloudinit/config/cc_write_files.py
index 31d1db61..0b6546e2 100644
--- a/cloudinit/config/cc_write_files.py
+++ b/cloudinit/config/cc_write_files.py
@@ -49,6 +49,10 @@ binary gzip data can be specified and will be decoded before being written.
...
path: /bin/arch
permissions: '0555'
+ - content: |
+ 15 * * * * root ship_logs
+ path: /etc/crontab
+ append: true
"""
import base64
@@ -113,7 +117,8 @@ def write_files(name, files):
contents = extract_contents(f_info.get('content', ''), extractions)
(u, g) = util.extract_usergroup(f_info.get('owner', DEFAULT_OWNER))
perms = decode_perms(f_info.get('permissions'), DEFAULT_PERMS)
- util.write_file(path, contents, mode=perms)
+ omode = 'ab' if util.get_cfg_option_bool(f_info, 'append') else 'wb'
+ util.write_file(path, contents, omode=omode, mode=perms)
util.chownbyname(path, u, g)
diff --git a/tests/unittests/test_handler/test_handler_write_files.py b/tests/unittests/test_handler/test_handler_write_files.py
index 7fa8fd21..bc8756ca 100644
--- a/tests/unittests/test_handler/test_handler_write_files.py
+++ b/tests/unittests/test_handler/test_handler_write_files.py
@@ -52,6 +52,18 @@ class TestWriteFiles(FilesystemMockingTestCase):
"test_simple", [{"content": expected, "path": filename}])
self.assertEqual(util.load_file(filename), expected)
+ def test_append(self):
+ self.patchUtils(self.tmp)
+ existing = "hello "
+ added = "world\n"
+ expected = existing + added
+ filename = "/tmp/append.file"
+ util.write_file(filename, existing)
+ write_files(
+ "test_append",
+ [{"content": added, "path": filename, "append": "true"}])
+ self.assertEqual(util.load_file(filename), expected)
+
def test_yaml_binary(self):
self.patchUtils(self.tmp)
data = util.load_yaml(YAML_TEXT)