From d64cc64e4ab47cf6ba29886dc55b8b43123d7e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Nowacki?= Date: Wed, 2 Jul 2025 21:28:38 +0200 Subject: [PATCH 1/5] start --- envs/deployed/docker-compose.yml | 5 +++ installer/README.md | 59 ++++++++++++++++++++++++++ installer/install.sh | 73 ++++++++++++++++++++++++++++++++ installer/update_compose.sh | 61 ++++++++++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 envs/deployed/docker-compose.yml create mode 100644 installer/README.md create mode 100755 installer/install.sh create mode 100755 installer/update_compose.sh diff --git a/envs/deployed/docker-compose.yml b/envs/deployed/docker-compose.yml new file mode 100644 index 0000000..14e94d2 --- /dev/null +++ b/envs/deployed/docker-compose.yml @@ -0,0 +1,5 @@ +services: + hello-world: + image: alpine:latest + restart: unless-stopped + command: sh -c "while true; do echo 'hello world'; sleep 1; done" \ No newline at end of file diff --git a/installer/README.md b/installer/README.md new file mode 100644 index 0000000..5e8c6e1 --- /dev/null +++ b/installer/README.md @@ -0,0 +1,59 @@ +# Test Validator Installer + +This directory contains scripts to install and maintain a test validator node. + +## Quick Installation + +You can install the test validator with a single command: + +```bash +curl -s https://raw.githubusercontent.com/backend-developers-ltd/installer-test/refs/heads/deploy-config-prod/installer/install.sh | bash +``` + +This will: +1. Create a working directory at `~/test-validator/` (default) +2. Prompt you for configuration values if needed +3. Set up a cron job to automatically update your validator when changes are published +4. Download and start the validator services using Docker Compose + +## Custom Installation + +If you want to customize the installation, you can pass arguments to the script: + +```bash +curl -s https://raw.githubusercontent.com/backend-developers-ltd/installer-test/refs/heads/deploy-config-prod/installer/install.sh | bash -s -- [ENV_NAME] [WORKING_DIRECTORY] +``` + +Where: +- `ENV_NAME`: The environment to use (defaults to "prod") +- `WORKING_DIRECTORY`: The directory where the validator will be installed (defaults to "~/test-validator/") + +Example with custom working directory: + +```bash +curl -s https://raw.githubusercontent.com/backend-developers-ltd/installer-test/refs/heads/deploy-config-prod/installer/install.sh | bash -s -- prod /opt/test-validator +``` + +## Prerequisites + +- Docker and Docker Compose installed +- cron running +- curl installed +- bash shell +- Internet access to GitHub + +## What the Installer Does + +1. Creates a working directory +2. Sets up a .env file with your configuration +3. Downloads the latest docker-compose.yml file +4. Sets up a cron job to check for updates every 15 minutes +5. Starts the validator services + +## Manual Update + +If you want to manually update your validator, you can run: + +```bash +curl -s https://raw.githubusercontent.com/backend-developers-ltd/installer-test/refs/heads/deploy-config-prod/installer/update_compose.sh | bash -s -- [ENV_NAME] [WORKING_DIRECTORY] +``` diff --git a/installer/install.sh b/installer/install.sh new file mode 100755 index 0000000..ecf7f54 --- /dev/null +++ b/installer/install.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +# Script to install a cron job that fetches and runs the updater script + +set -euo pipefail + +# Default values for arguments +ENV_NAME="${1:-prod}" +WORKING_DIRECTORY="${2:-~/test-validator/}" + +# Ensure the working directory exists +mkdir -p "${WORKING_DIRECTORY}" + +# Check if .env file exists in the working directory +ENV_FILE="${WORKING_DIRECTORY}/.env" +if [ ! -f "${ENV_FILE}" ]; then + echo "Creating .env file..." + + # Prompt for user inputs with default values + read -p "Enter BITTENSOR_NETUID [9999]: " BITTENSOR_NETUID + BITTENSOR_NETUID=${BITTENSOR_NETUID:-9999} + + read -p "Enter BITTENSOR_NETWORK [finney]: " BITTENSOR_NETWORK + BITTENSOR_NETWORK=${BITTENSOR_NETWORK:-finney} + + read -p "Enter HOST_WALLET_DIR [~/.bittensor/wallets]: " HOST_WALLET_DIR + HOST_WALLET_DIR=${HOST_WALLET_DIR:-~/.bittensor/wallets} + + read -p "Enter BITTENSOR_WALLET_NAME [validator]: " BITTENSOR_WALLET_NAME + BITTENSOR_WALLET_NAME=${BITTENSOR_WALLET_NAME:-validator} + + read -p "Enter BITTENSOR_WALLET_HOTKEY_NAME [default]: " BITTENSOR_WALLET_HOTKEY_NAME + BITTENSOR_WALLET_HOTKEY_NAME=${BITTENSOR_WALLET_HOTKEY_NAME:-default} + + # Generate a random string for SECRET_KEY + SECRET_KEY=$(openssl rand -base64 64) + + # Create the .env file + cat > "${ENV_FILE}" << EOL +BITTENSOR_NETUID=${BITTENSOR_NETUID} +BITTENSOR_NETWORK=${BITTENSOR_NETWORK} +HOST_WALLET_DIR=${HOST_WALLET_DIR} +BITTENSOR_WALLET_NAME=${BITTENSOR_WALLET_NAME} +BITTENSOR_WALLET_HOTKEY_NAME=${BITTENSOR_WALLET_HOTKEY_NAME} +POSTGRES_PASSWORD=123456789 +SECRET_KEY=${SECRET_KEY} +EOL + + echo ".env file created successfully." +fi + +# Hardcoded GitHub raw URL +GITHUB_URL="https://raw.githubusercontent.com/backend-developers-ltd/installer-test/refs/heads" + +# Run update_compose.sh once to ensure it works +echo "Running update_compose.sh once to ensure it works..." +curl -s "${GITHUB_URL}/deploy-config-${ENV_NAME}/installer/update_compose.sh" > /tmp/update_compose.sh +chmod +x /tmp/update_compose.sh +if ! /tmp/update_compose.sh "${ENV_NAME}" "${WORKING_DIRECTORY}"; then + echo "Error: update_compose.sh failed. Not adding cronline." + exit 1 +fi +echo "update_compose.sh ran successfully." + +# Create the cron job command with a unique identifier comment +# This will fetch the updater script from GitHub and run it with the provided arguments +CRON_CMD="*/15 * * * * cd ${WORKING_DIRECTORY} && curl -s ${GITHUB_URL}/deploy-config-${ENV_NAME}/installer/update_compose.sh > /tmp/update_compose.sh && chmod +x /tmp/update_compose.sh && /tmp/update_compose.sh ${ENV_NAME} ${WORKING_DIRECTORY} # TEST_VALIDATOR_UPDATE" + +# Install the cron job +(crontab -l 2>/dev/null || echo "") | grep -v "TEST_VALIDATOR_UPDATE" | { cat; echo "${CRON_CMD}"; } | crontab - + +echo "Cron job installed successfully. It will run every 15 minutes." +echo "Environment: ${ENV_NAME}" +echo "Working directory: ${WORKING_DIRECTORY}" diff --git a/installer/update_compose.sh b/installer/update_compose.sh new file mode 100755 index 0000000..23119b8 --- /dev/null +++ b/installer/update_compose.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# Script to update docker-compose.yml and restart services if needed + +set -euo pipefail + +# Default values for arguments +ENV_NAME="${1:-prod}" +WORKING_DIRECTORY="${2:-~/test-validator/}" + +# Ensure we're in the working directory +cd "${WORKING_DIRECTORY}" + +# Hardcoded GitHub raw URL +GITHUB_URL="https://raw.githubusercontent.com/backend-developers-ltd/installer-test/refs/heads" + +# Use a fixed temporary file for the remote docker-compose.yml +TEMP_FILE="/tmp/test_compose_update.yml" +curl -s "${GITHUB_URL}/deploy-config-${ENV_NAME}/envs/deployed/docker-compose.yml" > "${TEMP_FILE}" + +# Path to the local docker-compose.yml file +LOCAL_FILE="${WORKING_DIRECTORY}/docker-compose.yml" + +# Check if the local file exists +if [ ! -f "${LOCAL_FILE}" ]; then + echo "Local docker-compose.yml does not exist. Creating it." + cat "${TEMP_FILE}" > "${LOCAL_FILE}" + UPDATED=true +else + # Compare the files + if diff -q "${TEMP_FILE}" "${LOCAL_FILE}" > /dev/null; then + echo "No changes detected in docker-compose.yml" + UPDATED=false + else + echo "Changes detected in docker-compose.yml. Updating..." + cat "${TEMP_FILE}" > "${LOCAL_FILE}" + UPDATED=true + fi +fi + +# We're using a fixed temporary file, so we don't need to clean it up + +# If the file was updated, restart the services +if [ "${UPDATED}" = true ]; then + echo "Updating services..." + + # Check if docker compose or docker-compose should be used + if command -v docker &> /dev/null && docker compose version &> /dev/null; then + # Docker Compose V2 (docker compose) + docker compose up -d --remove-orphans + elif command -v docker-compose &> /dev/null; then + # Docker Compose V1 (docker-compose) + docker-compose up -d --remove-orphans + else + echo "Error: Neither docker compose nor docker-compose is available." + exit 1 + fi + + echo "Services updated successfully." +fi + +echo "Update process completed." From aafa257db4886d09158925fb7192bc09cad3a648 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Nowacki?= Date: Fri, 4 Jul 2025 13:03:35 +0200 Subject: [PATCH 2/5] fixes --- envs/deployed/docker-compose.yml | 4 +++- installer/update_compose.sh | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/envs/deployed/docker-compose.yml b/envs/deployed/docker-compose.yml index 14e94d2..d31a938 100644 --- a/envs/deployed/docker-compose.yml +++ b/envs/deployed/docker-compose.yml @@ -2,4 +2,6 @@ services: hello-world: image: alpine:latest restart: unless-stopped - command: sh -c "while true; do echo 'hello world'; sleep 1; done" \ No newline at end of file + command: sh -c "while true; do echo 'hello world'; sleep 1; done" + env_file: + - .env \ No newline at end of file diff --git a/installer/update_compose.sh b/installer/update_compose.sh index 23119b8..904bd26 100755 --- a/installer/update_compose.sh +++ b/installer/update_compose.sh @@ -7,6 +7,8 @@ set -euo pipefail ENV_NAME="${1:-prod}" WORKING_DIRECTORY="${2:-~/test-validator/}" +mkdir -p "${WORKING_DIRECTORY}" + # Ensure we're in the working directory cd "${WORKING_DIRECTORY}" From 012e9a8c5fa55c7ce0baa0ba03840039d3aa4f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Nowacki?= Date: Fri, 4 Jul 2025 13:05:46 +0200 Subject: [PATCH 3/5] fixes --- installer/update_compose.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/installer/update_compose.sh b/installer/update_compose.sh index 904bd26..023e4ec 100755 --- a/installer/update_compose.sh +++ b/installer/update_compose.sh @@ -2,6 +2,7 @@ # Script to update docker-compose.yml and restart services if needed set -euo pipefail +set -x # Default values for arguments ENV_NAME="${1:-prod}" From 6787287d88fcadcbebc620a68265a06f142d0d12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Nowacki?= Date: Fri, 4 Jul 2025 13:18:29 +0200 Subject: [PATCH 4/5] fixes --- envs/deployed/docker-compose.yml | 6 ++++++ installer/install.sh | 14 +++++++------- installer/update_compose.sh | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/envs/deployed/docker-compose.yml b/envs/deployed/docker-compose.yml index d31a938..0153614 100644 --- a/envs/deployed/docker-compose.yml +++ b/envs/deployed/docker-compose.yml @@ -3,5 +3,11 @@ services: image: alpine:latest restart: unless-stopped command: sh -c "while true; do echo 'hello world'; sleep 1; done" + env_file: + - .env + hello-world2: + image: alpine:latest + restart: unless-stopped + command: sh -c "while true; do echo 'hello world2'; sleep 1; done" env_file: - .env \ No newline at end of file diff --git a/installer/install.sh b/installer/install.sh index ecf7f54..fcff7a1 100755 --- a/installer/install.sh +++ b/installer/install.sh @@ -5,7 +5,7 @@ set -euo pipefail # Default values for arguments ENV_NAME="${1:-prod}" -WORKING_DIRECTORY="${2:-~/test-validator/}" +WORKING_DIRECTORY="${2:-$HOME/test-validator/}" # Ensure the working directory exists mkdir -p "${WORKING_DIRECTORY}" @@ -16,23 +16,23 @@ if [ ! -f "${ENV_FILE}" ]; then echo "Creating .env file..." # Prompt for user inputs with default values - read -p "Enter BITTENSOR_NETUID [9999]: " BITTENSOR_NETUID + read -p "Enter BITTENSOR_NETUID [9999]: " BITTENSOR_NETUID "${ENV_FILE}" << EOL diff --git a/installer/update_compose.sh b/installer/update_compose.sh index 023e4ec..3bee094 100755 --- a/installer/update_compose.sh +++ b/installer/update_compose.sh @@ -6,7 +6,7 @@ set -x # Default values for arguments ENV_NAME="${1:-prod}" -WORKING_DIRECTORY="${2:-~/test-validator/}" +WORKING_DIRECTORY="${2:-$HOME/test-validator/}" mkdir -p "${WORKING_DIRECTORY}" From 888128cbca28e23c87e14c7530faa7ccc48c04be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Nowacki?= Date: Sat, 5 Jul 2025 13:41:42 +0200 Subject: [PATCH 5/5] last fix --- installer/install.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/installer/install.sh b/installer/install.sh index fcff7a1..3116660 100755 --- a/installer/install.sh +++ b/installer/install.sh @@ -10,6 +10,8 @@ WORKING_DIRECTORY="${2:-$HOME/test-validator/}" # Ensure the working directory exists mkdir -p "${WORKING_DIRECTORY}" +WORKING_DIRECTORY=$(realpath "${WORKING_DIRECTORY}") + # Check if .env file exists in the working directory ENV_FILE="${WORKING_DIRECTORY}/.env" if [ ! -f "${ENV_FILE}" ]; then