diff options
-rwxr-xr-x | scripts/run_bp.py | 78 |
1 files changed, 53 insertions, 25 deletions
diff --git a/scripts/run_bp.py b/scripts/run_bp.py index c7b071104..46ebbd5cb 100755 --- a/scripts/run_bp.py +++ b/scripts/run_bp.py @@ -38,7 +38,7 @@ except Exception: sys.exit(1) -CONTAINER_NAME = 'tangerine' +CONTAINER_NAME_BASE = 'tangerine' NUM_SLOTS = 5 POLLING_INTERVAL = 30 SLEEP_RAND_RANGE = 1800 @@ -51,19 +51,28 @@ WHITELISTED_FILES = [ ] +def get_container_name(testnet): + return (CONTAINER_NAME_BASE + if not testnet else CONTAINER_NAME_BASE + '-testnet') + + def get_shard_id(nodekey): """Return shard ID. - Shard ID is calculate as (first byte of sha3 of Node Key address) mode NUM_SLOTS. + Shard ID is calculate as (first byte of sha3 of Node Key address) mode + NUM_SLOTS. """ wd = os.getcwd() - p = subprocess.Popen(['docker', 'run', '-v', '%s:/mnt' % wd, '-t', TOOLS_IMAGE, + p = subprocess.Popen(['docker', 'run', '-v', '%s:/mnt' % wd, '--rm', + '-t', TOOLS_IMAGE, 'nodekey', 'inspect', '/mnt/%s' % nodekey], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() - address = re.search('^Node Address: (0x[0-9a-fA-F]{40})', stdout.decode('utf-8')).group(1) - return int(hashlib.sha256(address.encode('utf-8')).hexdigest()[0], base=16) % NUM_SLOTS + address = re.search('^Node Address: (0x[0-9a-fA-F]{40})', + stdout.decode('utf-8')).group(1) + return int(hashlib.sha256( + address.encode('utf-8')).hexdigest()[0], base=16) % NUM_SLOTS def get_tangerine_image(args): @@ -85,19 +94,22 @@ def generate_node_key(nodekey): raise RuntimeError('node.key already exists, abort.') wd = os.getcwd() - subprocess.Popen(['docker', 'run', '-v', '%s:/mnt' % wd, '-t', TOOLS_IMAGE, + subprocess.Popen(['docker', 'run', '-v', '%s:/mnt' % wd, '--rm', + '-t', TOOLS_IMAGE, 'nodekey', 'generate', '/mnt/%s' % nodekey]).wait() print('Node key generated') print('\n\033[5;91;49mPlease backup node.key in a secure place!\033[0m') - print('\n\033[0;91mSend at least 1 ETH (use Rinkeby ETH for testnet) to node key address!\033[0m') - print('\033[0;91mThese ETHs are used for then Tangerine network recovery mechanism.\033[0m\n\n') + print('\n\033[0;91mSend at least 1 ETH (use Rinkeby ETH for testnet) to ' + 'node key address!\033[0m') + print('\033[0;91mThese ETHs are used for then Tangerine network recovery ' + 'mechanism.\033[0m\n\n') def get_image_version(image): """Get a docker image's version.""" p = subprocess.Popen(['docker', 'inspect', '-f', '{{.Id}}', image], - stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() return stdout @@ -112,43 +124,51 @@ def check_environment(): """Check execution environment.""" for f in os.listdir(os.getcwd()): if f not in WHITELISTED_FILES: - raise RuntimeError('please execute this script in an empty directory, abort') + raise RuntimeError('please execute this script in an empty ' + 'directory, abort') if get_time_delta() > 0.05: - raise RuntimeError('please sync your network time by installing a NTP client') + raise RuntimeError('please sync your network time by installing a ' + 'NTP client') if platform.system() == 'Linux': p1 = subprocess.Popen('ps aux | grep -q "[n]tp"', shell=True).wait() p2 = subprocess.Popen('ps aux | grep -q "[c]hrony"', shell=True).wait() if p1 != 0 and p2 != 0: - raise RuntimeError('please install ntpd or chrony to synchronize system time') + raise RuntimeError('please install ntpd or chrony to synchronize ' + 'system time') def start(args, force=False): """Start the docker container.""" - p = subprocess.Popen(['docker', 'inspect', '-f', '{{.State.Running}}', CONTAINER_NAME], + container_name = get_container_name(args.testnet) + + p = subprocess.Popen(['docker', 'inspect', '-f', '{{.State.Running}}', + container_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() if stdout.strip() == b'true': if force: print('Stopping old container ...') - subprocess.Popen(['docker', 'rm', '-f', CONTAINER_NAME]).wait() + subprocess.Popen(['docker', 'rm', '-f', container_name]).wait() else: print('Container already running.') return elif stdout.strip() == b'false': print('Stopping old container ...') - subprocess.Popen(['docker', 'rm', '-f', CONTAINER_NAME]).wait() + subprocess.Popen(['docker', 'rm', '-f', container_name]).wait() tangerine_image = get_tangerine_image(args) + port = 30303 if not args.testnet else 40303 + wd = os.getcwd() cmd = ['docker', 'run', - '-d', '--name=%s' % CONTAINER_NAME, + '-d', '--name=%s' % container_name, '-v', '%s:/mnt' % wd, - '-p', '30303:30303/tcp', - '-p', '30303:30303/udp', + '-p', '%d:%d/tcp' % (port, port), + '-p', '%d:%d/udp' % (port, port), '--restart', 'always', '-t', tangerine_image, '--identity=%s' % args.identity, @@ -158,7 +178,8 @@ def start(args, force=False): '--syncmode=fast', '--cache=1024', '--verbosity=3', - '--gcmode=archive'] + '--gcmode=archive', + '--port=%d' % port] if args.testnet: cmd.append('--testnet') @@ -167,7 +188,8 @@ def start(args, force=False): update_image(tangerine_image) subprocess.Popen(cmd, stdout=subprocess.PIPE).wait() - print('\nContainer running, check logs with `docker logs -f %s\'\n' % CONTAINER_NAME) + print('\nContainer running, check logs with `docker logs -f %s\'\n' % + container_name) def monitor(args): @@ -181,7 +203,8 @@ def monitor(args): if new_image != old_image: sleep_time = random.randint(0, SLEEP_RAND_RANGE) - print('New image found, sleeping for %s seconds before updating ...' % sleep_time) + print('New image found, sleeping for %s seconds before ' + 'updating ...' % sleep_time) time.sleep(sleep_time) # Check update again. @@ -196,18 +219,23 @@ def monitor(args): def main(): """Main.""" - parser = argparse.ArgumentParser(description='Script for launching a Tangerine Node') + parser = argparse.ArgumentParser( + description='Script for launching a Tangerine Node') parser.add_argument('--nodekey', default='node.key', dest='nodekey', help='Path to nodekey, default to `node.key\'') - parser.add_argument('--identity', default=socket.gethostname(), - dest='identity', help='Name of the node, e.g. ByzantineLab') - parser.add_argument('--testnet', default=False, action='store_true', dest='testnet', + parser.add_argument('--identity', default=None, dest='identity', + help='Name of the node, e.g. ByzantineLab') + parser.add_argument('--testnet', default=False, action='store_true', + dest='testnet', help='Whether or not to run a testnet node') args = parser.parse_args() check_environment() + if args.identity is None: + raise RuntimeError("please specify identity (node name)") + if not os.path.exists(args.nodekey): res = input('No node key found, generate a new one? [y/N] ') if res == 'y': |