Skip to content

Commit

Permalink
Added posibility for custom SQL scripts during DB init
Browse files Browse the repository at this point in the history
  • Loading branch information
dotneft committed Aug 27, 2022
1 parent d0311c4 commit 8b52ff7
Show file tree
Hide file tree
Showing 39 changed files with 577 additions and 231 deletions.
37 changes: 26 additions & 11 deletions Dockerfiles/proxy-mysql/alpine/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ ZABBIX_USER_HOME_DIR="/var/lib/zabbix"
# Configuration files directory
ZABBIX_ETC_DIR="/etc/zabbix"

: ${DB_CHARACTER_SET:="utf8mb4"}
: ${DB_CHARACTER_COLLATE:="utf8mb4_bin"}

# usage: file_env VAR [DEFAULT]
# as example: file_env 'MYSQL_PASSWORD' 'zabbix'
# (will allow for "$MYSQL_PASSWORD_FILE" to fill in the value of "$MYSQL_PASSWORD" from a file)
Expand Down Expand Up @@ -250,6 +253,28 @@ mysql_query() {
echo $result
}

exec_sql_file() {
sql_script=$1

local command="cat"

ssl_opts="$(db_tls_params)"

export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"

if [ "${sql_script: -3}" == ".gz" ]; then
command="zcat"
fi

$command "$sql_script" | mysql --silent --skip-column-names \
--default-character-set=${DB_CHARACTER_SET} \
-h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} \
-u ${DB_SERVER_ROOT_USER} $ssl_opts \
${DB_SERVER_DBNAME} 1>/dev/null

unset MYSQL_PWD
}

create_db_user_mysql() {
[ "${CREATE_ZBX_DB_USER}" == "true" ] || return

Expand Down Expand Up @@ -290,17 +315,7 @@ create_db_schema_mysql() {
if [ -z "${ZBX_DB_VERSION}" ]; then
echo "** Creating '${DB_SERVER_DBNAME}' schema in MySQL"

ssl_opts="$(db_tls_params)"

export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"

zcat /usr/share/doc/zabbix-proxy-mysql/create.sql.gz | mysql --silent --skip-column-names \
--default-character-set=utf8mb4 \
-h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} \
-u ${DB_SERVER_ROOT_USER} $ssl_opts \
${DB_SERVER_DBNAME} 1>/dev/null

unset MYSQL_PWD
exec_sql_file "/usr/share/doc/zabbix-proxy-mysql/create.sql.gz"
fi
}

Expand Down
38 changes: 26 additions & 12 deletions Dockerfiles/proxy-mysql/centos/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ ZABBIX_USER_HOME_DIR="/var/lib/zabbix"
# Configuration files directory
ZABBIX_ETC_DIR="/etc/zabbix"

: ${DB_CHARACTER_SET:="utf8mb4"}
: ${DB_CHARACTER_COLLATE:="utf8mb4_bin"}

# usage: file_env VAR [DEFAULT]
# as example: file_env 'MYSQL_PASSWORD' 'zabbix'
# (will allow for "$MYSQL_PASSWORD_FILE" to fill in the value of "$MYSQL_PASSWORD" from a file)
Expand Down Expand Up @@ -201,7 +204,6 @@ db_tls_params() {
echo $result
}


check_db_connect_mysql() {
echo "********************"
echo "* DB_SERVER_HOST: ${DB_SERVER_HOST}"
Expand Down Expand Up @@ -248,6 +250,28 @@ mysql_query() {
echo $result
}

exec_sql_file() {
sql_script=$1

local command="cat"

ssl_opts="$(db_tls_params)"

export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"

if [ "${sql_script: -3}" == ".gz" ]; then
command="zcat"
fi

$command "$sql_script" | mysql --silent --skip-column-names \
--default-character-set=${DB_CHARACTER_SET} \
-h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} \
-u ${DB_SERVER_ROOT_USER} $ssl_opts \
${DB_SERVER_DBNAME} 1>/dev/null

unset MYSQL_PWD
}

create_db_user_mysql() {
[ "${CREATE_ZBX_DB_USER}" == "true" ] || return

Expand Down Expand Up @@ -288,17 +312,7 @@ create_db_schema_mysql() {
if [ -z "${ZBX_DB_VERSION}" ]; then
echo "** Creating '${DB_SERVER_DBNAME}' schema in MySQL"

ssl_opts="$(db_tls_params)"

export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"

zcat /usr/share/doc/zabbix-proxy-mysql/create.sql.gz | mysql --silent --skip-column-names \
--default-character-set=utf8mb4 \
-h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} \
-u ${DB_SERVER_ROOT_USER} $ssl_opts \
${DB_SERVER_DBNAME} 1>/dev/null

unset MYSQL_PWD
exec_sql_file "/usr/share/doc/zabbix-proxy-mysql/create.sql.gz"
fi
}

