From 9677c48f39c32aff40e53238d15b8e8906179ea0 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Tue, 3 Jan 2023 20:03:36 +0200 Subject: [PATCH] docker: Add helper script to build docker config from fragments. Set PIVX_PARAMSDIR automatically when usecontainers is set. Fix PIVX wallet encryption when added. --- bin/basicswap_prepare.py | 9 +- docker/production/.gitignore | 1 + .../compose-fragments/9_swapprepare.yml | 18 ++++ docker/production/notes.md | 27 +++++- docker/production/scripts/build_yml_files.py | 83 +++++++++++++++++++ 5 files changed, 134 insertions(+), 4 deletions(-) create mode 100755 docker/production/scripts/build_yml_files.py diff --git a/bin/basicswap_prepare.py b/bin/basicswap_prepare.py index f37c516..f40dcf8 100755 --- a/bin/basicswap_prepare.py +++ b/bin/basicswap_prepare.py @@ -833,7 +833,7 @@ def prepareDataDir(coin, settings, chain, particl_mnemonic, extra_opts={}): params_dir = os.path.join(data_dir, 'pivx-params') downloadPIVXParams(params_dir) fp.write('prune=4000\n') - PIVX_PARAMSDIR = os.getenv('PIVX_PARAMSDIR', params_dir) + PIVX_PARAMSDIR = os.getenv('PIVX_PARAMSDIR', '/data/pivx-params' if extra_opts.get('use_containers', False) else params_dir) fp.write(f'paramsdir={PIVX_PARAMSDIR}\n') if PIVX_RPC_USER != '': fp.write('rpcauth={}:{}${}\n'.format(PIVX_RPC_USER, salt, password_to_hmac(salt, PIVX_RPC_PWD))) @@ -1085,7 +1085,7 @@ def initialise_wallets(particl_wallet_mnemonic, with_coins, data_dir, settings, try: swap_client = BasicSwap(fp, data_dir, settings, chain) - coins_to_create_wallets_for = (Coins.PART, Coins.BTC, Coins.LTC, Coins.PIVX) + coins_to_create_wallets_for = (Coins.PART, Coins.BTC, Coins.LTC) # Always start Particl, it must be running to initialise a wallet in addcoin mode # Particl must be loaded first as subsequent coins are initialised from the Particl mnemonic start_daemons = ['particl', ] + [c for c in with_coins if c != 'particl'] @@ -1145,7 +1145,10 @@ def initialise_wallets(particl_wallet_mnemonic, with_coins, data_dir, settings, swap_client.waitForDaemonRPC(c) swap_client.initialiseWallet(c) if WALLET_ENCRYPTION_PWD != '' and c not in coins_to_create_wallets_for: - swap_client.ci(c).changeWalletPassword('', WALLET_ENCRYPTION_PWD) + try: + swap_client.ci(c).changeWalletPassword('', WALLET_ENCRYPTION_PWD) + except Exception as e: + logger.warning(f'changeWalletPassword failed for {coin_name}.') finally: if swap_client: diff --git a/docker/production/.gitignore b/docker/production/.gitignore index 3d7f25c..18bc55a 100644 --- a/docker/production/.gitignore +++ b/docker/production/.gitignore @@ -1,3 +1,4 @@ .env docker-compose-prepare.yml docker-compose.yml +*_bkp_*.yml diff --git a/docker/production/compose-fragments/9_swapprepare.yml b/docker/production/compose-fragments/9_swapprepare.yml index 49d0f84..c5a6c37 100644 --- a/docker/production/compose-fragments/9_swapprepare.yml +++ b/docker/production/compose-fragments/9_swapprepare.yml @@ -11,6 +11,9 @@ - ${DATA_PATH}/particl:/data/particl - ${DATA_PATH}/bitcoin:/data/bitcoin - ${DATA_PATH}/litecoin:/data/litecoin + - ${DATA_PATH}/pivx:/data/pivx + - ${DATA_PATH}/dash:/data/dash + - ${DATA_PATH}/firo:/data/firo environment: - TZ - UI_HTML_PORT @@ -42,4 +45,19 @@ - XMR_WALLET_RPC_USER - XMR_WALLET_RPC_PWD - DEFAULT_XMR_RESTORE_HEIGHT + - PIVX_DATA_DIR + - PIVX_RPC_HOST + - PIVX_RPC_PORT + - PIVX_RPC_USER + - PIVX_RPC_PWD + - DASH_DATA_DIR + - DASH_RPC_HOST + - DASH_RPC_PORT + - DASH_RPC_USER + - DASH_RPC_PWD + - FIRO_DATA_DIR + - FIRO_RPC_HOST + - FIRO_RPC_PORT + - FIRO_RPC_USER + - FIRO_RPC_PWD restart: "no" diff --git a/docker/production/notes.md b/docker/production/notes.md index f807280..97aeb42 100644 --- a/docker/production/notes.md +++ b/docker/production/notes.md @@ -30,6 +30,11 @@ Set the latest Monero chain height, or the height your wallet must restore from: Create docker-compose config: + # Using the helper script: + ./scripts/build_yml_files.py -c bitcoin monero + + # Or + cat compose-fragments/0_start.yml > docker-compose.yml # Add the relevant coin fragments @@ -124,6 +129,20 @@ Start BasicSwap: ## Add a coin + +Stop all running containers + + docker-compose stop + + +Update docker-compose config: + +Rebuild using the helper script (must list all enabled coins): + + ./scripts/build_yml_files.py -c bitcoin monero + +Or + cat compose-fragments/1_monero-wallet.yml >> docker-compose.yml cat compose-fragments/1_monero-wallet.yml >> docker-compose-prepare.yml @@ -131,10 +150,16 @@ Start BasicSwap: cat compose-fragments/8_monero-daemon.yml >> docker-compose.yml +Prepare config files: + + docker-compose -f docker-compose-prepare.yml build swapprepare export ADD_COIN=monero docker-compose -f docker-compose-prepare.yml run --rm swapprepare \ - basicswap-prepare --nocores --addcoin=${ADD_COIN} --htmlhost="0.0.0.0" --particl_mnemonic=none + basicswap-prepare --nocores --usecontainers --addcoin=${ADD_COIN} --htmlhost="0.0.0.0" --particl_mnemonic=none + + +Prepare wallet: docker-compose build monero_daemon docker-compose build diff --git a/docker/production/scripts/build_yml_files.py b/docker/production/scripts/build_yml_files.py new file mode 100755 index 0000000..5ae4219 --- /dev/null +++ b/docker/production/scripts/build_yml_files.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Copyright (c) 2023 tecnovert +# Distributed under the MIT software license, see the accompanying +# file LICENSE or http://www.opensource.org/licenses/mit-license.php. + +""" +Join docker compose fragments +""" + +__version__ = '0.1' + +import os +import argparse + + +def get_bkp_offset(filename, ext='yml'): + for i in range(1000): + if not os.path.exists(f'{filename}_bkp_{i}.{ext}'): + return i + raise ValueError(f'Unable to get backup filename for: {filename}.{ext}') + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('-v', '--version', action='version', + version='%(prog)s {version}'.format(version=__version__)) + parser.add_argument('-c', '--coins', nargs='+', help=' Select coins', required=True) + args = parser.parse_args() + + with_coins = ['particl', ] + for coin_name in args.coins: + parsed_name = coin_name.lower() + if parsed_name not in with_coins: + with_coins.append(parsed_name) + + print('Preparing docker compose files with coins:', ','.join(with_coins)) + + num_docker_compose = get_bkp_offset('docker-compose') + num_docker_compose_prepare = get_bkp_offset('docker-compose-prepare') + + if os.path.exists('docker-compose.yml'): + os.rename('docker-compose.yml', f'docker-compose_bkp_{num_docker_compose}.yml') + if os.path.exists('docker-compose-prepare.yml'): + os.rename('docker-compose-prepare.yml', f'docker-compose-prepare_bkp_{num_docker_compose_prepare}.yml') + + fragments_dir = 'compose-fragments' + with open('docker-compose.yml', 'wb') as fp, open('docker-compose-prepare.yml', 'wb') as fpp: + with open(os.path.join(fragments_dir, '0_start.yml'), 'rb') as fp_in: + for line in fp_in: + fp.write(line) + fpp.write(line) + + for coin_name in with_coins: + if coin_name == 'particl': + # Nothing to do + continue + if coin_name == 'monero': + with open(os.path.join(fragments_dir, '1_monero-wallet.yml'), 'rb') as fp_in: + for line in fp_in: + fp.write(line) + fpp.write(line) + with open(os.path.join(fragments_dir, '8_monero-daemon.yml'), 'rb') as fp_in: + for line in fp_in: + fp.write(line) + continue + with open(os.path.join(fragments_dir, f'1_{coin_name}.yml'), 'rb') as fp_in: + for line in fp_in: + fp.write(line) + fpp.write(line) + + with open(os.path.join(fragments_dir, '8_swapclient.yml'), 'rb') as fp_in: + for line in fp_in: + fp.write(line) + with open(os.path.join(fragments_dir, '9_swapprepare.yml'), 'rb') as fp_in: + for line in fp_in: + fpp.write(line) + print('Done.') + + +if __name__ == '__main__': + main()