-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuilder.rs
More file actions
326 lines (298 loc) · 10.7 KB
/
Copy pathbuilder.rs
File metadata and controls
326 lines (298 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//! Fluent builder for [`EnvironmentCreationConfig`].
//!
//! Provides a type-safe, ergonomic way to construct deployment configurations
//! without hand-crafting JSON strings.
use thiserror::Error;
use super::environment_config::{EnvironmentCreationConfig, EnvironmentSection};
use super::provider::{HetznerProviderSection, LxdProviderSection, ProviderSection};
use super::ssh_credentials_config::SshCredentialsConfig;
use super::tracker::{
DatabaseSection, HealthCheckApiSection, HttpApiSection, HttpTrackerSection, TrackerCoreSection,
TrackerSection, UdpTrackerSection,
};
/// Default health-check API bind address used when none is set.
const DEFAULT_HEALTH_CHECK_BIND: &str = "127.0.0.1:1313";
/// Errors that can occur when building an [`EnvironmentCreationConfig`].
#[derive(Debug, Error)]
pub enum EnvironmentCreationConfigBuildError {
/// No environment name was set.
#[error("missing required field: name — call .name(\"my-env\")")]
MissingName,
/// No SSH private key path was set.
#[error(
"missing required field: SSH private key path — call .ssh_keys(private_path, public_path)"
)]
MissingPrivateKey,
/// No SSH public key path was set.
#[error(
"missing required field: SSH public key path — call .ssh_keys(private_path, public_path)"
)]
MissingPublicKey,
/// No provider was set.
#[error(
"missing required field: provider — call .provider_lxd(profile) or .provider_hetzner(…)"
)]
MissingProvider,
/// No database driver was set.
#[error("missing required field: database — call .sqlite(db_name) or .mysql(…)")]
MissingDatabase,
/// No HTTP API was set.
#[error("missing required field: HTTP API — call .api(bind_address, admin_token)")]
MissingApi,
}
/// Fluent builder for [`EnvironmentCreationConfig`].
///
/// Construct it via [`EnvironmentCreationConfig::builder`] or
/// [`EnvironmentCreationConfigBuilder::new`].
///
/// # Required fields
///
/// | Method | Sets |
/// |--------|------|
/// | [`name`](Self::name) | environment name |
/// | [`ssh_keys`](Self::ssh_keys) | private & public key paths |
/// | [`provider_lxd`](Self::provider_lxd) / [`provider_hetzner`](Self::provider_hetzner) | VM provider |
/// | [`sqlite`](Self::sqlite) / [`mysql`](Self::mysql) | tracker database |
/// | [`api`](Self::api) | HTTP management API |
///
/// # Optional fields
///
/// | Method | Default |
/// |--------|---------|
/// | [`udp`](Self::udp) | none (call once per listener) |
/// | [`http`](Self::http) | none (call once per listener) |
/// | [`ssh_username`](Self::ssh_username) | `"torrust"` |
/// | [`ssh_port`](Self::ssh_port) | `22` |
/// | [`private`](Self::private) | `false` (public tracker) |
/// | [`health_check`](Self::health_check) | `"127.0.0.1:1313"` |
///
/// # Example
///
/// ```rust,no_run
/// use torrust_tracker_deployer_lib::application::command_handlers::create::config::EnvironmentCreationConfig;
///
/// let config = EnvironmentCreationConfig::builder()
/// .name("my-tracker")
/// .ssh_keys("/path/to/key", "/path/to/key.pub")
/// .provider_lxd("torrust-profile")
/// .sqlite("tracker.db")
/// .udp("0.0.0.0:6969")
/// .http("0.0.0.0:7070")
/// .api("0.0.0.0:1212", "MyToken")
/// .build()
/// .expect("Failed to build configuration");
/// ```
#[derive(Debug, Default)]
pub struct EnvironmentCreationConfigBuilder {
name: Option<String>,
ssh_private_key: Option<String>,
ssh_public_key: Option<String>,
ssh_username: Option<String>,
ssh_port: Option<u16>,
provider: Option<ProviderSection>,
database: Option<DatabaseSection>,
private_tracker: bool,
udp_trackers: Vec<UdpTrackerSection>,
http_trackers: Vec<HttpTrackerSection>,
api_bind_address: Option<String>,
api_admin_token: Option<String>,
health_check_bind_address: Option<String>,
}
impl EnvironmentCreationConfigBuilder {
/// Create a new empty builder.
///
/// Prefer [`EnvironmentCreationConfig::builder`] at call sites.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Set the environment name (required).
#[must_use]
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
/// Set the SSH key pair paths (required).
#[must_use]
pub fn ssh_keys(
mut self,
private_key_path: impl Into<String>,
public_key_path: impl Into<String>,
) -> Self {
self.ssh_private_key = Some(private_key_path.into());
self.ssh_public_key = Some(public_key_path.into());
self
}
/// Override the SSH username (optional, default: `"torrust"`).
#[must_use]
pub fn ssh_username(mut self, username: impl Into<String>) -> Self {
self.ssh_username = Some(username.into());
self
}
/// Override the SSH port (optional, default: `22`).
#[must_use]
pub fn ssh_port(mut self, port: u16) -> Self {
self.ssh_port = Some(port);
self
}
/// Use the LXD provider (required unless `provider_hetzner` is called).
#[must_use]
pub fn provider_lxd(mut self, profile_name: impl Into<String>) -> Self {
self.provider = Some(ProviderSection::Lxd(LxdProviderSection {
profile_name: profile_name.into(),
}));
self
}
/// Use the Hetzner provider (required unless `provider_lxd` is called).
#[must_use]
pub fn provider_hetzner(
mut self,
api_token: impl Into<String>,
server_type: impl Into<String>,
location: impl Into<String>,
image: impl Into<String>,
) -> Self {
self.provider = Some(ProviderSection::Hetzner(HetznerProviderSection {
api_token: api_token.into(),
server_type: server_type.into(),
location: location.into(),
image: image.into(),
}));
self
}
/// Use `SQLite` as the tracker database (required unless `mysql` is called).
#[must_use]
pub fn sqlite(mut self, database_name: impl Into<String>) -> Self {
self.database = Some(DatabaseSection::Sqlite {
database_name: database_name.into(),
});
self
}
/// Use `MySQL` as the tracker database (required unless `sqlite` is called).
#[must_use]
pub fn mysql(
mut self,
host: impl Into<String>,
port: u16,
database_name: impl Into<String>,
username: impl Into<String>,
password: impl Into<String>,
) -> Self {
self.database = Some(DatabaseSection::Mysql {
host: host.into(),
port,
database_name: database_name.into(),
username: username.into(),
password: password.into(),
root_password: None,
});
self
}
/// Set the tracker privacy mode (optional, default: `false` = public).
#[must_use]
pub fn private(mut self, private: bool) -> Self {
self.private_tracker = private;
self
}
/// Add a UDP tracker listener (optional, repeatable).
#[must_use]
pub fn udp(mut self, bind_address: impl Into<String>) -> Self {
self.udp_trackers.push(UdpTrackerSection {
bind_address: bind_address.into(),
domain: None,
});
self
}
/// Add an HTTP tracker listener (optional, repeatable).
#[must_use]
pub fn http(mut self, bind_address: impl Into<String>) -> Self {
self.http_trackers.push(HttpTrackerSection {
bind_address: bind_address.into(),
domain: None,
use_tls_proxy: None,
});
self
}
/// Set the HTTP management API bind address and admin token (required).
#[must_use]
pub fn api(mut self, bind_address: impl Into<String>, admin_token: impl Into<String>) -> Self {
self.api_bind_address = Some(bind_address.into());
self.api_admin_token = Some(admin_token.into());
self
}
/// Override the health-check API bind address (optional, default: `"127.0.0.1:1313"`).
#[must_use]
pub fn health_check(mut self, bind_address: impl Into<String>) -> Self {
self.health_check_bind_address = Some(bind_address.into());
self
}
/// Build the [`EnvironmentCreationConfig`].
///
/// # Errors
///
/// Returns [`EnvironmentCreationConfigBuildError`] when any required field is missing.
pub fn build(self) -> Result<EnvironmentCreationConfig, EnvironmentCreationConfigBuildError> {
let name = self
.name
.ok_or(EnvironmentCreationConfigBuildError::MissingName)?;
let private_key_path = self
.ssh_private_key
.ok_or(EnvironmentCreationConfigBuildError::MissingPrivateKey)?;
let public_key_path = self
.ssh_public_key
.ok_or(EnvironmentCreationConfigBuildError::MissingPublicKey)?;
let provider = self
.provider
.ok_or(EnvironmentCreationConfigBuildError::MissingProvider)?;
let database = self
.database
.ok_or(EnvironmentCreationConfigBuildError::MissingDatabase)?;
let api_bind_address = self
.api_bind_address
.ok_or(EnvironmentCreationConfigBuildError::MissingApi)?;
let api_admin_token = self
.api_admin_token
.ok_or(EnvironmentCreationConfigBuildError::MissingApi)?;
let ssh_credentials = SshCredentialsConfig {
private_key_path,
public_key_path,
username: self.ssh_username.unwrap_or_else(|| "torrust".to_string()),
port: self.ssh_port.unwrap_or(22),
};
let tracker = TrackerSection {
core: TrackerCoreSection {
database,
private: self.private_tracker,
},
udp_trackers: self.udp_trackers,
http_trackers: self.http_trackers,
http_api: HttpApiSection {
bind_address: api_bind_address,
admin_token: api_admin_token,
domain: None,
use_tls_proxy: None,
},
health_check_api: HealthCheckApiSection {
bind_address: self
.health_check_bind_address
.unwrap_or_else(|| DEFAULT_HEALTH_CHECK_BIND.to_string()),
domain: None,
use_tls_proxy: None,
},
};
Ok(EnvironmentCreationConfig {
environment: EnvironmentSection {
name,
description: None,
instance_name: None,
},
ssh_credentials,
provider,
tracker,
prometheus: None,
grafana: None,
https: None,
backup: None,
})
}
}