Expand Down
38 changes: 26 additions & 12 deletions Dockerfiles/proxy-mysql/ol/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ ZABBIX_USER_HOME_DIR="/var/lib/zabbix"
# Configuration files directory
ZABBIX_ETC_DIR="/etc/zabbix"

: ${DB_CHARACTER_SET:="utf8mb4"}
: ${DB_CHARACTER_COLLATE:="utf8mb4_bin"}

# usage: file_env VAR [DEFAULT]
# as example: file_env 'MYSQL_PASSWORD' 'zabbix'
# (will allow for "$MYSQL_PASSWORD_FILE" to fill in the value of "$MYSQL_PASSWORD" from a file)
Expand Down Expand Up @@ -201,7 +204,6 @@ db_tls_params() {
echo $result
}


check_db_connect_mysql() {
echo "********************"
echo "* DB_SERVER_HOST: ${DB_SERVER_HOST}"
Expand Down Expand Up @@ -248,6 +250,28 @@ mysql_query() {
echo $result
}

exec_sql_file() {
sql_script=$1

local command="cat"

ssl_opts="$(db_tls_params)"

export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"

if [ "${sql_script: -3}" == ".gz" ]; then
command="zcat"
fi

$command "$sql_script" | mysql --silent --skip-column-names \
--default-character-set=${DB_CHARACTER_SET} \
-h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} \
-u ${DB_SERVER_ROOT_USER} $ssl_opts \
${DB_SERVER_DBNAME} 1>/dev/null

unset MYSQL_PWD
}

create_db_user_mysql() {
[ "${CREATE_ZBX_DB_USER}" == "true" ] || return

Expand Down Expand Up @@ -288,17 +312,7 @@ create_db_schema_mysql() {
if [ -z "${ZBX_DB_VERSION}" ]; then
echo "** Creating '${DB_SERVER_DBNAME}' schema in MySQL"

ssl_opts="$(db_tls_params)"

export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"

zcat /usr/share/doc/zabbix-proxy-mysql/create.sql.gz | mysql --silent --skip-column-names \
--default-character-set=utf8mb4 \
-h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} \
-u ${DB_SERVER_ROOT_USER} $ssl_opts \
${DB_SERVER_DBNAME} 1>/dev/null

unset MYSQL_PWD
exec_sql_file "/usr/share/doc/zabbix-proxy-mysql/create.sql.gz"
fi
}

Expand Down
39 changes: 26 additions & 13 deletions Dockerfiles/proxy-mysql/rhel/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ ZABBIX_USER_HOME_DIR="/var/lib/zabbix"
# Configuration files directory
ZABBIX_ETC_DIR="/etc/zabbix"

: ${DB_CHARACTER_SET:="utf8mb4"}
: ${DB_CHARACTER_COLLATE:="utf8mb4_bin"}

# usage: file_env VAR [DEFAULT]
# as example: file_env 'MYSQL_PASSWORD' 'zabbix'
# (will allow for "$MYSQL_PASSWORD_FILE" to fill in the value of "$MYSQL_PASSWORD" from a file)
Expand Down Expand Up @@ -201,7 +204,6 @@ db_tls_params() {
echo $result
}


check_db_connect_mysql() {
echo "********************"
echo "* DB_SERVER_HOST: ${DB_SERVER_HOST}"
Expand Down Expand Up @@ -248,6 +250,28 @@ mysql_query() {
echo $result
}

exec_sql_file() {
sql_script=$1

local command="cat"

ssl_opts="$(db_tls_params)"

export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"

if [ "${sql_script: -3}" == ".gz" ]; then
command="zcat"
fi

$command "$sql_script" | mysql --silent --skip-column-names \
--default-character-set=${DB_CHARACTER_SET} \
-h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} \
-u ${DB_SERVER_ROOT_USER} $ssl_opts \
${DB_SERVER_DBNAME} 1>/dev/null

unset MYSQL_PWD
}

