Class: Pvectl::Commands::Cloudinit::Pending

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

Overview

Handler for the ‘pvectl cloudinit pending vm <id>` subcommand.

Lists cloud-init configuration entries currently differing from the values used to build the active ISO. Each entry contains a key, the current value, the pending value, and a delete flag.

Examples:

Usage

pvectl cloudinit pending vm 100
pvectl cloudinit pending vm 100 -o json

Class Method Summary collapse

Class Method Details

.execute(args, options, global_options) ⇒ Integer

Executes the pending subcommand.

Parameters:

  • args (Array<String>)

    command arguments

  • options (Hash)

    command-local options

  • global_options (Hash)

    global CLI options

Returns:

  • (Integer)

    exit code



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pvectl/commands/cloudinit/pending.rb', line 62

def self.execute(args, options, global_options)
  resource_type = args[0]
  vmid_arg = args[1]

  return Cloudinit.usage_error("Resource type required (vm)") unless resource_type
  return Cloudinit.usage_error("VMID is required") unless vmid_arg
  return Cloudinit.unknown_resource_type(resource_type) unless resource_type == "vm"

  Cloudinit.with_service(global_options) do |service|
    entries = service.pending(vmid_arg.to_i, node: options[:node])
    render(entries, global_options)
  end
end

.register_subcommand(parent) ⇒ void

This method returns an undefined value.

Registers the pending subcommand under the cloudinit parent.

Parameters:

  • parent (GLI::Command)

    parent cloudinit command



21
22
23
24
25
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
# File 'lib/pvectl/commands/cloudinit/pending.rb', line 21

def self.register_subcommand(parent)
  parent.desc "List pending cloud-init configuration changes"
  parent.long_desc <<~HELP
    DESCRIPTION
      Show cloud-init configuration entries that differ from the
      values currently embedded in the active cloud-init ISO. Use
      this to preview what will change on the next regeneration.

    EXAMPLES
      Show pending changes for VM 100:
        $ pvectl cloudinit pending vm 100

      JSON output for scripting:
        $ pvectl cloudinit pending vm 100 -o json

    NOTES
      An empty list means the active ISO is in sync with the
      current Proxmox configuration.

      Entries with the +action+ column set to +delete+ are about
      to be removed from the ISO.

    SEE ALSO
      pvectl help cloudinit regenerate   Apply pending changes
      pvectl help cloudinit dump         Inspect generated YAML
  HELP
  parent.arg_name "RESOURCE_TYPE ID"
  parent.command :pending do |c|
    c.action do |global_options, options, args|
      exit_code = execute(args, options, global_options)
      exit exit_code if exit_code != 0
    end
  end
end

.render(entries, global_options) ⇒ void

This method returns an undefined value.

Renders pending entries via the configured output formatter.

For table output, prints a flat 4-column table (key, current, pending, action) or a friendly “no pending changes” notice when the list is empty. For json/yaml output, emits the raw collection so it can be parsed downstream.

Parameters:

  • entries (Array<Hash>)

    pending entries from the service

  • global_options (Hash)

    global CLI options



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pvectl/commands/cloudinit/pending.rb', line 86

def self.render(entries, global_options)
  format = global_options[:output] || "table"

  case format
  when "json"
    require "json"
    $stdout.puts JSON.pretty_generate(entries)
  when "yaml"
    require "yaml"
    $stdout.puts entries.map { |e| stringify_keys(e) }.to_yaml
  else
    render_table(entries)
  end
end

.render_table(entries) ⇒ void

This method returns an undefined value.

Renders pending entries as a plain text table.

Parameters:

  • entries (Array<Hash>)

    pending entries



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pvectl/commands/cloudinit/pending.rb', line 105

def self.render_table(entries)
  if entries.empty?
    $stdout.puts "No pending cloud-init changes."
    return
  end

  rows = entries.map do |e|
    action = e[:delete].to_i.positive? ? "delete" : (e[:pending] ? "update" : "-")
    [e[:key].to_s, (e[:value] || "-").to_s, (e[:pending] || "-").to_s, action]
  end

  headers = %w[KEY CURRENT PENDING ACTION]
  widths = headers.each_with_index.map do |h, i|
    [h.length, *rows.map { |r| r[i].length }].max
  end

  $stdout.puts headers.each_with_index.map { |h, i| h.ljust(widths[i]) }.join("  ")
  rows.each do |row|
    $stdout.puts row.each_with_index.map { |v, i| v.ljust(widths[i]) }.join("  ")
  end
end

.stringify_keys(hash) ⇒ Hash

Stringifies hash keys for YAML output consistency.

Parameters:

  • hash (Hash)

    hash with symbol or string keys

Returns:

  • (Hash)

    hash with string keys



131
132
133
# File 'lib/pvectl/commands/cloudinit/pending.rb', line 131

def self.stringify_keys(hash)
  hash.each_with_object({}) { |(k, v), out| out[k.to_s] = v }
end