From 9c99fa4d17f4db21014adb32a91f721a5f08375d Mon Sep 17 00:00:00 2001 From: boilerrat <128boilerrat@gmail.com> Date: Sun, 16 Mar 2025 14:26:25 -0400 Subject: [PATCH] Add password automation with sshpass and environment variables --- .env.example | 4 ++++ deploy-website.sh | 31 +++++++++++++++++++++++-------- install-sshpass.sh | 33 +++++++++++++++++++++++++++++++++ setup-vps.sh | 21 +++++++++++++++++++++ update-nginx-config.sh | 41 +++++++++++++++++++++++++++++------------ 5 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 .env.example create mode 100755 install-sshpass.sh diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..c295e42 --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +# VPS Configuration +SERVER_IP="your_server_ip" +SERVER_USER="root" +SERVER_PASSWORD="your_server_password" \ No newline at end of file diff --git a/deploy-website.sh b/deploy-website.sh index 6d8894f..900547e 100755 --- a/deploy-website.sh +++ b/deploy-website.sh @@ -2,10 +2,25 @@ # This script deploys the website to the VPS +# Load environment variables +if [ -f .env ]; then + source .env +else + echo "Error: .env file not found. Please create it based on .env.example." + exit 1 +fi + +# Check if sshpass is installed +if ! command -v sshpass &> /dev/null; then + echo "Error: sshpass is not installed. Please run ./install-sshpass.sh first." + exit 1 +fi + +# Set SSH and SCP commands with password +SSH_CMD="sshpass -p \"$SERVER_PASSWORD\" ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP" +SCP_CMD="sshpass -p \"$SERVER_PASSWORD\" scp -o StrictHostKeyChecking=no" + # Set variables -SERVER_IP="66.179.188.130" -REMOTE_USER="root" -REMOTE_HOST=$SERVER_IP REMOTE_DIR="/var/www/boilerhaus.org" LOCAL_DIR="." @@ -23,14 +38,14 @@ tar -czf website.tar.gz -C ./deploy-temp . # Upload the tarball to the server echo "Uploading to server..." -scp website.tar.gz $REMOTE_USER@$REMOTE_HOST:/tmp/ +eval "$SCP_CMD website.tar.gz $SERVER_USER@$SERVER_IP:/tmp/" # Extract the tarball on the server echo "Extracting files on server..." -ssh $REMOTE_USER@$REMOTE_HOST "mkdir -p $REMOTE_DIR && \ - tar -xzf /tmp/website.tar.gz -C $REMOTE_DIR && \ - chown -R www-data:www-data $REMOTE_DIR && \ - rm /tmp/website.tar.gz" +eval "$SSH_CMD \"mkdir -p $REMOTE_DIR && \ + tar -xzf /tmp/website.tar.gz -C $REMOTE_DIR && \ + chown -R www-data:www-data $REMOTE_DIR && \ + rm /tmp/website.tar.gz\"" # Clean up local files echo "Cleaning up..." diff --git a/install-sshpass.sh b/install-sshpass.sh new file mode 100755 index 0000000..62a341a --- /dev/null +++ b/install-sshpass.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# This script installs sshpass, which is needed for password automation + +# Check if sshpass is already installed +if command -v sshpass &> /dev/null; then + echo "sshpass is already installed." + exit 0 +fi + +# Install sshpass based on the detected package manager +if command -v apt-get &> /dev/null; then + echo "Installing sshpass using apt..." + sudo apt-get update + sudo apt-get install -y sshpass +elif command -v dnf &> /dev/null; then + echo "Installing sshpass using dnf..." + sudo dnf install -y sshpass +elif command -v yum &> /dev/null; then + echo "Installing sshpass using yum..." + sudo yum install -y sshpass +elif command -v pacman &> /dev/null; then + echo "Installing sshpass using pacman..." + sudo pacman -S --noconfirm sshpass +elif command -v brew &> /dev/null; then + echo "Installing sshpass using Homebrew..." + brew install hudochenkov/sshpass/sshpass +else + echo "Error: Could not detect package manager. Please install sshpass manually." + exit 1 +fi + +echo "sshpass has been installed successfully." \ No newline at end of file diff --git a/setup-vps.sh b/setup-vps.sh index 806e571..1d42673 100755 --- a/setup-vps.sh +++ b/setup-vps.sh @@ -2,6 +2,27 @@ # This script sets up the VPS with the correct Nginx configuration and deploys the website +# Check if .env file exists +if [ ! -f .env ]; then + echo "Error: .env file not found. Please create it based on .env.example." + exit 1 +fi + +# Source the .env file +source .env + +# Check if sshpass is installed +if ! command -v sshpass &> /dev/null; then + echo "sshpass is not installed. Installing it now..." + ./install-sshpass.sh + + # Check if installation was successful + if ! command -v sshpass &> /dev/null; then + echo "Error: Failed to install sshpass. Please install it manually." + exit 1 + fi +fi + # Set variables SERVER_IP="66.179.188.130" diff --git a/update-nginx-config.sh b/update-nginx-config.sh index 78a25c7..7a10c0f 100755 --- a/update-nginx-config.sh +++ b/update-nginx-config.sh @@ -2,13 +2,27 @@ # This script updates the Nginx configuration for boilerhaus.org and its subdomains -# Set variables -SERVER_IP="66.179.188.130" -SERVER_USER="root" # Using root as requested +# Load environment variables +if [ -f .env ]; then + source .env +else + echo "Error: .env file not found. Please create it based on .env.example." + exit 1 +fi + +# Check if sshpass is installed +if ! command -v sshpass &> /dev/null; then + echo "Error: sshpass is not installed. Please run ./install-sshpass.sh first." + exit 1 +fi + +# Set SSH and SCP commands with password +SSH_CMD="sshpass -p \"$SERVER_PASSWORD\" ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP" +SCP_CMD="sshpass -p \"$SERVER_PASSWORD\" scp -o StrictHostKeyChecking=no" # Create backup of existing configuration echo "Creating backup of existing Nginx configuration..." -ssh $SERVER_USER@$SERVER_IP "mkdir -p /etc/nginx/sites-available && cp -f /etc/nginx/sites-available/boilerhaus.org.conf /etc/nginx/sites-available/boilerhaus.org.conf.bak 2>/dev/null || true" +eval "$SSH_CMD \"mkdir -p /etc/nginx/sites-available && cp -f /etc/nginx/sites-available/boilerhaus.org.conf /etc/nginx/sites-available/boilerhaus.org.conf.bak 2>/dev/null || true\"" # Create updated configuration file cat > boilerhaus.org.conf.new << 'EOL' @@ -91,30 +105,33 @@ EOL # Upload the new configuration echo "Uploading new configuration..." -scp boilerhaus.org.conf.new $SERVER_USER@$SERVER_IP:/etc/nginx/sites-available/boilerhaus.org.conf +eval "$SCP_CMD boilerhaus.org.conf.new $SERVER_USER@$SERVER_IP:/tmp/boilerhaus.org.conf.new" +eval "$SSH_CMD \"mv /tmp/boilerhaus.org.conf.new /etc/nginx/sites-available/boilerhaus.org.conf\"" # Make sure the site is enabled echo "Ensuring site is enabled..." -ssh $SERVER_USER@$SERVER_IP "ln -sf /etc/nginx/sites-available/boilerhaus.org.conf /etc/nginx/sites-enabled/boilerhaus.org.conf" +eval "$SSH_CMD \"ln -sf /etc/nginx/sites-available/boilerhaus.org.conf /etc/nginx/sites-enabled/boilerhaus.org.conf\"" # Test Nginx configuration echo "Testing Nginx configuration..." -ssh $SERVER_USER@$SERVER_IP "nginx -t" +NGINX_TEST=$(eval "$SSH_CMD \"nginx -t 2>&1\"") +NGINX_TEST_EXIT_CODE=$? # If the test is successful, start or reload Nginx -if [ $? -eq 0 ]; then +if [ $NGINX_TEST_EXIT_CODE -eq 0 ]; then echo "Starting or reloading Nginx..." - ssh $SERVER_USER@$SERVER_IP "systemctl is-active nginx || systemctl start nginx" - ssh $SERVER_USER@$SERVER_IP "systemctl reload nginx || systemctl restart nginx" + eval "$SSH_CMD \"systemctl is-active nginx || systemctl start nginx\"" + eval "$SSH_CMD \"systemctl reload nginx || systemctl restart nginx\"" echo "Configuration updated successfully!" else echo "Nginx configuration test failed. Restoring backup..." - ssh $SERVER_USER@$SERVER_IP "cp -f /etc/nginx/sites-available/boilerhaus.org.conf.bak /etc/nginx/sites-available/boilerhaus.org.conf 2>/dev/null || true" + echo "$NGINX_TEST" + eval "$SSH_CMD \"cp -f /etc/nginx/sites-available/boilerhaus.org.conf.bak /etc/nginx/sites-available/boilerhaus.org.conf 2>/dev/null || true\"" echo "Backup restored. Please check the configuration and try again." fi # Run Certbot to ensure SSL certificates are set up for all domains echo "Running Certbot to ensure SSL certificates are set up..." -ssh $SERVER_USER@$SERVER_IP "certbot --nginx --expand -d boilerhaus.org -d www.boilerhaus.org -d cloud.boilerhaus.org -d git.boilerhaus.org -d bw.boilerhaus.org --non-interactive --agree-tos --email admin@boilerhaus.org" +eval "$SSH_CMD \"certbot --nginx --expand -d boilerhaus.org -d www.boilerhaus.org -d cloud.boilerhaus.org -d git.boilerhaus.org -d bw.boilerhaus.org --non-interactive --agree-tos --email admin@boilerhaus.org\"" echo "Done!" \ No newline at end of file