aboutsummaryrefslogtreecommitdiffstats
path: root/resources/tools/testbed-setup/cimc/cimclib.py
blob: f91832e0c95083e5ef16a6ba8cce3f0dae9add01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/python
#
# Copyright (c) 2016 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import requests
import xml.etree.ElementTree as et
import re

from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

BASEDN = "sys/rack-unit-1"

###
### Helper function - iterate through a list in pairs
###
def chunks(lst, chunksize):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(lst), chunksize):
        yield lst[i:i+chunksize]

###
### Helper function: Perform an XML request to CIMC
###
def xml_req(ip, xml, debug=False):
    if debug:
        print "DEBUG: XML-REQUEST:"
        et.dump(xml)
    headers = {'Content-Type': 'text/xml'}
    req = requests.post('https://' + ip + '/nuova', headers=headers,
                        verify=False, data=et.tostring(xml))
    resp = et.fromstring(req.content)
    if debug:
        print "DEBUG: XML-RESPONSE:"
        et.dump(resp)

    if resp.tag == 'error':
        if debug:
            print "XML response contains error:"
            et.dump(error)
        raise RuntimeError('XML response contains error')
    return resp

###
### Authenticate (Log-In) to CIMC and obtain a cookie
###
def login(ip, username, password):
    reqxml = et.Element('aaaLogin',
                        attrib={'inName':username, 'inPassword':password})
    respxml = xml_req(ip, reqxml)
    try:
        cookie = respxml.attrib['outCookie']
    except:
        print "Cannot find cookie in CIMC server response."
        print "CIMC server output:"
        et.dump(respxml)
        raise

    return cookie

###
### Log out from CIMC.
###
### Note: There is a maximum session limit in CIMC and sessions to take a long
### time (10 minutes) to time out. Therefore, calling this function during
### testing is essential, otherwise one will quickly exhaust all available
### sessions.
###
def logout(ip, cookie):
    reqxml = et.Element('aaaLogout', attrib={'cookie': cookie,
                                             'inCookie': cookie})
    xml_req(ip, reqxml)

###
### Power off the host
###
def powerOff(ip, cookie, debug=False):
    reqxml = et.Element('configConfMo',
                        attrib={'cookie': cookie, 'inHierarchical': 'false',
                                'dn': BASEDN})
    inconfig = et.SubElement(reqxml, 'inConfig')
    et.SubElement(inconfig, 'computeRackUnit',
                  attrib={'adminPower': 'down', 'dn': BASEDN})
    respxml = xml_req(ip, reqxml, debug)
    return respxml

###
### Power on the host
###
def powerOn(ip, cookie, debug=False):
    reqxml = et.Element('configConfMo',
                        attrib={'cookie': cookie, 'inHierarchical': 'false',
                                'dn': BASEDN})
    inconfig = et.SubElement(reqxml, 'inConfig')
    et.SubElement(inconfig, 'computeRackUnit',
                  attrib={'adminPower': 'up', 'dn': BASEDN})
    respxml = xml_req(ip, reqxml, debug)
    return respxml

###
### Restore BIOS to default settings
###
def restoreBiosDefaultSettings(ip, cookie, debug=False):
    reqxml = et.Element('configResolveClass',
                        attrib={'cookie': cookie, 'inHierarchical': 'true',
                                'classId': 'biosPlatformDefaults'})
    respxml = xml_req(ip, reqxml, debug)

    configs = respxml.find('outConfigs')
    defaults = configs.find('biosPlatformDefaults')

    reqxml = et.Element('configConfMo',
                        attrib={'cookie': cookie, 'inHierarchical': 'true',
                                'dn': "{}/bios/bios-settings".format(BASEDN)})
    inconfig = et.SubElement(reqxml, 'inConfig')
    biosset = et.SubElement(inconfig, 'biosSettings')
    biosset.extend(defaults)

    respxml = xml_req(ip, reqxml, debug)
    return respxml

