Files
nebuleair_pro_4g/installation.sh
Your Name 92ec2a0bb9 update
2025-02-25 14:57:29 +01:00

174 lines
6.1 KiB
Bash
Executable File

#!/bin/bash
# Exit on error, unset variable usage, and error in a pipeline
set -euo pipefail
# Define color variables
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No color
# Function to print messages in color
info() { echo -e "${BLUE}[INFO]${NC} $1"; }
success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
# Check for root privileges
if [[ "$EUID" -ne 0 ]]; then
error "This script must be run as root. Use 'sudo ./installation.sh'"
fi
# Update and install necessary packages
info "Updating package list and installing necessary packages..."
sudo apt update && sudo apt install -y git gh apache2 php php-sqlite3 python3 python3-pip jq autossh i2c-tools python3-smbus || error "Failed to install required packages."
# Install Python libraries
info "Installing Python libraries..."
sudo pip3 install pyserial requests RPi.GPIO adafruit-circuitpython-bme280 crcmod psutil --break-system-packages || error "Failed to install Python libraries."
# Ask user if they want to set up SSH keys
read -p "Do you want to set up an SSH key for /var/www? (y/n): " answer
answer=${answer,,} # Convert to lowercase
if [[ "$answer" == "y" ]]; then
info "Setting up SSH keys..."
sudo mkdir -p /var/www/.ssh
sudo chmod 700 /var/www/.ssh
if [[ ! -f /var/www/.ssh/id_rsa ]]; then
sudo ssh-keygen -t rsa -b 4096 -f /var/www/.ssh/id_rsa -N ""
success "SSH key generated successfully."
else
warning "SSH key already exists. Skipping key generation."
fi
sudo ssh-copy-id -i /var/www/.ssh/id_rsa.pub -p 50221 airlab_server1@aircarto.fr || warning "Failed to copy SSH key. Please check the server connection."
success "SSH setup complete!"
else
warning "Skipping SSH key setup."
fi
# Clone the repository (check if it exists first)
REPO_DIR="/var/www/nebuleair_pro_4g"
if [[ -d "$REPO_DIR" ]]; then
warning "Repository already exists. Skipping clone."
else
info "Cloning the NebuleAir Pro 4G repository..."
sudo git clone http://gitea.aircarto.fr/PaulVua/nebuleair_pro_4g.git "$REPO_DIR" || error "Failed to clone repository."
fi
# Set up repository files and permissions
info "Setting up repository files and permissions..."
sudo mkdir -p "$REPO_DIR/logs"
sudo touch "$REPO_DIR/logs/app.log" "$REPO_DIR/logs/master.log" "$REPO_DIR/logs/master_errors.log" "$REPO_DIR/wifi_list.csv"
sudo cp "$REPO_DIR/config.json.dist" "$REPO_DIR/config.json"
sudo chmod -R 755 "$REPO_DIR/"
sudo chown -R www-data:www-data "$REPO_DIR/"
sudo git config --global core.fileMode false
#sudo git -C /var/www/nebuleair_pro_4g config core.fileMode false
sudo git config --global --add safe.directory "$REPO_DIR"
# Set up cron jobs (ensure file exists first)
info "Setting up cron jobs..."
if [[ -f "$REPO_DIR/cron_jobs" ]]; then
sudo crontab "$REPO_DIR/cron_jobs"
success "Cron jobs set up successfully."
else
warning "Cron jobs file not found. Skipping."
fi
# Create databases
info "Creating databases..."
if [[ -f "$REPO_DIR/sqlite/create_db.py" ]]; then
sudo /usr/bin/python3 "$REPO_DIR/sqlite/create_db.py" || error "Failed to create databases."
success "Databases created successfully."
else
warning "Database creation script not found."
fi
# Configure Apache
info "Configuring Apache..."
APACHE_CONF="/etc/apache2/sites-available/000-default.conf"
if grep -q "DocumentRoot /var/www/nebuleair_pro_4g" "$APACHE_CONF"; then
warning "Apache configuration already set. Skipping."
else
sudo sed -i 's|DocumentRoot /var/www/html|DocumentRoot /var/www/nebuleair_pro_4g|' "$APACHE_CONF"
sudo systemctl reload apache2
success "Apache configuration updated and reloaded."
fi
# Add sudo authorization (prevent duplicate entries)
info "Setting up sudo authorization..."
if ! sudo grep -q "/usr/bin/nmcli" /etc/sudoers; then
echo -e "ALL ALL=(ALL) NOPASSWD: /usr/bin/nmcli, /usr/sbin/reboot\nwww-data ALL=(ALL) NOPASSWD: /usr/bin/git pull\nwww-data ALL=(ALL) NOPASSWD: /usr/bin/ssh\nwww-data ALL=(ALL) NOPASSWD: /usr/bin/python3 *" | sudo tee -a /etc/sudoers > /dev/null
success "Sudo authorization added."
else
warning "Sudo authorization already set. Skipping."
fi
# Open all UART serial ports (avoid duplication)
info "Configuring UART serial ports..."
if ! grep -q "enable_uart=1" /boot/firmware/config.txt; then
echo -e "\nenable_uart=1\ndtoverlay=uart0\ndtoverlay=uart1\ndtoverlay=uart2\ndtoverlay=uart3\ndtoverlay=uart4\ndtoverlay=uart5" | sudo tee -a /boot/firmware/config.txt > /dev/null
success "UART configuration added."
else
warning "UART configuration already set. Skipping."
fi
# Ensure correct permissions for serial devices
info "Setting permissions for serial devices..."
sudo chmod 666 /dev/ttyAMA* || warning "Failed to set permissions for /dev/ttyAMA*"
# Enable I2C ports
info "Enabling I2C ports..."
sudo raspi-config nonint do_i2c 0
success "I2C ports enabled."
#Add master_nebuleair.service
SERVICE_FILE="/etc/systemd/system/master_nebuleair.service"
info "Setting up systemd service for master_nebuleair..."
# Create the systemd service file (overwrite if necessary)
sudo bash -c "cat > $SERVICE_FILE" <<EOF
[Unit]
Description=Master manager for the Python loop scripts
After=network.target
[Service]
ExecStart=/usr/bin/python3 /var/www/nebuleair_pro_4g/master.py
Restart=always
User=root
WorkingDirectory=/var/www/nebuleair_pro_4g
StandardOutput=append:/var/www/nebuleair_pro_4g/logs/master.log
StandardError=append:/var/www/nebuleair_pro_4g/logs/master_errors.log
[Install]
WantedBy=multi-user.target
EOF
success "Systemd service file created: $SERVICE_FILE"
# Reload systemd to recognize the new service
info "Reloading systemd daemon..."
sudo systemctl daemon-reload
# Enable the service to start on boot
info "Enabling the service to start on boot..."
sudo systemctl enable master_nebuleair.service
# Start the service immediately
info "Starting the service..."
sudo systemctl start master_nebuleair.service
# Completion message
success "Setup completed successfully!"
info "System will reboot in 5 seconds..."
sleep 5
sudo reboot