From f9c0227ec702596b5ca68fe386cbc1029de2c42e Mon Sep 17 00:00:00 2001 From: DaMandal0rian Date: Fri, 1 Nov 2024 01:34:44 +0300 Subject: [PATCH] improve the subql stack launch script fix variables remove run script since its not stable add env variables --- .../base/bootstrap_nova_subql_provisioner.tf | 47 ++++++----- .../subql/base/bootstrap_subql_provisioner.tf | 45 ++++++----- .../subql/base/scripts/install_subql_stack.sh | 78 +++++++++++-------- 3 files changed, 98 insertions(+), 72 deletions(-) diff --git a/templates/terraform/subql/base/bootstrap_nova_subql_provisioner.tf b/templates/terraform/subql/base/bootstrap_nova_subql_provisioner.tf index f4e9502..274d73c 100644 --- a/templates/terraform/subql/base/bootstrap_nova_subql_provisioner.tf +++ b/templates/terraform/subql/base/bootstrap_nova_subql_provisioner.tf @@ -65,7 +65,7 @@ resource "null_resource" "setup-nova-blue-subql-nodes" { } resource "null_resource" "setup-nova-green-subql-nodes" { - count = length(local.green_subql_node_ip_v4) + count = length(local.nova_green_subql_node_ip_v4) depends_on = [aws_instance.nova_subql_green_node] @@ -150,17 +150,21 @@ resource "null_resource" "start-nova-blue-subql-nodes" { "sudo systemctl restart docker.service", # set hostname - "sudo hostnamectl set-hostname subql-${var.blue-subql-node-config.network-name}", + "sudo hostnamectl set-hostname subql-${var.nova-blue-subql-node-config.deployment-color}-${var.nova-blue-subql-node-config.network-name}", + + # run subql install script + "chmod +x /home/${var.ssh_user}/subql/install_subql_stack.sh", + "bash /home/${var.ssh_user}/subql/install_subql_stack.sh", + "echo 'Installation Complete'", # create .env file - "echo NR_API_KEY=${var.nr_api_key} >> /home/${var.ssh_user}/subql/.env", - "echo DOCKER_TAG=${var.blue-subql-node-config.docker-tag} >> /home/${var.ssh_user}/subql/.env", - "echo POSTGRES_PASSWORD=${var.postgres_password} >> /home/${var.ssh_user}/subql/.env", - "echo HASURA_GRAPHQL_ADMIN_SECRET=${var.hasura_graphql_admin_secret} >> /home/${var.ssh_user}/subql/.env", - - # run subql lauch script - "chmod +x /home/${var.ssh_user}/subql/subql_stack.sh", - "bash /home/${var.ssh_user}/subql/subql_stack.sh", + "grep -q '^NR_AGENT_IDENTIFIER=' /home/${var.ssh_user}/astral/.env && sed -i 's/^NR_AGENT_IDENTIFIER=.*/NR_AGENT_IDENTIFIER=subql-${var.nova-blue-subql-node-config.deployment-color}-${var.nova-blue-subql-node-config.network-name}/' /home/${var.ssh_user}/astral/.env || echo NR_AGENT_IDENTIFIER=subql-${var.nova-blue-subql-node-config.deployment-color}-${var.nova-blue-subql-node-config.network-name} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^NR_API_KEY=' /home/${var.ssh_user}/astral/.env && sed -i 's/^NR_API_KEY=.*/NR_API_KEY=${var.nr_api_key}/' /home/${var.ssh_user}/astral/.env || echo NR_API_KEY=${var.nr_api_key} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^DOCKER_TAG=' /home/${var.ssh_user}/astral/.env && sed -i 's/^DOCKER_TAG=.*/DOCKER_TAG=${var.nova-blue-subql-node-config.docker-tag}/' /home/${var.ssh_user}/astral/.env || echo DOCKER_TAG=${var.nova-blue-subql-node-config.docker-tag} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^DB_PASSWORD=' /home/${var.ssh_user}/astral/.env && sed -i 's/^DB_PASSWORD=.*/DB_PASSWORD=${var.postgres_password}/' /home/${var.ssh_user}/astral/.env || echo DB_PASSWORD=${var.postgres_password} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^HASURA_GRAPHQL_ADMIN_SECRET=' /home/${var.ssh_user}/astral/.env && sed -i 's/^HASURA_GRAPHQL_ADMIN_SECRET=.*/HASURA_GRAPHQL_ADMIN_SECRET=${var.hasura_graphql_admin_secret}/' /home/${var.ssh_user}/astral/.env || echo HASURA_GRAPHQL_ADMIN_SECRET=${var.hasura_graphql_admin_secret} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^HASURA_GRAPHQL_JWT_SECRET=' /home/${var.ssh_user}/astral/.env && sed -i 's|^HASURA_GRAPHQL_JWT_SECRET=.*|HASURA_GRAPHQL_JWT_SECRET={\"type\":\"HS256\",\"key\":\"${var.hasura_graphql_jwt_secret}\"}|' /home/${var.ssh_user}/astral/.env || echo HASURA_GRAPHQL_JWT_SECRET={\"type\":\"HS256\",\"key\":\"${var.hasura_graphql_jwt_secret}\"} >> /home/${var.ssh_user}/astral/.env", + "echo 'Installation Complete'", ] } @@ -208,18 +212,21 @@ resource "null_resource" "nova-start-green-subql-nodes" { "sudo systemctl restart docker.service", # set hostname - "sudo hostnamectl set-hostname subql-${var.green-subql-node-config.deployment-color}-${var.green-subql-node-config.network-name}", + "sudo hostnamectl set-hostname subql-${var.nova-green-subql-node-config.deployment-color}-${var.nova-green-subql-node-config.network-name}", + + # run subql install script + "chmod +x /home/${var.ssh_user}/subql/install_subql_stack.sh", + "bash /home/${var.ssh_user}/subql/install_subql_stack.sh", + "echo 'Installation Complete'", # create .env file - "echo NR_API_KEY=${var.nr_api_key} >> /home/${var.ssh_user}/subql/.env", - "echo DOCKER_TAG=${var.blue-subql-node-config.docker-tag} >> /home/${var.ssh_user}/subql/.env", - "echo POSTGRES_PASSWORD=${var.postgres_password} >> /home/${var.ssh_user}/subql/.env", - "echo HASURA_GRAPHQL_ADMIN_SECRET=${var.hasura_graphql_admin_secret} >> /home/${var.ssh_user}/subql/.env", - "echo HASURA_GRAPHQL_JWT_SECRET=${var.hasura_graphql_jwt_secret} >> /home/${var.ssh_user}/subql/.env", - - # run subql lauch script - "chmod +x /home/${var.ssh_user}/subql/subql_stack.sh", - "bash /home/${var.ssh_user}/subql/subql_stack.sh", + "grep -q '^NR_AGENT_IDENTIFIER=' /home/${var.ssh_user}/astral/.env && sed -i 's/^NR_AGENT_IDENTIFIER=.*/NR_AGENT_IDENTIFIER=subql-${var.nova-green-subql-node-config.deployment-color}-${var.nova-green-subql-node-config.network-name}/' /home/${var.ssh_user}/astral/.env || echo NR_AGENT_IDENTIFIER=subql-${var.nova-green-subql-node-config.deployment-color}-${var.nova-green-subql-node-config.network-name} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^NR_API_KEY=' /home/${var.ssh_user}/astral/.env && sed -i 's/^NR_API_KEY=.*/NR_API_KEY=${var.nr_api_key}/' /home/${var.ssh_user}/astral/.env || echo NR_API_KEY=${var.nr_api_key} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^DOCKER_TAG=' /home/${var.ssh_user}/astral/.env && sed -i 's/^DOCKER_TAG=.*/DOCKER_TAG=${var.nova-green-subql-node-config.docker-tag}/' /home/${var.ssh_user}/astral/.env || echo DOCKER_TAG=${var.nova-green-subql-node-config.docker-tag} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^DB_PASSWORD=' /home/${var.ssh_user}/astral/.env && sed -i 's/^DB_PASSWORD=.*/DB_PASSWORD=${var.postgres_password}/' /home/${var.ssh_user}/astral/.env || echo DB_PASSWORD=${var.postgres_password} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^HASURA_GRAPHQL_ADMIN_SECRET=' /home/${var.ssh_user}/astral/.env && sed -i 's/^HASURA_GRAPHQL_ADMIN_SECRET=.*/HASURA_GRAPHQL_ADMIN_SECRET=${var.hasura_graphql_admin_secret}/' /home/${var.ssh_user}/astral/.env || echo HASURA_GRAPHQL_ADMIN_SECRET=${var.hasura_graphql_admin_secret} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^HASURA_GRAPHQL_JWT_SECRET=' /home/${var.ssh_user}/astral/.env && sed -i 's|^HASURA_GRAPHQL_JWT_SECRET=.*|HASURA_GRAPHQL_JWT_SECRET={\"type\":\"HS256\",\"key\":\"${var.hasura_graphql_jwt_secret}\"}|' /home/${var.ssh_user}/astral/.env || echo HASURA_GRAPHQL_JWT_SECRET={\"type\":\"HS256\",\"key\":\"${var.hasura_graphql_jwt_secret}\"} >> /home/${var.ssh_user}/astral/.env", + "echo 'Installation Complete'", ] } diff --git a/templates/terraform/subql/base/bootstrap_subql_provisioner.tf b/templates/terraform/subql/base/bootstrap_subql_provisioner.tf index 0ce92d0..94039f7 100644 --- a/templates/terraform/subql/base/bootstrap_subql_provisioner.tf +++ b/templates/terraform/subql/base/bootstrap_subql_provisioner.tf @@ -53,7 +53,7 @@ resource "null_resource" "setup-blue-subql-nodes" { # copy subql launch script provisioner "file" { source = "${var.path_to_scripts}/install_subql_stack.sh" - destination = "/home/${var.ssh_user}/subql/subql_stack.sh" + destination = "/home/${var.ssh_user}/subql/install_subql_stack.sh" } # copy docker install file @@ -107,7 +107,7 @@ resource "null_resource" "setup-green-subql-nodes" { # copy subql launch script provisioner "file" { source = "${var.path_to_scripts}/install_subql_stack.sh" - destination = "/home/${var.ssh_user}/subql/subql_stack.sh" + destination = "/home/${var.ssh_user}/subql/install_subql_stack.sh" } # copy docker install file @@ -152,16 +152,19 @@ resource "null_resource" "start-blue-subql-nodes" { # set hostname "sudo hostnamectl set-hostname subql-${var.blue-subql-node-config.deployment-color}-${var.blue-subql-node-config.network-name}", + # run subql install script + "chmod +x /home/${var.ssh_user}/subql/install_subql_stack.sh", + "bash /home/${var.ssh_user}/subql/install_subql_stack.sh", + "echo 'Installation Complete'", + # create .env file - "echo NR_API_KEY=${var.nr_api_key} >> /home/${var.ssh_user}/subql/.env", - "echo DOCKER_TAG=${var.blue-subql-node-config.docker-tag} >> /home/${var.ssh_user}/subql/.env", - "echo POSTGRES_PASSWORD=${var.postgres_password} >> /home/${var.ssh_user}/subql/.env", - "echo HASURA_GRAPHQL_ADMIN_SECRET=${var.hasura_graphql_admin_secret} >> /home/${var.ssh_user}/subql/.env", - "echo HASURA_GRAPHQL_JWT_SECRET=${var.hasura_graphql_jwt_secret} >> /home/${var.ssh_user}/subql/.env", - - # run subql lauch script - "chmod +x /home/${var.ssh_user}/subql/subql_stack.sh", - "bash /home/${var.ssh_user}/subql/subql_stack.sh", + "grep -q '^NR_AGENT_IDENTIFIER=' /home/${var.ssh_user}/astral/.env && sed -i 's/^NR_AGENT_IDENTIFIER=.*/NR_AGENT_IDENTIFIER=subql-${var.blue-subql-node-config.deployment-color}-${var.blue-subql-node-config.network-name}/' /home/${var.ssh_user}/astral/.env || echo NR_AGENT_IDENTIFIER=subql-${var.blue-subql-node-config.deployment-color}-${var.blue-subql-node-config.network-name} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^NR_API_KEY=' /home/${var.ssh_user}/astral/.env && sed -i 's/^NR_API_KEY=.*/NR_API_KEY=${var.nr_api_key}/' /home/${var.ssh_user}/astral/.env || echo NR_API_KEY=${var.nr_api_key} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^DOCKER_TAG=' /home/${var.ssh_user}/astral/.env && sed -i 's/^DOCKER_TAG=.*/DOCKER_TAG=${var.blue-subql-node-config.docker-tag}/' /home/${var.ssh_user}/astral/.env || echo DOCKER_TAG=${var.blue-subql-node-config.docker-tag} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^DB_PASSWORD=' /home/${var.ssh_user}/astral/.env && sed -i 's/^DB_PASSWORD=.*/DB_PASSWORD=${var.postgres_password}/' /home/${var.ssh_user}/astral/.env || echo DB_PASSWORD=${var.postgres_password} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^HASURA_GRAPHQL_ADMIN_SECRET=' /home/${var.ssh_user}/astral/.env && sed -i 's/^HASURA_GRAPHQL_ADMIN_SECRET=.*/HASURA_GRAPHQL_ADMIN_SECRET=${var.hasura_graphql_admin_secret}/' /home/${var.ssh_user}/astral/.env || echo HASURA_GRAPHQL_ADMIN_SECRET=${var.hasura_graphql_admin_secret} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^HASURA_GRAPHQL_JWT_SECRET=' /home/${var.ssh_user}/astral/.env && sed -i 's|^HASURA_GRAPHQL_JWT_SECRET=.*|HASURA_GRAPHQL_JWT_SECRET={\"type\":\"HS256\",\"key\":\"${var.hasura_graphql_jwt_secret}\"}|' /home/${var.ssh_user}/astral/.env || echo HASURA_GRAPHQL_JWT_SECRET={\"type\":\"HS256\",\"key\":\"${var.hasura_graphql_jwt_secret}\"} >> /home/${var.ssh_user}/astral/.env", + "echo 'Installation Complete'", ] } @@ -211,15 +214,19 @@ resource "null_resource" "start-green-subql-nodes" { # set hostname "sudo hostnamectl set-hostname subql-${var.green-subql-node-config.deployment-color}-${var.green-subql-node-config.network-name}", + # run subql install script + "chmod +x /home/${var.ssh_user}/subql/install_subql_stack.sh", + "bash /home/${var.ssh_user}/subql/install_subql_stack.sh", + "echo 'Installation Complete'", + # create .env file - "echo NR_API_KEY=${var.nr_api_key} >> /home/${var.ssh_user}/subql/.env", - "echo DOCKER_TAG=${var.blue-subql-node-config.docker-tag} >> /home/${var.ssh_user}/subql/.env", - "echo POSTGRES_PASSWORD=${var.postgres_password} >> /home/${var.ssh_user}/subql/.env", - "echo HASURA_GRAPHQL_ADMIN_SECRET=${var.hasura_graphql_admin_secret} >> /home/${var.ssh_user}/subql/.env", - - # run subql lauch script - "chmod +x /home/${var.ssh_user}/subql/subql_stack.sh", - "bash /home/${var.ssh_user}/subql/subql_stack.sh", + "grep -q '^NR_AGENT_IDENTIFIER=' /home/${var.ssh_user}/astral/.env && sed -i 's/^NR_AGENT_IDENTIFIER=.*/NR_AGENT_IDENTIFIER=subql-${var.green-subql-node-config.deployment-color}-${var.green-subql-node-config.network-name}/' /home/${var.ssh_user}/astral/.env || echo NR_AGENT_IDENTIFIER=subql-${var.green-subql-node-config.deployment-color}-${var.green-subql-node-config.network-name} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^NR_API_KEY=' /home/${var.ssh_user}/astral/.env && sed -i 's/^NR_API_KEY=.*/NR_API_KEY=${var.nr_api_key}/' /home/${var.ssh_user}/astral/.env || echo NR_API_KEY=${var.nr_api_key} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^DOCKER_TAG=' /home/${var.ssh_user}/astral/.env && sed -i 's/^DOCKER_TAG=.*/DOCKER_TAG=${var.green-subql-node-config.docker-tag}/' /home/${var.ssh_user}/astral/.env || echo DOCKER_TAG=${var.green-subql-node-config.docker-tag} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^DB_PASSWORD=' /home/${var.ssh_user}/astral/.env && sed -i 's/^DB_PASSWORD=.*/DB_PASSWORD=${var.postgres_password}/' /home/${var.ssh_user}/astral/.env || echo DB_PASSWORD=${var.postgres_password} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^HASURA_GRAPHQL_ADMIN_SECRET=' /home/${var.ssh_user}/astral/.env && sed -i 's/^HASURA_GRAPHQL_ADMIN_SECRET=.*/HASURA_GRAPHQL_ADMIN_SECRET=${var.hasura_graphql_admin_secret}/' /home/${var.ssh_user}/astral/.env || echo HASURA_GRAPHQL_ADMIN_SECRET=${var.hasura_graphql_admin_secret} >> /home/${var.ssh_user}/astral/.env", + "grep -q '^HASURA_GRAPHQL_JWT_SECRET=' /home/${var.ssh_user}/astral/.env && sed -i 's|^HASURA_GRAPHQL_JWT_SECRET=.*|HASURA_GRAPHQL_JWT_SECRET={\"type\":\"HS256\",\"key\":\"${var.hasura_graphql_jwt_secret}\"}|' /home/${var.ssh_user}/astral/.env || echo HASURA_GRAPHQL_JWT_SECRET={\"type\":\"HS256\",\"key\":\"${var.hasura_graphql_jwt_secret}\"} >> /home/${var.ssh_user}/astral/.env", + "echo 'Installation Complete'", ] } diff --git a/templates/terraform/subql/base/scripts/install_subql_stack.sh b/templates/terraform/subql/base/scripts/install_subql_stack.sh index 15dbd05..c4b4a0f 100644 --- a/templates/terraform/subql/base/scripts/install_subql_stack.sh +++ b/templates/terraform/subql/base/scripts/install_subql_stack.sh @@ -1,5 +1,8 @@ #!/bin/bash +# Exit on any error +set -e + # Check if 'tmux' is installed if ! command -v tmux &> /dev/null; then echo "'tmux' is required but not installed. Please install 'tmux' and try again." @@ -11,46 +14,55 @@ if ! command -v git &> /dev/null; then echo "'git' is required but not installed. Please install 'git' and try again." exit 1 fi +# Install nvm (Node Version Manager) +export NVM_DIR="$HOME/.nvm" +if [ ! -d "$NVM_DIR" ]; then + git clone https://github.com/nvm-sh/nvm.git "$NVM_DIR" + cd "$NVM_DIR" && git checkout `git describe --abbrev=0 --tags` + cd - +fi -# Install Node.js latest LTS version -## Fetch the latest LTS version of Node.js using NodeSource -curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - - -# Install Node.js -sudo DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs +# Load nvm +export NVM_DIR="$HOME/.nvm" +[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" -# Verify installation -node -v -npm -v +# Add nvm to .bashrc for future sessions +if ! grep -q 'NVM_DIR' "$HOME/.bashrc"; then + echo 'export NVM_DIR="$HOME/.nvm"' >> "$HOME/.bashrc" + echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> "$HOME/.bashrc" +fi -# clone the astral repository from Github -git clone https://github.com/autonomys/astral.git -cd astral +# Source .bashrc to ensure nvm is available in the current session +source "$HOME/.bashrc" -# # Function to run a command in a new tmux session -# run_in_tmux_session() { -# local session_name="$1" -# local cmd="$2" +# Install the latest LTS version of Node.js using nvm +nvm install --lts +nvm use --lts -# tmux new-session -d -s "$session_name" "$cmd" -# } +# Add Node.js and npm to PATH and update .bashrc +export PATH="$NVM_DIR/versions/node/$(nvm version)/bin:$PATH" +if ! grep -q 'NVM_DIR/versions/node' "$HOME/.bashrc"; then + echo 'export PATH="$NVM_DIR/versions/node/$(nvm version)/bin:$PATH"' >> "$HOME/.bashrc" +fi -# # Start indexers stack -# echo "Starting indexers stack..." -# cd indexers || exit 1 -# yarn || exit 1 -# run_in_tmux_session "indexers_dev" "yarn prod" || exit 1 +# Check if 'yarn' is installed +if ! command -v yarn &> /dev/null; then + echo "'yarn' is not installed. Installing 'yarn'..." + npm install --global yarn +fi -# # Wait for indexers to sync (adjust sleep duration as needed) -# echo "Waiting for indexers to sync blocks... (sleeping for 30 seconds)" -# sleep 30 +# Add yarn to PATH and update .bashrc +export PATH="$(yarn global bin):$PATH" +if ! grep -q 'yarn global bin' "$HOME/.bashrc"; then + echo 'export PATH="$(yarn global bin):$PATH"' >> "$HOME/.bashrc" +fi -# # Run metadata in a new session -# echo "Running yarn metadata in a new session..." -# run_in_tmux_session "indexers_metadata" "cd indexers && yarn metadata && yarn migrate --database-name taurus" || exit 1 +source "$HOME/.bashrc" -# # Start Hasura console in a new session -# echo "Starting Hasura console in a new session..." -# run_in_tmux_session "hasura_console" "cd indexers && yarn console" || exit 1 +# Verify installation +node -v +npm -v +yarn -v -exit 0 +# clone the astral repository from Github +git clone https://github.com/autonomys/astral.git