use std::fmt;
use std::path::PathBuf;
use crate::project::platform::Protocol;
use serde::{Deserialize, Serialize};
use super::owner::Owner;
use crate::error::ErrorCode;
use crate::name::RunName;
use crate::state::{Desired, Op, RunState};
use crate::store::{LastObserved, RunRecord};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(transparent)]
pub struct OperationId(pub i64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(transparent)]
pub struct ResourceId(pub i64);
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(transparent)]
pub struct EventCursor(pub u64);
impl fmt::Display for OperationId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "#{}", self.0)
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct EventPage {
pub events: Vec<(EventCursor, crate::events::Event)>,
pub unreadable: usize,
pub last: EventCursor,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RunRow {
pub name: RunName,
pub project: PathBuf,
pub source_digest: String,
pub desired: Desired,
pub state: RunState,
pub failed_cause: Option<ErrorCode>,
pub state_op: Option<OperationId>,
pub state_at: String,
pub record: RunRecord,
pub observed: Option<LastObserved>,
pub observed_at: Option<String>,
}
impl RunRow {
#[must_use]
pub fn converged(&self) -> bool {
self.desired.met_by() == self.state
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Outcome {
Succeeded,
Failed,
Cancelled,
Abandoned,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[non_exhaustive]
pub struct Ended {
pub at: String,
pub outcome: Outcome,
pub code: Option<ErrorCode>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct OperationRow {
pub id: OperationId,
pub run: RunName,
pub op: Op,
pub queued_at: String,
pub started_at: Option<String>,
pub ended: Option<Ended>,
pub cancel_requested: bool,
pub owner: Owner,
pub resumed_from: Option<OperationId>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Scope {
Provider,
Local,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ResourceKind {
Vm,
Container,
Network,
Volume,
Hub,
Process,
Module,
HostKeys,
StateFiles,
Archive,
Consoles,
ToolProcess,
}
impl ResourceKind {
#[must_use]
pub fn scope(self) -> Scope {
match self {
ResourceKind::Vm
| ResourceKind::Container
| ResourceKind::Network
| ResourceKind::Volume
| ResourceKind::Hub
| ResourceKind::Process
| ResourceKind::Module => Scope::Provider,
ResourceKind::HostKeys | ResourceKind::StateFiles | ResourceKind::Archive | ResourceKind::Consoles | ResourceKind::ToolProcess => Scope::Local,
}
}
#[must_use]
pub fn lifetime(self) -> Option<Lifetime> {
match self.scope() {
Scope::Provider => None,
Scope::Local if self == ResourceKind::HostKeys => Some(Lifetime::Machines),
Scope::Local => Some(Lifetime::Run),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ResourceState {
Creating,
Created,
Destroying,
Gone,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Lifetime {
Machines,
Run,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(transparent)]
pub struct CopyName(String);
impl CopyName {
pub fn rendered(rendered: impl Into<String>, key: &str) -> Result<CopyName, NotPerCopy> {
let rendered = rendered.into();
if key.is_empty() || !rendered.contains(key) {
return Err(NotPerCopy { name: rendered });
}
Ok(CopyName(rendered))
}
pub(crate) fn stored(name: String) -> CopyName {
CopyName(name)
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("`{name}` doesn't hold the run's key: another copy could have it")]
pub struct NotPerCopy {
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ResourceRow {
pub id: ResourceId,
pub run: RunName,
pub key: String,
pub kind: ResourceKind,
pub scope: Scope,
pub copy_name: Option<CopyName>,
pub native_id: Option<String>,
pub state: ResourceState,
pub lifetime: Lifetime,
pub created_by: OperationId,
pub destroyed_by: Option<OperationId>,
pub detail: Option<serde_json::Value>,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct NewResource<'a> {
pub run: &'a RunName,
pub key: &'a str,
pub kind: ResourceKind,
pub copy_name: Option<CopyName>,
pub lifetime: Option<Lifetime>,
pub by: OperationId,
pub detail: Option<serde_json::Value>,
}
impl<'a> NewResource<'a> {
#[must_use]
pub fn new(run: &'a RunName, key: &'a str, kind: ResourceKind, by: OperationId) -> NewResource<'a> {
NewResource {
run,
key,
kind,
copy_name: None,
lifetime: None,
by,
detail: None,
}
}
#[must_use]
pub fn named(mut self, name: CopyName) -> NewResource<'a> {
self.copy_name = Some(name);
self
}
#[must_use]
pub fn living(mut self, lifetime: Lifetime) -> NewResource<'a> {
self.lifetime = Some(lifetime);
self
}
#[must_use]
pub fn with_detail(mut self, detail: serde_json::Value) -> NewResource<'a> {
self.detail = Some(detail);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[non_exhaustive]
pub struct Lease {
pub scope: String,
pub owner: Owner,
pub acquired_at: String,
pub expires_at: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum LeaseOutcome {
Acquired {
took_over: Option<Lease>,
},
Held(Lease),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PortReservation {
pub port: crate::port::HostPort,
pub protocol: Protocol,
pub run: RunName,
pub operation: Option<OperationId>,
}