root / snf-cyclades-app / synnefo / api / management / commands / server-list.py @ 3170076a
History | View | Annotate | Download (5.3 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from optparse import make_option |
35 |
|
36 |
from django.core.management.base import BaseCommand, CommandError |
37 |
from synnefo.management.common import (format_vm_state, get_backend, |
38 |
filter_results, pprint_table, UUIDCache) |
39 |
from synnefo.api.util import get_image |
40 |
from synnefo.db.models import VirtualMachine |
41 |
|
42 |
|
43 |
FIELDS = VirtualMachine._meta.get_all_field_names() |
44 |
|
45 |
|
46 |
class Command(BaseCommand): |
47 |
help = "List servers"
|
48 |
|
49 |
option_list = BaseCommand.option_list + ( |
50 |
make_option('-c',
|
51 |
action='store_true',
|
52 |
dest='csv',
|
53 |
default=False,
|
54 |
help="Use pipes to separate values"),
|
55 |
make_option('--suspended',
|
56 |
action='store_true',
|
57 |
dest='suspended',
|
58 |
default=False,
|
59 |
help="List only suspended servers"),
|
60 |
make_option('--build',
|
61 |
action='store_true',
|
62 |
dest='build',
|
63 |
default=False,
|
64 |
help="List only servers in the building state"),
|
65 |
make_option('--deleted',
|
66 |
action='store_true',
|
67 |
dest='deleted',
|
68 |
default=False,
|
69 |
help="Include deleted servers"),
|
70 |
make_option('--backend-id',
|
71 |
dest='backend_id',
|
72 |
help="List only servers of the specified backend"),
|
73 |
make_option('--filter-by',
|
74 |
dest='filter_by',
|
75 |
help="Filter results. Comma seperated list of key `cond` val pairs"
|
76 |
" that displayed entries must satisfy. e.g."
|
77 |
" --filter-by \"operstate=STARTED,id>=22\"."
|
78 |
" Available keys are: %s" % ", ".join(FIELDS)), |
79 |
make_option('--uuids',
|
80 |
action='store_true',
|
81 |
dest='use_uuids',
|
82 |
default=False,
|
83 |
help="Display UUIDs instead of user emails"),
|
84 |
) |
85 |
|
86 |
def handle(self, *args, **options): |
87 |
if args:
|
88 |
raise CommandError("Command doesn't accept any arguments") |
89 |
|
90 |
if options['backend_id']: |
91 |
backend = get_backend(options['backend_id'])
|
92 |
servers = backend.virtual_machines |
93 |
else:
|
94 |
servers = VirtualMachine.objects |
95 |
|
96 |
if options['deleted']: |
97 |
servers = servers.all() |
98 |
else:
|
99 |
servers = servers.filter(deleted=False)
|
100 |
|
101 |
if options['suspended']: |
102 |
servers = servers.filter(suspended=True)
|
103 |
|
104 |
if options['build']: |
105 |
servers = servers.filter(operstate='BUILD')
|
106 |
|
107 |
filter_by = options['filter_by']
|
108 |
if filter_by:
|
109 |
servers = filter_results(servers, filter_by) |
110 |
|
111 |
cache = ImageCache() |
112 |
ucache = UUIDCache() |
113 |
|
114 |
headers = ('id', 'name', 'owner', 'flavor', 'image', 'state', |
115 |
'backend')
|
116 |
|
117 |
table = [] |
118 |
for server in servers.order_by('id'): |
119 |
try:
|
120 |
name = server.name.decode('utf8')
|
121 |
except UnicodeEncodeError: |
122 |
name = server.name |
123 |
|
124 |
flavor = server.flavor.name |
125 |
|
126 |
try:
|
127 |
image = cache.get_image(server.imageid, server.userid)['name']
|
128 |
except:
|
129 |
image = server.imageid |
130 |
|
131 |
state = format_vm_state(server) |
132 |
|
133 |
user = server.userid |
134 |
if not options['use_uuids']: |
135 |
try:
|
136 |
user = ucache.get_user(server.userid) |
137 |
except:
|
138 |
pass
|
139 |
|
140 |
fields = (str(server.id), name, user, flavor, image,
|
141 |
state, str(server.backend))
|
142 |
table.append(fields) |
143 |
|
144 |
separator = " | " if options['csv'] else None |
145 |
pprint_table(self.stdout, table, headers, separator)
|
146 |
|
147 |
|
148 |
class ImageCache(object): |
149 |
def __init__(self): |
150 |
self.images = {}
|
151 |
|
152 |
def get_image(self, imageid, userid): |
153 |
if not imageid in self.images: |
154 |
self.images[imageid] = get_image(imageid, userid)
|
155 |
return self.images[imageid] |