Commit c8ab15f0 authored by sokai's avatar sokai

upgrade function added

parent d3e0a2c9
......@@ -7,12 +7,21 @@
#logfile=/tmp/ffp-apdb.log
#exec > $logfile 2>&1
SCRIPTNAME='ffp-apdb'
SCRIPTPATH='/etc/init.d/' # _with_ trailing / !
CRONINTERVAL=55 # run cron every x minutes (max 59?)
UPDATEURL='http://freifunk-potsdam.de/apdb/' # _with_ trailing / !
UPGRADEURL="http://freifunk-potsdam.de/${SCRIPTNAME}.php"
##### ----- do NOT edit from this line on ----- #####
## needed for init script
START=99
EXTRA_COMMANDS="update status"
EXTRA_COMMANDS="status update upgrade"
INTERVAL=55 # run cron every x minutes (max 59?)
URL='http://freifunk-potsdam.de/apdb/' # with / at the end!
## script
SCRIPTVERSION='160122-bf'
SCRIPT=${SCRIPTPATH}${SCRIPTNAME}
CRONTAB='/var/spool/cron/crontabs/root'
WIF=$(ifconfig | awk '/10\.22\.[0-9]{1,3}\.[0-9]{1,3}/ {print $1}' RS="\n\n" | grep wlan) # http://stackoverflow.com/a/18344694/338049
#WIFI= uci show = [email protected][0].ifname=wlan0-adhoc-2
......@@ -39,19 +48,21 @@ CLIENTS=$(iw dev ${WIF} station dump|grep -c Station)
#CLIENTS=$(cat /tmp/dhcp.leases|wc -l)
start() {
update="*/$INTERVAL * * * * /etc/init.d/ffp-apdb update # every $INTERVAL minutes"
sed -ie '/\/etc\/init.d\/ffp-apdb/d' /var/spool/cron/crontabs/root
echo "$update" >> /var/spool/cron/crontabs/root
echo "root" > /var/spool/cron/crontabs/cron.update
logger -t 'ffp-apdb' "cron job added (/etc/init.d/ffp-apdb start)"
update
if ! $(grep -q ${SCRIPT} ${CRONTAB}); then
CRONJOB="*/${CRONINTERVAL} * * * * ${SCRIPT} update # FFP Access Points DataBase"
echo "${CRONJOB}" >> ${CRONTAB}
echo "root" > /var/spool/cron/crontabs/cron.update # reload crontab
logger -t ${SCRIPTNAME} "cron job added (${SCRIPT} start)"
update
fi
}
stop() {
if grep -q /etc/init.d/ffp-apdb /var/spool/cron/crontabs/root;then
sed -ie '/\/etc\/init.d\/ffp-apdb/d' /var/spool/cron/crontabs/root
echo "root" > /var/spool/cron/crontabs/cron.update
logger -t 'ffp-apdb' "cron job removed (/etc/init.d/ffp-apdb stop)"
if $(grep -q ${SCRIPT} ${CRONTAB}) ;then
# better grep than sed with unescaped file pathes ... -> http://stackoverflow.com/a/5413132/338049
grep -v ${SCRIPT} ${CRONTAB} > /tmp/crontab.temp && mv /tmp/crontab.temp ${CRONTAB} && chmod 600 ${CRONTAB}
echo "root" > /var/spool/cron/crontabs/cron.update # reload crontab
logger -t ${SCRIPTNAME} "cron job removed (${SCRIPT} stop)"
fi
}
......@@ -97,7 +108,7 @@ status() {
update() {
# only one instance should be running at time
# if exists an other one kill them and all wgets
PID_FILE="/tmp/run/ffp-apdb.pid"
PID_FILE="/tmp/run/${SCRIPTNAME}.pid"
if [ -f $PID_FILE ]; then
PID="$(cat ${PID_FILE} 2> /dev/null)"
if [ ! -z $PID ]; then
......@@ -116,12 +127,46 @@ update() {
echo "$$" > $PID_FILE
KEY=$(wget -q -O - ${URL}ap.php?mode=get)
KEY=$(wget -q -O - ${UPDATEURL}ap.php?mode=get)
PARAM="&key=${KEY}&ip=%22${IP}%22&host=%22${HOSTNAME}%22&geoloc=%22${GEOLOC}%22&location=%22${LOCATION}%22&contact=%22${CONTACT}%22&gw_ip=%22${GWIP}%22&uptime=%22${UPTIME}%22&machine=%22${MACHINE}%22&systemtype=%22${SYSTEMTYPE}%22&scriptversion=%22${SCRIPTVERSION}%22&firmware=%22${FIRMWARE}%22"
wget -q -O - ${URL}ap.php?mode=put"${PARAM}"
wget -q -O - ${UPDATEURL}ap.php?mode=put"${PARAM}"
#echo ${URL}ap.php?mode=put"${PARAM}"
logger -t 'ffp-apdb' "AP information transfered to AP database (/etc/init.d/ffp-apdb update)"
logger -t ${SCRIPTNAME} "AP information transfered to AP database (${SCRIPT} update)"
# everything looks fine
rm $PID_FILE
}
upgrade() {
## self-upgrade of this script
# 1. stop
stop
# 2. diable
disable
# 3. backup the orig. script
mv ${SCRIPT} /tmp/${SCRIPTNAME}
# 4. get and install new version
wget -q -O ${SCRIPT} $UPGRADEURL && chmod +x ${SCRIPT}
if [ -f ${SCRIPT} ]; then
VERSION=$(grep 'SCRIPTVERSION=' ${SCRIPT}| cut -f2 -d'='|sed 's/ /%20/g'|sed "s/'//g")
SUCCESS="new version (${VERSION}) installed at ${SCRIPT} ((${SCRIPT} upgrade))"
echo ${SUCCESS}
logger -t ${SCRIPTNAME} ${SUCCESS}
else
ERROR="couldn't download new version - old version at ${SCRIPT} re-enabled (${SCRIPT} upgrade)"
echo ${ERROR}
logger -t ${SCRIPTNAME} ${ERROR}
mv /tmp/${SCRIPTNAME} ${SCRIPT}
fi
# 5. enable
enable
# 6. start
start
}
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