Skip to content
This repository has been archived by the owner on Nov 29, 2020. It is now read-only.

Introduce environment variable preset features #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions 10.0/bootstrap_mariadb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

/usr/bin/mysqld_safe > /dev/null 2>&1 &

# Sleep until MariaDB is up and running
RET=1
while [[ RET -ne 0 ]]; do
echo "=> Waiting for confirmation of MariaDB service startup"
sleep 5
mysql -uroot -e "status" > /dev/null 2>&1
RET=$?
done

# Run startup scripts
/create_admin_user.sh
/create_default_db.sh
/startup_sql_import.sh

mysqladmin -uroot shutdown
24 changes: 5 additions & 19 deletions 10.1/create_mariadb_admin_user.sh → 10.0/create_admin_user.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
#!/bin/bash

/usr/bin/mysqld_safe > /dev/null 2>&1 &

RET=1
while [[ RET -ne 0 ]]; do
echo "=> Waiting for confirmation of MariaDB service startup"
sleep 5
mysql -uroot -e "status" > /dev/null 2>&1
RET=$?
done


USER=${MARIADB_USER:-admin}
PASS=${MARIADB_PASS:-$(pwgen -s 12 1)}
_word=$( [ ${MARIADB_PASS} ] && echo "preset" || echo "random" )
echo "=> Creating MariaDB admin user with ${_word} password"
echo "=> Creating MariaDB admin user ${USER} with ${_word} password"

mysql -uroot -e "CREATE USER 'admin'@'%' IDENTIFIED BY '$PASS'"
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION"
mysql -uroot -e "CREATE USER '$USER'@'%' IDENTIFIED BY '$PASS'"
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '$USER'@'%' WITH GRANT OPTION"

echo "=> Done!"

echo "========================================================================"
echo "You can now connect to this MariaDB Server using:"
echo ""
echo " mysql -uadmin -p$PASS -h<host> -P<port>"
echo " mysql -u$USER -p$PASS -h<host> -P<port>"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "MariaDB user 'root' has no password but only allows local connections"
echo "========================================================================"

mysqladmin -uroot shutdown
5 changes: 5 additions & 0 deletions 10.0/create_default_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if [ -n "$ON_CREATE_DB" ]; then
echo "=> Creating database $ON_CREATE_DB"
mysql -uroot -e "CREATE DATABASE IF NOT EXISTS $ON_CREATE_DB"
echo "=> Database ${ON_CREATE_DB} created."
fi
15 changes: 11 additions & 4 deletions 10.0/create_mariadb_admin_user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@ while [[ RET -ne 0 ]]; do
done


USER=${MARIADB_USER:-admin}
PASS=${MARIADB_PASS:-$(pwgen -s 12 1)}
_word=$( [ ${MARIADB_PASS} ] && echo "preset" || echo "random" )
echo "=> Creating MariaDB admin user with ${_word} password"
echo "=> Creating MariaDB admin user ${USER} with ${_word} password"

mysql -uroot -e "CREATE USER 'admin'@'%' IDENTIFIED BY '$PASS'"
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION"
mysql -uroot -e "CREATE USER '$USER'@'%' IDENTIFIED BY '$PASS'"

if [$ON_CREATE_DB]; then
echo "=> Creating database $DB"
mysql -uroot -e "CREATE DATABASE IF NOT EXISTS $DB"
fi

mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '$USER'@'%' WITH GRANT OPTION"

echo "=> Done!"

echo "========================================================================"
echo "You can now connect to this MariaDB Server using:"
echo ""
echo " mysql -uadmin -p$PASS -h<host> -P<port>"
echo " mysql -u$USER -p$PASS -h<host> -P<port>"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "MariaDB user 'root' has no password but only allows local connections"
Expand Down
4 changes: 2 additions & 2 deletions 10.0/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ if [[ ! -d $VOLUME_HOME/mysql ]]; then
echo "=> An empty or uninitialized MariaDB volume is detected in $VOLUME_HOME"
echo "=> Installing MariaDB ..."
mysql_install_db > /dev/null 2>&1
echo "=> Done!"
/create_mariadb_admin_user.sh
echo "=> Done!"
/bootstrap_mariadb.sh
else
echo "=> Using an existing volume of MariaDB"
fi
Expand Down
20 changes: 20 additions & 0 deletions 10.0/startup_sql_import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ImportSql ()
{
for FILE in ${STARTUP_SQL}; do
echo "=> Importing SQL file ${FILE}"
if [ "$ON_CREATE_DB" ]; then
mysql -uroot "$ON_CREATE_DB" < "${FILE}"
else
mysql -uroot < "${FILE}"
fi
done
}

