Skip to content

Commit e1a726d

Browse files
mpywelllbajolet-hashicorp
authored andcommitted
add backwards compatibility support for ISOs
- Add conversion handling, tests, UI deprecation warnings for deprecated iso builder config options - Add conversion handling, tests, UI deprecation warnings for `device` field in ISOsConfig struct - Add logic to handle statically assigned ISO devices from `device` field - Add default static mapping of boot ISO to ide2 if boot_iso `type` undefined in packer config - Update documentation
1 parent 0f5b0f7 commit e1a726d

File tree

13 files changed

+524
-147
lines changed

13 files changed

+524
-147
lines changed

.web-docs/components/builder/clone/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -671,12 +671,16 @@ In HCL2:
671671

672672
<!-- Code generated from the comments of the ISOsConfig struct in builder/proxmox/common/config.go; DO NOT EDIT MANUALLY -->
673673

674+
- `device` (string) - DEPRECATED. Assign bus type with `type`. Optionally assign a bus index with `index`.
675+
Bus type and bus index that the ISO will be mounted on. Can be `ideX`,
676+
`sataX` or `scsiX`.
677+
For `ide` the bus index ranges from 0 to 3, for `sata` from 0 to 5 and for
678+
`scsi` from 0 to 30.
679+
Defaulted to `ide3` in versions up to v1.8, now defaults to dynamic assignment (next available bus index after hard disks are allocated)
680+
674681
- `type` (string) - Bus type that the ISO will be mounted on. Can be `ide`, `sata` or `scsi`. Defaults to `ide`.
675-
676-
In v1.9 bus indexes are no longer accepted for ISOs. ISOs are now attached to VMs in the order
677-
they are configured, using free bus indexes after disks are attached.
678-
Example: if two Disks and one ISO are defined as type `sata`, the disks will be attached to the VM
679-
as `sata0`, `sata1`, and the ISO will be mapped to `sata2` (the next free device index)
682+
683+
- `index` (string) - Optional: Used in combination with `type` to statically assign an ISO to a bus index.
680684

681685
- `iso_file` (string) - Path to the ISO file to boot from, expressed as a
682686
proxmox datastore path, for example

.web-docs/components/builder/iso/README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ in the image's Cloud-Init settings for provisioning.
3737

3838
<!-- Code generated from the comments of the Config struct in builder/proxmox/iso/config.go; DO NOT EDIT MANUALLY -->
3939

40-
- `iso` (common.ISOsConfig) - Boot ISO attached to the virtual machine.
40+
- `boot_iso` (common.ISOsConfig) - Boot ISO attached to the virtual machine.
4141

4242
JSON Example:
4343

