✨ SWU: Update capability for single file plugins
Also added a bunch of new version checks for that usecase.
This commit is contained in:
1 parent
37526800c1
commit
31e98b8949
8 files changed
+315
-38
No files matched your search
@@ -52,6 +52,9 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin,
|
||||
octoprint.plugin.EventHandlerPlugin):
|
||||
|
||||
COMMIT_TRACKING_TYPES = ("github_commit", "bitbucket_commit")
|
||||
CURRENT_TRACKING_TYPES = COMMIT_TRACKING_TYPES + ("etag", "lastmodified", "jsondata")
|
||||
|
||||
OCTOPRINT_RESTART_TYPES = ("pip", "single_file_plugin")
|
||||
|
||||
DATA_FORMAT_VERSION = "v4"
|
||||
|
||||
@@ -172,7 +175,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin,
|
||||
# This used to be part of the settings migration (version 2) due to a bug - it can't
|
||||
# stay there though since it interferes with manual entries to the checks not
|
||||
# originating from within a plugin. Hence we do that step now here.
|
||||
if "type" not in effective_config or effective_config["type"] not in self.COMMIT_TRACKING_TYPES:
|
||||
if "type" not in effective_config or effective_config["type"] not in self.CURRENT_TRACKING_TYPES:
|
||||
deletables = ["current", "displayVersion"]
|
||||
else:
|
||||
deletables = []
|
||||
@@ -507,7 +510,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin,
|
||||
configured_checks = self._settings.get(["checks"], incl_defaults=False)
|
||||
if configured_checks is not None and "octoprint" in configured_checks:
|
||||
octoprint_check = dict(configured_checks["octoprint"])
|
||||
if "type" not in octoprint_check or octoprint_check["type"] not in self.COMMIT_TRACKING_TYPES:
|
||||
if "type" not in octoprint_check or octoprint_check["type"] not in self.CURRENT_TRACKING_TYPES:
|
||||
deletables=["current", "displayName", "displayVersion"]
|
||||
else:
|
||||
deletables=[]
|
||||
@@ -860,8 +863,13 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin,
|
||||
try:
|
||||
version_checker = self._get_version_checker(target, check)
|
||||
information, is_current = version_checker.get_latest(target, check, online=online)
|
||||
if information is not None and not is_current:
|
||||
update_available = True
|
||||
if information is not None:
|
||||
if is_current and check["type"] in self.CURRENT_TRACKING_TYPES and check["current"] is None:
|
||||
self._persist_check_current(target, check, information["remote"]["value"])
|
||||
del self._version_cache[target]
|
||||
self._version_cache_dirty = True
|
||||
elif not is_current:
|
||||
update_available = True
|
||||
except exceptions.CannotCheckOffline:
|
||||
update_possible = False
|
||||
information["needs_online"] = True
|
||||
@@ -882,6 +890,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin,
|
||||
updater = self._get_updater(target, check)
|
||||
update_possible = updater.can_perform_update(target, check, online=online)
|
||||
except Exception:
|
||||
self._logger.exception("Error while checking if {} can be updated".format(target))
|
||||
update_possible = False
|
||||
|
||||
self._version_cache[target] = dict(timestamp=time.time(),
|
||||
@@ -966,7 +975,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin,
|
||||
|
||||
if "restart" in check:
|
||||
target_restart_type = check["restart"]
|
||||
elif "pip" in check:
|
||||
elif "pip" in check or check.get("method") in self.OCTOPRINT_RESTART_TYPES:
|
||||
target_restart_type = "octoprint"
|
||||
else:
|
||||
target_restart_type = None
|
||||
@@ -1090,21 +1099,30 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin,
|
||||
self._settings.load()
|
||||
|
||||
# persist the new version if necessary for check type
|
||||
if check["type"] in self.COMMIT_TRACKING_TYPES:
|
||||
dummy_default = dict(plugins=dict())
|
||||
dummy_default["plugins"][self._identifier] = dict(checks=dict())
|
||||
dummy_default["plugins"][self._identifier]["checks"][target] = dict(current=None)
|
||||
self._settings.set(["checks", target, "current"], target_version, defaults=dummy_default)
|
||||
|
||||
# we have to save here (even though that makes us save quite often) since otherwise the next
|
||||
# load will overwrite our changes we just made
|
||||
self._settings.save()
|
||||
self._persist_check_current(target, check, target_version)
|
||||
|
||||
del self._version_cache[target]
|
||||
self._version_cache_dirty = True
|
||||
|
||||
return target_error, target_result
|
||||
|
||||
def _persist_check_current(self, target, check, current):
|
||||
if check["type"] not in self.CURRENT_TRACKING_TYPES:
|
||||
return
|
||||
|
||||
self._settings.load()
|
||||
|
||||
dummy_default = dict(plugins=dict())
|
||||
dummy_default["plugins"][self._identifier] = dict(checks=dict())
|
||||
dummy_default["plugins"][self._identifier]["checks"][target] = dict(current=None)
|
||||
self._settings.set(["checks", target, "current"], current, defaults=dummy_default)
|
||||
|
||||
# we have to save here (even though that makes us save quite often) since otherwise the next
|
||||
# load will overwrite our changes we just made
|
||||
self._settings.save()
|
||||
|
||||
check["current"] = current
|
||||
|
||||
def _perform_restart(self, restart_command):
|
||||
"""
|
||||
Performs a restart using the supplied restart_command.
|
||||
@@ -1214,7 +1232,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin,
|
||||
# displayVersion AND current missing or None
|
||||
result["displayVersion"] = "unknown"
|
||||
|
||||
if check["type"] in self.COMMIT_TRACKING_TYPES:
|
||||
if check["type"] in self.CURRENT_TRACKING_TYPES:
|
||||
result["current"] = check.get("current", None)
|
||||
else:
|
||||
result["current"] = check.get("current", check.get("displayVersion", None))
|
||||
@@ -1285,13 +1303,15 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin,
|
||||
cannot be determined.
|
||||
"""
|
||||
|
||||
mapping = dict(update_script=updaters.update_script,
|
||||
pip=updaters.pip,
|
||||
python_updater=updaters.python_updater,
|
||||
sleep_a_bit=updaters.sleep_a_bit)
|
||||
method = self._get_update_method(target, check)
|
||||
if method is None:
|
||||
raise exceptions.UnknownUpdateType()
|
||||
|
||||
method = self._get_update_method(target, check, valid_methods=list(mapping.keys()))
|
||||
return mapping[method]
|
||||
updater = getattr(updaters, method)
|
||||
if updater is None:
|
||||
raise exceptions.UnknownUpdateType()
|
||||
|
||||
return updater
|
||||
|
||||
def _get_octoprint_checkout_folder(self, checks=None):
|
||||
if checks is None:
|
||||
|
||||
@@ -5,4 +5,4 @@ __author__ = "Gina Häußge <osd@foosel.net>"
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
from . import pip, python_updater, update_script, sleep_a_bit
|
||||
from . import pip, python_updater, update_script, sleep_a_bit, single_file_plugin
|
||||
@@ -0,0 +1,75 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2020 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
import tempfile
|
||||
import logging
|
||||
import shutil
|
||||
import os
|
||||
import ast
|
||||
import io
|
||||
|
||||
from octoprint.util.net import download_file
|
||||
from octoprint.settings import settings
|
||||
from .. import exceptions
|
||||
|
||||
logger = logging.getLogger("octoprint.plugins.softwareupdate.updaters.download_single_file_plugin")
|
||||
|
||||
def can_perform_update(target, check, online=True):
|
||||
return online or check.get("offline", False)
|
||||
|
||||
def perform_update(target, check, target_version, log_cb=None, online=True, force=False):
|
||||
if not online and not check.get("offline", False):
|
||||
raise exceptions.CannotUpdateOffline()
|
||||
|
||||
url = check.get("url")
|
||||
if url is None:
|
||||
raise exceptions.ConfigurationInvalid("download_single_file_plugin updater needs url set")
|
||||
|
||||
def _log_call(*lines):
|
||||
_log(lines, prefix=" ", stream="call")
|
||||
|
||||
def _log_message(*lines):
|
||||
_log(lines, prefix="#", stream="message")
|
||||
|
||||
def _log(lines, prefix=None, stream=None):
|
||||
if log_cb is None:
|
||||
return
|
||||
log_cb(lines, prefix=prefix, stream=stream)
|
||||
|
||||
folder = None
|
||||
try:
|
||||
try:
|
||||
_log_message("Download file from {}".format(url))
|
||||
folder = tempfile.TemporaryDirectory()
|
||||
path = download_file(url, folder.name)
|
||||
except Exception as exc:
|
||||
raise exceptions.NetworkError(cause=exc)
|
||||
|
||||
filename = os.path.basename(path)
|
||||
_, ext = os.path.splitext(filename)
|
||||
if ext not in (".py",):
|
||||
raise exceptions.UpdateError("File is not a python file: {}".format(filename), None)
|
||||
|
||||
try:
|
||||
with io.open(path, 'rb') as f:
|
||||
ast.parse(f.read(), filename=path)
|
||||
except Exception:
|
||||
logger.exception("Could not parse {} as python file".format(path), None)
|
||||
raise exceptions.UpdateError("Could not parse {} as python file.".format(filename), None)
|
||||
|
||||
destination = os.path.join(settings().getBaseFolder("plugins"), filename)
|
||||
|
||||
try:
|
||||
_log_message("Copy {} to {}".format(path, destination))
|
||||
shutil.copy(path, destination)
|
||||
except Exception:
|
||||
logger.exception("Could not copy {} to {}".format(path, destination))
|
||||
raise exceptions.UpdateError("Could not copy {} to {}".format(path, destination), None)
|
||||
|
||||
return "ok"
|
||||
finally:
|
||||
if folder is not None:
|
||||
folder.cleanup()
|
||||
@@ -5,7 +5,8 @@ __author__ = "Gina Häußge <osd@foosel.net>"
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
from . import commandline, git_commit, github_commit, github_release, bitbucket_commit, python_checker, never_current, always_current, pypi_release
|
||||
from . import commandline, git_commit, github_commit, github_release, bitbucket_commit, \
|
||||
python_checker, never_current, always_current, pypi_release, etag, lastmodified, jsondata
|
||||
|
||||
def log_github_ratelimit(logger, r):
|
||||
ratelimit = r.headers["X-RateLimit-Limit"] if "X-RateLimit-Limit" in r.headers else "?"
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2020 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
import requests
|
||||
import logging
|
||||
|
||||
from ..exceptions import ConfigurationInvalid, NetworkError, CannotCheckOffline
|
||||
|
||||
logger = logging.getLogger("octoprint.plugins.softwareupdate.version_checks.etag")
|
||||
|
||||
def get_latest(target, check, online=True):
|
||||
if not online:
|
||||
raise CannotCheckOffline()
|
||||
|
||||
url = check.get("etag_url", check.get("url"))
|
||||
current = check.get("current")
|
||||
|
||||
if url is None:
|
||||
raise ConfigurationInvalid("ETag version check needs etag_url or url set")
|
||||
|
||||
try:
|
||||
with requests.head(url) as r:
|
||||
latest = r.headers.get("Etag")
|
||||
except Exception as exc:
|
||||
raise NetworkError(cause=exc)
|
||||
|
||||
information = dict(local=dict(name="ETag {}".format(current if current else "-"), value=current),
|
||||
remote=dict(name="ETag {}".format(latest if latest else "-"), value=latest))
|
||||
|
||||
logger.debug("Target: {}, local: {}, remote: {}".format(target, information["local"]["name"], information["remote"]["name"]))
|
||||
|
||||
return information, current is None or current == latest or latest is None
|
||||
@@ -0,0 +1,37 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2020 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
import requests
|
||||
import logging
|
||||
|
||||
from ..exceptions import ConfigurationInvalid, NetworkError, CannotCheckOffline
|
||||
|
||||
logger = logging.getLogger("octoprint.plugins.softwareupdate.version_checks.jsondata")
|
||||
|
||||
def get_latest(target, check, online=True):
|
||||
if not online:
|
||||
raise CannotCheckOffline()
|
||||
|
||||
url = check.get("jsondata")
|
||||
current = check.get("current")
|
||||
|
||||
if url is None:
|
||||
raise ConfigurationInvalid("jsondata version check needs jsondata set")
|
||||
|
||||
try:
|
||||
with requests.get(url) as r:
|
||||
data = r.json()
|
||||
except Exception as exc:
|
||||
raise NetworkError(cause=exc)
|
||||
|
||||
latest = data.get("version")
|
||||
|
||||
information = dict(local=dict(name=current if current else "-", value=current),
|
||||
remote=dict(name=latest if latest else "-", value=latest))
|
||||
|
||||
logger.debug("Target: {}, local: {}, remote: {}".format(target, information["local"]["name"], information["remote"]["name"]))
|
||||
|
||||
return information, current is None or current == latest or latest is None
|
||||
@@ -0,0 +1,35 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2020 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
import requests
|
||||
import logging
|
||||
|
||||
from ..exceptions import ConfigurationInvalid, NetworkError, CannotCheckOffline
|
||||
|
||||
logger = logging.getLogger("octoprint.plugins.softwareupdate.version_checks.etag")
|
||||
|
||||
def get_latest(target, check, online=True):
|
||||
if not online:
|
||||
raise CannotCheckOffline()
|
||||
|
||||
url = check.get("lastmodified_url", check.get("url"))
|
||||
current = check.get("current")
|
||||
|
||||
if url is None:
|
||||
raise ConfigurationInvalid("Last-Modified version check needs either lastmodified_url or url set")
|
||||
|
||||
try:
|
||||
with requests.head(url) as r:
|
||||
latest = r.headers.get("Last-Modified")
|
||||
except Exception as exc:
|
||||
raise NetworkError(cause=exc)
|
||||
|
||||
information = dict(local=dict(name="Last-Modified {}".format(current if current else "-"), value=current),
|
||||
remote=dict(name="Last-Modified {}".format(latest if latest else "-"), value=latest))
|
||||
|
||||
logger.debug("Target: {}, local: {}, remote: {}".format(target, information["local"]["name"], information["remote"]["name"]))
|
||||
|
||||
return information, current is None or current == latest or latest is None
|
||||
Reference in new issue
Block a user