minco-dev 0.5.0

Deterministic local process plans and coordinated development supervision for Minco
Documentation
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Deterministic local development plans and coordinated process supervision.
#![forbid(unsafe_code)]

use serde::{Deserialize, Serialize, ser::SerializeStruct};
use std::collections::{BTreeMap, BTreeSet};
use thiserror::Error;

mod supervisor;

pub use supervisor::{DevEvent, DevStream, Supervisor, SupervisorError};

const SUPPORTED_LOCAL_AWS_SERVICES: &[&str] = &["dynamodb", "s3", "sqs", "ssm", "sts"];

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DevDatabase {
    Postgres,
    Sqlite,
    None,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct CommandSpec {
    pub program: String,
    #[serde(default)]
    pub arguments: Vec<String>,
    #[serde(default)]
    pub environment: BTreeMap<String, String>,
}

impl Serialize for CommandSpec {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let environment = self
            .environment
            .iter()
            .map(|(name, value)| {
                (
                    name.as_str(),
                    if is_sensitive_environment_name(name) {
                        "<redacted>"
                    } else {
                        value.as_str()
                    },
                )
            })
            .collect::<BTreeMap<_, _>>();
        let mut state = serializer.serialize_struct("CommandSpec", 3)?;
        state.serialize_field("program", &self.program)?;
        state.serialize_field("arguments", &self.arguments)?;
        state.serialize_field("environment", &environment)?;
        state.end()
    }
}

