Skip to content

Instantly share code, notes, and snippets.

@gauravsabharwal
Last active October 4, 2018 18:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gauravsabharwal/c05684d99b7ae28f2e48a096c6589082 to your computer and use it in GitHub Desktop.
Save gauravsabharwal/c05684d99b7ae28f2e48a096c6589082 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# This script is used to setup netbox on a server running CentOS.
# Details at http://www.opentechshed.com/deploying-netbox-digital-ocean
#
### User Modifiable Variables ###
LOGFILE="/tmp/netbox-install.log"
PGCONF="/var/lib/pgsql/10/data/pg_hba.conf"
DBPASS=`date +%s | sha256sum | base64 | head -c 32`
NBADMIN='admin'
NBADMINEMAIL='admin@example.com'
NBPASS=$(date +%s | sha256sum | base64 | head -c 32)
export PIP_DISABLE_PIP_VERSION_CHECK=1
### End User modifiable variables ###
exec > >(tee "$LOGFILE") 2>&1
### Start PostgreSQL Installation and Configuration
#Check postgres installed
if rpm -qa | grep postgresql-server; then
echo "PostgreSQL is already installed on the system. Can't proceed"
exit 1
fi
echo "Installing and configuring PostgreSQL"
# Install all required postgresql packages
rpm -Uvh https://yum.postgresql.org/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
yum install -y postgresql10 postgresql10-server postgresql10-devel python-psycopg2
/usr/pgsql-10/bin/postgresql-10-setup initdb
sed -i 's/\(^host *all *all *127.0.0.1\/32 *\)ident/\1md5/' $PGCONF
sed -i 's/\(^host *all *all *::1\/128 *\)ident/\1md5/' $PGCONF
systemctl enable postgresql-10
systemctl start postgresql-10
### Create netbox database and configure AA
echo "Creating database netbox, with username: netbox and password: $DBPASS"
#DBPASS=`date +%s | sha256sum | base64 | head -c 32`
su - postgres -c "psql -c \"CREATE DATABASE netbox\""
su - postgres -c "psql -c \"CREATE USER netbox WITH PASSWORD '$DBPASS'\""
su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox\"";
### Start netbox installation and configuration
yum install -y epel-release
yum install -y gcc python2 python-devel python-pip libxml2-devel libxslt-devel libffi-devel graphviz openssl-devel git
mkdir /home/netbox
cd /home/netbox
# Clone the repository from github
git clone -b master https://github.com/digitalocean/netbox.git .
# pip 10 doesn't handles dependencies well. pip upgrade disabled
# pip install --upgrade pip
# Install all Python dependancies
pip install -r requirements.txt
# Disable selinux
setenforce 0
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
# Configure netbox
cd netbox/netbox/
NBCONFIG='configuration.py'
cp configuration.example.py $NBCONFIG
SECRETKEY=$(python /home/netbox/netbox/generate_secret_key.py)
export DROPLETIP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
sed -i "s/^ALLOWED_HOSTS.*/ALLOWED_HOSTS = ['$DROPLETIP']/" $NBCONFIG
sed -i "s/.*USER.*PostgreSQL username$/'USER': 'netbox',/" $NBCONFIG
sed -i "s/.*PASSWORD.*PostgreSQL password$/'PASSWORD': '$DBPASS',/" $NBCONFIG
sed -i "s/^SECRET_KEY.*/SECRET_KEY='$SECRETKEY'/" $NBCONFIG
sed -i "s/^LOGIN_REQUIRED.*/LOGIN_REQUIRED = 'True'/" $NBCONFIG
# Setup netbox database and add user
cd /home/netbox/netbox/
python manage.py migrate
echo "from django.contrib.auth.models import User; User.objects.create_superuser('$NBADMIN', '$NBADMINEMAIL', '$NBPASS')" | ./manage.py shell
./manage.py collectstatic --noinput
./manage.py loaddata initial_data
# Install and configure Apache
yum -y install httpd
systemctl enable httpd
DROPLETNAME=$(curl -s http://169.254.169.254/metadata/v1/hostname)
echo "
Listen 8000
<VirtualHost *:8000>
ProxyPreserveHost On
ServerName $DROPLETNAME
Alias /static /home/netbox/netbox/static
<Directory /home/netbox/netbox/static>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
<Location /static>
ProxyPass !
</Location>
ProxyPass / http://127.0.0.1:8001/
ProxyPassReverse / http://127.0.0.1:8001/
</VirtualHost>
" > /etc/httpd/conf.d/netbox.conf
systemctl restart httpd
# Install gunicorn and supervisord
pip install gunicorn
yum -y install supervisor
echo "
command = '/usr/bin/gunicorn'
pythonpath = '/home/netbox/netbox'
bind = '127.0.0.1:8001'
workers = 3
user = 'apache'
" > /home/netbox/gunicorn_config.py
echo "
[program:netbox]
command = gunicorn -c /home/netbox/gunicorn_config.py netbox.wsgi
directory = /home/netbox/netbox/
user = apache
" > /etc/supervisord.d/netbox.ini
systemctl enable supervisord
systemctl start supervisord
echo "Install Complete
DROPLETNAME: $DROPLETNAME
DROPLETIP: $DROPLETIP
Database Password: $DBPASS
Netbox Admin: $NBADMIN
Netbox Email: $NBADMINEMAIL
Netbox Password: $NBPASS
" >> /root/netbox-info.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment