202 lines
6.3 KiB
Rust
202 lines
6.3 KiB
Rust
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
//! Apply records (one per user action: the history the UI shows, docs/ui-api.md §2.1) and the
|
|
//! apply journal (what a crash interrupted).
|
|
//!
|
|
//! The journal (`journal.json`) is written, atomically and durably, before every step that
|
|
//! changes the outside world, so after a crash control knows how far an apply got:
|
|
//!
|
|
//! | stage | what has happened | recovery |
|
|
//! |:--|:--|:--|
|
|
//! | `prepared` | commit object made, branch not moved | branch at base: nothing to undo; at the commit: as `committed` |
|
|
//! | `committed` | branch moved to the commit | revert commit, abort trial, drop trial files |
|
|
//! | `trial_written` | trial files written | same |
|
|
//! | `trial_loaded` | edge serves the trial (and reverts by itself at its deadline) | same |
|
|
//! | `confirmed` | edge confirmed the trial | promote trial files, record kept |
|
|
//! | `promoted` | files promoted | record kept |
|
|
//! | `reverting` | revert started | finish the revert |
|
|
//!
|
|
//! Throughout, `edge.json` and `caddy.json` only ever hold a config that passed its watch.
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use utoipa::ToSchema;
|
|
|
|
use crate::hostspec::HostSpec;
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, ToSchema)]
|
|
pub struct Step {
|
|
/// `validate`, `compile`, `write`, `reload`, `watch` or `push`.
|
|
pub key: String,
|
|
/// `pending`, `running`, `done`, `failed` or `skipped`.
|
|
pub state: String,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default, ToSchema)]
|
|
pub struct WatchHost {
|
|
pub hostname: String,
|
|
pub passed: u32,
|
|
pub failed: u32,
|
|
pub total: u32,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, ToSchema)]
|
|
pub struct Watch {
|
|
/// Unix seconds.
|
|
pub ends: u64,
|
|
pub hosts: Vec<WatchHost>,
|
|
}
|
|
|
|
/// Why an apply did not stay (or a detail of why it did).
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default, ToSchema)]
|
|
pub struct Reason {
|
|
pub code: String,
|
|
/// Plain English, from `messages`.
|
|
pub message: String,
|
|
pub hostname: Option<String>,
|
|
pub upstream: Option<String>,
|
|
pub field: Option<String>,
|
|
pub count: Option<u32>,
|
|
pub of: Option<u32>,
|
|
/// Technical detail (core's text, edge's error); never a secret.
|
|
pub detail: Option<String>,
|
|
#[serde(default)]
|
|
pub params: BTreeMap<String, String>,
|
|
}
|
|
|
|
/// One field problem, in the API error shape.
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, ToSchema)]
|
|
pub struct FieldError {
|
|
pub code: String,
|
|
pub message: String,
|
|
pub field: Option<String>,
|
|
pub params: BTreeMap<String, String>,
|
|
pub detail: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
pub struct ApplyRecord {
|
|
pub id: String,
|
|
/// `add`, `edit`, `remove`, `undo`, `restore` or `share_links`.
|
|
pub kind: String,
|
|
pub summary: String,
|
|
pub hosts: Vec<String>,
|
|
/// `queued`, `validating`, `applying`, `watching`, `kept`, `reverted`, `failed` or
|
|
/// `needs_attention`.
|
|
pub state: String,
|
|
pub steps: Vec<Step>,
|
|
pub created: u64,
|
|
pub started: Option<u64>,
|
|
pub finished: Option<u64>,
|
|
pub watch: Option<Watch>,
|
|
pub reason: Option<Reason>,
|
|
#[serde(default)]
|
|
pub errors: Vec<FieldError>,
|
|
/// Branch head when the apply started.
|
|
pub base: Option<String>,
|
|
/// The commit of this change (none if it never got one).
|
|
pub commit: Option<String>,
|
|
/// The commit that undid it, for reverted applies.
|
|
pub revert_commit: Option<String>,
|
|
pub undo_of: Option<String>,
|
|
pub restore_of: Option<String>,
|
|
/// Token name of whoever asked.
|
|
pub actor: String,
|
|
/// What was tried, for "Edit and try again".
|
|
pub attempted_host: Option<HostSpec>,
|
|
/// For undo: what it does ("Removes x").
|
|
pub effect: Option<String>,
|
|
}
|
|
|
|
pub const STEP_KEYS: [&str; 6] = ["validate", "compile", "write", "reload", "watch", "push"];
|
|
|
|
impl ApplyRecord {
|
|
pub fn new(
|
|
id: String,
|
|
kind: &str,
|
|
summary: String,
|
|
hosts: Vec<String>,
|
|
actor: &str,
|
|
t: u64,
|
|
) -> ApplyRecord {
|
|
ApplyRecord {
|
|
id,
|
|
kind: kind.into(),
|
|
summary,
|
|
hosts,
|
|
state: "queued".into(),
|
|
steps: STEP_KEYS
|
|
.iter()
|
|
.map(|k| Step {
|
|
key: (*k).into(),
|
|
state: "pending".into(),
|
|
})
|
|
.collect(),
|
|
created: t,
|
|
started: None,
|
|
finished: None,
|
|
watch: None,
|
|
reason: None,
|
|
errors: Vec::new(),
|
|
base: None,
|
|
commit: None,
|
|
revert_commit: None,
|
|
undo_of: None,
|
|
restore_of: None,
|
|
actor: actor.into(),
|
|
attempted_host: None,
|
|
effect: None,
|
|
}
|
|
}
|
|
|
|
pub fn step(&mut self, key: &str, state: &str) {
|
|
if let Some(s) = self.steps.iter_mut().find(|s| s.key == key) {
|
|
s.state = state.into();
|
|
}
|
|
}
|
|
|
|
pub fn is_active(&self) -> bool {
|
|
matches!(
|
|
self.state.as_str(),
|
|
"queued" | "validating" | "applying" | "watching"
|
|
)
|
|
}
|
|
|
|
pub fn is_running(&self) -> bool {
|
|
matches!(self.state.as_str(), "validating" | "applying" | "watching")
|
|
}
|
|
|
|
/// A kept change with a commit: something Undo can target.
|
|
pub fn is_kept_change(&self) -> bool {
|
|
self.state == "kept" && self.commit.is_some()
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Journal {
|
|
pub apply_id: String,
|
|
pub stage: String,
|
|
pub base: String,
|
|
pub commit: String,
|
|
pub edge_sha256: Option<String>,
|
|
pub reason: Option<Reason>,
|
|
}
|
|
|
|
/// Push state, in `push.json`.
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct PushState {
|
|
pub pushed: Option<String>,
|
|
pub last_push_at: Option<u64>,
|
|
pub last_attempt_at: Option<u64>,
|
|
pub next_try_at: Option<u64>,
|
|
pub error_code: Option<String>,
|
|
pub error_detail: Option<String>,
|
|
#[serde(default)]
|
|
pub failures: u32,
|
|
}
|