use crate::observation::{CheckSkipReason, LogPhase, SmokeResult, WatchResult};
use crate::preflight::AbortReason;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum StepStatus {
Ok,
Failed,
Unknown,
NotStarted,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct StepTimes {
pub started_at_ms: Option<u128>,
pub finished_at_ms: Option<u128>,
pub result_seen_at_ms: Option<u128>,
}
impl StepTimes {
pub fn duration_ms(self) -> Option<u128> {
Some(self.finished_at_ms?.saturating_sub(self.started_at_ms?))
}
}
pub(crate) fn down_may_have_started(status: &StepStatus) -> bool {
!matches!(status, StepStatus::NotStarted)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MaintenancePhase {
Down,
Up,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConnectionTarget {
Step { index: usize },
Maintenance { phase: MaintenancePhase },
LogWatch,
}
impl fmt::Display for ConnectionTarget {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Step { index } => write!(formatter, "step {index}"),
Self::Maintenance {
phase: MaintenancePhase::Down,
} => formatter.write_str("maintenance on"),
Self::Maintenance {
phase: MaintenancePhase::Up,
} => formatter.write_str("maintenance off"),
Self::LogWatch => formatter.write_str("the log watch"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum StopReason {
Requested,
LockLost,
ConnectFailed(String),
JournalFailed(String),
}
impl fmt::Display for StopReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Requested => write!(f, "a stop was requested"),
Self::LockLost => write!(f, "the deploy lock was taken over"),
Self::ConnectFailed(reason) => write!(f, "could not reach the server: {reason}"),
Self::JournalFailed(reason) => write!(f, "could not save the receipt: {reason}"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DeployOutcome {
Succeeded,
FailedAtStep {
step: usize,
partial_update: bool,
},
StoppedAfterStep {
step: usize,
reason: StopReason,
},
CancelledBeforeChanges,
AbortedBeforeChanges(AbortReason),
StoppedInMaintenance(AbortReason),
Unknown {
step: usize,
reason: String,
},
}
impl DeployOutcome {
pub fn summary(&self) -> String {
match self {
Self::Succeeded => "Deploy run succeeded.".into(),
Self::FailedAtStep {
step,
partial_update,
} => format!(
"Deploy failed at step {step}{}.",
if *partial_update {
"; the server may have been partially updated"
} else {
""
}
),
Self::StoppedAfterStep { step, reason } => {
format!("Deploy stopped after step {step}: {reason}.")
}
Self::CancelledBeforeChanges => "Deploy was cancelled before changes.".into(),
Self::AbortedBeforeChanges(reason) => {
format!("Deploy was aborted before changes: {reason}.")
}
Self::StoppedInMaintenance(reason) => format!(
"Deploy stopped after maintenance mode was turned on; no deploy steps ran: {reason}."
),
Self::Unknown { step, reason } => {
format!("Outcome of step {step} is unknown: {reason}")
}
}
}
pub fn label(&self) -> String {
match self {
Self::Succeeded => "Succeeded".into(),
Self::FailedAtStep {
step,
partial_update: true,
} => format!("Failed at step {step} (partial update)"),
Self::FailedAtStep { step, .. } => format!("Failed at step {step}"),
Self::StoppedAfterStep { step, .. } => format!("Stopped after step {step}"),
Self::CancelledBeforeChanges => "Cancelled before changes".into(),
Self::AbortedBeforeChanges(_) => "Aborted before changes".into(),
Self::StoppedInMaintenance(_) => "Stopped in maintenance mode, no steps ran".into(),
Self::Unknown { step, .. } => format!("Unknown at step {step}"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum DeployEvent {
ConnectionLost {
target: ConnectionTarget,
at_ms: u128,
detail: String,
},
Reattached {
target: ConnectionTarget,
at_ms: u128,
},
StepStarted {
index: usize,
name: String,
},
Output {
index: usize,
line: String,
},
StepFinished {
index: usize,
status: StepStatus,
exit_code: Option<i32>,
times: StepTimes,
},
Detached {
index: usize,
},
Interrupted {
index: usize,
reason: String,
},
RunError {
reason: String,
},
Warning {
reason: String,
},
MaintenanceStarted {
phase: MaintenancePhase,
},
MaintenanceOutput {
phase: MaintenancePhase,
line: String,
},
MaintenanceFinished {
phase: MaintenancePhase,
status: StepStatus,
exit_code: Option<i32>,
times: StepTimes,
},
AppLeftDown,
ServerState {
head: Option<String>,
tree_dirty: Option<bool>,
},
NewLogError {
phase: LogPhase,
message: String,
file_line: Option<String>,
},
WatchStarted {
window: Duration,
},
ChecksSkipped {
reason: CheckSkipReason,
},
WatchFinished(WatchResult),
SmokeFinished(SmokeResult),
OutcomeKnown(DeployOutcome),
Finished(DeployOutcome),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn outcome_wording_is_stable() {
let cases = [
(
DeployOutcome::Succeeded,
"Deploy run succeeded.",
"Succeeded",
),
(
DeployOutcome::FailedAtStep {
step: 2,
partial_update: false,
},
"Deploy failed at step 2.",
"Failed at step 2",
),
(
DeployOutcome::FailedAtStep {
step: 0,
partial_update: true,
},
"Deploy failed at step 0; the server may have been partially updated.",
"Failed at step 0 (partial update)",
),
(
DeployOutcome::StoppedAfterStep {
step: 1,
reason: StopReason::ConnectFailed("timed out".into()),
},
"Deploy stopped after step 1: could not reach the server: timed out.",
"Stopped after step 1",
),
(
DeployOutcome::CancelledBeforeChanges,
"Deploy was cancelled before changes.",
"Cancelled before changes",
),
(
DeployOutcome::AbortedBeforeChanges(AbortReason::LockLost),
"Deploy was aborted before changes: the deploy lock was taken over.",
"Aborted before changes",
),
(
DeployOutcome::StoppedInMaintenance(AbortReason::Cancelled),
"Deploy stopped after maintenance mode was turned on; no deploy steps ran: the run was cancelled.",
"Stopped in maintenance mode, no steps ran",
),
(
DeployOutcome::Unknown {
step: 3,
reason: "connection lost".into(),
},
"Outcome of step 3 is unknown: connection lost",
"Unknown at step 3",
),
];
for (outcome, summary, label) in cases {
assert_eq!(outcome.summary(), summary);
assert_eq!(outcome.label(), label);
}
}
}