Revision 60c42f9f kamaki/cli/commands/cyclades.py

b/kamaki/cli/commands/cyclades.py
67 67
    '  MODEL: permition in octal (e.g. 0777 or o+rwx)']
68 68

  
69 69

  
70
class _server_wait(object):
71

  
72
    wait_arguments = dict(
73
        progress_bar=ProgressBarArgument(
74
            'do not show progress bar',
75
            ('-N', '--no-progress-bar'),
76
            False
77
        )
78
    )
79

  
80
    def _wait(self, server_id, currect_status):
81
        (progress_bar, wait_cb) = self._safe_progress_bar(
82
            'Server %s still in %s mode' % (server_id, currect_status))
83

  
84
        try:
85
            new_mode = self.client.wait_server(
86
                server_id,
87
                currect_status,
88
                wait_cb=wait_cb)
89
        except Exception:
90
            self._safe_progress_bar_finish(progress_bar)
91
            raise
92
        finally:
93
            self._safe_progress_bar_finish(progress_bar)
94
        if new_mode:
95
            print('Server %s is now in %s mode' % (server_id, new_mode))
96
        else:
97
            raiseCLIError(None, 'Time out')
98

  
99

  
70 100
class _init_cyclades(_command_init):
71 101
    @errors.generic.all
72 102
    @addLogSettings
