diff --git a/GethNode/Dockerfile b/GethNode/Dockerfile index 2ecfd61..0365832 100644 --- a/GethNode/Dockerfile +++ b/GethNode/Dockerfile @@ -22,14 +22,16 @@ RUN apt-get update -y && \ RUN apt-get update -y && \ apt-get install -y \ - ethereum \ + ethereum=1.13.4+build29068+jammy \ + python3 \ + python3-pip \ openssl \ curl # Prysm RUN mkdir /opt/prysm -RUN curl https://github.com/prysmaticlabs/prysm/raw/master/prysm.sh \ +RUN curl -L https://github.com/prysmaticlabs/prysm/raw/v4.1.1/prysm.sh \ --output /opt/prysm/prysm.sh RUN chmod 755 /opt/prysm/prysm.sh ################################################################################ @@ -43,8 +45,8 @@ RUN mkdir /geth WORKDIR /geth # test script -COPY test.sh /bin/test.sh -RUN chmod 755 /bin/test.sh +# COPY test.sh /bin/test.sh +# RUN chmod 755 /bin/test.sh # entrypoint COPY init-geth /bin/init-geth diff --git a/GethNode/init-geth b/GethNode/init-geth index 5a14a26..90ee835 100755 --- a/GethNode/init-geth +++ b/GethNode/init-geth @@ -1,10 +1,84 @@ -#!/bin/bash +#!/usr/bin/env python3 +# -*- coding:utf-8 -*- +### +# Copyright (c) 2023 Haofan Zheng +# Use of this source code is governed by an MIT-style +# license that can be found in the LICENSE file or at +# https://opensource.org/licenses/MIT. +### -set -e -if [[ ! -f /geth/jwt.hex ]]; then - echo "JWT not found, generating..." - openssl rand -hex 32 | tr -d "\n" > "/geth/jwt.hex" -fi +import os +import shlex +import signal +import subprocess +import sys +import threading + + +JWT_PATH = os.path.join(os.path.sep, 'geth', 'jwt.hex') +TERMINATE_EVENT = threading.Event() + + +def OnTerminate(): + TERMINATE_EVENT.set() + + +def main(): + if not os.path.isfile(JWT_PATH): + with open(JWT_PATH, 'w') as f: + f.write(os.urandom(32).hex()) + + gethAddArgs = [] + envVal = os.getenv('GETH_OPTS', None) + if envVal is not None: + gethAddArgs = shlex.split(envVal) + + gethCmd = [ + '/usr/bin/geth' + ] + gethAddArgs + gethEnv = { + k: v for k, v in os.environ.items() if (k.startswith('GETH_') or k == 'PATH') + } + + prysmAddArgs = [] + envVal = os.getenv('PRYSM_OPTS', None) + if envVal is not None: + prysmAddArgs = shlex.split(envVal) + + prysmCmd = [ + '/opt/prysm/prysm.sh', + ] + prysmAddArgs + prysmEnv = { + 'PATH': os.environ.get('PATH', ''), + } + + gethProc = subprocess.Popen(gethCmd, env=gethEnv, stdout=sys.stdout, stderr=sys.stderr) + prysmProc = subprocess.Popen(prysmCmd, env=prysmEnv, stdout=sys.stdout, stderr=sys.stderr) + + # register signal handler + signal.signal(signal.SIGTERM, OnTerminate) + signal.signal(signal.SIGINT, OnTerminate) + + # wait for termination + TERMINATE_EVENT.wait() + + # terminate processes + while prysmProc.poll() is None: + prysmProc.terminate() + try: + prysmProc.wait(timeout=5) + except subprocess.TimeoutExpired: + pass + + while gethProc.poll() is None: + gethProc.terminate() + try: + gethProc.wait(timeout=5) + except subprocess.TimeoutExpired: + pass + + +if __name__ == '__main__': + main() -/bin/test.sh --existing-arg1 --existing-arg2 ${GETH_OPTS}