Example #1
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)
Example #2
0
def handle(_name, cfg, cloud, log, args):

    if len(args) != 0:
        ph_cfg = util.read_conf(args[0])
    else:
        if not 'phone_home' in cfg:
            return
        ph_cfg = cfg['phone_home']

    if 'url' not in ph_cfg:
        log.warn("No 'url' token in phone_home")
        return

    url = ph_cfg['url']
    post_list = ph_cfg.get('post', 'all')
    tries = ph_cfg.get('tries')
    timeout = ph_cfg.get('timeout')
    try_wait = ph_cfg.get('try_wait')
    try:
        tries = int(tries)
    except:
        tries = 10
        log.warn("Tries is not an integer. using %s", tries)

    try:
        timeout = int(timeout)
    except:
        timeout = 5
        log.warn("Timeout is not an integer. using %s", timeout)

    try:
        try_wait = int(try_wait)
    except:
        try_wait = 3
        log.warn("Wait time between tries is not an integer. using %s", try_wait)

    if post_list == "all":
        post_list = ['pub_key_dsa', 'pub_key_rsa',
                     'pub_key_ecdsa', 'instance_id',
                     'hostname']

    all_keys = {}
    all_keys['instance_id'] = cloud.get_instance_id()
    all_keys['hostname'] = cloud.get_hostname()

    pubkeys = {
        'pub_key_dsa': '/etc/ssh/ssh_host_dsa_key.pub',
        'pub_key_rsa': '/etc/ssh/ssh_host_rsa_key.pub',
        'pub_key_ecdsa': '/etc/ssh/ssh_host_ecdsa_key.pub',
    }

    for n, path in pubkeys.iteritems():
        try:
            with open(path, "rb") as fp:
                all_keys[n] = fp.read()
        except:
            log.warn("%s: failed to open" % path)

    submit_keys = {}
    for k in post_list:
        if k in all_keys:
            submit_keys[k] = all_keys[k]
        else:
            submit_keys[k] = "N/A"
            log.warn("Requested key %s from 'post' list not available")

    url = util.render_string(url, {'INSTANCE_ID': all_keys['instance_id']})
    last_e = None
    for i in range(0, tries):
        try:
            util.readurl(url, submit_keys, timeout=timeout)
            log.debug("Succeeded submit to %s on try %i" % (url, i + 1))
            return
        except Exception as e:
            log.warn("Failed to post to %s on try %i" % (url, i + 1))
            last_e = e
        log.info("Waiting %s seconds before the next attempt", try_wait)
        sleep(try_wait)

    log.warn("Failed to post to %s in %i tries," % (url, tries))
    if last_e is not None:
        raise last_e