Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / server-show.py @ 291c782f

History | View | Annotate | Download (3.2 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 28578e52 Christos Stavrakakis
from django.core.management.base import CommandError
35 28578e52 Christos Stavrakakis
from synnefo.webproject.management.commands import SynnefoCommand
36 225cea18 Christos Stavrakakis
from synnefo.management.common import (format_vm_state, get_vm,
37 76a13815 Christos Stavrakakis
                                       get_image)
38 04a1b675 Christos Stavrakakis
from snf_django.lib.astakos import UserCache
39 18c4414d Giorgos Korfiatis
from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN,
40 e3ff6830 Georgios D. Tsoukalas
                              ASTAKOS_BASE_URL)
41 28578e52 Christos Stavrakakis
from synnefo.webproject.management import utils
42 11300d7b Giorgos Verigakis
43 11300d7b Giorgos Verigakis
44 28578e52 Christos Stavrakakis
class Command(SynnefoCommand):
45 11300d7b Giorgos Verigakis
    args = "<server ID>"
46 11300d7b Giorgos Verigakis
    help = "Show server info"
47 77fccdd4 Christos Stavrakakis
48 11300d7b Giorgos Verigakis
    def handle(self, *args, **options):
49 11300d7b Giorgos Verigakis
        if len(args) != 1:
50 11300d7b Giorgos Verigakis
            raise CommandError("Please provide a server ID")
51 77fccdd4 Christos Stavrakakis
52 b84ed662 Christos Stavrakakis
        server = get_vm(args[0])
53 77fccdd4 Christos Stavrakakis
54 11300d7b Giorgos Verigakis
        flavor = '%s (%s)' % (server.flavor.id, server.flavor.name)
55 77fccdd4 Christos Stavrakakis
        userid = server.userid
56 77fccdd4 Christos Stavrakakis
57 77fccdd4 Christos Stavrakakis
        imageid = server.imageid
58 77fccdd4 Christos Stavrakakis
        try:
59 77fccdd4 Christos Stavrakakis
            image_name = get_image(imageid, userid).get('name')
60 77fccdd4 Christos Stavrakakis
        except:
61 77fccdd4 Christos Stavrakakis
            image_name = "None"
62 77fccdd4 Christos Stavrakakis
        image = '%s (%s)' % (imageid, image_name)
63 77fccdd4 Christos Stavrakakis
64 e3ff6830 Georgios D. Tsoukalas
        usercache = UserCache(ASTAKOS_BASE_URL, ASTAKOS_TOKEN)
65 11300d7b Giorgos Verigakis
        kv = {
66 28578e52 Christos Stavrakakis
          'id': server.id,
67 28578e52 Christos Stavrakakis
          'name': server.name,
68 28578e52 Christos Stavrakakis
          'owner_uuid': userid,
69 e3ff6830 Georgios D. Tsoukalas
          'owner_name': usercache.get_name(userid),
70 28578e52 Christos Stavrakakis
          'created': utils.format_date(server.created),
71 28578e52 Christos Stavrakakis
          'updated': utils.format_date(server.updated),
72 28578e52 Christos Stavrakakis
          'image': image,
73 28578e52 Christos Stavrakakis
          'host id': server.hostid,
74 28578e52 Christos Stavrakakis
          'flavor': flavor,
75 28578e52 Christos Stavrakakis
          'deleted': utils.format_bool(server.deleted),
76 28578e52 Christos Stavrakakis
          'suspended': utils.format_bool(server.suspended),
77 28578e52 Christos Stavrakakis
          'state': format_vm_state(server),
78 11300d7b Giorgos Verigakis
        }
79 77fccdd4 Christos Stavrakakis
80 28578e52 Christos Stavrakakis
        utils.pprint_table(self.stdout, [kv.values()], kv.keys(),
81 28578e52 Christos Stavrakakis
                           options["output_format"], vertical=True)