2019-09-23 15:04:52 +00:00
|
|
|
#!/usr/bin/python2
|
2014-04-07 20:43:39 +00:00
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
2017-01-06 18:28:28 +00:00
|
|
|
# Copyright 2015-2017 Lenovo
|
2014-04-07 20:43:39 +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.
|
|
|
|
|
2017-01-06 18:28:28 +00:00
|
|
|
import optparse
|
2014-03-05 02:09:17 +00:00
|
|
|
import os
|
2017-06-20 18:56:24 +00:00
|
|
|
import signal
|
2014-03-05 02:09:17 +00:00
|
|
|
import sys
|
|
|
|
|
2017-06-20 18:56:24 +00:00
|
|
|
try:
|
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2014-03-05 02:09:17 +00:00
|
|
|
path = os.path.dirname(os.path.realpath(__file__))
|
2015-03-19 19:06:31 +00:00
|
|
|
path = os.path.realpath(os.path.join(path, '..', 'lib', 'python'))
|
|
|
|
if path.startswith('/opt'):
|
|
|
|
sys.path.append(path)
|
2014-03-05 02:09:17 +00:00
|
|
|
|
2014-05-06 17:37:31 +00:00
|
|
|
import confluent.client as client
|
2014-03-05 02:09:17 +00:00
|
|
|
|
2017-01-06 18:28:28 +00:00
|
|
|
argparser = optparse.OptionParser(
|
2021-06-16 15:25:10 +00:00
|
|
|
usage="Usage: %prog [options] <noderange> "
|
2022-05-11 18:59:54 +00:00
|
|
|
"([status|on|off|shutdown|boot|reset|pdu_status|pdu_off|pdu_on])")
|
2018-03-05 20:41:28 +00:00
|
|
|
argparser.add_option('-p', '--showprevious', dest='previous',
|
|
|
|
action='store_true', default=False,
|
|
|
|
help='Show previous power state')
|
2019-10-23 18:20:40 +00:00
|
|
|
argparser.add_option('-m', '--maxnodes', type='int',
|
|
|
|
help='Specify a maximum number of '
|
|
|
|
'nodes to change power state, '
|
|
|
|
'prompting if over the threshold')
|
|
|
|
|
2017-01-06 18:28:28 +00:00
|
|
|
(options, args) = argparser.parse_args()
|
2015-03-25 21:19:58 +00:00
|
|
|
try:
|
2017-01-06 18:28:28 +00:00
|
|
|
noderange = args[0]
|
2015-03-25 21:19:58 +00:00
|
|
|
except IndexError:
|
2017-01-06 18:28:28 +00:00
|
|
|
argparser.print_help()
|
2015-03-25 21:19:58 +00:00
|
|
|
sys.exit(1)
|
2017-11-13 16:49:40 +00:00
|
|
|
client.check_globbing(noderange)
|
2015-03-19 19:06:31 +00:00
|
|
|
setstate = None
|
2018-07-18 15:00:01 +00:00
|
|
|
if len(args) > 1:
|
2022-05-11 20:01:43 +00:00
|
|
|
setstate = args[1]
|
2015-03-19 19:06:31 +00:00
|
|
|
if setstate == 'softoff':
|
|
|
|
setstate = 'shutdown'
|
2014-03-05 02:09:17 +00:00
|
|
|
|
2022-05-26 21:41:54 +00:00
|
|
|
if setstate not in (None, 'on', 'off', 'shutdown', 'boot', 'reset', 'pdu_status', 'pdu_stat', 'pdu_on', 'pdu_off', 'status', 'stat', 'state'):
|
2017-06-20 18:56:24 +00:00
|
|
|
argparser.print_help()
|
|
|
|
sys.exit(1)
|
2014-03-05 02:09:17 +00:00
|
|
|
session = client.Command()
|
2015-03-19 19:06:31 +00:00
|
|
|
exitcode = 0
|
2017-02-02 22:00:09 +00:00
|
|
|
session.add_precede_key('oldstate')
|
2022-05-11 18:59:54 +00:00
|
|
|
powurl = 'state'
|
|
|
|
if setstate and setstate.startswith('pdu_'):
|
|
|
|
setstate = setstate.replace('pdu_', '')
|
|
|
|
powurl = 'inlets/all'
|
|
|
|
if setstate in ('status', 'state', 'stat'):
|
|
|
|
setstate = None
|
2018-03-05 20:41:28 +00:00
|
|
|
|
|
|
|
if options.previous:
|
|
|
|
# get previous states
|
|
|
|
prev = {}
|
2022-05-11 18:59:54 +00:00
|
|
|
for rsp in session.read("/noderange/{0}/power/{1}".format(noderange, powurl)):
|
2018-03-05 20:41:28 +00:00
|
|
|
# gets previous (current) states
|
|
|
|
|
|
|
|
databynode = rsp["databynode"]
|
|
|
|
|
|
|
|
for node in databynode:
|
|
|
|
prev[node] = databynode[node]["state"]["value"]
|
|
|
|
|
|
|
|
# add dictionary to session
|
|
|
|
session.add_precede_dict(prev)
|
|
|
|
|
2022-05-11 20:01:43 +00:00
|
|
|
def outhandler(node, res):
|
|
|
|
for k in res[node]:
|
|
|
|
client.cprint('{0}: {1}: {2}'.format(node, k.replace('inlet_', ''), res[node][k]))
|
|
|
|
sys.exit(session.simple_noderange_command(noderange, '/power/{0}'.format(powurl), setstate, promptover=options.maxnodes, key='state', outhandler=outhandler))
|