pub(crate) fn is_sensitive_environment_name(name: &str) -> bool {
    let name = name.to_ascii_uppercase();
    name.ends_with("_URL")
        || name.ends_with("_DSN")
        || name.ends_with("_KEY")
        || name.contains("_KEY_")
        || [
            "AUTHORIZATION",
            "COOKIE",
            "CREDENTIAL",
            "PASSPHRASE",
            "PASSWORD",
            "SECRET",
            "TOKEN",
        ]
        .iter()
        .any(|marker| name.contains(marker))
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ReadinessProbe {
    Process,
    Http { url: String },
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProcessConfig {
    pub id: String,
    pub command: CommandSpec,
    pub readiness: ReadinessProbe,
    #[serde(default)]
    pub default_enabled: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DevGraph {
    pub application: String,
    pub environment: String,
    pub compose_file: String,
    pub database: DevDatabase,
    #[serde(default)]
    pub local_aws_services: Vec<String>,
    pub api: ProcessConfig,
    #[serde(default)]
    pub workers: Vec<ProcessConfig>,
    pub frontend: Option<ProcessConfig>,
    pub migration: Option<CommandSpec>,
    #[serde(default)]
    pub seeds: BTreeMap<String, CommandSpec>,
    #[serde(default)]
    pub schedules: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DevOptions {
    pub profile: String,
    pub migrate: bool,
    pub seed: Option<String>,
    pub with_workers: BTreeSet<String>,
    pub without_workers: BTreeSet<String>,
    pub frontend: Option<bool>,
    pub port: Option<u16>,
    pub rustack_port: Option<u16>,
}

impl Default for DevOptions {
    fn default() -> Self {
        Self {
            profile: "default".into(),
            migrate: true,
            seed: None,
            with_workers: BTreeSet::new(),
            without_workers: BTreeSet::new(),
            frontend: None,
            port: None,
            rustack_port: None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ServiceKind {
    Postgres,
    Sqlite,
    Rustack,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ServicePlan {
    pub id: String,
    pub kind: ServiceKind,
    pub port: Option<u16>,
    pub local_only: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub aws_services: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start: Option<CommandSpec>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop: Option<CommandSpec>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LifecycleKind {
    Migrate,
    Seed,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LifecyclePlan {
    pub id: String,
    pub kind: LifecycleKind,
    pub command: CommandSpec,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProcessRole {
    Api,
    Worker,
    Frontend,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProcessPlan {
    pub id: String,
    pub role: ProcessRole,
    pub command: CommandSpec,
    pub readiness: ReadinessProbe,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DevPlan {
    pub schema_version: u32,
    pub application: String,
    pub environment: String,
    pub profile: String,
    pub external_aws_contact: bool,
    pub services: Vec<ServicePlan>,
    pub lifecycle: Vec<LifecyclePlan>,
    pub processes: Vec<ProcessPlan>,
    pub omitted_schedule_ids: Vec<String>,
}

impl DevPlan {
    pub fn derive(graph: &DevGraph, options: &DevOptions) -> Result<Self, DevPlanError> {
        let mut process_ids = BTreeSet::new();
        for process in std::iter::once(&graph.api)
            .chain(graph.workers.iter())
            .chain(graph.frontend.iter())
        {
            if !process_ids.insert(process.id.as_str()) {
                return Err(DevPlanError::Invalid(format!(
                    "duplicate development process identifier `{}`",
                    process.id
                )));
            }
        }
        if let Some(worker) = options
            .with_workers
            .intersection(&options.without_workers)
            .next()
        {
            return Err(DevPlanError::Invalid(format!(
                "worker `{worker}` cannot be both included and omitted"
            )));
        }
        let declared_workers = graph
            .workers
            .iter()
            .map(|worker| worker.id.as_str())
            .collect::<BTreeSet<_>>();
        for worker in options
            .with_workers
            .iter()
            .chain(options.without_workers.iter())
        {
            if !declared_workers.contains(worker.as_str()) {
                return Err(DevPlanError::Invalid(format!(
                    "worker `{worker}` is not declared"
                )));
            }
        }

        for service in &graph.local_aws_services {
            if !SUPPORTED_LOCAL_AWS_SERVICES.contains(&service.as_str()) {
                return Err(DevPlanError::Invalid(format!(
                    "local AWS service `{service}` is not supported"
                )));
            }
        }
        if options.frontend == Some(true) && graph.frontend.is_none() {
            return Err(DevPlanError::Invalid(
                "frontend was requested but development.frontend is not declared".into(),
            ));
        }

        let mut services = Vec::new();
        match graph.database {
            DevDatabase::Postgres => services.push(ServicePlan {
                id: "postgres".into(),
                kind: ServiceKind::Postgres,
                port: Some(55_432),
                local_only: true,
                aws_services: Vec::new(),
                start: Some(compose_command(
                    &graph.compose_file,
                    &["up", "-d", "--wait"],
                    "postgres",
                    BTreeMap::new(),
                )),
                stop: Some(compose_command(
                    &graph.compose_file,
                    &["stop"],
                    "postgres",
                    BTreeMap::new(),
                )),
            }),
            DevDatabase::Sqlite => services.push(ServicePlan {
                id: "sqlite".into(),
                kind: ServiceKind::Sqlite,
                port: None,
                local_only: true,
                aws_services: Vec::new(),
                start: None,
                stop: None,
            }),
            DevDatabase::None => {}
        }

        if !graph.local_aws_services.is_empty() {
            let mut aws_services = graph.local_aws_services.clone();
            aws_services.sort();
            aws_services.dedup();
            let rustack_port = options.rustack_port.unwrap_or(4_566);
            let environment = BTreeMap::from([
                ("MINCO_RUSTACK_PORT".into(), rustack_port.to_string()),
                ("MINCO_RUSTACK_SERVICES".into(), aws_services.join(",")),
            ]);
            services.push(ServicePlan {
                id: "rustack".into(),
                kind: ServiceKind::Rustack,
                port: Some(rustack_port),
                local_only: true,
                aws_services,
                start: Some(compose_command(
                    &graph.compose_file,
                    &["up", "-d", "--wait"],
                    "rustack",
                    environment,
                )),
                stop: Some(compose_command(
                    &graph.compose_file,
                    &["stop"],
                    "rustack",
                    BTreeMap::new(),
                )),
            });
        }

        let mut lifecycle = Vec::new();
        if options.migrate
            && let Some(command) = &graph.migration
        {
            lifecycle.push(LifecyclePlan {
                id: "migrate".into(),
                kind: LifecycleKind::Migrate,
                command: command.clone(),
            });
        }
        if let Some(seed) = &options.seed {
            let command = graph.seeds.get(seed).ok_or_else(|| {
                DevPlanError::Invalid(format!("seed profile `{seed}` is not declared"))
            })?;
            lifecycle.push(LifecyclePlan {
                id: format!("seed:{seed}"),
                kind: LifecycleKind::Seed,
                command: command.clone(),
            });
        }

        let mut api_command = graph.api.command.clone();
        if let Some(port) = options.port {
            api_command
                .environment
                .insert("PORT".into(), port.to_string());
        }
        let api_readiness = override_readiness_port(&graph.api.readiness, options.port)?;
        let mut processes = vec![ProcessPlan {
            id: graph.api.id.clone(),
            role: ProcessRole::Api,
            command: api_command,
            readiness: api_readiness,
        }];
        let mut workers = graph
            .workers
            .iter()
            .filter(|worker| {
                (worker.default_enabled || options.with_workers.contains(&worker.id))
                    && !options.without_workers.contains(&worker.id)
            })
            .map(|worker| ProcessPlan {
                id: worker.id.clone(),
                role: ProcessRole::Worker,
                command: worker.command.clone(),
                readiness: worker.readiness.clone(),
            })
            .collect::<Vec<_>>();
        workers.sort_by(|left, right| left.id.cmp(&right.id));
        processes.extend(workers);

        if let Some(frontend) = graph
            .frontend
            .as_ref()
            .filter(|frontend| options.frontend.unwrap_or(frontend.default_enabled))
        {
            processes.push(ProcessPlan {
                id: frontend.id.clone(),
                role: ProcessRole::Frontend,
                command: frontend.command.clone(),
                readiness: frontend.readiness.clone(),
            });
        }

        let mut omitted_schedule_ids = graph.schedules.clone();
        omitted_schedule_ids.sort();
        omitted_schedule_ids.dedup();

        Ok(Self {
            schema_version: 1,
            application: graph.application.clone(),
            environment: graph.environment.clone(),
            profile: options.profile.clone(),
            external_aws_contact: false,
            services,
            lifecycle,
            processes,
            omitted_schedule_ids,
        })
    }
}

fn override_readiness_port(
    readiness: &ReadinessProbe,
    port: Option<u16>,
) -> Result<ReadinessProbe, DevPlanError> {
    let (ReadinessProbe::Http { url }, Some(port)) = (readiness, port) else {
        return Ok(readiness.clone());
    };
    let mut url = reqwest::Url::parse(url)
        .map_err(|_| DevPlanError::Invalid("API readiness URL is invalid".into()))?;
    url.set_port(Some(port))
        .map_err(|()| DevPlanError::Invalid("API readiness URL cannot accept a port".into()))?;
    Ok(ReadinessProbe::Http { url: url.into() })
}

fn compose_command(
    compose_file: &str,
    action: &[&str],
    service: &str,
    environment: BTreeMap<String, String>,
) -> CommandSpec {
    let mut arguments = vec!["compose".into(), "-f".into(), compose_file.into()];
    arguments.extend(action.iter().map(ToString::to_string));
    arguments.push(service.into());
    CommandSpec {
        program: "docker".into(),
        arguments,
        environment,
    }
}

#[derive(Debug, Error, PartialEq, Eq)]
pub enum DevPlanError {
    #[error("invalid development plan: {0}")]
    Invalid(String),
}