Skip to content

Commit

Permalink
improve the subql stack launch script
Browse files Browse the repository at this point in the history
fix variables

remove run script since its not stable

add env variables
  • Loading branch information
DaMandal0rian committed Nov 1, 2024
1 parent 5ace78a commit f9c0227
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 72 deletions.
47 changes: 27 additions & 20 deletions templates/terraform/subql/base/bootstrap_nova_subql_provisioner.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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'",
]
}
Expand Down Expand Up @@ -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'",
]
}
Expand Down
45 changes: 26 additions & 19 deletions templates/terraform/subql/base/bootstrap_subql_provisioner.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'",
]
}
Expand Down Expand Up @@ -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'",
]
}
Expand Down
78 changes: 45 additions & 33 deletions templates/terraform/subql/base/scripts/install_subql_stack.sh
Original file line number Diff line number Diff line change
@@ -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."
Expand All @@ -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

0 comments on commit f9c0227

Please sign in to comment.