Revision 7b2e4bf1 kamaki/clients/cyclades/__init__.py

b/kamaki/clients/cyclades/__init__.py
257 257
            req = dict(remove=dict(attachment=nic))
258 258
            self.networks_post(netid, 'action', json_data=req)
259 259

  
260
    def wait_server(
261
            self,
262
            server_id,
263
            current_status='BUILD',
264
            delay=0.5,
265
            max_wait=128,
266
            wait_cb=None):
267
        """Wait for server while its status is current_status
260
    def _wait(
261
            self, item_id, current_status, get_status,
262
            delay=1, max_wait=100, wait_cb=None):
263
        """Wait for item while its status is current_status
268 264

  
269 265
        :param server_id: integer (str or int)
270 266

  
271
        :param current_status: (str) BUILD|ACTIVE|STOPPED|DELETED|REBOOT
267
        :param current_status: (str)
268

  
269
        :param get_status: (method(self, item_id)) if called, returns
270
            (status, progress %) If no way to tell progress, return None
272 271

  
273 272
        :param delay: time interval between retries
274 273

  
275
        :param wait_cb: if set a progressbar is used to show progress
274
        :param wait_cb: if set a progress bar is used to show progress
276 275

  
277
        :returns: (str) the new mode if succesfull, (bool) False if timed out
276
        :returns: (str) the new mode if successful, (bool) False if timed out
278 277
        """
279
        r = self.get_server_details(server_id)
280
        if r['status'] != current_status:
281
            return r['status']
278
        status, progress = get_status(self, item_id)
279
        if status != current_status:
280
            return status
282 281
        old_wait = total_wait = 0
283 282

  
284
        if current_status == 'BUILD':
285
            max_wait = 100
286
            wait_gen = wait_cb(max_wait) if wait_cb else None
287
        elif wait_cb:
283
        if wait_cb:
288 284
            wait_gen = wait_cb(1 + max_wait // delay)
289 285
            wait_gen.next()
290 286

  
291
        while r['status'] == current_status and total_wait <= max_wait:
292
            if current_status == 'BUILD':
293
                total_wait = int(r['progress'])
294
                if wait_cb:
295
                    for i in range(int(old_wait), int(total_wait)):
287
        while status == current_status and total_wait <= max_wait:
288
            if wait_cb:
289
                try:
290
                    for i in range(total_wait - old_wait):
296 291
                        wait_gen.next()
297
                    old_wait = total_wait
298
                else:
299
                    stdout.write('.')
300
                    stdout.flush()
292
                except Exception:
293
                    break
301 294
            else:
302
                if wait_cb:
303
                    wait_gen.next()
304
                else:
305
                    stdout.write('.')
306
                    stdout.flush()
307
                total_wait += delay
295
                stdout.write('.')
296
                stdout.flush()
297
            old_wait = total_wait
298
            total_wait = progress or (total_wait + 1)
308 299
            sleep(delay)
309
            r = self.get_server_details(server_id)
300
            status, progress = get_status(self, item_id)
310 301

  
311
        if r['status'] != current_status:
302
        if total_wait < max_wait:
312 303
            if wait_cb:
313 304
                try:
314
                    while True:
305
                    for i in range(max_wait):
315 306
                        wait_gen.next()
316 307
                except:
317 308
                    pass
318
            return r['status']
319
        return False
309
        return status if status != current_status else False
310

  
311
    def wait_server(
312
            self, server_id,
313
            current_status='BUILD',
314
            delay=1, max_wait=100, wait_cb=None):
315
        """Wait for server while its status is current_status
316

  
317
        :param server_id: integer (str or int)
318

  
319
        :param current_status: (str) BUILD|ACTIVE|STOPPED|DELETED|REBOOT
320

  
321
        :param delay: time interval between retries
322

  
323
        :param wait_cb: if set a progressbar is used to show progress
324

  
325
        :returns: (str) the new mode if succesfull, (bool) False if timed out
326
        """
327

  
328
        def get_status(self, server_id):
329
            r = self.get_server_details(server_id)
330
            return r['status'], (r.get('progress', None) if (
331
                            current_status in ('BUILD', )) else None)
332

  
333
        return self._wait(
334
            server_id, current_status, get_status, delay, max_wait, wait_cb)
335

  
336
    def wait_network(
337
            self, net_id,
338
            current_status='LALA', delay=1, max_wait=100, wait_cb=None):
339
        """Wait for network while its status is current_status
340

  
341
        :param net_id: integer (str or int)
342

  
343
        :param current_status: (str) PENDING | ACTIVE | DELETED
344

  
345
        :param delay: time interval between retries
346

  
347
        :param wait_cb: if set a progressbar is used to show progress
348

  
349
        :returns: (str) the new mode if succesfull, (bool) False if timed out
350
        """
351

  
352
        def get_status(self, net_id):
353
            r = self.get_network_details(net_id)
354
            return r['status'], None
355

  
356
        return self._wait(
357
            net_id, current_status, get_status, delay, max_wait, wait_cb)
320 358

  
321 359
    def get_floating_ip_pools(self):
322 360
        """

Also available in: Unified diff