diff --git a/crawlers/deploy/deploy.bash b/crawlers/deploy/deploy.bash index f8c5dc50..dd7184c8 100755 --- a/crawlers/deploy/deploy.bash +++ b/crawlers/deploy/deploy.bash @@ -30,9 +30,6 @@ PARAMETERS_SCRIPT="${SCRIPT_DIR}/parameters.py" CHECKENV_PARAMETERS_SCRIPT="${SCRIPT_DIR}/parameters.bash" CHECKENV_NODES_CONNECTIONS_SCRIPT="${SCRIPT_DIR}/nodes-connections.bash" -# Crawlers server service file -CRAWLERS_SERVICE_FILE="moonstreamcrawlers.service" - # Ethereum service files ETHEREUM_SYNCHRONIZE_SERVICE="ethereum-synchronize.service" ETHEREUM_TRENDING_SERVICE_FILE="ethereum-trending.service" @@ -57,14 +54,6 @@ cd "${APP_CRAWLERS_DIR}/ethtxpool" HOME=/root /usr/local/go/bin/go build -o "${APP_CRAWLERS_DIR}/ethtxpool/ethtxpool" "${APP_CRAWLERS_DIR}/ethtxpool/main.go" cd "${EXEC_DIR}" -echo -echo -echo -e "${PREFIX_INFO} Building executable server of moonstreamcrawlers with Go" -EXEC_DIR=$(pwd) -cd "${APP_CRAWLERS_DIR}/server" -HOME=/root /usr/local/go/bin/go build -o "${APP_CRAWLERS_DIR}/server/moonstreamcrawlers" "${APP_CRAWLERS_DIR}/server/main.go" -cd "${EXEC_DIR}" - echo echo echo -e "${PREFIX_INFO} Upgrading Python pip and setuptools" @@ -133,15 +122,6 @@ cp "${SCRIPT_DIR}/${POLYGON_SYNCHRONIZE_SERVICE}" "/etc/systemd/system/${POLYGON systemctl daemon-reload systemctl restart "${POLYGON_SYNCHRONIZE_SERVICE}" -echo -echo -echo -e "${PREFIX_INFO} Replacing existing moonstreamcrawlers service definition with ${CRAWLERS_SERVICE_FILE}" -chmod 644 "${SCRIPT_DIR}/${CRAWLERS_SERVICE_FILE}" -cp "${SCRIPT_DIR}/${CRAWLERS_SERVICE_FILE}" "/etc/systemd/system/${CRAWLERS_SERVICE_FILE}" -systemctl daemon-reload -systemctl restart "${CRAWLERS_SERVICE_FILE}" -systemctl status "${CRAWLERS_SERVICE_FILE}" - echo echo echo -e "${PREFIX_INFO} Replacing existing Polygon missing service and timer with: ${POLYGON_MISSING_SERVICE_FILE}, ${POLYGON_MISSING_TIMER_FILE}" diff --git a/crawlers/deploy/moonstreamcrawlers.service b/crawlers/deploy/moonstreamcrawlers.service deleted file mode 100644 index 3b6e7104..00000000 --- a/crawlers/deploy/moonstreamcrawlers.service +++ /dev/null @@ -1,14 +0,0 @@ -[Unit] -Description=moonstreamcrawlers-service -After=network.target - -[Service] -User=ubuntu -Group=www-data -WorkingDirectory=/home/ubuntu/moonstream/crawlers/server -EnvironmentFile=/home/ubuntu/moonstream-secrets/app.env -ExecStart=/home/ubuntu/moonstream/crawlers/server/moonstreamcrawlers -host 0.0.0.0 -port "${MOONSTREAM_CRAWLERS_SERVER_PORT}" -SyslogIdentifier=moonstreamcrawlers - -[Install] -WantedBy=multi-user.target \ No newline at end of file diff --git a/crawlers/deploy/polygon-synchronize.service b/crawlers/deploy/polygon-synchronize.service index 3f002132..8f0d14ef 100644 --- a/crawlers/deploy/polygon-synchronize.service +++ b/crawlers/deploy/polygon-synchronize.service @@ -7,7 +7,7 @@ User=ubuntu Group=www-data WorkingDirectory=/home/ubuntu/moonstream/crawlers/mooncrawl EnvironmentFile=/home/ubuntu/moonstream-secrets/app.env -ExecStart=/home/ubuntu/moonstream-env/bin/python -m mooncrawl.crawler blocks synchronize --blockchain polygon -c 6 -j 1 +ExecStart=/home/ubuntu/moonstream-env/bin/python -m mooncrawl.crawler blocks synchronize --blockchain polygon -c 40 -j 1 SyslogIdentifier=polygon-synchronize [Install] diff --git a/crawlers/mooncrawl/mooncrawl/crawler.py b/crawlers/mooncrawl/mooncrawl/crawler.py index b2071553..49f05180 100644 --- a/crawlers/mooncrawl/mooncrawl/crawler.py +++ b/crawlers/mooncrawl/mooncrawl/crawler.py @@ -174,7 +174,7 @@ def crawler_blocks_missing_handler(args: argparse.Namespace) -> None: block_range = args.blocks if block_range is None: - confirmations = 50 + confirmations = 150 shift = 2000 _, latest_block_number = get_latest_blocks( AvailableBlockchainType(args.blockchain), confirmations diff --git a/crawlers/server/configs/settings.go b/crawlers/server/configs/settings.go deleted file mode 100644 index 87e63d79..00000000 --- a/crawlers/server/configs/settings.go +++ /dev/null @@ -1,14 +0,0 @@ -package settings - -import ( - "fmt" - "os" -) - -// Geth configs -var MOONSTREAM_NODE_ETHEREUM_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_ETHEREUM_IPC_ADDR") -var MOONSTREAM_NODE_ETHEREUM_IPC_PORT = os.Getenv("MOONSTREAM_NODE_ETHEREUM_IPC_PORT") -var MOONSTREAM_IPC_PATH = fmt.Sprintf("http://%s:%s", MOONSTREAM_NODE_ETHEREUM_IPC_ADDR, MOONSTREAM_NODE_ETHEREUM_IPC_PORT) - -// CORS -var MOONSTREAM_CORS_ALLOWED_ORIGINS = os.Getenv("MOONSTREAM_CORS_ALLOWED_ORIGINS") diff --git a/crawlers/server/go.mod b/crawlers/server/go.mod deleted file mode 100644 index 11b592d6..00000000 --- a/crawlers/server/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/bugout-dev/moonstream/crawlers/server - -go 1.17 diff --git a/nodes/deploy/ethereum/deploy.bash b/nodes/deploy/ethereum/deploy.bash index 537b0d90..9c6a72f7 100755 --- a/nodes/deploy/ethereum/deploy.bash +++ b/nodes/deploy/ethereum/deploy.bash @@ -1,6 +1,7 @@ #!/usr/bin/env bash -# Deployment script - intended to run on Moonstream node control server +# Deployment script - intended to run on Moonstream Ethereum node control server + # Colors C_RESET='\033[0m' C_RED='\033[1;31m' @@ -14,54 +15,72 @@ PREFIX_CRIT="${C_RED}[CRIT]${C_RESET} [$(date +%d-%m\ %T)]" # Main AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-east-1}" +APP_DIR="${APP_DIR:-/home/ubuntu/moonstream}" +APP_NODES_DIR="${APP_DIR}/nodes" SECRETS_DIR="${SECRETS_DIR:-/home/ubuntu/moonstream-secrets}" -NODE_PARAMETERS_ENV_PATH="${SECRETS_DIR}/node.env" +PARAMETERS_ENV_PATH="${SECRETS_DIR}/app.env" SCRIPT_DIR="$(realpath $(dirname $0))" BLOCKCHAIN="ethereum" -ETHEREUM_GETH_SERVICE="ethereum-node.service" + +# Parameters scripts +CHECKENV_PARAMETERS_SCRIPT="${SCRIPT_DIR}/parameters.bash" +CHECKENV_NODES_CONNECTIONS_SCRIPT="${SCRIPT_DIR}/nodes-connections.bash" + +# Nodes server service file +NODES_SERVER_SERVICE_FILE="moonstreamnodes.service" + +# Ethereum geth service file +ETHEREUM_GETH_SERVICE_FILE="geth.service" set -eu +echo +echo +echo -e "${PREFIX_INFO} Building executable server of moonstreamnodes with Go" +EXEC_DIR=$(pwd) +cd "${APP_NODES_DIR}/server" +HOME=/root /usr/local/go/bin/go build -o "${APP_NODES_DIR}/server/moonstreamnodes" "${APP_NODES_DIR}/server/main.go" +cd "${EXEC_DIR}" + echo echo echo -e "${PREFIX_INFO} Retrieving deployment parameters" mkdir -p "${SECRETS_DIR}" -> "${NODE_PARAMETERS_ENV_PATH}" +> "${PARAMETERS_ENV_PATH}" +bash "${CHECKENV_PARAMETERS_SCRIPT}" -vn -p "moonstream" -o "${PARAMETERS_ENV_PATH}" +echo +echo +echo -e "${PREFIX_INFO} Updating nodes connection parameters" +bash "${CHECKENV_NODES_CONNECTIONS_SCRIPT}" -v -f "${PARAMETERS_ENV_PATH}" + +echo +echo LOCAL_IP="$(ec2metadata --local-ipv4)" -echo -e "${PREFIX_INFO} Found assign subnet IP ${C_GREEN}${LOCAL_IP}${C_RESET} for machine" -ENV_PARAMETERS=$(aws ssm describe-parameters \ - --parameter-filters Key=tag:Product,Values=moonstream Key=tag:Blockchain,Values=$BLOCKCHAIN \ - | jq -r .Parameters[].Name) -ENV_PARAMETERS_VALUES=$(aws ssm get-parameters \ - --names $ENV_PARAMETERS \ - --query "Parameters[*].{Name:Name,Value:Value}") -ENV_PARAMETERS_VALUES_LENGTH=$(echo $ENV_PARAMETERS_VALUES | jq length) -echo -e "${PREFIX_INFO} Extracted ${ENV_PARAMETERS_VALUES_LENGTH} parameters" -for i in $(seq 0 $(($ENV_PARAMETERS_VALUES_LENGTH - 1))) -do - param_key=$(echo $ENV_PARAMETERS_VALUES | jq -r .[$i].Name) - if [ "$param_key" == "MOONSTREAM_NODE_ETHEREUM_IPC_ADDR" ] && [ -n "$LOCAL_IP" ] - then - param_value="\"$LOCAL_IP\"" - else - param_value=$(echo $ENV_PARAMETERS_VALUES | jq .[$i].Value) - fi - echo "$param_key=$param_value" >> "${NODE_PARAMETERS_ENV_PATH}" -done +echo -e "${PREFIX_INFO} Replacing current node IP environment variable with local IP ${C_GREEN}${LOCAL_IP}${C_RESET}" +sed -i "s|MOONSTREAM_NODE_ETHEREUM_IPC_ADDR=.*|MOONSTREAM_NODE_ETHEREUM_IPC_ADDR=\"$LOCAL_IP\"|" "${PARAMETERS_ENV_PATH}" + +echo +echo +echo -e "${PREFIX_INFO} Replacing existing moonstreamnodes service definition with ${NODES_SERVER_SERVICE_FILE}" +chmod 644 "${SCRIPT_DIR}/${NODES_SERVER_SERVICE_FILE}" +cp "${SCRIPT_DIR}/${NODES_SERVER_SERVICE_FILE}" "/etc/systemd/system/${NODES_SERVER_SERVICE_FILE}" +systemctl daemon-reload +systemctl restart "${NODES_SERVER_SERVICE_FILE}" +systemctl status "${NODES_SERVER_SERVICE_FILE}" echo echo echo -e "${PREFIX_INFO} Updating Ethereum Geth service" -if systemctl is-active --quiet "${ETHEREUM_GETH_SERVICE}" +if systemctl is-active --quiet "${ETHEREUM_GETH_SERVICE_FILE}" then - echo -e "${PREFIX_WARN} Ethereum Geth service ${ETHEREUM_GETH_SERVICE} already running" + echo -e "${PREFIX_WARN} Ethereum Geth service ${ETHEREUM_GETH_SERVICE_FILE} already running" else - echo -e "${PREFIX_INFO} Restart Geth service ${ETHEREUM_GETH_SERVICE}" - chmod 644 "${SCRIPT_DIR}/${ETHEREUM_GETH_SERVICE}" - cp "${SCRIPT_DIR}/${ETHEREUM_GETH_SERVICE}" "/etc/systemd/system/${ETHEREUM_GETH_SERVICE}" + echo -e "${PREFIX_INFO} Restart Geth service ${ETHEREUM_GETH_SERVICE_FILE}" + chmod 644 "${SCRIPT_DIR}/${ETHEREUM_GETH_SERVICE_FILE}" + cp "${SCRIPT_DIR}/${ETHEREUM_GETH_SERVICE_FILE}" "/etc/systemd/system/${ETHEREUM_GETH_SERVICE_FILE}" systemctl daemon-reload - systemctl disable "${ETHEREUM_GETH_SERVICE}" - systemctl restart "${ETHEREUM_GETH_SERVICE}" + systemctl disable "${ETHEREUM_GETH_SERVICE_FILE}" + systemctl restart "${ETHEREUM_GETH_SERVICE_FILE}" sleep 10 fi diff --git a/nodes/deploy/ethereum/ethereum-node.service b/nodes/deploy/ethereum/geth.service similarity index 52% rename from nodes/deploy/ethereum/ethereum-node.service rename to nodes/deploy/ethereum/geth.service index 22cd22ea..009bb9d2 100644 --- a/nodes/deploy/ethereum/ethereum-node.service +++ b/nodes/deploy/ethereum/geth.service @@ -5,11 +5,14 @@ After=network.target [Service] User=ubuntu Group=www-data -EnvironmentFile=/home/ubuntu/moonstream-secrets/node.env +EnvironmentFile=/home/ubuntu/moonstream-secrets/app.env ExecStart=/usr/bin/geth --syncmode snap --cache 4096 \ - --port "${MOONSTREAM_NODE_ETHEREUM_LISTENING_PORT}" --datadir /mnt/disks/nodes/ethereum \ + --port "${MOONSTREAM_NODE_ETHEREUM_LISTENING_PORT}" \ + --datadir /mnt/disks/nodes/ethereum \ --txpool.globalslots 153600 --txpool.globalqueue 3072 \ - --http --http.addr "${MOONSTREAM_NODE_ETHEREUM_IPC_ADDR}" --http.port "${MOONSTREAM_NODE_ETHEREUM_IPC_PORT}" --http.api eth,web3,txpool + --http --http.api eth,web3,txpool \ + --http.addr "${MOONSTREAM_NODE_ETHEREUM_IPC_ADDR}" \ + --http.port "${MOONSTREAM_NODE_ETHEREUM_IPC_PORT}" ExecStop=/bin/kill -s SIGINT -$MAINPID TimeoutStopSec=300 SyslogIdentifier=ethereum-node diff --git a/nodes/deploy/ethereum/moonstreamnodes.service b/nodes/deploy/ethereum/moonstreamnodes.service new file mode 100644 index 00000000..d31e36a0 --- /dev/null +++ b/nodes/deploy/ethereum/moonstreamnodes.service @@ -0,0 +1,14 @@ +[Unit] +Description=Moonstream node server +After=network.target + +[Service] +User=ubuntu +Group=www-data +WorkingDirectory=/home/ubuntu/moonstream/nodes/server +EnvironmentFile=/home/ubuntu/moonstream-secrets/app.env +ExecStart=/home/ubuntu/moonstream/nodes/server/moonstreamnodes -blockchain ethereum -host "${MOONSTREAM_NODE_ETHEREUM_IPC_ADDR}" -port "${MOONSTREAM_NODES_SERVER_PORT}" +SyslogIdentifier=moonstreamnodes + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/nodes/deploy/ethereum/nodes-connections.bash b/nodes/deploy/ethereum/nodes-connections.bash new file mode 100755 index 00000000..3718b5c9 --- /dev/null +++ b/nodes/deploy/ethereum/nodes-connections.bash @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# +# Update nodes connection address environment variables +# from AWS Route53 internal hosted zone + +VERSION='0.0.1' + +# Colors +C_RESET='\033[0m' +C_RED='\033[1;31m' +C_GREEN='\033[1;32m' +C_YELLOW='\033[1;33m' + +# Logs +PREFIX_INFO="${C_GREEN}[INFO]${C_RESET} [$(date +%d-%m\ %T)]" +PREFIX_WARN="${C_YELLOW}[WARN]${C_RESET} [$(date +%d-%m\ %T)]" +PREFIX_CRIT="${C_RED}[CRIT]${C_RESET} [$(date +%d-%m\ %T)]" + +# Print help message +function usage { + echo "Usage: $0 [-h] -p PRODUCT -f FILEPATH" + echo + echo "CLI to update nodes connection address environment + variables from AWS Route53 internal hosted zone" + echo + echo "Optional arguments:" + echo " -h Show this help message and exit" + echo " -f File path where environment variables update at" +} + +file_flag="" +verbose_flag="false" + +while getopts 'f:v' flag; do + case "${flag}" in + f) file_flag="${OPTARG}" ;; + h) usage + exit 1 ;; + v) verbose_flag="true" ;; + *) usage + exit 1 ;; + esac +done + +# Log messages +function verbose { + if [ "${verbose_flag}" == "true" ]; then + echo -e "$1" + fi +} + +# File flag should be specified +if [ -z "${file_flag}" ]; then + verbose "${PREFIX_CRIT} Please specify file path" + usage + exit 1 +fi + +if [ ! -f "${file_flag}" ]; then + verbose "${PREFIX_CRIT} Provided file does not exist" + usage + exit 1 +fi + +verbose "${PREFIX_INFO} Script version: v${VERSION}" + +verbose "${PREFIX_INFO} Source environment variables" +. ${file_flag} + +verbose "${PREFIX_INFO} Retrieving Ethereum node address" +RETRIEVED_NODE_ETHEREUM_IPC_ADDR=$(aws route53 list-resource-record-sets --hosted-zone-id "${MOONSTREAM_INTERNAL_HOSTED_ZONE_ID}" --query "ResourceRecordSets[?Name == '${MOONSTREAM_ETHEREUM_WEB3_PROVIDER_URI}.'].ResourceRecords[].Value" | jq -r .[0]) +if [ "$RETRIEVED_NODE_ETHEREUM_IPC_ADDR" == "null" ]; then + verbose "${PREFIX_CRIT} Ethereum node internal DNS record address is null" + exit 1 +fi + +verbose "${PREFIX_INFO} Retrieving Polygon node address" +RETRIEVED_NODE_POLYGON_IPC_ADDR=$(aws route53 list-resource-record-sets --hosted-zone-id "${MOONSTREAM_INTERNAL_HOSTED_ZONE_ID}" --query "ResourceRecordSets[?Name == '${MOONSTREAM_POLYGON_WEB3_PROVIDER_URI}.'].ResourceRecords[].Value" | jq -r .[0]) +if [ "$RETRIEVED_NODE_POLYGON_IPC_ADDR" == "null" ]; then + verbose "${PREFIX_CRIT} Polygon node internal DNS record address is null" + exit 1 +fi + +# TODO(kompotkot): Modify regexp to work with export prefix +verbose "${PREFIX_INFO} Updating MOONSTREAM_NODE_ETHEREUM_IPC_ADDR with ${RETRIEVED_NODE_ETHEREUM_IPC_ADDR}" +sed -i "s|^MOONSTREAM_NODE_ETHEREUM_IPC_ADDR=.*|MOONSTREAM_NODE_ETHEREUM_IPC_ADDR=\"$RETRIEVED_NODE_ETHEREUM_IPC_ADDR\"|" ${file_flag} + +verbose "${PREFIX_INFO} Updating MOONSTREAM_NODE_POLYGON_IPC_ADDR with ${RETRIEVED_NODE_POLYGON_IPC_ADDR}" +sed -i "s|^MOONSTREAM_NODE_POLYGON_IPC_ADDR=.*|MOONSTREAM_NODE_POLYGON_IPC_ADDR=\"$RETRIEVED_NODE_POLYGON_IPC_ADDR\"|" ${file_flag} diff --git a/nodes/deploy/ethereum/parameters.bash b/nodes/deploy/ethereum/parameters.bash new file mode 100755 index 00000000..605e4d28 --- /dev/null +++ b/nodes/deploy/ethereum/parameters.bash @@ -0,0 +1,98 @@ +#!/usr/bin/env bash +# +# Collect secrets from AWS SSM Parameter Store and +# opt out as environment variable exports. + +VERSION='0.0.2' + +# Colors +C_RESET='\033[0m' +C_RED='\033[1;31m' +C_GREEN='\033[1;32m' +C_YELLOW='\033[1;33m' + +# Logs +PREFIX_INFO="${C_GREEN}[INFO]${C_RESET} [$(date +%d-%m\ %T)]" +PREFIX_WARN="${C_YELLOW}[WARN]${C_RESET} [$(date +%d-%m\ %T)]" +PREFIX_CRIT="${C_RED}[CRIT]${C_RESET} [$(date +%d-%m\ %T)]" + +# Print help message +function usage { + echo "Usage: $0 [-h] -p PRODUCT -o OUTPUT" + echo + echo "CLI to collect secrets from AWS SSM Parameter Store +and output as environment variable exports" + echo + echo "Optional arguments:" + echo " -h Show this help message and exit" + echo " -n Provide true if server is Blockchain node" + echo " -o Output file name environment variables export to" + echo " -p Product tag (moonstream, spire, brood, drones)" +} + +# TODO(kompotkot): Flag for export prefix +node_flag="" +output_flag="" +product_flag="" +verbose_flag="false" + +while getopts 'no:p:v' flag; do + case "${flag}" in + n) node_flag="true" ;; + o) output_flag="${OPTARG}" ;; + p) product_flag="${OPTARG}" ;; + h) usage + exit 1 ;; + v) verbose_flag="true" ;; + *) usage + exit 1 ;; + esac +done + +# Log messages +function verbose { + if [ "${verbose_flag}" == "true" ]; then + echo -e "$1" + fi +} + +# Product flag should be specified +# TODO(kompotkot): Extend script to work with few product at once +if [ -z "${product_flag}" ]; then + verbose "${PREFIX_CRIT} Please specify product tag" + usage + exit 1 +fi + +verbose "${PREFIX_INFO} Script version: v${VERSION}" + +PARAMETER_FILTERS="Key=tag:Product,Values=${product_flag}" +if [ "${node_flag}" == "true" ]; then + verbose "${PREFIX_INFO} Node flag provided, extracting environment variables only for nodes" + PARAMETER_FILTERS="$PARAMETER_FILTERS Key=tag:Node,Values=true" +fi + +verbose "${PREFIX_INFO} Retrieving deployment parameters with tag ${C_GREEN}Product:${product_flag}${C_RESET}" +ENV_PARAMETERS=$(aws ssm describe-parameters \ + --parameter-filters ${PARAMETER_FILTERS} \ + | jq -r .Parameters[].Name) +if [ -z "${ENV_PARAMETERS}" ]; then + verbose "${PREFIX_CRIT} There no parameters for provided product tag" + exit 1 +fi + +verbose "${PREFIX_INFO} Retrieving parameters values" +ENV_PARAMETERS_VALUES=$(aws ssm get-parameters \ + --names ${ENV_PARAMETERS} \ + --query "Parameters[*].{Name:Name,Value:Value}") +ENV_PARAMETERS_VALUES_LENGTH=$(echo ${ENV_PARAMETERS_VALUES} | jq length) +verbose "${PREFIX_INFO} Extracted ${ENV_PARAMETERS_VALUES_LENGTH} parameters" +for i in $(seq 0 $((${ENV_PARAMETERS_VALUES_LENGTH} - 1))); do + param_key=$(echo ${ENV_PARAMETERS_VALUES} | jq -r .[$i].Name) + param_value=$(echo ${ENV_PARAMETERS_VALUES} | jq .[$i].Value) + if [ -z "${output_flag}" ]; then + echo "${param_key}=${param_value}" + else + echo "${param_key}=${param_value}" >> "${output_flag}" + fi +done diff --git a/nodes/deploy/polygon/deploy.bash b/nodes/deploy/polygon/deploy.bash index edb18314..ef18cf7b 100755 --- a/nodes/deploy/polygon/deploy.bash +++ b/nodes/deploy/polygon/deploy.bash @@ -1,6 +1,7 @@ #!/usr/bin/env bash -# Deployment script - intended to run on Moonstream node control server +# Deployment script - intended to run on Moonstream Polygon node control server + # Colors C_RESET='\033[0m' C_RED='\033[1;31m' @@ -14,26 +15,74 @@ PREFIX_CRIT="${C_RED}[CRIT]${C_RESET} [$(date +%d-%m\ %T)]" # Main AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-east-1}" +APP_DIR="${APP_DIR:-/home/ubuntu/moonstream}" +APP_NODES_DIR="${APP_DIR}/nodes" SECRETS_DIR="${SECRETS_DIR:-/home/ubuntu/moonstream-secrets}" -NODE_PARAMETERS_ENV_PATH="${SECRETS_DIR}/node.env" +PARAMETERS_ENV_PATH="${SECRETS_DIR}/app.env" SCRIPT_DIR="$(realpath $(dirname $0))" BLOCKCHAIN="polygon" HEIMDALL_HOME="/mnt/disks/nodes/${BLOCKCHAIN}/.heimdalld" +# Parameters scripts +CHECKENV_PARAMETERS_SCRIPT="${SCRIPT_DIR}/parameters.bash" +CHECKENV_NODES_CONNECTIONS_SCRIPT="${SCRIPT_DIR}/nodes-connections.bash" + +# Nodes server service file +NODES_SERVER_SERVICE_FILE="moonstreamnodes.service" + +# Polygon heimdalld service files +POLYGON_HEIMDALLD_SERVICE_FILE="heimdalld.service" +POLYGON_HEIMDALLD_BRIDGE_SERVICE_FILE="heimdalld-bridge.service" +POLYGON_HEIMDALLD_REST_SERVICE_FILE="heimdalld-rest-server.service" + +# Polygon bor service file +POLYGON_BOR_SERVICE_FILE="bor.service" + set -eu +echo +echo +echo -e "${PREFIX_INFO} Building executable server of moonstreamnodes with Go" +EXEC_DIR=$(pwd) +cd "${APP_NODES_DIR}/server" +HOME=/root /usr/local/go/bin/go build -o "${APP_NODES_DIR}/server/moonstreamnodes" "${APP_NODES_DIR}/server/main.go" +cd "${EXEC_DIR}" + echo echo echo -e "${PREFIX_INFO} Retrieving deployment parameters" mkdir -p "${SECRETS_DIR}" -> "${NODE_PARAMETERS_ENV_PATH}" +> "${PARAMETERS_ENV_PATH}" +bash "${CHECKENV_PARAMETERS_SCRIPT}" -vn -p "moonstream" -o "${PARAMETERS_ENV_PATH}" -GETH_NODE_ADDR=$(dig +short ethereum.moonstream.internal) -GETH_NODE_PORT=$(aws ssm get-parameters --names MOONSTREAM_NODE_ETHEREUM_IPC_PORT --query "Parameters[*]" | jq -r .[0].Value) -if [ -n "$GETH_NODE_ADDR" ] && [ -n "$GETH_NODE_PORT" ] -then - MOONSTREAM_NODE_ETHEREUM_IPC_URI="http://$GETH_NODE_ADDR:$GETH_NODE_PORT" - echo "MOONSTREAM_NODE_ETHEREUM_IPC_URI=\"$MOONSTREAM_NODE_ETHEREUM_IPC_URI\"" >> "${NODE_PARAMETERS_ENV_PATH}" - sed -i "s|^eth_rpc_url =.*|eth_rpc_url = \"$MOONSTREAM_NODE_ETHEREUM_IPC_URI\"|" $HEIMDALL_HOME/config/heimdall-config.toml - echo -e "${PREFIX_INFO} Updated ${C_GREEN}eth_rpc_url = $MOONSTREAM_NODE_ETHEREUM_IPC_URI${C_RESET} for Heimdall" -fi +echo +echo +echo -e "${PREFIX_INFO} Updating nodes connection parameters" +bash "${CHECKENV_NODES_CONNECTIONS_SCRIPT}" -v -f "${PARAMETERS_ENV_PATH}" + +echo +echo +LOCAL_IP="$(ec2metadata --local-ipv4)" +echo -e "${PREFIX_INFO} Replacing current node IP environment variable with local IP ${C_GREEN}${LOCAL_IP}${C_RESET}" +sed -i "s|MOONSTREAM_NODE_POLYGON_IPC_ADDR=.*|MOONSTREAM_NODE_POLYGON_IPC_ADDR=\"$LOCAL_IP\"|" "${PARAMETERS_ENV_PATH}" + +echo +echo +echo -e "${PREFIX_INFO} Replacing existing moonstreamnodes service definition with ${NODES_SERVER_SERVICE_FILE}" +chmod 644 "${SCRIPT_DIR}/${NODES_SERVER_SERVICE_FILE}" +cp "${SCRIPT_DIR}/${NODES_SERVER_SERVICE_FILE}" "/etc/systemd/system/${NODES_SERVER_SERVICE_FILE}" +systemctl daemon-reload +systemctl restart "${NODES_SERVER_SERVICE_FILE}" +systemctl status "${NODES_SERVER_SERVICE_FILE}" + +echo +echo +echo -e "${PREFIX_INFO} Source extracted parameters" +. "${PARAMETERS_ENV_PATH}" + +echo +echo +MOONSTREAM_NODE_ETHEREUM_IPC_URI="http://$MOONSTREAM_NODE_ETHEREUM_IPC_ADDR:$MOONSTREAM_NODE_ETHEREUM_IPC_PORT" +echo -e "${PREFIX_INFO} Update heimdall config file with Ethereum URI ${C_GREEN}${MOONSTREAM_NODE_ETHEREUM_IPC_URI}${C_RESET}" +sed -i "s|^eth_rpc_url =.*|eth_rpc_url = \"$MOONSTREAM_NODE_ETHEREUM_IPC_URI\"|" "${HEIMDALL_HOME}/config/heimdall-config.toml" +echo -e "${PREFIX_INFO} Updated ${C_GREEN}eth_rpc_url = $MOONSTREAM_NODE_ETHEREUM_IPC_URI${C_RESET} for heimdall" diff --git a/nodes/deploy/polygon/moonstreamnodes.service b/nodes/deploy/polygon/moonstreamnodes.service new file mode 100644 index 00000000..ecd16e08 --- /dev/null +++ b/nodes/deploy/polygon/moonstreamnodes.service @@ -0,0 +1,14 @@ +[Unit] +Description=Moonstream node server +After=network.target + +[Service] +User=ubuntu +Group=www-data +WorkingDirectory=/home/ubuntu/moonstream/nodes/server +EnvironmentFile=/home/ubuntu/moonstream-secrets/app.env +ExecStart=/home/ubuntu/moonstream/nodes/server/moonstreamnodes -blockchain polygon -host "${MOONSTREAM_NODE_POLYGON_IPC_ADDR}" -port "${MOONSTREAM_NODES_SERVER_PORT}" +SyslogIdentifier=moonstreamnodes + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/nodes/deploy/polygon/nodes-connections.bash b/nodes/deploy/polygon/nodes-connections.bash new file mode 100755 index 00000000..3718b5c9 --- /dev/null +++ b/nodes/deploy/polygon/nodes-connections.bash @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# +# Update nodes connection address environment variables +# from AWS Route53 internal hosted zone + +VERSION='0.0.1' + +# Colors +C_RESET='\033[0m' +C_RED='\033[1;31m' +C_GREEN='\033[1;32m' +C_YELLOW='\033[1;33m' + +# Logs +PREFIX_INFO="${C_GREEN}[INFO]${C_RESET} [$(date +%d-%m\ %T)]" +PREFIX_WARN="${C_YELLOW}[WARN]${C_RESET} [$(date +%d-%m\ %T)]" +PREFIX_CRIT="${C_RED}[CRIT]${C_RESET} [$(date +%d-%m\ %T)]" + +# Print help message +function usage { + echo "Usage: $0 [-h] -p PRODUCT -f FILEPATH" + echo + echo "CLI to update nodes connection address environment + variables from AWS Route53 internal hosted zone" + echo + echo "Optional arguments:" + echo " -h Show this help message and exit" + echo " -f File path where environment variables update at" +} + +file_flag="" +verbose_flag="false" + +while getopts 'f:v' flag; do + case "${flag}" in + f) file_flag="${OPTARG}" ;; + h) usage + exit 1 ;; + v) verbose_flag="true" ;; + *) usage + exit 1 ;; + esac +done + +# Log messages +function verbose { + if [ "${verbose_flag}" == "true" ]; then + echo -e "$1" + fi +} + +# File flag should be specified +if [ -z "${file_flag}" ]; then + verbose "${PREFIX_CRIT} Please specify file path" + usage + exit 1 +fi + +if [ ! -f "${file_flag}" ]; then + verbose "${PREFIX_CRIT} Provided file does not exist" + usage + exit 1 +fi + +verbose "${PREFIX_INFO} Script version: v${VERSION}" + +verbose "${PREFIX_INFO} Source environment variables" +. ${file_flag} + +verbose "${PREFIX_INFO} Retrieving Ethereum node address" +RETRIEVED_NODE_ETHEREUM_IPC_ADDR=$(aws route53 list-resource-record-sets --hosted-zone-id "${MOONSTREAM_INTERNAL_HOSTED_ZONE_ID}" --query "ResourceRecordSets[?Name == '${MOONSTREAM_ETHEREUM_WEB3_PROVIDER_URI}.'].ResourceRecords[].Value" | jq -r .[0]) +if [ "$RETRIEVED_NODE_ETHEREUM_IPC_ADDR" == "null" ]; then + verbose "${PREFIX_CRIT} Ethereum node internal DNS record address is null" + exit 1 +fi + +verbose "${PREFIX_INFO} Retrieving Polygon node address" +RETRIEVED_NODE_POLYGON_IPC_ADDR=$(aws route53 list-resource-record-sets --hosted-zone-id "${MOONSTREAM_INTERNAL_HOSTED_ZONE_ID}" --query "ResourceRecordSets[?Name == '${MOONSTREAM_POLYGON_WEB3_PROVIDER_URI}.'].ResourceRecords[].Value" | jq -r .[0]) +if [ "$RETRIEVED_NODE_POLYGON_IPC_ADDR" == "null" ]; then + verbose "${PREFIX_CRIT} Polygon node internal DNS record address is null" + exit 1 +fi + +# TODO(kompotkot): Modify regexp to work with export prefix +verbose "${PREFIX_INFO} Updating MOONSTREAM_NODE_ETHEREUM_IPC_ADDR with ${RETRIEVED_NODE_ETHEREUM_IPC_ADDR}" +sed -i "s|^MOONSTREAM_NODE_ETHEREUM_IPC_ADDR=.*|MOONSTREAM_NODE_ETHEREUM_IPC_ADDR=\"$RETRIEVED_NODE_ETHEREUM_IPC_ADDR\"|" ${file_flag} + +verbose "${PREFIX_INFO} Updating MOONSTREAM_NODE_POLYGON_IPC_ADDR with ${RETRIEVED_NODE_POLYGON_IPC_ADDR}" +sed -i "s|^MOONSTREAM_NODE_POLYGON_IPC_ADDR=.*|MOONSTREAM_NODE_POLYGON_IPC_ADDR=\"$RETRIEVED_NODE_POLYGON_IPC_ADDR\"|" ${file_flag} diff --git a/nodes/deploy/polygon/parameters.bash b/nodes/deploy/polygon/parameters.bash new file mode 100755 index 00000000..605e4d28 --- /dev/null +++ b/nodes/deploy/polygon/parameters.bash @@ -0,0 +1,98 @@ +#!/usr/bin/env bash +# +# Collect secrets from AWS SSM Parameter Store and +# opt out as environment variable exports. + +VERSION='0.0.2' + +# Colors +C_RESET='\033[0m' +C_RED='\033[1;31m' +C_GREEN='\033[1;32m' +C_YELLOW='\033[1;33m' + +# Logs +PREFIX_INFO="${C_GREEN}[INFO]${C_RESET} [$(date +%d-%m\ %T)]" +PREFIX_WARN="${C_YELLOW}[WARN]${C_RESET} [$(date +%d-%m\ %T)]" +PREFIX_CRIT="${C_RED}[CRIT]${C_RESET} [$(date +%d-%m\ %T)]" + +# Print help message +function usage { + echo "Usage: $0 [-h] -p PRODUCT -o OUTPUT" + echo + echo "CLI to collect secrets from AWS SSM Parameter Store +and output as environment variable exports" + echo + echo "Optional arguments:" + echo " -h Show this help message and exit" + echo " -n Provide true if server is Blockchain node" + echo " -o Output file name environment variables export to" + echo " -p Product tag (moonstream, spire, brood, drones)" +} + +# TODO(kompotkot): Flag for export prefix +node_flag="" +output_flag="" +product_flag="" +verbose_flag="false" + +while getopts 'no:p:v' flag; do + case "${flag}" in + n) node_flag="true" ;; + o) output_flag="${OPTARG}" ;; + p) product_flag="${OPTARG}" ;; + h) usage + exit 1 ;; + v) verbose_flag="true" ;; + *) usage + exit 1 ;; + esac +done + +# Log messages +function verbose { + if [ "${verbose_flag}" == "true" ]; then + echo -e "$1" + fi +} + +# Product flag should be specified +# TODO(kompotkot): Extend script to work with few product at once +if [ -z "${product_flag}" ]; then + verbose "${PREFIX_CRIT} Please specify product tag" + usage + exit 1 +fi + +verbose "${PREFIX_INFO} Script version: v${VERSION}" + +PARAMETER_FILTERS="Key=tag:Product,Values=${product_flag}" +if [ "${node_flag}" == "true" ]; then + verbose "${PREFIX_INFO} Node flag provided, extracting environment variables only for nodes" + PARAMETER_FILTERS="$PARAMETER_FILTERS Key=tag:Node,Values=true" +fi + +verbose "${PREFIX_INFO} Retrieving deployment parameters with tag ${C_GREEN}Product:${product_flag}${C_RESET}" +ENV_PARAMETERS=$(aws ssm describe-parameters \ + --parameter-filters ${PARAMETER_FILTERS} \ + | jq -r .Parameters[].Name) +if [ -z "${ENV_PARAMETERS}" ]; then + verbose "${PREFIX_CRIT} There no parameters for provided product tag" + exit 1 +fi + +verbose "${PREFIX_INFO} Retrieving parameters values" +ENV_PARAMETERS_VALUES=$(aws ssm get-parameters \ + --names ${ENV_PARAMETERS} \ + --query "Parameters[*].{Name:Name,Value:Value}") +ENV_PARAMETERS_VALUES_LENGTH=$(echo ${ENV_PARAMETERS_VALUES} | jq length) +verbose "${PREFIX_INFO} Extracted ${ENV_PARAMETERS_VALUES_LENGTH} parameters" +for i in $(seq 0 $((${ENV_PARAMETERS_VALUES_LENGTH} - 1))); do + param_key=$(echo ${ENV_PARAMETERS_VALUES} | jq -r .[$i].Name) + param_value=$(echo ${ENV_PARAMETERS_VALUES} | jq .[$i].Value) + if [ -z "${output_flag}" ]; then + echo "${param_key}=${param_value}" + else + echo "${param_key}=${param_value}" >> "${output_flag}" + fi +done diff --git a/crawlers/server/cmd/data.go b/nodes/server/cmd/data.go similarity index 100% rename from crawlers/server/cmd/data.go rename to nodes/server/cmd/data.go diff --git a/crawlers/server/cmd/middlewares.go b/nodes/server/cmd/middlewares.go similarity index 95% rename from crawlers/server/cmd/middlewares.go rename to nodes/server/cmd/middlewares.go index 444cfc7b..a106f2a5 100644 --- a/crawlers/server/cmd/middlewares.go +++ b/nodes/server/cmd/middlewares.go @@ -6,7 +6,7 @@ import ( "strings" "time" - settings "github.com/bugout-dev/moonstream/crawlers/server/configs" + settings "github.com/bugout-dev/moonstream/nodes/server/configs" ) // Handle panic errors to prevent server shutdown diff --git a/crawlers/server/cmd/routes.go b/nodes/server/cmd/routes.go similarity index 74% rename from crawlers/server/cmd/routes.go rename to nodes/server/cmd/routes.go index 7b1dcad6..fd3ca537 100644 --- a/crawlers/server/cmd/routes.go +++ b/nodes/server/cmd/routes.go @@ -9,7 +9,7 @@ import ( "strconv" "strings" - settings "github.com/bugout-dev/moonstream/crawlers/server/configs" + settings "github.com/bugout-dev/moonstream/nodes/server/configs" ) func pingRoute(w http.ResponseWriter, req *http.Request) { @@ -19,7 +19,7 @@ func pingRoute(w http.ResponseWriter, req *http.Request) { } // Fetch latest block from Geth -func pingGethRoute(w http.ResponseWriter, req *http.Request) { +func (es *extendedServer) pingGethRoute(w http.ResponseWriter, req *http.Request) { postBody, err := json.Marshal(map[string]interface{}{ "jsonrpc": "2.0", "method": "eth_blockNumber", @@ -31,7 +31,17 @@ func pingGethRoute(w http.ResponseWriter, req *http.Request) { http.Error(w, http.StatusText(500), http.StatusInternalServerError) return } - gethResponse, err := http.Post(settings.MOONSTREAM_IPC_PATH, "application/json", + var IPC_PATH string + if es.blockchain == "ethereum" { + IPC_PATH = settings.MOONSTREAM_NODE_ETHEREUM_IPC_PATH + } else if es.blockchain == "polygon" { + IPC_PATH = settings.MOONSTREAM_NODE_POLYGON_IPC_PATH + } else { + log.Printf("Unaccepted blockchain type: %s", es.blockchain) + http.Error(w, http.StatusText(400), http.StatusBadRequest) + return + } + gethResponse, err := http.Post(IPC_PATH, "application/json", bytes.NewBuffer(postBody)) if err != nil { log.Printf("Unable to request geth, error: %s", err) diff --git a/crawlers/server/cmd/server.go b/nodes/server/cmd/server.go similarity index 67% rename from crawlers/server/cmd/server.go rename to nodes/server/cmd/server.go index cef4e36b..e5d2d4a5 100644 --- a/crawlers/server/cmd/server.go +++ b/nodes/server/cmd/server.go @@ -7,16 +7,24 @@ import ( "time" ) +type extendedServer struct { + blockchain string +} + func InitServer() { var listeningAddr string var listeningPort string + var blockchain string flag.StringVar(&listeningAddr, "host", "127.0.0.1", "Server listening address") flag.StringVar(&listeningPort, "port", "8080", "Server listening port") + flag.StringVar(&blockchain, "blockchain", "", "Blockchain to work with (Ethereum/Polygon)") flag.Parse() + es := extendedServer{blockchain: blockchain} + serverMux := http.NewServeMux() serverMux.HandleFunc("/ping", pingRoute) - serverMux.HandleFunc("/status", pingGethRoute) + serverMux.HandleFunc("/status", es.pingGethRoute) // Set middlewares from bottom to top serverHandler := corsMiddleware(serverMux) @@ -30,6 +38,6 @@ func InitServer() { WriteTimeout: 10 * time.Second, } - log.Printf("Starting server at %s:%s\n", listeningAddr, listeningPort) + log.Printf("Starting server at %s:%s for blockchain %s\n", listeningAddr, listeningPort, blockchain) server.ListenAndServe() } diff --git a/nodes/server/configs/settings.go b/nodes/server/configs/settings.go new file mode 100644 index 00000000..9d8c0672 --- /dev/null +++ b/nodes/server/configs/settings.go @@ -0,0 +1,19 @@ +package settings + +import ( + "fmt" + "os" +) + +// Geth configs +var MOONSTREAM_NODE_ETHEREUM_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_ETHEREUM_IPC_ADDR") +var MOONSTREAM_NODE_ETHEREUM_IPC_PORT = os.Getenv("MOONSTREAM_NODE_ETHEREUM_IPC_PORT") +var MOONSTREAM_NODE_ETHEREUM_IPC_PATH = fmt.Sprintf("http://%s:%s", MOONSTREAM_NODE_ETHEREUM_IPC_ADDR, MOONSTREAM_NODE_ETHEREUM_IPC_PORT) + +// Bor configs +var MOONSTREAM_NODE_POLYGON_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_POLYGON_IPC_ADDR") +var MOONSTREAM_NODE_POLYGON_IPC_PORT = os.Getenv("MOONSTREAM_NODE_POLYGON_IPC_PORT") +var MOONSTREAM_NODE_POLYGON_IPC_PATH = fmt.Sprintf("http://%s:%s", MOONSTREAM_NODE_POLYGON_IPC_ADDR, MOONSTREAM_NODE_POLYGON_IPC_PORT) + +// CORS +var MOONSTREAM_CORS_ALLOWED_ORIGINS = os.Getenv("MOONSTREAM_CORS_ALLOWED_ORIGINS") diff --git a/crawlers/server/dev.sh b/nodes/server/dev.sh similarity index 67% rename from crawlers/server/dev.sh rename to nodes/server/dev.sh index 2e464806..0453ee64 100755 --- a/crawlers/server/dev.sh +++ b/nodes/server/dev.sh @@ -5,5 +5,6 @@ set -e MOONSTREAM_CLUSTER_SERVER_HOST="${MOONSTREAM_CLUSTER_SERVER_HOST:-0.0.0.0}" MOONSTREAM_CLUSTER_SERVER_PORT="${MOONSTREAM_CLUSTER_SERVER_PORT:-8080}" +MOONSTREAM_CLUSTER_BLOCKCHAIN="${MOONSTREAM_CLUSTER_BLOCKCHAIN:-ethereum}" -go run main.go -host "${MOONSTREAM_CLUSTER_SERVER_HOST}" -port "${MOONSTREAM_CLUSTER_SERVER_PORT}" +go run main.go -host "${MOONSTREAM_CLUSTER_SERVER_HOST}" -port "${MOONSTREAM_CLUSTER_SERVER_PORT}" -blockchain "${MOONSTREAM_CLUSTER_BLOCKCHAIN}" diff --git a/nodes/server/go.mod b/nodes/server/go.mod new file mode 100644 index 00000000..d92d7f57 --- /dev/null +++ b/nodes/server/go.mod @@ -0,0 +1,3 @@ +module github.com/bugout-dev/moonstream/nodes/server + +go 1.17 diff --git a/crawlers/server/main.go b/nodes/server/main.go similarity index 51% rename from crawlers/server/main.go rename to nodes/server/main.go index 014ea56a..cde1547c 100644 --- a/crawlers/server/main.go +++ b/nodes/server/main.go @@ -1,7 +1,7 @@ package main import ( - "github.com/bugout-dev/moonstream/crawlers/server/cmd" + "github.com/bugout-dev/moonstream/nodes/server/cmd" ) func main() { diff --git a/crawlers/server/sample.env b/nodes/server/sample.env similarity index 70% rename from crawlers/server/sample.env rename to nodes/server/sample.env index 7be46542..e0d7211a 100644 --- a/crawlers/server/sample.env +++ b/nodes/server/sample.env @@ -1,3 +1,5 @@ export MOONSTREAM_NODE_ETHEREUM_IPC_ADDR="127.0.0.1" export MOONSTREAM_NODE_ETHEREUM_IPC_PORT="8545" +export MOONSTREAM_NODE_POLYGON_IPC_ADDR="127.0.0.1" +export MOONSTREAM_NODE_POLYGON_IPC_PORT="8545" export MOONSTREAM_CORS_ALLOWED_ORIGINS="http://localhost:3000,https://moonstream.to,https://www.moonstream.to,https://alpha.moonstream.to"