Class: Pvectl::Services::ServiceLifecycle

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/services/service_lifecycle.rb,
sig/pvectl/services/service_lifecycle.rbs

Overview

Orchestrates systemd service lifecycle operations (start/stop/restart/reload) on a Proxmox node.

Wraps Repositories::Service to provide a uniform return shape suitable for table/JSON/YAML formatting (Models::NodeOperationResult) and to convert API errors into structured results instead of raising.

Examples:

Restarting a service

service = ServiceLifecycle.new(service_repository: repo)
result = service.execute(operation: :restart, node: "pve1", service: "pveproxy")
result.successful? # => true

Constant Summary collapse

OPERATIONS =

Operations supported by the Proxmox services API.

i[start stop restart reload].freeze

Instance Method Summary collapse

Constructor Details

#initialize(service_repository:) ⇒ ServiceLifecycle

Creates a new ServiceLifecycle orchestrator.



24
25
26
# File 'lib/pvectl/services/service_lifecycle.rb', line 24

def initialize(service_repository:)
  @service_repository = service_repository
end

Instance Method Details

#build_result(operation:, node:, service:, **attrs) ⇒ Models::NodeOperationResult

Builds a NodeOperationResult for the operation.



78
79
80
81
82
83
84
85
86
# File 'lib/pvectl/services/service_lifecycle.rb', line 78

def build_result(operation:, node:, service:, **attrs)
  node_model = Models::Node.new(name: node)
  Models::NodeOperationResult.new(
    operation: operation,
    node_model: node_model,
    resource: { service: service, node: node },
    **attrs
  )
end

#execute(operation:, node:, service:) ⇒ Models::NodeOperationResult

Executes a lifecycle operation on a single service.

Raises:

  • (ArgumentError)

    when operation is unknown



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pvectl/services/service_lifecycle.rb', line 35

def execute(operation:, node:, service:)
  validate_operation!(operation)

  begin
    upid = @service_repository.public_send(operation, node, service)
    build_result(
      operation: operation,
      node: node,
      service: service,
      task_upid: upid,
      success: :pending
    )
  rescue StandardError => e
    build_result(
      operation: operation,
      node: node,
      service: service,
      success: false,
      error: e.message
    )
  end
end

#validate_operation!(operation) ⇒ void

This method returns an undefined value.

Validates operation is one of the supported actions.

Raises:

  • (ArgumentError)

    when not in OPERATIONS



64
65
66
67
68
69
# File 'lib/pvectl/services/service_lifecycle.rb', line 64

def validate_operation!(operation)
  return if OPERATIONS.include?(operation)

  raise ArgumentError,
        "Unknown service operation: #{operation}. Valid: #{OPERATIONS.join(', ')}"
end