Package ready

This commit is contained in:
Maurice
2025-07-25 19:31:55 +02:00
parent 3d600026c5
commit 5bcd4bdca2
5 changed files with 299 additions and 54 deletions

200
Cargo.lock generated
View File

@@ -2,6 +2,102 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "anstream"
version = "0.6.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301af1932e46185686725e0fad2f8f2aa7da69dd70bf6ecc44d6b703844a3933"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd"
[[package]]
name = "anstyle-parse"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8bdeb6047d8983be085bab0ba1472e6dc604e7041dbf6fcd5e71523014fae9"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "403f75924867bb1033c59fbf0797484329750cfbe3c4325cd33127941fabc882"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
]
[[package]]
name = "clap"
version = "4.5.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be92d32e80243a54711e5d7ce823c35c41c9d929dc4ab58e1276f625841aadf9"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "707eab41e9622f9139419d573eca0900137718000c517d47da73045f54331c3d"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef4f52386a59ca4c860f7393bcf8abd8dfd91ecccc0f774635ff68e92eeef491"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675"
[[package]]
name = "colorchoice"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]] [[package]]
name = "equivalent" name = "equivalent"
version = "1.0.2" version = "1.0.2"
@@ -14,6 +110,12 @@ version = "0.15.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5" checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.10.0" version = "2.10.0"
@@ -24,10 +126,23 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "once_cell_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad"
[[package]] [[package]]
name = "podman-openrc" name = "podman-openrc"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap",
"serde", "serde",
"toml", "toml",
] ]
@@ -79,6 +194,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.104" version = "2.0.104"
@@ -135,6 +256,85 @@ version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]] [[package]]
name = "winnow" name = "winnow"
version = "0.7.12" version = "0.7.12"

View File

@@ -2,7 +2,16 @@
name = "podman-openrc" name = "podman-openrc"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
license = "MIT"
keywords = ["podman", "openrc", "service", "generator"]
categories = ["command-line-utilities"]
publish = true
description = "A CLI tool to generate OpenRC scripts from Podman service definitions in (unofficial) TOML format."
homepage = "https://git.plabble.org/Maurice/podman-openrc"
repository = "https://git.plabble.org/Maurice/podman-openrc"
readme = "README.md"
[dependencies] [dependencies]
clap = { version = "4.5.41", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
toml = "0.9.2" toml = "0.9.2"

68
README.md Normal file
View File

@@ -0,0 +1,68 @@
# Podman OpenRC generator
This is a simple application written in Rust to convert a simple Podman service definition in TOML format to an OpenRC service script.
# Installation
```sh
cargo install podman-openrc
```
# Usage
```sh
podman-openrc <INPUT TOML FILE> <OUTPUT OPENRC FILE>
# For example: podman-openrc input.toml output.service.sh
```
# TOML service description format
The TOML format describing a Podman service is non-standard. It is NOT a Podlet.
The format is like this:
```toml
user = "<USERNAME>" # Optional property, set if you don't want to run the Podman command with the root user
# Required section
[service]
name = "<CONTAINER NAME>" # Container name, required
image = "<IMAGE>" # Podman image name
network = "<NETWORK>" # Optional, if you want to run the container within a specific network. Set to "host" if you don't want to use the podman networking.
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.
hostname = "<HOSTNAME>" # Host name, optional.
command = "<COMMAND>" # Container command to run, optional.
# Optionally set one or more environment variables
[environment]
ASPNETCORE_ENVIRONMENT = "Test"
# If you have a not TOML-compatible key name, use "" around the key name
# Optionally, you can assign one or more port mappings
[[ports]]
host = 80 # Port on your computer
container = 8080 # Port inside the container
protocol = "tcp" # Protocol, optional
# Optionally you can also assign volumes
[[volumes]]
volume = "<VOLUME NAME>" # Volume name or path on host
path = "<PATH>" # Volume location/target inside container
# Or you can make more advanced volumes with mounts
[[mounts]]
typ = "bind" # Mount type
source = "/etc/hosts" # Source file
target = "/etc/hosts" # Target file
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
# Optionally, you can configure a healthcheck
[service.healthcheck]
cmd = "<HEALTHCHECK COMMAND>" # The command or route to run/check
interval = "5m" # Interval, optional
start_period = "30s" # Start period (start after), optional
retries = 3 # Max retries, optional
on_failure = "none" # On failure options, optional
```

View File

@@ -1,48 +0,0 @@
user = "maurict"
[service]
name = "nc-test-api"
image = "numberchords-api"
network = "nc-network"
depend = ["nc-test-database"]
restart = "unless-stopped"
detach = true
hostname = "nc-test-api-hn"
command = "dotnet NumberChords.Api.dll"
[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"
"NC_Security__OtherIssuerKeys__identity.numberchords.com__AuthPublicKey" = ""
[[secrets]]
key = "connection_string"
target = "Database__ConnectionString"
[[secrets]]
key = "api_secret_key"
[[ports]]
host = 80
container = 8080
protocol = "tcp"
[[ports]]
host = 22
container = 2222
[[volumes]]
volume = "test"
path = "/data/test"
[[mounts]]
typ = "bind"
source = "/etc/hosts"
target = "/etc/hosts"
read_only = true

View File

@@ -1,10 +1,12 @@
use std::fs; use std::fs;
use clap::Parser;
use crate::service::ServiceConfig; use crate::service::ServiceConfig;
mod service; mod service;
pub fn generate_openrc(config: &ServiceConfig) { pub fn generate_openrc(config: &ServiceConfig) -> String {
let mut script = String::from("#!/sbin/openrc-run\n# !!! AUTO GENERATED - DO NOT EDIT !!!\n\n"); let mut script = String::from("#!/sbin/openrc-run\n# !!! AUTO GENERATED - DO NOT EDIT !!!\n\n");
let wrap = |cmd: &str| { let wrap = |cmd: &str| {
if let Some(user) = config.user.as_ref() { if let Some(user) = config.user.as_ref() {
@@ -113,13 +115,27 @@ pub fn generate_openrc(config: &ServiceConfig) {
script.push_str("\n}\n\n"); script.push_str("\n}\n\n");
// } // }
println!("\n\n{}", script); script
}
/// Program to generate OpenRC scripts from Podman service definitions in TOML format.
#[derive(Debug, Parser)]
struct Args {
/// Definition file in TOML format
definition: String,
/// Output file for the OpenRC script
out: String,
} }
fn main() { fn main() {
let file = fs::read_to_string("example.toml").unwrap(); let args = Args::parse();
let service: ServiceConfig = toml::from_str(&file).unwrap(); let input = fs::read_to_string(&args.definition)
println!("{:?}", service); .expect("Failed to read definition file");
let config: ServiceConfig = toml::from_str(&input)
.expect("Failed to parse definition file");
generate_openrc(&service); let output = generate_openrc(&config);
fs::write(&args.out, output)
.expect("Failed to write OpenRC script to output file");
} }