// 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, } /// 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, pub upstream: Option, pub field: Option, pub count: Option, pub of: Option, /// Technical detail (core's text, edge's error); never a secret. pub detail: Option, #[serde(default)] pub params: BTreeMap, } /// 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, pub params: BTreeMap, pub detail: Option, } #[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, /// `queued`, `validating`, `applying`, `watching`, `kept`, `reverted`, `failed` or /// `needs_attention`. pub state: String, pub steps: Vec, pub created: u64, pub started: Option, pub finished: Option, pub watch: Option, pub reason: Option, #[serde(default)] pub errors: Vec, /// Branch head when the apply started. pub base: Option, /// The commit of this change (none if it never got one). pub commit: Option, /// The commit that undid it, for reverted applies. pub revert_commit: Option, pub undo_of: Option, pub restore_of: Option, /// Token name of whoever asked. pub actor: String, /// What was tried, for "Edit and try again". pub attempted_host: Option, /// For undo: what it does ("Removes x"). pub effect: Option, } 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, 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, pub reason: Option, } /// Push state, in `push.json`. #[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)] #[serde(deny_unknown_fields)] pub struct PushState { pub pushed: Option, pub last_push_at: Option, pub last_attempt_at: Option, pub next_try_at: Option, pub error_code: Option, pub error_detail: Option, #[serde(default)] pub failures: u32, }