From 2be16465fb77e1b82abdd66e3a6affe39602c90e Mon Sep 17 00:00:00 2001 From: tecnovert Date: Mon, 28 Jun 2021 23:56:45 +0200 Subject: [PATCH] preparescript: Support production docker config. --- basicswap/__init__.py | 2 +- basicswap/basicswap.py | 16 ++- bin/basicswap_prepare.py | 47 ++++++- docker/production/.env | 20 +++ docker/production/.gitignore | 1 + docker/production/bitcoin/Dockerfile | 5 +- docker/production/docker-compose.yml | 118 +++++++++++++----- docker/production/example.env | 21 ++++ docker/production/litecoin/Dockerfile | 7 +- docker/production/monero_wallet/Dockerfile | 8 +- docker/production/monero_wallet/entrypoint.sh | 6 +- docker/production/particl/Dockerfile | 5 +- docker/production/swapclient/.gitignore | 1 + docker/production/swapclient/Dockerfile | 7 +- 14 files changed, 206 insertions(+), 58 deletions(-) create mode 100644 docker/production/.gitignore create mode 100644 docker/production/example.env create mode 100644 docker/production/swapclient/.gitignore diff --git a/basicswap/__init__.py b/basicswap/__init__.py index 1eeffa1..b8f1ad3 100644 --- a/basicswap/__init__.py +++ b/basicswap/__init__.py @@ -1,3 +1,3 @@ name = "basicswap" -__version__ = "0.0.18" +__version__ = "0.0.19" diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 95ccdea..87a3ea2 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -729,13 +729,17 @@ class BasicSwap(BaseApp): if c == Coins.PART: self.coin_clients[c]['have_spent_index'] = ci.haveSpentIndex() - # Sanity checks - rv = self.callcoinrpc(c, 'extkey') - if 'result' in rv and 'No keys to list.' in rv['result']: - raise ValueError('No keys loaded.') + try: + # Sanity checks + rv = self.callcoinrpc(c, 'extkey') + if 'result' in rv and 'No keys to list.' in rv['result']: + raise ValueError('No keys loaded.') + + if self.callcoinrpc(c, 'getstakinginfo')['enabled'] is not False: + self.log.warning('%s staking is not disabled.', ci.coin_name()) + except Exception as e: + self.log.error('Sanity checks failed: %s', str(e)) - if self.callcoinrpc(c, 'getstakinginfo')['enabled'] is not False: - self.log.warning('%s staking is not disabled.', ci.coin_name()) elif c == Coins.XMR: ci.ensureWalletExists() diff --git a/bin/basicswap_prepare.py b/bin/basicswap_prepare.py index 8443804..2a2ffc6 100755 --- a/bin/basicswap_prepare.py +++ b/bin/basicswap_prepare.py @@ -27,6 +27,7 @@ from basicswap.rpc import ( ) from basicswap.basicswap import BasicSwap from basicswap.chainparams import Coins +from basicswap.contrib.rpcauth import generate_salt, password_to_hmac from bin.basicswap_run import startDaemon, startXmrWalletDaemon @@ -77,6 +78,14 @@ LTC_RPC_PORT = int(os.getenv('LTC_RPC_PORT', 19795)) BTC_RPC_PORT = int(os.getenv('BTC_RPC_PORT', 19796)) NMC_RPC_PORT = int(os.getenv('NMC_RPC_PORT', 19798)) +PART_RPC_USER = os.getenv('PART_RPC_USER', '') +PART_RPC_PWD = os.getenv('PART_RPC_PWD', '') +BTC_RPC_USER = os.getenv('BTC_RPC_USER', '') +BTC_RPC_PWD = os.getenv('BTC_RPC_PWD', '') +LTC_RPC_USER = os.getenv('LTC_RPC_USER', '') +LTC_RPC_PWD = os.getenv('LTC_RPC_PWD', '') + +COINS_BIND_IP = os.getenv('COINS_BIND_IP', '127.0.0.1') extract_core_overwrite = True @@ -328,18 +337,23 @@ def prepareDataDir(coin, settings, chain, particl_mnemonic): fp.write('testnet=1\n') fp.write('data-dir={}\n'.format(data_dir)) fp.write('rpc-bind-port={}\n'.format(core_settings['rpcport'])) - fp.write('rpc-bind-ip=127.0.0.1\n') + fp.write('rpc-bind-ip={}\n'.format(COINS_BIND_IP)) fp.write('zmq-rpc-bind-port={}\n'.format(core_settings['zmqport'])) - fp.write('zmq-rpc-bind-ip=127.0.0.1\n') + fp.write('zmq-rpc-bind-ip={}\n'.format(COINS_BIND_IP)) fp.write('prune-blockchain=1\n') - wallet_conf_path = os.path.join(data_dir, coin + '_wallet.conf') + wallets_dir = core_settings.get('walletsdir', data_dir) + if not os.path.exists(wallets_dir): + os.makedirs(wallets_dir) + + wallet_conf_path = os.path.join(wallets_dir, coin + '_wallet.conf') if os.path.exists(wallet_conf_path): exitWithError('{} exists'.format(wallet_conf_path)) with open(wallet_conf_path, 'w') as fp: fp.write('daemon-address={}:{}\n'.format(core_settings['rpchost'], core_settings['rpcport'])) fp.write('no-dns=1\n') fp.write('rpc-bind-port={}\n'.format(core_settings['walletrpcport'])) + fp.write('rpc-bind-ip={}\n'.format(COINS_BIND_IP)) fp.write('wallet-dir={}\n'.format(os.path.join(data_dir, 'wallets'))) fp.write('log-file={}\n'.format(os.path.join(data_dir, 'wallet.log'))) fp.write('shared-ringdb-dir={}\n'.format(os.path.join(data_dir, 'shared-ringdb'))) @@ -358,25 +372,36 @@ def prepareDataDir(coin, settings, chain, particl_mnemonic): else: logger.warning('Unknown chain %s', chain) + if COINS_BIND_IP != '127.0.0.1': + fp.write('rpcallowip=127.0.0.1\n') + fp.write('rpcallowip=172.0.0.0/8\n') # Allow 172.x.x.x, range used by docker + fp.write('rpcbind={}\n'.format(COINS_BIND_IP)) + fp.write('rpcport={}\n'.format(core_settings['rpcport'])) fp.write('printtoconsole=0\n') fp.write('daemon=0\n') fp.write('wallet=wallet.dat\n') + salt = generate_salt(16) if coin == 'particl': fp.write('debugexclude=libevent\n') - fp.write('zmqpubsmsg=tcp://127.0.0.1:{}\n'.format(settings['zmqport'])) + fp.write('zmqpubsmsg=tcp://{}:{}\n'.format(COINS_BIND_IP, settings['zmqport'])) fp.write('spentindex=1\n') fp.write('txindex=1\n') fp.write('staking=0\n') - + if PART_RPC_USER != '': + fp.write('rpcauth={}:{}${}\n'.format(PART_RPC_USER, salt, password_to_hmac(salt, PART_RPC_PWD))) if particl_mnemonic == 'none': fp.write('createdefaultmasterkey=1') elif coin == 'litecoin': fp.write('prune=2000\n') + if LTC_RPC_USER != '': + fp.write('rpcauth={}:{}${}\n'.format(LTC_RPC_USER, salt, password_to_hmac(salt, LTC_RPC_PWD))) elif coin == 'bitcoin': fp.write('prune=2000\n') fp.write('fallbackfee=0.0002\n') + if BTC_RPC_USER != '': + fp.write('rpcauth={}:{}${}\n'.format(BTC_RPC_USER, salt, password_to_hmac(salt, BTC_RPC_PWD))) elif coin == 'namecoin': fp.write('prune=2000\n') else: @@ -627,6 +652,18 @@ def main(): } } + if PART_RPC_USER != '': + chainclients['particl']['rpcuser'] = PART_RPC_USER + chainclients['particl']['rpcpassword'] = PART_RPC_PWD + if LTC_RPC_USER != '': + chainclients['litecoin']['rpcuser'] = LTC_RPC_USER + chainclients['litecoin']['rpcpassword'] = LTC_RPC_PWD + if BTC_RPC_USER != '': + chainclients['bitcoin']['rpcuser'] = BTC_RPC_USER + chainclients['bitcoin']['rpcpassword'] = BTC_RPC_PWD + + chainclients['monero']['walletsdir'] = os.getenv('XMR_WALLETS_DIR', chainclients['monero']['datadir']) + if disable_coin != '': logger.info('Disabling coin: %s', disable_coin) if not os.path.exists(config_path): diff --git a/docker/production/.env b/docker/production/.env index f50a058..211efcc 100644 --- a/docker/production/.env +++ b/docker/production/.env @@ -1,2 +1,22 @@ HTML_PORT=127.0.0.1:12700:12700 TZ=UTC +DATA_PATH=/mnt/hdd50/docker2 + +PART_RPC_HOST=particl_core +LTC_RPC_HOST=litecoin_core +BTC_RPC_HOST=bitcoin_core + +PART_RPC_USER=particl_user +PART_RPC_PWD=particl_pwd +BTC_RPC_USER=bitcoin_user +BTC_RPC_PWD=bitcoin_pwd +LTC_RPC_USER=litecoin_user +LTC_RPC_PWD=litecoin_pwd + +PART_DATA_DIR=/data/particl +LTC_DATA_DIR=/data/litecoin +BTC_DATA_DIR=/data/bitcoin +XMR_DATA_DIR=/data/monero_daemon +XMR_WALLETS_DIR=/data/monero_wallet + +COINS_BIND_IP=0.0.0.0 diff --git a/docker/production/.gitignore b/docker/production/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/docker/production/.gitignore @@ -0,0 +1 @@ +.env diff --git a/docker/production/bitcoin/Dockerfile b/docker/production/bitcoin/Dockerfile index 6ab7e47..5b8b313 100644 --- a/docker/production/bitcoin/Dockerfile +++ b/docker/production/bitcoin/Dockerfile @@ -2,7 +2,8 @@ FROM i_swapclient as install_stage -RUN basicswap-prepare --preparebinonly --bindir=/coin_bin --withcoin=bitcoin --withoutcoins=particl +RUN basicswap-prepare --preparebinonly --bindir=/coin_bin --withcoin=bitcoin --withoutcoins=particl && \ + find /coin_bin -name *.tar.gz -delete FROM debian:buster-slim COPY --from=install_stage /coin_bin . @@ -23,4 +24,4 @@ COPY entrypoint.sh /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] EXPOSE 8332 8333 18332 18333 18443 18444 -CMD ["bitcoind"] +CMD ["/bitcoin/bitcoind", "--datadir=/data"] diff --git a/docker/production/docker-compose.yml b/docker/production/docker-compose.yml index 290cee3..79220e8 100644 --- a/docker/production/docker-compose.yml +++ b/docker/production/docker-compose.yml @@ -8,9 +8,9 @@ services: dockerfile: Dockerfile container_name: particl_core volumes: - - /var/swapdata/particl:/data - ports: - - "51738:51738" + - ${DATA_PATH}/particl:/data + #ports: + # - "51738:51738" expose: - 51735 logging: @@ -19,18 +19,32 @@ services: max-size: "10m" max-file: "3" restart: unless-stopped - bitcoin_core: - image: i_bitcoin + #bitcoin_core: + #image: i_bitcoin + #build: + #context: bitcoin + #dockerfile: Dockerfile + #container_name: bitcoin_core + #volumes: + #- ${DATA_PATH}/bitcoin:/data + ##ports: + ## - "8333:8333" + #expose: + #- 8332 + #logging: + #driver: "json-file" + #options: + #max-size: "10m" + #max-file: "3" + #restart: unless-stopped + litecoin_core: + image: i_litecoin build: - context: bitcoin + context: litecoin dockerfile: Dockerfile - container_name: bitcoin_core + container_name: litecoin_core volumes: - - /var/swapdata/bitcoin:/data - ports: - - "8333:8333" - expose: - - 8332 + - ${DATA_PATH}/litecoin:/data logging: driver: "json-file" options: @@ -44,7 +58,7 @@ services: #dockerfile: Dockerfile #container_name: monero_daemon #volumes: - #- /var/swapdata/monero_daemon:/data + #- ${DATA_PATH}/monero_daemon:/data #ports: #- "18080:18080" #expose: @@ -55,22 +69,22 @@ services: #max-size: "10m" #max-file: "3" #restart: unless-stopped - monero_wallet: - image: i_monero_wallet - build: - context: monero_wallet - dockerfile: Dockerfile - container_name: monero_wallet - volumes: - - /var/swapdata/monero_wallet:/data - expose: - - 8332 - logging: - driver: "json-file" - options: - max-size: "10m" - max-file: "3" - restart: unless-stopped + #monero_wallet: + #image: i_monero_wallet + #build: + #context: monero_wallet + #dockerfile: Dockerfile + #container_name: monero_wallet + #volumes: + #- ${DATA_PATH}/monero_wallet:/data + #expose: + #- 8332 + #logging: + #driver: "json-file" + #options: + #max-size: "10m" + #max-file: "3" + #restart: unless-stopped swapclient: image: i_swapclient build: @@ -78,7 +92,7 @@ services: dockerfile: Dockerfile container_name: swapclient volumes: - - /var/swapdata/swapclient:/data + - ${DATA_PATH}/swapclient:/data ports: - "${HTML_PORT}" # Expose only to localhost, see .env environment: @@ -91,6 +105,50 @@ services: depends_on: - particl_core restart: unless-stopped + swapprepare: + image: i_swapclient + build: + context: swapclient + dockerfile: Dockerfile + container_name: swapprepare + volumes: + - ${DATA_PATH}/swapclient:/data/swapclient + - ${DATA_PATH}/monero_daemon:/data/monero_daemon + - ${DATA_PATH}/monero_wallet:/data/monero_wallet + - ${DATA_PATH}/particl:/data/particl + - ${DATA_PATH}/bitcoin:/data/bitcoin + - ${DATA_PATH}/litecoin:/data/litecoin + environment: + - TZ + - PART_RPC_HOST + - LTC_RPC_HOST + - BTC_RPC_HOST + - PART_RPC_PORT + - LTC_RPC_PORT + - BTC_RPC_PORT + - XMR_RPC_HOST + - BASE_XMR_RPC_PORT + - BASE_XMR_ZMQ_PORT + - BASE_XMR_WALLET_PORT + - XMR_WALLET_RPC_HOST + - XMR_WALLET_RPC_USER + - XMR_WALLET_RPC_PWD + - DEFAULT_XMR_RESTORE_HEIGHT + - UI_HTML_PORT + - PART_ZMQ_PORT + - PART_RPC_USER + - PART_RPC_PWD + - BTC_RPC_USER + - BTC_RPC_PWD + - LTC_RPC_USER + - LTC_RPC_PWD + - PART_DATA_DIR + - LTC_DATA_DIR + - BTC_DATA_DIR + - XMR_DATA_DIR + - XMR_WALLETS_DIR + - COINS_BIND_IP + restart: "no" networks: default: external: diff --git a/docker/production/example.env b/docker/production/example.env new file mode 100644 index 0000000..898fa4b --- /dev/null +++ b/docker/production/example.env @@ -0,0 +1,21 @@ +HTML_PORT=127.0.0.1:12700:12700 +TZ=UTC +DATA_PATH=/var/swapdata/ +PART_RPC_HOST=particl_core +LTC_RPC_HOST=litecoin_core +BTC_RPC_HOST=bitcoin_core + +PART_RPC_USER=particl_user +PART_RPC_PWD=particl_pwd +BTC_RPC_USER=bitcoin_user +BTC_RPC_PWD=bitcoin_pwd +LTC_RPC_USER=litecoin_user +LTC_RPC_PWD=litecoin_pwd + +PART_DATA_DIR=/data/particl +LTC_DATA_DIR=/data/litecoin +BTC_DATA_DIR=/data/bitcoin +XMR_DATA_DIR=/data/monero_daemon +XMR_WALLETS_DIR=/data/monero_wallet + +COINS_BIND_IP=0.0.0.0 diff --git a/docker/production/litecoin/Dockerfile b/docker/production/litecoin/Dockerfile index ad87862..ca0dbf7 100644 --- a/docker/production/litecoin/Dockerfile +++ b/docker/production/litecoin/Dockerfile @@ -1,13 +1,14 @@ FROM i_swapclient as install_stage -RUN basicswap-prepare --preparebinonly --bindir=/coin_bin --withcoin=litecoin --withoutcoin=particl +RUN basicswap-prepare --preparebinonly --bindir=/coin_bin --withcoin=litecoin --withoutcoin=particl && \ + find /coin_bin -name *.tar.gz -delete FROM debian:buster-slim COPY --from=install_stage /coin_bin . ENV LITECOIN_DATA /data -RUN groupadd -r particl && useradd -r -m -g litecoin litecoin \ +RUN groupadd -r litecoin && useradd -r -m -g litecoin litecoin \ && apt-get update \ && apt-get install -qq --no-install-recommends gosu \ && rm -rf /var/lib/apt/lists/* \ @@ -21,4 +22,4 @@ COPY entrypoint.sh /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] EXPOSE 8332 8333 18332 18333 18443 18444 -CMD ["litecoind"] +CMD ["/litecoin/litecoind", "--datadir=/data"] diff --git a/docker/production/monero_wallet/Dockerfile b/docker/production/monero_wallet/Dockerfile index 0a2baa8..043dd84 100644 --- a/docker/production/monero_wallet/Dockerfile +++ b/docker/production/monero_wallet/Dockerfile @@ -2,18 +2,16 @@ FROM i_monero_daemon ENV MONERO_DATA /data -RUN groupadd -r monero && useradd -r -m -g monero monero \ +RUN groupadd -r monero_wallet && useradd -r -m -g monero_wallet monero_wallet \ && apt-get update \ && apt-get install -qq --no-install-recommends gosu \ && rm -rf /var/lib/apt/lists/* \ && mkdir -p "$MONERO_DATA" \ - && chown -R monero:monero "$MONERO_DATA" \ - && ln -sfn "$MONERO_DATA" /home/monero/.monero \ - && chown -h monero:monero /home/monero/.monero + && chown -R monero_wallet:monero_wallet "$MONERO_DATA" VOLUME $MONERO_DATA COPY entrypoint.sh /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] EXPOSE 18080 -CMD ["monero-wallet-rpc", "--non-interactive", "--config-file=/home/monero/.monero/monerod.conf"] +CMD ["monero-wallet-rpc", "--non-interactive", "--config-file=/data/monero_wallet.conf"] diff --git a/docker/production/monero_wallet/entrypoint.sh b/docker/production/monero_wallet/entrypoint.sh index bb20b8d..ff4b98d 100755 --- a/docker/production/monero_wallet/entrypoint.sh +++ b/docker/production/monero_wallet/entrypoint.sh @@ -1,11 +1,11 @@ #!/bin/bash set -e -if [[ "$1" == "monerod" ]]; then +if [[ "$1" == "monero-wallet-rpc" ]]; then mkdir -p "$MONERO_DATA" - chown -h monero:monero /home/monero/.monero - exec gosu monero "$@" + chown -h monero_wallet:monero_wallet /data + exec gosu monero_wallet "$@" else exec "$@" fi diff --git a/docker/production/particl/Dockerfile b/docker/production/particl/Dockerfile index 7425a7f..0cf2585 100644 --- a/docker/production/particl/Dockerfile +++ b/docker/production/particl/Dockerfile @@ -1,6 +1,7 @@ FROM i_swapclient as install_stage -RUN basicswap-prepare --preparebinonly --bindir=/coin_bin --withcoin=particl +RUN basicswap-prepare --preparebinonly --bindir=/coin_bin --withcoin=particl && \ + find /coin_bin -name *.tar.gz -delete FROM debian:buster-slim COPY --from=install_stage /coin_bin . @@ -21,4 +22,4 @@ COPY entrypoint.sh /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] EXPOSE 51735 20792 51738 -CMD ["particld"] +CMD ["/particl/particld", "--datadir=/data"] diff --git a/docker/production/swapclient/.gitignore b/docker/production/swapclient/.gitignore new file mode 100644 index 0000000..87c4a4c --- /dev/null +++ b/docker/production/swapclient/.gitignore @@ -0,0 +1 @@ +test_code diff --git a/docker/production/swapclient/Dockerfile b/docker/production/swapclient/Dockerfile index f4221b9..2b19eb9 100644 --- a/docker/production/swapclient/Dockerfile +++ b/docker/production/swapclient/Dockerfile @@ -18,7 +18,12 @@ RUN wget -O basicswap-master.zip https://github.com/tecnovert/basicswap/archive/ protoc -I=basicswap --python_out=basicswap basicswap/messages.proto; \ pip3 install .; -RUN useradd -ms /bin/bash swap_user && \ +#COPY ./test_code basicswap-master +#RUN cd basicswap-master; \ +# protoc -I=basicswap --python_out=basicswap basicswap/messages.proto; \ +# pip3 install .; + +RUN groupadd -r swap_user && useradd -g swap_user -ms /bin/bash swap_user && \ mkdir /data && chown swap_user -R /data # Expose html port