2017-11-01 20:48:17 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
|
|
|
# Copyright 2017 Lenovo
|
|
|
|
#
|
|
|
|
# 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 os
|
|
|
|
import signal
|
|
|
|
import optparse
|
|
|
|
import sys
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
class NullOpt(object):
|
|
|
|
blame = None
|
|
|
|
clear = None
|
|
|
|
|
|
|
|
|
|
|
|
def bailout(msg, code=1):
|
|
|
|
sys.stderr.write(msg + '\n')
|
|
|
|
sys.exit(code)
|
|
|
|
|
|
|
|
|
|
|
|
argparser = optparse.OptionParser()
|
2018-02-06 21:31:20 +00:00
|
|
|
argparser.add_option('-c', '--comparedefault', dest='comparedefault',
|
|
|
|
action='store_true', default=False,
|
|
|
|
help='Compare given settings to default or list settings '
|
|
|
|
'that are non default')
|
2018-02-06 21:52:13 +00:00
|
|
|
argparser.add_option('-d', '--detail', dest='detail',
|
|
|
|
action='store_true', default=False,
|
|
|
|
help='Provide verbose information as available, such as '
|
|
|
|
'help text and possible valid values')
|
2018-02-07 20:46:08 +00:00
|
|
|
argparser.add_option('-x', '--exclude', dest='exclude',
|
|
|
|
action='store_true', default=False,
|
|
|
|
help='Treat positional arguments as items to not '
|
|
|
|
'examine, compare, or restore default')
|
2017-11-01 20:48:17 +00:00
|
|
|
(options, args) = argparser.parse_args()
|
|
|
|
|
|
|
|
cfgpaths = {
|
2017-11-03 14:36:32 +00:00
|
|
|
'bmc.ipv4_address': (
|
2017-11-01 20:48:17 +00:00
|
|
|
'configuration/management_controller/net_interfaces/management',
|
|
|
|
'ipv4_address'),
|
|
|
|
'bmc.ipv4_method': (
|
|
|
|
'configuration/management_controller/net_interfaces/management',
|
|
|
|
'ipv4_configuration'),
|
|
|
|
'bmc.ipv4_gateway': (
|
|
|
|
'configuration/management_controller/net_interfaces/management',
|
|
|
|
'ipv4_gateway'),
|
2018-03-27 20:32:37 +00:00
|
|
|
'bmc.hostname': (
|
|
|
|
'configuration/management_controller/hostname', 'hostname'),
|
2017-11-01 20:48:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 14:31:00 +00:00
|
|
|
autodeps = {
|
2017-11-03 14:36:32 +00:00
|
|
|
'bmc.ipv4_address': (('bmc.ipv4_method', 'static'),)
|
2017-11-03 14:31:00 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 20:48:17 +00:00
|
|
|
try:
|
|
|
|
noderange = args[0]
|
|
|
|
except IndexError:
|
|
|
|
argparser.print_help()
|
|
|
|
sys.exit(1)
|
2017-11-13 16:49:40 +00:00
|
|
|
client.check_globbing(noderange)
|
2017-11-01 20:48:17 +00:00
|
|
|
setmode = None
|
|
|
|
assignment = {}
|
|
|
|
queryparms = {}
|
2018-02-02 22:17:02 +00:00
|
|
|
printsys = []
|
|
|
|
setsys = {}
|
2018-02-09 00:39:39 +00:00
|
|
|
forceset = False
|
|
|
|
needval = None
|
2017-11-02 21:07:44 +00:00
|
|
|
|
2018-02-07 20:46:08 +00:00
|
|
|
if len(args) == 1 or options.exclude:
|
|
|
|
if not options.exclude:
|
|
|
|
printsys = 'all'
|
2017-11-03 14:36:32 +00:00
|
|
|
for candidate in cfgpaths:
|
|
|
|
path, attrib = cfgpaths[candidate]
|
|
|
|
path = '/noderange/{0}/{1}'.format(noderange, path)
|
|
|
|
if path not in queryparms:
|
|
|
|
queryparms[path] = {}
|
|
|
|
queryparms[path][attrib] = candidate
|
2018-02-09 00:39:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _assign_value():
|
|
|
|
if key not in cfgpaths:
|
|
|
|
setsys[key] = value
|
|
|
|
for depkey, depval in autodeps.get(key, []):
|
|
|
|
assignment[depkey] = depval
|
|
|
|
assignment[key] = value
|
|
|
|
|
|
|
|
|
2017-11-01 20:48:17 +00:00
|
|
|
for param in args[1:]:
|
2018-02-09 00:39:39 +00:00
|
|
|
if param == 'show':
|
|
|
|
continue # forgive muscle memory of pasu users
|
|
|
|
if param == 'set':
|
|
|
|
setmode = True
|
|
|
|
forceset = True
|
|
|
|
continue
|
|
|
|
if needval:
|
|
|
|
key = needval
|
|
|
|
value = param
|
|
|
|
_assign_value()
|
|
|
|
continue
|
|
|
|
if '=' in param or param[-1] == ':' or forceset:
|
2017-11-01 20:48:17 +00:00
|
|
|
if setmode is None:
|
|
|
|
setmode = True
|
|
|
|
if setmode != True:
|
|
|
|
bailout('Cannot do set and query in same command')
|
2018-02-09 00:39:39 +00:00
|
|
|
if '=' in param:
|
|
|
|
key, _, value = param.partition('=')
|
|
|
|
_assign_value()
|
|
|
|
elif param[-1] == ':':
|
|
|
|
needval = param[:-1]
|
|
|
|
else:
|
|
|
|
needval = param
|
2017-11-01 20:48:17 +00:00
|
|
|
else:
|
|
|
|
if setmode is None:
|
|
|
|
setmode = False
|
|
|
|
if setmode != False:
|
|
|
|
bailout('Cannot do set and query in same command')
|
|
|
|
if '.' not in param:
|
|
|
|
matchedparms = False
|
|
|
|
for candidate in cfgpaths:
|
|
|
|
if candidate.startswith('{0}.'.format(param)):
|
|
|
|
matchedparms = True
|
2018-02-07 20:46:08 +00:00
|
|
|
if not options.exclude:
|
|
|
|
path, attrib = cfgpaths[candidate]
|
|
|
|
path = '/noderange/{0}/{1}'.format(noderange, path)
|
|
|
|
if path not in queryparms:
|
|
|
|
queryparms[path] = {}
|
|
|
|
queryparms[path][attrib] = candidate
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
del queryparms[path]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2017-11-01 20:48:17 +00:00
|
|
|
if not matchedparms:
|
2018-02-02 22:17:02 +00:00
|
|
|
printsys.append(param)
|
2017-11-01 20:48:17 +00:00
|
|
|
elif param not in cfgpaths:
|
2018-02-02 22:17:02 +00:00
|
|
|
printsys.append(param)
|
2017-11-01 20:48:17 +00:00
|
|
|
else:
|
2017-11-02 21:07:44 +00:00
|
|
|
path, attrib = cfgpaths[param]
|
|
|
|
path = '/noderange/{0}/{1}'.format(noderange, path)
|
|
|
|
if path not in queryparms:
|
|
|
|
queryparms[path] = {}
|
|
|
|
queryparms[path][attrib] = param
|
2017-11-01 20:48:17 +00:00
|
|
|
session = client.Command()
|
2018-02-05 20:23:13 +00:00
|
|
|
rcode = 0
|
2017-11-01 20:48:17 +00:00
|
|
|
if setmode:
|
2018-02-07 20:46:08 +00:00
|
|
|
if options.exclude:
|
|
|
|
sys.stderr.write('Cannot use exclude and assign at the same time\n')
|
|
|
|
sys.exit(1)
|
2017-11-02 21:07:44 +00:00
|
|
|
updatebypath = {}
|
|
|
|
attrnamebypath = {}
|
|
|
|
for key in assignment:
|
|
|
|
if key not in cfgpaths:
|
2018-02-02 22:17:02 +00:00
|
|
|
path = 'configuration/system/all'
|
|
|
|
attrib = key
|
|
|
|
else:
|
|
|
|
path, attrib = cfgpaths[key]
|
2017-11-02 21:07:44 +00:00
|
|
|
if path not in updatebypath:
|
|
|
|
updatebypath[path] = {}
|
|
|
|
attrnamebypath[path] = {}
|
|
|
|
updatebypath[path][attrib] = assignment[key]
|
|
|
|
attrnamebypath[path][attrib] = key
|
|
|
|
# well, we want to expand things..
|
|
|
|
# check ipv4, if requested change method to static
|
|
|
|
for path in updatebypath:
|
2018-02-05 20:23:13 +00:00
|
|
|
for fr in session.update('/noderange/{0}/{1}'.format(noderange, path),
|
2017-11-02 21:07:44 +00:00
|
|
|
updatebypath[path]):
|
2018-05-16 15:21:26 +00:00
|
|
|
rcode |= client.printerror(fr)
|
|
|
|
for node in fr.get('databynode', []):
|
2018-02-05 20:23:13 +00:00
|
|
|
r = fr['databynode'][node]
|
2018-05-16 15:21:26 +00:00
|
|
|
exitcode |= client.printerror(r, node)
|
2018-02-05 20:23:13 +00:00
|
|
|
if 'value' not in r:
|
|
|
|
continue
|
|
|
|
keyval = r['value']
|
2017-11-02 21:07:44 +00:00
|
|
|
key, val = keyval.split('=')
|
|
|
|
if key in attrnamebypath[path]:
|
|
|
|
key = attrnamebypath[path][key]
|
|
|
|
print('{0}: {1}: {2}'.format(node, key, val))
|
2017-11-01 20:48:17 +00:00
|
|
|
else:
|
|
|
|
for path in queryparms:
|
2018-02-06 21:31:20 +00:00
|
|
|
if options.comparedefault:
|
|
|
|
continue
|
2017-11-02 21:07:44 +00:00
|
|
|
client.print_attrib_path(path, session, list(queryparms[path]),
|
|
|
|
NullOpt(), queryparms[path])
|
2018-02-07 20:46:08 +00:00
|
|
|
if printsys or options.exclude:
|
2018-02-02 22:17:02 +00:00
|
|
|
if printsys == 'all':
|
|
|
|
printsys = []
|
|
|
|
path = '/noderange/{0}/configuration/system/all'.format(noderange)
|
|
|
|
client.print_attrib_path(path, session, printsys,
|
2018-02-06 21:31:20 +00:00
|
|
|
options)
|
2018-02-05 20:23:13 +00:00
|
|
|
sys.exit(rcode)
|