Re-organize the build/start phases.

- ./launcher bootstrap => init seafile server data & letsencrypt, then
build a local image based on seafileorg/server:<version>
- ./launcher start => start a container using the local image
This commit is contained in:
Shuai Lin 2016-11-15 16:59:02 +08:00
parent 2c5dace56e
commit 1832ad5acb
9 changed files with 110 additions and 35 deletions

View file

@ -3,3 +3,5 @@
*.swp *.swp
.DS_Store .DS_Store
*.pyc *.pyc
shared/*

View file

@ -7,13 +7,15 @@ if [[ ! -d /var/lib/mysql/mysql ]]; then
echo 'Rebuilding mysql data dir' echo 'Rebuilding mysql data dir'
chown -R mysql.mysql /var/lib/mysql chown -R mysql.mysql /var/lib/mysql
mysql_install_db > /dev/null
mysql_install_db >/var/log/mysql-bootstrap.log 2>&1
# TODO: print the log if mysql_install_db fails
rm -rf /var/run/mysqld/* rm -rf /var/run/mysqld/*
echo 'Starting mysqld' echo 'Starting mysqld'
# The sleep 1 is there to make sure that inotifywait starts up before the socket is created # The sleep 1 is there to make sure that inotifywait starts up before the socket is created
mysqld_safe & mysqld_safe >>/var/log/bootstrap-mysql.log &
echo 'Waiting for mysqld to come online' echo 'Waiting for mysqld to come online'
while [[ ! -x /var/run/mysqld/mysqld.sock ]]; do while [[ ! -x /var/run/mysqld/mysqld.sock ]]; do

View file

@ -9,6 +9,3 @@ RUN mkdir -p /opt/seafile/ && \
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
RUN mkdir -p /templates/
ADD seafile.nginx.conf /templates/seafile.nginx.conf

View file

@ -5,6 +5,7 @@ set -o pipefail
version=6.0.5 version=6.0.5
image=seafileorg/server:$version image=seafileorg/server:$version
local_image=local_seafile/server:latest
dockerdir=$(cd "$(dirname $0)"; pwd -P) dockerdir=$(cd "$(dirname $0)"; pwd -P)
sharedir=$dockerdir/shared sharedir=$dockerdir/shared
installdir=/opt/seafile/seafile-server-$version installdir=/opt/seafile/seafile-server-$version
@ -40,6 +41,27 @@ set_ports() {
ports="" ports=""
} }
set_bootstrap_volumes() {
local mounts
init_shared
mounts=(
$sharedir:/shared
$sharedir/logs/var-log:/var/log
$sharedir/db:/var/lib/mysql
$dockerdir/bootstrap:/bootstrap
$dockerdir/scripts:/scripts:ro
$dockerdir/templates:/templates:ro
$dockerdir/scripts/tmp/check_init_admin.py:$installdir/check_init_admin.py:ro
$sharedir/.bash_history:/root/.bash_history
)
volumes=""
local m
for m in ${mounts[*]}; do
volumes="$volumes -v $m"
done
}
set_volumes() { set_volumes() {
local mounts local mounts
init_shared init_shared
@ -48,9 +70,6 @@ set_volumes() {
$sharedir:/shared $sharedir:/shared
$sharedir/logs/var-log:/var/log $sharedir/logs/var-log:/var/log
$sharedir/db:/var/lib/mysql $sharedir/db:/var/lib/mysql
$dockerdir/bootstrap:/bootstrap:ro
$dockerdir/scripts:/scripts:ro
$dockerdir/scripts/tmp/check_init_admin.py:$installdir/check_init_admin.py:ro
$sharedir/.bash_history:/root/.bash_history $sharedir/.bash_history:/root/.bash_history
) )
volumes="" volumes=""
@ -65,17 +84,28 @@ bootstrap() {
if [[ ! -e $bootstrap_conf ]]; then if [[ ! -e $bootstrap_conf ]]; then
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
# First initialize seafile server and letsencrypt
set_bootstrap_volumes
set_ports
docker run --rm -it --name seafile-bootstrap -e SEAFILE_BOOTSRAP=1 $volumes $ports $image /sbin/my_init -- /scripts/bootstrap.py
docker build -f bootstrap/generated/Dockerfile -t local_seafile/server:latest .
}
oldstart() {
set_volumes set_volumes
set_ports set_ports
docker run --rm -it --name seafile-bootstrap -e SEAFILE_BOOTSRAP=1 $volumes $ports $image /sbin/my_init -- /scripts/bootstrap.py docker run --rm -it --name seafile $volumes $ports $image \
/sbin/my_init -- bash -l
# /sbin/my_init -- /scripts/start.py
} }
start() { start() {
set_volumes set_volumes
set_ports set_ports
docker run --rm -it --name seafile $volumes $ports $image \ docker run --rm -it --name seafile $volumes $ports $local_image
/sbin/my_init -- /scripts/start.py
# /sbin/my_init -- bash -l
} }
enter() { enter() {
@ -87,10 +117,10 @@ main() {
while [[ $# -gt 0 ]] while [[ $# -gt 0 ]]
do do
case "$1" in case "$1" in
bootstrap|start|enter) action=$1 ; shift 1 ;; bootstrap|oldstart|start|enter) action=$1 ; shift 1 ;;
--debug) debug=true ; shift 1 ;; --debug) debug=true ; shift 1 ;;
--dummy) dummy=$2 ; shift 2 ;; --dummy) dummy=$2 ; shift 2 ;;
*) err_and_quit "Argument error. Please see help." ;; *) err_and_quit "Argument error. Please see help." ;;
esac esac
done done
"$action" "$action"

View file

@ -13,14 +13,16 @@ import shutil
import sys import sys
import uuid import uuid
from utils import call, get_conf, get_install_dir, get_script, render_nginx_conf from utils import call, get_conf, get_install_dir, get_script, render_template, get_seafile_version
seafile_version = get_seafile_version()
installdir = get_install_dir() installdir = get_install_dir()
topdir = dirname(installdir) topdir = dirname(installdir)
shared_seafiledir = '/shared/seafile' shared_seafiledir = '/shared/seafile'
ssl_dir = '/shared/ssl' ssl_dir = '/shared/ssl'
generated_dir = '/bootstrap/generated'
def init_letsencryt(): def init_letsencrypt():
if not exists(ssl_dir): if not exists(ssl_dir):
os.mkdir(ssl_dir) os.mkdir(ssl_dir)
@ -29,17 +31,41 @@ def init_letsencryt():
'https': False, 'https': False,
'domain': domain, 'domain': domain,
} }
render_nginx_conf('/templates/seafile.nginx.conf',
'/etc/nginx/sites-enabled/seafile.nginx.conf', context) # Create a temporary nginx conf to start a server, which would accessed by letsencrypt
render_template('/templates/seafile.nginx.conf.template',
'/etc/nginx/sites-enabled/seafile.nginx.conf', context)
call('nginx -s reload') call('nginx -s reload')
call('/scripts/ssl.sh {0} {1}'.format(ssl_dir, domain)) call('/scripts/ssl.sh {0} {1}'.format(ssl_dir, domain))
# Now create the final nginx configuratin
context = {
'https': True,
'domain': domain,
}
render_template('/templates/seafile.nginx.conf.template', join(generated_dir, 'seafile.nginx.conf'), context)
def is_https():
return get_conf('server.https', '').lower() == 'true'
def generate_local_dockerfile():
context = {
'seafile_version': seafile_version,
'https': is_https(),
'domain': get_conf('server.domain'),
}
render_template('/templates/Dockerfile.template', join(generated_dir, 'Dockerfile'), context)
def main(): def main():
if not exists(shared_seafiledir): if not exists(shared_seafiledir):
os.mkdir(shared_seafiledir) os.mkdir(shared_seafiledir)
if not exists(generated_dir):
os.mkdir(generated_dir)
if get_conf('server.https', '').lower() == 'true': generate_local_dockerfile()
init_letsencryt()
if is_https():
init_letsencrypt()
env = { env = {
'SERVER_NAME': 'seafile', 'SERVER_NAME': 'seafile',

View file

@ -14,7 +14,7 @@ import shutil
import sys import sys
import time import time
from utils import call, get_conf, get_install_dir, get_script, get_command_output, render_nginx_conf from utils import call, get_conf, get_install_dir, get_script, get_command_output, render_template
installdir = get_install_dir() installdir = get_install_dir()
topdir = dirname(installdir) topdir = dirname(installdir)
@ -30,18 +30,7 @@ def watch_controller():
print 'seafile controller exited unexpectedly.' print 'seafile controller exited unexpectedly.'
sys.exit(1) sys.exit(1)
def init_https():
domain = get_conf('server.hostname')
context = {
'https': True,
'domain': domain,
}
render_nginx_conf('/templates/seafile.nginx.conf',
'/etc/nginx/sites-enabled/seafile.nginx.conf', context)
call('nginx -t && nginx -s reload')
def main(): def main():
init_https()
admin_pw = { admin_pw = {
'email': get_conf('admin.email'), 'email': get_conf('admin.email'),
'password': get_conf('admin.password'), 'password': get_conf('admin.password'),

View file

@ -4,6 +4,7 @@ from __future__ import print_function
from ConfigParser import ConfigParser from ConfigParser import ConfigParser
from contextlib import contextmanager from contextlib import contextmanager
import os import os
import datetime
from os.path import abspath, basename, exists, dirname, join, isdir, expanduser from os.path import abspath, basename, exists, dirname, join, isdir, expanduser
import platform import platform
import sys import sys
@ -221,9 +222,17 @@ def get_conf(key, default=None):
return _config.get("server", key) if _config.has_option("server", key) \ return _config.get("server", key) if _config.has_option("server", key) \
else default else default
def render_nginx_conf(template, target, context): def _add_default_context(context):
default_context = {
'current_timestr': datetime.datetime.now().strftime('%m/%d/%Y %H:%M:%S'),
}
for k in default_context:
context.setdefault(k, default_context[k])
def render_template(template, target, context):
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader(dirname(template))) env = Environment(loader=FileSystemLoader(dirname(template)))
_add_default_context(context)
content = env.get_template(basename(template)).render(**context) content = env.get_template(basename(template)).render(**context)
with open(target, 'w') as fp: with open(target, 'w') as fp:
fp.write(content) fp.write(content)

View file

@ -0,0 +1,18 @@
# -*- mode: dockerfile -*-
# This is a jinja2 template to generate the real Dockerfile to build the local image
FROM seafileorg/server:{{ seafile_version }}
ADD scripts /scripts
ADD bootstrap /bootstrap
# This can be removed for seafile server 6.0.6 and later
ADD scripts/tmp/check_init_admin.py /opt/seafile/seafile-server-{{ seafile_version }}/check_init_admin.py
ADD bootstrap/generated/seafile.nginx.conf /etc/nginx/sites-enabled/seafile.nginx.conf
{% if https -%}
# TODO: add a cron job to renew letsencrypt
{% endif -%}
CMD ["/sbin/my_init", "--", "/scripts/start.py"]

View file

@ -1,3 +1,5 @@
# -*- mode: nginx -*-
# Auto generated at {{ current_timestr }}
{% if https -%} {% if https -%}
server { server {
listen 80; listen 80;