Support mysql.

At this moment, we only support running mysql server inside the
container. Support for running mysql(and memcached) in a separate
container would be added later.
This commit is contained in:
Shuai Lin 2016-11-14 14:47:40 +08:00
parent 2dc6f8d5bc
commit 99d2c8456b
11 changed files with 119 additions and 14 deletions

View file

@ -1,9 +1,11 @@
# lastet phusion baseimage as of 2016.11, based on ubuntu 16.04 # Lastet phusion baseimage as of 2016.11, based on ubuntu 16.04
# See https://hub.docker.com/r/phusion/baseimage/tags/
FROM phusion/baseimage:0.9.19 FROM phusion/baseimage:0.9.19
ENV UPDATED_AT 20161110 ENV UPDATED_AT=20161110 \
DEBIAN_FRONTEND=noninteractive
RUN apt-get update -qq && apt-get -qq -y install memcached sqlite3 nginx \ RUN apt-get update -qq && apt-get -qq -y install memcached sqlite3 mariadb-server nginx \
python2.7-dev python-imaging python-ldap python-mysqldb python2.7-dev python-imaging python-ldap python-mysqldb
# Utility tools # Utility tools
@ -28,4 +30,13 @@ RUN mkdir -p /etc/service/nginx && \
mv /services/nginx.conf /etc/nginx/nginx.conf && \ mv /services/nginx.conf /etc/nginx/nginx.conf && \
mv /services/nginx.sh /etc/service/nginx/run mv /services/nginx.sh /etc/service/nginx/run
RUN mkdir -p /etc/service/mysql && \
mv /services/mysql.sh /etc/service/mysql/run
RUN mkdir -p /etc/my_init.d && rm -f /etc/my_init.d/00_regen_ssh_host_keys.sh
ADD my_init.d/99_mysql_setup.sh /etc/my_init.d/
CMD ["/sbin/my_init", "--", "bash", "-l"] CMD ["/sbin/my_init", "--", "bash", "-l"]
# TODO: clean up & squash layers with docker-squash
# See https://github.com/jwilder/docker-squash

View file

