From 1cbc3229048a1a66c2a4249e966a4f34173fcb0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 17 Sep 2020 18:23:09 +0200 Subject: [PATCH 01/23] =?UTF-8?q?=F0=9F=90=9B=20Fix=20settings=20overlays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Defaults were not being merged --- src/octoprint/settings.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 2a3c9b36b..73b697dfe 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -475,7 +475,7 @@ class HierarchicalChainMap(ChainMap): except KeyError: return False - def get_by_path(self, path, only_local=False, only_defaults=False): + def get_by_path(self, path, only_local=False, only_defaults=False, merged=False): if only_defaults: current = self.parents elif only_local: @@ -490,6 +490,8 @@ class HierarchicalChainMap(ChainMap): else: raise KeyError(key) + if merged: + current = current.deep_dict() return current[path[-1]] def set_by_path(self, path, value): @@ -1448,7 +1450,7 @@ class Settings(object): if isinstance(value, dict) and merged: try: - default_value = chain.get_by_path(parent_path + [key], only_defaults=True) + default_value = chain.get_by_path(parent_path + [key], only_defaults=True, merged=True) if default_value is not None: value = dict_merge(default_value, value) except KeyError: From 250842e561290837d3ce60977ccf8cb5bbaaa73a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 17 Sep 2020 18:23:51 +0200 Subject: [PATCH 02/23] =?UTF-8?q?=E2=9C=A8=20Support=20removal=20of=20sett?= =?UTF-8?q?ings=20overlays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/settings.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 73b697dfe..44d26a480 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -592,6 +592,8 @@ class Settings(object): However, these would be invalid paths: ``["key"]``, ``["serial", "port", "value"]``, ``["server", "host", 3]``. """ + OVERLAY_KEY = "__overlay__" + def __init__(self, configfile=None, basedir=None): self._logger = logging.getLogger(__name__) @@ -939,13 +941,36 @@ class Settings(object): self._migrate_config(config) return config - def add_overlay(self, overlay, at_end=False): + def add_overlay(self, overlay, at_end=False, key=None): + assert isinstance(overlay, dict) + + if key is None: + overlay_yaml = yaml.safe_dump(overlay) + import hashlib + hash = hashlib.md5() + hash.update(overlay_yaml.encode('utf-8')) + key = hash.hexdigest() + + overlay[self.OVERLAY_KEY] = key if at_end: pos = len(self._map.maps) - 1 self._map.maps.insert(pos, overlay) else: self._map.maps.insert(1, overlay) + return key + + def remove_overlay(self, key): + index = -1 + for i, overlay in enumerate(self._overlay_maps): + if key == overlay.get(self.OVERLAY_KEY): + index = i + + if index > -1: + del self._map.maps[index + 1] + return True + return False + def _migrate_config(self, config=None, persist=False): if config is None: config = self._config From 40c21acf38d8c7fcc6e4e65818e0a98c62c768be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 18 Sep 2020 12:37:12 +0200 Subject: [PATCH 03/23] =?UTF-8?q?=E2=9C=A8=20Support=20settings=20overlays?= =?UTF-8?q?=20from=20plugins?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/plugin/__init__.py | 49 +++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/src/octoprint/plugin/__init__.py b/src/octoprint/plugin/__init__.py index 69ff0c79d..57df7f92f 100644 --- a/src/octoprint/plugin/__init__.py +++ b/src/octoprint/plugin/__init__.py @@ -318,6 +318,27 @@ class PluginSettings(object): :type force: boolean :param trigger_event: Trigger the ``SettingsUpdated`` :ref:`event ` on save. :type trigger_event: boolean + + .. method:: add_overlay(overlay, at_end=False, key=None) + + Adds a new config overlay for the plugin's settings. Will return the overlay's key in the map. + + :param overlay: Overlay dict to add + :type overlay: dict + :param at_end: Whether to add overlay at end or start (default) of config hierarchy + :type at_end: boolean + :param key: Key to use to identify overlay. If not set one will be built based on the overlay's hash + :type key: str + :rtype: str + + .. method:: remove_overlay(key) + + Removes an overlay from the settings based on its key. Return ``True`` if the overlay could be found and was + removed, ``False`` otherwise. + + :param key: The key of the overlay to remove + :type key: str + :rtype: boolean """ def __init__(self, settings, plugin_key, defaults=None, get_preprocessors=None, set_preprocessors=None): @@ -368,17 +389,25 @@ class PluginSettings(object): kwargs.update(preprocessors=self.set_preprocessors) return kwargs + def wrap_overlay(args): + result = list(args) + overlay = result[0] + result[0] = dict(plugins={plugin_key: overlay}) + return result + self.access_methods = dict( - has =("has", prefix_path_in_args, add_getter_kwargs), - get =("get", prefix_path_in_args, add_getter_kwargs), - get_int =("getInt", prefix_path_in_args, add_getter_kwargs), - get_float =("getFloat", prefix_path_in_args, add_getter_kwargs), - get_boolean=("getBoolean", prefix_path_in_args, add_getter_kwargs), - set =("set", prefix_path_in_args, add_setter_kwargs), - set_int =("setInt", prefix_path_in_args, add_setter_kwargs), - set_float =("setFloat", prefix_path_in_args, add_setter_kwargs), - set_boolean=("setBoolean", prefix_path_in_args, add_setter_kwargs), - remove =("remove", prefix_path_in_args, lambda x: x) + has = ("has", prefix_path_in_args, add_getter_kwargs), + get = ("get", prefix_path_in_args, add_getter_kwargs), + get_int = ("getInt", prefix_path_in_args, add_getter_kwargs), + get_float = ("getFloat", prefix_path_in_args, add_getter_kwargs), + get_boolean = ("getBoolean", prefix_path_in_args, add_getter_kwargs), + set = ("set", prefix_path_in_args, add_setter_kwargs), + set_int = ("setInt", prefix_path_in_args, add_setter_kwargs), + set_float = ("setFloat", prefix_path_in_args, add_setter_kwargs), + set_boolean = ("setBoolean", prefix_path_in_args, add_setter_kwargs), + remove = ("remove", prefix_path_in_args, lambda x: x), + add_overlay = ("add_overlay", wrap_overlay, lambda x: x), + remove_overlay = ("remove_overlay", lambda x: x, lambda x: x) ) self.deprecated_access_methods = dict( getInt ="get_int", From b8294f2cb7708e35b9b88ffef5590a1eec9fd2b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 18 Sep 2020 15:20:34 +0200 Subject: [PATCH 04/23] =?UTF-8?q?=E2=9C=A8=20SWU:=20Support=20update=20che?= =?UTF-8?q?ck=20overlay=20from=20plugin=20repo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the future this will allow us to push changes to the update endpoints straight to people's instances without having to ask them to manually switch over to a new version of the plugin hosted elsewhere. Should make future maintainer changes way easier, but also accidentally misconfigured plugins can be fixed this way and even OctoPrint core. --- .../plugins/softwareupdate/__init__.py | 71 ++++++++++++++++++- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/src/octoprint/plugins/softwareupdate/__init__.py b/src/octoprint/plugins/softwareupdate/__init__.py index d3a4ade7e..ae2a3bd99 100644 --- a/src/octoprint/plugins/softwareupdate/__init__.py +++ b/src/octoprint/plugins/softwareupdate/__init__.py @@ -17,6 +17,7 @@ import time import logging import logging.handlers import hashlib +import requests # noinspection PyCompatibility from concurrent import futures @@ -58,6 +59,8 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, DATA_FORMAT_VERSION = "v4" + CHECK_OVERLAY_KEY = "softwareupdate_check_overlay" + # noinspection PyMissingConstructor def __init__(self): self._update_in_progress = False @@ -75,6 +78,10 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, self._version_cache_dirty = False self._version_cache_timestamp = None + self._overlay_cache = dict() + self._overlay_cache_ttl = 0 + self._overlay_cache_timestamp = None + self._environment_supported = True self._environment_versions = dict() self._environment_ready = threading.Event() @@ -93,6 +100,8 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, self._version_cache_path = os.path.join(self.get_plugin_data_folder(), "versioncache.yaml") self._load_version_cache() + self._overlay_cache_ttl = self._settings.get_int(["check_overlay_ttl"]) * 60 + def refresh_checks(name, plugin): self._refresh_configured_checks = True self._send_client_message("update_versions") @@ -147,8 +156,13 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, def _get_configured_checks(self): with self._configured_checks_mutex: - if self._refresh_configured_checks or self._configured_checks is None: + if self._refresh_configured_checks \ + or self._configured_checks is None \ + or self._check_overlays_stale: + self._refresh_configured_checks = False + + overlays = self._get_check_overlays() self._configured_checks = self._settings.get(["checks"], merged=True) update_check_hooks = self._plugin_manager.get_hooks("octoprint.plugin.softwareupdate.check_config") @@ -174,6 +188,10 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, check_providers[key] = name + if key in overlays: + overlay_config = overlays[key] + default_config = dict_merge(default_config, overlay_config) + yaml_config = dict() effective_config = default_config if key in self._configured_checks: @@ -269,6 +287,45 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, if self._storage_sufficient else "insufficient")) + @property + def _check_overlays_stale(self): + return self._overlay_cache is None or self._overlay_cache_timestamp is None \ + or self._overlay_cache_timestamp + self._overlay_cache_ttl < time.time() + + def _get_check_overlays(self, force=False): + if self._check_overlays_stale or force: + if self._connectivity_checker.online: + # reload from URL + url = self._settings.get(["check_overlay_url"]) + self._logger.info("Fetching check overlays from {}".format(url)) + try: + r = requests.get(url, timeout=3.1) + r.raise_for_status() + data = r.json() + except Exception as exc: + self._logger.error("Could not fetch check overlay from {}: {}".format(url, exc)) + self._overlay_cache = dict() + else: + self._overlay_cache = data + + else: + self._logger.info("Not fetching check overlays, we are offline") + self._overlay_cache = dict() + + self._overlay_cache_timestamp = time.time() + + default_overlay = dict() + defaults = self.get_settings_defaults() + for key in defaults["checks"]: + if key in self._overlay_cache: + default_overlay[key] = self._overlay_cache[key] + + self._settings.remove_overlay(self.CHECK_OVERLAY_KEY) + if default_overlay: + self._settings.add_overlay(dict(checks=default_overlay), key=self.CHECK_OVERLAY_KEY) + + return self._overlay_cache + def _load_version_cache(self): if not os.path.isfile(self._version_cache_path): return @@ -362,7 +419,11 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, "ignore_throttled": False, - "minimum_free_storage": 150 + "minimum_free_storage": 150, + + "check_overlay_url": "https://plugins.octoprint.org/update_check_overlay.json", + + "check_overlay_ttl": 6 * 60, } def on_settings_load(self): @@ -704,6 +765,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, hash_update(str(self._environment_supported)) hash_update(str(self._version_cache_timestamp)) + hash_update(str(self._overlay_cache_timestamp)) hash_update(str(self._connectivity_checker.online)) hash_update(str(self._update_in_progress)) hash_update(self.DATA_FORMAT_VERSION) @@ -714,7 +776,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, return with_revalidation_checking(etag_factory=lambda *args, **kwargs: etag(), condition=lambda *args, **kwargs: condition(), - unless=lambda: force)(view)() + unless=lambda: force or self._check_overlays_stale)(view)() @octoprint.plugin.BlueprintPlugin.route("/update", methods=["POST"]) @@ -858,6 +920,9 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, :param check_targets: an iterable defining the targets to check, if not supplied defaults to all targets """ + if force: + self._get_check_overlays(force=True) + checks = self._get_configured_checks() if check_targets is None: check_targets = list(checks.keys()) From cac1e4b0bd78bd6b4c0e5ff394b960a67f6d3e7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 21 Sep 2020 13:07:54 +0200 Subject: [PATCH 05/23] =?UTF-8?q?=F0=9F=90=9B=20PMGR:=20Ensure=20log=20not?= =?UTF-8?q?ification=20is=20actually=20visible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Caused by a change while analysing/solving #3729 --- .../plugins/pluginmanager/static/js/pluginmanager.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js index 39d8b087c..3b27a9409 100644 --- a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js +++ b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js @@ -1049,6 +1049,11 @@ $(function() { self.logNotification.update(options); } } + + // make sure the notification is visible + if (self.logNotification.state !== 'open' && self.logNotification.state !== 'opening') { + self.logNotification.open(); + } }; self._markWorking = function(title, line) { From dd78fd3ed30580db366560addcab63b10be704b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 21 Sep 2020 14:11:06 +0200 Subject: [PATCH 06/23] =?UTF-8?q?=F0=9F=9A=B8=20Clarify=20tool=20selection?= =?UTF-8?q?=20purpose=20&=20limitations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #3738 --- src/octoprint/templates/tabs/control.jinja2 | 23 ++++++++++++--------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/octoprint/templates/tabs/control.jinja2 b/src/octoprint/templates/tabs/control.jinja2 index 0b8c80065..80d1cf1b3 100644 --- a/src/octoprint/templates/tabs/control.jinja2 +++ b/src/octoprint/templates/tabs/control.jinja2 @@ -95,24 +95,27 @@