kitchen-incus 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 +7 -0
- data/CHANGELOG.md +18 -0
- data/LICENSE +217 -0
- data/README.md +87 -0
- data/lib/kitchen/driver/incus.rb +252 -0
- data/lib/kitchen/incus/cli.rb +177 -0
- data/lib/kitchen/incus/inspec_support.rb +27 -0
- data/lib/kitchen/incus/version.rb +7 -0
- data/lib/kitchen/transport/incus.rb +138 -0
- data/lib/train-incus.rb +66 -0
- metadata +131 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c78973342a2b93eab14c8bec45e92867b1280e5aec1c766949c24d25d54101fe
|
|
4
|
+
data.tar.gz: 18578dc78622ae94a27d34b698ea11757eaeb407ff8051d95182ff21e8311fbf
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8e79f774bb991d984088c0733c3357f684070ad8943ded3970f58fb4398e6012830c5cf01ef8f5fdfbbfe42ae62a92ae700e63cffe3fbd3fc89ff54249ee3784
|
|
7
|
+
data.tar.gz: 0e354aea27540b346213fe108d496cdfd16a34200545b038ce594d3f99df81ada6ef7b4ec6fb0bb68cb221bf91400c50698a72918f394a2c9234065e8df6789c
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be 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
|
+
## [0.1.0] - 2026-10-27
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Initial release.
|
|
13
|
+
- Test Kitchen `incus` driver: create, start, wait, delete, and live status.
|
|
14
|
+
- Test Kitchen `incus` transport: `incus exec`, tar-stream uploads with `incus file push` fallback, `incus file pull` downloads, and `kitchen login`.
|
|
15
|
+
- Bundled `incus` InSpec train backend for the `inspec` verifier.
|
|
16
|
+
- Cinc-by-default provisioning via `kitchen-cinc`.
|
|
17
|
+
|
|
18
|
+
[0.1.0]: https://github.com/aae42/kitchen-incus/releases/tag/v0.1.0
|
data/LICENSE
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
Copyright 2026 Andrew Brossia
|
|
206
|
+
|
|
207
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
208
|
+
you may not use this file except in compliance with the License.
|
|
209
|
+
You may obtain a copy of the License at
|
|
210
|
+
|
|
211
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
212
|
+
|
|
213
|
+
Unless required by applicable law or agreed to in writing, software
|
|
214
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
215
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
216
|
+
See the License for the specific language governing permissions and
|
|
217
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# kitchen-incus
|
|
2
|
+
|
|
3
|
+
A [Test Kitchen](https://kitchen.ci) driver and transport for [Incus](https://linuxcontainers.org/incus/) containers and virtual machines.
|
|
4
|
+
|
|
5
|
+
Everything goes through the `incus` CLI, so any remote your client can reach works: the local unix socket, or a remote HTTPS server or cluster. Instances don't need SSH or a network route from your workstation.
|
|
6
|
+
|
|
7
|
+
- **Driver** (`incus`): creates, starts, waits for, and deletes instances. It also reports live status for `kitchen list --live`.
|
|
8
|
+
- **Transport** (`incus`): runs commands with `incus exec`, uploads files as a tar stream (falling back to `incus file push`), downloads with `incus file pull`, and opens `kitchen login` with `incus exec … -- su -l`.
|
|
9
|
+
- **InSpec**: when the transport is `incus`, the `inspec` verifier runs through a bundled `incus` train backend.
|
|
10
|
+
- **Cinc by default**: this gem depends on `kitchen-cinc`. Use `cinc_infra`, `cinc_zero`, `cinc_solo`, or `cinc_apply`. The `chef_*` provisioner names also install Cinc unless `kitchen-chef-enterprise` is installed.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```yaml
|
|
15
|
+
driver:
|
|
16
|
+
name: incus
|
|
17
|
+
|
|
18
|
+
transport:
|
|
19
|
+
name: incus
|
|
20
|
+
|
|
21
|
+
provisioner:
|
|
22
|
+
name: cinc_infra
|
|
23
|
+
|
|
24
|
+
verifier:
|
|
25
|
+
name: inspec
|
|
26
|
+
|
|
27
|
+
platforms:
|
|
28
|
+
- name: debian-12 # image defaults to images:debian/12
|
|
29
|
+
- name: rockylinux-9
|
|
30
|
+
- name: ubuntu-24.04-vm
|
|
31
|
+
driver:
|
|
32
|
+
image: images:ubuntu/24.04
|
|
33
|
+
vm: true
|
|
34
|
+
config:
|
|
35
|
+
limits.cpu: 2
|
|
36
|
+
limits.memory: 2GiB
|
|
37
|
+
|
|
38
|
+
suites:
|
|
39
|
+
- name: default
|
|
40
|
+
run_list:
|
|
41
|
+
- recipe[my_cookbook::default]
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`kitchen create`, `converge`, `verify`, `test`, `login`, `exec`, `destroy`, and `list --live --probe` all work.
|
|
45
|
+
|
|
46
|
+
## Driver options
|
|
47
|
+
|
|
48
|
+
| Option | Default | Description |
|
|
49
|
+
| --- | --- | --- |
|
|
50
|
+
| `remote` | client default | Incus remote. The resolved name is saved in state, so `destroy` still targets the right server if you later switch remotes. |
|
|
51
|
+
| `project` | remote's current project | Incus project |
|
|
52
|
+
| `image` | `images:<distro>/<version>` | Derived from the platform name (`ubuntu-24.04` → `images:ubuntu/24.04`) |
|
|
53
|
+
| `vm` | `false` | Launch a VM instead of a container |
|
|
54
|
+
| `profiles` | `[]` | Profiles to apply. Empty means the server default. Setting this replaces `default`. |
|
|
55
|
+
| `config` | `{}` | Instance config keys, e.g. `security.nesting: true` |
|
|
56
|
+
| `devices` | `{}` | `{ name => { type: disk, source: /src, path: /dst } }` |
|
|
57
|
+
| `storage_pool` | `nil` | `--storage` |
|
|
58
|
+
| `network` | `nil` | `--network` |
|
|
59
|
+
| `instance_type` | `nil` | `--type`, e.g. `c2-m4` |
|
|
60
|
+
| `instance_name` | `kitchen-<instance>-<random>` | Explicit instance name |
|
|
61
|
+
| `ready_timeout` | `300` | Seconds to wait for the instance to accept commands |
|
|
62
|
+
| `wait_for_network` | `true` | Also wait for a global IP address |
|
|
63
|
+
| `install_prerequisites` | `true` | Install `curl`, `sudo`, `tar`, and `ca-certificates` if they're missing (apt, dnf, yum, zypper, apk, pacman) |
|
|
64
|
+
| `provision_commands` | `[]` | Shell commands to run after boot |
|
|
65
|
+
| `incus_binary` | `incus` | Path to the client |
|
|
66
|
+
|
|
67
|
+
## Transport options
|
|
68
|
+
|
|
69
|
+
| Option | Default | Description |
|
|
70
|
+
| --- | --- | --- |
|
|
71
|
+
| `user` | root | uid that commands run as |
|
|
72
|
+
| `env` | `{}` | Extra environment variables for every command |
|
|
73
|
+
| `login_command` | `["su", "-l"]` | Command that `kitchen login` runs |
|
|
74
|
+
| `ready_timeout` | `300` | Seconds `wait_until_ready` waits |
|
|
75
|
+
|
|
76
|
+
## Standalone InSpec / cinc-auditor
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
cinc-auditor exec profile -t incus://my-instance --remote myremote
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Development
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
bundle exec rspec
|
|
86
|
+
cd test/kitchen && RUBYLIB=../../lib kitchen test # live run on your current Incus remote
|
|
87
|
+
```
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require "time"
|
|
5
|
+
require "kitchen"
|
|
6
|
+
require "kitchen/driver/base"
|
|
7
|
+
require_relative "../incus/cli"
|
|
8
|
+
require_relative "../incus/version"
|
|
9
|
+
|
|
10
|
+
module Kitchen
|
|
11
|
+
module Driver
|
|
12
|
+
# Test Kitchen driver for Incus containers and virtual machines.
|
|
13
|
+
#
|
|
14
|
+
# Instances are managed with the `incus` CLI, so any remote configured in
|
|
15
|
+
# the client (local unix socket or a remote HTTPS server/cluster) works.
|
|
16
|
+
class Incus < Kitchen::Driver::Base
|
|
17
|
+
kitchen_driver_api_version 2
|
|
18
|
+
|
|
19
|
+
plugin_version Kitchen::Incus::VERSION
|
|
20
|
+
|
|
21
|
+
# Incus remote to use; nil uses the client's default remote.
|
|
22
|
+
default_config :remote, nil
|
|
23
|
+
# Incus project; nil uses the remote's current project.
|
|
24
|
+
default_config :project, nil
|
|
25
|
+
# Image to launch. Defaults to images:<distro>/<version> derived from
|
|
26
|
+
# the platform name (e.g. ubuntu-24.04 -> images:ubuntu/24.04).
|
|
27
|
+
default_config :image do |driver|
|
|
28
|
+
driver.default_image
|
|
29
|
+
end
|
|
30
|
+
# Launch a virtual machine instead of a container.
|
|
31
|
+
default_config :vm, false
|
|
32
|
+
# Profiles to apply (empty = server default profile).
|
|
33
|
+
default_config :profiles, []
|
|
34
|
+
# Instance config keys (e.g. limits.cpu: 2, security.nesting: true).
|
|
35
|
+
default_config :config, {}
|
|
36
|
+
# Extra devices: { name => { type: disk, source: /x, path: /y } }.
|
|
37
|
+
default_config :devices, {}
|
|
38
|
+
default_config :storage_pool, nil
|
|
39
|
+
default_config :network, nil
|
|
40
|
+
# Instance type shorthand (e.g. c2-m4) passed to `incus init --type`.
|
|
41
|
+
default_config :instance_type, nil
|
|
42
|
+
# Explicit instance name; default is kitchen-<instance>-<random>.
|
|
43
|
+
default_config :instance_name, nil
|
|
44
|
+
# Seconds to wait for the instance to accept commands (and get an IP).
|
|
45
|
+
default_config :ready_timeout, 300
|
|
46
|
+
# Wait for a global IP address before continuing (needed to download Cinc).
|
|
47
|
+
default_config :wait_for_network, true
|
|
48
|
+
# Shell commands run inside the instance after it boots.
|
|
49
|
+
default_config :provision_commands, []
|
|
50
|
+
# Install curl/ca-certificates/sudo when missing, so Cinc can be
|
|
51
|
+
# downloaded and the provisioner's default `sudo -E` works.
|
|
52
|
+
default_config :install_prerequisites, true
|
|
53
|
+
default_config :incus_binary, "incus"
|
|
54
|
+
|
|
55
|
+
# Shell script that installs the packages the Cinc installer and Test
|
|
56
|
+
# Kitchen expect, using whichever package manager is present.
|
|
57
|
+
PREREQS_SCRIPT = <<~'SH'
|
|
58
|
+
missing=""
|
|
59
|
+
command -v curl >/dev/null 2>&1 || command -v wget >/dev/null 2>&1 || missing="$missing curl"
|
|
60
|
+
command -v sudo >/dev/null 2>&1 || missing="$missing sudo"
|
|
61
|
+
command -v tar >/dev/null 2>&1 || missing="$missing tar"
|
|
62
|
+
[ -e /etc/ssl/certs/ca-certificates.crt ] || [ -e /etc/pki/tls/certs/ca-bundle.crt ] || missing="$missing ca-certificates"
|
|
63
|
+
[ -z "$missing" ] && exit 0
|
|
64
|
+
echo "Installing missing prerequisites:$missing"
|
|
65
|
+
if command -v apt-get >/dev/null 2>&1; then
|
|
66
|
+
export DEBIAN_FRONTEND=noninteractive
|
|
67
|
+
apt-get update -q && apt-get install -y -q $missing
|
|
68
|
+
elif command -v dnf >/dev/null 2>&1; then dnf install -y $missing
|
|
69
|
+
elif command -v yum >/dev/null 2>&1; then yum install -y $missing
|
|
70
|
+
elif command -v zypper >/dev/null 2>&1; then zypper --non-interactive install $missing
|
|
71
|
+
elif command -v apk >/dev/null 2>&1; then apk add --no-cache $missing
|
|
72
|
+
elif command -v pacman >/dev/null 2>&1; then pacman -Sy --noconfirm $missing
|
|
73
|
+
else echo "No supported package manager found; please install:$missing" >&2; exit 1
|
|
74
|
+
fi
|
|
75
|
+
SH
|
|
76
|
+
|
|
77
|
+
def finalize_config!(instance)
|
|
78
|
+
super
|
|
79
|
+
warn_if_chef_provisioner(instance)
|
|
80
|
+
self
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# (see Base#create)
|
|
84
|
+
def create(state)
|
|
85
|
+
super
|
|
86
|
+
|
|
87
|
+
if state[:incus_instance] && cli(state).state(state[:incus_instance])
|
|
88
|
+
info("Incus instance #{cli(state).target(state[:incus_instance])} already exists")
|
|
89
|
+
cli(state).run("start", *cli(state).project_args, cli(state).target(state[:incus_instance]), allow_failure: true)
|
|
90
|
+
else
|
|
91
|
+
state[:incus_instance] = config[:instance_name] || generate_name
|
|
92
|
+
# Record the resolved remote/project so later actions hit the same
|
|
93
|
+
# server even if the client's default remote changes.
|
|
94
|
+
state[:incus_remote] = config[:remote] || resolve_default_remote
|
|
95
|
+
state[:incus_project] = config[:project]
|
|
96
|
+
launch(state)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
wait_until_ready(state)
|
|
100
|
+
state[:hostname] = ip_address(cli(state).state(state[:incus_instance]))
|
|
101
|
+
bootstrap(state)
|
|
102
|
+
info("Incus instance #{cli(state).target(state[:incus_instance])} ready#{state[:hostname] ? " (#{state[:hostname]})" : ""}")
|
|
103
|
+
rescue Kitchen::Incus::CommandFailed => e
|
|
104
|
+
raise ActionFailed, e.message
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# (see Base#destroy)
|
|
108
|
+
def destroy(state)
|
|
109
|
+
name = state[:incus_instance]
|
|
110
|
+
return if name.nil?
|
|
111
|
+
|
|
112
|
+
c = cli(state)
|
|
113
|
+
info("Destroying Incus instance #{c.target(name)}")
|
|
114
|
+
result = c.run("delete", *c.project_args, "--force", c.target(name), allow_failure: true)
|
|
115
|
+
if !result.success? && result.stderr !~ /not found/i
|
|
116
|
+
raise ActionFailed, "Failed to delete #{c.target(name)}: #{result.stderr.strip}"
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
%i{incus_instance incus_remote incus_project hostname}.each { |k| state.delete(k) }
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# (see Base#status)
|
|
123
|
+
def status(state)
|
|
124
|
+
name = state[:incus_instance]
|
|
125
|
+
now = Time.now.utc.iso8601
|
|
126
|
+
return { live: false, state: "not created", source: "incus", checked_at: now } if name.nil?
|
|
127
|
+
|
|
128
|
+
doc = cli(state).state(name)
|
|
129
|
+
if doc.nil?
|
|
130
|
+
return { live: false, state: "missing", source: "incus", resource_id: name,
|
|
131
|
+
message: "Instance not found on Incus server", checked_at: now }
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
status = doc["status"].to_s
|
|
135
|
+
{ live: status == "Running", state: status.downcase, source: "incus",
|
|
136
|
+
resource_id: cli(state).target(name), checked_at: now }
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# (see Base#doctor)
|
|
140
|
+
def doctor(_state)
|
|
141
|
+
result = cli({}).run("version", allow_failure: true, timeout: 30)
|
|
142
|
+
if result.stdout =~ /Server version: unreachable/ || !result.success?
|
|
143
|
+
error("Unable to reach Incus server: #{(result.stdout + result.stderr).strip}")
|
|
144
|
+
return true
|
|
145
|
+
end
|
|
146
|
+
false
|
|
147
|
+
rescue Kitchen::Incus::CommandFailed => e
|
|
148
|
+
error(e.message)
|
|
149
|
+
true
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Derive an images: remote alias from the platform name.
|
|
153
|
+
#
|
|
154
|
+
# @return [String]
|
|
155
|
+
def default_image
|
|
156
|
+
name = instance.platform.name
|
|
157
|
+
if (m = name.match(/\A(.+?)-(\d[\w.]*)\z/))
|
|
158
|
+
"images:#{m[1]}/#{m[2]}"
|
|
159
|
+
else
|
|
160
|
+
"images:#{name}"
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
private
|
|
165
|
+
|
|
166
|
+
def cli(state)
|
|
167
|
+
Kitchen::Incus::CLI.new(
|
|
168
|
+
binary: config[:incus_binary],
|
|
169
|
+
remote: state.fetch(:incus_remote, config[:remote]),
|
|
170
|
+
project: state.fetch(:incus_project, config[:project]),
|
|
171
|
+
logger:
|
|
172
|
+
)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def resolve_default_remote
|
|
176
|
+
remote = cli({}).run("remote", "get-default", timeout: 30).stdout.strip
|
|
177
|
+
remote.empty? ? nil : remote
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def launch(state)
|
|
181
|
+
c = cli(state)
|
|
182
|
+
target = c.target(state[:incus_instance])
|
|
183
|
+
info("Creating Incus #{config[:vm] ? "VM" : "container"} #{target} from #{config[:image]}")
|
|
184
|
+
|
|
185
|
+
args = ["init", *c.project_args, config[:image], target, "--quiet"]
|
|
186
|
+
args << "--vm" if config[:vm]
|
|
187
|
+
Array(config[:profiles]).each { |p| args.push("--profile", p) }
|
|
188
|
+
config[:config].each { |k, v| args.push("--config", "#{k}=#{v}") }
|
|
189
|
+
args.push("--storage", config[:storage_pool]) if config[:storage_pool]
|
|
190
|
+
args.push("--network", config[:network]) if config[:network]
|
|
191
|
+
args.push("--type", config[:instance_type]) if config[:instance_type]
|
|
192
|
+
c.run(*args)
|
|
193
|
+
|
|
194
|
+
config[:devices].each do |dev, props|
|
|
195
|
+
props = props.transform_keys(&:to_s)
|
|
196
|
+
type = props.delete("type") || raise(ActionFailed, "device #{dev} is missing a type")
|
|
197
|
+
c.run("config", "device", "add", *c.project_args, target, dev.to_s, type,
|
|
198
|
+
*props.map { |k, v| "#{k}=#{v}" })
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
c.run("start", *c.project_args, target)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def wait_until_ready(state)
|
|
205
|
+
c = cli(state)
|
|
206
|
+
name = state[:incus_instance]
|
|
207
|
+
deadline = Time.now + config[:ready_timeout].to_i
|
|
208
|
+
info("Waiting for #{c.target(name)} to accept commands#{config[:wait_for_network] ? " and get an IP" : ""}")
|
|
209
|
+
|
|
210
|
+
loop do
|
|
211
|
+
ready = c.exec(name, "true", allow_failure: true, timeout: 30).success?
|
|
212
|
+
ready &&= !ip_address(c.state(name)).nil? if config[:wait_for_network]
|
|
213
|
+
return if ready
|
|
214
|
+
raise ActionFailed, "Timed out after #{config[:ready_timeout]}s waiting for #{c.target(name)}" if Time.now > deadline
|
|
215
|
+
|
|
216
|
+
sleep 2
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# First global address on a non-loopback interface, IPv4 preferred.
|
|
221
|
+
def ip_address(doc)
|
|
222
|
+
addrs = (doc&.dig("network") || {}).reject { |_, n| n["type"] == "loopback" }
|
|
223
|
+
.values.flat_map { |n| n["addresses"] || [] }
|
|
224
|
+
.select { |a| a["scope"] == "global" }
|
|
225
|
+
(addrs.find { |a| a["family"] == "inet" } || addrs.first)&.dig("address")
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def bootstrap(state)
|
|
229
|
+
c = cli(state)
|
|
230
|
+
name = state[:incus_instance]
|
|
231
|
+
commands = []
|
|
232
|
+
commands << PREREQS_SCRIPT if config[:install_prerequisites]
|
|
233
|
+
commands.concat(Array(config[:provision_commands]))
|
|
234
|
+
commands.each { |cmd| c.exec(name, cmd, live: true) }
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def generate_name
|
|
238
|
+
base = instance.name.downcase.gsub(/[^a-z0-9-]/, "-").squeeze("-")
|
|
239
|
+
# Incus names: <= 63 chars, letters/digits/hyphens, must not start with a digit.
|
|
240
|
+
"kitchen-#{base}"[0, 55].sub(/-+\z/, "") + "-#{SecureRandom.hex(3)}"
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def warn_if_chef_provisioner(instance)
|
|
244
|
+
prov = instance.provisioner
|
|
245
|
+
return unless prov.class.name.to_s =~ /\AKitchen::Provisioner::Chef/
|
|
246
|
+
return if defined?(Kitchen::Provisioner::CincBase) && prov.is_a?(Kitchen::Provisioner::CincBase)
|
|
247
|
+
|
|
248
|
+
warn("Provisioner #{prov.name} will install Chef Infra Client; use `cinc_infra` (kitchen-cinc) to install Cinc instead")
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
end
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "open3"
|
|
5
|
+
require "mixlib/shellout"
|
|
6
|
+
|
|
7
|
+
module Kitchen
|
|
8
|
+
module Incus
|
|
9
|
+
# Raised when an `incus` CLI invocation fails.
|
|
10
|
+
class CommandFailed < StandardError
|
|
11
|
+
attr_reader :exit_code, :stderr
|
|
12
|
+
|
|
13
|
+
def initialize(message, exit_code = nil, stderr = nil)
|
|
14
|
+
@exit_code = exit_code
|
|
15
|
+
@stderr = stderr
|
|
16
|
+
super(message)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Thin wrapper around the `incus` command line client.
|
|
21
|
+
#
|
|
22
|
+
# Every command is executed with an argument vector (never through a
|
|
23
|
+
# shell), so instance names, paths and commands are not subject to shell
|
|
24
|
+
# interpolation on the workstation.
|
|
25
|
+
class CLI
|
|
26
|
+
Result = Struct.new(:stdout, :stderr, :exit_code) do
|
|
27
|
+
def success?
|
|
28
|
+
exit_code == 0
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Default timeout for long running commands (converges, image pulls).
|
|
33
|
+
DEFAULT_TIMEOUT = 60 * 60 * 3
|
|
34
|
+
|
|
35
|
+
attr_reader :binary, :remote, :project
|
|
36
|
+
|
|
37
|
+
# @param binary [String] path to the incus client
|
|
38
|
+
# @param remote [String, nil] incus remote name; nil uses the default remote
|
|
39
|
+
# @param project [String, nil] incus project; nil uses the remote's current project
|
|
40
|
+
# @param logger [Kitchen::Logger, nil] logger used for debug + live output
|
|
41
|
+
def initialize(binary: "incus", remote: nil, project: nil, logger: nil)
|
|
42
|
+
@binary = binary || "incus"
|
|
43
|
+
@remote = blank?(remote) ? nil : remote.to_s
|
|
44
|
+
@project = blank?(project) ? nil : project.to_s
|
|
45
|
+
@logger = logger
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# @param name [String] instance name
|
|
49
|
+
# @return [String] `remote:name` or `name`
|
|
50
|
+
def target(name)
|
|
51
|
+
remote ? "#{remote}:#{name}" : name.to_s
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# `--project` flag (must follow the sub-command).
|
|
55
|
+
def project_args
|
|
56
|
+
project ? ["--project", project] : []
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Run an incus sub-command and capture its output.
|
|
60
|
+
#
|
|
61
|
+
# @param args [Array<String>] sub-command and arguments
|
|
62
|
+
# @param input [String, nil] data written to stdin
|
|
63
|
+
# @param live [Boolean] stream output to the logger while running
|
|
64
|
+
# @param allow_failure [Boolean] return the result instead of raising
|
|
65
|
+
# @return [Result]
|
|
66
|
+
def run(*args, input: nil, live: false, allow_failure: false, timeout: DEFAULT_TIMEOUT)
|
|
67
|
+
argv = [binary, *args.flatten.map(&:to_s)]
|
|
68
|
+
debug("[incus] #{argv.join(" ")}")
|
|
69
|
+
opts = { timeout:, environment: { "LC_ALL" => nil } }
|
|
70
|
+
opts[:input] = input if input
|
|
71
|
+
opts[:live_stream] = @logger if live && @logger
|
|
72
|
+
sh = Mixlib::ShellOut.new(*argv, **opts)
|
|
73
|
+
sh.run_command
|
|
74
|
+
result = Result.new(sh.stdout, sh.stderr, sh.exitstatus)
|
|
75
|
+
if !result.success? && !allow_failure
|
|
76
|
+
raise CommandFailed.new(
|
|
77
|
+
"`#{argv.join(" ")}` exited #{result.exit_code}: #{result.stderr.strip}",
|
|
78
|
+
result.exit_code, result.stderr
|
|
79
|
+
)
|
|
80
|
+
end
|
|
81
|
+
result
|
|
82
|
+
rescue Errno::ENOENT
|
|
83
|
+
raise CommandFailed, "incus client `#{binary}` not found on PATH"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Execute a shell command inside an instance.
|
|
87
|
+
#
|
|
88
|
+
# @param name [String] instance name
|
|
89
|
+
# @param command [String] POSIX shell command
|
|
90
|
+
# @param user [Integer, nil] uid to run as
|
|
91
|
+
# @param env [Hash] environment variables
|
|
92
|
+
# @return [Result]
|
|
93
|
+
def exec(name, command, user: nil, env: {}, **opts)
|
|
94
|
+
run(*exec_args(name, user:, env:), "--", "sh", "-c", command, **opts)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Arguments for `incus exec` up to (not including) the `--` separator.
|
|
98
|
+
def exec_args(name, user: nil, env: {}, interactive: false)
|
|
99
|
+
args = ["exec", *project_args]
|
|
100
|
+
args << (interactive ? "-t" : "-T")
|
|
101
|
+
args += ["--user", user.to_s] if user
|
|
102
|
+
env.each { |k, v| args += ["--env", "#{k}=#{v}"] }
|
|
103
|
+
args << target(name)
|
|
104
|
+
args
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Copy a local file or directory into the instance under remote_dir.
|
|
108
|
+
#
|
|
109
|
+
# Uses a tar stream over `incus exec` so a whole directory tree is moved
|
|
110
|
+
# in a single API round trip (much faster than `incus file push -r`
|
|
111
|
+
# against remote servers). Falls back to `incus file push` when the
|
|
112
|
+
# instance has no tar.
|
|
113
|
+
def push(name, local, remote_dir, user: nil)
|
|
114
|
+
local = File.expand_path(local)
|
|
115
|
+
unless guest_has_tar?(name)
|
|
116
|
+
return run("file", "push", *project_args, "-r", "-p", local, "#{target(name)}#{remote_dir.chomp("/")}/")
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
tar = ["tar", "-C", File.dirname(local), "-cf", "-", File.basename(local)]
|
|
120
|
+
incus = [binary, *exec_args(name, user:), "--", "tar", "-C", remote_dir, "-xf", "-"]
|
|
121
|
+
debug("[incus] #{tar.join(" ")} | #{incus.join(" ")}")
|
|
122
|
+
|
|
123
|
+
statuses = nil
|
|
124
|
+
err_r, err_w = IO.pipe
|
|
125
|
+
# COPYFILE_DISABLE stops macOS bsdtar from adding ._ resource files.
|
|
126
|
+
Open3.pipeline_w([{ "COPYFILE_DISABLE" => "1" }, *tar], incus, err: err_w) do |stdin, threads|
|
|
127
|
+
stdin.close
|
|
128
|
+
statuses = threads.map(&:value)
|
|
129
|
+
end
|
|
130
|
+
err_w.close
|
|
131
|
+
stderr = err_r.read
|
|
132
|
+
err_r.close
|
|
133
|
+
return if statuses.all?(&:success?)
|
|
134
|
+
|
|
135
|
+
raise CommandFailed.new("upload of #{local} to #{target(name)}:#{remote_dir} failed: #{stderr.strip}",
|
|
136
|
+
statuses.map(&:exitstatus).find { |c| c != 0 }, stderr)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Copy a file or directory out of the instance.
|
|
140
|
+
def pull(name, remote_path, local)
|
|
141
|
+
run("file", "pull", *project_args, "-r", "#{target(name)}#{remote_path}", local)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# @return [Hash, nil] the instance state document, or nil if absent
|
|
145
|
+
def state(name)
|
|
146
|
+
result = run("query", api_path("/1.0/instances/#{name}/state"), allow_failure: true, timeout: 60)
|
|
147
|
+
return JSON.parse(result.stdout) if result.success?
|
|
148
|
+
return nil if result.stderr =~ /not found/i
|
|
149
|
+
|
|
150
|
+
raise CommandFailed.new("unable to query #{target(name)}: #{result.stderr.strip}", result.exit_code, result.stderr)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
private
|
|
154
|
+
|
|
155
|
+
def guest_has_tar?(name)
|
|
156
|
+
@guest_has_tar ||= {}
|
|
157
|
+
return @guest_has_tar[name] if @guest_has_tar.key?(name)
|
|
158
|
+
|
|
159
|
+
@guest_has_tar[name] = exec(name, "command -v tar", allow_failure: true, timeout: 60).success?
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# `incus query` refuses `--project`, so the project goes into the URL.
|
|
163
|
+
def api_path(path)
|
|
164
|
+
path = "#{path}?project=#{project}" if project
|
|
165
|
+
remote ? "#{remote}:#{path}" : path
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def blank?(value)
|
|
169
|
+
value.nil? || value.to_s.strip.empty?
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def debug(msg)
|
|
173
|
+
@logger&.debug(msg)
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "train-incus"
|
|
4
|
+
|
|
5
|
+
module Kitchen
|
|
6
|
+
module Incus
|
|
7
|
+
# Mixed into a Kitchen::Verifier::Inspec instance by the incus transport
|
|
8
|
+
# so InSpec runs through the `incus` train backend (see train-incus.rb).
|
|
9
|
+
# kitchen-inspec dispatches on `runner_options_for_<transport name>`.
|
|
10
|
+
module InspecSupport
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def runner_options_for_incus(config_data)
|
|
14
|
+
opts = instance.transport.connection_options(config_data)
|
|
15
|
+
{
|
|
16
|
+
"backend" => "incus",
|
|
17
|
+
"logger" => logger,
|
|
18
|
+
"host" => opts[:instance_name],
|
|
19
|
+
"remote" => opts[:remote],
|
|
20
|
+
"project" => opts[:project],
|
|
21
|
+
"incus_binary" => opts[:incus_binary],
|
|
22
|
+
"incus_user" => opts[:user],
|
|
23
|
+
}.compact
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "kitchen"
|
|
4
|
+
require "kitchen/transport/base"
|
|
5
|
+
require_relative "../incus/cli"
|
|
6
|
+
require_relative "../incus/version"
|
|
7
|
+
|
|
8
|
+
module Kitchen
|
|
9
|
+
module Transport
|
|
10
|
+
# Test Kitchen transport that talks to Incus instances through the
|
|
11
|
+
# `incus` CLI (`incus exec`), so no SSH or network reachability to the
|
|
12
|
+
# instance is required.
|
|
13
|
+
class Incus < Kitchen::Transport::Base
|
|
14
|
+
kitchen_transport_api_version 1
|
|
15
|
+
|
|
16
|
+
plugin_version Kitchen::Incus::VERSION
|
|
17
|
+
|
|
18
|
+
default_config :incus_binary, "incus"
|
|
19
|
+
# uid to run commands as inside the instance (nil = root).
|
|
20
|
+
default_config :user, nil
|
|
21
|
+
# Extra environment variables for every command.
|
|
22
|
+
default_config :env, {}
|
|
23
|
+
# Command used for `kitchen login`.
|
|
24
|
+
default_config :login_command, ["su", "-l"]
|
|
25
|
+
# Seconds to wait for the instance agent to accept commands.
|
|
26
|
+
default_config :ready_timeout, 300
|
|
27
|
+
|
|
28
|
+
def finalize_config!(instance)
|
|
29
|
+
super
|
|
30
|
+
# Teach kitchen-inspec how to reach Incus instances, if it is in use.
|
|
31
|
+
verifier = instance.verifier
|
|
32
|
+
if defined?(Kitchen::Verifier::Inspec) && verifier.is_a?(Kitchen::Verifier::Inspec)
|
|
33
|
+
require_relative "../incus/inspec_support"
|
|
34
|
+
verifier.extend(Kitchen::Incus::InspecSupport)
|
|
35
|
+
end
|
|
36
|
+
self
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def connection(state, &block)
|
|
40
|
+
options = connection_options(config.to_hash.merge(state))
|
|
41
|
+
Kitchen::Transport::Incus::Connection.new(options, &block)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Builds the options shared by the transport connection and the InSpec
|
|
45
|
+
# integration.
|
|
46
|
+
#
|
|
47
|
+
# @api private
|
|
48
|
+
def connection_options(data)
|
|
49
|
+
# These keys are written to the instance state by the incus driver.
|
|
50
|
+
name = data[:incus_instance]
|
|
51
|
+
raise ActionFailed, "No Incus instance recorded for #{instance.to_str}; run `kitchen create` first" if name.nil?
|
|
52
|
+
|
|
53
|
+
{
|
|
54
|
+
instance_name: name,
|
|
55
|
+
remote: data[:incus_remote],
|
|
56
|
+
project: data[:incus_project],
|
|
57
|
+
incus_binary: data[:incus_binary],
|
|
58
|
+
user: data[:user],
|
|
59
|
+
env: data[:env] || {},
|
|
60
|
+
login_command: Array(data[:login_command]),
|
|
61
|
+
ready_timeout: data[:ready_timeout],
|
|
62
|
+
logger:,
|
|
63
|
+
}
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Connection that executes commands with `incus exec` and moves files
|
|
67
|
+
# with tar streams.
|
|
68
|
+
class Connection < Kitchen::Transport::Base::Connection
|
|
69
|
+
def init_options(options)
|
|
70
|
+
super
|
|
71
|
+
@name = @options[:instance_name]
|
|
72
|
+
@cli = Kitchen::Incus::CLI.new(
|
|
73
|
+
binary: @options[:incus_binary],
|
|
74
|
+
remote: @options[:remote],
|
|
75
|
+
project: @options[:project],
|
|
76
|
+
logger:
|
|
77
|
+
)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# (see Base::Connection#execute)
|
|
81
|
+
def execute(command)
|
|
82
|
+
return if command.nil? || command.to_s.strip.empty?
|
|
83
|
+
|
|
84
|
+
debug("[Incus] #{@cli.target(@name)} executing: #{command}")
|
|
85
|
+
@cli.exec(@name, command, user: @options[:user], env: @options[:env], live: true)
|
|
86
|
+
rescue Kitchen::Incus::CommandFailed => e
|
|
87
|
+
raise TransportFailed.new(e.message, e.exit_code)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# (see Base::Connection#upload)
|
|
91
|
+
def upload(locals, remote)
|
|
92
|
+
@cli.exec(@name, "mkdir -p #{shell_quote(remote)}", user: @options[:user])
|
|
93
|
+
Array(locals).each do |local|
|
|
94
|
+
debug("[Incus] uploading #{local} to #{@cli.target(@name)}:#{remote}")
|
|
95
|
+
@cli.push(@name, local, remote, user: @options[:user])
|
|
96
|
+
end
|
|
97
|
+
rescue Kitchen::Incus::CommandFailed => e
|
|
98
|
+
raise TransportFailed.new(e.message, e.exit_code)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# (see Base::Connection#download)
|
|
102
|
+
def download(remotes, local)
|
|
103
|
+
FileUtils.mkdir_p(local)
|
|
104
|
+
Array(remotes).each do |remote|
|
|
105
|
+
@cli.pull(@name, remote, local)
|
|
106
|
+
end
|
|
107
|
+
rescue Kitchen::Incus::CommandFailed => e
|
|
108
|
+
raise TransportFailed.new(e.message, e.exit_code)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# (see Base::Connection#login_command)
|
|
112
|
+
def login_command
|
|
113
|
+
args = @cli.exec_args(@name, user: @options[:user], env: @options[:env], interactive: true)
|
|
114
|
+
LoginCommand.new(@cli.binary, [*args, "--", *@options[:login_command]])
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Waits until the Incus agent (VMs) or init (containers) accepts
|
|
118
|
+
# commands.
|
|
119
|
+
def wait_until_ready
|
|
120
|
+
deadline = Time.now + (@options[:ready_timeout] || 300).to_i
|
|
121
|
+
loop do
|
|
122
|
+
result = @cli.exec(@name, "true", allow_failure: true, timeout: 30)
|
|
123
|
+
return if result.success?
|
|
124
|
+
raise TransportFailed, "#{@cli.target(@name)} not ready: #{result.stderr.strip}" if Time.now > deadline
|
|
125
|
+
|
|
126
|
+
sleep 2
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
private
|
|
131
|
+
|
|
132
|
+
def shell_quote(str)
|
|
133
|
+
"'#{str.to_s.gsub("'", "'\\\\''")}'"
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
data/lib/train-incus.rb
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Train transport plugin for Incus instances, used by InSpec / cinc-auditor.
|
|
4
|
+
#
|
|
5
|
+
# Commands run via `incus exec`, so the instance does not need SSH or a
|
|
6
|
+
# network path from the workstation. Can also be used standalone:
|
|
7
|
+
#
|
|
8
|
+
# inspec exec profile -t incus://my-instance --remote myremote --project p
|
|
9
|
+
#
|
|
10
|
+
require "train"
|
|
11
|
+
require "train/plugins"
|
|
12
|
+
require_relative "kitchen/incus/cli"
|
|
13
|
+
|
|
14
|
+
module Train
|
|
15
|
+
module Transports
|
|
16
|
+
class Incus < Train.plugin(1)
|
|
17
|
+
name "incus"
|
|
18
|
+
|
|
19
|
+
option :host, required: true, desc: "Incus instance name"
|
|
20
|
+
option :remote, default: nil, desc: "Incus remote (defaults to the client's default remote)"
|
|
21
|
+
option :project, default: nil, desc: "Incus project"
|
|
22
|
+
option :incus_binary, default: "incus", desc: "Path to the incus client"
|
|
23
|
+
option :incus_user, default: nil, desc: "uid to run commands as"
|
|
24
|
+
|
|
25
|
+
def connection(_instance_opts = nil)
|
|
26
|
+
@connection ||= Connection.new(@options)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class Connection < BaseConnection
|
|
30
|
+
def initialize(options)
|
|
31
|
+
super(options)
|
|
32
|
+
@name = @options[:host]
|
|
33
|
+
@user = @options[:incus_user]
|
|
34
|
+
@cli = Kitchen::Incus::CLI.new(
|
|
35
|
+
binary: @options[:incus_binary],
|
|
36
|
+
remote: @options[:remote],
|
|
37
|
+
project: @options[:project]
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def uri
|
|
42
|
+
"incus://#{@cli.target(@name)}"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def unique_identifier
|
|
46
|
+
@cli.target(@name)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def local?
|
|
50
|
+
false
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def run_command_via_connection(cmd, &_data_handler)
|
|
56
|
+
result = @cli.exec(@name, cmd, user: @user, allow_failure: true)
|
|
57
|
+
CommandResult.new(result.stdout, result.stderr, result.exit_code)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def file_via_connection(path, *_args)
|
|
61
|
+
Train::File::Remote::Linux.new(self, path)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: kitchen-incus
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Andrew Brossia
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: test-kitchen
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '3.0'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '5'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '3.0'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '5'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: kitchen-cinc
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - "~>"
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '1.0'
|
|
39
|
+
type: :runtime
|
|
40
|
+
prerelease: false
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - "~>"
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '1.0'
|
|
46
|
+
- !ruby/object:Gem::Dependency
|
|
47
|
+
name: mixlib-shellout
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - "~>"
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '3.0'
|
|
53
|
+
type: :runtime
|
|
54
|
+
prerelease: false
|
|
55
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - "~>"
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '3.0'
|
|
60
|
+
- !ruby/object:Gem::Dependency
|
|
61
|
+
name: rake
|
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
|
63
|
+
requirements:
|
|
64
|
+
- - "~>"
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: '13.0'
|
|
67
|
+
type: :development
|
|
68
|
+
prerelease: false
|
|
69
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - "~>"
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: '13.0'
|
|
74
|
+
- !ruby/object:Gem::Dependency
|
|
75
|
+
name: rspec
|
|
76
|
+
requirement: !ruby/object:Gem::Requirement
|
|
77
|
+
requirements:
|
|
78
|
+
- - "~>"
|
|
79
|
+
- !ruby/object:Gem::Version
|
|
80
|
+
version: '3.0'
|
|
81
|
+
type: :development
|
|
82
|
+
prerelease: false
|
|
83
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
84
|
+
requirements:
|
|
85
|
+
- - "~>"
|
|
86
|
+
- !ruby/object:Gem::Version
|
|
87
|
+
version: '3.0'
|
|
88
|
+
description: Runs Test Kitchen instances as Incus containers or virtual machines,
|
|
89
|
+
using the incus CLI for exec, file transfer, login and InSpec verification.
|
|
90
|
+
email:
|
|
91
|
+
- abrossia@gmail.com
|
|
92
|
+
executables: []
|
|
93
|
+
extensions: []
|
|
94
|
+
extra_rdoc_files: []
|
|
95
|
+
files:
|
|
96
|
+
- CHANGELOG.md
|
|
97
|
+
- LICENSE
|
|
98
|
+
- README.md
|
|
99
|
+
- lib/kitchen/driver/incus.rb
|
|
100
|
+
- lib/kitchen/incus/cli.rb
|
|
101
|
+
- lib/kitchen/incus/inspec_support.rb
|
|
102
|
+
- lib/kitchen/incus/version.rb
|
|
103
|
+
- lib/kitchen/transport/incus.rb
|
|
104
|
+
- lib/train-incus.rb
|
|
105
|
+
homepage: https://github.com/aae42/kitchen-incus
|
|
106
|
+
licenses:
|
|
107
|
+
- Apache-2.0
|
|
108
|
+
metadata:
|
|
109
|
+
homepage_uri: https://github.com/aae42/kitchen-incus
|
|
110
|
+
source_code_uri: https://github.com/aae42/kitchen-incus/tree/main
|
|
111
|
+
changelog_uri: https://github.com/aae42/kitchen-incus/blob/main/CHANGELOG.md
|
|
112
|
+
bug_tracker_uri: https://github.com/aae42/kitchen-incus/issues
|
|
113
|
+
rubygems_mfa_required: 'true'
|
|
114
|
+
rdoc_options: []
|
|
115
|
+
require_paths:
|
|
116
|
+
- lib
|
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
118
|
+
requirements:
|
|
119
|
+
- - ">="
|
|
120
|
+
- !ruby/object:Gem::Version
|
|
121
|
+
version: '3.1'
|
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
|
+
requirements:
|
|
124
|
+
- - ">="
|
|
125
|
+
- !ruby/object:Gem::Version
|
|
126
|
+
version: '0'
|
|
127
|
+
requirements: []
|
|
128
|
+
rubygems_version: 3.7.2
|
|
129
|
+
specification_version: 4
|
|
130
|
+
summary: Test Kitchen driver and transport for Incus containers and VMs
|
|
131
|
+
test_files: []
|