Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.2 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)
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
        )
80

    
81
    def handle(self, *args, **options):
82
        if args:
83
            raise CommandError("Command doesn't accept any arguments")
84

    
85
        if options['backend_id']:
86
            backend = get_backend(options['backend_id'])
87
            servers = backend.virtual_machines
88
        else:
89
            servers = VirtualMachine.objects
90

    
91
        if options['deleted']:
92
            servers = servers.all()
93
        else:
94
            servers = servers.filter(deleted=False)
95

    
96
        if options['suspended']:
97
            servers = servers.filter(suspended=True)
98

    
99
        if options['build']:
100
            servers = servers.filter(operstate='BUILD')
101

    
102
        filter_by = options['filter_by']
103
        if filter_by:
104
            servers = filter_results(servers, filter_by)
105

    
106
        labels = ('id', 'name', 'owner', 'flavor', 'image', 'state',
107
                  'backend')
108
        columns = (3, 12, 20, 11, 12, 9, 40)
109

    
110
        if not options['csv']:
111
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
112
            self.stdout.write(line + '\n')
113
            sep = '-' * len(line)
114
            self.stdout.write(sep + '\n')
115

    
116
        cache = ImageCache()
117

    
118
        for server in servers.order_by('id'):
119
            id = str(server.id)
120
            try:
121
                name = server.name.decode('utf8')
122
            except UnicodeEncodeError:
123
                name = server.name
124
            flavor = server.flavor.name
125
            try:
126
                image = cache.get_image(server.imageid, server.userid)['name']
127
            except:
128
                image = server.imageid
129

    
130
            state = format_vm_state(server)
131
            fields = (id, name, server.userid, flavor, image, state,
132
                      str(server.backend))
133

    
134
            if options['csv']:
135
                line = '|'.join(fields)
136
            else:
137
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
138

    
139
            self.stdout.write(line.encode('utf8') + '\n')
140

    
141

    
142
class ImageCache(object):
143
    def __init__(self):
144
        self.images = {}
145

    
146
    def get_image(self, imageid, userid):
147
        if not imageid in self.images:
148
            self.images[imageid] = get_image(imageid, userid)
149
        return self.images[imageid]