Statistics
| Branch: | Tag: | Revision:

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

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
        if options['use_uuids'] is False:
113
            ucache = UUIDCache()
114

    
115
        headers = ('id', 'name', 'owner', 'flavor', 'image', 'state',
116
                   'backend')
117

    
118
        table = []
119
        for server in servers.order_by('id'):
120
            try:
121
                name = server.name.decode('utf8')
122
            except UnicodeEncodeError:
123
                name = server.name
124

    
125
            flavor = server.flavor.name
126

    
127
            try:
128
                image = cache.get_image(server.imageid, server.userid)['name']
129
            except:
130
                image = server.imageid
131

    
132
            state = format_vm_state(server)
133

    
134
            user = server.userid
135
            if options['use_uuids'] is False:
136
                user = ucache.get_user(server.userid)
137

    
138
            fields = (str(server.id), name, user, flavor, image,
139
                      state, str(server.backend))
140
            table.append(fields)
141

    
142
        separator = " | " if options['csv'] else None
143
        pprint_table(self.stdout, table, headers, separator)
144

    
145

    
146
class ImageCache(object):
147
    def __init__(self):
148
        self.images = {}
149

    
150
    def get_image(self, imageid, userid):
151
        if not imageid in self.images:
152
            self.images[imageid] = get_image(imageid, userid)
153
        return self.images[imageid]