Last active
November 5, 2022 07:01
-
-
Save ojpojao/4f83203a041282d30b98fa45717ab875 to your computer and use it in GitHub Desktop.
Realiza a instalação do NetBox com https
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Tentativa de automatizar(porcamente) a instalação do Netbox kkkkkkk | |
# João Paulo dos Santos <[email protected]> | |
# Algum dia de outubro de 2022 | |
# chmod +x install_netbox.sh | |
# execute com "sudo ./install_netbox.sh" | |
set -xe | |
# /opt/netbox/netbox/netbox/configuration.py | |
export DB_NETBOX_NAME="netbox" | |
export DB_NETBOX_USER="netbox" | |
export DB_NETBOX_PASS="J5brHrAXFLQSif0K" | |
export PSQL_MAJOR_VERSION="12" | |
export TIMEZONE="America\/Belem" | |
# webuser | |
export DJANGO_SUPERUSER_USERNAME="admin" | |
export DJANGO_SUPERUSER_PASSWORD="senha" | |
export DJANGO_SUPERUSER_EMAIL="[email protected]" | |
# ssl | |
export SSL_COUNTRY_NAME="BR" | |
export SSL_PROVINCE_NAME="PARA" | |
export SSL_LOCALITY_NAME="ANANINDEUA" | |
export SSL_ORGANIZATION_NAME="JOAO TRANQUEIRAS LTDA" | |
export SSL_ORGANIZATION_UNIT="" | |
export SSL_COMMON_NAME="" | |
export SSL_EMAIL_ADDRESS="[email protected]" | |
# pacotes de base e dependências | |
apt update && apt install -y \ | |
git \ | |
gnupg2 \ | |
python3 \ | |
python3-pip \ | |
python3-venv \ | |
python3-dev \ | |
build-essential \ | |
libxml2-dev \ | |
libxslt1-dev \ | |
libffi-dev \ | |
libpq-dev \ | |
libssl-dev \ | |
zlib1g-dev \ | |
lolcat | |
## Setup Postgresql. | |
## NetBox só suporta PostgreSQL | |
echo "Instalando PostgreSQL" | /usr/games/lolcat | |
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' | |
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - | |
apt update && \ | |
apt -y install \ | |
postgresql-$PSQL_MAJOR_VERSION \ | |
postgresql-client-$PSQL_MAJOR_VERSION | |
systemctl enable --now postgresql@$PSQL_MAJOR_VERSION-main.service | |
sleep 2 | |
echo -n "Versão PostgreSQL instalada: " | /usr/games/lolcat | |
psql -V | awk '{print $2,$3}' | /usr/games/lolcat | |
sudo -u postgres psql -c "CREATE DATABASE $DB_NETBOX_NAME;" || true | |
sudo -u postgres psql -c "CREATE USER $DB_NETBOX_USER WITH PASSWORD '$DB_NETBOX_PASS';" || true | |
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NETBOX_NAME TO $DB_NETBOX_USER;" || true | |
echo "Instalação Postgres finalizada!" | /usr/games/lolcat | |
# installig redis-server | |
echo "Instalando redis" | /usr/games/lolcat | |
apt install -y redis-server | |
redis-server -v | |
redis-cli ping | |
echo "Instalação redis finalizada!" | /usr/games/lolcat | |
# installing netbox | |
echo "Instalando NetBox" | /usr/games/lolcat | |
mkdir -p /opt/netbox | |
cd /opt/netbox | |
git clone -b master --depth 1 https://github.com/netbox-community/netbox.git . || true | |
adduser --system --group netbox | |
chown --recursive netbox /opt/netbox/netbox/media/ | |
cd /opt/netbox/netbox/netbox/ | |
cp configuration_example.py configuration.py | |
sed -i "s/^ALLOWED_HOSTS[[:space:]]=[[:space:]]\[\]/ALLOWED_HOSTS = ['*']/" configuration.py | |
sed -i "s/\(^[[:space:]]\{4\}'USER':[[:space:]]\)\(''\)/\1'${DB_NETBOX_USER}'/" configuration.py | |
sed -i "s/\(^[[:space:]]\{4\}'PASSWORD':[[:space:]]\)\(''\)/\1'${DB_NETBOX_PASS}'/" configuration.py | |
sed -i "s/\(^TIME_ZONE[[:space:]]=[[:space:]]\)\('.*'\)/\1'${TIMEZONE}'/" configuration.py | |
## secret key | |
SECRET_KEY=$(python3 ../generate_secret_key.py) | |
sleep 2 | |
echo "Secret Key is: ${SECRET_KEY}" | /usr/games/lolcat | |
sed -i "s/\(^SECRET_KEY[[:space:]]=[[:space:]]\)\('.*'\)/\1'${SECRET_KEY}'/" configuration.py | |
## upgrade netbox | |
echo "Atualizando netbox" | /usr/games/lolcat | |
sudo /opt/netbox/upgrade.sh | |
## create superuser | |
echo "Criando usuário web" | /usr/games/lolcat | |
source /opt/netbox/venv/bin/activate | |
cd /opt/netbox/netbox | |
python3 manage.py createsuperuser --noinput | |
ln -s /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping | |
# gunicorn | |
cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py | |
# systemd | |
cp -v /opt/netbox/contrib/*.service /etc/systemd/system/ | |
systemctl daemon-reload | |
systemctl start netbox netbox-rq | |
systemctl enable netbox netbox-rq | |
# nginx https | |
## openssl | |
echo "Criando chaves SSL" | /usr/games/lolcat | |
openssl req \ | |
-new \ | |
-newkey rsa:4096 \ | |
-days 365 \ | |
-nodes \ | |
-x509 \ | |
-subj "/C=$SSL_COUNTRY_NAME/ST=$SSL_PROVINCE_NAME/L=$SSL_LOCALITY_NAME/O=$SSL_ORGANIZATION_NAME/OU=$SSL_ORGANIZATION_UNIT/CN=$SSL_COMMON_NAME" \ | |
-keyout /etc/ssl/private/netbox.key \ | |
-out /etc/ssl/certs/netbox.crt | |
# -keyout ~/netbox.key \ | |
# -out ~/netbox.crt | |
## nginx | |
echo "Instalando NGINX" | /usr/games/lolcat | |
apt install -y nginx | |
cp /opt/netbox/contrib/nginx.conf /etc/nginx/sites-available/netbox | |
rm /etc/nginx/sites-enabled/default | |
ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-enabled/netbox | |
systemctl restart nginx | |
systemctl status nginx netbox netbox-rq --no-pager | /usr/games/lolcat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment