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>
20
hook = os.path.basename(sys.argv[0])
23
hook_func = hooks[hook]
26
"This charm doesn't know how to handle '{}'.".format(hook))
39
subprocess.check_call(cmd)
41
TEMPLATES_DIR = 'templates'
46
install('python-jinja2')
52
install('python-dnspython')
56
def render_template(template_name, context, template_dir=TEMPLATES_DIR):
57
templates = jinja2.Environment(
58
loader=jinja2.FileSystemLoader(template_dir)
60
template = templates.get_template(template_name)
61
return template.render(context)
64
""" # Ubuntu Cloud Archive
65
deb http://ubuntu-cloud.archive.canonical.com/ubuntu {} main
68
CLOUD_ARCHIVE_POCKETS = {
69
'folsom': 'precise-updates/folsom',
70
'folsom/updates': 'precise-updates/folsom',
71
'folsom/proposed': 'precise-proposed/folsom',
72
'grizzly': 'precise-updates/grizzly',
73
'grizzly/updates': 'precise-updates/grizzly',
74
'grizzly/proposed': 'precise-proposed/grizzly',
75
'havana': 'precise-updates/havana',
76
'havana/updates': 'precise-updates/havana',
77
'havana/proposed': 'precise-proposed/havana',
81
def configure_source():
82
source = str(config_get('openstack-origin'))
85
if source.startswith('ppa:'):
90
subprocess.check_call(cmd)
91
if source.startswith('cloud:'):
92
# CA values should be formatted as cloud:ubuntu-openstack/pocket, eg:
93
# cloud:precise-folsom/updates or cloud:precise-folsom/proposed
94
install('ubuntu-cloud-keyring')
95
pocket = source.split(':')[1]
96
pocket = pocket.split('-')[1]
97
with open('/etc/apt/sources.list.d/cloud-archive.list', 'w') as apt:
98
apt.write(CLOUD_ARCHIVE.format(CLOUD_ARCHIVE_POCKETS[pocket]))
99
if source.startswith('deb'):
100
l = len(source.split('|'))
102
(apt_line, key) = source.split('|')
105
'adv', '--keyserver keyserver.ubuntu.com',
108
subprocess.check_call(cmd)
112
with open('/etc/apt/sources.list.d/quantum.list', 'w') as apt:
113
apt.write(apt_line + "\n")
118
subprocess.check_call(cmd)
125
def expose(port, protocol='TCP'):
128
'{}/{}'.format(port, protocol)
130
subprocess.check_call(cmd)
133
def juju_log(severity, message):
136
'--log-level', severity,
139
subprocess.check_call(cmd)
146
def wrapper(*args, **kwargs):
148
key = str((func, args, kwargs))
152
res = func(*args, **kwargs)
159
def relation_ids(relation):
164
result = str(subprocess.check_output(cmd)).split()
172
def relation_list(rid):
177
result = str(subprocess.check_output(cmd)).split()
185
def relation_get(attribute, unit=None, rid=None):
192
cmd.append(attribute)
195
value = subprocess.check_output(cmd).strip() # IGNORE:E1103
203
def relation_get_dict(relation_id=None, remote_unit=None):
204
"""Obtain all relation data as dict by way of JSON"""
206
'relation-get', '--format=json'
210
cmd.append(relation_id)
213
cmd.append(remote_unit)
214
j = subprocess.check_output(cmd)
217
# convert unicode to strings
218
for k, v in d.iteritems():
219
settings[str(k)] = str(v)
223
def relation_set(**kwargs):
228
for k, v in kwargs.items():
234
args.append('{}={}'.format(k, v))
236
subprocess.check_call(cmd)
240
def unit_get(attribute):
245
value = subprocess.check_output(cmd).strip() # IGNORE:E1103
253
def config_get(attribute):
259
out = subprocess.check_output(cmd).strip() # IGNORE:E1103
260
cfg = json.loads(out)
263
return cfg[attribute]
269
def get_unit_hostname():
270
return socket.gethostname()
274
def get_host_ip(hostname=unit_get('private-address')):
276
# Test to see if already an IPv4 address
277
socket.inet_aton(hostname)
280
answers = dns.resolver.query(hostname, 'A')
282
return answers[0].address
286
def _svc_control(service, action):
287
subprocess.check_call(['service', service, action])
290
def restart(*services):
291
for service in services:
292
_svc_control(service, 'restart')
296
for service in services:
297
_svc_control(service, 'stop')
300
def start(*services):
301
for service in services:
302
_svc_control(service, 'start')
305
def reload(*services):
306
for service in services:
308
_svc_control(service, 'reload')
309
except subprocess.CalledProcessError:
310
# Reload failed - either service does not support reload
311
# or it was not running - restart will fixup most things
312
_svc_control(service, 'restart')
315
def running(service):
317
output = subprocess.check_output(['service', service, 'status'])
318
except subprocess.CalledProcessError:
321
if ("start/running" in output or
322
"is running" in output):
328
def is_relation_made(relation, key='private-address'):
329
for r_id in (relation_ids(relation) or []):
330
for unit in (relation_list(r_id) or []):
331
if relation_get(key, rid=r_id, unit=unit):