def action(self): ''' Pull the queue for functions to execute ''' if not self.fun_in.value: return exchange = self.fun_in.value.pop() data = exchange['load'] match = getattr(self.matcher, '{0}_match'.format(data.get('tgt_type', 'glob')))(data['tgt']) if not match: return self.fun_ack.value.append(exchange) if 'user' in data: log.info('User {0[user]} Executing command {0[fun]} with jid ' '{0[jid]}'.format(data)) else: log.info( 'Executing command {0[fun]} with jid {0[jid]}'.format(data)) log.debug('Command details {0}'.format(data)) ex_yard = yarding.Yard(yid=data['jid'], prefix=self.opts['id'], dirpath=self.opts['sock_dir']) self.uxd_stack.value.addRemoteYard(ex_yard) self.executors[data['jid']] = {'yard': ex_yard} process = multiprocessing.Process(target=self.proc_run, args=(exchange)) process.start() # Don't join this process! The process daemonizes
def fudal(): ''' Make a single process raet uxd stack ''' lord_stack = stacking.StackUxd(name='lord', lanename='execute', yid=0, dirpath='/tmp') serf_stack = stacking.StackUxd(name='serf', lanename='execute', yid=1, dirpath='/tmp') lord_yard = yarding.Yard(yid=0, prefix='execute', dirpath='/tmp') #serf_yard = yarding.Yard(name=serf_stack.yard.name, prefix='execute') serf_stack.addRemoteYard(lord_yard) #print 'stack: {0}\nyid: {1}\nname: {2}\nha: {3}\ndirpath: {4}'.format(lord_stack.yard.stack, lord_stack.yard.yid, lord_stack.yard.name, lord_stack.yard.ha, lord_stack.yard.dirpath) #lord_stack.addRemoteYard(serf_yard) src = (ESTATE, serf_stack.yard.name, None) dst = (ESTATE, lord_stack.yard.name, None) route = {'src': src, 'dst': dst} msg = {'route': route, 'stuff': 'Serf to Lord, I am not a couch'} serf_stack.transmit(msg=msg) serf_stack.serviceAll() lord_stack.serviceAll()
def _register_event_yard(self, msg): ''' register an incoming event request with the requesting yard id ''' ev_yard = yarding.Yard(yid=msg['load']['yid'], prefix='master', dirpath=msg['load']['dirpath']) self.event_yards.value.add(ev_yard.name)
def __prep_stack(self): yid = salt.utils.gen_jid() self.stack = stacking.StackUxd(yid=yid, lanename=self.node, dirpath=self.sock_dir) self.router_yard = yarding.Yard(prefix='master', yid=0, dirpath=self.sock_dir) self.stack.addRemoteYard(self.router_yard) route = { 'dst': (None, self.router_yard.name, 'event_req'), 'src': (None, self.stack.yard.name, None) } msg = {'route': route, 'load': {'yid': yid, 'dirpath': self.sock_dir}} self.stack.transmit(msg) self.stack.serviceAll()
def __prep_stack(self): ''' Prepare the stack objects ''' self.stack = stacking.StackUxd(name='ex{0}'.format( self.opts['__ex_id']), lanename=self.opts['id'], yid=self.opts['__ex_id'], dirpath=self.opts['sock_dir']) self.router_yard = yarding.Yard(yid=0, prefix=self.opts['id'], dirpath=self.opts['sock_dir']) self.stack.addRemoteYard(self.router_yard) src = (self.opts['id'], self.stack.yard.name, None) dst = (self.opts['id'], 'router', None) self.route = {'src': src, 'dst': dst}
def _return_pub(self, ret): ''' Send the return data back via the uxd socket ''' ret_stack = stacking.StackUxd( lanename=self.opts['id'], yid=ret['jid'], dirpath=self.opts['sock_dir']) main_yard = yarding.Yard( yid=0, prefix=self.opts['id'], dirpath=self.opts['sock_dir'] ) ret_stack.addRemoteYard(main_yard) route = {'src': (self.opts['id'], ret_stack.yard.name, 'jid_ret'), 'dst': ('master', None, 'return')} msg = {'route': route, 'return': ret} ret_stack.transmit(msg, 'yard0') ret_stack.serviceAll()
def serf(lord_name, lord_yid, id_, dirpath): ''' Call to spawn a serf and start sending messages ''' serf_stack = stacking.StackUxd( name='serf{0}'.format(id_), lanename='execute', yid=id_, dirpath=dirpath) lord_yard = yarding.Yard(yid=lord_yid, prefix='execute', dirpath=dirpath) serf_stack.addRemoteYard(lord_yard) src = (ESTATE, serf_stack.yard.name, None) dst = (ESTATE, lord_name, None) route = {'src': src, 'dst': dst} msg = {'route': route, 'stuff': 'Serf {0} to Lord, I am not a couch'.format(id_)} while True: serf_stack.transmit(msg=msg) serf_stack.serviceAll() print 'serf messages transmitted' time.sleep(1)
def pub(self, tgt, fun, arg=(), expr_form='glob', ret='', jid='', timeout=5, **kwargs): ''' Publish the command! ''' payload_kwargs = self._prep_pub(tgt, fun, arg=(), expr_form='glob', ret='', jid='', timeout=5, **kwargs) yid = salt.utils.gen_jid() stack = stacking.StackUxd(yid=yid, lanename='master', dirpath=self.opts['sock_dir']) router_yard = yarding.Yard(prefix='master', yid=0, dirpath=self.opts['sock_dir']) stack.addRemoteYard(router_yard) route = { 'dst': (None, router_yard.name, 'local_cmd'), 'src': (None, stack.yard.name, None) } msg = {'route': route, 'load': payload_kwargs} stack.transmit(msg) stack.serviceAll() while True: time.sleep(0.001) stack.serviceAll() for msg in stack.rxMsgs: print msg return msg
def action(self): ''' Pull the queue for functions to execute ''' if not self.fun_in.value: return exchange = self.fun_in.value.popleft() data = exchange.get('pub') # convert top raw strings - take this out once raet is using msgpack for key, val in data.items(): if isinstance(val, basestring): data[str(key)] = str(val) else: data[str(key)] = val match = getattr( self.matcher, '{0}_match'.format( data.get('tgt_type', 'glob') ) )(data['tgt']) if not match: return if 'user' in data: log.info( 'User {0[user]} Executing command {0[fun]} with jid ' '{0[jid]}'.format(data)) else: log.info( 'Executing command {0[fun]} with jid {0[jid]}'.format(data) ) log.debug('Command details {0}'.format(data)) ex_yard = yarding.Yard( yid=data['jid'], prefix=self.opts['id'], dirpath=self.opts['sock_dir']) self.uxd_stack.value.addRemoteYard(ex_yard) process = multiprocessing.Process( target=self.proc_run, kwargs={'exchange': exchange} ) process.start() # Don't join this process! The process daemonizes
def lord(serfs=5): ''' Make a lord that can spawn serfs ''' lord_yid = 0 dirpath = '/tmp' lord_stack = stacking.StackUxd(name='lord', lanename='execute', yid=lord_yid, dirpath=dirpath) lord_stack.serviceAll() for serf_id in range(1, serfs + 1): serf_yard = yarding.Yard(yid=serf_id, prefix='execute', dirpath=dirpath) lord_stack.addRemoteYard(serf_yard) proc = multiprocessing.Process(target=serf, args=(lord_stack.yard.name, lord_yid, serf_id, dirpath)) proc.start() while True: lord_stack.serviceAll() print 'serviced lord stack' print lord_stack.rxMsgs for msg in lord_stack.rxMsgs: print msg time.sleep(1)
def lord(serfs=5): ''' Make a lord that can spawn serfs ''' lord_yid = 0 dirpath = '/tmp' lord_stack = stacking.StackUxd(name='lord', lanename='execute', yid=lord_yid, dirpath=dirpath) lord_stack.serviceAll() for serf_id in range(1, serfs + 1): serf_yard = yarding.Yard(yid=serf_id, prefix='execute', dirpath=dirpath) lord_stack.addRemoteYard(serf_yard) proc = multiprocessing.Process(target=serf, args=(lord_stack.yard.name, lord_yid, serf_id, dirpath)) proc.start() while True: lord_stack.serviceAll() print 'serviced lord stack' print lord_stack.rxMsgs while lord_stack.rxMsgs: rxmsg = lord_stack.rxMsgs.popleft() print rxmsg src = (ESTATE, lord_stack.yard.name, None) dst = (ESTATE, rxmsg['route']['src'][1], None) route = {'src': src, 'dst': dst} msg = { 'route': route, 'stuff': 'Master to Serf {0}, you stay'.format(rxmsg['route']['src'][1]) } lord_stack.transmit(msg) print lord_stack.yards time.sleep(1)
def testStackUxd(): ''' initially ''' console.reinit(verbosity=console.Wordage.concise) #lord stack #yard0 = yarding.Yard(name='lord') stack0 = stacking.StackUxd() #serf stack #yard1 = yarding.Yard(name='serf', yid=1) stack1 = stacking.StackUxd() stack0.addRemoteYard(stack1.yard) stack1.addRemoteYard(stack0.yard) print "{0} yard name={1} ha={2}".format(stack0.name, stack0.yard.name, stack0.yard.ha) print "{0} yards=\n{1}".format(stack0.name, stack0.yards) print "{0} names=\n{1}".format(stack0.name, stack0.names) print "{0} yard name={1} ha={2}".format(stack1.name, stack1.yard.name, stack1.yard.ha) print "{0} yards=\n{1}".format(stack1.name, stack1.yards) print "{0} names=\n{1}".format(stack1.name, stack1.names) print "\n********* UXD Message lord to serf serf to lord **********" msg = odict(what="This is a message to the serf. Get to Work", extra="Fix the fence.") stack0.transmit(msg=msg) msg = odict(what="This is a message to the lord. Let me be", extra="Go away.") stack1.transmit(msg=msg) timer = Timer(duration=0.5) timer.restart() while not timer.expired: stack0.serviceAll() stack1.serviceAll() print "{0} Received Messages".format(stack0.name) for msg in stack0.rxMsgs: print msg print print "{0} Received Messages".format(stack1.name) for msg in stack1.rxMsgs: print msg print print "\n********* Multiple Messages Both Ways **********" stack1.transmit(odict(house="Mama mia1", queue="fix me"), None) stack1.transmit(odict(house="Mama mia2", queue="help me"), None) stack1.transmit(odict(house="Mama mia3", queue="stop me"), None) stack1.transmit(odict(house="Mama mia4", queue="run me"), None) stack0.transmit(odict(house="Papa pia1", queue="fix me"), None) stack0.transmit(odict(house="Papa pia2", queue="help me"), None) stack0.transmit(odict(house="Papa pia3", queue="stop me"), None) stack0.transmit(odict(house="Papa pia4", queue="run me"), None) #big packets stuff = [] for i in range(300): stuff.append(str(i).rjust(4, " ")) stuff = "".join(stuff) stack1.transmit(odict(house="Mama mia1", queue="big stuff", stuff=stuff), None) stack0.transmit(odict(house="Papa pia4", queue="gig stuff", stuff=stuff), None) timer.restart(duration=2) while not timer.expired: stack1.serviceAll() stack0.serviceAll() print "{0} Received Messages".format(stack0.name) for msg in stack0.rxMsgs: print msg print print "{0} Received Messages".format(stack1.name) for msg in stack1.rxMsgs: print msg print src = ('minion', 'serf', None) dst = ('master', None, None) route = odict(src=src, dst=dst) msg = odict(route=route, stuff="Hey buddy what is up?") stack0.transmit(msg) timer.restart(duration=2) while not timer.expired: stack1.serviceAll() stack0.serviceAll() print "{0} Received Messages".format(stack0.name) for msg in stack0.rxMsgs: print msg print print "{0} Received Messages".format(stack1.name) for msg in stack1.rxMsgs: print msg print estate = 'minion1' #lord stack yard0 stack0 = stacking.StackUxd(name='lord', lanename='cherry') #serf stack yard1 stack1 = stacking.StackUxd(name='serf', lanename='cherry') print "Yid", yarding.Yard.Yid print "\n********* Attempt Auto Accept ************" #stack0.addRemoteYard(stack1.yard) yard = yarding.Yard(name=stack0.yard.name, prefix='cherry') stack1.addRemoteYard(yard) print "{0} yard name={1} ha={2}".format(stack0.name, stack0.yard.name, stack0.yard.ha) print "{0} yards=\n{1}".format(stack0.name, stack0.yards) print "{0} names=\n{1}".format(stack0.name, stack0.names) print "{0} yard name={1} ha={2}".format(stack1.name, stack1.yard.name, stack1.yard.ha) print "{0} yards=\n{1}".format(stack1.name, stack1.yards) print "{0} names=\n{1}".format(stack1.name, stack1.names) print "\n********* UXD Message serf to lord **********" src = (estate, stack1.yard.name, None) dst = (estate, stack0.yard.name, None) route = odict(src=src, dst=dst) msg = odict(route=route, stuff="Serf to my lord. Feed me!") stack1.transmit(msg=msg) timer = Timer(duration=0.5) timer.restart() while not timer.expired: stack0.serviceAll() stack1.serviceAll() print "{0} Received Messages".format(stack0.name) for msg in stack0.rxMsgs: print msg print print "{0} Received Messages".format(stack1.name) for msg in stack1.rxMsgs: print msg print print "\n********* UXD Message lord to serf **********" src = (estate, stack0.yard.name, None) dst = (estate, stack1.yard.name, None) route = odict(src=src, dst=dst) msg = odict(route=route, stuff="Lord to serf. Feed yourself!") stack0.transmit(msg=msg) timer = Timer(duration=0.5) timer.restart() while not timer.expired: stack0.serviceAll() stack1.serviceAll() print "{0} Received Messages".format(stack0.name) for msg in stack0.rxMsgs: print msg print print "{0} Received Messages".format(stack1.name) for msg in stack1.rxMsgs: print msg print print "{0} yard name={1} ha={2}".format(stack0.name, stack0.yard.name, stack0.yard.ha) print "{0} yards=\n{1}".format(stack0.name, stack0.yards) print "{0} names=\n{1}".format(stack0.name, stack0.names) print "{0} yard name={1} ha={2}".format(stack1.name, stack1.yard.name, stack1.yard.ha) print "{0} yards=\n{1}".format(stack1.name, stack1.yards) print "{0} names=\n{1}".format(stack1.name, stack1.names)