2026-02-02 07:19:22 +01:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
|
|
use tracing::{debug, info, warn};
|
2025-12-10 10:11:45 +01:00
|
|
|
|
|
|
|
|
use crate::{
|
2026-02-02 07:19:22 +01:00
|
|
|
env::ComposeDeployEnv,
|
2025-12-10 10:11:45 +01:00
|
|
|
errors::ComposeRunnerError,
|
2025-12-10 15:26:34 +01:00
|
|
|
infrastructure::{
|
|
|
|
|
environment::StackEnvironment,
|
|
|
|
|
ports::{HostPortMapping, discover_host_ports},
|
|
|
|
|
},
|
2025-12-10 10:11:45 +01:00
|
|
|
};
|
|
|
|
|
|
2026-02-02 07:19:22 +01:00
|
|
|
pub struct PortManager<E: ComposeDeployEnv> {
|
|
|
|
|
_env: PhantomData<E>,
|
|
|
|
|
}
|
2025-12-10 10:11:45 +01:00
|
|
|
|
2026-02-02 07:19:22 +01:00
|
|
|
impl<E: ComposeDeployEnv> PortManager<E> {
|
2025-12-10 10:11:45 +01:00
|
|
|
pub async fn prepare(
|
|
|
|
|
environment: &mut StackEnvironment,
|
2026-02-02 07:19:22 +01:00
|
|
|
descriptors: &E::Deployment,
|
2025-12-10 10:11:45 +01:00
|
|
|
) -> Result<HostPortMapping, ComposeRunnerError> {
|
2026-04-10 11:06:03 +02:00
|
|
|
let nodes = E::node_container_ports(descriptors).map_err(|source| {
|
|
|
|
|
ComposeRunnerError::Config(crate::errors::ConfigError::Descriptor { source })
|
|
|
|
|
})?;
|
2025-12-11 09:00:14 +01:00
|
|
|
debug!(
|
2026-02-02 07:19:22 +01:00
|
|
|
nodes = nodes.len(),
|
2025-12-11 09:00:14 +01:00
|
|
|
"resolving host ports for compose services"
|
|
|
|
|
);
|
2026-02-02 07:19:22 +01:00
|
|
|
let mapping = match discover_host_ports(environment, &nodes).await {
|
|
|
|
|
Ok(mapping) => mapping,
|
|
|
|
|
Err(error) => return Err(fail_host_port_resolution(environment, error).await),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
info!(
|
|
|
|
|
node_ports = ?mapping.node_api_ports(),
|
|
|
|
|
"resolved container host ports"
|
|
|
|
|
);
|
2025-12-15 22:29:36 +01:00
|
|
|
|
2026-02-02 07:19:22 +01:00
|
|
|
Ok(mapping)
|
2025-12-10 10:11:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-02 07:19:22 +01:00
|
|
|
|
|
|
|
|
async fn fail_host_port_resolution(
|
|
|
|
|
environment: &mut StackEnvironment,
|
|
|
|
|
error: ComposeRunnerError,
|
|
|
|
|
) -> ComposeRunnerError {
|
|
|
|
|
environment
|
|
|
|
|
.fail("failed to determine container host ports")
|
|
|
|
|
.await;
|
|
|
|
|
warn!(%error, "failed to resolve host ports");
|
|
|
|
|
error
|
|
|
|
|
}
|