Skip to content
This repository was archived by the owner on Dec 15, 2025. It is now read-only.

Commit e2e6c80

Browse files
committed
updated zinit-client to reflect new rpc crates
1 parent 7776184 commit e2e6c80

File tree

8 files changed

+555
-1001
lines changed

8 files changed

+555
-1001
lines changed

Cargo.lock

Lines changed: 37 additions & 419 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,4 @@ path = "src/lib.rs"
4545

4646
[[bin]]
4747
name = "zinit"
48-
path = "src/main.rs"
49-
# [[bin]]
50-
# name = "testapp"
51-
# path = "src/bin/testapp.rs"
52-
# [[bin]]
53-
# name = "zinit-http"
54-
# path = "src/bin/zinit-http.rs"
55-
48+
path = "src/main.rs"

docker/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ FROM ubuntu:18.04
22

33
RUN mkdir -p /etc/zinit
44
ADD zinit /sbin/zinit
5-
# ADD zinit-http /sbin/zinit-http
65

76
ENTRYPOINT ["/sbin/zinit", "init"]

zinit-client/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ authors = ["ThreeFold Tech, https://github.com/threefoldtech"]
88

99
[dependencies]
1010
anyhow = "1.0"
11+
async-trait = "0.1.88"
12+
jsonrpsee = { version = "0.24.9", features = ["server", "client", "macros"] }
13+
reth-ipc = { git = "https://github.com/paradigmxyz/reth", package = "reth-ipc" }
1114
tokio = { version = "1.14.0", features = ["full"] }
1215
serde = { version = "1.0", features = ["derive"] }
1316
serde_json = "1.0"
14-
reqwest = { version = "0.11", features = ["json"] }
1517
thiserror = "1.0"
1618
log = "0.4"
1719

@@ -21,4 +23,4 @@ path = "examples/basic_usage.rs"
2123

2224
[[example]]
2325
name = "http_client"
24-
path = "examples/http_client.rs"
26+
path = "examples/http_client.rs"
Lines changed: 37 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,50 @@
11
use anyhow::Result;
2-
use serde_json::json;
3-
use std::time::Duration;
4-
use tokio::time;
52
use zinit_client::Client;
63

