def guest_destroy(argv): ''' destroy only ''' UUID = argv[0] conn = libvirt.open(None) if conn == None: return 'Failed to open connection to the hypervisor' try: dom = conn.lookupByUUID(general.transformUUID(UUID)) except: return 'Failed to find your domain' runningState = dom.info()[0] if runningState == 1 or runningState == 3: try: dom.destroy() except: return "destroy error" return "OK" else: return 'Cannot forced-off your domain in this runningState(%d)' % ( dom.info()[0])
def guest_scale_memory(argv): ''' scale memory while it's running ''' UUID = argv[0] memory = int(argv[1]) conn = libvirt.open(None) if conn == None: return 'Failed to open connection to the hypervisor' try: dom = conn.lookupByUUID(general.transformUUID(UUID)) except: return 'Failed to find your domain' if dom.info()[0] == 1: try: result = dom.setMaxMemory(memory * 1024) result += dom.setMemory(memory * 1024) if result == 0: return "OK" elif result < 0: return 'Failed to scale memory immediately' else: return 'this line should not be done' except: return 'Failed to scale memory immediately' else: return 'Cannot scale memory in this status(%d)' % (dom.info()[0])
def guest_resume(argv): ''' resume guest only ''' UUID = argv[0] conn = libvirt.open(None) if conn == None: return 'Failed to open connection to the hypervisor' try: dom = conn.lookupByUUID(general.transformUUID(UUID)) except: return 'Failed to find your domain' if dom.info()[0] == 3: try: dom.resume() except: return 'Failed to resume your domain' #nothing to do with database return str(1) #return runningState #dom.info()[0] should be 1 else: return 'Cannot suspend this domain in this status(%d)' % ( dom.info()[0])
def guest_update_status(argv): ''' check current status of this guest and update in database ''' UUID = argv[0] guestID = argv[1] infoHost = cacheFile.getDatabaseIP() db = MySQLdb.connect(infoHost, setting.DB_USERNAME, setting.DB_PASSWORD, setting.DB_NAME) cursor = db.cursor() cursor.execute('''SELECT `status`, `activity` FROM `guests` WHERE `guestID`=%s;''' % (guestID)) guestData = cursor.fetchone() if guestData == None: return 'Invalid guestID' lastStatus = guestData[0] lastActivity = guestData[1] if lastActivity != 0: return json.dumps({'status': lastStatus, 'runningState': 0}) if lastStatus == 2 or lastStatus == 0: return json.dumps({'status': lastStatus, 'runningState': 0}) conn = libvirt.open(None) if conn == None: return 'Failed to open connection to the hypervisor' try: dom = conn.lookupByUUID(general.transformUUID(UUID)) result = {'status': 1, 'runningState': dom.info()[0]} except: cursor.execute( '''UPDATE `guests` SET `status`=0 WHERE `guestID`=%s''' % (guestID)) result = {'status': 0, 'runningState': 0} db.close() return json.dumps(result)
def guest_force_off(argv): ''' force clost a guest ''' if 'macMode' in argv: guestMAC = argv[0] else: guestID = argv[0] UUID = argv[1] conn = libvirt.open(None) if conn == None: return 'Failed to open connection to the hypervisor' try: dom = conn.lookupByUUID(general.transformUUID(UUID)) except: return 'Failed to find your domain' #runningState=dom.info()[0] #if runningState==1 or runningState==3 : try: dom.destroy() except: return "destroy error" infoHost = cacheFile.getDatabaseIP() db = MySQLdb.connect(infoHost, setting.DB_USERNAME, setting.DB_PASSWORD, setting.DB_NAME) cursor = db.cursor() if 'macMode' in argv: condition = " WHERE `MACAddress`='%s'" % (guestMAC) else: condition = " WHERE `guestID`=%s" % (guestID) cursor.execute( "UPDATE `guests` SET `status`=0, `activity`=0, `lastUUID`=NULL, `lastHostID`=NULL" + condition) db.close() return "OK"
def guest_send_reboot_signal(argv): ''' only send reboot signal to the guest ''' UUID = argv[0] conn = libvirt.open(None) if conn == None: return 'Failed to open connection to the hypervisor' try: dom = conn.lookupByUUID(general.transformUUID(UUID)) except: return 'Failed to find your domain' try: dom.reboot() except: return 'Failed to send reboot signal' return "OK"
def guest_migrate(argv): guestID = argv[0] currentUUID = argv[1] currentHostIP = argv[2] targetHostIP = argv[3] targetHostID = argv[4] connSource = libvirt.open(None) if connSource == None: return 'Failed to open connection to the hypervisor' try: dom = connSource.lookupByUUID(general.transformUUID(currentUUID)) except: return 'Failed to find your domain' try: destURI = "qemu+tls://%s/system" % (targetHostIP) connDest = libvirt.open(destURI) if connDest == None: return 'Failed to find targetHost' except: return 'Failed to find targetHost' infoHost = cacheFile.getDatabaseIP() db = MySQLdb.connect(infoHost, setting.DB_USERNAME, setting.DB_PASSWORD, setting.DB_NAME) cursor = db.cursor() #I'm migrating cursor.execute("UPDATE `guests` SET `activity`=5 WHERE `guestID`=%s" % (guestID)) """ aThread=LongMigration() aThread.setArguments(str(guestID),dom,connDest,targetHostID) aThread.start() if argv[-1]=='{block}': aThread.join() return threadResult #value sent from thread """ try: #newDom=dom.migrate(connDest,libvirt.VIR_MIGRATE_LIVE,None,None,0) #can use at magi, error at steam #newDom=dom.migrate(connDest,libvirt.VIR_MIGRATE_LIVE+libvirt.VIR_MIGRATE_PEER2PEER,None,destURI,0) #error at steam, unknown at magi migrateURI = "tcp:" + str(targetHostIP) newDom = dom.migrate(connDest, libvirt.VIR_MIGRATE_LIVE, None, migrateURI, 0) newUUID = newDom.UUIDString().replace('-', '') #finish and tell database that you finish cursor.execute( '''UPDATE `guests` SET `activity`=0, `status`=1, `lastUUID`='%s', `lastHostID`=%s WHERE `guestID`=%s''' % (newUUID, targetHostID, str(guestID))) db.close() except: print "migration error" #fix activity back to None cursor.execute( "UPDATE `guests` SET `activity`=0, `status`=1 WHERE `guestID`=%s" % (str(guestID))) db.close() return MySQLdb.escape_string(traceback.format_exc()) return 'OK'
def guest_get_current_info(argv): ''' all-in-one function ''' UUID = argv[0] conn = libvirt.open(None) if conn == None: return 'Failed to open connection to the hypervisor' try: dom = conn.lookupByUUID(general.transformUUID(UUID)) except: return 'Failed to find your domain' try: currentInfoString = open(setting.CURRENT_INFO_PATH + UUID + '.info', 'r').read() except: if ('cpu' in argv) or ('network' in argv) or ('io' in argv): return 'Current info of this domain is not ready now please try again in ' + str( setting.GUEST_MONITOR_PERIOD) + ' seconds.' currentInfo = {} for element in currentInfoString.split('\n'): if element == '': continue temp = element.split(':') currentInfo[temp[0]] = temp[1] resultData = {} if 'cpu' in argv: cpuInfo = { 'usage': currentInfo['CPU_USAGE'], 'cpuTime': currentInfo['CPU_TIME'] } resultData['cpuInfo'] = cpuInfo if 'network' in argv: networkInfo = { 'rxRate': currentInfo['INTERFACE_RX_RATE'], 'txRate': currentInfo['INTERFACE_TX_RATE'], 'rxUsed': currentInfo['INTERFACE_RX_USED'], 'txUsed': currentInfo['INTERFACE_TX_USED'] } resultData['networkInfo'] = networkInfo if 'io' in argv: ioInfo = { 'rxRate': currentInfo['DISK_READ_RATE'], 'wxRate': currentInfo['DISK_WRITE_RATE'], 'rxUsed': currentInfo['DISK_READ_USED'], 'wxUsed': currentInfo['DISK_WRITE_USED'] } resultData['ioInfo'] = ioInfo if ('cpu' in argv) or ('network' in argv) or ('io' in argv): resultData['lastUpdate'] = currentInfo['UPDATE'] if 'memory' in argv: info = dom.info() memoryInfo = { 'usage': str(dom.info()[2] * 1024), 'total': str(dom.info()[1] * 1024) } resultData['memoryInfo'] = memoryInfo return json.dumps(resultData)
def guest_save(argv): ''' save guest and update database ''' UUID = argv[0] volumeFileName = argv[1] guestID = argv[2] saveFileName = volumeFileName.split('.') saveFileName[-1] = 'sav' saveFileName = '.'.join(saveFileName) conn = libvirt.open(None) if conn == None: return 'Failed to open connection to the hypervisor' try: dom = conn.lookupByUUID(general.transformUUID(UUID)) except: return 'Failed to find your domain' runningState = dom.info()[0] if runningState == 1 or runningState == 3: infoHost = cacheFile.getDatabaseIP() db = MySQLdb.connect(infoHost, setting.DB_USERNAME, setting.DB_PASSWORD, setting.DB_NAME) cursor = db.cursor() #I'm saving cursor.execute("UPDATE `guests` SET `activity`=3 WHERE `guestID`=%s" % (guestID)) """ aThread=LongSaving() aThread.setArguments(dom,str(guestID),saveFileName) aThread.start() #new version (blocked method) if argv[-1]=='{block}': aThread.join() """ result = dom.save(setting.TARGET_IMAGE_PATH + saveFileName) if result == -1: print "saving fail" cursor.execute( "UPDATE `guests` SET `activity`=-1 WHERE `guestID`=%s" % (str(guestID))) db.close() return result #finish and tell database that you finish cursor.execute( "UPDATE `guests` SET `activity`=0, `status`=2 WHERE `guestID`=%s" % (str(guestID))) db.close() return "OK" else: return 'Cannot save your domain in this status(%d)' % (dom.info()[0])