diff options
-rw-r--r-- | OS/Dockerfile | 2 | ||||
-rw-r--r-- | OS/README.md | 3 | ||||
-rw-r--r-- | Services/mariadb/Dockerfile | 4 | ||||
-rw-r--r-- | Services/mariadb/docker-compose.yml | 5 | ||||
-rw-r--r-- | Services/mariadb/my.cnf | 49 | ||||
-rw-r--r-- | Services/mariadb/run.sh | 139 | ||||
-rw-r--r-- | Services/mariadb/sx/mariadb.sh | 178 | ||||
-rw-r--r-- | Services/mariadb/sx/mariadb_run.sh | 21 |
8 files changed, 175 insertions, 226 deletions
diff --git a/OS/Dockerfile b/OS/Dockerfile index 878005c..4e393d1 100644 --- a/OS/Dockerfile +++ b/OS/Dockerfile @@ -5,7 +5,7 @@ USER root # Install minimal packages RUN dnf -y install deltarpm pwgen tar python-dnf-plugins-extras-migrate && \ dnf-2 migrate && \ - dnf -y install psmisc coreutils findutils wget logrotate && \ + dnf -y install psmisc procps coreutils findutils wget logrotate && \ dnf clean all COPY *.sh /bin/ RUN chmod 775 /bin/sx-lib.sh /bin/run.sh diff --git a/OS/README.md b/OS/README.md index 4f24e9c..eda12f5 100644 --- a/OS/README.md +++ b/OS/README.md @@ -1,5 +1,6 @@ # STARTX OS docker-images : Fedora -Fedora image builded with minimal and updated rpm and core-utils loaded +Fedora image builded with minimal and updated rpm and core-utils loaded (utils like ps, top, find, wget) +All other images on this repository are build on top of this container image ## Running from docker registry diff --git a/Services/mariadb/Dockerfile b/Services/mariadb/Dockerfile index fbeacce..09a7acb 100644 --- a/Services/mariadb/Dockerfile +++ b/Services/mariadb/Dockerfile @@ -21,9 +21,9 @@ RUN chmod 775 /bin/run.sh && \ mkdir $LOG_PATH && \ touch $STARTUPLOG && \ touch $DATA_PATH/.keep && \ - chown -R mariadb:mariadb $LOG_PATH $DATA_PATH $LOADSQL_PATH + chown -R mysql:mysql $LOG_PATH $DATA_PATH $LOADSQL_PATH EXPOSE 3306 VOLUME [$DATA_PATH,$LOG_PATH] -CMD ["/sx/run.sh"]
\ No newline at end of file +CMD ["/bin/run.sh"]
\ No newline at end of file diff --git a/Services/mariadb/docker-compose.yml b/Services/mariadb/docker-compose.yml index b1f6807..f6a1143 100644 --- a/Services/mariadb/docker-compose.yml +++ b/Services/mariadb/docker-compose.yml @@ -10,6 +10,11 @@ server: CONTAINER_TYPE: "service" CONTAINER_SERVICE: "mariadb" CONTAINER_INSTANCE: "sx-mariadb" + SERVER_NAME: "localhost" + MYSQL_ROOT_PASSWORD: "secretdemerde" + MYSQL_USER: "user-test" + MYSQL_PASSWORD: "pwd-test" + MYSQL_DATABASE: "db_test" volumes: - "/tmp/container/logs:/data/logs" - "/tmp/container/mariadb:/data/mariadb"
\ No newline at end of file diff --git a/Services/mariadb/my.cnf b/Services/mariadb/my.cnf index ac882ac..309cc10 100644 --- a/Services/mariadb/my.cnf +++ b/Services/mariadb/my.cnf @@ -1,29 +1,36 @@ [mysqld] -datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock -# Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 -# Settings user and group are ignored when systemd is used. -# If you need to run mysqld under a different user or group, -# customize your systemd unit file for mysqld/mariadb according to the -# instructions in http://fedoraproject.org/wiki/Systemd - -# Currently, there are mariadb and community-mysql packages in Fedora. -# This particular config file is included in respective RPMs of both of them, -# so the following settings are general and will be also used by both of them. -# Otherwise the RPMs would be in conflict. -# Settings for particular implementations like MariaDB are then -# defined in appropriate sections; for MariaDB server in [mariadb] section in -# /etc/my.cnf.d/server.cnf (part of mariadb-server). -# It doesn't matter that we set these settings only for [mysqld] here, -# because they will be read and used in mysqld_safe as well. -log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid +[mariadb] +pid-file=/var/run/mariadb/mariadb.pid + [mysqld_safe] -# -# include all files from the config directory -# -!includedir /etc/my.cnf.d +[mysql] + +[mysql_upgrade] + +[mysqladmin] + +[mysqlbinlog] + +[mysqlcheck] + +[mysqldump] + +[mysqlimport] + +[mysqlshow] + +[mysqlslap] + +[client] + +[client-mariadb] + +[server] + +[embedded] diff --git a/Services/mariadb/run.sh b/Services/mariadb/run.sh index cd22b99..a2e4dfb 100644 --- a/Services/mariadb/run.sh +++ b/Services/mariadb/run.sh @@ -27,7 +27,7 @@ function display_container_mariadb_header { echo "+=====================================================" echo "| Container : $HOSTNAME" echo "| OS : $(</etc/redhat-release)" - echo "| Engine : " mysql -V + echo "| Engine : $(mysql -V)" if [ -v CONTAINER_TYPE ]; then echo "| Type : $CONTAINER_TYPE" fi @@ -57,8 +57,138 @@ function display_container_mariadb_header { # and start generating host keys function begin_config { echo "=> BEGIN MARIADB CONFIGURATION" + if [[ ! -d $DATA_PATH ]]; then + echo "log directory $DATA_PATH not found" + mkdir -p $DATA_PATH; chmod 0774 $DATA_PATH + echo "data directory $DATA_PATH CREATED" + else + echo "data directory $DATA_PATH EXIST" + fi + if [[ ! -d $LOG_PATH ]]; then + echo "log directory $LOG_PATH not found" + mkdir -p $LOG_PATH; chmod 0774 $LOG_PATH + echo "log directory $LOG_PATH CREATED" + else + echo "log directory $LOG_PATH EXIST" + fi + chmod 0774 $DATA_PATH $LOG_PATH; + chown mysql:mysql $DATA_PATH $LOG_PATH; + if [[ -d $LOADSQL_PATH ]]; then + echo "sql directory $LOADSQL_PATH EXIST" + chmod 0774 $LOADSQL_PATH; + chown mysql:mysql $LOADSQL_PATH + fi + VOLUME_HOME=$DATA_PATH/mysql + if [[ ! -d $VOLUME_HOME ]]; then + echo "mariadb directory is empty or uninitialized" + echo "Installing MariaDB in $DATA_PATH ..." + mysql_install_db > /dev/null 2>&1 + chown mysql:mysql -R $DATA_PATH + echo "Installing MariaDB in $DATA_PATH is DONE !" + else + echo "mariadb directory is initialized" + echo "Reusing MariaDB in $DATA_PATH ..." + chown mysql:mysql -R $DATA_PATH + fi + echo "" >> $MY_CONF + echo "[mysqld]" >> $MY_CONF + echo "datadir=$DATA_PATH" >> $MY_CONF + echo "log-error=$LOG_PATH/mysqld.log" >> $MY_CONF + echo "" >> $MY_CONF + echo "[mariadb]" >> $MY_CONF + echo "log-error=$LOG_PATH/mariadb.log" >> $MY_CONF +} + +function config_startserver { + echo "start database for initial setup" + /usr/bin/mysqld_safe > /dev/null 2>&1 & + RET=1 + while [[ RET -ne 0 ]]; do + echo "- waiting for database response" + sleep 1 + mysql -uroot -e "status" > /dev/null 2>&1 + RET=$? + done +} + +function config_stopserver { + echo "stop database after initial setup" + mysqladmin -uroot shutdown +} + + +function config_createadmin { + PASS=${MYSQL_ROOT_PASSWORD:-$(pwgen -s 12 1)} + _word=$( [ ${MYSQL_ROOT_PASSWORD} ] && echo "preset" || echo "random" ) + echo "Creating MariaDB admin 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" + echo "" + echo " +------------------------------------------------------" + echo " | You can now connect to this server using:" + echo " | user : admin" + echo " | password : $PASS" + echo " | shell : mysql -uadmin -p$PASS -h<host> -P<port>" + echo " +------------------------------------------------------" + echo "" } +function config_createuser { + if [[ -n "$MYSQL_USER" ]]; then + echo "Creating MariaDB $MYSQL_USER user with preset password" + mysql -uroot -e "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD'" + mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '$MYSQL_USER'@'%' WITH GRANT OPTION" + echo "" + echo " +------------------------------------------------------" + echo " | You can now connect to this server using:" + echo " | user : $MYSQL_USER" + echo " | password : $MYSQL_PASSWORD" + echo " | shell : mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -h<host> -P<port>" + echo " +------------------------------------------------------" + echo "" + fi +} + +function config_createdatabase { + if [[ -n "$MYSQL_DATABASE" ]]; then + echo "processing database " $MYSQL_DATABASE + if [[ ! -d $DIR_DB_DATA/$MYSQL_DATABASE ]]; then + echo "database " $MYSQL_DATABASE " doesn't exist" + mysql -uroot -e "CREATE DATABASE $MYSQL_DATABASE DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;" + echo "database " $MYSQL_DATABASE " CREATED" + else + echo "database " $MYSQL_DATABASE " already exist" + fi + else + echo "no database to create" + fi +} + +function config_importsql { + if [[ -n "$LOADSQL_PATH" ]]; then + echo "import sql data into " $MYSQL_DATABASE + if [[ ! -d $LOADSQL_PATH ]]; then + SCHEMALIST=$(find $LOADSQL_PATH/schema-*.sql -type f -printf "%f\n") + for SCHEMAFILE in $SCHEMALIST; do + echo "SET NAMES utf8;"|cat - $LOADSQL_PATH/$SCHEMAFILE > /tmp/out && mv /tmp/out $LOADSQL_PATH/$SCHEMAFILE + echo -n "Creating schema " $SCHEMAFILE " ... " + mysql -uroot $MYSQL_DATABASE < $LOADSQL_PATH/$SCHEMAFILE + echo " DONE" + done + DATALIST=$(find $LOADSQL_PATH/data-*.sql -type f -printf "%f\n") + for DATAFILE in $DATALIST; do + echo "SET NAMES utf8;"|cat - $LOADSQL_PATH/$DATAFILE > /tmp/out && mv /tmp/out $LOADSQL_PATH/$DATAFILE + echo -n "Creating data " $DATAFILE " ... " + mysql -uroot $MYSQL_DATABASE < $LOADSQL_PATH/$DATAFILE + echo " DONE" + done + fi + else + echo "no sql data to import into " $MYSQL_DATABASE + fi +} + + # End configuration process just before starting daemon function end_config { echo "=> END MARIADB CONFIGURATION" @@ -80,6 +210,11 @@ fi check_mariadb_environment | tee -a $STARTUPLOG display_container_mariadb_header | tee -a $STARTUPLOG begin_config | tee -a $STARTUPLOG +config_startserver | tee -a $STARTUPLOG +config_createadmin | tee -a $STARTUPLOG +config_createuser | tee -a $STARTUPLOG +config_createdatabase | tee -a $STARTUPLOG +config_importsql | tee -a $STARTUPLOG +config_stopserver | tee -a $STARTUPLOG end_config | tee -a $STARTUPLOG -display_container_started | tee -a $STARTUPLOG start_daemon | tee -a $STARTUPLOG diff --git a/Services/mariadb/sx/mariadb.sh b/Services/mariadb/sx/mariadb.sh deleted file mode 100644 index 7ff208e..0000000 --- a/Services/mariadb/sx/mariadb.sh +++ /dev/null @@ -1,178 +0,0 @@ -#!/bin/bash - -export TERM=dumb -export logfile="/var/logs/mysql/mysqld.log" - -# Begin configuration before starting daemonized process -# redirect mysql.log to /dev/stderr -function begin_config { - echo "=> Begin mariadb configuration for host $HOSTNAME" - ln -s /dev/stderr $logfile - if [ "$(ls -1 /var/lib/mysql | wc -l)" -le "3" ]; then - echo "=> directory /var/lib/mysql is empty, start mysql installation ..." - install_db - else - echo "=> data found in /var/lib/mysql, skip mysql installation ..." - fi; - if [ ! -f /var/lib/mysql/mysql.sock ]; then - echo "=> mysqld is not running, start server ..." - start_server - else - echo "=> mysqld is already running ..." - fi; - update_rootuser -} - -# End configuration process just before starting daemon -# stop output of mysql.log to /dev/stderr and create mysql.log file -function end_config { - stop_server - rm $logfile - touch $logfile - echo "=> End mariadb configuration ..." -} - -# Preform installation of database structure. Must be used when no -# database is already set -function install_db { - echo "===> Installing mariadb databases ..." - mysql_install_db -u mysql > /dev/null 2>&1 - chown -R mysql:mysql /var/lib/mysql -} - -# Start the mysqld server in background. Used to perform config -# against the database structure such as user creation -function start_server { - echo "===> Starting mariadb server ..." - /usr/bin/mysqld_safe > /dev/null 2>&1 & - sleep 8 -} - -# Stop the mysqld server running in background. -function stop_server { - echo "===> Stopping mariadb server ..." - killall mysqld mysqld_safe - sleep 8 -} - -# Start the mysqld server as a deamon and execute it inside -# the running shell -function start_daemon { - echo "=> Starting mariadb daemon ..." - exec /usr/libexec/mysqld -} - -# Set new root password and grant permissions to all databases -function update_rootuser { - if [ "$mysql_newadminpwd" = "" ]; then - export mysql_newadminpwd=$(pwgen 13 1); - fi - local n=$mysql_newadminpwd; - echo "===> Update root user password and permission" - mysqladmin -u root password $n - mysql -u root -p$n -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$n' WITH GRANT OPTION; FLUSH PRIVILEGES;" - unset mysql_newadminpwd; - export MARIADB_ROOTPWD=$n; - echo "========================================================================"; - echo "You can now connect to this MariaDB Server using the following credentials:"; - echo " "; - echo " user type : administrator"; - echo " username : root"; - echo " password : $n"; - echo " "; - echo " mysql -u root -p$n"; - echo "========================================================================"; - echo " "; - return 0 -} - -# Find all sqlfiles in /tmp/ and import then using admin user -function import_sqlfiles { - local filedir=$1; local p=$MARIADB_ROOTPWD; local del=$2; - if [ "$(ls -1 $filedir | wc -l)" -ge "1" ]; then - echo "=> Found SQL files to import ..." - for filename in "$filedir"; do - import_sqlfile $filename $del - done; - fi; - return 0 -} - -# Find all sqlfiles in /tmp/ and import then using admin user -function import_sqlfile { - local filename=$1; local p=$MARIADB_ROOTPWD; local del=$2; - if [ -f "$filename" ]; then - echo "===> Importing sql file : $filename" - mysql -u root -p$p < $filename - if [ "$del" = "delete"]; then - rm -f $filename - echo "====> Deleting $filename after import" - fi; - else - echo "====> Could not find sql file $filename. Skip import..." - fi; - return 0 -} - -# Set new root password and grant permissions to all databases -function create_userdb { - local userdb="$1"; local pass="$2"; local p=$MARIADB_ROOTPWD; - if [ "$pass" = "" ]; then - local pass=$(pwgen 13 1); - fi - echo "===> Create new user $userdb with database $userdb" - mysql -u root -p$p -e "CREATE USER '$userdb'@'%';SET PASSWORD FOR '$userdb'@'%' = PASSWORD('$pass');\ - CREATE USER '$userdb'@'localhost';SET PASSWORD FOR '$userdb'@'localhost' = PASSWORD('$pass');\ - DROP DATABASE IF EXISTS $userdb; \ - CREATE DATABASE $userdb DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; \ - GRANT ALL PRIVILEGES ON $userdb.* TO '$userdb'@'%' IDENTIFIED BY '$pass'; \ - GRANT ALL PRIVILEGES ON $userdb.* TO '$userdb'@'localhost' IDENTIFIED BY '$pass'; \ - FLUSH PRIVILEGES;" - echo "========================================================================"; - echo "You can now connect to this MariaDB Server using the following credentials:"; - echo " "; - echo " user type : user "; - echo " username : $userdb "; - echo " password : $pass"; - echo " database : $userdb "; - echo " "; - echo " mysql -u $userdb -p$pass $userdb"; - echo "========================================================================"; - echo " "; - return 0 -} - -# Set new root password and grant permissions to all databases -function create_user { - local user="$1"; local pass="$2"; local p=$MARIADB_ROOTPWD; - if [ "$pass" = "" ]; then - local pass=$(pwgen 13 1); - fi - echo "===> Create new user $user" - mysql -u root -p$p -e "CREATE USER '$user'@'%';SET PASSWORD FOR '$user'@'%' = PASSWORD('$pass');\ - CREATE USER '$user'@'localhost';SET PASSWORD FOR '$user'@'localhost' = PASSWORD('$pass');" - echo "========================================================================"; - echo "You can now connect to this MariaDB Server using the following credentials:"; - echo " "; - echo " user type : user "; - echo " username : $user "; - echo " password : $pass "; - echo " "; - echo " mysql -u $user -p$pass"; - echo "========================================================================"; - echo " "; - return 0 -} - -# Create a new database -function create_db { - local db=$1; local p=$MARIADB_ROOTPWD; - echo "===> Create new database $db" - mysql -u root -p$p -e "CREATE DATABASE $db DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;"; - echo "===> New database $db CREATED" - return 0 -} - -if [[ "$0" == *"mariadb.sh" && ! $1 = "" ]];then - eval "$@"; -fi
\ No newline at end of file diff --git a/Services/mariadb/sx/mariadb_run.sh b/Services/mariadb/sx/mariadb_run.sh deleted file mode 100644 index 7dcf9b2..0000000 --- a/Services/mariadb/sx/mariadb_run.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash -source /sx/mariadb.sh - -begin_config -## if you wan't to add a new user with database -#create_userdb 'dbuser1' 'password' -## if you wan't to add a new user with database (generated password) -#create_userdb 'dbuser2' -## if you wan't to add a new user -#create_user 'username' 'password' -## if you wan't to add a new database -#create_db 'dbname' -## Execute an SQL request to get the user list -#mysql -u root -p$MARIADB_ROOTPWD -e 'select user, host FROM mysql.user;' -## Execute SQL scripts located into a directory -#import_sqlfiles /tmp/sql/*.sql -## Execute a single SQL script and delete it -#import_sqlfile /tmp/sql/example.sql delete -end_config - -start_daemon
\ No newline at end of file |