4444
```json
4545

46-
"iso": {
46+
"boot_iso": {
4747
"type": "scsi",
4848
"iso_file": "local:iso/debian-12.5.0-amd64-netinst.iso",
4949
"unmount": true,
@@ -55,7 +55,7 @@ in the image's Cloud-Init settings for provisioning.
5555

5656
```hcl
5757
58-
iso {
58+
boot_iso {
5959
type = "scsi"
6060
iso_file = "local:iso/debian-12.5.0-amd64-netinst.iso"
6161
unmount = true
@@ -221,6 +221,24 @@ in the image's Cloud-Init settings for provisioning.
221221

222222
<!-- Code generated from the comments of the Config struct in builder/proxmox/iso/config.go; DO NOT EDIT MANUALLY -->
223223

224+
- `iso_file` (string) - DEPRECATED. Define Boot ISO config with the `boot_iso` block instead.
225+
Path to the ISO file to boot from, expressed as a
226+
proxmox datastore path, for example
227+
`local:iso/Fedora-Server-dvd-x86_64-29-1.2.iso`.
228+
Either `iso_file` OR `iso_url` must be specifed.
229+
230+
- `iso_storage_pool` (string) - DEPRECATED. Define Boot ISO config with the `boot_iso` block instead.
231+
Proxmox storage pool onto which to upload
232+
the ISO file.
233+
234+
- `iso_download_pve` (bool) - DEPRECATED. Define Boot ISO config with the `boot_iso` block instead.
235+
Download the ISO directly from the PVE node rather than through Packer.
236+
237+
Defaults to `false`
238+
239+
- `unmount_iso` (bool) - DEPRECATED. Define Boot ISO config with the `boot_iso` block instead.
240+
If true, remove the mounted ISO from the template
241+
after finishing. Defaults to `false`.
224242

225243
<!-- End of code generated from the comments of the Config struct in builder/proxmox/iso/config.go; -->
226244

@@ -331,12 +349,16 @@ HCL2 example:
331349

332350
<!-- Code generated from the comments of the ISOsConfig struct in builder/proxmox/common/config.go; DO NOT EDIT MANUALLY -->
333351

352+
- `device` (string) - DEPRECATED. Assign bus type with `type`. Optionally assign a bus index with `index`.
353+
Bus type and bus index that the ISO will be mounted on. Can be `ideX`,
354+
`sataX` or `scsiX`.
355+
For `ide` the bus index ranges from 0 to 3, for `sata` from 0 to 5 and for
356+
`scsi` from 0 to 30.
357+
Defaulted to `ide3` in versions up to v1.8, now defaults to dynamic assignment (next available bus index after hard disks are allocated)
358+
334359
- `type` (string) - Bus type that the ISO will be mounted on. Can be `ide`, `sata` or `scsi`. Defaults to `ide`.
335-
336-
In v1.9 bus indexes are no longer accepted for ISOs. ISOs are now attached to VMs in the order
337-
they are configured, using free bus indexes after disks are attached.
338-
Example: if two Disks and one ISO are defined as type `sata`, the disks will be attached to the VM
339-
as `sata0`, `sata1`, and the ISO will be mapped to `sata2` (the next free device index)
360+
361+
- `index` (string) - Optional: Used in combination with `type` to statically assign an ISO to a bus index.
340362

341363
- `iso_file` (string) - Path to the ISO file to boot from, expressed as a
342364
proxmox datastore path, for example

builder/proxmox/common/config.go

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,17 @@ type Config struct {
237237
// ```
238238
type ISOsConfig struct {
239239
commonsteps.ISOConfig `mapstructure:",squash"`
240+
// DEPRECATED. Assign bus type with `type`. Optionally assign a bus index with `index`.
241+
// Bus type and bus index that the ISO will be mounted on. Can be `ideX`,
242+
// `sataX` or `scsiX`.
243+
// For `ide` the bus index ranges from 0 to 3, for `sata` from 0 to 5 and for
244+
// `scsi` from 0 to 30.
245+
// Defaulted to `ide3` in versions up to v1.8, now defaults to dynamic assignment (next available bus index after hard disks are allocated)
246+
Device string `mapstructure:"device"`
240247
// Bus type that the ISO will be mounted on. Can be `ide`, `sata` or `scsi`. Defaults to `ide`.
241-
//
242-
// In v1.9 bus indexes are no longer accepted for ISOs. ISOs are now attached to VMs in the order
243-
// they are configured, using free bus indexes after disks are attached.
244-
// Example: if two Disks and one ISO are defined as type `sata`, the disks will be attached to the VM
245-
// as `sata0`, `sata1`, and the ISO will be mapped to `sata2` (the next free device index)
246248
Type string `mapstructure:"type"`
249+
// Optional: Used in combination with `type` to statically assign an ISO to a bus index.
250+
Index string `mapstructure:"index"`
247251
// Path to the ISO file to boot from, expressed as a
248252
// proxmox datastore path, for example
249253
// `local:iso/Fedora-Server-dvd-x86_64-29-1.2.iso`.
@@ -687,6 +691,52 @@ func (c *Config) Prepare(upper interface{}, raws ...interface{}) ([]string, []st
687691
}
688692
c.ISOs[idx].ShouldUploadISO = true
689693
}
694+
// validate device field
695+
if c.ISOs[idx].Device != "" {
696+
warnings = append(warnings, "additional_iso_files field 'device' is deprecated and will be removed in a future release, assign bus type with 'type'. Optionally assign a bus index with 'index'")
697+
if strings.HasPrefix(c.ISOs[idx].Device, "ide") {
698+
busnumber, err := strconv.Atoi(c.ISOs[idx].Device[3:])
699+
if err != nil {
700+
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("%s is not a valid bus index", c.ISOs[idx].Device[3:]))
701+
}
702+
if busnumber > 3 {
703+
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("IDE bus index can't be higher than 3"))
704+
} else {
705+
// convert device field to type and index fields
706+
log.Printf("converting deprecated field 'device' value %s to 'type' %s and 'index' %d", c.ISOs[idx].Device, "ide", busnumber)
707+
c.ISOs[idx].Type = "ide"
708+
c.ISOs[idx].Index = strconv.Itoa(busnumber)
709+
}
710+
}
711+
if strings.HasPrefix(c.ISOs[idx].Device, "sata") {
712+
busnumber, err := strconv.Atoi(c.ISOs[idx].Device[4:])
713+
if err != nil {
714+
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("%s is not a valid bus index", c.ISOs[idx].Device[4:]))
715+
}
716+
if busnumber > 5 {
717+
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("SATA bus index can't be higher than 5"))
718+
} else {
719+
// convert device field to type and index fields
720+
log.Printf("converting deprecated field 'device' value %s to 'type' %s and 'index' %d", c.ISOs[idx].Device, "sata", busnumber)
721+
c.ISOs[idx].Type = "sata"
722+
c.ISOs[idx].Index = strconv.Itoa(busnumber)
723+
}
724+
}
725+
if strings.HasPrefix(c.ISOs[idx].Device, "scsi") {
726+
busnumber, err := strconv.Atoi(c.ISOs[idx].Device[4:])
727+
if err != nil {
728+
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("%s is not a valid bus index", c.ISOs[idx].Device[4:]))
729+
}
730+
if busnumber > 30 {
731+
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("SCSI bus index can't be higher than 30"))
732+
} else {
733+
// convert device field to type and index fields
734+
log.Printf("converting deprecated field 'device' value %s to 'type' %s and 'index' %d", c.ISOs[idx].Device, "scsi", busnumber)
735+
c.ISOs[idx].Type = "scsi"
736+
c.ISOs[idx].Index = strconv.Itoa(busnumber)
737+
}
738+
}
739+
}
690740
// validate device type, assign if unset
691741
switch c.ISOs[idx].Type {
692742
case "ide", "sata", "scsi":

builder/proxmox/common/config.hcl2spec.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/proxmox/common/config_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package proxmox
55

66
import (
77
"fmt"
8+
"regexp"
89
"strings"
910
"testing"
1011

@@ -244,7 +245,47 @@ func TestISOs(t *testing.T) {
244245
}
245246
})
246247
}
248+
}
247249

