refactor: Update.page with web component

This commit is contained in:
Zack Spear
2023-09-25 15:14:22 -07:00
parent a6be030a3a
commit 2a992a4582

View File

@@ -13,47 +13,48 @@ Tag="upload"
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* If /boot/previous/bzroot exists, then the user has the option to downgrade to the previous version.
* Parse the text file /boot/previous/changes.txt to get the version number of the previous version.
* Then we move some file around and reboot.
*/
?>
<?
$version = $branch = $date = _('unknown');
$bzroot = file_exists('/boot/previous/bzroot');
$check = $notify['unraidos'] ? 0 : 1;
$changes = '/boot/previous/changes.txt';
$zip = htmlspecialchars(str_replace(' ','_',strtolower($var['NAME'])));
$check = $notify['unraidos'] ? 0 : 1;
if (file_exists($changes)) {
exec("head -n4 $changes",$rows);
$restoreVersion = $restoreBranch = $restoreVersionReleaseDate = 'unknown';
$restoreExists = file_exists('/boot/previous/bzroot');
$restoreChangelogPath = '/boot/previous/changes.txt';
$diagnosticsZip = htmlspecialchars(str_replace(' ', '_', strtolower($var['NAME'])));
if (file_exists($restoreChangelogPath)) {
exec("head -n4 $restoreChangelogPath", $rows);
foreach ($rows as $row) {
$i = stripos($row,'version');
if ($i !== false) {
[$version,$date] = explode(' ',trim(substr($row,$i+7)));
[$restoreVersion, $restoreVersionReleaseDate] = explode(' ', trim(substr($row, $i+7)));
break;
}
}
$branch = strpos($version,'rc')!==false ? _('Next') : (strpos($version,'beta')!==false ? _('Beta') : _('Stable'));
$restoreBranch = strpos($restoreVersion, 'rc') !== false
? _('Next')
: (strpos($restoreVersion, 'beta') !== false
? _('Beta')
: _('Stable'));
}
$reboot = preg_match("/^\*\*(REBOOT REQUIRED|DOWNGRADE)/",@file_get_contents("$docroot/plugins/unRAIDServer/README.md",false,null,0,20));
?>
<style>
ul,li{margin:0;padding-top:0;padding-bottom:0}
pre.pre{margin:30px 0}
pre>p{margin:0;padding:0}
pre#swaltext{height:600px!important}
@media (max-width:960px){pre#swaltext{height:400px!important}}
@media (max-width:960px){.sweet-alert.nchan{height:600px;width:900px;margin-left:-470px}}
@media (max-height:768px){pre#swaltext{height:400px!important}}
@media (max-height:768px){.sweet-alert.nchan{height:600px;width:900px;margin-left:-470px}}
input[value="_(Install)_"],input[value="_(Update)_"],input[value="_(Restore)_"]{margin:0}
<?if ($themes1):?>
span.vhshift{margin-top:13px!important}
<?endif;?>
</style>
<unraid-i18n-host>
<unraid-update-os
restore-version="<?= $restoreExists && $restoreVersion != 'unknown' ? $restoreVersion : '' ?>"
restore-release-date="<?= $restoreExists && $restoreVersionReleaseDate != 'unknown' ? $restoreVersionReleaseDate : '' ?>"></unraid-update-os>
</unraid-i18n-host>
<script>
var diagnosticsFile = "";
var nchan_diagnostics = new NchanSubscriber('/sub/diagnostics',{subscriber:'websocket'});
var original = null;
var nchan_diagnostics = new NchanSubscriber('/sub/diagnostics', { subscriber: 'websocket' });
const args = {};
nchan_diagnostics.on('message', function(data) {
@@ -65,135 +66,71 @@ nchan_diagnostics.on('message', function(data) {
setTimeout(cleanUp,4000);
} else if (data) {
let box = $('pre#swaltext');
box.html(box.html()+'<br>'+data).scrollTop(box[0].scrollHeight);
box.html(box.html() + '<br>' + data).scrollTop(box[0].scrollHeight);
}
});
function openInstall(cmd,title,plg) {
if (cmd == null) {
openPlugin(args.cmd,args.title,args.plg);
return;
}
args.cmd = cmd;
args.title = title;
args.plg = plg;
$.get('/plugins/dynamix.plugin.manager/include/ShowPlugins.php',{cmd:'alert'},function(data) {
if (data==0) {
// no alert message - proceed with update
setTimeout(openInstall);
} else {
// show alert message and ask for confirmation
openAlert("showchanges <?=$alerts?>","_(Alert Message)_",'openInstall');
}
});
}
function update_table(branch) {
if (original) {
if (branch != original) branch = '';
} else {
if (branch) original = branch;
}
$.get('/plugins/dynamix.plugin.manager/include/ShowPlugins.php',{system:true,branch:branch},function(data) {
data = data.split('\0');
updateInfo(data[0]);
$('#os_table').trigger('update');
});
function downgradeAction() {
$.get('/plugins/dynamix.plugin.manager/include/Downgrade.php', { version: '<?=$restoreVersion?>' }, function() { refresh(); });
}
function downgrade() {
swal({title:"_(Diagnostics)_",text:"_(Please provide diagnostics when experiencing problems)_<br>_(Post these in the forums)_",html:true,type:'warning',showCancelButton:true,confirmButtonText:"<?=_('Diagnostics')?>",cancelButtonText:"<?=_('Restore')?>"},function(diag){
swal({
title: "_(Diagnostics)_",
text: "_(Please provide diagnostics when experiencing problems)_<br>_(Post these in the forums)_",
html: true,
type: 'warning',
showCancelButton: true,
confirmButtonText: "<?= _('Diagnostics') ?>",
cancelButtonText: "<?= _('Restore') ?>",
}, function(diag) {
if (diag) {
// get diagnostics and then downgrade
setTimeout(function(){diagnostics(zipfile());},250);
setTimeout(function() {
diagnostics(zipfile());
}, 250);
} else {
// downgrade immediately
$.get('/plugins/dynamix.plugin.manager/include/Downgrade.php',{version:'<?=$version?>'},function(){refresh();});
downgradeAction();
}
});
}
function updateInfo(data) {
var updates = data.split('\n');
for (var n=0,update; update=updates[n]; n++) {
var fields = update.split('\r');
for (var i=0,field; field=fields[i]; i++) {
var row = field.split('::');
$('#'+row[0]).attr('data',row[1]).html(row[2]);
}
}
}
function initlist() {
$.get('/plugins/dynamix.plugin.manager/include/ShowPlugins.php',{init:true,system:true,check:<?=$check?>},function(data) {
$('#os_list').html(data);
<?if ($bzroot):?>
$('#previous').show();
<?endif;?>
loadlist();
});
}
function warning (data) {
$('#change_branch').prop('disabled',true);
return data.replace('green','orange').replace("<?=_('up-to-date')?>","<?=_('Reboot')?>").replace('checking','warning');
}
function loadlist(id,check) {
$.get('/plugins/dynamix.plugin.manager/include/ShowPlugins.php',{system:true,audit:id,check:check||<?=$check?>},function(data) {
var list = $('#os_list');
if (id) {
var cmd = id.split(':');
var tr = 'tr#'+cmd[0].replace(/[\. _]/g,'');
switch (cmd[1]) {
case 'update' : data = warning(data);
case 'return' : updateInfo(data); break;
case 'install': list.find(tr).remove(); list.append(warning(data)); break;
}
} else {
<?if (!$reboot):?>
updateInfo(data);
<?else:?>
updateInfo(warning(data));
<?endif;?>
}
$('#os_table').trigger('update');
$('#checkos').prop('disabled',false);
<?if ($reboot):?>
$('#change_branch').prop('disabled',true);
<?endif;?>
});
}
function cleanUp() {
if (document.hasFocus()) {
$.post('/webGui/include/Download.php',{cmd:'delete',file:diagnosticsFile},function(){
$.get('/plugins/dynamix.plugin.manager/include/Downgrade.php',{version:'<?=$version?>'},function(){refresh();});
});
$.post('/webGui/include/Download.php', { cmd: 'delete', file: diagnosticsFile }, downgradeAction());
} else {
setTimeout(cleanUp,2000);
}
}
function zipfile(){
var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0,-1);
return '<?=$zip?>-diagnostics-'+localISOTime.substr(0,16).replace(/[-:]/g,'').replace('T','-')+'.zip';
function zipfile() {
const tzoffset = (new Date()).getTimezoneOffset() * 60000; // offset in milliseconds
const localISOTime = (new Date(Date.now() - tzoffset));
const year = localISOTime.getFullYear();
const month = String(localISOTime.getMonth() + 1).padStart(2, '0');
const day = String(localISOTime.getDate()).padStart(2, '0');
const hours = String(localISOTime.getHours()).padStart(2, '0');
const minutes = String(localISOTime.getMinutes()).padStart(2, '0');
const dateOutput = `${year}${month}${day}_${hours}${minutes}`;
return '<?=$diagnosticsZip?>-diagnostics-' + dateOutput + '.zip';
}
function diagnostics(file) {
nchan_diagnostics.start();
$.post('/webGui/include/Download.php',{cmd:'diag',file:file,anonymize:''},function(zip) {
$.post('/webGui/include/Download.php', { cmd:'diag', file: file, anonymize: '' }, function(zip) {
if (zip) {
diagnosticsFile = zip;
swal({title:"_(Downloading)_...",text:"/boot/logs"+zip+"<hr><pre id='swaltext'></pre>",html:true,animation:'none',showConfirmButton:false});
swal({
title: "_(Downloading)_...",
text: "/boot/logs" + zip + "<hr><pre id='swaltext'></pre>",
html: true,
animation: 'none',
showConfirmButton: false,
});
$('.sweet-alert').addClass('nchan');
$('button.confirm').prop('disabled',true);
$('button.confirm').prop('disabled', true);
} else {
nchan_diagnostics.stop();
}
});
}
$(function() {
initlist();
$('.tabs').append("<span class='status vhshift'><input type='button' id='checkos' value=\"_(Check for Updates)_\" onclick='openPlugin(\"plugin checkos\",\"_(System Update Check)_\",\":return\")' disabled></span>");
});
</script>
<table class='tablesorter plugins shift' id='os_table'>
<thead><tr><th></th><th>_(Component)_</th><th>_(Author)_</th><th>_(Version)_</th><th>_(Status)_</th><th>_(Branch)_</th></tr></thead>
<tbody id="os_list"><tr><td colspan="6"></td></tr></tbody>
<?if ($bzroot):?>
<tbody id="previous" style="display:none"><tr><td><img src="/plugins/unRAIDServer/images/unRAIDServer.png" class="list"></td><td><b>_(Unraid OS)_ (_(previous)_)</b></td><td>LimeTech</td><td><?=$version?></td><td><input type="button" value="_(Restore)_" onclick="downgrade()"></td><td><?=$branch?></td></tbody>
<?endif;?>
</table>