Пример #1
0
    def test_service_base(self):
        sbus = s_service.SvcBus()

        dmon = s_daemon.Daemon()
        dmon.share('syn.svcbus', sbus)

        link = dmon.listen('tcp://127.0.0.1:0/')

        port = link[1].get('port')

        prox = s_telepath.openurl('tcp://127.0.0.1/syn.svcbus', port=port)

        woot = Woot()

        s_service.runSynSvc('syn.woot', woot, prox)

        svcs = prox.getSynSvcs()

        self.assertEqual( len(svcs), 1 )
        self.assertEqual( svcs[0][1].get('name'), 'syn.woot' )

        dyntask = s_async.newtask('foo',10,y=30)

        job = prox.callx(svcs[0][0], dyntask)
        self.assertEqual( prox.sync(job), 40 )

        prox.fini()
        dmon.fini()
        sbus.fini()
Пример #2
0
    def test_service_base(self):

        with s_daemon.Daemon() as dmon:

            sbus = s_service.SvcBus()
            dmon.share('syn.svcbus', sbus, fini=True)

            link = dmon.listen('tcp://127.0.0.1:0/')

            port = link[1].get('port')

            with s_telepath.openurl('tcp://127.0.0.1/syn.svcbus',
                                    port=port) as prox:

                woot = Woot()

                s_service.runSynSvc('syn.woot', woot, prox)

                svcs = prox.getSynSvcs()

                self.eq(len(svcs), 1)
                self.eq(svcs[0][1].get('name'), 'syn.woot')

                dyntask = s_async.newtask('foo', 10, y=30)

                job = prox.callx(svcs[0][0], dyntask)
                self.eq(prox.syncjob(job), 40)
Пример #3
0
    def test_service_base(self):
        sbus = s_service.SvcBus()

        dmon = s_daemon.Daemon()
        dmon.share('syn.svcbus', sbus)

        link = dmon.listen('tcp://127.0.0.1:0/')

        port = link[1].get('port')

        prox = s_telepath.openurl('tcp://127.0.0.1/syn.svcbus', port=port)

        woot = Woot()

        s_service.runSynSvc('syn.woot', woot, prox)

        svcs = prox.getSynSvcs()

        self.assertEqual( len(svcs), 1 )
        self.assertEqual( svcs[0][1].get('name'), 'syn.woot' )

        dyntask = s_async.newtask('foo',10,y=30)

        job = prox.callx(svcs[0][0], dyntask)
        self.assertEqual( prox.syncjob(job), 40 )

        prox.fini()
        dmon.fini()
        sbus.fini()
Пример #4
0
    def getSwarmEnv(self):
        tenv = TstEnv()

        core0 = s_cortex.openurl('ram://')
        core1 = s_cortex.openurl('ram://')

        self.addTstForms(core0)
        self.addTstForms(core1)

        tenv.add('core0', core0, fini=True)
        tenv.add('core1', core1, fini=True)

        tufo0 = core0.formTufoByProp('strform', 'baz', foo='visi')
        tufo1 = core0.formTufoByProp('strform', 'faz', foo='visi')
        tufo2 = core1.formTufoByProp('strform', 'lol', foo='visi')
        tufo3 = core1.formTufoByProp('strform', 'hai', foo='visi')

        tufo4 = core0.formTufoByProp('intform', 10, foo='visi')
        tufo5 = core1.formTufoByProp('intform', 12, foo='romp')

        tenv.add('tufo0', tufo0)
        tenv.add('tufo1', tufo1)
        tenv.add('tufo2', tufo2)
        tenv.add('tufo3', tufo3)

        dmon = s_daemon.Daemon()
        link = dmon.listen('tcp://127.0.0.1:0')

        tenv.add('link', link)
        tenv.add('dmon', dmon, fini=True)

        port = link[1].get('port')

        svcbus = s_service.SvcBus()
        tenv.add('svcbus', svcbus, fini=True)

        dmon.share('syn.svcbus', svcbus)

        svcrmi = s_telepath.openurl('tcp://127.0.0.1/syn.svcbus', port=port)
        tenv.add('svcrmi', svcrmi, fini=True)

        s_service.runSynSvc('cortex', core0, svcrmi, tags=('hehe.haha', ))
        s_service.runSynSvc('cortex', core1, svcrmi, tags=('hehe.hoho', ))

        runt = s_runtime.Runtime(svcrmi)

        tenv.add('runt', runt, fini=True)

        return tenv
