use std::{
fmt::{self, Display, Formatter},
future::Future,
};
use indicatif::ProgressBar;
use super::operations;
use crate::{
error::Error,
inventory::Host,
process::ProcessContext,
status::{self, DeploymentPhase},
store_path::StorePath,
verification::{self, ConfiguredOutcome},
};
#[derive(Clone, Copy)]
enum HostStatus {
UpToDate,
OutOfDate,
Failed,
}
impl Display for HostStatus {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str(match self {
Self::UpToDate => "up to date",
Self::OutOfDate => "out of date",
Self::Failed => "failed",
})
}
}
#[derive(Clone, Copy)]
pub(super) enum Validity {
Valid,
Missing,
Failed,
Unchecked,
}
impl Display for Validity {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str(match self {
Self::Valid => "yes",
Self::Missing | Self::Unchecked => "-",
Self::Failed => "failed",
})
}
}
#[derive(tabled::Tabled)]
pub(super) struct HostRow {
#[tabled(rename = "ID")]
id: String,
#[tabled(rename = "FLAKE")]
flake: String,
#[tabled(rename = "STATUS")]
status: HostStatus,
#[tabled(rename = "VALID")]
valid: Validity,
#[tabled(rename = "CHANGE")]
change: String,
}
impl HostRow {
pub(super) fn failed(host: &Host, valid: Validity) -> Self {
Self {
id: host.id().to_owned(),
flake: host.display_flake(),
status: HostStatus::Failed,
valid,
change: "-".to_owned(),
}
}
fn collected(host: &Host, deployed: &StorePath, expected: &StorePath, valid: Validity) -> Self {
let up_to_date = deployed == expected;
Self {
id: host.id().to_owned(),
flake: host.display_flake(),
status: if up_to_date {
HostStatus::UpToDate
} else {
HostStatus::OutOfDate
},
valid,
change: if up_to_date {
"-".to_owned()
} else {
format!("{} -> {}", deployed.short_hash(), expected.short_hash())
},
}
}
pub(super) fn needs_activation(&self) -> bool {
matches!(self.status, HostStatus::OutOfDate)
}
}
pub(super) enum ReviewOutcome {
UpToDate(HostRow),
OutOfDate(HostRow, ReviewedHost),
}
pub(super) struct ReviewedHost {
pub(super) host: Host,
pub(super) deployed: StorePath,
pub(super) expected: StorePath,
}
impl ReviewedHost {
pub(super) fn id(&self) -> &str {
self.host.id()
}
}
async fn collect_step<T, E>(
progress: &ProgressBar,
source: &str,
step: impl Future<Output = Result<T, E>>,
) -> Result<T, E> {
let result = step.await;
let status = if result.is_ok() {
"collected"
} else {
"failed"
};
progress.set_message(format!("{source} info {status}"));
result
}
pub(super) async fn deployed(
host: &Host,
processes: &ProcessContext<'_>,
progress: &ProgressBar,
) -> Result<StorePath, Error> {
collect_step(
progress,
"remote",
operations::deployed_store_path(host, processes, progress),
)
.await
}
pub(super) async fn expected(
host: &Host,
processes: &ProcessContext<'_>,
progress: &ProgressBar,
) -> Result<StorePath, Error> {
collect_step(
progress,
"local",
operations::expected_store_path(host, processes, progress),
)
.await
}
pub(super) async fn validity(
host: &Host,
processes: &ProcessContext<'_>,
progress: &ProgressBar,
) -> Result<ConfiguredOutcome, Error> {
let result = verification::run_configured(host, processes, progress).await;
status::set_phase(progress, host.id(), DeploymentPhase::CheckingStatus);
let status = if result.is_ok() {
"collected"
} else {
"failed"
};
progress.set_message(format!("validation info {status}"));
result
}
pub(super) fn skip_expected(progress: &ProgressBar) {
progress.set_message("local info skipped");
}
pub(super) fn skip_validity(progress: &ProgressBar) {
progress.set_message("validation skipped");
}
pub(super) fn review(
host: Host,
deployed: StorePath,
expected: &StorePath,
valid: Validity,
) -> ReviewOutcome {
let row = HostRow::collected(&host, &deployed, expected, valid);
if &deployed == expected {
return ReviewOutcome::UpToDate(row);
}
ReviewOutcome::OutOfDate(
row,
ReviewedHost {
host,
deployed,
expected: expected.clone(),
},
)
}