###
### Apply specified BIOS settings.
###
### These must be a list of strings in XML format. Not currently very
### user friendly. Format can either be obtained from CIMC
### documention, or by setting them manually and then fetching
### BIOS settings via CIMC XML API.
###
def setBiosSettings(ip, cookie, settings, debug=False):
    reqxml = et.Element('configConfMo',
                        attrib={'cookie': cookie, 'inHierarchical': 'true',
                                'dn': "{}/bios/bios-settings".format(BASEDN)})
    inconfig = et.SubElement(reqxml, 'inConfig')
    biosset = et.SubElement(inconfig, 'biosSettings')
    print "Applying settings:"
    print settings
    for s in settings:
        x = et.fromstring(s)
        et.dump(x)
        biosset.append(et.fromstring(s))

    respxml = xml_req(ip, reqxml, debug)
    return respxml
###
### Delete any existing virtual drives
###
### WARNING: THIS WILL ERASE ALL DATA ON ALL DISKS, WITHOUT ANY CONFIRMATION
### QUESTION.
###
### The server must be POWERED ON for this to succeed.
###
def deleteAllVirtualDrives(ip, cookie, debug=False):
    reqxml = et.Element('configResolveClass',
                        attrib={'cookie': cookie, 'inHierarchical': 'true',
                                'classId': 'storageController'})
    respxml = xml_req(ip, reqxml, debug)

    configs = respxml.find('outConfigs')
    for sc in configs.iter('storageController'):
        if debug:
            print "DEBUG: SC DN {} ID {}".format(sc.attrib['dn'],
                                                 sc.attrib['id'])
        reqxml = et.Element('configConfMo',
                            attrib={'cookie': cookie, 'inHierarchical': 'true',
                                    'dn': sc.attrib['dn']})
        inconfig = et.SubElement(reqxml, 'inConfig')
        et.SubElement(inconfig, 'storageController',
                      attrib={'adminAction': 'delete-all-vds-reset-pds',
                              'dn': sc.attrib['dn']})
        xml_req(ip, reqxml, debug)

###
### Create a single RAID-10 across all drives.
###
### The server must be POWERED ON for this to succeed.
###
def createRaid10_all(ip, cookie, debug=False):
    reqxml = et.Element('configResolveClass',
                        attrib={'cookie': cookie, 'inHierarchical': 'true',
                                'classId': 'storageController'})
    respxml = xml_req(ip, reqxml, debug)

    configs = respxml.find('outConfigs')
    for sc in configs.iter('storageController'):
        if debug:
            print "DEBUG: SC DN {} ID {}".format(sc.attrib['dn'],
                                                 sc.attrib['id'])
        #
        # Find disk size and number of disks
        #
        disks = []
        total_size = 0
        for pd in sc.iter('storageLocalDisk'):
            if debug:
                print "DEBUG: PD {} size {}".format(pd.attrib['id'],
                                                    pd.attrib['coercedSize'])
            disks.append(pd.attrib['id'])
            total_size += int(pd.attrib['coercedSize'].split(' ')[0])

        #
        # Create a RAID10 array of all available disks, as in:
        # [1,2][3,4][5,6][7,8][9,10][11,12][13,14][15,16][17,18]
        #
        raid_size = total_size/2
        raid_span = ''
        for p in list(chunks(disks, 2)):
            raid_span += "[{},{}]".format(p[0], p[1])

        reqxml = et.Element('configConfMo',
                            attrib={'cookie': cookie, 'inHierarchical': 'true',
                                    'dn': sc.attrib['dn']})
        inconfig = et.SubElement(reqxml, 'inConfig')
        et.SubElement(inconfig,
                      'storageVirtualDriveCreatorUsingUnusedPhysicalDrive',
                      attrib={'virtualDriveName': 'raid10-all',
                              'size': str(raid_size)+' MB',
                              'raidLevel': '10', 'driveGroup': raid_span,
                              'adminState': 'trigger'})

        xml_req(ip, reqxml, debug)

