root / snf-cyclades-app / synnefo / api / management / commands / server-inspect.py @ c346aed0
History | View | Annotate | Download (5.6 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 datetime import datetime |
35 |
from optparse import make_option |
36 |
from django.core.management.base import BaseCommand, CommandError |
37 |
|
38 |
from synnefo.lib.utils import merge_time |
39 |
from synnefo.logic.rapi import GanetiApiError |
40 |
from synnefo.management import common |
41 |
|
42 |
|
43 |
# Fields to print from a gnt-instance info
|
44 |
GANETI_INSTANCE_FIELDS = ('name', 'oper_state', 'admin_state', 'status', |
45 |
'pnode', 'snode', 'network_port', 'disk_template', |
46 |
'disk_usage', 'oper_ram', 'oper_vcpus', 'mtime', |
47 |
'nic.ips', 'nic.macs', 'nic.networks', 'nic.modes') |
48 |
|
49 |
# Fields to print from a gnt-job info
|
50 |
GANETI_JOB_FIELDS = ('id', 'status', 'summary', 'opresult', 'opstatus', |
51 |
'oplog', 'start_ts', 'end_ts') |
52 |
|
53 |
|
54 |
class Command(BaseCommand): |
55 |
help = "Inspect a server on DB and Ganeti"
|
56 |
args = "<server ID>"
|
57 |
|
58 |
option_list = BaseCommand.option_list + ( |
59 |
make_option('--jobs', action='store_true', |
60 |
dest='jobs', default=False, |
61 |
help="Show non-archived jobs concerning server."
|
62 |
), |
63 |
) |
64 |
|
65 |
def handle(self, *args, **options): |
66 |
if len(args) != 1: |
67 |
raise CommandError("Please provide a server ID") |
68 |
|
69 |
vm = common.get_vm(args[0])
|
70 |
|
71 |
try:
|
72 |
image = common.get_image(vm.imageid, vm.userid)['name']
|
73 |
except:
|
74 |
image = vm.imageid |
75 |
|
76 |
sep = '-' * 80 + '\n' |
77 |
labels = ('name', 'owner', 'flavor', 'image', 'state', 'backend', |
78 |
'deleted', 'action', 'backendjobid', 'backendopcode', |
79 |
'backendjobstatus', 'backend_time') |
80 |
fields = (vm.name, vm.userid, vm.flavor.name, image, |
81 |
common.format_vm_state(vm), str(vm.backend),
|
82 |
str(vm.deleted), str(vm.action), str(vm.backendjobid), |
83 |
str(vm.backendopcode), str(vm.backendjobstatus), |
84 |
str(vm.backendtime))
|
85 |
|
86 |
self.stdout.write(sep)
|
87 |
self.stdout.write('State of Server in DB\n') |
88 |
self.stdout.write(sep)
|
89 |
for l, f in zip(labels, fields): |
90 |
self.stdout.write(l.ljust(18) + ': ' + f.ljust(20) + '\n') |
91 |
self.stdout.write('\n') |
92 |
for nic in vm.nics.all(): |
93 |
self.stdout.write("nic/%d: IPv4: %s, MAC: %s, IPv6:%s, Network: %s\n"\ |
94 |
% (nic.index, nic.ipv4, nic.mac, nic.ipv6, nic.network)) |
95 |
|
96 |
client = vm.get_client() |
97 |
try:
|
98 |
g_vm = client.GetInstance(vm.backend_vm_id) |
99 |
self.stdout.write('\n') |
100 |
self.stdout.write(sep)
|
101 |
self.stdout.write('State of Server in Ganeti\n') |
102 |
self.stdout.write(sep)
|
103 |
for i in GANETI_INSTANCE_FIELDS: |
104 |
try:
|
105 |
value = g_vm[i] |
106 |
if i.find('time') != -1: |
107 |
value = datetime.fromtimestamp(value) |
108 |
self.stdout.write(i.ljust(14) + ': ' + str(value) + '\n') |
109 |
except KeyError: |
110 |
pass
|
111 |
except GanetiApiError as e: |
112 |
if e.code == 404: |
113 |
self.stdout.write('Server does not exist in backend %s\n' % |
114 |
vm.backend.clustername) |
115 |
else:
|
116 |
raise e
|
117 |
|
118 |
if not options['jobs']: |
119 |
return
|
120 |
|
121 |
self.stdout.write('\n') |
122 |
self.stdout.write(sep)
|
123 |
self.stdout.write('Non-archived jobs concerning Server in Ganeti\n') |
124 |
self.stdout.write(sep)
|
125 |
jobs = client.GetJobs() |
126 |
for j in jobs: |
127 |
info = client.GetJobStatus(j) |
128 |
summary = ' '.join(info['summary']) |
129 |
if summary.startswith("INSTANCE") and \ |
130 |
summary.find(vm.backend_vm_id) != -1:
|
131 |
for i in GANETI_JOB_FIELDS: |
132 |
value = info[i] |
133 |
if i.find('_ts') != -1: |
134 |
value = merge_time(value) |
135 |
try:
|
136 |
self.stdout.write(i.ljust(14) + ': ' + str(value) +\ |
137 |
'\n')
|
138 |
except KeyError: |
139 |
pass
|
140 |
self.stdout.write('\n' + sep) |
141 |
# Return the RAPI client to pool
|
142 |
vm.put_client(client) |