Skip to main content

bro

#!/usr/bin/env bash

# -----------------------------------------------------------------------------
# Configuration
# -----------------------------------------------------------------------------
# Username and host/IP for the jump (bastion) server
JUMP_USER="jumpuser"
JUMP_HOST="jump.example.com"

# Username and host/IP for the final remote server
REMOTE_USER="remoteuser"
REMOTE_HOST="192.168.1.100"
REMOTE_DIR="/path/on/remote/server/myproject"

# Which Python do we use on the remote? e.g. python3.9
REMOTE_PYTHON="python3.9"

# -----------------------------------------------------------------------------
# 1) Prepare offline wheelhouse locally
# -----------------------------------------------------------------------------
echo "[Local] Creating/updating offline wheelhouse..."
pip freeze > requirements.txt
mkdir -p packages
pip download -r requirements.txt -d packages

# -----------------------------------------------------------------------------
# 2) Package the project for deployment
# -----------------------------------------------------------------------------
echo "[Local] Packaging project..."
tar czf myproject.tar.gz \
    --exclude=".venv" \
    --exclude="myproject.tar.gz" \
    --exclude="deploy.sh" \
    .  # everything else in the current directory

# -----------------------------------------------------------------------------
# 3) Secure copy the tarball to remote (via jump server)
# -----------------------------------------------------------------------------
echo "[Local] Copying archive to remote server via jump..."
scp -o ProxyJump="${JUMP_USER}@${JUMP_HOST}" \
    myproject.tar.gz \
    "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}"

# -----------------------------------------------------------------------------
# 4) SSH into the remote server (via jump), unpack, create fresh venv, install
# -----------------------------------------------------------------------------
echo "[Local] Deploying on remote server..."
ssh -J "${JUMP_USER}@${JUMP_HOST}" "${REMOTE_USER}@${REMOTE_HOST}" << EOF
    set -e  # Exit on any error

    echo "[Remote] Entering \$HOME / \$(pwd)."
    cd "${REMOTE_DIR}"
    
    echo "[Remote] Extracting myproject.tar.gz..."
    tar xzf myproject.tar.gz

    echo "[Remote] Creating fresh virtual environment..."
    ${REMOTE_PYTHON} -m venv .venv

    echo "[Remote] Activating virtual environment and installing packages..."
    source .venv/bin/activate
    pip install --upgrade pip
    pip install --no-index --find-links=packages -r requirements.txt

    echo "[Remote] Deployment complete!"
EOF

echo "[Local] Done!"