###
### Create a single RAID across from empty drives as provided.
###
### The server must be POWERED ON for this to succeed.
###
def createRaid(ip, cookie, name, raidlevel, size, drives, debug=False):
    reqxml = et.Element('configResolveClass',
                        attrib={'cookie': cookie, 'inHierarchical': 'true',
                                'classId': 'storageController'})
    respxml = xml_req(ip, reqxml, debug)

    configs = respxml.find('outConfigs')
    for sc in configs.iter('storageController'):
        if debug:
            print "DEBUG: SC DN {} ID {}".format(sc.attrib['dn'],
                                                 sc.attrib['id'])

        reqxml = et.Element('configConfMo',
                            attrib={'cookie': cookie, 'inHierarchical': 'true',
                                    'dn': sc.attrib['dn']})
        inconfig = et.SubElement(reqxml, 'inConfig')
        et.SubElement(inconfig,
                      'storageVirtualDriveCreatorUsingUnusedPhysicalDrive',
                      attrib={'virtualDriveName': name,
                              'size': str(size)+' MB',
                              'raidLevel': raidlevel,
                              'driveGroup': drives,
                              'adminState': 'trigger'})

        xml_req(ip, reqxml, debug)

###
### Enable Serial-Over-LAN (SOL) console and redirect BIOS output to
### serial console
###
def enableConsoleRedir(ip, cookie, debug=False):
    reqxml = et.Element('configConfMo',
                        attrib={'cookie': cookie, 'inHierarchical': 'false',
                                'dn': "{}/bios/bios-settings".format(BASEDN)})
    inconfig = et.SubElement(reqxml, 'inConfig')
    bs = et.SubElement(inconfig, 'biosSettings',
                       attrib={'dn': "{}/bios/bios-settings".format(BASEDN)})
    et.SubElement(bs,
                  'biosVfConsoleRedirection',
                  attrib={'vpConsoleRedirection': 'com-0',
                          'vpBaudRate': '115200'})
    respxml = xml_req(ip, reqxml, debug)
    reqxml = et.Element('configConfMo',
                        attrib={'cookie': cookie, 'inHierarchical': 'false',
                                'dn': BASEDN+'/sol-if'})
    inconfig = et.SubElement(reqxml, 'inConfig')
    et.SubElement(inconfig, 'solIf',
                  attrib={'dn': BASEDN+'/sol-if', 'adminState': 'enable',
                          'speed': '115200', 'comport': 'com0'})
    respxml = xml_req(ip, reqxml, debug)
    return respxml

###
### Boot into UEFI bootloader (we may use this to "park" the host in
### powered-on state)
###
def bootIntoUefi(ip, cookie, debug=False):
    reqxml = et.Element('configConfMo',
                        attrib={'cookie': cookie, 'inHierarchical': 'false',
                                'dn': BASEDN+'/boot-policy'})
    inconfig = et.SubElement(reqxml, 'inConfig')
    bootDef = et.SubElement(inconfig, 'lsbootDef',
                            attrib={'dn': BASEDN+'/boot-policy',
                                    'rebootOnUpdate': 'yes'})
    et.SubElement(bootDef, 'lsbootEfi',
                  attrib={'rn': 'efi-read-only', 'order': '1',
                          'type': 'efi'})

    respxml = xml_req(ip, reqxml, debug)
    return respxml

###
### Boot via PXE. Reboot immediately.
###
def bootPXE(ip, cookie, debug=False):
    reqxml = et.Element('configConfMo',
                        attrib={'cookie': cookie, 'inHierarchical': 'false',
                                'dn': BASEDN+'/boot-policy'})
    inconfig = et.SubElement(reqxml, 'inConfig')
    bootDef = et.SubElement(inconfig, 'lsbootDef',
                            attrib={'dn': BASEDN+'/boot-policy',
                                    'rebootOnUpdate': 'yes'})
    et.SubElement(bootDef, 'lsbootLan',
                  attrib={'rn': 'lan-read-only', 'order': '1',
                          'type': 'lan', 'prot': 'pxe'})

    respxml = xml_req(ip, reqxml, debug)
    return respxml


###
### Boot via Local HDD first, then via PXE. Do not reboot immediately.
###
def bootHDDPXE(ip, cookie, debug=False):
    reqxml = et.Element('configConfMo',
                        attrib={'cookie': cookie, 'inHierarchical': 'false',
                                'dn': BASEDN+'/boot-policy'})
    inconfig = et.SubElement(reqxml, 'inConfig')
    bootDef = et.SubElement(inconfig, 'lsbootDef',
                            attrib={'dn': BASEDN+'/boot-policy',
                                    'rebootOnUpdate': 'no'})
    storage = et.SubElement(bootDef, 'lsbootStorage',
                            attrib={'rn': 'storage-read-write',
                                    'access': 'read-write',
                                    'order': '1', 'type': 'storage'})
    et.SubElement(storage, 'lsbootLocalStorage',
                  attrib={'rn': 'local-storage'})
    et.SubElement(bootDef, 'lsbootLan',
                  attrib={'rn': 'lan-read-only', 'order': '2',
                          'type': 'lan', 'prot': 'pxe'})

    respxml = xml_req(ip, reqxml, debug)
    return respxml

###
### Return LOM port 1 MAC address
###
def getLOMMacAddress(ip, cookie, debug=False):
    reqxml = et.Element('configResolveClass',
                        attrib={'cookie': cookie, 'inHierarchical': 'true',
                                'classId': 'networkAdapterUnit'})
    respxml = xml_req(ip, reqxml, debug)
    reqxml = et.Element('configResolveDn',
                        attrib={'cookie': cookie, 'inHierarchical': 'true',
                                'dn': BASEDN+'/network-adapter-L/eth-1'})
    respxml = xml_req(ip, reqxml, debug)

    oc = respxml.find('outConfig')
    netw = oc.find('networkAdapterEthIf')
    if debug:
        print "DEBUG: MAC address is {}".format(netw.get('mac'))
    return netw.get('mac')

###
### Return all port MAC addresses
###
def getMacAddresses(ip, cookie, debug=False):
    maclist = {}
    reqxml = et.Element('configResolveClass',
                        attrib={'cookie': cookie, 'inHierarchical': 'true',
                                'classId': 'networkAdapterUnit'})
    respxml = xml_req(ip, reqxml, debug)
    oc = respxml.find('outConfigs')
    for adapter in oc.iter('networkAdapterUnit'):
        if debug:
            print "DEBUG: ADAPTER SLOT {} MODEL {}".format(adapter.attrib['slot'],
                                                           adapter.attrib['model'])
        slot = adapter.attrib['slot']
        maclist[slot] = {}
        for port in adapter.iter('networkAdapterEthIf'):
            if debug:
                print "DEBUG:    SLOT {} PORT {} MAC {}".format(slot,
                                                                port.attrib['id'],
                                                                port.attrib['mac'])
            maclist[slot][port.attrib['id']] = port.attrib['mac'].lower()

    reqxml = et.Element('configResolveClass',
                        attrib={'cookie': cookie, 'inHierarchical': 'true',
                                'classId': 'adaptorUnit'})
    respxml = xml_req(ip, reqxml, debug)
    oc = respxml.find('outConfigs')
    for adapter in oc.iter('adaptorUnit'):
        if debug:
            print "DEBUG: VIC ADAPTER SLOT {} MODEL {}".format(adapter.attrib['pciSlot'],
                                                               adapter.attrib['model'])
        slot = adapter.attrib['pciSlot']
        maclist[slot] = {}
        for port in adapter.iter('adaptorHostEthIf'):
            portnum = int(re.sub('eth([0-9]+)', '\\1', port.attrib['name']))+1
            if debug:
                print "DEBUG:    VIC SLOT {} PORT {} MAC {}".format(slot,
                                                                    portnum,
                                                                    port.attrib['mac'])
            maclist[slot][portnum] = port.attrib['mac'].lower()

    return maclist