def main(): log.startLogging(stdout) from_address = config.get("alerts", "from") to_address = config.get("alerts", "to") subject_prefix = config.get("alerts", "subject") alerts = Alerts(from_address, to_address, subject_prefix) alerts.start() alerts_export = AlertsExport(alerts) pull_share_async(alerts_export, config.get("alerts", "export")) reactor.run()
def main(): fo = log.startLogging(sys.stdout) fo.formatTime = lambda x: datetime.datetime.fromtimestamp(x).strftime("%Y-%m-%d %H:%M:%S.%f") engine = database.make_engine() timeout = config.getint("ledger", "timeout") ledger = Ledger(engine, timeout) accountant_export = AccountantExport(ledger) watchdog(config.get("watchdog", "ledger")) router_share_async(accountant_export, config.get("ledger", "accountant_export")) reactor.run()
def main(): fo = log.startLogging(sys.stdout) fo.formatTime = lambda x: datetime.datetime.fromtimestamp(x).strftime( "%Y-%m-%d %H:%M:%S.%f") engine = database.make_engine() timeout = config.getint("ledger", "timeout") ledger = Ledger(engine, timeout) accountant_export = AccountantExport(ledger) watchdog(config.get("watchdog", "ledger")) router_share_async(accountant_export, config.get("ledger", "accountant_export")) reactor.run()
def main(): log.startLogging(sys.stdout) session = database.make_session() safe_price_subscriber = connect_subscriber(config.get("safe_price_forwarder", "zmq_backend_address")) safe_price_subscriber.subscribe('') sendmail = Sendmail(config.get("riskmanager", "from_email")) accountant = AccountantProxy("dealer", config.get("accountant", "riskmanager_export"), config.getint("accountant", "riskmanager_export_base_port")) riskmanager = RiskManager(session, sendmail, safe_price_subscriber, accountant) reactor.run()
def main(): import sys log.startLogging(sys.stdout) subscriber = bind_subscriber(config.get("safe_price_forwarder", "zmq_frontend_address")) publisher = bind_publisher(config.get("safe_price_forwarder", "zmq_backend_address")) subscriber.subscribe("") safe_prices = {} def onPrice(*args): update = json.loads(args[0]) log.msg("received update: %s" % update) safe_prices.update(update) publisher.publish(json.dumps(safe_prices), tag=b'') subscriber.gotMessage = onPrice reactor.run()
def main(): log.startLogging(sys.stdout) session = database.make_session() safe_price_subscriber = connect_subscriber( config.get("safe_price_forwarder", "zmq_backend_address")) safe_price_subscriber.subscribe('') sendmail = Sendmail(config.get("riskmanager", "from_email")) accountant = AccountantProxy( "dealer", config.get("accountant", "riskmanager_export"), config.getint("accountant", "riskmanager_export_base_port")) riskmanager = RiskManager(session, sendmail, safe_price_subscriber, accountant) reactor.run()
def validator(full_uri): uri, fragment = jsonschema.compat.urldefrag(full_uri) schema_root = config.get("specs", "schema_root") with open(os.path.join(schema_root, uri)) as schema_file: top_schema = json.load(schema_file) resolver = jsonschema.RefResolver("file://" + schema_root + "/", top_schema) schema = resolver.resolve_fragment(top_schema, fragment) jsonschema.Draft4Validator.check_schema(schema) return jsonschema.Draft4Validator(schema, resolver=resolver)
def get_uri(**kwargs): # If we are not root, override SQL username to be myself my_user = getpass.getuser() if my_user != 'root': kwargs['username'] = my_user uri = config.get("database", "uri", vars=kwargs) if uri.split(":")[0] == "sqlite": sqlalchemy.event.listen(Engine, "connect", set_sqlite_pragma) return uri
def main(): import sys log.startLogging(sys.stdout) subscriber = bind_subscriber( config.get("safe_price_forwarder", "zmq_frontend_address")) publisher = bind_publisher( config.get("safe_price_forwarder", "zmq_backend_address")) subscriber.subscribe("") safe_prices = {} def onPrice(*args): update = json.loads(args[0]) log.msg("received update: %s" % update) safe_prices.update(update) publisher.publish(json.dumps(safe_prices), tag=b'') subscriber.gotMessage = onPrice reactor.run()
def __init__(self): DatabasePlugin.__init__(self) # noinspection PyUnresolvedReferences dbpassword = config.get("database", "password") if dbpassword: dbpool = MyConnectionPool(config.get("database", "adapter"), user=config.get("database", "username"), password=dbpassword, host=config.get("database", "host"), port=config.get("database", "port"), database=config.get("database", "dbname")) else: dbpool = MyConnectionPool(config.get("database", "adapter"), user=config.get("database", "username"), database=config.get("database", "dbname")) self.dbpool = dbpool
def __init__(self): DatabasePlugin.__init__(self) # noinspection PyUnresolvedReferences dbpassword = config.get("database", "password") if dbpassword: dbpool = MyConnectionPool(config.get("database", "adapter"), user=config.get("database", "username"), password=dbpassword, host=config.get("database", "host"), port=config.get("database", "port"), database=config.get( "database", "dbname")) else: dbpool = MyConnectionPool(config.get("database", "adapter"), user=config.get("database", "username"), database=config.get( "database", "dbname")) self.dbpool = dbpool
def main(): log.startLogging(sys.stdout) monitors = ["administrator", "cashier", "ledger", "webserver"] session = database.make_session() proxy = AlertsProxy(config.get("alerts", "export")) watchdogs = {} for name in monitors: watchdogs[name] = Watchdog(name, config.get("watchdog", name), proxy) watchdogs[name].run() num_accountants = config.getint("accountant", "num_procs") for i in range(num_accountants): name = "accountant_%d" % i watchdogs[name] = Watchdog(name, config.get("watchdog", "accountant") % (config.getint("watchdog", "accountant_base_port") + i), proxy) watchdogs[name].run() engine_base_port = config.getint("watchdog", "engine_base_port") for contract in session.query(models.Contract).filter_by(active=True).all(): if contract.contract_type != "cash": watchdogs[contract.ticker] = Watchdog(contract.ticker, config.get("watchdog", "engine") % (engine_base_port + int(contract.id)), proxy) watchdogs[contract.ticker].run() reactor.run()
import sys import os sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../server")) sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../dist/config")) from sputnik import database, models from sputnik import txbitcoinrpc import getpass from sputnik import config from twisted.internet import defer, reactor, task db_session = database.make_session(username=getpass.getuser()) print config.get("cashier","bitcoin_conf") conn = txbitcoinrpc.BitcoinRpc(config.get("cashier", "bitcoin_conf")) #conn.walletpassphrase('pass',10, dont_raise=True) count = 0 def go(): d = conn.keypoolrefill() def get_addresses(result): quantity = 100 dl = defer.DeferredList([conn.getnewaddress() for i in range(quantity)]) def add_addresses(results): for r in results: addr = r[1]['result']
# License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. # from sqlalchemy.orm.exc import NoResultFound import sys import os from sputnik.database import database, models from sputnik.rpc import txbitcoinrpc import getpass from sputnik import config from twisted.internet import defer, reactor, task db_session = database.make_session(username=getpass.getuser()) print config.get("cashier", "bitcoin_conf") conn = txbitcoinrpc.BitcoinRpc(config.get("cashier", "bitcoin_conf")) #conn.walletpassphrase('pass',10, dont_raise=True) count = 0 def go(): d = conn.keypoolrefill() def get_addresses(result): quantity = 100 dl = defer.DeferredList( [conn.getnewaddress() for i in range(quantity)])
def __init__(self): BackendPlugin.__init__(self) self.proxy = accountant.AccountantProxy( "dealer", config.get("accountant", "webserver_export"), config.getint("accountant", "webserver_export_base_port"))
def __init__(self): BackendPlugin.__init__(self) self.proxy = ap(config.get("alerts", "export"))
def __init__(self): BackendPlugin.__init__(self) self.proxy = dealer_proxy_async( config.get("administrator", "webserver_export"))
def init(self): self.share = pull_share_async( self, config.get("webserver", "accountant_export"))
class Cron: def __init__(self, administrator): self.administrator = administrator def mail_statements(self, period): return self.administrator.mail_statements(period) def mtm_futures(self): self.administrator.notify_expired() return self.administrator.mtm_futures() if __name__ == "__main__": log.startLogging(sys.stdout) administrator = dealer_proxy_async(config.get("administrator", "cron_export"), timeout=300) cron = Cron(administrator) # Parse arguments to figure out what to do parser = argparse.ArgumentParser( description="Run Sputnik jobs out of cron") subparsers = parser.add_subparsers( description="job that is to be performed", metavar="command", dest="command") parser_mail_statements = subparsers.add_parser( "mail_statements", help="Mail statements to users") parser_mail_statements.add_argument("--period", dest="period", action="store",
def main(): log.startLogging(sys.stdout) accountant = AccountantProxy( "dealer", config.get("accountant", "cashier_export"), config.getint("accountant", "cashier_export_base_port")) session = db.make_session() bitcoin_conf = config.get("cashier", "bitcoin_conf") log.msg('connecting to bitcoin client') bitcoinrpc = {'BTC': BitcoinRpc(bitcoin_conf, 1)} compropago = Compropago(config.get("cashier", "compropago_key")) cold_wallet_period = config.getint("cashier", "cold_wallet_period") sendmail = Sendmail(config.get("administrator", "email")) minimum_confirmations = config.getint("cashier", "minimum_confirmations") alerts_proxy = AlertsProxy(config.get("alerts", "export")) bitgo_config = { 'use_production': not config.getboolean("cashier", "testnet"), 'client_id': config.get("bitgo", "client_id"), 'client_secret': config.get("bitgo", "client_secret") } bitgo = BitGo(**bitgo_config) bitgo_private_key_file = config.get("cashier", "bitgo_private_key_file") cashier = Cashier( session, accountant, bitcoinrpc, compropago, cold_wallet_period=cold_wallet_period, sendmail=sendmail, minimum_confirmations=minimum_confirmations, alerts=alerts_proxy, bitgo=bitgo, bitgo_private_key_file=bitgo_private_key_file, testnet=config.getboolean("cashier", "testnet"), ) administrator_export = AdministratorExport(cashier) accountant_export = AccountantExport(cashier) webserver_export = WebserverExport(cashier) watchdog(config.get("watchdog", "cashier")) router_share_async(administrator_export, config.get("cashier", "administrator_export")) pull_share_async(accountant_export, config.get("cashier", "accountant_export")) router_share_async(webserver_export, config.get("cashier", "webserver_export")) public_server = Resource() public_server.putChild('compropago', CompropagoHook(cashier)) private_server = Resource() private_server.putChild('bitcoin', BitcoinNotify(cashier)) if config.getboolean("webserver", "ssl"): key = config.get("webserver", "ssl_key") cert = config.get("webserver", "ssl_cert") cert_chain = config.get("webserver", "ssl_cert_chain") contextFactory = ssl.ChainedOpenSSLContextFactory(key, cert_chain) reactor.listenSSL(config.getint("cashier", "public_port"), Site(public_server), contextFactory, interface=config.get("cashier", "public_interface")) else: reactor.listenTCP(config.getint("cashier", "public_port"), Site(public_server), interface=config.get("cashier", "public_interface")) reactor.listenTCP(config.getint("cashier", "private_port"), Site(private_server), interface=config.get("cashier", "private_interface")) reactor.run()
def main(pm): from sputnik.webserver.router.twisted.wamp import RouterFactory router_factory = RouterFactory() router_factory.router = SputnikRouter router_factory.authz_plugins = \ pm.services.get("sputnik.webserver.plugins.authz", []) router_factory.schema_plugins = \ pm.services.get("sputnik.webserver.plugins.schema", []) from sputnik.webserver.router.twisted.wamp import RouterSessionFactory session_factory = RouterSessionFactory(router_factory) session_factory.session = SputnikRouterSession authn_stack = [("ip.IPFilter", "requisite"), ("anonymous.AnonymousLogin", "sufficient"), ("cookie.CookieLogin", "sufficient"), ("wampcra.WAMPCRALogin", "requisite"), ("totp.TOTPVerification", "requisite")] session_factory.plugins = [] for plugin_name, flag in authn_stack: path = "sputnik.webserver.plugins.authn." + plugin_name session_factory.plugins.append((pm.plugins[path], flag)) rpc_plugins = pm.services.get("sputnik.webserver.plugins.rpc", []) feeds_plugins = pm.services.get("sputnik.webserver.plugins.feeds", []) svc_plugins = rpc_plugins + feeds_plugins for plugin in svc_plugins: component_session = plugin component_session.config.realm = u"sputnik" session_factory.add(component_session, plugin.plugin_path.decode("ascii"), u"trusted") uri = "ws://" if config.getboolean("webserver", "ssl"): uri = "wss://" address = config.get("webserver", "ws_address") port = config.getint("webserver", "ws_port") uri += "%s:%s/" % (address, port) from autobahn.twisted.websocket import WampWebSocketServerFactory transport_factory = WampWebSocketServerFactory(session_factory, uri, debug = False, debug_wamp = False) transport_factory.setProtocolOptions(failByDrop = False) watchdog(config.get("watchdog", "webserver")) from twisted.web.server import Site from autobahn.twisted.resource import WebSocketResource root = Root() ws_resource = WebSocketResource(transport_factory) rest_resource = pm.plugins['sputnik.webserver.rest.RESTProxy'] root.putChild("ws", ws_resource) root.putChild("api", rest_resource) site = Site(root) site.noisy = False site.log = lambda _: None from twisted.internet.endpoints import serverFromString, quoteStringArgument if config.getboolean("webserver", "ssl"): key = config.get("webserver", "ssl_key") cert = config.get("webserver", "ssl_cert") cert_chain = config.get("webserver", "ssl_cert_chain") # TODO: Add dhparameters # See https://twistedmatrix.com/documents/14.0.0/core/howto/endpoints.html server = serverFromString(reactor, b"ssl:%d:privateKey=%s:certKey=%s:extraCertChain=%s:sslmethod=TLSv1_METHOD" % (port, quoteStringArgument(key), quoteStringArgument(cert), quoteStringArgument(cert_chain))) else: server = serverFromString(reactor, b"tcp:%d" % port) server.listen(site)
def main(): log.startLogging(sys.stdout) accountant = AccountantProxy("dealer", config.get("accountant", "cashier_export"), config.getint("accountant", "cashier_export_base_port")) session = db.make_session() bitcoin_conf = config.get("cashier", "bitcoin_conf") log.msg('connecting to bitcoin client') bitcoinrpc = {'BTC': BitcoinRpc(bitcoin_conf, 1)} compropago = Compropago(config.get("cashier", "compropago_key")) cold_wallet_period = config.getint("cashier", "cold_wallet_period") sendmail=Sendmail(config.get("administrator", "email")) minimum_confirmations = config.getint("cashier", "minimum_confirmations") alerts_proxy = AlertsProxy(config.get("alerts", "export")) bitgo_config = {'use_production': not config.getboolean("cashier", "testnet"), 'client_id': config.get("bitgo", "client_id"), 'client_secret': config.get("bitgo", "client_secret")} bitgo = BitGo(**bitgo_config) bitgo_private_key_file = config.get("cashier", "bitgo_private_key_file") cashier = Cashier(session, accountant, bitcoinrpc, compropago, cold_wallet_period=cold_wallet_period, sendmail=sendmail, minimum_confirmations=minimum_confirmations, alerts=alerts_proxy, bitgo=bitgo, bitgo_private_key_file=bitgo_private_key_file, testnet=config.getboolean("cashier", "testnet"), ) administrator_export = AdministratorExport(cashier) accountant_export = AccountantExport(cashier) webserver_export = WebserverExport(cashier) watchdog(config.get("watchdog", "cashier")) router_share_async(administrator_export, config.get("cashier", "administrator_export")) pull_share_async(accountant_export, config.get("cashier", "accountant_export")) router_share_async(webserver_export, config.get("cashier", "webserver_export")) public_server = Resource() public_server.putChild('compropago', CompropagoHook(cashier)) private_server = Resource() private_server.putChild('bitcoin', BitcoinNotify(cashier)) if config.getboolean("webserver", "ssl"): key = config.get("webserver", "ssl_key") cert = config.get("webserver", "ssl_cert") cert_chain = config.get("webserver", "ssl_cert_chain") contextFactory = ssl.ChainedOpenSSLContextFactory(key, cert_chain) reactor.listenSSL(config.getint("cashier", "public_port"), Site(public_server), contextFactory, interface=config.get("cashier", "public_interface")) else: reactor.listenTCP(config.getint("cashier", "public_port"), Site(public_server), interface=config.get("cashier", "public_interface")) reactor.listenTCP(config.getint("cashier", "private_port"), Site(private_server), interface=config.get("cashier", "private_interface")) reactor.run()
def init(self): self.share = pull_share_async(self, config.get("webserver", "engine_export"))
except Exception, e: session.rollback() log.err("Cannot determine ticker id. %s" % e) log.err() raise e engine = Engine() administrator_export = AdministratorExport(engine) accountant_port = config.getint("engine", "accountant_base_port") + contract.id administrator_port = config.getint("engine", "administrator_base_port") + contract.id router_share_async(administrator_export, "tcp://127.0.0.1:%d" % administrator_port) logger = LoggingListener(engine, contract) acct = accountant.AccountantProxy("push", config.get("accountant", "engine_export"), config.getint("accountant", "engine_export_base_port")) accountant_notifier = AccountantNotifier(engine, acct, contract) webserver = push_proxy_async(config.get("webserver", "engine_export")) webserver_notifier = WebserverNotifier(engine, webserver, contract) watchdog(config.get("watchdog", "engine") % (config.getint("watchdog", "engine_base_port") + contract.id)) forwarder = connect_publisher(config.get("safe_price_forwarder", "zmq_frontend_address")) safe_price_notifier = SafePriceNotifier(session, engine, acct, webserver, forwarder, contract) accountant_export = AccountantExport(engine, safe_price_notifier, webserver_notifier) router_share_async(accountant_export, "tcp://127.0.0.1:%d" % accountant_port)
def __init__(self): BackendPlugin.__init__(self) self.proxy = dealer_proxy_async( config.get("cashier", "webserver_export"))
def run(pm, reactor): from sputnik.webserver.router.twisted.wamp import RouterFactory router_factory = RouterFactory() router_factory.router = SputnikRouter router_factory.authz_plugins = \ pm.services.get("sputnik.webserver.plugins.authz", []) router_factory.schema_plugins = \ pm.services.get("sputnik.webserver.plugins.schema", []) from sputnik.webserver.router.twisted.wamp import RouterSessionFactory session_factory = RouterSessionFactory(router_factory) session_factory.session = SputnikRouterSession authn_stack = [("ip.IPFilter", "requisite"), ("anonymous.AnonymousLogin", "sufficient"), ("cookie.CookieLogin", "sufficient"), ("wampcra.WAMPCRALogin", "requisite"), ("totp.TOTPVerification", "requisite")] session_factory.plugins = [] for plugin_name, flag in authn_stack: path = "sputnik.webserver.plugins.authn." + plugin_name session_factory.plugins.append((pm.plugins[path], flag)) rpc_plugins = pm.services.get("sputnik.webserver.plugins.rpc", []) feeds_plugins = pm.services.get("sputnik.webserver.plugins.feeds", []) svc_plugins = rpc_plugins + feeds_plugins for plugin in svc_plugins: component_session = plugin component_session.config.realm = u"sputnik" session_factory.add(component_session, plugin.plugin_path.decode("ascii"), u"trusted") uri = "ws://" if config.getboolean("webserver", "ssl"): uri = "wss://" address = config.get("webserver", "ws_address") port = config.getint("webserver", "ws_port") uri += "%s:%s/" % (address, port) from autobahn.twisted.websocket import WampWebSocketServerFactory transport_factory = WampWebSocketServerFactory(session_factory, uri, debug=False, debug_wamp=False) transport_factory.setProtocolOptions(failByDrop=False) watchdog(config.get("watchdog", "webserver")) from twisted.web.server import Site from autobahn.twisted.resource import WebSocketResource root = Root() ws_resource = WebSocketResource(transport_factory) rest_resource = pm.plugins['sputnik.webserver.rest.RESTProxy'] root.putChild("ws", ws_resource) root.putChild("api", rest_resource) site = Site(root) site.noisy = False site.log = lambda _: None from twisted.internet.endpoints import serverFromString, quoteStringArgument if config.getboolean("webserver", "ssl"): key = config.get("webserver", "ssl_key") cert = config.get("webserver", "ssl_cert") cert_chain = config.get("webserver", "ssl_cert_chain") # TODO: Add dhparameters # See https://twistedmatrix.com/documents/14.0.0/core/howto/endpoints.html server = serverFromString( reactor, b"ssl:%d:privateKey=%s:certKey=%s:extraCertChain=%s:sslmethod=TLSv1_METHOD" % (port, quoteStringArgument(key), quoteStringArgument(cert), quoteStringArgument(cert_chain))) else: server = serverFromString(reactor, b"tcp:%d" % port) server.listen(site)
def __init__(self): BackendPlugin.__init__(self) self.proxy = accountant.AccountantProxy("dealer", config.get("accountant", "webserver_export"), config.getint("accountant", "webserver_export_base_port"))
def init(self): self.market = self.require("sputnik.webserver.plugins.rpc.market.MarketService") self.share = router_share_async(self, config.get("webserver", "administrator_export"))
def init(self): self.share = pull_share_async(self, config.get("webserver", "accountant_export"))
class Cron: def __init__(self, administrator): self.administrator = administrator def mail_statements(self, period): return self.administrator.mail_statements(period) def mtm_futures(self): self.administrator.notify_expired() return self.administrator.mtm_futures() if __name__ == "__main__": log.startLogging(sys.stdout) administrator = dealer_proxy_async(config.get("administrator", "cron_export"), timeout=300) cron = Cron(administrator) # Parse arguments to figure out what to do parser = argparse.ArgumentParser(description="Run Sputnik jobs out of cron") subparsers = parser.add_subparsers(description="job that is to be performed", metavar="command", dest="command") parser_mail_statements = subparsers.add_parser("mail_statements", help="Mail statements to users") parser_mail_statements.add_argument("--period", dest="period", action="store", default="monthly", help="Statement period", choices=["monthly", "weekly", "daily"]) parser_mtm_futures = subparsers.add_parser("mtm_futures", help="mark futures contracts to market") kwargs = vars(parser.parse_args()) command = kwargs["command"] del kwargs["command"] method = getattr(cron, command)