-
Notifications
You must be signed in to change notification settings - Fork 10
/
zbox.php
188 lines (174 loc) · 6.44 KB
/
zbox.php
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
#!/opt/zbox/bin/php
<?php
array_shift($argv);
$flipArgv = array_flip($argv);
$basePath = dirname(dirname(__FILE__));
if($basePath != '/opt/zbox') die("Run it in path /opt/zbox/\n");
if(empty($argv) or isset($flipArgv['--help']) or isset($flipArgv['-h']))
{
echo <<<EOD
Usage: zbox.php {start|stop|restart|status}
Options:
-h --help Show help.
-ap --aport Apache port, default 80.
-mp --mport Mysql port, default 3306.
EOD;
exit;
}
if(is_dir("$basePath/app/zentao/"))
{
`chmod -R 777 $basePath/app/zentao/tmp`;
`chmod -R 777 $basePath/app/zentao/www/data`;
`chmod 777 $basePath/app/zentao/www/`;
`chmod 777 $basePath/app/zentao/config/`;
`chmod -R a+rx $basePath/app/zentao/bin/*`;
}
if(is_dir("$basePath/app/zentaopro/"))
{
`chmod -R 777 $basePath/app/zentaopro/tmp`;
`chmod -R 777 $basePath/app/zentaopro/www/data`;
`chmod 777 $basePath/app/zentaopro/www/`;
`chmod 777 $basePath/app/zentaopro/config/`;
`chmod -R a+rx $basePath/app/zentaopro/bin/*`;
}
if(is_dir("$basePath/app/zentaoep/"))
{
`chmod -R 777 $basePath/app/zentaoep/tmp`;
`chmod -R 777 $basePath/app/zentaoep/www/data`;
`chmod 777 $basePath/app/zentaoep/www/`;
`chmod 777 $basePath/app/zentaoep/config/`;
`chmod -R a+rx $basePath/app/zentaoep/bin/*`;
}
/* Process argv. */
$params = array();
foreach($flipArgv as $key => $val)
{
if(strpos($key, '-') !== 0) continue;
if($key == '--aport') $key = '-ap';
if($key == '--mport') $key = '-mp';
if(isset($argv[$val + 1]) and is_numeric($argv[$val + 1]))
{
$params[$key] = $argv[$val + 1];
unset($argv[$val]);
unset($argv[$val + 1]);
}
}
if(isset($params['-ap'])) changePort($basePath . '/etc/apache/httpd.conf', $params['-ap'], array('^Listen +([0-9]+)', '<VirtualHost +.*:([0-9]+)>'));
if(isset($params['-mp']))
{
changePort($basePath . '/etc/mysql/my.cnf', $params['-mp'], '^port *= *([0-9]+)');
changePort($basePath . '/app/htdocs/index.php', $params['-mp'], 'localhost\:([0-9]+)\&');
$myReg = '^\$config->db->port *= *.([0-9]+)..*;';
if(file_exists("$basePath/app/zentao/config/my.php"))
{
`chmod 777 $basePath/app/zentao/config/my.php`;
$myFile = "$basePath/app/zentao/config/my.php";
changePort($myFile, $params['-mp'], $myReg);
}
if(file_exists("$basePath/app/zentaopro/config/my.php"))
{
`chmod 777 $basePath/app/zentaopro/config/my.php`;
$myFile = "$basePath/app/zentaopro/config/my.php";
changePort($myFile, $params['-mp'], $myReg);
}
if(file_exists("$basePath/app/zentaoep/config/my.php"))
{
`chmod 777 $basePath/app/zentaoep/config/my.php`;
$myFile = "$basePath/app/zentaoep/config/my.php";
changePort($myFile, $params['-mp'], $myReg);
}
}
if(!empty($argv)) $params['-k'] = reset($argv);
if(isset($params['-k']))
{
if(strpos(file_get_contents('/etc/group'), 'nogroup') === false) echo `groupadd nogroup`;
if(strpos(file_get_contents('/etc/passwd'), 'nobody') === false) echo `useradd nobody`;
`chmod -R 777 $basePath/tmp`;
`chmod -R 777 $basePath/logs`;
`chown -R nobody $basePath/data/mysql`;
switch($params['-k'])
{
case 'start':
$httpd = `ps aux|grep '\/opt\/zbox\/run\/apache\/httpd '`;
if($httpd)
{
echo "Apache is running\n";
}
else
{
echo `$basePath/run/apache/apachectl start`;
sleep(2);
$httpd = `ps aux|grep '\/opt\/zbox\/run\/apache\/httpd '`;
echo empty($httpd) ? "Start Apache fail. You can see the log /opt/zbox/logs/apache_error.log\n" : "Start Apache success\n";
}
$mysql = `ps aux|grep '\/opt\/zbox\/run\/mysql\/mariadbd '`;
if($mysql)
{
echo "Mysql is running\n";
}
else
{
echo `$basePath/run/mysql/mysql.server start --defaults-file=$basePath/etc/mysql/my.cnf`;
sleep(2);
$mysql = `ps aux|grep '\/opt\/zbox\/run\/mysql\/mariadbd '`;
echo empty($mysql) ? "Start Mysql fail. You can see the log /opt/zbox/logs/mysql_error.log\n" : "Start Mysql success\n";
}
break;
case 'stop':
$httpd = `ps aux|grep '\/opt\/zbox\/run\/apache\/httpd '`;
if($httpd)
{
echo `$basePath/run/apache/apachectl stop`;
sleep(2);
$httpd = `ps aux|grep '\/opt\/zbox\/run\/apache\/httpd '`;
echo empty($httpd) ? "Stop Apache success\n" : "Stop Apache fail. You can see the log /opt/zbox/logs/apache_error.log\n";
}
else
{
echo "Apache is not running\n";
}
$mysql = `ps aux|grep '\/opt\/zbox\/run\/mysql\/mariadbd '`;
if($mysql)
{
echo `$basePath/run/mysql/mysql.server stop`;
sleep(2);
$mysql = `ps aux|grep '\/opt\/zbox\/run\/mysql\/mariadbd '`;
echo empty($mysql) ? "Stop Mysql success\n" : "Stop Mysql fail. You can see the log /opt/zbox/logs/mysql_error.log\n";
}
else
{
echo "Mysql is not running\n";
}
break;
case 'restart':
echo `$basePath/run/apache/apachectl restart`;
sleep(2);
$httpd = `ps aux|grep '\/opt\/zbox\/run\/apache\/httpd '`;
echo empty($httpd) ? "Restart Apache fail. You can see the log /opt/zbox/logs/apache_error.log\n" : "Retart Apache success\n";
echo `$basePath/run/mysql/mysql.server stop`;
sleep(2);
echo `$basePath/run/mysql/mysql.server start --defaults-file=$basePath/etc/mysql/my.cnf`;
sleep(2);
$mysql = `ps aux|grep '\/opt\/zbox\/run\/mysql\/mariadbd '`;
echo empty($mysql) ? "Restart Mysql fail. You can see the log /opt/zbox/logs/mysql_error.log\n" : "Retart Mysql success\n";
break;
case 'status':
$httpd = `ps aux|grep '\/opt\/zbox\/run\/apache\/httpd '`;
$mysql = `ps aux|grep '\/opt\/zbox\/run\/mysql\/mariadbd '`;
echo empty($httpd) ? "Apache is not running\n" : "Apache is running\n";
echo empty($mysql) ? "Mysql is not running\n" : "Mysql is running\n";
}
}
function changePort($file, $port, $regs)
{
if(!is_array($regs)) $regs = array($regs);
$lines = file($file);
foreach($lines as $i => $line)
{
foreach($regs as $reg)
{
if(preg_match("/$reg/", $line, $matches)) $lines[$i] = str_replace($matches[1], $port, $line);
}
}
file_put_contents($file, join($lines));
}