2019-09-23 14:59:00 +00:00
|
|
|
#!/usr/bin/python2
|
2018-01-03 20:35:08 +00:00
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
|
|
|
# Copyright 2018 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 optparse
|
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
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
|
|
|
|
import confluent.screensqueeze as sq
|
|
|
|
|
|
|
|
exitcode = 0
|
|
|
|
|
2018-01-04 18:39:51 +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
|
|
|
|
|
|
|
|
|
2018-01-03 20:35:08 +00:00
|
|
|
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
|
2018-01-29 16:21:46 +00:00
|
|
|
for node in res.get('databynode', ()):
|
|
|
|
printerror(res['databynode'][node], node)
|
|
|
|
|
2018-01-03 20:35:08 +00:00
|
|
|
|
|
|
|
def attach_media(noderange, media):
|
|
|
|
global exitcode
|
|
|
|
session = client.Command()
|
2018-01-24 18:23:35 +00:00
|
|
|
if ':' not in media:
|
2018-01-24 18:42:13 +00:00
|
|
|
sys.stderr.write('Full URL required for attach\n')
|
2018-01-24 18:23:35 +00:00
|
|
|
sys.exit(1)
|
2018-01-03 20:35:08 +00:00
|
|
|
resource = '/noderange/{0}/media/attach'.format(noderange)
|
2018-01-23 15:32:48 +00:00
|
|
|
for res in session.update(resource, {'url': media}):
|
2018-01-17 15:06:05 +00:00
|
|
|
printerror(res)
|
2018-01-30 20:18:20 +00:00
|
|
|
list_media(noderange, media)
|
2018-01-03 20:35:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def list_media(noderange, media):
|
|
|
|
session = client.Command()
|
|
|
|
resource = '/noderange/{0}/media/current'.format(noderange)
|
|
|
|
for res in session.read(resource):
|
|
|
|
printerror(res)
|
|
|
|
for node in res.get('databynode', []):
|
2018-01-29 19:19:25 +00:00
|
|
|
url = res['databynode'][node].get('url', None)
|
|
|
|
name = res['databynode'][node].get('name', None)
|
2019-06-11 15:06:59 +00:00
|
|
|
if (url and not url.startswith('file:') and
|
|
|
|
not res['databynode'][node].get('secure', False)):
|
2018-01-23 15:36:25 +00:00
|
|
|
name += ' (insecure)'
|
2018-01-29 19:19:25 +00:00
|
|
|
if not name:
|
|
|
|
continue
|
2018-01-04 18:21:22 +00:00
|
|
|
print('{0}: {1}'.format(node, url + '/' + name if url else name))
|
2018-01-03 20:35:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def detach_media(noderange, media):
|
|
|
|
global exitcode
|
|
|
|
session = client.Command()
|
|
|
|
resource = '/noderange/{0}/media/detach'.format(noderange)
|
2018-01-04 18:21:22 +00:00
|
|
|
for res in session.update(resource, {'detachall': 1}):
|
2018-01-03 20:35:08 +00:00
|
|
|
printerror(res)
|
|
|
|
|
|
|
|
|
|
|
|
def upload_media(noderange, media):
|
|
|
|
global exitcode
|
2018-01-29 19:36:44 +00:00
|
|
|
if not os.path.exists(media):
|
|
|
|
sys.stderr.write('Unable to locate requested file {0}\n'.format(
|
|
|
|
media))
|
|
|
|
sys.exit(404)
|
2018-01-03 20:35:08 +00:00
|
|
|
session = client.Command()
|
|
|
|
output = sq.ScreenPrinter(noderange, session)
|
|
|
|
filename = os.path.abspath(media)
|
|
|
|
resource = '/noderange/{0}/media/uploads/'.format(noderange)
|
|
|
|
upargs = {'filename': filename}
|
|
|
|
noderrs = {}
|
|
|
|
nodeurls = {}
|
|
|
|
for res in session.create(resource, upargs):
|
|
|
|
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
|
|
|
|
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
|
|
|
|
if status in ('error', 'complete', 'pending'):
|
|
|
|
list(session.delete(nodeurls[node]))
|
|
|
|
del nodeurls[node]
|
|
|
|
output.set_output(node, progress)
|
|
|
|
time.sleep(2)
|
|
|
|
allerrnodes = ','.join(noderrs)
|
|
|
|
if noderrs:
|
|
|
|
sys.stderr.write(
|
|
|
|
'Nodes had errors receiving media ({0})!\n'.format(allerrnodes))
|
|
|
|
for node in noderrs:
|
|
|
|
sys.stderr.write('{0}: {1}\n'.format(node, noderrs[node]))
|
2018-01-30 20:18:20 +00:00
|
|
|
list_media(noderange, media)
|
2018-01-03 20:35:08 +00:00
|
|
|
|
|
|
|
funmap = {
|
|
|
|
'upload': upload_media,
|
|
|
|
'attach': attach_media,
|
2018-01-22 20:56:59 +00:00
|
|
|
'detachall': detach_media,
|
2018-01-03 20:35:08 +00:00
|
|
|
'list': list_media,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-22 21:12:29 +00:00
|
|
|
class OptParser(optparse.OptionParser):
|
|
|
|
|
|
|
|
def format_epilog(self, formatter):
|
|
|
|
return self.expand_prog_name(self.epilog)
|
|
|
|
|
2018-01-03 20:35:08 +00:00
|
|
|
def main():
|
2018-01-22 21:12:29 +00:00
|
|
|
argparser = OptParser(
|
2018-01-22 20:56:59 +00:00
|
|
|
usage="Usage: %prog <noderange> [list|upload|attach|detachall] "
|
2018-01-03 20:35:08 +00:00
|
|
|
"<filename>|all|<url>",
|
2018-01-22 21:12:29 +00:00
|
|
|
epilog='\nupload will take the specified file and upload it to the '
|
|
|
|
'BMC.\n\n'
|
2018-01-03 20:35:08 +00:00
|
|
|
'attach will instruct the BMC to connect a remote media to the '
|
2018-01-22 21:12:29 +00:00
|
|
|
'specified url.\n\ndetachall will remove *ALL* uploaded and '
|
|
|
|
'attached urls from the BMC\n\nlist shows currently mounted '
|
2018-01-23 15:23:56 +00:00
|
|
|
'media.\n\nSee `man %prog` for more info.\n')
|
2018-01-03 20:35:08 +00:00
|
|
|
(options, args) = argparser.parse_args()
|
|
|
|
media = None
|
|
|
|
try:
|
|
|
|
noderange = args[0]
|
|
|
|
operation = args[1]
|
2018-01-22 20:56:59 +00:00
|
|
|
arglength = 2
|
2018-01-04 18:39:51 +00:00
|
|
|
if operation in ('attach', 'upload'):
|
2018-01-03 20:35:08 +00:00
|
|
|
media = args[2]
|
2018-01-22 20:56:59 +00:00
|
|
|
arglength = 3
|
|
|
|
if len(args) > arglength:
|
|
|
|
argparser.print_help()
|
|
|
|
sys.exit(1)
|
2018-01-03 20:35:08 +00:00
|
|
|
except IndexError:
|
|
|
|
argparser.print_help()
|
|
|
|
sys.exit(1)
|
|
|
|
client.check_globbing(noderange)
|
|
|
|
try:
|
|
|
|
handler = funmap[operation]
|
|
|
|
except KeyError:
|
|
|
|
argparser.print_help()
|
|
|
|
sys.exit(1)
|
|
|
|
handler(noderange, media)
|
2021-03-08 13:00:47 +00:00
|
|
|
|
2018-01-03 20:35:08 +00:00
|
|
|
if __name__ == '__main__':
|
2019-09-23 14:59:00 +00:00
|
|
|
main()
|
2021-03-08 13:00:47 +00:00
|
|
|
sys.exit(exitcode)
|