Allow user sorting preference for containers and VMs

Sorting preference also dictates the sequence in which containers and
VMs are started and stopped (reversed)
This commit is contained in:
bergware
2018-03-17 13:53:05 +01:00
parent f9a1d237cc
commit a3fe701dfe
11 changed files with 136 additions and 44 deletions
@@ -34,13 +34,14 @@ img.stopped{opacity:0.3}
.started{color:#009900}
.stopped{color:#EF3D47}
.switch-button-label.off{color:inherit}
th.three{width:3%}
th.five{width:5%}
th.eight{width:8%}
</style>
<div id="dialog-confirm" style="display:none;" title="Dialog Title"></div>
<div id="iframe-popup" style="display:none;-webkit-overflow-scrolling:touch;"></div>
<span class="status" style="margin-top:-44px"><span><input type="checkbox" class="advancedview"></span></span>
<table class="tablesorter shift" id="docker_containers">
<thead><tr><th></th><th>Application</th><th>Version</th><th>Network</th><th>Port Mappings <small>(App to Host)</small></th><th>Volume Mappings <small>(App to Host)</small></th><th width="80px" style="text-align:center">Autostart</th><th class="three">Log</th></tr></thead>
<thead><tr><th></th><th>Application</th><th>Version</th><th>Network</th><th>Port Mappings <small>(App to Host)</small></th><th>Volume Mappings <small>(App to Host)</small></th><th class="eight">Autostart</th><th class="five">Log</th><th class="five" style="text-align:right;padding-right:24px"><i class="fa fa-sort"></i></th></tr></thead>
<tbody id="docker_list"><tr><td colspan='8' style='text-align:center;padding:24px'><i class='fa fa-circle-o-notch fa-spin fa-3x fa-fw'></i></td></tr></tbody>
</table>
<input type="button" onclick="addContainer()" value="Add Container" style="display:none">
@@ -85,10 +86,6 @@ function loadlist(update) {
$('.autostart').change(function() {
$.post( "/plugins/dynamix.docker.manager/include/UpdateConfig.php",{action:'autostart',container:$(this).attr('container'),response:'json'},function(data){$(this).prop('checked',data.autostart);},'json');
});
$('#docker_containers').tablesorter({
sortList:[[1,0]],headers:{0:{sorter:false},4:{sorter:false},5:{sorter:false},7:{sorter:false}},
textExtraction:{6:function(node,table,cellIndex){return $(node).find('input').prop('checked')?1:0;}}
});
if ($.cookie('docker_listview_mode')=='advanced') {$('.advanced').show(); $('.basic').hide();}
listview();
context.init({preventDoubleContext:false});
@@ -98,6 +95,24 @@ function loadlist(update) {
} else {
$('input#checkAll').show(); $('input#updateAll').hide();
}
$('i.up,i.down').click(function() {
var row = $(this).parents('tr:first');
var color1 = row.css('background-color');
if ($(this).is('.up')) {
var color2 = row.prev().css('background-color');
row.prev().css('background-color',color1);
row.css('background-color',color2);
row.insertBefore(row.prev());
} else {
var color2 = row.next().css('background-color');
row.next().css('background-color',color1);
row.css('background-color',color2);
row.insertAfter(row.next());
}
var names = ''; var index = '';
row.parent().children().find('td.ct-name').each(function(){names+=$(this).text().replace(/Container ID.*$/,'')+';';index+=$(this).parent().parent().children().index($(this).parent())+';';});
$.post('/plugins/dynamix.docker.manager/include/UserPrefs.php',{names:names,index:index});
});
});
}
$(function() {
@@ -12,16 +12,23 @@
*/
?>
<?
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
$user_prefs = '/boot/config/plugins/dockerMan/userprefs.cfg';
exec("docker ps -a --format='{{.Names}}'",$container);
exec("docker ps -a --format='{{.Names}}'",$all_containers);
if (file_exists($user_prefs)) {
$prefs = parse_ini_file($user_prefs); $sort = [];
foreach ($all_containers as $ct) $sort[] = array_search($ct,$prefs) ?? 999;
array_multisort($sort,SORT_NUMERIC,$all_containers);
}
$action = $_POST['action'];
switch ($action) {
case 'stop' : $state = 'true'; break;
case 'stop' : $state = 'true'; $all_containers = array_reverse($all_containers); break;
case 'start': $state = 'false'; break;
}
foreach ($container as $ct) {
foreach ($all_containers as $ct) {
if (exec("docker inspect --format='{{.State.Running}}' $ct")==$state) exec("docker $action $ct >/dev/null");
}
?>
@@ -439,8 +439,7 @@ function xmlToCommand($xml, $create_paths=false) {
function execCommand($command) {
// $command should have all its args already properly run through 'escapeshellarg'
$cmdTmp = explode(";",$command);
$command = $cmdTmp[0];
// $command = explode(';',$command)[0]; // commented out, not sure if we want this!
$descriptorspec = [
0 => ["pipe", "r"], // stdin is a pipe that the child will read from
@@ -20,6 +20,7 @@ $dockerManPaths = [
'template-repos' => '/boot/config/plugins/dockerMan/template-repos',
'templates-user' => '/boot/config/plugins/dockerMan/templates-user',
'templates-storage' => '/boot/config/plugins/dockerMan/templates',
'user-prefs' => '/boot/config/plugins/dockerMan/userprefs.cfg',
'images-ram' => '/usr/local/emhttp/state/plugins/dynamix.docker.manager/images',
'images-storage' => '/boot/config/plugins/dockerMan/images',
'webui-info' => '/usr/local/emhttp/state/plugins/dynamix.docker.manager/docker.json',
@@ -17,6 +17,7 @@ $docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
// Add the Docker JSON client
require_once "$docroot/plugins/dynamix.docker.manager/include/DockerClient.php";
$user_prefs = $dockerManPaths['user-prefs'];
$DockerClient = new DockerClient();
$DockerTemplates = new DockerTemplates();
@@ -26,17 +27,23 @@ if (!$all_containers) {
return;
}
if (file_exists($user_prefs)) {
$prefs = parse_ini_file($user_prefs); $sort = [];
foreach ($all_containers as $ct) $sort[] = array_search($ct['Name'],$prefs) ?? 999;
array_multisort($sort,SORT_NUMERIC,$all_containers);
}
// Read network settings
extract(parse_ini_file('state/network.ini',true));
// Read container info
$all = $DockerTemplates->getAllInfo();
$all_info = $DockerTemplates->getAllInfo();
$menu = [];
$docker = ['var docker=[];'];
foreach ($all_containers as $ct) {
$name = $ct['Name'];
$info = &$all[$name];
$info = &$all_info[$name];
$mode = $ct['NetworkMode'];
$id = $ct['Id'];
$imageID = $ct['ImageId'];
@@ -80,7 +87,7 @@ foreach ($all_containers as $ct) {
echo "<div id='$id' style='display:block; cursor:pointer'><div style='position:relative;width:48px;height:48px;margin:0px auto'>";
echo "<img src='".htmlspecialchars($icon)."' class='".htmlspecialchars($status)."' style='position:absolute;top:0;bottom:0;left:0;right:0;width:48px;height:48px'>";
echo "<i class='fa iconstatus fa-$shape $status' title='".htmlspecialchars($status)."'></i></div></div>";
echo "</td><td>";
echo "</td><td class='ct-name'>";
if ($template) {
echo "<a class='exec' onclick=\"editContainer('".addslashes(htmlspecialchars($name))."','".addslashes(htmlspecialchars($template))."')\">".htmlspecialchars($name)."</a>";
} else {
@@ -109,7 +116,9 @@ foreach ($all_containers as $ct) {
echo "<td style='white-space:nowrap'><span class='docker_readmore'>".implode('<br>',$ports)."</span></td>";
echo "<td style='word-break:break-all'><span class='docker_readmore'>".implode('<br>',$paths)."</span></td>";
echo "<td><input type='checkbox' class='autostart' container='".htmlspecialchars($name)."'".($info['autostart'] ? ' checked':'')."></td>";
echo "<td><a class='log' onclick=\"containerLogs('".addslashes(htmlspecialchars($name))."','$id',false,false)\"><img class='basic' src='/plugins/dynamix/icons/log.png'><div class='advanced' style='width:124px;'>".htmlspecialchars(str_replace('Up','Uptime',$ct['Status']))."</div><div class='advanced'>Created ".htmlspecialchars($ct['Created'])."</div></a></td></tr>";
echo "<td><a class='log' onclick=\"containerLogs('".addslashes(htmlspecialchars($name))."','$id',false,false)\"><img src='/plugins/dynamix/icons/log.png'></a><span class='advanced' style='padding-left:8px'>";
echo htmlspecialchars(str_replace('Up','Uptime',$ct['Status']))."</span><div class='advanced' style='margin-top:4px'>Created ".htmlspecialchars($ct['Created'])."</div></td>";
echo "<td style='text-align:right;padding-right:12px'><a href='#' title='Move row up'><i class='fa fa-arrow-up up'></i></a>&nbsp;<a href='#' title='Move row down'><i class='fa fa-arrow-down down'></i></a></td></tr>";
}
foreach ($DockerClient->getDockerImages() as $image) {
if (count($image['usedBy'])) continue;
@@ -1,6 +1,7 @@
<?PHP
/* Copyright 2005-2017, Lime Technology
* Copyright 2014-2017, Guilherme Jardim, Eric Schultz, Jon Panozzo.
/* Copyright 2005-2018, Lime Technology
* Copyright 2014-2018, Guilherme Jardim, Eric Schultz, Jon Panozzo.
* Copyright 2012-2018, Bergware International.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2,
@@ -14,13 +15,12 @@
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once "$docroot/plugins/dynamix.docker.manager/include/DockerClient.php";
// Autostart file
global $dockerManPaths;
$autostart_file = $dockerManPaths['autostart-file'];
$template_repos = $dockerManPaths['template-repos'];
$user_prefs = $dockerManPaths['user-prefs'];
// Update the start/stop configuration
if ($_POST['action'] == "autostart" ){
if ($_POST['action'] == 'autostart' ){
$json = ($_POST['response'] == 'json') ? true : false;
if (!$json) readfile("$docroot/update.htm");
@@ -28,21 +28,27 @@ if ($_POST['action'] == "autostart" ){
$container = urldecode(($_POST['container']));
unset($_POST['container']);
$allAutoStart = @file($autostart_file, FILE_IGNORE_NEW_LINES);
if ($allAutoStart===FALSE) $allAutoStart = [];
$allAutoStart = @file($autostart_file, FILE_IGNORE_NEW_LINES) ?: [];
$key = array_search($container, $allAutoStart);
if ($key===FALSE) {
if ($key===false) {
array_push($allAutoStart, $container);
if ($json) echo json_encode(['autostart' => true]);
}
else {
} else {
unset($allAutoStart[$key]);
if ($json) echo json_encode(['autostart' => false]);
}
file_put_contents($autostart_file, implode(PHP_EOL, $allAutoStart).(count($allAutoStart)? PHP_EOL : ""));
// sort containers for start-up
if (file_exists($user_prefs)) {
$prefs = parse_ini_file($user_prefs); $sort = [];
foreach ($allAutoStart as $ct) $sort[] = array_search($ct,$prefs) ?? 999;
array_multisort($sort,SORT_NUMERIC,$allAutoStart);
} else {
natcasesort($allAutoStart);
}
$allAutoStart ? file_put_contents($autostart_file, implode(PHP_EOL, $allAutoStart).PHP_EOL) : @unlink($autostart_file);
}
if ($_POST['#action'] == "templates" ){
if ($_POST['#action'] == 'templates' ){
readfile("$docroot/update.htm");
$repos = $_POST['template_repos'];
file_put_contents($template_repos, $repos);
@@ -0,0 +1,36 @@
<?PHP
/* Copyright 2005-2018, Lime Technology
* Copyright 2015-2018, Derek Macias, Eric Schultz, Jon Panozzo.
* Copyright 2012-2018, Bergware International.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
?>
<?
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once "$docroot/plugins/dynamix.docker.manager/include/DockerClient.php";
$autostart_file = $dockerManPaths['autostart-file'];
$user_prefs = $dockerManPaths['user-prefs'];
$names = explode(';',$_POST['names']);
$index = explode(';',$_POST['index']);
$save = []; $i = 0;
foreach ($names as $name) if ($name) $save[] = $index[$i++]."=\"".$name."\""; else $i++;
file_put_contents($user_prefs, implode("\n",$save)."\n");
// sort containers for start-up
if (file_exists($autostart_file)) {
$prefs = parse_ini_file($user_prefs); $sort = [];
$allAutoStart = file($autostart_file, FILE_IGNORE_NEW_LINES);
foreach ($allAutoStart as $ct) $sort[] = array_search($ct,$prefs) ?? 999;
array_multisort($sort,SORT_NUMERIC,$allAutoStart);
file_put_contents($autostart_file, implode(PHP_EOL, $allAutoStart).PHP_EOL);
}
?>