212 lines
6.4 KiB
V
212 lines
6.4 KiB
V
module zinit
|
|
|
|
import net.unix
|
|
import json
|
|
import time
|
|
|
|
// service_status shows detailed status information for a specific service
|
|
// name: the name of the service
|
|
pub fn (mut c Client) service_status(name string) !ServiceStatus {
|
|
params := [name]
|
|
response := c.send_request('service_status', params)!
|
|
|
|
result_map := response.result as map[string]interface{}
|
|
|
|
mut after_map := map[string]string{}
|
|
if after_raw := result_map['after'] {
|
|
if after_obj := after_raw as map[string]interface{} {
|
|
for key, value in after_obj {
|
|
after_map[key] = value.str()
|
|
}
|
|
}
|
|
}
|
|
|
|
return ServiceStatus{
|
|
name: result_map['name'] or { '' }.str()
|
|
pid: result_map['pid'] or { 0 }.int()
|
|
state: result_map['state'] or { '' }.str()
|
|
target: result_map['target'] or { '' }.str()
|
|
after: after_map
|
|
}
|
|
}
|
|
|
|
// service_start starts a service
|
|
// name: the name of the service to start
|
|
pub fn (mut c Client) service_start(name string) ! {
|
|
params := [name]
|
|
c.send_request('service_start', params)!
|
|
}
|
|
|
|
// service_stop stops a service
|
|
// name: the name of the service to stop
|
|
pub fn (mut c Client) service_stop(name string) ! {
|
|
params := [name]
|
|
c.send_request('service_stop', params)!
|
|
}
|
|
|
|
// service_monitor starts monitoring a service
|
|
// The service configuration is loaded from the config directory
|
|
// name: the name of the service to monitor
|
|
pub fn (mut c Client) service_monitor(name string) ! {
|
|
params := [name]
|
|
c.send_request('service_monitor', params)!
|
|
}
|
|
|
|
// service_forget stops monitoring a service
|
|
// You can only forget a stopped service
|
|
// name: the name of the service to forget
|
|
pub fn (mut c Client) service_forget(name string) ! {
|
|
params := [name]
|
|
c.send_request('service_forget', params)!
|
|
}
|
|
|
|
// service_kill sends a signal to a running service
|
|
// name: the name of the service to send the signal to
|
|
// signal: the signal to send (e.g., SIGTERM, SIGKILL)
|
|
pub fn (mut c Client) service_kill(name string, signal string) ! {
|
|
params := [name, signal]
|
|
c.send_request('service_kill', params)!
|
|
}
|
|
|
|
// service_create creates a new service configuration file
|
|
// name: the name of the service to create
|
|
// config: the service configuration
|
|
pub fn (mut c Client) service_create(name string, config ServiceConfig) !string {
|
|
params := [name, config]
|
|
response := c.send_request('service_create', params)!
|
|
return response.result.str()
|
|
}
|
|
|
|
// service_delete deletes a service configuration file
|
|
// name: the name of the service to delete
|
|
pub fn (mut c Client) service_delete(name string) !string {
|
|
params := [name]
|
|
response := c.send_request('service_delete', params)!
|
|
return response.result.str()
|
|
}
|
|
|
|
// service_get gets a service configuration file
|
|
// name: the name of the service to get
|
|
pub fn (mut c Client) service_get(name string) !ServiceConfig {
|
|
params := [name]
|
|
response := c.send_request('service_get', params)!
|
|
|
|
result_map := response.result as map[string]interface{}
|
|
|
|
mut after_list := []string{}
|
|
if after_raw := result_map['after'] {
|
|
if after_array := after_raw as []interface{} {
|
|
for item in after_array {
|
|
after_list << item.str()
|
|
}
|
|
}
|
|
}
|
|
|
|
mut env_map := map[string]string{}
|
|
if env_raw := result_map['env'] {
|
|
if env_obj := env_raw as map[string]interface{} {
|
|
for key, value in env_obj {
|
|
env_map[key] = value.str()
|
|
}
|
|
}
|
|
}
|
|
|
|
return ServiceConfig{
|
|
exec: result_map['exec'] or { '' }.str()
|
|
oneshot: result_map['oneshot'] or { false }.bool()
|
|
after: after_list
|
|
log: result_map['log'] or { '' }.str()
|
|
env: env_map
|
|
shutdown_timeout: result_map['shutdown_timeout'] or { 0 }.int()
|
|
}
|
|
}
|
|
|
|
// service_stats gets memory and CPU usage statistics for a service
|
|
// name: the name of the service to get stats for
|
|
pub fn (mut c Client) service_stats(name string) !ServiceStats {
|
|
params := [name]
|
|
response := c.send_request('service_stats', params)!
|
|
|
|
result_map := response.result as map[string]interface{}
|
|
|
|
mut children_list := []ChildStats{}
|
|
if children_raw := result_map['children'] {
|
|
if children_array := children_raw as []interface{} {
|
|
for child_raw in children_array {
|
|
if child_map := child_raw as map[string]interface{} {
|
|
children_list << ChildStats{
|
|
pid: child_map['pid'] or { 0 }.int()
|
|
memory_usage: child_map['memory_usage'] or { i64(0) }.i64()
|
|
cpu_usage: child_map['cpu_usage'] or { 0.0 }.f64()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return ServiceStats{
|
|
name: result_map['name'] or { '' }.str()
|
|
pid: result_map['pid'] or { 0 }.int()
|
|
memory_usage: result_map['memory_usage'] or { i64(0) }.i64()
|
|
cpu_usage: result_map['cpu_usage'] or { 0.0 }.f64()
|
|
children: children_list
|
|
}
|
|
}
|
|
|
|
// system_shutdown stops all services and powers off the system
|
|
pub fn (mut c Client) system_shutdown() ! {
|
|
c.send_request('system_shutdown', []interface{})!
|
|
}
|
|
|
|
// system_reboot stops all services and reboots the system
|
|
pub fn (mut c Client) system_reboot() ! {
|
|
c.send_request('system_reboot', []interface{})!
|
|
}
|
|
|
|
// system_start_http_server starts an HTTP/RPC server at the specified address
|
|
// address: the network address to bind the server to (e.g., '127.0.0.1:8080')
|
|
pub fn (mut c Client) system_start_http_server(address string) !string {
|
|
params := [address]
|
|
response := c.send_request('system_start_http_server', params)!
|
|
return response.result.str()
|
|
}
|
|
|
|
// system_stop_http_server stops the HTTP/RPC server if running
|
|
pub fn (mut c Client) system_stop_http_server() ! {
|
|
c.send_request('system_stop_http_server', []interface{})!
|
|
}
|
|
|
|
// stream_current_logs gets current logs from zinit and monitored services
|
|
// name: optional service name filter. If provided, only logs from this service will be returned
|
|
pub fn (mut c Client) stream_current_logs(name ?string) ![]string {
|
|
mut params := []interface{}
|
|
if service_name := name {
|
|
params << service_name
|
|
}
|
|
|
|
response := c.send_request('stream_currentLogs', params)!
|
|
|
|
if logs_array := response.result as []interface{} {
|
|
mut logs := []string{}
|
|
for log_entry in logs_array {
|
|
logs << log_entry.str()
|
|
}
|
|
return logs
|
|
}
|
|
|
|
return []string{}
|
|
}
|
|
|
|
// stream_subscribe_logs subscribes to log messages generated by zinit and monitored services
|
|
// name: optional service name filter. If provided, only logs from this service will be returned
|
|
// Note: This method returns a single log message. For continuous streaming, call this method repeatedly
|
|
pub fn (mut c Client) stream_subscribe_logs(name ?string) !string {
|
|
mut params := []interface{}
|
|
if service_name := name {
|
|
params << service_name
|
|
}
|
|
|
|
response := c.send_request('stream_subscribeLogs', params)!
|
|
return response.result.str()
|
|
}
|