Class: Pvectl::Commands::WakeonlanNode

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

Overview

Handler for the pvectl wakeonlan node command.

Sends a Wake-on-LAN magic packet to a cluster node. The target node must have its MAC address registered in the cluster configuration (via pvecm or the web UI) — without it, Proxmox cannot assemble the packet and the command returns an error.

The Proxmox API returns the MAC address used for the packet on success, which is surfaced in the output for confirmation.

Examples:

Wake up a node

pvectl wakeonlan node pve3

JSON output for scripting

pvectl wakeonlan node pve3 -o json

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_name, options, global_options, service: nil) ⇒ WakeonlanNode

Creates a new command instance.



75
76
77
78
79
80
# File 'lib/pvectl/commands/wakeonlan_node.rb', line 75

def initialize(node_name, options, global_options, service: nil)
  @node_name = node_name
  @options = options
  @global_options = global_options
  @service = service
end

Class Method Details

.register(cli) ⇒ void

This method returns an undefined value.

Registers the wakeonlan node command with the CLI.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pvectl/commands/wakeonlan_node.rb', line 26

def self.register(cli)
  cli.desc "Send Wake-on-LAN packet to a cluster node"
  cli.long_desc "    Trigger a Wake-on-LAN magic packet to a cluster node via the\n    Proxmox API (POST /nodes/{node}/wakeonlan).\n\n    EXAMPLES\n      Wake a node:\n        $ pvectl wakeonlan node pve3\n\n      Output as JSON:\n        $ pvectl wakeonlan node pve3 -o json\n\n    NOTES\n      The target node must have its MAC address registered in the\n      cluster configuration beforehand. Use `pvecm` or the Proxmox\n      web UI to set the MAC for each node before relying on WoL.\n\n      The packet is sent by another online node in the cluster, so\n      at least one other node must be reachable.\n\n    SEE ALSO\n      pvectl help get nodes      List nodes and current status\n      pvectl help describe node  Show node details\n  HELP\n  cli.arg_name \"RESOURCE_TYPE NAME\"\n  cli.command :wakeonlan do |c|\n    c.action do |global_options, _options, args|\n      resource_type = args.shift\n      resource_name = args.shift\n\n      unless resource_type == \"node\"\n        $stderr.puts \"Error: Only `pvectl wakeonlan node <NAME>` is supported\"\n        exit Pvectl::ExitCodes::USAGE_ERROR\n      end\n\n      cmd = WakeonlanNode.new(resource_name, {}, global_options)\n      exit_code = cmd.execute\n      exit exit_code if exit_code != 0\n    end\n  end\nend\n"

Instance Method Details

#color_enabledBoolean

Determines color output based on global flag and TTY status.



135
136
137
138
139
140
# File 'lib/pvectl/commands/wakeonlan_node.rb', line 135

def color_enabled
  explicit = @global_options[:color]
  return explicit unless explicit.nil?

  $stdout.tty?
end

#executeInteger

Executes the wakeonlan command.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pvectl/commands/wakeonlan_node.rb', line 85

def execute
  return usage_error("NODE name is required") if @node_name.nil? || @node_name.to_s.empty?

  result = service.execute(node_name: @node_name)
  render(result)

  result.successful? ? ExitCodes::SUCCESS : ExitCodes::GENERAL_ERROR
rescue Pvectl::Config::ConfigNotFoundError,
       Pvectl::Config::InvalidConfigError,
       Pvectl::Config::ContextNotFoundError,
       Pvectl::Config::ClusterNotFoundError,
       Pvectl::Config::UserNotFoundError
  raise
rescue Timeout::Error, Errno::ECONNREFUSED, SocketError => e
  $stderr.puts "Error: #{e.message}"
  ExitCodes::CONNECTION_ERROR
rescue StandardError => e
  $stderr.puts "Error: #{e.message}"
  ExitCodes::GENERAL_ERROR
end

#render(result) ⇒ void

This method returns an undefined value.

Renders the operation result using the requested output format.



125
126
127
128
129
130
# File 'lib/pvectl/commands/wakeonlan_node.rb', line 125

def render(result)
  format = @global_options[:output] || "table"
  presenter = Pvectl::Presenters::NodeOperationResult.new
  formatter = Pvectl::Formatters::Registry.for(format)
  puts formatter.format([result], presenter, color_enabled: color_enabled)
end

#serviceServices::Wakeonlan

Lazily builds the Wakeonlan service from current config.



111
112
113
114
115
116
117
118
119
# File 'lib/pvectl/commands/wakeonlan_node.rb', line 111

def service
  @service ||= begin
    config_service = Pvectl::Config::Service.new
    config_service.load(config: @global_options[:config])
    connection = Pvectl::Connection.new(config_service.current_config)
    node_repo = Pvectl::Repositories::Node.new(connection)
    Pvectl::Services::Wakeonlan.new(node_repository: node_repo)
  end
end

#usage_error(msg) ⇒ Integer

Outputs a usage error and returns the proper exit code.



146
147
148
149
150
# File 'lib/pvectl/commands/wakeonlan_node.rb', line 146

def usage_error(msg)
  $stderr.puts "Error: #{msg}"
  $stderr.puts "Usage: pvectl wakeonlan node <NODE>"
  ExitCodes::USAGE_ERROR
end