def enable_safemode(ctx, **kwargs): """Sets the safe mode flag for the next start.""" from octoprint import FatalStartupError, init_settings logging.basicConfig( level=logging.DEBUG if get_ctx_obj_option(ctx, "verbosity", 0) > 0 else logging.WARN ) try: 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) click.echo("There was a fatal error initializing the settings manager.", err=True) ctx.exit(-1) else: settings.setBoolean(["server", "startOnceInSafeMode"], True) settings.save() click.echo( "Safe mode flag set, OctoPrint will start in safe mode on next restart." )
def _initialize(self, ctx): if self._initialized: return if ctx.obj is None: ctx.obj = OctoPrintContext() # initialize settings and plugin manager based on provided # context (basedir and configfile) from octoprint import init_settings, init_pluginsystem, FatalStartupError try: self.settings = init_settings( get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None)) self.plugin_manager = init_pluginsystem( self.settings, safe_mode=get_ctx_obj_option(ctx, "safe_mode", False)) except FatalStartupError as e: click.echo(e.message, err=True) click.echo( "There was a fatal error initializing the settings or the plugin system.", err=True) ctx.exit(-1) # fetch registered hooks self.hooks = self.plugin_manager.get_hooks("octoprint.cli.commands") logging.basicConfig( level=logging.DEBUG if ctx.obj.verbosity > 0 else logging.WARN) self._initialized = True
def user(ctx): """ User management. Note that this currently only supports managing user accounts stored in the configured user manager, not any user managers added through plugins and the "octoprint.users.factory" hook. """ try: logging.basicConfig( level=logging.DEBUG if get_ctx_obj_option(ctx, "verbosity", 0) > 0 else logging.WARN) settings = init_settings(get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None)) name = settings.get(["accessControl", "userManager"]) try: clazz = get_class(name) manager = clazz(settings=settings) except Exception: click.echo( "Could not instantiate user manager {}, falling back to FilebasedUserManager!" .format(name), err=True) manager = FilebasedUserManager(settings=settings) finally: manager.enabled = settings.getBoolean(["accessControl", "enabled"]) ctx.obj.user_manager = manager except Exception: click.echo("Could not instantiate user manager", err=True) return
def client(ctx, apikey, host, port, httpuser, httppass, https, prefix): """Basic API client.""" try: settings = None if not host or not port or not apikey: settings = init_settings( get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None), ) ctx.obj.client = create_client( settings=settings, apikey=apikey, host=host, port=port, httpuser=httpuser, httppass=httppass, https=https, prefix=prefix, ) except FatalStartupError as e: click.echo(str(e), err=True) click.echo("There was a fatal error initializing the client.", err=True) ctx.exit(-1)
def client(ctx, apikey, host, port, httpuser, httppass, https, prefix): """Basic API client.""" try: if not host or not port or not apikey: settings = init_settings( get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None)) if not host: host = settings.get(["server", "host"]) host = host if host != "0.0.0.0" else "127.0.0.1" if not port: port = settings.getInt(["server", "port"]) if not apikey: apikey = settings.get(["api", "key"]) baseurl = octoprint_client.build_base_url(https=https, httpuser=httpuser, httppass=httppass, host=host, port=port, prefix=prefix) ctx.obj.client = octoprint_client.Client(baseurl, apikey) except FatalStartupError as e: click.echo(e.message, err=True) click.echo("There was a fatal error initializing the client.", err=True) ctx.exit(-1)
def client(ctx, apikey, host, port, httpuser, httppass, https, prefix): """Basic API client.""" try: if not host or not port or not apikey: settings = init_settings(get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None)) if not host: host = settings.get(["server", "host"]) host = host if host != "0.0.0.0" else "127.0.0.1" if not port: port = settings.getInt(["server", "port"]) if not apikey: apikey = settings.get(["api", "key"]) baseurl = octoprint_client.build_base_url(https=https, httpuser=httpuser, httppass=httppass, host=host, port=port, prefix=prefix) ctx.obj.client = octoprint_client.Client(baseurl, apikey) except FatalStartupError as e: click.echo(e.message, err=True) click.echo("There was a fatal error initializing the client.", err=True) ctx.exit(-1)
def _initialize(self, ctx): if self._initialized: return click.echo("Initializing settings & plugin subsystem...") if ctx.obj is None: ctx.obj = OctoPrintContext() # initialize settings and plugin manager based on provided # context (basedir and configfile) from octoprint import init_settings, init_pluginsystem, FatalStartupError try: self.settings = init_settings(get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None)) self.plugin_manager = init_pluginsystem(self.settings, safe_mode=get_ctx_obj_option(ctx, "safe_mode", False)) except FatalStartupError as e: click.echo(e.message, err=True) click.echo("There was a fatal error initializing the settings or the plugin system.", err=True) ctx.exit(-1) # fetch registered hooks self.hooks = self.plugin_manager.get_hooks("octoprint.cli.commands") logging.basicConfig(level=logging.DEBUG if ctx.obj.verbosity > 0 else logging.WARN) self._initialized = True
def config(ctx): """Basic config manipulation.""" logging.basicConfig(level=logging.DEBUG if get_ctx_obj_option(ctx, "verbosity", 0) > 0 else logging.WARN) try: ctx.obj.settings = init_settings(get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None)) except FatalStartupError as e: click.echo(e.message, err=True) click.echo("There was a fatal error initializing the client.", err=True) ctx.exit(-1)
def client(ctx, host, port, httpuser, httppass, https, prefix): """Basic API client.""" try: ctx.obj.settings = init_settings(get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None)) octoprint_client.init_client(ctx.obj.settings, https=https, httpuser=httpuser, httppass=httppass, host=host, port=port, prefix=prefix) except FatalStartupError as e: click.echo(e.message, err=True) click.echo("There was a fatal error initializing the client.", err=True) ctx.exit(-1)
def user(ctx): """ User management. Note that this currently only supports managing user accounts stored in the configured user manager, not any user managers added through plugins and the "octoprint.users.factory" hook. """ try: logging.basicConfig( level=logging.DEBUG if get_ctx_obj_option(ctx, "verbosity", 0) > 0 else logging.WARN) 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"]) try: clazz = get_class(group_manager_name) group_manager = clazz() except AttributeError: click.echo( "Could not instantiate group manager {}, " "falling back to FilebasedGroupManager!".format( group_manager_name), err=True, ) group_manager = FilebasedGroupManager() ctx.obj.group_manager = group_manager name = settings.get(["accessControl", "userManager"]) try: clazz = get_class(name) user_manager = clazz(group_manager=group_manager, settings=settings) except CorruptUserStorage: raise except Exception: click.echo( "Could not instantiate user manager {}, falling back to FilebasedUserManager!" .format(name), err=True, ) user_manager = FilebasedUserManager(group_manager, settings=settings) ctx.obj.user_manager = user_manager except Exception: click.echo("Could not instantiate user manager", err=True) ctx.exit(-1)
def enable_safemode(ctx, **kwargs): """Sets the safe mode flag for the next start.""" from octoprint import init_settings, FatalStartupError logging.basicConfig(level=logging.DEBUG if get_ctx_obj_option(ctx, "verbosity", 0) > 0 else logging.WARN) try: settings = init_settings(get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None)) except FatalStartupError as e: click.echo(e.message, err=True) click.echo("There was a fatal error initializing the settings manager.", err=True) ctx.exit(-1) settings.setBoolean(["server", "startOnceInSafeMode"], True) settings.save() click.echo("Safe mode flag set, OctoPrint will start in safe mode on next restart.")
def _initialize(self, ctx): if self._initialized: return click.echo("Initializing settings & plugin subsystem...") if ctx.obj is None: ctx.obj = OctoPrintContext() logging_config = dict_merge( LOGGING_CONFIG, { "root": { "level": logging.DEBUG if ctx.obj.verbosity > 0 else logging.WARNING } }, ) logging.config.dictConfig(logging_config) # initialize settings and plugin manager based on provided # context (basedir and configfile) from octoprint import FatalStartupError, init_pluginsystem, init_settings try: 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)) except FatalStartupError as e: click.echo(str(e), err=True) click.echo( "There was a fatal error initializing the settings or the plugin system.", err=True, ) ctx.exit(-1) # fetch registered hooks self.hooks = self.plugin_manager.get_hooks("octoprint.cli.commands") self._initialized = True
def client(ctx, apikey, host, port, httpuser, httppass, https, prefix): """Basic API client.""" try: settings = None if not host or not port or not apikey: settings = init_settings(get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None)) ctx.obj.client = create_client(settings=settings, apikey=apikey, host=host, port=port, httpuser=httpuser, httppass=httppass, https=https, prefix=prefix) except FatalStartupError as e: click.echo(e.message, err=True) click.echo("There was a fatal error initializing the client.", err=True) ctx.exit(-1)
def cli(ctx): """Basic config manipulation.""" logging.basicConfig( level=logging. DEBUG if get_ctx_obj_option(ctx, "verbosity", 0) > 0 else logging.WARN) try: 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), ) ctx.obj.plugin_manager = init_pluginsystem( ctx.obj.settings, safe_mode=get_ctx_obj_option(ctx, "safe_mode", False)) except FatalStartupError as e: click.echo(str(e), err=True) click.echo( "There was a fatal error initializing the settings manager.", err=True) ctx.exit(-1)
def get_value(key): return get_ctx_obj_option(ctx, key, kwargs.get(key))