7
7
# Paul Collins <paul.collins@canonical.com>
18
hook = os.path.basename(sys.argv[0])
21
hook_func = hooks[hook]
24
"This charm doesn't know how to handle '{}'.".format(hook))
37
subprocess.check_call(cmd)
12
from charmhelpers.core.hookenv import (
16
from charmhelpers.core.host import (
18
filter_installed_packages
39
21
TEMPLATES_DIR = 'templates'
43
25
except ImportError:
44
install('python-jinja2')
26
apt_install(filter_installed_packages(['python-jinja2']),
48
31
import dns.resolver
49
32
except ImportError:
50
install('python-dnspython')
33
apt_install(filter_installed_packages(['python-dnspython']),
51
35
import dns.resolver
54
38
def render_template(template_name, context, template_dir=TEMPLATES_DIR):
55
39
templates = jinja2.Environment(
56
loader=jinja2.FileSystemLoader(template_dir)
40
loader=jinja2.FileSystemLoader(template_dir))
58
41
template = templates.get_template(template_name)
59
42
return template.render(context)
63
""" # Ubuntu Cloud Archive
64
deb http://ubuntu-cloud.archive.canonical.com/ubuntu {} main
68
def configure_source():
69
source = str(config_get('source'))
72
if source.startswith('ppa:'):
77
subprocess.check_call(cmd)
78
if source.startswith('cloud:'):
79
install('ubuntu-cloud-keyring')
80
pocket = source.split(':')[1]
81
with open('/etc/apt/sources.list.d/cloud-archive.list', 'w') as apt:
82
apt.write(CLOUD_ARCHIVE.format(pocket))
83
if source.startswith('http:'):
84
with open('/etc/apt/sources.list.d/ceph.list', 'w') as apt:
85
apt.write("deb " + source + "\n")
86
key = config_get('key')
90
'adv', '--keyserver keyserver.ubuntu.com',
93
subprocess.check_call(cmd)
98
subprocess.check_call(cmd)
101
45
def enable_pocket(pocket):
102
46
apt_sources = "/etc/apt/sources.list"
103
47
with open(apt_sources, "r") as sources:
110
54
sources.write(line)
117
def expose(port, protocol='TCP'):
120
'{}/{}'.format(port, protocol)
122
subprocess.check_call(cmd)
125
def juju_log(severity, message):
128
'--log-level', severity,
131
subprocess.check_call(cmd)
134
def relation_ids(relation):
139
return subprocess.check_output(cmd).split() # IGNORE:E1103
142
def relation_list(rid):
147
return subprocess.check_output(cmd).split() # IGNORE:E1103
150
def relation_get(attribute, unit=None, rid=None):
157
cmd.append(attribute)
160
value = str(subprocess.check_output(cmd)).strip()
167
def relation_set(**kwargs):
172
for k, v in kwargs.items():
177
args.append('{}={}'.format(k, v))
179
subprocess.check_call(cmd)
182
def unit_get(attribute):
187
value = str(subprocess.check_output(cmd)).strip()
194
def config_get(attribute):
199
value = str(subprocess.check_output(cmd)).strip()
206
58
def get_unit_hostname():
207
59
return socket.gethostname()
210
def get_host_ip(hostname=unit_get('private-address')):
63
def get_host_ip(hostname=None):
64
hostname = hostname or unit_get('private-address')
212
66
# Test to see if already an IPv4 address
213
67
socket.inet_aton(hostname)