summaryrefslogtreecommitdiff
path: root/cloudinit/config/cc_write_files.py
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2012-07-10 17:06:45 -0700
committerJoshua Harlow <harlowja@yahoo-inc.com>2012-07-10 17:06:45 -0700
commit7b24230437a27779bafa0828e73ab595c5aeb202 (patch)
tree1e0da0898e4361aefec5ba72dd6e346f008e095e /cloudinit/config/cc_write_files.py
parent86f0d708564ab210f2f58f3e76f94a4af56e360b (diff)
downloadvyos-cloud-init-7b24230437a27779bafa0828e73ab595c5aeb202.tar.gz
vyos-cloud-init-7b24230437a27779bafa0828e73ab595c5aeb202.zip
Fixes 1012854 by implementing file writing, adjusts
other code to have user/group parsing in util instead of in stages.py, renames decomp_str to decomp_gzip since it is more meaningful when named that (as thats all it can decompress).
Diffstat (limited to 'cloudinit/config/cc_write_files.py')
-rw-r--r--cloudinit/config/cc_write_files.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/cloudinit/config/cc_write_files.py b/cloudinit/config/cc_write_files.py
new file mode 100644
index 00000000..683eac27
--- /dev/null
+++ b/cloudinit/config/cc_write_files.py
@@ -0,0 +1,69 @@
+# vi: ts=4 expandtab
+#
+# Copyright (C) 2012 Yahoo! Inc.
+#
+# Author: Joshua Harlow <harlowja@yahoo-inc.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3, as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import base64
+import os
+
+from cloudinit import util
+from cloudinit.settings import PER_INSTANCE
+
+frequency = PER_INSTANCE
+
+DEFAULT_PERMS = 0644
+
+
+def handle(name, cfg, _cloud, log, _args):
+ files = cfg.get('files')
+ if not files:
+ log.debug(("Skipping module named %s,"
+ " no/empty 'files' key in configuration"), name)
+ return
+ write_files(name, files, log)
+
+
+def write_files(name, files, log):
+ if not files:
+ return
+
+ for (i, f_info) in enumerate(files):
+ path = f_info.get('path')
+ if not path:
+ log.warn("No path provided to write for entry %s in module %s",
+ i + 1, name)
+ continue
+ path = os.path.abspath(path)
+ contents = decode_string(f_info.get('content', ''),
+ f_info.get('compression'))
+ (u, g) = util.extract_usergroup(f_info.get('owner'))
+ perms = safe_int(f_info.get('permissions'), DEFAULT_PERMS)
+ util.write_file(path, contents, mode=perms)
+ util.chownbyname(path, u, g)
+
+
+def safe_int(text, default):
+ try:
+ return int(text)
+ except (TypeError, ValueError):
+ return default
+
+
+def decode_string(contents, content_type):
+ if util.is_true(content_type, addons=['gzip', 'gz']):
+ contents_dec = base64.b64decode(contents)
+ contents = util.decomp_gzip(contents_dec, quiet=False)
+ return contents