Exemplo n.º 1
0
def handle(_name, _cfg, cloud, log, _args):
    try:
        ud = cloud.get_userdata_raw()
    except:
        log.warn("failed to get raw userdata in %s" % my_name)
        return

    try:
        mdict = parse_qs(ud)
        if not my_hookname in mdict:
            return
    except:
        log.warn("failed to urlparse.parse_qa(userdata_raw())")
        raise

    scripts_d = get_ipath_cur('scripts')
    i = 0
    first_e = None
    for url in mdict[my_hookname]:
        fname = "%s/rightscale-%02i" % (scripts_d, i)
        i = i + 1
        try:
            content = util.readurl(url)
            util.write_file(fname, content, mode=0700)
        except Exception as e:
            if not first_e:
                first_e = None
            log.warn("%s failed to read %s: %s" % (my_name, url, e))

    if first_e:
        raise (e)
def handle(_name, _cfg, cloud, log, _args):
    try:
        ud = cloud.get_userdata_raw()
    except:
        log.warn("failed to get raw userdata in %s" % my_name)
        return

    try:
        mdict = parse_qs(ud)
        if not my_hookname in mdict:
            return
    except:
        log.warn("failed to urlparse.parse_qa(userdata_raw())")
        raise

    scripts_d = get_ipath_cur('scripts')
    i = 0
    first_e = None
    for url in mdict[my_hookname]:
        fname = "%s/rightscale-%02i" % (scripts_d, i)
        i = i + 1
        try:
            content = util.readurl(url)
            util.write_file(fname, content, mode=0700)
        except Exception as e:
            if not first_e:
                first_e = None
            log.warn("%s failed to read %s: %s" % (my_name, url, e))

    if first_e:
        raise(e)
Exemplo n.º 3
0
def get_cmdline_url(names=('cloud-config-url', 'url'),
                    starts="#cloud-config", cmdline=None):

    if cmdline == None:
        cmdline = util.get_cmdline()

    data = util.keyval_str_to_dict(cmdline)
    url = None
    key = None
    for key in names:
        if key in data:
            url = data[key]
            break
    if url == None:
        return (None, None, None)

    contents = util.readurl(url)

    if contents.startswith(starts):
        return (key, url, contents)

    return (key, url, None)
Exemplo n.º 4
0
def get_cmdline_url(
        names=('cloud-config-url', 'url'), starts="#cloud-config",
        cmdline=None):

    if cmdline == None:
        cmdline = util.get_cmdline()

    data = util.keyval_str_to_dict(cmdline)
    url = None
    key = None
    for key in names:
        if key in data:
            url = data[key]
            break
    if url == None:
        return (None, None, None)

    contents = util.readurl(url)

    if contents.startswith(starts):
        return (key, url, contents)

    return (key, url, None)
Exemplo n.º 5
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', 10)
    try:
        tries = int(tries)
    except:
        log.warn("tries is not an integer. using 10")
        tries = 10

    if post_list == "all":
        post_list = post_list_all

    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:
            fp = open(path, "rb")
            all_keys[n] = fp.read()
            fp.close()
        except:
            log.warn("%s: failed to open in phone_home" % 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']})

    null_exc = object()
    last_e = null_exc
    for i in range(0, tries):
        try:
            util.readurl(url, submit_keys)
            log.debug("succeeded submit to %s on try %i" % (url, i + 1))
            return
        except Exception as e:
            log.debug("failed to post to %s on try %i" % (url, i + 1))
            last_e = e
        sleep(3)

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

    return