-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
191 lines (161 loc) · 4.97 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/sh
function runas_librenms() {
su - librenms -s /bin/sh -c "$1"
}
TZ=${TZ:-"UTC"}
PUID=${PUID:-1000}
PGID=${PGID:-1000}
MEMORY_LIMIT=${MEMORY_LIMIT:-"256M"}
UPLOAD_MAX_SIZE=${UPLOAD_MAX_SIZE:-"16M"}
OPCACHE_MEM_SIZE=${OPCACHE_MEM_SIZE:-"128"}
LIBRENMS_POLLER_THREADS=${LIBRENMS_POLLER_THREADS:-"16"}
LIBRENMS_SNMP_COMMUNITY=${LIBRENMS_SNMP_COMMUNITY:-"librenmsdocker"}
DB_PORT=${DB_PORT:-"3306"}
DB_NAME=${DB_NAME:-"librenms"}
DB_USER=${DB_USER:-"librenms"}
DB_PASSWORD=${DB_PASSWORD:-"asupersecretpassword"}
# Timezone
echo "Setting timezone to ${TZ}..."
ln -snf /usr/share/zoneinfo/${TZ} /etc/localtime
echo ${TZ} > /etc/timezone
sed -i -e "s|date\.timezone.*|date\.timezone = ${TZ}|" /etc/php7/php.ini \
# Change librenms UID / GID
echo "Checking if librenms UID / GID has changed..."
if [ $(id -u librenms) != ${PUID} ]; then
usermod -u ${PUID} librenms
fi
if [ $(id -g librenms) != ${PGID} ]; then
groupmod -g ${PGID} librenms
fi
# PHP
echo "Setting PHP-FPM configuration..."
sed -e "s/@MEMORY_LIMIT@/$MEMORY_LIMIT/g" \
-e "s/@UPLOAD_MAX_SIZE@/$UPLOAD_MAX_SIZE/g" \
/tpls/etc/php7/php-fpm.d/www.conf > /etc/php7/php-fpm.d/www.conf
# OpCache
echo "Setting OpCache configuration..."
sed -e "s/@OPCACHE_MEM_SIZE@/$OPCACHE_MEM_SIZE/g" \
/tpls/etc/php7/conf.d/opcache.ini > /etc/php7/conf.d/opcache.ini
# Nginx
echo "Setting Nginx configuration..."
sed -e "s/@UPLOAD_MAX_SIZE@/$UPLOAD_MAX_SIZE/g" \
/tpls/etc/nginx/nginx.conf > /etc/nginx/nginx.conf
# SNMP
echo "Updating SNMP community..."
sed -i -e "s/RANDOMSTRINGGOESHERE/${LIBRENMS_SNMP_COMMUNITY}/" /etc/snmp/snmpd.conf
# Init files and folders
echo "Initializing LibreNMS files / folders..."
mkdir -p ${DATA_PATH}/config \
${DATA_PATH}/logs \
${DATA_PATH}/rrd
rm -f ${LIBRENMS_PATH}/config.d/*
echo "Setting LibreNMS configuration..."
# Config : Directories
cat > ${LIBRENMS_PATH}/config.d/directories.php <<EOL
<?php
\$config['install_dir'] = '${LIBRENMS_PATH}';
\$config['log_dir'] = '${DATA_PATH}/logs';
\$config['rrd_dir'] = '${DATA_PATH}/rrd';
EOL
# Config : Database
if [ -z "$DB_HOST" ]; then
>&2 echo "ERROR: DB_HOST must be defined"
exit 1
fi
cat > ${LIBRENMS_PATH}/config.d/database.php <<EOL
<?php
\$config['db_host'] = '${DB_HOST}';
\$config['db_port'] = ${DB_PORT};
\$config['db_user'] = '${DB_USER}';
\$config['db_pass'] = '${DB_PASSWORD}';
\$config['db_name'] = '${DB_NAME}';
EOL
dbcmd="mysql -h ${DB_HOST} -P ${DB_PORT} -u "${DB_USER}" "-p${DB_PASSWORD}""
unset DB_PASSWORD
# Config : User
cat > ${LIBRENMS_PATH}/config.d/user.php <<EOL
<?php
\$config['user'] = "librenms";
EOL
# Config : Fping
cat > ${LIBRENMS_PATH}/config.d/fping.php <<EOL
<?php
\$config['fping'] = "/usr/sbin/fping";
EOL
# Config : Disable autoupdate
cat > ${LIBRENMS_PATH}/config.d/autoupdate.php <<EOL
<?php
\$config['update'] = 0;
EOL
# Config : Memcached
if [ ! -z "${MEMCACHED_HOST}" ]; then
cat > ${LIBRENMS_PATH}/config.d/memcached.php <<EOL
<?php
\$config['memcached']['enable'] = true;
\$config['memcached']['host'] = '$MEMCACHED_HOST';
\$config['memcached']['port'] = 11211;
EOL
fi
# Config : RRDcached
if [ ! -z "${RRDCACHED_HOST}" ]; then
cat > ${LIBRENMS_PATH}/config.d/rrdcached.php <<EOL
<?php
\$config['rrdcached'] = "${RRDCACHED_HOST}:42217";
EOL
fi
# Sidecar cron container ?
if [ "$1" == "/usr/local/bin/cron" ]; then
echo ">>"
echo ">> Sidecar cron container detected"
echo ">>"
# Init
rm -rf ${CRONTAB_PATH}
mkdir -m 0644 -p ${CRONTAB_PATH}
touch ${CRONTAB_PATH}/librenms
# Add crons
cat ${LIBRENMS_PATH}/librenms.nonroot.cron > ${CRONTAB_PATH}/librenms
sed -i -e "s/ librenms //" ${CRONTAB_PATH}/librenms
sed -i -e "s/poller-wrapper.py 16/poller-wrapper.py ${LIBRENMS_POLLER_THREADS}/g" ${CRONTAB_PATH}/librenms
# Fix perms
echo "Fixing permissions..."
chmod -R 0644 ${CRONTAB_PATH}
elif [ "$1" == "/usr/sbin/syslog-ng" ]; then
echo ">>"
echo ">> Sidecar syslog-ng container detected"
echo ">>"
# Init
mkdir -p ${DATA_PATH}/syslog-ng /run/syslog-ng
chown -R librenms. ${DATA_PATH}/syslog-ng /run/syslog-ng
else
# Fix perms
echo "Fixing permissions..."
chown -R librenms. ${DATA_PATH} \
${LIBRENMS_PATH}/config.d \
${LIBRENMS_PATH}/bootstrap \
${LIBRENMS_PATH}/storage
chmod ug+rw ${DATA_PATH}/logs \
${DATA_PATH}/rrd \
${LIBRENMS_PATH}/bootstrap/cache \
${LIBRENMS_PATH}/storage \
${LIBRENMS_PATH}/storage/framework/*
echo "Waiting database..."
waitdb_timeout=30
counter=1
while ! ${dbcmd} -e "show databases;" > /dev/null 2>&1; do
sleep 1
counter=`expr $counter + 1`
if [ ${counter} -gt ${waitdb_timeout} ]; then
>&2 echo "ERROR: Failed to connect to database on $DB_HOST"
exit 1
fi;
done
echo "Database up!"
counttables=$(echo 'SHOW TABLES' | ${dbcmd} "$DB_NAME" | wc -l)
echo "Updating database schema..."
runas_librenms "php build-base.php"
if [ "${counttables}" -eq "0" ]; then
echo "Creating admin user..."
runas_librenms "php adduser.php librenms librenms 10 librenms@librenms.docker"
fi
fi
exec "$@"