Revision fc1fb924
b/snf-cyclades-app/synnefo/api/management/commands/server-list.py | ||
---|---|---|
41 | 41 |
|
42 | 42 |
class Command(BaseCommand): |
43 | 43 |
help = "List servers" |
44 |
|
|
44 |
|
|
45 | 45 |
option_list = BaseCommand.option_list + ( |
46 | 46 |
make_option('-c', |
47 | 47 |
action='store_true', |
... | ... | |
59 | 59 |
make_option('--backend_id', dest='backend_id', |
60 | 60 |
help="List only servers of the specified backend") |
61 | 61 |
) |
62 |
|
|
62 |
|
|
63 | 63 |
def handle(self, *args, **options): |
64 | 64 |
if args: |
65 | 65 |
raise CommandError("Command doesn't accept any arguments") |
... | ... | |
77 | 77 |
|
78 | 78 |
if options['build']: |
79 | 79 |
servers = servers.filter(operstate='BUILD') |
80 |
|
|
80 |
|
|
81 | 81 |
labels = ('id', 'name', 'owner', 'flavor', 'image', 'state', |
82 | 82 |
'backend') |
83 | 83 |
columns = (3, 12, 20, 11, 12, 9, 40) |
84 |
|
|
84 |
|
|
85 | 85 |
if not options['csv']: |
86 | 86 |
line = ' '.join(l.rjust(w) for l, w in zip(labels, columns)) |
87 | 87 |
self.stdout.write(line + '\n') |
88 | 88 |
sep = '-' * len(line) |
89 | 89 |
self.stdout.write(sep + '\n') |
90 |
|
|
90 |
|
|
91 |
cache = ImageCache() |
|
92 |
|
|
91 | 93 |
for server in servers: |
92 | 94 |
id = str(server.id) |
93 | 95 |
try: |
... | ... | |
96 | 98 |
name = server.name |
97 | 99 |
flavor = server.flavor.name |
98 | 100 |
try: |
99 |
image = get_image(server.imageid, server.userid)['name'] |
|
101 |
image = cache.get_image(server.imageid, server.userid)['name']
|
|
100 | 102 |
except: |
101 | 103 |
image = server.imageid |
102 | 104 |
fields = (id, name, server.userid, flavor, image, server.operstate, |
103 | 105 |
str(server.backend)) |
104 |
|
|
106 |
|
|
105 | 107 |
if options['csv']: |
106 | 108 |
line = '|'.join(fields) |
107 | 109 |
else: |
108 | 110 |
line = ' '.join(f.rjust(w) for f, w in zip(fields, columns)) |
109 |
|
|
111 |
|
|
110 | 112 |
self.stdout.write(line.encode('utf8') + '\n') |
113 |
|
|
114 |
|
|
115 |
class ImageCache(object): |
|
116 |
def __init__(self): |
|
117 |
self.images = {} |
|
118 |
|
|
119 |
def get_image(self, imageid, userid): |
|
120 |
if not imageid in self.images: |
|
121 |
self.images[imageid] = get_image(imageid, userid) |
|
122 |
return self.images[imageid] |
Also available in: Unified diff