Project

General

Profile

« Previous | Next » 

Revision 35fcf1c1

Added by Stephen Beaver almost 10 years ago

Fixes #5291
Fixes #5279

View differences:

src/usr/local/www/pkg_mgr_install.php
97 97
//		log:
98 98
//		exitcode:
99 99
//		data:{current:, total}
100
//		pid:
101 100
//
102 101
// Todo:
103 102
//		Respect next_log_line and append log to output window rather than writing it
104 103

  
105
// isvalidpid($g['varrun_path']/$g['product_name']-upgrade.pid)
106 104

  
107
if($_REQUEST['ajax']) {
105
if ($_REQUEST['ajax']) {
108 106
	$response = "";
109 107
	$code = 0;
110 108

  
109
	// Check to see if our process is still running
110
	$pidfile = $g['varrun_path'] . '/' . $g['product_name'] . '-upgrade.pid';
111

  
112
	if (!isvalidpid($pidfile)) {
113
		$running = "stopped";
114
		// The log files may not be complete when the process terminates so we need wait
115
		waitfor_string_in_file($_REQUEST['logfilename'] . '.txt', "__RC=", 10);
116
	} else {
117
		$running = "running";
118
	}
119

  
120
	$pidarray = array('pid' => $running);
121

  
111 122
	// Process log file -----------------------------------------------------------------------------------------------
112 123
	$logfile = @fopen($_REQUEST['logfilename'] . '.txt', "r");
113 124

  
114
	if($logfile != FALSE) {
125
	if ($logfile != FALSE) {
115 126
		$resparray = array();
116 127
		$statusarray = array();
117 128

  
118 129
		// Log file is read a line at a time so that we can detect/modify certain entries
119 130
		while (($logline = fgets($logfile)) !== false) {
120 131
			// Check for return codes and replace with suitable strings
121
			if(strpos($logline, "_RC=") != false) {
132
			if (strpos($logline, "_RC=") != false) {
122 133
				$code = str_replace("__RC=", "", $logline);
123 134

  
124
				if($code == 0) {
135
				if ($code == 0) {
125 136
					$logline = gettext("Success") . "\n";
126 137
				} else {
127 138
					$logline = gettext("Failed") . "\n";
......
148 159

  
149 160
	$JSONfile = @fopen($_REQUEST['logfilename'] . '.json', "r");
150 161

  
151
	if($JSONfile != FALSE) {
162
	if ($JSONfile != FALSE) {
152 163
		while (($logline = fgets($JSONfile)) !== false) {
153
			 if(strpos($logline, 'INFO_PROGRESS_TICK')) {
154
				$progress = $logline;
155
			 }
164
			if (!feof($JSONfile)	 && (strpos($logline, 'INFO_PROGRESS_TICK') !== false)) {
165
				if (strpos($logline, '}}') !== false) {
166
					$progress = $logline;
167
				}
168
			}
156 169
		}
157 170

  
158 171
		fclose($JSONfile);
159 172

  
160
		if(strlen($progress) > 0) {
173
		if (strlen($progress) > 0) {
161 174
			$progarray = json_decode($progress, true);
162
			if($progarray == NULL) {
163
				$progarray = array();
164
			}
165 175
		}
166 176
	}
167 177

  
168

  
169
	// Check to see if our process is still running
170
	$pidarray = array('pid' => (file_exists("/proc/" . $_REQUEST['pid']) ? "running":"stopped"));
171

  
172 178
	// Glob all the arrays we have made together, and convert to JSON
173 179
	print(json_encode($resparray + $pidarray + $statusarray + $progarray));
174 180
	exit;
175 181
}
176 182

  
183
function waitfor_string_in_file($filename, $string, $timeout) {
184
	$start = $now = time();
185

  
186
	while (($now - $start) < $timeout) {
187
		$testfile = @fopen($filename, "r");
188

  
189
		if ($testfile != FALSE) {
190
			while (($line = fgets($testfile)) !== false) {
191
				if (strpos($line, $string) !== false) {
192
					fclose($testfile);
193
					return(true);
194
				}
195
			}
196

  
197
			fclose($testfile);
198
		}
199
	usleep(100000);
200
	$now = time();
201
	}
202

  
203
	return(false);
204
}
205

  
177 206
if ($_POST) {
178 207
	if (empty($_POST['id']) && $_POST['mode'] != 'reinstallall') {
179 208
		header("Location: pkg_mgr_installed.php");
......
256 285
	</div>
257 286
<?php endif;
258 287

  
259
if($_POST['mode'] == 'delete') {
288
if ($_POST['mode'] == 'delete') {
260 289
	$modetxt = gettext("removal");
261
} else if($_POST['mode'] == 'reinstallpkg') {
290
} else if ($_POST['mode'] == 'reinstallpkg') {
262 291
	$modetxt = gettext("reinstallation");
263 292
} else {
264 293
	$modetxt = gettext("installation");
265 294
}
266 295

  
267 296
if (!empty($_POST['id']) || $_GET['mode'] == 'showlog' || ($_GET['mode'] == 'installedinfo' && !empty($_GET['pkg']))):
268
//	$pidfile = $g['varrun_path'] . '/' . $g['product_name'] . '-upgrade.pid';
269
//	if(isvalidpid($pidfile)) {
270
//		exec("/bin/pgrep -nF {$pidfile}", $output, $retval);
271
//		$pid = $output[0];
272
//		$start_polling = true;
273
//	}
297
	// What if the user navigates away from this page and then come back via his/her "Back" button?
298
	$pidfile = $g['varrun_path'] . '/' . $g['product_name'] . '-upgrade.pid';
299

  
300
	if (isvalidpid($pidfile)) {
301
		$start_polling = true;
302
	}
274 303
?>
275 304

  
276 305
	<div class="progress" style="display: none;">
......
361 390
	conf_mount_ro();
362 391
}
363 392

  
364
include('foot.inc')?>
393
?>
365 394

  
366 395
<script>
367

  
396
//<![CDATA[
368 397
//	Update the progress indicator
369 398
function setProgress(barName, percent) {
370 399
	$('.progress').show()
......
388 417
// Ask the user to wait a bit
389 418
function show_info() {
390 419
	$('#final').addClass("alert-info");
391
	$('#final').html("Please wait while the " + "<?=$modetxt?>" + " of " + "<?=$pkgid?>" + " " + "completes." + "<br />(Some packages may take several minutes!)");
420
	$('#final').html("Please wait while the " + "<?=$modetxt?>" + " of " + "<?=$pkgid?>" + " " + "completes." + "<br />" +
421
	"<?=gettext("(Some packages may take several minutes!)")?>");
392 422
	$('#final').show();
393 423
}
394 424

  
......
403 433
			url: "pkg_mgr_install.php",
404 434
			type: "post",
405 435
			data: { ajax: "ajax",
406
					pid: "<?=$pid?>",
407 436
					logfilename: "/tmp/webgui-log",
408 437
					next_log_line: "0"
409 438
				  }
......
411 440

  
412 441
	// Deal with the results of the above ajax call
413 442
	ajaxRequest.done(function (response, textStatus, jqXHR) {
414
//		alert(response);
415 443
		var json = new Object;
444

  
416 445
		json = jQuery.parseJSON(response);
417 446

  
418
		if(json.log != "not ready") {
447
		if (json.log != "not ready") {
419 448
			// Write the log file to the "output" textarea
420 449
			$('#output').html(json.log);
421 450
			$('#output').scrollTop($('#output')[0].scrollHeight);
422 451

  
423 452
			// Update the progress bar
424 453
			progress = 0;
425
			if(json.data) {
454
			if (json.data) {
426 455
				setProgress('progressbar', ((json.data.current * 100) / json.data.total));
427 456
				progress = json.data.total - json.data.current
428 457
			}
429 458

  
430 459
			// Now we need to determine if the installation/removal was successful, and tell the user. Not as easy as it sounds :)
431

  
432
			if((json.pid == "stopped") && (progress == 0) && (json.exitstatus == 0)) {
460
			if ((json.pid == "stopped") && (progress == 0) && (json.exitstatus == 0)) {
433 461
				show_success();
434 462
				repeat = false;
435 463
			}
436
/*
437
			if((json.pid == "stopped") && ((progress != 0) || (json.exitstatus != 0))) {
438
				show_failure();
439
				repeat = false;
440
			}
441 464

  
442
			if((json.pid == "stopped") && ((progress != 0) || (json.exitstatus != 0))) {
465
			if ((json.pid == "stopped") && ((progress != 0) || (json.exitstatus != 0))) {
443 466
				show_failure();
444 467
				repeat = false;
445 468
			}
446
*/
447
			// ToDo: THere are more end conditions we need to catch
469
			// ToDo: There are more end conditions we need to catch
448 470
		}
449 471

  
450 472
		// And maybe do it again
451
		if(repeat)
473
		if (repeat)
452 474
			setTimeout(getLogsStatus, 500);
453 475
	});
454 476
}
455 477

  
456
if("<?=$start_polling?>") {
457
	setTimeout(getLogsStatus, 1000);
458
	show_info();
459
}
478
events.push(function(){
479
	if ("<?=$start_polling?>") {
480
		setTimeout(getLogsStatus, 1000);
481
		show_info();
482
	}
483
});
484
//]]>
485
</script>
460 486

  
461
</script>
487
<?php
488
include('foot.inc');

Also available in: Unified diff