Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / server-list.py @ 7856a37a

History | View | Annotate | Download (6.5 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, Omit,
38
                                       filter_results, pprint_table, UserCache)
39
from synnefo.api.util import get_image
40
from synnefo.db.models import VirtualMachine
41

    
42
import logging
43
log = logging.getLogger(__name__)
44

    
45
FIELDS = VirtualMachine._meta.get_all_field_names()
46

    
47

    
48
class Command(BaseCommand):
49
    help = "List servers"
50

    
51
    option_list = BaseCommand.option_list + (
52
        make_option('-c',
53
            action='store_true',
54
            dest='csv',
55
            default=False,
56
            help="Use pipes to separate values"),
57
        make_option('--suspended',
58
            action='store_true',
59
            dest='suspended',
60
            default=False,
61
            help="List only suspended servers"),
62
        make_option('--build',
63
            action='store_true',
64
            dest='build',
65
            default=False,
66
            help="List only servers in the building state"),
67
        make_option('--deleted',
68
            action='store_true',
69
            dest='deleted',
70
            default=False,
71
            help="Include deleted servers"),
72
        make_option('--backend-id',
73
            dest='backend_id',
74
            help="List only servers of the specified backend"),
75
        make_option('--user',
76
            dest='user',
77
            help="List only servers of the specified user (uuid or email)"),
78
        make_option('--filter-by',
79
            dest='filter_by',
80
            help="Filter results. Comma seperated list of key `cond` val pairs"
81
                 " that displayed entries must satisfy. e.g."
82
                 " --filter-by \"operstate=STARTED,id>=22\"."
83
                 " Available keys are: %s" % ", ".join(FIELDS)),
84
        make_option('--displayname',
85
            action='store_true',
86
            dest='displayname',
87
            default=False,
88
            help="Display both uuid and display name"),
89
    )
90

    
91
    def handle(self, *args, **options):
92
        if args:
93
            raise CommandError("Command doesn't accept any arguments")
94

    
95
        ucache = UserCache()
96

    
97
        if options['backend_id']:
98
            backend = get_backend(options['backend_id'])
99
            servers = backend.virtual_machines
100
        else:
101
            servers = VirtualMachine.objects
102

    
103
        if options['deleted']:
104
            servers = servers.all()
105
        else:
106
            servers = servers.filter(deleted=False)
107

    
108
        if options['suspended']:
109
            servers = servers.filter(suspended=True)
110

    
111
        if options['build']:
112
            servers = servers.filter(operstate='BUILD')
113

    
114
        user = options['user']
115
        if user:
116
            if '@' in user:
117
                user = ucache.get_uuid(user)
118
            servers = servers.filter(userid=user)
119

    
120
        filter_by = options['filter_by']
121
        if filter_by:
122
            servers = filter_results(servers, filter_by)
123

    
124
        displayname = options['displayname']
125

    
126
        cache = ImageCache()
127

    
128
        headers = filter(lambda x: x is not Omit,
129
                         ['id',
130
                          'name',
131
                          'owner_uuid',
132
                          'owner_name' if displayname else Omit,
133
                          'flavor',
134
                          'image',
135
                          'state',
136
                          'backend',
137
                          ])
138

    
139
        uuids = list(set([server.userid for server in servers]))
140
        ucache.fetch_names(uuids)
141

    
142
        table = []
143
        for server in servers.order_by('id'):
144
            try:
145
                name = server.name.decode('utf8')
146
            except UnicodeEncodeError:
147
                name = server.name
148

    
149
            flavor = server.flavor.name
150

    
151
            image = cache.get_image(server.imageid, server.userid)
152

    
153
            state = format_vm_state(server)
154

    
155
            uuid = server.userid
156
            if displayname:
157
                dname = ucache.get_name(server.userid)
158

    
159
            fields = filter(lambda x: x is not Omit,
160
                            [str(server.id),
161
                             name,
162
                             uuid,
163
                             dname if displayname else Omit,
164
                             flavor,
165
                             image,
166
                             state,
167
                             str(server.backend),
168
                             ])
169
            table.append(fields)
170

    
171
        separator = " | " if options['csv'] else None
172
        pprint_table(self.stdout, table, headers, separator)
173

    
174

    
175
class ImageCache(object):
176
    def __init__(self):
177
        self.images = {}
178

    
179
    def get_image(self, imageid, userid):
180
        if not imageid in self.images:
181
            try:
182
                self.images[imageid] = get_image(imageid, userid)['name']
183
            except Exception as e:
184
                log.warning("Error getting image name from imageid %s",
185
                            imageid, e)
186
                self.images[imageid] = imageid
187

    
188
        return self.images[imageid]