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 main(): ################################################################## # command line parsing ################################################################## parser = OptionParser() parser.add_option("-s", dest="createsnap", action="store_true",default=False,help="Create snapshot from source volume") parser.add_option("-r", dest="rollsnaps", action="store_true", default=False, help="Roll-off old snapshots on source volume") parser.add_option("-f", dest="flexclone", metavar="guest", help="Refresh flexclone from source snapshot. GUEST is the RHEV-M guest that will be refreshed.") (options, args) = parser.parse_args() if options.createsnap: create_sourcesnap() exit(0) elif options.rollsnaps: create_rollsnaps() elif options.flexclone: refresh_flexclone(options.flexclone) exit(0) else: plog.print_error("No options.") exit(1)
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_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 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 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_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 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_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 getVMId(vm_name): URL = "https://%s:%s/api/vms/" % (rm_host, rm_port) request = urllib2.Request(URL) base64string = base64.encodestring('%s:%s' % (rm_user, rm_pass)).strip() 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)
def getVMId(vm_name): URL = "https://%s:%s/api/vms/" %(rm_host,rm_port) request = urllib2.Request(URL) base64string = base64.encodestring('%s:%s' % (rm_user, rm_pass)).strip() 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)
def connect_filer(host,user,passwd): plog.print_debug("Host: %s, User: %s" %(host,user)) s = NaServer(host, 1, 1) s.set_server_type("Filer") s.set_admin_user(user,passwd) response = s.set_transport_type('HTTP') if(response and response.results_errno() != 0 ): r = response.results_reason() plog.print_error("Unable to set HTTP transport " + r + "\n") sys.exit (1) response = s.set_style('LOGIN') if(response and response.results_errno() != 0 ): r = response.results_reason() plog.print_error("Unable to set authentication style " + r + "\n") sys.exit (1) return s
def main(): ################################################################## # command line parsing ################################################################## parser = OptionParser() parser.add_option("-s", dest="createsnap", action="store_true", default=False, help="Create snapshot from source volume") parser.add_option("-r", dest="rollsnaps", action="store_true", default=False, help="Roll-off old snapshots on source volume") parser.add_option( "-f", dest="flexclone", metavar="guest", help= "Refresh flexclone from source snapshot. GUEST is the RHEV-M guest that will be refreshed." ) (options, args) = parser.parse_args() if options.createsnap: create_sourcesnap() exit(0) elif options.rollsnaps: create_rollsnaps() elif options.flexclone: refresh_flexclone(options.flexclone) exit(0) else: plog.print_error("No options.") exit(1)
def is_guest_up(vm_name): vm_id = getVMId(vm_name) if vm_id == None: plog.print_error("Cannot find virtual machine: %s" %(vm_name)) sys.exit(1) URL = "https://%s:%s/api/vms/%s" %(rm_host,rm_port,vm_id) request = urllib2.Request(URL) base64string = base64.encodestring('%s:%s' % (rm_user, rm_pass)).strip() request.add_header("Authorization", "Basic %s" % base64string) xmldata = urllib2.urlopen(request).read() tree = ElementTree.XML(xmldata) list = tree.findall("status") for item in list: plog.print_debug("Current state is: %s" %(item.find("state").text)) if (item.find("state").text == "up"): return True else: return False
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 is_guest_up(vm_name): vm_id = getVMId(vm_name) if vm_id == None: plog.print_error("Cannot find virtual machine: %s" % (vm_name)) sys.exit(1) URL = "https://%s:%s/api/vms/%s" % (rm_host, rm_port, vm_id) request = urllib2.Request(URL) base64string = base64.encodestring('%s:%s' % (rm_user, rm_pass)).strip() request.add_header("Authorization", "Basic %s" % base64string) xmldata = urllib2.urlopen(request).read() tree = ElementTree.XML(xmldata) list = tree.findall("status") for item in list: plog.print_debug("Current state is: %s" % (item.find("state").text)) if (item.find("state").text == "up"): return True else: return False
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)
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.")
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.")