Skip to content

Commit 309f5b6

Browse files
Kunal NaikKunal Naik
authored andcommitted
added vmcp command
1 parent dfad2d4 commit 309f5b6

4 files changed

Lines changed: 278 additions & 53 deletions

File tree

smtLayer/getHost.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from zvmsdk import config
2828
from datetime import datetime
29+
from smtLayer.vmcpHandler import VMCPHandler
2930

3031
modId = 'GHO'
3132
version = "1.0.0"
@@ -425,34 +426,43 @@ def getCPUCount(rh):
425426

426427
rh.printSysLog("Enter getHost.lparCPUCount")
427428
rh.results['overallRC'] = 0
429+
handler = VMCPHandler(rh)
428430

429431
# LPAR CPUs total and used is not support mixed CP + IFL
430432
# So get cpu num from System_Processor_Query
431433
# to override LPAR CPUs total and used
432-
parms = []
433-
results = invokeSMCLI(rh, "System_Processor_Query", parms)
434-
cpu_total = 0
435-
cpu_use = 0
436-
if results['overallRC'] == 0:
437-
flag = 0
438-
for line in results['response'].splitlines():
439-
line_value = line.partition(' ')[2]
440-
if not line_value.strip():
441-
continue
442-
else:
443-
type_row = line_value.split(' ')
444-
if len(type_row) > 1:
445-
type_row = line_value.split(' ')[1]
446-
if type_row == 'TYPE':
447-
flag = 1
448-
if flag == 1:
449-
status_row = line_value.split(' ')[0]
450-
if (status_row.find('MASTER') != -1 or
451-
status_row == 'ALTERNATE' or
452-
status_row == 'PARKED'):
453-
cpu_use = cpu_use + 1
454-
if (type_row == 'CP' or type_row == 'IFL'):
455-
cpu_total = cpu_total + 1
434+
if config.CONF.zvm.prefer_vmcp_query == 'yes':
435+
cpu_total, cpu_use = handler._query_system_processor(rh)
436+
else:
437+
parms = []
438+
results = invokeSMCLI(rh, "System_Processor_Query", parms)
439+
cpu_total = 0
440+
cpu_use = 0
441+
if results['overallRC'] == 0:
442+
flag = 0
443+
for line in results['response'].splitlines():
444+
line_value = line.partition(' ')[2]
445+
if not line_value.strip():
446+
continue
447+
else:
448+
type_row = line_value.split(' ')
449+
if len(type_row) > 1:
450+
type_row = line_value.split(' ')[1]
451+
if type_row == 'TYPE':
452+
flag = 1
453+
if flag == 1:
454+
status_row = line_value.split(' ')[0]
455+
if (status_row.find('MASTER') != -1 or
456+
status_row == 'ALTERNATE' or
457+
status_row == 'PARKED'):
458+
cpu_use = cpu_use + 1
459+
if (type_row == 'CP' or type_row == 'IFL'):
460+
cpu_total = cpu_total + 1
461+
462+
rh.printSysLog("Exit getHost.lparCPUCount, cpu_total: " +
463+
str(cpu_total) + ", cpu_use: " + str(cpu_use) +
464+
", rc: " + str(rh.results['overallRC']))
465+
456466
return cpu_total, cpu_use
457467

458468

smtLayer/vmUtils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from smtLayer import msgs
2626
from smtLayer import vmStatus
27+
from smtLayer.vmcpHandler import VMCPHandler
2728

2829
from zvmsdk import config
2930

@@ -1272,7 +1273,12 @@ def purgeReader(rh):
12721273

12731274
parms = ['-T', rh.userid, '-k', 'spoolids=all']
12741275

1275-
results = invokeSMCLI(rh, "System_RDR_File_Manage", parms)
1276+
handler = VMCPHandler(rh)
1277+
1278+
if config.CONF.zvm.prefer_vmcp_query == 'yes':
1279+
results = handler._purge_reader()
1280+
else:
1281+
results = invokeSMCLI(rh, "System_RDR_File_Manage", parms)
12761282

12771283
if results['overallRC'] != 0:
12781284
rh.printLn("ES", results['response'])

smtLayer/vmcpHandler

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
import subprocess
2+
3+
from smtLayer import msgs
4+
5+
6+
class VMCPHandler:
7+
8+
def __init__(self, rh):
9+
"""
10+
Initialise the handler with a Request Handle.
11+
12+
Input:
13+
rh - smtLayer ReqHandle instance
14+
"""
15+
self._rh = rh
16+
17+
def _query_system_processor(self):
18+
"""
19+
To get LPAR total cpu and cpu in use
20+
21+
Returns cpu_total and cpu_use.
22+
"""
23+
24+
cmd = ["sudo", "/sbin/vmcp", "query PROCESSORS EXPANDED"]
25+
strCmd = ' '.join(cmd)
26+
rh.printSysLog("Invoking: " + strCmd)
27+
28+
cpu_total = 0
29+
cpu_use = 0
30+
rh = self._rh
31+
32+
try:
33+
response = subprocess.check_output(
34+
cmd,
35+
close_fds=True,
36+
stderr=subprocess.STDOUT)
37+
response = bytes.decode(response)
38+
39+
for line in response.splitlines():
40+
parts = line.split()
41+
42+
if len(parts) < 4 or parts[0] != 'PROCESSOR':
43+
continue
44+
status_row = parts[2]
45+
type_row = parts[3]
46+
if (status_row.find('MASTER') != -1 or
47+
status_row == 'ALTERNATE' or
48+
status_row == 'PARKED'):
49+
cpu_use = cpu_use + 1
50+
if (type_row == 'CP' or type_row == 'IFL'):
51+
cpu_total = cpu_total + 1
52+
53+
except subprocess.CalledProcessError as e:
54+
rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd,
55+
e.returncode, e.output))
56+
results = msgs.msg['0415'][0]
57+
results['rs'] = e.returncode
58+
rh.updateResults(results)
59+
60+
except Exception as e:
61+
rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
62+
type(e).__name__, str(e)))
63+
rh.updateResults(msgs.msg['0421'][0])
64+
65+
return cpu_total, cpu_use
66+
67+
def _purge_reader(self):
68+
"""
69+
Purge all reader files for rh.userid
70+
71+
Returns a standard results dict.
72+
"""
73+
rh = self._rh
74+
cmd = ["sudo", "/sbin/vmcp", 'purge', rh.userid, 'rdr', 'all']
75+
strCmd = ' '.join(cmd)
76+
rh.printSysLog("Invoking: " + strCmd)
77+
78+
79+
results = {'overallRC': 0,
80+
'rc': 0,
81+
'rs': 0,
82+
'errno': 0,
83+
'strError': '',
84+
'response': ''}
85+
86+
try:
87+
output = subprocess.check_output(
88+
cmd,
89+
close_fds=True,
90+
stderr=subprocess.STDOUT)
91+
92+
results['response'] = output.decode().strip()
93+
94+
except subprocess.CalledProcessError as e:
95+
rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd,
96+
e.returncode, e.output))
97+
results = msgs.msg['0415'][0]
98+
results['rs'] = e.returncode
99+
rh.updateResults(results)
100+
101+
except Exception as e:
102+
rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
103+
type(e).__name__, str(e)))
104+
rh.updateResults(msgs.msg['0421'][0])
105+
106+
return results
107+
108+
def _create_nic(self, vdev):
109+
"""
110+
Create a QDIO NIC at virtual device address <vdev> for rh.userid.
111+
112+
Returns a standard results dict.
113+
"""
114+
rh = self._rh
115+
rh.printSysLog("Enter VMCPHandler.create_nic, userid: %s vdev: %s"
116+
% (rh.userid, vdev))
117+
118+
try:
119+
result = self._run(['DEFINE', 'NIC', vdev, 'TYPE', 'QDIO'])
120+
if result['overallRC'] != 0:
121+
rh.printSysLog("Exit VMCPHandler.create_nic, rc: " +
122+
str(result['overallRC']))
123+
return result
124+
125+
verify = self._run(['QUERY', 'VIRTUAL', 'NIC', vdev])
126+
if verify['overallRC'] == 0:
127+
result['response'] = verify['response']
128+
129+
except subprocess.CalledProcessError as e:
130+
rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd,
131+
e.returncode, e.output))
132+
results = msgs.msg['0415'][0]
133+
results['rs'] = e.returncode
134+
rh.updateResults(results)
135+
136+
except Exception as e:
137+
rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
138+
type(e).__name__, str(e)))
139+
rh.updateResults(msgs.msg['0421'][0])
140+
141+
rh.printSysLog("Exit VMCPHandler.create_nic, rc: " +
142+
str(result['overallRC']))
143+
return result
144+
145+
146+
def _query_osa(self):
147+
"""
148+
Query OSA for rh.userid.
149+
150+
Returns a standard results dict.
151+
"""
152+
OSA_info = {}
153+
154+
try:
155+
result = self._run(['QUERY', 'OSA'])
156+
if result['overallRC'] != 0:
157+
return OSA_info
158+
159+
output = result['response']
160+
for line in output.splitlines():
161+
parts = line.split()
162+
if len(parts) < 3 or parts[0] != 'OSA':
163+
continue
164+
165+
osa_addr = parts[1]
166+
status_word = parts[2]
167+
168+
osa_type = 'OSA'
169+
if 'DEVTYPE' in parts:
170+
dt_idx = parts.index('DEVTYPE')
171+
if dt_idx + 1 < len(parts):
172+
osa_type = parts[dt_idx + 1]
173+
174+
if osa_type not in OSA_info:
175+
OSA_info[osa_type] = {
176+
'FREE': [], 'BOXED': [], 'OFFLINE': [], 'ATTACHED': []}
177+
178+
if status_word == 'ATTACHED':
179+
attached_id = parts[4] if len(parts) > 4 else 'UNKNOWN'
180+
OSA_info[osa_type]['ATTACHED'].append((attached_id, osa_addr))
181+
elif status_word in OSA_info[osa_type]:
182+
OSA_info[osa_type][status_word].append(osa_addr)
183+
184+
except subprocess.CalledProcessError as e:
185+
rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd,
186+
e.returncode, e.output))
187+
results = msgs.msg['0415'][0]
188+
results['rs'] = e.returncode
189+
rh.updateResults(results)
190+
191+
except Exception as e:
192+
# All other exceptions.
193+
rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
194+
type(e).__name__, str(e)))
195+
rh.updateResults(msgs.msg['0421'][0])
196+
197+
return OSA_info

zvmsdk/smtclient.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from zvmsdk import log
4848
from zvmsdk import returncode
4949
from zvmsdk import utils as zvmutils
50+
from smtLayer.vmcpHandler import VMCPHandler
5051

5152

5253
CONF = config.CONF
@@ -2049,40 +2050,46 @@ def _create_nic(self, userid, vdev, nic_id=None, mac_addr=None,
20492050
if mac_addr is not None:
20502051
LOG.warning("Ignore the mac address %s when "
20512052
"adding nic on an active system" % mac_addr)
2052-
requestData = ' '.join((
2053-
'SMAPI %s API Virtual_Network_Adapter_Create_Extended' %
2054-
userid,
2055-
"--operands",
2056-
"-k image_device_number=%s" % vdev,
2057-
"-k adapter_type=QDIO"))
2058-
2059-
try:
2060-
self._request(requestData)
2061-
except (exception.SDKSMTRequestFailed,
2062-
exception.SDKInternalError) as err1:
2063-
msg1 = err1.format_message()
2064-
persist_OK = True
2053+
handler = VMCPHandler(rh)
2054+
if config.CONF.zvm.prefer_vmcp_query == 'yes':
2055+
result = handler._create_nic(
2056+
userid, vdev)
2057+
else:
20652058
requestData = ' '.join((
2066-
'SMAPI %s API Virtual_Network_Adapter_Delete_DM' % userid,
2059+
'SMAPI %s API Virtual_Network_Adapter_Create_Extended' %
2060+
userid,
20672061
"--operands",
2068-
'-v %s' % vdev))
2062+
"-k image_device_number=%s" % vdev,
2063+
"-k adapter_type=QDIO"))
2064+
20692065
try:
20702066
self._request(requestData)
20712067
except (exception.SDKSMTRequestFailed,
2072-
exception.SDKInternalError) as err2:
2073-
results = err2.results
2074-
msg2 = err2.format_message()
2075-
if ((results['rc'] == 404) and
2076-
(results['rs'] == 8)):
2077-
persist_OK = True
2068+
exception.SDKInternalError) as err1:
2069+
msg1 = err1.format_message()
2070+
persist_OK = True
2071+
requestData = ' '.join((
2072+
'SMAPI %s API Virtual_Network_Adapter_Delete_DM' %
2073+
userid,
2074+
"--operands",
2075+
'-v %s' % vdev))
2076+
try:
2077+
self._request(requestData)
2078+
except (exception.SDKSMTRequestFailed,
2079+
exception.SDKInternalError) as err2:
2080+
results = err2.results
2081+
msg2 = err2.format_message()
2082+
if ((results['rc'] == 404) and
2083+
(results['rs'] == 8)):
2084+
persist_OK = True
2085+
else:
2086+
persist_OK = False
2087+
if persist_OK:
2088+
self._create_nic_active_exception(err1, userid, vdev)
20782089
else:
2079-
persist_OK = False
2080-
if persist_OK:
2081-
self._create_nic_active_exception(err1, userid, vdev)
2082-
else:
2083-
raise exception.SDKNetworkOperationError(rs=4,
2084-
nic=vdev, userid=userid,
2085-
create_err=msg1, revoke_err=msg2)
2090+
raise exception.SDKNetworkOperationError(rs=4,
2091+
nic=vdev, userid=userid,
2092+
create_err=msg1, revoke_err=msg2)
20862093

20872094
self._NetDbOperator.switch_add_record(userid, vdev, port=nic_id)
20882095
msg = ('Create nic device %(vdev)s for guest %(vm)s successfully'
@@ -3364,6 +3371,11 @@ def _is_OSA_free(self, OSA_device):
33643371
return False
33653372

33663373
def _query_OSA(self):
3374+
handler = VMCPHandler(rh)
3375+
3376+
if config.CONF.zvm.prefer_vmcp_query == 'yes':
3377+
return handler._query_osa()
3378+
33673379
smt_userid = zvmutils.get_smt_userid()
33683380
rd = "SMAPI %s API Virtual_Network_OSA_Query" % smt_userid
33693381
OSA_info = {}

0 commit comments

Comments
 (0)