Пример #5
0
    def test_service_proxy(self):

        with s_daemon.Daemon() as dmon:

            sbus = s_service.SvcBus()
            dmon.share('syn.svcbus', sbus, fini=True)

            link = dmon.listen('tcp://127.0.0.1:0/')

            port = link[1].get('port')

            with s_telepath.openurl('tcp://127.0.0.1/syn.svcbus',
                                    port=port) as prox:

                woot0 = Woot()
                woot1 = Woot()

                s_service.runSynSvc('woots.woot0', woot0, prox)
                s_service.runSynSvc('woots.woot1',
                                    woot1,
                                    prox,
                                    tags=('foo.bar', ))

                svcp = s_service.SvcProxy(prox)

                foos = svcp.getTagProxy('foo')
                woots = svcp.getTagProxy('woots')

                vals = tuple(sorted(foos.foo(20, y=20)))
                self.eq(vals, (40, ))

                vals = tuple(sorted(woots.foo(10, y=20)))
                self.eq(vals, (30, 30))

                def runNewpMeth():
                    for foo in woots.newp(44, y=33):
                        pass

                self.eq(2, len(svcp.getSynSvcs()))
                self.eq(2, len(svcp.getSynSvcsByTag('woots')))

                woots = svcp.getTagProxy(
                    'class.synapse.tests.test_lib_service.Woot')

                vals = tuple(sorted(woots.foo(10, y=20)))
                self.eq(vals, (30, 30))
Пример #6
0
    def getSwarmEnv(self):
        tenv = TestEnv()

        core0 = s_cortex.openurl('ram://')
        core1 = s_cortex.openurl('ram://')

        tenv.add('core0',core0,fini=True)
        tenv.add('core1',core1,fini=True)

        tufo0 = core0.formTufoByProp('foo:bar','baz',vvv='visi')
        tufo1 = core0.formTufoByProp('foo:bar','faz',vvv='visi')
        tufo2 = core1.formTufoByProp('foo:bar','lol',vvv='visi')
        tufo3 = core1.formTufoByProp('foo:bar','hai',vvv='visi')

        tufo4 = core0.formTufoByProp('zzz:woot',10,vvv='visi')
        tufo5 = core1.formTufoByProp('zzz:woot',12,vvv='romp')

        tenv.add('tufo0',tufo0)
        tenv.add('tufo1',tufo1)
        tenv.add('tufo2',tufo2)
        tenv.add('tufo3',tufo3)

        dmon = s_daemon.Daemon()
        link = dmon.listen('tcp://127.0.0.1:0')

        tenv.add('link',link)
        tenv.add('dmon',dmon,fini=True)

        port = link[1].get('port')

        svcbus = s_service.SvcBus()
        tenv.add('svcbus',svcbus,fini=True)

        dmon.share('syn.svcbus',svcbus)

        svcrmi = s_telepath.openurl('tcp://127.0.0.1/syn.svcbus', port=port)
        tenv.add('svcrmi',svcrmi,fini=True)

        s_service.runSynSvc('cortex',core0,svcrmi,tags=('hehe.haha',))
        s_service.runSynSvc('cortex',core1,svcrmi,tags=('hehe.hoho',))

        runt = s_runtime.Runtime(svcrmi)

        tenv.add('runt',runt,fini=True)

        return tenv
