✨ Introduce downloadable systeminfo bundle
This commit is contained in:
1 parent
a0bfa0fe93
commit
9041561576
4 files changed
+162
-2
No files matched your search
@@ -62,6 +62,7 @@ INSTALL_REQUIRES = [
|
||||
"frozendict>=1.2,<2",
|
||||
"sentry-sdk>=0.15.1,<1",
|
||||
"filetype>=1.0.7,<2",
|
||||
"zipstream-new>=1.1.8,<1.2",
|
||||
# vendor bundled dependencies
|
||||
"unidecode>=0.04.14,<0.05", # dependency of awesome-slugify
|
||||
"blinker>=1.4,<2", # dependency of flask_principal
|
||||
|
||||
@@ -720,6 +720,11 @@ class Server(object):
|
||||
app, util.flask.permission_validator, permissions.Permissions.WEBCAM
|
||||
),
|
||||
] + access_validators_from_plugins
|
||||
systeminfo_validators = [
|
||||
util.tornado.access_validation_factory(
|
||||
app, util.flask.permission_validator, permissions.Permissions.SYSTEM
|
||||
)
|
||||
] + access_validators_from_plugins
|
||||
|
||||
timelapse_permission_validator = {
|
||||
"access_validation": util.tornado.validation_chain(*timelapse_validators)
|
||||
@@ -733,6 +738,9 @@ class Server(object):
|
||||
camera_permission_validator = {
|
||||
"access_validation": util.tornado.validation_chain(*camera_validators)
|
||||
}
|
||||
systeminfo_permission_validator = {
|
||||
"access_validation": util.tornado.validation_chain(*systeminfo_validators)
|
||||
}
|
||||
|
||||
no_hidden_files_validator = {
|
||||
"path_validation": util.tornado.path_validation_factory(
|
||||
@@ -772,6 +780,7 @@ class Server(object):
|
||||
timelapse_path_validator,
|
||||
),
|
||||
),
|
||||
# uploaded printables
|
||||
(
|
||||
r"/downloads/files/local/(.*)",
|
||||
util.tornado.LargeResponseHandler,
|
||||
@@ -787,6 +796,7 @@ class Server(object):
|
||||
additional_mime_types,
|
||||
),
|
||||
),
|
||||
# log files
|
||||
(
|
||||
r"/downloads/logs/([^/]*)",
|
||||
util.tornado.LargeResponseHandler,
|
||||
@@ -800,6 +810,12 @@ class Server(object):
|
||||
log_permission_validator,
|
||||
),
|
||||
),
|
||||
# system info bundle
|
||||
(
|
||||
r"/downloads/systeminfo.zip",
|
||||
util.tornado.SystemInfoBundleHandler,
|
||||
systeminfo_permission_validator,
|
||||
),
|
||||
# camera snapshot
|
||||
(
|
||||
r"/downloads/camera/current",
|
||||
|
||||
@@ -22,7 +22,7 @@ import tornado.iostream
|
||||
import tornado.tcpserver
|
||||
import tornado.util
|
||||
import tornado.web
|
||||
from past.builtins import unicode
|
||||
from past.builtins import basestring, unicode
|
||||
|
||||
import octoprint.util
|
||||
|
||||
@@ -1402,6 +1402,146 @@ class DeprecatedEndpointHandler(CorsSupportMixin, tornado.web.RequestHandler):
|
||||
options = _handle_method
|
||||
|
||||
|
||||
class StaticZipBundleHandler(CorsSupportMixin, tornado.web.RequestHandler):
|
||||
def initialize(
|
||||
self, files=None, as_attachment=True, attachment_name=None, access_validation=None
|
||||
):
|
||||
if files is None:
|
||||
files = []
|
||||
if as_attachment and not attachment_name:
|
||||
raise ValueError("attachment name must be set if as_attachment is True")
|
||||
|
||||
self._files = files
|
||||
self._as_attachment = as_attachment
|
||||
self._attachment_name = attachment_name
|
||||
self._access_validator = access_validation
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
if self._access_validator is not None:
|
||||
self._access_validator(self.request)
|
||||
return self.stream_zip(self._files)
|
||||
|
||||
def get_attachment_name(self):
|
||||
return self._attachment_name
|
||||
|
||||
# noinspection PyCompatibility
|
||||
def normalize_files(self, files):
|
||||
result = []
|
||||
for f in files:
|
||||
if isinstance(f, basestring):
|
||||
result.append({"path": f})
|
||||
elif isinstance(f, dict) and ("path" in f or "iter" in f or "content" in f):
|
||||
result.append(f)
|
||||
return result
|
||||
|
||||
@tornado.gen.coroutine
|
||||
def stream_zip(self, files):
|
||||
self.set_header("Content-Type", "application/zip")
|
||||
if self._as_attachment:
|
||||
self.set_header(
|
||||
"Content-Disposition",
|
||||
'attachment; filename="{}"'.format(self.get_attachment_name()),
|
||||
)
|
||||
|
||||
import zipstream
|
||||
|
||||
z = zipstream.ZipFile()
|
||||
for f in self.normalize_files(files):
|
||||
# noinspection PyCompatibility
|
||||
name = f.get("name")
|
||||
path = f.get("path")
|
||||
iter = f.get("iter")
|
||||
content = f.get("content")
|
||||
|
||||
if path:
|
||||
z.write(path, arcname=name, compress_type=zipstream.ZIP_STORED)
|
||||
elif iter and name:
|
||||
z.write_iter(name, iter, compress_type=zipstream.ZIP_STORED)
|
||||
elif content and name:
|
||||
z.writestr(name, content, compress_type=zipstream.ZIP_STORED)
|
||||
|
||||
for chunk in z:
|
||||
try:
|
||||
self.write(chunk)
|
||||
yield self.flush()
|
||||
except tornado.iostream.StreamClosedError:
|
||||
return
|
||||
|
||||
|
||||
class SystemInfoBundleHandler(StaticZipBundleHandler):
|
||||
# noinspection PyMethodOverriding
|
||||
def initialize(self, access_validation=None):
|
||||
self._as_attachment = True
|
||||
self._attachment_name = "octoprint-systeminfo-{datetime}.zip"
|
||||
self._access_validator = access_validation
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
if self._access_validator is not None:
|
||||
self._access_validator(self.request)
|
||||
|
||||
from octoprint.cli.systeminfo import get_systeminfo
|
||||
from octoprint.server import (
|
||||
connectivityChecker,
|
||||
environmentDetector,
|
||||
printer,
|
||||
safe_mode,
|
||||
)
|
||||
from octoprint.settings import settings
|
||||
from octoprint.util import dict_flatten
|
||||
|
||||
systeminfo = get_systeminfo(environmentDetector, connectivityChecker)
|
||||
systeminfo["browser.user_agent"] = self.request.headers.get("User-Agent")
|
||||
systeminfo["octoprint.safe_mode"] = safe_mode is not None
|
||||
|
||||
terminaltxt = None
|
||||
if printer and printer.is_operational():
|
||||
firmware_info = printer.firmware_info
|
||||
if firmware_info:
|
||||
systeminfo.update(
|
||||
dict_flatten({"firmware": firmware_info["name"]}, prefix="printer")
|
||||
)
|
||||
|
||||
if hasattr(printer, "_log"):
|
||||
terminaltxt = list(printer._log)
|
||||
|
||||
logbase = settings().getBaseFolder("logs")
|
||||
|
||||
systeminfotxt = []
|
||||
for k in sorted(systeminfo.keys()):
|
||||
systeminfotxt.append("{}: {}".format(k, systeminfo[k]))
|
||||
|
||||
files = [
|
||||
{
|
||||
"name": "systeminfo.txt",
|
||||
"content": octoprint.util.to_bytes("\n".join(systeminfotxt)),
|
||||
},
|
||||
{"path": os.path.join(logbase, "octoprint.log"), "name": "octoprint.log"},
|
||||
{"path": os.path.join(logbase, "serial.log"), "name": "serial.log"},
|
||||
{
|
||||
"path": os.path.join(logbase, "plugin_softwareupdate_console.log"),
|
||||
"name": "plugin_softwareupdate_console.log",
|
||||
},
|
||||
{
|
||||
"path": os.path.join(logbase, "plugin_pluginmanager_console.log"),
|
||||
"name": "plugin_pluginmanager_console.log",
|
||||
},
|
||||
]
|
||||
if terminaltxt is not None:
|
||||
files.append(
|
||||
{
|
||||
"name": "terminal.txt",
|
||||
"content": octoprint.util.to_bytes("\n".join(terminaltxt)),
|
||||
}
|
||||
)
|
||||
|
||||
return self.stream_zip(files)
|
||||
|
||||
def get_attachment_name(self):
|
||||
import time
|
||||
|
||||
return "octoprint-systeminfo-{}.zip".format(time.strftime("%Y%m%d%H%M%S"))
|
||||
|
||||
|
||||
class GlobalHeaderTransform(tornado.web.OutputTransform):
|
||||
|
||||
HEADERS = {}
|
||||
|
||||
@@ -7,4 +7,7 @@
|
||||
<strong data-bind="text: key"></strong>: <span data-bind="text: value"></span><br />
|
||||
</p>
|
||||
|
||||
<a href="javascript:void(0)" data-bind="click: copySystemInfo"><i class="far fa-copy"></i> {{ _('Copy') }}</a>
|
||||
|
||||
<a href="{{ url_for('index', _external=True) }}downloads/systeminfo.zip" class="btn btn-primary"><i class="fas fa-download"></i> {{ _('Download Systeminfo Bundle') }}</a>
|
||||
|
||||
<a href="javascript:void(0)" class="btn" data-bind="click: copySystemInfo"><i class="far fa-copy"></i> {{ _('Copy') }}</a>
|
||||
Reference in new issue
Block a user