Revision f533f224 tools/cloud
b/tools/cloud | ||
---|---|---|
1 | 1 |
#!/usr/bin/env python |
2 | 2 |
|
3 |
from functools import wraps |
|
4 | 3 |
from httplib import HTTPConnection |
5 | 4 |
from optparse import OptionParser |
6 | 5 |
from os.path import basename |
... | ... | |
23 | 22 |
return decorator |
24 | 23 |
|
25 | 24 |
|
26 |
def address_to_string(address): |
|
27 |
key = address['id'] |
|
28 |
val = ' '.join(ip['addr'] for ip in address['values']) |
|
29 |
return '%s: %s' % (key, val) |
|
25 |
def print_addresses(networks): |
|
26 |
for i, net in enumerate(networks): |
|
27 |
key = 'addresses:'.rjust(13) if i == 0 else ' ' * 13 |
|
28 |
addr = '' |
|
29 |
if 'values' in net: |
|
30 |
addr = '[%s]' % ' '.join(ip['addr'] for ip in net['values']) |
|
31 |
|
|
32 |
val = '%s/%s %s %s' % (net['id'], net['name'], net['mac'], addr) |
|
33 |
print '%s %s' % (key, val) |
|
30 | 34 |
|
31 | 35 |
def print_dict(d, show_empty=True): |
32 | 36 |
for key, val in sorted(d.items()): |
33 | 37 |
if key == 'metadata': |
34 | 38 |
val = ', '.join('%s="%s"' % x for x in val['values'].items()) |
35 | 39 |
elif key == 'addresses': |
36 |
val = ', '.join(address_to_string(address) for address in val['values']) |
|
40 |
print_addresses(val['values']) |
|
41 |
continue |
|
37 | 42 |
elif key == 'servers': |
38 | 43 |
val = ', '.join(str(server_id) for server_id in val['values']) |
39 | 44 |
if val or show_empty: |
... | ... | |
273 | 278 |
reply = self.http_get(path) |
274 | 279 |
|
275 | 280 |
addresses = [reply['network']] if network else reply['addresses']['values'] |
276 |
for address in addresses: |
|
277 |
print address_to_string(address) |
|
281 |
print_addresses(addresses) |
|
278 | 282 |
|
279 | 283 |
|
280 | 284 |
@command_name('lsflv') |
... | ... | |
488 | 492 |
id = network.pop('id') |
489 | 493 |
name = network.pop('name') |
490 | 494 |
if self.detail: |
491 |
print '%d %s' % (id, name)
|
|
495 |
print '%s %s' % (id, name)
|
|
492 | 496 |
print_dict(network) |
493 | 497 |
|
494 | 498 |
else: |
495 |
print '%3d %s' % (id, name)
|
|
499 |
print '%3s %s' % (id, name)
|
|
496 | 500 |
|
497 | 501 |
|
498 | 502 |
@command_name('createnet') |
... | ... | |
542 | 546 |
self.http_delete(path) |
543 | 547 |
|
544 | 548 |
|
545 |
@command_name('addnet')
|
|
549 |
@command_name('connect')
|
|
546 | 550 |
class AddNetwork(Command): |
547 |
description = 'add server to a network'
|
|
551 |
description = 'connect a server to a network'
|
|
548 | 552 |
syntax = '<server id> <network id>' |
549 | 553 |
|
550 | 554 |
def execute(self, server_id, network_id): |
... | ... | |
553 | 557 |
self.http_post(path, body, expected_status=202) |
554 | 558 |
|
555 | 559 |
|
556 |
@command_name('removenet')
|
|
560 |
@command_name('disconnect')
|
|
557 | 561 |
class RemoveNetwork(Command): |
558 |
description = 'remove server from a network'
|
|
562 |
description = 'disconnect a server from a network'
|
|
559 | 563 |
syntax = '<server id> <network id>' |
560 | 564 |
|
561 | 565 |
def execute(self, server_id, network_id): |
... | ... | |
564 | 568 |
self.http_post(path, body, expected_status=202) |
565 | 569 |
|
566 | 570 |
|
571 |
def print_usage(): |
|
572 |
print 'Usage: %s <command>' % basename(argv[0]) |
|
573 |
|
|
574 |
print 'Commands:' |
|
575 |
for name, cls in sorted(commands.items()): |
|
576 |
description = getattr(cls, 'description', '') |
|
577 |
print ' %s %s' % (name.ljust(12), description) |
|
578 |
|
|
567 | 579 |
def main(): |
568 | 580 |
try: |
569 | 581 |
name = argv[1] |
570 | 582 |
cls = commands[name] |
571 | 583 |
except (IndexError, KeyError): |
572 |
print 'Usage: %s <command>' % basename(argv[0]) |
|
573 |
|
|
574 |
print 'Commands:' |
|
575 |
for name, cls in sorted(commands.items()): |
|
576 |
description = getattr(cls, 'description', '') |
|
577 |
print ' %s %s' % (name.ljust(12), description) |
|
584 |
print_usage() |
|
578 | 585 |
exit(1) |
579 | 586 |
|
580 | 587 |
try: |
581 |
commands[name](argv[2:])
|
|
588 |
cls(argv[2:])
|
|
582 | 589 |
except TypeError: |
583 | 590 |
syntax = getattr(cls, 'syntax', '') |
584 | 591 |
if syntax: |
Also available in: Unified diff