Beispiel #1
0
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)
Beispiel #2
0
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)
Beispiel #3
0
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
Beispiel #4
0
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."
        )
Beispiel #5
0
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)
Beispiel #6
0
    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
Beispiel #7
0
	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
Beispiel #8
0
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)
Beispiel #9
0
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)
Beispiel #10
0
def client(ctx, obj, host, port, httpuser, httppass, https, prefix):
	"""Basic API client."""
	try:
		obj.settings = init_settings(obj.basedir, obj.configfile)
		octoprint_client.init_client(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)
Beispiel #11
0
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)
Beispiel #12
0
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)
Beispiel #13
0
def config(ctx, obj):
    """Basic config manipulation."""
    logging.basicConfig(
        level=logging.DEBUG if obj.verbosity > 0 else logging.WARN)
    try:
        obj.settings = init_settings(obj.basedir, obj.configfile)
    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)
Beispiel #14
0
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)
Beispiel #15
0
def client(ctx, obj, host, port, httpuser, httppass, https, prefix):
    """Basic API client."""
    try:
        obj.settings = init_settings(obj.basedir, obj.configfile)
        octoprint_client.init_client(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)
Beispiel #16
0
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.")
Beispiel #17
0
    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
Beispiel #18
0
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)
Beispiel #19
0
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)
Beispiel #20
0
	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(ctx.obj.basedir, ctx.obj.configfile)
			self.plugin_manager = init_pluginsystem(self.settings, safe_mode=ctx.obj.safe_mode)
		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")

		self._initialized = True