-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtomcat-patcher.php
More file actions
456 lines (375 loc) · 12 KB
/
Copy pathtomcat-patcher.php
File metadata and controls
456 lines (375 loc) · 12 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/usr/bin/php
<?php
define('GOOD', 1);
define('TOMCAT_DOWN', 2);
define('TOMCAT_NOTEXIST', 3);
define('TOMCAT_NOSHUTDOWN', 4);
define('FILE_MISSING', 5);
define('IN_PROGRESS', 10);
$args = $_SERVER['argv'];
if (count($args) !== 4) {
print "Three parameters are required: token, patcher URL, and patch directory. \n";
exit(1);
}
// ESTABLISH VARIABLES
$immediate = null;
$now = time();
$output = array();
$token = $args[1];
$url = $args[2];
$patchdir = $args[3];
if (!is_dir($patchdir)) {
mkdir ($patchdir);
if (!is_dir($patchdir)) {
print "Could not create $patchdir \n";
exit(1);
}
}
// FIND ALL IPS THIS SERVER IS RESPONSIBLE FOR
$ips = array();
$ignore = exec("/sbin/ifconfig -a | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1'", $ips);
$context = stream_context_create(array('http'=> array('method' => "GET", 'header' => "X-Auth-Token: $token\r\n")));
// IGNORE NOT INTERNAL IPS
foreach ($ips AS $key => $val) {
$pieces = explode('.', $val);
if ((int)$pieces[0] !== 192 && (int)$pieces[0] != 10) {
unset($ips[$key]);
}
}
// DIE IF WE HAVE NO IPS
if (count($ips) == 0) {
print "No IPs found\n";
exit(1);
}
// CHECK IF WE HAVE ANY RELEVANT PATCHES
$str = file_get_contents ($url . "/json/patches?ips=" . json_encode (array_values($ips)), false, $context);
$patch = json_decode ($str);
// EXIT IF NO PATCHES
if (!$patch) {
exit(0);
}
// run the patch
patch($patch);
function patch($patch) {
global $url, $patchdir;
$hotdeploy = $patch->hotdeploy && $patch->hotdeploy > 0 ? true : false;
$test_file = $patch->tomcat_dir . '/conf/server.xml';
// DIE IF WE DONT HAVE ACCESS TO THE PATCH
if (!is_file($test_file)) {
update_patch($patch->patch_id, TOMCAT_NOTEXIST);
}
// FIND THE OWNER OF A CONF FILE
$sakai_uid = fileowner($test_file);
// Sometimes posix_getuid is not available so we don't get to do this check
$script_uid = function_exists('posix_getuid') ? posix_getuid() : $sakai_uid;
if (!$sakai_uid || !$script_uid) {
update_patch ($patch->patch_id, TOMCAT_NOTEXIST);
}
// ONLY RUN IF WE ARE RUNNING UNDER THE RIGHT USER!!!!!!!!!
if ($script_uid === 0 || $sakai_uid !== $script_uid) {
print "UID running the script does not match the owner of Tomcat dir\n";
exit(1);
}
// MARK THAT WE ARE IN PROGRESS SO PATCHES DONT OVERRUN EACH OTHER
update_patch($patch->patch_id, IN_PROGRESS);
// SEE IF WE NEED TO MODIFY THE SAKAI.PROEPRTIES
if (!empty($patch->sakaiprops)) {
$propstring = preg_replace('/\r\n|\r/', "\n", $patch->sakaiprops);
$props = explode("\n", $propstring);
$lines = file($patch->tomcat_dir . '/sakai/sakai.properties');
$newlines = array();
foreach ($lines AS $line_num => $line) {
if (strpos($line, "=") === FALSE) {
$newlines[] = $line;
continue;
}
if (substr($line, 0, 1) == "#") {
$newlines[] = $line;
continue;
}
$first_part_line = explode("=", $line);
$matched = FALSE;
foreach ($props AS $prop_key => $prop) {
$first_part_prop = explode("=", $prop);
// MATCH FOR AN EXISTING LINE
if ($first_part_line[0] == $first_part_prop[0]) {
$newlines[] = "#" . $line;
$newlines[] = "#SAKAIPATCHER ID: " . $patch->patch_id . " ; DATE: " . date('c') . "\n";
$newlines[] = $prop . "\n";
$matched = TRUE;
unset ($props[$prop_key]);
}
}
if (!$matched) {
$newlines[] = $line;
}
}
// SEE IF ANY OF THE PROPS WERE NEW ONES AND NOT REPLACEMENTS
if (count($props) > 0) {
foreach ($props AS $prop_key => $prop) {
$newlines[] = $prop . "\n";
}
}
file_put_contents($patch->tomcat_dir . '/sakai/sakai.properties', $newlines);
}
// SET UP ARRAY OF FILES TO CLEAN BEFORE PATCHING
$removes = array();
$removes[] = 'work/Catalina/localhost';
// ONLY CHECK OUT THE TAR IF WE HAVE FILES
if (!empty($patch->files)) {
// Need to transfer the files over to this server
$individual_files = array($patch->files);
if (strpos($patch->files, " ") !== FALSE) {
$individual_files = explode(" ", $patch->files);
}
foreach ($individual_files AS $sf) {
$f = pathinfo($sf);
if (!is_file($patchdir . $f['basename'])) {
$ret = copy ('http://74.201.2.240/public_patches/' . $f['basename'], $patchdir . $f['basename']);
print "Copying " . $f['basename'] . " to $patchdir :: $ret \n";
}
}
$patch->files = str_replace("/mnt/master/longsight/sakai-builder/", $patchdir, $patch->files);
// SEE WHAT FILES ARE IN THE PATCH
$tar_contents = array();
// SEE IF THERE ARE MULTIPLE FILES
if (strpos($patch->files, " ") !== false) {
$thefiles = explode(" ", $patch->files);
foreach ($thefiles AS $indiv) {
if (!is_file($indiv)) update_patch ($patch->patch_id, FILE_MISSING);
$ignore = exec('tar tzf ' . $indiv, $tar_contents);
}
}
else {
if (!is_file($patch->files)) update_patch ($patch->patch_id, FILE_MISSING);
$ignore = exec('tar tzf ' . $patch->files, $tar_contents);
}
// LOOK INSIDE THE PATCH
$components_number = 0;
$comp = array();
$comp_wildcards = array();
foreach ($tar_contents AS $key => $filename) {
$ext = null;
// CHECK THE FINAL FOUR CHARACTERS
$four = substr($filename, strlen($filename) - 4);
if (substr($four, 0, 1) == '.') {
$ext = substr($filename, strrpos($filename, '.') + 1);
}
$first = substr($filename, 0, strpos($filename, '/'));
// CLEAN OUT ENTIRE COMPONENTS DIR
if ($first == 'components' && strlen($filename) > strlen('components/a') && count($tar_contents) > 5 ) {
$compStr = substr($filename, 0, strpos($filename, '/', 12));
$comp[md5($compStr)] = $compStr;
$components_number++;
if (!empty($ext) && strtolower($ext) == 'jar') {
$replace_name_array = get_wildcarded_filename($filename);
$dirname = dirname($filename);
foreach ($replace_name_array AS $replace_name) {
$comp_wildcards[] = $dirname . '/' . $replace_name;
}
}
}
// CLEAN OUT SHARED WITH AGGRESSIVE WILDCARD
elseif ($first == 'shared' && !empty($ext) && strtolower($ext) == 'jar' && strlen($filename) > strlen('shared/lib/a')) {
$replace_name_array = get_wildcarded_filename($filename);
foreach ($replace_name_array AS $replace_name) {
$replace_name = 'shared/lib/' . $replace_name;
$removes[md5($replace_name)] = $replace_name;
}
}
// IF THIS IS A WAR FILE, CLEAN OUT OLD ONE TO BE SAFE
elseif ($first == 'webapps' && !empty($ext) && strtolower($ext) == 'war') {
$removes[md5($filename)] = $filename;
$removes[md5($filename)] = str_replace('.' . $ext, '', $filename);
}
elseif ($first == 'common' && !empty($ext) && strtolower($ext) == 'jar' && strlen($filename) > strlen('common/lib/a')) {
$replace_name_array = get_wildcarded_filename($filename);
foreach ($replace_name_array AS $replace_name) {
$replace_name = 'common/lib/' . $replace_name;
$removes[md5($replace_name)] = $replace_name;
}
}
}
if (count($comp_wildcards) > 0) {
foreach ($comp_wildcards AS $cw) {
$removes[md5($cw)] = $cw;
}
}
}
// CHECK IF THE PROCESS IS ALIVE
$proc = check_for_process($patch->tomcat_dir);
if ($proc) {
if(!$hotdeploy){
$cmd = "cd " . $patch->tomcat_dir . ";bin/shutdown.sh";
$output[] = $cmd;
exec($cmd, $output);
for ($x = 15; $x < 135;$x++) {
$proc = check_for_process($patch->tomcat_dir);
sleep(1);
if (!$proc) break;
}
// CHECK PROCESS ONE LAST TIME
$proc = check_for_process($patch->tomcat_dir);
// TRY TO KILL TOMCAT
if ($proc) {
$ps = get_process_number($patch->tomcat_dir);
if ($ps) {
$cmd = 'kill -9 ' . $ps;
$output[] = $cmd;
exec($cmd, $output);
sleep(5);
}
}
// CHECK PROCESS ONE LAST TIME
$proc = check_for_process($patch->tomcat_dir);
if ($proc) {
update_patch($patch->patch_id, TOMCAT_NOSHUTDOWN, $output);
}
}
}
// REMOVE OLD DIRS
foreach ($removes AS $dir) {
$first_letter = (substr($dir,0,1));
if ($first_letter == "/" || $first_letter == ".") {
die("BAD REMOVE FIELDS");
}
$cmd = "cd " . $patch->tomcat_dir . ";rm -rf " . $dir;
$output[] = $cmd;
exec($cmd, $output);
}
// ONLY UNROLL THE TAR IF WE HAVE FILES
if (!empty($patch->files)) {
// SEE IF THERE ARE MULTIPLE FILES
if (strpos($patch->files, " ") !== false) {
$thefiles = explode(" ", $patch->files);
foreach ($thefiles AS $indiv) {
$cmd = "cd " . $patch->tomcat_dir . ";tar xzf " . $indiv;
$output[] = $cmd;
exec($cmd, $output);
}
}
else {
$cmd = "cd " . $patch->tomcat_dir . ";tar xzf " . $patch->files;
$output[] = $cmd;
exec($cmd, $output);
}
}
if(!$hotdeploy){
$cmd = "cd " . $patch->tomcat_dir . ";bin/startup.sh";
$output[] = $cmd;
exec($cmd, $output);
// SLEEP FOR 60 seconds and then check the logs
sleep(50);
for ($x = 0; $x < 220; $x++) {
$cmd = 'tail -n 250 ' . $patch->tomcat_dir . '/logs/catalina.out';
$logs = array();
exec ($cmd, $logs);
// LOOK AT THE LOGS
if (in_array('destroy', $logs)) {
update_patch($patch->patch_id, TOMCAT_DOWN, $output);
}
// SEE HOW LONG IT TOOK TO STARTUP
foreach ($logs AS $key => $lin) {
if (preg_match('/Server startup in/i', $lin)) {
$twoparts = explode('Server startup in', $lin);
$int1 = (int)$twoparts[1];
update_patch($patch->patch_id, GOOD, $output, $int1);
}
}
sleep(2);
}
}
else {
//Since this is hotdeployed, just update the patch to GOOD (which will exit the script)
update_patch($patch->patch_id, GOOD, $output, 0);
}
update_patch($patch->patch_id, TOMCAT_DOWN, $output, 0);
}
function update_patch($patch_id, $result, $output=array(), $startup = 0) {
global $url;
$time = time();
$text = '';
if (count($output)) {
$text = implode("\n", $output);
}
// The submitted form data, encoded as query-string-style
// name-value pairs
$body = "result_value=$result&start_uptime=$startup&last_attempt=$time&patch_id=$patch_id&result=$text";
$c = curl_init ($url . '/remote/patch/update');
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
if ($result == IN_PROGRESS) {
return;
}
if ($result == 1) {
exit(0);
}
else {
exit($result);
}
}
function check_for_process($dir) {
// create our system command
$cmd = "ps x|grep $dir|grep -v grep|grep java";
if (strpos($dir, 'dev') === FALSE) {
$cmd .= "|grep -v '\-dev'";
}
// run the system command and assign output to a variable ($output)
exec($cmd, $output, $result);
// check the number of lines that were returned
if(count($output) >= 1){
// the process is still alive
return true;
}
// the process is dead
return false;
}
function get_process_number($dir) {
// create our system command
$cmd = "ps x|grep $dir|grep -v grep|grep java";
if (strpos($dir, 'dev') === FALSE) {
$cmd .= "|grep -v '\-dev'";
}
// run the system command and assign output to a variable ($output)
exec($cmd, $output, $result);
// check the number of lines that were returned
if(count($output) == 1) {
$arr = explode(" ", trim($output[0]));
$ps = (int)$arr[0];
if ($ps > 100) {
return $ps;
}
}
// the process is dead
return false;
}
function get_wildcarded_filename($filename) {
//assuming all jars consist of {basename}{version-number}, we break it into 2 parts:
//take just the base name (i.e. everything until a number) and then remove
//everything that consist of {basename}#.
//
//ex: "messageforums-api-2.7.4-SNAPSHOT.jar"
// $basename = messageforums-api-
// rm messageforums-api-1*, rm messageforums-api-2*, rm messageforums-api-3*...
//step 1, get base name: (ie all letters until you hit a number)
$bname = basename($filename, ".jar");
$newBaseName = "";
$sSplit = str_split($bname);
foreach($sSplit as $c){
if(intval($c)){
break;
}
$newBaseName .= $c;
}
$bname = $newBaseName;
//step 2: create all "rm" commands based on basename + # + *.jar
$returnArray = array();
for ($i = 0; $i < 10; $i++) {
$returnArray[$i] = $bname . $i . "*.jar";
}
return $returnArray;
}