From 5b9d236ca07d38148404357310f01d73c159ffc6 Mon Sep 17 00:00:00 2001 From: Damian Orzepowski Date: Tue, 30 Jan 2024 15:36:36 +0100 Subject: [PATCH] feat(BUX-516): add possibility to run Pulse; use the same script as in bux-wallet-frontend. --- README.md | 29 +-- docker-compose.yml | 86 ++++++-- start-bux-server.sh | 272 ----------------------- start.sh | 512 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 580 insertions(+), 319 deletions(-) delete mode 100755 start-bux-server.sh create mode 100755 start.sh diff --git a/README.md b/README.md index 62db130f3..f2aea3c54 100644 --- a/README.md +++ b/README.md @@ -280,7 +280,7 @@ make bench ### Docker Compose Quickstart -To get started with development, `bux-server` provides a `start-bux-server.sh` script +To get started with development, `bux-server` provides a `start.sh` script which is using `docker-compose.yml` file to starts up Bux Server with selected database and cache storage. To start, we need to fill the config json which we want to use, for example: `config/envs/development.json`. @@ -291,38 +291,17 @@ There are two way of running this script: 1. with manual configuration - Every option is displayed in terminal and user can choose which database/cache storage use and configure how to run bux-server. ```bash - ./start-bux-server.sh + ./start.sh ``` 2. with flags which define how to set up docker services. Ever option is displayed when you ran the script with flag `-h` or `--help`. Possible options: - - ```bash - ./start-bux-server.sh --help - - Welcome in Bux Server! - Usage: ./start-bux-server.sh [OPTIONS] - - This script helps you to run Bux server with your preferred database and cache storage. - - Options: - - -db, --database Define database - postgresql, mongodb, sqlite - -c, --cache Define cache storage - freecache(in-memory), redis - -bs, --bux-server Whether the bux-server should be run - true/false - -env, --environment Define bux-server environment - development/staging/production - -b, --background Whether the bux-server should be run in background - true/false - -x, --xpub Define admin xPub - -l, --load Load .env.config file and run bux-server with its settings - -rb --rebuild Rebuild docker images before running - ``` - ```bash - ./start-bux-server.sh -db postgresql -c redis -bs true -env development -b false + ./start.sh -db postgresql -c redis -bs true -env development -b false ``` `-l/--load` option add possibility to use previously created `.env.config` file and run bux-server with simple command: ```bash - ./start-bux-server.sh -l + ./start.sh -l ```
diff --git a/docker-compose.yml b/docker-compose.yml index e4a56a998..ac0fe5a9a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,23 +1,66 @@ -# !NOTE: This file should be used only by start-bux-server.sh -version: "3.9" - +########################################################## +### !NOTE: This file is meant to be used by start.sh! ### +######################################################## services: + bux-wallet-frontend: + image: bsvb/bux-wallet-frontend:latest + pull_policy: always + ports: + - "3002:80" + configs: + - source: frontend-env-config + target: /usr/share/nginx/html/env-config.json + + bux-wallet-backend: + image: bsvb/bux-wallet-backend:latest + pull_policy: always + environment: + BUX_PAYMAIL_DOMAIN: '${RUN_PAYMAIL_DOMAIN}' + HTTP_SERVER_CORS_ALLOWEDDOMAINS: 'http://localhost:3002' + env_file: + - .env.config + ports: + - "8081:8080" + links: + - bux-postgresql + depends_on: + bux-postgresql: + condition: service_healthy + + pulse: + image: bsvb/pulse:latest + pull_policy: always + command: + - "--preloaded" + ports: + - "8080:8080" + volumes: + - pulse-data:/app/data + + bux-server: + build: . + environment: + BUX_PAYMAIL_DOMAINS: '${RUN_PAYMAIL_DOMAIN}' + env_file: + - .env.config + ports: + - "3003:3003" + restart: unless-stopped + bux-redis: image: redis - container_name: bux-redis hostname: redis ports: - "6379:6379" volumes: - - redis-data:/data + - bux-redis-data:/data healthcheck: test: ["CMD", "redis-cli", "ping"] bux-postgresql: image: postgres - container_name: bux-postgresql volumes: - - db-data:/var/lib/postgresql/data:Z + - bux-postgresql-data:/var/lib/postgresql/data environment: - POSTGRES_NAME=postgres - POSTGRES_USER=postgres @@ -31,7 +74,6 @@ services: bux-mongodb: image: mongo - container_name: bux-mongodb environment: MONGO_INITDB_ROOT_USERNAME: mongo MONGO_INITDB_ROOT_PASSWORD: mongo @@ -39,7 +81,7 @@ services: ports: - '27017:27017' volumes: - - db-data:/data/db + - bux-mongodb-data:/data/db healthcheck: test: echo 'db.runCommand("ping").ok' | mongosh mongodb:27017/test --quiet interval: 10s @@ -47,20 +89,20 @@ services: retries: 5 start_period: 40s - bux-server: - build: . - container_name: bux-server - env_file: - - .env.config - ports: - - "3003:3003" - volumes: - - ./config:/config:Z - volumes: - app-data: + bux-postgresql-data: + driver: local + bux-redis-data: driver: local - db-data: + bux-mongodb-data: driver: local - redis-data: + pulse-data: driver: local + +configs: + frontend-env-config: + content: | + { + "paymailDomain": "${RUN_PAYMAIL_DOMAIN}", + "apiUrl": "http://localhost:8081" + } diff --git a/start-bux-server.sh b/start-bux-server.sh deleted file mode 100755 index a85e50028..000000000 --- a/start-bux-server.sh +++ /dev/null @@ -1,272 +0,0 @@ -#!/bin/bash - -reset='\033[0m' -choice='' - -function ask_for_choice() { - local prompt="$1" - local options=("${@:2}") - - echo -e "$prompt" - for (( i = 0; i < ${#options[@]}; i++ )); do - echo "$((i+1)). ${options[i]}" - done - - - read -p "> " choice - - while ! [[ "$choice" =~ ^[0-9]+$ ]] || (( choice < 1 || choice > ${#options[@]} )); do - echo -e "\033[0;31mInvalid choice! Please select a valid option.$reset" - read -p "> " choice - done -} - -function ask_for_yes_or_no() { - local prompt="$1" - echo -e "$prompt" - - local response - read -p "(y/n)> " response - - while ! [[ "$response" =~ ^(yes|no|y|n)$ ]]; do - echo -e "\033[0;31mInvalid response! Please enter 'yes' or 'no'.$reset" - read -p "> " response - done - - if [[ "$response" =~ ^(yes|y)$ ]]; then - choice="true" - else - choice="false" - fi -} - -compose_plugin=false -if command docker compose version &> /dev/null; then - compose_plugin=true -fi - -function docker_compose_up() { - if [ compose_plugin == true ]; then - docker compose up $1 - else - docker-compose up $1 - fi -} - -# Welcome message -echo -e "\033[0;33m\033[1mWelcome in Bux Server!$reset" - -while [[ $# -gt 0 ]]; do - key="$1" - - case $key in - -db|--database) - database="$2" - shift - ;; - -c|--cache) - cache="$2" - shift - ;; - -bs|--bux-server) - bux_server="$2" - shift - ;; - -b|--background) - background="$2" - shift - ;; - -x|--xpub) - admin_xpub="$2" - shift - ;; - -l|--load) - load_config="true" - shift - ;; - -rb|--rebuild) - rebuild="true" - load_config="true" - ;; - -h|--help) - echo -e "\033[1mUsage: ./start-bux-server.sh [OPTIONS]$reset" - echo "" - echo "This script helps you to run Bux server with your preferred database and cache storage." - echo "" - echo -e "Options:$reset" - echo -e " -db, --database\t Define database - postgresql, mongodb, sqlite$reset" - echo -e " -c, --cache\t\t Define cache storage - freecache(in-memory), redis$reset" - echo -e " -bs, --bux-server\t Whether the bux-server should be run - true/false$reset" - echo -e " -b, --background\t Whether the bux-server should be run in background - true/false$reset" - echo -e " -x, --xpub\t\t Define admin xPub$reset" - echo -e " -l, --load\t\t Load .env.config file and run bux-server with its settings$reset" - echo -e " -rb, --rebuild\t Rebuild docker images before running$reset" - exit 1; - shift - ;; - *) - ;; - esac - shift -done - -if [ "$load_config" == "true" ]; then - if [ -f .env.config ]; then - echo "File .env.config exists." - - while IFS= read -r line; do - if [[ "$line" =~ ^(BUX_DB_DATASTORE_ENGINE=) ]]; then - value="${line#*=}" - database="${value//\"}" - fi - if [[ "$line" =~ ^(BUX_CACHE_ENGINE=) ]]; then - value="${line#*=}" - cache="${value//\"}" - fi - if [[ "$line" =~ ^(RUN_BUX_SERVER=) ]]; then - value="${line#*=}" - bux_server="${value//\"}" - fi - if [[ "$line" =~ ^(RUN_BUX_SERVER_BACKGROUND=) ]]; then - value="${line#*=}" - background="${value//\"}" - fi - if [[ "$line" =~ ^(BUX_AUTH_ADMIN_KEY=) ]]; then - value="${line#*=}" - admin_xpub="${value//\"}" - fi - done < ".env.config" - else - echo "File .env.config does not exist." - fi -fi - -if [ "$database" == "" ]; then - database_options=("postgresql" "mongodb" "sqlite") - ask_for_choice "\033[1mSelect your database: $reset" "${database_options[@]}" - - case $choice in - 1) database="postgresql";; - 2) database="mongodb";; - 3) database="sqlite";; - esac -fi - -if [ "$cache" == "" ]; then - cache_options=("freecache" "redis") - ask_for_choice "\033[1mSelect your cache storage:$reset" "${cache_options[@]}" - - case $choice in - 1) cache="freecache";; - 2) cache="redis";; - esac -fi - -if [ "$load_config" != "true" ]; then - # Create the .env.config file - echo -e "\033[0;32mCreating .env.config file...$reset" - cat << EOF > .env.config -BUX_CACHE_ENGINE="$cache" -BUX_DB_DATASTORE_ENGINE="$database" -EOF - - # Add additional settings to .env.config file based on the selected database - if [ "$database" == "postgresql" ]; then - echo 'BUX_DB_SQL_HOST="bux-postgresql"' >> .env.config - echo 'BUX_DB_SQL_NAME="postgres"' >> .env.config - echo 'BUX_DB_SQL_USER="postgres"' >> .env.config - echo 'BUX_DB_SQL_PASSWORD="postgres"' >> .env.config - fi - - # Add additional settings to .env.config file based on the selected database - if [ "$database" == "mongodb" ]; then - echo 'BUX_DB_MONGODB_URI="mongodb://mongo:mongo@bux-mongodb:27017/"' >> .env.config - fi - - # Add additional settings to .env.config file based on the selected cache storage - if [ "$cache" == "redis" ]; then - echo 'BUX_CACHE_REDIS_URL="redis://redis:6379"' >> .env.config - fi -fi - -echo -e "\033[0;32mStarting additional services with docker-compose...$reset" -if [ "$cache" == "redis" ]; then - echo -e "\033[0;37mdocker compose up -d bux-redis$reset" - docker_compose_up "-d bux-redis" -fi - -if [ "$database" != "sqlite" ]; then - echo -e "\033[0;37mdocker compose up -d bux-$database$reset" - docker_compose_up "-d bux-$database" -fi - -if [ "$bux_server" == "" ]; then - ask_for_yes_or_no "\033[1mDo you want to run Bux-server?$reset" - bux_server=$choice -fi - -if [ "$load_config" != "true" ]; then - echo "RUN_BUX_SERVER=\"$bux_server\"" >> .env.config -fi - -if [ "$bux_server" == "true" ]; then - if [ "$load_config" != "true" ]; then - if [ "$admin_xpub" == "" ]; then - # Ask for admin xPub choice - echo -e "\033[1mDefine admin xPub $reset" - echo -e "\033[4mLeave empty to use the one from selected environment config file $reset" - read -p "> " admin_input - - if [[ -n "$admin_input" ]]; then - admin_xpub=$admin_input - fi - fi - fi - - if [ "$rebuild" == "" ]; then - ask_for_yes_or_no "\033[1mDo you want to rebuild docker images before running?$reset" - rebuild=$choice - fi - - if [ "$rebuild" == "true" ]; then - docker_command_attributes="--build" - fi - - if [ "$background" == "" ]; then - ask_for_yes_or_no "\033[1mDo you want to run Bux-server in background?$reset" - background=$choice - fi - - if [ "$load_config" != "true" ]; then - echo "RUN_BUX_SERVER_BACKGROUND=\"$background\"" >> .env.config - - if [ "$admin_xpub" != "" ]; then - echo "BUX_AUTH_ADMIN_KEY=\"$admin_xpub\"" >> .env.config - fi - fi - - echo -e "\033[0;32mRunning Bux-server...$reset" - if [ "$background" == "true" ]; then - echo -e "\033[0;37mdocker compose up $docker_command_attributes -d bux-server$reset" - docker_compose_up "$docker_command_attributes -d bux-server" - else - echo -e "\033[0;37mdocker compose up $docker_command_attributes bux-server$reset" - docker_compose_up "$docker_command_attributes bux-server" - - function cleanup { - echo -e "\033[0;31mStopping additional services...$reset" - if [ compose_plugin == true ]; then - docker compose stop - else - docker-compose stop - fi - echo -e "\033[0;31mExiting program...$reset" - } - - trap cleanup EXIT - fi -else - echo -e "\033[0;33m\033[1mThanks for using Bux configurator!" - echo -e "Additional services are working, remember to start Bux-server manually!$reset" - exit 1 -fi diff --git a/start.sh b/start.sh new file mode 100755 index 000000000..fc7bf312a --- /dev/null +++ b/start.sh @@ -0,0 +1,512 @@ +#!/bin/bash + +# Define color codes +color_success="\033[0;32m" # green +color_danger="\033[0;31m" # red +color_warning="\033[0;33m" # yellow +color_debug="\033[0;34m" # blue +color_user="\033[0;35m" # purple + +# Reset color code +color_reset="\033[0m" +choice='' + +function print_debug() { + if [ "$debug" == "true" ]; then + echo -e "${color_debug}$1${color_reset}" + fi +} + +function print_success() { + echo -e "${color_success}$1${color_reset}" +} + +function print_warning() { + echo -e "${color_warning}$1${color_reset}" +} + +function print_error() { + echo -e "${color_danger}$1${color_reset}" +} + +function printPrompt() { + echo -e "${color_user}$1${color_reset}" +} + +function ask_for_value() { + local prompt="$1" + local prefix="$2" + + printPrompt "$prompt" + read -p ">" choice + + if [[ -z "$choice" ]]; then + return + fi + + while ! [[ "$choice" =~ ^"$prefix".*$ ]]; do + echo -e "${color_danger}Invalid value!${color_reset}" + read -p "> " choice + if [[ -z "$choice" ]]; then + return + fi + done +} + +function ask_for_choice() { + local prompt="$1" + local options=("${@:2}") + + printPrompt "$prompt" + for (( i = 0; i < ${#options[@]}; i++ )); do + echo "$((i+1)). ${options[i]}" + done + + + read -p ">" choice + + while ! [[ "$choice" =~ ^[0-9]+$ ]] || (( choice < 1 || choice > ${#options[@]} )); do + echo -e "${color_danger}Invalid choice! Please select a valid option.${color_reset}" + read -p "> " choice + done +} + +function ask_for_yes_or_no() { + local prompt="$1" + local default_value="${2:-true}" + + local default_prompt="[Y/n]" + local inverse_default_prompt="[y/N]" + + if [[ "$default_value" == "true" ]]; then + prompt="$prompt $default_prompt" + elif [[ "$default_value" == "false" ]]; then + prompt="$prompt $inverse_default_prompt" + fi + + printPrompt "$prompt" + + local response + read -p ">" response + + if [[ -z "$response" ]]; then + choice="$default_value" + return + fi + + while ! [[ "$response" =~ ^(yes|no|y|n)$ ]]; do + echo -e "${color_danger}Invalid response! Please enter 'yes' or 'no'.${color_reset}" + read -p "> " response + if [[ -z "$response" ]]; then + choice="$default_value" + return + fi + done + + if [[ "$response" =~ ^(yes|y)$ ]]; then + choice="true" + else + choice="false" + fi +} + +function print_state() { + print_debug "State:" + print_debug " database=${database}" + print_debug " cache=${cache}" + print_debug " bux_server=${bux_server}" + print_debug " bux_wallet_frontend=${bux_wallet_frontend}" + print_debug " bux_wallet_backend=${bux_wallet_backend}" + print_debug " background=${background}" + print_debug " default_xpub: $default_xpub" + print_debug " admin_xpub=${admin_xpub}" + print_debug " admin_xpriv=${admin_xpriv}" + print_debug " load_config=${load_config}" + print_debug " no_rebuild=${no_rebuild}" + print_debug " debug=${debug}" + print_debug "" +} + +function load_from() { + local key="$1" + local variable="$2" + + if [[ -n "${!variable}" ]]; then + return + fi + + if [[ "$line" =~ ^("$key"=) ]]; then + print_debug "Loading $key to variable $variable" + value="${line#*=}" + value="${value//\"}" + print_debug "Value for $variable is '$value'" + eval "$variable=\"$value\"" + fi +} + +function save_to() { + local key="$1" + local variable="$2" + + if [[ "${!variable}" == "" ]]; then + return + fi + + save_value "$key" "${!variable}" +} + +function save_value() { + local key="$1" + local value="$2" + echo "$key=\"${value}\"" >> .env.config +} + +function docker_compose_up() { + local compose_plugin=false + if command docker compose version &> /dev/null; then + compose_plugin=true + fi + + local run="true" + + if [ "$debug" == 'true' ]; then + echo "" + if [ $compose_plugin == true ]; then + print_debug "docker compose up $1" + else + print_debug "docker-compose up $1" + fi + echo "" + ask_for_yes_or_no "You use debug mode. Do you want to run docker compose now?" + run=$choice + fi + if [ "$run" != "true" ]; then + return + fi + + if [ $compose_plugin == true ]; then + docker compose up $1 + else + docker-compose up $1 + fi +} + +function prefix_each() { + local delimiter="$1" + local result="" + shift + for element in "$@"; do + result+="$delimiter $element " + done + echo "$result" +} + +# === LOAD FROM CLI === + +while [[ $# -gt 0 ]]; do + key="$1" + + case $key in + -db|--database) + database="$2" + shift + ;; + -c|--cache) + cache="$2" + shift + ;; + -bs|--bux-server) + bux_server="$2" + shift + ;; + -pl|--pulse) + pulse="$2" + shift + ;; + -bwf|--bux-wallet-frontend) + bux_wallet_frontend="$2" + shift + ;; + -bwb|--bux-wallet-backend) + bux_wallet_backend="$2" + shift + ;; + --xpub) + admin_xpub="$2" + shift + ;; + --xprv) + admin_xpriv="$2" + shift + ;; + -pm|--paymail) + paymail_domain="$2" + shift + ;; + -l|--load) + load_config="true" + # no additional arguments so now `shift` command + ;; + -nrb|--no-rebuild) + no_rebuild="true" + # no additional arguments so now `shift` command + ;; + -b|--background) + background="true" + # no additional arguments so now `shift` command + ;; + -d|--debug) + debug="true" + # no additional arguments so now `shift` command + ;; + -h|--help) + echo -e "Usage: ./start.sh [OPTIONS]" + echo "" + echo "This script helps you to run SPV Wallet with your preferred database and cache storage." + echo "" + echo -e "Options:" + echo -e " -pm, --paymail\t\t PayMail domain for which to run all applications" + echo -e " -l, --load\t\t\t Load previously stored config from .env.config file" + echo -e " -nrb, --no-rebuild\t\t Prevent rebuild of docker images before running" + echo -e " -b, --background\t\t Whether the applications should be run in background" + echo -e " -d, --debug\t\t\t Run in debug mode" + echo -e " -h, --help\t\t\t Show this message" + echo -e "" + echo -e "<---------- BUX SERVER SECTION" + echo -e " -bs, --bux-server\t\t Whether the bux-server should be run - true/false" + echo -e " -db, --database\t\t Define database - postgresql, mongodb, sqlite" + echo -e " -c, --cache\t\t\t Define cache storage - freecache(in-memory), redis" + echo -e " --xpub\t\t\t Define admin xPub" + echo "" + echo -e "<---------- BUX WALLET SECTION" + echo -e " -bwf, --bux-wallet-frontend\t Whether the bux-wallet-frontend should be run - true/false" + echo -e " -bwb, --bux-wallet-backend\t Whether the bux-wallet-backend should be run - true/false" + echo -e " --xprv\t\t\t Define admin xPriv" + exit 1; + shift + ;; + *) + ;; + esac + shift +done + +# Welcome message +echo -e "${color_user}Welcome in SPV Wallet!${color_reset}" + +print_debug "Loaded config from CLI:" +print_state + + +# === LOAD FROM FILE === +if [ "$load_config" == "true" ]; then + if [ -f .env.config ]; then + print_debug "Loading config from .env.config" + + while IFS= read -r line; do + print_debug "Checking line '$line'" + load_from 'BUX_DB_DATASTORE_ENGINE' database + load_from 'BUX_CACHE_ENGINE' cache + load_from 'RUN_BUX_SERVER' bux_server + load_from 'RUN_PULSE' pulse + load_from 'RUN_BUX_WALLET_FRONTEND' bux_wallet_frontend + load_from 'RUN_BUX_WALLET_BACKEND' bux_wallet_backend + load_from 'RUN_PAYMAIL_DOMAIN' paymail_domain + load_from 'RUN_IN_BACKGROUND' background + load_from 'RUN_WITH_DEFAULT_XPUB' default_xpub + load_from 'BUX_AUTH_ADMIN_KEY' admin_xpub + load_from 'BUX_ADMIN_XPRIV' admin_xpriv + load_from 'RUN_WITHOUT_REBUILD' no_rebuild + done < ".env.config" + print_success "Config loaded from .env.config file" + print_debug "Config after loading .env.config:" + print_state + else + print_warning "File .env.config does not exist, but you choose to load from it." + fi +fi + +# === COLLECT CONFIG FROM USER IF NEEDED === + +# <---------- BUX SERVER SECTION +if [ "$database" == "" ]; then + database_options=("postgresql" "mongodb" "sqlite") + ask_for_choice "Select your database:" "${database_options[@]}" + + case $choice in + 1) database="postgresql";; + 2) database="mongodb";; + 3) database="sqlite";; + esac + print_debug "database: $database" +fi + +if [ "$cache" == "" ]; then + cache_options=("freecache" "redis") + ask_for_choice "Select your cache storage:" "${cache_options[@]}" + + case $choice in + 1) cache="freecache";; + 2) cache="redis";; + esac + print_debug "cache: $cache" +fi + +if [ "$bux_server" == "" ]; then + ask_for_yes_or_no "Do you want to run Bux-server?" + bux_server="$choice" + print_debug "bux_server: $bux_server" +fi + +if [ "$pulse" == "" ]; then + ask_for_yes_or_no "Do you want to run Pulse?" + pulse="$choice" + print_debug "pulse: $pulse" +fi + +if [ "$bux_wallet_frontend" == "" ]; then + ask_for_yes_or_no "Do you want to run bux-wallet-frontend?" + bux_wallet_frontend="$choice" + print_debug "bux_wallet_frontend: $bux_wallet_frontend" +fi + +if [ "$bux_wallet_backend" == "" ]; then + ask_for_yes_or_no "Do you want to run bux-wallet-backend?" + bux_wallet_backend="$choice" + print_debug "bux_wallet_backend: $bux_wallet_backend" +fi + +if [ "$bux_server" == "true" ] && [ "$admin_xpub" == "" ] && [ "$default_xpub" != "true" ]; then + ask_for_value "Define admin xPub (Leave empty to use the default one)" 'xpub' + + if [[ -n "$choice" ]]; then + admin_xpub=$choice + else + default_xpub="true" + fi + print_debug "admin_xpub: $admin_xpub" + print_debug "default_xpub: $default_xpub" +fi + +if [ "$bux_wallet_backend" == "true" ] && [ "$admin_xpriv" == "" ] && [ "$default_xpub" != "true" ]; then + ask_for_value "Define admin xPriv (Leave empty to use the default one)" 'xprv' + admin_xpriv=$choice + print_debug "admin_xpriv: $admin_xpriv" +fi + +if [ "$paymail_domain" == "" ] && { [ "$bux_wallet_backend" == "true" ] || [ "$bux_wallet_frontend" == "true" ] || [ "$bux_server" == "true" ]; }; then + ask_for_value "What PayMail domain should be configured in applications?" + paymail_domain=$choice + print_debug "paymail_domain: $paymail_domain" +fi + +if [ "$background" == "" ]; then + ask_for_yes_or_no "Do you want to run everything in the background?" "false" + background="$choice" + print_debug "background: $background" +fi + +# === SAVE CONFIG === +print_debug "Config before storing:" +print_state + +# Create the .env.config file +print_debug "Creating/Cleaning .env.config file." +echo "# Used by start.sh. All unknown variables will be removed after running the script" > ".env.config" +save_to 'BUX_DB_DATASTORE_ENGINE' database +save_to 'BUX_CACHE_ENGINE' cache +save_to 'RUN_BUX_SERVER' bux_server +save_to 'RUN_PULSE' pulse +save_to 'RUN_PAYMAIL_DOMAIN' paymail_domain +save_to 'RUN_BUX_WALLET_FRONTEND' bux_wallet_frontend +save_to 'RUN_BUX_WALLET_BACKEND' bux_wallet_backend +save_to 'RUN_IN_BACKGROUND' background +save_to 'RUN_WITH_DEFAULT_XPUB' default_xpub +save_to 'BUX_AUTH_ADMIN_KEY' admin_xpub +save_to 'BUX_ADMIN_XPRIV' admin_xpriv +save_to 'RUN_WITHOUT_REBUILD' no_rebuild +case $database in + postgresql) + save_value 'BUX_DB_SQL_HOST' "bux-postgresql" + save_value 'BUX_DB_SQL_NAME' "postgres" + save_value 'BUX_DB_SQL_USER' "postgres" + save_value 'BUX_DB_SQL_PASSWORD' "postgres" + ;; + mongodb) + save_value 'BUX_DB_MONGODB_URI' "mongodb://mongo:mongo@bux-mongodb:27017/" + ;; +esac + +if [ "$cache" == "redis" ]; then + save_value 'BUX_CACHE_REDIS_URL' "redis://redis:6379" +fi + +if [ "$bux_server" == "true" ]; then + save_value 'BUX_SERVER_URL' "http://bux-server:3003/v1" +else + save_value 'BUX_SERVER_URL' "http://host.docker.internal:3003/v1" +fi +if [ "$bux_wallet_backend" == "true" ]; then + save_value 'DB_HOST' "bux-postgresql" +fi + +if [ "$pulse" == "true" ]; then + save_value 'BUX_PAYMAIL_BEEF_PULSE_URL' "http://pulse:8080/api/v1/chain/merkleroot/verify" +else + save_value 'BUX_PAYMAIL_BEEF_PULSE_URL' "http://host.docker.internal:8080/api/v1/chain/merkleroot/verify" +fi +print_debug "Exporting RUN_PAYMAIL_DOMAIN environment variable" +export RUN_PAYMAIL_DOMAIN="$paymail_domain" + +print_success "File .env.config updated!" +print_debug "$(cat .env.config)" + +# === RUN WHAT IS NEEDED === +servicesToRun=() +servicesToHideLogs=() +additionalFlags=() + +case $database in + postgresql) + servicesToRun+=("bux-postgresql") + servicesToHideLogs+=("bux-postgresql") + ;; + mongodb) + servicesToRun+=("bux-mongodb") + servicesToHideLogs+=("bux-mongodb") + ;; +esac + +if [ "$cache" == "redis" ]; then + servicesToRun+=("bux-redis") + servicesToHideLogs+=("bux-redis") +fi + +if [ "$bux_server" == "true" ]; then + servicesToRun+=("bux-server") +fi + +if [ "$pulse" == "true" ]; then + servicesToRun+=("pulse") +fi + +if [ "$bux_wallet_backend" == "true" ]; then + servicesToRun+=("bux-wallet-backend") + servicesToRun+=("bux-postgresql") + servicesToHideLogs+=("bux-postgresql") +fi + +if [ "$bux_wallet_frontend" == "true" ]; then + servicesToRun+=("bux-wallet-frontend") + servicesToHideLogs+=("bux-wallet-frontend") +fi + +if [ "$no_rebuild" != "true" ]; then + additionalFlags+=("--build") +fi + +if [ "$background" == "true" ]; then + additionalFlags+=("-d") +fi + +docker_compose_up "${servicesToRun[*]} ${additionalFlags[*]} $(prefix_each '--no-attach ' ${servicesToHideLogs[*]})"