#!/usr/bin/python import getopt, os, sys, Queue, posix, posixpath, string sys.path.append("/usr/share/pydar/pydar") sys.path.append("pydar") from xmlrpclib import Server from SimpleXMLRPCServer import SimpleXMLRPCServer from log import Log from config import Config from thread import start_new_thread from command import Command from store import Store import time os.environ['HOME'] = '/root' class CommandsThread: def __init__(self,myLog,myConfig,myStore): METHOD_NAME = "CommandsThread(..) - " self.log = myLog self.config = myConfig self.store = myStore self.log.debug(METHOD_NAME + "start") def run(self): METHOD_NAME = "CommandsThread::run(..) - " self.log.debug(METHOD_NAME + "start") while True: self.log.debug(METHOD_NAME + "sleeping...") time.sleep(60) #item = self.queue.get() #self.log.debug(METHOD_NAME + "item: " + str(item)) #if item == "SVNUPDATE": # self.doSvnUpdate() #elif item == "CHECKALLSPECS": # self.doCheckAllSpecs() #else: # self.doBuildSpec(item) # #self.log.warn(METHOD_NAME + "unknown command: " + str(item)) def startCommandsThread(myLog,myConfig,myStore): myThread = CommandsThread(myLog,myConfig,myStore) myThread.run() class MasterRpc: def __init__(self,mylog,myconfig,mystore): METHOD_NAME = "MasterRpc::__init__(..) - " self.log = mylog self.config = myconfig self.store = mystore self.log.debug(METHOD_NAME + "initialized") def registerSlave(self,buildmachineId): METHOD_NAME = "MasterRpc::registerSlave(..) - " self.log.debug(METHOD_NAME + "start, buildmachineId=" + buildmachineId) if self.store.checkIfValidBuildMachineId(buildmachineId): self.store.registerSlave(buildmachineId) return "OK" else: return "UNKNOWN BUILDMACHINEID" def longWait(self): METHOD_NAME = "longWait(self) - " self.log.debug(METHOD_NAME + "start") time.sleep(200) self.log.debug(METHOD_NAME + "end") return "OK" def sendResultFileName(self, buildmachineid, commandId,fileName): METHOD_NAME = "sendResultFileName(..) - " self.log.debug(METHOD_NAME + "buildmachineid=" + buildmachineid + ",commandId=" + str(commandId) + ",fileName=" + fileName) if self.store.checkIfValidBuildMachineId(buildmachineid): self.store.addResultFileName(buildmachineid,commandId,fileName) return "OK" else: return "UNKNOWN BUILDMACHINEID" def sendBuildResult(self, buildmachineid, commandId, buildResult): METHOD_NAME = "sendBuildResult(..) - " self.log.debug(METHOD_NAME + "start,buildmachineid=" + buildmachineid + ",commandId=" + str(commandId) + ",buildResult=" + str(buildResult)) if self.store.checkIfValidBuildMachineId(buildmachineid): self.store.setBuildResult(buildmachineid,commandId,buildResult) return "OK" else: return "UNKNOWN BUILDMACHINEID" def addMachRoot(self,buildmachineId,machRoot): METHOD_NAME = "MasterRpc::addMachRoot(..) - " self.log.debug(METHOD_NAME + "start, buildmachineId=" + buildmachineId + ", machRoot=" + machRoot) if self.store.checkIfValidBuildMachineId(buildmachineId): self.store.addMachRoot(buildmachineId,machRoot) return "OK" else: return "UNKNOWN BUILDMACHINEID" def getCommand(self,buildmachineId): METHOD_NAME = "MasterRpc::getCommand(..) - " self.log.debug(METHOD_NAME + "start, buildmachineId=" + buildmachineId) if self.store.checkIfValidBuildMachineId(buildmachineId): aCommand = self.store.reserveCommand(buildmachineId) return aCommand else: return "UNKNOWN BUILDMACHINEID" def buildSpec(self, userId, specFileName, toEmail, releaseTag, machRoot): METHOD_NAME = "MasterRpc::buildSpec(..) - " self.log.debug(METHOD_NAME + "start, userId=" + userId + ",specFileName=" + specFileName + ",toEmail=" + toEmail + ",releaseTag=" + releaseTag + ",machRoot=" + machRoot) if self.store.checkIfValidUserId(userId): self.store.createCommand("BUILD",userId, specFileName,toEmail,releaseTag,machRoot) #self.commandQueue.put(aCommand) return "OK" else: return "UNKNOWN USERID" def testBuildSpec(self, userId, specFileContents, toEmail, releaseTag, machRoot): METHOD_NAME = "MasterRpc::buildSpec(..) - " self.log.debug(METHOD_NAME + "start, userId=" + userId + ",specFileContents.length=" + str(len(specFileContents)) + ",toEmail=" + toEmail + ",releaseTag=" + releaseTag + ",machRoot=" + machRoot) if self.store.checkIfValidUserId(userId): self.store.createCommand("TESTBUILD",userId, specFileContents,toEmail,releaseTag,machRoot) #self.commandQueue.put(aCommand) return "OK" else: return "UNKNOWN USERID" # def showQueueSize(self): # METHOD_NAME = "MasterRpc::showQueueSize(..) - " # self.log.debug(METHOD_NAME + "start, result=" + self.commandQueue.qsize() # return self.commandQueue.qsize() class Master: def __init__(self,mylog,myconfig): METHOD_NAME = "Master::__init__(..) - " self.log = mylog self.config = myconfig # open a connection to the db self.store = Store(self.log,self.config) self.log.debug(METHOD_NAME + "initialized") def run(self): METHOD_NAME = "Master::run(self) - " self.log.debug(METHOD_NAME + "start") ## create a queue for commands ##self.commandQueue = Queue.Queue() # start a thread which executes commands ##self.startCommandThread() # wait for commands from pydar-remote self.startListener() def startCommandThread(self): METHOD_NAME = "Master::startCommandThread(self) - " self.log.debug(METHOD_NAME + "start") start_new_thread(startCommandsThread, (self.log,self.config,self.store)) def startListener(self): # open a listen socket and wait for commands from the master METHOD_NAME = "Master::startListener(self) - " self.log.debug(METHOD_NAME + "start") self.log.debug(METHOD_NAME + "buildmasterhostname=" +self.config.buildmasterhostname + ",buildmasterport=" + str(self.config.buildmasterport)) myslaveserver = SimpleXMLRPCServer((self.config.buildmasterhostname,self.config.buildmasterport)) myslaveserver.register_instance(MasterRpc(self.log,self.config,self.store)) myslaveserver.serve_forever() def main(logFile): #try: myLog = Log(logFile) myConfig = Config() myInstance = Master(myLog,myConfig) myInstance.run() #except: # myLog.print_exc() # raise # code from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012 if __name__ == "__main__": if len(sys.argv) > 2 and sys.argv[1] == "-d": # do the UNIX double-fork magic, see Stevens' "Advanced # Programming in the UNIX Environment" for details (ISBN 0201563177) try: pid = os.fork() if pid > 0: # exit first parent sys.exit(0) except OSError, e: print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror) sys.exit(1) # decouple from parent environment os.chdir("/") os.setsid() os.umask(0) # do second fork try: pid = os.fork() if pid > 0: # exit from second parent, print eventual PID before print "Daemon PID %d" % pid sys.exit(0) except OSError, e: print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror) sys.exit(1) # start the daemon main loop main('/var/log/pydar-buildserver-master') else: main(None)