Skip to content

Commit fc7c147

Browse files
committed
initial commit
0 parents  commit fc7c147

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
env

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
CourseAdvisor infrastructure repository
2+
=======================================
3+
4+
[ Work in progress ]
5+
6+
This repository contains containers and scripts for setting up the courseadvisor
7+
backend as well as replication slaves, backup and deployment scripts.
8+
9+
10+
## Prerequisites
11+
12+
This repository assumes a regular linux distro with the following programs accessible on the cli:
13+
- docker
14+
- mysql
15+
16+
## Usage
17+
18+
TBD. The general idea would be to have frontend.sh expose all functionnalities although
19+
sub-projects should be as disconnected as possible.
20+
21+
For now, one can create a slave instance like this:
22+
23+
`./frontend.sh slave setup # Creates and starts a replication slave`
24+
25+
And remove it with
26+
`./frontend.sh slave remove`
27+
28+
The container can be started/stopped using the start and stop commands
29+
```sh
30+
./frontend.sh slave stop
31+
./frontend.sh slave start
32+
```

db/slave/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Replication slave
2+
=================
3+
4+
Creates a mysql docker container configured to replicate the production server.
5+
`conf/` is mounted to the container's /etc/mysql/conf.d and contains a configuration file
6+
edited on the fly to contain appropriate host values.
7+
8+
The container exports it's mysql port to the host's $SLAVE_EXPORTED_PORT (default 3300)

