Skip to content

Commit 0067a2e

Browse files
new scripts for NetBox (#308)
* new scripts for NetBox * Update json/netbox.json Co-Authored-By: Håvard Gjøby Thom <[email protected]> * Fixed issues after pr --------- Co-authored-by: Håvard Gjøby Thom <[email protected]>
1 parent ad18c05 commit 0067a2e

File tree

3 files changed

+272
-0
lines changed

3 files changed

+272
-0
lines changed

ct/netbox.sh

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env bash
2+
source <(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/build.func)
3+
# Copyright (c) 2021-2024 community-scripts ORG
4+
# Author: bvdberg01
5+
# License: MIT
6+
# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
7+
8+
function header_info {
9+
clear
10+
cat <<"EOF"
11+
_ __ __ ____
12+
/ | / /__ / /_/ __ )____ _ __
13+
/ |/ / _ \/ __/ __ / __ \| |/_/
14+
/ /| / __/ /_/ /_/ / /_/ /> <
15+
/_/ |_/\___/\__/_____/\____/_/|_|
16+
17+
EOF
18+
}
19+
header_info
20+
echo -e "Loading..."
21+
APP="NetBox"
22+
var_disk="4"
23+
var_cpu="2"
24+
var_ram="2048"
25+
var_os="debian"
26+
var_version="12"
27+
variables
28+
color
29+
catch_errors
30+
31+
function default_settings() {
32+
CT_TYPE="1"
33+
PW=""
34+
CT_ID=$NEXTID
35+
HN=$NSAPP
36+
DISK_SIZE="$var_disk"
37+
CORE_COUNT="$var_cpu"
38+
RAM_SIZE="$var_ram"
39+
BRG="vmbr0"
40+
NET="dhcp"
41+
GATE=""
42+
APT_CACHER=""
43+
APT_CACHER_IP=""
44+
DISABLEIP6="no"
45+
MTU=""
46+
SD=""
47+
NS=""
48+
MAC=""
49+
VLAN=""
50+
SSH="no"
51+
VERB="no"
52+
echo_default
53+
}
54+
55+
function update_script() {
56+
header_info
57+
check_container_storage
58+
check_container_resources
59+
if [[ ! -f /etc/systemd/system/netbox.service ]]; then msg_error "No ${APP} Installation Found!"; exit; fi
60+
61+
RELEASE=$(curl -s https://api.github.com/repos/netbox-community/netbox/releases/latest | grep "tag_name" | awk '{print substr($2, 3, length($2)-4) }')
62+
if [[ ! -f /opt/${APP}_version.txt ]] || [[ "${RELEASE}" != "$(cat /opt/${APP}_version.txt)" ]]; then
63+
64+
msg_info "Stopping ${APP}"
65+
systemctl stop netbox netbox-rq
66+
msg_ok "Stopped ${APP}"
67+
68+
msg_info "Updating $APP to v${RELEASE}"
69+
mv /opt/netbox/ /opt/netbox-backup
70+
cd /opt
71+
wget -q "https://github.com/netbox-community/netbox/archive/refs/tags/v${RELEASE}.zip"
72+
unzip -q "v${RELEASE}.zip"
73+
mv /opt/netbox-${RELEASE}/ /opt/netbox/
74+
75+
cp -r /opt/netbox-backup/netbox/netbox/configuration.py /opt/netbox/netbox/netbox/
76+
cp -r /opt/netbox-backup/netbox/media/ /opt/netbox/netbox/
77+
cp -r /opt/netbox-backup/netbox/scripts /opt/netbox/netbox/
78+
cp -r /opt/netbox-backup/netbox/reports /opt/netbox/netbox/
79+
cp -r /opt/netbox-backup/gunicorn.py /opt/netbox/
80+
81+
if [ -f /opt/netbox-backup/local_requirements.txt ]; then
82+
cp -r /opt/netbox-backup/local_requirements.txt /opt/netbox/
83+
fi
84+
85+
if [ -f /opt/netbox-backup/netbox/netbox/ldap_config.py ]; then
86+
cp -r /opt/netbox-backup/netbox/netbox/ldap_config.py /opt/netbox/netbox/netbox/
87+
fi
88+
89+
/opt/netbox/upgrade.sh &>/dev/null
90+
echo "${RELEASE}" >/opt/${APP}_version.txt
91+
msg_ok "Updated $APP to v${RELEASE}"
92+
93+
msg_info "Starting ${APP}"
94+
systemctl start netbox netbox-rq
95+
msg_ok "Started ${APP}"
96+
97+
msg_info "Cleaning up"
98+
rm -r "/opt/v${RELEASE}.zip"
99+
rm -r /opt/netbox-backup
100+
msg_ok "Cleaned"
101+
msg_ok "Updated Successfully"
102+
else
103+
msg_ok "No update required. ${APP} is already at v${RELEASE}"
104+
fi
105+
exit
106+
}
107+
108+
start
109+
build_container
110+
description
111+
112+
msg_ok "Completed Successfully!\n"
113+
echo -e "${APP} should be reachable by going to the following URL.
114+
${BL}https://${IP}${CL} \n"

install/netbox-install.sh

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright (c) 2021-2024 community-scripts ORG
4+
# Author: bvdberg01
5+
# License: MIT
6+
# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
7+
8+
source /dev/stdin <<< "$FUNCTIONS_FILE_PATH"
9+
color
10+
verb_ip6
11+
catch_errors
12+
setting_up_container
13+
network_check
14+
update_os
15+
16+
msg_info "Installing Dependencies"
17+
$STD apt-get install -y \
18+
curl \
19+
sudo \
20+
mc \
21+
apache2 \
22+
redis-server \
23+
postgresql \
24+
python3 \
25+
python3-pip \
26+
python3-venv \
27+
python3-dev \
28+
build-essential \
29+
libxml2-dev \
30+
libxslt1-dev \
31+
libffi-dev \
32+
libpq-dev \
33+
libssl-dev \
34+
zlib1g-dev
35+
msg_ok "Installed Dependencies"
36+
37+
msg_info "Setting up PostgreSQL"
38+
DB_NAME=netbox
39+
DB_USER=netbox
40+
DB_PASS=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | cut -c1-13)
41+
$STD sudo -u postgres psql -c "CREATE ROLE $DB_USER WITH LOGIN PASSWORD '$DB_PASS';"
42+
$STD sudo -u postgres psql -c "CREATE DATABASE $DB_NAME WITH OWNER $DB_USER TEMPLATE template0;"
43+
{
44+
echo "Netbox-Credentials"
45+
echo -e "Netbox Database User: \e[32m$DB_USER\e[0m"
46+
echo -e "Netbox Database Password: \e[32m$DB_PASS\e[0m"
47+
echo -e "Netbox Database Name: \e[32m$DB_NAME\e[0m"
48+
} >> ~/netbox.creds
49+
msg_ok "Set up PostgreSQL"
50+
51+
msg_info "Installing NetBox (Patience)"
52+
cd /opt
53+
RELEASE=$(curl -s https://api.github.com/repos/netbox-community/netbox/releases/latest | grep "tag_name" | awk '{print substr($2, 3, length($2)-4) }')
54+
wget -q "https://github.com/netbox-community/netbox/archive/refs/tags/v${RELEASE}.zip"
55+
unzip -q "v${RELEASE}.zip"
56+
mv /opt/netbox-${RELEASE}/ /opt/netbox
57+
58+
$STD adduser --system --group netbox
59+
chown --recursive netbox /opt/netbox/netbox/media/
60+
chown --recursive netbox /opt/netbox/netbox/reports/
61+
chown --recursive netbox /opt/netbox/netbox/scripts/
62+
63+
mv /opt/netbox/netbox/netbox/configuration_example.py /opt/netbox/netbox/netbox/configuration.py
64+
65+
SECRET_KEY=$(python3 /opt/netbox/netbox/generate_secret_key.py)
66+
ESCAPED_SECRET_KEY=$(printf '%s\n' "$SECRET_KEY" | sed 's/[&/\]/\\&/g')
67+
68+
sed -i 's/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = ["*"]/' /opt/netbox/netbox/netbox/configuration.py
69+
sed -i "s|SECRET_KEY = ''|SECRET_KEY = '${ESCAPED_SECRET_KEY}'|" /opt/netbox/netbox/netbox/configuration.py
70+
sed -i "/DATABASE = {/,/}/s/'USER': '[^']*'/'USER': '$DB_USER'/" /opt/netbox/netbox/netbox/configuration.py
71+
sed -i "/DATABASE = {/,/}/s/'PASSWORD': '[^']*'/'PASSWORD': '$DB_PASS'/" /opt/netbox/netbox/netbox/configuration.py
72+
73+
$STD /opt/netbox/upgrade.sh
74+
ln -s /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping
75+
76+
mv /opt/netbox/contrib/apache.conf /etc/apache2/sites-available/netbox.conf
77+
$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"
78+
$STD a2enmod ssl proxy proxy_http headers rewrite
79+
$STD a2ensite netbox
80+
systemctl restart apache2
81+
82+
mv /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py
83+
mv /opt/netbox/contrib/*.service /etc/systemd/system/
84+
systemctl daemon-reload
85+
systemctl enable -q --now netbox netbox-rq
86+
87+
echo "${RELEASE}" >/opt/${APPLICATION}_version.txt
88+
echo -e "Netbox Secret: \e[32m$SECRET_KEY\e[0m" >> ~/netbox.creds
89+
msg_ok "Installed NetBox"
90+
91+
msg_info "Setting up Django Admin"
92+
DJANGO_USER=Admin
93+
DJANGO_PASS=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | cut -c1-13)
94+
95+
source /opt/netbox/venv/bin/activate
96+
$STD python3 /opt/netbox/netbox/manage.py shell << EOF
97+
from django.contrib.auth import get_user_model
98+
UserModel = get_user_model()
99+
user = UserModel.objects.create_user('$DJANGO_USER', password='$DJANGO_PASS')
100+
user.is_superuser = True
101+
user.is_staff = True
102+
user.save()
103+
EOF
104+
{
105+
echo ""
106+
echo "Netbox-Django-Credentials"
107+
echo -e "Django User: \e[32m$DJANGO_USER\e[0m"
108+
echo -e "Django Password: \e[32m$DJANGO_PASS\e[0m"
109+
} >> ~/netbox.creds
110+
msg_ok "Setup Django Admin"
111+
112+
motd_ssh
113+
customize
114+
115+
msg_info "Cleaning up"
116+
rm "/opt/v${RELEASE}.zip"
117+
$STD apt-get -y autoremove
118+
$STD apt-get -y autoclean
119+
msg_ok "Cleaned"

json/netbox.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "NetBox",
3+
"slug": "netbox",
4+
"categories": [
5+
11
6+
],
7+
"date_created": "2024-11-17",
8+
"type": "ct",
9+
"updateable": true,
10+
"privileged": false,
11+
"interface_port": "443",
12+
"documentation": "https://netboxlabs.com/docs/netbox/en/stable/",
13+
"website": "https://netboxlabs.com/",
14+
"logo": "https://github.com/netbox-community/netbox/wiki/images/logos/netbox_icon.svg",
15+
"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",
16+
"install_methods": [
17+
{
18+
"type": "default",
19+
"script": "ct/netbox.sh",
20+
"resources": {
21+
"cpu": "2",
22+
"ram": "2048",
23+
"hdd": "4",
24+
"os": "debian",
25+
"version": "12"
26+
}
27+
}
28+
],
29+
"default_credentials": {
30+
"username": null,
31+
"password": null
32+
},
33+
"notes": [
34+
{
35+
"text": "Show login and database credentials: `cat netbox.creds`",
36+
"type": "info"
37+
}
38+
]
39+
}

0 commit comments

Comments
 (0)