Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
159 lines (118 loc) · 4.8 KB
/
Copy pathmain.cpp
File metadata and controls
159 lines (118 loc) · 4.8 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
// License: GPLv3 or later
// Author: Sasha
#include "functions.h"
#include "cBuild.h"
#include <string>
#include <map>
#include <pwd.h>
int main(int argc, char **argv){
if (getuid()!=0){
std::cout << "run only root" << std::endl;
return 1;
}
MYSQL_RES *result;
MYSQL_ROW row;
MYSQL *connection, mysql;
// Защита от двойного запуска
int fd = -1;
if ((fd = open(PATHLOCK, O_RDWR| O_CREAT, S_IRUSR| S_IWUSR)) == -1) {
int myerr = errno;
printf("ERROR: open errno(%d): %s\n", errno,strerror(myerr));
return EXIT_FAILURE;
}
if (LockFile(fd)==EXIT_FAILURE){
std::cout << "ERROR: is already running" << std::endl;
return 1;
}
// Читаем в map параметры командной строки
map<string,string> cmdParam = loadArgv(argc,argv);
if (cmdParam["--daemon"]=="true") run_as_daemon();
map<string,string> conf = loadConfig();
//string pathAum=conf["paths/pathAum"];
string pathUserHome=conf["paths/pathUserHome"];
//string pathPbs=conf["paths/pathPbs"];
string pathTemp=conf["paths/pathTemp"];
string cmdLine="";
string sqlQuery="";
int buildError=0; // Состояние ошибки
mysql_init(&mysql);
connection = mysql_real_connect(&mysql,conf["mysql/hostname"].c_str(), conf["mysql/username"].c_str(), conf["mysql/password"].c_str(), conf["mysql/database"].c_str(), 0, NULL,0);
if( connection == NULL ) {
printf("%s\n",mysql_error(&mysql));
return 1;
}
srand (time(NULL));
// Тут будет основной цикл
while(1){
int state = mysql_query(connection,
"SELECT t1.id,t1.file_id, t2.filename, t3.user, t4.os, t4.arch "
"FROM jobs as t1, tsrc t2, users as t3, platforms as t4 "
"WHERE t1.status=0 and t2.id=t1.file_id and t3.id=t2.user_id and t4.id=t1.platform_id ");
if( state != 0 ) {
printf("%s\n",mysql_error(connection));
break;
}
result = mysql_store_result(connection);
while( ( row = mysql_fetch_row(result)) != NULL ) {
printf("%s, %s\n", row[0],row[1]);
//
sqlQuery="UPDATE jobs SET status='1' WHERE id='"+string(row[0])+"'";
mysql_query(connection,sqlQuery.c_str());
string dbNSrcFile=string(row[1]);
string dbOsName=string(row[4]);
string dbOsArch=string(row[5]);
string username=string(row[3]);
string jobFilename=string(row[2]);
string aumOsKey=dbOsName+"_"+dbOsArch;
string aumLogFile=pathUserHome+"/"+username+"/logs/"+dbNSrcFile+"/"+aumOsKey+".txt";
string aumUploadDir=pathTemp+"/files/"+dbNSrcFile;
string aumFileName=pathUserHome+"/"+username+"/srpms/"+dbNSrcFile+"/"+jobFilename;
std::cout << " " << dbNSrcFile << " " << dbOsName << " " << dbOsArch << " " << username << " " << jobFilename << " " << aumOsKey << std::endl;
string aumChroot=conf[aumOsKey+"/chroot"];
string aumDistrib=conf[aumOsKey+"/distrib"];
string aumPathTemp=pathTemp+"/pbs-chroot-"+dbNSrcFile+"-"+aumOsKey;
//Создание временной папки
pbsMkdir(aumUploadDir);
pbsMkdir(aumPathTemp);
map<string,string> options;
// Формирование строки
//cmdLine=pathAum+" ";
options["chroot"]=aumChroot;
options["distrib"]=aumDistrib;
options["log-file"]=aumLogFile;
options["upload-dir"]=aumUploadDir;
options["tmp-dir"]=aumPathTemp;
if (boost::filesystem::exists(pathUserHome+"/"+username+"/.rpmmacros")==true){
options["rpmmacros"]=pathUserHome+"/"+username+"/.rpmmacros";
}
options["srpm"]=aumFileName;
// Запуск сборки
//buildError=system(cmdLine.c_str());
cBuild bb;
buildError=bb.build(options);
if (buildError!=0){
string sqlQuery="UPDATE jobs SET status='11' WHERE id='"+string(row[0])+"'";
mysql_query(connection,sqlQuery.c_str());
continue;
}
string targetOs = pathUserHome+"/"+username+"/repo/"+dbOsName;
int countFile=copyRpmToRepo(aumUploadDir,targetOs);
if (countFile==0){
string sqlQuery="UPDATE jobs SET status='11' WHERE id='"+string(row[0])+"'";
mysql_query(connection,sqlQuery.c_str());
continue;
}
// Удаление временной папки
boost::filesystem::remove_all(aumUploadDir);
// Пересоздание репозитория
exec_cmd("pbs-hdlistgen "+username+ " "+dbOsName);
sqlQuery="UPDATE jobs SET status='10' WHERE id='"+string(row[0])+"'";
mysql_query(connection,sqlQuery.c_str());
std::cout << "Build end" << std::endl;
}
mysql_free_result(result);
sleep(1);
}
mysql_close(connection);
return 0;
}