diff --git a/chainquery/.env b/chainquery/.env index 2ab5aea..ca64fcf 100644 --- a/chainquery/.env +++ b/chainquery/.env @@ -10,7 +10,7 @@ DEBUGMODE=false ## Mysql Creds ## ################# MYSQL_SERVER=10.5.1.10 -MYSQL_USER=changeme +MYSQL_USER=chainquery MYSQL_PASSWORD=changeme MYSQL_DATABASE=chainquery MYSQL_ROOT_PASSWORD=changeme diff --git a/chainquery/compile/.dockerignore b/chainquery/compile/.dockerignore new file mode 100644 index 0000000..5d1735a --- /dev/null +++ b/chainquery/compile/.dockerignore @@ -0,0 +1,4 @@ +data/ +data.z* +chainquery.z* +compile/ diff --git a/chainquery/compile/.env b/chainquery/compile/.env new file mode 100644 index 0000000..2ab5aea --- /dev/null +++ b/chainquery/compile/.env @@ -0,0 +1,16 @@ +COMPOSE_PROJECT_NAME=chainquery + +######################### +## Chainquery Settings ## +######################### +RPC_ALLOW_IP=10.5.1.3 +DEBUGMODE=false + +################# +## Mysql Creds ## +################# +MYSQL_SERVER=10.5.1.10 +MYSQL_USER=changeme +MYSQL_PASSWORD=changeme +MYSQL_DATABASE=chainquery +MYSQL_ROOT_PASSWORD=changeme diff --git a/chainquery/compile/.gitignore b/chainquery/compile/.gitignore new file mode 100644 index 0000000..5d1735a --- /dev/null +++ b/chainquery/compile/.gitignore @@ -0,0 +1,4 @@ +data/ +data.z* +chainquery.z* +compile/ diff --git a/chainquery/compile/Dockerfile b/chainquery/compile/Dockerfile new file mode 100644 index 0000000..5df53a6 --- /dev/null +++ b/chainquery/compile/Dockerfile @@ -0,0 +1,52 @@ +## This base image is for running latest chainquery +FROM ubuntu:18.04 as prep +LABEL MAINTAINER="leopere [at] nixc [dot] us" + +## Install everything needed to unzip the file containing Chainquery +RUN apt-get update && \ + apt-get -y install unzip curl && \ + apt-get autoclean -y && \ + rm -rf /var/lib/apt/lists/* && \ + mkdir /download + +## Download and extract the latest Zip containing Linux binary for Chainquery. +RUN curl -L -o /download/chainquery.zip $(curl -s https://api.github.com/repos/lbryio/chainquery/releases | fgrep 'Linux_x86_64.zip' | grep download | head -n 1 | cut -d'"' -f4) && \ + cd /download && \ + ls -lAh /download && pwd && \ + unzip chainquery.zip && \ + rm chainquery.zip + +## TODO: Use this instead of everything above here at some point. To do this we will need the LBRY team to host all of their release binaries in their latest form internally I guess since github doesn't support latest tags. +# ADD --chown=1000:1000 https://github.com/lbryio/chainquery/releases/download/latest/chainquery_latest_Linux_x86_64.zip /download + + +# TODO: Switch to Alpine eventually we might need to compile in Alpine for this. +FROM ubuntu:18.04 as app + +## Run as unprivileged user. +RUN adduser chainquery --gecos GECOS --shell /bin/bash --disabled-password --home /home/chainquery + +# Pull chainquery executable from prep container. +COPY --from=0 /download/chainquery /usr/bin + +# ADD --chown=1000:1000 https://github.com/lbryio/chainquery/releases/download/latest/chainquery_latest_Linux_x86_64.zip /usr/bin +# COPY stuff/chainqueryconfig.toml /etc/chainquery/ + +## Get latest chainqueryconfig.toml from master on github as template. +# TODO: add magic which pattern matches for chainqueryconfig.toml DEFAULT strings and replaces them with configured settings on container start. +ADD https://raw.githubusercontent.com/lbryio/chainquery/master/config/default/chainqueryconfig.toml /etc/chainquery/chainqueryconfig.toml.orig + +## Install start.sh as executable script in container $PATH +COPY stuff/start.sh /usr/local/bin/start + +# TODO: Implement docker-entrypoint if later required this might be handy if we need certain maintainence tasks to be executed in the live container. +# COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint + +# ## From here onward we're doing this with no root. +# USER chainquery + +## Expose Chainquery API port +EXPOSE 6300 + +## Execute start script earlier installed into $PATH +CMD ["start"] diff --git a/chainquery/compile/docker-compose.yml b/chainquery/compile/docker-compose.yml new file mode 100644 index 0000000..a101711 --- /dev/null +++ b/chainquery/compile/docker-compose.yml @@ -0,0 +1,57 @@ +version: '3.4' + +networks: + lbrynet: + external: true + +services: +########### +## MYSQL ## +########### +## MariaDB is currently not supported and neither is later versions of MySQL this may change. +## https://hub.docker.com/r/_/mariadb/ + mysql: + image: mysql:5.7.23 + restart: always + networks: + lbrynet: + ipv4_address: 10.5.1.10 + aliases: + - mysql + env_file: + - .env + expose: + - 3306 + ## TODO: I want to find a way that is acceptable to everyone to lock this up + ## and not share it with everyone at least eventually. + ports: + - 3306:3306 + volumes: + - ./data/db:/var/lib/mysql + - ./stuff/my.cnf:/etc/mysql/conf.d/chainquery-optimizations.cnf + +################ +## Chainquery ## +################ + chainquery: + build: + context: . + target: app + restart: always + networks: + lbrynet: + ipv4_address: 10.5.1.3 + env_file: + - .env + - ../lbrycrd/.env + labels: + - "traefik.expose=false" + expose: + - 6300 + ports: + - 6300:6300 + depends_on: + - mysql + ## TODO: Uncomment this in a docker-compose.override.yml to allow for external configurations. + # volumes: + # - ./data/config/chainqueryconfig.toml:/etc/chainquery/chainqueryconfig.toml diff --git a/chainquery/compile/quick-bootstrap.sh b/chainquery/compile/quick-bootstrap.sh new file mode 100644 index 0000000..953e821 --- /dev/null +++ b/chainquery/compile/quick-bootstrap.sh @@ -0,0 +1,148 @@ +#!/usr/bin/env bash + +## TODO: Be Polite and ask for confirmation. +function QandA() { + read -r -p "Continue with $1 [y/N] " response + response=${response,,} # tolower + if [[ "$response" =~ ^(yes|y)$ ]]; then + echo "Continuing with this." + eval $1 + else + echo "Skipping the $1." + fi +} + +## Check your $PATH for required dependencies. +## Stop and complain for now, later automagically install them. +## TODO: Add dependency checker. +function test_for_deps() { + ## Test for Command + if ! [ -x "$(command -v $1)" ]; then + echo "Error: $1 is not installed." >&2 + echo "You must have $1 installed." + else + echo "Info: $1 is installed." + fi +} + +## Declare Linux app dependencies to check for. +DEPENDENCIES=( + docker + docker-compose +) + +## TODO: Check for docker and docker-compose +function check_deps() { +for i in "${!DEPENDENCIES[@]}"; do + echo ${DEPENDENCIES[$i]}"_KEY" + ## Indirect references http://tldp.org/LDP/abs/html/ivr.html + eval TESTDEP=\$"${DEPENDENCIES[$i]}" + test_for_deps $TESTDEP +done +} + +function get_checkpoint() { + ## Get DB Checkpoint data. + echo Asked to get the latest checkpoint data, downloading latest checkpoint. + echo This data is fairly large so this saves you a few days of parsing the LBRY blockchain. + docker run -v $(pwd)/:/download --rm leopere/axel-docker http://chainquery-data.s3.amazonaws.com/chainquery-data.zip -o ./chainquery.zip +} + +################################# +## The real action begins here ## +################################# +## TODO: Add ways to get into and out of a bind here. +case $1 in + getdata ) + if [[ -f ./chainquery.zip ]]; then + echo "Found a copy of ./chainquery.zip already in your system." + echo "We recommend that you delete this data before proceeding and grab a fresh copy." + QandA "rm -f ./chainquery.zip" + get_checkpoint + else + get_checkpoint + fi + ;; + extract ) + ## Unpack the data again if need be. + echo Asked to unpack chainquery.zip if downloaded. + if [[ -f ./chainquery.zip ]]; then + docker run -v $(pwd)/:/data --rm leopere/unzip-docker ./chainquery.zip + else + echo "Could not extractas chainquery.zip did not exist." + echo "Feel free to execute './quick-bootstrap.sh getdata' first next time." + fi + ;; + cleanup ) + ## Remove any junk here. + echo Asked to clean up leftover chainquery.zip to save on disk space. + rm chainquery.zip + ;; + reset ) + ## Give up on everything and try again. + ## TODO: Make it very obvious with a nice little Y/N prompt that you're about to trash your settings and start over. + echo "Agressively Killing all chainquery and dependency containers." + echo "executing: docker-compose kill" + docker-compose kill + echo "Cleaning up stopped containers." + echo "executing: docker-compose rm -f" + docker-compose rm -f + rm -Rf ./data + rm -f ./chainquery.zip + ;; + start ) + ## Unsupported start command to start containers. + ## You can use this if you want to start this thing gracefully. + ## Ideally you would not use this in production. + echo "Asked to start chainquery gracefully for you." + echo "executing: docker-compose up -d mysql" + docker-compose up -d mysql + echo "giving mysql some time to establish schema, crypto, users, permissions, and tables" + sleep 30 + echo "Starting Chainquery" + echo "executing: docker-compose up -d chainquery" + docker-compose up -d chainquery + ## TODO: verify chainquery instance is up and healthy, this requires a functional HEALTHCHECK + echo "This should have chainquery up and running, currently theres no checks in this function to verify this however." + echo "Do feel free to execute 'docker-compose ps' to verify if its running and not restarting or exited." + echo "Final Note: You should try to use the docker-compose commands in the tutorial at https://github.com/lbryio/lbry-docker/blob/master/chainquery/README.md" + ;; + compress-latest-checkpoint-data ) + ## This is not intended for public use. + docker-compose stop chainquery + docker-compose stop mysql + sudo zip -r chainquery-data.zip data + docker-compose up -d mysql + sleep 30 + docker-compose up -d chainquery + ;; + upload-latest-checkpoint-data ) + ## This is not intended for public use. + aws s3 cp ./chainquery-data.zip s3://chainquery-data/chainquery-data.new + aws s3 rm s3://chainquery-data/chainquery-data.zip + aws s3 mv s3://chainquery-data/chainquery-data.new s3://chainquery-data/chainquery-data.zip + ;; + * ) + echo "==================================================" + echo "You look like you need usage examples let me help." + echo "==================================================" + echo "./quick-boostrap.sh {Parameter}" + echo "Example: ./quick-bootstrap.sh getdata # Downloads the latest Chainquery checkpoint data from a LBRYio official aws instance." + echo "" + echo "" + echo "==================================================" + echo "Usage example and available parameters" + echo "==================================================" + echo "" + echo "getdata # This function grabs the latest Chainquery checkpoint data." + echo "extract # Unpacks the chainquery data into the correct directory. ./data/" + echo "cleanup # Removes chainquery.zip" + echo "reset # Reset the state of these containers entirely, use if all else fails." + echo "" + echo "" + echo "==================================================" + echo "==================================================" + echo "Any other functions that are not documented here are not intended for public use." + echo " These functions are included in this repository to keep things in one place." + ;; +esac diff --git a/chainquery/compile/stuff/chainqueryconfig.toml b/chainquery/compile/stuff/chainqueryconfig.toml new file mode 100644 index 0000000..21f0832 --- /dev/null +++ b/chainquery/compile/stuff/chainqueryconfig.toml @@ -0,0 +1,16 @@ +## TODO: Don't hardcode this stuff for production + +#Debug mode outputs specific information to the console +debugmode=false + +#LBRYcrd URL is required for chainquery to query the blockchain +lbrycrdurl="rpc://lbryrpc:changeme@10.5.1.2:9245" + +#MySQL DSN is required for chainquery to store information. +mysqldsn="changeme:changeme@tcp(10.5.1.10:3306)/chainquery" + +#API MySQL DSN is required for chainquery to expose a SQL query service +apimysqldsn="changeme:changeme@tcp(10.5.1.10:3306)/chainquery" + +#The command that should be executed to trigger a self update of the software. For linux, for example, `.sh` +#DEFAULT-autoupdatecommand=[unset] diff --git a/chainquery/compile/stuff/debugpaste-it.sh b/chainquery/compile/stuff/debugpaste-it.sh new file mode 100644 index 0000000..f1f641a --- /dev/null +++ b/chainquery/compile/stuff/debugpaste-it.sh @@ -0,0 +1 @@ +#!/usr/bin/env bash diff --git a/chainquery/compile/stuff/docker-entrypoint.sh b/chainquery/compile/stuff/docker-entrypoint.sh new file mode 100644 index 0000000..77e798a --- /dev/null +++ b/chainquery/compile/stuff/docker-entrypoint.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +# default to run whatever the user wanted like "/bin/bash" +## If user runs no need to run any more of the entrypoint script. +if [[ -z "$@" ]]; then + echo "User did not attempt input. Now executing docker-entrypoint." +else + echo "Running $@." + exec "$@" + exit 1 +fi + +/bin/bash diff --git a/chainquery/compile/stuff/env-example b/chainquery/compile/stuff/env-example new file mode 100644 index 0000000..7ed2b53 --- /dev/null +++ b/chainquery/compile/stuff/env-example @@ -0,0 +1,19 @@ +COMPOSE_PROJECT_NAME=chainquery + +######################### +## Chainquery Settings ## +######################### +## TODO: Test to ensure these vars can be pulled from the lbrycrd .env +#RPC_USER=lbryrpc ## Not super necessery to change this. +#RPC_PASSWORD=changeme ## Please replace changeme. + +RPC_ALLOW_IP=10.5.1.3 ## You're better off not changing this. + +################# +## Mysql Creds ## +################# +MYSQL_SERVER=10.5.1.10 ## You're better off not changing this. +MYSQL_USER=changeme ## This could be changed. +MYSQL_PASSWORD=changeme ## This could be set to something random it sets this string for both Mysql's main user and Chainquery's MysqlDSN. +MYSQL_DATABASE=chainquery ## This can stay the same. +MYSQL_ROOT_PASSWORD=changeme ## Set this to something random and obnoxious we're not using it. diff --git a/chainquery/compile/stuff/healthcheck.sh b/chainquery/compile/stuff/healthcheck.sh new file mode 100644 index 0000000..24cd27b --- /dev/null +++ b/chainquery/compile/stuff/healthcheck.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +## TODO: Implement this at some point it requires CURL in the container. +curl http://localhost:6300/api/status diff --git a/chainquery/compile/stuff/my.cnf b/chainquery/compile/stuff/my.cnf new file mode 100644 index 0000000..cb0dbed --- /dev/null +++ b/chainquery/compile/stuff/my.cnf @@ -0,0 +1,9 @@ +# Default Homebrew MySQL server config +[mysqld] +# Only allow connections from localhost +innodb_log_file_size=5G +key_buffer_size=1G +innodb_flush_log_at_trx_commit = 0 +innodb_autoinc_lock_mode=2 +innodb_buffer_pool_size=1G +innodb_log_buffer_size=1G diff --git a/chainquery/compile/stuff/start.sh b/chainquery/compile/stuff/start.sh new file mode 100644 index 0000000..6b96167 --- /dev/null +++ b/chainquery/compile/stuff/start.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash + +## Config setup + +## Setup Values +DEBUGMODE=$(echo "debugmode=$DEBUGMODE") +LBRYCRDURL=$(echo "lbrycrdurl=\"rpc://$RPC_USER:$RPC_PASSWORD@10.5.1.2:9245\"") +MYSQLDSN=$(echo "mysqldsn=\"$MYSQL_USER:$MYSQL_PASSWORD@tcp($MYSQL_SERVER:3306)/$MYSQL_DATABASE\"") +APIMYSQLDSN=$(echo "apimysqldsn=\"$MYSQL_USER:$MYSQL_PASSWORD@tcp($MYSQL_SERVER:3306)/$MYSQL_DATABASE\"") + +## Setup Defaults +DEBUGMODE_DEFAULT='#DEFAULT-debugmode=false' +LBRYCRDURL_DEFAULT='#DEFAULT-lbrycrdurl="rpc://lbry:lbry@localhost:9245"' +MYSQLDSN_DEFAULT='#DEFAULT-mysqldsn="lbry:lbry@tcp(localhost:3306)/chainquery"' +APIMYSQLDSN_DEFAULT='#DEFAULT-apimysqldsn="lbry:lbry@tcp(localhost:3306)/chainquery"' + +## Add setup value variable name to this list to get processed on container start +CONFIG_SETTINGS=( + DEBUGMODE + LBRYCRDURL + MYSQLDSN + APIMYSQLDSN +) + +function set_configs() { + ## Set configs on container start if not already set. + for i in "${!CONFIG_SETTINGS[@]}"; do + ## Indirect references http://tldp.org/LDP/abs/html/ivr.html + eval FROM_STRING=\$"${CONFIG_SETTINGS[$i]}_DEFAULT" + eval TO_STRING=\$${CONFIG_SETTINGS[$i]} + ## TODO: Add a bit more magic to make sure that you're only configuring things if not set by config mounts. + sed -i "s~$FROM_STRING~"$TO_STRING"~g" /etc/chainquery/chainqueryconfig.toml + done + echo "Reading config for debugging." + cat /etc/chainquery/chainqueryconfig.toml +} + +if [[ ! -f /etc/chainquery/chainqueryconfig.toml ]]; then + echo "[INFO]: Did not find chainqueryconfig.toml" + echo " Installing default and configuring with provided environment variables if any." + ## Install fresh copy of config file. + echo "cp -v /etc/chainquery/chainqueryconfig.toml.orig /etc/chainquery/chainqueryconfig.toml" + cp -v /etc/chainquery/chainqueryconfig.toml.orig /etc/chainquery/chainqueryconfig.toml + chmod 755 /etc/chainquery/chainqueryconfig.toml + ls -lAh /etc/chainquery/ + set_configs +else + echo "[INFO]: Found a copy of chainqueryconfig.toml in /etc/chainquery" + echo " Attempting to non destructively install any new environment configurations." + set_configs +fi + +## For now keeping this simple. Potentially eventually add all command args as envvars for the Dockerfile or use safe way to add args via docker-compose.yml +su -c "chainquery serve -c "/etc/chainquery/"" chainquery