#!/bin/bash set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo -e "${GREEN}Oculog Client Installation${NC}" echo "==================================" echo "" # Check if running as root if [ "$EUID" -ne 0 ]; then echo -e "${RED}Please run as root (use sudo)${NC}" exit 1 fi # Check if Python 3 is installed if ! command -v python3 &> /dev/null; then echo -e "${RED}Python 3 is not installed. Please install it first:${NC}" echo " sudo apt update && sudo apt install -y python3 python3-pip" exit 1 fi # Get installation directory SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" INSTALL_DIR="/opt/oculog" # Check if this is an upgrade IS_UPGRADE=false SERVICE_WAS_RUNNING=false SERVICE_WAS_ENABLED=false # Check if service unit file exists if [ -f /etc/systemd/system/oculog-client.service ] || systemctl list-unit-files | grep -q "^oculog-client.service"; then IS_UPGRADE=true echo -e "${YELLOW}Detected existing installation - performing upgrade...${NC}" echo "" # Check if service is enabled if systemctl is-enabled --quiet oculog-client.service 2>/dev/null; then SERVICE_WAS_ENABLED=true fi # Check if service is running if systemctl is-active --quiet oculog-client.service 2>/dev/null; then SERVICE_WAS_RUNNING=true echo -e "${YELLOW}Service is currently running. Stopping it...${NC}" systemctl stop oculog-client.service || true sleep 1 if systemctl is-active --quiet oculog-client.service 2>/dev/null; then echo -e "${RED}Warning: Service did not stop cleanly${NC}" else echo -e "${GREEN}Service stopped${NC}" fi fi else echo -e "${GREEN}Fresh installation detected${NC}" echo "" fi echo -e "${GREEN}Step 1: Creating installation directory...${NC}" mkdir -p "$INSTALL_DIR" mkdir -p /var/log/oculog mkdir -p /etc/oculog echo -e "${GREEN}Step 2: Copying client files...${NC}" cp "$SCRIPT_DIR/client.py" "$INSTALL_DIR/" chmod +x "$INSTALL_DIR/client.py" echo -e "${GREEN}Step 3: Installing Python dependencies...${NC}" # Try to install via apt first (for newer Ubuntu versions with externally-managed environment) if apt-cache show python3-psutil >/dev/null 2>&1 && apt-cache show python3-requests >/dev/null 2>&1; then echo -e "${YELLOW}Installing via apt (system-managed packages)...${NC}" apt-get update if apt-get install -y python3-psutil python3-requests; then PYTHON_BIN="/usr/bin/python3" echo -e "${GREEN}System packages installed successfully${NC}" else echo -e "${YELLOW}Apt installation failed, using virtual environment...${NC}" USE_VENV=1 fi else echo -e "${YELLOW}System packages not available, using virtual environment...${NC}" USE_VENV=1 fi # If apt packages aren't available, use a virtual environment if [ "$USE_VENV" = "1" ]; then if [ -d "$INSTALL_DIR/venv" ]; then echo -e "${YELLOW}Updating existing virtual environment...${NC}" PYTHON_BIN="$INSTALL_DIR/venv/bin/python3" else echo -e "${YELLOW}Setting up virtual environment...${NC}" apt-get update apt-get install -y python3-venv python3-pip || true # Create virtual environment python3 -m venv "$INSTALL_DIR/venv" PYTHON_BIN="$INSTALL_DIR/venv/bin/python3" fi # Install/upgrade packages in virtual environment "$INSTALL_DIR/venv/bin/pip" install --quiet --upgrade pip "$INSTALL_DIR/venv/bin/pip" install --quiet --upgrade -r "$SCRIPT_DIR/requirements.txt" echo -e "${GREEN}Virtual environment ready and packages installed${NC}" fi # Update shebang in client script to use correct Python sed -i "1s|.*|#!${PYTHON_BIN}|" "$INSTALL_DIR/client.py" echo -e "${GREEN}Step 4: Creating symlink...${NC}" ln -sf "$INSTALL_DIR/client.py" /usr/local/bin/oculog-client echo -e "${GREEN}Step 5: Setting up configuration...${NC}" if [ ! -f /etc/oculog/client.conf ]; then # Fresh installation - create new config if [ -f "$SCRIPT_DIR/client.conf.example" ]; then cp "$SCRIPT_DIR/client.conf.example" /etc/oculog/client.conf echo -e "${YELLOW}Configuration file created at /etc/oculog/client.conf${NC}" echo -e "${YELLOW}Please edit it with your server URL and server ID${NC}" else cat > /etc/oculog/client.conf << EOF { "server_url": "http://localhost:3001", "server_id": "$(hostname)", "interval": 30 } EOF echo -e "${YELLOW}Default configuration created at /etc/oculog/client.conf${NC}" fi else # Existing installation - preserve config if [ "$IS_UPGRADE" = true ]; then # Backup existing config before upgrade BACKUP_FILE="/etc/oculog/client.conf.backup.$(date +%Y%m%d_%H%M%S)" cp /etc/oculog/client.conf "$BACKUP_FILE" echo -e "${GREEN}Configuration file preserved (backup: $BACKUP_FILE)${NC}" else echo -e "${GREEN}Configuration file already exists, preserving...${NC}" fi fi echo -e "${GREEN}Step 6: Installing systemd service...${NC}" # Update service file to use correct Python binary sed "s|ExecStart=/usr/local/bin/oculog-client|ExecStart=${PYTHON_BIN} $INSTALL_DIR/client.py|" "$SCRIPT_DIR/oculog-client.service" > /etc/systemd/system/oculog-client.service systemctl daemon-reload systemctl enable oculog-client.service # Restart service if it was running before upgrade if [ "$IS_UPGRADE" = true ]; then if [ "$SERVICE_WAS_RUNNING" = true ] || [ "$SERVICE_WAS_ENABLED" = true ]; then echo -e "${GREEN}Step 7: Starting service...${NC}" systemctl start oculog-client.service || true sleep 2 if systemctl is-active --quiet oculog-client.service 2>/dev/null; then echo -e "${GREEN}Service started successfully${NC}" else echo -e "${YELLOW}Warning: Service may have failed to start. Check status with: systemctl status oculog-client${NC}" fi fi fi echo "" if [ "$IS_UPGRADE" = true ]; then echo -e "${GREEN}Upgrade completed successfully!${NC}" else echo -e "${GREEN}Installation completed successfully!${NC}" fi echo "" if [ "$IS_UPGRADE" = false ]; then echo "Next steps:" echo "1. Edit /etc/oculog/client.conf with your server URL" echo "2. Start the service: sudo systemctl start oculog-client" echo "3. Check status: sudo systemctl status oculog-client" echo "4. View logs: sudo journalctl -u oculog-client -f" else echo "Upgrade complete. Service status:" systemctl status oculog-client.service --no-pager -l || true echo "" echo "View logs: sudo journalctl -u oculog-client -f" fi echo ""