✨ New hook for bundled plugins to add to systeminfo bundles
SWU and PMGR make use of that to add console logs. SWU now also adds the update log as plugin_softwareupdate_update.log. Also improved caching and sorting behaviour on SWU update log.
This commit is contained in:
1 parent
8bb3312bba
commit
fc106e5918
4 files changed
+131
-48
No files matched your search
@@ -42,7 +42,7 @@ def get_systeminfo(environment_detector, connectivity_checker, additional_fields
|
||||
return flattened
|
||||
|
||||
|
||||
def get_systeminfo_bundle(systeminfo, logbase, printer=None):
|
||||
def get_systeminfo_bundle(systeminfo, logbase, printer=None, plugin_manager=None):
|
||||
from octoprint.util import to_bytes
|
||||
|
||||
systeminfotxt = []
|
||||
@@ -83,20 +83,40 @@ def get_systeminfo_bundle(systeminfo, logbase, printer=None):
|
||||
for log in (
|
||||
"octoprint.log",
|
||||
"serial.log",
|
||||
"plugin_softwareupdate_console.log",
|
||||
"plugin_pluginmanager_console.log",
|
||||
):
|
||||
logpath = os.path.join(logbase, log)
|
||||
if os.path.exists(logpath):
|
||||
z.write(logpath, arcname=log, compress_type=compress_type)
|
||||
|
||||
# add additional logs as needed
|
||||
for log, logpath in {
|
||||
"webcamd.log": "/var/log/webcamd.log",
|
||||
"haproxy.log": "/var/log/haproxy.log",
|
||||
}.items():
|
||||
if os.path.exists(logpath) and os.access(logpath, os.R_OK):
|
||||
z.write(logpath, arcname=log, compress_type=compress_type)
|
||||
# add additional bundle contents from bundled plugins
|
||||
if plugin_manager:
|
||||
for name, hook in plugin_manager.get_hooks(
|
||||
"octoprint.systeminfo.additional_bundle_files"
|
||||
).items():
|
||||
try:
|
||||
plugin = plugin_manager.get_plugin_info(name)
|
||||
if not plugin.bundled:
|
||||
# we only support this for bundled plugins because we don't want
|
||||
# third party logs to blow up the bundles
|
||||
continue
|
||||
|
||||
logs = hook()
|
||||
|
||||
for log, content in logs.items():
|
||||
if isinstance(content, str):
|
||||
# log path
|
||||
if os.path.exists(content) and os.access(content, os.R_OK):
|
||||
z.write(content, arcname=log, compress_type=compress_type)
|
||||
elif callable(content):
|
||||
# content generating callable
|
||||
z.writestr(log, to_bytes(content()), compress_type=compress_type)
|
||||
except Exception:
|
||||
logging.getLogger(__name__).exception(
|
||||
"Error while retrieving additional bundle contents for plugin {}".format(
|
||||
name
|
||||
),
|
||||
extra={"plugin": name},
|
||||
)
|
||||
|
||||
return z
|
||||
|
||||
@@ -148,7 +168,9 @@ def systeminfo_command(ctx, path, **kwargs):
|
||||
zipfilename = os.path.join(path, get_systeminfo_bundle_name())
|
||||
click.echo("Writing systeminfo bundle to {}...".format(zipfilename))
|
||||
|
||||
z = get_systeminfo_bundle(systeminfo, settings.getBaseFolder("logs"))
|
||||
z = get_systeminfo_bundle(
|
||||
systeminfo, settings.getBaseFolder("logs"), plugin_manager=plugin_manager
|
||||
)
|
||||
try:
|
||||
with open(zipfilename, "wb") as f:
|
||||
for data in z:
|
||||
|
||||
@@ -248,6 +248,12 @@ class PluginManagerPlugin(
|
||||
},
|
||||
]
|
||||
|
||||
# Additional bundle contents
|
||||
|
||||
def get_additional_bundle_files(self, *args, **kwargs):
|
||||
console_log = self._settings.get_plugin_logfile_path(postfix="console")
|
||||
return {os.path.basename(console_log): console_log}
|
||||
|
||||
##~~ StartupPlugin
|
||||
|
||||
def on_after_startup(self):
|
||||
@@ -2158,6 +2164,7 @@ def __plugin_load__():
|
||||
"octoprint.ui.web.templatetypes": __plugin_implementation__.get_template_types,
|
||||
"octoprint.events.register_custom_events": _register_custom_events,
|
||||
"octoprint.access.permissions": __plugin_implementation__.get_additional_permissions,
|
||||
"octoprint.systeminfo.additional_bundle_files": __plugin_implementation__.get_additional_bundle_files,
|
||||
}
|
||||
|
||||
global __plugin_helpers__
|
||||
|
||||
@@ -49,7 +49,7 @@ MINIMUM_PYTHON = "2.7.9"
|
||||
MINIMUM_SETUPTOOLS = "39.0.1"
|
||||
MINIMUM_PIP = "9.0.3"
|
||||
|
||||
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
|
||||
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
|
||||
|
||||
|
||||
##~~ Plugin
|
||||
@@ -98,6 +98,7 @@ class SoftwareUpdatePlugin(
|
||||
self._update_log = []
|
||||
self._update_log_path = None
|
||||
self._update_log_dirty = False
|
||||
self._update_log_mutex = threading.RLock()
|
||||
|
||||
self._environment_supported = True
|
||||
self._environment_versions = {}
|
||||
@@ -164,6 +165,36 @@ class SoftwareUpdatePlugin(
|
||||
},
|
||||
]
|
||||
|
||||
# Additional bundle contents
|
||||
|
||||
def get_additional_bundle_files(self, *args, **kwargs):
|
||||
console_log = self._settings.get_plugin_logfile_path(postfix="console")
|
||||
|
||||
def updatelog():
|
||||
if not self._update_log:
|
||||
self._load_update_log()
|
||||
|
||||
result = []
|
||||
for entry in self._update_log:
|
||||
line = "{datetime} - {display_name} - {current_version} -> {target_version} - {success} - {text}".format(
|
||||
datetime=entry["datetime"].replace("T", " "),
|
||||
display_name=entry["display_name"],
|
||||
current_version=entry["current_version"],
|
||||
target_version=entry["target_version"],
|
||||
success="SUCCESS" if entry["success"] else "FAILED",
|
||||
text=entry["text"]
|
||||
+ " (Release notes: {})".format(entry["release_notes"])
|
||||
if entry["release_notes"]
|
||||
else "",
|
||||
)
|
||||
result.append(line)
|
||||
return "\n".join(result)
|
||||
|
||||
return {
|
||||
os.path.basename(console_log): console_log,
|
||||
"plugin_softwareupdate_update.log": updatelog,
|
||||
}
|
||||
|
||||
def on_startup(self, host, port):
|
||||
console_logging_handler = logging.handlers.RotatingFileHandler(
|
||||
self._settings.get_plugin_logfile_path(postfix="console"),
|
||||
@@ -531,11 +562,14 @@ class SoftwareUpdatePlugin(
|
||||
datetime.datetime.now()
|
||||
- datetime.timedelta(minutes=self._settings.get_int(["updatelog_cutoff"]))
|
||||
).strftime(DATETIME_FORMAT)
|
||||
data = list(
|
||||
filter(
|
||||
lambda x: x.get("datetime") and x["datetime"] > cutoff,
|
||||
data,
|
||||
)
|
||||
data = sorted(
|
||||
list(
|
||||
filter(
|
||||
lambda x: x.get("datetime") and x["datetime"] > cutoff,
|
||||
data,
|
||||
)
|
||||
),
|
||||
key=lambda x: x["datetime"],
|
||||
)
|
||||
cleaned_up = len(data) - before_cleanup
|
||||
if cleaned_up:
|
||||
@@ -543,8 +577,9 @@ class SoftwareUpdatePlugin(
|
||||
"Cleaned up {} old update log entries".format(cleaned_up)
|
||||
)
|
||||
|
||||
self._update_log = sorted(data, key=lambda x: x["datetime"], reverse=True)
|
||||
self._update_log_dirty = False
|
||||
with self._update_log_mutex:
|
||||
self._update_log = data
|
||||
self._update_log_dirty = False
|
||||
|
||||
self._logger.info("Loaded update log from disk")
|
||||
|
||||
@@ -553,17 +588,18 @@ class SoftwareUpdatePlugin(
|
||||
|
||||
from octoprint.util import atomic_write
|
||||
|
||||
with atomic_write(
|
||||
self._update_log_path, mode="wt", max_permissions=0o666
|
||||
) as file_obj:
|
||||
yaml.safe_dump(
|
||||
self._update_log,
|
||||
stream=file_obj,
|
||||
default_flow_style=False,
|
||||
indent=2,
|
||||
allow_unicode=True,
|
||||
)
|
||||
self._update_log_dirty = False
|
||||
with self._update_log_mutex:
|
||||
with atomic_write(
|
||||
self._update_log_path, mode="wt", max_permissions=0o666
|
||||
) as file_obj:
|
||||
yaml.safe_dump(
|
||||
sorted(self._update_log, key=lambda x: x["datetime"]),
|
||||
stream=file_obj,
|
||||
default_flow_style=False,
|
||||
indent=2,
|
||||
allow_unicode=True,
|
||||
)
|
||||
self._update_log_dirty = False
|
||||
|
||||
self._logger.info("Saved update log to disk")
|
||||
|
||||
@@ -581,22 +617,23 @@ class SoftwareUpdatePlugin(
|
||||
if display_name is None:
|
||||
display_name = target
|
||||
|
||||
self._update_log.append(
|
||||
{
|
||||
"datetime": datetime.datetime.now().strftime(DATETIME_FORMAT),
|
||||
"target": target,
|
||||
"current_version": current_version,
|
||||
"target_version": target_version,
|
||||
"display_name": display_name,
|
||||
"release_notes": release_notes if release_notes else "",
|
||||
"success": success,
|
||||
"text": text,
|
||||
}
|
||||
)
|
||||
self._update_log_dirty = True
|
||||
with self._update_log_mutex:
|
||||
self._update_log.append(
|
||||
{
|
||||
"datetime": datetime.datetime.now().strftime(DATETIME_FORMAT),
|
||||
"target": target,
|
||||
"current_version": current_version,
|
||||
"target_version": target_version,
|
||||
"display_name": display_name,
|
||||
"release_notes": release_notes if release_notes else "",
|
||||
"success": success,
|
||||
"text": text,
|
||||
},
|
||||
)
|
||||
self._update_log_dirty = True
|
||||
|
||||
if save:
|
||||
self._save_update_log()
|
||||
if save:
|
||||
self._save_update_log()
|
||||
|
||||
# ~~ SettingsPlugin API
|
||||
|
||||
@@ -1027,7 +1064,17 @@ class SoftwareUpdatePlugin(
|
||||
@Permissions.ADMIN.require(403)
|
||||
def get_update_log(self):
|
||||
def view():
|
||||
return flask.jsonify(updatelog=self._update_log)
|
||||
return flask.jsonify(updatelog=list(reversed(self._update_log)))
|
||||
|
||||
def etag(lm=None):
|
||||
hash = hashlib.sha1()
|
||||
|
||||
def hash_update(value):
|
||||
value = value.encode("utf-8")
|
||||
hash.update(value)
|
||||
|
||||
hash_update(repr(self._update_log))
|
||||
return hash.hexdigest()
|
||||
|
||||
def lastmodified():
|
||||
if os.path.exists(self._update_log_path):
|
||||
@@ -1035,7 +1082,9 @@ class SoftwareUpdatePlugin(
|
||||
else:
|
||||
return 0
|
||||
|
||||
return with_revalidation_checking(lastmodified_factory=lastmodified)(view)()
|
||||
return with_revalidation_checking(
|
||||
etag_factory=etag, lastmodified_factory=lastmodified
|
||||
)(view)()
|
||||
|
||||
@octoprint.plugin.BlueprintPlugin.route("/check", methods=["GET"])
|
||||
@no_firstrun_access
|
||||
@@ -2347,4 +2396,5 @@ def __plugin_load__():
|
||||
"octoprint.cli.commands": cli.commands,
|
||||
"octoprint.events.register_custom_events": _register_custom_events,
|
||||
"octoprint.access.permissions": __plugin_implementation__.get_additional_permissions,
|
||||
"octoprint.systeminfo.additional_bundle_files": __plugin_implementation__.get_additional_bundle_files,
|
||||
}
|
||||
@@ -1571,6 +1571,7 @@ class SystemInfoBundleHandler(CorsSupportMixin, tornado.web.RequestHandler):
|
||||
from octoprint.server import (
|
||||
connectivityChecker,
|
||||
environmentDetector,
|
||||
pluginManager,
|
||||
printer,
|
||||
safe_mode,
|
||||
)
|
||||
@@ -1587,7 +1588,10 @@ class SystemInfoBundleHandler(CorsSupportMixin, tornado.web.RequestHandler):
|
||||
)
|
||||
|
||||
z = get_systeminfo_bundle(
|
||||
systeminfo, settings().getBaseFolder("logs"), printer=printer
|
||||
systeminfo,
|
||||
settings().getBaseFolder("logs"),
|
||||
printer=printer,
|
||||
plugin_manager=pluginManager,
|
||||
)
|
||||
|
||||
self.set_header("Content-Type", "application/zip")
|
||||
|
||||
Reference in new issue
Block a user