~openstack-charmers/charms/trusty/nova-compute/0mq

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/network/ip.py

  • Committer: james.page at ubuntu
  • Date: 2015-03-16 14:18:05 UTC
  • mfrom: (79.2.28 nova-compute)
  • Revision ID: james.page@ubuntu.com-20150316141805-eko8x0x1gfyrqzeo
Rebase

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import glob
18
18
import re
19
19
import subprocess
 
20
import six
 
21
import socket
20
22
 
21
23
from functools import partial
22
24
 
23
25
from charmhelpers.core.hookenv import unit_get
24
26
from charmhelpers.fetch import apt_install
25
27
from charmhelpers.core.hookenv import (
26
 
    log
 
28
    log,
 
29
    WARNING,
27
30
)
28
31
 
29
32
try:
365
368
            return True
366
369
 
367
370
    return False
 
371
 
 
372
 
 
373
def is_ip(address):
 
374
    """
 
375
    Returns True if address is a valid IP address.
 
376
    """
 
377
    try:
 
378
        # Test to see if already an IPv4 address
 
379
        socket.inet_aton(address)
 
380
        return True
 
381
    except socket.error:
 
382
        return False
 
383
 
 
384
 
 
385
def ns_query(address):
 
386
    try:
 
387
        import dns.resolver
 
388
    except ImportError:
 
389
        apt_install('python-dnspython')
 
390
        import dns.resolver
 
391
 
 
392
    if isinstance(address, dns.name.Name):
 
393
        rtype = 'PTR'
 
394
    elif isinstance(address, six.string_types):
 
395
        rtype = 'A'
 
396
    else:
 
397
        return None
 
398
 
 
399
    answers = dns.resolver.query(address, rtype)
 
400
    if answers:
 
401
        return str(answers[0])
 
402
    return None
 
403
 
 
404
 
 
405
def get_host_ip(hostname, fallback=None):
 
406
    """
 
407
    Resolves the IP for a given hostname, or returns
 
408
    the input if it is already an IP.
 
409
    """
 
410
    if is_ip(hostname):
 
411
        return hostname
 
412
 
 
413
    ip_addr = ns_query(hostname)
 
414
    if not ip_addr:
 
415
        try:
 
416
            ip_addr = socket.gethostbyname(hostname)
 
417
        except:
 
418
            log("Failed to resolve hostname '%s'" % (hostname),
 
419
                level=WARNING)
 
420
            return fallback
 
421
    return ip_addr
 
422
 
 
423
 
 
424
def get_hostname(address, fqdn=True):
 
425
    """
 
426
    Resolves hostname for given IP, or returns the input
 
427
    if it is already a hostname.
 
428
    """
 
429
    if is_ip(address):
 
430
        try:
 
431
            import dns.reversename
 
432
        except ImportError:
 
433
            apt_install("python-dnspython")
 
434
            import dns.reversename
 
435
 
 
436
        rev = dns.reversename.from_address(address)
 
437
        result = ns_query(rev)
 
438
        if not result:
 
439
            return None
 
440
    else:
 
441
        result = address
 
442
 
 
443
    if fqdn:
 
444
        # strip trailing .
 
445
        if result.endswith('.'):
 
446
            return result[:-1]
 
447
        else:
 
448
            return result
 
449
    else:
 
450
        return result.split('.')[0]