You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A universal Rust client library for interacting with the [Zinit](https://github.com/threefoldtech/zinit) service manager that works seamlessly with both old and new server versions.
7
+
A Rust client library for the [Zinit](https://github.com/threefoldtech/zinit) service manager.
9
8
10
-
## 🌟 Universal Compatibility
11
-
12
-
This client automatically detects and adapts to different Zinit server versions:
13
-
14
-
-**Old Servers (v0.2.14)**: Uses raw command protocol with graceful feature degradation
15
-
-**New Servers (v0.2.25+)**: Uses JSON-RPC protocol with full feature support
16
-
-**Automatic Detection**: No configuration needed - the client detects the server type automatically
17
-
-**Consistent API**: Same client code works with both server versions
9
+
**Universal Compatibility**: Automatically works with both old (v0.2.14) and new (v0.2.25+) Zinit servers through automatic protocol detection.
18
10
19
11
## Features
20
12
21
-
### Universal Interface
22
-
23
-
-**Automatic Protocol Detection**: Seamlessly switches between JSON-RPC and raw commands
24
-
-**Feature Awareness**: Knows what each server version supports
25
-
-**Graceful Degradation**: Helpful error messages for unsupported features
26
-
-**Backward Compatibility**: Full support for legacy zinit installations
You can customize the client behavior using `ClientConfig`:
55
+
### Service Management
181
56
182
57
```rust
183
-
usezinit_client::{ZinitClient, ClientConfig};
184
-
usestd::time::Duration;
185
-
186
-
letconfig=ClientConfig {
187
-
socket_path:"/var/run/zinit.sock".into(),
188
-
connection_timeout:Duration::from_secs(5),
189
-
operation_timeout:Duration::from_secs(30),
190
-
max_retries:3,
191
-
retry_delay:Duration::from_millis(100),
192
-
max_retry_delay:Duration::from_secs(5),
193
-
retry_jitter:true,
194
-
};
195
-
196
-
letclient=ZinitClient::with_config(config);
197
-
```
198
-
199
-
## Examples
58
+
// List all services
59
+
letservices=client.list().await?;
200
60
201
-
See the [examples](./examples) directory for more usage examples:
61
+
// Service lifecycle
62
+
client.start("service-name").await?;
63
+
client.stop("service-name").await?;
64
+
client.restart("service-name").await?;
202
65
203
-
### Running Examples
204
-
205
-
To run the examples, you need a running Zinit instance. The examples will try to connect to Zinit at the default socket path (`/var/run/zinit.sock`).
206
-
207
-
```bash
208
-
# Basic usage example (requires Zinit)
209
-
cargo run --example basic_usage
210
-
211
-
# Comprehensive service management with CRUD operations (interactive, requires Zinit)
212
-
cargo run --example service_management
213
-
214
-
# Log streaming example (interactive, requires Zinit)
215
-
cargo run --example log_streaming
216
-
217
-
# Mock server demo (doesn't require Zinit)
218
-
cargo run --example mock_server_demo
219
-
```
220
-
221
-
If you want to use a different socket path, you'll need to modify the examples. Open the example file and change the socket path in the `ZinitClient::new()` call:
// Create/delete services (if supported by server)
70
+
client.create_service("name", config).await?;
71
+
client.delete_service("name").await?;
229
72
```
230
73
231
-
### Running Without Zinit
232
-
233
-
If you don't have Zinit running, you can still test the client by using the mock server provided in the tests directory. The mock server simulates a Zinit instance for testing purposes.
234
-
235
-
Here's how to use it:
236
-
237
-
```rust
238
-
usestd::path::PathBuf;
239
-
usetempfile::tempdir;
240
-
usezinit_client::ZinitClient;
241
-
usezinit_client::tests::MockZinitServer;
242
-
243
-
#[tokio::main]
244
-
asyncfnmain() ->Result<()> {
245
-
// Create a temporary directory for the socket
246
-
lettemp_dir=tempdir().expect("Failed to create temp dir");
server.start().await.expect("Failed to start mock server");
252
-
253
-
// Add some mock services
254
-
server.add_service(MockService {
255
-
name:"test-service".to_string(),
256
-
pid:1001,
257
-
state:MockServiceState::Running,
258
-
target:MockServiceTarget::Up,
259
-
after:HashMap::new(),
260
-
});
261
-
262
-
// Create a client to connect to the mock server
263
-
letclient=ZinitClient::new(&socket_path);
264
-
265
-
// Use the client as normal
266
-
letservices=client.list().await?;
267
-
println!("Services: {:?}", services);
268
-
269
-
// Stop the mock server when done
270
-
server.stop().await;
271
-
272
-
Ok(())
273
-
}
274
-
```
275
-
276
-
Note: The examples assume Zinit is running and listening on the default socket path (`/var/run/zinit.sock`). If your Zinit instance is using a different socket path, you'll need to modify the examples accordingly.
277
-
278
-
## Testing
279
-
280
-
The library includes both integration tests and a mock server for testing without requiring a real Zinit instance.
74
+
## Examples
281
75
282
-
### Running Tests
76
+
Run the demo to see the universal interface in action:
283
77
284
78
```bash
285
-
# Run all tests
286
-
cargo test
287
-
288
-
# Run a specific test
289
-
cargo test test_client_reconnection
79
+
cargo run --example <example_name><sock_path>
290
80
```
291
81
292
-
All tests use the mock server, so they can be run without requiring a real Zinit instance. The mock server simulates a Zinit instance for testing purposes, allowing for reliable and reproducible tests.
293
-
294
-
## CI/CD
295
-
296
-
This project uses GitHub Actions for continuous integration and delivery:
297
-
298
-
-**Rust CI**: Builds the project, runs tests, and checks code formatting and linting
299
-
-**Rust Examples**: Builds and runs all examples to ensure they work correctly
300
-
-**Code Coverage**: Generates code coverage reports and uploads them to Codecov
301
-
-**Security Scan**: Performs security audits on dependencies using cargo-audit and cargo-deny
302
-
-**Publish**: Automatically publishes the crate to crates.io when a new release is created
82
+
## Documentation
303
83
304
-
All workflows run on every push to any branch and on all pull requests.
84
+
For detailed API documentation, visit [docs.rs/zinit-client](https://docs.rs/zinit-client).
305
85
306
86
## License
307
87
308
-
See[LICENSE](LICENSE) file for details.
88
+
This project is licensed under the MIT License - see the[LICENSE](LICENSE) file for details.
0 commit comments