-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjudgemgr.cpp
More file actions
297 lines (276 loc) · 14 KB
/
Copy pathjudgemgr.cpp
File metadata and controls
297 lines (276 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include<bits/stdc++.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/resource.h>
#include<sys/wait.h>
#include<mysql/mysql.h>
#include<jsoncpp/json/json.h>
#include"mysqld.h"
using namespace std;
string system2(const char* cmd) {
FILE *stream,*wstream;
char buf[1024*1024];
memset(buf,'\0',sizeof(buf));
stream=popen(("sudo "+string(cmd)+" 2>&1").c_str(),"r");
int k=fread(buf,sizeof(char),sizeof(buf),stream);
pclose(stream);return buf;
}
bool checkCommandExist(const char* cmd) {
string ret=system2(("sudo command -v "+string(cmd)).c_str());
return ret!="";
}
bool checkHeaderExist(const char* header,const char* cmd) {
string output_code="#include<"+string(header)+">\nint main(){}";
ofstream fout("/tmp/main.cpp");
fout<<output_code;fout.close();
string ret=system2(("g++ /tmp/main.cpp -o /tmp/main "+string(cmd)).c_str());
unlink("/tmp/main.cpp");if (ret=="") unlink("/tmp/main");
return ret=="";
}
bool exec(string hint,string cmd) {
cout<<" * "<<hint<<endl;
int tmp=system(("sudo "+cmd).c_str());
sleep(1);return tmp;
}
int main(int argc,char * argv[]) {
if (argc<=1) {
cout<<"judgemgr: missing operand"<<endl;
cout<<"Try 'judgemgr help' for more information."<<endl;
return 0;
} string cmd=string(argv[1]);
char tmp[1024]="";char* kkkk=getcwd(tmp,1024);string path=tmp;
if (cmd=="build") {
string mysqladdr,mysqlname,mysqlpw,mysqldb;int mysqlport;
cout<<endl<<"Collecting Datas..."<<endl;
cout<<" * Login to the MySQL/MariaDB Server: "<<endl;
cout<<"address: ";cin>>mysqladdr;
cout<<"port: ";cin>>mysqlport;
cout<<"username: ";cin>>mysqlname;
cout<<"password: ";cin>>mysqlpw;
cout<<"database: ";cin>>mysqldb;
cout<<" * Connecting...";
mysqld conn=mysqli_connect(mysqladdr.c_str(),mysqlname.c_str(),
mysqlpw.c_str(),mysqldb.c_str(),mysqlport);
if (!conn) {
cout<<endl<<"Failed to Connect MySQL/MariaDB Server!"<<endl;
cout<<mysqli_error(conn)<<endl;
return 0;
} cout<<" Success!"<<endl;
cout<<endl<<"Clearing..."<<endl;
// exec("Deleting Backup Main Directory... (/etc/judge.back/)","rm /etc/judge.back -r");
// exec("Deleting Backup Log Directory... (/var/log/judge.back/)","rm /var/log/judge.back -r");
// exec("Backup Exist Main Directory... (/etc/judge/ -> /etc/judge.back/)","mv /etc/judge /etc/judge.back");
// exec("Backup Log Directory... (/var/log/judge/ -> /var/log/judge.back)","mv /var/log/judge /var/log/judge.back");
// exec("Backup MySQL/MariaDB Data... ("+mysqldb+" > ./"+mysqldb+".sql)",
// "mysqldump -h"+mysqladdr+" --port="+to_string(mysqlport)+" -u"+mysqlname+" -p"+mysqlpw+" "+mysqldb+" > ./"+mysqldb+".sql");
exec("Deleting Exist Main Directory... (/etc/judge/)","rm /etc/judge -r");
exec("Deleting Log Directory... (/var/log/judge/)","rm /var/log/judge -r");
exec("Deleting MySQL/MariaDB Data...","");
mysqld row,result=mysqli_query(conn,("select concat('drop table ',table_name,';')"+
(string)"from information_schema.TABLES where table_schema='"+mysqldb+"'").c_str());
while (row=mysqli_fetch_assoc(result)) {
string exe=row["concat('drop table ',table_name,';')"];
mysqli_query(conn,exe.c_str());
}
cout<<endl<<"Solving..."<<endl;
exec("Initialize MySQL/MariaDB Database...",
"mysql -h"+mysqladdr+" --port="+to_string(mysqlport)+" -u"+mysqlname+" -p"+mysqlpw+" "+mysqldb+" -e'source ./init.sql'");
exec("Creating Log Directory... (/var/log/judge/)","mkdir /var/log/judge/");
exec("Creating Configure Directory... (/etc/judge/)","mkdir /etc/judge/");
exec("Creating Temporary Directory... (/etc/judge/tmp/)","mkdir /etc/judge/tmp");
exec("Creating Special Judge Template Directory... (/etc/judge/spjtemp/)","mkdir /etc/judge/spjtemp");
exec("Creating Problem Directory... (/etc/judge/problem/)","mkdir /etc/judge/problem");
exec("Creating Website Root Directory... (/etc/judge/web/)","mkdir /etc/judge/web");
exec("Copy config.json... (./config.json -> /etc/judge/config.json)","cp ./config.json /etc/judge/config.json");
exec("Copy Special Judge Template... (./spjtemp/* -> /etc/judge/spjtemp/)","cp ./spjtemp/* /etc/judge/spjtemp -r");
exec("Copy Website Files... (./web/* -> /etc/judge/web/)","cp ./web/* /etc/judge/web/ -r");
exec("Copy judge.cpp... (./judge.cpp -> /etc/judge/judge.cpp)","cp ./judge.cpp /etc/judge/judge.cpp");
exec("Copy mysqld.h... (./mysqld.h -> /etc/judge/mysqld.h)","cp ./mysqld.h /etc/judge/mysqld.h");
cout<<endl<<"Compiling..."<<endl;
exec("Compiling Special Judge...","g++ /etc/judge/spjtemp/1.cpp -O2 -std=c++14 -o /etc/judge/spjtemp/1");
exec("Compiling Main Judge Service...","g++ /etc/judge/judge.cpp -O2 --std=c++14 -g -o /usr/bin/judge -lmysqlclient -ljsoncpp -lpthread");
cout<<endl<<"Success!"<<endl;
return 0;
}
if (cmd=="start") {
cout<<" * Starting MySQL/MariaDB Database... ";cout<<flush;
string kkk=system2("service mysql start");sleep(1);
string tmp=system2("ps -ef|grep mysqld|grep -v grep");
if (tmp=="") {cout<<"Failed!"<<endl;return 0;}
else cout<<"Success!"<<endl;
cout<<" * Starting Judge Service... ";cout<<flush;
int ret=system("ulimit -s unlimited");
ret=system("sudo nohup judge >/dev/null 2>&1 &");sleep(1);
tmp=system2("ps -ef|grep judge|grep -v grep|grep -v judgemgr");
if (tmp=="") cout<<"Failed!"<<endl;
else cout<<"Success!"<<endl;
return 0;
}
if (cmd=="compile") {
exec("Compiling Special Judge...","g++ ./spjtemp/1.cpp -o /etc/judge/spjtemp/1");
exec("Compiling Main Judge Service...","g++ ./judge.cpp -O2 --std=c++14 -g -o /usr/bin/judge -lmysqlclient -ljsoncpp -lpthread");
cout<<endl<<"Success!"<<endl;
return 0;
}
if (cmd=="stop") {
cout<<" * Stoping Judge Service... ";cout<<flush;
string tmp=system2("killall judge");sleep(1);
tmp=system2("ps -ef|grep judge|grep -v grep|grep -v judgemgr");
if (tmp=="") cout<<"Success!"<<endl;
else cout<<"Failed!"<<endl;cout<<flush;
return 0;
}
if (cmd=="status") {
string tmp=system2("ps -ef|grep judge|grep -v grep|grep -v judgemgr");
if (tmp=="") cout<<" * Judge Service is not running!"<<endl;
else cout<<" * Judge Service is running!"<<endl;cout<<flush;
return 0;
}
if (cmd=="restart") {
cout<<" * Stoping Judge Service... ";cout<<flush;
string tmp=system2("killall judge");sleep(1);
tmp=system2("ps -ef|grep judge|grep -v grep|grep -v judgemgr");
if (tmp=="") cout<<"Success!"<<endl;
else {cout<<"Failed!"<<endl;return 0;}cout<<flush;
cout<<" * Starting Judge Service... ";cout<<flush;
int ret=system("sudo nohup judge >/dev/null 2>&1 &");sleep(1);
tmp=system2("ps -ef|grep judge|grep -v grep|grep -v judgemgr");
if (tmp=="") cout<<"Failed!"<<endl;
else cout<<"Success!"<<endl;
return 0;
}
if (cmd=="output") {
int tmp=system("sudo tail -f /var/log/judge/info.log");
return 0;
}
if (cmd=="check") {
cout<<"Applications: "<<endl;
cout<<" * Checking MySQL/MariaDB Database... ";
if (!checkCommandExist("mysql")) {
cout<<endl<<"We can not find your MySQL/MariaDB Database."<<endl;
cout<<"Please check whether you have installed it!"<<endl;
return 0;
} else cout<<"Found!"<<endl;
cout<<" * Checking Nginx Web Server... ";
if (!checkCommandExist("nginx")) {
cout<<endl<<"We can not find your Nginx Web Server."<<endl;
cout<<"Please check whether you have installed it!"<<endl;
return 0;
} else cout<<"Found!"<<endl;
cout<<" * Checking php-cli Interpreter... ";
if (!checkCommandExist("php")) {
cout<<endl<<"We can not find your php-cli Interpreter."<<endl;
cout<<"Please check whether you have installed it!"<<endl;
return 0;
} else cout<<"Found!"<<endl;
cout<<" * Checking Zip Compressor... ";
if (!checkCommandExist("zip")) {
cout<<endl<<"We can not find your Zip Compressor."<<endl;
cout<<"You may cannot use backup function!"<<endl;
} else cout<<"Found!"<<endl;
cout<<endl<<"Dependence:"<<endl;
cout<<" * Checking libmysqlclient-dev... ";
if (!checkHeaderExist("mysql/mysql.h","-lmysqlclient")) {
cout<<endl<<"We can not find libmysqlclient-dev."<<endl;
cout<<"Please check whether you have installed it!"<<endl;
return 0;
} else cout<<"Found!"<<endl;
cout<<" * Checking libjsoncpp-dev... ";
if (!checkHeaderExist("jsoncpp/json/json.h","-ljsoncpp")) {
cout<<endl<<"We can not find libjsoncpp-dev."<<endl;
cout<<"Please check whether you have installed it!"<<endl;
return 0;
} else cout<<"Found!"<<endl;
cout<<endl<<"Success!"<<endl;
return 0;
}
if (cmd=="backup") {
string mysqladdr,mysqlname,mysqlpw,mysqldb;int mysqlport;
cout<<"Collecting Datas..."<<endl;
cout<<" * Login to the MySQL/MariaDB Server: "<<endl;
cout<<"address: ";cin>>mysqladdr;
cout<<"port: ";cin>>mysqlport;
cout<<"username: ";cin>>mysqlname;
cout<<"password: ";cin>>mysqlpw;
cout<<"database: ";cin>>mysqldb;
cout<<" * Connecting...";
mysqld conn=mysqli_connect(mysqladdr.c_str(),mysqlname.c_str(),
mysqlpw.c_str(),mysqldb.c_str(),mysqlport);
if (!conn) {
cout<<endl<<"Failed to Connect MySQL/MariaDB Server!"<<endl;
cout<<mysqli_error(conn)<<endl;
return 0;
} cout<<" Success!"<<endl;
cout<<endl<<"Backuping..."<<endl;
exec("Creating Temporary Directory... (/tmp/judge/)","mkdir /tmp/judge");
exec("Copying Main Directory... (/etc/judge/* -> /tmp/judge/)","cp /etc/judge/* /tmp/judge -r");
exec("Removing tmp Diretory... (/tmp/judge/tmp/)","rm /tmp/judge/tmp -r");
exec("Creating Log Directory... (/tmp/judge/log/)","mkdir /tmp/judge/log");
exec("Copying Log Directory... (/var/log/judge/* -> /tmp/judge/log/)","cp /var/log/judge/* /tmp/judge/log -r");
exec(("Backup MySQL/MariaDB Database... ("+mysqldb+" -> /tmp/judge/data.sql)").c_str(),
("mysqldump -h"+mysqladdr+" --port="+to_string(mysqlport)+" -u"+mysqlname+" -p"+mysqlpw+" "+mysqldb+" > /tmp/judge/data.sql").c_str());
int ret=chdir("/tmp/judge");
exec("Compressing... (/tmp/judge/* -> ./judge.zip)",("zip -q "+path+"/judge.zip ./* -r").c_str());
ret=chdir(path.c_str());
exec("Removing Temporary Files... (/tmp/judge/)","rm /tmp/judge -r");
cout<<"Success!"<<endl;
return 0;
}
if (cmd=="restore") {
string mysqladdr,mysqlname,mysqlpw,mysqldb;int mysqlport;
cout<<"Collecting Datas..."<<endl;
cout<<" * Login to the MySQL/MariaDB Server: "<<endl;
cout<<"address: ";cin>>mysqladdr;
cout<<"port: ";cin>>mysqlport;
cout<<"username: ";cin>>mysqlname;
cout<<"password: ";cin>>mysqlpw;
cout<<"database: ";cin>>mysqldb;
cout<<" * Connecting...";
mysqld conn=mysqli_connect(mysqladdr.c_str(),mysqlname.c_str(),
mysqlpw.c_str(),mysqldb.c_str(),mysqlport);
if (!conn) {
cout<<endl<<"Failed to Connect MySQL/MariaDB Server!"<<endl;
cout<<mysqli_error(conn)<<endl;
return 0;
} cout<<" Success!"<<endl;
cout<<endl<<"Restoring..."<<endl;
exec("Extracting Compressed Files... (./judge.zip -> /etc/judge/)","unzip -q -o judge.zip -d /etc/judge/");
exec("Copying Log Directory... (/etc/judge/log/* -> /var/log/judge/)","cp /etc/judge/log/* /var/log/judge -r");
exec("Removing Log Directory... (/etc/judge/log/)","rm /etc/judge/log -r");
exec("Deleting MySQL/MariaDB Data...","");
mysqld row,result=mysqli_query(conn,("select concat('drop table ',table_name,';')"+
(string)"from information_schema.TABLES where table_schema='"+mysqldb+"'").c_str());
while (row=mysqli_fetch_assoc(result)) {
string exe=row["concat('drop table ',table_name,';')"];
mysqli_query(conn,exe.c_str());
}
exec(("Restoring MySQL/MariaDB Database... (/etc/judge/data.sql -> "+mysqldb+")").c_str(),
("mysql -h"+mysqladdr+" --port="+to_string(mysqlport)+" -u"+mysqlname+" -p"+mysqlpw+" "+mysqldb+" -e'source /etc/judge/data.sql'").c_str());
cout<<"Success!"<<endl;
return 0;
}
if (cmd=="help") {
cout<<"Usage: judgemgr <command>"<<endl;
cout<<endl;
cout<<"Online judger manager can help to manage background judge service."<<endl;
cout<<"It will be more convenience to build it by judgemgr."<<endl;
cout<<endl;
cout<<"Commands:"<<endl;
cout<<" build: Build the judge service."<<endl;
cout<<" compile: Re-compile all the necessary executable programs."<<endl;
cout<<" start: Start judge service."<<endl;
cout<<" stop: Stop judge service."<<endl;
cout<<" restart: Restart judge service."<<endl;
cout<<" check: Check necessary dependences."<<endl;
cout<<" status: View judge service status."<<endl;
cout<<" output: View output information of judge service."<<endl;
cout<<" backup: Backup your all data to a zip file(need library zip)"<<endl;
cout<<" restore: Restore your all data from a zip file(need library zip)"<<endl;
cout<<endl;
cout<<" This manager tool has Super Cow Powers."<<endl;
return 0;
}
cout<<"judgemgr: unknown command '"<<cmd<<"'"<<endl;
cout<<"Try 'judgemgr help' for more information."<<endl;
return 0;
}