Пример #7
0
    def test_service_proxy(self):
        sbus = s_service.SvcBus()

        dmon = s_daemon.Daemon()
        dmon.share('syn.svcbus', sbus)

        link = dmon.listen('tcp://127.0.0.1:0/')

        port = link[1].get('port')

        prox = s_telepath.openurl('tcp://127.0.0.1/syn.svcbus', port=port)

        woot0 = Woot()
        woot1 = Woot()

        s_service.runSynSvc('woots.woot0', woot0, prox)
        s_service.runSynSvc('woots.woot1', woot1, prox, tags=('foo.bar',))

        svcp = s_service.SvcProxy(prox)

        foos = svcp.getTagProxy('foo')
        woots = svcp.getTagProxy('woots')

        vals = tuple( sorted( foos.foo(20,y=20) ) )
        self.assertEqual( vals, (40,) )

        vals = tuple( sorted( woots.foo(10,y=20) ) )
        self.assertEqual( vals, (30,30) )

        def runNewpMeth():
            for foo in woots.newp(44,y=33):
                pass

        self.assertEqual( 2, len(svcp.getSynSvcs()) )
        self.assertEqual( 2, len(svcp.getSynSvcsByTag('woots')) )

        #self.assertRaises( JobErr, runNewpMeth )

        woots = svcp.getTagProxy('class.synapse.tests.test_service.Woot')

        vals = tuple( sorted( woots.foo(10,y=20) ) )
        self.assertEqual( vals, (30,30) )

        prox.fini()
        dmon.fini()
        sbus.fini()
Пример #8
0
    def getSwarmEnv(self):
        tenv = TestEnv()

        core0 = s_cortex.openurl("ram://")
        core1 = s_cortex.openurl("ram://")

        tenv.add("core0", core0, fini=True)
        tenv.add("core1", core1, fini=True)

        tufo0 = core0.formTufoByProp("foo:bar", "baz", vvv="visi")
        tufo1 = core0.formTufoByProp("foo:bar", "faz", vvv="visi")
        tufo2 = core1.formTufoByProp("foo:bar", "lol", vvv="visi")
        tufo3 = core1.formTufoByProp("foo:bar", "hai", vvv="visi")

        tufo4 = core0.formTufoByProp("zzz:woot", 10, vvv="visi")
        tufo5 = core1.formTufoByProp("zzz:woot", 12, vvv="romp")

        tenv.add("tufo0", tufo0)
        tenv.add("tufo1", tufo1)
        tenv.add("tufo2", tufo2)
        tenv.add("tufo3", tufo3)

        dmon = s_daemon.Daemon()
        link = dmon.listen("tcp://127.0.0.1:0")

        tenv.add("link", link)
        tenv.add("dmon", dmon, fini=True)

        port = link[1].get("port")

        svcbus = s_service.SvcBus()
        tenv.add("svcbus", svcbus, fini=True)

        dmon.share("syn.svcbus", svcbus)

        svcrmi = s_telepath.openurl("tcp://127.0.0.1/syn.svcbus", port=port)
        tenv.add("svcrmi", svcrmi, fini=True)

        s_service.runSynSvc("cortex", core0, svcrmi, tags=("hehe.haha",))
        s_service.runSynSvc("cortex", core1, svcrmi, tags=("hehe.hoho",))

        runt = s_runtime.Runtime(svcrmi)

        tenv.add("runt", runt, fini=True)

        return tenv
Пример #9
0
    def loadDmonConf(self, conf):
        '''
        '''
        for path in conf.get('includes',()):
            fullpath = os.path.expanduser(path)
            self.loadDmonFile(fullpath)

        self._addConfValu(conf,'poolsize')

        for name,url in conf.get('ctors',()):

            item = s_telepath.evalurl(url,locs=self.locs)
            self.locs[name] = item

            self.fire('dmon:conf:ctor', name=name, item=item)

        for name,url in conf.get('addons',()):

            try:
                item = s_telepath.evalurl(url,locs=self.locs)
            except Exception as e:
                raise Exception('DmonConf addon (%s): %s %s' % (name,e.__class__.__name__,e))

            self.addons[name] = item

            self.fire('dmon:conf:addon', name=name, item=item)

        svcbus = self.addons.get('syn.svcbus')

        svcruns = conf.get('svc:run',())
        if svcruns and not svcbus:
            raise Exception('svc:run with no syn.svcbus addon!')

        for name,opts in svcruns:
            item = self.locs.get(name)
            if item == None:
                raise NoSuchObj(name)

            tags = opts.get('tags',())
            svcname = opts.get('name',name)

            s_service.runSynSvc(svcname, item, svcbus, tags=tags)
