knife-proxmox-ve 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 44a5c4bf238510fc6036949da54fbc039f51c0ff7d3d9afd851be8b4f6e2681e
4
+ data.tar.gz: e3de5df406aa26142068a1f087f485604e7f0f0237fc20da7473ff7ec6caf187
5
+ SHA512:
6
+ metadata.gz: 3d4500fc914372ba78bec85f72786a58bae1177fa3d900f8faa75896f664bda4f6805da6ea2f2aad35f81abbc995981f7de52b85b464b7f4b8142d28a9ab5580
7
+ data.tar.gz: 63006a55bc3d193884fbca1344d99f88719e7bfe475b9d67a48ddadde9ccff2d723f06c757e26263964206c2631511f2b3dc48aba1ea556883ded843925803a5
data/CHANGELOG.md ADDED
@@ -0,0 +1,24 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-06-04
11
+
12
+ ### Added
13
+
14
+ - `knife proxmox vm create` — create a VM on Proxmox VE by cloning a prepared
15
+ template and configuring CPU/RAM/network/cloud-init.
16
+ - `knife proxmox vm bootstrap` — create a VM and automatically bootstrap it with
17
+ Chef/CINC in a single command, waiting for the VM to boot first.
18
+ - Read-only helpers: `knife proxmox cluster list`, `knife proxmox template list`
19
+ and `knife proxmox vm list`.
20
+ - Support for multiple Proxmox clusters defined in `~/.cinc/config.rb`.
21
+ - Authentication via Proxmox VE API tokens.
22
+
23
+ [Unreleased]: https://github.com/pwojcieszonek/knife-proxmox-ve/compare/v0.1.0...HEAD
24
+ [0.1.0]: https://github.com/pwojcieszonek/knife-proxmox-ve/releases/tag/v0.1.0
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or Derivative
95
+ Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and do
117
+ not modify the License. You may add Your own attribution notices
118
+ within Derivative Works that You distribute, alongside or as an
119
+ addendum to the NOTICE text from the Work, provided that such
120
+ additional attribution notices cannot be construed as modifying
121
+ the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 Piotr Wojcieszonek
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,225 @@
1
+ # knife-proxmox-ve
2
+
3
+ A [knife](https://docs.chef.io/workstation/knife/) plugin (Chef Infra / CINC
4
+ Workstation) that **creates VMs on Proxmox VE** by cloning a pre-configured template,
5
+ configuring it, starting it, and waiting for it to boot. Use `vm create` to provision a
6
+ VM, or `vm bootstrap` to provision it **and automatically bootstrap it with Chef/CINC**
7
+ in one command.
8
+
9
+ The plugin only ever *clones* a prepared template; it never builds an image. All VM
10
+ parameters are optional: anything you don't pass keeps the value baked into the
11
+ template.
12
+
13
+ ## Requirements
14
+
15
+ - Chef Workstation / CINC Workstation 18+ (the plugin depends on the `knife` gem ≥ 18).
16
+ - A Proxmox VE **API token** (see [Authentication](#authentication)).
17
+ - A template that already has a **cloud-init drive** (e.g. `ide2=<storage>:cloudinit`)
18
+ and the **QEMU guest agent** enabled. Cloud-init settings are silently ignored by
19
+ Proxmox if the template has no cloud-init drive — the plugin fails fast when it
20
+ detects this.
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ chef gem install knife-proxmox-ve # Chef Workstation
26
+ cinc gem install knife-proxmox-ve # CINC Workstation
27
+ ```
28
+
29
+ knife discovers the commands automatically (no registration step). For development
30
+ from a checkout, run via `cinc exec bundle exec knife proxmox ...`.
31
+
32
+ ## Configuration
33
+
34
+ Proxmox clusters are defined in `~/.cinc/config.rb` (or `~/.chef/config.rb`) under
35
+ `knife[:proxmox_clusters]`. Each key is a cluster name selectable with
36
+ `--proxmox-cluster`.
37
+
38
+ ```ruby
39
+ knife[:proxmox_clusters] = {
40
+ "production" => {
41
+ host: "pve1.example.com", # required
42
+ port: 8006, # optional (default 8006)
43
+ token_id: "knife@pve!automation", # required; the USER@REALM!TOKENID part
44
+ token_secret: nil, # optional in-file; prefer the ENV override below
45
+ verify_ssl: true, # optional (default true); false warns every run
46
+
47
+ # Optional per-cluster provisioning defaults (any CLI flag overrides these):
48
+ storage: "local-lvm",
49
+ bridge: "vmbr0",
50
+ ciuser: "ubuntu",
51
+ nameserver: "10.0.0.1",
52
+ searchdomain: "example.com",
53
+ target_node: nil,
54
+ },
55
+ }
56
+
57
+ # Cluster used when --proxmox-cluster is omitted:
58
+ knife[:proxmox_default_cluster] = "production"
59
+ ```
60
+
61
+ ### Authentication
62
+
63
+ The plugin authenticates with a Proxmox **API token** (`Authorization: PVEAPIToken=...`).
64
+ The token secret is resolved with this precedence (highest first):
65
+
66
+ 1. `KNIFE_PROXMOX_TOKEN_SECRET` — global override for whichever cluster is selected
67
+ 2. `KNIFE_PROXMOX_TOKEN_SECRET_<CLUSTER>` — per cluster (key upcased, non-alphanumerics → `_`)
68
+ 3. `token_secret:` in the cluster hash
69
+
70
+ Prefer an environment variable so the secret never sits in a file. **If you do store
71
+ `token_secret:` in `config.rb`, `chmod 600 ~/.cinc/config.rb`** — anyone who can read
72
+ the file can read the token.
73
+
74
+ ```bash
75
+ export KNIFE_PROXMOX_TOKEN_SECRET="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
76
+ ```
77
+
78
+ > Proxmox API tokens default to **privilege separation**: a fresh token has no rights
79
+ > until you grant ACLs to the token itself. A first-run `HTTP 403` almost always means
80
+ > this. The token needs: `VM.Clone`, `VM.Config.Disk`, `VM.Config.Network`,
81
+ > `VM.Config.Memory`, `VM.Config.CPU`, `VM.Config.Cloudinit`, `VM.Config.Options`,
82
+ > `VM.PowerMgmt`, `VM.Audit`, `VM.Monitor` (guest-agent calls; PVE 8 and earlier),
83
+ > `VM.Allocate`, and `Datastore.AllocateSpace` on the target storage (full clones).
84
+
85
+ ### TLS
86
+
87
+ HTTPS to `:8006` is verified by default. Set `verify_ssl: false` per cluster only for
88
+ self-signed lab hosts — the plugin warns on **every** command when verification is off,
89
+ because the connection (carrying the token) is then MITM-exploitable.
90
+
91
+ ## Commands
92
+
93
+ ### `knife proxmox vm create NAME`
94
+
95
+ Clone a template → configure → start. Stops there — **no Chef bootstrap runs**. Use it to
96
+ stand up a VM you do not (yet) want to manage with Chef. `NAME` (the new VM's name) and
97
+ `--template` are the only required inputs; every other parameter falls back to the template's
98
+ value.
99
+
100
+ Because it never connects to the VM, `vm create` does **not** wait for SSH and does **not**
101
+ fail if the guest has no network — its job is done once the VM is cloned, configured and
102
+ started. The IP is reported best-effort: known immediately for a static `--ip`, or polled
103
+ from the guest agent (bounded by `--boot-timeout`) for `dhcp`; if none is found it warns and
104
+ still succeeds, omitting the IP from the summary.
105
+
106
+ cloud-init auth (`--ssh-public-key` / `--cipassword`) is **optional** here: a template with
107
+ a baked-in login is a valid result. Pass a key/password if you want one planted on the guest.
108
+
109
+ ```bash
110
+ knife proxmox vm create web-01 \
111
+ --proxmox-cluster production \
112
+ --template ubuntu-2404-template \
113
+ --cores 2 --memory 4096 \
114
+ --bridge vmbr0 --vlan 100 \
115
+ --ip 10.0.10.50/24 --gateway 10.0.10.1 \
116
+ --nameserver 10.0.0.1 --searchdomain example.com \
117
+ --ciuser ubuntu --ssh-public-key ~/.ssh/id_ed25519.pub
118
+ ```
119
+
120
+ ### `knife proxmox vm bootstrap NAME`
121
+
122
+ Everything `vm create` does, then **bootstraps the node with Chef/CINC**: clone → configure →
123
+ start → wait for SSH → `knife bootstrap`. Because it must connect, an SSH public key or a
124
+ cloud-init password is **required**. `NAME` and `--template` are otherwise the only required
125
+ inputs; every other parameter falls back to the template's value.
126
+
127
+ ```bash
128
+ knife proxmox vm bootstrap web-01 \
129
+ --proxmox-cluster production \
130
+ --template ubuntu-2404-template \
131
+ --cores 2 --memory 4096 \
132
+ --bridge vmbr0 --vlan 100 \
133
+ --ip 10.0.10.50/24 --gateway 10.0.10.1 \
134
+ --nameserver 10.0.0.1 --searchdomain example.com \
135
+ --ciuser ubuntu --ssh-public-key ~/.ssh/id_ed25519.pub \
136
+ --node web-01 --run-list 'role[base]'
137
+ ```
138
+
139
+ Both commands share the provisioning flags below.
140
+
141
+ | Flag | Meaning |
142
+ |------|---------|
143
+ | `--template NAME_OR_VMID` | Source template (name or numeric VMID). **Required.** |
144
+ | `--proxmox-cluster NAME` | Cluster key from `knife[:proxmox_clusters]`. |
145
+ | `--target-node NODE` | Node to clone onto (requires shared storage for a cross-node clone). |
146
+ | `--newid VMID` | Use a specific VMID instead of the cluster's next free id. |
147
+ | `--linked-clone` | Linked clone instead of the default full clone (`--storage` is then ignored). |
148
+ | `--storage NAME` / `--pool NAME` | Target storage / resource pool. |
149
+ | `--cores N` / `--sockets N` / `--memory MiB` | CPU / RAM. |
150
+ | `--bridge vmbrN` / `--vlan TAG` | net0 bridge and VLAN tag (needs a VLAN-aware bridge). |
151
+ | `--ip CIDR\|dhcp` / `--gateway IP` / `--prefix N` | Static IPv4 (`10.0.10.5/24`, or a bare IP with `--prefix`) or `dhcp`. |
152
+ | `--nameserver IP` / `--searchdomain DOMAIN` | cloud-init DNS. |
153
+ | `--ciuser USER` | cloud-init user. |
154
+ | `--ssh-public-key PATH` | Public key injected via cloud-init (preferred auth). |
155
+ | `--cipassword` | Prompt (no echo) for a cloud-init password; or set `KNIFE_PROXMOX_CIPASSWORD`. |
156
+ | `--clone-timeout SEC` / `--boot-timeout SEC` | Clone and boot/SSH wait limits (default 600 / 300). |
157
+
158
+ For `vm bootstrap`, the standard `knife bootstrap` options are inherited: `-N/--node-name`,
159
+ `-r/--run-list`, `-E/--environment`, `--connection-user`, `--ssh-identity-file`,
160
+ `--bootstrap-version`, `--ssh-verify-host-key` (defaults to `:accept_new` for the
161
+ freshly-created host), `--yes`, etc. They do not apply to `vm create`, which never bootstraps.
162
+
163
+ **Clone mode.** The clone is a **full clone by default**. Pass `--linked-clone` for a linked
164
+ clone (faster, thin) — it requires a storage that supports it, and any `--storage` (CLI or
165
+ cluster default) is then dropped, because Proxmox rejects a target storage on a linked clone.
166
+
167
+ **Bootstrap product (CINC by default).** A `vm bootstrap`'d VM is bootstrapped with **CINC** out of the
168
+ box — the install is pinned to the CINC omnitruck regardless of whether you invoke a CINC- or
169
+ Chef-branded `knife`, which also avoids Chef's commercial license gate. To bootstrap with Chef
170
+ Infra Client instead, opt in via `config.rb`:
171
+
172
+ ```ruby
173
+ knife[:proxmox_bootstrap_product] = "chef" # or "chef-ice"; standard Chef licensing then applies
174
+ ```
175
+
176
+ An explicit `--bootstrap-product`, `--bootstrap-url` or `--bootstrap-install-command` always wins.
177
+
178
+ **cloud-init wait.** Before installing the client, `vm bootstrap` waits for cloud-init to finish
179
+ (`cloud-init status --wait`) so the omnibus install never races cloud-init for the apt/dpkg lock
180
+ on the freshly booted VM. This makes `vm bootstrap` deterministic in a single run; override it
181
+ with `--bootstrap-preinstall-command` (it is skipped on images without cloud-init).
182
+
183
+ **SSH authentication.** Injecting a public key (`--ssh-public-key`) is the preferred path — for
184
+ `vm bootstrap` the matching private key is then used for the bootstrap connection. A cloud-init
185
+ password is supported as a fallback via `KNIFE_PROXMOX_CIPASSWORD` or the `--cipassword` no-echo
186
+ prompt (never as a literal command-line argument). A credential is **required** for `vm bootstrap`
187
+ (it must connect) and **optional** for `vm create`. For `--ip dhcp` the guest must run the QEMU
188
+ guest agent so the leased address can be discovered.
189
+
190
+ ### `knife proxmox cluster list`
191
+
192
+ Lists the configured clusters with host, token id, `verify_ssl`, and whether a token
193
+ secret resolves (the secret value is never printed). Marks the default cluster with `*`.
194
+ Makes no API call.
195
+
196
+ ### `knife proxmox template list [--proxmox-cluster NAME]`
197
+
198
+ Lists the qemu templates on the selected cluster (name, VMID, node, disk, memory).
199
+
200
+ ### `knife proxmox vm list [--proxmox-cluster NAME] [--node NODE]`
201
+
202
+ Lists the cluster's qemu VMs (name, VMID, node, status, memory, uptime). `--node` filters
203
+ client-side.
204
+
205
+ All read commands honour `--format json|yaml` for scripting.
206
+
207
+ ## Development
208
+
209
+ Tests run under **cinc-workstation**, so the suite exercises the same `knife` build the
210
+ gem ships on (a cinc-internal build that differs from the rubygems release — e.g.
211
+ `Bootstrap#fetch_license` was removed in it). No bundler is involved.
212
+
213
+ ```bash
214
+ cinc exec rake # chefstyle + rspec (the default task)
215
+ cinc exec rake spec # rspec only
216
+ cinc exec rake style # chefstyle only
217
+ ```
218
+
219
+ > Do **not** use `bundle exec` against cinc: `Gemfile.lock` pins the public knife, which the
220
+ > cinc gem repo does not carry, so `cinc exec bundle exec` fails. The `Gemfile`/`Gemfile.lock`
221
+ > exist only for `gem build`/release (`cinc exec rake build`).
222
+
223
+ ## License
224
+
225
+ Apache-2.0.
@@ -0,0 +1,172 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "knife-proxmox-ve/client"
4
+ require "knife-proxmox-ve/api"
5
+
6
+ class Chef
7
+ class Knife
8
+ # Shared behaviour mixed into every `knife proxmox ...` command (SEAM 2).
9
+ #
10
+ # Owns cluster resolution, the Client/Api factory and the secret-from-ENV override so
11
+ # individual commands stay declarative: they call #proxmox_api, #proxmox_client and
12
+ # #msg_pair and never touch the clusters hash or the environment directly.
13
+ #
14
+ # The token secret is resolved with ENV precedence and is never sent empty — a missing
15
+ # secret is a loud, fatal error, not a silent default.
16
+ module ProxmoxBase
17
+ # Global ENV override applied to whichever cluster is selected.
18
+ ENV_SECRET_GLOBAL = "KNIFE_PROXMOX_TOKEN_SECRET"
19
+
20
+ # Per-cluster ENV override prefix; suffix is the cluster key normalized via #env_key.
21
+ ENV_SECRET_PREFIX = "KNIFE_PROXMOX_TOKEN_SECRET_"
22
+
23
+ def self.included(includer)
24
+ includer.class_eval do
25
+ option :proxmox_cluster,
26
+ long: "--proxmox-cluster NAME",
27
+ description: "Proxmox cluster key from knife[:proxmox_clusters]"
28
+ end
29
+ end
30
+
31
+ # The selected cluster as a symbol-keyed Hash, with :token_secret resolved through the
32
+ # ENV override. Fatals (and lists available keys) when no cluster is selected or the
33
+ # selected key is unknown. Memoized.
34
+ def proxmox_cluster_config
35
+ @proxmox_cluster_config ||= resolve_cluster_config
36
+ end
37
+
38
+ # The transport client for the selected cluster. Warns on every run when TLS
39
+ # verification is disabled. Memoized.
40
+ def proxmox_client
41
+ @proxmox_client ||= build_proxmox_client
42
+ end
43
+
44
+ # The endpoint map over #proxmox_client. Memoized.
45
+ def proxmox_api
46
+ # Anchor at top level: inside Chef::Knife, a bare Knife resolves to Chef::Knife.
47
+ @proxmox_api ||= ::Knife::Proxmox::Api.new(proxmox_client)
48
+ end
49
+
50
+ # Print a colored "label: value" line, skipping blank values. Not a knife-core helper;
51
+ # commands depend on it being defined here.
52
+ def msg_pair(label, value, color = :cyan)
53
+ ui.info("#{ui.color(label, color)}: #{value}") if value && !value.to_s.empty?
54
+ end
55
+
56
+ # Whether a token secret resolves for the given cluster (symbol-keyed hash), using the
57
+ # same precedence as #resolve_token_secret but without fatal-ing. Lets `cluster list`
58
+ # report secret presence from a single source of truth.
59
+ def proxmox_token_secret_present?(cluster_key, cluster_hash)
60
+ !token_secret_from_sources(cluster_key, cluster_hash).nil?
61
+ end
62
+
63
+ private
64
+
65
+ def resolve_cluster_config
66
+ cluster_key = config[:proxmox_cluster] || Chef::Config[:knife][:proxmox_default_cluster]
67
+ if cluster_key.nil? || cluster_key.to_s.empty?
68
+ ui.fatal!(
69
+ "no Proxmox cluster selected: pass --proxmox-cluster NAME or set " \
70
+ "knife[:proxmox_default_cluster]. Available: #{available_cluster_keys}"
71
+ )
72
+ end
73
+
74
+ cluster = proxmox_clusters[cluster_key.to_s] || proxmox_clusters[cluster_key.to_sym]
75
+ unless cluster
76
+ ui.fatal!(
77
+ "unknown Proxmox cluster #{cluster_key.inspect}. " \
78
+ "Available: #{available_cluster_keys}"
79
+ )
80
+ end
81
+
82
+ cluster = cluster.transform_keys(&:to_sym)
83
+ cluster[:token_secret] = resolve_token_secret(cluster_key.to_s, cluster)
84
+ cluster
85
+ end
86
+
87
+ def build_proxmox_client
88
+ cluster = proxmox_cluster_config
89
+ verify_ssl = cluster.fetch(:verify_ssl, true) != false
90
+ ui.warn(tls_disabled_warning(cluster)) unless verify_ssl
91
+
92
+ # Anchor at top level: inside Chef::Knife, a bare Knife resolves to Chef::Knife.
93
+ ::Knife::Proxmox::Client.new(
94
+ host: cluster[:host],
95
+ token_id: cluster[:token_id],
96
+ token_secret: cluster[:token_secret],
97
+ port: cluster.fetch(:port, 8006),
98
+ verify_ssl:
99
+ )
100
+ end
101
+
102
+ def proxmox_clusters
103
+ # The nested clusters hash has no CLI equivalent; read it straight from Chef::Config.
104
+ Chef::Config[:knife][:proxmox_clusters] || {}
105
+ end
106
+
107
+ # Resolve the token secret or fatal, so an empty secret never reaches the wire.
108
+ def resolve_token_secret(cluster_key, cluster_hash)
109
+ guard_env_key_collisions
110
+
111
+ secret = token_secret_from_sources(cluster_key, cluster_hash)
112
+ unless secret
113
+ ui.fatal!(
114
+ "no Proxmox token secret for cluster #{cluster_key.inspect}: set #{ENV_SECRET_GLOBAL}, " \
115
+ "#{per_cluster_env_var(cluster_key)}, or knife[:proxmox_clusters][#{cluster_key.inspect}][:token_secret]"
116
+ )
117
+ end
118
+
119
+ secret
120
+ end
121
+
122
+ # Token-secret precedence: global ENV, then per-cluster ENV, then the config file.
123
+ # The single source of truth for the precedence order (reused by the presence check).
124
+ def token_secret_from_sources(cluster_key, cluster_hash)
125
+ present(ENV[ENV_SECRET_GLOBAL]) ||
126
+ present(ENV[per_cluster_env_var(cluster_key)]) ||
127
+ present(cluster_hash[:token_secret])
128
+ end
129
+
130
+ # The per-cluster ENV var name for a given cluster key.
131
+ def per_cluster_env_var(cluster_key)
132
+ "#{ENV_SECRET_PREFIX}#{env_key(cluster_key)}"
133
+ end
134
+
135
+ # Normalize a cluster key into an ENV-var-safe suffix: upcase, non-alphanumeric -> "_".
136
+ def env_key(cluster_key)
137
+ cluster_key.to_s.upcase.gsub(/[^A-Z0-9]/, "_")
138
+ end
139
+
140
+ # Fatal if two configured cluster keys collapse to the same per-cluster ENV var name,
141
+ # because an override meant for one would silently apply to the other.
142
+ def guard_env_key_collisions
143
+ groups = proxmox_clusters.keys.group_by { |key| env_key(key) }
144
+ collision = groups.find { |_normalized, keys| keys.length > 1 }
145
+ return unless collision
146
+
147
+ normalized, keys = collision
148
+ ui.fatal!(
149
+ "Proxmox cluster keys #{keys.map(&:to_s).sort.inspect} both map to ENV var " \
150
+ "#{ENV_SECRET_PREFIX}#{normalized}; rename one to disambiguate the per-cluster secret override"
151
+ )
152
+ end
153
+
154
+ def available_cluster_keys
155
+ keys = proxmox_clusters.keys.map(&:to_s).sort
156
+ keys.empty? ? "(none configured)" : keys.join(", ")
157
+ end
158
+
159
+ def present(value)
160
+ return nil if value.nil?
161
+
162
+ str = value.to_s
163
+ str.empty? ? nil : value
164
+ end
165
+
166
+ def tls_disabled_warning(cluster)
167
+ "TLS certificate verification is DISABLED for Proxmox host " \
168
+ "#{cluster[:host]} (verify_ssl: false) — connection is vulnerable to MITM"
169
+ end
170
+ end
171
+ end
172
+ end