create_db_user_mysql() {
[ "${CREATE_ZBX_DB_USER}" == "true" ] || return

Expand All @@ -262,7 +286,6 @@ create_db_user_mysql() {
fi

mysql_query "GRANT ALL PRIVILEGES ON $DB_SERVER_DBNAME. * TO '${DB_SERVER_ZBX_USER}'@'%'" 1>/dev/null
mysql_query "GRANT SESSION_VARIABLES_ADMIN ON *. * TO '${DB_SERVER_ZBX_USER}'@'%'" 1>/dev/null
}

create_db_database_mysql() {
Expand All @@ -289,17 +312,7 @@ create_db_schema_mysql() {
if [ -z "${ZBX_DB_VERSION}" ]; then
echo "** Creating '${DB_SERVER_DBNAME}' schema in MySQL"

ssl_opts="$(db_tls_params)"

export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"

zcat /usr/share/doc/zabbix-proxy-mysql/create.sql.gz | mysql --silent --skip-column-names \
--default-character-set=utf8mb4 \
-h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} \
-u ${DB_SERVER_ROOT_USER} $ssl_opts \
${DB_SERVER_DBNAME} 1>/dev/null

unset MYSQL_PWD
exec_sql_file "/usr/share/doc/zabbix-proxy-mysql/create.sql.gz"
fi
}

Expand Down
39 changes: 27 additions & 12 deletions Dockerfiles/proxy-mysql/ubuntu/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ ZABBIX_USER_HOME_DIR="/var/lib/zabbix"
# Configuration files directory
ZABBIX_ETC_DIR="/etc/zabbix"

: ${DB_CHARACTER_SET:="utf8mb4"}
: ${DB_CHARACTER_COLLATE:="utf8mb4_bin"}

# usage: file_env VAR [DEFAULT]
# as example: file_env 'MYSQL_PASSWORD' 'zabbix'
# (will allow for "$MYSQL_PASSWORD_FILE" to fill in the value of "$MYSQL_PASSWORD" from a file)
Expand Down Expand Up @@ -219,7 +222,7 @@ check_db_connect_mysql() {
WAIT_TIMEOUT=5

ssl_opts="$(db_tls_params)"

export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"

while [ ! "$(mysqladmin ping -h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} -u ${DB_SERVER_ROOT_USER} \
Expand Down Expand Up @@ -247,6 +250,28 @@ mysql_query() {
echo $result
}

exec_sql_file() {
sql_script=$1

local command="cat"

ssl_opts="$(db_tls_params)"

export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"

if [ "${sql_script: -3}" == ".gz" ]; then
command="zcat"
fi

$command "$sql_script" | mysql --silent --skip-column-names \
--default-character-set=${DB_CHARACTER_SET} \
-h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} \
-u ${DB_SERVER_ROOT_USER} $ssl_opts \
${DB_SERVER_DBNAME} 1>/dev/null

unset MYSQL_PWD
}

create_db_user_mysql() {
[ "${CREATE_ZBX_DB_USER}" == "true" ] || return

Expand Down Expand Up @@ -287,17 +312,7 @@ create_db_schema_mysql() {
if [ -z "${ZBX_DB_VERSION}" ]; then
echo "** Creating '${DB_SERVER_DBNAME}' schema in MySQL"

ssl_opts="$(db_tls_params)"

export MYSQL_PWD="${DB_SERVER_ROOT_PASS}"

zcat /usr/share/doc/zabbix-proxy-mysql/create.sql.gz | mysql --silent --skip-column-names \
--default-character-set=utf8mb4 \
-h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} \
-u ${DB_SERVER_ROOT_USER} $ssl_opts \
${DB_SERVER_DBNAME} 1>/dev/null

unset MYSQL_PWD
exec_sql_file "/usr/share/doc/zabbix-proxy-mysql/create.sql.gz"
fi
}

Expand Down
2 changes: 2 additions & 0 deletions Dockerfiles/server-mysql/alpine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ RUN set -eux && \
mkdir -p /etc/zabbix && \
mkdir -p /var/lib/zabbix && \
mkdir -p /usr/lib/zabbix/alertscripts && \
mkdir -p /usr/lib/zabbix/dbscripts && \
mkdir -p /var/lib/zabbix/dbscripts && \
mkdir -p /var/lib/zabbix/enc && \
mkdir -p /var/lib/zabbix/export && \
mkdir -p /usr/lib/zabbix/externalscripts && \
Expand Down
Loading

0 comments on commit 8b52ff7

Please sign in to comment.