obelisk 0.41.5

Deterministic workflow engine
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
use crate::args::{self, DeploymentSource};
use crate::client::{ClientStartup, send_bytes, send_json};
use crate::config::deployment::sanitize_deployment_relative_path;
use crate::config::deployment::{
    PreparedDeploymentManifest, prepare_deployment_manifest_from_disk,
};
use crate::server::web_api_server::deployment::{
    DeploymentRecordSer, DeploymentStateSer, DeploymentStatusSer, DeploymentSubmitPayload,
    DeploymentSubmitResponse, DeploymentSwitchPayload, GcOrphanFilesResponseSer,
    SubmitPackageErrorBody,
};
use anyhow::{Context as _, bail};
use concepts::prefixed_ulid::DeploymentId;
use http::header::ACCEPT;
use serde::Deserialize;
use std::path::PathBuf;

impl args::Deployment {
    pub(crate) async fn run(self, client_startup: ClientStartup) -> Result<(), anyhow::Error> {
        match self {
            args::Deployment::Submit {
                file,
                empty,
                allow_unavailable_runtime_config,
                description,
                deployment_id,
                api_url,
            } => {
                let prepared = prepare_manifest_from_file_or_empty(file, empty).await?;
                let id = upload_and_submit_manifest(
                    &client_startup,
                    &api_url,
                    prepared,
                    allow_unavailable_runtime_config,
                    description,
                    deployment_id,
                )
                .await?;
                println!("{id}");
                Ok(())
            }

            args::Deployment::Enqueue {
                source,
                empty,
                allow_unavailable_runtime_config,
                description,
                deployment_id,
                api_url,
            } => {
                let id = submit_deployment(
                    &client_startup,
                    &api_url,
                    source,
                    empty,
                    allow_unavailable_runtime_config,
                    description,
                    deployment_id,
                )
                .await?;
                switch_deployment(
                    &client_startup,
                    &api_url,
                    id,
                    allow_unavailable_runtime_config,
                    SwitchCommand::Enqueue,
                )
                .await
            }

            args::Deployment::Apply {
                source,
                empty,
                description,
                deployment_id,
                api_url,
            } => {
                let id = submit_deployment(
                    &client_startup,
                    &api_url,
                    source,
                    empty,
                    false,
                    description,
                    deployment_id,
                )
                .await?;
                switch_deployment(&client_startup, &api_url, id, false, SwitchCommand::Apply).await
            }

            args::Deployment::List { api_url } => {
                let client = client_startup.web_api_client()?;
                let deployments: Vec<DeploymentStateSer> = send_json(
                    client
                        .get(format!("{api_url}/v1/deployments"))
                        .header(ACCEPT, "application/json"),
                )
                .await?;

                if deployments.is_empty() {
                    println!("No deployments found.");
                    return Ok(());
                }

                println!(
                    "{:<32}  {:<12}  {:<19}  {:<19}  DESCRIPTION",
                    "ID", "STATUS", "CREATED_AT", "LAST_ACTIVE_AT"
                );
                for dep in deployments {
                    let id = dep.deployment_id;
                    let status = format_status(&dep.status);
                    let created = dep.created_at.format("%Y-%m-%d %H:%M:%S");
                    let last_active = dep
                        .last_active_at
                        .map(|t| t.format("%Y-%m-%d %H:%M:%S").to_string())
                        .unwrap_or_default();
                    println!(
                        "{id:<32}  {status:<12}  {created:<19}  {last_active:<19}  {}",
                        dep.description.unwrap_or_default()
                    );
                }
                Ok(())
            }

            args::Deployment::Gc { api_url } => {
                let client = client_startup.web_api_client()?;
                let resp: GcOrphanFilesResponseSer = send_json(
                    client
                        .delete(format!("{api_url}/v1/files/orphans"))
                        .header(ACCEPT, "application/json"),
                )
                .await?;
                println!("Deleted {} orphan file blob(s).", resp.deleted_count);
                Ok(())
            }

            args::Deployment::Active { api_url, json } => {
                let client = client_startup.web_api_client()?;
                let id: DeploymentId = send_json(
                    client
                        .get(format!("{api_url}/v1/deployment-id"))
                        .header(ACCEPT, "application/json"),
                )
                .await?;
                if json {
                    println!("\"{id}\"");
                } else {
                    println!("{id}");
                }
                Ok(())
            }

            args::Deployment::Show {
                id,
                file,
                json,
                api_url,
            } => {
                let client = client_startup.web_api_client()?;
                let dep: DeploymentRecordSer = send_json(
                    client
                        .get(format!("{api_url}/v1/deployments/{id}"))
                        .header(ACCEPT, "application/json")
                        .query(&[("include_generated_metadata", "true")]),
                )
                .await?;
                let deployment_toml = dep.deployment_toml;

                if let Some(file) = file {
                    // Normalize the requested path the same way `get` writes it, so a
                    // `./scripts/x` or `scripts//x` still matches the stored ref path.
                    let rel = sanitize_deployment_relative_path(&file)
                        .with_context(|| format!("invalid source path `{file}`"))?;
                    let file_ref = dep.files.iter().find(|f| f.path == rel).with_context(|| {
                        format!("deployment {id} has no deployment-owned source file `{rel}`")
                    })?;
                    let bytes = fetch_file(&client, &api_url, &file_ref.digest).await?;
                    print!("{}", String::from_utf8_lossy(&bytes));
                    return Ok(());
                }

                if json {
                    // The manifest is the source of truth; render it as JSON for tooling.
                    let value: toml::Value = toml::from_str(&deployment_toml)
                        .context("cannot parse stored deployment manifest")?;
                    println!("{}", serde_json::to_string_pretty(&value)?);
                    return Ok(());
                }

                print!("{deployment_toml}");
                Ok(())
            }

            args::Deployment::Get {
                id,
                output,
                force,
                include_generated_metadata,
                api_url,
            } => {
                let client = client_startup.web_api_client()?;
                let dep: DeploymentRecordSer = send_json(
                    client
                        .get(format!("{api_url}/v1/deployments/{id}"))
                        .header(ACCEPT, "application/json")
                        .query(&[(
                            "include_generated_metadata",
                            include_generated_metadata.to_string(),
                        )]),
                )
                .await?;
                let deployment_toml = dep.deployment_toml;

                let output_dir = output.unwrap_or_else(|| PathBuf::from("."));
                tokio::fs::create_dir_all(&output_dir)
                    .await
                    .with_context(|| format!("cannot create output directory {output_dir:?}"))?;

                let toml_path = output_dir.join("deployment.toml");
                write_new_file(&toml_path, deployment_toml.as_bytes(), force).await?;
                let file_count = dep.files.len();
                for file_ref in &dep.files {
                    // Defensively re-validate the relative path so a malformed stored
                    // manifest can never write outside the output directory.
                    let rel =
                        sanitize_deployment_relative_path(&file_ref.path).with_context(|| {
                            format!("refusing to write unsafe source path `{}`", file_ref.path)
                        })?;
                    let path = output_dir.join(&rel);
                    if let Some(parent) = path.parent() {
                        tokio::fs::create_dir_all(parent).await.with_context(|| {
                            format!("cannot create source directory {parent:?}")
                        })?;
                    }
                    let bytes = fetch_file(&client, &api_url, &file_ref.digest).await?;
                    write_new_file(&path, &bytes, force).await?;
                }
                println!(
                    "Wrote {} ({file_count} source file{}) for deployment {id}",
                    toml_path.display(),
                    if file_count == 1 { "" } else { "s" }
                );
                Ok(())
            }

            // `deployment verify` is dispatched through the local verification path in `main`.
            args::Deployment::Verify(_) => unreachable!("handled in main before ClientStartup"),
        }
    }
}

