def register_search(): try: sock.get_sock().chain('plumbing/jisho/search') >> receiver def search(body): global cid # {'match': match, 'user': user, 'instruction': instruction, 'body': body, 'send_reply': send_reply} query = body['match'].group(2) if not query: return "usage: jisho words [keywords]" cid += 1 my_cid = cid body = { 'cid': my_cid, 'return_path': 'plumbing/jisho/search', 'query': query } asc = AsyncCall() tracker[my_cid] = asc sock.get_sock().send_multipart('MSG', 'api/jisho/words', json.dumps(body).encode('utf-8')) raise asc command_registry.getRegistry().registerInstruction( re.compile(r'jisho (words) ?(.*)?'), search, ("jisho words [keywords] - Search jisho.org for words", )) except: print("Failed to register macros") traceback.print_exc(None)
def register_salt_macros(): try: sock.get_sock().chain('input/salt/run') >> receiver from config import salt # salt = { # 'macros': { # 'test': {'target': '*', 'instruction': 'test.ping', 'cmd_arg': ()} # } # } macros = salt.get('macros', {}) def salt_macro(body): # {'match': match, 'user': user, 'instruction': instruction, 'body': body, 'send_reply': send_reply} macro_name = body['match'].group(1) if not macro_name: return "Known macros: {}".format(", ".join(macros.keys())) macro = macros.get(macro_name.strip(), None) if not macro: return "Unknown macro" run_command(**macro)({}) command_registry.getRegistry( ).registerInstruction(re.compile(r'salt macro( .*)?'), salt_macro, ( "salt macro [name] - Run specified macro, omit name to get a list of macros", )) except: print("Failed to register macros") traceback.print_exc(None)
def register_subs(): try: sock.get_sock().subscribe('config/request/rss', rss_config) sock.get_sock().subscribe('input/http/rss', rss_receiver) command_registry.getRegistry().registerInstruction( re.compile(r'rss force (.*)'), force_feed, ("rss force [tag] - If you know the tag, you can force a recheck for it",)) except: print("Failed to register subscriptions") traceback.print_exc(None)
def command_runner(dummy): global cid cid += 1 my_cid = cid body = { 'cid': my_cid, 'target': target, 'instruction': instruction, 'cmd_arg': cmd_arg } asc = AsyncCall() tracker[my_cid] = asc sock.get_sock().send_multipart('MSG', 'output/salt/run', json.dumps(body).encode('utf-8')) raise asc
def poll_feed(tag, url, period = 10*60): global feeds, callbacks if tag not in feeds: feeds[tag] = {'feed': url, 'period': period} sock.get_sock().send_multipart('MSG', 'config/service/rss', json.dumps(feeds)) if tag not in callbacks: callbacks[tag] = [] c = Chain() callbacks[tag].append(c) return c
def register_subs(): try: sock.get_sock().subscribe('config/request/rss', rss_config) sock.get_sock().subscribe('input/http/rss', rss_receiver) command_registry.getRegistry( ).registerInstruction(re.compile(r'rss force (.*)'), force_feed, ( "rss force [tag] - If you know the tag, you can force a recheck for it", )) except: print("Failed to register subscriptions") traceback.print_exc(None)
def poll_feed(tag, url, period=10 * 60): global feeds, callbacks if tag not in feeds: feeds[tag] = {'feed': url, 'period': period} sock.get_sock().send_multipart('MSG', 'config/service/rss', json.dumps(feeds)) if tag not in callbacks: callbacks[tag] = [] c = Chain() callbacks[tag].append(c) return c
def search(body): global cid # {'match': match, 'user': user, 'instruction': instruction, 'body': body, 'send_reply': send_reply} query = body['match'].group(2) if not query: return "usage: jisho words [keywords]" cid += 1 my_cid = cid body = {'cid': my_cid, 'return_path': 'plumbing/jisho/search', 'query': query} asc = AsyncCall() tracker[my_cid] = asc sock.get_sock().send_multipart('MSG', 'api/jisho/words', json.dumps(body).encode('utf-8')) raise asc
def search(body): global cid # {'match': match, 'user': user, 'instruction': instruction, 'body': body, 'send_reply': send_reply} query = body['match'].group(2) if not query: return "usage: jisho words [keywords]" cid += 1 my_cid = cid body = { 'cid': my_cid, 'return_path': 'plumbing/jisho/search', 'query': query } asc = AsyncCall() tracker[my_cid] = asc sock.get_sock().send_multipart('MSG', 'api/jisho/words', json.dumps(body).encode('utf-8')) raise asc
def register_search(): try: sock.get_sock().chain('plumbing/jisho/search') >> receiver def search(body): global cid # {'match': match, 'user': user, 'instruction': instruction, 'body': body, 'send_reply': send_reply} query = body['match'].group(2) if not query: return "usage: jisho words [keywords]" cid += 1 my_cid = cid body = {'cid': my_cid, 'return_path': 'plumbing/jisho/search', 'query': query} asc = AsyncCall() tracker[my_cid] = asc sock.get_sock().send_multipart('MSG', 'api/jisho/words', json.dumps(body).encode('utf-8')) raise asc command_registry.getRegistry().registerInstruction(re.compile(r'jisho (words) ?(.*)?'), search, ("jisho words [keywords] - Search jisho.org for words",)) except: print("Failed to register macros") traceback.print_exc(None)
def register_salt_macros(): try: sock.get_sock().chain('input/salt/run') >> receiver from config import salt # salt = { # 'macros': { # 'test': {'target': '*', 'instruction': 'test.ping', 'cmd_arg': ()} # } # } macros = salt.get('macros', {}) def salt_macro(body): # {'match': match, 'user': user, 'instruction': instruction, 'body': body, 'send_reply': send_reply} macro_name = body['match'].group(1) if not macro_name: return "Known macros: {}".format(", ".join(macros.keys())) macro = macros.get(macro_name.strip(), None) if not macro: return "Unknown macro" run_command(**macro)({}) command_registry.getRegistry().registerInstruction(re.compile(r'salt macro( .*)?'), salt_macro, ("salt macro [name] - Run specified macro, omit name to get a list of macros",)) except: print("Failed to register macros") traceback.print_exc(None)
def send_reply(message): return_body['message'] = message sock.get_sock().send_multipart('MSG', return_target, json.dumps(return_body))
from utils.protocol import Socket import config if getattr(config, 'http_sink', False): my_config = { 'broker': getattr(config, 'broker', '127.0.0.1'), 'node_name': 'http_sink', 'port': 8808, 'sinks': [], 'pollers': [], 'apis': [] } my_config.update(config.http_sink) protocolsock = Socket(my_config['node_name'], my_config['broker'], './certificates') import tornado.ioloop ioloop = tornado.ioloop.IOLoop.current() protocolsock.tornado_register(ioloop) from .base import Periodic, Api import importlib import traceback import tornado.web if my_config['sinks']: print('Configuring for {} sink modules'.format(len(my_config['sinks']))) sink_handlers = [] for sink in my_config['sinks']: s = None try: s = importlib.import_module('nodes.py3_http_sink.sinks.{}'.format(sink))
print("No channel") else: print("No server") # This stack of nested if/else statements would likely be better refactored to use exceptions. if getattr(config, 'irc', False): my_config = { 'broker': getattr(config, 'broker', '127.0.0.1'), 'node_name': 'irc', 'nickname': 'muhubot', 'servers': [] } my_config.update(config.irc) if my_config['servers']: print('Configuring for {} servers'.format(len(my_config['servers']))) zmq_sock = Socket(my_config['node_name'], my_config['broker'], './certificates') import tornado.ioloop ioloop = tornado.ioloop.IOLoop.current() zmq_sock.tornado_register(ioloop) import pydle pool = pydle.ClientPool() for s in my_config['servers']: if 'server' not in s: print('Missing "server" config value') elif ('channels' not in s) or (type(s['channels']) != list) or (len(s['channels']) == 0): print('Invalid "channels" list for {}'.format(s['server'])) else: opts = { 'nickname': my_config['nickname'], 'port': 6667, 'ssl': False, 'password': "", 'user_map': {} } opts.update(s)
def baked_send_to_channel(message): ircmsg = { 'server': server, 'channel': channel, 'message': message } sock.get_sock().send_multipart('MSG', 'output/irc/msg', json.dumps(ircmsg))
def force_feed(body): # {'match': match, 'user': user, 'instruction': instruction, 'body': body, 'send_reply': send_reply} sock.get_sock().msg('config/force/rss', {'tag': body['match'].group(1)}) return "I'll ask for you..."
def baked_send_to_channel(message): ircmsg = {'server': server, 'channel': channel, 'message': message} sock.get_sock().send_multipart('MSG', 'output/irc/msg', json.dumps(ircmsg))
from utils.protocol import Socket import config if getattr(config, 'http_sink', False): my_config = { 'broker': getattr(config, 'broker', '127.0.0.1'), 'node_name': 'http_sink', 'port': 8808, 'sinks': [], 'pollers': [], 'apis': [] } my_config.update(config.http_sink) protocolsock = Socket(my_config['node_name'], my_config['broker'], './certificates') import tornado.ioloop ioloop = tornado.ioloop.IOLoop.current() protocolsock.tornado_register(ioloop) from .base import Periodic, Api import importlib import traceback import tornado.web if my_config['sinks']: print('Configuring for {} sink modules'.format(len( my_config['sinks']))) sink_handlers = []
if not event: return callbacks = proj_tracking[event].get( body['project']['path_with_namespace'], None) if callbacks: cb = Chain() >> callbacks cb(body) if all_tracking[event]: cb = Chain() >> all_tracking[event] cb(body) for k, v in event_map.items(): all_tracking[v] = [] proj_tracking[v] = {} sock.get_sock().subscribe(k, gitlab_receiver) # Tag push # 'tag_push': 'input/http/commit/tag_push', # 'issue': 'input/http/commit/issue', # 'note': 'input/http/commit/note', # 'merge_request': 'input/http/commit/merge_request', # 'pipeline': 'input/http/commit/pipeline' # 'project_create': 'input/http/gitlab_admin/project', # 'project_destroy': 'input/http/gitlab_admin/project', # 'project_rename': 'input/http/gitlab_admin/project', # 'project_transferred': 'input/http/gitlab_admin/project', # 'user_add_to_team': 'input/http/gitlab_admin/team', # 'user_remove_from_team': 'input/http/gitlab_admin/team', # 'user_create': 'input/http/gitlab_admin/user',
def rss_config(cmd, channel, body): global feeds body = json.loads(body) sock.get_sock().send_multipart('MSG', body['requesting'], json.dumps(feeds))
else: print("No server") # This stack of nested if/else statements would likely be better refactored to use exceptions. if getattr(config, 'irc', False): my_config = { 'broker': getattr(config, 'broker', '127.0.0.1'), 'node_name': 'irc', 'nickname': 'muhubot', 'servers': [] } my_config.update(config.irc) if my_config['servers']: print('Configuring for {} servers'.format(len(my_config['servers']))) zmq_sock = Socket(my_config['node_name'], my_config['broker'], './certificates') import tornado.ioloop ioloop = tornado.ioloop.IOLoop.current() zmq_sock.tornado_register(ioloop) import pydle pool = pydle.ClientPool() for s in my_config['servers']: if 'server' not in s: print('Missing "server" config value') elif ('channels' not in s) or (type(s['channels']) != list) or (len( s['channels']) == 0): print('Invalid "channels" list for {}'.format(s['server'])) else: opts = { 'nickname': my_config['nickname'],
from __future__ import print_function, unicode_literals from utils.protocol import Socket import config my_config = { 'broker': getattr(config, 'broker', '127.0.0.1'), 'node_name': 'plumbing' } try: my_config.update(config.plumbing_options) except: print("Using default options") s = Socket(my_config['node_name'], my_config['broker'], './certificates') config.plumbing() s.foreground()
target = body.get('target', '*') client = LocalClient() t = client.cmd(target, instruction, arg=cmd_arg, timeout=240) if (instruction == "state.sls"): ans = formatter_state(t) else: ans = formatter_raw(t) result = {'cid': body.get('cid', None), 'result': ans} zmq_sock.send_multipart('MSG', 'input/salt/run', json.dumps(result)) except: traceback.print_exc(None) result = { 'cid': body.get('cid', None), 'result': 'An exception occurred' } zmq_sock.send_multipart('MSG', 'input/salt/run', json.dumps(result)) my_config = { 'zmq_endpoint': getattr(config, 'zmq_endpoint', 'tcp://127.0.0.1:{}'), 'node_name': 'salt' } if getattr(config, 'salt', False): my_config.update(config.salt) zmq_sock = Socket(my_config['node_name'], my_config['zmq_endpoint'], './client_certs') zmq_sock.subscribe('output/salt/run', do_run) zmq_sock.foreground()
# 'input/http/commit/push' receives object_kind "push" event = event_map.get(channel, None) if not event: return callbacks = proj_tracking[event].get(body['project']['path_with_namespace'], None) if callbacks: cb = Chain() >> callbacks cb(body) if all_tracking[event]: cb = Chain() >> all_tracking[event] cb(body) for k,v in event_map.items(): all_tracking[v] = [] proj_tracking[v] = {} sock.get_sock().subscribe(k, gitlab_receiver) # Tag push # 'tag_push': 'input/http/commit/tag_push', # 'issue': 'input/http/commit/issue', # 'note': 'input/http/commit/note', # 'merge_request': 'input/http/commit/merge_request', # 'pipeline': 'input/http/commit/pipeline' # 'project_create': 'input/http/gitlab_admin/project', # 'project_destroy': 'input/http/gitlab_admin/project', # 'project_rename': 'input/http/gitlab_admin/project', # 'project_transferred': 'input/http/gitlab_admin/project', # 'user_add_to_team': 'input/http/gitlab_admin/team', # 'user_remove_from_team': 'input/http/gitlab_admin/team',
def do_run(cmd, channel, body): # {'cid': 'blahblah', 'target': '*', 'instruction': 'test.ping', cmd_arg: ['']} try: cmd_arg = body.get('cmd_arg', ()) instruction = body.get('instruction', 'test.ping') target = body.get('target', '*') client = LocalClient() t = client.cmd(target, instruction, arg=cmd_arg, timeout=240) if (instruction == "state.sls"): ans = formatter_state(t) else: ans = formatter_raw(t) result = {'cid': body.get('cid', None), 'result': ans} zmq_sock.send_multipart('MSG', 'input/salt/run', json.dumps(result)) except: traceback.print_exc(None) result = {'cid': body.get('cid', None), 'result': 'An exception occurred'} zmq_sock.send_multipart('MSG', 'input/salt/run', json.dumps(result)) my_config = { 'zmq_endpoint': getattr(config, 'zmq_endpoint', 'tcp://127.0.0.1:{}'), 'node_name': 'salt' } if getattr(config, 'salt', False): my_config.update(config.salt) zmq_sock = Socket(my_config['node_name'], my_config['zmq_endpoint'], './client_certs') zmq_sock.subscribe('output/salt/run', do_run) zmq_sock.foreground()