示例#1
0
    def get_data_source(self):
        if self.datasource is not None:
            return True

        if self.restore_from_cache():
            log.debug("Restored from cached datasource: %s" % self.datasource)
            return True

        cfglist = self.cfg['datasource_list']
        dslist = list_sources(cfglist, self.ds_deps)
        dsnames = [f.__name__ for f in dslist]
        log.debug("Searching for data source in %s" % dsnames)
        for cls in dslist:
            ds = cls.__name__
            try:
                s = cls(sys_cfg=self.cfg)
                log.debug("Checking if %r can provide us the needed data.", ds)
                if s.get_data():
                    self.datasource = s
                    self.datasource_name = ds
                    return True
            except Exception:
                log.warn("Get data of %s raised!", ds)
                util.logexc(log)

        msg = "Did not find data source. Searched classes: %s" % (dsnames)
        log.debug(msg)
        raise excp.DataSourceNotFoundException(msg)
示例#2
0
def handler_handle_part(mod, ctype, filename, payload, frequency):
    modfreq = getattr(mod, "frequency", per_instance)
    if not (modfreq == per_always or
            (frequency == per_instance and modfreq == per_instance)):
        return
    try:
        mod.handle_part(ctype, filename, payload)
    except:
        util.logexc(log)
示例#3
0
def handle(_name, cfg, cloud, log, _args):
    if util.get_cfg_option_bool(cfg, "preserve_hostname", False):
        log.debug("Option 'preserve_hostname' is set. Not setting hostname.")
        return True

    (hostname, _fqdn) = util.get_hostname_fqdn(cfg, cloud)
    try:
        old_name = set_hostname(hostname, log)
        if old_name:
            prev_fn = "%s/%s" % (cloud.get_cpath('data'), "previous-hostname")
            util.write_file(prev_fn, "%s\n" % (old_name), 0644)
    except Exception:
        util.logexc(log)
        log.warn("Failed to set hostname to %s", hostname)

    return True
示例#4
0
    def get_config_obj(self, cfgfile):
        try:
            cfg = util.read_conf(cfgfile)
        except Exception:
            log.critical("Failed loading of cloud config '%s'. "
                         "Continuing with empty config" % cfgfile)
            util.logexc(log)
            cfg = None

        if cfg is None:
            cfg = {}

        try:
            ds_cfg = self.cloud.datasource.get_config_obj()
        except Exception:
            ds_cfg = {}

        cfg = util.mergedict(cfg, ds_cfg)
        return util.mergedict(cfg, self.cloud.cfg)
示例#5
0
    def wait_for_metadata_service(self):
        mcfg = self.ds_cfg

        if not hasattr(mcfg, "get"):
            mcfg = {}

        max_wait = 120
        try:
            max_wait = int(mcfg.get("max_wait", max_wait))
        except Exception:
            util.logexc(log)
            log.warn("Failed to get max wait. using %s" % max_wait)

        if max_wait == 0:
            return False

        timeout = 50
        try:
            timeout = int(mcfg.get("timeout", timeout))
        except Exception:
            util.logexc(log)
            log.warn("Failed to get timeout, using %s" % timeout)

        def_mdurls = ["http://169.254.169.254", "http://instance-data:8773"]
        mdurls = mcfg.get("metadata_urls", def_mdurls)

        # Remove addresses from the list that wont resolve.
        filtered = [x for x in mdurls if util.is_resolvable_url(x)]

        if set(filtered) != set(mdurls):
            log.debug(
                "Removed the following unresolveable addrs from metadata urls: %s" % list((set(mdurls) - set(filtered)))
            )

        if len(filtered):
            mdurls = filtered
        else:
            log.warn("Empty metadata url list! using default list %s", def_mdurls)
            mdurls = def_mdurls

        urls = []
        url2base = {}
        for url in mdurls:
            cur = "%s/%s/meta-data/instance-id" % (url, self.api_ver)
            urls.append(cur)
            url2base[cur] = url

        starttime = time.time()

        def status_cb(url, why, details):
            log.warn("Calling %r failed due to %r: %s", url, why, details)

        url = wait_for_metadata_service(urls=urls, max_wait=max_wait, timeout=timeout, status_cb=status_cb)

        if url:
            log.info("Using metadata source: '%s'" % url2base.get(url))
        else:
            log.critical("Giving up on metadata fetching after %i seconds" % int(time.time() - starttime))

        self.metadata_address = url2base.get(url) or False
        return bool(url)