/// Write `contents` to `path`. Refuses to overwrite an existing file unless `force`.
async fn write_new_file(
    path: &std::path::Path,
    contents: &[u8],
    force: bool,
) -> anyhow::Result<()> {
    use tokio::io::AsyncWriteExt as _;
    let mut file = tokio::fs::OpenOptions::new()
        .write(true)
        .create(true) // allow creating new files
        .truncate(true) // truncate when overwriting is permitted
        .create_new(!force) // when set, only new-file creation is allowed (no overwrite)
        .open(path)
        .await
        .with_context(|| {
            format!(
                "cannot open {path:?} for writing{}",
                if force { "" } else { ", try using `--force`" }
            )
        })?;
    file.write_all(contents)
        .await
        .with_context(|| format!("cannot write {path:?}"))?;
    Ok(())
}

/// If the source is a file, submit it and return the new ID. If it's an ID, return it directly.
/// If `empty`, submit an empty deployment and return the new ID.
async fn submit_deployment(
    client_startup: &ClientStartup,
    api_url: &str,
    source: Option<DeploymentSource>,
    empty: bool,
    allow_unavailable_runtime_config: bool,
    description: Option<String>,
    deployment_id: Option<DeploymentId>,
) -> anyhow::Result<DeploymentId> {
    assert_ne!(source.is_some(), empty);
    let prepared = match source {
        Some(DeploymentSource::Id(id)) => {
            if description.is_some() {
                bail!("--description cannot be used with an existing deployment ID");
            }
            if deployment_id.is_some() {
                bail!("--deployment-id cannot be used with an existing deployment ID source");
            }
            return Ok(id);
        }
        Some(DeploymentSource::File(path)) => prepare_deployment_manifest_from_disk(&path).await?,
        None => prepare_manifest_from_file_or_empty(None, empty).await?,
    };
    let id = upload_and_submit_manifest(
        client_startup,
        api_url,
        prepared,
        allow_unavailable_runtime_config,
        description,
        deployment_id,
    )
    .await?;
    println!("Submitted as {id}");
    Ok(id)
}