Пример #10
0
    def loadDmonConf(self, conf):
        '''
        {
            'title':'The Foo Thing',

            'vars':{
                'foo':10,
                'bar':["baz","faz"]
            }

            'forks':(
                ('name',{
                    # nested config for forked dmon
                },
            ),

            'includes':(
                '~/foo/config.json',
            ),

            'ctors':(
                ('baz','ctor://foo.bar.Baz()'),
                ('mybus','tcp://host.com/syn.svcbus'),
            ),

            'services':(
                ('mybus',(
                    ('baz',{'tags':('foo.bar','baz')}),
                ),
            ),
        }
        '''
        checkConfDict(conf)
        self.locs.update( conf.get('vars',{}) )

        # handle forks first to prevent socket bind weirdness
        for name,subconf in conf.get('forks',()):
            self._fireDmonFork(name,subconf)

        title = conf.get('title')
        if title != None:
            s_thisplat.setProcName('dmon: %s' % title)

        # handle includes next
        for path in conf.get('includes',()):
            fullpath = os.path.expanduser(path)
            try:
                self.loadDmonFile(fullpath)
            except Exception as e:
                raise Exception('Include Error (%s): %s' % (path,e))

        self._addConfValu(conf,'poolsize')

        for name,url in conf.get('ctors',()):

            item = s_telepath.evalurl(url,locs=self.locs)
            self.locs[name] = item

            self.fire('dmon:conf:ctor', name=name, item=item)

        for busname,svcruns in conf.get('services',()):
            svcbus = self.locs.get(busname)
            if svcbus == None:
                raise NoSuchObj(name)

            for svcname,svcopts in svcruns:
                item = self.locs.get(svcname)
                if item == None:
                    raise NoSuchObj(svcname)

                tags = svcopts.get('tags',())
                svcname = svcopts.get('name',svcname)

                s_service.runSynSvc(svcname, item, svcbus, tags=tags)
Пример #11
0
    def _loadDmonConf(self, conf):

        checkConfDict(conf)
        self.locs.update(conf.get('vars', {}))

        # handle forks first to prevent socket bind weirdness
        for name, subconf in conf.get('forks', ()):
            self._fireDmonFork(name, subconf)

        title = conf.get('title')
        if title is not None:
            s_thisplat.setProcName('dmon: %s' % title)

        # handle explicit module load requests
        for name, info in conf.get('modules', ()):
            modu = s_dyndeps.getDynMod(name)
            if modu is None:
                logger.warning('dmon mod not loaded: %s', name)

        # handle includes next
        for path in conf.get('includes', ()):
            fullpath = os.path.expanduser(path)
            try:
                self.loadDmonFile(fullpath)
            except Exception as e:
                raise Exception('Include Error (%s): %s' % (path, e))

        configs = conf.get('configs', {})

        for row in conf.get('ctors', ()):

            copts = {}  # ctor options
            if len(row) == 2:
                name, url = row
            elif len(row) == 3:
                name, url, copts = row
            else:
                raise Exception('Invalid ctor row: %r' % (row, ))

            if url.find('://') == -1:
                # this is a (name,dynfunc,config) formatted ctor...
                item = s_dyndeps.tryDynFunc(url, copts)
            else:
                item = self.dmoneval(url)

            self.locs[name] = item

            # check for a ctor opt that wants us to load a config dict by name
            cfgname = copts.get('config')
            if cfgname is not None:
                if not isinstance(item, s_config.Configable):
                    raise Exception('dmon ctor: %s does not support configs' %
                                    name)

                opts = configs.get(cfgname)
                if opts is None:
                    raise s_common.NoSuchConf(name=cfgname)

                item.setConfOpts(opts)

            # check for a ctor opt that wants us to "flatten" several configs in order
            cfgnames = copts.get('configs')
            if cfgnames is not None:

                if not isinstance(item, s_config.Configable):
                    raise Exception('dmon ctor: %s does not support configs' %
                                    name)

                opts = {}
                for cfgname in cfgnames:

                    cfgopts = configs.get(cfgname)
                    if cfgopts is None:
                        raise s_common.NoSuchConf(name=cfgname)

                    opts.update(cfgopts)

                item.setConfOpts(opts)

            # check for a match between config and ctor names
            opts = configs.get(name)
            if opts is not None:
                item.setConfOpts(opts)

            self.fire('dmon:conf:ctor', name=name, item=item)

        for busname, svcruns in conf.get('services', ()):

            svcbus = self.dmoneval(busname)
            if svcbus is None:
                raise s_common.NoSuchObj(name)

            for svcname, svcopts in svcruns:

                item = self.locs.get(svcname)
                if item is None:
                    raise s_common.NoSuchObj(svcname)

                svcname = svcopts.get('name', svcname)
                s_service.runSynSvc(svcname, item, svcbus, **svcopts)
