|
1 |
| -#!/bin/bash |
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | +### |
| 4 | +# Copyright (c) 2023 Haofan Zheng |
| 5 | +# Use of this source code is governed by an MIT-style |
| 6 | +# license that can be found in the LICENSE file or at |
| 7 | +# https://opensource.org/licenses/MIT. |
| 8 | +### |
2 | 9 |
|
3 |
| -set -e |
4 | 10 |
|
5 |
| -if [[ ! -f /geth/jwt.hex ]]; then |
6 |
| - echo "JWT not found, generating..." |
7 |
| - openssl rand -hex 32 | tr -d "\n" > "/geth/jwt.hex" |
8 |
| -fi |
| 11 | +import os |
| 12 | +import shlex |
| 13 | +import signal |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | +import threading |
| 17 | + |
| 18 | + |
| 19 | +JWT_PATH = os.path.join(os.path.sep, 'geth', 'jwt.hex') |
| 20 | +TERMINATE_EVENT = threading.Event() |
| 21 | + |
| 22 | + |
| 23 | +def OnTerminate(): |
| 24 | + TERMINATE_EVENT.set() |
| 25 | + |
| 26 | + |
| 27 | +def main(): |
| 28 | + if not os.path.isfile(JWT_PATH): |
| 29 | + with open(JWT_PATH, 'w') as f: |
| 30 | + f.write(os.urandom(32).hex()) |
| 31 | + |
| 32 | + gethAddArgs = [] |
| 33 | + envVal = os.getenv('GETH_OPTS', None) |
| 34 | + if envVal is not None: |
| 35 | + gethAddArgs = shlex.split(envVal) |
| 36 | + |
| 37 | + gethCmd = [ |
| 38 | + '/usr/bin/geth' |
| 39 | + ] + gethAddArgs |
| 40 | + gethEnv = { |
| 41 | + k: v for k, v in os.environ.items() if (k.startswith('GETH_') or k == 'PATH') |
| 42 | + } |
| 43 | + |
| 44 | + prysmAddArgs = [] |
| 45 | + envVal = os.getenv('PRYSM_OPTS', None) |
| 46 | + if envVal is not None: |
| 47 | + prysmAddArgs = shlex.split(envVal) |
| 48 | + |
| 49 | + prysmCmd = [ |
| 50 | + '/opt/prysm/prysm.sh', |
| 51 | + ] + prysmAddArgs |
| 52 | + prysmEnv = { |
| 53 | + 'PATH': os.environ.get('PATH', ''), |
| 54 | + } |
| 55 | + |
| 56 | + gethProc = subprocess.Popen(gethCmd, env=gethEnv, stdout=sys.stdout, stderr=sys.stderr) |
| 57 | + prysmProc = subprocess.Popen(prysmCmd, env=prysmEnv, stdout=sys.stdout, stderr=sys.stderr) |
| 58 | + |
| 59 | + # register signal handler |
| 60 | + signal.signal(signal.SIGTERM, OnTerminate) |
| 61 | + signal.signal(signal.SIGINT, OnTerminate) |
| 62 | + |
| 63 | + # wait for termination |
| 64 | + TERMINATE_EVENT.wait() |
| 65 | + |
| 66 | + # terminate processes |
| 67 | + while prysmProc.poll() is None: |
| 68 | + prysmProc.terminate() |
| 69 | + try: |
| 70 | + prysmProc.wait(timeout=5) |
| 71 | + except subprocess.TimeoutExpired: |
| 72 | + pass |
| 73 | + |
| 74 | + while gethProc.poll() is None: |
| 75 | + gethProc.terminate() |
| 76 | + try: |
| 77 | + gethProc.wait(timeout=5) |
| 78 | + except subprocess.TimeoutExpired: |
| 79 | + pass |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + main() |
9 | 84 |
|
10 |
| -/bin/test.sh --existing-arg1 --existing-arg2 ${GETH_OPTS} |
|
0 commit comments