/// Submit the enriched manifest as a CAS-efficient package: first without file
/// blobs, and if the server reports missing files, retry the same submit with only
/// those blobs attached. The requested TOML is identical on both attempts.
async fn upload_and_submit_manifest(
    client_startup: &ClientStartup,
    api_url: &str,
    prepared: PreparedDeploymentManifest,
    allow_unavailable_runtime_config: bool,
    description: Option<String>,
    deployment_id: Option<DeploymentId>,
) -> anyhow::Result<DeploymentId> {
    let client = client_startup.web_api_client()?;
    // Preflight: no blobs, so digests already in the CAS are not re-uploaded.
    let missing = match submit_attempt(
        &client,
        api_url,
        &prepared,
        allow_unavailable_runtime_config,
        description.as_deref(),
        deployment_id,
        &[],
    )
    .await?
    {
        SubmitAttempt::Stored(id) => return Ok(id),
        SubmitAttempt::Missing(digests) => digests,
    };

    // Retry with only the blobs the server is missing.
    let files: Vec<_> = prepared
        .files
        .iter()
        .filter(|file| missing.contains(&file.digest.to_string()))
        .collect();
    match submit_attempt(
        &client,
        api_url,
        &prepared,
        allow_unavailable_runtime_config,
        description.as_deref(),
        deployment_id,
        &files,
    )
    .await?
    {
        SubmitAttempt::Stored(id) => Ok(id),
        SubmitAttempt::Missing(digests) => bail!(
            "server is still missing {} file blob(s) after upload: {}",
            digests.len(),
            digests.join(", ")
        ),
    }
}

enum SubmitAttempt {
    Stored(DeploymentId),
    /// Digests the server is missing; the deployment was not stored.
    Missing(Vec<String>),
}

/// One submit request. A conflict carrying only `missing_files` becomes `Missing`.
async fn submit_attempt(
    client: &reqwest::Client,
    api_url: &str,
    prepared: &PreparedDeploymentManifest,
    allow_unavailable_runtime_config: bool,
    description: Option<&str>,
    deployment_id: Option<DeploymentId>,
    files: &[&crate::config::deployment::DeploymentManifestFile],
) -> anyhow::Result<SubmitAttempt> {
    let url = format!("{api_url}/v1/deployments");
    let request = if files.is_empty() {
        client
            .post(url)
            .header(ACCEPT, "application/json")
            .json(&DeploymentSubmitPayload {
                deployment_toml: prepared.deployment_toml.clone(),
                description: description.map(str::to_string),
                allow_unavailable_runtime_config,
                deployment_id: deployment_id.map(|id| id.to_string()),
            })
    } else {
        let mut form = reqwest::multipart::Form::new()
            .text("deployment_toml", prepared.deployment_toml.clone())
            .text(
                "allow_unavailable_runtime_config",
                allow_unavailable_runtime_config.to_string(),
            );
        if let Some(description) = description {
            form = form.text("description", description.to_string());
        }
        if let Some(deployment_id) = deployment_id {
            form = form.text("deployment_id", deployment_id.to_string());
        }
        for file in files {
            form = form.part(
                file.digest.to_string(),
                reqwest::multipart::Part::bytes(file.bytes.clone()).file_name(file.path.clone()),
            );
        }
        client
            .post(url)
            .header(ACCEPT, "application/json")
            .multipart(form)
    };
    let response = request.send().await?;
    let status = response.status();
    if status.is_success() {
        let response: DeploymentSubmitResponse = response.json().await?;
        return Ok(SubmitAttempt::Stored(response.deployment_id.parse()?));
    }
    if status == reqwest::StatusCode::CONFLICT {
        let detail: SubmitPackageErrorBody = response.json().await?;
        let only_missing = detail.unexpected_files.is_empty()
            && detail.digest_mismatches.is_empty()
            && detail.oversized_files.is_empty()
            && detail.missing_digest_fields.is_empty()
            && !detail.missing_files.is_empty();
        if only_missing {
            return Ok(SubmitAttempt::Missing(
                detail
                    .missing_files
                    .iter()
                    .filter_map(|issue| issue.digest.clone())
                    .collect(),
            ));
        }
        bail!(
            "deployment submit rejected: {}",
            format_submit_detail(&detail)
        );
    }
    let body = response.text().await.unwrap_or_default();
    bail!("server returned {status}: {body}")
}

