def test_cobra_authentication(self): testobj = c_tests.TestObject() daemon = cobra.CobraDaemon(port=60601) daemon.setAuthModule(c_auth.CobraAuthenticator()) daemon.fireThread() objname = daemon.shareObject(testobj) # Lets fail because of no-auth first try: p = cobra.CobraProxy('cobra://localhost:60601/%s' % objname) raise Exception('Allowed un-authd connection!') except cobra.CobraAuthException as e: pass # Now fail with wrong auth try: p = cobra.CobraProxy('cobra://localhost:60601/%s' % objname, authinfo={}) raise Exception('Allowed bad-auth connection!') except cobra.CobraAuthException as e: pass # Now lets succeed authinfo = {'user': '******', 'passwd': 'secret'} t = cobra.CobraProxy('cobra://localhost:60601/%s' % objname, authinfo=authinfo) c_tests.accessTestObject(t) daemon.stopServer()
def getTrace(self): trace = vtrace.getTrace() host, port = cobra.getLocalInfo() unique = vtrace.cobra_daemon.shareObject(trace) trace.proxy = cobra.CobraProxy("cobra://%s:%d/%s" % (host, port, unique)) return unique
def getServerObject(self, objname): ''' Get a CobraProxy for an object shared by the ClusterServer (eg. a VivWorkspace) ''' uri = self.server.getServerObjectUri(objname) proxy = cobra.CobraProxy(uri, timeout=60, retrymax=3) return proxy
def getAndDoWork(uri): proxy = cobra.CobraProxy(uri) work = proxy.getWork() # If we got work, do it. if work != None: work.work() proxy.doneWork(work)
def getAndDoWork(uri, docode=False): # If we wanna use dcode, set it up logger.debug("getAndDoWork: uri=", uri) try: if docode: host, port = getHostPortFromUri(uri) cobra.dcode.addDcodeServer(host, port=port) # Use a cobra proxy with timeout/maxretry so we # don't hang forever if the server goes away proxy = cobra.CobraProxy(uri, timeout=60, retrymax=3) work = proxy.getWork() # If we got work, do it. if work is not None: runAndWaitWork(proxy, work) except Exception: logger.error(traceback.format_exc()) # Any way it goes we wanna exit now. Work units may have # spun up non-daemon threads, so lets GTFO. gc.collect() # Try to call destructors sys.exit(0) # GTFO
def connectToServer(hostname): builder = cobra.initSocketBuilder(hostname, viv_port) builder.setTimeout(timeo_sock) server = cobra.CobraProxy("cobra://%s:%d/VivServer" % (hostname, viv_port), msgpack=True) version = server.getServerVersion() if version != server_version: raise Exception('Incompatible Server: his ver: %d our ver: %d' % (version, server_version)) return server
def getTrace(self): trace = vtrace.getTrace() host, port = cobra.getLocalInfo() unique = md5(os.urandom(20)).hexdigest() vtrace.cobra_daemon.shareObject(trace, unique) trace.proxy = cobra.CobraProxy("cobra://%s:%d/%s" % (host, port, unique)) return unique
def __init__(self, uri): object.__init__(self) if not cobra.isCobraUri(uri): raise ImportError try: self.cobra = cobra.CobraProxy(uri) except Exception, e: raise ImportError
def addClusterQueen(self, queenhost): ''' Inform the ClusterServer about the presence of a ClusterQueen instance on the given host. When the ClusterServer begins to announce work, he will do so in "infrastructure mode" and ask any set queens for help. ''' queen = cobra.CobraProxy('cobra://%s:%d/ClusterQueen' % (queenhost, queen_port)) self.queens.append(queen)
def msgpacktest(): testobj = c_unittests.TestObject() daemon = cobra.CobraDaemon(port=60610, msgpack=True) objname = daemon.shareObject( testobj ) daemon.fireThread() t = cobra.CobraProxy('cobra://localhost:60610/%s?msgpack=1' % objname) c_unittests.accessTestObject( t )
def basictest(): testobj = c_unittests.TestObject() daemon = cobra.CobraDaemon(port=60600) objname = daemon.shareObject( testobj ) daemon.fireThread() t = cobra.CobraProxy('cobra://localhost:60600/%s' % objname) c_unittests.accessTestObject( t )
def __init__(self, path): self.symcaches = [] for path in path.split(';'): if os.path.isdir(path): self.symcaches.append(SymbolCache(dirname=path)) continue if path.startswith('cobra://') or path.startswith('cobrassl://'): self.symcaches.append(cobra.CobraProxy(path)) continue
def openSharedFile(self, filename): ''' A helper API to open a file like object on the server. Example: fd = self.openSharedFile('/foo/bar/baz') fbytes = fd.read() NOTE: The server must use shareFileToWorkers(). ''' uri = self.server.openSharedFile(filename) return cobra.CobraProxy(uri)
def test_cobra_proxy(self): testobj = c_tests.TestObject() daemon = cobra.CobraDaemon(port=60600) objname = daemon.shareObject(testobj) daemon.fireThread() t = cobra.CobraProxy('cobra://localhost:60600/%s' % objname) c_tests.accessTestObject(t) daemon.stopServer()
def reftest(): testobj = c_unittests.TestObject() daemon = cobra.CobraDaemon(port=60660) objname = daemon.shareObject(testobj, doref=True) daemon.fireThread() with cobra.CobraProxy('cobra://localhost:60660/%s' % objname) as t: c_unittests.accessTestObject(t) assert (daemon.getSharedObject(objname) == None)
def getCallbackProxy(trace, notifier): """ Get a proxy object to reference *notifier* from the perspective of *trace*. The trace is specified so we may check on our side of the connected socket to give him the best possible ip address... """ global callback_daemon port = getCallbackPort() host, nothing = cobra.getCobraSocket(trace).getSockName() unique = md5.md5(os.urandom(20)).hexdigest() callback_daemon.shareObject(notifier, unique) return cobra.CobraProxy("cobra://%s:%d/%s" % (host, port, unique))
def test_cobra_newobj(self): daemon = cobra.CobraDaemon(port=60500, msgpack=True) objname = daemon.shareObject(NewObjectReturn()) daemon.fireThread() t = cobra.CobraProxy('cobra://localhost:60500/%s?msgpack=1' % objname) with t.open() as fd: self.assertEqual(fd.read(), b'asdf') self.assertEqual(len(daemon.shared.keys()), 1) daemon.stopServer()
def test_cobra_refcount(self): testobj = c_tests.TestObject() daemon = cobra.CobraDaemon(port=60660) objname = daemon.shareObject(testobj, doref=True) daemon.fireThread() with cobra.CobraProxy('cobra://localhost:60660/%s' % objname) as t: c_tests.accessTestObject(t) self.assertIsNone(daemon.getSharedObject(objname)) daemon.stopServer()
def getCallbackProxy(trace, notifier): """ Get a proxy object to reference *notifier* from the perspective of *trace*. The trace is specified so we may check on our side of the connected socket to give him the best possible ip address... """ global callback_daemon port = getCallbackPort() host, nothing = trace._cobra_getsock().getSockName() unique = callback_daemon.getSharedName(notifier) if unique is None: unique = callback_daemon.shareObject(notifier) return cobra.CobraProxy("cobra://%s:%d/%s" % (host, port, unique))
def test_cobra_authentication(self): testobj = c_tests.TestObject() daemon = cobra.CobraDaemon(port=60601) daemon.setAuthModule(c_auth.CobraAuthenticator()) daemon.fireThread() objname = daemon.shareObject(testobj) # Lets fail because of no-auth first try: p = cobra.CobraProxy('cobra://localhost:60601/%s' % objname) raise Exception('Allowed un-authd connection!') except cobra.CobraAuthException, e: pass
def test_cobra_msgpack(self): try: import msgpack except ImportError: self.skipTest('No msgpack installed!') testobj = c_tests.TestObject() daemon = cobra.CobraDaemon(port=60610, msgpack=True) objname = daemon.shareObject(testobj) daemon.fireThread() t = cobra.CobraProxy('cobra://localhost:60610/%s?msgpack=1' % objname) c_tests.accessTestObject(t) daemon.stopServer()
def cobraPoolGetProxy(self, rotate=False): if rotate: self.uris.rotate(1) while True: try: uri = self.uris[0] proxy = self.proxycache.get(uri) if proxy is None: proxy = cobra.CobraProxy(uri) self.proxycache[uri] = proxy return proxy except Exception as e: self.uris.rotate(1) logger.warning('CobraPool Proxy Failure: %s %s', uri, e) time.sleep(self.faildelay) # FIXME track per?
def test_cobra_shadowauth(self): testobj = c_tests.TestObject() daemon = cobra.CobraDaemon(port=60602) shadowfile = c_tests.testFileName('shadowpass.txt') authmod = c_auth_shadow.ShadowFileAuth( shadowfile ) daemon.setAuthModule( authmod ) daemon.fireThread() objname = daemon.shareObject( testobj ) # Now lets succeed authinfo = { 'user':'******', 'passwd':'secret' } t = cobra.CobraProxy('cobra://localhost:60602/%s' % objname, authinfo=authinfo) c_tests.accessTestObject(t) self.assertEqual( t.getUser(), 'invisigoth') daemon.stopServer()
def getAndDoWork(uri, docode=False): # If we wanna use dcode, set it up try: if docode: host, port = getHostPortFromUri(uri) cobra.dcode.addDcodeServer(host, port=port) # Use a cobra proxy with timeout/maxretry so we # don't hang forever if the server goes away proxy = cobra.CobraProxy(uri, timeout=60, retrymax=3) work = proxy.getWork() # If we got work, do it. if work != None: runAndWaitWork(proxy, work) except Exception, e: traceback.print_exc()
def cobraPoolGetProxy(self, rotate=False): if rotate: self.uris.rotate(1) while True: try: uri = self.uris[0] proxy = self.proxycache.get(uri) if proxy == None: proxy = cobra.CobraProxy(uri) self.proxycache[uri] = proxy return proxy except Exception, e: # proxy construction error! self.uris.rotate(1) traceback.print_exc() print('CobraPool Proxy Failure: %s %s' % (uri, e)) time.sleep(self.faildelay) # FIXME track per?
def shadowauth(): testobj = c_unittests.TestObject() daemon = cobra.CobraDaemon(port=60602) shadowfile = os.path.join(os.path.dirname(__file__), 'shadowpass.txt') authmod = c_auth_shadow.ShadowFileAuth(shadowfile) daemon.setAuthModule(authmod) daemon.fireThread() objname = daemon.shareObject(testobj) # Now lets succeed authinfo = {'user': '******', 'passwd': 'secret'} t = cobra.CobraProxy('cobra://localhost:60602/%s' % objname, authinfo=authinfo) c_unittests.accessTestObject(t) assert (t.getUser() == 'invisigoth')
def _getAndRunApp(uri): # We dont want our *local* code, we want the remote code. cwd = os.getcwd() if cwd in sys.path: sys.path.remove(cwd) if '' in sys.path: sys.path.remove('') duri = cobra.swapCobraObject(uri, 'DcodeServer') cobra.dcode.addDcodeUri(duri) server = cobra.CobraProxy(uri) scheme, host, port, name, urlparams = cobra.chopCobraUri(uri) module = importlib.import_module(name) if hasattr(module, 'remotemain'): module.remotemain(server) else: module.main()
def __init__(self, uri, retrymax=3, timeout=10): object.__init__(self) if not cobra.isCobraUri(uri): raise ImportError path = None if uri.find("|") != -1: uri, path = uri.split("|") path = [ path, ] self.uri = uri self.path = path try: self.cobra = cobra.CobraProxy(uri, retrymax=retrymax, timeout=timeout) except Exception, e: raise ImportError
class CobraBasicTest(unittest.TestCase): #def setUp(self): #pass #def tearDown(self): #pass def test_cobra_proxy(self): testobj = c_tests.TestObject() daemon = cobra.CobraDaemon(port=60600) objname = daemon.shareObject(testobj) daemon.fireThread() t = cobra.CobraProxy('cobra://localhost:60600/%s' % objname) c_tests.accessTestObject(t) daemon.stopServer() def test_cobra_msgpack(self): try: import msgpack except ImportError, e: self.skipTest('No msgpack installed!') testobj = c_tests.TestObject() daemon = cobra.CobraDaemon(port=60610, msgpack=True) objname = daemon.shareObject(testobj) daemon.fireThread() t = cobra.CobraProxy('cobra://localhost:60610/%s?msgpack=1' % objname) c_tests.accessTestObject(t) daemon.stopServer()
import cobra cobra.verbose = True p = cobra.CobraProxy('cobra://localhost/woot') print p.printwoot('blah')