Commit e6eb5c60 authored by Jos Schellevis's avatar Jos Schellevis

Even more updates on upgrade checking.. now with configurable timeout

parent cdf99701
......@@ -33,7 +33,7 @@
# repository: error|ok
# last_ckeck: <date_time_stamp>
# updates: <#num_of_updates>
# core_version: current|<new core version>
# core_version: unknown|<current core version>
# download_size: none|<size_of_total_downloads>
# extra_space_required: none|<size_of_total_extra_space_required>
# new_packages: array with { name: <package_name>, version: <package_version> }
......@@ -55,6 +55,8 @@ required_space="none"
download_size="none"
itemcount=0
linecount=0
timer=0
timeout=30 # Wait for a maximum number of seconds to determine connection issues
# File location variables
package_json_output="/tmp/pkg_status.json"
......@@ -62,30 +64,53 @@ tmp_pkg_output_file="/tmp/packages.output"
tmp_pkg_update_file="/tmp/pkg_updates.output"
# Check if pkg is already runnig
pkg_running=`ps | grep "pkg " | grep -v "grep"`
pkg_running=`ps -x | grep "pkg " | grep -v "grep"`
if [ "$pkg_running" == "" ]; then
# start pkg update
pkg update -f > $tmp_pkg_update_file 2>&1 &
pid=$!
# wait for defined number of seconds for connection
sleep 8
# check if pkg is done, if not we have a connection issue
pkg_running=`ps | grep $pid | grep -v "grep"`
if [ "$pkg_running" == "" ]; then
pkg update -f > $tmp_pkg_update_file &
pkg_running="started" # Set running state to arbitrary value
timer=$timeout # Reset our timer
# Timeout loop for pkg update -f
while [ "$pkg_running" != "" ] && [ $timer -ne 0 ];
do
sleep 1 # wait for 1 second
pkg_running=`ps -x | grep "pkg " | grep -v "grep"`
timer=`echo $timer - 1 | bc`
done
## check if timeout is not reached
if [ $timer -gt 0 ] ; then
# Connection is ok
connection="ok"
repo_ok=`cat $tmp_pkg_update_file | grep 'Unable to update repository'`
# Now check if there are upgrades
pkg upgrade -n > $tmp_pkg_output_file &
# Reset timer before getting upgrade info
timer=$timeout # Reset our timer
pkg_running="started" # Set running state to arbitrary value
# Timeout loop for pkg upgrade -n
while [ "$pkg_running" != "" ] && [ $timer -ne 0 ];
do
sleep 1 # wait for 1 second
#pkg_running=`ps | grep 'pkg update -f' | grep -v 'grep' | tail -n 1 | awk -F '[ ]' '{print $1}'`
pkg_running=`ps -x | grep "pkg " | grep -v "grep"`
timer=`echo $timer - 1 | bc`
done
## check if timeout is not reached
if [ $timer -gt 0 ] ; then
# Check for additional repository errors
repo_ok=`cat $tmp_pkg_output_file | grep 'Unable to update repository'`
if [ "$repo_ok" == "" ]; then
# Repository can be used for updates
repository="ok"
# Now check if there are upgrades
pkg upgrade -n > $tmp_pkg_output_file
updates=`cat $tmp_pkg_output_file | grep 'The following' | awk -F '[ ]' '{print $3}'`
if [ "$updates" == "" ]; then
# There are no updates
updates="0"
else
core_version=`cat $tmp_pkg_output_file | grep 'opnsense:' | awk -F '[ ]' '{print $4}'` # Not really needed but usefull for fast version check
core_version=`pkg info opnsense | grep 'Version' | awk -F '[:]' '{print $2}'` # Changed to reflect current installed core version
required_space=`cat $tmp_pkg_output_file | grep 'The process will require' | awk -F '[ ]' '{print $5$6}'`
if [ "$required_space" == "" ]; then
required_space="none"
......@@ -148,15 +173,29 @@ if [ "$pkg_running" == "" ]; then
fi
done
if [ "$core_version" == "" ]; then
core_version="current"
core_version="unknown"
fi
fi
fi
else
# We have an connection issue and could not reach the pkg repository in timely fashion
# Kill all running pkg instances
pkg_running=`ps -x | grep "pkg " | grep -v "grep"`
if [ "$pkg_running" != "" ]; then
killall pkg
fi
fi
else
# We have an connection issue and could not reach the pkg repository in timely fashion
# Kill all running pkg instances
pkg_running=`ps -x | grep "pkg " | grep -v "grep"`
if [ "$pkg_running" != "" ]; then
killall pkg
fi
fi
# Get date/timestamp
last_check=`date`
# Write our json structure to disk
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
# pkg is already running, quitting
......
#!/bin/sh
# Copyright (C) 2014 Deciso B.V.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# USAGE:
# Add this file to a CRON job to check for pakcage updates regularly
#
# It generates different files to reflect repository/packages state
# /tmp/pkg_updates.output -> Output of pkg update
# /tmp/pkg_connection_issue -> Empty file to signal a connection issue
# /tmp/pkg_repository.error -> Empty file to signal a repository error
# /tmp/pkg_upgrades.output -> Output of pkg upgrade -n
# /tmp/pkg_updates.available -> File with content the number of upgrades or new installs
# /tmp/pkg_core_update.available -> File with content the new OPNsense version number
# /tmp/pkg_last.check -> Write date of last check to file
pkg_running=""
# Check if pkg is already runnig
pkg_running=`ps | grep "pkg " | grep -v "grep"`
if [ "$pkg_running" == "" ]; then
# start pkg update
pkg update -f > /tmp/pkg_updates.output 2>&1 &
pid=$!
# wait for defined number of seconds for connection
sleep 8
# write date time stamp to disk
echo `date` > /tmp/pkg_last.check
# check if pkg is done, if not we have a connection issue
pkg_running=`ps | grep $pid | grep -v "grep"`
if [ "$pkg_running" == "" ]; then
# Connection is ok
# Lets cleanup old connection errors
if [ -f /tmp/pkg_connection_issue ]; then
rm /tmp/pkg_connection_issue
fi
repo_ok=`cat /tmp/pkg_updates.output | grep 'Unable to update repository'`
if [ "$repo_ok" == "" ]; then
# Repository can be used for updates
# Check if earlier attemps left repository error file
if [ -f /tmp/pkg_repository.error ]; then
# Remove previous error file
rm /tmp/pkg_repository.error
fi
# Now check if there are upgrades
pkg upgrade -n > /tmp/pkg_upgrades.output
updates=`cat /tmp/pkg_upgrades.output | grep 'The following' | awk -F '[ ]' '{print $3}'`
if [ "$updates" == "" ]; then
# There are no updates
# Lets remove any leftover update files
if [ -f /tmp/pkg_updates.available ]; then
rm /tmp/pkg_updates.available
fi
else
echo $updates > /tmp/pkg_updates.available
opnsense_core_update=`cat /tmp/pkg_upgrades.output | grep 'opnsense:' | awk -F '[ ]' '{print $4}'`
if [ "$opnsense_core_update" == "" ]; then
# There is no update
# Lets cleanup leftovers
if [ -f /tmp/pkg_core_update.available] ]; then
rm /tmp/pkg_core_update.available
fi
else
echo $opnse_core_update > /tmp/pkg_core_update.available
fi
fi
else
# There is an issue with the repository
# lets let other process know
touch /tmp/pkg_repository.error
fi
else
# We have an connection issue and can reach the pkg repository in timely fashion
touch /tmp/pkg_connection_issue
killall pkg
fi
else
# pkg is already running, quitting
fi
......@@ -55,7 +55,7 @@ if($_REQUEST['getupdatestatus']) {
$pkg_status = json_decode($json,true);
if ($pkg_status["updates"]=="0") {
echo "Last check at (".$pkg_status["last_check"]."), you where up to date";
echo "Last check at (".$pkg_status["last_check"]."), you were up to date";
} else {
echo "<span class='text-danger'>A total of ".$pkg_status["updates"]." update(s) are available.</span>";
}
......
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