new: new auto-update plugin

This commit is contained in:
Simone Margaritelli 2019-10-05 13:25:34 +02:00
parent 03094264f0
commit e9b9a1edb9
3 changed files with 65 additions and 3 deletions
sdcard/rootfs/root/pwnagotchi
config.yml
scripts/pwnagotchi/plugins/default

@ -6,6 +6,9 @@ main:
custom_plugins:
# which plugins to load and enable
plugins:
auto-update:
enabled: false
interval: 1 # every day
auto-backup:
enabled: false
interval: 1 # every day

@ -10,7 +10,7 @@ import subprocess
from datetime import datetime
OPTIONS = dict()
LAST_BACKUP = None
LAST_UPDATE = None
READY = False
def on_loaded():
@ -18,7 +18,7 @@ def on_loaded():
Gets called when the plugin gets loaded
"""
global READY
global LAST_BACKUP
global LAST_UPDATE
if 'files' not in OPTIONS or ('files' in OPTIONS and OPTIONS['files'] is None):
logging.error("AUTO-BACKUP: No files to backup.")
@ -42,7 +42,7 @@ def on_internet_available(display, config, log):
"""
Called in manual mode when there's internet connectivity
"""
global LAST_BACKUP
global LAST_UPDATE
if READY:
if LAST_BACKUP is not None:

@ -0,0 +1,59 @@
__author__ = 'evilsocket@gmail.com'
__version__ = '1.0.0'
__name__ = 'auto-update'
__license__ = 'GPL3'
__description__ = 'This plugin performs an "apt update && apt upgrade" when internet is availaible.'
import os
import logging
import subprocess
from datetime import datetime
OPTIONS = dict()
LAST_UPDATE = None
READY = False
STATUS_FILE = '/root/.auto-update'
def on_loaded():
global READY
global LAST_UPDATE
if 'interval' not in OPTIONS or ('interval' in OPTIONS and OPTIONS['interval'] is None):
logging.error("AUTO-UPDATE: Interval is not set.")
return
if os.path.exists(STATUS_FILE):
LAST_UPDATE = datetime.fromtimestamp(os.path.getmtime(STATUS_FILE))
READY = True
def on_internet_available(display, config, log):
global LAST_UPDATE
if READY:
if LAST_UPDATE is not None:
if (datetime.now() - LAST_UPDATE).days < OPTIONS['interval']:
return
try:
logging.info("AUTO-UPDATE: updating packages index ...")
update = subprocess.Popen('apt update -y', shell=True, stdin=None,
stdout=open("/dev/null", "w"), stderr=None, executable="/bin/bash")
update.wait()
logging.info("AUTO-UPDATE: updating packages ...")
upgrade = subprocess.Popen('apt upgrade -y', shell=True, stdin=None,
stdout=open("/dev/null", "w"), stderr=None, executable="/bin/bash")
upgrade.wait()
logging.info("AUTO-UPDATE: complete.")
LAST_UPDATE = datetime.now()
with open(STATUS_FILE, 'w') as f:
f.write('success')
except Exception as e:
logging.exception("AUTO-UPDATE ERROR")