Class: Pvectl::Commands::EditHosts

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/commands/edit_hosts.rb

Overview

Handler for the ‘pvectl edit hosts` command.

Opens /etc/hosts for a node in the user’s editor. On save, POSTs the new content back via /nodes/node/hosts with the original digest for optimistic concurrency control.

The node identifier may be supplied either as a positional argument (matching ‘pvectl edit node NAME` semantics) or via the –node flag.

Examples:

Basic usage

pvectl edit hosts pve1
pvectl edit hosts --node pve1

Dry-run mode

pvectl edit hosts pve1 --dry-run

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, options, global_options) ⇒ EditHosts

Initializes the command.

Parameters:

  • args (Array<String>)

    command arguments

  • options (Hash)

    command options

  • global_options (Hash)

    global CLI options



37
38
39
40
41
# File 'lib/pvectl/commands/edit_hosts.rb', line 37

def initialize(args, options, global_options)
  @args = args
  @options = options
  @global_options = global_options
end

Class Method Details

.execute(args, options, global_options) ⇒ Integer

Executes the edit hosts command.

Parameters:

  • args (Array<String>)

    command arguments (positional NODE)

  • options (Hash)

    command options (:node, :editor, :“dry-run”)

  • global_options (Hash)

    global CLI options

Returns:

  • (Integer)

    exit code



28
29
30
# File 'lib/pvectl/commands/edit_hosts.rb', line 28

def self.execute(args, options, global_options)
  new(args, options, global_options).execute
end

Instance Method Details

#executeInteger

Executes the edit flow.

Returns:

  • (Integer)

    exit code



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pvectl/commands/edit_hosts.rb', line 46

def execute
  node_name = @args.first || @options[:node]
  return usage_error("NODE is required (positional argument or --node flag)") if node_name.nil? || node_name.empty?

  load_config
  connection = Pvectl::Connection.new(@config)
  service = build_edit_service(connection)
  result = service.execute(node_name: node_name)

  if result.nil?
    $stdout.puts "Edit cancelled, no changes made."
    return ExitCodes::SUCCESS
  end

  if result.successful?
    if @options[:"dry-run"]
      $stdout.puts "(dry-run mode — no changes applied)"
    else
      $stdout.puts "/etc/hosts on node #{node_name} updated successfully."
    end
    ExitCodes::SUCCESS
  else
    $stderr.puts "Error: #{result.error}"
    ExitCodes::GENERAL_ERROR
  end
rescue Pvectl::Config::ConfigNotFoundError,
       Pvectl::Config::InvalidConfigError,
       Pvectl::Config::ContextNotFoundError,
       Pvectl::Config::ClusterNotFoundError,
       Pvectl::Config::UserNotFoundError
  raise
rescue StandardError => e
  $stderr.puts "Error: #{e.message}"
  ExitCodes::GENERAL_ERROR
end