def wake_up_global_controller(argv): ''' start mkapi ''' conn =argv[0][0] s =argv[0][1] general.runDaemonCommand("service mkapi start",conn,s,pipe=True) return "OK"
def restart_libvirtd(argv): conn = argv[0][0] s = argv[0][1] general.runDaemonCommand("service libvirtd restart", conn, s) vconn = libvirt.open(None) time.sleep(1) vconn.close() return "OK"
def start(rootpassword="******", conn=None, sock=None): general.runDaemonCommand("service mysqld start", conn, sock) #output=result.communicate()[0] #if output!=None and len(output)>50: #this is the first time of start #this line will error but it will not give bug (error as normal, cuz i cannot check it's the first time or not) result2 = subprocess.Popen(shlex.split("mysqladmin -u root password '%s'" % (rootpassword)), stdout=subprocess.PIPE) result2.wait() return True
def update_cloud_info(argv): ''' get info and update ''' conn=argv[0][0] s=argv[0][1] dataDict=json.loads(argv[1]) if 'masterDB' not in dataDict.keys(): dataDict['masterDB']=None if 'masterDB_MAC' not in dataDict.keys(): dataDict['masterDB_MAC']=None if 'slaveDB' not in dataDict.keys(): dataDict['slaveDB']=None if 'globalController' not in dataDict.keys(): dataDict['globalController']=None if 'network' not in dataDict.keys(): dataDict['network']=None newDataDict=cacheFile.setValue(masterDB=dataDict['masterDB'],masterDB_MAC=dataDict['masterDB_MAC'],slaveDB=dataDict['slaveDB'],globalController=dataDict['globalController'],network=dataDict['network']) infoHost=newDataDict['masterDB'] myIP=network.getMyIPAddr() #if INFO_HOST is not me and i am not slave , i should stop mysqld service #if infoHost!=str(myIP): # dbController.stop() if 'nfs' in argv: #connect to database to get data of NFS Server to mount (may call updateLocal() to make everything) db = MySQLdb.connect(infoHost, setting.DB_USERNAME, setting.DB_PASSWORD, setting.DB_NAME ) cursor = db.cursor() cursor.execute('''SELECT `IPAddress` FROM `hosts` WHERE `isStorageHolder`=1;''') storageHost=str(cursor.fetchone()[0]) db.close() #debugger.countdown(10,"before local unmount and mount") print "Umount:",nfsController.umount() print "Mount :",nfsController.mount(storageHost) if 'planner' in argv: general.runDaemonCommand("service mkplanner restart",conn,s,True) #can be True in log system if 'dynamic' in argv: network.configToAuto() #(conn,s) return 'OK'
def you_are_next_global_controller(argv): ''' promote myself to be global_controller and DHCP server ''' conn = argv[0][0] s = argv[0][1] mode = argv[1] whitelistString = argv[2] #config and start dhcp server dhcpInfo=dhcpController.getDHCPInfoFromDatabase() dhcpController.configAll(dhcpInfo['networkID'],dhcpInfo['subnetMask'],dhcpInfo['defaultRoute'],dhcpInfo['dns'],dhcpInfo['hostBindings'],conn,s) #network.configToStaticFromCacheFile() #(conn,s) #new system no need to do this #generate whitelist file aFile=open(setting.API_WHITELIST_FILE,'w') aFile.write(whitelistString) aFile.close() #start global controller (mkapi and mkworker and [scheduler]) general.runDaemonCommand(command="service mkapi start",conn=conn,sock=s,pipe=True) #can be True in log system #general.runDaemonCommand("service mkworker start debug",conn,s) infoHost=cacheFile.getDatabaseIP() db = MySQLdb.connect(infoHost, setting.DB_USERNAME, setting.DB_PASSWORD, setting.DB_NAME ) cursor = db.cursor() if mode=='migrate': #must tell old GlobalController to stop service cursor.execute("SELECT `IPAddress` FROM `hosts` WHERE `isGlobalController`=1;") hostData=cursor.fetchone() if hostData!=None: result=connection.socketCall(hostData[0],setting.LOCAL_PORT,"close_global_controller_and_dhcp_server",['{socket_connection}']) if result!='OK': print 'close_global_controller_and_dhcp_server was not complete.(can leave it, no problem)' cursor.execute("UPDATE `hosts` SET `isGlobalController`=1 WHERE `IPAddress`='%s';"%(str(network.getMyIPAddr()))) db.close() return "OK"
def run ( self ): closeSocketEvent.wait() network.configToStaticFromCacheFile() #(conn,s) #runServer may be error currentDict=cacheFile.getCurrentDict() if currentDict['globalController']==currentDict['masterDB']: #masterDB is down if currentDict['slaveDB']==None: print "cloud is stopping service, cannot restore(no slave db)" return else: #try to connect to slave db while True: result=connection.socketCall(currentDict['slaveDB'], setting.LOCAL_PORT, 'hello') if result=='OK': break time.sleep(2) #must promote the slave db up print "wait 10 sec" time.sleep(10) #wait for every host to repair own ip dbService.promote() currentDict=cacheFile.getCurrentDict() #get new masterDB else: #try to connect to master db while True: result=connection.socketCall(currentDict['masterDB'], setting.LOCAL_PORT, 'hello') if result=='OK': break time.sleep(2) infoHost=currentDict['masterDB'] #lock dequeuing by global_lock db = MySQLdb.connect(infoHost, setting.DB_USERNAME, setting.DB_PASSWORD, setting.DB_NAME ) cursor = db.cursor() cursor.execute("UPDATE `cloud_variables` SET `value`='1' WHERE `key`='global_lock'") #don't forget to open when finish everything db.close() #config and start dhcp server dhcpInfo=dhcpController.getDHCPInfoFromDatabase() dhcpController.configAll(dhcpInfo['networkID'],dhcpInfo['subnetMask'],dhcpInfo['defaultRoute'],dhcpInfo['dns'],dhcpInfo['hostBindings'],conn,s) db = MySQLdb.connect(infoHost, setting.DB_USERNAME, setting.DB_PASSWORD, setting.DB_NAME ) cursor = db.cursor() #must repair CA here cursor.execute("SELECT `IPAddress` FROM `hosts` WHERE `isCA`=1") caIP=cursor.fetchone()[0] if caIP==currentDict['masterDB']: #CA is down if caService.promote()==False: print "cannot promote CA, ending" return #update database cursor.execute("UPDATE `hosts` SET `status`=0, `isGlobalController`=0, `inInformationServer`=0, `isCA`=0 WHERE `IPAddress`=%s"%(currentDict['globalController'])) cursor.execute("UPDATE `hosts` SET `isGlobalController`=1 WHERE `IPAddress`=%s"%(currentDict['myLastIP'])) #broadcast new global controller cursor.execute("SELECT `IPAddress` FROM `hosts` WHERE `status`=1") activeHosts=cursor.fetchall() dataString=json.dumps({ 'globalController':str(currentDict['myLastIP']) }) for host in activeHosts: #every host should be static as it can, in new system #if host[0]==currentDict['myLastIP']: # option=[] #else: # option=['dynamic'] result=connection.socketCall(host[0], setting.LOCAL_PORT, 'update_cloud_info', ['{socket_connection}',dataString,'planner']+option) if result!='OK': print "connection problem, cannot update_cloud_info to",host[0] general.runDaemonCommand("service mkapi start",conn,s,True) #can be true in log system #fix queue cursor.execute("SELECT `taskID`, `processID` FROM `tasks` WHERE `status`=1") tmpData=cursor.fetchall() for element in tmpData: queue.propagateError(element[0]) #next is check and repair host and guests ha.recover() cursor.execute("UPDATE `cloud_variables` SET `value`='0' WHERE `key`='global_lock'") #unlock global db.close() #tell queue to do next work connection.socketCall("127.0.0.1",setting.WORKER_PORT,'start_work',['{socket_connection}']) return
def start(conn=None, sock=None): general.runDaemonCommand("service nfs start", conn, sock) return True
def stop(): general.runDaemonCommand("service nfs stop") return True
def stop_mkplanner(argv): ''' just stop mkplanner (should do when remove host from system) ''' general.runDaemonCommand("service mkplanner stop") return 'OK'
def restart(conn=None, sock=None): general.runDaemonCommand("service mysqld restart", conn, sock) return True
def restart(conn=None, sock=None): waitConfigTest() general.runDaemonCommand("service dhcpd restart", conn, sock) return True
def update_pki(argv): ''' argv[0]=[conn,s,event] #socket argv[1]=cerHostIP (optional) ''' conn = argv[0][0] s = argv[0][1] #add later for testing #print "service libvirtd stop" #general.runDaemonCommand("service libvirtd stop",conn,s) #find cerHostIP if len(argv) == 1: #find CA Server from database infoHost = cacheFile.getDatabaseIP() db = MySQLdb.connect(infoHost, setting.DB_USERNAME, setting.DB_PASSWORD, setting.DB_NAME) cursor = db.cursor() cursor.execute('''SELECT `IPAddress` FROM `hosts` WHERE `isCA`=1;''') tmpData = cursor.fetchone() if tmpData == None: return 'CA Server not found.' cerHostIP = tmpData[0] else: cerHostIP = argv[1] #create directory result = subprocess.Popen( shlex.split("mkdir -p /etc/pki/libvirt/private/"), stdout=subprocess.PIPE) result.wait() #generate private key result = subprocess.Popen(shlex.split( "certtool --generate-privkey --outfile /etc/pki/libvirt/private/serverkey.pem" ), stdout=subprocess.PIPE) result.wait() #get cacert from CA to install at /etc/pki/CA/cacert.pema cacertString = connection.socketCall(cerHostIP, setting.LOCAL_PORT, 'request_cacert') cacertFile = open('/etc/pki/CA/cacert.pem', 'w') cacertFile.write(cacertString) cacertFile.close() ''' not secure #send private key to sign at CA server cerString = connection.socketCall(cerHostIP, setting.LOCAL_PORT, 'request_sign_certificate',[str(network.getMyIPAddr()),open("/etc/pki/libvirt/private/serverkey.pem",'r').read()]) ''' #more secure (sending request) serverIP = str(network.getMyIPAddr()) #create template file infoString = ''' organization = Maekin cn = "%s" tls_www_server tls_www_client encryption_key signing_key ''' % (serverIP) infoFile = open('/etc/pki/libvirt/' + serverIP + '.info', 'w') infoFile.write(infoString) infoFile.close() #generate request from private key result = subprocess.Popen(shlex.split( "certtool --generate-request --load-privkey /etc/pki/libvirt/private/serverkey.pem --template /etc/pki/libvirt/%s.info --outfile /etc/pki/libvirt/%s.req" % (serverIP, serverIP)), stdout=subprocess.PIPE) result.wait() requestString = open('/etc/pki/libvirt/' + serverIP + '.req', 'r').read() cerString = connection.socketCall(cerHostIP, setting.LOCAL_PORT, 'request_sign_certificate', [serverIP, infoString, requestString]) #write to file cerFile = open('/etc/pki/libvirt/servercert.pem', 'w') cerFile.write(cerString) cerFile.close() #copy (already test : this command doesn't ask when must overwrite) shutil.copy2('/etc/pki/libvirt/private/serverkey.pem', '/etc/pki/libvirt/private/clientkey.pem') shutil.copy2('/etc/pki/libvirt/servercert.pem', '/etc/pki/libvirt/clientcert.pem') #delete temporary file os.remove('/etc/pki/libvirt/' + serverIP + '.info') os.remove('/etc/pki/libvirt/' + serverIP + '.req') #then restart libvirtd (very important) #*step should be like this #general.runDaemonCommand("service mklocm stop") #do not stop because mklocm can resist error of libvirtd restart #debugger.countdown(10,"before restart libvirtd") print "service libvirtd restart" general.runDaemonCommand("service libvirtd stop", conn, s) general.runDaemonCommandUntilOK("service libvirtd start", conn, s) #time.sleep(2) #debugger.countdown(10,"after restart libvirtd") print "wait conn" vconn = libvirt.open(None) while vconn == None: time.sleep(1) vconn = libvirt.open(None) vconn.close() #debugger.countdown(10,"vconn.close()") #general.runDaemonCommand("service mklocm start",conn,s) #do not stop because mklocm can resist error of libvirtd restart return 'OK'
def run(self): closeSocketEvent.wait() #now this process is free from socket general.runDaemonCommand("service mklocd restart")