~gary/charms/oneiric/buildbot-master/typo

« back to all changes in this revision

Viewing changes to hooks/helpers.py

  • Committer: Graham Binns
  • Date: 2012-02-13 18:06:04 UTC
  • mfrom: (24.3.10 master-smart-timeouts)
  • Revision ID: graham@canonical.com-20120213180604-659cy3phqblstl9s
Merged smarter timeouts work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
229
229
    return item
230
230
 
231
231
 
 
232
def get_machine_data():
 
233
    return yaml.safe_load(run('juju', 'status'))['machines']
 
234
 
 
235
 
 
236
def wait_for_machine(num_machines=1, timeout=300):
 
237
    """Wait `timeout` seconds for `num_machines` machines to come up.
 
238
 
 
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.
 
242
    """
 
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':
 
248
        return
 
249
    start_time = time.time()
 
250
    while True:
 
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
 
254
        # we're in LXC.
 
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
 
263
                    break
 
264
            if all_machines_running:
 
265
                break
 
266
        if time.time() - start_time >= timeout:
 
267
            raise RuntimeError('timeout waiting for service to start')
 
268
        time.sleep(0.1)
 
269
 
 
270
 
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()
234
275
    while True:
235
276
        state = unit_info(service_name, 'state')
243
284
 
244
285
 
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()
247
289
    while True:
248
290
        relation = unit_info(service_name, 'relations').get(relation_name)
253
295
        time.sleep(0.1)
254
296
 
255
297
 
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()
258
301
    while True:
259
302
        try:
262
305
            pass
263
306
        else:
264
307
            page = stream.read()
265
 
            if s in page:
 
308
            if expected_contents in page:
266
309
                return page
267
310
        if time.time() - start_time >= timeout:
268
311
            raise RuntimeError('timeout waiting for contents of ' + url)