Commit 51758409 authored by Jos Schellevis's avatar Jos Schellevis

WIP cleanup system information widget and us json with ajax calls... so much nicer

parent 84684f08
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
# USAGE: # USAGE:
# Add this file to a CRON job to check for pakcage updates regularly # Add this file to a CRON job to check for package updates regularly
# #
# This script generates a json structured file with the following content: # This script generates a json structured file with the following content:
# connection: error|ok # connection: error|ok
...@@ -80,8 +80,7 @@ if [ "$pkg_running" == "" ]; then ...@@ -80,8 +80,7 @@ if [ "$pkg_running" == "" ]; then
repository="ok" repository="ok"
# Now check if there are upgrades # Now check if there are upgrades
pkg upgrade -n > $tmp_pkg_output_file pkg upgrade -n > $tmp_pkg_output_file
updates=`cat $tmp_pkg_output_file | grep 'The following' | awk -F '[ ]' '{print $3}'` # > /tmp/pkg_upgrades.output updates=`cat $tmp_pkg_output_file | grep 'The following' | awk -F '[ ]' '{print $3}'`
#updates=`cat /tmp/pkg_upgrades.output | grep 'The following' | awk -F '[ ]' '{print $3}'`
if [ "$updates" == "" ]; then if [ "$updates" == "" ]; then
# There are no updates # There are no updates
updates="0" updates="0"
...@@ -148,7 +147,7 @@ if [ "$pkg_running" == "" ]; then ...@@ -148,7 +147,7 @@ if [ "$pkg_running" == "" ]; then
itemcount=`echo $linecount + 4 | bc` itemcount=`echo $linecount + 4 | bc`
fi fi
done done
if [ "$opnsense_core_update" == "" ]; then if [ "$core_version" == "" ]; then
core_version="current" core_version="current"
fi fi
fi fi
...@@ -158,7 +157,7 @@ if [ "$pkg_running" == "" ]; then ...@@ -158,7 +157,7 @@ if [ "$pkg_running" == "" ]; then
killall pkg killall pkg
fi fi
last_check=`date` last_check=`date`
echo "{\"connection\":\"$connection\"},{\"repository\":\"$repository\"},{\"last_check\":,\"$last_check\"},{\"updates\":\"$updates\"},{\"core_version\":\"$core_version\"},{\"download_size\":\"$download_size\"},{\"extra_space_required\":\"$required_space\"},{\"new_packages\":[$packages_new]},{\"upgrade_packages\":[$packages_upgraded]}" > $package_json_output echo "{\"connection\":\"$connection\",\"repository\":\"$repository\",\"last_check\":\"$last_check\",\"updates\":\"$updates\",\"core_version\":\"$core_version\",\"download_size\":\"$download_size\",\"extra_space_required\":\"$required_space\",\"new_packages\":[$packages_new],\"upgrade_packages\":[$packages_upgraded]}" > $package_json_output
else else
# pkg is already running, quitting # pkg is already running, quitting
fi fi
...@@ -37,45 +37,31 @@ require_once('notices.inc'); ...@@ -37,45 +37,31 @@ require_once('notices.inc');
include_once("includes/functions.inc.php"); include_once("includes/functions.inc.php");
require_once("script/load_phalcon.php"); require_once("script/load_phalcon.php");
/* Check for updates on /tmp/pkg_* files, generated by */ $file_pkg_status="/tmp/pkg_status.json";
$file_connection="/tmp/pkg_connection_issue";
$file_repository="/tmp/pkg_repository.error";
$file_updates="/tmp/pkg_updates.available";
$file_newcore_version="/tmp/pkg_core_update.available";
$file_last_check="/tmp/pkg_last.check";
$update_text="(update in 10 seconds)";
if($_POST['action'] == 'pkg_update') {
if($_POST['action'] == 'call_this') {
/* Setup Shell variables */ /* Setup Shell variables */
$shell_output = array(); $shell_output = array();
$shell = new Core\Shell(); $shell = new Core\Shell();
// execute shell command and collect (only valid) info into named array // execute shell command and collect (only valid) info into named array
if ($shell->exec("/usr/local/opnsense/scripts/pkg_updates.sh&",false,false,$shell_output) == 0 ) $shell->exec("/usr/local/opnsense/scripts/pkg_updatecheck.sh",false,false,$shell_output);
{
// Aplication done
}
} }
if($_REQUEST['getupdatestatus']) { if($_REQUEST['getupdatestatus']) {
if (file_exists($file_connection)) { if (file_exists($file_pkg_status)) {
/* There is an connection issue, repository can not be reached */
$update_text="<div class='text-danger'>Connection error</div><a href='' onclick='checkupdate()'>Click to retry now</a>"; $json = file_get_contents($file_pkg_status);
} elseif (file_exists($file_repository)) { $pkg_status = json_decode($json,true);
/* The repository is not corect */
$update_text="<div class='text-danger'>Repository error</div><a href='' onclick='checkupdate()'>Click to retry now</a>"; if ($pkg_status["updates"]=="0") {
} elseif (file_exists($file_updates)) { echo "Last check at (".$pkg_status["last_check"]."), you where up to date";
/* There are updates available */
$updates=file_get_contents($file_updates);
$update_text="<div class='text-info'>There are <b>".$updates."</b> update(s) available</div><a href='/system_firmware.php'> Click to update </a>";
} elseif (file_exists($file_last_check)) {
$last_check=file_get_contents($file_last_check);
$update_text="<div class='text-info'>No updates found (checked once a day) </div><div class='text-info'>last time on: ".$last_check."</div><a href='' onclick='checkupdate()'>Click to check now</a>" ;
} else { } else {
$update_text="<div class='text-info'>Daily cron has not yet run</div><a href='' onclick='checkupdate()'>Click here to try now</a>"; echo "<span class='text-danger'>A total of ".$pkg_status["updates"]." update(s) are available.</span>";
}
} else {
echo "Unknown<span class='btn-link' onclick='checkupdate()'>Click to check now</span>";
} }
echo $update_text;
exit; exit;
} }
...@@ -131,8 +117,7 @@ $filesystems = get_mounted_filesystems(); ...@@ -131,8 +117,7 @@ $filesystems = get_mounted_filesystems();
Updates Updates
</td> </td>
<td> <td>
<div id='updatestatus'><?php echo $update_text; ?></div> <div id='updatestatus'><span class="text-info">Fetching status</span></div>
</td> </td>
</tr> </tr>
<?php endif; ?> <?php endif; ?>
...@@ -327,16 +312,22 @@ $filesystems = get_mounted_filesystems(); ...@@ -327,16 +312,22 @@ $filesystems = get_mounted_filesystems();
jQuery('#uname').html("<?php echo php_uname("a"); ?>"); jQuery('#uname').html("<?php echo php_uname("a"); ?>");
} }
function checkupdate() { function checkupdate() {
jQuery('#updatestatus').html('<span class="text-info">Updating.... (takes about 10 seconds) </span>');
jQuery.ajax({ jQuery.ajax({
type: "POST", type: "POST",
url: '/widgets/widgets/system_information.widget.php', url: '/widgets/widgets/system_information.widget.php',
data:{action:'call_this'}, data:{action:'pkg_update'},
success:function(html) { success:function(html) {
alert(html); //alert(html);
getstatus();
} }
}); });
} }
window.onload = function(){
getstatus();
}
<?php if(!isset($config['system']['firmware']['disablecheck'])): ?> <?php if(!isset($config['system']['firmware']['disablecheck'])): ?>
function getstatus() { function getstatus() {
scroll(0,0); scroll(0,0);
...@@ -355,7 +346,6 @@ $filesystems = get_mounted_filesystems(); ...@@ -355,7 +346,6 @@ $filesystems = get_mounted_filesystems();
// to avoid this we set the innerHTML property // to avoid this we set the innerHTML property
jQuery('#updatestatus').prop('innerHTML',transport.responseText); jQuery('#updatestatus').prop('innerHTML',transport.responseText);
} }
setTimeout('getstatus()', 10000); // Update after 10 seconds
<?php endif; ?> <?php endif; ?>
//]]> //]]>
</script> </script>
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment