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

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/utils.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:
23
23
import subprocess
24
24
import json
25
25
import os
26
 
import socket
27
26
import sys
28
27
 
29
28
import six
30
29
import yaml
31
30
 
 
31
from charmhelpers.contrib.network import ip
 
32
 
32
33
from charmhelpers.core.hookenv import (
33
34
    config,
34
35
    log as juju_log,
421
422
    else:
422
423
        zap_disk(block_device)
423
424
 
424
 
 
425
 
def is_ip(address):
426
 
    """
427
 
    Returns True if address is a valid IP address.
428
 
    """
429
 
    try:
430
 
        # Test to see if already an IPv4 address
431
 
        socket.inet_aton(address)
432
 
        return True
433
 
    except socket.error:
434
 
        return False
435
 
 
436
 
 
437
 
def ns_query(address):
438
 
    try:
439
 
        import dns.resolver
440
 
    except ImportError:
441
 
        apt_install('python-dnspython')
442
 
        import dns.resolver
443
 
 
444
 
    if isinstance(address, dns.name.Name):
445
 
        rtype = 'PTR'
446
 
    elif isinstance(address, six.string_types):
447
 
        rtype = 'A'
448
 
    else:
449
 
        return None
450
 
 
451
 
    answers = dns.resolver.query(address, rtype)
452
 
    if answers:
453
 
        return str(answers[0])
454
 
    return None
455
 
 
456
 
 
457
 
def get_host_ip(hostname):
458
 
    """
459
 
    Resolves the IP for a given hostname, or returns
460
 
    the input if it is already an IP.
461
 
    """
462
 
    if is_ip(hostname):
463
 
        return hostname
464
 
 
465
 
    return ns_query(hostname)
466
 
 
467
 
 
468
 
def get_hostname(address, fqdn=True):
469
 
    """
470
 
    Resolves hostname for given IP, or returns the input
471
 
    if it is already a hostname.
472
 
    """
473
 
    if is_ip(address):
474
 
        try:
475
 
            import dns.reversename
476
 
        except ImportError:
477
 
            apt_install('python-dnspython')
478
 
            import dns.reversename
479
 
 
480
 
        rev = dns.reversename.from_address(address)
481
 
        result = ns_query(rev)
482
 
        if not result:
483
 
            return None
484
 
    else:
485
 
        result = address
486
 
 
487
 
    if fqdn:
488
 
        # strip trailing .
489
 
        if result.endswith('.'):
490
 
            return result[:-1]
491
 
        else:
492
 
            return result
493
 
    else:
494
 
        return result.split('.')[0]
 
425
is_ip = ip.is_ip
 
426
ns_query = ip.ns_query
 
427
get_host_ip = ip.get_host_ip
 
428
get_hostname = ip.get_hostname
495
429
 
496
430
 
497
431
def get_matchmaker_map(mm_file='/etc/oslo/matchmaker_ring.json'):