Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / management / commands / server-inspect.py @ 56a1134d

History | View | Annotate | Download (5.8 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

    
35
from datetime import datetime
36
from optparse import make_option
37
from django.core.management.base import BaseCommand, CommandError
38

    
39
from synnefo.api.util import get_image
40
from synnefo.lib.utils import merge_time
41
from synnefo.db.models import VirtualMachine
42
from synnefo.util.rapi import GanetiApiError
43

    
44

    
45
# Fields to print from a gnt-instance info
46
GANETI_INSTANCE_FIELDS = ('name', 'oper_state', 'admin_state', 'status',
47
                          'pnode', 'snode', 'network_port', 'disk_template',
48
                          'disk_usage', 'oper_ram', 'oper_vcpus', 'mtime',
49
                          'nic.ips', 'nic.macs', 'nic.networks', 'nic.modes')
50

    
51
# Fields to print from a gnt-job info
52
GANETI_JOB_FIELDS = ('id', 'status', 'summary', 'opresult', 'opstatus',
53
                     'oplog', 'start_ts', 'end_ts')
54

    
55

    
56
class Command(BaseCommand):
57
    help = "Inspect a server on DB and Ganeti"
58

    
59
    option_list = BaseCommand.option_list + (
60
        make_option('--jobs', action='store_true',
61
                    dest='jobs', default='False',
62
                    help="Show non-archived jobs concerning server."
63
            ),
64
    )
65

    
66
    def handle(self, *args, **options):
67
        if len(args) != 1:
68
            raise CommandError("Please provide a server ID")
69

    
70
        try:
71
            vm_id = int(args[0])
72
        except ValueError:
73
            raise CommandError("Invalid server ID")
74

    
75
        try:
76
            vm = VirtualMachine.objects.get(id=vm_id)
77
        except VirtualMachine.DoesNotExist:
78
            raise CommandError("Server not found in DB")
79

    
80
        try:
81
            image = get_image(vm.imageid, vm.userid)['name']
82
        except:
83
            image = vm.imageid
84

    
85
        sep = '-' * 80 + '\n'
86
        labels = ('name', 'owner', 'flavor', 'image', 'state', 'backend',
87
                  'deleted', 'action', 'backendjobid', 'backendopcodoe',
88
                  'backendjobstatus', 'backend_time')
89
        fields = (vm.name, vm.userid, vm.flavor.name, image, vm.operstate,
90
                  str(vm.backend), str(vm.deleted), str(vm.action),
91
                  str(vm.backendjobid), str(vm.backendopcode),
92
                  str(vm.backendjobstatus), str(vm.backendtime))
93

    
94
        self.stdout.write(sep)
95
        self.stdout.write('State of Server in DB\n')
96
        self.stdout.write(sep)
97
        for l, f in zip(labels, fields):
98
            self.stdout.write(l.ljust(18) + ': ' + f.ljust(20) + '\n')
99
        self.stdout.write('\n')
100
        for nic in vm.nics.all():
101
            self.stdout.write("nic/%d: IPv4: %s, MAC: %s, IPv6:%s,  Network: %s\n"\
102
                              % (nic.index, nic.ipv4, nic.mac, nic.ipv6,  nic.network))
103

    
104
        client = vm.backend.client
105
        try:
106
            g_vm = client.GetInstance(vm.backend_vm_id)
107
        except GanetiApiError as e:
108
            if e.code == 404:
109
                self.stdout.write('Server does not exist in backend %s\n' %
110
                                  vm.backend.clustername)
111
                return
112
            else:
113
                raise e
114

    
115
        self.stdout.write('\n')
116
        self.stdout.write(sep)
117
        self.stdout.write('State of Server in Ganeti\n')
118
        self.stdout.write(sep)
119
        for i in GANETI_INSTANCE_FIELDS:
120
            try:
121
                value = g_vm[i]
122
                if i.find('time') != -1:
123
                    value = datetime.fromtimestamp(value)
124
                self.stdout.write(i.ljust(14) + ': ' + str(value) + '\n')
125
            except KeyError:
126
                pass
127

    
128
        if not options['jobs']:
129
            return
130

    
131
        self.stdout.write('\n')
132
        self.stdout.write(sep)
133
        self.stdout.write('Non-archived jobs concering Server in Ganeti\n')
134
        self.stdout.write(sep)
135
        jobs = client.GetJobs()
136
        for j in jobs:
137
            info = client.GetJobStatus(j)
138
            summary = ' '.join(info['summary'])
139
            if summary.startswith("INSTANCE") and \
140
               summary.find(vm.backend_vm_id) != -1:
141
                for i in GANETI_JOB_FIELDS:
142
                    value = info[i]
143
                    if i.find('_ts') != -1:
144
                        value = merge_time(value)
145
                    try:
146
                        self.stdout.write(i.ljust(14) + ': ' + str(value) +\
147
                                          '\n')
148
                    except KeyError:
149
                        pass
150
                self.stdout.write('\n' + sep)