@ -0,0 +1,39 @@
#!/bin/bash
# Init mysql data dir.
# Borrowed from https://github.com/fideloper/docker-mysql/blob/master/etc/my_init.d/99_mysql_setup.sh
if [[ ! -d /var/lib/mysql/mysql ]]; then
echo 'Rebuilding mysql data dir'
chown -R mysql.mysql /var/lib/mysql
mysql_install_db > /dev/null
rm -rf /var/run/mysqld/*
echo 'Starting mysqld'
# The sleep 1 is there to make sure that inotifywait starts up before the socket is created
mysqld_safe &
echo 'Waiting for mysqld to come online'
while [[ ! -x /var/run/mysqld/mysqld.sock ]]; do
sleep 1
done
echo 'Setting root password to root'
/usr/bin/mysqladmin -u root password ''
# if [ -d /var/lib/mysql/setup ]; then
# echo 'Found /var/lib/mysql/setup - scanning for SQL scripts'
# for sql in $(ls /var/lib/mysql/setup/*.sql 2>/dev/null | sort); do
# echo 'Running script:' $sql
# mysql -uroot -proot -e "\. $sql"
# mv $sql $sql.processed
# done
# else
# echo 'No setup directory with extra sql scripts to run'
# fi
echo 'Shutting down mysqld'
mysqladmin -uroot shutdown
fi

16
image/base/services/mysql.sh Executable file
View file

@ -0,0 +1,16 @@
#!/bin/sh
set -e
shutdown_mysql() {
if [[ -d /var/run/mysqld/mysqld.sock ]]; then
mysqladmin -u root shutdown
fi
}
trap shutdown_mysql EXIT
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
/sbin/setuser mysql /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --skip-log-error --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306 >&1 >/var/log/mysql.log 2>&1

View file

@ -5,8 +5,7 @@ ENV SEAFILE_VERSION=6.0.5
RUN mkdir -p /opt/seafile/ && \ RUN mkdir -p /opt/seafile/ && \
curl -sSL -o - https://bintray.com/artifact/download/seafile-org/seafile/seafile-server_6.0.5_x86-64.tar.gz \ curl -sSL -o - https://bintray.com/artifact/download/seafile-org/seafile/seafile-server_6.0.5_x86-64.tar.gz \
| tar xzf - -C /opt/seafile/ && \ | tar xzf - -C /opt/seafile/
ln -sf /opt/seafile/seafile-server-${SEAFILE_VERSION} /opt/seafile/seafile-server-latest
RUN mkdir -p /etc/my_init.d RUN mkdir -p /etc/my_init.d
ADD create_data_links.sh /etc/my_init.d/create_data_links.sh ADD create_data_links.sh /etc/my_init.d/create_data_links.sh

View file

@ -26,4 +26,8 @@ if [[ -e /shared/logs/seafile ]]; then
ln -sf /shared/logs/seafile/ /opt/seafile/logs ln -sf /shared/logs/seafile/ /opt/seafile/logs
fi fi
if [[ ! -e /opt/seafile/seafile-server-latest ]]; then
ln -sf /opt/seafile/seafile-server-$SEAFILE_VERSION /opt/seafile/seafile-server-latest
fi
# TODO: create avatars link # TODO: create avatars link

View file

@ -23,7 +23,7 @@ err_and_quit () {
} }
init_shared() { init_shared() {
mkdir -p $sharedir/seafile/ mkdir -p $sharedir/{seafile,db}
mkdir -p $sharedir/logs/{seafile,var-log} mkdir -p $sharedir/logs/{seafile,var-log}
mkdir -p $sharedir/logs/var-log/nginx mkdir -p $sharedir/logs/var-log/nginx
@ -42,6 +42,7 @@ set_volumes() {
mounts=( mounts=(
$sharedir:/shared $sharedir:/shared
$sharedir/logs/var-log:/var/log $sharedir/logs/var-log:/var/log
$sharedir/db:/var/lib/mysql
$dockerdir/bootstrap:/bootstrap:ro $dockerdir/bootstrap:/bootstrap:ro
$dockerdir/scripts:/scripts:ro $dockerdir/scripts:/scripts:ro
$dockerdir/scripts/tmp/check_init_admin.py:$installdir/check_init_admin.py:ro $dockerdir/scripts/tmp/check_init_admin.py:$installdir/check_init_admin.py:ro
@ -60,7 +61,7 @@ bootstrap() {
err_and_quit "The file $bootstrap_conf doesn't exist. Have you run seafile-server-setup?" err_and_quit "The file $bootstrap_conf doesn't exist. Have you run seafile-server-setup?"
fi fi
set_volumes set_volumes
docker run --rm -it -e SEAFILE_BOOTSRAP=1 $volumes $image /scripts/bootstrap.py docker run --rm -it -e SEAFILE_BOOTSRAP=1 $volumes $image /sbin/my_init -- /scripts/bootstrap.py
} }
start() { start() {

View file

@ -0,0 +1,5 @@
[server]
server.hostname = seafile.example.com
admin.email = me@example.com
admin.password = asecret
db.type = mysql

View file

@ -1,4 +1,5 @@
[server] [server]
server.hostname = seafile.example.com server.hostname = seafile.example.com
admin.email = me@example.com admin.email = me@example.com
admin.password = asecret admin.password = asecret
db.type = sqlite3

View file

@ -11,6 +11,7 @@ import os
from os.path import abspath, basename, exists, dirname, join, isdir from os.path import abspath, basename, exists, dirname, join, isdir
import shutil import shutil
import sys import sys
import uuid
from utils import call, get_conf, get_install_dir, get_script from utils import call, get_conf, get_install_dir, get_script
@ -21,12 +22,34 @@ shared_seafiledir = '/shared/seafile'
def main(): def main():
if not exists(shared_seafiledir): if not exists(shared_seafiledir):
os.mkdir(shared_seafiledir) os.mkdir(shared_seafiledir)
db_type = get_conf('db.type', 'mysql')
env = { env = {
'SERVER_NAME': 'seafile', 'SERVER_NAME': 'seafile',
'SERVER_IP': get_conf("server.hostname"), 'SERVER_IP': get_conf('server.hostname'),
} }
call('{} auto'.format(get_script('setup-seafile.sh')), env=env)
for fn in ('conf', 'ccnet', 'seafile-data', 'seahub-data', 'seahub.db'): if db_type == 'sqlite3':
setup_script = get_script('setup-seafile.sh')
else:
setup_script = get_script('setup-seafile-mysql.sh')
env.update({
'MYSQL_USER': 'seafile',
'MYSQL_USER_PASSWD': str(uuid.uuid4()),
'MYSQL_USER_HOST': '127.0.0.1',
# Default MariaDB root user has empty password and can only connect from localhost.
'MYSQL_ROOT_PASSWD': '',
})
# Change the script to allow mysql root password to be empty
call('''sed -i -e 's/if not mysql_root_passwd/if not mysql_root_passwd and "MYSQL_ROOT_PASSWD" not in os.environ/g' {}'''
.format(get_script('setup-seafile-mysql.py')), check_call=True)
call('{} auto -n seafile'.format(setup_script), env=env)
files_to_copy = ['conf', 'ccnet', 'seafile-data', 'seahub-data',]
if db_type in ('sqlite', 'sqlite3'):
files_to_copy += ['seahub.db']
for fn in files_to_copy:
src = join(topdir, fn) src = join(topdir, fn)
dst = join(shared_seafiledir, fn) dst = join(shared_seafiledir, fn)
if not exists(dst) and exists(src): if not exists(dst) and exists(src):

View file

@ -46,7 +46,12 @@ def main():
if exists(password_file): if exists(password_file):
os.unlink(password_file) os.unlink(password_file)
watch_controller() print 'seafile server is running now.'
try:
watch_controller()
except KeyboardInterrupt:
print 'Stopping seafile server.'
sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -213,9 +213,10 @@ def get_script(script):
_config = None _config = None
def get_conf(key): def get_conf(key, default=None):
global _config global _config
if _config is None: if _config is None:
_config = ConfigParser() _config = ConfigParser()
_config.read("/bootstrap/bootstrap.conf") _config.read("/bootstrap/bootstrap.conf")
return _config.get("server", key) return _config.get("server", key) if _config.has_option("server", key) \
else default