# Import Startup SQL
if [ -n "${STARTUP_SQL}" ]; then
if [ ! -f /sql_imported ]; then
echo "=> Initializing DB with ${STARTUP_SQL}"
ImportSql
touch /sql_imported
fi
fi
5 changes: 4 additions & 1 deletion 10.1/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ RUN apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a
#change bind address to 0.0.0.0
RUN sed -i -r 's/bind-address.*$/bind-address = 0.0.0.0/' /etc/mysql/my.cnf

ADD create_mariadb_admin_user.sh /create_mariadb_admin_user.sh
ADD create_admin_user.sh /create_admin_user.sh
ADD create_default_db.sh /create_default_db.sh
ADD startup_sql_import.sh /startup_sql_import.sh
ADD bootstrap_mariadb.sh /bootstrap_mariadb.sh
ADD run.sh /run.sh
RUN chmod 775 /*.sh

Expand Down
19 changes: 19 additions & 0 deletions 10.1/bootstrap_mariadb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

/usr/bin/mysqld_safe > /dev/null 2>&1 &

# Sleep until MariaDB is up and running
RET=1
while [[ RET -ne 0 ]]; do
echo "=> Waiting for confirmation of MariaDB service startup"
sleep 5
mysql -uroot -e "status" > /dev/null 2>&1
RET=$?
done

# Run startup scripts
/create_admin_user.sh
/create_default_db.sh
/startup_sql_import.sh

mysqladmin -uroot shutdown
18 changes: 18 additions & 0 deletions 10.1/create_admin_user.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
USER=${MARIADB_USER:-admin}
PASS=${MARIADB_PASS:-$(pwgen -s 12 1)}
_word=$( [ ${MARIADB_PASS} ] && echo "preset" || echo "random" )
echo "=> Creating MariaDB admin user ${USER} with ${_word} password"

mysql -uroot -e "CREATE USER '$USER'@'%' IDENTIFIED BY '$PASS'"
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '$USER'@'%' WITH GRANT OPTION"

echo "=> Done!"

echo "========================================================================"
echo "You can now connect to this MariaDB Server using:"
echo ""
echo " mysql -u$USER -p$PASS -h<host> -P<port>"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "MariaDB user 'root' has no password but only allows local connections"
echo "========================================================================"
5 changes: 5 additions & 0 deletions 10.1/create_default_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if [ -n "$ON_CREATE_DB" ]; then
echo "=> Creating database $ON_CREATE_DB"
mysql -uroot -e "CREATE DATABASE IF NOT EXISTS $ON_CREATE_DB"
echo "=> Database ${ON_CREATE_DB} created."
fi
4 changes: 2 additions & 2 deletions 10.1/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ if [[ ! -d $VOLUME_HOME/mysql ]]; then
echo "=> An empty or uninitialized MariaDB volume is detected in $VOLUME_HOME"
echo "=> Installing MariaDB ..."
mysql_install_db > /dev/null 2>&1
echo "=> Done!"
/create_mariadb_admin_user.sh
echo "=> Done!"
/bootstrap_mariadb.sh
else
echo "=> Using an existing volume of MariaDB"
fi
Expand Down
20 changes: 20 additions & 0 deletions 10.1/startup_sql_import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ImportSql ()
{
for FILE in ${STARTUP_SQL}; do
echo "=> Importing SQL file ${FILE}"
if [ "$ON_CREATE_DB" ]; then
mysql -uroot "$ON_CREATE_DB" < "${FILE}"
else
mysql -uroot < "${FILE}"
fi
done
}

# Import Startup SQL
if [ -n "${STARTUP_SQL}" ]; then
if [ ! -f /sql_imported ]; then
echo "=> Initializing DB with ${STARTUP_SQL}"
ImportSql
touch /sql_imported
fi
fi
19 changes: 19 additions & 0 deletions 5.5/bootstrap_mariadb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

/usr/bin/mysqld_safe > /dev/null 2>&1 &

# Sleep until MariaDB is up and running
RET=1
while [[ RET -ne 0 ]]; do
echo "=> Waiting for confirmation of MariaDB service startup"
sleep 5
mysql -uroot -e "status" > /dev/null 2>&1
RET=$?
done

# Run startup scripts
/create_admin_user.sh
/create_default_db.sh
/startup_sql_import.sh

mysqladmin -uroot shutdown
18 changes: 18 additions & 0 deletions 5.5/create_admin_user.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
USER=${MARIADB_USER:-admin}
PASS=${MARIADB_PASS:-$(pwgen -s 12 1)}
_word=$( [ ${MARIADB_PASS} ] && echo "preset" || echo "random" )
echo "=> Creating MariaDB admin user ${USER} with ${_word} password"

mysql -uroot -e "CREATE USER '$USER'@'%' IDENTIFIED BY '$PASS'"
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '$USER'@'%' WITH GRANT OPTION"

echo "=> Done!"

echo "========================================================================"
echo "You can now connect to this MariaDB Server using:"
echo ""
echo " mysql -u$USER -p$PASS -h<host> -P<port>"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "MariaDB user 'root' has no password but only allows local connections"
echo "========================================================================"
5 changes: 5 additions & 0 deletions 5.5/create_default_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if [ -n "$ON_CREATE_DB" ]; then
echo "=> Creating database $ON_CREATE_DB"
mysql -uroot -e "CREATE DATABASE IF NOT EXISTS $ON_CREATE_DB"
echo "=> Database ${ON_CREATE_DB} created."
fi
15 changes: 11 additions & 4 deletions 5.5/create_mariadb_admin_user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@ while [[ RET -ne 0 ]]; do
done


USER=${MARIADB_USER:-admin}
PASS=${MARIADB_PASS:-$(pwgen -s 12 1)}
_word=$( [ ${MARIADB_PASS} ] && echo "preset" || echo "random" )
echo "=> Creating MariaDB admin user with ${_word} password"
echo "=> Creating MariaDB admin user ${USER} with ${_word} password"

mysql -uroot -e "CREATE USER 'admin'@'%' IDENTIFIED BY '$PASS'"
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION"
mysql -uroot -e "CREATE USER '$USER'@'%' IDENTIFIED BY '$PASS'"

if [$ON_CREATE_DB]; then
echo "=> Creating database $DB"
mysql -uroot -e "CREATE DATABASE IF NOT EXISTS $DB"
fi

mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '$USER'@'%' WITH GRANT OPTION"

echo "=> Done!"

echo "========================================================================"
echo "You can now connect to this MariaDB Server using:"
echo ""
echo " mysql -uadmin -p$PASS -h<host> -P<port>"
echo " mysql -u$USER -p$PASS -h<host> -P<port>"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "MariaDB user 'root' has no password but only allows local connections"
Expand Down
4 changes: 2 additions & 2 deletions 5.5/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ if [[ ! -d $VOLUME_HOME/mysql ]]; then
echo "=> An empty or uninitialized MariaDB volume is detected in $VOLUME_HOME"
echo "=> Installing MariaDB ..."
mysql_install_db > /dev/null 2>&1
echo "=> Done!"
/create_mariadb_admin_user.sh
echo "=> Done!"
/bootstrap_mariadb.sh
else
echo "=> Using an existing volume of MariaDB"
fi
Expand Down
20 changes: 20 additions & 0 deletions 5.5/startup_sql_import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ImportSql ()
{
for FILE in ${STARTUP_SQL}; do
echo "=> Importing SQL file ${FILE}"
if [ "$ON_CREATE_DB" ]; then
mysql -uroot "$ON_CREATE_DB" < "${FILE}"
else
mysql -uroot < "${FILE}"
fi
done
}

# Import Startup SQL
if [ -n "${STARTUP_SQL}" ]; then
if [ ! -f /sql_imported ]; then
echo "=> Initializing DB with ${STARTUP_SQL}"
ImportSql
touch /sql_imported
fi
fi
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ In this case, `xVN33tWOhM3u` is the password assigned to the `admin` user.
Done!


Environment Variables
---------------------

Environment variables can be used to preset admin account username & password,
import SQL and create a database at startup.

The following environment variables can be used:

MARIADB_USER - Sets the admin account username (defaults to 'admin')
MARIADB_PASS - Sets the admin account password (defaults to random string, see docker logs <CONTAINER_ID>)
STARTUP_SQL - SQL to import at startup (needs to exist, can be mounted)
ON_CREATE_DB - Database to create at startup.


Setting a specific password for the admin account
-------------------------------------------------

Expand Down