Пример #12
0
    def loadDmonConf(self, conf):
        '''
        {
            'title':'The Foo Thing',

            'vars':{
                'foo':10,
                'bar':["baz","faz"]
            }

            'forks':(
                ('name',{
                    # nested config for forked dmon
                },
            ),

            'includes':(
                '~/foo/config.json',
            ),

            'ctors':(
                ('baz', 'ctor://foo.bar.Baz()'),
                ('faz', 'ctor://foo.bar.Baz()', {'config':'woot'}),
                ('mybus', 'tcp://host.com/syn.svcbus'),
            ),

            'services':(
                ('mybus',(
                    ('baz',{'tags':('foo.bar','baz')}),
                ),
            ),

            'addons':(
                ('auth','tcp://host.com:8899/synauth'),
                ('logging','ctor://mypkg.mymod.MyLogger()'),
            ),

            'configs':{
                'woot':{
                    'fooopt':10,
                },
            },
        }
        '''
        checkConfDict(conf)
        self.locs.update(conf.get('vars', {}))

        # handle forks first to prevent socket bind weirdness
        for name, subconf in conf.get('forks', ()):
            self._fireDmonFork(name, subconf)

        title = conf.get('title')
        if title != None:
            s_thisplat.setProcName('dmon: %s' % title)

        # handle includes next
        for path in conf.get('includes', ()):
            fullpath = os.path.expanduser(path)
            try:
                self.loadDmonFile(fullpath)
            except Exception as e:
                raise Exception('Include Error (%s): %s' % (path, e))

        configs = conf.get('configs', {})

        for row in conf.get('ctors', ()):

            copts = {}  # ctor options
            if len(row) == 2:
                name, url = row
            elif len(row) == 3:
                name, url, copts = row
            else:
                raise Exception('Invalid ctor row: %r' % (row, ))

            item = self.dmoneval(url)
            self.locs[name] = item

            # check for a ctor opt that wants us to load a config dict by name
            cfgname = copts.get('config')
            if cfgname != None:
                if not isinstance(item, s_config.Configable):
                    raise Exception('dmon ctor: %s does not support configs' %
                                    name)

                opts = configs.get(cfgname)
                if opts == None:
                    raise NoSuchConf(name=cfgname)

                item.setConfOpts(opts)

            # check for a ctor opt that wants us to "flatten" several configs in order
            cfgnames = copts.get('configs')
            if cfgnames != None:

                if not isinstance(item, s_config.Configable):
                    raise Exception('dmon ctor: %s does not support configs' %
                                    name)

                opts = {}
                for cfgname in cfgnames:

                    cfgopts = configs.get(cfgname)
                    if cfgopts == None:
                        raise NoSuchConf(name=cfgname)

                    opts.update(cfgopts)

                item.setConfOpts(opts)

            # check for a match between config and ctor names
            opts = configs.get(name)
            if opts != None:
                item.setConfOpts(opts)

            self.fire('dmon:conf:ctor', name=name, item=item)

        for busname, svcruns in conf.get('services', ()):

            svcbus = self.dmoneval(busname)
            if svcbus == None:
                raise NoSuchObj(name)

            for svcname, svcopts in svcruns:
                item = self.locs.get(svcname)
                if item == None:
                    raise NoSuchObj(svcname)

                tags = svcopts.get('tags', ())
                svcname = svcopts.get('name', svcname)

                s_service.runSynSvc(svcname, item, svcbus, tags=tags)
