Module: Chef::Knife::ProxmoxBase
- Included in:
- ProxmoxClusterList, ProxmoxTemplateList, ProxmoxVmBootstrap, ProxmoxVmCreate, ProxmoxVmList
- Defined in:
- lib/chef/knife/helpers/proxmox_base.rb
Overview
Shared behaviour mixed into every ‘knife proxmox …` command (SEAM 2).
Owns cluster resolution, the Client/Api factory and the secret-from-ENV override so individual commands stay declarative: they call #proxmox_api, #proxmox_client and #msg_pair and never touch the clusters hash or the environment directly.
The token secret is resolved with ENV precedence and is never sent empty — a missing secret is a loud, fatal error, not a silent default.
Constant Summary collapse
- ENV_SECRET_GLOBAL =
Global ENV override applied to whichever cluster is selected.
"KNIFE_PROXMOX_TOKEN_SECRET"- ENV_SECRET_PREFIX =
Per-cluster ENV override prefix; suffix is the cluster key normalized via #env_key.
"KNIFE_PROXMOX_TOKEN_SECRET_"- BYTE_UNITS =
Binary byte units, lowest first. Proxmox reports sizes in bytes against a 1024 base, so render in IEC units (KiB, MiB, …) to match the values shown in the Proxmox UI.
%w{B KiB MiB GiB TiB PiB}.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#human_bytes(value) ⇒ Object
Render a byte count as a human-readable IEC size (e.g. 10_737_418_240 -> “10 GiB”).
-
#human_duration(seconds) ⇒ Object
Render a second count as a compact duration (e.g. 3600 -> “1h”, 90_061 -> “1d 1h 1m 1s”).
-
#msg_pair(label, value, color = :cyan) ⇒ Object
Print a colored “label: value” line, skipping blank values.
-
#proxmox_api ⇒ Object
The endpoint map over #proxmox_client.
-
#proxmox_client ⇒ Object
The transport client for the selected cluster.
-
#proxmox_cluster_config ⇒ Object
The selected cluster as a symbol-keyed Hash, with :token_secret resolved through the ENV override.
-
#proxmox_token_secret_present?(cluster_key, cluster_hash) ⇒ Boolean
Whether a token secret resolves for the given cluster (symbol-keyed hash), using the same precedence as #resolve_token_secret but without fatal-ing.
Class Method Details
.included(includer) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/chef/knife/helpers/proxmox_base.rb', line 23 def self.included(includer) includer.class_eval do option :proxmox_cluster, long: "--proxmox-cluster NAME", description: "Proxmox cluster key from knife[:proxmox_clusters]" end end |
Instance Method Details
#human_bytes(value) ⇒ Object
Render a byte count as a human-readable IEC size (e.g. 10_737_418_240 -> “10 GiB”). Passes nil through untouched so absent fields (a template has no live size) stay nil rather than becoming a misleading “0 B”.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/chef/knife/helpers/proxmox_base.rb', line 63 def human_bytes(value) return value if value.nil? size = value.to_f unit = 0 while size >= 1024 && unit < BYTE_UNITS.length - 1 size /= 1024 unit += 1 end format("%g %s", size.round(2), BYTE_UNITS[unit]) end |
#human_duration(seconds) ⇒ Object
Render a second count as a compact duration (e.g. 3600 -> “1h”, 90_061 -> “1d 1h 1m 1s”). nil passes through (a stopped/template VM reports no uptime); 0 renders as “0s”.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/chef/knife/helpers/proxmox_base.rb', line 77 def human_duration(seconds) return seconds if seconds.nil? days, rest = seconds.to_i.divmod(86_400) hours, rest = rest.divmod(3_600) minutes, secs = rest.divmod(60) parts = [] parts << "#{days}d" if days > 0 parts << "#{hours}h" if hours > 0 parts << "#{minutes}m" if minutes > 0 parts << "#{secs}s" if secs > 0 || parts.empty? parts.join(" ") end |
#msg_pair(label, value, color = :cyan) ⇒ Object
Print a colored “label: value” line, skipping blank values. Not a knife-core helper; commands depend on it being defined here.
52 53 54 |
# File 'lib/chef/knife/helpers/proxmox_base.rb', line 52 def msg_pair(label, value, color = :cyan) ui.info("#{ui.color(label, color)}: #{value}") if value && !value.to_s.empty? end |
#proxmox_api ⇒ Object
The endpoint map over #proxmox_client. Memoized.
45 46 47 48 |
# File 'lib/chef/knife/helpers/proxmox_base.rb', line 45 def proxmox_api # Anchor at top level: inside Chef::Knife, a bare Knife resolves to Chef::Knife. @proxmox_api ||= ::Knife::Proxmox::Api.new(proxmox_client) end |
#proxmox_client ⇒ Object
The transport client for the selected cluster. Warns on every run when TLS verification is disabled. Memoized.
40 41 42 |
# File 'lib/chef/knife/helpers/proxmox_base.rb', line 40 def proxmox_client @proxmox_client ||= build_proxmox_client end |
#proxmox_cluster_config ⇒ Object
The selected cluster as a symbol-keyed Hash, with :token_secret resolved through the ENV override. Fatals (and lists available keys) when no cluster is selected or the selected key is unknown. Memoized.
34 35 36 |
# File 'lib/chef/knife/helpers/proxmox_base.rb', line 34 def proxmox_cluster_config @proxmox_cluster_config ||= resolve_cluster_config end |
#proxmox_token_secret_present?(cluster_key, cluster_hash) ⇒ Boolean
Whether a token secret resolves for the given cluster (symbol-keyed hash), using the same precedence as #resolve_token_secret but without fatal-ing. Lets ‘cluster list` report secret presence from a single source of truth.
95 96 97 |
# File 'lib/chef/knife/helpers/proxmox_base.rb', line 95 def proxmox_token_secret_present?(cluster_key, cluster_hash) !token_secret_from_sources(cluster_key, cluster_hash).nil? end |