✨ New --overlay CLI flag
Allows to specify additional config overlay paths to use between the default settings and config.yaml. Can be either individual yaml files, or a whole directory containing an arbitrary number of yaml files. Allows to change the default configuration without interfering with config.yaml. OctoPrint side solution for #3821, OctoPi needs to be modified to make use of this for its platform specific configuration.
This commit is contained in:
1 parent
b124de4f04
commit
572ea6f2a7
7 files changed
+75
-8
No files matched your search
@@ -86,6 +86,7 @@ class FatalStartupError(Exception):
|
||||
def init_platform(
|
||||
basedir,
|
||||
configfile,
|
||||
overlays=None,
|
||||
use_logging_file=True,
|
||||
logging_file=None,
|
||||
logging_config=None,
|
||||
@@ -117,7 +118,7 @@ def init_platform(
|
||||
after_preinit_logging(**kwargs)
|
||||
|
||||
try:
|
||||
settings = init_settings(basedir, configfile)
|
||||
settings = init_settings(basedir, configfile, overlays=overlays)
|
||||
except Exception as ex:
|
||||
raise FatalStartupError("Could not initialize settings manager", cause=ex)
|
||||
kwargs["settings"] = settings
|
||||
@@ -220,13 +221,15 @@ def init_platform(
|
||||
)
|
||||
|
||||
|
||||
def init_settings(basedir, configfile):
|
||||
def init_settings(basedir, configfile, overlays=None):
|
||||
"""Inits the settings instance based on basedir and configfile to use."""
|
||||
|
||||
from octoprint.settings import InvalidSettings, settings
|
||||
|
||||
try:
|
||||
return settings(init=True, basedir=basedir, configfile=configfile)
|
||||
return settings(
|
||||
init=True, basedir=basedir, configfile=configfile, overlays=overlays
|
||||
)
|
||||
except InvalidSettings as e:
|
||||
raise FatalStartupError(str(e))
|
||||
|
||||
|
||||
@@ -62,6 +62,7 @@ def init_platform_for_cli(ctx):
|
||||
components = init_platform(
|
||||
get_ctx_obj_option(ctx, "basedir", None),
|
||||
get_ctx_obj_option(ctx, "configfile", None),
|
||||
overlays=get_ctx_obj_option(ctx, "overlays", None),
|
||||
safe_mode=True,
|
||||
)
|
||||
|
||||
@@ -180,8 +181,7 @@ def standard_options(hidden=False):
|
||||
"""
|
||||
Decorator to add the standard options shared among all "octoprint" commands.
|
||||
|
||||
Adds the options ``--basedir``, ``--config`` and ``--verbose``. If ``hidden``
|
||||
is set to ``True``, the options will be available on the command but not
|
||||
If ``hidden`` is set to ``True``, the options will be available on the command but not
|
||||
listed in its help page.
|
||||
"""
|
||||
|
||||
@@ -209,6 +209,16 @@ def standard_options(hidden=False):
|
||||
expose_value=False,
|
||||
help="Specify the config file to use.",
|
||||
),
|
||||
factory(
|
||||
"--overlay",
|
||||
"overlays",
|
||||
type=click.Path(),
|
||||
callback=set_ctx_obj_option,
|
||||
is_eager=True,
|
||||
multiple=True,
|
||||
expose_value=False,
|
||||
help="Specify additional config overlays to use.",
|
||||
),
|
||||
factory(
|
||||
"--verbose",
|
||||
"-v",
|
||||
|
||||
@@ -65,6 +65,7 @@ def config(ctx):
|
||||
ctx.obj.settings = init_settings(
|
||||
get_ctx_obj_option(ctx, "basedir", None),
|
||||
get_ctx_obj_option(ctx, "configfile", None),
|
||||
overlays=get_ctx_obj_option(ctx, "overlays", None),
|
||||
)
|
||||
except FatalStartupError as e:
|
||||
click.echo(str(e), err=True)
|
||||
|
||||
@@ -84,6 +84,7 @@ class OctoPrintPluginCommands(click.MultiCommand):
|
||||
self.settings = init_settings(
|
||||
get_ctx_obj_option(ctx, "basedir", None),
|
||||
get_ctx_obj_option(ctx, "configfile", None),
|
||||
overlays=get_ctx_obj_option(ctx, "overlays", None),
|
||||
)
|
||||
self.plugin_manager = init_pluginsystem(
|
||||
self.settings, safe_mode=get_ctx_obj_option(ctx, "safe_mode", False)
|
||||
|
||||
@@ -33,6 +33,7 @@ def run_server(
|
||||
safe_mode,
|
||||
ignore_blacklist,
|
||||
octoprint_daemon=None,
|
||||
overlays=None,
|
||||
):
|
||||
"""Initializes the environment and starts up the server."""
|
||||
|
||||
@@ -113,6 +114,7 @@ def run_server(
|
||||
components = init_platform(
|
||||
basedir,
|
||||
configfile,
|
||||
overlays=overlays,
|
||||
logging_file=logging_config,
|
||||
debug=debug,
|
||||
verbosity=verbosity,
|
||||
@@ -260,6 +262,7 @@ def enable_safemode(ctx, **kwargs):
|
||||
settings = init_settings(
|
||||
get_ctx_obj_option(ctx, "basedir", None),
|
||||
get_ctx_obj_option(ctx, "configfile", None),
|
||||
overlays=get_ctx_obj_option(ctx, "overlays", None),
|
||||
)
|
||||
except FatalStartupError as e:
|
||||
click.echo(str(e), err=True)
|
||||
@@ -297,6 +300,7 @@ def serve_command(ctx, **kwargs):
|
||||
verbosity = get_value("verbosity")
|
||||
safe_mode = "flag" if get_value("safe_mode") else None
|
||||
ignore_blacklist = get_value("ignore_blacklist")
|
||||
overlays = get_value("overlays")
|
||||
|
||||
if v4 and not host:
|
||||
host = "0.0.0.0"
|
||||
@@ -313,6 +317,7 @@ def serve_command(ctx, **kwargs):
|
||||
verbosity,
|
||||
safe_mode,
|
||||
ignore_blacklist,
|
||||
overlays=overlays,
|
||||
)
|
||||
|
||||
|
||||
@@ -350,6 +355,7 @@ if sys.platform != "win32" and sys.platform != "darwin":
|
||||
|
||||
basedir = get_value("basedir")
|
||||
configfile = get_value("configfile")
|
||||
overlays = get_value("overlays")
|
||||
verbosity = get_value("verbosity")
|
||||
safe_mode = "flag" if get_value("safe_mode") else None
|
||||
ignore_blacklist = get_value("ignore_blacklist")
|
||||
@@ -369,6 +375,7 @@ if sys.platform != "win32" and sys.platform != "darwin":
|
||||
pidfile,
|
||||
basedir,
|
||||
configfile,
|
||||
overlays,
|
||||
host,
|
||||
port,
|
||||
v6_only,
|
||||
@@ -383,6 +390,7 @@ if sys.platform != "win32" and sys.platform != "darwin":
|
||||
|
||||
self._basedir = basedir
|
||||
self._configfile = configfile
|
||||
self._overlays = overlays
|
||||
self._host = host
|
||||
self._port = port
|
||||
self._v6_only = v6_only
|
||||
@@ -407,12 +415,14 @@ if sys.platform != "win32" and sys.platform != "darwin":
|
||||
self._safe_mode,
|
||||
self._ignore_blacklist,
|
||||
octoprint_daemon=self,
|
||||
overlays=self._overlays,
|
||||
)
|
||||
|
||||
octoprint_daemon = OctoPrintDaemon(
|
||||
pid,
|
||||
basedir,
|
||||
configfile,
|
||||
overlays,
|
||||
host,
|
||||
port,
|
||||
v6,
|
||||
|
||||
@@ -48,6 +48,7 @@ def user(ctx):
|
||||
settings = init_settings(
|
||||
get_ctx_obj_option(ctx, "basedir", None),
|
||||
get_ctx_obj_option(ctx, "configfile", None),
|
||||
overlays=get_ctx_obj_option(ctx, "overlays", None),
|
||||
)
|
||||
|
||||
group_manager_name = settings.get(["accessControl", "groupManager"])
|
||||
|
||||
@@ -24,6 +24,7 @@ __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agp
|
||||
__copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
import copy
|
||||
import fnmatch
|
||||
import io
|
||||
import logging
|
||||
import os
|
||||
@@ -47,6 +48,11 @@ try:
|
||||
except ImportError:
|
||||
from collections import KeysView
|
||||
|
||||
try:
|
||||
from os import scandir
|
||||
except ImportError:
|
||||
from scandir import scandir
|
||||
|
||||
from octoprint.util import (
|
||||
CaseInsensitiveSet,
|
||||
atomic_write,
|
||||
@@ -60,7 +66,7 @@ _APPNAME = "OctoPrint"
|
||||
_instance = None
|
||||
|
||||
|
||||
def settings(init=False, basedir=None, configfile=None):
|
||||
def settings(init=False, basedir=None, configfile=None, overlays=None):
|
||||
"""
|
||||
Factory method for initially constructing and consecutively retrieving the :class:`~octoprint.settings.Settings`
|
||||
singleton.
|
||||
@@ -75,6 +81,7 @@ def settings(init=False, basedir=None, configfile=None):
|
||||
``~/Library/Application Support/OctoPrint`` on MacOS.
|
||||
configfile (str): Path of the configuration file (``config.yaml``) to work on. If not set the default will
|
||||
be used: ``<basedir>/config.yaml`` for ``basedir`` as defined above.
|
||||
overlays (list): List of paths to config overlays to put between default settings and config.yaml
|
||||
|
||||
Returns:
|
||||
Settings: The fully initialized :class:`Settings` instance.
|
||||
@@ -89,7 +96,9 @@ def settings(init=False, basedir=None, configfile=None):
|
||||
|
||||
else:
|
||||
if init:
|
||||
_instance = Settings(configfile=configfile, basedir=basedir)
|
||||
_instance = Settings(
|
||||
configfile=configfile, basedir=basedir, overlays=overlays
|
||||
)
|
||||
else:
|
||||
raise ValueError("Settings not initialized yet")
|
||||
|
||||
@@ -659,14 +668,18 @@ class Settings(object):
|
||||
|
||||
OVERLAY_KEY = "__overlay__"
|
||||
|
||||
def __init__(self, configfile=None, basedir=None):
|
||||
def __init__(self, configfile=None, basedir=None, overlays=None):
|
||||
self._logger = logging.getLogger(__name__)
|
||||
|
||||
self._basedir = None
|
||||
|
||||
if overlays is None:
|
||||
overlays = []
|
||||
|
||||
assert isinstance(default_settings, dict)
|
||||
|
||||
self._map = HierarchicalChainMap({}, default_settings)
|
||||
self.load_overlays(overlays)
|
||||
|
||||
self._config = None
|
||||
self._dirty = False
|
||||
@@ -1013,6 +1026,34 @@ class Settings(object):
|
||||
|
||||
self._forget_hashes()
|
||||
|
||||
def load_overlays(self, overlays, migrate=True):
|
||||
for overlay in overlays:
|
||||
if not os.path.exists(overlay):
|
||||
continue
|
||||
|
||||
def process(path):
|
||||
try:
|
||||
overlay_config = self.load_overlay(path, migrate=migrate)
|
||||
self.add_overlay(overlay_config)
|
||||
self._logger.info("Added config overlay from {}".format(path))
|
||||
except Exception:
|
||||
self._logger.exception(
|
||||
"Could not add config overlay from {}".format(path)
|
||||
)
|
||||
|
||||
if os.path.isfile(overlay):
|
||||
process(overlay)
|
||||
|
||||
elif os.path.isdir(overlay):
|
||||
for entry in scandir(overlay):
|
||||
name = entry.name
|
||||
path = entry.path
|
||||
|
||||
if is_hidden_path(path) or not fnmatch.fnmatch(name, "*.yaml"):
|
||||
continue
|
||||
|
||||
process(path)
|
||||
|
||||
def load_overlay(self, overlay, migrate=True):
|
||||
config = None
|
||||
|
||||
|
||||
Reference in new issue
Block a user