2018-10-30 19:46:54 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# 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
|
|
|
|
|
|
|
|
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
|
|
|
|
exitcode = 0
|
|
|
|
|
|
|
|
class OptParser(optparse.OptionParser):
|
|
|
|
|
|
|
|
def format_epilog(self, formatter):
|
|
|
|
return self.expand_prog_name(self.epilog)
|
|
|
|
|
2018-11-01 14:43:28 +00:00
|
|
|
def mbtohuman(mb):
|
|
|
|
if mb > 1000000:
|
|
|
|
return '{0:.3f} TB'.format(mb/1000000.0)
|
|
|
|
if mb > 1000:
|
|
|
|
return '{0:.3f} GB'.format(mb/1000.0)
|
|
|
|
return '{0:.3f} MB'.format(mb)
|
2018-10-30 19:46:54 +00:00
|
|
|
|
|
|
|
def showstorage(noderange, options, args):
|
|
|
|
global exitcode
|
|
|
|
session = client.Command()
|
|
|
|
disks = {}
|
|
|
|
arrays = {}
|
|
|
|
volumes = {}
|
|
|
|
scfg = session.read('/noderange/{0}/configuration/storage/all'.format(
|
|
|
|
noderange))
|
2018-11-16 16:27:26 +00:00
|
|
|
_print_cfg(scfg)
|
|
|
|
|
|
|
|
|
|
|
|
def _print_cfg(scfg):
|
|
|
|
global exitcode
|
|
|
|
storagebynode = {}
|
2018-10-30 19:46:54 +00:00
|
|
|
for e in scfg:
|
|
|
|
if 'error' in e:
|
|
|
|
sys.stderr.write(e['error'] + '\n')
|
|
|
|
exitcode = e.get('errorcode', 1)
|
|
|
|
for node in e.get('databynode', {}):
|
|
|
|
if node not in storagebynode:
|
2018-11-16 16:27:26 +00:00
|
|
|
storagebynode[node] = {'disks': [], 'arrays': [],
|
|
|
|
'volumes': []}
|
2018-10-30 19:46:54 +00:00
|
|
|
curr = e['databynode'][node]
|
|
|
|
storagebynode[node][curr['type'] + 's'].append(curr)
|
|
|
|
for node in storagebynode:
|
2018-11-01 14:43:28 +00:00
|
|
|
for disk in sorted(storagebynode[node]['disks'],
|
|
|
|
key=lambda x: x['name']):
|
|
|
|
print('{0}: Disk {1} Description: {2}'.format(
|
|
|
|
node, disk['name'], disk['description']))
|
|
|
|
print('{0}: Disk {1} State: {2}'.format(node, disk['name'],
|
2018-11-16 16:27:26 +00:00
|
|
|
disk['state']))
|
2018-11-01 14:43:28 +00:00
|
|
|
print('{0}: Disk {1} FRU: {2}'.format(node, disk['name'],
|
|
|
|
disk['fru']))
|
|
|
|
print('{0}: Disk {1} Serial Number: {2}'.format(node, disk['name'],
|
2018-11-16 16:27:26 +00:00
|
|
|
disk['serial']))
|
2018-11-01 14:43:28 +00:00
|
|
|
if disk['array']:
|
|
|
|
print('{0}: Disk {1} Array: {2}'.format(node, disk['name'],
|
|
|
|
disk['array']))
|
|
|
|
for arr in storagebynode[node]['arrays']:
|
|
|
|
print('{0}: Array {1} Available Capacity: {2}'.format(
|
|
|
|
node, arr['id'], mbtohuman(arr['available'])))
|
|
|
|
print('{0}: Array {1} Total Capacity: {2}'.format(
|
|
|
|
node, arr['id'], mbtohuman(arr['capacity'])))
|
|
|
|
print('{0}: Array {1} RAID: {2}'.format(node, arr['id'],
|
|
|
|
arr['raid']))
|
|
|
|
print('{0}: Array {1} Disks: {2}'.format(node, arr['id'], ','.join(
|
|
|
|
arr['disks'])))
|
2018-11-16 16:27:26 +00:00
|
|
|
print(
|
|
|
|
'{0}: Array {1} Volumes: {2}'.format(node, arr['id'], ','.join(
|
|
|
|
arr['volumes'])))
|
2018-11-01 14:43:28 +00:00
|
|
|
for vol in storagebynode[node]['volumes']:
|
|
|
|
print('{0}: Volume {1}: Size: {2}'.format(node, vol['name'],
|
2018-11-16 16:27:26 +00:00
|
|
|
mbtohuman(vol['size'])))
|
2018-11-01 14:43:28 +00:00
|
|
|
print('{0}: Volume {1}: State: {2}'.format(node, vol['name'],
|
|
|
|
vol['state']))
|
|
|
|
print('{0}: Volume {1}: Array {2}'.format(node, vol['name'],
|
|
|
|
vol['array']))
|
2018-10-30 19:46:54 +00:00
|
|
|
|
2018-11-16 16:27:26 +00:00
|
|
|
|
2018-10-30 19:46:54 +00:00
|
|
|
def createstorage(noderange, options, args):
|
2018-11-01 20:59:42 +00:00
|
|
|
if options.raidlevel is None or options.disks is None:
|
|
|
|
sys.stderr.write('-r and -d are required arguments to create array\n')
|
|
|
|
sys.exit(1)
|
2018-10-30 19:46:54 +00:00
|
|
|
session = client.Command()
|
2018-11-01 20:59:42 +00:00
|
|
|
names = options.name
|
|
|
|
if names is None:
|
2018-11-19 20:32:03 +00:00
|
|
|
names = ''.join(args)
|
2018-11-15 21:25:03 +00:00
|
|
|
parms = {'disks': options.disks, 'raidlevel': options.raidlevel,
|
2018-11-16 16:27:26 +00:00
|
|
|
'name': names}
|
2018-11-01 20:59:42 +00:00
|
|
|
if options.size:
|
|
|
|
parms['size'] = options.size
|
2018-11-16 16:27:26 +00:00
|
|
|
_print_cfg(session.create(
|
2018-11-01 20:59:42 +00:00
|
|
|
'/noderange/{0}/configuration/storage/volumes/{1}'.format(
|
2018-11-16 16:27:26 +00:00
|
|
|
noderange, names), parms))
|
2018-11-01 20:59:42 +00:00
|
|
|
|
2018-10-30 19:46:54 +00:00
|
|
|
|
|
|
|
def deletestorage(noderange, options, args):
|
2018-11-01 20:59:42 +00:00
|
|
|
if options.name is None:
|
|
|
|
if len(args) == 1:
|
|
|
|
names = args[0]
|
|
|
|
else:
|
|
|
|
sys.stderr.write('-n is required to indicate volume(s) to delete\n')
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
names = options.name
|
|
|
|
session = client.Command()
|
|
|
|
for rsp in session.delete(
|
|
|
|
'/noderange/{0}/configuration/storage/volumes/{1}'.format(
|
|
|
|
noderange, names)):
|
2018-11-15 21:18:24 +00:00
|
|
|
if 'deleted' in rsp:
|
|
|
|
print('Deleted: {0}'.format(rsp['deleted']))
|
2018-11-16 16:54:46 +00:00
|
|
|
elif 'databynode' in rsp:
|
|
|
|
for node in rsp['databynode']:
|
|
|
|
if 'error' in rsp['databynode'][node]:
|
|
|
|
sys.stderr.write('{0}: {1}\n'.format(
|
|
|
|
node, rsp['databynode'][node]['error']))
|
|
|
|
else:
|
|
|
|
sys.stderr.write('{0}: {1}\n'.format(
|
|
|
|
node, repr(rsp['databynode'][node])))
|
2018-11-15 21:18:24 +00:00
|
|
|
else:
|
|
|
|
print(repr(rsp))
|
2018-11-01 20:59:42 +00:00
|
|
|
|
2018-10-30 19:46:54 +00:00
|
|
|
|
|
|
|
def setstorage(noderange, options, args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
funmap = {
|
|
|
|
'create': createstorage,
|
|
|
|
'show': showstorage,
|
|
|
|
'set': setstorage,
|
|
|
|
'delete': deletestorage,
|
|
|
|
'rm': deletestorage,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argparser = OptParser(
|
|
|
|
usage='Usage: %prog <noderange> [show|create|set|delete]',
|
|
|
|
epilog='',
|
|
|
|
)
|
|
|
|
argparser.add_option('-r', '--raidlevel', type='int',
|
|
|
|
help='RAID level to use when creating an array')
|
|
|
|
argparser.add_option('-d', '--disks', type='str',
|
|
|
|
help='Comma separated list of disks to use, or the '
|
|
|
|
'word "rest" to indicate use of all available '
|
|
|
|
'disks')
|
|
|
|
argparser.add_option('-s', '--size', type='str',
|
|
|
|
help='Comma separated list of sizes to use when '
|
|
|
|
'creating volumes. The sizes may be absolute '
|
|
|
|
'size (e.g. 16gb), percentage (10%) or the word '
|
|
|
|
'"rest" to use remaining capacity, default '
|
|
|
|
'behavior is to use all capacity to make a '
|
|
|
|
'volume')
|
|
|
|
argparser.add_option('-n', '--name', type='str',
|
|
|
|
help='Comma separated list of names to use when '
|
|
|
|
'naming volumes, or selecting a volume for '
|
|
|
|
'delete. Default behavior is to use '
|
|
|
|
'implementation provided default names.')
|
|
|
|
(options, args) = argparser.parse_args()
|
2019-01-18 19:54:55 +00:00
|
|
|
if len(args) == 1:
|
|
|
|
args.append('show')
|
2018-10-30 19:46:54 +00:00
|
|
|
try:
|
|
|
|
noderange = args[0]
|
|
|
|
operation = args[1]
|
|
|
|
except IndexError:
|
|
|
|
argparser.print_help()
|
|
|
|
sys.exit(1)
|
|
|
|
client.check_globbing(noderange)
|
|
|
|
try:
|
|
|
|
handler = funmap[operation]
|
|
|
|
except KeyError:
|
|
|
|
argparser.print_help()
|
|
|
|
sys.exit(1)
|
2018-11-01 20:59:42 +00:00
|
|
|
handler(noderange, options, args[2:])
|
2018-10-30 19:46:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-01-18 19:54:55 +00:00
|
|
|
main()
|