Debian Codegraph @

test/mysql-router/run_http.sh

raw
1 #!/bin/bash
2 set -e
3
4 if [ "$1" = 'mysqlrouter' ]; then
5
6 mysqlrouter --version
7
8 if [[ -z $MYSQL_HOST || -z $MYSQL_PORT || -z $MYSQL_USER || -z $MYSQL_PASSWORD ]]; then
9 echo "We require all of"
10 echo " MYSQL_HOST"
11 echo " MYSQL_PORT"
12 echo " MYSQL_USER"
13 echo " MYSQL_PASSWORD"
14 echo "to be set. Exiting."
15 exit 1
16 fi
17
18 PASSFILE=$(mktemp)
19 echo "$MYSQL_PASSWORD" > "$PASSFILE"
20 DEFAULTS_EXTRA_FILE=$(mktemp)
21 cat >"$DEFAULTS_EXTRA_FILE" <<EOF
22 [client]
23 password="$MYSQL_PASSWORD"
24 EOF
25 unset MYSQL_PASSWORD
26 max_tries=12
27 attempt_num=0
28 until (echo > "/dev/tcp/$MYSQL_HOST/$MYSQL_PORT") >/dev/null 2>&1; do
29 echo "Waiting for mysql server $MYSQL_HOST ($attempt_num/$max_tries)"
30 sleep $(( attempt_num++ ))
31 if (( attempt_num == max_tries )); then
32 exit 1
33 fi
34 done
35 echo "Successfully contacted mysql server at $MYSQL_HOST. Checking for cluster state."
36
37 mysql --defaults-extra-file="$DEFAULTS_EXTRA_FILE" -u "$MYSQL_USER" -h "$MYSQL_HOST" -P "$MYSQL_PORT" -e "show status;"
38
39 if ! [[ "$(mysql --defaults-extra-file="$DEFAULTS_EXTRA_FILE" -u "$MYSQL_USER" -h "$MYSQL_HOST" -P "$MYSQL_PORT" -e "show status;" 2> /dev/null)" ]]; then
40 echo "Can not connect to database. Exiting."
41 exit 1
42 fi
43 if [[ -n $MYSQL_INNODB_CLUSTER_MEMBERS ]]; then
44 attempt_num=0
45 echo $attempt_num
46 echo $max_tries
47 until [ "$(mysql --defaults-extra-file="$DEFAULTS_EXTRA_FILE" -u "$MYSQL_USER" -h "$MYSQL_HOST" -P "$MYSQL_PORT" -N performance_schema -e "select count(MEMBER_STATE) = $MYSQL_INNODB_CLUSTER_MEMBERS from replication_group_members where MEMBER_STATE = 'ONLINE';" 2> /dev/null)" -eq 1 ]; do
48 echo "Waiting for $MYSQL_INNODB_CLUSTER_MEMBERS cluster instances to become available via $MYSQL_HOST ($attempt_num/$max_tries)"
49 sleep $(( attempt_num++ ))
50 if (( attempt_num == max_tries )); then
51 exit 1
52 fi
53 done
54 echo "Successfully contacted cluster with $MYSQL_INNODB_CLUSTER_MEMBERS members. Bootstrapping."
55 fi
56 echo "Successfully contacted mysql server at $MYSQL_HOST. Trying to bootstrap."
57 echo "Hello! I'm tired for creating this fuck containers!"
58
59 chown mysqlrouter /mysqlrouter.pwd
60
61 mysqlrouter --bootstrap "$MYSQL_USER@$MYSQL_HOST:$MYSQL_PORT" --user=mysqlrouter --directory /tmp/mysqlrouter --force < "$PASSFILE"
62 sed -i -e 's/logging_folder=.*$/logging_folder=/' /tmp/mysqlrouter/mysqlrouter.conf
63 # Remove sections related to rest api
64 sed -i -e '60,83d' /tmp/mysqlrouter/mysqlrouter.conf
65 # then add
66 cat << EOF >> /tmp/mysqlrouter/mysqlrouter.conf
67 [http_server]
68 port=8080
69
70 [rest_api]
71
72 [rest_router]
73 require_realm=somerealm
74
75 [rest_routing]
76 require_realm=somerealm
77
78 [rest_metadata_cache]
79 require_realm=somerealm
80
81 [http_auth_realm:somerealm]
82 backend=somebackend
83 method=basic
84 name=Some Realm
85
86 [http_auth_backend:somebackend]
87 backend=file
88 filename=/mysqlrouter.pwd
89 EOF
90 echo "Starting mysql-router."
91
92 exec "$@" --config /tmp/mysqlrouter/mysqlrouter.conf
93 fi
94
95 rm -f "$PASSFILE"
96 rm -f "$DEFAULTS_EXTRA_FILE"
97 unset DEFAULTS_EXTRA_FILE
98
99 exec "$@"