Skip to content

Commit 00fe32d

Browse files
committed
[bugfix] replace neutron command with python SDK to fix encoding issue
redmine 7707 Signed-off-by: blkart <[email protected]>
1 parent 0d35a7f commit 00fe32d

File tree

2 files changed

+35
-50
lines changed

2 files changed

+35
-50
lines changed

eayunstack_tools/doctor/net.py

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from eayunstack_tools.sys_utils import ssh_connect, ssh_connect2
55
from eayunstack_tools.utils import get_controllers_hostname
66
from eayunstack_tools.doctor.utils import run_doctor_cmd_on_node
7+
from eayunstack_tools.pythonclient import PythonClient
78
import logging
89
import commands
910
import json
@@ -22,39 +23,6 @@ def run_command(cmd):
2223
reval = out
2324
return reval
2425

25-
26-
# FIXME: some neutronclient does not support json output, hack it
27-
def csv2dict(csv):
28-
"""Convert result format from csv to dict:
29-
csv format:
30-
"id","name","mac_address"
31-
"596afd3e-b60a-41f5-97c3-39495979e6d8","","fa:16:3e:3a:ee:97"
32-
"70cb55cd-d5cb-4c12-8ad2-8edf18c2fa94","","fa:16:3e:f7:e9:8c"
33-
34-
dict format:
35-
[{"id": "596afd3e", "name": "", "mac_address": "fa:16:3e:3a:ee:97"},
36-
{"id": "70cb55cd", "name": "", "mac_address": "fa:16:3e:f7:e9:8c"}]
37-
"""
38-
field = csv.split('\n')[0]
39-
p = re.compile(r'"(.*)"')
40-
column = []
41-
for i in field.split(','):
42-
column.append(p.match(i).groups()[0])
43-
routers = []
44-
out = csv.split('\n')[1:]
45-
for r in out:
46-
router = {}
47-
r = r.split(',')
48-
index = 0
49-
for index in range(len(column)):
50-
try:
51-
router[column[index]] = p.match(r[index]).groups()[0]
52-
except AttributeError:
53-
router[column[index]] = r[index]
54-
routers.append(router)
55-
return routers
56-
57-
5826
def vrouter_get_gw_remote(l3_host, rid):
5927
cmd = "ip netns exec qrouter-%s ip route show | "\
6028
"grep 'default' | awk '{print $3}'" % (rid)
@@ -164,11 +132,10 @@ def port_log(device_owner, s):
164132
LOG.debug('this port is normal port, do not need to check gateway')
165133

166134
def vrouter_check_one(rid):
167-
cmd = 'neutron router-port-list %s -f csv -F id -F name' % (rid)
168-
out = run_command(cmd)
169-
ports = []
170-
if out:
171-
ports = csv2dict(out)
135+
logging.disable(logging.INFO)
136+
pc = PythonClient()
137+
ports = pc.neutron_router_port_list(rid)
138+
logging.disable(logging.NOTSET)
172139

173140
l3_host = vrouter_get_l3_host(rid)
174141
if l3_host:
@@ -180,11 +147,12 @@ def vrouter_check_one(rid):
180147

181148

182149
def vrouter_get_l3_host(rid):
183-
cmd = "neutron l3-agent-list-hosting-router -f csv %s" \
184-
% (rid)
185-
out = run_command(cmd).strip('\r\n')
186-
if out:
187-
hosts = csv2dict(out)
150+
logging.disable(logging.INFO)
151+
pc = PythonClient()
152+
hosts = pc.neutron_l3_agent_list_hosting_router(rid)
153+
logging.disable(logging.NOTSET)
154+
155+
if hosts:
188156
return hosts[0]['host']
189157
else:
190158
LOG.error('can not get l3 host for router %s' % (rid))
@@ -198,13 +166,11 @@ def _vrouter_check(parser):
198166
vrouter_check_one(parser.rid)
199167
else:
200168
# 1) Get valid routers list
201-
cmd = 'neutron router-list -f csv -F id -F name'
202-
if parser.tid:
203-
cmd += ' --tenant-id %s' % (parser.tid)
204-
out = run_command(cmd)
205-
routers = []
206-
if out:
207-
routers = csv2dict(out)
169+
tenant_id = parser.tid
170+
logging.disable(logging.INFO)
171+
pc = PythonClient()
172+
routers = pc.neutron_router_list(tenant_id)
173+
logging.disable(logging.NOTSET)
208174

209175
# 2) Check every router one by one: .e.g. status, ip address ..., this
210176
# is done on neutron node which namespace belong to.

eayunstack_tools/pythonclient.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ def nova_volume(self, server_id, volume_id):
6262
def neutron_delete_port(self, port_id):
6363
self.neutronclient.delete_port(port_id)
6464

65+
def neutron_router_list(self, tenant_id=None):
66+
if tenant_id:
67+
router_list = \
68+
self.neutronclient.list_routers(tenant_id=tenant_id)['routers']
69+
else:
70+
router_list = self.neutronclient.list_routers()['routers']
71+
return router_list
72+
73+
def neutron_router_port_list(self, router_id):
74+
router_port_list = \
75+
self.neutronclient.list_ports(device_id=router_id)['ports']
76+
return router_port_list
77+
78+
def neutron_l3_agent_list_hosting_router(self, router_id):
79+
l3_agent_list_hosting_router = \
80+
self.neutronclient.list_l3_agent_hosting_routers(
81+
router_id)['agents']
82+
return l3_agent_list_hosting_router
83+
6584
def nova_flavor(self, flavor_id):
6685
flavor = self.novaclient.flavors.get(flavor_id)
6786
return flavor

0 commit comments

Comments
 (0)