1 | #!/bin/bash
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | RED='\033[1;31m'
|
7 | GREEN='\033[1;32m'
|
8 | YELLOW='\033[1;33m'
|
9 | CYAN='\033[1;36m'
|
10 | PLAIN='\033[0m'
|
11 |
|
12 |
|
13 | MYSQL_CONTAINER="mysql_c"
|
14 | HOST="localhost"
|
15 | USER="root"
|
16 | PASSWORD="pass"
|
17 | PORT=3306
|
18 | DATABASE="testdb"
|
19 | if [ "$1" ]; then
|
20 | HOST=$1
|
21 | fi
|
22 | if [ "$2" ]; then
|
23 | PORT=$2
|
24 | fi
|
25 | if [ "$3" ]; then
|
26 | USER=$3
|
27 | fi
|
28 | if [ "$4" ]; then
|
29 | PASSWORD=$4
|
30 | fi
|
31 | if [ "$5" ]; then
|
32 | DATABASE=$5
|
33 | fi
|
34 |
|
35 |
|
36 | printf "\n${RED}>> Checking for docker${PLAIN} ${GREEN}...${PLAIN}"
|
37 | docker -v > /dev/null 2>&1
|
38 | DOCKER_EXISTS=$?
|
39 | if [ "$DOCKER_EXISTS" -ne 0 ]; then
|
40 | printf "\n\n${CYAN}Status: ${PLAIN}${RED}Docker not found. Terminating setup.${PLAIN}\n\n"
|
41 | exit 1
|
42 | fi
|
43 | printf "\n${CYAN}Found docker. Moving on with the setup.${PLAIN}\n"
|
44 |
|
45 |
|
46 |
|
47 | printf "\n${RED}>> Finding old builds and cleaning up${PLAIN} ${GREEN}...${PLAIN}"
|
48 | docker rm -f $MYSQL_CONTAINER > /dev/null 2>&1
|
49 | printf "\n${CYAN}Clean up complete.${PLAIN}\n"
|
50 |
|
51 | DOCKER_IMAGE=mysql:8.0
|
52 |
|
53 |
|
54 | printf "\n${RED}>> Pulling ${DOCKER_IMAGE} image${PLAIN} ${GREEN}...${PLAIN}"
|
55 |
|
56 | docker pull ${DOCKER_IMAGE} > /dev/null 2>&1
|
57 | printf "\n${CYAN}Image successfully built.${PLAIN}\n"
|
58 |
|
59 |
|
60 | printf "\n${RED}>> Starting the mysql container${PLAIN} ${GREEN}...${PLAIN}"
|
61 | CONTAINER_STATUS=$(docker run --name $MYSQL_CONTAINER -e MYSQL_ROOT_USER=$USER -e MYSQL_ROOT_PASSWORD=$PASSWORD -p $PORT:3306 -d ${DOCKER_IMAGE} 2>&1)
|
62 | if [[ "$CONTAINER_STATUS" == *"Error"* ]]; then
|
63 | printf "\n\n${CYAN}Status: ${PLAIN}${RED}Error starting container. Terminating setup.${PLAIN}\n\n"
|
64 | exit 1
|
65 | fi
|
66 | docker cp ./test/schema.sql $MYSQL_CONTAINER:/home/ > /dev/null 2>&1
|
67 | printf "\n${CYAN}Container is up and running.${PLAIN}\n"
|
68 |
|
69 |
|
70 | printf "\n${RED}>> Exporting default schema${PLAIN} ${GREEN}...${PLAIN}\n"
|
71 |
|
72 |
|
73 | docker exec -it $MYSQL_CONTAINER /bin/sh -c "mysql -u$USER -p$PASSWORD < /home/schema.sql" > /dev/null 2>&1
|
74 |
|
75 |
|
76 | OUTPUT=$?
|
77 | TIMEOUT=120
|
78 | TIME_PASSED=0
|
79 | WAIT_STRING="."
|
80 |
|
81 | printf "\n${GREEN}Waiting for mysql to respond with updated schema $WAIT_STRING${PLAIN}"
|
82 | while [ "$OUTPUT" -ne 0 ] && [ "$TIMEOUT" -gt 0 ]
|
83 | do
|
84 | docker exec -it $MYSQL_CONTAINER /bin/sh -c "mysql -u$USER -p$PASSWORD < /home/schema.sql" > /dev/null 2>&1
|
85 | OUTPUT=$?
|
86 | sleep 1s
|
87 | TIMEOUT=$((TIMEOUT - 1))
|
88 | TIME_PASSED=$((TIME_PASSED + 1))
|
89 |
|
90 | if [ "$TIME_PASSED" -eq 5 ]; then
|
91 | printf "${GREEN}.${PLAIN}"
|
92 | TIME_PASSED=0
|
93 | fi
|
94 | done
|
95 |
|
96 | if [ "$TIMEOUT" -le 0 ]; then
|
97 | printf "\n\n${CYAN}Status: ${PLAIN}${RED}Failed to export schema. Terminating setup.${PLAIN}\n\n"
|
98 | exit 1
|
99 | fi
|
100 | printf "\n${CYAN}Successfully exported schema to database.${PLAIN}\n"
|
101 |
|
102 |
|
103 | printf "\n${RED}>> Creating the database${PLAIN} ${GREEN}...${PLAIN}"
|
104 | docker exec -it $MYSQL_CONTAINER /bin/sh -c "mysql -u$USER -p$PASSWORD -e \"DROP DATABASE IF EXISTS $DATABASE\"" > /dev/null 2>&1
|
105 | docker exec -it $MYSQL_CONTAINER /bin/sh -c "mysql -u$USER -p$PASSWORD -e \"CREATE DATABASE $DATABASE\"" > /dev/null 2>&1
|
106 | DATABASE_CREATED=$?
|
107 | if [ "$DATABASE_CREATED" -ne 0 ]; then
|
108 | printf "\n\n${CYAN}Status: ${PLAIN}${RED}Database could not be created. Terminating setup.${PLAIN}\n\n"
|
109 | exit 1
|
110 | fi
|
111 | printf "\n${CYAN}Successfully created the database.${PLAIN}\n"
|
112 |
|
113 |
|
114 | printf "\n${RED}>> Setting env variables to run test${PLAIN} ${GREEN}...${PLAIN}"
|
115 | export MYSQL_HOST=$HOST
|
116 | export MYSQL_PORT=$PORT
|
117 | export MYSQL_USER=$USER
|
118 | export MYSQL_PASSWORD=$PASSWORD
|
119 | export MYSQL_DATABASE=$DATABASE
|
120 | printf "\n${CYAN}Env variables set.${PLAIN}\n"
|
121 |
|
122 | printf "\n${CYAN}Status: ${PLAIN}${GREEN}Set up completed successfully.${PLAIN}\n"
|
123 | printf "\n${CYAN}Instance url: ${YELLOW}mysql://$USER:$PASSWORD@$HOST/$DATABASE${PLAIN}\n"
|
124 | printf "\n${CYAN}To run the test suite:${PLAIN} ${YELLOW}npm test${PLAIN}\n\n"
|