163 lines
4.6 KiB
Bash
Executable file
163 lines
4.6 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
#
|
|
# Installation script for email_tunnel systemd service
|
|
#
|
|
# This script will:
|
|
# 1. Verify prerequisites
|
|
# 2. Copy service file to systemd directory with correct paths/user
|
|
# 3. Enable and start the service
|
|
# 4. Show status and usage information
|
|
|
|
set -euo pipefail
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Get script directory
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
echo "======================================"
|
|
echo "email_tunnel Service Installer"
|
|
echo "======================================"
|
|
echo ""
|
|
|
|
# Check if running as root
|
|
if [ "$EUID" -eq 0 ]; then
|
|
echo -e "${RED}Error: Do not run this script as root or with sudo${NC}"
|
|
echo "The script will request sudo privileges when needed."
|
|
exit 1
|
|
fi
|
|
|
|
# Verify required files exist
|
|
echo "Checking required files..."
|
|
REQUIRED_FILES=(
|
|
"$SCRIPT_DIR/email_tunnel.py"
|
|
"$SCRIPT_DIR/.env"
|
|
"$SCRIPT_DIR/email_tunnel.service"
|
|
)
|
|
|
|
for file in "${REQUIRED_FILES[@]}"; do
|
|
if [ ! -f "$file" ]; then
|
|
echo -e "${RED}Error: Required file not found: $file${NC}"
|
|
exit 1
|
|
fi
|
|
done
|
|
echo -e "${GREEN}✓ All required files found${NC}"
|
|
echo ""
|
|
|
|
# Check for required commands
|
|
echo "Checking dependencies..."
|
|
MISSING_DEPS=()
|
|
for cmd in python3 systemctl; do
|
|
if ! command -v "$cmd" &> /dev/null; then
|
|
MISSING_DEPS+=("$cmd")
|
|
fi
|
|
done
|
|
|
|
if [ ${#MISSING_DEPS[@]} -gt 0 ]; then
|
|
echo -e "${RED}Error: Missing required dependencies: ${MISSING_DEPS[*]}${NC}"
|
|
echo "Please install them and try again."
|
|
exit 1
|
|
fi
|
|
echo -e "${GREEN}✓ All dependencies installed${NC}"
|
|
echo ""
|
|
|
|
# Verify .env file has required variables
|
|
echo "Validating .env configuration..."
|
|
REQUIRED_VARS=(IMAP_IP IMAP_PORT IMAP_USER IMAP_PASSWORD IMAP_FOLDER SMTP_PORT SMTP_IP)
|
|
MISSING_ENV=()
|
|
for var in "${REQUIRED_VARS[@]}"; do
|
|
if ! grep -q "^${var}=" "$SCRIPT_DIR/.env"; then
|
|
MISSING_ENV+=("$var")
|
|
fi
|
|
done
|
|
|
|
if [ ${#MISSING_ENV[@]} -gt 0 ]; then
|
|
echo -e "${YELLOW}Warning: .env may be missing: ${MISSING_ENV[*]}${NC}"
|
|
read -p "Continue anyway? (y/N) " -n 1 -r
|
|
echo ""
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
exit 1
|
|
fi
|
|
else
|
|
echo -e "${GREEN}✓ .env configuration looks good${NC}"
|
|
fi
|
|
echo ""
|
|
|
|
# Test script syntax
|
|
echo "Testing email_tunnel.py byte-compile..."
|
|
if python3 -m py_compile "$SCRIPT_DIR/email_tunnel.py"; then
|
|
echo -e "${GREEN}✓ Byte-compile succeeded${NC}"
|
|
else
|
|
echo -e "${RED}Error: Byte-compile failed${NC}"
|
|
exit 1
|
|
fi
|
|
echo ""
|
|
|
|
# Check if service is already installed
|
|
SERVICE_NAME="email_tunnel.service"
|
|
if systemctl list-unit-files | grep -q "$SERVICE_NAME"; then
|
|
echo -e "${YELLOW}Warning: $SERVICE_NAME is already installed${NC}"
|
|
read -p "Reinstall? (y/N) " -n 1 -r
|
|
echo ""
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
exit 0
|
|
fi
|
|
echo "Stopping existing service..."
|
|
sudo systemctl stop "$SERVICE_NAME" 2>/dev/null || true
|
|
sudo systemctl disable "$SERVICE_NAME" 2>/dev/null || true
|
|
fi
|
|
|
|
# Prepare service file with correct paths
|
|
echo "Preparing service file..."
|
|
TEMP_SERVICE=$(mktemp)
|
|
sed "s|/opt/email-tunnel|$SCRIPT_DIR|g" "$SCRIPT_DIR/email_tunnel.service" > "$TEMP_SERVICE"
|
|
|
|
# Update user/group in service file
|
|
CURRENT_USER=$(whoami)
|
|
CURRENT_GROUP=$(id -gn)
|
|
sed -i "s|User=email-tunnel|User=$CURRENT_USER|g" "$TEMP_SERVICE"
|
|
sed -i "s|Group=email-tunnel|Group=$CURRENT_GROUP|g" "$TEMP_SERVICE"
|
|
|
|
# Install service file
|
|
echo "Installing systemd service file..."
|
|
sudo cp "$TEMP_SERVICE" /etc/systemd/system/$SERVICE_NAME
|
|
rm "$TEMP_SERVICE"
|
|
sudo chmod 644 /etc/systemd/system/$SERVICE_NAME
|
|
echo -e "${GREEN}✓ Service file installed${NC}"
|
|
echo ""
|
|
|
|
# Reload systemd
|
|
echo "Reloading systemd daemon..."
|
|
sudo systemctl daemon-reload
|
|
echo -e "${GREEN}✓ Systemd reloaded${NC}"
|
|
echo ""
|
|
|
|
# Enable and start service
|
|
echo "Enabling and starting $SERVICE_NAME..."
|
|
sudo systemctl enable "$SERVICE_NAME"
|
|
sudo systemctl start "$SERVICE_NAME"
|
|
echo -e "${GREEN}✓ Service enabled and started${NC}"
|
|
echo ""
|
|
|
|
# Show status
|
|
echo "======================================"
|
|
echo "Installation Complete!"
|
|
echo "======================================"
|
|
echo ""
|
|
echo "Service Status:"
|
|
sudo systemctl status "$SERVICE_NAME" --no-pager -l || true
|
|
echo ""
|
|
|
|
echo -e "${GREEN}email_tunnel is now running!${NC}"
|
|
echo ""
|
|
echo "Useful commands:"
|
|
echo " View status: systemctl status $SERVICE_NAME"
|
|
echo " View logs: ./logs_email_tunnel.sh -f"
|
|
echo " Restart service: sudo systemctl restart $SERVICE_NAME"
|
|
echo " Stop service: sudo systemctl stop $SERVICE_NAME"
|
|
echo " Uninstall: ./uninstall_email_tunnel.sh"
|
|
echo ""
|