2015-10-13 18:12:43 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
2017-01-06 18:28:28 +00:00
|
|
|
# Copyright 2015-2017 Lenovo
|
2015-10-13 18:12:43 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
__author__ = 'jjohnson2'
|
|
|
|
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
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
|
2016-04-12 19:18:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def attrrequested(attr, attrlist, seenattributes):
|
|
|
|
for candidate in attrlist:
|
|
|
|
truename = candidate
|
|
|
|
if candidate.startswith('hm'):
|
|
|
|
candidate = candidate.replace('hm', 'hardwaremanagement', 1)
|
|
|
|
if candidate == attr:
|
|
|
|
seenattributes.add(truename)
|
|
|
|
return True
|
|
|
|
elif '.' not in candidate and attr.startswith(candidate + '.'):
|
|
|
|
seenattributes.add(truename)
|
|
|
|
return True
|
|
|
|
return False
|
2015-10-13 18:12:43 +00:00
|
|
|
argparser = optparse.OptionParser(
|
2017-01-06 18:28:28 +00:00
|
|
|
usage="Usage: %prog [options] noderange [list of attributes]")
|
2015-10-13 18:12:43 +00:00
|
|
|
argparser.add_option('-b', '--blame', action='store_true',
|
|
|
|
help='Show information about how attributes inherited')
|
|
|
|
(options, args) = argparser.parse_args()
|
|
|
|
try:
|
|
|
|
noderange = args[0]
|
2015-10-14 15:07:31 +00:00
|
|
|
nodelist = '/noderange/{0}/nodes/'.format(noderange)
|
2015-10-13 18:12:43 +00:00
|
|
|
except IndexError:
|
2015-10-14 15:07:31 +00:00
|
|
|
nodelist = '/nodes/'
|
2015-10-13 18:12:43 +00:00
|
|
|
session = client.Command()
|
|
|
|
exitcode = 0
|
|
|
|
if len(args) > 1:
|
|
|
|
seenattributes = set([])
|
|
|
|
for res in session.read('/noderange/{0}/attributes/all'.format(noderange)):
|
2015-10-19 18:09:37 +00:00
|
|
|
if 'error' in res:
|
2015-10-19 18:23:02 +00:00
|
|
|
sys.stderr.write(res['error'] + '\n')
|
2015-10-19 18:09:37 +00:00
|
|
|
exitcode = 1
|
|
|
|
continue
|
2015-10-13 18:12:43 +00:00
|
|
|
for node in res['databynode']:
|
|
|
|
for attr in res['databynode'][node]:
|
|
|
|
seenattributes.add(attr)
|
|
|
|
currattr = res['databynode'][node][attr]
|
2016-04-12 19:18:31 +00:00
|
|
|
if attrrequested(attr, args[1:], seenattributes):
|
2015-10-13 18:12:43 +00:00
|
|
|
if 'value' in currattr:
|
|
|
|
if currattr['value'] is not None:
|
|
|
|
attrout = '{0}: {1}: {2}'.format(
|
|
|
|
node, attr, currattr['value'])
|
|
|
|
else:
|
|
|
|
attrout = '{0}: {1}:'.format(node, attr)
|
|
|
|
elif 'isset' in currattr:
|
|
|
|
if currattr['isset']:
|
|
|
|
attrout = '{0}: {1}: ********'.format(node, attr)
|
|
|
|
else:
|
|
|
|
attrout = '{0}: {1}:'.format(node, attr)
|
2017-02-10 21:41:47 +00:00
|
|
|
elif 'broken' in currattr:
|
|
|
|
attrout = '{0}: {1}: *ERROR* BROKEN EXPRESSION: ' \
|
|
|
|
'{2}'.format(node, attr,
|
|
|
|
currattr['broken'])
|
2017-03-03 18:10:29 +00:00
|
|
|
elif isinstance(currattr, list) or isinstance(currattr, tuple):
|
|
|
|
attrout = '{0}: {1}: {2}'.format(node, attr, ', '.join(map(str, currattr)))
|
|
|
|
elif isinstance(currattr, dict):
|
|
|
|
dictout = []
|
|
|
|
for k, v in currattr.items:
|
|
|
|
dictout.append("{0}={1}".format(k, v))
|
|
|
|
attrout = '{0}: {1}: {2}'.format(node, attr, ', '.join(map(str, dictout)))
|
|
|
|
else:
|
|
|
|
print ("CODE ERROR" + repr(attr))
|
|
|
|
|
2017-02-10 21:41:47 +00:00
|
|
|
if options.blame or 'broken' in currattr:
|
2015-10-13 18:12:43 +00:00
|
|
|
blamedata = []
|
|
|
|
if 'inheritedfrom' in currattr:
|
|
|
|
blamedata.append('inherited from group {0}'.format(
|
|
|
|
currattr['inheritedfrom']
|
|
|
|
))
|
|
|
|
if 'expression' in currattr:
|
|
|
|
blamedata.append(
|
|
|
|
'derived from expression "{0}"'.format(
|
|
|
|
currattr['expression']))
|
|
|
|
if blamedata:
|
|
|
|
attrout += ' (' + ', '.join(blamedata) + ')'
|
|
|
|
print attrout
|
2015-10-19 18:09:37 +00:00
|
|
|
if not exitcode:
|
|
|
|
for attr in args[1:]:
|
|
|
|
if attr not in seenattributes:
|
|
|
|
sys.stderr.write('Error: {0} not a valid attribute\n'.format(attr))
|
|
|
|
exitcode = 1
|
2015-10-13 18:12:43 +00:00
|
|
|
else:
|
2015-10-14 15:07:31 +00:00
|
|
|
for res in session.read(nodelist):
|
|
|
|
if 'error' in res:
|
|
|
|
sys.stderr.write(res['error'] + '\n')
|
|
|
|
exitcode = 1
|
|
|
|
else:
|
|
|
|
print res['item']['href'].replace('/', '')
|
2015-10-13 18:12:43 +00:00
|
|
|
sys.exit(exitcode)
|