Class: Pvectl::Repositories::Service

Inherits:
Base
  • Object
show all
Defined in:
lib/pvectl/repositories/service.rb,
sig/pvectl/repositories/service.rbs

Overview

Repository for systemd services on Proxmox nodes.

Wraps the /nodes/{node}/services API endpoints. Provides listing of services on a node and lifecycle operations (start/stop/restart/reload) which return Proxmox task UPIDs.

Examples:

Listing services on a node

repo = Service.new(connection)
services = repo.list(node: "pve1")
services.each { |s| puts "#{s.service}: #{s.active_state}" }

Restarting a service

upid = repo.restart("pve1", "pveproxy")

See Also:

Instance Attribute Summary

Attributes inherited from Base

#connection

Instance Method Summary collapse

Methods inherited from Base

#extract_data, #get, #initialize, #models_from, #unwrap

Constructor Details

This class inherits a constructor from Pvectl::Repositories::Base

Instance Method Details

#build_model(data) ⇒ Models::Service

Builds Service model from API response data.



95
96
97
# File 'lib/pvectl/repositories/service.rb', line 95

def build_model(data)
  Models::Service.new(data)
end

#list(node: nil) ⇒ Array<Models::Service>

Lists systemd services on a node.

When node is nil, iterates over all online nodes in the cluster. When node is specified, queries only that node.



30
31
32
33
34
35
36
# File 'lib/pvectl/repositories/service.rb', line 30

def list(node: nil)
  if node
    services_for_node(node)
  else
    online_nodes.flat_map { |node_name| services_for_node(node_name) }
  end
end

#online_nodesArray<String>

Fetches list of online node names.



128
129
130
131
132
133
134
135
136
# File 'lib/pvectl/repositories/service.rb', line 128

def online_nodes
  response = connection.client["nodes"].get
  nodes_data = unwrap(response)
  nodes_data
    .select { |n| n[:status] == "online" }
    .map { |n| n[:node] || n[:name] }
rescue StandardError
  []
end

#post_action(node, service, action) ⇒ String

POSTs a lifecycle action and extracts the UPID from the response.



107
108
109
110
111
# File 'lib/pvectl/repositories/service.rb', line 107

def post_action(node, service, action)
  resp = connection.client["nodes/#{node}/services/#{service}/#{action}"].post({})
  data = extract_data(resp)
  data.is_a?(String) ? data : data.to_s
end

#reload(node, service) ⇒ String

Reloads a service (falls back to restart if unsupported). Returns the task UPID.



85
86
87
# File 'lib/pvectl/repositories/service.rb', line 85

def reload(node, service)
  post_action(node, service, "reload")
end

#restart(node, service) ⇒ String

Hard-restarts a service. Returns the task UPID.



76
77
78
# File 'lib/pvectl/repositories/service.rb', line 76

def restart(node, service)
  post_action(node, service, "restart")
end

#services_for_node(node_name) ⇒ Array<Models::Service>

Fetches services for a single node.



117
118
119
120
121
122
123
# File 'lib/pvectl/repositories/service.rb', line 117

def services_for_node(node_name)
  response = connection.client["nodes/#{node_name}/services"].get
  services_data = unwrap(response)
  services_data.map { |data| build_model(data.merge(node: node_name)) }
rescue StandardError
  []
end

#start(node, service) ⇒ String

Starts a service. Returns the task UPID.



58
59
60
# File 'lib/pvectl/repositories/service.rb', line 58

def start(node, service)
  post_action(node, service, "start")
end

#state(node, service) ⇒ Models::Service?

Reads single service state.



43
44
45
46
47
48
49
50
51
# File 'lib/pvectl/repositories/service.rb', line 43

def state(node, service)
  resp = connection.client["nodes/#{node}/services/#{service}/state"].get
  data = extract_data(resp)
  return nil if data.nil? || data.empty?

  build_model(data.merge(node: node))
rescue StandardError
  nil
end

#stop(node, service) ⇒ String

Stops a service. Returns the task UPID.



67
68
69
# File 'lib/pvectl/repositories/service.rb', line 67

def stop(node, service)
  post_action(node, service, "stop")
end