Skip to content

Commit

Permalink
Fix #74 improve dockerization
Browse files Browse the repository at this point in the history
  • Loading branch information
micheljung committed Feb 15, 2017
1 parent bca95af commit 53def9e
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 307 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ __pycache__
*.egg-info
.idea
*.pyc
*.iml
flyway.conf
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ before_script:
- sudo /etc/init.d/mysql stop

script:
- ./setup_db.sh -d
- docker build -t faf-db .
- docker run -d --name faf-db -e MYSQL_ROOT_PASSWORD=banana -p 3306:3306 faf-db
- until docker exec -i faf-db ./healthcheck.sh 2>/dev/null; do sleep 1; done

after_success:
- ./dump.sh
26 changes: 24 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM mysql:5.7

ENV FLYWAY_VERSION 4.0.3

# Install mysql_config which allows to set up a no-password login with --login-path
RUN apt-get update -y > /dev/null && apt-get install -y libmysqlclient-dev wget > /dev/null

# Download flyway
Expand All @@ -10,8 +11,29 @@ RUN wget -q http://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/${FLYW
&& rm flyway-commandline-${FLYWAY_VERSION}-linux-x64.tar.gz \
&& mv flyway-${FLYWAY_VERSION} flyway \
&& chmod +x flyway/flyway
RUN chown -R mysql /flyway

# Download mysql driver
RUN wget -q http://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/6.0.4/mysql-connector-java-6.0.4.jar -O flyway/drivers/mysql-connector-java-6.0.4.jar
# Download MariaDB driver (compatible with MySQL but supports local sockets
RUN wget -q http://search.maven.org/remotecontent?filepath=org/mariadb/jdbc/mariadb-java-client/1.5.7/mariadb-java-client-1.5.7.jar -O flyway/drivers/mariadb-java-client-1.5.7.jar
# Download JNA which is needed for local socket support of the MariaDB driver
RUN wget -q http://search.maven.org/remotecontent?filepath=net/java/dev/jna/jna/4.3.0/jna-4.3.0.jar -O flyway/drivers/jna-4.3.0.jar

COPY healthcheck.sh /
RUN chmod +x healthcheck.sh

HEALTHCHECK --interval=30s --timeout=5s --retries=5 CMD ["./healthcheck.sh"]

# Copy init scripts which will be executed automatically if the database doesn't exist yet
COPY init/* /docker-entrypoint-initdb.d/
RUN chmod +x /docker-entrypoint-initdb.d/*.sh

# Copy migration scripts
COPY migrate.sh /
RUN chmod +x migrate.sh
COPY migrations/* /flyway/sql/

# Copy dump script
COPY dump-structure.sh /
RUN chmod +x dump-structure.sh

EXPOSE 3306
29 changes: 10 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
# FAForever DB project

Contains a dockerfile to run our database in a contained environment, along with tools for managing the database.
Provides the Docker container and migration tools for a production-ready FAF database.

## Set your Database up
## Creating a new database

Install [docker](http://docker.com).
We highly recommended to use [faf-stack](https://github.com/FAForever/faf-stack) to create a new database container, like so:

**Make sure your port 3306 is not occupied**. Install and initialize the database:
docker-compose up -d faf-db

./setup_db.sh
## Updating the database

Now your FAF database is up and running and contains some dummy data (from `db-data.sql`)
In order to update an existing database to the newest schema version, execute:

Currently supported flags for `setup_db.sh`:
docker exec -ti faf-db ./migrate.sh

-d Dump DB schema to container STDOUT.
-c file Dump DB schema to provided file location. If a directory is provided, the file name will be dump.sql.
-h Print script command line options."
## Connecting to the database

## Update your Database

If you have an existing database which you need to update, run:

./migrate.sh faf-db

## Connect to your Database

To get a mysql session where you can execute queries conveniently, execute:
In order to connect to the database using the mysql client, execute:

docker exec -ti faf-db mysql -uroot -pbanana

Where you have to replace `root` and `banana` with your custom credentials.
9 changes: 9 additions & 0 deletions dump-structure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

if [ ! "${MYSQL_ROOT_PASSWORD+1}" ]; then
echo "This script is not intended for use outside the docker container (variable MYSQL_ROOT_PASSWORD has not been set)"
echo "Try instead: docker -i faf-db ${0}"
exit 1
fi

mysqldump -uroot -p${MYSQL_ROOT_PASSWORD} --no-data faf || exit 1;
169 changes: 0 additions & 169 deletions example_flyway.conf

This file was deleted.

5 changes: 5 additions & 0 deletions healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

echo "SELECT 1;" | mysql --host="127.0.0.1" --user="root" --password="${MYSQL_ROOT_PASSWORD}" > /dev/null

exit $?
26 changes: 26 additions & 0 deletions init/0_init_db_and_users.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

create() {
database=$1
username=$2
password=$3

mysql --user=root --password=${MYSQL_ROOT_PASSWORD} <<SQL_SCRIPT
CREATE DATABASE IF NOT EXISTS \`${database}\`;
CREATE USER '${username}'@'%' IDENTIFIED BY '${password}';
GRANT ALL PRIVILEGES ON \`${database}\`.* TO '${username}'@'%';
FLUSH PRIVILEGES;
SQL_SCRIPT
}

create "faf" "faf-api" "${MYSQL_API_PASSWORD}"
create "faf" "faf-legacy-apps" "${MYSQL_LEGACY_APPS_PASSWORD}"
create "faf" "faf-legacy-live-replay-server" "${MYSQL_LEGACY_LIVE_REPLAY_SERVER_PASSWORD}"
create "faf" "faf-legacy-secondary-server" "${MYSQL_LEGACY_SECONDARY_SERVER_PASSWORD}"
create "faf" "faf-legacy-updater" "${MYSQL_LEGACY_UPDATER_PASSWORD}"
create "faf-murmur" "faf-murmur" "${MYSQL_MURMUR_PASSWORD}"
create "faf" "faf-server" "${MYSQL_SERVER_PASSWORD}"
create "faf-softvote" "faf-softvote" "${MYSQL_SOFTVOTE_PASSWORD}"
create "faf-anope" "faf-anope" "${MYSQL_ANOPE_PASSWORD}"
create "faf-wiki" "faf-wiki" "${MYSQL_WIKI_PASSWORD}"
create "faf-wordpress" "faf-wordpress" "${MYSQL_WORDPRESS_PASSWORD}"
3 changes: 3 additions & 0 deletions init/1_migrate_faf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

./migrate.sh
Loading

0 comments on commit 53def9e

Please sign in to comment.