Class: Pvectl::Presenters::Container

Inherits:
Base
  • Object
show all
Defined in:
lib/pvectl/presenters/container.rb

Overview

Presenter for LXC containers.

Defines column layout and formatting for table output. Used by formatters to render container data in various formats.

Standard columns: NAME, CTID, STATUS, NODE, CPU, MEMORY Wide columns add: UPTIME, TEMPLATE, TAGS, SWAP, DISK, NETIN, NETOUT, POOL

Description output is organized by Proxmox VE web UI tabs: Summary, Resources, Network, DNS, Options, Task History, Snapshots, HA.

Examples:

Using with formatter

presenter = Container.new
formatter = Formatters::Table.new
output = formatter.format(containers, presenter)

See Also:

Direct Known Subclasses

TopContainer

Instance Method Summary collapse

Methods inherited from Base

#tags_array, #tags_display, #template_display, #to_wide_row, #uptime_human, #wide_columns

Instance Method Details

#columnsArray<String>

Returns column headers for standard table output.

Returns:

  • (Array<String>)

    column headers



30
31
32
# File 'lib/pvectl/presenters/container.rb', line 30

def columns
  %w[NAME CTID STATUS NODE CPU MEMORY]
end

#cpu_percentString

Returns CPU usage as percentage string.

For running containers, shows actual usage percentage. For stopped containers, shows “-” for usage but includes core count if available.

Returns:

  • (String)

    CPU percentage (e.g., “12%/4”) or “-/4” for stopped containers



182
183
184
185
186
187
188
# File 'lib/pvectl/presenters/container.rb', line 182

def cpu_percent
  return "-" if container.maxcpu.nil?
  return "-/#{container.maxcpu}" unless container.running?
  return "-/#{container.maxcpu}" if container.cpu.nil?

  "#{(container.cpu * 100).round}%/#{container.maxcpu}"
end

#disk_displayString

Returns disk formatted as “used/total GiB”.

Returns:

  • (String)

    formatted disk (e.g., “15.0/50.0 GiB”) or “-” if unavailable



272
273
274
275
276
# File 'lib/pvectl/presenters/container.rb', line 272

def disk_display
  return "-" if disk_used_gib.nil?

  "#{disk_used_gib}/#{disk_total_gib} GiB"
end

#disk_total_gibFloat?

Returns total disk in GiB.

Returns:

  • (Float, nil)

    total disk in GiB, or nil if unavailable



263
264
265
266
267
# File 'lib/pvectl/presenters/container.rb', line 263

def disk_total_gib
  return nil if container.maxdisk.nil?

  (container.maxdisk.to_f / 1024 / 1024 / 1024).round(1)
end

#disk_used_gibFloat?

Returns disk used in GiB.

Returns:

  • (Float, nil)

    disk used in GiB, or nil if unavailable



254
255
256
257
258
# File 'lib/pvectl/presenters/container.rb', line 254

def disk_used_gib
  return nil if container.disk.nil?

  (container.disk.to_f / 1024 / 1024 / 1024).round(1)
end

#display_nameString

Returns display name, falling back to “CT-ctid” if name is nil.

Returns:

  • (String)

    display name



172
173
174
# File 'lib/pvectl/presenters/container.rb', line 172

def display_name
  container.name || "CT-#{container.vmid}"
end

#extra_columnsArray<String>

Returns additional column headers for wide output.

Returns:

  • (Array<String>)

    extra column headers



37
38
39
# File 'lib/pvectl/presenters/container.rb', line 37

def extra_columns
  %w[UPTIME TEMPLATE TAGS SWAP DISK NETIN NETOUT POOL]
end

#extra_values(model, **_context) ⇒ Array<String>

Returns additional values for wide output.

Parameters:

Returns:

  • (Array<String>)

    extra values matching extra_columns order



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pvectl/presenters/container.rb', line 63

def extra_values(model, **_context)
  @container = model
  [
    uptime_human,
    template_display,
    tags_display,
    swap_display,
    disk_display,
    netin_display,
    netout_display,
    pool_display
  ]
end

#memory_displayString

Returns memory formatted as “used/total GiB”.

For running containers, shows actual usage and total. For stopped containers, shows “-” for usage but includes total if available.

Returns:

  • (String)

    formatted memory (e.g., “2.1/4.0 GiB”) or “-/4.0 GiB” for stopped



214
215
216
217
218
219
220
# File 'lib/pvectl/presenters/container.rb', line 214

def memory_display
  return "-" if memory_total_gib.nil?
  return "-/#{memory_total_gib} GiB" unless container.running?
  return "-/#{memory_total_gib} GiB" if memory_used_gib.nil?

  "#{memory_used_gib}/#{memory_total_gib} GiB"
end

#memory_total_gibFloat?

Returns total memory in GiB.

Returns:

  • (Float, nil)

    total memory in GiB, or nil if unavailable



202
203
204
205
206
# File 'lib/pvectl/presenters/container.rb', line 202

def memory_total_gib
  return nil if container.maxmem.nil?

  (container.maxmem.to_f / 1024 / 1024 / 1024).round(1)
end

#memory_used_gibFloat?

Returns memory used in GiB.

Returns:

  • (Float, nil)

    memory used in GiB, or nil if unavailable



193
194
195
196
197
# File 'lib/pvectl/presenters/container.rb', line 193

def memory_used_gib
  return nil if container.mem.nil?

  (container.mem.to_f / 1024 / 1024 / 1024).round(1)
