240 lines
7.7 KiB
Bash
240 lines
7.7 KiB
Bash
#!/bin/bash
|
|
|
|
# Exit on error
|
|
set -e
|
|
|
|
echo "===== Project Mycelium Development Deployment ====="
|
|
echo "Environment: dev.threefold.pro"
|
|
echo "Date: $(date)"
|
|
echo "User: $(whoami)"
|
|
|
|
# Load deployment credentials for git authentication
|
|
DEPLOY_ENV="/tmp/tf-marketplace-deploy.env"
|
|
if [ -f "$DEPLOY_ENV" ]; then
|
|
echo "Loading deployment credentials from $DEPLOY_ENV"
|
|
source "$DEPLOY_ENV"
|
|
else
|
|
echo "Warning: Deployment credentials not found at $DEPLOY_ENV"
|
|
echo "Please run 'make deploy-setup' to copy deployment credentials"
|
|
fi
|
|
|
|
# Variables
|
|
BASE_DIR="/root/code/git.ourworld.tf/tfgrid_research"
|
|
INSTALL_DIR="$BASE_DIR/dev/projectmycelium"
|
|
BRANCH="development"
|
|
PORT=9998
|
|
DOMAIN="dev.threefold.pro"
|
|
|
|
# Construct authenticated Git URL if credentials are available
|
|
if [ -n "$GITEA_USER" ] && [ -n "$GITEA_TOKEN" ]; then
|
|
REPO_URL="https://${GITEA_USER}:${GITEA_TOKEN}@git.ourworld.tf/tfgrid_research/projectmycelium.git"
|
|
echo "Using authenticated Git access"
|
|
else
|
|
REPO_URL="https://git.ourworld.tf/tfgrid_research/projectmycelium.git"
|
|
echo "Warning: No Gitea credentials found, using unauthenticated access"
|
|
fi
|
|
|
|
echo "===== Setting up directory structure ====="
|
|
# Create base directory if it doesn't exist
|
|
mkdir -p "$BASE_DIR"
|
|
cd "$BASE_DIR"
|
|
|
|
echo "===== Repository management ====="
|
|
if [ -d "$INSTALL_DIR" ]; then
|
|
echo "Directory exists. Checking if it's a git repository..."
|
|
cd "$INSTALL_DIR"
|
|
|
|
# Check if it's a git repository
|
|
if [ -d ".git" ]; then
|
|
echo "Valid git repository found. Updating..."
|
|
# Clean up git state
|
|
if [ -f ".git/index.lock" ]; then
|
|
echo "Removing git lock file..."
|
|
rm -f ".git/index.lock"
|
|
fi
|
|
|
|
# Clean any uncommitted changes
|
|
git reset --hard HEAD 2>/dev/null || true
|
|
git clean -fd 2>/dev/null || true
|
|
|
|
# Update remote URL with authentication if available
|
|
if [ -n "$GITEA_USER" ] && [ -n "$GITEA_TOKEN" ]; then
|
|
git remote set-url origin "$REPO_URL"
|
|
fi
|
|
|
|
git fetch origin
|
|
git checkout "$BRANCH"
|
|
git reset --hard "origin/$BRANCH"
|
|
echo "Repository updated to latest $BRANCH branch"
|
|
else
|
|
echo "Directory exists but is not a git repository. Removing and cloning fresh..."
|
|
cd "$BASE_DIR"
|
|
rm -rf "$INSTALL_DIR"
|
|
echo "Cloning repository..."
|
|
git clone "$REPO_URL" "$INSTALL_DIR"
|
|
cd "$INSTALL_DIR"
|
|
git checkout "$BRANCH"
|
|
echo "Repository cloned and checked out to $BRANCH branch"
|
|
fi
|
|
else
|
|
echo "Cloning repository..."
|
|
git clone "$REPO_URL" "$INSTALL_DIR"
|
|
cd "$INSTALL_DIR"
|
|
git checkout "$BRANCH"
|
|
echo "Repository cloned and checked out to $BRANCH branch"
|
|
fi
|
|
|
|
echo "===== Cleaning build cache ====="
|
|
# Clean cargo cache to ensure fresh build
|
|
echo "Cleaning cargo build cache..."
|
|
"$CARGO_CMD" clean 2>/dev/null || true
|
|
|
|
echo "===== Verifying environment ====="
|
|
echo "Current PATH: $PATH"
|
|
echo "Current USER: $(whoami)"
|
|
echo "Current HOME: $HOME"
|
|
|
|
# Find cargo using multiple methods with robust detection
|
|
CARGO_CMD=""
|
|
|
|
# Method 1: Check if cargo is in PATH
|
|
echo "Checking for cargo in PATH..."
|
|
if command -v cargo &> /dev/null; then
|
|
CARGO_CMD="cargo"
|
|
echo "✓ Found cargo in PATH: $(which cargo)"
|
|
# Method 2: Check common Rust installation locations
|
|
elif [ -f "$HOME/.cargo/bin/cargo" ]; then
|
|
CARGO_CMD="$HOME/.cargo/bin/cargo"
|
|
echo "✓ Found cargo at $HOME/.cargo/bin/cargo"
|
|
# Add to PATH for this session
|
|
export PATH="$HOME/.cargo/bin:$PATH"
|
|
# Method 3: Check root cargo location
|
|
elif [ -f "/root/.cargo/bin/cargo" ]; then
|
|
CARGO_CMD="/root/.cargo/bin/cargo"
|
|
echo "✓ Found cargo at /root/.cargo/bin/cargo"
|
|
export PATH="/root/.cargo/bin:$PATH"
|
|
# Method 4: Check system-wide installation
|
|
elif [ -f "/usr/local/bin/cargo" ]; then
|
|
CARGO_CMD="/usr/local/bin/cargo"
|
|
echo "✓ Found cargo at /usr/local/bin/cargo"
|
|
elif [ -f "/usr/bin/cargo" ]; then
|
|
CARGO_CMD="/usr/bin/cargo"
|
|
echo "✓ Found cargo at /usr/bin/cargo"
|
|
# Method 5: Use whereis to find cargo
|
|
else
|
|
echo "Searching for cargo with whereis..."
|
|
CARGO_PATHS=$(whereis cargo 2>/dev/null | cut -d' ' -f2-)
|
|
echo "whereis found: $CARGO_PATHS"
|
|
|
|
for path in $CARGO_PATHS; do
|
|
if [ -f "$path" ] && [ -x "$path" ]; then
|
|
CARGO_CMD="$path"
|
|
echo "✓ Found executable cargo at $path"
|
|
break
|
|
fi
|
|
done
|
|
|
|
# Method 6: Last resort - try to find cargo anywhere
|
|
if [ -z "$CARGO_CMD" ]; then
|
|
echo "Searching for cargo with find..."
|
|
FOUND_CARGO=$(find /usr /root /home -name "cargo" -type f -executable 2>/dev/null | head -1)
|
|
if [ -n "$FOUND_CARGO" ] && [ -f "$FOUND_CARGO" ]; then
|
|
CARGO_CMD="$FOUND_CARGO"
|
|
echo "✓ Found cargo at $FOUND_CARGO"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
# Final verification
|
|
if [ -n "$CARGO_CMD" ]; then
|
|
echo "Using cargo command: $CARGO_CMD"
|
|
echo "Testing cargo command..."
|
|
if "$CARGO_CMD" --version &> /dev/null; then
|
|
echo "✓ Cargo is working: $($CARGO_CMD --version)"
|
|
else
|
|
echo "✗ Cargo command failed to execute"
|
|
CARGO_CMD=""
|
|
fi
|
|
fi
|
|
|
|
# If still no cargo found, try installation
|
|
if [ -z "$CARGO_CMD" ]; then
|
|
echo "Cargo not found anywhere. Installing Rust toolchain..."
|
|
|
|
# Install Rust using rustup
|
|
if ! command -v rustup &> /dev/null; then
|
|
echo "Installing rustup..."
|
|
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable
|
|
|
|
# Source the cargo environment
|
|
if [ -f "$HOME/.cargo/env" ]; then
|
|
source "$HOME/.cargo/env"
|
|
fi
|
|
|
|
# Add to PATH
|
|
export PATH="$HOME/.cargo/bin:$PATH"
|
|
fi
|
|
|
|
# Verify installation
|
|
if command -v cargo &> /dev/null; then
|
|
CARGO_CMD="cargo"
|
|
echo "✓ Rust toolchain installed successfully"
|
|
elif [ -f "$HOME/.cargo/bin/cargo" ]; then
|
|
CARGO_CMD="$HOME/.cargo/bin/cargo"
|
|
export PATH="$HOME/.cargo/bin:$PATH"
|
|
echo "✓ Rust toolchain installed successfully"
|
|
else
|
|
echo "✗ Failed to install Rust toolchain"
|
|
echo "Please install Rust manually: https://rustup.rs/"
|
|
echo "Waiting 30 seconds before retrying..."
|
|
sleep 30
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# Check if we're in the right directory
|
|
if [ ! -f "Cargo.toml" ]; then
|
|
echo "Error: Cargo.toml not found. Are we in the right directory?"
|
|
pwd
|
|
exit 1
|
|
fi
|
|
|
|
echo "===== Setting up application environment ====="
|
|
# Generate SECRET_KEY if .env doesn't exist or doesn't have a valid key
|
|
if [ ! -f ".env" ] || ! grep -q "^SECRET_KEY=" .env || grep -q "your_secret_key_here" .env; then
|
|
echo "Generating SECRET_KEY for application..."
|
|
SECRET_KEY=$(openssl rand -base64 64 | tr -d '\n')
|
|
|
|
# Create .env from template if it doesn't exist
|
|
if [ ! -f ".env" ] && [ -f ".env.example" ]; then
|
|
cp .env.example .env
|
|
fi
|
|
|
|
# Update or add SECRET_KEY
|
|
if [ -f ".env" ]; then
|
|
if grep -q "^SECRET_KEY=" .env; then
|
|
sed -i "s/^SECRET_KEY=.*/SECRET_KEY=$SECRET_KEY/" .env
|
|
else
|
|
echo "SECRET_KEY=$SECRET_KEY" >> .env
|
|
fi
|
|
else
|
|
echo "SECRET_KEY=$SECRET_KEY" > .env
|
|
fi
|
|
echo "SECRET_KEY generated and saved to .env"
|
|
else
|
|
echo "Using existing SECRET_KEY from .env"
|
|
fi
|
|
|
|
echo "===== Starting application ====="
|
|
echo "Running Project Mycelium on port $PORT..."
|
|
echo "Working directory: $(pwd)"
|
|
echo "Branch: $(git branch --show-current)"
|
|
echo "Commit: $(git rev-parse --short HEAD)"
|
|
|
|
# Set environment variables
|
|
export RUST_LOG=info
|
|
|
|
# Run the application (following existing pattern)
|
|
git checkout "$BRANCH"
|
|
exec "$CARGO_CMD" run --release --bin projectmycelium -- --port "$PORT"
|