vpnmanager/manager/device.py

69 lines
1.9 KiB
Python

from .signals import *
from django.utils import timezone
def makewificonfig(device):
output = ""
i = 1
for wifi in device.wifi.all():
output += """config wifi-iface
option network 'wwan%i'
option ssid '%s'
option encryption 'psk2'
option device 'radio1'
option mode 'sta'
option key '%s'
""" % (i, wifi.ssid, wifi.key)
i += 1
return output
def heartbeathandler(device, ip):
if device.update:
sigRebootDevice(device.serial, None, False)
device.reboot = False
code = """. /etc/vpnsecret
if /usr/bin/wget -O/tmp/update.bin https://${HOSTNAME:-admin360.kumi.host}/update --post-data "secret=$SECRET" --no-check-certificate 2>/var/log/wget;
then
/sbin/sysupgrade -F -n /tmp/update.bin
fi
"""
elif device.model.wwan and ((not device.wireless) or device.wireless < device.changed):
code = """
. /etc/vpnsecret
if /usr/bin/wget -O/tmp/wireless.in https://${HOSTNAME:-admin360.kumi.host}/wireless --post-data "secret=$SECRET" --no-check-certificate 2>/var/log/wget;
then
while read p
do
echo $p >/tmp/wireless.line
if /bin/grep wifi-iface /tmp/wireless.line && ! /bin/grep radio /tmp/wireless.line;
then
break
fi
echo $p >>/tmp/wireless.og
done </etc/config/wireless
/bin/cat /tmp/wireless.in >>/tmp/wireless.og
/bin/cp /tmp/wireless.og /etc/config/wireless
/sbin/uci commit /etc/config/wireless
/bin/rm /tmp/wireless.*
fi
"""
device.lastbeat = timezone.now()
elif device.reboot:
code = "/sbin/reboot"
else:
code = """
. /etc/vpnsecret
/usr/bin/wget -O/etc/hosts https://${HOSTNAME:-admin360.kumi.host}/hosts --post-data "secret=$SECRET" --no-check-certificate >/var/log/wget 2>&1
"""
device.lastbeat = timezone.now()
if ip:
device.lasttime = timezone.now()
device.curip = ip
device.save()
return code