Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New script: NetBox LXC #308

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions ct/netbox.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/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
havardthom marked this conversation as resolved.
Show resolved Hide resolved
check_container_storage
check_container_resources
if [[ ! -f /etc/systemd/system/netbox.service ]]; 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 [[ ! -f /opt/${APP}_version.txt ]] || [[ "${RELEASE}" != "$(cat /opt/${APP}_version.txt)" ]]; then

msg_info "Stopping ${APP}"
systemctl stop netbox netbox-rq
msg_ok "Stopped ${APP}"

msg_info "Updating $APP to v${RELEASE}"
mv /opt/netbox/ /opt/netbox-backup
cd /opt
wget -q "https://github.com/netbox-community/netbox/archive/refs/tags/v${RELEASE}.zip"
unzip -q "v${RELEASE}.zip"
mv /opt/netbox-${RELEASE}/ /opt/netbox/

cp -r /opt/netbox-backup/netbox/netbox/configuration.py /opt/netbox/netbox/netbox/
cp -r /opt/netbox-backup/netbox/media/ /opt/netbox/netbox/
cp -r /opt/netbox-backup/netbox/scripts /opt/netbox/netbox/
cp -r /opt/netbox-backup/netbox/reports /opt/netbox/netbox/
cp -r /opt/netbox-backup/gunicorn.py /opt/netbox/

if [ -f /opt/netbox-backup/local_requirements.txt ]; then
cp -r /opt/netbox-backup/local_requirements.txt /opt/netbox/
fi

if [ -f /opt/netbox-backup/netbox/netbox/ldap_config.py ]; then
cp -r /opt/netbox-backup/netbox/netbox/ldap_config.py /opt/netbox/netbox/netbox/
fi

/opt/netbox/upgrade.sh &>/dev/null
echo "${RELEASE}" >/opt/${APP}_version.txt
msg_ok "Updated $APP to v${RELEASE}"

msg_info "Starting ${APP}"
systemctl start netbox netbox-rq
msg_ok "Started ${APP}"

msg_info "Cleaning up"
rm -r "/opt/v${RELEASE}.zip"
rm -r /opt/netbox-backup
msg_ok "Cleaned"
msg_ok "Updated Successfully"
else
msg_ok "No update required. ${APP} is already at v${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"
119 changes: 119 additions & 0 deletions install/netbox-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/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"
bvdberg01 marked this conversation as resolved.
Show resolved Hide resolved
$STD apt-get install -y \
curl \
sudo \
mc \
apache2 \
redis-server \
postgresql \
python3 \
python3-pip \
python3-venv \
python3-dev \
build-essential \
libxml2-dev \
libxslt1-dev \
libffi-dev \
libpq-dev \
libssl-dev \
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;"
{
echo "Netbox-Credentials"
echo -e "Netbox Database User: \e[32m$DB_USER\e[0m"
echo -e "Netbox Database Password: \e[32m$DB_PASS\e[0m"
echo -e "Netbox Database Name: \e[32m$DB_NAME\e[0m"
} >> ~/netbox.creds
msg_ok "Set up PostgreSQL"

msg_info "Installing NetBox (Patience)"
cd /opt
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}.zip"
unzip -q "v${RELEASE}.zip"
mv /opt/netbox-${RELEASE}/ /opt/netbox

$STD adduser --system --group netbox
havardthom marked this conversation as resolved.
Show resolved Hide resolved
chown --recursive netbox /opt/netbox/netbox/media/
chown --recursive netbox /opt/netbox/netbox/reports/
chown --recursive netbox /opt/netbox/netbox/scripts/

mv /opt/netbox/netbox/netbox/configuration_example.py /opt/netbox/netbox/netbox/configuration.py
bvdberg01 marked this conversation as resolved.
Show resolved Hide resolved

SECRET_KEY=$(python3 /opt/netbox/netbox/generate_secret_key.py)
ESCAPED_SECRET_KEY=$(printf '%s\n' "$SECRET_KEY" | 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_KEY}'|" /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
ln -s /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping

mv /opt/netbox/contrib/apache.conf /etc/apache2/sites-available/netbox.conf
$STD 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"
$STD a2enmod ssl proxy proxy_http headers rewrite
$STD a2ensite netbox
systemctl restart apache2

mv /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py
mv /opt/netbox/contrib/*.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable -q --now netbox netbox-rq

echo "${RELEASE}" >/opt/${APPLICATION}_version.txt
echo -e "Netbox Secret: \e[32m$SECRET_KEY\e[0m" >> ~/netbox.creds
msg_ok "Installed NetBox"

msg_info "Setting up Django Admin"
DJANGO_USER=Admin
DJANGO_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('$DJANGO_USER', password='$DJANGO_PASS')
user.is_superuser = True
user.is_staff = True
user.save()
EOF
{
echo ""
echo "Netbox-Django-Credentials"
echo -e "Django User: \e[32m$DJANGO_USER\e[0m"
echo -e "Django Password: \e[32m$DJANGO_PASS\e[0m"
} >> ~/netbox.creds
bvdberg01 marked this conversation as resolved.
Show resolved Hide resolved
msg_ok "Setup Django Admin"

motd_ssh
customize

msg_info "Cleaning up"
rm "/opt/v${RELEASE}.zip"
$STD apt-get -y autoremove
$STD apt-get -y autoclean
msg_ok "Cleaned"
39 changes: 39 additions & 0 deletions json/netbox.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "NetBox",
"slug": "netbox",
"categories": [
11
],
"date_created": "2024-11-17",
"type": "ct",
"updateable": true,
"privileged": false,
"interface_port": "443",
"documentation": "https://netboxlabs.com/docs/netbox/en/stable/",
"website": "https://netboxlabs.com/",
"logo": "https://github.com/netbox-community/netbox/wiki/images/logos/netbox_icon.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": null,
"password": null
},
"notes": [
{
"text": "Show login and database credentials: `cat netbox.creds`",
"type": "info"
}
]
}