Пример #13
0
    def loadDmonConf(self, conf):
        '''
        {
            'title':'The Foo Thing',

            'vars':{
                'foo':10,
                'bar':["baz","faz"]
            }

            'forks':(
                ('name',{
                    # nested config for forked dmon
                },
            ),

            'includes':(
                '~/foo/config.json',
            ),

            'ctors':(
                ('baz', 'ctor://foo.bar.Baz()'),
                ('faz', 'ctor://foo.bar.Baz()', {'config':'woot'}),
                ('mybus', 'tcp://host.com/syn.svcbus'),
            ),

            'services':(
                ('mybus',(
                    ('baz',{'tags':('foo.bar','baz')}),
                ),
            ),

            'addons':(
                ('auth','tcp://host.com:8899/synauth'),
                ('logging','ctor://mypkg.mymod.MyLogger()'),
            ),

            'configs':{
                'woot':{
                    'fooopt':10,
                },
            },
        }
        '''
        checkConfDict(conf)
        self.locs.update( conf.get('vars',{}) )

        # handle forks first to prevent socket bind weirdness
        for name,subconf in conf.get('forks',()):
            self._fireDmonFork(name,subconf)

        title = conf.get('title')
        if title != None:
            s_thisplat.setProcName('dmon: %s' % title)

        # handle includes next
        for path in conf.get('includes',()):
            fullpath = os.path.expanduser(path)
            try:
                self.loadDmonFile(fullpath)
            except Exception as e:
                raise Exception('Include Error (%s): %s' % (path,e))

        configs = conf.get('configs',{})

        for row in conf.get('ctors',()):

            copts = {}   # ctor options
            if len(row) == 2:
                name,url = row
            elif len(row) == 3:
                name,url,copts = row
            else:
                raise Exception('Invalid ctor row: %r' % (row,))

            item = self.dmoneval(url)
            self.locs[name] = item

            # check for a ctor opt that wants us to load a config dict by name
            cfgname = copts.get('config')
            if cfgname != None:
                if not isinstance(item,s_config.ConfigMixin):
                    raise Exception('dmon ctor: %s does not support configs' % name)

                opts = configs.get(cfgname)
                if opts == None:
                    raise NoSuchConf(name=cfgname)

                item.setConfOpts(opts)

            # check for a ctor opt that wants us to "flatten" several configs in order
            cfgnames = copts.get('configs')
            if cfgnames != None:

                if not isinstance(item,s_config.ConfigMixin):
                    raise Exception('dmon ctor: %s does not support configs' % name)

                opts = {}
                for cfgname in cfgnames:

                    cfgopts = configs.get(cfgname)
                    if cfgopts == None:
                        raise NoSuchConf(name=cfgname)

                    opts.update(cfgopts)

                item.setConfOpts(opts)

            self.fire('dmon:conf:ctor', name=name, item=item)

        for busname,svcruns in conf.get('services',()):

            svcbus = self.dmoneval(busname)
            if svcbus == None:
                raise NoSuchObj(name)

            for svcname,svcopts in svcruns:
                item = self.locs.get(svcname)
                if item == None:
                    raise NoSuchObj(svcname)

                tags = svcopts.get('tags',())
                svcname = svcopts.get('name',svcname)

                s_service.runSynSvc(svcname, item, svcbus, tags=tags)