generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 95
/
step_reattach_cdrom.go
104 lines (90 loc) · 3.25 KB
/
step_reattach_cdrom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
//go:generate packer-sdc struct-markdown
//go:generate packer-sdc mapstructure-to-hcl2 -type ReattachCDRomConfig
package common
import (
"context"
"fmt"
"math"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/driver"
)
type ReattachCDRomConfig struct {
// Reattach one or more configured CD-ROM devices. Range: 1-4.
// You can reattach up to 4 CD-ROM devices to the final build artifact.
// If set to 0, `reattach_cdroms` is ignored and the step is skipped.
// When set to a value in the range, `remove_cdrom` is ignored and
// the CD-ROM devices are kept without any attached media.
ReattachCDRom int `mapstructure:"reattach_cdroms"`
}
type StepReattachCDRom struct {
Config *ReattachCDRomConfig
CDRomConfig *CDRomConfig
}
func (s *StepReattachCDRom) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packersdk.Ui)
vm := state.Get("vm").(driver.VirtualMachine)
var err error
// Check if `reattach_cdroms` is set.
ReattachCDRom := s.Config.ReattachCDRom
if ReattachCDRom == 0 {
return multistep.ActionContinue
}
if ReattachCDRom < 1 || ReattachCDRom > 4 {
err := fmt.Errorf("'reattach_cdroms' should be between 1 and 4. if set to 0, `reattach_cdroms` is ignored and the step is skipped")
state.Put("error", fmt.Errorf("error reattach cdrom: %v", err))
return multistep.ActionHalt
}
ui.Say("Reattaching CD-ROM devices...")
// Add the CD-ROM devices to the image based on the value of `reattach_cdroms`.
// A valid ISO path is required for this step. The media will subsequently be ejected.
nAttachableCdroms := ReattachCDRom - len(s.CDRomConfig.ISOPaths)
if nAttachableCdroms < 0 {
err = vm.RemoveNCdroms(int(math.Abs(float64(nAttachableCdroms))))
if err != nil {
state.Put("error", fmt.Errorf("error removing cdrom prior to reattaching: %v", err))
return multistep.ActionHalt
}
ui.Say("Ejecting CD-ROM media...")
// Eject media from CD-ROM devices.
err = vm.EjectCdroms()
if err != nil {
state.Put("error", fmt.Errorf("error ejecting cdrom media: %v", err))
return multistep.ActionHalt
}
} else {
// Eject media from the existing CD-ROM devices.
err = vm.EjectCdroms()
if err != nil {
state.Put("error", fmt.Errorf("error ejecting cdrom media: %v", err))
return multistep.ActionHalt
}
}
// Add CD-ROMs, if required.
if nAttachableCdroms > 0 {
// If the CD-ROM device type is SATA, make sure SATA controller is present.
if s.CDRomConfig.CdromType == "sata" {
if _, err := vm.FindSATAController(); err == driver.ErrNoSataController {
ui.Say("Adding SATA controller...")
if err := vm.AddSATAController(); err != nil {
state.Put("error", fmt.Errorf("error adding sata controller: %v", err))
return multistep.ActionHalt
}
}
}
ui.Say("Adding CD-ROM devices...")
for i := 0; i < nAttachableCdroms; i++ {
err := vm.AddCdrom(s.CDRomConfig.CdromType, "")
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
}
}
return multistep.ActionContinue
}
func (s *StepReattachCDRom) Cleanup(state multistep.StateBag) {
// no cleanup
}