class AutoUpdate(plugins.Plugin): __author__ = '*****@*****.**' __version__ = '1.1.1' __name__ = 'auto-update' __license__ = 'GPL3' __description__ = 'This plugin checks when updates are available and applies them when internet is available.' def __init__(self): self.ready = False self.status = StatusFile('/root/.auto-update') self.lock = Lock() def on_loaded(self): if 'interval' not in self.options or ( 'interval' in self.options and self.options['interval'] is None): logging.error( "[update] main.plugins.auto-update.interval is not set") return self.ready = True logging.info("[update] plugin loaded.") def on_internet_available(self, agent): with self.lock: logging.debug( "[update] internet connectivity is available (ready %s)" % self.ready) if not self.ready: return if self.status.newer_then_hours(self.options['interval']): logging.debug( "[update] last check happened less than %d hours ago" % self.options['interval']) return logging.info("[update] checking for updates ...") display = agent.view() prev_status = display.get('status') try: display.update(force=True, new_data={'status': 'Checking for updates ...'}) to_install = [] to_check = [ ('bettercap/bettercap', parse_version('bettercap -version'), True, 'bettercap'), ('evilsocket/pwngrid', parse_version('pwngrid -version'), True, 'pwngrid-peer'), ('evilsocket/pwnagotchi', pwnagotchi.version, False, 'pwnagotchi') ] for repo, local_version, is_native, svc_name in to_check: info = check(local_version, repo, is_native) if info['url'] is not None: logging.warning( "update for %s available (local version is '%s'): %s" % (repo, info['current'], info['url'])) info['service'] = svc_name to_install.append(info) num_updates = len(to_install) num_installed = 0 if num_updates > 0: if self.options['install']: for update in to_install: plugins.on('updating') if install(display, update): num_installed += 1 else: prev_status = '%d new update%c available!' % ( num_updates, 's' if num_updates > 1 else '') logging.info("[update] done") self.status.update() if num_installed > 0: display.update(force=True, new_data={'status': 'Rebooting ...'}) pwnagotchi.reboot() except Exception as e: logging.error("[update] %s" % e) display.update(force=True, new_data={ 'status': prev_status if prev_status is not None else '' })
class AutoUpdate(plugins.Plugin): __author__ = '*****@*****.**' __version__ = '2.0.0' __name__ = 'auto-update' __license__ = 'GPL3' __description__ = 'This plugin checks when updates are available and applies them when internet is available.' __defaults__ = { 'enabled': True, 'install': True, 'interval': 1, } def __init__(self): self.ready = False self.status = StatusFile('/root/.auto-update', data_format='json') self.lock = Lock() self.done_caplets_check = False # only check once def on_loaded(self): self.ready = True logging.info("[auto-update] plugin loaded.") def on_internet_available(self, agent): if self.lock.locked(): return with self.lock: logging.debug("[auto-update] internet connectivity is available (ready %s)" % self.ready) if not self.ready: return if self.status.newer_then_hours(self.options['interval']): logging.debug("[auto-update] last check happened less than %d hours ago" % self.options['interval']) return logging.info("[auto-update] checking for updates ...") config = agent.config() display = agent.view() prev_status = display.get('status') try: display.update(force=True, new_data={'status': 'Checking for updates ...'}) to_install = [] to_check = [ ('dadav/bettercap', parse_version('bettercap -version'), True, 'bettercap'), ('dadav/pwnagotchi', pwnagotchi.__version__, False, 'pwnagotchi') ] for repo, local_version, is_native, svc_name in to_check: info = check(local_version, repo, is_native) if info['url'] is not None: logging.warning( "[auto-update] for %s available (local version is '%s'): %s" % ( repo, info['current'], info['url'])) info['service'] = svc_name to_install.append(info) num_updates = len(to_install) num_installed = 0 if num_updates > 0: if self.options['install']: for update in to_install: plugins.on('updating') if install(display, update): num_installed += 1 else: prev_status = '%d new update%c available!' % (num_updates, 's' if num_updates > 1 else '') if not self.done_caplets_check: prev_commit = self.status.data_field_or('caplets_version', '') try: logging.info('[auto-update] Checking for new caplets.') current_commit = fetch_last_commit('bettercap/caplets') if prev_commit != current_commit: logging.info('[auto-update] Updating caplets.') rc = os.system('bettercap -eval "caplets.update;q"') if rc == 0: self.status.update(data={'caplets_version': current_commit}) except Exception as ex: logging.error("[auto-update] %s", ex) finally: self.done_caplets_check = True # update plugins logging.info("[auto-update] Checking for new plugins") if plugin_update(config) == 0: logging.info("[auto-update] Upgrading plugins") plugin_upgrade(None, config) logging.info("[auto-update] done") self.status.update(data=self.status.data) if num_installed > 0: display.update(force=True, new_data={'status': 'Rebooting ...'}) pwnagotchi.reboot() except Exception as e: logging.error("[auto-update] %s", e) display.update(force=True, new_data={'status': prev_status if prev_status is not None else ''})