2016-05-12 15:04:26 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
2017-01-06 18:28:28 +00:00
|
|
|
# Copyright 2016-2017 Lenovo
|
2016-05-12 15:04:26 +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
|
2016-05-12 15:04:26 +00:00
|
|
|
import os
|
2017-06-20 18:56:24 +00:00
|
|
|
import signal
|
2016-05-12 15:04:26 +00:00
|
|
|
import sys
|
2017-07-26 19:54:21 +00:00
|
|
|
import time
|
2017-06-20 18:56:24 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2016-05-12 15:04:26 +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
|
2017-07-26 19:54:21 +00:00
|
|
|
import confluent.screensqueeze as sq
|
2016-05-12 15:04:26 +00:00
|
|
|
|
|
|
|
exitcode = 0
|
|
|
|
|
|
|
|
def printerror(res, node=None):
|
|
|
|
global exitcode
|
|
|
|
if 'errorcode' in res:
|
|
|
|
exitcode = res['errorcode']
|
|
|
|
if 'error' in res:
|
|
|
|
if node:
|
|
|
|
sys.stderr.write('{0}: {1}\n'.format(node, res['error']))
|
|
|
|
else:
|
|
|
|
sys.stderr.write('{0}\n'.format(res['error']))
|
|
|
|
if 'errorcode' not in res:
|
|
|
|
exitcode = 1
|
|
|
|
|
|
|
|
|
|
|
|
def printfirm(node, prefix, data):
|
|
|
|
if 'model' in data:
|
|
|
|
prefix += ' ' + data['model']
|
|
|
|
builddesc = []
|
|
|
|
if 'build' in data:
|
|
|
|
builddesc.append(data['build'])
|
|
|
|
if 'date' in data:
|
|
|
|
builddesc.append(data['date'])
|
|
|
|
if 'version' in data:
|
|
|
|
version = data['version']
|
|
|
|
if builddesc:
|
|
|
|
version += ' ({0})'.format(' '.join(builddesc))
|
|
|
|
else:
|
|
|
|
version = ' '.join(builddesc)
|
|
|
|
print('{0}: {1}: {2}'.format(node, prefix, version))
|
|
|
|
|
2018-02-14 19:38:20 +00:00
|
|
|
components = ['all']
|
2016-05-12 15:04:26 +00:00
|
|
|
|
2017-07-26 19:54:21 +00:00
|
|
|
argparser = optparse.OptionParser(
|
2018-02-14 19:38:20 +00:00
|
|
|
usage="Usage: %prog <noderange> [update [--backup <file>]]|[<components>]")
|
2017-08-16 21:33:23 +00:00
|
|
|
argparser.add_option('-b', '--backup', action='store_true',
|
|
|
|
help='Target a backup bank rather than primary')
|
2017-01-06 18:28:28 +00:00
|
|
|
(options, args) = argparser.parse_args()
|
2017-07-26 19:54:21 +00:00
|
|
|
upfile = None
|
2016-05-12 15:04:26 +00:00
|
|
|
try:
|
2017-01-06 18:28:28 +00:00
|
|
|
noderange = args[0]
|
2017-07-26 19:54:21 +00:00
|
|
|
if len(args) > 1:
|
2018-02-20 15:10:25 +00:00
|
|
|
if args[1] == 'update':
|
2018-02-14 19:38:20 +00:00
|
|
|
upfile = args[2]
|
|
|
|
else:
|
|
|
|
components = []
|
|
|
|
for arg in args[1:]:
|
|
|
|
components += arg.split(',')
|
|
|
|
|
2016-05-12 15:04:26 +00:00
|
|
|
except IndexError:
|
2017-01-06 18:28:28 +00:00
|
|
|
argparser.print_help()
|
2016-05-12 15:04:26 +00:00
|
|
|
sys.exit(1)
|
2017-11-13 16:49:40 +00:00
|
|
|
client.check_globbing(noderange)
|
2017-07-26 19:54:21 +00:00
|
|
|
|
|
|
|
def get_update_progress(session, url):
|
|
|
|
for res in session.read(url):
|
|
|
|
status = res['phase']
|
|
|
|
percent = res['progress']
|
|
|
|
detail = res['detail']
|
|
|
|
if status == 'error':
|
|
|
|
text = 'error!'
|
|
|
|
else:
|
|
|
|
text = '{0}: {1:3.0f}%'.format(status, percent)
|
|
|
|
return text, status, detail
|
|
|
|
|
|
|
|
def update_firmware(session, filename):
|
|
|
|
global exitcode
|
|
|
|
output = sq.ScreenPrinter(noderange, session)
|
|
|
|
nodeurls = {}
|
|
|
|
filename = os.path.abspath(filename)
|
|
|
|
resource = '/noderange/{0}/inventory/firmware/updates/active'.format(
|
|
|
|
noderange)
|
2017-08-16 21:33:23 +00:00
|
|
|
upargs = {'filename': filename}
|
|
|
|
if options.backup:
|
2017-09-22 13:06:42 +00:00
|
|
|
upargs['bank'] = 'backup'
|
2017-09-22 13:32:43 +00:00
|
|
|
noderrs = {}
|
2017-08-16 21:33:23 +00:00
|
|
|
for res in session.create(resource, upargs):
|
2017-09-22 13:32:43 +00:00
|
|
|
if 'created' not in res:
|
|
|
|
for nodename in res.get('databynode', ()):
|
|
|
|
output.set_output(nodename, 'error!')
|
|
|
|
noderrs[nodename] = res['databynode'][nodename].get(
|
|
|
|
'error', 'Unknown Error')
|
|
|
|
continue
|
2017-07-26 19:54:21 +00:00
|
|
|
watchurl = res['created']
|
|
|
|
currnode = watchurl.split('/')[1]
|
|
|
|
nodeurls[currnode] = '/' + watchurl
|
|
|
|
while nodeurls:
|
|
|
|
for node in list(nodeurls):
|
|
|
|
progress, status, err = get_update_progress(
|
|
|
|
session, nodeurls[node])
|
|
|
|
if status == 'error':
|
|
|
|
exitcode = 1
|
|
|
|
noderrs[node] = err
|
2017-08-14 21:10:42 +00:00
|
|
|
if status in ('error', 'complete', 'pending'):
|
2017-07-26 19:54:21 +00:00
|
|
|
list(session.delete(nodeurls[node]))
|
|
|
|
del nodeurls[node]
|
|
|
|
output.set_output(node, progress)
|
|
|
|
time.sleep(2)
|
|
|
|
allerrnodes = ','.join(noderrs)
|
2017-07-26 20:37:09 +00:00
|
|
|
if noderrs:
|
|
|
|
sys.stderr.write(
|
|
|
|
'Nodes had errors updating ({0})!\n'.format(allerrnodes))
|
2017-07-26 19:54:21 +00:00
|
|
|
for node in noderrs:
|
|
|
|
sys.stderr.write('{0}: {1}\n'.format(node, noderrs[node]))
|
|
|
|
|
|
|
|
def show_firmware(session):
|
2018-02-14 19:38:20 +00:00
|
|
|
for component in components:
|
|
|
|
for res in session.read(
|
|
|
|
'/noderange/{0}/inventory/firmware/all/{1}'.format(
|
|
|
|
noderange, component)):
|
|
|
|
printerror(res)
|
|
|
|
if 'databynode' not in res:
|
2016-05-12 15:04:26 +00:00
|
|
|
continue
|
2018-02-14 19:38:20 +00:00
|
|
|
for node in res['databynode']:
|
|
|
|
printerror(res['databynode'][node], node)
|
|
|
|
if 'firmware' not in res['databynode'][node]:
|
|
|
|
continue
|
|
|
|
for inv in res['databynode'][node]['firmware']:
|
|
|
|
for prefix in inv:
|
|
|
|
printfirm(node, prefix, inv[prefix])
|
2017-07-26 19:54:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
session = client.Command()
|
|
|
|
if upfile is None:
|
|
|
|
show_firmware(session)
|
|
|
|
else:
|
|
|
|
update_firmware(session, upfile)
|
2016-05-12 15:04:26 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print('')
|
|
|
|
sys.exit(exitcode)
|