forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploydb.sh
More file actions
executable file
·104 lines (86 loc) · 3.42 KB
/
deploydb.sh
File metadata and controls
executable file
·104 lines (86 loc) · 3.42 KB
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
#!/bin/bash
set -e
user="$1"
password="$2"
host="$3"
port="$4"
MYSQL='mysql'
if [[ `id -u` -ne 0 ]] && [[ x"$user" = x"root" ]]; then
MYSQL='sudo mysql'
fi
if command -v greatdb &> /dev/null; then
MYSQL='greatdb'
if [[ `id -u` -ne 0 ]] && [[ x"$user" = x"root" ]]; then
MYSQL='sudo greatdb'
fi
fi
base=`dirname $0`
if [[ ! -n $host ]] || [[ ! -n $port ]];then
loginCmd="--user=$user --password=$password"
else
loginCmd="--user=$user --password=$password --host=$host --port=$port"
fi
# Detect MySQL version
# Extract major version number from various MySQL/MariaDB/GreatDB output formats
db_version=$(${MYSQL} --version 2>/dev/null | grep -oP '\d+\.\d+\.\d+' | head -1 | cut -d'.' -f1 || echo "5")
# GreatDB and MySQL 8.0+ require CREATE USER before GRANT
if command -v greatdb &> /dev/null || [ "$db_version" -ge 8 ] 2>/dev/null; then
${MYSQL} ${loginCmd} << EOF
set global log_bin_trust_function_creators=1;
DROP DATABASE IF EXISTS zstack;
CREATE DATABASE zstack;
DROP DATABASE IF EXISTS zstack_rest;
CREATE DATABASE zstack_rest;
CREATE USER IF NOT EXISTS 'root'@'%' IDENTIFIED BY "${password}";
CREATE USER IF NOT EXISTS 'root'@'127.0.0.1' IDENTIFIED BY "${password}";
grant all privileges on zstack.* to root@'%';
grant all privileges on zstack_rest.* to root@'%';
grant all privileges on zstack.* to root@'127.0.0.1';
grant all privileges on zstack_rest.* to root@'127.0.0.1';
EOF
else
# MySQL 5.x: GRANT with IDENTIFIED BY auto-creates users
${MYSQL} ${loginCmd} << EOF
set global log_bin_trust_function_creators=1;
DROP DATABASE IF EXISTS zstack;
CREATE DATABASE zstack;
DROP DATABASE IF EXISTS zstack_rest;
CREATE DATABASE zstack_rest;
grant all privileges on zstack.* to root@'%' identified by "${password}";
grant all privileges on zstack_rest.* to root@'%' identified by "${password}";
grant all privileges on zstack.* to root@'127.0.0.1' identified by "${password}";
grant all privileges on zstack_rest.* to root@'127.0.0.1' identified by "${password}";
EOF
fi
# assign flyway version if not defined
: "${flywayver:=3.2.1}"
flyway="$base/../conf//tools/flyway-$flywayver/flyway"
flyway_sql="$base/../conf/tools/flyway-$flywayver/sql/"
mkdir -p ${flyway_sql}
eval "rm -f ${flyway_sql}/*"
cp ${base}/../conf/db/V0.6__schema.sql ${flyway_sql}
for f in ${base}/../conf/db/upgrade/*; do
[ "$(basename "$f")" = "beforeValidate.sql" ] && continue
cp "$f" ${flyway_sql}
done
tz_param="?serverTimezone=Asia/Shanghai"
if [[ ! -n $host ]] || [[ ! -n $port ]];then
url="jdbc:mysql://localhost:3306/zstack${tz_param}"
else
url="jdbc:mysql://$host:$port/zstack${tz_param}"
fi
${flyway} -user=${user} -password=${password} -url=${url} clean
# create baseline and clean its contents for 'beforeValidate.sql'
${flyway} -user=${user} -password=${password} -url=${url} baseline
${MYSQL} ${loginCmd} zstack -e "DELETE FROM schema_version"
${flyway} -outOfOrder=true -user=${user} -password=${password} -url=${url} migrate
eval "rm -f ${flyway_sql}/*"
cp ${base}/../conf/db/V0.6__schema_buildin_httpserver.sql ${flyway_sql}
if [[ ! -n $host ]] || [[ ! -n $port ]];then
url="jdbc:mysql://localhost:3306/zstack_rest${tz_param}"
else
url="jdbc:mysql://$host:$port/zstack_rest${tz_param}"
fi
${flyway} -user=${user} -password=${password} -url=${url} clean
${flyway} -outOfOrder=true -user=${user} -password=${password} -url=${url} migrate
eval "rm -f ${flyway_sql}/*"