diff --git a/README.md b/README.md index 60ebd8f..6fd36e7 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ Run the following command: curl -sSL https://git.uthmn.com/ufatih/vigil/raw/main/install.sh | sudo bash ``` - You can also review the script before executing it: +You can also review the script before executing it: ```bash curl -sSL https://git.uthmn.com/ufatih/vigil/raw/main/install.sh -o install.sh @@ -112,24 +112,19 @@ sudo bash install.sh rm install.sh ``` -You can now run Vigil using the following command: +You can also uninstall Vigil using the following command: ```bash -vigil +curl -sSL https://git.uthmn.com/ufatih/vigil/raw/main/uninstall.sh | sudo bash ``` -### Using pipx - -If you prefer to use pipx, you can install Vigil using the following command: +You can also review the script before executing it: ```bash -pipx install vigil -``` - -You can now run Vigil using the following command: - -```bash -vigil +curl -sSL https://git.uthmn.com/ufatih/vigil/raw/main/uninstall.sh -o uninstall.sh +less uninstall.sh +sudo bash uninstall.sh +rm uninstall.sh ``` ### Using docker @@ -240,10 +235,10 @@ If you installed Vigil using the install script, it can optionally create a syst You can manually control the service as follows: ```bash -sudo systemctl start vigil -sudo systemctl stop vigil -sudo systemctl enable vigil -sudo systemctl status vigil +sudo systemctl start vigil # Start vigil +sudo systemctl stop vigil # Stop vigil +sudo systemctl enable vigil # Enable vigil to start at boot +sudo systemctl status vigil # Check the status of vigil ``` This ensures Vigil runs continuously and automatically checks for updates at your configured interval. diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..5c07445 --- /dev/null +++ b/install.sh @@ -0,0 +1,362 @@ +#!/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) + +# Now check if we already have vigil installed +if [[ -d "/opt/vigil" ]]; then + echo "Vigil is already installed." + echo "Would you like to upgrade? This will preserve your .env and users.json files. (y/n)" + read -r answer + + if [[ "${answer,,}" == "y" ]]; then + echo "Backing up configuration files..." + + # Create temporary backup directory + BACKUP_DIR="/tmp/vigil_backup_$(date +%s)" + mkdir -p "$BACKUP_DIR" + + # Backup .env and users.json if they exist + if [[ -f "/opt/vigil/.env" ]]; then + cp /opt/vigil/.env "$BACKUP_DIR/.env" + echo "Backed up .env" + fi + + if [[ -f "/opt/vigil/users.json" ]]; then + cp /opt/vigil/users.json "$BACKUP_DIR/users.json" + echo "Backed up users.json" + fi + + # Stop the service if it's running + if systemctl is-active --quiet vigil.service; then + echo "Stopping vigil service..." + systemctl stop vigil.service + fi + + # Remove the old installation + echo "Removing old installation..." + rm -rf /opt/vigil + + echo "Upgrade preparation complete. Proceeding with fresh installation..." + echo "" + + # Set flag to restore config later + UPGRADE_MODE=true + else + echo "Installation cancelled. Please remove /opt/vigil manually if you want a fresh install." + exit 1 + fi +fi + +# Now 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 + +# 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 git and python3 installed, if not offer to install them +if ! command -v git &> /dev/null; then + echo "Git is not installed. Do you want to install it now? (y/n)" + read -r answer + if [[ "${answer,,}" == "y" ]]; then + apt install git -y + else + echo "Please install git before running this script." 1>&2 + exit 1 + fi +fi + +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 python3 -m 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 git clone the repository to /opt/vigil +echo "" +echo "Cloning Vigil repository..." +git clone https://git.uthmn.com/ufatih/vigil.git /opt/vigil + +# Now create a virtual environment 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 + +# Now create a .env file and request the user to fill in the required values +echo "" +echo "=== Email Configuration ===" +echo "" + +# Check if we're in upgrade mode and have a backup +if [[ "$UPGRADE_MODE" == true ]] && [[ -f "$BACKUP_DIR/.env" ]]; then + echo "Found existing .env configuration. Would you like to:" + echo "1) Keep existing configuration" + echo "2) Enter new configuration" + read -r config_choice + + if [[ "$config_choice" == "1" ]]; then + cp "$BACKUP_DIR/.env" /opt/vigil/.env + echo "Restored existing .env configuration" + chmod 600 /opt/vigil/.env + + # Skip to connection testing + SKIP_ENV_INPUT=true + fi +fi + +if [[ "$SKIP_ENV_INPUT" != true ]]; then + touch /opt/vigil/.env + + # SMTP settings + echo "Please enter SMTP server address:" + read -r SMTP_SERVER + + echo "Please enter SMTP port (default: 587 for TLS, 465 for SSL):" + read -r SMTP_PORT + + echo "Please enter SMTP username:" + read -r SMTP_USERNAME + + echo "Please enter SMTP password:" + read -rs SMTP_PASSWORD + echo "" + + # IMAP settings + echo "Please enter IMAP server address:" + read -r IMAP_SERVER + + echo "Please enter IMAP port (default: 993 for SSL):" + read -r IMAP_PORT + + echo "Please enter IMAP username:" + read -r IMAP_USERNAME + + echo "Please enter IMAP password:" + read -rs IMAP_PASSWORD + echo "" + + # Write to .env file + cat > /opt/vigil/.env << EOF +SMTP_SERVER=$SMTP_SERVER +SMTP_PORT=$SMTP_PORT +SMTP_USERNAME=$SMTP_USERNAME +SMTP_PASSWORD=$SMTP_PASSWORD + +IMAP_SERVER=$IMAP_SERVER +IMAP_PORT=$IMAP_PORT +IMAP_USERNAME=$IMAP_USERNAME +IMAP_PASSWORD=$IMAP_PASSWORD +EOF + + echo "Configuration saved to /opt/vigil/.env" + + # Set secure permissions on .env file (contains passwords) + chmod 600 /opt/vigil/.env +fi + +# Test if the inputs work/are valid, else tell the user to fill in later and continue +if [[ "$SKIP_ENV_INPUT" != true ]]; then + echo "" + echo "Testing connections..." + + # Check if required commands are available + if command -v nc >/dev/null 2>&1; then + # Test SMTP connection with netcat (timeout after 5 seconds) + echo "Testing SMTP connection to $SMTP_SERVER:$SMTP_PORT..." + if timeout 5 nc -zv "$SMTP_SERVER" "$SMTP_PORT" 2>&1 | grep -q succeeded; then + echo "[OK] SMTP connection successful" + SMTP_VALID=true + else + echo "[FAILED] SMTP connection failed - could not reach $SMTP_SERVER:$SMTP_PORT" + SMTP_VALID=false + fi + + echo "" + + # Test IMAP connection + echo "Testing IMAP connection to $IMAP_SERVER:$IMAP_PORT..." + if timeout 5 nc -zv "$IMAP_SERVER" "$IMAP_PORT" 2>&1 | grep -q succeeded; then + echo "[OK] IMAP connection successful" + IMAP_VALID=true + else + echo "[FAILED] IMAP connection failed - could not reach $IMAP_SERVER:$IMAP_PORT" + IMAP_VALID=false + fi + else + echo "[WARNING] Cannot test connections (nc not installed). Skipping validation." + SMTP_VALID=unknown + IMAP_VALID=unknown + fi + + # Summary and continue + echo "" + if [ "$SMTP_VALID" = false ] || [ "$IMAP_VALID" = false ]; then + echo "[WARNING] Some connections failed." + echo "Configuration has been saved to /opt/vigil/.env" + echo "Please verify your settings and update the file manually if needed." + echo "" + read -p "Press Enter to continue..." -r + else + echo "[OK] All connections successful!" + echo "Configuration saved to /opt/vigil/.env" + fi +else + echo "" + echo "[OK] Using existing email configuration" +fi + +# Now create a users.json file and request the user to fill in the required values (or leave empty to skip) +echo "" +echo "=== Notification Recipients ===" +echo "" + +# Check if we're in upgrade mode and have a backup +if [[ "$UPGRADE_MODE" == true ]] && [[ -f "$BACKUP_DIR/users.json" ]]; then + echo "Found existing users.json configuration. Would you like to:" + echo "1) Keep existing recipients" + echo "2) Enter new recipients" + read -r users_choice + + if [[ "$users_choice" == "1" ]]; then + cp "$BACKUP_DIR/users.json" /opt/vigil/users.json + echo "Restored existing notification recipients" + + # Clean up backup directory + rm -rf "$BACKUP_DIR" + + # Skip to next section + SKIP_USERS_INPUT=true + fi +fi + +if [[ "$SKIP_USERS_INPUT" != true ]]; then + echo "Please enter the email addresses vigil will send notifications to (comma separated):" + echo "(Leave empty to skip)" + read -r EMAILS + + # Strip whitespace and convert to JSON array + if [ -n "$EMAILS" ]; then + # Remove all whitespace, split by comma, and build JSON array + EMAILS_CLEAN=$(echo "$EMAILS" | tr -d '[:space:]') + + # Convert to JSON array format + EMAILS_JSON="[" + IFS=',' read -ra EMAIL_ARRAY <<< "$EMAILS_CLEAN" + for i in "${!EMAIL_ARRAY[@]}"; do + if [ $i -gt 0 ]; then + EMAILS_JSON="$EMAILS_JSON," + fi + EMAILS_JSON="$EMAILS_JSON\"${EMAIL_ARRAY[$i]}\"" + done + EMAILS_JSON="$EMAILS_JSON]" + + echo "$EMAILS_JSON" > /opt/vigil/users.json + echo "Notification recipients saved to /opt/vigil/users.json" + else + echo "[]" > /opt/vigil/users.json + echo "No recipients configured. You can add them later in /opt/vigil/users.json" + fi + + # Clean up backup directory if it exists + if [[ -n "$BACKUP_DIR" ]] && [[ -d "$BACKUP_DIR" ]]; then + rm -rf "$BACKUP_DIR" # Don't want to hit a steam + fi +fi + +# Set ownership of vigil directory to actual user +chown -R "$ACTUAL_USER:$ACTUAL_USER" /opt/vigil + +# Create the alias to the command +echo "" +echo "Adding 'vigil' command alias..." +ALIAS_LINE="alias vigil='source /opt/vigil/.venv/bin/activate && python3 /opt/vigil/main.py && deactivate'" + +# Add to user's bashrc if not already present +if ! grep -q "alias vigil=" "$USER_HOME/.bashrc" 2>/dev/null; then + echo "$ALIAS_LINE" >> "$USER_HOME/.bashrc" + echo "Alias added to $USER_HOME/.bashrc" +else + echo "Alias already exists in $USER_HOME/.bashrc" +fi + +# Suggest to add vigil to systemctl to autostart (after network is up) +echo "" +echo "Would you like to add vigil to autostart on boot? (y/n)" +read -r answer +if [[ "${answer,,}" == "y" ]]; then + # Create systemd service file + cat > /etc/systemd/system/vigil.service << EOF +[Unit] +Description=Vigil Monitoring Service +After=network-online.target +Wants=network-online.target + +[Service] +Type=simple +User=$ACTUAL_USER +WorkingDirectory=/opt/vigil +Environment="PATH=/opt/vigil/.venv/bin" +ExecStart=/opt/vigil/.venv/bin/python3 /opt/vigil/main.py +Restart=on-failure +RestartSec=10 + +[Install] +WantedBy=multi-user.target +EOF + + # Reload systemd, enable and start the service + systemctl daemon-reload + systemctl enable vigil.service + systemctl start vigil.service + + echo "[OK] Vigil service has been created and started" + echo "You can check its status with: systemctl status vigil" +else + echo "Skipping autostart configuration" +fi + +echo "" +echo "========================================" +echo "Vigil has been installed successfully!" +echo "========================================" +echo "" +echo "To use vigil, run: source ~/.bashrc && vigil" +echo "Or simply open a new terminal and run: vigil" +echo "" +if [[ "${answer,,}" == "y" ]]; then + echo "Vigil is now running as a system service." + echo "Use 'systemctl status vigil' to check its status" +fi \ No newline at end of file diff --git a/uninstall.sh b/uninstall.sh new file mode 100644 index 0000000..89c102f --- /dev/null +++ b/uninstall.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +# TODO: Add uninstall to readme + +# Just double check, check /opt/vigil exists, then remove it, also check+remove systemd service, remove aliases from bashrc and also dont offer but notify that dependencies not uninstalled + +# Check if vigil is installed, if not then exit +if [[ ! -d "/opt/vigil" ]]; then + echo "Vigil is not installed. Exiting." 1>&2 + exit 1 +fi + +# 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) + +# Now 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 + +# Warning and countdown +echo "WARNING: This will remove /opt/vigil and any related data." +echo "Press Ctrl+C to cancel..." +echo "" + +for i in 5 4 3 2 1; do + echo -n "$i..." + sleep 1 +done +echo "" +echo "" + +# Final confirmation +echo "Continue with uninstallation? (y/n)" +read -r answer +if [[ "${answer,,}" != "y" ]]; then + echo "Uninstallation cancelled." + exit 0 +fi + +# Now delete the vigil directory +echo "" +echo "Removing Vigil installation..." +rm -rf /opt/vigil + +# Now delete the systemd service (if it exists) +if [[ -f /etc/systemd/system/vigil.service ]]; then + echo "Removing Vigil systemd service..." + systemctl stop vigil.service 2>/dev/null + systemctl disable vigil.service 2>/dev/null + rm /etc/systemd/system/vigil.service + systemctl daemon-reload +else + echo "No systemd service found (skipping)" +fi + +# Now delete the aliases from bashrc +echo "Removing Vigil aliases from bashrc..." +if grep -q "alias vigil=" "$USER_HOME/.bashrc" 2>/dev/null; then + sed -i '/alias vigil=/d' "$USER_HOME/.bashrc" + echo "Aliases removed from $USER_HOME/.bashrc" +else + echo "No aliases found in bashrc" +fi + +echo "" +echo "Dependencies not uninstalled. Please uninstall them manually if needed: python3, git, pip" +echo "Vigil has been uninstalled successfully."