Commit dc53aae0 authored by Kristian Feldsam's avatar Kristian Feldsam

Filtering by image id(s), datastore id(s), image label(s) and imag exclude label(s)

Signed-off-by: Kristian Feldsam's avatarKristián Feldsam <feldsam@gmail.com>
parent 6bf955b2
......@@ -14,9 +14,9 @@ parser = argparse.ArgumentParser(description='OpenNebula Backup Tool')
parser.add_argument('-i', '--image', help='Image id or comma separated list of image ids to backup. Omit for backup all images', type=functions.list_of_int_arg)
parser.add_argument('-S', '--startImage', help='Image id to start backup from. Backups all following images including defined one', type=int)
parser.add_argument('-a', '--dataStore', help='Datastore id or comma separated list of datastore ids to backup from. Omit to backup from all datastores to backup', type=functions.list_of_int_arg)
parser.add_argument('-l', '--label', help='Label or comma separated list of labels of tagged images or datastores', type=functions.list_arg)
parser.add_argument('-e', '--exclude', help='Skip (exclude) by label or comma separated list of labels of tagged images or datastores', type=functions.list_arg)
parser.add_argument('-a', '--datastore', help='Datastore id or comma separated list of datastore ids to backup from. Omit to backup from all datastores to backup', type=functions.list_of_int_arg)
parser.add_argument('-l', '--label', help='Label or comma separated list of labels of tagged images', type=functions.list_arg)
parser.add_argument('-e', '--exclude', help='Skip (exclude) by label or comma separated list of labels of tagged images', type=functions.list_arg)
parser.add_argument('-D', '--deployments', help='Backup also deployments files from system datastores', action='store_true')
parser.add_argument('-d', '--dryRun', help='Dry run - not execute any commands, all cmds will be just printed', action='store_true')
parser.add_argument('-v', '--verbose', help='Verbose mode', action='store_true')
......@@ -31,17 +31,23 @@ args = parser.parse_args()
# -----------------------
one = pyone.OneServer(config.ONE['address'], session='%s:%s' % (config.ONE['username'], config.ONE['password']))
imagePool = one.imagepool.info(-2, -1, -1)
datastores = functions.prepare_datastores(one, args)
all_images = functions.prepare_images(one, args)
images = functions.filter_images(all_images, datastores, args)
print datastores
print images
# connect and login to 3PAR
from drivers import _3par
_3par.login()
for image in imagePool.IMAGE:
dataStore = one.datastore.info(image.DATASTORE_ID)
for image_key in images:
image = images[image_key]
datastore = datastores[image.DATASTORE_ID]
# skip other datastores
if dataStore.TM_MAD != '3par':
# only datastores with 3PAR transfer manager
if datastore.TM_MAD != '3par':
continue
# persistent and attached to VM
......
......@@ -17,3 +17,108 @@ def get_vm_hostname(vm):
return vm.HISTORY_RECORDS.HISTORY.pop().HOSTNAME
return vm.HISTORY_RECORDS.HISTORY.HOSTNAME
def prepare_datastores(one, args):
datastores = {}
if args.datastore and len(args.datastore) == 1:
datastore = one.datastore.info(args.datastore[0])
datastores = {datastore.ID: datastore}
else:
datastore_pool = one.datastorepool.info()
for datastore in datastore_pool.DATASTORE:
if not args.datastore or datastore.ID in args.datastore:
datastores[datastore.ID] = datastore
return datastores
def prepare_images(one, args):
images = {}
if args.image and len(args.image) == 1:
image = one.image.info(args.image[0])
images = {image.ID: image}
else:
image_pool = one.imagepool.info(-2, -1, -1)
for image in image_pool.IMAGE:
if not args.image or image.ID in args.image:
images[image.ID] = image
return images
def filter_images(all_images, datastores, args):
# filter images by datastores only if user limit by datastores arg
if args.datastore:
images = {}
for image_key in all_images:
image = all_images[image_key]
if image.DATASTORE_ID in datastores:
images[image.ID] = image
all_images = images
# filter by labels
if args.label:
images = {}
for image_key in all_images:
image = all_images[image_key]
# image without lables, skipping
if not 'LABELS' in image.TEMPLATE:
continue
# get image labels
labels = list_arg(image.TEMPLATE['LABELS'])
# search for requested labels in image labels
for label in args.label:
if label in labels:
images[image.ID] = image
all_images = images
# exclude by labels
if args.exclude:
images = {}
for image_key in all_images:
image = all_images[image_key]
# image without lables, so not exclude
if not 'LABELS' in image.TEMPLATE:
images[image.ID] = image
continue
# get image labels
labels = list_arg(image.TEMPLATE['LABELS'])
# search for requested labels in image labels
# if label not found, add image to list
found = False
for label in args.exclude:
if label in labels:
found = True
break
# no label found, adding
if not found:
images[image.ID] = image
all_images = images
# user specified start image id, so filter out all previous images
if args.startImage:
images = {}
found = False
for image_key in all_images:
image = all_images[image_key]
if not found and image.ID == args.startImage:
found = True
if found:
images[image.ID] = image
return images
return all_images
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment