summaryrefslogtreecommitdiff
path: root/cloudinit/transforms/cc_rsyslog.py
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2012-06-15 18:01:03 -0700
committerJoshua Harlow <harlowja@yahoo-inc.com>2012-06-15 18:01:03 -0700
commit508168acb95aee070d493b45656f781a42bdd262 (patch)
treee816b241c500d99f1289fb6afffb33abb560df99 /cloudinit/transforms/cc_rsyslog.py
parent36c1da35c2c0cb1b2ee18b7374bc81df8349e3e2 (diff)
downloadvyos-cloud-init-508168acb95aee070d493b45656f781a42bdd262.tar.gz
vyos-cloud-init-508168acb95aee070d493b45656f781a42bdd262.zip
Complete initial cleanup for refactoring/rework.
Some of the cleanups were the following 1. Using standard (logged) utility functions for sub process work, writing, reading files, and other file system/operating system options 2. Having distrobutions impelement there own subclasses to handle system specifics (if applicable) 3. Having a cloud wrapper that provides just the functionality we want to expose (cloud.py) 4. Using a path class instead of globals for all cloud init paths (it is configured via config) 5. Removal of as much shared global state as possible (there should be none, minus a set of constants) 6. Other various cleanups that remove transforms/handlers/modules from reading/writing/chmoding there own files. a. They should be using util functions to take advantage of the logging that is now enabled in those util functions (very useful for debugging) 7. Urls being read and checked from a single module that serves this and only this purpose (+1 for code organization) 8. Updates to log whenever a transform decides not to run 9. Ensure whenever a exception is thrown (and possibly captured) that the util.logexc function is called a. For debugging, tracing this is important to not just drop them on the floor. 10. Code shuffling into utils.py where it makes sense (and where it could serve a benefit for other code now or in the future)
Diffstat (limited to 'cloudinit/transforms/cc_rsyslog.py')
-rw-r--r--cloudinit/transforms/cc_rsyslog.py52
1 files changed, 25 insertions, 27 deletions
diff --git a/cloudinit/transforms/cc_rsyslog.py b/cloudinit/transforms/cc_rsyslog.py
index ac7f2c74..ccbe68ff 100644
--- a/cloudinit/transforms/cc_rsyslog.py
+++ b/cloudinit/transforms/cc_rsyslog.py
@@ -18,16 +18,15 @@
# 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 cloudinit
-import logging
-import cloudinit.util as util
-import traceback
+import os
+
+from cloudinit import util
DEF_FILENAME = "20-cloud-config.conf"
DEF_DIR = "/etc/rsyslog.d"
-def handle(_name, cfg, _cloud, log, _args):
+def handle(name, cfg, cloud, log, _args):
# rsyslog:
# - "*.* @@192.158.1.1"
# - content: "*.* @@192.0.2.1:10514"
@@ -37,17 +36,17 @@ def handle(_name, cfg, _cloud, log, _args):
# process 'rsyslog'
if not 'rsyslog' in cfg:
+ log.debug("Skipping module named %s, no 'rsyslog' key in configuration", name)
return
def_dir = cfg.get('rsyslog_dir', DEF_DIR)
def_fname = cfg.get('rsyslog_filename', DEF_FILENAME)
files = []
- elst = []
- for ent in cfg['rsyslog']:
+ for i, ent in enumerate(cfg['rsyslog']):
if isinstance(ent, dict):
if not "content" in ent:
- elst.append((ent, "no 'content' entry"))
+ log.warn("No 'content' entry in config entry %s", i + 1)
continue
content = ent['content']
filename = ent.get("filename", def_fname)
@@ -55,8 +54,13 @@ def handle(_name, cfg, _cloud, log, _args):
content = ent
filename = def_fname
+ filename = filename.strip()
+ if not filename:
+ log.warn("Entry %s has an empty filename", i + 1)
+ continue
+
if not filename.startswith("/"):
- filename = "%s/%s" % (def_dir, filename)
+ filename = os.path.join(def_dir, filename)
omode = "ab"
# truncate filename first time you see it
@@ -67,35 +71,29 @@ def handle(_name, cfg, _cloud, log, _args):
try:
util.write_file(filename, content + "\n", omode=omode)
except Exception as e:
- log.debug(traceback.format_exc(e))
- elst.append((content, "failed to write to %s" % filename))
+ util.logexc(log, "Failed to write to %s", filename)
- # need to restart syslogd
+ # Attempt to restart syslogd
restarted = False
try:
- # if this config module is running at cloud-init time
+ # If this config module is running at cloud-init time
# (before rsyslog is running) we don't actually have to
# restart syslog.
#
- # upstart actually does what we want here, in that it doesn't
+ # Upstart actually does what we want here, in that it doesn't
# start a service that wasn't running already on 'restart'
# it will also return failure on the attempt, so 'restarted'
- # won't get set
- log.debug("restarting rsyslog")
+ # won't get set.
+ log.debug("Restarting rsyslog")
util.subp(['service', 'rsyslog', 'restart'])
restarted = True
-
except Exception as e:
- elst.append(("restart", str(e)))
+ util.logexc("Failed restarting rsyslog")
if restarted:
- # this only needs to run if we *actually* restarted
+ # This only needs to run if we *actually* restarted
# syslog above.
- cloudinit.logging_set_from_cfg_file()
- log = logging.getLogger()
- log.debug("rsyslog configured %s" % files)
-
- for e in elst:
- log.warn("rsyslog error: %s\n" % ':'.join(e))
-
- return
+ cloud.cycle_logging()
+ # This should now use rsyslog if
+ # the logging was setup to use it...
+ log.debug("%s configured %s files", name, files)