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'
78
def configure_source():
79
source = str(config_get('openstack-origin'))
82
if source.startswith('ppa:'):
87
subprocess.check_call(cmd)
88
if source.startswith('cloud:'):
89
# CA values should be formatted as cloud:ubuntu-openstack/pocket, eg:
90
# cloud:precise-folsom/updates or cloud:precise-folsom/proposed
91
install('ubuntu-cloud-keyring')
92
pocket = source.split(':')[1]
93
pocket = pocket.split('-')[1]
94
with open('/etc/apt/sources.list.d/cloud-archive.list', 'w') as apt:
95
apt.write(CLOUD_ARCHIVE.format(CLOUD_ARCHIVE_POCKETS[pocket]))
96
if source.startswith('deb'):
97
l = len(source.split('|'))
99
(apt_line, key) = source.split('|')
102
'adv', '--keyserver keyserver.ubuntu.com',
105
subprocess.check_call(cmd)
109
with open('/etc/apt/sources.list.d/quantum.list', 'w') as apt:
110
apt.write(apt_line + "\n")
115
subprocess.check_call(cmd)
122
def expose(port, protocol='TCP'):
125
'{}/{}'.format(port, protocol)
127
subprocess.check_call(cmd)
130
def juju_log(severity, message):
133
'--log-level', severity,
136
subprocess.check_call(cmd)
143
def wrapper(*args, **kwargs):
145
key = str((func, args, kwargs))
149
res = func(*args, **kwargs)
156
def relation_ids(relation):
161
result = str(subprocess.check_output(cmd)).split()
169
def relation_list(rid):
174
result = str(subprocess.check_output(cmd)).split()
182
def relation_get(attribute, unit=None, rid=None):
189
cmd.append(attribute)
192
value = subprocess.check_output(cmd).strip() # IGNORE:E1103
200
def relation_get_dict(relation_id=None, remote_unit=None):
201
"""Obtain all relation data as dict by way of JSON"""
203
'relation-get', '--format=json'
207
cmd.append(relation_id)
209
remote_unit_orig = os.getenv('JUJU_REMOTE_UNIT', None)
210
os.environ['JUJU_REMOTE_UNIT'] = remote_unit
211
j = subprocess.check_output(cmd)
212
if remote_unit and remote_unit_orig:
213
os.environ['JUJU_REMOTE_UNIT'] = remote_unit_orig
216
# convert unicode to strings
217
for k, v in d.iteritems():
218
settings[str(k)] = str(v)
222
def relation_set(**kwargs):
227
for k, v in kwargs.items():
233
args.append('{}={}'.format(k, v))
235
subprocess.check_call(cmd)
239
def unit_get(attribute):
244
value = subprocess.check_output(cmd).strip() # IGNORE:E1103
252
def config_get(attribute):
258
out = subprocess.check_output(cmd).strip() # IGNORE:E1103
259
cfg = json.loads(out)
262
return cfg[attribute]
268
def get_unit_hostname():
269
return socket.gethostname()
273
def get_host_ip(hostname=unit_get('private-address')):
275
# Test to see if already an IPv4 address
276
socket.inet_aton(hostname)
279
answers = dns.resolver.query(hostname, 'A')
281
return answers[0].address
285
def _svc_control(service, action):
286
subprocess.check_call(['service', service, action])
289
def restart(*services):
290
for service in services:
291
_svc_control(service, 'restart')
295
for service in services:
296
_svc_control(service, 'stop')
299
def start(*services):
300
for service in services:
301
_svc_control(service, 'start')
304
def reload(*services):
305
for service in services:
307
_svc_control(service, 'reload')
308
except subprocess.CalledProcessError:
309
# Reload failed - either service does not support reload
310
# or it was not running - restart will fixup most things
311
_svc_control(service, 'restart')
314
def running(service):
316
output = subprocess.check_output(['service', service, 'status'])
317
except subprocess.CalledProcessError:
320
if ("start/running" in output or
321
"is running" in output):
327
def is_relation_made(relation, key='private-address'):
328
for r_id in (relation_ids(relation) or []):
329
for unit in (relation_list(r_id) or []):
330
if relation_get(key, rid=r_id, unit=unit):