74
#[tokio::main]
85
async fn main() -> Result<()> {
96
// Create a client using Unix socket transport
10-
// let unix_client = Client::unix_socket("/var/run/zinit.sock");
11-
12-
// Or create a client using HTTP transport
13-
let http_client = Client::http("http://localhost:8080");
14-
15-
// Choose which client to use for this example
16-
let client = http_client;
17-
18-
// Service name for our example
19-
let service_name = "test1234";
20-
21-
// Step 1: List existing services
22-
println!("Listing all services before creating our test service:");
7+
let client = Client::unix_socket("/var/run/zinit.sock").await?;
8+
9+
// List all services
2310
let services = client.list().await?;
24-
25-
// Print all services
26-
for (name, state) in &services {
27-
println!("- {}: {}", name, state);
11+
println!("Services:");
12+
for (name, state) in services {
13+
println!("{}: {}", name, state);
2814
}
29-
30-
// Step 2: Create a new service
31-
println!("\n--- Creating service '{}' ---", service_name);
32-
33-
// Create the service configuration
34-
let service_config = json!({
35-
"exec": "echo 'hello from test service'",
36-
"oneshot": false,
37-
"log": "stdout"
38-
})
39-
.as_object()
40-
.unwrap()
41-
.clone();
42-
43-
// Create the service
44-
let result = client.create_service(service_name, service_config).await?;
45-
println!("Create result: {}", result);
46-
47-
// Step 3: Monitor the service
48-
println!("\n--- Monitoring service '{}' ---", service_name);
49-
client.monitor(service_name).await?;
50-
println!("Service is now being monitored");
51-
52-
// Wait a moment for the service to start
53-
println!("Waiting for service to start...");
54-
time::sleep(Duration::from_secs(2)).await;
55-
56-
// Step 4: Get the status of the service
57-
println!("\n--- Getting status for '{}' ---", service_name);
58-
let status = client.status(service_name).await?;
59-
println!("Name: {}", status.name);
60-
println!("PID: {}", status.pid);
61-
println!("State: {}", status.state);
62-
println!("Target: {}", status.target);
63-
println!("Dependencies:");
64-
for (dep, state) in &status.after {
65-
println!(" - {}: {}", dep, state);
15+
16+
// Get a specific service status
17+
let service_name = "example-service";
18+
match client.status(service_name).await {
19+
Ok(status) => {
20+
println!("\nService: {}", status.name);
21+
println!("PID: {}", status.pid);
22+
println!("State: {}", status.state);
23+
println!("Target: {}", status.target);
24+
println!("After:");
25+
for (dep, state) in status.after {
26+
println!(" {}: {}", dep, state);
27+
}
28+
},
29+
Err(e) => eprintln!("Failed to get status: {}", e),
6630
}
67-
68-
// Step 5: Stop the service
69-
println!("\n--- Stopping service '{}' ---", service_name);
70-
client.stop(service_name).await?;
71-
println!("Service stopped");
72-
73-
// Wait a moment for the service to stop
74-
time::sleep(Duration::from_secs(1)).await;
75-
76-
// Check status after stopping
77-
println!("\n--- Status after stopping ---");
78-
let status = client.status(service_name).await?;
79-
println!("State: {}", status.state);
80-
println!("Target: {}", status.target);
81-
82-
// Step 6: Delete the service
83-
println!("\n--- Deleting service '{}' ---", service_name);
84-
let delete_result = client.delete_service(service_name).await?;
85-
println!("Delete result: {}", delete_result);
86-
87-
// Step 7: Verify the service is gone
88-
println!("\n--- Listing services after deletion ---");
89-
let services_after = client.list().await?;
90-
for (name, state) in &services_after {
91-
println!("- {}: {}", name, state);
31+
32+
// Try to start a service
33+
match client.start(service_name).await {
34+
Ok(_) => println!("\nService started successfully"),
35+
Err(e) => eprintln!("Failed to start service: {}", e),
9236
}
93-
94-
// Check if our service was deleted
95-
if !services_after.contains_key(service_name) {
96-
println!("\nService '{}' was successfully deleted", service_name);
97-
} else {
98-
println!("\nWarning: Service '{}' still exists", service_name);
37+
38+
// Get logs for the service
39+
match client.logs(Some(service_name.to_string())).await {
40+
Ok(logs) => {
41+
println!("\nLogs:");
42+
for log in logs {
43+
println!("{}", log);
44+
}
45+
},
46+
Err(e) => eprintln!("Failed to get logs: {}", e),
9947
}
100-
48+
10149
Ok(())
10250
}
Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,75 @@
11
use anyhow::Result;
2+
use serde_json::json;
23
use zinit_client::Client;
34

45
#[tokio::main]
56
async fn main() -> Result<()> {
67
// Create a client using HTTP transport
7-
let client = Client::http("http://localhost:8080");
8-
9-
println!("Connecting to Zinit via HTTP...");
10-
8+
let client = Client::http("http://localhost:8080").await?;
9+
10+
// Create a new service
11+
let service_name = "example-http-service";
12+
let service_config = json!({
13+
"exec": "echo 'Hello from HTTP service'",
14+
"oneshot": true,
15+
"after": ["network"]
16+
}).as_object().unwrap().clone();
17+
18+
match client.create_service(service_name, service_config).await {
19+
Ok(msg) => println!("Service created: {}", msg),
20+
Err(e) => eprintln!("Failed to create service: {}", e),
21+
}
22+
23+
// Start the HTTP/RPC server on a specific address
24+
match client.start_http_server("0.0.0.0:8081").await {
25+
Ok(msg) => println!("HTTP server status: {}", msg),
26+
Err(e) => eprintln!("Failed to start HTTP server: {}", e),
27+
}
28+
1129
// List all services
12-
println!("Listing all services:");
13-
match client.list().await {
14-
Ok(services) => {
15-
if services.is_empty() {
16-
println!("No services found.");
17-
} else {
18-
for (name, state) in &services {
19-
println!("- {}: {}", name, state);
20-
}
21-
22-
// Try to get the first service for a status example
23-
if let Some(service_name) = services.keys().next() {
24-
println!("\nGetting status for {}:", service_name);
25-
match client.status(service_name).await {
26-
Ok(status) => {
27-
println!("Name: {}", status.name);
28-
println!("PID: {}", status.pid);
29-
println!("State: {}", status.state);
30-
println!("Target: {}", status.target);
31-
println!("Dependencies: {}", status.after.len());
32-
}
33-
Err(e) => println!("Error getting status: {}", e),
34-
}
35-
}
36-
}
37-
}
38-
Err(e) => {
39-
println!("Error connecting to Zinit HTTP proxy: {}", e);
40-
println!("Make sure the Zinit HTTP proxy is running on http://localhost:8080");
41-
}
30+
let services = client.list().await?;
31+
println!("\nServices:");
32+
for (name, state) in services {
33+
println!("{}: {}", name, state);
4234
}
43-
35+
36+
// Monitor the service
37+
match client.monitor(service_name).await {
38+
Ok(_) => println!("\nService is now monitored"),
39+
Err(e) => eprintln!("Failed to monitor service: {}", e),
40+
}
41+
42+
// Start the service
43+
match client.start(service_name).await {
44+
Ok(_) => println!("Service started successfully"),
45+
Err(e) => eprintln!("Failed to start service: {}", e),
46+
}
47+
48+
// Get logs
49+
let logs = client.logs(Some(service_name.to_string())).await?;
50+
println!("\nLogs:");
51+
for log in logs {
52+
println!("{}", log);
53+
}
54+
55+
// Clean up - forget the service
56+
println!("\nCleaning up...");
57+
match client.forget(service_name).await {
58+
Ok(_) => println!("Service has been forgotten"),
59+
Err(e) => eprintln!("Failed to forget service: {}", e),
60+
}
61+
62+
// Clean up - delete the service configuration
63+
match client.delete_service(service_name).await {
64+
Ok(msg) => println!("{}", msg),
65+
Err(e) => eprintln!("Failed to delete service: {}", e),
66+
}
67+
68+
// Stop the HTTP/RPC server
69+
match client.stop_http_server().await {
70+
Ok(_) => println!("HTTP server stopped"),
71+
Err(e) => eprintln!("Failed to stop HTTP server: {}", e),
72+
}
73+
4474
Ok(())
4575
}

0 commit comments

Comments
 (0)