diff --git a/README.md b/README.md index b23a8cd..79ea18d 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ protocol = "tcp" # Protocol, optional [[volumes]] source = "" # Volume name or path on host target = "" # Volume location/target inside container +create = true # Optional, set to true if volume is NOT a path but named volume and you want to create it # Or you can make more advanced volumes with mounts [[mounts]] diff --git a/src/main.rs b/src/main.rs index d539d5b..4d3ef04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,10 +39,13 @@ pub fn generate_openrc(config: &ServiceConfig) -> String { // start_pre() script.push_str("start_pre() {\n"); let mut start_pre_commands = Vec::new(); + start_pre_commands.push(format!("podman rm {} --ignore;", config.service.name)); for network in networks.iter() { start_pre_commands.push(format!("podman network create {} --ignore;", network)); } - start_pre_commands.push(format!("podman rm {} --ignore;", config.service.name)); + for volume in config.volumes.iter().filter(|v| v.create.is_some_and(|c|c)) { + start_pre_commands.push(format!("podman volume create {} --ignore;", volume.source)); + } script.push_str(&start_pre_commands.iter().map(|c|wrap(c)).collect::>().join("\n")); script.push_str("\n}\n\n"); // } @@ -167,6 +170,20 @@ pub fn generate_openrc(config: &ServiceConfig) -> String { "podman stop {} --ignore", config.service.name ))); + script.push_str("\n}\n\n"); + // } + + // cleanup() + script.push_str("cleanup() {\n"); + let mut cleanup_commands = Vec::new(); + cleanup_commands.push(format!("podman rm {} --ignore --force;", config.service.name)); + for network in networks.iter() { + cleanup_commands.push(format!("podman network rm {} --force;", network)); + } + for volume in config.volumes.iter().filter(|v| v.create.is_some_and(|c|c)) { + cleanup_commands.push(format!("podman volume rm {} --force;", volume.source)); + } + script.push_str(&cleanup_commands.iter().map(|c|wrap(c)).collect::>().join("\n")); script.push_str("\n}\n"); // } diff --git a/src/service.rs b/src/service.rs index b8e8d27..b9f5de9 100644 --- a/src/service.rs +++ b/src/service.rs @@ -70,7 +70,8 @@ pub struct PortMapping { #[derive(Debug, Deserialize)] pub struct VolumeMapping { pub source: String, - pub target: String + pub target: String, + pub create: Option } #[derive(Debug, Deserialize)]