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