|
| 1 | +#!/home/joinmarket/pyenv/bin/python |
| 2 | +"""Entrypoints for Docker containers to run joinmarket. |
| 3 | +
|
| 4 | +todo: wait_for_file should probably be a decorator |
| 5 | +todo: either always force wallet.json or make waiting for the wallet smarter |
| 6 | +""" |
| 7 | +import argparse |
| 8 | +import logging |
| 9 | +import os |
| 10 | +import os.path |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +import time |
| 14 | + |
| 15 | + |
| 16 | +# |
| 17 | +# Helpers |
| 18 | +# |
| 19 | + |
| 20 | +DEBUG_LOG_FORMAT = ( |
| 21 | + '-' * 80 + '\n' + |
| 22 | + '%(asctime)s %(levelname)s in %(name)s @ %(threadName)s:\n' + |
| 23 | + '%(pathname)s:%(lineno)d\n' + |
| 24 | + '%(message)s\n' + |
| 25 | + '-' * 80 |
| 26 | +) |
| 27 | +DEFAULT_LOG_FORMAT = '%(asctime)s %(levelname)s: %(threadName)s: %(name)s: %(message)s' # noqa |
| 28 | + |
| 29 | +log = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +def run(*args): |
| 33 | + """Run a python command inside the joinmarket virtualenv. |
| 34 | +
|
| 35 | + Raises subprocess.CalledProcessError if the command fails. |
| 36 | + """ |
| 37 | + if not args: |
| 38 | + raise ValueError("run needs at least one arg") |
| 39 | + |
| 40 | + command_list = [sys.executable] + map(str, args) |
| 41 | + |
| 42 | + log.info("Running %s...", command_list[1]) |
| 43 | + log.debug("Full command: %s", command_list) |
| 44 | + return subprocess.check_call(command_list, env=os.environ) |
| 45 | + |
| 46 | + |
| 47 | +def wait_for_config(*args): |
| 48 | + """Sleep until config loads. |
| 49 | +
|
| 50 | + Config loading includes bitcoind responding to getblockchaininfo |
| 51 | +
|
| 52 | + Todo: exponential backoff of the sleep. maybe log less, too |
| 53 | + Todo: args here are hacky. make this function and the command seperate |
| 54 | + """ |
| 55 | + while True: |
| 56 | + try: |
| 57 | + run('check-config.py') |
| 58 | + except subprocess.CalledProcessError as e: |
| 59 | + log.error("Unable to load config: %s. Sleeping..." % e) |
| 60 | + time.sleep(60) |
| 61 | + else: |
| 62 | + break |
| 63 | + |
| 64 | + return True |
| 65 | + |
| 66 | + |
| 67 | +def wait_for_file(filename, sleep=10): |
| 68 | + """Sleep until a given file exists.""" |
| 69 | + if os.path.exists(filename): |
| 70 | + return |
| 71 | + |
| 72 | + log.info("'%s' does not exist. Check the README", filename) |
| 73 | + |
| 74 | + log.info("Sleeping until '%s' exists...", filename) |
| 75 | + while not os.path.exists(filename): |
| 76 | + time.sleep(sleep) |
| 77 | + |
| 78 | + log.info("Found '%s'", filename) |
| 79 | + return |
| 80 | + |
| 81 | + |
| 82 | +# |
| 83 | +# Commands |
| 84 | +# |
| 85 | + |
| 86 | +def get_parser(): |
| 87 | + """Create an argument parser that routes to the command functions.""" |
| 88 | + # create the top-level parser |
| 89 | + parser = argparse.ArgumentParser() |
| 90 | + # todo: configurable log level |
| 91 | + subparsers = parser.add_subparsers() |
| 92 | + |
| 93 | + # create the parser for the "maker" command |
| 94 | + parser_maker = subparsers.add_parser('maker') |
| 95 | + parser_maker.set_defaults(func=maker) |
| 96 | + |
| 97 | + # create the parser for the "ob_watcher" command |
| 98 | + parser_ob_watcher = subparsers.add_parser('ob_watcher') |
| 99 | + parser_ob_watcher.set_defaults(func=ob_watcher) |
| 100 | + |
| 101 | + # create the parser for the "sendpayment" command |
| 102 | + parser_sendpayment = subparsers.add_parser('sendpayment') |
| 103 | + parser_sendpayment.set_defaults(func=sendpayment) |
| 104 | + |
| 105 | + # create the parser for the "taker" command |
| 106 | + parser_tumbler = subparsers.add_parser('tumbler') |
| 107 | + parser_tumbler.set_defaults(func=tumbler) |
| 108 | + |
| 109 | + # create the parser for the "wallet_tool" command |
| 110 | + parser_wallet_tool = subparsers.add_parser('wallet_tool') |
| 111 | + parser_wallet_tool.set_defaults(func=wallet_tool) |
| 112 | + |
| 113 | + # other scripts might find waiting for the config helpful, too |
| 114 | + parser_wait_for_config = subparsers.add_parser('wait_for_config') |
| 115 | + parser_wait_for_config.set_defaults(func=wait_for_config) |
| 116 | + |
| 117 | + return parser |
| 118 | + |
| 119 | + |
| 120 | +def maker(args): |
| 121 | + """Earn Bitcoins and privacy.""" |
| 122 | + wallet_filename = 'wallet.json' |
| 123 | + wait_for_file("wallets/%s" % wallet_filename) |
| 124 | + |
| 125 | + # wait for bitcoind to respond |
| 126 | + wait_for_config() |
| 127 | + |
| 128 | + run('yg-pe.py', wallet_filename, *args) |
| 129 | + |
| 130 | + |
| 131 | +def ob_watcher(args): |
| 132 | + """Watch the orderbook.""" |
| 133 | + # wait for bitcoind to respond |
| 134 | + # todo: although, why does the orderbook need bitcoind? |
| 135 | + wait_for_config() |
| 136 | + |
| 137 | + run('ob-watcher.py', *args) |
| 138 | + |
| 139 | + |
| 140 | +def sendpayment(args): |
| 141 | + """"Send Bitcoins with privacy. |
| 142 | +
|
| 143 | + todo: make sure we only sendpayment with coins that have already been |
| 144 | + joined at least once. |
| 145 | + """ |
| 146 | + wallet_filename = 'wallet.json' |
| 147 | + wait_for_file("wallets/%s" % wallet_filename) |
| 148 | + |
| 149 | + # wait for bitcoind to respond |
| 150 | + wait_for_config() |
| 151 | + |
| 152 | + run('sendpayment.py', *args) |
| 153 | + |
| 154 | + |
| 155 | +def tumbler(args): |
| 156 | + """"Send Bitcoins with layers of privacy.""" |
| 157 | + wallet_filename = 'wallet.json' |
| 158 | + wait_for_file("wallets/%s" % wallet_filename) |
| 159 | + |
| 160 | + # wait for bitcoind to respond |
| 161 | + wait_for_config() |
| 162 | + |
| 163 | + run('tumbler.py', *args) |
| 164 | + |
| 165 | + |
| 166 | +def wallet_tool(args): |
| 167 | + """Inspect and manage your Bitcoin wallet.""" |
| 168 | + run('wallet-tool.py', *args) |
| 169 | + |
| 170 | + |
| 171 | +def main(): |
| 172 | + """Manage joinmarket.""" |
| 173 | + parser = get_parser() |
| 174 | + args, passthrough_args = parser.parse_known_args() |
| 175 | + |
| 176 | + log_level = logging.DEBUG # todo: get log_level from args |
| 177 | + |
| 178 | + if log_level == logging.DEBUG: |
| 179 | + log_format = DEBUG_LOG_FORMAT |
| 180 | + else: |
| 181 | + log_format = DEFAULT_LOG_FORMAT |
| 182 | + |
| 183 | + logging.basicConfig( |
| 184 | + format=log_format, |
| 185 | + level=log_level, |
| 186 | + stream=sys.stdout, |
| 187 | + ) |
| 188 | + log.debug("Hello!") |
| 189 | + |
| 190 | + args.func(passthrough_args) |
| 191 | + |
| 192 | + |
| 193 | +if __name__ == '__main__': |
| 194 | + sys.exit(main()) |
0 commit comments