Add password automation with sshpass and environment variables

This commit is contained in:
boilerrat 2025-03-16 14:26:25 -04:00
parent ffe3bfc6cd
commit 9c99fa4d17
5 changed files with 110 additions and 20 deletions

4
.env.example Normal file
View File

@ -0,0 +1,4 @@
# VPS Configuration
SERVER_IP="your_server_ip"
SERVER_USER="root"
SERVER_PASSWORD="your_server_password"

View File

@ -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 && \
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"
rm /tmp/website.tar.gz\""
# Clean up local files
echo "Cleaning up..."

33
install-sshpass.sh Executable file
View File

@ -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."

View File

@ -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"

View File

@ -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!"