
* Introduce reusable BackgroundJob framework A new abstract class can be used to implement job function classes. It handles the necessary logic for starting and stopping jobs, including exception handling and rescheduling of recurring jobs. This commit also includes the migration of data source jobs to the new framework. * Restore using import_string for jobs Using the 'import_string()' utility from Django allows the job script class to be simplified, as module imports no longer need to avoid loops. This should make it easier to queue and maintain jobs. * Use SyncDataSourceJob for management command Instead of maintaining two separate job execution logics, the same job is now used for both background and interactive execution. * Implement BackgroundJob for running scripts The independent implementations of interactive and background script execution have been merged into a single BackgroundJob implementation. * Fix documentation of model features * Ensure consitent code style * Introduce reusable ScheduledJob A new abstract class can be used to implement job function classes that specialize in scheduling. These use the same logic as regular BackgroundJobs, but ensure that they are only scheduled once at any given time. * Introduce reusable SystemJob A new abstract class can be used to implement job function classes that specialize in system background tasks (e.g. synchronization or housekeeping). In addition to the features of the BackgroundJob and ScheduledJob classes, these implement additional logic to not need to be bound to an existing NetBox object and to setup job schedules on plugin load instead of an interactive request. * Add documentation for jobs framework * Revert "Use SyncDataSourceJob for management" This partially reverts commitdb591d4
. The 'run_now' parameter of 'enqueue()' remains, as its being used by following commits. * Merge enqueued status into JobStatusChoices * Fix logger for ScriptJob * Remove job name for scripts Because scripts are already linked through the Job Instance field, the name is displayed twice. Removing this reduces redundancy and opens up the possibility of simplifying the BackgroundJob framework in future commits. * Merge ScheduledJob into BackgroundJob Instead of using separate classes, the logic of ScheduledJob is now merged into the generic BackgroundJob class. This allows reusing the same logic, but dynamically deciding whether to enqueue the same job once or multiple times. * Add name attribute for BackgroundJob Instead of defining individual names on enqueue, BackgroundJob classes can now set a job name in their meta class. This is equivalent to other Django classes and NetBox scripts. * Drop enqueue_sync_job() method from DataSource * Import ScriptJob directly * Relax requirement for Jobs to reference a specific object * Rename 'run_now' arg on Job.enqueue() to 'immediate' * Fix queue lookup in Job enqueue * Collapse SystemJob into BackgroundJob * Remove legacy JobResultStatusChoices ChoiceSet was moved to core in40572b5
. * Use queue 'low' for system jobs by default System jobs usually perform low-priority background tasks and therefore can use a different queue than 'default', which is used for regular jobs related to specific objects. * Add test cases for BackgroundJob handling * Fix enqueue interval jobs As the job's name is set by enqueue(), it must not be passed in handle() to avoid duplicate kwargs with the same name. * Honor schedule_at for job's enqueue_once Not only can a job's interval change, but so can the time at which it is scheduled to run. If a specific scheduled time is set, it will also be checked against the current job schedule. If there are any changes, the job is rescheduled with the new time. * Switch BackgroundJob to regular methods Instead of using a class method for run(), a regular method is used for this purpose. This gives the possibility to add more convenience methods in the future, e.g. for interacting with the job object or for logging, as implemented for scripts. * Fix background tasks documentation * Test enqueue in combination with enqueue_once * Rename background jobs to tasks (to differentiate from RQ) * Touch up docs * Revert "Use queue 'low' for system jobs by default" This reverts commitb17b2050df
. * Remove system background job This commit reverts commits4880d81
and0b15ecf
. Using the database 'connection_created' signal for job registration feels a little wrong at this point, as it would trigger registration very often. However, the background job framework is prepared for this use case and can be used by plugins once the auto-registration of jobs is solved. * Fix runscript management command Defining names for background jobs was disabled withfb75389
. The preceeding changes in257976d
did forget the management command. * Use regular imports for ScriptJob * Rename BackgroundJob to JobRunner --------- Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
13 KiB
Plugins Development
!!! tip "Plugins Development Tutorial" Just getting started with plugins? Check out our NetBox Plugin Tutorial on GitHub! This in-depth guide will walk you through the process of creating an entire plugin from scratch. It even includes a companion demo plugin repo to ensure you can jump in at any step along the way. This will get you up and running with plugins in no time!
!!! tip "Plugin Certification Program" NetBox Labs offers a Plugin Certification Program for plugin developers interested in establishing a co-maintainer relationship. The program aims to assure ongoing compatibility, maintainability, and commercial supportability of key plugins.
NetBox can be extended to support additional data models and functionality through the use of plugins. A plugin is essentially a self-contained Django app which gets installed alongside NetBox to provide custom functionality. Multiple plugins can be installed in a single NetBox instance, and each plugin can be enabled and configured independently.
!!! info "Django Development" Django is the Python framework on which NetBox is built. As Django itself is very well-documented, this documentation covers only the aspects of plugin development which are unique to NetBox.
Plugins can do a lot, including:
- Create Django models to store data in the database
- Provide their own "pages" (views) in the web user interface
- Inject template content and navigation links
- Extend NetBox's REST and GraphQL APIs
- Load additional Django apps
- Add custom request/response middleware
However, keep in mind that each piece of functionality is entirely optional. For example, if your plugin merely adds a piece of middleware or an API endpoint for existing data, there's no need to define any new models.
!!! warning While very powerful, the NetBox plugins API is necessarily limited in its scope. The plugins API is discussed here in its entirety: Any part of the NetBox code base not documented here is not part of the supported plugins API, and should not be employed by a plugin. Internal elements of NetBox are subject to change at any time and without warning. Plugin authors are strongly encouraged to develop plugins using only the officially supported components discussed here and those provided by the underlying Django framework to avoid breaking changes in future releases.
Plugin Structure
Although the specific structure of a plugin is largely left to the discretion of its authors, a typical NetBox plugin might look something like this:
project-name/
- plugin_name/
- api/
- __init__.py
- serializers.py
- urls.py
- views.py
- migrations/
- __init__.py
- 0001_initial.py
- ...
- templates/
- plugin_name/
- *.html
- __init__.py
- filtersets.py
- graphql.py
- jobs.py
- models.py
- middleware.py
- navigation.py
- signals.py
- tables.py
- template_content.py
- urls.py
- views.py
- pyproject.toml
- README.md
The top level is the project root, which can have any name that you like. Immediately within the root should exist several items:
pyproject.toml
- is a standard configuration file used to install the plugin package within the Python environment.README.md
- A brief introduction to your plugin, how to install and configure it, where to find help, and any other pertinent information. It is recommended to writeREADME
files using a markup language such as Markdown to enable human-friendly display.- The plugin source directory. This must be a valid Python package name, typically comprising only lowercase letters, numbers, and underscores.
The plugin source directory contains all the actual Python code and other resources used by your plugin. Its structure is left to the author's discretion, however it is recommended to follow best practices as outlined in the Django documentation. At a minimum, this directory must contain an __init__.py
file containing an instance of NetBox's PluginConfig
class, discussed below.
Note: The Cookiecutter NetBox Plugin can be used to auto-generate all the needed directories and files for a new plugin.
PluginConfig
The PluginConfig
class is a NetBox-specific wrapper around Django's built-in AppConfig
class. It is used to declare NetBox plugin functionality within a Python package. Each plugin should provide its own subclass, defining its name, metadata, and default and required configuration parameters. An example is below:
from netbox.plugins import PluginConfig
class FooBarConfig(PluginConfig):
name = 'foo_bar'
verbose_name = 'Foo Bar'
description = 'An example NetBox plugin'
version = '0.1'
author = 'Jeremy Stretch'
author_email = 'author@example.com'
base_url = 'foo-bar'
required_settings = []
default_settings = {
'baz': True
}
django_apps = ["foo", "bar", "baz"]
config = FooBarConfig
NetBox looks for the config
variable within a plugin's __init__.py
to load its configuration. Typically, this will be set to the PluginConfig subclass, but you may wish to dynamically generate a PluginConfig class based on environment variables or other factors.
PluginConfig Attributes
Name | Description |
---|---|
name |
Raw plugin name; same as the plugin's source directory |
verbose_name |
Human-friendly name for the plugin |
version |
Current release (semantic versioning is encouraged) |
description |
Brief description of the plugin's purpose |
author |
Name of plugin's author |
author_email |
Author's public email address |
base_url |
Base path to use for plugin URLs (optional). If not specified, the project's name will be used. |
required_settings |
A list of any configuration parameters that must be defined by the user |
default_settings |
A dictionary of configuration parameters and their default values |
django_apps |
A list of additional Django apps to load alongside the plugin |
min_version |
Minimum version of NetBox with which the plugin is compatible |
max_version |
Maximum version of NetBox with which the plugin is compatible |
middleware |
A list of middleware classes to append after NetBox's build-in middleware |
queues |
A list of custom background task queues to create |
search_extensions |
The dotted path to the list of search index classes (default: search.indexes ) |
data_backends |
The dotted path to the list of data source backend classes (default: data_backends.backends ) |
template_extensions |
The dotted path to the list of template extension classes (default: template_content.template_extensions ) |
menu_items |
The dotted path to the list of menu items provided by the plugin (default: navigation.menu_items ) |
graphql_schema |
The dotted path to the plugin's GraphQL schema class, if any (default: graphql.schema ) |
user_preferences |
The dotted path to the dictionary mapping of user preferences defined by the plugin (default: preferences.preferences ) |
All required settings must be configured by the user. If a configuration parameter is listed in both required_settings
and default_settings
, the default setting will be ignored.
!!! tip "Accessing Config Parameters"
Plugin configuration parameters can be accessed using the get_plugin_config()
function. For example:
```python
from netbox.plugins import get_plugin_config
get_plugin_config('my_plugin', 'verbose_name')
```
Important Notes About django_apps
Loading additional apps may cause more harm than good and could make identifying problems within NetBox itself more difficult. The django_apps
attribute is intended only for advanced use cases that require a deeper Django integration.
Apps from this list are inserted before the plugin's PluginConfig
in the order defined. Adding the plugin's PluginConfig
module to this list changes this behavior and allows for apps to be loaded after the plugin.
Any additional apps must be installed within the same Python environment as NetBox or ImproperlyConfigured
exceptions will be raised when loading the plugin.
Create pyproject.toml
pyproject.toml
is the configuration file used to package and install our plugin once it's finished. It is used by packaging tools, as well as other tools. The primary function of this file is to call the build system to create a Python distribution package. We can pass a number of keyword arguments to control the package creation as well as to provide metadata about the plugin. There are three possible TOML tables in this file:
[build-system]
allows you to declare which build backend you use and which other dependencies (if any) are needed to build your project.[project]
is the format that most build backends use to specify your project’s basic metadata, such as the author's name, project URL, etc.[tool]
has tool-specific subtables, e.g.,[tool.black]
,[tool.mypy]
. Consult the particular tool’s documentation for reference.
An example pyproject.toml
is below:
# See PEP 518 for the spec of this file
# https://www.python.org/dev/peps/pep-0518/
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[project]
name = "my-example-plugin"
version = "0.1.0"
authors = [
{name = "John Doe", email = "test@netboxlabs.com"},
]
description = "An example NetBox plugin."
readme = "README.md"
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Natural Language :: English',
"Programming Language :: Python :: 3 :: Only",
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
]
requires-python = ">=3.10.0"
Many of these are self-explanatory, but for more information, see the pyproject.toml documentation.
Create a Virtual Environment
It is strongly recommended to create a Python virtual environment for the development of your plugin, as opposed to using system-wide packages. This will afford you complete control over the installed versions of all dependencies and avoid conflict with system packages. This environment can live wherever you'd like, however it should be excluded from revision control. (A popular convention is to keep all virtual environments in the user's home directory, e.g. ~/.virtualenvs/
.)
python3 -m venv ~/.virtualenvs/my_plugin
You can make NetBox available within this environment by creating a path file pointing to its location. This will add NetBox to the Python path upon activation. (Be sure to adjust the command below to specify your actual virtual environment path, Python version, and NetBox installation.)
echo /opt/netbox/netbox > $VENV/lib/python3.10/site-packages/netbox.pth
Development Installation
To ease development, it is recommended to go ahead and install the plugin at this point using setuptools' develop
mode. This will create symbolic links within your Python environment to the plugin development directory. Call pip
from the plugin's root directory with the -e
flag:
$ pip install -e .
More information on editable builds can be found at Editable installs for pyproject.toml .
Configure NetBox
To enable the plugin in NetBox, add it to the PLUGINS
parameter in configuration.py
:
PLUGINS = [
'my_plugin',
]