2016-11-11 04:54:47 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#coding: UTF-8
|
|
|
|
|
|
|
|
"""
|
|
|
|
This script calls the appropriate seafile init scripts (e.g.
|
|
|
|
setup-seafile.sh or setup-seafile-mysql.sh. It's supposed to run inside the
|
|
|
|
container.
|
|
|
|
"""
|
|
|
|
|
2016-11-16 01:46:08 +00:00
|
|
|
import argparse
|
2016-11-11 04:54:47 +00:00
|
|
|
import os
|
|
|
|
from os.path import abspath, basename, exists, dirname, join, isdir
|
|
|
|
import shutil
|
|
|
|
import sys
|
2016-11-14 06:47:40 +00:00
|
|
|
import uuid
|
2016-11-16 07:41:08 +00:00
|
|
|
import time
|
2016-11-11 04:54:47 +00:00
|
|
|
|
2016-11-15 08:59:02 +00:00
|
|
|
from utils import call, get_conf, get_install_dir, get_script, render_template, get_seafile_version
|
2016-11-11 04:54:47 +00:00
|
|
|
|
2016-11-15 08:59:02 +00:00
|
|
|
seafile_version = get_seafile_version()
|
2016-11-11 04:54:47 +00:00
|
|
|
installdir = get_install_dir()
|
|
|
|
topdir = dirname(installdir)
|
2016-11-12 06:03:52 +00:00
|
|
|
shared_seafiledir = '/shared/seafile'
|
2016-11-15 04:11:58 +00:00
|
|
|
ssl_dir = '/shared/ssl'
|
2016-11-15 08:59:02 +00:00
|
|
|
generated_dir = '/bootstrap/generated'
|
2016-11-15 04:11:58 +00:00
|
|
|
|
2016-11-15 08:59:02 +00:00
|
|
|
def init_letsencrypt():
|
2016-11-15 04:11:58 +00:00
|
|
|
if not exists(ssl_dir):
|
|
|
|
os.mkdir(ssl_dir)
|
|
|
|
|
|
|
|
domain = get_conf('server.hostname')
|
|
|
|
context = {
|
|
|
|
'https': False,
|
|
|
|
'domain': domain,
|
|
|
|
}
|
2016-11-15 08:59:02 +00:00
|
|
|
|
|
|
|
# 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)
|
2016-11-15 04:11:58 +00:00
|
|
|
call('nginx -s reload')
|
|
|
|
call('/scripts/ssl.sh {0} {1}'.format(ssl_dir, domain))
|
2016-11-11 04:54:47 +00:00
|
|
|
|
2016-11-15 08:59:02 +00:00
|
|
|
# Now create the final nginx configuratin
|
|
|
|
context = {
|
|
|
|
'https': True,
|
|
|
|
'domain': domain,
|
|
|
|
}
|
2016-11-16 07:41:08 +00:00
|
|
|
render_template(
|
|
|
|
'/templates/seafile.nginx.conf.template',
|
|
|
|
join(generated_dir, 'seafile.nginx.conf'),
|
|
|
|
context
|
|
|
|
)
|
|
|
|
|
|
|
|
context = {
|
|
|
|
'ssl_dir': ssl_dir,
|
|
|
|
'domain': domain,
|
|
|
|
}
|
|
|
|
render_template(
|
|
|
|
'/templates/letsencrypt.cron.template',
|
|
|
|
join(generated_dir, 'letsencrypt.cron'),
|
|
|
|
context
|
|
|
|
)
|
2016-11-15 08:59:02 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-11-16 01:46:08 +00:00
|
|
|
def parse_args():
|
|
|
|
ap = argparse.ArgumentParser()
|
|
|
|
ap.add_argument('--parse-ports', action='store_true')
|
|
|
|
|
|
|
|
return ap.parse_args()
|
|
|
|
|
|
|
|
def do_parse_ports():
|
|
|
|
"""
|
|
|
|
Parse the server.port_mappings option and print docker command line port
|
|
|
|
mapping flags like "-p 80:80 -p 443:443"
|
|
|
|
"""
|
|
|
|
# conf is like '80:80,443:443'
|
|
|
|
conf = get_conf('server.port_mappings', '').strip()
|
|
|
|
if conf:
|
|
|
|
sys.stdout.write(' '.join(['-p {}'.format(part.strip()) for part in conf.split(',')]))
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2016-11-11 04:54:47 +00:00
|
|
|
def main():
|
2016-11-16 01:46:08 +00:00
|
|
|
args = parse_args()
|
|
|
|
if args.parse_ports:
|
|
|
|
do_parse_ports()
|
|
|
|
return
|
2016-11-12 06:03:52 +00:00
|
|
|
if not exists(shared_seafiledir):
|
|
|
|
os.mkdir(shared_seafiledir)
|
2016-11-15 08:59:02 +00:00
|
|
|
if not exists(generated_dir):
|
|
|
|
os.mkdir(generated_dir)
|
|
|
|
|
|
|
|
generate_local_dockerfile()
|
2016-11-15 04:11:58 +00:00
|
|
|
|
2016-11-15 08:59:02 +00:00
|
|
|
if is_https():
|
|
|
|
init_letsencrypt()
|
2016-11-15 04:11:58 +00:00
|
|
|
|
2016-11-16 07:41:08 +00:00
|
|
|
init_seafile_server()
|
|
|
|
|
|
|
|
def init_seafile_server():
|
|
|
|
if exists(join(shared_seafiledir, 'seafile-data')):
|
|
|
|
print 'Skipping running setup-seafile-mysql.py because there is existing seafile-data folder.'
|
|
|
|
return
|
|
|
|
|
2016-11-11 04:54:47 +00:00
|
|
|
env = {
|
|
|
|
'SERVER_NAME': 'seafile',
|
2016-11-14 06:47:40 +00:00
|
|
|
'SERVER_IP': get_conf('server.hostname'),
|
2016-11-14 12:21:05 +00:00
|
|
|
'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': '',
|
2016-11-11 04:54:47 +00:00
|
|
|
}
|
2016-11-14 06:47:40 +00:00
|
|
|
|
2016-11-14 12:21:05 +00:00
|
|
|
# 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' {}'''
|
2016-11-15 04:11:58 +00:00
|
|
|
.format(get_script('setup-seafile-mysql.py')))
|
2016-11-14 06:47:40 +00:00
|
|
|
|
2016-11-14 12:21:05 +00:00
|
|
|
setup_script = get_script('setup-seafile-mysql.sh')
|
2016-11-14 06:47:40 +00:00
|
|
|
call('{} auto -n seafile'.format(setup_script), env=env)
|
|
|
|
|
|
|
|
files_to_copy = ['conf', 'ccnet', 'seafile-data', 'seahub-data',]
|
|
|
|
for fn in files_to_copy:
|
2016-11-11 04:54:47 +00:00
|
|
|
src = join(topdir, fn)
|
2016-11-12 06:03:52 +00:00
|
|
|
dst = join(shared_seafiledir, fn)
|
|
|
|
if not exists(dst) and exists(src):
|
|
|
|
shutil.move(src, shared_seafiledir)
|
2016-11-11 04:54:47 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-11-16 01:46:08 +00:00
|
|
|
# TODO: validate the content of bootstrap.conf is valid
|
2016-11-11 04:54:47 +00:00
|
|
|
main()
|