Improve heartbeat handling

This commit is contained in:
Kumi 2019-02-02 08:18:57 +00:00
parent d693653464
commit 6ada4c9757
3 changed files with 90 additions and 62 deletions

63
manager/device.py Normal file
View file

@ -0,0 +1,63 @@
from .signals import *
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://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://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 = ""
device.lastbeat = timezone.now()
if ip:
device.lasttime = timezone.now()
device.curip = ip
device.save()
return code

View file

@ -0,0 +1,23 @@
# Generated by Django 2.1.5 on 2019-02-02 07:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('manager', '0040_auto_20190131_1951'),
]
operations = [
migrations.AddField(
model_name='device',
name='wireless',
field=models.DateTimeField(blank=True, editable=False, null=True, verbose_name='Last Update of Wireless Config'),
),
migrations.AlterField(
model_name='device',
name='wifi',
field=models.ManyToManyField(blank=True, to='manager.Wifi'),
),
]

View file

@ -14,6 +14,7 @@ from django.conf import settings
from .models import Device, Organization, Network, Model, Wifi, UserStatus
from .signals import *
from .device import makewificonfig, heartbeathandler
from distutils.dir_util import copy_tree
@ -43,72 +44,13 @@ def heartbeat(request):
device = get_object_or_404(Device, secret=request.POST.get("secret", ""))
ip = request.POST.get("ip", "")
if device.update:
sigRebootDevice(device.serial, None, False)
device.reboot = False
code = """
. /etc/vpnsecret
if /usr/bin/wget -O/tmp/update.bin https://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 device.lastbeat < device.changed:
code = """
. /etc/vpnsecret
if /usr/bin/wget -O/tmp/wireless.in https://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 = ""
device.lastbeat = timezone.now()
if ip:
device.lasttime = timezone.now()
device.curip = ip
device.save()
return HttpResponse(code)
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
return HttpResponse(heartbeathandler(device, ip))
@csrf_exempt
def wireless(request):
device = get_object_or_404(Device, secret=request.POST.get("secret", ""))
device.wireless = timezone.now()
device.save()
return HttpResponse(makewificonfig(device))