Skip to content

Commit 632e9e2

Browse files
r4victorjvstme
andauthored
Simplify backend configurators and models (#2389)
* Drop Configurator.get_default_configs() * Replace get_config_values with validate_config Breaking change: drop /api/backends/config_values endpoint * Test validate_config * Make Configurator not to depend on server models * Pass default_creds_enabled to Configurator.validate_config() * Move configurators to core * Test OCIConfigurator.validate_config() * Move backend models to backend dir * Unify API and YAML backend config models * Move _CONFIGURATOR_CLASSES to core * Include project_id and backend_id in StoredBackendRecord to support external configurators working with db models * Reflect configurator/models refactoring in the backends guide * Handle no Kubernetes networking config * docs: update backend types in config.yml documentation * feat: add Nebius, RunPod and DataCrunch backend configurations * Fix server/config.yml schema references * Clean up old Azure config * Update src/dstack/_internal/core/backends/base/configurator.py Co-authored-by: jvstme <36324149+jvstme@users.noreply.github.com> * Document that Configurator.create_backend() can do extra validation --------- Co-authored-by: jvstme <36324149+jvstme@users.noreply.github.com>
1 parent 0aeecf4 commit 632e9e2

File tree

153 files changed

+2943
-4063
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

153 files changed

+2943
-4063
lines changed

contributing/BACKENDS.md

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ Follow [DEVELOPMENT.md](DEVELOPMENT.md)`.
8888

8989
#### 2.3. Add dependencies to setup.py
9090

91-
Add any dependencies required by your cloud provider to `setup.py`. Create a separate section with the provider's name for
92-
these dependencies, and ensure that you update the `all` section to include them as well.
91+
Add any dependencies required by your cloud provider to `setup.py`. Create a separate section with the provider's name for these dependencies, and ensure that you update the `all` section to include them as well.
9392

9493
#### 2.4. Implement the provider backend
9594

@@ -100,20 +99,20 @@ Use the name of the provider.
10099

101100
Then create a database [migration](MIGRATIONS.md) to reflect the new enum member.
102101

103-
##### 2.4.2. Create the provider directory
102+
##### 2.4.2. Create the backend directory
104103

105104
Create a new directory under `src/dstack/_internal/core/backends` with the name of the backend type.
106105

107106
##### 2.4.3. Create the backend class
108107

109-
Under the backend directory you've created, create the `__init__.py` file and define the
108+
Under the backend directory you've created, create the `backend.py` file and define the
110109
backend class there (should extend `dstack._internal.core.backends.base.Backend`).
111110

112111
Refer to examples:
113-
[datacrunch](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/datacrunch/__init__.py),
114-
[aws](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/aws/__init__.py),
115-
[gcp](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/gcp/__init__.py),
116-
[azure](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/azure/__init__.py), etc.
112+
[datacrunch](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/datacrunch/backend.py),
113+
[aws](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/aws/backend.py),
114+
[gcp](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/gcp/backend.py),
115+
[azure](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/azure/backend.py), etc.
117116

118117
##### 2.4.4. Create the backend compute class
119118

@@ -132,62 +131,38 @@ Refer to examples:
132131
[gcp](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/gcp/compute.py),
133132
[azure](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/azure/compute.py), etc.
134133

135-
##### 2.4.5. Create the backend config model class
134+
##### 2.4.5. Create and register the backend config models
136135

137-
Under the `src/dstack/_internal/core/models/backends` directory, create the file with the name of the backend, and define the
138-
backend config model classes there.
136+
Under the backend directory, create the `models.py` file and define the backend config model classes there.
137+
Every backend must define two models:
139138

140-
[//]: # (TODO: Mention what config model classes are and how they work)
139+
* `*BackendConfig` that contains all backend parameters available for user configuration except for creds.
140+
* `*BackendConfigWithCreds` that contains all backends parameters available for user configuration and also creds.
141141

142-
[//]: # (TODO: Mention what config values class is and how it works)
142+
These models are used in server/config.yaml, the API, and for backend configuration.
143143

144-
Refer to examples:
145-
[datacrunch](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/models/backends/datacrunch.py),
146-
[aws](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/models/backends/aws.py),
147-
[gcp](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/models/backends/gcp.py),
148-
[azure](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/models/backends/azure.py), etc.
149-
150-
##### 2.4.6. Create the backend config class
151-
152-
Under the backend directory you've created, create the `config.py` file and define the
153-
backend config class there (should extend `dstack._internal.core.backends.base.config.BackendConfig`
154-
and the backend configuration model class defined above).
155-
156-
[//]: # (TODO: Mention what config class is and how it works)
157-
158-
Refer to examples:
159-
[datacrunch](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/datacrunch/config.py),
160-
[aws](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/aws/config.py),
161-
[gcp](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/gcp/config.py),
162-
[azure](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/azure/config.py), etc.
163-
164-
##### 2.4.7. Import config model classes
165-
166-
Ensure the config model classes are imported
167-
into [`src/dstack/_internal/core/models/backends/__init__.py`](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/models/backends/__init__.py).
144+
The models should be added to `AnyBackendConfig*` unions in [`src/dstack/_internal/core/backends/models.py`](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/models.py).
168145

169-
##### 2.4.8. Create the configurator class
146+
It's not required but recommended to define `*BackendStoredConfig` that extends `*BackendConfig` to be able to store extra parameters in the DB. By the same logic, it's recommended to define `*Config` that extends `*BackendStoredConfig` with creds and use it as the main `Backend` and `Compute` config instead of using `*BackendConfigWithCreds` directly.
170147

171-
Create the file with the backend name under `src/dstack/_internal/server/services/backends/configurators`(https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/server/services/backends/configurators)
172-
and define the backend configurator class (must extend `dstack._internal.server.services.backends.configurators.base.Configurator`).
173-
174-
Refer to examples: [datacrunch](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/server/services/backends/configurators/datacrunch.py),
175-
[aws](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/server/services/backends/configurators/aws.py),
176-
[gcp](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/server/services/backends/configurators/gcp.py),
177-
[azure](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/server/services/backends/configurators/azure.py), etc.
148+
Refer to examples:
149+
[datacrunch](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/datacrunch/models.py),
150+
[aws](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/aws/models.py),
151+
[gcp](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/gcp/models.py),
152+
[azure](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/models.py), etc.
178153

179-
##### 2.4.9. Create the server config class
154+
##### 2.4.6. Create and register the configurator class
180155

181-
In [`src/dstack/_internal/server/services/config.py`](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/server/services/config.py),
182-
define the corresponding server config class (that represents the `~/.dstack/server/config.yml` file),
183-
and add it to `AnyBackendConfig` (in the same file).
156+
Under the backend directory, create the `configurator.py` file and and define the backend configurator class (must extend `dstack._internal.core.backends.base.configurator.Configurator`).
184157

185-
##### 2.4.10. Add safe imports
158+
Refer to examples: [datacrunch](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/datacrunch/configurator.py),
159+
[aws](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/aws/configurator.py),
160+
[gcp](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/gcp/configurator.py),
161+
[azure](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/azure/configurator.py), etc.
186162

187-
In [`src/dstack/_internal/server/services/backends/__init__.py`](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/server/services/backends/__init__.py),
188-
add the `try`/`except` block that imports the backend configurator and appends it to `_CONFIGURATOR_CLASSES`.
163+
Register configurator by appending it to `_CONFIGURATOR_CLASSES` in [`src/dstack/_internal/core/backends/configurators.py`](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/core/backends/configurators.py).
189164

190-
##### 2.4.11. (Optional) Override provisioning timeout
165+
##### 2.4.7. (Optional) Override provisioning timeout
191166

192167
If instances in the backend take more than 10 minutes to start, override the default provisioning timeout in
193168
[`src/dstack/_internal/server/background/tasks/common.py`](https://github.com/dstackai/dstack/blob/master/src/dstack/_internal/server/background/tasks/common.py).

0 commit comments

Comments
 (0)