From d413b746eb16f56a95c6b6bf6dc4eb8ec60a765d Mon Sep 17 00:00:00 2001 From: Masco Skray Date: Tue, 16 Jul 2019 14:13:28 +0800 Subject: [PATCH] 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. --- install/bundle/install.sh | 10 +- install/compose/docker-compose.yml | 47 +++++++++ install/db/Dockerfile | 9 ++ install/{bundle => db}/app_uoj233.sql | 0 install/db/install.sh | 29 ++++++ install/judge_client/Dockerfile | 30 ------ install/judge_client/config_judge_client | 37 ------- install/judge_client/install | 29 ------ install/judge_client/install_judge_client | 3 - install/judge_client/up | 7 -- install/judger/Dockerfile | 22 +++++ install/judger/add_judger.sql | 2 + install/judger/install.sh | 81 +++++++++++++++ install/web/Dockerfile | 22 +++++ install/web/install.sh | 115 ++++++++++++++++++++++ 15 files changed, 332 insertions(+), 111 deletions(-) create mode 100644 install/compose/docker-compose.yml create mode 100644 install/db/Dockerfile rename install/{bundle => db}/app_uoj233.sql (100%) create mode 100644 install/db/install.sh delete mode 100644 install/judge_client/Dockerfile delete mode 100644 install/judge_client/config_judge_client delete mode 100644 install/judge_client/install delete mode 100644 install/judge_client/install_judge_client delete mode 100644 install/judge_client/up create mode 100644 install/judger/Dockerfile create mode 100644 install/judger/add_judger.sql create mode 100644 install/judger/install.sh create mode 100644 install/web/Dockerfile create mode 100644 install/web/install.sh diff --git a/install/bundle/install.sh b/install/bundle/install.sh index 654077b..86bbd37 100644 --- a/install/bundle/install.sh +++ b/install/bundle/install.sh @@ -76,7 +76,7 @@ file_put_contents('/var/www/uoj/app/.config.php', " + +#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 diff --git a/install/bundle/app_uoj233.sql b/install/db/app_uoj233.sql similarity index 100% rename from install/bundle/app_uoj233.sql rename to install/db/app_uoj233.sql diff --git a/install/db/install.sh b/install/db/install.sh new file mode 100644 index 0000000..063cf41 --- /dev/null +++ b/install/db/install.sh @@ -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 < Setting web files\n" + #Import MySQL database + cat >/docker-entrypoint-initdb.d/000-native_password.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 diff --git a/install/judge_client/Dockerfile b/install/judge_client/Dockerfile deleted file mode 100644 index 5a240d8..0000000 --- a/install/judge_client/Dockerfile +++ /dev/null @@ -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 diff --git a/install/judge_client/config_judge_client b/install/judge_client/config_judge_client deleted file mode 100644 index 93d3c48..0000000 --- a/install/judge_client/config_judge_client +++ /dev/null @@ -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']) diff --git a/install/judge_client/install b/install/judge_client/install deleted file mode 100644 index 72105bb..0000000 --- a/install/judge_client/install +++ /dev/null @@ -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 <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 diff --git a/install/judge_client/install_judge_client b/install/judge_client/install_judge_client deleted file mode 100644 index e1fbce4..0000000 --- a/install/judge_client/install_judge_client +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -docker build -f Dockerfile . diff --git a/install/judge_client/up b/install/judge_client/up deleted file mode 100644 index 84c8083..0000000 --- a/install/judge_client/up +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh - -service ntp start - -su judger -c '~/judge_client/judge_client start' - -exec bash diff --git a/install/judger/Dockerfile b/install/judger/Dockerfile new file mode 100644 index 0000000..98f3c4c --- /dev/null +++ b/install/judger/Dockerfile @@ -0,0 +1,22 @@ +FROM ubuntu:18.04 +MAINTAINER MascoSkray +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 diff --git a/install/judger/add_judger.sql b/install/judger/add_judger.sql new file mode 100644 index 0000000..164db47 --- /dev/null +++ b/install/judger/add_judger.sql @@ -0,0 +1,2 @@ +USE `app_uoj233`; +insert into judger_info (judger_name, password, ip) values ('compose_judger', '_judger_password_', 'uoj-judger'); diff --git a/install/judger/install.sh b/install/judger/install.sh new file mode 100644 index 0000000..bcebe99 --- /dev/null +++ b/install/judger/install.sh @@ -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 <uoj_judger/include/uoj_work_path.h < 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 <0?1:0 )) +done diff --git a/install/web/Dockerfile b/install/web/Dockerfile new file mode 100644 index 0000000..d04dc17 --- /dev/null +++ b/install/web/Dockerfile @@ -0,0 +1,22 @@ +FROM ubuntu:18.04 +MAINTAINER MascoSkray +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 diff --git a/install/web/install.sh b/install/web/install.sh new file mode 100644 index 0000000..c24d2d3 --- /dev/null +++ b/install/web/install.sh @@ -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 < + #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 + +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 <include/uoj_work_path.h < 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