def convert_volumes_to_vm_images_by_vm_ids(destination, id_file): uuids = utils.read_ids_from_file(id_file) for uuid in uuids: volume = get_image_volume_by_vm_id(destination, uuid) print volume upload_volume_to_image_by_vm(destination, volume) print "converted volume to image"
def upload_single_volumes_to_image(destination, uuid_file): ids = utils.read_ids_from_file(uuid_file) # volumes = get_single_volumes(destination) for vol in ids: print "Creating image from volume, volume id:", vol upload_volume_to_image_by_volume_id(destination, vol, single=True)
def migrate_vms_from_image_with_network_mapping(id_file): ids = utils.read_ids_from_file(id_file) nova_from = get_nova("from") to_vms = get_vm_list('to') for uuid in ids: try: server = nova_from.servers.get(uuid) if server.status == 'SHUTOFF': print "Finding image for server with UUID:", uuid new_name = "migration_vm_image_" + server.id # print new_name image = glance_common.get_image_by_name("to", new_name) if image: print "Found image with name: ", image.name # need to check for VMs that were already re-created on the TO side: dup_vms = filter(lambda to_vms: to_vms.name == server.name, to_vms) duplicate = False for dup in dup_vms: if dup.metadata['original_vm_id'] == server.id: print "Duplicate VM on TO side already found, skipping VM:", server.name, server.id duplicate = True if duplicate is False: create_vm(server, image=image) else: print "Did not find image in 'to' environment with name:", new_name else: print "1 Server with UUID:", uuid, " is not shutoff. It must be in SHUTOFF status for this action." except nova_exc.NotFound: print "2 Server with UUID", uuid, "not found"
def convert_volumes_to_vm_images_by_vm_ids(destination, id_file): uuids = utils.read_ids_from_file(id_file) for uuid in uuids: volume = get_image_volume_by_vm_id(destination, uuid) print volume upload_volume_to_image_by_vm(destination, volume) print "converted volume to image"
def get_users_from_name_list(destination, user_name_file): names = utils.read_ids_from_file(user_name_file) users = [] for name in names: user = get_user_by_name(destination, name) users.append(user) return users
def create_image_from_vm(destination, id_file): ids = utils.read_ids_from_file(id_file) nova = get_nova(destination) for uuid in ids: try: server = nova.servers.get(uuid) if server.status != 'SHUTOFF': print "Server", server.name, "is not shut off." print "All servers in the migration ID file must be turned off for image creation to start." return except nova_exc.NotFound: print "6 Server with UUID", uuid, "not found" for uuid in ids: try: server = nova.servers.get(uuid) if server.status == 'SHUTOFF': print "Making image from server with UUID:", uuid new_name = "migration_vm_image_" + server.id metadata = {} metadata.update({'original_vm_id': server.id}) metadata.update({'original_vm_name': server.name}) print new_name server.create_image(new_name, metadata) else: print "7 Server with UUID:", uuid, " is not shutoff. It must be in SHUTOFF status for this action." # if server.__dict__['os-extended-volumes:volumes_attached']: # print "Creating image from volume attached to the VM, volume id:" # volumes = server.__dict__['os-extended-volumes:volumes_attached'] # for vol in volumes: # print vol['id'] # cinder_common.upload_volume_to_image_by_volume_id(destination, vol['id']) except nova_exc.NotFound: print "8 Server with UUID", uuid, "not found"
def make_volumes_from_snapshots(destination, id_file): vms = utils.read_ids_from_file(id_file) for vm in vms: volumes = get_volumes_for_vm("from", vm) for vol in volumes: snap = cinder_common.get_snapshot_by_volume_id(destination, vol.id) new_volume = cinder_common.make_volume_from_snapshot(destination, vol.id, snap)
def boot_from_vms_from_image_with_network_mapping(id_file, custom_network='none', key='default', user_data='default'): ids = utils.read_ids_from_file(id_file) nova_from = get_nova("from") to_vms = get_vm_list('to') for uuid in ids: try: server = nova_from.servers.get(uuid) if server.status == 'SHUTOFF': image_name = "migration_vm_image_" + server.id image = glance_common.get_image_by_name('to', image_name) # # need to check for VMs that were already re-created on the TO side: dup_vms = filter(lambda to_vms: to_vms.name == server.name, to_vms) duplicate = False for dup in dup_vms: if 'original_vm_id' in dup.metadata: if dup.metadata['original_vm_id'] == server.id: print "Duplicate VM on TO side already found, skipping VM:", server.name, server.id duplicate = True if duplicate is False: create_vm_from_image_with_network_mapping(server, image, network_name=custom_network, key=key, user_data=user_data) else: print "1 Server with UUID:", uuid, " is not shutoff. It must be in SHUTOFF status for this action." except nova_exc.NotFound: print "2 Server with UUID", uuid, "not found"
def boot_from_volume_vms_with_network_mapping(id_file, custom_network='none', key='default', user_data='default'): ids = utils.read_ids_from_file(id_file) nova_from = get_nova("from") to_vms = get_vm_list('to') for uuid in ids: try: server = nova_from.servers.get(uuid) # if server.status == 'SHUTOFF': if server: volumes = cinder_common.get_volume_list_by_vm_id('to', uuid) boot_volume = None for vol in volumes: if vol.name.startswith('migration_vm_image_'): boot_volume = vol if boot_volume: dup_vms = filter(lambda to_vms: to_vms.name == server.name, to_vms) duplicate = False for dup in dup_vms: if 'original_vm_id' in dup.metadata: if dup.metadata['original_vm_id'] == server.id: print "Duplicate VM on TO side already found, skipping VM:", server.name, server.id duplicate = True if duplicate is False: create_vm_from_volume_with_network_mapping_no_original_device(server, volume=boot_volume, network_name=custom_network, key=key, user_data=user_data) else: print 'boot volume not found' # else: # print "1 Server with UUID:", uuid, " is not shutoff. It must be in SHUTOFF status for this action." except nova_exc.NotFound: print "2 Server with UUID", uuid, "not found"
def get_users_from_name_list(destination, user_name_file): names = utils.read_ids_from_file(user_name_file) users = [] for name in names: user = get_user_by_name(destination, name) users.append(user) return users
def upload_single_volumes_to_image(destination, uuid_file): ids = utils.read_ids_from_file(uuid_file) # volumes = get_single_volumes(destination) for vol in ids: print "Creating image from volume, volume id:", vol upload_volume_to_image_by_volume_id(destination, vol, single=True)
def create_images(path, uuid_file): ids = utils.read_ids_from_file(uuid_file) for uuid in ids: filename = path + uuid image = get_image('from', uuid) if image.status == 'active': print "Uploading image name:", image.name image_create('to', image, filename)
def make_images_of_volumes_based_on_vms(destination, id_file): vms = utils.read_ids_from_file(id_file) for vm in vms: volumes = cinder_common.get_volume_list_by_vm_id(destination, vm) for vol in volumes: name = vol.metadata['original_volume_id'] print "original volume name " + name + ", snapshot volume id: " + vol.id cinder_common.upload_volume_to_image_by_volume_name(destination, vol, name)
def create_images(path, uuid_file): ids = utils.read_ids_from_file(uuid_file) for uuid in ids: filename = path + uuid image = get_image('from', uuid) if image.status == 'active': print "Uploading image name:", image.name image_create('to', image, filename)
def download_single_volumes(destination, path, id_file): volumes = utils.read_ids_from_file(id_file) # vols = get_single_volumes(destination) # volumes = map(lambda vols: vols.id, vols) glance_common.download_images_by_volume_uuid(destination, path, volumes, single=True)
def upload_images_by_vm_uuid(path, uuid_file): ids = utils.read_ids_from_file(uuid_file) for uuid in ids: image_name = "migration_vm_image_" + uuid filename = path + image_name image = get_image_by_name('from', image_name) print "Uploading image name:", image_name image_create('to', image, filename)
def set_allowed_pairs_for_vms(id_file): vms = utils.read_ids_from_file(id_file) ports = get_ports('from') for port in ports: if port['allowed_address_pairs']: if port['device_id'] in vms: print "Found allowed pair for VM UUID: " + port['device_id'] print port['allowed_address_pairs'] add_allowed_pairs_to_port(port['fixed_ips'], port['allowed_address_pairs'])
def get_from_vm_list_by_ids(id_file): all_vms = get_vm_list('from') ids = utils.read_ids_from_file(id_file) vms = [] for vm in all_vms: for id in ids: if vm.id == id: vms.append(vm) return vms
def upload_images_by_vm_uuid(path, uuid_file): ids = utils.read_ids_from_file(uuid_file) for uuid in ids: image_name = "migration_vm_image_" + uuid filename = path + image_name image = get_image_by_name('from', image_name) print "Uploading image name:", image_name image_create('to', image, filename)
def prepare_migrate_vms_from_image_snapshot(id_file): ids = utils.read_ids_from_file(id_file) ready = check_vm_are_off("from", id_file) if ready: for vm_uuid in ids: volumes = get_volumes_for_vm("from", vm_uuid) for v in volumes: cinder_common.create_volume_snapshot("from", v, vm_uuid) else: print "Please make sure that all migration VMs are powered off."
def upload_images_by_url(uuid_file): ids = utils.read_ids_from_file(uuid_file) auth = AuthStack() from_path = auth.from_nfs_glance_location for uuid in ids: image_name = "migration_vm_image_" + uuid image = get_image_by_name('from', image_name) filename = from_path + image.id print "Uploading image name:", image_name start_image_create_process('to', image, filename)
def upload_images_by_url(uuid_file): ids = utils.read_ids_from_file(uuid_file) auth = AuthStack() from_path = auth.from_nfs_glance_location for uuid in ids: image_name = "migration_vm_image_" + uuid image = get_image_by_name('from', image_name) filename = from_path + image.id print "Uploading image name:", image_name start_image_create_process('to', image, filename)
def print_allowed_pairs_for_vms(destination, id_file): vms = utils.read_ids_from_file(id_file) ports = get_ports(destination) for port in ports: if port['allowed_address_pairs']: if port['device_id'] in vms: print "Port for allowed pair for VM UUID: " + port['device_id'] # print port['allowed_address_pairs'] print port['id'], port['device_owner'], port['fixed_ips'] for pair in port['allowed_address_pairs']: print " " + pair['ip_address']
def get_ips_for_vms(destination, id_file): nova = nova_common.get_nova(destination) vms = utils.read_ids_from_file(id_file) ips = [] for vm in vms: interfaces = nova.servers.interface_list(vm) for interface in interfaces: for ip in interface.fixed_ips: # print ip['ip_address'] ips.append(ip['ip_address']) return ips
def copy_by_net_name(net_id_file): # from_network = find_from_net_by_network_name(network_name) ids = utils.read_ids_from_file(net_id_file) for uuid in ids: from_network = get_network_by_uuid('from', uuid) print "Copying network:" print from_network new_network = network_create_net('to', from_network) print "New network created: " print new_network create_subnets(from_network['id'], new_network['network']['id'], new_network['network']['tenant_id'])
def get_to_vm_list_by_original_ids(id_file): all_vms = get_vm_list('to') ids = utils.read_ids_from_file(id_file) vms = [] for vm in all_vms: for original_id in ids: if 'original_vm_id' in vm.metadata: original_vm_id = vm.metadata['original_vm_id'] if original_vm_id == original_id: vms.append(vm) return vms
def get_images_by_vm_id(destination, uuid_file): ids = utils.read_ids_from_file(uuid_file) vm_images = [] for vm in ids: name = "migration_vm_image_" + vm print "name", name img = get_image_by_name(destination, name) if img: vm_images.append(img) else: print "image not found: ", name return vm_images
def get_images_by_vm_id(destination, uuid_file): ids = utils.read_ids_from_file(uuid_file) vm_images = [] for vm in ids: name = "migration_vm_image_" + vm print "name", name img = get_image_by_name(destination, name) if img: vm_images.append(img) else: print "image not found: ", name return vm_images
def power_on_vms(destination, id_file): ids = utils.read_ids_from_file(id_file) nova = get_nova(destination) for uuid in ids: try: server = nova.servers.get(uuid) if server.status == 'SHUTOFF': print "Powering on server with UUID:", uuid server.start() else: print "Server with UUID:", uuid, "is running. It must be in SHUTOFF status for this action." except nova_exc.NotFound: print "14 Server with UUID", uuid, "not found"
def check_vm_are_on(destination, id_file): ids = utils.read_ids_from_file(id_file) # nova = get_nova(destination) ready = True for uuid in ids: try: server = get_vm_by_original_id('to', uuid) # server = nova.servers.get(uuid) if server.status != 'ACTIVE': print "Server", server.name, "is not ACTIVE." ready = False except Exception, e: print "Server with UUID", uuid, "not found when trying to check that it is powered on"
def power_off_vms(destination, id_file): ids = utils.read_ids_from_file(id_file) nova = get_nova(destination) for uuid in ids: try: server = nova.servers.get(uuid) if server.status == 'ACTIVE': print "Shutting down server with UUID:", uuid server.stop() else: print "3 Server with UUID:", uuid, "is not running. It must be in ACTIVE status for this action." except nova_exc.NotFound: print "4 Server with UUID", uuid, "not found"
def check_vm_are_on(destination, id_file): ids = utils.read_ids_from_file(id_file) # nova = get_nova(destination) ready = True for uuid in ids: try: server = get_vm_by_original_id('to', uuid) # server = nova.servers.get(uuid) if server.status != 'ACTIVE': print "Server", server.name, "is not ACTIVE." ready = False except nova_exc.NotFound: print "5 Server with UUID", uuid, "not found" return ready
def check_vm_are_off(destination, id_file): ids = utils.read_ids_from_file(id_file) nova = get_nova(destination) ready = True for uuid in ids: try: server = nova.servers.get(uuid) if server.status != 'SHUTOFF': print "Server", server.name, "is not shut off." print "All servers in the migration ID file must be turned off for image creation to start." ready = False except nova_exc.NotFound: print "15 Server with UUID", uuid, "not found" return ready
def download_images(destination, path, uuid_file): ids = utils.read_ids_from_file(uuid_file) if os.access(os.path.dirname(path), os.W_OK): for uuid in ids: i = get_image(destination, uuid=uuid) if i.name.startswith('migration_vm_image_') or i.name.startswith('migration_volume_image_'): continue else: if i.status == 'active': image_download(i.id, path) else: print "Image with this id is not available for downloads: " + i.id else: print "Invalid directory provided"
def get_volume_id_list_for_vm_ids(destination, id_file): ids = utils.read_ids_from_file(id_file) nova = get_nova(destination) volume_ids = [] for uuid in ids: try: server = nova.servers.get(uuid) if len(server.__dict__['os-extended-volumes:volumes_attached']) > 0: volumes = server.__dict__['os-extended-volumes:volumes_attached'] for vol in volumes: # print vol['id'] volume_ids.append(vol['id']) except nova_exc.NotFound: print "9 Server with UUID", uuid, "not found" return volume_ids
def download_images(destination, path, uuid_file): ids = utils.read_ids_from_file(uuid_file) if os.access(os.path.dirname(path), os.W_OK): for uuid in ids: i = get_image(destination, uuid=uuid) if i.name.startswith('migration_vm_image_') or i.name.startswith('migration_volume_image_'): continue else: if i.status == 'active': image_download(i.id, path) else: print "Image with this id is not available for downloads: " + i.id else: print "Invalid directory provided"
def download_images_by_vm_uuid(destination, path, uuid_file): ids = utils.read_ids_from_file(uuid_file) ready = True for uuid in ids: image_name = "migration_vm_image_" + uuid image = get_image_by_name(destination, image_name) if image.status != "active": print "Image", image.name, "is not in active status. All migration images must be active before proceeding" ready = False return ready for uuid in ids: image_name = "migration_vm_image_" + uuid print "Downloading image name:", image_name image = get_image_by_name(destination, image_name) image_download(image.id, path, fname=image_name) return ready
def upload_images_by_vm_uuid(path, uuid_file): ids = utils.read_ids_from_file(uuid_file) for uuid in ids: image_name = "migration_vm_image_" + uuid filename = path + image_name image = get_image_by_name('from', image_name) print "Uploading image name:", image_name image_create('to', image, filename) volume_ids = nova_common.get_volume_id_list_for_vm_ids('from', './id_file') for volume_id in volume_ids: image_name = "migration_volume_image_" + volume_id filename = path + image_name image = get_image_by_name('from', image_name) print "Uploading image name:", image_name image_create('to', image, filename)
def download_images_by_vm_uuid(destination, path, uuid_file): ids = utils.read_ids_from_file(uuid_file) ready = True for uuid in ids: image_name = "migration_vm_image_" + uuid image = get_image_by_name(destination, image_name) if image.status != "active": print "Image", image.name, "is not in active status. All migration images must be active before proceeding" ready = False return ready for uuid in ids: image_name = "migration_vm_image_" + uuid print "Downloading image name:", image_name image = get_image_by_name(destination, image_name) image_download(image.id, path, fname=image_name) return ready
def download_single_volumes(destination, path, id_file): volumes = utils.read_ids_from_file(id_file) # vols = get_single_volumes(destination) # volumes = map(lambda vols: vols.id, vols) glance_common.download_images_by_volume_uuid(destination, path, volumes, single=True)
def upload_single_volume_images_to_clouds(path, id_file): # vols = get_single_volumes('from') # volumes = map(lambda vols: vols.id, vols) volumes = utils.read_ids_from_file(id_file) glance_common.upload_volume_images(path, volumes)
def create_single_volumes_from_images(id_file): # vols = get_single_volumes('from') vols = utils.read_ids_from_file(id_file) for volume in vols: vol = get_volume_by_id('from', volume) create_volume_from_image('to', vol, single=True)
def retype_volumes_by_volume_ids(destination, volume_id_file, type): cinder = get_cinder(destination) volume_ids = utils.read_ids_from_file(volume_id_file) for volume in volume_ids: change_volume_type(destination, volume, type)