2018-12-07 18:59:01 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import csv
|
|
|
|
import optparse
|
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
try:
|
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
path = os.path.realpath(os.path.join(path, '..', 'lib', 'python'))
|
|
|
|
if path.startswith('/opt'):
|
|
|
|
sys.path.append(path)
|
|
|
|
|
|
|
|
import confluent.client as client
|
|
|
|
import confluent.sortutil as sortutil
|
|
|
|
|
|
|
|
|
|
|
|
def lookupdata(data, key):
|
|
|
|
ret = data.get(key, {}).get('value', '')
|
|
|
|
if ret is None:
|
|
|
|
ret = ''
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argparser = optparse.OptionParser(
|
|
|
|
usage='''\n %prog noderange -o ansible.hosts
|
|
|
|
\n ''')
|
|
|
|
argparser.add_option('-o', '--output',
|
|
|
|
help='xCAT stanza file')
|
2018-12-07 19:08:34 +00:00
|
|
|
argparser.add_option('-m', '--macs',
|
|
|
|
help='xCAT macs.csv to write')
|
2018-12-07 18:59:01 +00:00
|
|
|
(options, args) = argparser.parse_args()
|
|
|
|
try:
|
|
|
|
noderange = args[0]
|
|
|
|
except IndexError:
|
|
|
|
argparser.print_help()
|
|
|
|
sys.exit(1)
|
2018-12-07 19:08:34 +00:00
|
|
|
if not (options.output or options.macs):
|
|
|
|
sys.stderr.write('Output file must be specified by -o or -m\n')
|
2018-12-07 18:59:01 +00:00
|
|
|
sys.exit(1)
|
|
|
|
sess = client.Command()
|
|
|
|
databynode = {}
|
|
|
|
for res in sess.read('/noderange/{0}/attributes/all'.format(noderange)):
|
|
|
|
for node in res.get('databynode', {}):
|
|
|
|
if node not in databynode:
|
|
|
|
databynode[node] = {}
|
|
|
|
databynode[node].update(res['databynode'][node])
|
2018-12-07 19:08:34 +00:00
|
|
|
if options.output:
|
|
|
|
with open(options.output, 'w') as importfile:
|
|
|
|
for node in sortutil.natural_sort(databynode):
|
|
|
|
xcatattrs = collect_attribute_data(databynode, node)
|
|
|
|
importfile.write('{0}:\n'.format(node))
|
|
|
|
importfile.write(' objtype=node\n')
|
|
|
|
importfile.write(' arch=x86_64\n')
|
|
|
|
importfile.write(' netboot=xnba\n')
|
|
|
|
importfile.write(' mgt=ipmi\n')
|
|
|
|
for attr in xcatattrs:
|
|
|
|
if xcatattrs[attr] is None:
|
|
|
|
continue
|
|
|
|
importfile.write(' {0}={1}\n'.format(attr,
|
|
|
|
xcatattrs[attr]))
|
|
|
|
importfile.write('\n')
|
|
|
|
if options.macs:
|
|
|
|
with open(options.macs, 'w') as importfile:
|
|
|
|
macsv = csv.writer(importfile)
|
|
|
|
macsv.writerow(['#node', 'mac'])
|
|
|
|
for node in sortutil.natural_sort(databynode):
|
|
|
|
xcatattrs = collect_attribute_data(databynode, node)
|
|
|
|
macsv.writerow([node, xcatattrs['mac']])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def collect_attribute_data(databynode, node):
|
|
|
|
data = databynode[node]
|
|
|
|
xcatattrs = {}
|
|
|
|
xcatattrs['groups'] = ','.join(data.get('groups', []))
|
|
|
|
xcatattrs['bmc'] = data.get('hardwaremanagement.manager', {}).get(
|
|
|
|
'value', None)
|
|
|
|
xcatattrs['mpa'] = data.get('enclosure.manager', {}).get(
|
|
|
|
'value', None)
|
|
|
|
xcatattrs['slotid'] = data.get('enclosure.bay', {}).get(
|
|
|
|
'value', None)
|
|
|
|
gotmac = False
|
|
|
|
for key in data:
|
|
|
|
if key.startswith('net.') and 'hwaddr' in key:
|
|
|
|
currmac = data[key].get('value', None)
|
|
|
|
if currmac:
|
|
|
|
if gotmac:
|
|
|
|
sys.stderr.write(
|
|
|
|
'Ignoring {0} and using only {1} for mac, '
|
|
|
|
'multiple macs not supported by '
|
|
|
|
'confluent2xcat\n'.format(key, gotmac))
|
2018-12-07 18:59:01 +00:00
|
|
|
continue
|
2018-12-07 19:08:34 +00:00
|
|
|
gotmac = key
|
|
|
|
xcatattrs['mac'] = currmac
|
|
|
|
return xcatattrs
|
2018-12-07 18:59:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|