db/slave/slave.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/bin/sh -e
2+
# server-id = 2
3+
# relay-log = /var/log/mysql/mysql-relay-bin.log
4+
# log_bin = /var/log/mysql/mysql-bin.log
5+
# replicate-do-db = courseadvisor
6+
# report-host = hmil.fr
7+
#
8+
# position: 107
9+
10+
check_env() {
11+
if [ -z "$SLAVE_ROOT_PASSWORD" ] || [ -z "$SLAVE_REPLICATION_PASSWORD" ] || [ -z "$SLAVE_REPLICATION_USER" ]; then
12+
echo "An environment variable is missing. Run --help for usage info." 1>&2
13+
return 1
14+
fi
15+
if [ -z "$SLAVE_EXPORTED_PORT" ]; then
16+
export SLAVE_EXPORTED_PORT=3300
17+
fi
18+
if [ -z "$SLAVE_CONTAINER_NAME" ]; then
19+
export SLAVE_CONTAINER_NAME="courseadvisor-slave"
20+
fi
21+
}
22+
23+
wait_for_mysql() {
24+
set +e
25+
failure=1
26+
tries=30
27+
while [ "$failure" -ne 0 ]; do
28+
tries=`expr $tries - 1`
29+
if [ $tries -eq 0 ]; then
30+
echo "Could not connect to mysql server" 1>&2
31+
return 1
32+
fi
33+
sleep 1
34+
echo 'select 1;' | mysql -P$SLAVE_EXPORTED_PORT -h"127.0.0.1" -uroot -p$SLAVE_ROOT_PASSWORD 2>/dev/null
35+
failure=$?
36+
done
37+
return 0
38+
}
39+
40+
setup() {
41+
# prepare configuration with random slave number
42+
server_id=`tr -cd 0-9 </dev/urandom | head -c 6`
43+
server_name=`hostname`
44+
cat slave_config.cnf | sed "s/__SERVER_ID__/$server_id/" | sed "s/__SERVER_NAME__/$server_name/" > conf/slave_config.cnf
45+
# Create mysql docker image
46+
docker run --name ${SLAVE_CONTAINER_NAME} -p ${SLAVE_EXPORTED_PORT}:3306 -e MYSQL_ROOT_PASSWORD=${SLAVE_ROOT_PASSWORD} -e MYSQL_DATABASE=master -v `pwd`/conf:/etc/mysql/conf.d -d mysql:5.5
47+
echo "Waiting for mysql deamon to start"
48+
wait_for_mysql 1>/dev/null
49+
echo "Dumping master state (warning: locks master DB)"
50+
# Seeds database with snapshot data, replication log position is set to take over right after this snapshot
51+
mysqldump -P5467 -u${SLAVE_REPLICATION_USER} -h"courseadvisor.ch" -p${SLAVE_REPLICATION_PASSWORD} --master-data -B master |
52+
sed "s/CHANGE MASTER TO/CHANGE MASTER TO MASTER_HOST='courseadvisor.ch',MASTER_PORT=5467,MASTER_USER='${SLAVE_REPLICATION_USER}',MASTER_PASSWORD='${SLAVE_REPLICATION_PASSWORD}',/" |
53+
mysql -P${SLAVE_EXPORTED_PORT} -h"127.0.0.1" -uroot -p${SLAVE_ROOT_PASSWORD}
54+
55+
echo "Starting slave replication"
56+
# Sets up remote master
57+
echo "START SLAVE;" | mysql -P${SLAVE_EXPORTED_PORT} -h"127.0.0.1" -uroot -p${SLAVE_ROOT_PASSWORD}
58+
echo "Slave instance is running"
59+
}
60+
61+
stop() {
62+
docker stop $SLAVE_CONTAINER_NAME
63+
}
64+
65+
remove() {
66+
stop
67+
docker rm $SLAVE_CONTAINER_NAME
68+
}
69+
70+
start() {
71+
docker start $SLAVE_CONTAINER_NAME
72+
}
73+
74+
usage() {
75+
cat 1>&2 <<EOF
76+
usage: slave (setup|remove)
77+
78+
Sets up or removes the slave docker image. Sensible arguments are passed via env variables:
79+
80+
mandatory:
81+
SLAVE_REPLICATION_USER : Replication user on master host
82+
SLAVE_REPLICATION_PASSWORD : Replication user password on master host
83+
SLAVE_ROOT_PASSWORD : Password for root user on local slave instance
84+
85+
optionnal:
86+
SLAVE_EXPORTED_PORT : Which host port to bind to slave's SQL (default 3300)
87+
SLAVE_CONTAINER_NAME: Docker container name for the slave (default courseadvisor-slave)
88+
EOF
89+
}
90+
91+
check_env
92+
93+
case "$1" in
94+
"setup")
95+
setup
96+
;;
97+
"remove")
98+
remove
99+
;;
100+
"stop")
101+
stop
102+
;;
103+
"start")
104+
start
105+
;;
106+
*)
107+
usage
108+
;;
109+
esac

db/slave/slave_config.cnf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[mysqld]
2+
server-id = __SERVER_ID__
3+
#relay-log = /var/log/mysql/mysql-relay-bin.log
4+
#log_bin = /var/log/mysql/mysql-bin.log
5+
replicate-do-db = master
6+
report_host = __SERVER_NAME__

frontend.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/sh
2+
3+
usage() {
4+
cat 1>&2 <<EOF
5+
usage: frontend.sh (command [args] | help)
6+
7+
Available commands:
8+
slave : Replication slave
9+
help : Prints this help message
10+
11+
For command-specific usage instructions, run frontend.sh command --help
12+
EOF
13+
}
14+
15+
16+
if [ $# -lt 2 ]; then
17+
usage
18+
exit 1
19+
fi
20+
21+
if [ ! -e `pwd`/env ]; then
22+
echo "Please provide the environment variables file './env'" 1>&2
23+
exit 1
24+
fi
25+
26+
. `pwd`/env
27+
28+
prog="$1"
29+
shift
30+
31+
case "$prog" in
32+
help|--help|-h)
33+
usage
34+
exit 0
35+
;;
36+
slave)
37+
cd db/slave
38+
./slave.sh $@
39+
;;
40+
*)
41+
usage
42+
;;
43+
esac

0 commit comments

Comments
 (0)