2
# Copyright 2012 Canonical Ltd.
4
# This file is sourced from lp:openstack-charm-helpers
7
# James Page <james.page@ubuntu.com>
8
# Paul Collins <paul.collins@canonical.com>
9
# Adam Gandelman <adamg@ubuntu.com>
13
from charmhelpers.core.host import (
16
from charmhelpers.core.hookenv import (
22
TEMPLATES_DIR = 'templates'
27
apt_install('python-jinja2', fatal=True)
33
apt_install('python-dnspython', fatal=True)
37
def render_template(template_name, context, template_dir=TEMPLATES_DIR):
38
templates = jinja2.Environment(
39
loader=jinja2.FileSystemLoader(template_dir)
41
template = templates.get_template(template_name)
42
return template.render(context)
46
def get_unit_hostname():
47
return socket.gethostname()
51
def get_host_ip(hostname=None):
52
hostname = hostname or unit_get('private-address')
54
# Test to see if already an IPv4 address
55
socket.inet_aton(hostname)
58
answers = dns.resolver.query(hostname, 'A')
60
return answers[0].address