Create a backup of working config.yaml after startup

This commit is contained in:
Gina Häußge committed 2018-11-22 13:47:02 +01:00
1 parent 92629468c0
commit 170804d4df
3 files changed
+21 -5

No files matched your search

+4 -1
View File
@@ -8,6 +8,8 @@ __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms
from .exceptions import ScriptError
import logging
def execute(command, cwd=None, evaluate_returncode=True, **kwargs):
do_async = kwargs.get("do_async", kwargs.get("async", False))
@@ -16,8 +18,9 @@ def execute(command, cwd=None, evaluate_returncode=True, **kwargs):
p = None
try:
p = sarge.run(command, cwd=cwd, stdout=sarge.Capture(), stderr=sarge.Capture(), async_=do_async)
p = sarge.run(command, cwd=cwd, stdout=sarge.Capture(), stderr=sarge.Capture(), async=do_async)
except:
logging.getLogger(__name__).exception("Error while executing command: {}".format(command))
returncode = p.returncode if p is not None else None
stdout = p.stdout.text if p is not None and p.stdout is not None else ""
stderr = p.stderr.text if p is not None and p.stderr is not None else ""
+3
View File
@@ -711,6 +711,9 @@ class Server(object):
self._settings.setBoolean(["server", "startOnceInSafeMode"], False)
self._settings.save()
# make a backup of the current config
self._settings.backup(ext="backup")
# now this is somewhat ugly, but the issue is the following: startup plugins might want to do things for
# which they need the server to be already alive (e.g. for being able to resolve urls, such as favicons
# or service xmls or the like). While they are working though the ioloop would block. Therefore we'll
+14 -4
View File
@@ -1309,15 +1309,25 @@ class Settings(object):
return True
return False
def backup(self, suffix, path=None):
def backup(self, suffix=None, path=None, ext=None, hidden=False):
import shutil
if path is None:
path = os.path.dirname(self._configfile)
basename = os.path.basename(self._configfile)
name, ext = os.path.splitext(basename)
backup = os.path.join(path, "{}.{}{}".format(name, suffix, ext))
basename = os.path.basename(self._configfile)
name, default_ext = os.path.splitext(basename)
if ext is None:
ext = default_ext
if suffix is None and ext == default_ext:
raise ValueError("Need a suffix or a different extension")
if suffix is None:
suffix = ""
backup = os.path.join(path, "{}{}.{}{}".format("." if hidden else "", name, suffix, ext))
shutil.copy(self._configfile, backup)
return backup