vigil/install.sh
Uthman Fatih 312d3e9674 Refactor installation and uninstallation scripts; enhance logging and error handling
- Removed Docker installation instructions from README.md as they are not yet functional.
- Simplified the installation script by removing the upgrade process and directly checking for existing installations.
- Added checks for required dependencies (python3, curl, python3-venv) during installation.
- Improved logging throughout the application, replacing print statements with logger calls.
- Enhanced email validation using regex and added error handling for invalid email addresses.
- Updated the uninstall script to provide options for complete or partial removal of Vigil.
- Created a logger service to handle logging to both console and log files.
- Updated requirements.txt to use newer versions of dependencies.
- Added versioning to the main application and provided a version option in the command line interface.
2025-11-14 22:58:55 +00:00

210 lines
7.0 KiB
Bash

#!/bin/bash
# First check if we are running as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Get the actual user (not root) for later use
ACTUAL_USER=${SUDO_USER:-$USER}
USER_HOME=$(eval echo ~$ACTUAL_USER)
# /opt/vigil = static files/code
# /etc/vigil = configurations
# /var/log/vigil = logs
# Check if we are running on a supported system
if [[ "$OSTYPE" != "linux-gnu"* ]]; then
echo "This script is only supported on Linux" 1>&2
exit 1
fi
# Now check if we have apt installed
if ! command -v apt &> /dev/null; then
echo "Apt is not installed. Please install it before running this script." 1>&2
exit 1
fi
# Now check if we have python3 installed, if not offer to install it
if ! command -v python3 &> /dev/null; then
echo "Python 3 is not installed. Do you want to install it now? (y/n)"
read -r answer
if [[ "${answer,,}" == "y" ]]; then
apt install python3 -y
else
echo "Please install Python 3 before running this script." 1>&2
exit 1
fi
fi
# Now check if we have curl installed, if not offer to install it
if ! command -v curl &> /dev/null; then
echo "Curl is not installed. Do you want to install it now? (y/n)"
read -r answer
if [[ "${answer,,}" == "y" ]]; then
apt install curl -y
else
echo "Please install curl before running this script." 1>&2
exit 1
fi
fi
# Now check if we have python3-pip installed, if not offer to install it
if ! python3 -m pip --version &> /dev/null; then
echo "pip is not installed. Do you want to install it now? (y/n)"
read -r answer
if [[ "${answer,,}" == "y" ]]; then
apt install python3-pip python3-venv -y
else
echo "Please install pip before running this script." 1>&2
exit 1
fi
fi
# Now check if we have python3-venv installed, if not offer to install it
if ! python3 -m venv &> /dev/null; then
echo "Python3-venv is not installed. Do you want to install it now? (y/n)"
read -r answer
if [[ "${answer,,}" == "y" ]]; then
apt install python3-venv -y
else
echo "Please install python3-venv before running this script." 1>&2
exit 1
fi
fi
if [[ -d "/opt/vigil" ]]; then
echo "Vigil is already installed."
echo "Would you like to upgrade? This will preserve your configuration. (y/n)"
read -r answer
if [[ "${answer,,}" == "y" ]]; then
echo "Stopping vigil service..."
systemctl stop vigil 2>/dev/null
echo "Removing old installation..."
rm -rf /opt/vigil
# Continue with installation
else
echo "Installation cancelled."
exit 0
fi
fi
# First create the required directories
echo ""
# Creating the /opt/vigil directory
echo "Creating /opt/vigil directory..."
mkdir -p /opt/vigil
# Create the /opt/vigil/services directory
echo "Creating /opt/vigil/services directory..."
mkdir -p /opt/vigil/services
# Create the /etc/vigil directory
echo "Creating /etc/vigil directory..."
mkdir -p /etc/vigil
# Create the /var/log/vigil directory
echo "Creating /var/log/vigil directory..."
mkdir -p /var/log/vigil
# Now curl -fsSL each file into the /opt/vigil directory
curl -fsSL https://git.uthmn.com/ufatih/vigil/raw/branch/main/LICENSE -o /opt/vigil/LICENSE || { echo "Failed to download LICENSE file."; exit 1; }
echo " ✓ LICENSE"
curl -fsSL https://git.uthmn.com/ufatih/vigil/raw/branch/main/README.md -o /opt/vigil/README.md || { echo "Failed to download README.md file."; exit 1; }
echo " ✓ README.md"
curl -fsSL https://git.uthmn.com/ufatih/vigil/raw/branch/main/install.sh -o /opt/vigil/install.sh || { echo "Failed to download install.sh file."; exit 1; }
echo " ✓ install.sh"
curl -fsSL https://git.uthmn.com/ufatih/vigil/raw/branch/main/uninstall.sh -o /opt/vigil/uninstall.sh || { echo "Failed to download uninstall.sh file."; exit 1; }
echo " ✓ uninstall.sh"
curl -fsSL https://git.uthmn.com/ufatih/vigil/raw/branch/main/requirements.txt -o /opt/vigil/requirements.txt || { echo "Failed to download requirements.txt file."; exit 1; }
echo " ✓ requirements.txt"
curl -fsSL https://git.uthmn.com/ufatih/vigil/raw/branch/main/main.py -o /opt/vigil/main.py || { echo "Failed to download main.py file."; exit 1; }
echo " ✓ main.py"
curl -fsSL https://git.uthmn.com/ufatih/vigil/raw/branch/main/services/apt.py -o /opt/vigil/services/apt.py || { echo "Failed to download services/apt.py file."; exit 1; }
echo " ✓ services/apt.py"
curl -fsSL https://git.uthmn.com/ufatih/vigil/raw/branch/main/services/mail.py -o /opt/vigil/services/mail.py || { echo "Failed to download services/mail.py file."; exit 1; }
echo " ✓ services/mail.py"
curl -fsSL https://git.uthmn.com/ufatih/vigil/raw/branch/main/services/logger.py -o /opt/vigil/services/logger.py || { echo "Failed to download services/logger.py file."; exit 1; }
echo " ✓ services/logger.py"
curl -fsSL https://git.uthmn.com/ufatih/vigil/raw/branch/main/services/__init__.py -o /opt/vigil/services/__init__.py || { echo "Failed to download services/__init__.py file."; exit 1; }
echo " ✓ services/__init__.py"
echo "✓ All files downloaded successfully"
# Now create a venv and install the required packages
echo ""
echo "Setting up virtual environment and installing dependencies..."
python3 -m venv /opt/vigil/.venv
source /opt/vigil/.venv/bin/activate
pip install -r /opt/vigil/requirements.txt
deactivate
# Create a systemd service
echo "Creating systemd service..."
cat > /etc/systemd/system/vigil.service << 'EOF'
[Unit]
Description=Vigil - APT Update Monitor
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/opt/vigil/.venv/bin/python /opt/vigil/main.py serve --check-daily 18
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
echo "✓ Systemd service created (not enabled or started)"
# Create a command to run vigil
echo "Creating vigil command..."
cat > /usr/local/bin/vigil << 'EOF'
#!/bin/bash
exec /opt/vigil/.venv/bin/python /opt/vigil/main.py "$@"
EOF
chmod +x /usr/local/bin/vigil
echo "✓ Command 'vigil' available system-wide"
echo ""
echo "✓ Vigil installed successfully!"
echo ""
# Check what needs to be configured
NEEDS_CONFIG=false
if [[ ! -f /etc/vigil/.env ]]; then
echo "⚠️ Missing: /etc/vigil/.env"
echo " Create this file with your SMTP credentials"
echo " See README for template: /opt/vigil/README.md"
echo ""
NEEDS_CONFIG=true
fi
if [[ ! -f /etc/vigil/users.json ]]; then
echo "⚠️ Missing: /etc/vigil/users.json"
echo " Create this file with recipient email addresses"
echo " Example: [\"admin@example.com\"]"
echo ""
NEEDS_CONFIG=true
fi
if [[ "$NEEDS_CONFIG" == "true" ]]; then
echo "Next steps:"
echo " 1. Configure the files above"
echo " 2. Test: sudo vigil now"
echo " 3. Enable service: sudo systemctl enable --now vigil"
else
echo "✓ Configuration files found"
echo ""
echo "Next steps:"
echo " 1. Test: sudo vigil now"
echo " 2. Enable service: sudo systemctl enable --now vigil"
fi
echo ""