232
def get_machine_data():
233
return yaml.safe_load(run('juju', 'status'))['machines']
236
def wait_for_machine(num_machines=1, timeout=300):
237
"""Wait `timeout` seconds for `num_machines` machines to come up.
239
This wait_for... function can be called by other wait_for functions
240
whose timeouts might be too short in situations where only a bare
241
Juju setup has been bootstrapped.
243
# You may think this is a hack, and you'd be right. The easiest way
244
# to tell what environment we're working in (LXC vs EC2) is to check
245
# the dns-name of the first machine. If it's localhost we're in LXC
246
# and we can just return here.
247
if get_machine_data()[0]['dns-name'] == 'localhost':
249
start_time = time.time()
251
# Drop the first machine, since it's the Zookeeper and that's
252
# not a machine that we need to wait for. This will only work
253
# for EC2 environments, which is why we return early above if
255
machine_data = get_machine_data()
256
non_zookeeper_machines = [
257
machine_data[key] for key in machine_data.keys()[1:]]
258
if len(non_zookeeper_machines) >= num_machines:
259
all_machines_running = True
260
for machine in non_zookeeper_machines:
261
if machine['instance-state'] != 'running':
262
all_machines_running = False
264
if all_machines_running:
266
if time.time() - start_time >= timeout:
267
raise RuntimeError('timeout waiting for service to start')
232
271
def wait_for_unit(service_name, timeout=480):
272
"""Wait `timeout` seconds for a given service name to come up."""
273
wait_for_machine(num_machines=1)
233
274
start_time = time.time()
235
276
state = unit_info(service_name, 'state')
245
286
def wait_for_relation(service_name, relation_name, timeout=120):
287
"""Wait `timeout` seconds for a given relation to come up."""
246
288
start_time = time.time()
248
290
relation = unit_info(service_name, 'relations').get(relation_name)
256
def wait_for_page_contents(url, s, timeout=120):
298
def wait_for_page_contents(url, expected_contents, timeout=120):
299
"""Wait `timeout` seconds for a given URL to contain a given string."""
257
300
start_time = time.time()