chore(install): separate services with docker-compose support

Without bundle, now you can choose to download single container.
For example, you can only download judger container to setup a new judger.
Also, the web container now no db and judger containing, except local sandbox.
You can use docker-compose to get every single container work together from now.
Single DB this time uses the latest MySQL 8 version, so some preferences are different.
This commit is contained in:
Masco Skray 2019-07-16 14:13:28 +08:00
parent 28cd4ef8b8
commit d413b746eb
15 changed files with 332 additions and 111 deletions

View File

@ -76,7 +76,7 @@ file_put_contents('/var/www/uoj/app/.config.php', "<?php\nreturn ".str_replace('
UOJEOF UOJEOF
#Import MySQL database #Import MySQL database
service mysql restart service mysql restart
mysql -u root --password=$_database_password_ <app_uoj233.sql mysql -u root --password=$_database_password_ <../db/app_uoj233.sql
} }
setJudgeConf(){ setJudgeConf(){
@ -84,10 +84,10 @@ setJudgeConf(){
#Add local_main_judger user #Add local_main_judger user
useradd -m local_main_judger && usermod -a -G www-data local_main_judger useradd -m local_main_judger && usermod -a -G www-data local_main_judger
#Set uoj_data path #Set uoj_data path
mkdir /var/uoj_data && mkdir /var/uoj_data/upload mkdir -p /var/uoj_data/upload
chown -R www-data /var/uoj_data && chgrp -R www-data /var/uoj_data chown -R www-data:www-data /var/uoj_data
#Compile uoj_judger and set runtime #Compile uoj_judger and set runtime
chown -R local_main_judger /opt/uoj/judger && chgrp -R local_main_judger /opt/uoj/judger chown -R local_main_judger:local_main_judger /opt/uoj/judger
su local_main_judger <<EOD su local_main_judger <<EOD
ln -s /var/uoj_data /opt/uoj/judger/uoj_judger/data ln -s /var/uoj_data /opt/uoj/judger/uoj_judger/data
cd /opt/uoj/judger && chmod +x judge_client cd /opt/uoj/judger && chmod +x judge_client

View File

@ -0,0 +1,47 @@
version: "3"
services:
uoj-db:
image: universaloj/uoj-system:db
container_name: uoj-db
restart: always
volumes:
- ./uoj_data/db/mysql:/var/lib/mysql
environment:
- MYSQL_DATABASE=app_uoj233
- MYSQL_ROOT_PASSWORD=root
uoj-judger:
image: universaloj/uoj-system:judger
container_name: uoj-judger
restart: always
stdin_open: true
tty: true
cap_add:
- SYS_PTRACE
volumes:
- ./uoj_data/judger/log:/opt/uoj/judger/log
environment:
- UOJ_PROTOCOL=http
- UOJ_HOST=uoj-web
- JUDGER_NAME=compose_judger
- JUDGER_PASSWORD=_judger_password_
- SOCKET_PORT=2333
- SOCKET_PASSWORD=_judger_socket_password_
uoj-web:
image: universaloj/uoj-system:web
container_name: uoj-web
restart: always
stdin_open: true
tty: true
cap_add:
- SYS_PTRACE
depends_on:
- uoj-db
- uoj-judger
volumes:
- ./uoj_data/web/data:/var/uoj_data
ports:
- "80:80"
- "3690:3690"

9
install/db/Dockerfile Normal file
View File

@ -0,0 +1,9 @@
FROM mysql:latest
MAINTAINER MascoSkray <MascoSkray@gmail.com>
#Update apt and install curl
RUN apt-get update && apt-get install -y curl
#Run the latest UOJ Community verison db install script
RUN export RAW_URL=https://raw.githubusercontent.com/UniversalOJ/UOJ-System/master && curl $RAW_URL/install/db/install.sh | sh
ENV LANG=C.UTF-8 TZ=Asia/Shanghai

29
install/db/install.sh Normal file
View File

@ -0,0 +1,29 @@
#!/bin/bash
setLAMPConf(){
printf "\n\n==> Setting LAMP configs\n"
#Set MySQL connection config
cat >/etc/mysql/conf.d/uoj_mysqld.cnf <<UOJEOF
[mysqld]
default-time-zone='+8:00'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
init_connect='SET collation_connection = utf8mb4_unicode_ci'
skip-character-set-client-handshake
sql-mode=ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
UOJEOF
}
setWebConf(){
printf "\n\n==> Setting web files\n"
#Import MySQL database
cat >/docker-entrypoint-initdb.d/000-native_password.sql <<UOJEOF
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
UOJEOF
curl $RAW_URL/install/db/app_uoj233.sql >/docker-entrypoint-initdb.d/001-app_uoj233.sql
curl $RAW_URL/install/judger/add_judger.sql >/docker-entrypoint-initdb.d/002-add_judger.sql
}
echo 'Preparing UOJ System db environment...'
setLAMPConf;setWebConf

View File

@ -1,30 +0,0 @@
FROM ubuntu:14.04
MAINTAINER vfleaking vfleaking@163.com
COPY docker/sources.list /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y vim \
ntp \
build-essential \
python \
python-requests \
git \
unzip
COPY docker/jdk-7u76-linux-x64.tar.gz \
docker/jdk-8u31-linux-x64.tar.gz \
docker/judge_client/conf.json \
/root/
COPY docker/judge_client/cur_install /root/install
RUN cd /root && chmod +x install
RUN cd /root && ./install && rm * -rf
COPY docker/judge_client/up /root/up
RUN chmod +x /root/up
EXPOSE 2333
CMD /root/up

View File

@ -1,37 +0,0 @@
#!/usr/bin/python
import sys, json, random, os
def translate(filename, target, tab):
with open(filename, 'r') as f:
content = f.read()
for k, v in tab.items():
content = content.replace('__' + k + '__', v)
with open(target, 'w') as f:
f.write(content)
cid = raw_input('uoj container id: ')
ip = raw_input('uoj ip: ')
name = raw_input('judger name: ')
os.system("docker cp " + cid + ":/home/local_main_judger/judge_client/.conf.json conf.json")
with open('conf.json', 'r') as f:
conf = json.load(f)
conf['uoj_host'] = ip
conf['judger_name'] = name
conf['judger_password'] = ''.join(random.choice('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') for i in range(32))
with open('conf.json', 'w') as f:
json.dump(conf, f, indent=4, separators=(',', ': '))
print >>f
translate_table = {
'uoj_host': ip
}
translate('install', 'cur_install', translate_table)
print "please modify the database after getting the judger server ready:"
print "insert into judger_info (judger_name, password, ip) values ('%s', '%s', '__judger_ip_here__');" % (name, conf['judger_password'])

View File

@ -1,29 +0,0 @@
#!/bin/sh
sudo adduser judger --gecos "" --disabled-password
mv /root/jdk-7u76-linux-x64.tar.gz /root/jdk-8u31-linux-x64.tar.gz /root/conf.json /home/judger
chown judger /home/judger/jdk-7u76-linux-x64.tar.gz /home/judger/jdk-8u31-linux-x64.tar.gz /home/judger/conf.json
chgrp judger /home/judger/jdk-7u76-linux-x64.tar.gz /home/judger/jdk-8u31-linux-x64.tar.gz /home/judger/conf.json
mkdir /var/uoj_data_copy
chown judger /var/uoj_data_copy
su judger <<EOD
cd ~
git clone https://github.com/Universal/UOJ-System.git
ln -s UOJ-System/judge_client/1 judge_client
cd judge_client
echo '#define UOJ_WORK_PATH "/home/judger/judge_client/uoj_judger"' >uoj_judger/include/uoj_work_path.h
make
ln -s /var/uoj_data_copy ~/judge_client/uoj_judger/data
mkdir ~/judge_client/uoj_judger/run/runtime
mv ~/jdk-7u76-linux-x64.tar.gz ~/jdk-8u31-linux-x64.tar.gz ~/judge_client/uoj_judger/run/runtime
cd ~/judge_client/uoj_judger/run/runtime
tar -xzf jdk-7u76-linux-x64.tar.gz
tar -xzf jdk-8u31-linux-x64.tar.gz
mv ~/conf.json ~/judge_client/.conf.json
chmod 600 ~/judge_client/.conf.json
EOD

View File

@ -1,3 +0,0 @@
#!/bin/sh
docker build -f Dockerfile .

View File

@ -1,7 +0,0 @@
#!/bin/sh
service ntp start
su judger -c '~/judge_client/judge_client start'
exec bash

22
install/judger/Dockerfile Normal file
View File

@ -0,0 +1,22 @@
FROM ubuntu:18.04
MAINTAINER MascoSkray <MascoSkray@gmail.com>
ARG CLONE_ADDFLAG
WORKDIR /opt
#Update apt and install git
RUN apt-get update && apt-get install -y git
#Clone the latest UOJ Community verison to local
RUN git clone https://github.com/UniversalOJ/UOJ-System.git --depth 1 --single-branch ${CLONE_ADDFLAG} uoj
#Install environment and set startup script
RUN cd uoj/install/judger && sh install.sh -p && echo "\
#!/bin/sh\n\
if [ ! -f \"/opt/uoj/judger/.conf.json\" ]; then\n\
cd /opt/uoj/install/judger && sh install.sh -i\n\
fi\n\
service ntp start\n\
su judger -c \"/opt/uoj/judger/judge_client start\"\n\
exec bash\n" >/opt/up && chmod +x /opt/up
ENV LANG=C.UTF-8 TZ=Asia/Shanghai
EXPOSE 2333
CMD /opt/up

View File

@ -0,0 +1,2 @@
USE `app_uoj233`;
insert into judger_info (judger_name, password, ip) values ('compose_judger', '_judger_password_', 'uoj-judger');

81
install/judger/install.sh Normal file
View File

@ -0,0 +1,81 @@
#!/bin/bash
getAptPackage(){
printf "\n\n==> Getting environment packages\n"
export DEBIAN_FRONTEND=noninteractive
apt-get update && apt-get install -y vim ntp zip unzip curl wget build-essential fp-compiler python python3 python3-requests openjdk-8-jdk openjdk-11-jdk
}
setJudgeConf(){
printf "\n\n==> Setting judger files\n"
#Add judger user
adduser judger --gecos "" --disabled-password
#Set uoj_data path
mkdir /var/uoj_data_copy && chown judger /var/uoj_data_copy
#Compile uoj_judger and set runtime
chown -R judger:judger /opt/uoj/judger
su judger <<EOD
ln -s /var/uoj_data_copy /opt/uoj/judger/uoj_judger/data
cd /opt/uoj/judger && chmod +x judge_client
cat >uoj_judger/include/uoj_work_path.h <<UOJEOF
#define UOJ_WORK_PATH "/opt/uoj/judger/uoj_judger"
#define UOJ_JUDGER_BASESYSTEM_UBUNTU1804
#define UOJ_JUDGER_PYTHON3_VERSION "3.6"
#define UOJ_JUDGER_FPC_VERSION "3.0.4"
UOJEOF
cd uoj_judger && make -j$(($(nproc) + 1))
EOD
}
initProgress(){
printf "\n\n==> Doing initial config and start service\n"
#Check envs
if [ -z "$UOJ_PROTOCOL" -o -z "$UOJ_HOST" -o -z "$JUDGER_NAME" -o -z "$JUDGER_PASSWORD" -o -z "$SOCKET_PORT" -o -z "$SOCKET_PASSWORD" ]; then
echo "!! Environment variables not set! Please edit config file by yourself!"
else
#Set judge_client config file
cat >../../judger/.conf.json <<UOJEOF
{
"uoj_protocol": "$UOJ_PROTOCOL",
"uoj_host": "$UOJ_HOST",
"judger_name": "$JUDGER_NAME",
"judger_password": "$JUDGER_PASSWORD",
"socket_port": $SOCKET_PORT,
"socket_password": "$SOCKET_PASSWORD"
}
UOJEOF
chmod 600 ../../judger/.conf.json && chown judger ../../judger/.conf.json
chown -R judger:judger ../../judger/log
#Start services
service ntp restart
su judger -c '/opt/uoj/judger/judge_client start'
echo "please modify the database after getting the judger server ready:"
echo "insert into judger_info (judger_name, password, ip) values ('$JUDGER_NAME', '$JUDGER_PASSWORD', '__judger_ip_here__');"
printf "\n\n***Installation complete. Enjoy!***\n"
fi
}
prepProgress(){
getAptPackage;setJudgeConf
}
if [ $# -le 0 ]; then
echo 'Installing UOJ System judger...'
prepProgress;initProgress
fi
while [ $# -gt 0 ]; do
case "$1" in
-p | --prep)
echo 'Preparing UOJ System judger environment...'
prepProgress
;;
-i | --init)
echo 'Initing UOJ System judger...'
initProgress
;;
-? | --*)
echo "Illegal option $1"
;;
esac
shift $(( $#>0?1:0 ))
done

22
install/web/Dockerfile Normal file
View File

@ -0,0 +1,22 @@
FROM ubuntu:18.04
MAINTAINER MascoSkray <MascoSkray@gmail.com>
ARG CLONE_ADDFLAG
WORKDIR /opt
#Update apt and install git
RUN apt-get update && apt-get install -y git
#Clone the latest UOJ Community verison to local
RUN git clone https://github.com/UniversalOJ/UOJ-System.git --depth 1 --single-branch ${CLONE_ADDFLAG} uoj
#Install environment and set startup script
RUN cd uoj/install/web && sh install.sh -p && echo "\
#!/bin/sh\n\
if [ ! -f \"/var/uoj_data/.UOJSetupDone\" ]; then\n\
cd /opt/uoj/install/web && sh install.sh -i\n\
fi\n\
service ntp start\n\
service apache2 start\n\
exec bash\n" >/opt/up && chmod +x /opt/up
ENV LANG=C.UTF-8 TZ=Asia/Shanghai
EXPOSE 80 3690
CMD /opt/up

115
install/web/install.sh Normal file
View File

@ -0,0 +1,115 @@
#!/bin/bash
genRandStr(){
cat /dev/urandom | tr -dc [:alnum:] | head -c $1
}
#Set some vars
_database_host_=uoj-db
_database_password_=root
_judger_socket_port_=2333
_judger_socket_password_=_judger_socket_password_
getAptPackage(){
printf "\n\n==> Getting environment packages\n"
#Update apt sources and install
export DEBIAN_FRONTEND=noninteractive
dpkg -s gnupg 2>/dev/null || (apt-get update && apt-get install -y gnupg)
echo "deb http://ppa.launchpad.net/stesie/libv8/ubuntu bionic main" | tee /etc/apt/sources.list.d/stesie-libv8.list && apt-key adv --keyserver keyserver.ubuntu.com --recv-keys D858A0DF
apt-get update && apt-get install -y vim ntp zip unzip curl wget apache2 libapache2-mod-xsendfile libapache2-mod-php php php-dev php-pear php-zip php-mysql php-mbstring g++ cmake re2c libv8-7.5-dev libyaml-dev
#Install PHP extensions
printf "/opt/libv8-7.5\n\n" | pecl install v8js yaml
}
setLAMPConf(){
printf "\n\n==> Setting LAMP configs\n"
#Set Apache UOJ site conf
cat >/etc/apache2/sites-available/000-uoj.conf <<UOJEOF
<VirtualHost *:80>
#ServerName local_uoj.ac
ServerAdmin opensource@uoj.ac
DocumentRoot /var/www/uoj
SetEnvIf Request_URI "^/judge/.*$" judgelog
#LogLevel info ssl:warn
ErrorLog \${APACHE_LOG_DIR}/uoj_error.log
CustomLog \${APACHE_LOG_DIR}/uoj_judge.log common env=judgelog
CustomLog \${APACHE_LOG_DIR}/uoj_access.log combined env=!judgelog
XSendFile On
XSendFilePath /var/uoj_data
XSendFilePath /var/www/uoj/app/storage
XSendFilePath /opt/uoj/judger/uoj_judger/include
</VirtualHost>
UOJEOF
#Enable modules and make UOJ site conf enabled
a2ensite 000-uoj.conf && a2dissite 000-default.conf
a2enmod rewrite headers && sed -i -e '172s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf
#Create UOJ session save dir and make PHP extensions available
mkdir --mode=733 /var/lib/php/uoj_sessions && chmod +t /var/lib/php/uoj_sessions
sed -i -e '865a\extension=v8js.so\nextension=yaml.so' /etc/php/7.2/apache2/php.ini
}
setWebConf(){
printf "\n\n==> Setting web files\n"
#Set webroot path
ln -sf /opt/uoj/web /var/www/uoj
chown -R www-data /var/www/uoj/app/storage
#Set web config file
php -a <<UOJEOF
\$config = include '/var/www/uoj/app/.default-config.php';
\$config['database']['host']='$_database_host_';
\$config['database']['password']='$_database_password_';
\$config['judger']['socket']['port']='$_judger_socket_port_';
file_put_contents('/var/www/uoj/app/.config.php', "<?php\nreturn ".str_replace('\'_httpHost_\'','UOJContext::httpHost()',var_export(\$config, true)).";\n");
UOJEOF
#Prepare local sandbox
cd ../../judger/uoj_judger
cat >include/uoj_work_path.h <<UOJEOF
#define UOJ_WORK_PATH "/opt/uoj/judger/uoj_judger"
#define UOJ_JUDGER_BASESYSTEM_UBUNTU1804
#define UOJ_JUDGER_PYTHON3_VERSION "3.6"
#define UOJ_JUDGER_FPC_VERSION "3.0.4"
UOJEOF
make -j$(($(nproc) + 1)) && cd ../../install/web
}
initProgress(){
printf "\n\n==> Doing initial config and start service\n"
#Set uoj_data path
mkdir -p /var/uoj_data/upload
chown -R www-data:www-data /var/uoj_data
#Replace password placeholders
sed -i -e "s/salt0/$(genRandStr 32)/g" -e "s/salt1/$(genRandStr 16)/g" -e "s/salt2/$(genRandStr 16)/g" -e "s/salt3/$(genRandStr 16)/g" -e "s/_judger_socket_password_/$_judger_socket_password_/g" /var/www/uoj/app/.config.php
#Using cli upgrade to latest
php /var/www/uoj/app/cli.php upgrade:latest
#Start services
service ntp restart
service apache2 restart
#Touch SetupDone flag file
touch /var/uoj_data/.UOJSetupDone
printf "\n\n***Installation complete. Enjoy!***\n"
}
prepProgress(){
getAptPackage;setLAMPConf;setWebConf
}
if [ $# -le 0 ]; then
echo 'Installing UOJ System web...'
prepProgress;initProgress
fi
while [ $# -gt 0 ]; do
case "$1" in
-p | --prep)
echo 'Preparing UOJ System web environment...'
prepProgress
;;
-i | --init)
echo 'Initing UOJ System web...'
initProgress
;;
-? | --*)
echo "Illegal option $1"
;;
esac
shift $(( $#>0?1:0 ))
done