Compare commits

...

4 Commits

Author SHA1 Message Date
Maurice
23fc91f54e Update 2025-10-22 16:59:26 +02:00
Maurice
1953f8cf1c Change extension of service file 2025-09-30 13:04:06 +02:00
Maurice
4c7d1a857e Update version 2025-08-16 20:30:36 +02:00
Maurice
6e236f64df Update 2025-08-16 20:30:01 +02:00
5 changed files with 75 additions and 37 deletions

66
Cargo.lock generated
View File

@@ -54,9 +54,9 @@ dependencies = [
[[package]]
name = "clap"
version = "4.5.45"
version = "4.5.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fc0e74a703892159f5ae7d3aac52c8e6c392f5ae5f359c70b5881d60aaac318"
checksum = "e2134bb3ea021b78629caa971416385309e0131b351b25e01dc16fb54e1b5fae"
dependencies = [
"clap_builder",
"clap_derive",
@@ -64,9 +64,9 @@ dependencies = [
[[package]]
name = "clap_builder"
version = "4.5.44"
version = "4.5.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3e7f4214277f3c7aa526a59dd3fbe306a370daee1f8b7b8c987069cd8e888a8"
checksum = "c2ba64afa3c0a6df7fa517765e31314e983f51dda798ffba27b988194fb65dc9"
dependencies = [
"anstream",
"anstyle",
@@ -76,9 +76,9 @@ dependencies = [
[[package]]
name = "clap_derive"
version = "4.5.45"
version = "4.5.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14cb31bb0a7d536caef2639baa7fad459e15c3144efefa6dbd1c84562c4739f6"
checksum = "bbfd7eae0b0f1a6e63d4b13c9c478de77c2eb546fba158ad50b4203dc24b9f9c"
dependencies = [
"heck",
"proc-macro2",
@@ -124,9 +124,9 @@ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "indexmap"
version = "2.10.0"
version = "2.11.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe4cd85333e22411419a0bcae1297d25e58c9443848b11dc6a86fefe8c78a661"
checksum = "4b0f83760fb341a774ed326568e19f5a863af4a952def8c39f9ab92fd95b88e5"
dependencies = [
"equivalent",
"hashbrown",
@@ -155,7 +155,7 @@ checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad"
[[package]]
name = "podman-openrc"
version = "0.1.2"
version = "0.1.4"
dependencies = [
"clap",
"itertools",
@@ -183,18 +183,28 @@ dependencies = [
[[package]]
name = "serde"
version = "1.0.219"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [
"serde_core",
"serde_derive",
]
[[package]]
name = "serde_core"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.219"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
@@ -203,11 +213,11 @@ dependencies = [
[[package]]
name = "serde_spanned"
version = "1.0.0"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40734c41988f7306bb04f0ecf60ec0f3f1caa34290e4e8ea471dcd3346483b83"
checksum = "5417783452c2be558477e104686f7de5dae53dba813c28435e0e70f82d9b04ee"
dependencies = [
"serde",
"serde_core",
]
[[package]]
@@ -229,12 +239,12 @@ dependencies = [
[[package]]
name = "toml"
version = "0.9.5"
version = "0.9.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75129e1dc5000bfbaa9fee9d1b21f974f9fbad9daec557a521ee6e080825f6e8"
checksum = "00e5e5d9bf2475ac9d4f0d9edab68cc573dc2fd644b0dba36b0c30a92dd9eaa0"
dependencies = [
"indexmap",
"serde",
"serde_core",
"serde_spanned",
"toml_datetime",
"toml_parser",
@@ -244,27 +254,27 @@ dependencies = [
[[package]]
name = "toml_datetime"
version = "0.7.0"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bade1c3e902f58d73d3f294cd7f20391c1cb2fbcb643b73566bc773971df91e3"
checksum = "32f1085dec27c2b6632b04c80b3bb1b4300d6495d1e129693bdda7d91e72eec1"
dependencies = [
"serde",
"serde_core",
]
[[package]]
name = "toml_parser"
version = "1.0.2"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b551886f449aa90d4fe2bdaa9f4a2577ad2dde302c61ecf262d80b116db95c10"
checksum = "4cf893c33be71572e0e9aa6dd15e6677937abd686b066eac3f8cd3531688a627"
dependencies = [
"winnow",
]
[[package]]
name = "toml_writer"
version = "1.0.2"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcc842091f2def52017664b53082ecbbeb5c7731092bad69d2c63050401dfd64"
checksum = "d163a63c116ce562a22cda521fcc4d79152e7aba014456fb5eb442f6d6a10109"
[[package]]
name = "unicode-ident"
@@ -353,6 +363,6 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winnow"
version = "0.7.12"
version = "0.7.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3edebf492c8125044983378ecb5766203ad3b4c2f7a922bd7dd207f6d443e95"
checksum = "21a0236b59786fed61e2a80582dd500fe61f18b5dca67a4a067d0bc9039339cf"

View File

@@ -1,6 +1,6 @@
[package]
name = "podman-openrc"
version = "0.1.2"
version = "0.1.4"
edition = "2021"
license = "MIT"
keywords = ["podman", "openrc", "service", "generator"]
@@ -12,7 +12,7 @@ repository = "https://git.plabble.org/Maurice/podman-openrc"
readme = "README.md"
[dependencies]
clap = { version = "4.5.45", features = ["derive"] }
clap = { version = "4.5.48", features = ["derive"] }
itertools = "0.14.0"
serde = { version = "1.0", features = ["derive"] }
toml = "0.9.5"
toml = "0.9.7"

View File

@@ -9,7 +9,10 @@ cargo install podman-openrc
# Usage
```sh
podman-openrc <INPUT TOML FILE> <OUTPUT OPENRC FILE>
# For example: podman-openrc input.toml output.service.sh
# For example: podman-openrc input.toml output.service
# or:
podman-openrc ./input-folder /etc/init.d
```
# TOML service description format
@@ -27,6 +30,7 @@ image = "<IMAGE>" # Podman image name
depend = ["<SERVICE NAME>"] # Name of any service in /etc/init.d to depend on
restart = "unless-stopped" # Restart, optional. Defaults to "unless-stopped"
detach = true # Run container in detach mode, optional, default true. Recommended.
interactive = false # Run container in interactive mode, optional, default false
hostname = "<HOSTNAME>" # Host name, optional.
command = "<COMMAND>" # Container command to run, optional.
@@ -58,6 +62,7 @@ protocol = "tcp" # Protocol, optional
[[volumes]]
source = "<VOLUME NAME>" # Volume name or path on host
target = "<PATH>" # 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]]
@@ -69,7 +74,7 @@ read_only = true # Whether to use ro mode, optional
# Optionally you can use Podman secrets in an array
[[secrets]]
key = "<SECRET KEY>" # Secret key used in `podman secret`
target = "<TARGET SECRET FILE>" # Target secret filename in /var/run/secrets. Optional, defaults to the key
target = "<TARGET SECRET FILE>" # Target secret filename in {/var}/run/secrets. Optional, defaults to the key
# Between environment and secrets: get secret from Podman and set it as environment variable
[[environment_secrets]]

View File

@@ -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();
for network in networks.iter() {
start_pre_commands.push(format!("podman rm {} --ignore;", config.service.name));
for network in networks.iter().filter(|n| *n != "host" && *n != "bridge") {
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::<Vec<String>>().join("\n"));
script.push_str("\n}\n\n");
// }
@@ -70,6 +73,10 @@ pub fn generate_openrc(config: &ServiceConfig) -> String {
arguments.push("--detach".to_string());
}
if config.service.interactive.unwrap_or(false) {
arguments.push("--interactive".to_string());
}
for network in networks.iter() {
arguments.push(format!("--network {}", network));
}
@@ -167,6 +174,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().filter(|n| *n != "host" && *n != "bridge") {
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::<Vec<String>>().join("\n"));
script.push_str("\n}\n");
// }
@@ -288,7 +309,7 @@ fn main() {
// Write
for config in services {
let output = generate_openrc(&config);
fs::write(output_path.join(format!("{}.service.sh", config.service.name)), output)
fs::write(output_path.join(format!("{}.service", config.service.name)), output)
.expect("Failed to write OpenRC script to output file");
}
} else {

View File

@@ -41,6 +41,7 @@ pub struct Service {
pub restart: Option<String>,
pub detach: Option<bool>,
pub interactive: Option<bool>,
pub healthcheck: Option<HealthCheck>,
pub command: Option<String>,
@@ -70,7 +71,8 @@ pub struct PortMapping {
#[derive(Debug, Deserialize)]
pub struct VolumeMapping {
pub source: String,
pub target: String
pub target: String,
pub create: Option<bool>
}
#[derive(Debug, Deserialize)]