250+
func TestDeprecatedISOOptionsAreConverted(t *testing.T) {
251+
isotests := []struct {
252+
name string
253+
ISOs map[string]interface{}
254+
}{
255+
{
256+
// Ensure deprecated device field is converted
257+
name: "device should be converted to type and index",
258+
ISOs: map[string]interface{}{
259+
"device": "ide3",
260+
"iso_file": "local:iso/test.iso",
261+
},
262+
},
263+
}
264+
for _, c := range isotests {
265+
t.Run(c.name, func(t *testing.T) {
266+
cfg := mandatoryConfig(t)
267+
cfg["additional_iso_files"] = c.ISOs
268+
269+
var config Config
270+
_, _, err := config.Prepare(&config, cfg)
271+
if err != nil {
272+
t.Fatal(err)
273+
}
274+
275+
rd := regexp.MustCompile(`\D+`)
276+
bus := rd.FindString(config.ISOs[0].Device)
277+
278+
rb := regexp.MustCompile(`\d+`)
279+
index := rb.FindString(config.ISOs[0].Device)
280+
281+
if config.ISOs[0].Type != bus {
282+
t.Errorf("Expected device to be converted to type %s", bus)
283+
}
284+
if config.ISOs[0].Index != index {
285+
t.Errorf("Expected device to be converted to index %s", index)
286+
}
287+
})
288+
}
248289
}
249290

250291
func TestRng0(t *testing.T) {

0 commit comments

Comments
 (0)