コード例 #1
0
def ssh_compute_add(public_key, rid=None, unit=None, user=None):
    # If remote compute node hands us a hostname, ensure we have a
    # known hosts entry for its IP, hostname and FQDN.
    private_address = relation_get(rid=rid,
                                   unit=unit,
                                   attribute='private-address')
    hosts = [private_address]

    if not is_ipv6(private_address):
        if relation_get('hostname'):
            hosts.append(relation_get('hostname'))

        if not is_ip(private_address):
            hosts.append(get_host_ip(private_address))
            short = private_address.split('.')[0]
            if ns_query(short):
                hosts.append(short)
        else:
            hn = get_hostname(private_address)
            if hn:
                hosts.append(hn)
                short = hn.split('.')[0]
                if ns_query(short):
                    hosts.append(short)

    for host in list(set(hosts)):
        add_known_host(host, unit, user)

    if not ssh_authorized_key_exists(public_key, unit, user):
        log('Saving SSH authorized key for compute host at %s.' %
            private_address)
        add_authorized_key(public_key, unit, user)
コード例 #2
0
def ssh_compute_add_host_and_key(public_key,
                                 hostname,
                                 private_address,
                                 application_name,
                                 user=None):
    """Add a compute nodes ssh details to local cache.

    Collect various hostname variations and add the corresponding host keys to
    the local known hosts file. Finally, add the supplied public key to the
    authorized_key file.

    :param public_key: Public key.
    :type public_key: str
    :param hostname: Hostname to collect host keys from.
    :type hostname: str
    :param private_address:aCorresponding private address for hostname
    :type private_address: str
    :param application_name: Name of application eg nova-compute-something
    :type application_name: str
    :param user: The user that the ssh asserts are for.
    :type user: str
    """
    # If remote compute node hands us a hostname, ensure we have a
    # known hosts entry for its IP, hostname and FQDN.
    hosts = [private_address]

    if not is_ipv6(private_address):
        if hostname:
            hosts.append(hostname)

        if is_ip(private_address):
            hn = get_hostname(private_address)
            if hn:
                hosts.append(hn)
                short = hn.split('.')[0]
                if ns_query(short):
                    hosts.append(short)
        else:
            hosts.append(get_host_ip(private_address))
            short = private_address.split('.')[0]
            if ns_query(short):
                hosts.append(short)

    for host in list(set(hosts)):
        add_known_host(host, application_name, user)

    if not ssh_authorized_key_exists(public_key, application_name, user):
        log('Saving SSH authorized key for compute host at %s.' %
            private_address)
        add_authorized_key(public_key, application_name, user)
コード例 #3
0
def ssh_compute_add_host_and_key(public_key, hostname, private_address,
                                 application_name, user=None):
    """Add a compute nodes ssh details to local cache.

    Collect various hostname variations and add the corresponding host keys to
    the local known hosts file. Finally, add the supplied public key to the
    authorized_key file.

    :param public_key: Public key.
    :type public_key: str
    :param hostname: Hostname to collect host keys from.
    :type hostname: str
    :param private_address:aCorresponding private address for hostname
    :type private_address: str
    :param application_name: Name of application eg nova-compute-something
    :type application_name: str
    :param user: The user that the ssh asserts are for.
    :type user: str
    """
    # If remote compute node hands us a hostname, ensure we have a
    # known hosts entry for its IP, hostname and FQDN.
    hosts = [private_address]

    if not is_ipv6(private_address):
        if hostname:
            hosts.append(hostname)

        if is_ip(private_address):
            hn = get_hostname(private_address)
            if hn:
                hosts.append(hn)
                short = hn.split('.')[0]
                if ns_query(short):
                    hosts.append(short)
        else:
            hosts.append(get_host_ip(private_address))
            short = private_address.split('.')[0]
            if ns_query(short):
                hosts.append(short)

    for host in list(set(hosts)):
        add_known_host(host, application_name, user)

    if not ssh_authorized_key_exists(public_key, application_name, user):
        log('Saving SSH authorized key for compute host at %s.' %
            private_address)
        add_authorized_key(public_key, application_name, user)
コード例 #4
0
 def test_ns_query_trigger_apt_install(self, apt_install):
     fake_dns = FakeDNS('5.5.5.5')
     with patch(builtin_import, side_effect=[ImportError, fake_dns]):
         nsq = net_ip.ns_query('5.5.5.5')
         if six.PY2:
             apt_install.assert_called_with('python-dnspython', fatal=True)
         else:
             apt_install.assert_called_with('python3-dnspython', fatal=True)
     self.assertEquals(nsq, '5.5.5.5')
コード例 #5
0
 def test_ns_query_lookup_fail(self, apt_install):
     fake_dns = FakeDNS('')
     with patch(builtin_import, side_effect=[fake_dns, fake_dns]):
         nsq = net_ip.ns_query('nonexistant')
     self.assertEquals(nsq, None)
コード例 #6
0
 def test_ns_query_blank_record(self, apt_install):
     fake_dns = FakeDNS(None)
     with patch(builtin_import, side_effect=[fake_dns, fake_dns]):
         nsq = net_ip.ns_query(None)
     self.assertEquals(nsq, None)
コード例 #7
0
 def test_ns_query_a_record(self, apt_install):
     fake_dns = FakeDNS('127.0.0.1')
     fake_dns_name = FakeDNSName('www.somedomain.tld')
     with patch(builtin_import, side_effect=[fake_dns]):
         nsq = net_ip.ns_query(fake_dns_name)
     self.assertEquals(nsq, '127.0.0.1')
コード例 #8
0
 def test_ns_query_ptr_record(self, apt_install):
     fake_dns = FakeDNS('127.0.0.1')
     with patch(builtin_import, side_effect=[fake_dns]):
         nsq = net_ip.ns_query('127.0.0.1')
     self.assertEquals(nsq, '127.0.0.1')
コード例 #9
0
ファイル: test_ip.py プロジェクト: baycarbone/charm-helpers
 def test_ns_query_loopup_fail_real_implementation(self, apt_install):
     self.assertEqual(net_ip.ns_query('nonexistant'), None)
     apt_install.assert_not_called()