Files
zosbuilder/zinit-client/README.md
Jan De Landtsheer 2fda71af11 Squashed 'components/zinit/' content from commit 1b76c06
git-subtree-dir: components/zinit
git-subtree-split: 1b76c062fe31d552d1b7b23484ce163995a81482
2025-08-16 21:12:16 +02:00

2.5 KiB

Zinit Client Library

A simple Rust client library for interacting with the Zinit process manager.

Features

  • Connect to Zinit via Unix socket or HTTP
  • Manage services (start, stop, restart, monitor)
  • Query service status and information
  • Create and delete service configurations
  • System operations (shutdown, reboot)

Installation

Add this to your Cargo.toml:

[dependencies]
zinit-client = "0.1.0"

Usage

Creating a Client

You can create a client using either Unix socket or HTTP transport:

use zinit_client::Client;

// Using Unix socket (local only)
let client = Client::unix_socket("/var/run/zinit.sock");

// Using HTTP (works for remote Zinit instances)
let client = Client::http("http://localhost:8080");

Service Management

// List all services
let services = client.list().await?;
for (name, state) in services {
    println!("{}: {}", name, state);
}

// Get status of a specific service
let status = client.status("my-service").await?;
println!("PID: {}, State: {}", status.pid, status.state);

// Start a service
client.start("my-service").await?;

// Stop a service
client.stop("my-service").await?;

// Restart a service
client.restart("my-service").await?;

// Monitor a service
client.monitor("my-service").await?;

// Forget a service
client.forget("my-service").await?;

// Send a signal to a service
client.kill("my-service", "SIGTERM").await?;

Service Configuration

use serde_json::json;

// Create a new service
let config = json!({
    "exec": "nginx",
    "oneshot": false,
    "after": ["network"]
}).as_object().unwrap().clone();

client.create_service("nginx", config).await?;

// Get service configuration
let config = client.get_service("nginx").await?;
println!("Config: {:?}", config);

// Delete a service
client.delete_service("nginx").await?;

System Operations

// Shutdown the system
client.shutdown().await?;

// Reboot the system
client.reboot().await?;

Error Handling

The library provides a ClientError enum for handling errors:

match client.status("non-existent-service").await {
    Ok(status) => println!("Service status: {}", status.state),
    Err(e) => match e {
        ClientError::ServiceNotFound(_) => println!("Service not found"),
        ClientError::ConnectionError(_) => println!("Failed to connect to Zinit"),
        _ => println!("Other error: {}", e),
    },
}

Examples

See the examples directory for complete usage examples.

License

This project is licensed under the MIT License.