......
191 221

  
192 222

  
193 223
@command(server_cmds)
194
class server_create(_init_cyclades, _optional_json):
224
class server_create(_init_cyclades, _optional_json, _server_wait):
195 225
    """Create a server (aka Virtual Machine)
196 226
    Parameters:
197 227
    - name: (single quoted text)
......
201 231

  
202 232
    arguments = dict(
203 233
        personality=PersonalityArgument(
204
            (80 * ' ').join(howto_personality), ('-p', '--personality'))
234
            (80 * ' ').join(howto_personality), ('-p', '--personality')),
235
        wait=FlagArgument('Wait server to build', ('-w', '--wait'))
205 236
    )
206 237

  
207 238
    @errors.generic.all
......
209 240
    @errors.plankton.id
210 241
    @errors.cyclades.flavor_id
211 242
    def _run(self, name, flavor_id, image_id):
212
        self._print(
213
            self.client.create_server(
214
                name, int(flavor_id), image_id, self['personality']),
215
            print_dict)
243
        r = self.client.create_server(
244
            name, int(flavor_id), image_id, self['personality'])
245
        self._print(r, print_dict)
246
        if self['wait']:
247
            self._wait(r['id'], r['status'])
216 248

  
217 249
    def main(self, name, flavor_id, image_id):
218 250
        super(self.__class__, self)._run()
......
238 270

  
239 271

  
240 272
@command(server_cmds)
241
class server_delete(_init_cyclades, _optional_output_cmd):
273
class server_delete(_init_cyclades, _optional_output_cmd, _server_wait):
242 274
    """Delete a server (VM)"""
243 275

  
276
    arguments = dict(
277
        wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait'))
278
    )
279

  
244 280
    @errors.generic.all
245 281
    @errors.cyclades.connection
246 282
    @errors.cyclades.server_id
247 283
    def _run(self, server_id):
248
            self._optional_output(self.client.delete_server(int(server_id)))
284
            status = 'DELETED'
285
            if self['wait']:
286
                details = self.client.get_server_details(server_id)
287
                status = details['status']
288

  
289
            r = self.client.delete_server(int(server_id))
290
            self._optional_output(r)
291

  
292
            if self['wait']:
293
                self._wait(server_id, status)
249 294

  
250 295
    def main(self, server_id):
251 296
        super(self.__class__, self)._run()
......
253 298

  
254 299

  
255 300
@command(server_cmds)
256
class server_reboot(_init_cyclades, _optional_output_cmd):
301
class server_reboot(_init_cyclades, _optional_output_cmd, _server_wait):
257 302
    """Reboot a server (VM)"""
258 303

  
259 304
    arguments = dict(
260
        hard=FlagArgument('perform a hard reboot', ('-f', '--force'))
305
        hard=FlagArgument('perform a hard reboot', ('-f', '--force')),
306
        wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait'))
261 307
    )
262 308

  
263 309
    @errors.generic.all
264 310
    @errors.cyclades.connection
265 311
    @errors.cyclades.server_id
266 312
    def _run(self, server_id):
267
        self._optional_output(
268
            self.client.reboot_server(int(server_id), self['hard']))
313
        r = self.client.reboot_server(int(server_id), self['hard'])
314
        self._optional_output(r)
315

  
316
        if self['wait']:
317
            self._wait(server_id, 'REBOOT')
269 318

  
270 319
    def main(self, server_id):
271 320
        super(self.__class__, self)._run()
......
273 322

  
274 323

  
275 324
@command(server_cmds)
276
class server_start(_init_cyclades, _optional_output_cmd):
325
class server_start(_init_cyclades, _optional_output_cmd, _server_wait):
277 326
    """Start an existing server (VM)"""
278 327

  
328
    arguments = dict(
329
        wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait'))
330
    )
331

  
279 332
    @errors.generic.all
280 333
    @errors.cyclades.connection
281 334
    @errors.cyclades.server_id
282 335
    def _run(self, server_id):
283
        self._optional_output(self.client.start_server(int(server_id)))
336
        status = 'ACTIVE'
337
        if self['wait']:
338
            details = self.client.get_server_details(server_id)
339
            status = details['status']
340
            if status in ('ACTIVE', ):
341
                return
342

  
343
        r = self.client.start_server(int(server_id))
344
        self._optional_output(r)
345

  
346
        if self['wait']:
347
            self._wait(server_id, status)
284 348

  
285 349
    def main(self, server_id):
286 350
        super(self.__class__, self)._run()
......
288 352

  
289 353

  
290 354
@command(server_cmds)
291
class server_shutdown(_init_cyclades, _optional_output_cmd):
355
class server_shutdown(_init_cyclades, _optional_output_cmd, _server_wait):
292 356
    """Shutdown an active server (VM)"""
293 357

  
358
    arguments = dict(
359
        wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait'))
360
    )
361

  
294 362
    @errors.generic.all
295 363
    @errors.cyclades.connection
296 364
    @errors.cyclades.server_id
297 365
    def _run(self, server_id):
298
        self._optional_output(self.client.shutdown_server(int(server_id)))
366
        status = 'STOPPED'
367
        if self['wait']:
368
            details = self.client.get_server_details(server_id)
369
            status = details['status']
370
            if status in ('STOPPED', ):
371
                return
372

  
373
        r = self.client.shutdown_server(int(server_id))
374
        self._optional_output(r)
375

  
376
        if self['wait']:
377
            self._wait(server_id, status)
299 378

  
300 379
    def main(self, server_id):
301 380
        super(self.__class__, self)._run()
......
476 555

  
477 556

  
478 557
@command(server_cmds)
479
class server_wait(_init_cyclades):
558
class server_wait(_init_cyclades, _server_wait):
480 559
    """Wait for server to finish [BUILD, STOPPED, REBOOT, ACTIVE]"""
481 560

  
482
    arguments = dict(
483
        progress_bar=ProgressBarArgument(
484
            'do not show progress bar',
485
            ('-N', '--no-progress-bar'),
486
            False
487
        )
488
    )
489

  
490 561
    @errors.generic.all
491 562
    @errors.cyclades.connection
492 563
    @errors.cyclades.server_id
493 564
    def _run(self, server_id, currect_status):
494
        (progress_bar, wait_cb) = self._safe_progress_bar(
495
            'Server %s still in %s mode' % (server_id, currect_status))
496

  
497
        try:
498
            new_mode = self.client.wait_server(
499
                server_id,
500
                currect_status,
501
                wait_cb=wait_cb)
502
        except Exception:
503
            self._safe_progress_bar_finish(progress_bar)
504
            raise
505
        finally:
506
            self._safe_progress_bar_finish(progress_bar)
507
        if new_mode:
508
            print('Server %s is now in %s mode' % (server_id, new_mode))
509
        else:
510
            raiseCLIError(None, 'Time out')
565
        self._wait(server_id, currect_status)
511 566

  
512 567
    def main(self, server_id, currect_status='BUILD'):
513 568
        super(self.__class__, self)._run()

Also available in: Unified diff