def guest_start(vm_name): plog.print_log("starting up vm: %s" %(vm_name)) vm_id = getVMId(vm_name) if vm_id == None: plog.print_error("Cannot find virtual machine: %s" %(vm_name)) sys.exit(1) xml_request ="""<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <action/> """ # Setting URL URL = "https://%s:%s/api/vms/%s/start" %(rm_host,rm_port,vm_id) request = urllib2.Request(URL) plog.print_log("Connecting to: %s" %(URL)) base64string = base64.encodestring('%s:%s' % (rm_user, rm_pass)).strip() request.add_header("Authorization", "Basic %s" % base64string) request.add_header('Content-Type', 'application/xml') request.get_method = lambda: 'POST' try: ret = urllib2.urlopen(request, xml_request) except urllib2.URLError, e: plog.print_error( "guest is already Up.")
def guest_start(vm_name): plog.print_log("starting up vm: %s" % (vm_name)) vm_id = getVMId(vm_name) if vm_id == None: plog.print_error("Cannot find virtual machine: %s" % (vm_name)) sys.exit(1) xml_request = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <action/> """ # Setting URL URL = "https://%s:%s/api/vms/%s/start" % (rm_host, rm_port, vm_id) request = urllib2.Request(URL) plog.print_log("Connecting to: %s" % (URL)) base64string = base64.encodestring('%s:%s' % (rm_user, rm_pass)).strip() request.add_header("Authorization", "Basic %s" % base64string) request.add_header('Content-Type', 'application/xml') request.get_method = lambda: 'POST' try: ret = urllib2.urlopen(request, xml_request) except urllib2.URLError, e: plog.print_error("guest is already Up.")
def run_remote_command(host, command): # this requires sshkeys to function! plog.print_log("%s: running command: %s" %(host, command)) process = subprocess.Popen("ssh root@%s %s" % (host, command), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output,stderr = process.communicate() status = process.poll() return output
def map_lun(filer, igroup, path, lunid): output = filer.invoke("lun-map", "initiator-group", igroup, "path", path, "lun-id", lunid) if (output.results_errno() != 0): r = output.results_reason() plog.print_error("LUN map failed: %s" %(r) ) return False else: plog.print_log("lun %s is mapped to igroup: %s as id: %s." %(path, igroup, lunid)) return True
def create_sourcesnap(): plog.print_log("Creating source volume snapshot on %s." % (sourceVolume)) # check for existance (on the filer) of source volume if (netapp.does_vol_exist(filer, sourceVolume) == False): plog.print_error("Source volume %s doesnt exist." % (sourceVolume)) else: plog.print_log("Volume: %s exists, continuing." % (sourceVolume)) # create volume snapshot in ISO date format (pgds_refresh_2012_01_31) if (netapp.create_snap(filer, sourceVolume, today_snap) == False): plog.print_error("Unable to continue, snapshot failed.")
def create_flexclone(filer, sourceVolume, destVolume, snapshot): plog.print_log("Creating Flexclone Vol: %s from source vol: %s. Using Snapshot: %s" % (destVolume, sourceVolume, snapshot)) output = filer.invoke("volume-clone-create", "parent-snapshot", snapshot, "parent-volume", sourceVolume, "volume", destVolume, "space-reserve", "none") if (output.results_errno() != 0): r = output.results_reason() plog.print_error("Creation of flexclone failed: %s" %(r) ) return False else: plog.print_log("Flexclone %s created from Volume: %s." %(destVolume,sourceVolume)) return True
def create_snap(filer, volname, snapshot): plog.print_log("%s: creating snapshot: %s" % (volname, snapshot)) output = filer.invoke("snapshot-create", "snapshot", snapshot, "volume", volname) if (output.results_errno() != 0): r = output.results_reason() plog.print_error("Creation of snapshot failed: %s" %(r) ) return False else: plog.print_log("Snapshot %s created on Volume: %s." %(snapshot,volname)) return True
def remove_snap(filer, volname, snapshot): plog.print_log("%s: removing snapshot: %s" % (volname, snapshot)) output = filer.invoke("snapshot-delete", "snapshot", snapshot, "volume", volname) if (output.results_errno() != 0): r = output.results_reason() plog.print_error("Delete of snapshot failed: %s" %(r) ) return False else: plog.print_log("Snapshot %s removed from Volume: %s." %(snapshot,volname)) return True
def online_lun(filer, path): plog.print_log("Status change %s to Online." %(path)) output = filer.invoke("lun-online", "path", path) if (output.results_errno() != 0): r = output.results_reason() plog.print_error("Unable to get online lun %s. %s" %(path, r) ) return False else: plog.print_log("lun %s is now online." %(path)) return True
def create_sourcesnap(): plog.print_log("Creating source volume snapshot on %s." %(sourceVolume)) # check for existance (on the filer) of source volume if (netapp.does_vol_exist(filer,sourceVolume) == False): plog.print_error("Source volume %s doesnt exist." % (sourceVolume)) else: plog.print_log("Volume: %s exists, continuing." % (sourceVolume)) # create volume snapshot in ISO date format (pgds_refresh_2012_01_31) if (netapp.create_snap(filer, sourceVolume, today_snap) == False): plog.print_error("Unable to continue, snapshot failed.")
def guest_set_directlun(vm_name, deviceid, memorysize): xml_request ="""<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <vm> <custom_properties> <custom_property value="%s" name="directlun"/> </custom_properties> <memory>%s</memory> </vm> """ %(deviceid.strip(),memorysize) vm_id = getVMId(vm_name) if vm_id == None: plog.print_error("Cannot find virtual machine: %s" %(vm_name)) sys.exit(1) # Setting URL URL = "https://%s:%s/api/vms/%s" %(rm_host,rm_port,vm_id) request = urllib2.Request(URL) plog.print_log("Connecting %s to %s. " %(deviceid,vm_name)) base64string = base64.encodestring('%s:%s' % (rm_user, rm_pass)).strip() request.add_header("Authorization", "Basic %s" % base64string) request.add_header('Content-Type', 'application/xml') request.get_method = lambda: 'PUT' try: xmldata = urllib2.urlopen(request, xml_request) except urllib2.URLError, e: plog.print_log("Cannot connect to REST API: %s" % (e)) plog.print_log("Possible errors: ") plog.print_log("\t- Try to login using the same user/pass by the Admin Portal and check the error!") sys.exit(1)
def get_volume_snap_list(filer, volume): output = filer.invoke("snapshot-list-info", "volume", volume) if (output.results_errno() != 0): r = output.results_reason() plog.print_error("Unable to get snapshots for %s. %s" %(volume, r) ) return False else: plog.print_log("snapshots for %s:" %(volume) ) # # get snapshot list snapshotlist = output.child_get("snapshots") if ((snapshotlist == None) or (snapshotlist == "")) : # no snapshots to report plog.print_log("No snapshots on volume " + vol + "\n\n") sys.exit(0) # iterate through snapshot list snapshots = snapshotlist.children_get() snaplist = [] for ss in snapshots: snaplist.append(ss.child_get_string("name")) return snaplist
def create_rollsnaps(): plog.print_log("removing snapshots older than %s days" % (keepSnapsDays)) for item in netapp.get_volume_snap_list(filer, sourceVolume): if (sourceSnapPrefix in item): snapdate = datetime.strptime(item.rsplit("_")[1], "%Y%m%d") now = datetime.now() maxdelta = timedelta(days=int(keepSnapsDays)) thisdelta = now - snapdate if (thisdelta > maxdelta): plog.print_log("Deleting snap %s" % (item)) netapp.remove_snap(filer, sourceVolume, item) else: plog.print_log("Leaving snap %s" % (item))
def create_rollsnaps(): plog.print_log("removing snapshots older than %s days" %(keepSnapsDays)) for item in netapp.get_volume_snap_list(filer, sourceVolume): if (sourceSnapPrefix in item): snapdate = datetime.strptime(item.rsplit("_")[1], "%Y%m%d") now = datetime.now() maxdelta = timedelta(days=int(keepSnapsDays)) thisdelta = now - snapdate if (thisdelta > maxdelta): plog.print_log("Deleting snap %s" %(item)) netapp.remove_snap(filer, sourceVolume, item) else: plog.print_log("Leaving snap %s" %(item))
def destroy_vol(filer, volname): if (does_vol_exist(filer,volname) == True): plog.print_log("Offline %s" %(volname)) output = filer.invoke("volume-offline", "name", volname) if (output.results_errno() != 0): r = output.results_reason() plog.print_error("Volume-Offline operation failed: %s" %(r) ) else: plog.print_log("Volume Offline: %s." %(volname)) plog.print_log("Destroy %s" %(volname)) output = filer.invoke("volume-destroy", "name", volname) if (output.results_errno() != 0): r = output.results_reason() plog.print_error("Volume Destroy failed: %s" %(r) ) return False else: plog.print_log("Volume Destroy: %s." %(volname)) return True else: plog.print_warn("Volume: %s doesn't exist." %(volname))
def guest_set_directlun(vm_name, deviceid, memorysize): xml_request = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <vm> <custom_properties> <custom_property value="%s" name="directlun"/> </custom_properties> <memory>%s</memory> </vm> """ % (deviceid.strip(), memorysize) vm_id = getVMId(vm_name) if vm_id == None: plog.print_error("Cannot find virtual machine: %s" % (vm_name)) sys.exit(1) # Setting URL URL = "https://%s:%s/api/vms/%s" % (rm_host, rm_port, vm_id) request = urllib2.Request(URL) plog.print_log("Connecting %s to %s. " % (deviceid, vm_name)) base64string = base64.encodestring('%s:%s' % (rm_user, rm_pass)).strip() request.add_header("Authorization", "Basic %s" % base64string) request.add_header('Content-Type', 'application/xml') request.get_method = lambda: 'PUT' try: xmldata = urllib2.urlopen(request, xml_request) except urllib2.URLError, e: plog.print_log("Cannot connect to REST API: %s" % (e)) plog.print_log("Possible errors: ") plog.print_log( "\t- Try to login using the same user/pass by the Admin Portal and check the error!" ) sys.exit(1)
request.add_header("Authorization", "Basic %s" % base64string) try: xmldata = urllib2.urlopen(request).read() except urllib2.URLError, e: plog.print_error("Error: cannot connect to REST API: %s" % (e)) sys.exit(1) tree = ElementTree.XML(xmldata) list = tree.findall("vm") vm_id = None for item in list: if vm_name == item.find("name").text: vm_id = item.attrib["id"] plog.print_log("vm id %s" % (vm_id)) break return vm_id # shutdown VM_NAME def guest_shutdown(vm_name): plog.print_log("shutdown vm: %s" % (vm_name)) vm_id = getVMId(vm_name) if vm_id == None: plog.print_error("Cannot find virtual machine: %s" % (vm_name)) sys.exit(1)
def shutdown(host): plog.print_log("Sending shutdown command to %s" %(host)) cmd = "shutdown -h now" return run_remote_command(host, cmd)
def xfs_unfreeze(host,path): plog.print_log("unfreezeing xfs mount: %s on %s" %(path,host)) cmd = "xfs_unfreeze -u %s" %(path) return run_remote_command(host, cmd)
def remove_multipath_devices(host, lunid): device_id = get_device_id_from_lun(host, lunid) cmd = "multipath -f %s" %(device_id) plog.print_log("Removing multipath device with command: %s, on: %s" %(cmd, host)) return run_remote_command(host, cmd)
def add_multipath_devices(host): plog.print_log("Adding new multipath devices on %s" %(host)) cmd = "multipath" return run_remote_command(host, cmd)
def get_device_id_from_lun(host, lunid): plog.print_log("Getting SCSI id for lun %s on %s" %(lunid, host)) multipath = "multipath -ll |egrep -B3 \" [0-9]:[0-9]:[0-9]:%s \" | head -n1 | awk '{ print $1 }'" %(lunid) return run_remote_command(host, multipath)
def rescan_scsi(host): plog.print_log("Re-Scanning SCSI bus on %s" %(host)) rescan_bus = '/usr/bin/rescan-scsi-bus.sh' return run_remote_command(host, rescan_bus)
def refresh_flexclone(destination): plog.print_log("Starting flexclone refresh process for %s" %(destination)) if not (destination in guestList): plog.print_error("Guest %s not found, please check config." %(destination)) for guest in guestList: # if this is the guest specified in the command line parameter, then do your work! if (destination == guest): thisguest = config.get(guest, 'vm_name') thislunid = config.get(guest, 'lunid') thisvolume = "%s_%s" %(sourceVolume, thisguest) thislun = sourceLun thislunpath = "/vol/%s/%s" %(thisvolume, thislun) plog.print_log("%s %s" %(thisguest, thislunid)) # shutdown the guest if not (rhevm.is_guest_down(thisguest)): linux.shutdown(thisguest) i = 0 while (rhevm.is_guest_down(thisguest) == False): if (i < 30): time.sleep(15) plog.print_log("Waiting 15s for %s to shutdown." %(thisguest)) i += 1 else: plog.print_error("Unable to shutdown %s." %(thisguest)) exit(1) plog.print_log("%s is now down." %(thisguest)) #plog.print_log("Clearing %s directlun property." %(thisguest)) #rhevm.guest_set_directlun(thisguest, "donothinghere", "68719476736") for thishost in hostlist: plog.print_log("removing lun: %s from %s on %s." %(thislunid, thisguest, thishost[1])) linux.remove_multipath_devices(thishost[1], thislunid) # clean the old flexclone and lUN, remove the flexclone volume. netapp.destroy_vol(filer,thisvolume) time.sleep(10) # create a new flexclone based on today's snapshot if not (netapp.create_flexclone(filer, sourceVolume, thisvolume, today_snap)): plog.print_error("Unable to continue. Flexclone create failed.") sys.exit(1) time.sleep(10) # online the new lun netapp.online_lun(filer, thislunpath) time.sleep(10) netapp.map_lun(filer, sourceiGroup, thislunpath, thislunid) time.sleep(10) #for each host rescan scsi bus, run multipath #then get the device id and map it to the guest for thishost in hostlist: plog.print_log("Scanning for new luns.") #scan the scsi bus for new devices linux.rescan_scsi(thishost[1]) time.sleep(10) #add new devices to multipath linux.add_multipath_devices(thishost[1]) time.sleep(10) # I do this on each host scan, realistically it should be identical on all hosts. thisdeviceid = linux.get_device_id_from_lun(thishost[1], thislunid) time.sleep(10) # attach new directlun with deviceid to guest rhevm.guest_set_directlun(thisguest, thisdeviceid, "68719476736") time.sleep(10) # power on guest rhevm.guest_start(thisguest) i = 0 while not (httpcheck.get_status_code("%s:8888" %(thisguest))): if (i < 10): time.sleep(15) plog.print_log("Waiting 15s for %s for startup." %(thisguest)) i += 1 else: plog.print_error("Unable to startup %s." %(thisguest)) exit(1) plog.print_log("Refresh complete.")
request.add_header("Authorization", "Basic %s" % base64string) try: xmldata = urllib2.urlopen(request).read() except urllib2.URLError, e: plog.print_error("Error: cannot connect to REST API: %s" % (e)) sys.exit(1) tree = ElementTree.XML(xmldata) list = tree.findall("vm") vm_id = None for item in list: if vm_name == item.find("name").text: vm_id = item.attrib["id"] plog.print_log("vm id %s" % (vm_id)) break return vm_id # shutdown VM_NAME def guest_shutdown(vm_name): plog.print_log("shutdown vm: %s" %(vm_name)) vm_id = getVMId(vm_name) if vm_id == None: plog.print_error("Cannot find virtual machine: %s" %(vm_name)) sys.exit(1) xml_request ="""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
def refresh_flexclone(destination): plog.print_log("Starting flexclone refresh process for %s" % (destination)) if not (destination in guestList): plog.print_error("Guest %s not found, please check config." % (destination)) for guest in guestList: # if this is the guest specified in the command line parameter, then do your work! if (destination == guest): thisguest = config.get(guest, 'vm_name') thislunid = config.get(guest, 'lunid') thisvolume = "%s_%s" % (sourceVolume, thisguest) thislun = sourceLun thislunpath = "/vol/%s/%s" % (thisvolume, thislun) plog.print_log("%s %s" % (thisguest, thislunid)) # shutdown the guest if not (rhevm.is_guest_down(thisguest)): linux.shutdown(thisguest) i = 0 while (rhevm.is_guest_down(thisguest) == False): if (i < 30): time.sleep(15) plog.print_log("Waiting 15s for %s to shutdown." % (thisguest)) i += 1 else: plog.print_error("Unable to shutdown %s." % (thisguest)) exit(1) plog.print_log("%s is now down." % (thisguest)) #plog.print_log("Clearing %s directlun property." %(thisguest)) #rhevm.guest_set_directlun(thisguest, "donothinghere", "68719476736") for thishost in hostlist: plog.print_log("removing lun: %s from %s on %s." % (thislunid, thisguest, thishost[1])) linux.remove_multipath_devices(thishost[1], thislunid) # clean the old flexclone and lUN, remove the flexclone volume. netapp.destroy_vol(filer, thisvolume) time.sleep(10) # create a new flexclone based on today's snapshot if not (netapp.create_flexclone(filer, sourceVolume, thisvolume, today_snap)): plog.print_error( "Unable to continue. Flexclone create failed.") sys.exit(1) time.sleep(10) # online the new lun netapp.online_lun(filer, thislunpath) time.sleep(10) netapp.map_lun(filer, sourceiGroup, thislunpath, thislunid) time.sleep(10) #for each host rescan scsi bus, run multipath #then get the device id and map it to the guest for thishost in hostlist: plog.print_log("Scanning for new luns.") #scan the scsi bus for new devices linux.rescan_scsi(thishost[1]) time.sleep(10) #add new devices to multipath linux.add_multipath_devices(thishost[1]) time.sleep(10) # I do this on each host scan, realistically it should be identical on all hosts. thisdeviceid = linux.get_device_id_from_lun( thishost[1], thislunid) time.sleep(10) # attach new directlun with deviceid to guest rhevm.guest_set_directlun(thisguest, thisdeviceid, "68719476736") time.sleep(10) # power on guest rhevm.guest_start(thisguest) i = 0 while not (httpcheck.get_status_code("%s:8888" % (thisguest))): if (i < 10): time.sleep(15) plog.print_log("Waiting 15s for %s for startup." % (thisguest)) i += 1 else: plog.print_error("Unable to startup %s." % (thisguest)) exit(1) plog.print_log("Refresh complete.")