fn format_submit_detail(detail: &SubmitPackageErrorBody) -> String {
    let mut lines = Vec::new();
    for issue in &detail.missing_digest_fields {
        lines.push(format!("missing content_digest at {}", issue.field_path));
    }
    for issue in &detail.missing_files {
        lines.push(format!(
            "missing file at {} ({})",
            issue.field_path, issue.message
        ));
    }
    for issue in &detail.unexpected_files {
        lines.push(format!("unexpected file {}", issue.field_path));
    }
    for mismatch in &detail.digest_mismatches {
        lines.push(format!(
            "digest mismatch for {}: supplied {}, actual {}",
            mismatch.file.field_path, mismatch.supplied_digest, mismatch.actual_digest
        ));
    }
    for issue in &detail.oversized_files {
        lines.push(format!("oversized file {}", issue.field_path));
    }
    lines.join("; ")
}

/// Fetch a deployment file blob from the server's content-addressed store.
async fn fetch_file(
    client: &reqwest::Client,
    api_url: &str,
    digest: &str,
) -> anyhow::Result<bytes::Bytes> {
    send_bytes(client.get(format!("{api_url}/v1/files/{digest}")))
        .await
        .with_context(|| format!("cannot fetch deployment file `{digest}`"))
}

async fn switch_deployment(
    client_startup: &ClientStartup,
    api_url: &str,
    id: DeploymentId,
    allow_unavailable_runtime_config: bool,
    command: SwitchCommand,
) -> anyhow::Result<()> {
    #[derive(Deserialize)]
    struct SwitchResponse {
        ok: String,
    }
    let client = client_startup.web_api_client()?;
    let response: SwitchResponse = send_json(
        client
            .put(format!("{api_url}/v1/deployments/{id}/switch"))
            .header(ACCEPT, "application/json")
            .json(&DeploymentSwitchPayload {
                allow_unavailable_runtime_config,
                apply: command == SwitchCommand::Apply,
            }),
    )
    .await?;

    match (command, response.ok.as_str()) {
        (SwitchCommand::Apply, "switched") => {
            println!("Applied successfully.");
        }
        (SwitchCommand::Apply, "restart_required") => {
            bail!("Could not apply immediately; deployment enqueued. Restart the server to apply.");
        }
        (SwitchCommand::Enqueue, "switched") => {
            println!("Deployment already active; it will remain active after restart.");
        }
        (SwitchCommand::Enqueue, "restart_required") => {
            println!("Deployment enqueued. Restart the server to apply.");
        }
        (_, outcome) => bail!("unexpected outcome from server: {outcome}"),
    }
    Ok(())
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SwitchCommand {
    Apply,
    Enqueue,
}

async fn prepare_manifest_from_file_or_empty(
    file: Option<std::path::PathBuf>,
    empty: bool,
) -> anyhow::Result<PreparedDeploymentManifest> {
    assert_ne!(file.is_some(), empty);
    if let Some(path) = file {
        prepare_deployment_manifest_from_disk(&path).await
    } else {
        Ok(PreparedDeploymentManifest::empty())
    }
}

fn format_status(status: &DeploymentStatusSer) -> &'static str {
    match status {
        DeploymentStatusSer::Inactive => "Inactive",
        DeploymentStatusSer::Enqueued => "Enqueued",
        DeploymentStatusSer::Active => "Active",
    }
}