Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / listservers.py @ af6a3bc5

History | View | Annotate | Download (4.1 kB)

1 11300d7b Giorgos Verigakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 11300d7b Giorgos Verigakis
#
3 11300d7b Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 11300d7b Giorgos Verigakis
# without modification, are permitted provided that the following
5 11300d7b Giorgos Verigakis
# conditions are met:
6 11300d7b Giorgos Verigakis
#
7 11300d7b Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 11300d7b Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 11300d7b Giorgos Verigakis
#      disclaimer.
10 11300d7b Giorgos Verigakis
#
11 11300d7b Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 11300d7b Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 11300d7b Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 11300d7b Giorgos Verigakis
#      provided with the distribution.
15 11300d7b Giorgos Verigakis
#
16 11300d7b Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 11300d7b Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 11300d7b Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 11300d7b Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 11300d7b Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 11300d7b Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 11300d7b Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 11300d7b Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 11300d7b Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 11300d7b Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 11300d7b Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 11300d7b Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 11300d7b Giorgos Verigakis
#
29 11300d7b Giorgos Verigakis
# The views and conclusions contained in the software and
30 11300d7b Giorgos Verigakis
# documentation are those of the authors and should not be
31 11300d7b Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 11300d7b Giorgos Verigakis
# or implied, of GRNET S.A.
33 11300d7b Giorgos Verigakis
34 11300d7b Giorgos Verigakis
from optparse import make_option
35 11300d7b Giorgos Verigakis
36 11300d7b Giorgos Verigakis
from django.core.management.base import BaseCommand, CommandError
37 11300d7b Giorgos Verigakis
38 11300d7b Giorgos Verigakis
from synnefo.api.util import get_image
39 1a894bfe Christos Stavrakakis
from synnefo.db.models import VirtualMachine, Backend
40 11300d7b Giorgos Verigakis
41 11300d7b Giorgos Verigakis
42 11300d7b Giorgos Verigakis
class Command(BaseCommand):
43 11300d7b Giorgos Verigakis
    help = "List servers"
44 11300d7b Giorgos Verigakis
    
45 11300d7b Giorgos Verigakis
    option_list = BaseCommand.option_list + (
46 11300d7b Giorgos Verigakis
        make_option('-c',
47 11300d7b Giorgos Verigakis
            action='store_true',
48 11300d7b Giorgos Verigakis
            dest='csv',
49 11300d7b Giorgos Verigakis
            default=False,
50 11300d7b Giorgos Verigakis
            help="Use pipes to separate values"),
51 11300d7b Giorgos Verigakis
        make_option('--build',
52 11300d7b Giorgos Verigakis
            action='store_true',
53 11300d7b Giorgos Verigakis
            dest='build',
54 11300d7b Giorgos Verigakis
            default=False,
55 11300d7b Giorgos Verigakis
            help="List only servers in the building state"),
56 1a894bfe Christos Stavrakakis
        make_option('--non-deleted', action='store_true', dest='non_deleted',
57 1a894bfe Christos Stavrakakis
                    default=False,
58 1a894bfe Christos Stavrakakis
                    help="List only non-deleted servers"),
59 1a894bfe Christos Stavrakakis
        make_option('--backend_id', dest='backend_id',
60 1a894bfe Christos Stavrakakis
                    help="List only servers of the specified backend")
61 11300d7b Giorgos Verigakis
        )
62 11300d7b Giorgos Verigakis
    
63 11300d7b Giorgos Verigakis
    def handle(self, *args, **options):
64 11300d7b Giorgos Verigakis
        if args:
65 11300d7b Giorgos Verigakis
            raise CommandError("Command doesn't accept any arguments")
66 1a894bfe Christos Stavrakakis
67 1a894bfe Christos Stavrakakis
        if options['backend_id']:
68 1a894bfe Christos Stavrakakis
            servers = \
69 1a894bfe Christos Stavrakakis
            Backend.objects.get(id=options['backend_id']).virtual_machines
70 1a894bfe Christos Stavrakakis
        else:
71 1a894bfe Christos Stavrakakis
            servers = VirtualMachine.objects
72 1a894bfe Christos Stavrakakis
73 1a894bfe Christos Stavrakakis
        if options['non_deleted']:
74 1a894bfe Christos Stavrakakis
            servers = servers.filter(deleted=False)
75 1a894bfe Christos Stavrakakis
        else:
76 1a894bfe Christos Stavrakakis
            servers = servers.all()
77 1a894bfe Christos Stavrakakis
78 11300d7b Giorgos Verigakis
        if options['build']:
79 11300d7b Giorgos Verigakis
            servers = servers.filter(operstate='BUILD')
80 11300d7b Giorgos Verigakis
        
81 1a894bfe Christos Stavrakakis
        labels = ('id', 'name', 'owner', 'flavor', 'image', 'state',
82 1a894bfe Christos Stavrakakis
                  'backend')
83 1a894bfe Christos Stavrakakis
        columns = (3, 12, 20, 11, 12, 9, 40)
84 11300d7b Giorgos Verigakis
        
85 11300d7b Giorgos Verigakis
        if not options['csv']:
86 11300d7b Giorgos Verigakis
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
87 11300d7b Giorgos Verigakis
            self.stdout.write(line + '\n')
88 11300d7b Giorgos Verigakis
            sep = '-' * len(line)
89 11300d7b Giorgos Verigakis
            self.stdout.write(sep + '\n')
90 11300d7b Giorgos Verigakis
        
91 11300d7b Giorgos Verigakis
        for server in servers:
92 11300d7b Giorgos Verigakis
            id = str(server.id)
93 2caa409e Giorgos Verigakis
            try:
94 2caa409e Giorgos Verigakis
                name = server.name.decode('utf8')
95 2caa409e Giorgos Verigakis
            except UnicodeEncodeError:
96 2caa409e Giorgos Verigakis
                name = server.name
97 11300d7b Giorgos Verigakis
            flavor = server.flavor.name
98 f1e8a6e2 Giorgos Verigakis
            try:
99 f1e8a6e2 Giorgos Verigakis
                image = get_image(server.imageid, server.userid)['name']
100 f1e8a6e2 Giorgos Verigakis
            except:
101 f1e8a6e2 Giorgos Verigakis
                image = server.imageid
102 1a894bfe Christos Stavrakakis
            fields = (id, name, server.userid, flavor, image, server.operstate,
103 1a894bfe Christos Stavrakakis
                      str(server.backend))
104 11300d7b Giorgos Verigakis
            
105 11300d7b Giorgos Verigakis
            if options['csv']:
106 11300d7b Giorgos Verigakis
                line = '|'.join(fields)
107 11300d7b Giorgos Verigakis
            else:
108 11300d7b Giorgos Verigakis
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
109 11300d7b Giorgos Verigakis
            
110 11300d7b Giorgos Verigakis
            self.stdout.write(line.encode('utf8') + '\n')