summaryrefslogtreecommitdiffstats
path: root/Services/mariadb/run.sh
blob: a2e4dfb71a43d318a8d154a5b0e99fbeeb2f432f (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
source /bin/sx-lib.sh

function check_mariadb_environment {
    check_environment
    if [ ! -v SERVER_NAME ]; then
        SERVER_NAME="localhost"
        export SERVER_NAME
        echo "! WARNING : environment var SERVER_NAME is missing..."
        echo "! WARNING : auto-assigned value : $SERVER_NAME"
    fi
    if [ ! -v DOC_ROOT ]; then
        DOC_ROOT="/data/apache"
        export DOC_ROOT
    fi
    if [ ! -v APP_PATH ]; then
        APP_PATH="/data/apache"
        export APP_PATH
    fi
    if [ ! -v LOG_PATH ]; then
        LOG_PATH="/data/logs/apache"
        export LOG_PATH
    fi
}

function display_container_mariadb_header {
    echo "+====================================================="
    echo "| Container   : $HOSTNAME"
    echo "| OS          : $(</etc/redhat-release)"
    echo "| Engine      : $(mysql -V)"
    if [ -v CONTAINER_TYPE ]; then
        echo "| Type        : $CONTAINER_TYPE"
    fi
    if [ -v CONTAINER_INSTANCE ]; then
        echo "| Instance    : $CONTAINER_INSTANCE"
    fi
    if [ -v CONTAINER_SERVICE ]; then
        echo "| Service     : $CONTAINER_SERVICE"
    fi
    if [ -v CONTAINER_SERVICE ]; then
        echo "| ServerName  : $SERVER_NAME"
    fi
    if [ -v APP_PATH ]; then
        echo "| Data path   : $DATA_PATH"
    fi
    if [ -v LOG_PATH ]; then
        echo "| Log path    : $LOG_PATH"
    fi
    if [ -v LOADSQL_PATH ]; then
        echo "| sql path    : $LOADSQL_PATH"
    fi
    echo "+====================================================="
}


# Begin configuration before starting daemonized process
# 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"
}

# Start the mariadb server as a deamon and execute it inside 
# the running shell
function start_daemon {
    echo "=> Starting mariadb daemon ..."
    display_container_started
    exec mysqld_safe
}


if [[ "$0" == *"run.sh" && ! $1 = "" ]];then
    eval "$@"; 
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
start_daemon | tee -a $STARTUPLOG