180 lines
6.3 KiB
Bash
180 lines
6.3 KiB
Bash
#!/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
|
|
echo "Please install the required version of Python and rerun this script."
|
|
exit 1
|
|
fi
|
|
|
|
# Check other system requirements
|
|
echo "Checking other system requirements..."
|
|
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 in GB."
|
|
exit 1
|
|
fi
|
|
|
|
echo "System meets minimum requirements. Proceeding with installation..."
|
|
|
|
# Add Netbox repository
|
|
echo "Adding Netbox repository..."
|
|
echo "deb https://packagecloud.io/netbox-community/netbox/ubuntu/ $(lsb_release -sc) main" | tee -a /etc/apt/sources.list.d/netbox-community.list
|
|
curl -L https://packagecloud.io/netbox-community/netbox/gpgkey | apt-key add -
|
|
|
|
# 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
|
|
|
|
# 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!" |