|
| 1 | +# Plugins |
| 2 | + |
| 3 | +The `dstack` plugin system allows extending `dstack` server functionality using external Python packages. |
| 4 | + |
| 5 | +!!! info "Experimental" |
| 6 | + Plugins are currently an _experimental_ feature. |
| 7 | + Backward compatibility is not guaranteed across releases. |
| 8 | + |
| 9 | +## Enable plugins |
| 10 | + |
| 11 | +To enable a plugin, list it under `plugins` in [`server/config.yml`](../reference/server/config.yml.md): |
| 12 | + |
| 13 | +<div editor-title="server/config.yml"> |
| 14 | + |
| 15 | +```yaml |
| 16 | +plugins: |
| 17 | + - my_dstack_plugin |
| 18 | + - some_other_plugin |
| 19 | +projects: |
| 20 | +- name: main |
| 21 | +``` |
| 22 | +
|
| 23 | +</div> |
| 24 | +
|
| 25 | +On the next server restart, you should see a log message indicating that the plugin is loaded. |
| 26 | +
|
| 27 | +## Create plugins |
| 28 | +
|
| 29 | +To create a plugin, create a Python package that implements a subclass of |
| 30 | +`dstack.plugins.Plugin` and exports this subclass as a "dstack.plugins" entry point. |
| 31 | + |
| 32 | +1. Init the plugin package: |
| 33 | + |
| 34 | + <div class="termy"> |
| 35 | + |
| 36 | + ```shell |
| 37 | + $ uv init --library |
| 38 | + ``` |
| 39 | + |
| 40 | + </div> |
| 41 | + |
| 42 | +2. Define `ApplyPolicy` and `Plugin` subclasses: |
| 43 | + |
| 44 | + <div editor-title="src/example_plugin/__init__.py"> |
| 45 | + |
| 46 | + ```python |
| 47 | + from dstack.plugins import ApplyPolicy, Plugin, RunSpec, get_plugin_logger |
| 48 | +
|
| 49 | + logger = get_plugin_logger(__name__) |
| 50 | +
|
| 51 | + class ExamplePolicy(ApplyPolicy): |
| 52 | + def on_run_apply(self, user: str, project: str, spec: RunSpec) -> RunSpec: |
| 53 | + # ... |
| 54 | + return spec |
| 55 | + |
| 56 | + class ExamplePlugin(Plugin): |
| 57 | + |
| 58 | + def get_apply_policies(self) -> list[ApplyPolicy]: |
| 59 | + return [ExamplePolicy()] |
| 60 | + ``` |
| 61 | + |
| 62 | + </div> |
| 63 | + |
| 64 | +3. Specify a "dstack.plugins" entry point in `pyproject.toml`: |
| 65 | + |
| 66 | + <div editor-title="pyproject.toml"> |
| 67 | + |
| 68 | + ```toml |
| 69 | + [project.entry-points."dstack.plugins"] |
| 70 | + example_plugin = "example_plugin:ExamplePlugin" |
| 71 | + ``` |
| 72 | + |
| 73 | + </div> |
| 74 | + |
| 75 | +Then you can install the plugin package into your Python environment and enable it via `server/config.yml`. |
| 76 | + |
| 77 | +??? info "Plugins in Docker" |
| 78 | + If you deploy `dstack` using a Docker image you can add plugins either |
| 79 | + by including them in your custom image built upon the `dstack` server image, |
| 80 | + or by mounting installed plugins as volumes. |
| 81 | + |
| 82 | +## Apply policies |
| 83 | + |
| 84 | +Currently the only plugin functionality is apply policies. |
| 85 | +Apply policies allow modifying specs of runs, fleets, volumes, and gateways submitted on `dstack apply`. |
| 86 | +Subclass `dstack.plugins.ApplyPolicy` to implement them. |
| 87 | + |
| 88 | +Here's an example of how to enforce certain rules using apply policies: |
| 89 | + |
| 90 | +<div editor-title="src/example_plugin/__init__.py"> |
| 91 | + |
| 92 | +```python |
| 93 | +class ExamplePolicy(ApplyPolicy): |
| 94 | + def on_run_apply(self, user: str, project: str, spec: RunSpec) -> RunSpec: |
| 95 | + # Forcing some limits |
| 96 | + spec.configuration.max_price = 2.0 |
| 97 | + spec.configuration.max_duration = "1d" |
| 98 | + # Setting some extra tags |
| 99 | + if spec.configuration.tags is None: |
| 100 | + spec.configuration.tags = {} |
| 101 | + spec.configuration.tags |= { |
| 102 | + "team": "my_team", |
| 103 | + } |
| 104 | + # Forbid something |
| 105 | + if spec.configuration.privileged: |
| 106 | + logger.warning("User %s tries to run privileged containers", user) |
| 107 | + raise ValueError("Running privileged containers is forbidden") |
| 108 | + # Set some service-specific properties |
| 109 | + if isinstance(spec.configuration, Service): |
| 110 | + spec.configuration.https = True |
| 111 | + return spec |
| 112 | +``` |
| 113 | + |
| 114 | +</div> |
| 115 | + |
| 116 | +For more information on the plugin development, see the [plugin example](https://github.com/dstackai/dstack/tree/master/examples/plugins/example_plugin). |
0 commit comments