Replace openWindow() for openTerminal()

Use ttyd for logging windows
This commit is contained in:
bergware
2021-11-11 02:44:35 +01:00
parent 218cea0318
commit b18cef0219
9 changed files with 103 additions and 70 deletions
@@ -61,46 +61,6 @@ switch ($action) {
}
}
break;
case 'log':
if ($container) {
$since = unscript($_REQUEST['since']??'');
$title = unbundle($_REQUEST['title']??'');
require_once "$docroot/webGui/include/ColorCoding.php";
if (!$since) {
readfile("$docroot/plugins/dynamix.docker.manager/log.htm");
echo "<script>document.title = \"$title\";</script>";
echo "<script>addLog('".addslashes("<p style='text-align:center'><span class='error label'>"._('Error')."</span><span class='warn label'>"._('Warning')."</span><span class='system label'>"._('System')."</span><span class='array label'>"._('Array')."</span><span class='login label'>"._('Login')."</span></p>")."');</script>";
$tail = 350;
} else {
$tail = null;
}
$echo = function($s) use ($match) {
$line = substr(trim($s), 8);
$span = "span";
foreach ($match as $type) {
foreach ($type['text'] as $text) {
if (preg_match("/$text/i",$line)) {
$span = "span class='{$type['class']}'";
break 2;
}
}
}
echo "<script>addLog('".addslashes("<$span>".htmlspecialchars($line)."</span>")."');</script>";
@flush();
};
$DockerClient->getContainerLog($container, $echo, $tail, $since);
echo '<script>setTimeout("loadLog(\''.addslashes(htmlspecialchars($container)).'\',\''.time().'\')", 2000);</script>';
@flush();
exit;
}
break;
case 'terminal':
$shell = $_REQUEST['shell'] ?: 'sh';
$pid = exec("pgrep -a ttyd|awk '/\\/$name\\.sock/{print \$1}'");
if ($pid) exec("kill $pid");
@unlink("/var/tmp/$name.sock");
exec("ttyd-exec -o -i '/var/tmp/$name.sock' docker exec -it '$name' $shell");
break;
default:
$arrResponse = ['error' => _('Unknown action')." '$action'"];
break;
@@ -54,12 +54,7 @@ function addDockerImageContext(image, imageTag) {
context.attach('#'+image, opts);
}
function dockerTerminal(container,shell) {
var height = 600;
var width = 900;
var top = (screen.height-height)/2;
var left = (screen.width-width)/2;
var win = window.open('', container, 'resizeable=yes,scrollbars=yes,height='+height+',width='+width+',top='+top+',left='+left);
$.get(eventURL,{action:'terminal',name:container,shell:shell},function(){win.location='/dockerterminal/'+container+'/'; win.focus();});
openTerminal('docker',container,shell,600,900);
}
function popupWithIframe(title, cmd, reload, func) {
pauseEvents();
@@ -214,11 +209,5 @@ function rebuildAll() {
$.get('/plugins/dynamix.docker.manager/include/CreateDocker.php',{updateContainer:true,mute:true,ct},function(){loadlist();});
}
function containerLogs(container, id) {
var height = 600;
var width = 900;
var run = eventURL+'?action=log&container='+id+'&title='+_('Log for:')+' '+container;
var top = (screen.height-height) / 2;
var left = (screen.width-width) / 2;
var options = 'resizeable=yes,scrollbars=yes,height='+height+',width='+width+',top='+top+',left='+left;
window.open(run, _('log'), options);
openTerminal('docker',container,id,600,900);
}
+4 -3
View File
@@ -57,6 +57,7 @@ $vfio_allow_unsafe = detect($syslinux, 'allow_unsafe_interrupts');
$bgcolor = strstr('white,azure',$display['theme']) ? '#f2f2f2' : '#1c1c1c';
$started = $var['fsState']=='Started';
$libvirt_up = $libvirt_running=='yes';
$libvirt_log = file_exists("/var/log/libvirt/libvirtd.log");
?>
<link type="text/css" rel="stylesheet" href="<?autov('/webGui/styles/jquery.filetree.css')?>">
<link type="text/css" rel="stylesheet" href="<?autov('/webGui/styles/jquery.switchbutton.css')?>">
@@ -196,15 +197,15 @@ _(VFIO allow unsafe interrupts)_:
:vms_vfio_interupts_help:
</div>
<?if (file_exists("/var/log/libvirt/libvirtd.log")):?>
<input type="button" onclick="openWindow('/webGui/scripts/tail_log&arg1=libvirt/libvirtd.log','<?=_('Libvirt Log')?>',600,900);" value="<?=_('View libvirt log')?>"/>
<?if ($libvirt_log):?>
<input type="button" onclick="openTerminal('log','libvirt','libvirt/libvirtd.log',600,900);" value="<?=_('View libvirt log')?>"/>
<?else:?>
&nbsp;
<?endif;?>
<?endif;?>
: <input type="button" id="applyBtn" value="_(Apply)_" disabled><input type="button" value="_(Done)_" onclick="done()">
</form>
<?if (file_exists("/var/log/libvirt/libvirtd.log")):?>
<?if ($libvirt_log):?>
:vms_libvirt_log_help:
@@ -73,7 +73,7 @@ function addVMContext(name, uuid, template, state, vncurl, log){
}
opts.push({divider:true});
if (log !== "") {
opts.push({text:_("Logs"), icon:"fa-navicon", action:function(e){e.preventDefault(); openWindow('/webGui/scripts/tail_log&arg1='+log, _('Log for:')+name, 600, 900);}});
opts.push({text:_("Logs"), icon:"fa-navicon", action:function(e){e.preventDefault(); openTerminal('log',name,log,600,900);}});
}
opts.push({text:_("Edit"), icon:"fa-pencil", href:path+'/UpdateVM?uuid='+uuid});
if (state == "shutoff") {
+3 -3
View File
@@ -4,8 +4,8 @@ Icon="icon-u-log"
Code="e936"
---
<?PHP
/* Copyright 2005-2020, Lime Technology
* Copyright 2012-2020, Bergware International.
/* Copyright 2005-2021, Lime Technology
* Copyright 2012-2021, 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,
@@ -17,6 +17,6 @@ Code="e936"
?>
<script>
function LogButton() {
openWindow("/webGui/scripts/tail_log&arg1=syslog&arg2=<?=$locale?>","_(System Log)_",600,900);
openTerminal('syslog','syslog','',600,900);
}
</script>
+3 -8
View File
@@ -5,8 +5,8 @@ Href="/webterminal/"
Code="e93f"
---
<?PHP
/* Copyright 2005-2020, Lime Technology
* Copyright 2012-2020, Bergware International.
/* Copyright 2005-2021, Lime Technology
* Copyright 2012-2021, 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,
@@ -22,11 +22,6 @@ function TerminalButton() {
swal({title:"_(Unsupported Feature)_",text:"_(Sorry, this feature is not supported by MSIE/Edge)_.<br>_(Please try a different browser)_",html:true,type:'error',confirmButtonText:"_(Ok)_"});
return;
}
var d = new Date();
var height = 600;
var width = 900;
var top = (screen.height-height)/2;
var left = (screen.width-width)/2;
window.open('/webterminal/', 'Web Terminal '+d.getTime(), 'resizeable=yes,scrollbars=yes,height='+height+',width='+width+',top='+top+',left='+left).focus();
openTerminal('ttyd','ttyd','',600,900);
}
</script>
+1
View File
@@ -3,6 +3,7 @@ down="1"
stop="1"
[display]
font=""
tty="15"
date="%c"
time="%R"
number=".,"
+33 -2
View File
@@ -99,6 +99,19 @@ var before = new Date();
var timers = {};
timers.bannerWarning = null;
// opened tty windows
var tty_window = {};
var _cookies_ = document.cookie.split(';');
for (var i=0,_cookie_; _cookie_=_cookies_[i]; i++) {
var _tag_ = _cookie_.split('=')[0];
if (_tag_.search(/^.win-open-/)!=-1) {
var _name_ = _tag_.split('-')[2];
var _size_ = _cookie_.split('=')[1];
var _height_ = _size_.split('-')[0];
var _width_ = _size_.split('-')[1];
tty_window[_name_] = makeWindow(_name_,_height_,_width_);
}
}
// current csrf_token
var csrf_token = "<?=$var['csrf_token']?>";
@@ -189,6 +202,13 @@ function chkDelete(form, button) {
button.value = form.confirmDelete.checked ? "<?=_('Delete')?>" : "<?=_('Apply')?>";
button.disabled = false;
}
function makeWindow(name,height,width) {
var top = (screen.height-height)/2;
if (top < 0) {top = 0; height = screen.availHeight;}
var left = (screen.width-width)/2;
if (left < 0) {left = 0; width = screen.availWidth;}
return window.open('',name,'resizeable=yes,scrollbars=yes,height='+height+',width='+width+',top='+top+',left='+left);
}
function openBox(cmd,title,height,width,load,func,id) {
// open shadowbox window (run in foreground)
var uri = cmd.split('?');
@@ -213,10 +233,21 @@ function openWindow(cmd,title,height,width) {
if (top < 0) {top = 0; height = screen.availHeight;}
var left = (screen.availWidth-width)/2;
if (left < 0) {left = 0; width = screen.availWidth;}
var options = 'resizeable=yes,scrollbars=yes,height='+height+',width='+width+',top='+top+',left='+left;
window.open('', window_name, options);
makeWindow(window_name,height,width);
form.submit();
}
function openTerminal(tag,name,more,height,width) {
// open terminal window (run in background)
var top = (screen.height-height)/2;
if (top < 0) {top = 0; height = screen.availHeight;}
var left = (screen.width-width)/2;
if (left < 0) {left = 0; width = screen.availWidth;}
name = name.replace(/ /g,"_");
tty_window[name] = makeWindow(name,height,width);
$.cookie('win-open-'+name,height+'-'+width,{path:'/'});
var socket = (['ttyd','syslog'].includes(tag) ? '/webterminal/' : '/logterminal/')+(more.length==12 ? more : name)+'/';
$.get('/webGui/include/OpenTerminal.php',{tag:tag,name:name,more:more},function(){tty_window[name].location=socket; tty_window[name].focus();});
}
function showStatus(name,plugin,job) {
$.post('/webGui/include/ProcessStatus.php',{name:name,plugin:plugin,job:job},function(status){$(".tabs").append(status);});
}
+56
View File
@@ -0,0 +1,56 @@
<?PHP
/* Copyright 2005-2021, Lime Technology
* Copyright 2012-2021, 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/webGui/include/Secure.php";
switch ($_GET['tag']) {
case 'ttyd':
$pid = exec("pgrep -a ttyd|awk '/\\/var\\/run\\/ttyd.sock:{print \$1}'");
if (!$pid) {
@unlink('/var/run/ttyd.sock');
exec("ttyd-exec -i '/var/run/ttyd.sock' bash --login");
}
break;
case 'syslog':
$path = '/var/log/';
$file = realpath($path.$_GET['name']);
$pid = exec("pgrep -a ttyd|awk '/\\/var\\/run\\/syslog.sock:{print \$1}'");
if ($pid) exec("kill $pid");
@unlink('/var/run/syslog.sock');
$command = file_exists($file) ? "tail -n 40 -f '$file'" : "bash --login";
exec("ttyd-exec -i '/var/run/syslog.sock' $command");
break;
case 'log':
$path = '/var/log/';
$name = unbundle($_GET['name']);
$file = realpath($path.$_GET['more']);
$pid = exec("pgrep -a ttyd|awk '/\\/var\\/tmp\\/$name.sock:{print \$1}'");
if ($pid) exec("kill $pid");
@unlink('/var/tmp/$name.sock');
$command = file_exists($file) ? "tail -n 40 -f '$file'" : "bash --login";
exec("ttyd-exec -i '/var/tmp/$name.sock' $command");
break;
case 'docker':
$name = unbundle($_GET['name']);
$shell = unbundle($_GET['more']) ?: 'sh';
$exec = strlen($shell)!=12; // container-id
$id = $exec ? $name : $shell;
$pid = exec("pgrep -a ttyd|awk '/\\/var\\/tmp\\/$id\\.sock/{print \$1}'");
if ($pid) exec("kill $pid");
@unlink("/var/tmp/$id.sock");
$command = $exec ? "docker exec -it '$name' $shell" : "docker logs -f -n 40 '$name'";
exec("ttyd-exec -i '/var/tmp/$id.sock' $command");
break;
}
?>