This commit is contained in:
Your Name
2025-02-25 11:59:57 +01:00
parent 144d904813
commit b5aeafeb56

View File

@@ -1,94 +1,135 @@
#!/bin/bash #!/bin/bash
# Exit immediately if a command exits with a non-zero status # Exit on error, unset variable usage, and error in a pipeline
set -e 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 ./script.sh'"
fi
# Update and install necessary packages # Update and install necessary packages
echo "Updating package list and installing necessary packages..." info "Updating package list and installing necessary packages..."
sudo apt update 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."
sudo apt install git gh apache2 php php-sqlite3 python3 python3-pip jq autossh i2c-tools python3-smbus -y
# Install Python libraries # Install Python libraries
echo "Installing Python libraries..." info "Installing Python libraries..."
sudo pip3 install pyserial requests RPi.GPIO adafruit-circuitpython-bme280 crcmod psutil --break-system-packages 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 # 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 read -p "Do you want to set up an SSH key for /var/www? (y/n): " answer
answer=${answer,,} # Convert to lowercase
# Convert input to lowercase to accept 'Y' or 'y'
answer=${answer,,}
if [[ "$answer" == "y" ]]; then if [[ "$answer" == "y" ]]; then
echo "Setting up SSH keys..." info "Setting up SSH keys..."
# Create SSH directory if it doesn't exist
sudo mkdir -p /var/www/.ssh sudo mkdir -p /var/www/.ssh
sudo chmod 700 /var/www/.ssh sudo chmod 700 /var/www/.ssh
# Generate SSH key if it does not already exist
if [[ ! -f /var/www/.ssh/id_rsa ]]; then if [[ ! -f /var/www/.ssh/id_rsa ]]; then
sudo ssh-keygen -t rsa -b 4096 -f /var/www/.ssh/id_rsa -N "" sudo ssh-keygen -t rsa -b 4096 -f /var/www/.ssh/id_rsa -N ""
echo "SSH key generated successfully." success "SSH key generated successfully."
else else
echo "SSH key already exists. Skipping key generation." warning "SSH key already exists. Skipping key generation."
fi fi
# Copy SSH key to remote server 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."
sudo ssh-copy-id -i /var/www/.ssh/id_rsa.pub -p 50221 airlab_server1@aircarto.fr
echo "SSH setup complete!" success "SSH setup complete!"
else else
echo "Skipping SSH key setup." warning "Skipping SSH key setup."
fi fi
# Clone the repository (check if it exists first)
# Clone the repository REPO_DIR="/var/www/nebuleair_pro_4g"
echo "Cloning the NebuleAir Pro 4G repository..." if [[ -d "$REPO_DIR" ]]; then
sudo git clone http://gitea.aircarto.fr/PaulVua/nebuleair_pro_4g.git /var/www/nebuleair_pro_4g 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 # Set up repository files and permissions
echo "Setting up repository files and permissions..." info "Setting up repository files and permissions..."
sudo mkdir /var/www/nebuleair_pro_4g/logs sudo mkdir -p "$REPO_DIR/logs"
sudo touch /var/www/nebuleair_pro_4g/logs/app.log /var/www/nebuleair_pro_4g/logs/loop.log /var/www/nebuleair_pro_4g/wifi_list.csv sudo touch "$REPO_DIR/logs/app.log" "$REPO_DIR/logs/loop.log" "$REPO_DIR/wifi_list.csv"
sudo cp /var/www/nebuleair_pro_4g/config.json.dist /var/www/nebuleair_pro_4g/config.json sudo cp "$REPO_DIR/config.json.dist" "$REPO_DIR/config.json"
sudo chmod -R 777 /var/www/nebuleair_pro_4g/ sudo chmod -R 755 "$REPO_DIR/"
git config --global core.fileMode false sudo chown -R www-data:www-data "$REPO_DIR/"
git config --global --add safe.directory /var/www/nebuleair_pro_4g sudo -u www-data git config --global core.fileMode false
sudo -u www-data git config --global --add safe.directory "$REPO_DIR"
# Set up cron jobs # Set up cron jobs (ensure file exists first)
echo "Setting up cron jobs..." info "Setting up cron jobs..."
sudo crontab /var/www/nebuleair_pro_4g/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
#Creates databases # Create databases
echo "Creating databases." info "Creating databases..."
sudo /usr/bin/python3 /var/www/nebuleair_pro_4g/sqlite/create_db.py 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
#Apache # Configure Apache
echo "Set Up Apache" info "Configuring Apache..."
sudo sed -i 's|DocumentRoot /var/www/html|DocumentRoot /var/www/nebuleair_pro_4g|' /etc/apache2/sites-available/000-default.conf APACHE_CONF="/etc/apache2/sites-available/000-default.conf"
sudo systemctl reload apache2 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 # Add sudo authorization (prevent duplicate entries)
info "Setting up sudo authorization..."
if ! sudo grep -q "/usr/bin/nmcli" /etc/sudoers; then if ! sudo grep -q "/usr/bin/nmcli" /etc/sudoers; then
echo "Setting up sudo authorization..." 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
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 EDITOR="tee -a" visudo success "Sudo authorization added."
else else
echo "Sudo authorization already set. Skipping..." warning "Sudo authorization already set. Skipping."
fi fi
#Open all serial ports # Open all UART serial ports (avoid duplication)
info "Configuring UART serial ports..."
if ! grep -q "enable_uart=1" /boot/firmware/config.txt; then if ! grep -q "enable_uart=1" /boot/firmware/config.txt; then
echo "Adding UART configuration..."
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 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 else
echo "UART configuration already set. Skipping..." warning "UART configuration already set. Skipping."
fi fi
sudo chmod 777 /dev/ttyAMA* # 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*"
#Open I2C ports # Enable I2C ports
echo "Open I2C ports" info "Enabling I2C ports..."
sudo raspi-config nonint do_i2c 0 sudo raspi-config nonint do_i2c 0
success "I2C ports enabled."
echo "Setup completed successfully!" # Completion message
success "Setup completed successfully!"
info "System will reboot in 5 seconds..."
sleep 5
sudo reboot