From 515eb303f096bcfe575cecca3d2cad233a73286a Mon Sep 17 00:00:00 2001 From: bvdberg01 <74251551+bvdberg01@users.noreply.github.com> Date: Sun, 17 Nov 2024 16:19:17 +0100 Subject: [PATCH] new scripts for NetBox --- ct/netbox.sh | 100 +++++++++++++++++++++++++++++++++ install/netbox-install.sh | 115 ++++++++++++++++++++++++++++++++++++++ json/netbox.json | 39 +++++++++++++ 3 files changed, 254 insertions(+) create mode 100644 ct/netbox.sh create mode 100644 install/netbox-install.sh create mode 100644 json/netbox.json diff --git a/ct/netbox.sh b/ct/netbox.sh new file mode 100644 index 000000000..182c488d6 --- /dev/null +++ b/ct/netbox.sh @@ -0,0 +1,100 @@ +#!/usr/bin/env bash +source <(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/build.func) +# Copyright (c) 2021-2024 community-scripts ORG +# Author: bvdberg01 +# License: MIT +# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE + +function header_info { +clear +cat <<"EOF" + __ __ + ____ ___ / /_/ /_ ____ _ __ + / __ \/ _ \/ __/ __ \/ __ \| |/_/ + / / / / __/ /_/ /_/ / /_/ /> < +/_/ /_/\___/\__/_.___/\____/_/|_| + +EOF +} +header_info +echo -e "Loading..." +APP="Netbox" +var_disk="4" +var_cpu="2" +var_ram="2048" +var_os="debian" +var_version="12" +variables +color +catch_errors + +function default_settings() { + CT_TYPE="1" + PW="" + CT_ID=$NEXTID + HN=$NSAPP + DISK_SIZE="$var_disk" + CORE_COUNT="$var_cpu" + RAM_SIZE="$var_ram" + BRG="vmbr0" + NET="dhcp" + GATE="" + APT_CACHER="" + APT_CACHER_IP="" + DISABLEIP6="no" + MTU="" + SD="" + NS="" + MAC="" + VLAN="" + SSH="no" + VERB="no" + echo_default +} + +function update_script() { +header_info +if [[ ! -f /opt/netbox/netbox/netbox/configuration.py ]]; then msg_error "No ${APP} Installation Found!"; exit; fi +RELEASE=$(curl -s https://api.github.com/repos/netbox-community/netbox/releases/latest | grep "tag_name" | awk '{print substr($2, 3, length($2)-4) }') +if [ ! -d "/opt/netbox-${RELEASE}" ]; then + msg_info "Updating $APP LXC" + apt-get update &>/dev/null + apt-get -y upgrade &>/dev/null + + OLD_VERSION_PATH=$(ls -d /opt/netbox-*/) + wget -q "https://github.com/netbox-community/netbox/archive/refs/tags/v${RELEASE}.tar.gz" + tar -xzf "v${RELEASE}.tar.gz" -C /opt + ln -sfn "/opt/netbox-${RELEASE}/" /opt/netbox + rm "v${RELEASE}.tar.gz" + + cp "${OLD_VERSION_PATH}netbox/netbox/configuration.py" /opt/netbox/netbox/netbox/ + cp -pr "${OLD_VERSION_PATH}netbox/media/" /opt/netbox/netbox/ + cp -r "${OLD_VERSION_PATH}netbox/scripts" /opt/netbox/netbox/ + cp -r "${OLD_VERSION_PATH}netbox/reports" /opt/netbox/netbox/ + cp "${OLD_VERSION_PATH}gunicorn.py" /opt/netbox/ + + if [ -d "${OLD_VERSION_PATH}local_requirements.txt" ]; then + cp "${OLD_VERSION_PATH}local_requirements.txt" /opt/netbox/ + fi + + if [ -d "${OLD_VERSION_PATH}netbox/netbox/ldap_config.py" ]; then + cp "${OLD_VERSION_PATH}netbox/netbox/ldap_config.py" /opt/netbox/netbox/netbox/ + fi + + rm -r "${OLD_VERSION_PATH}" + /opt/netbox/upgrade.sh &>/dev/null + systemctl restart --now netbox netbox-rq + msg_ok "Updated $APP LXC" +else + msg_ok "No update required. ${APP} is already at ${RELEASE}" +fi +exit +} + +start +build_container +description + +msg_ok "Completed Successfully!\n" +echo -e "${APP} should be reachable by going to the following URL. + ${BL}https://${IP}${CL} \n" diff --git a/install/netbox-install.sh b/install/netbox-install.sh new file mode 100644 index 000000000..f1c907282 --- /dev/null +++ b/install/netbox-install.sh @@ -0,0 +1,115 @@ +#!/usr/bin/env bash + +# Copyright (c) 2021-2024 community-scripts ORG +# Author: bvdberg01 +# License: MIT +# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE + +source /dev/stdin <<< "$FUNCTIONS_FILE_PATH" +color +verb_ip6 +catch_errors +setting_up_container +network_check +update_os + +msg_info "Installing Dependencies" +$STD apt-get update +$STD apt-get install -y curl +$STD apt-get install -y sudo +$STD apt-get install -y mc +$STD apt-get install -y apache2 +$STD apt-get install -y redis-server +$STD apt-get install -y postgresql +$STD apt-get install -y python3 +$STD apt-get install -y python3-pip +$STD apt-get install -y python3-venv +$STD apt-get install -y python3-dev +$STD apt-get install -y build-essential +$STD apt-get install -y libxml2-dev +$STD apt-get install -y libxslt1-dev +$STD apt-get install -y libffi-dev +$STD apt-get install -y libpq-dev +$STD apt-get install -y libssl-dev +$STD apt-get install -y zlib1g-dev +msg_ok "Installed Dependencies" + +msg_info "Setting up PostgreSQL" +DB_NAME=netbox +DB_USER=netbox +DB_PASS=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | cut -c1-13) +$STD sudo -u postgres psql -c "CREATE ROLE $DB_USER WITH LOGIN PASSWORD '$DB_PASS';" +$STD sudo -u postgres psql -c "CREATE DATABASE $DB_NAME WITH OWNER $DB_USER TEMPLATE template0;" +msg_ok "Set up PostgreSQL" + +msg_info "Installing NetBox" +RELEASE=$(curl -s https://api.github.com/repos/netbox-community/netbox/releases/latest | grep "tag_name" | awk '{print substr($2, 3, length($2)-4) }') +wget -q "https://github.com/netbox-community/netbox/archive/refs/tags/v${RELEASE}.tar.gz" +tar -xzf "v${RELEASE}.tar.gz" -C /opt +ln -s "/opt/netbox-${RELEASE}/" /opt/netbox +rm "v${RELEASE}.tar.gz" + +$STD adduser --system --group netbox +chown --recursive netbox /opt/netbox/netbox/media/ +chown --recursive netbox /opt/netbox/netbox/reports/ +chown --recursive netbox /opt/netbox/netbox/scripts/ + +cp /opt/netbox/netbox/netbox/configuration_example.py /opt/netbox/netbox/netbox/configuration.py + +secret=$(python3 /opt/netbox/netbox/generate_secret_key.py) +escaped_secret=$(printf '%s\n' "$secret" | sed 's/[&/\]/\\&/g') + +sed -i 's/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = ["*"]/' /opt/netbox/netbox/netbox/configuration.py +sed -i "s|SECRET_KEY = ''|SECRET_KEY = '${escaped_secret}'|" /opt/netbox/netbox/netbox/configuration.py +sed -i "/DATABASE = {/,/}/s/'USER': '[^']*'/'USER': '$DB_USER'/" /opt/netbox/netbox/netbox/configuration.py +sed -i "/DATABASE = {/,/}/s/'PASSWORD': '[^']*'/'PASSWORD': '$DB_PASS'/" /opt/netbox/netbox/netbox/configuration.py + +$STD /opt/netbox/upgrade.sh &>/dev/null +sudo ln -s /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping + +sudo cp /opt/netbox/contrib/apache.conf /etc/apache2/sites-available/netbox.conf +openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/netbox.key -out /etc/ssl/certs/netbox.crt -subj "/C=US/O=NetBox/OU=Certificate/CN=localhost" &>/dev/null +$STD a2enmod ssl proxy proxy_http headers rewrite +$STD a2ensite netbox +systemctl restart apache2 + +cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py +cp /opt/netbox/contrib/*.service /etc/systemd/system/ +systemctl daemon-reload +systemctl enable -q --now netbox netbox-rq + +msg_ok "Installed NetBox" + +msg_info "Setting up Django Admin" +NetBox_USER=Admin +NetBox_PASS=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | cut -c1-13) + +source /opt/netbox/venv/bin/activate +$STD python3 /opt/netbox/netbox/manage.py shell << EOF +from django.contrib.auth import get_user_model +UserModel = get_user_model() +user = UserModel.objects.create_user('$NetBox_USER', password='$NetBox_PASS') +user.is_superuser = True +user.is_staff = True +user.save() +EOF +msg_ok "Setup Django Admin" + +msg_info "Save credential file" +echo "" >~/NetBox.creds +echo "NetBox Database Credentials" >>~/NetBox.creds +echo "" >>~/NetBox.creds +echo -e "NetBox Database User: \e[32m$DB_USER\e[0m" >>~/NetBox.creds +echo -e "NetBox Database Password: \e[32m$DB_PASS\e[0m" >>~/NetBox.creds +echo -e "NetBox Database Name: \e[32m$DB_NAME\e[0m" >>~/NetBox.creds +echo -e "NetBox Admin user: \e[32m$NetBox_USER\e[0m" >>~/NetBox.creds +echo -e "NetBox Admin Password: \e[32m$NetBox_PASS\e[0m" >>~/NetBox.creds +msg_ok "Save cred file" + +motd_ssh +customize + +msg_info "Cleaning up" +$STD apt-get -y autoremove +$STD apt-get -y autoclean +msg_ok "Cleaned" diff --git a/json/netbox.json b/json/netbox.json new file mode 100644 index 000000000..52d03796f --- /dev/null +++ b/json/netbox.json @@ -0,0 +1,39 @@ +{ + "name": "NetBox", + "slug": "netbox", + "categories": [ + 11 + ], + "date_created": "2024-11-17", + "type": "ct", + "updateable": false, + "privileged": false, + "interface_port": "443", + "documentation": "https://netboxlabs.com/docs/netbox/en/stable/", + "website": "https://github.com/netbox-community/netbox", + "logo": "https://netboxlabs.com/wp-content/uploads/2023/10/logo_netboxlabs_bright_teal.svg", + "description": "NetBox is the source of truth for everything on your network, from physical components like power systems and cabling to virtual assets like IP addresses and VLANs. Network automation and observability tools depend on NetBox’s authoritative data to roll out configurations, monitor changes, and accelerate operations across the enterprise", + "install_methods": [ + { + "type": "default", + "script": "ct/netbox.sh", + "resources": { + "cpu": "2", + "ram": "2048", + "hdd": "4", + "os": "debian", + "version": "12" + } + } + ], + "default_credentials": { + "username": "Admin", + "password": "See output: cat NetBox.creds" + }, + "notes": [ + { + "text": "Generated database & admin credentials see output: `cat NetBox.creds`", + "type": "warning" + } + ] +} \ No newline at end of file