-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate-database.sh
executable file
·251 lines (251 loc) · 10.3 KB
/
migrate-database.sh
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env bash
set -eu
TOPDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=Scripts/lib/test/logging.sh
. "$TOPDIR/Scripts/lib/logging.sh"
# shellcheck source=Scripts/lib/test/parsing.sh
. "$TOPDIR/Scripts/lib/parsing.sh"
# shellcheck source=Scripts/lib/test/shell_prompt.sh
. "$TOPDIR/Scripts/lib/shell_prompt.sh"
runner=$(parse_arg "-[rR]+|--runner" "$@")
docker=$(parse_arg "--docker" "$@")
travis=$(parse_arg "--travis" "$@")
pargs=$(parse_arg_trim "-[rR]+|--runner|--docker|--travis" "$@")
if [ -n "$runner" ]; then
slogger -st "$0" "Bootargs...: ${pargs}"
# shellcheck source=Scripts/bootargs.sh
. "$TOPDIR/Scripts/bootargs.sh" "$@"
else
slogger -st "$0" "Locally Testing values, bootargs...: ${pargs}"
# shellcheck source=Scripts/fooargs.sh
. "$TOPDIR/Scripts/fooargs.sh" "$@"
fi
LOG=$(new_cake_log "$travis" "$runner" "$docker") && slogger -st "$0" "$LOG"
usage=("" \
"Usage: $0 [sockfile.sock] [-u] [-y|n] [-o] [-p <word>] [-t <word>] [-i] [--sql-password=<password>] [--test-sql-password=<password>]" \
" To initialize the databases, enter in the ${MYSQL_HOST} host terminal: $0 -u -i" \
" -------------" \
" file.sock Set the socket file to connect SQL database" \
" -u Update the database in app/config/Schema/" \
" -y Overwrite database.php and default socket file" \
" -i --sql-password=<word> --test-sql-password=<word>" \
" Initialize databases with new passwords and reset MYSQL_DATABASE and TEST_DATABASE_NAME privileges" \
" -n, --runner" \
" CircleCI and self-host runner: resets database.php, keep socket and update the database" \
" Doesn't use the socket file" \
" --travis" \
" Travis CI job" \
" -p=<password>" \
" Exports MYSQL_ROOT_PASSWORD" \
" -t=<password>" \
" Exports MYSQL_PASSWORD" \
" --database=<name>" \
" Exports MYSQL_DATABASE" \
" --testunitbase=<name>" \
" Exports TEST_DATABASE_NAME" \
" --enable-ed25519-plugin" \
" Enable MariaDB plugin https://mariadb.com/kb/en/authentication-plugin-ed25519/" \
" -v, --verbose" \
" Outputs more debug information" \
" -h, --help Displays this help" \
"")
sql_connect="mysql"
# shellcheck disable=SC2153
sql_connect_host="-h ${MYSQL_HOST} -P ${MYSQL_TCP_PORT}"
dbfile=app/config/database.template
schemafile=app/config/Schema/schema.template
sockfile=/tmp/mysqld.sock
config_app_checked="-Y"
mode=0x00000
test_bit=0x10000
runner_bit=0x01000
update_bit=0x00100
docker_bit=0x00010
initialize_bit=0x00001
saved=( "$@" )
authentication_plugin=0
mysql_host="%"
cx_args="--connection=default"
# test_args="app AllTests --stderr"
test_args="app Controller/PagesController --stderr >> $LOG"
MARIADB_SHORT_NAME=$(docker_name "$SECONDARY_HUB")
while [ "$#" -gt 0 ]; do case "$1" in
--enable-ed25519-plugin*)
slogger -st "$0" "Enabled auth_ed25519 plugin for passwords..."
log_warning_msg "Plugin Not available from PHP PDO connect (you should avoid using it)"
authentication_plugin="ed25519";;
--docker )
mode=$((mode | docker_bit))
bash -c "./Scripts/start_daemon.sh ${docker}"
# Running docker ... mysql's allowed to connect without any local mysql installation
docker exec "$MARIADB_SHORT_NAME" hostname 2>> "$LOG"
sql_connect="docker exec $MARIADB_SHORT_NAME mysql"
sockfile="$(pwd)/deployment/images/mysqldb/mysqld/mysqld.sock"
;;
-[uU]* )
mode=$((mode | update_bit))
;;
--connection=test )
cx_args="$1"
mode=$((mode | test_bit))
;;
--connection* )
cx_args="$1";;
*.sock ) sockfile=$1;;
-[iI]* )
mode=$((mode | initialize_bit))
;;
--sql-password*)
OPTIND=1
parse_sql_password "set_DATABASE_PASSWORD" "Altering ${DATABASE_USER} password" "$@"
shift $((OPTIND -1))
;;
--test-sql-password*)
mode=$((mode | test_bit))
cx_args="--connection=test"
OPTIND=1
parse_sql_password "set_MYSQL_PASSWORD" "Altering ${MYSQL_USER} password" "$@"
shift $((OPTIND -1))
;;
-[vV]*|--verbose )
# Reset passed args (shift reset)
text=("" \
"Passed params : $0 ${saved[*]}" \
"and environment VARIABLES:" \
"$(export -p | grep "DATABASE\|MYSQL")" \
"")
printf "%s\n" "${text[@]}"
cx_args="${cx_args} -v"
test_args="${test_args} -v"
;;
-[hH]*|--help )
printf "%s\n" "${usage[@]}"
exit 0;;
-[nN]*|--runner|--travis)
mode=$((mode | runner))
sockfile=""
config_app_checked="-N"
;;
-[pP]* )
parse_sql_password "MYSQL_ROOT_PASSWORD" "current ${DATABASE_USER} password" "$@"
shift $((OPTIND -1))
;;
-[tT]* )
mode=$((mode | test_bit))
cx_args="--connection=test"
parse_sql_password "MYSQL_PASSWORD" "current ${MYSQL_USER} password" "$@"
shift $((OPTIND -1))
;;
--database*)
# Transform long options to short ones
arg=$1; shift
# shellcheck disable=SC2046
set -- $(echo "${arg}" \
| awk 'BEGIN{ FS="[ =]+" }{ print "-d " $2 }') "$@"
parse_and_export "d" "MYSQL_DATABASE" "${DATABASE_USER} database name" "$@"
shift $((OPTIND -1))
;;
--testunitbase*)
# Transform long options to short ones
arg=$1; shift
# shellcheck disable=SC2046
set -- $(echo "${arg}" \
| awk 'BEGIN{ FS="[ =]+" }{ print "-u " $2 }') "$@"
mode=$((mode | test_bit))
cx_args="--connection=test"
parse_and_export "u" "TEST_DATABASE_NAME" "${MYSQL_USER} database name" "$@"
shift $((OPTIND -1))
;;
*) echo "Invalid parameter: ${BASH_SOURCE[0]} $1" && exit 1;;
esac
shift; #echo "$@";
done
# configure user application database and eventually alter user database access
# shellcheck disable=SC2154
shell_prompt "$TOPDIR/Scripts/config_app_database.sh ${dbfile} ${schemafile} ${sockfile} ${docker}" \
"${cyan}Setup ${dbfile} connection and socket\n${nc}" "$config_app_checked"
if [[ $((mode & initialize_bit)) -gt 0 ]]; then
#; ---------------------------------- set MYSQL_ROOT_PASSWORD
export set_DATABASE_PASSWORD=${set_DATABASE_PASSWORD:-$MYSQL_ROOT_PASSWORD}
# shellcheck disable=SC2154
log_warning_msg "${red}WARNING: You will modify SQL ${DATABASE_USER} password !${nc}"
prompt="-Y"
if [ -z "${set_DATABASE_PASSWORD}" ]; then
# shellcheck disable=SC2154
log_warning_msg "${orange}WARNING: Using blank password for ${DATABASE_USER} !!${nc}"
prompt=${DEBIAN_FRONTEND:-''}
fi
if [ $authentication_plugin = "ed25519" ]; then
identifiedby="IDENTIFIED VIA ed25519 USING '${set_DATABASE_PASSWORD}'"
else
identifiedby="identified by '${set_DATABASE_PASSWORD}'"
fi
# ALTER USER is MariaDB 10.2 and above waiting for ARM binary
# "-e \"alter user '${DATABASE_USER}'@'${mysql_host}' ${identifiedby};\"" \
args=(\
"-e \"select version();\"" \
"-e \"use mysql;\"" \
"-e \"create user if not exists '${DATABASE_USER}'@'${mysql_host}' ${identifiedby};\"" \
"-e \"SET PASSWORD FOR '${DATABASE_USER}'@'${mysql_host}' = PASSWORD('${set_DATABASE_PASSWORD}');\"" \
"-e \"grant all PRIVILEGES on *.* to '${DATABASE_USER}'@'${mysql_host}' WITH GRANT OPTION;\"" \
"-e \"flush PRIVILEGES;\"" \
"-e \"create database if not exists ${MYSQL_DATABASE} default character set='utf8' default collate='utf8_bin';\"" \
"-e \"create database if not exists ${TEST_DATABASE_NAME};\"" \
"-e \"create database if not exists ${TEST_DATABASE_NAME}_2;\"" \
"-e \"create database if not exists ${TEST_DATABASE_NAME}_3;\"" \
"-e \"select plugin from user where user='${DATABASE_USER}';\"" \
"-e \"show databases;\"" \
"")
# enable failed-login tracking, such that three consecutive incorrect passwords cause temporary account locking for two days:
# "-e \"FAILED_LOGIN_ATTEMPTS 3 PASSWORD_LOCK_TIME 2;\""
slogger -st "$0" "Forked script to keep hidden table user secrets..."
password=""
user="${DATABASE_USER}"
if [ -n "${MYSQL_ROOT_PASSWORD:-}" ]; then
password="--password=${MYSQL_ROOT_PASSWORD}"
user="root"
fi
shell_prompt "${sql_connect} ${sql_connect_host} -u ${user} ${password} \
${args[*]} >> $LOG 2>&1" "Import default identities" "$prompt"\
&& export MYSQL_ROOT_PASSWORD=${set_DATABASE_PASSWORD}
#; ---------------------------------- set MYSQL_PASSWORD
slogger -st "$0" "\r${red}WARNING: You will modify SQL ${MYSQL_USER} password !${nc}"
export set_MYSQL_PASSWORD=${set_MYSQL_PASSWORD:-$MYSQL_PASSWORD}
if [ -z "${set_MYSQL_PASSWORD}" ]; then
slogger -st "$0" "\r${orange}WARNING: Using blank password for ${MYSQL_USER} !!${nc}"
prompt=${DEBIAN_FRONTEND:-''}
fi
if [ $authentication_plugin = "ed25519" ]; then
identifiedby="IDENTIFIED VIA ed25519 USING '${set_MYSQL_PASSWORD}'"
else
identifiedby="identified by '${set_MYSQL_PASSWORD}'"
fi
# ALTER USER is MariaDB 10.2 and above waiting for ARM binary
# "-e \"alter user '${MYSQL_USER}'@'${mysql_host}' ${identifiedby};\"" \
args=(\
"-e \"use mysql;\"" \
"-e \"create user if not exists '${MYSQL_USER}'@'${mysql_host}' ${identifiedby};\"" \
"-e \"SET PASSWORD FOR '${MYSQL_USER}'@'${mysql_host}'=PASSWORD('${set_MYSQL_PASSWORD}');\"" \
"-e \"grant all PRIVILEGES on ${MYSQL_DATABASE}.* to '${MYSQL_USER}'@'${mysql_host}';\"" \
"-e \"grant all PRIVILEGES on ${TEST_DATABASE_NAME}.* to '${MYSQL_USER}'@'${mysql_host}';\"" \
"-e \"grant all PRIVILEGES on ${TEST_DATABASE_NAME}_2.* to '${MYSQL_USER}'@'${mysql_host}';\"" \
"-e \"grant all PRIVILEGES on ${TEST_DATABASE_NAME}_3.* to '${MYSQL_USER}'@'${mysql_host}';\"" \
"-e \"flush PRIVILEGES;\"" \
"-e \"select plugin from user where user='${MYSQL_USER}';\"")
# enable failed-login tracking, such that three consecutive incorrect passwords cause temporary account locking for two days:
# "-e \"FAILED_LOGIN_ATTEMPTS 3 PASSWORD_LOCK_TIME 2;\""
shell_prompt "${sql_connect} ${sql_connect_host} -u ${user} ${password} \
${args[*]} >> $LOG 2>&1" "Import test identities" "$prompt" \
&& export MYSQL_PASSWORD=${set_MYSQL_PASSWORD}
check_log "$LOG"
fi
if [[ $((mode & (test_bit | update_bit | runner_bit | docker_bit))) -gt 0 ]]; then
pargs=" $travis $docker $runner"
if [[ $((mode & test_bit)) -gt 0 ]]; then
pargs="$pargs test $test_args"
elif [[ $((mode & update_bit)) -gt 0 ]]; then
pargs="$pargs update $cx_args"
fi
bash -c "./Scripts/bootstrap.sh $pargs"
check_log "$LOG"
fi