Debian Codegraph @

test/mysql-router/run_https.sh

raw
1 #!/bin/bash
2 set -e
3
4 if [ "$1" = 'mysqlrouter' ]; then
5 if [[ -z $MYSQL_HOST || -z $MYSQL_PORT || -z $MYSQL_USER || -z $MYSQL_PASSWORD ]]; then
6 echo "We require all of"
7 echo " MYSQL_HOST"
8 echo " MYSQL_PORT"
9 echo " MYSQL_USER"
10 echo " MYSQL_PASSWORD"
11 echo "to be set. Exiting."
12 exit 1
13 fi
14
15 PASSFILE=$(mktemp)
16 echo "$MYSQL_PASSWORD" > "$PASSFILE"
17 DEFAULTS_EXTRA_FILE=$(mktemp)
18 cat >"$DEFAULTS_EXTRA_FILE" <<EOF
19 [client]
20 password="$MYSQL_PASSWORD"
21 EOF
22 unset MYSQL_PASSWORD
23 max_tries=12
24 attempt_num=0
25 until (echo > "/dev/tcp/$MYSQL_HOST/$MYSQL_PORT") >/dev/null 2>&1; do
26 echo "Waiting for mysql server $MYSQL_HOST ($attempt_num/$max_tries)"
27 sleep $(( attempt_num++ ))
28 if (( attempt_num == max_tries )); then
29 exit 1
30 fi
31 done
32 echo "Successfully contacted mysql server at $MYSQL_HOST. Checking for cluster state."
33
34 mysql --defaults-extra-file="$DEFAULTS_EXTRA_FILE" -u "$MYSQL_USER" -h "$MYSQL_HOST" -P "$MYSQL_PORT" -e "show status;"
35
36 if ! [[ "$(mysql --defaults-extra-file="$DEFAULTS_EXTRA_FILE" -u "$MYSQL_USER" -h "$MYSQL_HOST" -P "$MYSQL_PORT" -e "show status;" 2> /dev/null)" ]]; then
37 echo "Can not connect to database. Exiting."
38 exit 1
39 fi
40 if [[ -n $MYSQL_INNODB_CLUSTER_MEMBERS ]]; then
41 attempt_num=0
42 echo $attempt_num
43 echo $max_tries
44 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
45 echo "Waiting for $MYSQL_INNODB_CLUSTER_MEMBERS cluster instances to become available via $MYSQL_HOST ($attempt_num/$max_tries)"
46 sleep $(( attempt_num++ ))
47 if (( attempt_num == max_tries )); then
48 exit 1
49 fi
50 done
51 echo "Successfully contacted cluster with $MYSQL_INNODB_CLUSTER_MEMBERS members. Bootstrapping."
52 fi
53 echo "Successfully contacted mysql server at $MYSQL_HOST. Trying to bootstrap."
54 echo "Hello! I'm tired for creating this fuck containers!"
55
56 chown mysqlrouter /mysqlrouter.pwd
57
58 mysqlrouter --bootstrap "$MYSQL_USER@$MYSQL_HOST:$MYSQL_PORT" --user=mysqlrouter --directory /tmp/mysqlrouter --force < "$PASSFILE"
59 sed -i -e 's/logging_folder=.*$/logging_folder=/' /tmp/mysqlrouter/mysqlrouter.conf
60 # Remove sections related to rest api
61 sed -i -e '60,83d' /tmp/mysqlrouter/mysqlrouter.conf
62 # then add
63 cat << EOF >> /tmp/mysqlrouter/mysqlrouter.conf
64 [http_server]
65 port=8443
66 ssl=1
67 ssl_cert=/server.crt
68 ssl_key=/server.key
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 "$@"