async def genTempStormsvcProxy(cmdrcore, svcname, svcctor, conf=None): if conf is None: conf = {} conf = copy.deepcopy(conf) with s_common.getTempDir() as dirn: async with await svcctor(dirn, conf=conf) as svc: svc.dmon.share(svcname, svc) root = await svc.auth.getUserByName('root') await root.setPasswd('secret') info = await svc.dmon.listen('tcp://127.0.0.1:0/') svc.dmon.test_addr = info host, port = info surl = f'tcp://*****:*****@127.0.0.1:{port}/{svcname}' await cmdrcore.storm(f'service.add {svcname} {surl}') await cmdrcore.storm(f'$lib.service.wait({svcname})') async with svc.getLocalProxy() as prox: # Use object.__setattr__ to hulk smash and avoid proxy getattr magick object.__setattr__(prox, '_svc', svc) yield prox
async def getCell(ctor, conf): loc = s_dyndeps.getDynLocal(ctor) if loc is None: raise s_exc.NoSuchCtor(mesg=f'Unable to resolve ctor [{ctor}]', ctor=ctor) with s_common.getTempDir() as dirn: async with await loc.anit(dirn, conf=conf) as cell: yield cell
async def genTempCoreProxy(mods=None): '''Get a temporary cortex proxy.''' with s_common.getTempDir() as dirn: async with await s_cortex.Cortex.anit(dirn) as core: if mods: for mod in mods: await core.loadCoreModule(mod) async with core.getLocalProxy() as prox: yield prox
async def genTempCoreProxy(mods=None): '''Get a temporary cortex proxy.''' with s_common.getTempDir() as dirn: async with await s_cortex.Cortex.anit(dirn) as core: if mods: for mod in mods: await core.loadCoreModule(mod) async with core.getLocalProxy() as prox: # Use object.__setattr__ to hulk smash and avoid proxy getattr magick object.__setattr__(prox, '_core', core) yield prox
async def genTempCoreProxy(mods=None): '''Get a temporary cortex proxy.''' with s_common.getTempDir() as dirn: async with await s_cortex.Cortex.anit(dirn) as core: if mods: for mod in mods: await core.loadCoreModule(mod) async with core.getLocalProxy() as prox: # Use object.__setattr__ to hulk smash and avoid proxy getattr magick object.__setattr__(prox, '_core', core) yield prox
def getTempCortex(mods=None): ''' Get a proxy to a cortex backed by a temporary directory. Args: mods (list): A list of modules which are loaded into the cortex. Notes: The cortex and temporary directory are town down on exit. This should only be called from synchronous code. Returns: Proxy to the cortex. ''' with s_common.getTempDir() as dirn: with s_coro.AsyncToSyncCMgr(s_glob.sync, Cortex.anit(dirn)) as core: if mods: for mod in mods: s_glob.sync(core.loadCoreModule(mod)) with s_coro.AsyncToSyncCMgr(core.getLocalProxy) as prox: yield prox
async def addHttpsPort(self, port, host='0.0.0.0', sslctx=None): addr = socket.gethostbyname(host) if sslctx is None: pkeypath = os.path.join(self.dirn, 'sslkey.pem') certpath = os.path.join(self.dirn, 'sslcert.pem') if not os.path.isfile(certpath): logger.warning('NO CERTIFICATE FOUND! generating self-signed certificate.') with s_common.getTempDir() as dirn: cdir = s_certdir.CertDir(dirn) pkey, cert = cdir.genHostCert('cortex') cdir.savePkeyPem(pkey, pkeypath) cdir.saveCertPem(cert, certpath) sslctx = self.initSslCtx(certpath, pkeypath) serv = self.wapp.listen(port, address=addr, ssl_options=sslctx) self.httpds.append(serv) return list(serv._sockets.values())[0].getsockname()
async def addHttpsPort(self, port, host='0.0.0.0', sslctx=None): addr = socket.gethostbyname(host) if sslctx is None: pkeypath = os.path.join(self.dirn, 'sslkey.pem') certpath = os.path.join(self.dirn, 'sslcert.pem') if not os.path.isfile(certpath): logger.warning('NO CERTIFICATE FOUND! generating self-signed certificate.') with s_common.getTempDir() as dirn: cdir = s_certdir.CertDir(dirn) pkey, cert = cdir.genHostCert('cortex') cdir.savePkeyPem(pkey, pkeypath) cdir.saveCertPem(cert, certpath) sslctx = self.initSslCtx(certpath, pkeypath) serv = self.wapp.listen(port, address=addr, ssl_options=sslctx) self.httpds.append(serv) return list(serv._sockets.values())[0].getsockname()
async def __anit__(self, core, layers=None): await s_base.Base.__anit__(self) if layers is None: layers = core.layers.values() self.core = core self.layers = layers self.dirn = await self.enter_context(s_common.getTempDir()) path = os.path.join(self.dirn, 'migr.lmdb') self.slab = await s_lmdbslab.Slab.anit(path) self.oldb2indx = self.slab.initdb('oldb2indx') self.onfini(self.slab.fini) self.ndefdelay = None for layr in layers: await self.enter_context(layr.disablingBuidCache())
async def cortex(): base = await s_base.Base.anit() dirn = await base.enter_context(s_common.getTempDir()) core = await s_cortex.Cortex.anit(dirn) core.onfini(base.fini) return core
async def getCell(ctor, conf): with s_common.getTempDir() as dirn: loc = s_dyndeps.getDynLocal(ctor) async with await loc.anit(dirn, conf=conf) as cell: yield cell
async def getTempSlab(self): with s_common.getTempDir() as dirn: path = os.path.join(dirn, 'migrate.lmdb') async with await s_lmdbslab.Slab.anit(path) as slab: yield slab
async def getTempSlab(self): opts = {'map_async': True} with s_common.getTempDir() as dirn: path = os.path.join(dirn, 'migrate.lmdb') async with await s_lmdbslab.Slab.anit(path, **opts) as slab: yield slab