forked from combro2k/docker-icinga2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
entrypoint.sh
126 lines (105 loc) · 6.21 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
#!/bin/bash
set -e
if [ ! -z "$IPV6ADDR" ]; then
echo $IPV6ADDR
ip -6 addr add "$IPV6ADDR" dev eth0
fi
sleep 2
if [ ! -z "$IPV6GW" ]; then
echo $IPV6GW
ip -6 route add default via "$IPV6GW" dev eth0
fi
if [ -z "$MYSQL_PORT_3306_TCP" ]; then
echo >&2 'error: missing MYSQL_PORT_3306_TCP environment variable'
echo >&2 ' Did you forget to --link some_mysql_container:mysql ?'
exit 1
fi
ICINGA_DATABASE=${ICINGA_DATABASE:=icinga2}
ICINGA_DB_USER=${ICINGA_DB_USER:=icinga2}
if [ -z "$ICINGA_DB_PASSWORD" ] ; then
ICINGA_DB_PASSWORD=`pwgen 32 1`
else
# Create IDO-MySQL configuration if connection info was passed as environment variables
cat >/etc/icinga2/features-available/ido-mysql.conf <<-EOF
library "db_ido_mysql"
object IdoMysqlConnection "ido-mysql" {
user = "$ICINGA_DB_USER",
password = "$ICINGA_DB_PASSWORD",
host = "$MYSQL_PORT_3306_TCP_ADDR",
database = "$ICINGA_DATABASE"
}
EOF
fi
ICINGAWEB_DATABASE=${ICINGAWEB_DATABASE:=icinga_web}
ICINGAWEB_DB_USER=${ICINGAWEB_DB_USER:=icinga_web}
if [ -z "$ICINGAWEB_DB_PASSWORD" ] ; then
ICINGAWEB_DB_PASSWORD=`pwgen 32 1`
else
# Create database configuration if connection info was passed as environment variables
cat >/etc/icinga-web/conf.d/database-ido.xml <<-EOF
<?xml version="1.0" encoding="UTF-8"?>
<databases xmlns:db="http://agavi.org/agavi/config/parts/databases/1.0" xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0">
<db:database xmlns="http://agavi.org/agavi/config/parts/databases/1.0" name="icinga" class="IcingaDoctrineDatabase">
<ae:parameter name="dsn">mysql://$ICINGA_DB_USER:$ICINGA_DB_PASSWORD@$MYSQL_PORT_3306_TCP_ADDR:$MYSQL_PORT_3306_TCP_PORT/$ICINGA_DATABASE</ae:parameter>
</db:database>
</databases>
EOF
cat >/etc/icinga-web/conf.d/database-web.xml <<-EOF
<?xml version="1.0" encoding="UTF-8"?>
<databases xmlns:db="http://agavi.org/agavi/config/parts/databases/1.0" xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0">
<db:database name="icinga_web" class="AppKitDoctrineDatabase">
<ae:parameter name="dsn">mysql://$ICINGAWEB_DB_USER:$ICINGAWEB_DB_PASSWORD@$MYSQL_PORT_3306_TCP_ADDR:$MYSQL_PORT_3306_TCP_PORT/$ICINGAWEB_DATABASE</ae:parameter>
</db:database>
</databases>
EOF
echo >&2 "TODO: Create Icinga WEB config"
fi
if [ "${1,,}" == "setup" ] ; then
echo "First time setup requested..."
if [ -z "$MYSQL_ENV_MYSQL_ROOT_PASSWORD" ] && [ -z "$MYSQL_ROOT_PASSWORD" ] ; then
echo >&2 "ERROR: Neither MYSQL_ENV_MYSQL_ROOT_PASSWORD nor MYSQL_ROOT_PASSWORD are set."
echo >&2 " To perform first-time setup, please provide MySQL's root password via"
echo >&2 " the MYSQL_ROOT_PASSWORD environment variable."
echo >&2 " Alternatively, the linked MySQL container can provide the root password"
echo >&2 " via the docker link."
fi
echo "MySQL root password is set. Using root access for first-time setup."
echo "Dropping database $ICINGA_DATABASE..."
echo "DROP DATABASE IF EXISTS $ICINGA_DATABASE;" | mysql -uroot "-p$MYSQL_ENV_MYSQL_ROOT_PASSWORD" "-h$MYSQL_PORT_3306_TCP_ADDR" "-P$MYSQL_PORT_3306_TCP_PORT" mysql
echo "Dropping database $ICINGAWEB_DATABASE..."
echo "DROP DATABASE IF EXISTS $ICINGAWEB_DATABASE;" | mysql -uroot "-p$MYSQL_ENV_MYSQL_ROOT_PASSWORD" "-h$MYSQL_PORT_3306_TCP_ADDR" "-P$MYSQL_PORT_3306_TCP_PORT" mysql
echo "Creating database $ICINGA_DATABASE..."
echo "CREATE DATABASE IF NOT EXISTS $ICINGA_DATABASE;" | mysql -uroot "-p$MYSQL_ENV_MYSQL_ROOT_PASSWORD" "-h$MYSQL_PORT_3306_TCP_ADDR" "-P$MYSQL_PORT_3306_TCP_PORT" mysql
echo "Creating database $ICINGAWEB_DATABASE..."
echo "CREATE DATABASE IF NOT EXISTS $ICINGAWEB_DATABASE;" | mysql -uroot "-p$MYSQL_ENV_MYSQL_ROOT_PASSWORD" "-h$MYSQL_PORT_3306_TCP_ADDR" "-P$MYSQL_PORT_3306_TCP_PORT" mysql
echo "Creating database user $ICINGA_DB_USER..."
echo "GRANT ALL ON $ICINGA_DATABASE.* TO '$ICINGA_DB_USER'@'%' IDENTIFIED BY '$ICINGA_DB_PASSWORD';" | mysql -uroot "-p$MYSQL_ENV_MYSQL_ROOT_PASSWORD" "-h$MYSQL_PORT_3306_TCP_ADDR" "-P$MYSQL_PORT_3306_TCP_PORT" mysql
echo "Creating database user $ICINGAWEB_DB_USER..."
echo "GRANT ALL ON $ICINGAWEB_DATABASE.* TO '$ICINGAWEB_DB_USER'@'%' IDENTIFIED BY '$ICINGAWEB_DB_PASSWORD';" | mysql -uroot "-p$MYSQL_ENV_MYSQL_ROOT_PASSWORD" "-h$MYSQL_PORT_3306_TCP_ADDR" "-P$MYSQL_PORT_3306_TCP_PORT" mysql
echo "GRANT ALL ON $ICINGA_DATABASE.* TO '$ICINGAWEB_DB_USER'@'%' IDENTIFIED BY '$ICINGAWEB_DB_PASSWORD';" | mysql -uroot "-p$MYSQL_ENV_MYSQL_ROOT_PASSWORD" "-h$MYSQL_PORT_3306_TCP_ADDR" "-P$MYSQL_PORT_3306_TCP_PORT" mysql
echo "Importing database schema into $ICINGA_DATABASE..."
mysql "-u$ICINGA_DB_USER" "-p$ICINGA_DB_PASSWORD" "-h$MYSQL_PORT_3306_TCP_ADDR" "-P$MYSQL_PORT_3306_TCP_PORT" "$ICINGA_DATABASE" < /usr/share/icinga2-ido-mysql/schema/mysql.sql
echo "Importing database schema into $ICINGAWEB_DATABASE..."
mysql "-u$ICINGAWEB_DB_USER" "-p$ICINGAWEB_DB_PASSWORD" "-h$MYSQL_PORT_3306_TCP_ADDR" "-P$MYSQL_PORT_3306_TCP_PORT" "$ICINGAWEB_DATABASE" < /usr/share/dbconfig-common/data/icinga-web/install/mysql
echo
echo "################################################################"
echo "## First-time setup complete. ##"
echo "## Icinga2 has been configured with the following data. ##"
echo "## You can now start the container in daemon mode and provide ##"
echo "## this data via environment variables. ##"
echo "################################################################"
printf "## ICINGA_DATABASE: %-33s ##\n" "$ICINGA_DATABASE"
printf "## ICINGA_DB_USER: %-33s ##\n" "$ICINGA_DB_USER"
printf "## ICINGA_DB_PASSWORD: %-33s ##\n" "$ICINGA_DB_PASSWORD"
printf "## ICINGAWEB_DB_USER: %-33s ##\n" "$ICINGAWEB_DB_USER"
printf "## ICINGAWEB_DB_PASSWORD: %-33s ##\n" "$ICINGAWEB_DB_PASSWORD"
printf "## ICINGAWEB_DATABASE: %-33s ##\n" "$ICINGAWEB_DATABASE"
echo "################################################################"
exit 0
fi
gpasswd -a www-data nagios
mkdir -p /var/run/icinga2/cmd
chown -R nagios:nagios /var/run/icinga2
#chown -R nagios:www-data /var/run/icinga2/cmd
#chmod g+rwx /var/run/icinga2/cmd
exec "$@"