-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
89 lines (67 loc) · 2.19 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
set -e
manage() {
sudo -HEu netbox ./manage.py "$@"
}
setup_environment_variables() {
ALLOWED_HOSTS=${ALLOWED_HOSTS:-localhost}
DB_NAME=${DB_NAME:-netbox}
DB_USER=${DB_USER:-netbox}
DB_PASS=${DB_PASS:-netbox}
DB_HOST=${DB_HOST:-db}
DB_PORT=${DB_PORT:-5432}
LOGIN_REQUIRED=${LOGIN_REQUIRED:-False}
: "${SECRET_KEY:?SECRET_KEY needs to be set}"
}
initialize_config() {
pushd netbox/ 2>&1 > /dev/null
cp configuration{.example,}.py
# Update allowed hosts
local allowed_hosts_list="$ALLOWED_HOSTS"
read -ra allowed_hosts_list <<<"$allowed_hosts_list"
local allowed_hosts=""
for host in "${allowed_hosts_list[@]}"; do
allowed_hosts="$allowed_hosts '$host',"
done
sed -i "/^ALLOWED_HOSTS =/c\\ALLOWED_HOSTS = [$allowed_hosts ]" configuration.py
# Update DB configuration
sed -i "/# PostgreSQL username/c\\ 'NAME': '$DB_NAME'," configuration.py
sed -i "/# PostgreSQL username/c\\ 'USER': '$DB_USER'," configuration.py
sed -i "/# PostgreSQL password/c\\ 'PASSWORD': '$DB_PASS'," configuration.py
sed -i "/# Database server/c\\ 'HOST': '$DB_HOST'," configuration.py
sed -i "/# Database port/c\\ 'PORT': '$DB_PORT'," configuration.py
# Update secret key
sed -i "/^SECRET_KEY = '/c\\SECRET_KEY = '$SECRET_KEY'" configuration.py
# Login required
sed -i "/^LOGIN_REQUIRED = /c\\LOGIN_REQUIRED = $LOGIN_REQUIRED" configuration.py
popd 2>&1 > /dev/null
}
check_db_connection() {
cmd=$(find /usr/lib/postgresql/ -name pg_isready)
cmd="$cmd -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -t 1"
timeout=30
while ! $cmd >/dev/null 2>&1; do
timeout=$(expr $timeout - 1)
if [[ $timeout -eq 0 ]]; then
echo
echo "Could not connect to database. Terminating."
return 1
fi
echo -n "."
sleep 1
done
echo
}
# Setup
setup_environment_variables
initialize_config
if [[ "$1" != "/"* ]]; then
# Check for database
check_db_connection
# Migrate database
manage migrate
# Collect static files
manage collectstatic --no-input
exec sudo -HEu netbox ./manage.py "$@"
fi
exec "$@"