2019-09-23 15:04:52 +00:00
|
|
|
#!/usr/bin/python2
|
2017-02-17 15:44:25 +00:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
__author__ = 'alin37'
|
|
|
|
|
2018-09-04 13:38:01 +00:00
|
|
|
from getpass import getpass
|
2017-02-17 15:44:25 +00:00
|
|
|
import optparse
|
|
|
|
import os
|
2017-06-20 18:56:24 +00:00
|
|
|
import signal
|
2017-02-17 15:44:25 +00:00
|
|
|
import sys
|
2023-10-05 15:44:04 +00:00
|
|
|
import shlex
|
2017-02-17 15:44:25 +00:00
|
|
|
|
2017-06-20 18:56:24 +00:00
|
|
|
try:
|
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2017-02-17 15:44:25 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
argparser = optparse.OptionParser(
|
2018-07-09 20:49:45 +00:00
|
|
|
usage='''\n %prog [-b] noderange [list of attributes or 'all'] \
|
2017-06-20 18:56:24 +00:00
|
|
|
\n %prog -c noderange <list of attributes> \
|
2018-02-22 14:55:46 +00:00
|
|
|
\n %prog -e noderange <attribute names to set> \
|
2017-06-20 18:56:24 +00:00
|
|
|
\n %prog noderange attribute1=value1 attribute2=value,...
|
2017-02-28 18:27:22 +00:00
|
|
|
\n ''')
|
2017-02-17 15:44:25 +00:00
|
|
|
argparser.add_option('-b', '--blame', action='store_true',
|
|
|
|
help='Show information about how attributes inherited')
|
2018-02-22 14:55:46 +00:00
|
|
|
argparser.add_option('-e', '--environment', action='store_true',
|
|
|
|
help='Set attributes, but from environment variable of '
|
|
|
|
'same name')
|
2017-02-28 18:27:22 +00:00
|
|
|
argparser.add_option('-c', '--clear', action='store_true',
|
2017-06-20 18:56:24 +00:00
|
|
|
help='Clear attributes')
|
2018-09-04 13:38:01 +00:00
|
|
|
argparser.add_option('-p', '--prompt', action='store_true',
|
|
|
|
help='Prompt for attribute values interactively')
|
2019-10-23 18:20:40 +00:00
|
|
|
argparser.add_option('-m', '--maxnodes', type='int',
|
|
|
|
help='Prompt if trying to set attributes on more '
|
|
|
|
'than specified number of nodes')
|
2023-09-20 18:29:21 +00:00
|
|
|
argparser.add_option('-s', '--set', dest='set', metavar='settings.batch',
|
|
|
|
default=False, help='set attributes using a batch file')
|
2017-02-17 15:44:25 +00:00
|
|
|
(options, args) = argparser.parse_args()
|
|
|
|
|
2017-04-06 15:30:08 +00:00
|
|
|
|
|
|
|
#setting minimal output to only output current information
|
2017-02-17 15:46:57 +00:00
|
|
|
showtype = 'current'
|
2017-02-17 15:44:25 +00:00
|
|
|
requestargs=None
|
|
|
|
try:
|
|
|
|
noderange = args[0]
|
|
|
|
nodelist = '/noderange/{0}/nodes/'.format(noderange)
|
|
|
|
except IndexError:
|
2017-06-20 18:56:24 +00:00
|
|
|
argparser.print_help()
|
|
|
|
sys.exit(1)
|
2017-11-13 16:49:40 +00:00
|
|
|
client.check_globbing(noderange)
|
2017-02-17 15:44:25 +00:00
|
|
|
session = client.Command()
|
|
|
|
exitcode = 0
|
|
|
|
|
2017-02-28 18:27:22 +00:00
|
|
|
#Sets attributes
|
2017-04-06 15:30:08 +00:00
|
|
|
nodetype="noderange"
|
|
|
|
|
2017-02-17 15:44:25 +00:00
|
|
|
if len(args) > 1:
|
2018-09-04 13:38:01 +00:00
|
|
|
if "=" in args[1] or options.clear or options.environment or options.prompt:
|
2017-10-19 14:02:30 +00:00
|
|
|
if "=" in args[1] and options.clear:
|
|
|
|
print("Can not clear and set at the same time!")
|
|
|
|
argparser.print_help()
|
|
|
|
sys.exit(1)
|
2018-09-04 13:38:01 +00:00
|
|
|
argassign = None
|
|
|
|
if options.prompt:
|
|
|
|
argassign = {}
|
|
|
|
for arg in args[1:]:
|
|
|
|
oneval = 1
|
|
|
|
twoval = 2
|
|
|
|
while oneval != twoval:
|
|
|
|
oneval = getpass('Enter value for {0}: '.format(arg))
|
|
|
|
twoval = getpass('Confirm value for {0}: '.format(arg))
|
|
|
|
if oneval != twoval:
|
|
|
|
print('Values did not match.')
|
|
|
|
argassign[arg] = twoval
|
2019-10-23 19:57:16 +00:00
|
|
|
session.stop_if_noderange_over(noderange, options.maxnodes)
|
2018-09-04 13:38:01 +00:00
|
|
|
exitcode=client.updateattrib(session,args,nodetype, noderange, options, argassign)
|
2017-04-06 15:30:08 +00:00
|
|
|
try:
|
|
|
|
# setting user output to what the user inputs
|
|
|
|
if args[1] == 'all':
|
2017-02-28 18:27:22 +00:00
|
|
|
showtype = 'all'
|
2017-04-12 14:36:27 +00:00
|
|
|
requestargs=args[2:]
|
2017-02-28 18:27:22 +00:00
|
|
|
elif args[1] == 'current':
|
|
|
|
showtype = 'current'
|
2017-04-12 14:36:27 +00:00
|
|
|
requestargs=args[2:]
|
|
|
|
else:
|
2017-06-20 18:56:24 +00:00
|
|
|
showtype = 'all'
|
2017-04-12 14:36:27 +00:00
|
|
|
requestargs=args[1:]
|
2017-04-06 15:30:08 +00:00
|
|
|
except:
|
|
|
|
pass
|
2022-02-14 14:24:15 +00:00
|
|
|
elif options.clear or options.environment or options.prompt:
|
|
|
|
sys.stderr.write('Attribute names required with specified options\n')
|
|
|
|
argparser.print_help()
|
|
|
|
exitcode = 400
|
2023-09-20 18:29:21 +00:00
|
|
|
|
|
|
|
elif options.set:
|
|
|
|
arglist = [noderange]
|
|
|
|
showtype='current'
|
|
|
|
argfile = open(options.set, 'r')
|
|
|
|
argset = argfile.readline()
|
|
|
|
while argset:
|
|
|
|
try:
|
|
|
|
argset = argset[:argset.index('#')]
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
argset = argset.strip()
|
|
|
|
if argset:
|
|
|
|
arglist += shlex.split(argset)
|
|
|
|
argset = argfile.readline()
|
|
|
|
session.stop_if_noderange_over(noderange, options.maxnodes)
|
|
|
|
exitcode=client.updateattrib(session,arglist,nodetype, noderange, options, None)
|
2017-04-06 15:30:08 +00:00
|
|
|
if exitcode != 0:
|
|
|
|
sys.exit(exitcode)
|
2017-02-17 15:44:25 +00:00
|
|
|
|
2017-02-28 18:27:22 +00:00
|
|
|
# Lists all attributes
|
2017-02-17 15:44:25 +00:00
|
|
|
if len(args) > 0:
|
2017-04-06 15:30:08 +00:00
|
|
|
# setting output to all so it can search since if we do have something to search, we want to show all outputs even if it is blank.
|
|
|
|
if requestargs is None:
|
|
|
|
showtype = 'current'
|
|
|
|
elif requestargs == []:
|
|
|
|
#showtype already set
|
|
|
|
pass
|
2017-04-12 14:36:27 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
requestargs.remove('all')
|
|
|
|
requestargs.remove('current')
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
exitcode = client.printattributes(session, requestargs, showtype,nodetype, noderange, options)
|
2017-02-17 15:44:25 +00:00
|
|
|
else:
|
|
|
|
for res in session.read(nodelist):
|
|
|
|
if 'error' in res:
|
|
|
|
sys.stderr.write(res['error'] + '\n')
|
|
|
|
exitcode = 1
|
|
|
|
else:
|
2019-06-27 19:28:59 +00:00
|
|
|
print(res['item']['href'].replace('/', ''))
|
2017-04-06 15:30:08 +00:00
|
|
|
|
2019-09-23 15:04:52 +00:00
|
|
|
sys.exit(exitcode)
|