From 419b9451684f2c1bc5bef73b26ae8fca1a17383d Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Fri, 15 Jun 2012 17:45:52 -0700 Subject: Instead of having the cloud pass large references to its constructor, this has been reduced to actual objects. Added a get template filename helper which can be used to locate template files for various handlers/transforms. Ensured that the config that we give back out is copied, so that it can't be modified by any 'malicous' handlers/transforms. Added helper method cycle_logging that can resetup logging, this is mainly used by the rsyslog transform. --- cloudinit/cloud.py | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/cloudinit/cloud.py b/cloudinit/cloud.py index 765e7d3a..b2dfc749 100644 --- a/cloudinit/cloud.py +++ b/cloudinit/cloud.py @@ -20,6 +20,9 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import copy +import os + from cloudinit import distros from cloudinit import helpers from cloudinit import log as logging @@ -28,18 +31,40 @@ LOG = logging.getLogger(__name__) class Cloud(object): - def __init__(self, datasource, paths, cfg): + def __init__(self, datasource, paths, cfg, distro, runners): self.datasource = datasource self.paths = paths - self.cfg = cfg - self.distro = distros.fetch(cfg, self) - self.runners = helpers.Runners(paths) + self.distro = distro + self._cfg = cfg + self._runners = runners + + # If a transform manipulates logging or logging services + # it is typically useful to cause the logging to be + # setup again. + def cycle_logging(self): + logging.setupLogging(self.cfg) + + @property + def cfg(self): + # Ensure that not indirectly modified + return copy.deepcopy(self._cfg) def run(self, name, functor, args, freq=None, clear_on_fail=False): - return self.runners.run(name, functor, args, freq, clear_on_fail) + return self._runners.run(name, functor, args, freq, clear_on_fail) + + def get_template_filename(self, name): + fn = self.paths.template_tpl % (name) + if not os.path.isfile(fn): + LOG.warn("No template found at %s for template named %s", fn, name) + return None + return fn + # The rest of thes are just useful proxies def get_userdata(self): return self.datasource.get_userdata() + + def get_instance_id(self): + return self.datasource.get_instance_id() def get_public_ssh_keys(self): return self.datasource.get_public_ssh_keys() @@ -47,7 +72,7 @@ class Cloud(object): def get_locale(self): return self.datasource.get_locale() - def get_mirror(self): + def get_local_mirror(self): return self.datasource.get_local_mirror() def get_hostname(self, fqdn=False): -- cgit v1.2.3