~corey.bryant/charms/trusty/glance/amulet-git-fixups

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/utils.py

  • Committer: James Page
  • Date: 2013-09-20 15:52:45 UTC
  • mto: (29.2.187 glance)
  • mto: This revision was merged to the branch mainline in revision 37.
  • Revision ID: james.page@canonical.com-20130920155245-udz175q6z2dw6f87
Renamed tests->unit_tests, updated as appropriate, re-synced charm-helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
3
3
# Common python helper functions used for OpenStack charms.
4
 
 
5
4
from collections import OrderedDict
6
5
 
7
6
import apt_pkg as apt
8
7
import subprocess
9
8
import os
 
9
import socket
10
10
import sys
11
11
 
12
12
from charmhelpers.core.hookenv import (
17
17
 
18
18
from charmhelpers.core.host import (
19
19
    lsb_release,
 
20
)
 
21
 
 
22
from charmhelpers.fetch import (
20
23
    apt_install,
21
24
)
22
25
 
130
133
        e = 'Could not determine version of uninstalled package: %s' % package
131
134
        error_out(e)
132
135
 
133
 
    vers = apt.UpstreamVersion(pkg.current_ver.ver_str)
 
136
    vers = apt.upstream_version(pkg.current_ver.ver_str)
134
137
 
135
138
    try:
136
139
        if 'swift' in pkg.name:
290
293
    available_vers = get_os_version_install_source(src)
291
294
    apt.init()
292
295
    return apt.version_compare(available_vers, cur_vers) == 1
 
296
 
 
297
 
 
298
def is_ip(address):
 
299
    """
 
300
    Returns True if address is a valid IP address.
 
301
    """
 
302
    try:
 
303
        # Test to see if already an IPv4 address
 
304
        socket.inet_aton(address)
 
305
        return True
 
306
    except socket.error:
 
307
        return False
 
308
 
 
309
 
 
310
def ns_query(address):
 
311
    try:
 
312
        import dns.resolver
 
313
    except ImportError:
 
314
        apt_install('python-dnspython')
 
315
        import dns.resolver
 
316
 
 
317
    if isinstance(address, dns.name.Name):
 
318
        rtype = 'PTR'
 
319
    elif isinstance(address, basestring):
 
320
        rtype = 'A'
 
321
 
 
322
    answers = dns.resolver.query(address, rtype)
 
323
    if answers:
 
324
        return str(answers[0])
 
325
    return None
 
326
 
 
327
 
 
328
def get_host_ip(hostname):
 
329
    """
 
330
    Resolves the IP for a given hostname, or returns
 
331
    the input if it is already an IP.
 
332
    """
 
333
    if is_ip(hostname):
 
334
        return hostname
 
335
 
 
336
    return ns_query(hostname)
 
337
 
 
338
 
 
339
def get_hostname(address):
 
340
    """
 
341
    Resolves hostname for given IP, or returns the input
 
342
    if it is already a hostname.
 
343
    """
 
344
    if not is_ip(address):
 
345
        return address
 
346
 
 
347
    try:
 
348
        import dns.reversename
 
349
    except ImportError:
 
350
        apt_install('python-dnspython')
 
351
        import dns.reversename
 
352
 
 
353
    rev = dns.reversename.from_address(address)
 
354
    result = ns_query(rev)
 
355
    if not result:
 
356
        return None
 
357
 
 
358
    # strip trailing .
 
359
    if result.endswith('.'):
 
360
        return result[:-1]
 
361
    return result