end

#netin_displayString

Returns network in bytes formatted.

Returns:

  • (String)

    formatted network in (e.g., “117.7 MiB”) or “-”



288
289
290
# File 'lib/pvectl/presenters/container.rb', line 288

def netin_display
  format_bytes(container.netin)
end

#netout_displayString

Returns network out bytes formatted.

Returns:

  • (String)

    formatted network out (e.g., “941.9 MiB”) or “-”



295
296
297
# File 'lib/pvectl/presenters/container.rb', line 295

def netout_display
  format_bytes(container.netout)
end

#pool_displayString

Returns pool display string.

Returns:

  • (String)

    pool name or “-” if no pool



281
282
283
# File 'lib/pvectl/presenters/container.rb', line 281

def pool_display
  container.pool || "-"
end

#swap_displayString

Returns swap formatted as “used/total MiB”.

Returns:

  • (String)

    formatted swap (e.g., “128/512 MiB”) or “-” if unavailable



243
244
245
246
247
248
249
# File 'lib/pvectl/presenters/container.rb', line 243

def swap_display
  return "-" if swap_total_mib.nil? || swap_total_mib.zero?
  return "-/#{swap_total_mib.round} MiB" unless container.running?
  return "-/#{swap_total_mib.round} MiB" if swap_used_mib.nil?

  "#{swap_used_mib.round}/#{swap_total_mib.round} MiB"
end

#swap_total_mibFloat?

Returns total swap in MiB.

Returns:

  • (Float, nil)

    total swap in MiB, or nil if unavailable



234
235
236
237
238
# File 'lib/pvectl/presenters/container.rb', line 234

def swap_total_mib
  return nil if container.maxswap.nil?

  (container.maxswap.to_f / 1024 / 1024).round(1)
end

#swap_used_mibFloat?

Returns swap used in MiB.

Returns:

  • (Float, nil)

    swap used in MiB, or nil if unavailable



225
226
227
228
229
# File 'lib/pvectl/presenters/container.rb', line 225

def swap_used_mib
  return nil if container.swap.nil?

  (container.swap.to_f / 1024 / 1024).round(1)
end

#to_description(model) ⇒ Hash

Converts Container model to description format for describe command.

Returns a structured Hash organized by Proxmox VE web UI tabs: Summary, Resources, Network, DNS, Options, Task History, Snapshots, High Availability. Nested Hashes create indented subsections. Arrays of Hashes render as inline tables.

Parameters:

Returns:

  • (Hash)

    structured hash for describe formatter



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/pvectl/presenters/container.rb', line 136

def to_description(model)
  @container = model
  @consumed_keys = Set.new
  data = container.describe_data || {}
  config = data[:config] || {}

  consume(:hostname, :description, :tags, :pool, :template, :lxc)

  {
    "Name" => display_name,
    "CTID" => container.vmid,
    "Status" => container.status,
    "Node" => container.node,
    "Tags" => tags_display,
    "Description" => container.description || config[:description] || "-",
    "Summary" => format_summary,
    "Resources" => format_resources(config),
    "Network" => format_network_interfaces(config),
    "DNS" => format_dns(config),
    "Options" => format_options(config),
    "Firewall" => format_firewall(data[:firewall]),
    "Firewall Rules" => format_firewall_rules(data[:firewall]),
    "Task History" => format_task_history(data[:tasks]),
    "Snapshots" => format_snapshots(data[:snapshots]),
    "High Availability" => format_ha,
    "Additional Configuration" => format_remaining(config)
  }
end

#to_hash(model) ⇒ Hash

Converts Container model to hash for JSON/YAML output.

Returns a structured hash with nested objects for complex data like CPU, memory, swap, disk, uptime, and network.

Parameters:

Returns:

  • (Hash)

    hash representation with string keys



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pvectl/presenters/container.rb', line 84

def to_hash(model)
  @container = model
  {
    "ctid" => container.vmid,
    "name" => container.name,
    "status" => container.status,
    "node" => container.node,
    "template" => container.template?,
    "pool" => container.pool,
    "cpu" => {
      "usage_percent" => container.cpu.nil? ? nil : (container.cpu * 100).round,
      "cores" => container.maxcpu
    },
    "memory" => {
      "used_gib" => memory_used_gib,
      "total_gib" => memory_total_gib,
      "used_bytes" => container.mem,
      "total_bytes" => container.maxmem
    },
    "swap" => {
      "used_mib" => swap_used_mib,
      "total_mib" => swap_total_mib,
      "used_bytes" => container.swap,
      "total_bytes" => container.maxswap
    },
    "disk" => {
      "used_gib" => disk_used_gib,
      "total_gib" => disk_total_gib,
      "used_bytes" => container.disk,
      "total_bytes" => container.maxdisk
    },
    "uptime" => {
      "seconds" => container.uptime,
      "human" => uptime_human
    },
    "network" => {
      "in_bytes" => container.netin,
      "out_bytes" => container.netout
    },
    "tags" => tags_array
  }
end

#to_row(model, **_context) ⇒ Array<String>

Converts Container model to table row values.

Parameters:

Returns:

  • (Array<String>)

    row values matching columns order



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pvectl/presenters/container.rb', line 46

def to_row(model, **_context)
  @container = model
  [
    display_name,
    container.vmid.to_s,
    container.status,
    container.node,
    cpu_percent,
    memory_display
  ]
end