def resolve_hostname_to_ip(hostname):
    """Resolve hostname to IP

    @param hostname: hostname to be resolved
    @returns IP address or None if resolution was not possible via DNS
    """
    try:
        import dns.resolver
    except ImportError:
        apt_install(filter_installed_packages(['python-dnspython']),
                    fatal=True)
        import dns.resolver

    if config('prefer-ipv6'):
        if is_ipv6(hostname):
            return hostname

        query_type = 'AAAA'
    elif is_ip(hostname):
        return hostname
    else:
        query_type = 'A'

    # This may throw an NXDOMAIN exception; in which case
    # things are badly broken so just let it kill the hook
    answers = dns.resolver.query(hostname, query_type)
    if answers:
        return answers[0].address
def resolve_hostname_to_ip(hostname):
    """Resolve hostname to IP

    @param hostname: hostname to be resolved
    @returns IP address or None if resolution was not possible via DNS
    """
    try:
        import dns.resolver
    except ImportError:
        apt_install(filter_installed_packages(['python-dnspython']),
                    fatal=True)
        import dns.resolver

    if config('prefer-ipv6'):
        if is_ipv6(hostname):
            return hostname

        query_type = 'AAAA'
    elif is_ip(hostname):
        return hostname
    else:
        query_type = 'A'

    # This may throw an NXDOMAIN exception; in which case
    # things are badly broken so just let it kill the hook
    answers = dns.resolver.query(hostname, query_type)
    if answers:
        return answers[0].address
def post_pg_license():
    '''
    Posts PLUMgrid License if it hasnt been posted already.
    '''
    key = config('plumgrid-license-key')
    if key is None:
        log('PLUMgrid License Key not specified')
        return 0
    PG_VIP = config('plumgrid-virtual-ip')
    if not is_ip(PG_VIP):
        raise ValueError('Invalid IP Provided')
    LICENSE_POST_PATH = 'https://%s/0/tenant_manager/license_key' % PG_VIP
    LICENSE_GET_PATH = 'https://%s/0/tenant_manager/licenses' % PG_VIP
    PG_CURL = '%s/opt/pg/scripts/pg_curl.sh' % PG_LXC_PATH
    license = {"key1": {"license": key}}
    licence_post_cmd = [
        PG_CURL,
        '-u',
        'plumgrid:plumgrid',
        LICENSE_POST_PATH,
        '-d',
        json.dumps(license)]
    licence_get_cmd = [PG_CURL, '-u', 'plumgrid:plumgrid', LICENSE_GET_PATH]
    try:
        old_license = subprocess.check_output(licence_get_cmd)
    except subprocess.CalledProcessError:
        log('No response from specified virtual IP')
        return 0
    _exec_cmd(cmd=licence_post_cmd,
              error_msg='Unable to post License', fatal=False)
    new_license = subprocess.check_output(licence_get_cmd)
    if old_license == new_license:
        log('No change in PLUMgrid License')
        return 0
    return 1
Ejemplo n.º 4
0
    def get_listening(self, listen=['0.0.0.0']):
        """Returns a list of addresses SSH can list on

        Turns input into a sensible list of IPs SSH can listen on. Input
        must be a python list of interface names, IPs and/or CIDRs.

        :param listen: list of IPs, CIDRs, interface names

        :returns: list of IPs available on the host
        """
        if listen == ['0.0.0.0']:
            return listen

        value = []
        for network in listen:
            try:
                ip = get_address_in_network(network=network, fatal=True)
            except ValueError:
                if is_ip(network):
                    ip = network
                else:
                    try:
                        ip = get_iface_addr(iface=network, fatal=False)[0]
                    except IndexError:
                        continue
            value.append(ip)
        if value == []:
            return ['0.0.0.0']
        return value
Ejemplo n.º 5
0
def sort_sans(sans):
    """
    Split SANs into IP SANs and name SANs

    :param sans: List of SANs
    :type sans: list
    :returns: List of IP SANs and list of name SANs
    :rtype: ([], [])
    """
    ip_sans = {s for s in sans if ch_ip.is_ip(s)}
    alt_names = set(sans).difference(ip_sans)
    return sorted(list(ip_sans)), sorted(list(alt_names))
Ejemplo n.º 6
0
def _sort_sans(sans):
    """
    Split SANs into IP SANs and name SANs
    :param sans: List of SANs
    :type sans: list
    :returns: List of IP SANs and list of name SANs
    :rtype: ([], [])
    """
    logging.info("Splitting '{}' into IP and alt names".format(sans))
    ip_sans = {s for s in sans if ch_ip.is_ip(s)}
    alt_names = set(sans).difference(ip_sans)
    return sorted(list(ip_sans)), sorted(list(alt_names))
Ejemplo n.º 7
0
 def test_is_ip(self):
     self.assertTrue(net_ip.is_ip('10.0.0.1'))
     self.assertTrue(net_ip.is_ip('2001:db8:1:0:2918:3444:852:5b8a'))
     self.assertFalse(net_ip.is_ip('www.ubuntu.com'))
Ejemplo n.º 8
0
 def test_is_ip(self):
     self.assertTrue(net_ip.is_ip('10.0.0.1'))
     self.assertFalse(net_ip.is_ip('www.ubuntu.com'))