def createConfFolders(self,num): server_secure_port = 13807 client_secure_port = 14807 for i in range(num): self.createConfFolder(i) server_conf = ServerConf(confdir=self.serverConfs[i],reload=True) server_conf.set('server_https_port',str(server_secure_port)) server_conf.set('client_secure_port',str(client_secure_port)) server_conf.set('mode',"debug") client_conf = ConnectionBundle(confdir=self.serverConfs[i],reload=True) client_conf.set('client_secure_port',str(client_secure_port)) client_conf.set('client_https_port',str(server_secure_port)) client_secure_port +=1 server_secure_port +=1
def __init__(self,host=None,port=None,conf=None): self.conf = conf if self.conf==None: self.conf = ConnectionBundle() self.host = host self.port = port if self.host == None: self.host = self.conf.getClientHost() if self.port == None: self.port = self.conf.getServerSecurePort() self.require_certificate_authentication=True self.privateKey = self.conf.getPrivateKey() self.keychain = self.conf.getCaChainFile()
def testBroadCastMessage(self): self.create5NodeNetwork() #this way we initialize the configs for a specific server which lets us test method calls as as specific server clientConf = ConnectionBundle(confdir=self.serverConfs[0], reload=True) serverConf = ServerConf(confdir=self.serverConfs[0], reload=True) broadCastMessage = BroadcastMessage() #logs should mention that we are setting elements to cache broadCastMessage.updateNetworkTopology() clientConf = ConnectionBundle(confdir=self.serverConfs[1], reload=True) serverConf = ServerConf(confdir=self.serverConfs[1], reload=True) #this should be brought from the cache(check in the logs) ServerToServerMessage.getNetworkTopology()
def initiateConnectionBundle(conffile): cf = None try: cf = ConnectionBundle(conffile) return cf except NoConfError: print "Could not find a connection bundle \nPlease specify one with " \ "with the -c flag or supply the file with the name\nclient.cnx" \ " in your configuration folder " sys.exit(1)
def testStart2Servers(self): numServers = 2 self.createConfFolders(numServers) hostname = gethostname() node0HttpsPort = 13807 node1HttpsPort = 13808 node0HttpPort = 14807 node1HttpPort = 14808 for i in range(numServers): args = [ '../../../../cpc-server', '-c', self.serverConfs[i], 'start' ] #doing cpc.server.server.forkAndRun(cf, debug ) directly here will will for some strange reason mess up things when shutting down, the process wont shutdown subprocess.call(args) time.sleep(2) #connect node 0 to node 1 args = [ '../../../../cpc-server', '-c', self.serverConfs[0], 'connnect-server', hostname, str(node1HttpPort) ] #doing cpc.server.server.forkAndRun(cf, debug ) directly here will will for some strange reason mess up things when shutting down, the process wont shutdown subprocess.call(args) args = [ '../../../../cpc-server', '-c', self.serverConfs[1], 'trust', hostname, str(node0HttpsPort) ] #doing cpc.server.server.forkAndRun(cf, debug ) directly here will will for some strange reason mess up things when shutting down, the process wont shutdown subprocess.call(args) #verify existense of of nodes in each conf file conf1 = ServerConf(confdir=self.serverConfs[0], reload=True) node0Nodes = conf1.getNodes() self.assertTrue(node0Nodes.exists(hostname, node1HttpsPort)) conf2 = ServerConf(confdir=self.serverConfs[1], reload=True) node1Nodes = conf2.getNodes() self.assertTrue(node1Nodes.exists(hostname, node0HttpsPort)) #do a network topology call conf = ConnectionBundle(confdir=self.serverConfs[0], reload=True) client = ClientMessage() topology = ProcessedResponse(client.networkTopology()).getData() self.assertEquals(topology.size(), 2)
def testCall(self): self.create5NodeNetwork() #this way we initialize the configs for a specific server which lets us test method calls as as specific server clientConf = ConnectionBundle(confdir=self.serverConfs[0], reload=True) serverConf = ServerConf(confdir=self.serverConfs[0], reload=True) #building a test server request cmdstring = 'test-server' fields = [] input = Input('cmd', cmdstring) fields.append(input) request = ServerRequest.prepareRequest(fields, [], dict())
def testNetworkTopology(self): self.create5NodeNetwork() conf = ConnectionBundle(confdir=self.serverConfs[0], reload=True) client = ClientMessage() topology = ProcessedResponse(client.networkTopology()).getData() self.assertEquals(topology.size(), 5) #verify that the topology is correct #node 0 should have 2 connections one to node 1 and one to node 2 node0 = "%s:%s" % (self.hostname, str(self.node0HttpsPort)) node = topology.get(node0) self.assertEquals(node.nodes.size(), 2) self.assertTrue(node.nodes.exists(self.hostname, self.node1HttpsPort)) self.assertTrue(node.nodes.exists(self.hostname, self.node2HttpsPort)) #node 1 should have 1 connection to node 2 node1 = "%s:%s" % (self.hostname, str(self.node1HttpsPort)) node = topology.get(node1) self.assertEquals(node.nodes.size(), 2) self.assertTrue(node.nodes.exists(self.hostname, self.node2HttpsPort)) #node2 should have 4 connections one to each other node node2 = "%s:%s" % (self.hostname, str(self.node2HttpsPort)) node = topology.get(node2) self.assertEquals(node.nodes.size(), 4) self.assertTrue(node.nodes.exists(self.hostname, self.node0HttpsPort)) self.assertTrue(node.nodes.exists(self.hostname, self.node1HttpsPort)) self.assertTrue(node.nodes.exists(self.hostname, self.node3HttpsPort)) self.assertTrue(node.nodes.exists(self.hostname, self.node4HttpsPort)) #node 3 should have 1 connection to node 2 node3 = "%s:%s" % (self.hostname, str(self.node3HttpsPort)) node = topology.get(node3) self.assertEquals(node.nodes.size(), 1) self.assertTrue(node.nodes.exists(self.hostname, self.node2HttpsPort)) #node 4 should have 1 connection to node 2 node4 = "%s:%s" % (self.hostname, str(self.node4HttpsPort)) node = topology.get(node4) self.assertEquals(node.nodes.size(), 1) self.assertTrue(node.nodes.exists(self.hostname, self.node2HttpsPort))
def testNetworkRoute(self): self.create5NodeNetwork() #send message from node 0 to 2 http conf = ConnectionBundle(confdir=self.serverConfs[0], reload=True) client = ClientMessage() response = ProcessedResponse( client.pingServer(self.hostname, self.node2HttpsPort)) print response.pprint() #send message from node 0 to 4 https response = ProcessedResponse( client.pingServer(self.hostname, self.node4HttpsPort)) print response.pprint() #send message from node 0 to 4 http #this throws an exception since we should not route with http response = ProcessedResponse( client.pingServer(self.hostname, self.node4Port)) print response.pprint()
def createConfFolders(self, num): server_secure_port = 13807 client_secure_port = 14807 for i in range(num): self.createConfFolder(i) server_conf = ServerConf(confdir=self.serverConfs[i], reload=True) server_conf.set('server_https_port', str(server_secure_port)) server_conf.set('client_secure_port', str(client_secure_port)) server_conf.set('mode', "debug") client_conf = ConnectionBundle(confdir=self.serverConfs[i], reload=True) client_conf.set('client_secure_port', str(client_secure_port)) client_conf.set('client_https_port', str(server_secure_port)) client_secure_port += 1 server_secure_port += 1
class WorkerMessage(ClientBase): ''' Messages not directly sent from a user of copernicus ''' def __init__(self,host=None,port=None,conf=None): self.conf = conf if self.conf==None: self.conf = ConnectionBundle() self.host = host self.port = port if self.host == None: self.host = self.conf.getClientHost() if self.port == None: self.port = self.conf.getServerSecurePort() self.require_certificate_authentication=True self.privateKey = self.conf.getPrivateKey() self.keychain = self.conf.getCaChainFile() def workerRequest(self, workerID, archdata): cmdstring='worker-ready' fields = [] fields.append(Input('cmd', cmdstring)) fields.append(Input('version', "1")) fields.append(Input('worker', archdata)) fields.append(Input('worker-id', workerID)) headers = dict() response= self.putRequest(ServerRequest.prepareRequest(fields, [], headers)) return response def commandFinishedRequest(self, cmdID, origServer, returncode, cputime, jobTarFileobj): cmdstring='command-finished' fields = [] fields.append(Input('cmd', cmdstring)) fields.append(Input('version', "2")) fields.append(Input('cmd_id', cmdID)) fields.append(Input('project_server', origServer)) if returncode is not None: fields.append(Input('return_code', str(returncode))) fields.append(Input('used_cpu_time', str(cputime))) jobTarFileobj.seek(0) files = [FileInput('run_data','cmd.tar.gz',jobTarFileobj)] headers = dict() # TODO we directly forward to the originating server. We don't have to # because there's active relaying, but for now this simplifies things headers['server-id'] = origServer log.debug("sending command finished for cmd id %s"%cmdID) response= self.putRequest(ServerRequest.prepareRequest(fields, files, headers)) return response def workerHeartbeatRequest(self, workerID, workerDir, first, last, changed, heartbeatItemsXML): cmdstring='worker-heartbeat' if first: iteration="first" elif last: iteration="last" elif changed: iteration="update" else: iteration="none" fields = [] fields.append(Input('cmd', cmdstring)) fields.append(Input('version', "2")) fields.append(Input('worker_id', workerID)) fields.append(Input('worker_dir', workerDir)) fields.append(Input('iteration', iteration)) fields.append(Input('heartbeat_items', heartbeatItemsXML)) response= self.putRequest(ServerRequest.prepareRequest(fields, [])) return response
def setupClient(self): ''' Creates a connection bundle for the Client and worker @returns ConnectionBundle ''' connectionBundle = ConnectionBundle(create=True, fqdn=self.cn) serverConf = ServerConf() #generate random ascii string randstring = ''.join( random.choice(string.ascii_uppercase + string.digits) for x in range(6)) tempDir = "%s/tmp/%s" % (self.conf.getConfDir(), randstring) privKeyFile = "%s/priv.pem" % tempDir pubKeyFile = "%s/pub.pem" % tempDir certReqConfigFile = "%s/cert_req.txt" % tempDir certFile = "%s/cert.pem" % tempDir os.makedirs(tempDir) #we create a temp dir for intermediate files self._generateKeyPair(privKeyFile=privKeyFile, pubKeyFile=pubKeyFile) self._generateCertReqConf( distinguished_cn="%s_%s" % (connectionBundle.CN_ID, self.cn), certReqConfigFile=certReqConfigFile) self._generateCert(privKeyFile, certFile, certReqConfigFile) #now we need to read everything in to the connection bundle connectionBundle.setPrivateKey(open(privKeyFile, 'r').read()) connectionBundle.setPublicKey(open(pubKeyFile, 'r').read()) connectionBundle.setCert(open(certFile, 'r').read()) connectionBundle.setCaCert(open(self.conf.getCACertFile(), "r").read()) shutil.rmtree(tempDir) connectionBundle.setClientSecurePort( serverConf.getClientSecurePort()) connectionBundle.setServerSecurePort( serverConf.getServerSecurePort()) connectionBundle.setHostname(ServerConf().getHostName()) return connectionBundle