Class: Pvectl::Services::Wakeonlan

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

Overview

Sends a Wake-on-LAN packet to a cluster node.

Verifies the node exists in the cluster, calls the WoL API endpoint, and wraps the outcome in a NodeOperationResult. The Proxmox API returns the MAC address used for the magic packet on success, which is surfaced in the result message for confirmation.

Examples:

Basic usage

service = Wakeonlan.new(node_repository: repo)
result = service.execute(node_name: "pve3")
result.successful? #=> true
result.message     #=> "Wake-on-LAN packet sent (MAC: AA:BB:CC:DD:EE:FF)"

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(node_repository:) ⇒ Wakeonlan

Creates a new service.



25
26
27
# File 'lib/pvectl/services/wakeonlan.rb', line 25

def initialize(node_repository:)
  @node_repository = node_repository
end

Instance Method Details

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

Builds a NodeOperationResult.



50
51
52
53
54
55
56
57
# File 'lib/pvectl/services/wakeonlan.rb', line 50

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

#execute(node_name:) ⇒ Models::NodeOperationResult

Sends the WoL packet to a node.



33
34
35
36
37
38
39
40
41
# File 'lib/pvectl/services/wakeonlan.rb', line 33

def execute(node_name:)
  node = @node_repository.get(node_name)
  return not_found_result(node_name) unless node

  mac = @node_repository.wakeonlan(node_name)
  build_result(node, success: true, message: success_message(mac))
rescue StandardError => e
  build_result(node || Models::Node.new(name: node_name), success: false, error: e.message)
end

#not_found_result(node_name) ⇒ Models::NodeOperationResult

Builds a not-found error result.



63
64
65
66
# File 'lib/pvectl/services/wakeonlan.rb', line 63

def not_found_result(node_name)
  node = Models::Node.new(name: node_name)
  build_result(node, success: false, error: "Node #{node_name} not found")
end

#success_message(mac) ⇒ String

Formats the success message including the MAC address when returned.



72
73
74
75
76
# File 'lib/pvectl/services/wakeonlan.rb', line 72

def success_message(mac)
  return "Wake-on-LAN packet sent" if mac.nil? || mac.to_s.empty?

  "Wake-on-LAN packet sent (MAC: #{mac})"
end