-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: new core plugins This adds new core plugins for Help, Webserver, and Health and supporting code to make it possible to load them. I had to overload some of the Err backend methods and monkey patch some methods in the PluginManager. Eventually, I'd like to upstream these changes and this will no longer be needed * fix: skip loading plugs if backend = True This allows setting a new flag in the Core section of the .plug file to indicate a plugin is a backend. These backend plugins are then skipped when loaded by _load_plugins_generic This code could eventually be upstreamed into Errbot to allow it to handle multiple plugins per module easier
- Loading branch information
1 parent
458697d
commit 70a61f4
Showing
13 changed files
with
463 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[Core] | ||
Name = APRSHealth | ||
Module = aprs_core_plugins | ||
Core = True | ||
|
||
[Documentation] | ||
Description = APRS Friendly Health Plugin | ||
|
||
[Python] | ||
Version = 3 | ||
|
||
[Errbot] | ||
Min=6.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[Core] | ||
Name = APRSHelp | ||
Module = aprs_core_plugins | ||
Core = True | ||
|
||
[Documentation] | ||
Description = APRS Friendly Help Plugin | ||
|
||
[Python] | ||
Version = 3 | ||
|
||
[Errbot] | ||
Min=6.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[Core] | ||
Name = APRSWebserver | ||
Module = aprs_core_plugins | ||
Core = True | ||
|
||
[Documentation] | ||
Description = APRS Friendly Web Plugin | ||
|
||
[Python] | ||
Version = 3 | ||
|
||
[Errbot] | ||
Min=6.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
[Core] | ||
Name = APRS | ||
Module = aprs | ||
Backend = True | ||
|
||
[Documentation] | ||
Description = Backend for APRS | ||
|
||
[Python] | ||
Version = 3 | ||
|
||
[Errbot] | ||
Min=6.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from aprs_backend.plugins import APRSHelp | ||
from aprs_backend.plugins import APRSWebserver | ||
from aprs_backend.plugins import APRSHealth | ||
|
||
__all__ = ["APRSHelp", "APRSWebserver", "APRSHealth"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from aprs_backend.plugins.help import APRSHelp | ||
from aprs_backend.plugins.web import APRSWebserver | ||
from aprs_backend.plugins.health import APRSHealth | ||
|
||
__all__ = ["APRSHelp", "APRSWebserver", "APRSHealth"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import gc | ||
from datetime import datetime | ||
|
||
from errbot import BotPlugin, webhook | ||
from errbot.utils import format_timedelta | ||
|
||
|
||
class APRSHealth(BotPlugin): | ||
"""Customized health plugin that shifts most of the outputs to webhooks and removes the botcmds""" | ||
|
||
@webhook | ||
def status(self, _): | ||
"""If I am alive I should be able to respond to this one""" | ||
pm = self._bot.plugin_manager | ||
all_blacklisted = pm.get_blacklisted_plugin() | ||
all_loaded = pm.get_all_active_plugin_names() | ||
all_attempted = sorted(pm.plugin_infos.keys()) | ||
plugins_statuses = [] | ||
for name in all_attempted: | ||
if name in all_blacklisted: | ||
if name in all_loaded: | ||
plugins_statuses.append(("BA", name)) | ||
else: | ||
plugins_statuses.append(("BD", name)) | ||
elif name in all_loaded: | ||
plugins_statuses.append(("A", name)) | ||
elif ( | ||
pm.get_plugin_obj_by_name(name) is not None | ||
and pm.get_plugin_obj_by_name(name).get_configuration_template() is not None | ||
and pm.get_plugin_configuration(name) is None | ||
): | ||
plugins_statuses.append(("C", name)) | ||
else: | ||
plugins_statuses.append(("D", name)) | ||
loads = self.status_load("") | ||
gc = self.status_gc("") | ||
|
||
return { | ||
"plugins_statuses": plugins_statuses, | ||
"loads": loads["loads"], | ||
"gc": gc["gc"], | ||
} | ||
|
||
@webhook | ||
def status_load(self, _): | ||
"""shows the load status""" | ||
try: | ||
from posix import getloadavg | ||
|
||
loads = getloadavg() | ||
except Exception: | ||
loads = None | ||
|
||
return {"loads": loads} | ||
|
||
@webhook | ||
def status_gc(self, _): | ||
"""shows the garbage collection details""" | ||
return {"gc": gc.get_count()} | ||
|
||
@webhook | ||
def uptime(self, _): | ||
"""Return the uptime of the bot""" | ||
return {"up": format_timedelta(datetime.now() - self._bot.startup_time), "since": self._bot.startup_time} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from errbot import BotPlugin, botcmd | ||
|
||
|
||
class APRSHelp(BotPlugin): | ||
"""An alternative help plugin. | ||
For now, it simply replies with preconfigured help text. | ||
In the future, it would be great to use the internal webserver to serve | ||
help text or generate it as a static file that could be served via | ||
static site serving | ||
""" | ||
|
||
def __init__(self, bot, name: str = "Help") -> None: | ||
""" | ||
Calls super init and adds a few plugin variables of our own. This makes PEP8 happy | ||
""" | ||
super().__init__(bot, name) | ||
self.help_text = getattr(self._bot.bot_config, "APRS_HELP_TEXT") | ||
|
||
@botcmd | ||
def help(self, _, __): | ||
return self.help_text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import logging | ||
from threading import Thread | ||
|
||
from webtest import TestApp | ||
from werkzeug.serving import ThreadedWSGIServer | ||
|
||
from errbot import BotPlugin, webhook | ||
from errbot.core_plugins import flask_app | ||
|
||
|
||
class APRSWebserver(BotPlugin): | ||
def __init__(self, *args, **kwargs): | ||
self.server = None | ||
self.server_thread = None | ||
self.ssl_context = None | ||
self.test_app = TestApp(flask_app) | ||
# TODO: Make this configurable in the APRS bot config, since there's no plugin config anymore | ||
self.web_config = {"HOST": "0.0.0.0", "PORT": 3141} # nosec | ||
super().__init__(*args, **kwargs) | ||
|
||
def activate(self): | ||
if self.server_thread and self.server_thread.is_alive(): | ||
raise Exception("Invalid state, you should not have a webserver already running.") | ||
self.server_thread = Thread(target=self.run_server, name="Webserver Thread") | ||
self.server_thread.start() | ||
self.log.debug("Webserver started.") | ||
|
||
super().activate() | ||
|
||
def deactivate(self): | ||
if self.server is not None: | ||
self.log.info("Shutting down the internal webserver.") | ||
self.server.shutdown() | ||
self.log.info("Waiting for the webserver thread to quit.") | ||
self.server_thread.join() | ||
self.log.info("Webserver shut down correctly.") | ||
super().deactivate() | ||
|
||
def run_server(self): | ||
host = self.web_config["HOST"] | ||
port = self.web_config["PORT"] | ||
self.log.info("Starting the webserver on %s:%i", host, port) | ||
try: | ||
self.server = ThreadedWSGIServer( | ||
host, | ||
port, | ||
flask_app, | ||
) | ||
wsgi_log = logging.getLogger("werkzeug") | ||
wsgi_log.setLevel(self.bot_config.BOT_LOG_LEVEL) | ||
self.server.serve_forever() | ||
except KeyboardInterrupt: | ||
self.log.info("Keyboard interrupt, request a global shutdown.") | ||
self.server.shutdown() | ||
except Exception as exc: | ||
self.log.exception("Exception with webserver: %s", exc) | ||
self.log.debug("Webserver stopped") | ||
|
||
@webhook | ||
def echo(self, incoming_request): | ||
""" | ||
A simple test webhook | ||
""" | ||
self.log.debug("Your incoming request is: %s", incoming_request) | ||
return str(incoming_request) |
Oops, something went wrong.