Commit f30b7676 authored by Mikhail Karpenko's avatar Mikhail Karpenko
Browse files

Add frontend/backend scripts for disk formatter

parent 93ad0442
Loading
Loading
Loading
Loading
+60 −0
Original line number Original line Diff line number Diff line
/** 
 * @file format_disk.js
 * @brief Disk formatting front end for Elphel393 series camera
 * @copyright Copyright (C) 2017 Elphel Inc.
 * @author Mikhail Karpenko <mikhail@elphel.com>
 *
 * @licstart  The following is the entire license notice for the 
 * JavaScript code in this page.
 *
 *   The JavaScript code in this page is free software: you can
 *   redistribute it and/or modify it under the terms of the GNU
 *   General Public License (GNU GPL) as published by the Free Software
 *   Foundation, either version 3 of the License, or (at your option)
 *   any later version.  The code is distributed WITHOUT ANY WARRANTY;
 *   without even the implied warranty of MERCHANTABILITY or FITNESS
 *   FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.
 *
 *   As additional permission under GNU GPL version 3 section 7, you
 *   may distribute non-source (e.g., minimized or compacted) forms of
 *   that code without the copy of the GNU GPL normally required by
 *   section 4, provided you include this license notice and a URL
 *   through which recipients can access the Corresponding Source.
 *
 *  @licend  The above is the entire license notice
 *  for the JavaScript code in this page.
 */

function init_actions() {
	$("#btn_format").click(function() {
		var btn_class_disabled = "btn btn-danger disabled";
		if ($("#btn_format").attr("class") != btn_class_disabled) {
			// introduce this var to fasicilitate further upgrades
			var index = 0;
			var disk_path = $("#path_cell_" + index).text()
			
			force = '';
			if ($("#chk_force").is(':checked'))
				force = '&force';
			else
				force = '';
			$("#status_cell_" + index).text("Formatting");
			$.ajax({
				url:"format_disk.php?cmd=format&disk_path=" + disk_path + force,
				success: function(result) {
					if (result == "OK") {
						$("#status_cell_" + index).text("Done");
						$("#btn_format").attr("class", btn_class_disabled);
						$("#chk_force").attr("disabled", true);
					} else {
						if (result == "")
							result = "Unrecognized error";
						console.log(result);
						$("#status_cell_" + index).text(result);
						$("#disk_row_" + index).attr("class", "danger");
					}
				}
			})
		}
	});
}
+138 −0
Original line number Original line Diff line number Diff line
<?php
/**
 * @file format_disk.php
 * @brief Disk formatting back end for Elphel393 series camera
 * @copyright Copyright (C) 2017 Elphel Inc.
 * @author Mikhail Karpenko <mikhail@elphel.com>
 *
 * @par <b>License</b>:
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

$parted_script = "format_disk.py";

function get_disks_list()
{
	global $parted_script;
	
	exec($parted_script . " --list", $output, $ret_val);
	return array("ret_val" => $ret_val, "disks" => $output);
}

function table_row($index, $disk_path, $disk_size, $sys_size, $status = "")
{
	echo "<tr id='disk_row_" . $index . "'>" .
			"<td id='path_cell_" . $index . "'>" . $disk_path . "</td>" .
			"<td>" . $disk_size . "</td>" .
			"<td>" . $sys_size . "</td>" .
			"<td id='status_cell_". $index . "'>" . $status . "</td>" .
			"</tr>";
}

function table_row_err($msg)
{
	echo "<tr>" . "<td colspan='4'>" . $msg . "</td>" . "</tr>";
}

function table_body($disks)
{
	$ret_val = $disks["ret_val"];
	$num = count($disks["disks"]);
	if ($ret_val == 0 && $num > 0) {
		for ($i = 0; $i < $num; $i++) {
			$data = explode(":", $disks["disks"][$i]);
			table_row($i, $data[0], $data[1], $data[2]);
		}
	} else if ($ret_val == 0 && $num == 0) {
		$msg = "No disks suitable for partitioning";
		table_row_err($msg);
	} else {
		table_row_err($disks["disks"][0]);
	}
}

function btn_class($inactive)
{
	$class = "btn btn-danger";
	if ($inactive)
		echo $class . " disabled";
	else
		echo $class;
}

/* process commands */
if (isset($_GET["cmd"]))
	$cmd = $_GET["cmd"];
else
	$cmd = "no_command";
if ($cmd == "format") {
	if (isset($_GET["disk_path"])) {
		if (isset($_GET["force"]))
			$force = ' -f ';
		else
			$force = ' ';
		$disk_path = $_GET["disk_path"];
		exec($parted_script . $force . $disk_path, $output, $ret_val);
		if ($ret_val == 0) {
			print("OK");
		} else {
			foreach ($output as $key => $val)
				if ($val == '')
					unset($output[$key]);
			print(implode(', ', $output));
		}
		exit();
	}
} else {
	// just create the page
	$disks_list = get_disks_list();
	if ($disks_list["ret_val"] != 0 || count($disks_list["disks"]) == 0)
		$no_disk = true;
	else
		$no_disk = false;
}
	
?>

<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8"/>
	<meta name="author" content="Elphel"/>
	<link rel="stylesheet" href="js/bootstrap/css/bootstrap.css">
	<script src="js/jquery-2.2.3.js"></script>
	<script src="js/jquery-ui/jquery-ui.js"></script>
	<script src="js/bootstrap/js/bootstrap.js"></script>
	<script src="format_disk.js"></script>	
</head>

<body onload="init_actions()" style="padding-top:0px;">
	<h2 id="title" style="padding-left:10px;">Format disk</h2>
	<div style="padding-left:10px;">
		<table class="table">
			<thead>
				<tr><th>Disk</th><th>Total size</th><th>System partition</th><th>Status</th></tr>
			</thead>
			<tbody id="disks_list">
				<?php table_body($disks_list);	?>
			</tbody>
		</table>
	</div>
	<div style="padding-left:10px;">
		<span class="checkbox"><label><input id="chk_force" type="checkbox">Forse 'mkfs' to create a file system</label></span>
		<button id="btn_format" type="button" class="<?php btn_class($no_disk); ?>"><b>Format</b></button>
	</div>
</body>

</html>
 No newline at end of file