Added health etc

This commit is contained in:
Maurice
2025-07-23 18:21:13 +02:00
parent da47934db9
commit 401ab5f016
3 changed files with 91 additions and 6 deletions

View File

@@ -5,6 +5,16 @@ name = "nc-test-api"
image = "numberchords-api"
network = "nc-network"
depend = ["nc-test-database"]
restart = "unless-stopped"
detach = true
hostname = "nc-test-api-hn"
[service.healthcheck]
cmd = "pg_isready --dbname=$DB_DATABASE_NAME --username=$DB_USERNAME || exit 1"
interval = "5s"
start_period = "30s"
retries = 3
on_failure = "none"
[environment]
ASPNETCORE_ENVIRONMENT = "Test"
@@ -29,3 +39,9 @@ container = 2222
[[volumes]]
volume = "test"
path = "/data/test"
[[mounts]]
typ = "bind"
source = "/etc/hosts"
target = "/etc/hosts"
read_only = true

View File

@@ -31,11 +31,27 @@ pub fn generate_openrc(config: &ServiceConfig) {
// start()
script.push_str("start() {\n");
let mut arguments = vec![String::from("--restart unless-stopped"), format!("--name {}", config.service.name)];
let mut arguments = vec![
format!("--restart {}", config.service.restart.as_deref().unwrap_or("unless-stopped")),
format!("--name {}", config.service.name)
];
if let Some(hostname) = &config.service.hostname {
arguments.push(format!("--hostname {}", hostname));
}
if config.service.detach.unwrap_or(true) {
arguments.push("--detach".to_string());
}
if let Some(network) = &config.service.network {
arguments.push(format!("--network {}", network));
}
for capability in &config.capabilities {
arguments.push(format!("--cap-add {}", capability));
}
for secret in &config.secrets {
if let Some(target) = &secret.target {
arguments.push(format!("--secret {},target={}", &secret.key, target));
@@ -45,16 +61,40 @@ pub fn generate_openrc(config: &ServiceConfig) {
}
for (key, value) in &config.environment {
arguments.push(format!("--env {}={}", key, value));
arguments.push(format!("--env {}='{}'", key, value));
}
for volume in &config.volumes {
arguments.push(format!("--volume {}:{}", &volume.volume, &volume.path));
}
for mount in &config.mounts {
let mut mount_str = format!("--mount type={},source={},target={}", mount.typ, mount.source, mount.target);
if mount.read_only.unwrap_or(false) {
mount_str.push_str(",readonly");
}
arguments.push(mount_str);
}
if let Some(healthcheck) = &config.service.healthcheck {
arguments.push(format!("--health-cmd '{}'", healthcheck.cmd));
if let Some(interval) = &healthcheck.interval {
arguments.push(format!("--health-interval {}", interval));
}
if let Some(start_period) = &healthcheck.start_period {
arguments.push(format!("--health-start-period {}", start_period));
}
if let Some(retries) = healthcheck.retries {
arguments.push(format!("--health-retries {}", retries));
}
if let Some(on_failure) = &healthcheck.on_failure {
arguments.push(format!("--health-on-failure {}", on_failure));
}
}
arguments.push(config.service.image.clone());
script.push_str(&wrap(&format!("podman run -d {}", arguments.iter()
script.push_str(&wrap(&format!("podman run {}", arguments.iter()
.enumerate()
.map(|(i, arg)| if i > 0 { format!("\t{}", arg) } else { arg.to_string() })
.collect::<Vec<_>>()

View File

@@ -18,16 +18,27 @@ pub struct ServiceConfig {
#[serde(default)]
pub volumes: Vec<VolumeMapping>,
pub user: Option<String>
#[serde(default)]
pub mounts: Vec<MountMapping>,
pub user: Option<String>,
#[serde(default)]
pub capabilities: Vec<String>,
}
#[derive(Debug, Deserialize)]
pub struct Service {
pub name: String,
pub hostname: Option<String>,
pub image: String,
pub network: Option<String>,
pub restart: Option<String>,
pub detach: Option<bool>,
pub healthcheck: Option<HealthCheck>,
#[serde(default)]
pub depend: Vec<String>
pub depend: Vec<String>,
}
#[derive(Debug, Deserialize)]
@@ -48,3 +59,21 @@ pub struct VolumeMapping {
pub volume: String,
pub path: String
}
#[derive(Debug, Deserialize)]
pub struct MountMapping {
pub typ: String, // e.g., "bind", "volume"
pub source: String,
pub target: String,
pub read_only: Option<bool>
}
// --health-cmd /healthcheck --health-on-failure=none --health-retries=1
#[derive(Debug, Deserialize)]
pub struct HealthCheck {
pub cmd: String,
pub interval: Option<String>,
pub start_period: Option<String>,
pub retries: Option<u32>,
pub on_failure: Option<String>,
}