Revision 5fa91dc4
b/snf-cyclades-app/synnefo/api/management/commands/port-inspect.py | ||
---|---|---|
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, convert_api_faults |
|
42 |
from synnefo.management import common |
|
43 |
from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN, |
|
44 |
ASTAKOS_BASE_URL) |
|
45 |
|
|
46 |
from synnefo.api.util import get_port |
|
47 |
class Command(BaseCommand): |
|
48 |
help = "Inspect a port on DB and Ganeti" |
|
49 |
args = "<port ID>" |
|
50 |
|
|
51 |
option_list = BaseCommand.option_list + ( |
|
52 |
make_option( |
|
53 |
'--jobs', |
|
54 |
action='store_true', |
|
55 |
dest='jobs', |
|
56 |
default=False, |
|
57 |
help="Show non-archived jobs concerning port."), |
|
58 |
make_option( |
|
59 |
'--displayname', |
|
60 |
action='store_true', |
|
61 |
dest='displayname', |
|
62 |
default=False, |
|
63 |
help="Display both uuid and display name"), |
|
64 |
) |
|
65 |
|
|
66 |
@convert_api_faults |
|
67 |
def handle(self, *args, **options): |
|
68 |
if len(args) != 1: |
|
69 |
raise CommandError("Please provide a port ID") |
|
70 |
|
|
71 |
port = get_port(args[0], None) |
|
72 |
|
|
73 |
sep = '-' * 80 + '\n' |
|
74 |
labels = ['name', 'id', 'device_id', 'network_id', |
|
75 |
'device_owner', 'mac_address', 'ipv4', 'subnet4', |
|
76 |
'ipv6', 'subnet6', 'state', |
|
77 |
'security_groups', 'user_id'] |
|
78 |
|
|
79 |
uuid = port.userid |
|
80 |
security_groups = port.security_groups.values_list('id', |
|
81 |
flat=True) |
|
82 |
sg_csv = ','.join(map(str, security_groups)) |
|
83 |
|
|
84 |
ipv4 = '' |
|
85 |
ipv6 = '' |
|
86 |
subnet4 = '' |
|
87 |
subnet6 = '' |
|
88 |
for ip in port.ips.all(): |
|
89 |
if ip.subnet.ipversion == 4: |
|
90 |
ipv4 = ip.address |
|
91 |
subnet4 = str(ip.subnet.id) |
|
92 |
else: |
|
93 |
ipv6 = ip.address |
|
94 |
subnet6 = str(ip.subnet.id) |
|
95 |
|
|
96 |
fields = [port.name, str(port.id), str(port.machine.id), |
|
97 |
str(port.network.id), port.device_owner, port.mac, |
|
98 |
ipv4, subnet4, ipv6, subnet6, port.state, sg_csv, uuid] |
|
99 |
|
|
100 |
self.stdout.write(sep) |
|
101 |
self.stdout.write('State of port in DB\n') |
|
102 |
self.stdout.write(sep) |
|
103 |
for l, f in zip(labels, fields): |
|
104 |
if f: |
|
105 |
self.stdout.write(l.ljust(18) + ': ' + f.ljust(20) + '\n') |
|
106 |
else: |
|
107 |
self.stdout.write(l.ljust(18) + ': ' + '\n') |
|
108 |
|
|
109 |
self.stdout.write('\n') |
|
110 |
''' |
|
111 |
client = vm.get_client() |
|
112 |
try: |
|
113 |
g_vm = client.GetInstance(vm.backend_vm_id) |
|
114 |
self.stdout.write('\n') |
|
115 |
self.stdout.write(sep) |
|
116 |
self.stdout.write('State of Server in Ganeti\n') |
|
117 |
self.stdout.write(sep) |
|
118 |
for i in GANETI_INSTANCE_FIELDS: |
|
119 |
try: |
|
120 |
value = g_vm[i] |
|
121 |
if i.find('time') != -1: |
|
122 |
value = datetime.fromtimestamp(value) |
|
123 |
self.stdout.write(i.ljust(14) + ': ' + str(value) + '\n') |
|
124 |
except KeyError: |
|
125 |
pass |
|
126 |
except GanetiApiError as e: |
|
127 |
if e.code == 404: |
|
128 |
self.stdout.write('Server does not exist in backend %s\n' % |
|
129 |
vm.backend.clustername) |
|
130 |
else: |
|
131 |
raise e |
|
132 |
|
|
133 |
if not options['jobs']: |
|
134 |
return |
|
135 |
|
|
136 |
self.stdout.write('\n') |
|
137 |
self.stdout.write(sep) |
|
138 |
self.stdout.write('Non-archived jobs concerning Server in Ganeti\n') |
|
139 |
self.stdout.write(sep) |
|
140 |
jobs = client.GetJobs() |
|
141 |
for j in jobs: |
|
142 |
info = client.GetJobStatus(j) |
|
143 |
summary = ' '.join(info['summary']) |
|
144 |
job_is_relevant = summary.startswith("INSTANCE") and\ |
|
145 |
(summary.find(vm.backend_vm_id) != -1) |
|
146 |
if job_is_relevant: |
|
147 |
for i in GANETI_JOB_FIELDS: |
|
148 |
value = info[i] |
|
149 |
if i.find('_ts') != -1: |
|
150 |
value = merge_time(value) |
|
151 |
try: |
|
152 |
self.stdout.write(i.ljust(14) + ': ' + str(value) + |
|
153 |
'\n') |
|
154 |
except KeyError: |
|
155 |
pass |
|
156 |
self.stdout.write('\n' + sep) |
|
157 |
# Return the RAPI client to pool |
|
158 |
vm.put_client(client) |
|
159 |
''' |
Also available in: Unified diff