275 lines
6.8 KiB
V
275 lines
6.8 KiB
V
module main
|
|
|
|
import zinit
|
|
|
|
|
|
// Example 1: Get the OpenRPC API specification
|
|
fn example_rpc_discover(mut client zinit.Client) {
|
|
println('1. Getting API specification...')
|
|
|
|
spec := client.rpc_discover() or {
|
|
eprintln('Error getting API spec: ${err}')
|
|
return
|
|
}
|
|
|
|
println('API Title: ${spec['info'] or { map[string]interface{}{} }['title'] or { 'Unknown' }}')
|
|
println('API Version: ${spec['info'] or { map[string]interface{}{} }['version'] or { 'Unknown' }}')
|
|
println()
|
|
}
|
|
|
|
// Example 2: List all services
|
|
fn example_service_list(mut client zinit.Client) {
|
|
println('2. Listing all services...')
|
|
|
|
services := client.service_list() or {
|
|
eprintln('Error listing services: ${err}')
|
|
return
|
|
}
|
|
|
|
if services.len == 0 {
|
|
println('No services found.')
|
|
} else {
|
|
for name, state in services {
|
|
println(' ${name}: ${state}')
|
|
}
|
|
}
|
|
println()
|
|
}
|
|
|
|
// Example 3: Get detailed status for a specific service
|
|
fn example_service_status(mut client zinit.Client) {
|
|
println('3. Getting service status...')
|
|
|
|
// Try to get status for a service (replace 'redis' with an actual service name)
|
|
service_name := 'redis'
|
|
|
|
status := client.service_status(service_name) or {
|
|
eprintln('Error getting service status: ${err}')
|
|
return
|
|
}
|
|
|
|
println('Service: ${status.name}')
|
|
println(' State: ${status.state}')
|
|
println(' Target: ${status.target}')
|
|
println(' PID: ${status.pid}')
|
|
|
|
if status.after.len > 0 {
|
|
println(' Dependencies:')
|
|
for dep_name, dep_state in status.after {
|
|
println(' ${dep_name}: ${dep_state}')
|
|
}
|
|
}
|
|
println()
|
|
}
|
|
|
|
// Example 4: Create a new service configuration
|
|
fn example_service_create(mut client zinit.Client) {
|
|
println('4. Creating a new service...')
|
|
|
|
// Create a simple service configuration
|
|
config := zinit.ServiceConfig{
|
|
exec: '/usr/bin/echo "Hello from my service"'
|
|
oneshot: true
|
|
log: 'stdout'
|
|
env: {
|
|
'MY_VAR': 'my_value'
|
|
'PATH': '/usr/bin:/bin'
|
|
}
|
|
shutdown_timeout: 30
|
|
}
|
|
|
|
service_name := 'example_service'
|
|
|
|
result := client.service_create(service_name, config) or {
|
|
eprintln('Error creating service: ${err}')
|
|
return
|
|
}
|
|
|
|
println('Service created: ${result}')
|
|
println()
|
|
}
|
|
|
|
// Example 5: Service management operations
|
|
fn example_service_management(mut client zinit.Client) {
|
|
println('5. Service management operations...')
|
|
|
|
service_name := 'example_service'
|
|
|
|
// Monitor the service
|
|
client.service_monitor(service_name) or {
|
|
eprintln('Error monitoring service: ${err}')
|
|
return
|
|
}
|
|
println('Service ${service_name} is now being monitored')
|
|
|
|
// Start the service
|
|
client.service_start(service_name) or {
|
|
eprintln('Error starting service: ${err}')
|
|
return
|
|
}
|
|
println('Service ${service_name} started')
|
|
|
|
// Wait a moment for the service to run (since it's oneshot)
|
|
// In a real application, you might want to check the status instead
|
|
|
|
// Stop the service (if it's still running)
|
|
client.service_stop(service_name) or {
|
|
// This might fail if the service already finished (oneshot)
|
|
println('Note: Could not stop service (might have already finished): ${err}')
|
|
}
|
|
|
|
// Forget the service (stop monitoring)
|
|
client.service_forget(service_name) or {
|
|
eprintln('Error forgetting service: ${err}')
|
|
return
|
|
}
|
|
println('Service ${service_name} is no longer being monitored')
|
|
|
|
// Delete the service configuration
|
|
client.service_delete(service_name) or {
|
|
eprintln('Error deleting service: ${err}')
|
|
return
|
|
}
|
|
println('Service ${service_name} configuration deleted')
|
|
println()
|
|
}
|
|
|
|
// Example 6: Get service statistics
|
|
fn example_service_stats(mut client zinit.Client) {
|
|
println('6. Getting service statistics...')
|
|
|
|
// Try to get stats for a running service
|
|
service_name := 'redis' // Replace with an actual running service
|
|
|
|
stats := client.service_stats(service_name) or {
|
|
eprintln('Error getting service stats: ${err}')
|
|
return
|
|
}
|
|
|
|
println('Service: ${stats.name}')
|
|
println(' PID: ${stats.pid}')
|
|
println(' Memory Usage: ${stats.memory_usage} bytes (${stats.memory_usage / 1024 / 1024} MB)')
|
|
println(' CPU Usage: ${stats.cpu_usage}%')
|
|
|
|
if stats.children.len > 0 {
|
|
println(' Child Processes:')
|
|
for child in stats.children {
|
|
println(' PID ${child.pid}: ${child.memory_usage} bytes, ${child.cpu_usage}% CPU')
|
|
}
|
|
}
|
|
println()
|
|
}
|
|
|
|
// Example 7: Log streaming
|
|
fn example_log_streaming(mut client zinit.Client) {
|
|
println('7. Getting current logs...')
|
|
|
|
// Get all current logs
|
|
logs := client.stream_current_logs(none) or {
|
|
eprintln('Error getting logs: ${err}')
|
|
return
|
|
}
|
|
|
|
if logs.len == 0 {
|
|
println('No logs available')
|
|
} else {
|
|
println('Recent logs:')
|
|
for i, log_entry in logs {
|
|
println(' ${i + 1}: ${log_entry}')
|
|
if i >= 4 { // Show only first 5 logs
|
|
println(' ... (${logs.len - 5} more logs)')
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// Get logs for a specific service
|
|
service_logs := client.stream_current_logs('redis') or {
|
|
eprintln('Error getting service logs: ${err}')
|
|
return
|
|
}
|
|
|
|
if service_logs.len > 0 {
|
|
println('Redis service logs:')
|
|
for log_entry in service_logs {
|
|
println(' ${log_entry}')
|
|
}
|
|
}
|
|
println()
|
|
}
|
|
|
|
// Example 8: System operations
|
|
fn example_system_operations(mut client zinit.Client) {
|
|
println('8. System operations...')
|
|
|
|
// Start HTTP server (be careful with this in production)
|
|
server_result := client.system_start_http_server('127.0.0.1:8080') or {
|
|
eprintln('Error starting HTTP server: ${err}')
|
|
return
|
|
}
|
|
println('HTTP server: ${server_result}')
|
|
|
|
// Stop HTTP server
|
|
client.system_stop_http_server() or {
|
|
eprintln('Error stopping HTTP server: ${err}')
|
|
return
|
|
}
|
|
println('HTTP server stopped')
|
|
|
|
// Note: system_shutdown() and system_reboot() are commented out
|
|
// as they would actually shut down or reboot the system!
|
|
|
|
// client.system_shutdown() or {
|
|
// eprintln('Error shutting down system: ${err}')
|
|
// return
|
|
// }
|
|
// println('System shutdown initiated')
|
|
|
|
// client.system_reboot() or {
|
|
// eprintln('Error rebooting system: ${err}')
|
|
// return
|
|
// }
|
|
// println('System reboot initiated')
|
|
|
|
println('System operations completed (shutdown/reboot skipped for safety)')
|
|
println()
|
|
}
|
|
|
|
|
|
// Create a new zinit client with default socket path (/tmp/zinit.sock)
|
|
mut client := zinit.new_default_client()
|
|
|
|
// Alternatively, you can specify a custom socket path:
|
|
// mut client := zinit.new_client('/custom/path/to/zinit.sock')
|
|
|
|
// Ensure we disconnect when done
|
|
defer {
|
|
client.disconnect()
|
|
}
|
|
|
|
println('=== Zinit Client Example ===\n')
|
|
|
|
// Example 1: Get API specification
|
|
example_rpc_discover(mut client)
|
|
|
|
// Example 2: List all services
|
|
example_service_list(mut client)
|
|
|
|
// Example 3: Get service status
|
|
example_service_status(mut client)
|
|
|
|
// Example 4: Create a new service
|
|
example_service_create(mut client)
|
|
|
|
// Example 5: Service management operations
|
|
example_service_management(mut client)
|
|
|
|
// Example 6: Get service statistics
|
|
example_service_stats(mut client)
|
|
|
|
// Example 7: Log streaming
|
|
example_log_streaming(mut client)
|
|
|
|
// Example 8: System operations
|
|
example_system_operations(mut client)
|