Class: Pvectl::Presenters::Container
- 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.
Direct Known Subclasses
Instance Method Summary collapse
-
#columns ⇒ Array<String>
Returns column headers for standard table output.
-
#cpu_percent ⇒ String
Returns CPU usage as percentage string.
-
#disk_display ⇒ String
Returns disk formatted as “used/total GiB”.
-
#disk_total_gib ⇒ Float?
Returns total disk in GiB.
-
#disk_used_gib ⇒ Float?
Returns disk used in GiB.
-
#display_name ⇒ String
Returns display name, falling back to “CT-ctid” if name is nil.
-
#extra_columns ⇒ Array<String>
Returns additional column headers for wide output.
-
#extra_values(model, **_context) ⇒ Array<String>
Returns additional values for wide output.
-
#memory_display ⇒ String
Returns memory formatted as “used/total GiB”.
-
#memory_total_gib ⇒ Float?
Returns total memory in GiB.
-
#memory_used_gib ⇒ Float?
Returns memory used in GiB.
-
#netin_display ⇒ String
Returns network in bytes formatted.
-
#netout_display ⇒ String
Returns network out bytes formatted.
-
#pool_display ⇒ String
Returns pool display string.
-
#swap_display ⇒ String
Returns swap formatted as “used/total MiB”.
-
#swap_total_mib ⇒ Float?
Returns total swap in MiB.
-
#swap_used_mib ⇒ Float?
Returns swap used in MiB.
-
#to_description(model) ⇒ Hash
Converts Container model to description format for describe command.
-
#to_hash(model) ⇒ Hash
Converts Container model to hash for JSON/YAML output.
-
#to_row(model, **_context) ⇒ Array<String>
Converts Container model to table row values.
Methods inherited from Base
#tags_array, #tags_display, #template_display, #to_wide_row, #uptime_human, #wide_columns
Instance Method Details
#columns ⇒ Array<String>
Returns column headers for standard table output.
30 31 32 |
# File 'lib/pvectl/presenters/container.rb', line 30 def columns %w[NAME CTID STATUS NODE CPU MEMORY] end |
#cpu_percent ⇒ String
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.
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_display ⇒ String
Returns disk formatted as “used/total GiB”.
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_gib ⇒ Float?
Returns total disk in GiB.
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_gib ⇒ Float?
Returns disk used in GiB.
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_name ⇒ String
Returns display name, falling back to “CT-ctid” if name is nil.
172 173 174 |
# File 'lib/pvectl/presenters/container.rb', line 172 def display_name container.name || "CT-#{container.vmid}" end |
#extra_columns ⇒ Array<String>
Returns additional column headers for wide output.
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.
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, , swap_display, disk_display, netin_display, netout_display, pool_display ] end |
#memory_display ⇒ String
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.
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_gib ⇒ Float?
Returns total memory in GiB.
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_gib ⇒ Float?
Returns memory used in GiB.
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_display ⇒ String
Returns network in bytes formatted.
288 289 290 |
# File 'lib/pvectl/presenters/container.rb', line 288 def netin_display format_bytes(container.netin) end |
#netout_display ⇒ String
Returns network out bytes formatted.
295 296 297 |
# File 'lib/pvectl/presenters/container.rb', line 295 def netout_display format_bytes(container.netout) end |
#pool_display ⇒ String
Returns pool display string.
281 282 283 |
# File 'lib/pvectl/presenters/container.rb', line 281 def pool_display container.pool || "-" end |
#swap_display ⇒ String
Returns swap formatted as “used/total MiB”.
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_mib ⇒ Float?
Returns total swap in MiB.
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_mib ⇒ Float?
Returns swap used in MiB.
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.
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" => , "Description" => container.description || config[:description] || "-", "Summary" => format_summary, "Resources" => format_resources(config), "Network" => format_network_interfaces(config), "DNS" => format_dns(config), "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.
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" => } end |
#to_row(model, **_context) ⇒ Array<String>
Converts Container model to table row values.
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 |