feat(install/bundle): add install script PHP7 and Ubuntu 16.04 support

In order to support PHP7 and Ubuntu 16.04, there must be some changes to make it runs properly.
The install script added php7 and mysql 5.7, newer v8js packages and supports, adjusted some config files.
Also used new way to detect install args, and change tab to 4-char-long space char.

BREAKING CHANGE: This branch can only be used for PHP7, and Ubuntu 14.04 users shouln't execute this script.
This commit is contained in:
Masco Skray 2018-04-15 15:06:43 +08:00
parent 2d0b0814fd
commit 163dd8bb30

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
genRandStr(){ genRandStr(){
cat /dev/urandom | tr -dc [:alnum:] | head -c $1 cat /dev/urandom | tr -dc [:alnum:] | head -c $1
} }
#Set some vars #Set some vars
_database_password_=root _database_password_=root
@ -11,94 +11,103 @@ _svn_ourroot_password_=$(genRandStr 32)
_svn_certroot_password_=$(genRandStr 32) _svn_certroot_password_=$(genRandStr 32)
getAptPackage(){ getAptPackage(){
echo -e "\n\n==> Getting environment packages" echo -e "\n\n==> Getting environment packages"
#Set MySQL root password #Set MySQL root password
debconf-set-selections <<< "mysql-server-5.7 mysql-server/root_password password $_database_password_" && debconf-set-selections <<< "mysql-server-5.7 mysql-server/root_password_again password $_database_password_" debconf-set-selections <<< "mysql-server mysql-server/root_password password $_database_password_" && debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $_database_password_"
#Update apt sources and install #Update apt sources and install
apt-get update -y echo "deb http://ppa.launchpad.net/pinepain/libv8/ubuntu xenial main" | tee -a /etc/apt/sources.list.d/pinepain-libv8.list && apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 60C60AA4
apt install -y vim ntp zip unzip curl wget subversion apache2 mysql-server mysql-client php libapache2-mod-php7.0 php7.0-mysql cmake fp-compiler re2c libv8-dev libyaml-dev python python3 python-requests apt-get update -y && apt-get install -y vim ntp zip unzip curl wget subversion apache2 libapache2-mod-xsendfile libapache2-mod-php php php-dev php-pear php-zip php-mysql mysql-server cmake fp-compiler re2c libv8-6.6-dev libyaml-dev python python3 python-requests
#Install PHP extensions #Install PHP extensions
echo -e "\n\n" | pecl install v8js-0.1.3 yaml-1.3.1 cp -a /opt/libv8*/* /usr && echo -e "\n\n" | pecl install v8js yaml
} }
getOracleJDK(){ getOracleJDK(){
echo -e "\n\n==> Getting JDK runtime files" echo -e "\n\n==> Getting JDK runtime files"
#Add judger user #Add 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
#Get newest jdk dist file #Get newest jdk dist file
JDK_MIRROR_LINK=http://ftp.osuosl.org/pub/funtoo/distfiles/oracle-java/ JDK_MIRROR_LINK=http://ftp.osuosl.org/pub/funtoo/distfiles/oracle-java/
JDK_CNMIRROR_LINK=http://funtoo.neu.edu.cn/funtoo/distfiles/oracle-java/ JDK_CNMIRROR_LINK=http://funtoo.neu.edu.cn/funtoo/distfiles/oracle-java/
curl -s ${JDK_MIRROR_LINK} | grep -oP '>jdk-[7,8].*-linux-x64.tar' | sed -e 's/[\",>]//g' -e 's/-linux-x64.tar//g' >jdkdist.list curl -s ${JDK_MIRROR_LINK} | grep -oP '>jdk-[7,8].*-linux-x64.tar' | sed -e 's/[\",>]//g' -e 's/-linux-x64.tar//g' >jdkdist.list
wget ${JDK_MIRROR_LINK}$(sed -n '1p' jdkdist.list)-linux-x64.tar.gz && wget ${JDK_MIRROR_LINK}$(sed -n '2p' jdkdist.list)-linux-x64.tar.gz wget ${JDK_MIRROR_LINK}$(sed -n '1p' jdkdist.list)-linux-x64.tar.gz && wget ${JDK_MIRROR_LINK}$(sed -n '2p' jdkdist.list)-linux-x64.tar.gz
#Change jdk version to faq.php #Change jdk version to faq.php
sed -i -e "s/jdk-7u76/$(sed -n '1p' jdkdist.list)/g" -e "s/jdk-8u31/$(sed -n '2p' jdkdist.list)/g" ../../uoj/1/app/controllers/faq.php sed -i -e "s/jdk-7u76/$(sed -n '1p' jdkdist.list)/g" -e "s/jdk-8u31/$(sed -n '2p' jdkdist.list)/g" ../../uoj/1/app/controllers/faq.php
#Move jdk file to judge user root #Move jdk file to judge user root
chown local_main_judger jdkdist.list jdk-*-linux-x64.tar.gz chown local_main_judger jdkdist.list jdk-*-linux-x64.tar.gz
mv jdkdist.list jdk-*-linux-x64.tar.gz /home/local_main_judger/ mv jdkdist.list jdk-*-linux-x64.tar.gz /home/local_main_judger/
} }
setLAMPConf(){ setLAMPConf(){
echo -e "\n\n==> Setting LAMP configs" echo -e "\n\n==> Setting LAMP configs"
#Set Apache UOJ site conf #Set Apache UOJ site conf
cat >/etc/apache2/sites-available/000-uoj.conf <<UOJEOF cat >/etc/apache2/sites-available/000-uoj.conf <<UOJEOF
<VirtualHost *:80> <VirtualHost *:80>
#ServerName local_uoj.ac #ServerName local_uoj.ac
ServerAdmin vfleaking@163.com ServerAdmin opensource@uoj.ac
DocumentRoot /var/www/uoj DocumentRoot /var/www/uoj
#LogLevel info ssl:warn #LogLevel info ssl:warn
ErrorLog \${APACHE_LOG_DIR}/uoj_error.log ErrorLog \${APACHE_LOG_DIR}/uoj_error.log
CustomLog \${APACHE_LOG_DIR}/uoj_access.log combined CustomLog \${APACHE_LOG_DIR}/uoj_access.log combined
XSendFile On XSendFile On
XSendFilePath /var/uoj_data XSendFilePath /var/uoj_data
XSendFilePath /var/www/uoj/app/storage XSendFilePath /var/www/uoj/app/storage
XSendFilePath /home/local_main_judger/judge_client/uoj_judger/include XSendFilePath /home/local_main_judger/judge_client/uoj_judger/include
</VirtualHost> </VirtualHost>
UOJEOF UOJEOF
#Enable modules and make UOJ site conf enabled #Enable modules and make UOJ site conf enabled
a2ensite 000-uoj.conf && a2dissite 000-default.conf a2ensite 000-uoj.conf && a2dissite 000-default.conf
a2enmod rewrite headers && sed -i -e '166s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf a2enmod rewrite headers && sed -i -e '166s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf
#Create UOJ session save dir and make PHP extensions available #Create UOJ session save dir and make PHP extensions available
mkdir --mode=733 /var/lib/php5/uoj && chmod +t /var/lib/php5/uoj mkdir --mode=733 /var/lib/php/uoj_sessions && chmod +t /var/lib/php/uoj_sessions
sed -i -e '866a\extension=v8js.so\nextension=yaml.so' /etc/php5/apache2/php.ini sed -i -e 's/session.save_path \/var\/lib\/php5\/uoj/session.save_path \/var\/lib\/php\/uoj_sessions/g' ../../uoj/1/.htaccess
#Set MySQL user directory and connection config sed -i -e '876a\extension=v8js.so\nextension=yaml.so' /etc/php/7.0/apache2/php.ini
usermod -d /var/lib/mysql/ mysql #Set MySQL user directory and connection config
sed -i -e "108a\init_connect='SET collation_connection = utf8_unicode_ci'\ninit_connect='SET NAMES utf8'\ncharacter-set-server=utf8\ncollation-server=utf8_unicode_ci\nskip-character-set-client-handshake" -e "117a\default-character-set=utf8" /etc/mysql/my.cnf usermod -d /var/lib/mysql/ mysql
cat >/etc/mysql/mysql.conf.d/uoj_mysqld.cnf <<UOJEOF
[mysqld]
character-set-server=utf8
collation-server=utf8_unicode_ci
init_connect='SET NAMES utf8'
init_connect='SET collation_connection = utf8_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_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
UOJEOF
} }
setSVNServe(){ setSVNServe(){
echo -e "\n\n==> Setting SVN server" echo -e "\n\n==> Setting SVN server"
#Make SVN work dir #Make SVN work dir
mkdir /var/svn && svnserve -d -r /var/svn mkdir /var/svn && svnserve -d -r /var/svn
mkdir /var/svn/problem && chown www-data /var/svn/problem -R mkdir /var/svn/problem && chown www-data /var/svn/problem -R
svnadmin create /var/svn/uoj && svnadmin create /var/svn/judge_client svnadmin create /var/svn/uoj && svnadmin create /var/svn/judge_client
#Set SVN server config file and password db #Set SVN server config file and password db
sed -i -e 's/# store-plaintext-passwords = no/store-plaintext-passwords = yes/g' /etc/subversion/servers sed -i -e 's/# store-plaintext-passwords = no/store-plaintext-passwords = yes/g' /etc/subversion/servers
sed -i -e 's/# anon-access/anon-access/g' -e 's/# auth-access/auth-access/g' -e 's/# password-db/password-db/g' /var/svn/uoj/conf/svnserve.conf sed -i -e 's/# anon-access/anon-access/g' -e 's/# auth-access/auth-access/g' -e 's/# password-db/password-db/g' /var/svn/uoj/conf/svnserve.conf
rm -r /var/svn/judge_client/conf/passwd && ln -s /var/svn/uoj/conf/passwd /var/svn/judge_client/conf/passwd rm -r /var/svn/judge_client/conf/passwd && ln -s /var/svn/uoj/conf/passwd /var/svn/judge_client/conf/passwd
cat /var/svn/uoj/conf/svnserve.conf >/var/svn/judge_client/conf/svnserve.conf cat /var/svn/uoj/conf/svnserve.conf >/var/svn/judge_client/conf/svnserve.conf
cat >/var/svn/uoj/conf/passwd <<UOJEOF cat >/var/svn/uoj/conf/passwd <<UOJEOF
[users] [users]
root = $_svn_certroot_password_ root = $_svn_certroot_password_
UOJEOF UOJEOF
#Set SVN hook scripts #Set SVN hook scripts
cat >/var/svn/uoj/hooks/post-commit <<UOJEOF cat >/var/svn/uoj/hooks/post-commit <<UOJEOF
#!/bin/sh #!/bin/sh
cd /var/svn/uoj/cur/uoj && svn update --username root --password $_svn_certroot_password_ cd /var/svn/uoj/cur/uoj && svn update --username root --password $_svn_certroot_password_
UOJEOF UOJEOF
chmod +x /var/svn/uoj/hooks/post-commit chmod +x /var/svn/uoj/hooks/post-commit
cat >/var/svn/judge_client/hooks/post-commit <<UOJEOF cat >/var/svn/judge_client/hooks/post-commit <<UOJEOF
#!/bin/sh #!/bin/sh
su local_main_judger -c '~/judge_client/judge_client update' su local_main_judger -c '~/judge_client/judge_client update'
exit \$? exit \$?
UOJEOF UOJEOF
chmod +x /var/svn/judge_client/hooks/post-commit chmod +x /var/svn/judge_client/hooks/post-commit
cat >/var/svn/problem/new_problem.sh <<UOJEOF cat >/var/svn/problem/new_problem.sh <<UOJEOF
if [ \$# -ne 1 ] if [ \$# -ne 1 ]
then then
echo 'invalid argument' echo 'invalid argument'
exit 1 exit 1
fi fi
path=/var/svn/problem/\$1 path=/var/svn/problem/\$1
@ -129,8 +138,8 @@ cat >\$path/hooks/post-commit <<EODEOD
EODEOD EODEOD
chmod +x \$path/hooks/post-commit chmod +x \$path/hooks/post-commit
UOJEOF UOJEOF
chmod +x /var/svn/problem/new_problem.sh chmod +x /var/svn/problem/new_problem.sh
cat >/var/svn/problem/post-commit.sh <<UOJEOF cat >/var/svn/problem/post-commit.sh <<UOJEOF
#!/bin/sh #!/bin/sh
svnusr="our-root" svnusr="our-root"
svnpwd="$_svn_ourroot_password_" svnpwd="$_svn_ourroot_password_"
@ -138,26 +147,26 @@ cd /var/svn/problem/\$1/cur/\$1
svn update --username \$svnusr --password \$svnpwd svn update --username \$svnusr --password \$svnpwd
chown www-data /var/svn/problem/\$1 -R chown www-data /var/svn/problem/\$1 -R
UOJEOF UOJEOF
chmod +x /var/svn/problem/post-commit.sh chmod +x /var/svn/problem/post-commit.sh
#Precheckout to cur folder #Precheckout to cur folder
mkdir /var/svn/uoj/cur /var/svn/judge_client/cur mkdir /var/svn/uoj/cur /var/svn/judge_client/cur
svn co svn://127.0.0.1/uoj --username root --password $_svn_certroot_password_ /var/svn/uoj/cur/uoj svn co svn://127.0.0.1/uoj --username root --password $_svn_certroot_password_ /var/svn/uoj/cur/uoj
svn co svn://127.0.0.1/judge_client --username root --password $_svn_certroot_password_ /var/svn/judge_client/cur/judge_client svn co svn://127.0.0.1/judge_client --username root --password $_svn_certroot_password_ /var/svn/judge_client/cur/judge_client
chown local_main_judger /var/svn/judge_client/cur/judge_client -R chown local_main_judger /var/svn/judge_client/cur/judge_client -R
} }
setWebConf(){ setWebConf(){
echo -e "\n\n==> Setting web files" echo -e "\n\n==> Setting web files"
#Commit web source file #Commit web source file
svn co svn://127.0.0.1/uoj --username root --password $_svn_certroot_password_ svn co svn://127.0.0.1/uoj --username root --password $_svn_certroot_password_
mv ../../uoj/1 uoj/1 && cd uoj mv ../../uoj/1 uoj/1 && cd uoj
svn add 1 && svn ci -m "Installtion commit" --username root --password $_svn_certroot_password_ svn add 1 && svn ci -m "Installtion commit" --username root --password $_svn_certroot_password_
cd .. && rm uoj /var/www/uoj -r cd .. && rm uoj /var/www/uoj -r
#Set webroot path #Set webroot path
ln -s /var/svn/uoj/cur/uoj/1 /var/www/uoj ln -s /var/svn/uoj/cur/uoj/1 /var/www/uoj
chown www-data /var/www/uoj/app/storage -R chown www-data /var/www/uoj/app/storage -R
#Set web config file #Set web config file
php -a <<UOJEOF php -a <<UOJEOF
\$config = include '/var/www/uoj/app/.default-config.php'; \$config = include '/var/www/uoj/app/.default-config.php';
\$config['database']['password']='$_database_password_'; \$config['database']['password']='$_database_password_';
\$config['security']['user']['client_salt']='$(genRandStr 32)'; \$config['security']['user']['client_salt']='$(genRandStr 32)';
@ -167,23 +176,23 @@ setWebConf(){
\$config['svn']['our-root']['password']='$_svn_ourroot_password_'; \$config['svn']['our-root']['password']='$_svn_ourroot_password_';
file_put_contents('/var/www/uoj/app/.config.php', "<?php\nreturn ".str_replace('\'_httpHost_\'','UOJContext::httpHost()',var_export(\$config, true)).";\n"); file_put_contents('/var/www/uoj/app/.config.php', "<?php\nreturn ".str_replace('\'_httpHost_\'','UOJContext::httpHost()',var_export(\$config, true)).";\n");
UOJEOF UOJEOF
#Import MySQL database #Import MySQL database
service mysql start service mysql start
mysql -u root --password=$_database_password_ <app_uoj233.sql mysql -u root --password=$_database_password_ <app_uoj233.sql
} }
setJudgeConf(){ setJudgeConf(){
echo -e "\n\n==> Setting judge_client files" echo -e "\n\n==> Setting judge_client files"
#Commit judge_client source file #Commit judge_client source file
svn co svn://127.0.0.1/judge_client --username root --password $_svn_certroot_password_ svn co svn://127.0.0.1/judge_client --username root --password $_svn_certroot_password_
mv ../../judge_client/1 judge_client/1 && cd judge_client mv ../../judge_client/1 judge_client/1 && cd judge_client
svn add 1 && svn ci -m "Installation commit" --username root --password $_svn_certroot_password_ svn add 1 && svn ci -m "Installation commit" --username root --password $_svn_certroot_password_
cd .. && rm judge_client -r cd .. && rm judge_client -r
#Set uoj_data path #Set uoj_data path
mkdir /var/uoj_data mkdir /var/uoj_data
chown www-data /var/uoj_data -R && chgrp www-data /var/uoj_data -R chown www-data /var/uoj_data -R && chgrp www-data /var/uoj_data -R
#Compile judge_client and set runtime #Compile judge_client and set runtime
su local_main_judger <<EOD su local_main_judger <<EOD
svn update /var/svn/judge_client/cur/judge_client --username root --password $_svn_certroot_password_ svn update /var/svn/judge_client/cur/judge_client --username root --password $_svn_certroot_password_
ln -s /var/svn/judge_client/cur/judge_client/1 ~/judge_client ln -s /var/svn/judge_client/cur/judge_client/1 ~/judge_client
ln -s /var/uoj_data ~/judge_client/uoj_judger/data ln -s /var/uoj_data ~/judge_client/uoj_judger/data
@ -195,57 +204,73 @@ mv ~/jdkdist.list ~/jdk-*-linux-x64.tar.gz .
tar -xzf jdk-7*-linux-x64.tar.gz && tar -xzf jdk-8*-linux-x64.tar.gz tar -xzf jdk-7*-linux-x64.tar.gz && tar -xzf jdk-8*-linux-x64.tar.gz
mv jdk1.7* jdk1.7.0_latest && mv jdk1.8* jdk1.8.0_latest mv jdk1.7* jdk1.7.0_latest && mv jdk1.8* jdk1.8.0_latest
EOD EOD
#Set judge_client config file #Set judge_client config file
cat >/home/local_main_judger/judge_client/.conf.json <<UOJEOF cat >/home/local_main_judger/judge_client/.conf.json <<UOJEOF
{ {
"uoj_protocol": "http", "uoj_protocol": "http",
"uoj_host": "127.0.0.1", "uoj_host": "127.0.0.1",
"judger_name": "main_judger", "judger_name": "main_judger",
"judger_password": "$_main_judger_password_", "judger_password": "$_main_judger_password_",
"socket_port": $_judger_socket_port_, "socket_port": $_judger_socket_port_,
"socket_password": "$_judger_socket_password_", "socket_password": "$_judger_socket_password_",
"svn_username": "root", "svn_username": "root",
"svn_password": "$_svn_certroot_password_" "svn_password": "$_svn_certroot_password_"
} }
UOJEOF UOJEOF
chmod 600 /home/local_main_judger/judge_client/.conf.json chmod 600 /home/local_main_judger/judge_client/.conf.json
chown local_main_judger /home/local_main_judger/judge_client/.conf.json chown local_main_judger /home/local_main_judger/judge_client/.conf.json
#Import judge_client to MySQL database #Import judge_client to MySQL database
echo "insert into judger_info (judger_name, password) values (\"main_judger\", \"$_main_judger_password_\")" | mysql app_uoj233 -u root --password=$_database_password_ echo "insert into judger_info (judger_name, password) values (\"main_judger\", \"$_main_judger_password_\")" | mysql app_uoj233 -u root --password=$_database_password_
} }
endUpProgress(){ endUpProgress(){
echo -e "\n\n==> Ending progress and start service" echo -e "\n\n==> Ending progress and start service"
#Using cli upgrade to latest #Using cli upgrade to latest
php /var/www/uoj/app/cli.php upgrade:latest php /var/www/uoj/app/cli.php upgrade:latest
#Start services #Start services
service ntp restart service ntp restart
service mysql restart service mysql restart
service apache2 restart service apache2 restart
su local_main_judger -c '~/judge_client/judge_client start' su local_main_judger -c '~/judge_client/judge_client start'
#Set SetupDone flag file #Set SetupDone flag file
echo 'Congratulations!' > /var/svn/.UOJSetupDone echo 'Congratulations!' > /var/svn/.UOJSetupDone
echo -e "\n\n***Installation complete. Enjoy!***" echo -e "\n\n***Installation complete. Enjoy!***"
} }
if [ "$1" == "-e" ] ;then if [ $# -le 0 ] ;then
echo 'Setting UOJ System bundle environment...' echo 'Installing UOJ System bundle...'
getAptPackage getAptPackage
getOracleJDK getOracleJDK
setLAMPConf setLAMPConf
elif [ "$1" == "-c" ] ;then setSVNServe
echo 'Configuring UOJ System bundle...' setWebConf
setSVNServe setJudgeConf
setWebConf endUpProgress
setJudgeConf
endUpProgress
else
echo 'Installing UOJ System bundle...'
getAptPackage
getOracleJDK
setLAMPConf
setSVNServe
setWebConf
setJudgeConf
endUpProgress
fi fi
while [ $# -gt 0 ]; do
case "$1" in
-e)
;&
--environment)
echo 'Setting UOJ System bundle environment...'
getAptPackage
getOracleJDK
setLAMPConf
;;
-c)
;&
--config)
echo 'Configuring UOJ System bundle...'
setSVNServe
setWebConf
setJudgeConf
endUpProgress
;;
-?)
;&
--*)
echo "Illegal option $1"
;;
esac
shift $(( $#>0?1:0 ))
done