Annotate function definitions, class attributes, and local variables as necessary for type hinting. This is useful for IDEs with linters that support type hints and also can be used by GitHub Actions to detect erroneous code.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Add a SentinelClass singleton. This can be used to generate Sentinels by
modules that need to use the Sentinel pattern for default values. This
allows for more robust type checking, as the Sentinel type should not be
included as a return value.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
On slower platforms it is possible for this to take time and block the event loop. Run shutil.rmtree in another thread to prevent this from occuring.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
If an inotify event is generated as the result of an API request, the notifications are synchronized so that they occur after the request returns. Use a mutex to prevent multiple "write" requests processing concurrently.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
If a file is moved from one location to another in the "gcodes" root it isn't necessary to rescan the file metadata as the underlying file information does not change. Add the ability to change the metadata's key and move associated thumbnails when a file is moved.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Since filelist_changed notifications are now delayed until after metadata is processed there is no need to send an additional metadata notification.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Rather than tracking watch paths directly, wrap each watch in a node. This makes makes it a bit easier to handle move and delete changes, as we don't have to manage several different dicts to store the information we need.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
When a directory is created, attempt to suppress notifications generated as its children are created.
Wait until all items are copied before notifying clients and scanning metadata.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
The copy methods are blocking and will block the asyncio event loop. Run them in a ThreadPoolExecutor to keep the event loop free.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
When a directory is deleted this attempts to suppress delete events for all children. Clients only need to be notified of the parent node, as its deletion implies all children have been deleted.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This guarantees that .ufps will not be unzipped in parallel and offloads a potential blocking operation to another process.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This removes all calls to "notify_filelist_changed" in the FileManager class. This also simplfies the previous `_convert_path()` method, as it is no longer necessary to return the path relative to the root.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Generally speaking static files do not require authorization, so there should be little harm in bypassing auth checks. This allows clients to retrieve assets without XHRs or requesting tokens.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
On some devices gpiod does not report the correct state. Since Moonraker maintains exclusive control over GPIO lines, its unnecessary to query the line itself for state, as we can maintain it ourselves.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This should allow git's internal timeout mechanisms to trigger at the expense of potentially delaying response time if a fetch or pull hangs.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
It is now possible for any authorized request to delete a user, however a logged in user cannot delete its own account.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
A single endpoint that can be used to both toggle and retrieve device state is useful for interoperability with home automation software. The previous endpoints will remain as they are still useful for batch operations.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Make sure that the current device state is correct in the event that a device is toggled by an external source.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Always run git fsck when initializing a repo. If a loose object error is detected when running git pull or git fetch, attempt to remove loose objects and retry.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Don't continuously log a change in throttled state, only log when a new throttled flag is detected.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Log the current user for each request. Dont' log requests that return with 200, 204, 206, or 304 in release mode.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
A Moonraker component may now register a namespace as "forbidden". API requests have no read/write access to forbidden namespaces and this cannot be overridden by the database debug option.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
CONFIG CHANGE: This deprecates the "enabled" option in the [authorization] section. Authorization will be enabled if the section is included in moonraker.conf, otherwise it will be disabled.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Force an update to cached file hashes should the modified time change. Re-enable the "should_return_304" check so clients can re-implement caching.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Some static files, such as logs, can change size during a request. This results in a content length mismatch and error. Cap the amount read based on the original content length.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Add a timer parameter to shelly and tasmota integration.
Use case: 3D printer mains power is connected to one of these devices. Just turning off the smart switch will just kill power to the RaspberryPI. Adding a timer will allow the PI to shutdown cleanly before killing the power
Signed-off-by: Dominik Weis fsironman@gmail.com
This allows protected namespaces to be modified externally. This is for dev use only, modifying protected namespaces can potentially corrupt the database.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This fixes a bug where it is protected namespace can be modified in an optional component if that component is disabled.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
add a check and handle consequently in case tasmota device is only one relay and would return a power value when asked a power1 value
Signed-off-by: Arnaud Schaer <arnaud.schaer@wanadoo.fr>
The Klipper repo always initializes last. Send a notification at this time, so if any clients are connected they can receive the full update status after init.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
During normal updates dependencies will always been updated when necessary. It is possible to force a dependency updating using the new "/machine/update/recover" endpoint.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Gives callers flexibility as to which signal is used to cancel a command first. By default shell_command will start with SIGTERM, then proceed to SIGKILL.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Rather than attempt to automate recovery, provide an endpoint for clients
to initiate. Clients can choose between a "hard" recovery, which removes
a corrupt repo and restores from a backup, or a "soft" recover which attempts
a git clean followed by a git reset.
Backups are now only taken when a valid repo is detected.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Note that this does not replace references to "Cura" or "Octoprint" plugins, which are also included in this module.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
It isn't necessary for each updater to have their own init lock. Combine them all into a single Event that is set after the "initialize_updaters" method completes.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Give more time for a standard command to complete. This may help prevent a git command from being terminated prematurely.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Git repos can occasionally become corrupted when attempting to deploy updates thought automated means. Use rysnc to create a backup of each git repo, allowing the repo to be restored in the event of a corrupted repo.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Since "run_with_response" now raises an exception there is no need to inspect the response for a None type. Instead the exception can be handled by the caller directly.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This error contains the return code and anything received from stdout and stderr when "run_with_response" encounters an error. This allows the caller to receive the output and easily distinguish between a successfully run command and an erronous command.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Sometimes git will erroneously leave the lock file in place when a command fails. Check for locked git repo before initialization, waiting up to 60 seconds before forcefully removing the lock. Also check for a locked repo after a failed fetch or pull, removing it if it exists.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Git clients may specify a branch other than "master" as the primary to pass the validation checks.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This field does not persist in the database as it is subject change if a file is moved or deleted when Moonraker is not running.
Also add thumbnail information to the metadata sans the base64 image data for each entry.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Other modules may look up this plugin to determine if a particular file exists at the specified root.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Moonraker now checks the modified time of both the install script and requirements file for git repos. If either is out of date their associated dependencies will be installed.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This ensures that jobs in the database will be stored in the order they were started. Doing so also eliminates the need for the `history_auto_inc_id", as we can initialize the next id from the database keys.
This also caches history keys in memory so we do not need to read them from the database on each access.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Add an additional checks to determine if a job needs to be cancelled when we receive a new "printing" state. A file name mismatch or a decrease in total_duration indicates a virtual sdcard reset.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Add GET and DELETE `/server/history/job` APIs and remvoe the `DELETE /server/history/delete` API. When returning a list of jobs, return it as an array rather than as a object. The `/server/history/list` API no longer accepts a job id.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This allows the repo init sequence to distinguish between a broken git repo and an invalid git repo.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
If a trusted IP address is passed as the origin then it is safe to assume that CORS access to this origin should be granted, but only if CORS is enabled by specifying at least one cors domain.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
A user may unintentionally allow access to dangerous domains if they place a wildcard in the top level domain portion of an entry. Raise a config error when this condition is detected.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Move git command functionality to its own class outside of the Updater class. This class is responsible for tracking repo state and executing commands on the repo. Fetch and Pull no longer use built in command timeouts, instead a callback is scheduled to see if the command returned progress. Only when no progress is returned will a fetch or pull be terminated after a timeout.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This class handles running shell commands and sending http API requests. Each updater class shares an instance of the command helper rather than the UpdateManager instance.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
In the "run()" method verbosity should still be allowed if log_stderr is True, even
if both callbacks are not set.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This allows plugins to interact with other plugins before they are closed. For example, a plugin may wish to save persistent state to the database before it is closed.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This handler provides consistent error reporting in the event that the client attempts to access an unknown endpoint. If the request is unauthorized, an 401 will be returned. Otherwise a 404 will be returned, however if CORS is available the CORS headers will be set.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Pass GIT_HTTP_LOW_SPEED_LIMIT and GIT_HTTP_LOW_SPEED_TIME environment variables to the "git fetch" and "git pull" commands. If the remote is unreachable this should force the command to timeout before Moonraker forcefully terminates it.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This brings consistency to the "run" methods. It also removes potential confusion between the previous "quiet" argument and the "verbose" argument in "run()".
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
A consumer of shell command can achieve "fire and forget" by scheduling the future returned by "run()" to execute on the event loop.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
It is now possible to redirect stderr to a callback for asynchronous message transfer. Also added is the env option, allowing custom environment variables to be passed to the subprocess.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Rather than use terminate to pre-emptively exit a shell command, attempt to exit with SIGINT, SIGTERM, and finally SIGKILL.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Moving log level init to the app module and have it explicitly set the level. This allows the logging level to be toggled with a server restart. Default debug logging to False.
When debug logging is disabled do not log HTTP requests.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Only report extruder and bed temperatures. Use dict "get()" methods to make sure that the fields are available.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This error is an indication that a JSON body was sent without the content type set to "application/json".
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
The prior implementations assumed that uploaded files would be buffered in memory.
This is no longer the case, so UFP extraction is more efficient when done straight to disk.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This PR is a minimal implementation of the Octoprint REST API that is required
for Cura to be able to establish a connection and send gcode/UFP files to
moonraker without errors.
Currently it only supports the "global apikey authentication" method.
Signed-off-by: Nickolas Grigoriadis <nagrigoriadis@gmail.com>
When a websocket is closed, set "is_closed" to True in the on_closed handler. When closing all websockets, iterate through a list of values.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This allows gcode file metadata to persist through restarts, reducing the time spent parsing. The gcode file path also persists, so it is available even when Klippy is not.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
At times a subprocess created by shell command fails. Attempt to mitigate this by using asyncio's version of subprocess, which should be more stable when running via the event loop.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Instead, look up klippy_apis after all plugins are loaded. This allows the "CORE_PLUGINS" list to determine the exact order in which core plugins are loaded.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This provides an lmdb database which can be used to keep persistent data. This can be used by internal plugins or by clients.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Rather than require developers to add new notification handlers to websockets.py directly, implement a method that may be called to register websocket notifications during init.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
When a closed websocket is detected set a flag to prevent further status updates until the websocket is removed.
Also make sure that the associated subscriptions are removed if a closed websocket is detected during a notification.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This allows clients to define files that will persist through an update, useful if the client has some sort of static configuration.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
When this option is set Mooraker will restart Klipper after the device has been powered on. An additional "restart_delay" option has been added to customize the delay in which the restart is executed.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Move the service restart functionality out of the "service restart" request into its own method. This allows the "service restart" to be called internally by other modules.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Fix issue where apt-get update is called on startup. This should only be done by the "auto updater" or by a specific request to refresh.
Always set an Updater's init event to true when refresh() is called, regardless of the result.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Decrease the frequency of auto updates roughly every 24 hours. The manager will attempt to update once between 12am and 4am.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This is a workaround for an issue where the http client fails to propogate an exception back to the caller, hanging the update process.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Attach "is_complete" to the response so clients know that the transaction is done, even though it resulted in an error.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
The entire configuration should be validated prior to device initialization. In the event of an error, the GPIO Chip factory must be closed.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
We need to be sure that the entire update_manager is correctly configured prior to queueing the callbacks for the initial refresh. Rather than have each updater spawn a "refesh" callback, move them all into a single init method spawned after the module has been fully configured.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
The server can still operate and be used to fetch the logs if a plugin fails to load. Add a 'failed_plugins' field to the /server/info response so clients can notify users that this plugin failed to load.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Previously this check was only on done when a directory was requested by the API. Moving this check to _list_directory ()updates metadata for internal calls as well.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
The slower system package update will now only executed by the auto-refresh routine. Moonraker will check for updates roughly every 2 hours, however system packages updates will only occur between 1am and 4am local time.
If a print is in progress any attempt to refresh or update will be aborted.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This should reduce the load on low perfomance devices. This also makes it possible to queue update requests.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
Calls to "refresh" should not be allowed to occur while a client a current refresh is in progress. Updates will wait for a pending refresh to complete before beginning the update procedure.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
It is possible for the log files to be registered after a rollover, which can result in the file not existing. Force the log endpoints to register, bypassing the existance check.
Signed-off-by: Eric Callahan <arksine.code@gmail.com>