Added docker autostart wait period

- fixed autostart entries sometimes wrongly sorted
This commit is contained in:
bergware
2018-08-05 13:34:46 +02:00
parent 894a2b2375
commit 9c0cff4782
4 changed files with 69 additions and 38 deletions

View File

@@ -37,6 +37,7 @@ th.five{width:5%}
th.eight{width:8%}
th.load{width:100px}
tbody > tr.sortable:hover{cursor:move}
input.wait{width:24px;margin:0 4px;padding:0 5px;border:none;box-shadow:none;background-color:transparent}
</style>
<div id="dialog-confirm" style="display:none;" title="Dialog Title"></div>
<div id="iframe-popup" style="display:none;-webkit-overflow-scrolling:touch;"></div>
@@ -75,11 +76,17 @@ function resetSorting() {
$.post('/plugins/dynamix.docker.manager/include/UserPrefs.php',{reset:true},function(){loadlist();});
}
function listview() {
if ($.cookie('docker_listview_mode')=='advanced') {
var more = $.cookie('docker_listview_mode')=='advanced';
if (more) {
$('.docker_readmore').readmore('destroy');
} else {
$('.docker_readmore').readmore({maxHeight:32,moreLink:"<a href='#' style='text-align:center'><i class='fa fa-chevron-down'></i></a>",lessLink:"<a href='#' style='text-align:center'><i class='fa fa-chevron-up'></i></a>"});
}
$('input.autostart').each(function(){
var wait = $('#'+$(this).prop('id').replace('auto','wait'));
var auto = $(this).prop('checked');
if (auto && more) wait.show(); else wait.hide();
});
}
var sortableHelper = function(e,i){
i.children().each(function(){
@@ -105,21 +112,28 @@ function loadlist() {
$('.iconstatus').each(function(){
if ($(this).hasClass('stopped')) $('div.'+$(this).prop('id')).hide();
});
$('.autostart').switchButton({labels_placement:"right"});
$('.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');
$('.autostart').switchButton({labels_placement:'right'});
$('.autostart').change(function(){
var more = $.cookie('docker_listview_mode')=='advanced';
var wait = $('#'+$(this).prop('id').replace('auto','wait'));
var auto = $(this).prop('checked');
if (auto && more) wait.show(); else wait.hide();
$.post('/plugins/dynamix.docker.manager/include/UpdateConfig.php',{action:'autostart',container:$(this).attr('container'),auto:auto,wait:wait.find('input.wait').val()});
});
$('input.wait').change(function(){
$.post('/plugins/dynamix.docker.manager/include/UpdateConfig.php',{action:'wait',container:$(this).attr('container'),wait:$(this).val()});
});
if ($.cookie('docker_listview_mode')=='advanced') {$('.advanced').show(); $('.basic').hide();}
listview();
context.init({preventDoubleContext:false});
$('input[type=button]').prop('disabled',false).show('slow');
var update = false;
for (var i=0,ct; ct=docker[i]; i++) if (ct.update=='false') {update = true; break;};
if (!update) $('input#updateAll').hide();
listview();
});
}
var watchDocker = new NchanSubscriber('/sub/dockerload', /^((?!chrome|android).)*safari/i.test(navigator.userAgent) ? {subscriber:'longpoll'} : {});
watchDocker.on('message', function(data) {
watchDocker.on('message', function(data){
data = data.split('\n');
for (var i=0,row; row=data[i]; i++) {
var id = row.split(' ');
@@ -129,7 +143,7 @@ watchDocker.on('message', function(data) {
});
$(function() {
$('.advancedview').switchButton({labels_placement:'left', on_label:'Advanced View', off_label:'Basic View', checked:$.cookie('docker_listview_mode')=='advanced'});
$('.advancedview').change(function() {
$('.advancedview').change(function(){
$('.advanced').toggle('slow');
$('.basic').toggle('slow');
$.cookie('docker_listview_mode',$('.advancedview').is(':checked')?'advanced':'basic',{expires:3650});

View File

@@ -38,6 +38,10 @@ $driver = DockerUtil::driver();
$docker_cfgfile = '/boot/config/docker.cfg';
$dockercfg = parse_ini_file($docker_cfgfile);
function var_split($item, $i=0) {
return explode(' ',$item)[$i];
}
#######################################
## DOCKERTEMPLATES CLASS ##
#######################################
@@ -257,7 +261,7 @@ class DockerTemplates {
$DockerUpdate = new DockerUpdate();
//$DockerUpdate->verbose = $this->verbose;
$info = DockerUtil::loadJSON($dockerManPaths['webui-info']);
$autoStart = @file($dockerManPaths['autostart-file'], FILE_IGNORE_NEW_LINES) ?: [];
$autoStart = array_map('var_split', @file($dockerManPaths['autostart-file'], FILE_IGNORE_NEW_LINES) ?: []);
foreach ($DockerClient->getDockerContainers() as $ct) {
$name = $ct['Name'];
$image = $ct['Image'];

View File

@@ -20,6 +20,7 @@ $DockerTemplates = new DockerTemplates();
$containers = $DockerClient->getDockerContainers();
$images = $DockerClient->getDockerImages();
$user_prefs = $dockerManPaths['user-prefs'];
$autostart_file = $dockerManPaths['autostart-file'];
if (!$containers && !$images) {
echo "<tr><td colspan='8' style='text-align:center;padding-top:12px'>No Docker containers installed</td></tr>";
@@ -39,6 +40,9 @@ $docker = ['var docker=[];'];
$null = '0.0.0.0';
$menu = [];
$autostart = @file($autostart_file, FILE_IGNORE_NEW_LINES) ?: [];
$names = array_map('var_split', $autostart);
foreach ($containers as $ct) {
$name = $ct['Name'];
$id = $ct['Id'];
@@ -57,6 +61,7 @@ foreach ($containers as $ct) {
$shape = $running ? ($paused ? 'pause' : 'play') : 'square';
$status = $running ? ($paused ? 'paused' : 'started') : 'stopped';
$icon = $info['icon'] ?: '/plugins/dynamix.docker.manager/images/question.png';
$wait = var_split($autostart[array_search($name,$names)],1);
$ports = [];
foreach ($ct['Ports'] as $port) {
$intern = $running ? ($ct['NetworkMode']=='host' ? $host : $port['IP']) : $null;
@@ -102,7 +107,8 @@ foreach ($containers as $ct) {
echo "<td style='word-break:break-all'><span class='docker_readmore'>".implode('<br>',$paths)."</span></td>";
echo "<td class='advanced'><div class='usage-disk sys load-$id'><span id='cpu-$id' style='width:0'></span></div></td>";
echo "<td class='advanced'><div class='usage-disk sys load-$id'><span id='mem-$id' style='width:0'></span></div></td>";
echo "<td><input type='checkbox' class='autostart' container='".htmlspecialchars($name)."'".($info['autostart'] ? ' checked':'')."></td>";
echo "<td><input type='checkbox' id='$id-auto' class='autostart' container='".htmlspecialchars($name)."'".($info['autostart'] ? ' checked':'').">";
echo "<span id='$id-wait' style='float:right;display:none'>wait<input class='wait' container='".htmlspecialchars($name)."' type='number' value='$wait' placeholder='0' title='seconds'></span></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'>";
echo htmlspecialchars(str_replace('Up','Uptime',$ct['Status']))."</div><div class='advanced' style='margin-top:4px'>Created ".htmlspecialchars($ct['Created'])."</div></a></td></tr>";
}

View File

@@ -17,46 +17,53 @@ require_once "$docroot/plugins/dynamix.docker.manager/include/DockerClient.php";
$autostart_file = $dockerManPaths['autostart-file'];
$template_repos = $dockerManPaths['template-repos'];
$user_prefs = $dockerManPaths['user-prefs'];
// Update the start/stop configuration
if ($_POST['action'] == 'autostart' ){
$json = ($_POST['response'] == 'json') ? true : false;
if (!$json) readfile("$docroot/update.htm");
$user_prefs = $dockerManPaths['user-prefs'];
switch ($_POST['action']) {
case 'autostart':
// update container autostart setting
$container = urldecode(($_POST['container']));
unset($_POST['container']);
$allAutoStart = @file($autostart_file, FILE_IGNORE_NEW_LINES) ?: [];
$key = array_search($container, $allAutoStart);
if ($key===false) {
array_push($allAutoStart, $container);
if ($json) echo json_encode(['autostart' => true]);
$wait = $_POST['wait'];
$item = rtrim("$container $wait");
$autostart = @file($autostart_file, FILE_IGNORE_NEW_LINES) ?: [];
$key = array_search($item, $autostart);
if ($_POST['auto']=='true') {
if ($key===false) $autostart[] = $item;
} else {
unset($allAutoStart[$key]);
if ($json) echo json_encode(['autostart' => false]);
unset($autostart[$key]);
}
// 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 ($autostart) {
if (file_exists($user_prefs)) {
$prefs = parse_ini_file($user_prefs); $sort = [];
foreach ($autostart as $ct) $sort[] = array_search(var_split($ct),$prefs) ?? 999;
array_multisort($sort,$autostart);
} else {
natcasesort($autostart);
}
file_put_contents($autostart_file, implode("\n", $autostart)."\n");
} else @unlink($autostart_file);
break;
case 'wait':
// update wait period used after container autostart
$container = urldecode(($_POST['container']));
$wait = $_POST['wait'];
$item = rtrim("$container $wait");
$autostart = file($autostart_file, FILE_IGNORE_NEW_LINES) ?: [];
$names = array_map('var_split', $autostart);
$autostart[array_search($container,$names)] = $item;
file_put_contents($autostart_file, implode("\n", $autostart)."\n");
break;
case 'templates':
// update template
readfile("$docroot/update.htm");
$repos = $_POST['template_repos'];
file_put_contents($template_repos, $repos);
$DockerTemplates = new DockerTemplates();
$DockerTemplates->downloadTemplates();
break;
}
if ( isset($_GET['is_dir'] )) {
if (isset($_GET['is_dir'])) {
echo json_encode(['is_dir' => is_dir($_GET['is_dir'])]);
}
?>