Skip to content

Commit 14404a1

Browse files
committed
Add upgrade check and warning
1 parent 92b5f91 commit 14404a1

File tree

5 files changed

+128
-83
lines changed

5 files changed

+128
-83
lines changed

readme-vars.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,17 @@ app_setup_block: |
9494
```
9595
This will have the same effect as setting the `REMOTE_SQL` environment variable. The sql will only be run on the containers first boot and setup.
9696
97+
### Upgrading
98+
99+
When this container initializes, if `MYSQL_ROOT_PASSWORD` is set an upgrade check will run. If an upgrade is required the log will indicate the need to run:
100+
101+
```
102+
mariadb-upgrade -u root password <PASSWORD>
103+
```
104+
97105
# changelog
98106
changelogs:
107+
- { date: "09.12.22:", desc: "Add upgrade check warning." }
99108
- { date: "11.10.22:", desc: "Rebase master to Alpine 3.16, migrate to s6v3, remove password escape logic which caused problems for a small subset of users." }
100109
- { date: "06.07.21:", desc: "Rebase master to alpine." }
101110
- { date: "03.07.21:", desc: "Rebase to 3.14." }

root/etc/s6-overlay/s6-rc.d/init-mariadb-config/run

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/with-contenv bash
2+
# shellcheck shell=bash
23

34
# make folders if required
45
mkdir -p \
Lines changed: 112 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,20 @@
11
#!/usr/bin/with-contenv bash
2+
# shellcheck shell=bash
23

3-
# set start function that creates user and password, used later
4-
start_mysql(){
5-
mysqld --datadir="${DATADIR}" --init-file="${tempSqlFile}" --user=abc &
4+
# set start function used later
5+
start_mariadb() {
6+
mariadbd --datadir="${DATADIR}" --init-file="${tempSqlFile}" --user=abc &
67
pid="$!"
78
RET=1
89
while [[ RET -ne 0 ]]; do
9-
mysql -uroot -e "status" > /dev/null 2>&1
10+
mariadb -uroot -e "status" >/dev/null 2>&1
1011
RET=$?
1112
sleep 1
1213
done
1314
}
1415

15-
# test for existence of mysql folder in datadir and start initialise if not present
16-
# BEGIN: No indentation due to heredocs
17-
if [[ ! -d "${DATADIR}/mysql" ]]; then
18-
19-
# load env file if it exists
20-
if [[ -f "/config/env" ]]; then
21-
source /config/env
22-
fi
23-
24-
# set basic sql command
16+
# make temp sql init file
2517
tempSqlFile=$(mktemp)
26-
cat > "${tempSqlFile}" <<-EOSQL
27-
DELETE FROM mysql.user WHERE user <> 'mariadb.sys';
28-
EOSQL
29-
30-
# set what to display if no password set with variable MYSQL_ROOT_PASSWORD
31-
NOPASS_SET=$(mktemp)
32-
cat > "${NOPASS_SET}" <<-EOFPASS
33-
#################################################################
34-
# No root password or too short a password, min of 4 characters #
35-
# No root password will be set, this is not a good thing #
36-
# You shoud set one after initialisation with the command #
37-
# mysqladmin -u root password <PASSWORD> #
38-
#################################################################
39-
EOFPASS
4018

4119
# test for empty password variable, if it's set to 0 or less than 4 characters
4220
if [[ -z "${MYSQL_ROOT_PASSWORD}" ]]; then
@@ -45,81 +23,136 @@ else
4523
TEST_LEN=${#MYSQL_ROOT_PASSWORD}
4624
fi
4725

48-
if [[ "${TEST_LEN}" -lt "4" ]]; then
49-
MYSQL_PASS="CREATE USER 'root'@'%' IDENTIFIED BY '' ;"
50-
else
51-
MYSQL_PASS="CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;"
52-
fi
26+
# test for existence of mysql folder in datadir and start initialise if not present
27+
if [[ ! -d "${DATADIR}/mysql" ]]; then
28+
# load env file if it exists
29+
if [[ -f "/config/env" ]]; then
30+
# shellcheck source=/dev/null
31+
source /config/env
32+
fi
33+
34+
# set basic sql command
35+
cat >"${tempSqlFile}" <<-EOSQL
36+
DELETE FROM mysql.user WHERE user <> 'mariadb.sys';
37+
EOSQL
5338

54-
# Make sure all user and database settings are set and pass is more than 4 characters
55-
# At the end change to default database created with environment variables to run init and remote scripts there
56-
if [[ "${MYSQL_USER+x}" ]] && \
57-
[[ "${MYSQL_DATABASE+x}" ]] && \
58-
[[ "${MYSQL_PASSWORD+x}" ]] && \
59-
[[ "${#MYSQL_PASSWORD}" -gt "3" ]]; then
60-
read -r -d '' MYSQL_DB_SETUP << EOM
39+
if [[ "${TEST_LEN}" -lt "4" ]]; then
40+
MYSQL_PASS="CREATE USER 'root'@'%' IDENTIFIED BY '' ;"
41+
else
42+
MYSQL_PASS="CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;"
43+
fi
44+
45+
# Make sure all user and database settings are set and pass is more than 4 characters
46+
# At the end change to default database created with environment variables to run init and remote scripts there
47+
if [[ "${MYSQL_USER+x}" ]] &&
48+
[[ "${MYSQL_DATABASE+x}" ]] &&
49+
[[ "${MYSQL_PASSWORD+x}" ]] &&
50+
[[ "${#MYSQL_PASSWORD}" -gt "3" ]]; then
51+
read -r -d '' MYSQL_DB_SETUP <<-EOM
6152
CREATE DATABASE \`${MYSQL_DATABASE}\`;
6253
CREATE USER '${MYSQL_USER}'@'%' IDENTIFIED BY '${MYSQL_PASSWORD}';
6354
GRANT ALL PRIVILEGES ON \`${MYSQL_DATABASE}\`.* TO '${MYSQL_USER}'@'%';
6455
USE \`${MYSQL_DATABASE}\`;
6556
EOM
66-
fi
57+
fi
6758

68-
# add rest of sql commands based on password set or not
69-
cat >> "${tempSqlFile}" <<-EONEWSQL
59+
# add rest of sql commands based on password set or not
60+
cat >>"${tempSqlFile}" <<-EONEWSQL
7061
$MYSQL_PASS
7162
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
7263
DROP DATABASE IF EXISTS test ;
7364
$MYSQL_DB_SETUP
7465
EONEWSQL
7566

76-
echo "Setting Up Initial Databases"
67+
echo "Setting Up Initial Databases"
7768

78-
# add all sql from a user defined directory on first init
79-
if [[ -e "/config/initdb.d" ]] && [[ -n "$(/bin/ls -A /config/initdb.d/*.sql 2>/dev/null)" ]]; then
80-
cat /config/initdb.d/*.sql >> "${tempSqlFile}"
81-
fi
69+
# add all sql from a user defined directory on first init
70+
if [[ -e "/config/initdb.d" ]] && [[ -n "$(/bin/ls -A /config/initdb.d/*.sql 2>/dev/null)" ]]; then
71+
cat /config/initdb.d/*.sql >>"${tempSqlFile}"
72+
fi
8273

83-
chown -R abc:abc "${tempSqlFile}"
74+
# ingest remote sql if REMOTE_SQL is set
75+
if [[ -n "${REMOTE_SQL+set}" ]]; then
76+
IFS=, read -ra URLS <<<"${REMOTE_SQL}"
77+
for URL in "${URLS[@]}"; do
78+
if [[ "$(curl -I -sL -w "%{http_code}" "${URL}" -o /dev/null)" == 200 ]]; then
79+
curl -sL "${URL}" >>"${tempSqlFile}"
80+
fi
81+
done
82+
fi
8483

84+
# set some permissions needed before we begin
85+
lsiown -R abc:abc "${tempSqlFile}" /config/log/mysql /var/run/mysqld /var/lib/mysql
86+
chmod -R 777 /config/log/mysql /var/run/mysqld /var/lib/mysql
8587

86-
# ingest remote sql if REMOTE_SQL is set
87-
if [[ -n "${REMOTE_SQL+set}" ]]; then
88-
IFS=, read -ra URLS <<< "${REMOTE_SQL}"
89-
for URL in "${URLS[@]}"; do
90-
if [[ "$(curl -I -sL -w "%{http_code}" "${URL}" -o /dev/null)" == 200 ]]; then
91-
curl -sL "${URL}" >> "${tempSqlFile}"
92-
fi
93-
done
94-
fi
95-
# set some permissions needed before we begin initialising
96-
chown -R abc:abc /config/log/mysql /var/run/mysqld /var/lib/mysql
97-
chmod -R 777 /config/log/mysql /var/run/mysqld /var/lib/mysql
88+
# initialise database structure
89+
mariadb-install-db --datadir="${DATADIR}" --user=abc --auth-root-authentication-method=normal
90+
91+
# start mariadb and apply our sql commands we set above
92+
start_mariadb
93+
94+
# shut down after apply sql commands, waiting for pid to stop
95+
mariadb-admin -u root shutdown
96+
wait "${pid}"
97+
echo "Database Setup Completed"
9898

99-
# initialise database structure
100-
mysql_install_db --datadir="${DATADIR}" --user=abc --auth-root-authentication-method=normal
99+
# display a message about password if not set or too short
100+
if [[ "${TEST_LEN}" -lt "4" ]]; then
101+
cat <<-EOFPASS
101102

102-
# start mysql and apply our sql commands we set above
103-
start_mysql
104103

105-
# shut down after apply sql commands, waiting for pid to stop
106-
mysqladmin -u root shutdown
107-
wait "${pid}"
108-
echo "Database Setup Completed"
109104

110-
# display a message about password if not set or too short
111-
if [[ "${TEST_LEN}" -lt "4" ]]; then
112-
printf '\n\n\n%s\n\n\n' "$(<"${NOPASS_SET}")"
113-
sleep 5s
105+
#################################################################
106+
# No root password or too short a password, min of 4 characters #
107+
# No root password will be set, this is not a good thing #
108+
# You shoud set one after initialisation with the command #
109+
# mariadb-admin -u root password <PASSWORD> #
110+
#################################################################
111+
112+
113+
114+
EOFPASS
115+
116+
sleep 5s
117+
fi
118+
else
119+
if [[ "${TEST_LEN}" -lt "4" ]]; then
120+
echo "MYSQL_ROOT_PASSWORD is not set or too short, skipping upgrade check"
121+
else
122+
# set some permissions needed before we begin
123+
lsiown -R abc:abc "${tempSqlFile}"
124+
125+
# start mariadb
126+
start_mariadb
127+
128+
# display a message about upgrading database if needed
129+
if mariadb-upgrade -u root -p"${MYSQL_ROOT_PASSWORD}" --check-if-upgrade-is-needed >/dev/null 2>&1; then
130+
cat <<-EOFPASS
131+
132+
133+
134+
#################################################################
135+
# An upgrade is required on your databases. Run the command #
136+
# mariadb-upgrade -u root password <PASSWORD> #
137+
#################################################################
138+
139+
140+
141+
EOFPASS
142+
143+
sleep 5s
144+
fi
145+
146+
# shut down after apply sql commands, waiting for pid to stop
147+
mariadb-admin -u root shutdown
148+
wait "${pid}"
149+
echo "Database Upgrade Check Completed"
150+
fi
114151
fi
115152

116153
# clean up any old install files from /tmp
117-
rm -f "${NOPASS_SET}"
118154
rm -f "${tempSqlFile}"
119155

120-
# END: No indentation due to heredocs
121-
fi
122-
123156
# own the folder the pid for mysql runs in
124-
chown -R abc:abc /var/run/mysqld
125-
chown -R abc:abc /config
157+
lsiown -R abc:abc /var/run/mysqld
158+
lsiown -R abc:abc /config

root/etc/s6-overlay/s6-rc.d/svc-mariadb/finish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/with-contenv bash
2+
# shellcheck shell=bash
23

34
echo "Caught SIGTERM signal!"
45

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#!/usr/bin/with-contenv bash
2+
# shellcheck shell=bash
23

34
s6-notifyoncheck -d -n 300 -w 1000 -c "nc -z localhost 3306" \
45
s6-setuidgid abc \
5-
/usr/bin/mariadbd-safe \
6-
--datadir="${DATADIR}" \
7-
--pid-file=/var/run/mysqld/mysqld.pid \
8-
--user=abc &
6+
/usr/bin/mariadbd-safe \
7+
--datadir="${DATADIR}" \
8+
--pid-file=/var/run/mysqld/mysqld.pid \
9+
--user=abc &
910

1011
wait

0 commit comments

Comments
 (0)