feat(install, svn): remove svn (#45)

* feat(install, svn): remove svn

Remove everything related to svn.
Rename uoj-svn-lib to uoj-data-lib.
Save preparing problem data to /var/uoj_data/prepare/problem_id.

* Typo: uoj_date -> uoj_data

* add: backward compatibility

BREAKING CHANGE: Preparing problem data location is changed
You need to move files from /var/svn/problem/{id}/cur/{id}/1 to
/var/uoj_data/upload/{id}
This commit is contained in:
mcfx0 2019-06-14 23:30:02 +08:00 committed by Masco Skray
parent 94fbfea97f
commit 69912ba9b3
13 changed files with 61 additions and 302 deletions

View File

@ -16,7 +16,6 @@ fi\n\
service ntp start\n\ service ntp start\n\
service mysql start\n\ service mysql start\n\
service apache2 start\n\ service apache2 start\n\
svnserve -d -r /var/svn\n\
su local_main_judger -c \"~/judge_client/judge_client start\"\n\ su local_main_judger -c \"~/judge_client/judge_client start\"\n\
exec bash\n" >/root/up && chmod +x /root/up exec bash\n" >/root/up && chmod +x /root/up

View File

@ -7,8 +7,6 @@ _database_password_=root
_judger_socket_port_=2333 _judger_socket_port_=2333
_judger_socket_password_=$(genRandStr 32) _judger_socket_password_=$(genRandStr 32)
_main_judger_password_=$(genRandStr 32) _main_judger_password_=$(genRandStr 32)
_svn_ourroot_password_=$(genRandStr 32)
_svn_certroot_password_=$(genRandStr 32)
getAptPackage(){ getAptPackage(){
printf "\n\n==> Getting environment packages\n" printf "\n\n==> Getting environment packages\n"
@ -18,7 +16,7 @@ getAptPackage(){
#Update apt sources and install #Update apt sources and install
dpkg -s gnupg 2>/dev/null || (apt-get update && apt-get install -y gnupg) dpkg -s gnupg 2>/dev/null || (apt-get update && apt-get install -y gnupg)
echo "deb http://ppa.launchpad.net/pinepain/libv8/ubuntu artful main" | tee /etc/apt/sources.list.d/pinepain-libv8.list && apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 60C60AA4 echo "deb http://ppa.launchpad.net/pinepain/libv8/ubuntu artful main" | tee /etc/apt/sources.list.d/pinepain-libv8.list && apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 60C60AA4
apt-get update && 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 php-mbstring mysql-server cmake fp-compiler re2c libv8-6.6-dev libyaml-dev python python3 python-requests 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 mysql-server cmake fp-compiler re2c libv8-6.6-dev libyaml-dev python python3 python-requests
#Install PHP extensions #Install PHP extensions
cp -a /opt/libv8*/* /usr && printf "\n\n" | pecl install v8js yaml cp -a /opt/libv8*/* /usr && printf "\n\n" | pecl install v8js yaml
} }
@ -81,94 +79,10 @@ sql-mode=ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_Z
UOJEOF UOJEOF
} }
setSVNServe(){
printf "\n\n==> Setting SVN server\n"
#Make SVN work dir
mkdir /var/svn && svnserve -d -r /var/svn
mkdir /var/svn/problem && chown -R www-data /var/svn/problem
svnadmin create /var/svn/uoj && svnadmin create /var/svn/judge_client
#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/# 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
cat /var/svn/uoj/conf/svnserve.conf >/var/svn/judge_client/conf/svnserve.conf
cat >/var/svn/uoj/conf/passwd <<UOJEOF
[users]
root = _svn_certroot_password_
UOJEOF
#Set SVN hook scripts
cat >/var/svn/uoj/hooks/post-commit <<UOJEOF
#!/bin/sh
cd /var/svn/uoj/cur/uoj && svn update --username root --password _svn_certroot_password_
UOJEOF
chmod +x /var/svn/uoj/hooks/post-commit
cat >/var/svn/judge_client/hooks/post-commit <<UOJEOF
#!/bin/sh
su local_main_judger -c '~/judge_client/judge_client update'
exit \$?
UOJEOF
chmod +x /var/svn/judge_client/hooks/post-commit
cat >/var/svn/problem/new_problem.sh <<UOJEOF
if [ \$# -ne 1 ]
then
echo 'invalid argument'
exit 1
fi
path=/var/svn/problem/\$1
mkdir \$path
svnadmin create \$path
cat >\$path/conf/svnserve.conf <<EOD
[general]
anon-access = none
auth-access = write
password-db = passwd
EOD
svnusr="our-root"
svnpwd="_svn_ourroot_password_"
cat >\$path/conf/passwd <<EOD
[users]
\$svnusr = \$svnpwd
EOD
mkdir \$path/cur && cd \$path/cur
svn checkout svn://127.0.0.1/problem/\$1 --username \$svnusr --password \$svnpwd
mkdir /var/uoj_data/\$1
cat >\$path/hooks/post-commit <<EODEOD
#!/bin/sh
/var/svn/problem/post-commit.sh \$1
EODEOD
chmod +x \$path/hooks/post-commit
UOJEOF
chmod +x /var/svn/problem/new_problem.sh
cat >/var/svn/problem/post-commit.sh <<UOJEOF
#!/bin/sh
svnusr="our-root"
svnpwd="_svn_ourroot_password_"
cd /var/svn/problem/\$1/cur/\$1
svn update --username \$svnusr --password \$svnpwd
chown -R www-data /var/svn/problem/\$1
UOJEOF
chmod +x /var/svn/problem/post-commit.sh
#Precheckout to cur folder
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/judge_client --username root --password '_svn_certroot_password_' /var/svn/judge_client/cur/judge_client
chown -R local_main_judger /var/svn/judge_client/cur/judge_client
}
setWebConf(){ setWebConf(){
printf "\n\n==> Setting web files\n" printf "\n\n==> Setting web files\n"
#Commit web source file
svn co svn://127.0.0.1/uoj --username root --password '_svn_certroot_password_'
cp -r ../../uoj/1 uoj/1 && cd uoj
svn add 1 && svn ci -m "Installtion commit" --username root --password '_svn_certroot_password_'
cd .. && rm -r uoj
#Set webroot path #Set webroot path
ln -sf /var/svn/uoj/cur/uoj/1 /var/www/uoj cp -r ../../uoj/1 /var/www/uoj
chown -R www-data /var/www/uoj/app/storage chown -R www-data /var/www/uoj/app/storage
#Set web config file #Set web config file
php -a <<UOJEOF php -a <<UOJEOF
@ -184,18 +98,13 @@ UOJEOF
setJudgeConf(){ setJudgeConf(){
printf "\n\n==> Setting judge_client files\n" printf "\n\n==> Setting judge_client files\n"
#Commit judge_client source file
svn co svn://127.0.0.1/judge_client --username root --password '_svn_certroot_password_'
cp -r ../../judge_client/1 judge_client/1 && cd judge_client
svn add 1 && svn ci -m "Installation commit" --username root --password '_svn_certroot_password_'
cd .. && rm -r judge_client
#Set uoj_data path #Set uoj_data path
mkdir /var/uoj_data mkdir /var/uoj_data
mkdir /var/uoj_data/prepare
chown -R www-data /var/uoj_data && chgrp -R www-data /var/uoj_data chown -R www-data /var/uoj_data && chgrp -R www-data /var/uoj_data
#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_' cp -r ../../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
cd ~/judge_client && chmod +x judge_client cd ~/judge_client && chmod +x judge_client
cat >uoj_judger/include/uoj_work_path.h <<UOJEOF cat >uoj_judger/include/uoj_work_path.h <<UOJEOF
@ -218,9 +127,7 @@ EOD
"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_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
@ -230,8 +137,6 @@ UOJEOF
initProgress(){ initProgress(){
printf "\n\n==> Doing initial config and start service\n" printf "\n\n==> Doing initial config and start service\n"
#Replace password placeholders #Replace password placeholders
sed -i "s/_svn_ourroot_password_/$_svn_ourroot_password_/g" /var/svn/problem/new_problem.sh /var/svn/problem/post-commit.sh /var/www/uoj/app/.config.php
sed -i "s/_svn_certroot_password_/$_svn_certroot_password_/g" /var/svn/uoj/conf/passwd /var/svn/uoj/hooks/post-commit /home/local_main_judger/judge_client/.conf.json
sed -i -e "s/_main_judger_password_/$_main_judger_password_/g" -e "s/_judger_socket_password_/$_judger_socket_password_/g" /home/local_main_judger/judge_client/.conf.json sed -i -e "s/_main_judger_password_/$_main_judger_password_/g" -e "s/_judger_socket_password_/$_judger_socket_password_/g" /home/local_main_judger/judge_client/.conf.json
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 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
#Import judge_client to MySQL database #Import judge_client to MySQL database
@ -245,12 +150,11 @@ initProgress(){
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'
#Touch SetupDone flag file #Touch SetupDone flag file
touch /var/svn/.UOJSetupDone
printf "\n\n***Installation complete. Enjoy!***\n" printf "\n\n***Installation complete. Enjoy!***\n"
} }
prepProgress(){ prepProgress(){
getAptPackage;getOracleJDK;setLAMPConf;setSVNServe;setWebConf;setJudgeConf getAptPackage;getOracleJDK;setLAMPConf;setWebConf;setJudgeConf
} }
if [ $# -le 0 ] ;then if [ $# -le 0 ] ;then

View File

@ -9,7 +9,7 @@ RUN apt-get install -y vim \
build-essential \ build-essential \
python \ python \
python-requests \ python-requests \
subversion \ git \
unzip unzip
COPY docker/jdk-7u76-linux-x64.tar.gz \ COPY docker/jdk-7u76-linux-x64.tar.gz \

View File

@ -28,7 +28,6 @@ with open('conf.json', 'w') as f:
print >>f print >>f
translate_table = { translate_table = {
'svn_cert': '--username %s --password %s' % (conf['svn_username'], conf['svn_password']),
'uoj_host': ip 'uoj_host': ip
} }

View File

@ -11,11 +11,8 @@ chown judger /var/uoj_data_copy
su judger <<EOD su judger <<EOD
cd ~ cd ~
mkdir judge_client_svn git clone https://github.com/Universal/UOJ-System.git
cd judge_client_svn ln -s UOJ-System/judge_client/1 judge_client
svn co svn://__uoj_host__/judge_client __svn_cert__
cd ~
ln -s judge_client_svn/judge_client/1 judge_client
cd judge_client cd judge_client
echo '#define UOJ_WORK_PATH "/home/judger/judge_client/uoj_judger"' >uoj_judger/include/uoj_work_path.h echo '#define UOJ_WORK_PATH "/home/judger/judge_client/uoj_judger"' >uoj_judger/include/uoj_work_path.h
make make

View File

@ -57,8 +57,6 @@ def init():
assert 'judger_password' in jconf assert 'judger_password' in jconf
assert 'socket_port' in jconf assert 'socket_port' in jconf
assert 'socket_password' in jconf assert 'socket_password' in jconf
assert 'svn_username' in jconf
assert 'svn_password' in jconf
# socket server # socket server
def socket_server_loop(): def socket_server_loop():
@ -138,7 +136,6 @@ def handle_task():
task = taskQ.get_nowait() task = taskQ.get_nowait()
if task['cmd'] == 'update': if task['cmd'] == 'update':
execute('make clean && svn update --username %s --password %s && make' % (jconf['svn_username'], jconf['svn_password']))
if jconf['judger_name'] == 'main_judger': if jconf['judger_name'] == 'main_judger':
uoj_sync_judge_client() uoj_sync_judge_client()
need_restart = True need_restart = True

View File

@ -50,12 +50,6 @@ return [
'password' => '_judger_socket_password_' 'password' => '_judger_socket_password_'
] ]
], ],
'svn' => [
'our-root' => [
'username' => 'our-root',
'password' => '_svn_ourroot_password_'
]
],
'switch' => [ 'switch' => [
'web-analytics' => false, 'web-analytics' => false,
'blog-use-subdomain' => false 'blog-use-subdomain' => false

View File

@ -1,6 +1,6 @@
<?php <?php
requirePHPLib('judger'); requirePHPLib('judger');
requirePHPLib('svn'); requirePHPLib('data');
if (!authenticateJudger()) { if (!authenticateJudger()) {
become404Page(); become404Page();
@ -83,7 +83,7 @@
if ($result['score']) { if ($result['score']) {
list($problem_id) = DB::selectFirst("select problem_id from hacks where id = ${_POST['id']}", MYSQLI_NUM); list($problem_id) = DB::selectFirst("select problem_id from hacks where id = ${_POST['id']}", MYSQLI_NUM);
if (validateUploadedFile('hack_input') && validateUploadedFile('std_output')) { if (validateUploadedFile('hack_input') && validateUploadedFile('std_output')) {
svnAddExtraTest(queryProblemBrief($problem_id), $_FILES["hack_input"]["tmp_name"], $_FILES["std_output"]["tmp_name"]); dataAddExtraTest(queryProblemBrief($problem_id), $_FILES["hack_input"]["tmp_name"], $_FILES["std_output"]["tmp_name"]);
} else { } else {
error_log("hack successfully but received no data. id: ${_POST['id']}"); error_log("hack successfully but received no data. id: ${_POST['id']}");
} }
@ -209,4 +209,4 @@
} }
echo json_encode($submission); echo json_encode($submission);
?> ?>

View File

@ -1,6 +1,5 @@
<?php <?php
requirePHPLib('judger'); requirePHPLib('judger');
requirePHPLib('svn');
if (!authenticateJudger()) { if (!authenticateJudger()) {
become404Page(); become404Page();

View File

@ -1,7 +1,7 @@
<?php <?php
requirePHPLib('form'); requirePHPLib('form');
requirePHPLib('judger'); requirePHPLib('judger');
requirePHPLib('svn'); requirePHPLib('data');
if (!validateUInt($_GET['id']) || !($problem = queryProblemBrief($_GET['id']))) { if (!validateUInt($_GET['id']) || !($problem = queryProblemBrief($_GET['id']))) {
become404Page(); become404Page();
@ -13,34 +13,6 @@
$oj_name = UOJConfig::$data['profile']['oj-name']; $oj_name = UOJConfig::$data['profile']['oj-name'];
$problem_extra_config = getProblemExtraConfig($problem); $problem_extra_config = getProblemExtraConfig($problem);
if (isset($_POST['getsvn'])) {
if (Auth::check()) {
$html = <<<EOD
<base target="_blank" />
<p>{$myUser['username']}您好,</p>
<p>您的SVN密码是{$myUser['svn_password']}</p>
<p>{$oj_name}</p>
<style type="text/css">
body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}
pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word}
</style>
EOD;
$mailer = UOJMail::noreply();
$mailer->addAddress($myUser['email'], $myUser['username']);
$mailer->Subject = "SVN密码";
$mailer->msgHTML($html);
if ($mailer->send()) {
echo 'good';
} else {
error_log('PHPMailer: '.$mailer->ErrorInfo);
}
die();
}
}
$data_dir = "/var/uoj_data/${problem['id']}"; $data_dir = "/var/uoj_data/${problem['id']}";
function echoFileNotFound($file_name) { function echoFileNotFound($file_name) {
@ -84,9 +56,16 @@ EOD;
move_uploaded_file($_FILES["problem_data_file"]["tmp_name"], $up_filename); move_uploaded_file($_FILES["problem_data_file"]["tmp_name"], $up_filename);
$zip = new ZipArchive; $zip = new ZipArchive;
if ($zip->open($up_filename) === TRUE){ if ($zip->open($up_filename) === TRUE){
$zip->extractTo("/var/svn/problem/{$problem['id']}/cur/{$problem['id']}/1"); $zip->extractTo("/var/uoj_data/upload/{$problem['id']}");
$zip->close(); $zip->close();
svnCommitZipData($problem['id'], 'data'); exec(
<<<EOD
cd /var/uoj_data/upload/$problem['id']
for sub_dir in `find -maxdepth 1 -type d ! -name .`; do
mv -f \$sub_dir/* . && rm -rf \$sub_dir
done
EOD
);
echo "<script>alert('上传成功!')</script>"; echo "<script>alert('上传成功!')</script>";
}else{ }else{
$errmsg = "解压失败!"; $errmsg = "解压失败!";
@ -103,10 +82,7 @@ EOD;
//添加配置文件 //添加配置文件
if($_POST['problem_settings_file_submit']=='submit'){ if($_POST['problem_settings_file_submit']=='submit'){
if($_POST['use_builtin_checker'] and $_POST['n_tests'] and $_POST['input_pre'] and $_POST['input_suf'] and $_POST['output_pre'] and $_POST['output_suf'] and $_POST['time_limit'] and $_POST['memory_limit']){ if($_POST['use_builtin_checker'] and $_POST['n_tests'] and $_POST['input_pre'] and $_POST['input_suf'] and $_POST['output_pre'] and $_POST['output_suf'] and $_POST['time_limit'] and $_POST['memory_limit']){
if(!is_dir("/var/svn/problem/{$problem['id']}/cur/{$problem['id']}/1/")){ $set_filename="/var/uoj_data/upload/{$problem['id']}/problem.conf";
mkdir("/var/svn/problem/{$problem['id']}/cur/{$problem['id']}/1/");
}
$set_filename="/var/svn/problem/{$problem['id']}/cur/{$problem['id']}/1/problem.conf";
$has_legacy=false; $has_legacy=false;
if(file_exists($set_filename)){ if(file_exists($set_filename)){
$has_legacy=true; $has_legacy=true;
@ -135,7 +111,6 @@ EOD;
fwrite($setfile, "time_limit ".$_POST['time_limit']."\n"); fwrite($setfile, "time_limit ".$_POST['time_limit']."\n");
fwrite($setfile, "memory_limit ".$_POST['memory_limit']."\n"); fwrite($setfile, "memory_limit ".$_POST['memory_limit']."\n");
fclose($setfile); fclose($setfile);
svnCommitZipData($problem['id'], 'conf');
if(!$has_legacy){ if(!$has_legacy){
echo "<script>alert('添加成功!')</script>"; echo "<script>alert('添加成功!')</script>";
}else{ }else{
@ -153,61 +128,16 @@ EOD;
$download_url = HTML::url("/download.php?type=problem&id={$problem['id']}"); $download_url = HTML::url("/download.php?type=problem&id={$problem['id']}");
$info_form->appendHTML(<<<EOD $info_form->appendHTML(<<<EOD
<div class="form-group"> <div class="form-group">
<label class="col-sm-3 control-label">SVN地址</label>
<div class="col-sm-9">
<div class="form-control-static">
<button id="button-getsvn" type="button" class="btn btn-info btn-xs pull-right">查看/发送SVN密码</button>
<a>svn://{$http_host}/problem/{$problem['id']}</a>
</div>
</div>
<!--<label class="col-sm-3 control-label">zip上传数据</label> <!--<label class="col-sm-3 control-label">zip上传数据</label>
<div class="col-sm-9"> <div class="col-sm-9">
<div class="form-control-static"> <div class="form-control-static">
<row> <row>
<button type="button" style="width:30%" class="btn btn-primary" data-toggle="modal" data-target="#UploadDataModal">上传数据</button> <button type="button" style="width:30%" class="btn btn-primary" data-toggle="modal" data-target="#UploadDataModal">上传数据</button>
<button type="submit" style="width:30%" id="button-submit-data" name="submit-data" value="data" class="btn btn-danger">与SVN仓库同步</button> <button type="submit" style="width:30%" id="button-submit-data" name="submit-data" value="data" class="btn btn-danger">检验配置并同步数据</button>
</row> </row>
</div> </div>
</div>--> </div>-->
</div> </div>
<script type="text/javascript">
$('#button-getsvn').click(function(){
if (!confirm("您的SVN密码是{$myUser['svn_password']}。是否要发送SVN密码到${myUser['email']}")) {
return;
}
$.post('${_SERVER['REQUEST_URI']}', {
getsvn : ''
}, function(res) {
if (res == "good") {
BootstrapDialog.show({
title : "操作成功",
message : "SVN密码已经发送至您的邮箱请查收。",
type : BootstrapDialog.TYPE_SUCCESS,
buttons: [{
label: '好的',
action: function(dialog) {
dialog.close();
}
}],
});
} else {
BootstrapDialog.show({
title : "操作失败",
message : "邮件未发送成功",
type : BootstrapDialog.TYPE_DANGER,
buttons: [{
label: '好吧',
action: function(dialog) {
dialog.close();
}
}],
});
}
});
});
</script>
EOD EOD
); );
$info_form->appendHTML(<<<EOD $info_form->appendHTML(<<<EOD
@ -527,7 +457,7 @@ EOD
global $problem; global $problem;
$problem['hackable'] = !$problem['hackable']; $problem['hackable'] = !$problem['hackable'];
//$problem['hackable'] = 0; //$problem['hackable'] = 0;
$ret = svnSyncProblemData($problem); $ret = dataSyncProblemData($problem);
if ($ret) { if ($ret) {
becomeMsgPage('<div>' . $ret . '</div><a href="/problem/'.$problem['id'].'/manage/data">返回</a>'); becomeMsgPage('<div>' . $ret . '</div><a href="/problem/'.$problem['id'].'/manage/data">返回</a>');
} }
@ -543,19 +473,19 @@ EOD
$data_form->handle = function() { $data_form->handle = function() {
global $problem, $myUser; global $problem, $myUser;
set_time_limit(60 * 5); set_time_limit(60 * 5);
$ret = svnSyncProblemData($problem, $myUser); $ret = dataSyncProblemData($problem, $myUser);
if ($ret) { if ($ret) {
becomeMsgPage('<div>' . $ret . '</div><a href="/problem/'.$problem['id'].'/manage/data">返回</a>'); becomeMsgPage('<div>' . $ret . '</div><a href="/problem/'.$problem['id'].'/manage/data">返回</a>');
} }
}; };
$data_form->submit_button_config['class_str'] = 'btn btn-danger btn-block'; $data_form->submit_button_config['class_str'] = 'btn btn-danger btn-block';
$data_form->submit_button_config['text'] = '与SVN仓库同步'; $data_form->submit_button_config['text'] = '检验配置并同步数据';
$data_form->submit_button_config['smart_confirm'] = ''; $data_form->submit_button_config['smart_confirm'] = '';
$clear_data_form = new UOJForm('clear_data'); $clear_data_form = new UOJForm('clear_data');
$clear_data_form->handle = function() { $clear_data_form->handle = function() {
global $problem; global $problem;
svnClearProblemData($problem); dataClearProblemData($problem);
}; };
$clear_data_form->submit_button_config['class_str'] = 'btn btn-danger btn-block'; $clear_data_form->submit_button_config['class_str'] = 'btn btn-danger btn-block';
$clear_data_form->submit_button_config['text'] = '清空题目数据'; $clear_data_form->submit_button_config['text'] = '清空题目数据';

View File

@ -1,6 +1,5 @@
<?php <?php
requirePHPLib('form'); requirePHPLib('form');
requirePHPLib('svn');
if (!validateUInt($_GET['id']) || !($problem = queryProblemBrief($_GET['id']))) { if (!validateUInt($_GET['id']) || !($problem = queryProblemBrief($_GET['id']))) {
become404Page(); become404Page();
@ -23,10 +22,6 @@
} else if ($type == '-') { } else if ($type == '-') {
DB::query("delete from problems_permissions where problem_id = ${problem['id']} and username = '$username'"); DB::query("delete from problems_permissions where problem_id = ${problem['id']} and username = '$username'");
} }
},
function() {
global $problem;
svnRefreshPasswordOfProblem($problem['id']);
} }
); );

View File

@ -1,7 +1,7 @@
<?php <?php
requirePHPLib('form'); requirePHPLib('form');
requirePHPLib('judger'); requirePHPLib('judger');
requirePHPLib('svn'); requirePHPLib('data');
if (isSuperUser($myUser)) { if (isSuperUser($myUser)) {
$new_problem_form = new UOJForm('new_problem'); $new_problem_form = new UOJForm('new_problem');
@ -9,7 +9,7 @@
DB::query("insert into problems (title, is_hidden, submission_requirement) values ('New Problem', 1, '{}')"); DB::query("insert into problems (title, is_hidden, submission_requirement) values ('New Problem', 1, '{}')");
$id = DB::insert_id(); $id = DB::insert_id();
DB::query("insert into problems_contents (id, statement, statement_md) values ($id, '', '')"); DB::query("insert into problems_contents (id, statement, statement_md) values ($id, '', '')");
svnNewProblem($id); dataNewProblem($id);
}; };
$new_problem_form->submit_button_config['align'] = 'right'; $new_problem_form->submit_button_config['align'] = 'right';
$new_problem_form->submit_button_config['class_str'] = 'btn btn-primary'; $new_problem_form->submit_button_config['class_str'] = 'btn btn-primary';

View File

@ -2,21 +2,12 @@
// Actually, these things should be done by main_judger so that the code would be much simpler. // Actually, these things should be done by main_judger so that the code would be much simpler.
// However, this lib exists due to some history issues. // However, this lib exists due to some history issues.
function svnNewProblem($id) { function dataNewProblem($id) {
exec("/var/svn/problem/new_problem.sh $id"); mkdir("/var/uoj_data/upload/$id");
svnRefreshPasswordOfProblem($id); mkdir("/var/uoj_data/$id");
exec("cd /var/uoj_data; rm $id.zip; zip $id.zip $id -r -q"); exec("cd /var/uoj_data; rm $id.zip; zip $id.zip $id -r -q");
} }
function svnRefreshPasswordOfProblem($id) {
$result = DB::query("select user_info.username, svn_password from problems_permissions, user_info where problem_id = $id and user_info.username = problems_permissions.username");
$content = "[users]\n";
$content .= UOJConfig::$data['svn']['our-root']['username']." = ".UOJConfig::$data['svn']['our-root']['password']."\n";
while ($row = DB::fetch($result, MYSQLI_NUM)) {
$content .= "${row[0]} = ${row[1]}\n";
}
file_put_contents("/var/svn/problem/$id/conf/passwd", $content);
}
class UOJProblemConfException extends Exception { class UOJProblemConfException extends Exception {
public function __construct($message) { public function __construct($message) {
@ -29,21 +20,21 @@
} }
} }
function svnClearProblemData($problem) { function dataClearProblemData($problem) {
$id = $problem['id']; $id = $problem['id'];
if (!validateUInt($id)) { if (!validateUInt($id)) {
error_log("svnClearProblemData: hacker detected"); error_log("dataClearProblemData: hacker detected");
return "invalid problem id"; return "invalid problem id";
} }
exec("rm /var/svn/problem/$id -r"); exec("rm /var/uoj_data/upload/$id -r");
exec("rm /var/uoj_data/$id -r"); exec("rm /var/uoj_data/$id -r");
svnNewProblem($id); dataNewProblem($id);
} }
class SvnSyncProblemDataHandler { class SyncProblemDataHandler {
private $problem, $user; private $problem, $user;
private $svn_data_dir, $data_dir, $prepare_dir; private $upload_dir, $data_dir, $prepare_dir;
private $requirement, $problem_extra_config; private $requirement, $problem_extra_config;
private $problem_conf, $final_problem_conf; private $problem_conf, $final_problem_conf;
private $allow_files; private $allow_files;
@ -62,9 +53,9 @@
if (!isset($this->allow_files[$file_name])) { if (!isset($this->allow_files[$file_name])) {
throw new UOJFileNotFoundException($file_name); throw new UOJFileNotFoundException($file_name);
} }
$src = escapeshellarg("{$this->svn_data_dir}/$file_name"); $src = escapeshellarg("{$this->upload_dir}/$file_name");
$dest = escapeshellarg("{$this->prepare_dir}/$file_name"); $dest = escapeshellarg("{$this->prepare_dir}/$file_name");
if (isset($this->problem_extra_config['dont_use_formatter']) || !is_file("{$this->svn_data_dir}/$file_name")) { if (isset($this->problem_extra_config['dont_use_formatter']) || !is_file("{$this->upload_dir}/$file_name")) {
exec("cp $src $dest -r", $output, $ret); exec("cp $src $dest -r", $output, $ret);
} else { } else {
exec("$uojMainJudgerWorkPath/run/formatter <$src >$dest", $output, $ret); exec("$uojMainJudgerWorkPath/run/formatter <$src >$dest", $output, $ret);
@ -75,7 +66,7 @@
} }
private function copy_file_to_prepare($file_name) { private function copy_file_to_prepare($file_name) {
global $uojMainJudgerWorkPath; global $uojMainJudgerWorkPath;
if (!isset($this->allow_files[$file_name]) || !is_file("{$this->svn_data_dir}/$file_name")) { if (!isset($this->allow_files[$file_name]) || !is_file("{$this->upload_dir}/$file_name")) {
throw new UOJFileNotFoundException($file_name); throw new UOJFileNotFoundException($file_name);
} }
$this->copy_to_prepare($file_name); $this->copy_to_prepare($file_name);
@ -158,11 +149,11 @@
public function handle() { public function handle() {
$id = $this->problem['id']; $id = $this->problem['id'];
if (!validateUInt($id)) { if (!validateUInt($id)) {
error_log("svnSyncProblemData: hacker detected"); error_log("dataSyncProblemData: hacker detected");
return "invalid problem id"; return "invalid problem id";
} }
$this->svn_data_dir = "/var/svn/problem/$id/cur/$id/1"; $this->upload_dir = "/var/uoj_data/upload/$id";
$this->data_dir = "/var/uoj_data/$id"; $this->data_dir = "/var/uoj_data/$id";
$this->prepare_dir = "/var/uoj_data/prepare_$id"; $this->prepare_dir = "/var/uoj_data/prepare_$id";
@ -175,11 +166,11 @@
$this->problem_extra_config = json_decode($this->problem['extra_config'], true); $this->problem_extra_config = json_decode($this->problem['extra_config'], true);
mkdir($this->prepare_dir, 0755); mkdir($this->prepare_dir, 0755);
if (!is_file("{$this->svn_data_dir}/problem.conf")) { if (!is_file("{$this->upload_dir}/problem.conf")) {
throw new UOJFileNotFoundException("problem.conf"); throw new UOJFileNotFoundException("problem.conf");
} }
$this->problem_conf = getUOJConf("{$this->svn_data_dir}/problem.conf"); $this->problem_conf = getUOJConf("{$this->upload_dir}/problem.conf");
$this->final_problem_conf = $this->problem_conf; $this->final_problem_conf = $this->problem_conf;
if ($this->problem_conf === -1) { if ($this->problem_conf === -1) {
throw new UOJFileNotFoundException("problem.conf"); throw new UOJFileNotFoundException("problem.conf");
@ -187,14 +178,14 @@
throw new UOJProblemConfException("syntax error"); throw new UOJProblemConfException("syntax error");
} }
$this->allow_files = array_flip(array_filter(scandir($this->svn_data_dir), function($x){return $x !== '.' && $x !== '..';})); $this->allow_files = array_flip(array_filter(scandir($this->upload_dir), function($x){return $x !== '.' && $x !== '..';}));
$zip_file = new ZipArchive(); $zip_file = new ZipArchive();
if ($zip_file->open("{$this->prepare_dir}/download.zip", ZipArchive::CREATE) !== true) { if ($zip_file->open("{$this->prepare_dir}/download.zip", ZipArchive::CREATE) !== true) {
throw new Exception("<strong>download.zip</strong> : failed to create the zip file"); throw new Exception("<strong>download.zip</strong> : failed to create the zip file");
} }
if (isset($this->allow_files['require']) && is_dir("{$this->svn_data_dir}/require")) { if (isset($this->allow_files['require']) && is_dir("{$this->upload_dir}/require")) {
$this->copy_to_prepare('require'); $this->copy_to_prepare('require');
} }
@ -307,10 +298,10 @@
} }
putUOJConf("{$this->prepare_dir}/problem.conf", $this->final_problem_conf); putUOJConf("{$this->prepare_dir}/problem.conf", $this->final_problem_conf);
if (isset($this->allow_files['download']) && is_dir("{$this->svn_data_dir}/download")) { if (isset($this->allow_files['download']) && is_dir("{$this->upload_dir}/download")) {
foreach (scandir("{$this->svn_data_dir}/download") as $file_name) { foreach (scandir("{$this->upload_dir}/download") as $file_name) {
if (is_file("{$this->svn_data_dir}/download/{$file_name}")) { if (is_file("{$this->upload_dir}/download/{$file_name}")) {
$zip_file->addFile("{$this->svn_data_dir}/download/{$file_name}", $file_name); $zip_file->addFile("{$this->upload_dir}/download/{$file_name}", $file_name);
} }
} }
} }
@ -336,18 +327,15 @@
} }
} }
function svnSyncProblemData($problem, $user = null) { function dataSyncProblemData($problem, $user = null) {
return (new SvnSyncProblemDataHandler($problem, $user))->handle(); return (new SyncProblemDataHandler($problem, $user))->handle();
} }
function svnAddExtraTest($problem, $input_file_name, $output_file_name) { function dataAddExtraTest($problem, $input_file_name, $output_file_name) {
$id = $problem['id']; $id = $problem['id'];
$svnusr = UOJConfig::$data['svn']['our-root']['username']; $cur_dir = "/var/uoj_data/upload/$id";
$svnpwd = UOJConfig::$data['svn']['our-root']['password'];
$cur_dir = "/var/svn/problem/$id/cur/$id"; $problem_conf = getUOJConf("{$cur_dir}/problem.conf");
$problem_conf = getUOJConf("{$cur_dir}/1/problem.conf");
if ($problem_conf == -1 || $problem_conf == -2) { if ($problem_conf == -1 || $problem_conf == -2) {
return $problem_conf; return $problem_conf;
} }
@ -356,57 +344,14 @@
$new_input_name = getUOJProblemExtraInputFileName($problem_conf, $problem_conf['n_ex_tests']); $new_input_name = getUOJProblemExtraInputFileName($problem_conf, $problem_conf['n_ex_tests']);
$new_output_name = getUOJProblemExtraOutputFileName($problem_conf, $problem_conf['n_ex_tests']); $new_output_name = getUOJProblemExtraOutputFileName($problem_conf, $problem_conf['n_ex_tests']);
putUOJConf("$cur_dir/1/problem.conf", $problem_conf); putUOJConf("$cur_dir/problem.conf", $problem_conf);
move_uploaded_file($input_file_name, "$cur_dir/1/$new_input_name"); move_uploaded_file($input_file_name, "$cur_dir/$new_input_name");
move_uploaded_file($output_file_name, "$cur_dir/1/$new_output_name"); move_uploaded_file($output_file_name, "$cur_dir/$new_output_name");
exec( if (dataSyncProblemData($problem) === '') {
<<<EOD
cd $cur_dir
svn add 1/$new_input_name --username $svnusr --password $svnpwd
svn add 1/$new_output_name --username $svnusr --password $svnpwd
svn commit -m "add new extra test." --username $svnusr --password $svnpwd
EOD
);
if (svnSyncProblemData($problem) === '') {
rejudgeProblemAC($problem); rejudgeProblemAC($problem);
} else { } else {
error_log('hack successfully but sync failed.'); error_log('hack successfully but sync failed.');
} }
} }
function svnCommitZipData($problem, $type) {
$id = $problem['id'];
$cur_dir = "/var/svn/problem/$id/cur/$id";
$svnusr = UOJConfig::$data['svn']['our-root']['username'];
$svnpwd = UOJConfig::$data['svn']['our-root']['password'];
if($type=='conf'){
exec(
<<<EOD
cd $cur_dir
svn add 1/problem.conf --username $svnusr --password $svnpwd
svn commit -m "update problem.conf by online conf editor." --username $svnusr --password $svnpwd
EOD
);
}
else if($type=='data'){
exec(
<<<EOD
cd $cur_dir/1
for sub_dir in `find -maxdepth 1 -type d ! -name .`; do
mv -f \$sub_dir/* . && rm -rf \$sub_dir
done
cd $cur_dir
svn add * --username $svnusr --password $svnpwd
svn commit -m "add testdata from zip file online." --username $svnusr --password $svnpwd
EOD
);
}
else{
error_log("svnCommitZipData: invalid argument");
return "invalid argument";
}
}
?> ?>