install-scripts/netbox/install.sh

201 lines
6.9 KiB
Bash
Raw Normal View History

#!/bin/bash
# Check if running as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Update and upgrade the system
echo "Updating and upgrading the system..."
apt update && apt upgrade -y
# Install dependencies
echo "Installing dependencies..."
apt install -y python3 python3-pip python3-dev build-essential libxml2 libxml2-dev libxslt1-dev libffi-dev graphviz git libpq-dev libssl-dev redis-server postgresql postgresql-contrib nginx
# Check Python version
echo "Checking Python version. Please ensure this meets the minimum requirements of Python 3.8, 3.9, 3.10 or 3.11.:"
python3 --version
# Prompt user to confirm if the currently installed Python version meets the minimum requirements
read -p "Does the installed Python version meet the minimum requirements of Python 3.8, 3.9, 3.10 or 3.11.? (y/n): " PYTHON_CONFIRM
if [[ $PYTHON_CONFIRM != "y" ]]; then
2024-04-16 15:04:24 +00:00
echo "Minimum Python version requirement confirmed!"
exit 0
fi
2024-04-15 22:16:11 +00:00
# Check other system requirements
echo "Checking the system meets the minimum requirements..."
2024-04-15 22:16:11 +00:00
RAM=$(free -m | awk '/Mem/ {print $2}')
CPU=$(grep -c ^processor /proc/cpuinfo)
DISK=$(df -h / | awk 'NR==2 {print $4}')
MIN_RAM=2048 # Minimum RAM in MB
MIN_CPU=2 # Minimum CPU cores
MIN_DISK=10 # Minimum free disk space in GB
# Check RAM
if [ "$RAM" -lt "$MIN_RAM" ]; then
echo "Error: Insufficient RAM. At least $MIN_RAM MB of RAM is required."
exit 1
fi
# Check CPU cores
if [ "$CPU" -lt "$MIN_CPU" ]; then
echo "Error: Insufficient CPU cores. At least $MIN_CPU CPU cores are required."
exit 1
fi
# Check disk space
DISK_NUM=$(echo "$DISK" | grep -o -E '[0-9]+')
DISK_UNIT=$(echo "$DISK" | grep -o -E '[A-Za-z]+')
if [ "$DISK_UNIT" == "G" ]; then
if [ "$DISK_NUM" -lt "$MIN_DISK" ]; then
echo "Error: Insufficient disk space. At least $MIN_DISK GB of free disk space is required."
exit 1
fi
else
echo "Error: Unsupported disk space unit. Please ensure disk space is at least 10GB."
2024-04-15 22:16:11 +00:00
exit 1
fi
echo "System meets minimum requirements. Proceeding with installation..."
2024-11-23 19:45:16 +00:00
# Download the latest version of netbox and unzip it
echo "Downloading latest version of Netbox and moving files..."
sudo wget https://github.com/netbox-community/netbox/archive/refs/tags/vX.Y.Z.tar.gz
sudo tar -xzf vX.Y.Z.tar.gz -C /opt
sudo ln -s /opt/netbox-X.Y.Z/ /opt/netbox
# Prompt user for PostgreSQL and Netbox password
read -p "Enter PostgreSQL password for 'netbox' user: " PG_PASSWORD
read -p "Enter password for Netbox application: " NETBOX_PASSWORD
# Create PostgreSQL user and database
echo "Creating PostgreSQL user and database..."
sudo -u postgres psql -c "CREATE DATABASE netbox;"
sudo -u postgres psql -c "CREATE USER netbox WITH PASSWORD '$PG_PASSWORD';"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;"
# Create new user for Netbox
echo "Creating new user for Netbox..."
useradd -m -s /bin/bash netbox
# Install Netbox
echo "Installing Netbox..."
apt update
apt install -y netbox
# Move into NetBox configuration directory and make a copy of configuration_example.py
echo "Copying Netbox configuration file..."
cd /opt/netbox/netbox/netbox/
cp configuration.example.py configuration.py
# Populate database fields in configuration.py with PostgreSQL information
echo "Populating the Netbox configuration file with prior PostgreSQL information..."
sed -i "s/'USER': '',/'USER': 'netbox',/g" configuration.py
sed -i "s/'NAME': '',/'NAME': 'netbox',/g" configuration.py
sed -i "s/'PASSWORD': '',/'PASSWORD': '$PG_PASSWORD',/g" configuration.py
sed -i "s/'HOST': '',/'HOST': 'localhost',/g" configuration.py
# Generate secret key and parse it into configuration.py
echo "Generating secret key..."
SECRET_KEY=$(python3 /opt/netbox/generate_secret_key.py)
sed -i "s/SECRET_KEY = ''/SECRET_KEY = '$SECRET_KEY'/g" configuration.py
# Prompt user to enter ALLOWED_HOSTS
echo "Please enter manually input the following inforation..."
read -p "Enter ALLOWED_HOSTS (separated by commas): " ALLOWED_HOSTS
# Parse user input into configuration.py
echo "Parsing the entered information to configuration.py..."
sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \[$ALLOWED_HOSTS\]/g" configuration.py
sed -i "s/SECRET_KEY = ''/SECRET_KEY = '$SECRET_KEY'/g" configuration.py
# Perform initial Netbox database migration
echo "Performing initial Netbox database migration..."
sudo -u netbox /opt/netbox/upgrade.sh
# Create directory for Netbox reports
echo "Creating directory for Netbox reports..."
mkdir -p /opt/netbox/netbox/media/reports
# Check if directories exist
echo "Checking if directories exist..."
if [ ! -d "/opt/netbox/netbox/media/reports" ] || [ ! -d "/opt/netbox/netbox/media/" ] || [ ! -d "/opt/netbox/netbox/static/" ]; then
read -p "Required directories are missing. Do you want to create them? (y/n): " CREATE_DIRS
if [[ $CREATE_DIRS == "y" ]]; then
mkdir -p /opt/netbox/netbox/media/reports
mkdir -p /opt/netbox/netbox/media/
mkdir -p /opt/netbox/netbox/static/
else
echo "Please create the required directories manually and rerun the script."
exit 1
fi
fi
# Collect static files
echo "Collecting static files..."
sudo -u netbox /opt/netbox/manage.py collectstatic --no-input
# Enable plugin support
echo "Enabling plugin support..."
echo "PLUGINS = []" >> /opt/netbox/netbox/configuration.py
# Prompt user to enter plugin names
read -p "Enter names of plugins to add (separated by commas, leave empty to skip): " PLUGIN_NAMES
# Add plugins to configuration file
if [ -n "$PLUGIN_NAMES" ]; then
IFS=',' read -ra PLUGINS <<< "$PLUGIN_NAMES"
for PLUGIN in "${PLUGINS[@]}"; do
echo "Adding plugin: $PLUGIN"
echo " '$PLUGIN'," >> /opt/netbox/netbox/configuration.py
done
fi
2024-04-16 15:04:24 +00:00
# Prompt user to create a new superuser
echo "Please create a new superuser for Netbox:"
sudo -u netbox /opt/netbox/manage.py createsuperuser
# Set file and directory permissions
echo "Setting file permissions..."
chown -R netbox:netbox /opt/netbox/netbox/media
chown -R netbox:netbox /opt/netbox/netbox/static
chown -R netbox:netbox /opt/netbox/netbox/media/reports
# Enable and start services
echo "Enabling and starting services..."
systemctl enable netbox
systemctl enable redis-server
systemctl enable postgresql
systemctl start netbox
systemctl start redis-server
systemctl start postgresql
# NGINX reverse proxy config
echo "Example NGINX reverse proxy configuration for Netbox:"
echo "
server {
listen 80;
server_name netbox.example.com;
location / {
proxy_pass http://localhost:8001;
proxy_set_header X-Forwarded-Host \$server_name;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
location /static/ {
alias /opt/netbox/netbox/static/;
}
location /media/ {
alias /opt/netbox/netbox/media/;
}
}
"
echo "Netbox installation completed successfully!"