Skip to content

Commit 3e40f44

Browse files
authored
chore: Improve builder clarity (#351)
Added and updated comments in `builder/vmware/iso/builder.go` and `builder/vmware/vmx/builder.go` to clarify the responsibilities of methods and steps in the build process. Removed unnecessary log import and logging, and improved consistency in comments and code structure for better maintainability. Signed-off-by: Ryan Johnson <ryan.johnson@broadcom.com>
1 parent 58fd179 commit 3e40f44

2 files changed

Lines changed: 30 additions & 16 deletions

File tree

builder/vmware/iso/builder.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ import (
1717
vmwcommon "github.com/hashicorp/packer-plugin-vmware/builder/vmware/common"
1818
)
1919

20+
// Builder is responsible for constructing the virtual machine based on
21+
// provided settings and steps.
2022
type Builder struct {
2123
config Config
2224
runner multistep.Runner
2325
}
2426

27+
// ConfigSpec returns the HCL2 object specification for the builder's
28+
// configuration.
2529
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
2630

31+
// Prepare validates the raw configuration and updates the builder's settings
32+
// returning warnings and errors if any occur.
2733
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
2834
warnings, errs := b.config.Prepare(raws...)
2935
if errs != nil {
@@ -33,17 +39,19 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
3339
return nil, warnings, nil
3440
}
3541

42+
// Run executes the builder's steps in sequence, orchestrating the virtual
43+
// machine creation and returning the resulting artifact.
3644
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
3745
driver, err := vmwcommon.NewDriver(&b.config.DriverConfig, &b.config.SSHConfig, b.config.VMName)
3846
if err != nil {
3947
return nil, fmt.Errorf("failed creating driver : %s", err)
4048
}
41-
49+
// Verify that ovftool is installed if exporting the virtual machine.
4250
if err := driver.VerifyOvfTool(b.config.SkipExport, b.config.SkipValidateCredentials); err != nil {
4351
return nil, err
4452
}
4553

46-
// Set up the state bag.
54+
// Set up the state.
4755
state := new(multistep.BasicStateBag)
4856
state.Put("config", &b.config)
4957
state.Put("debug", b.config.PackerDebug)
@@ -52,8 +60,9 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
5260
state.Put("ui", ui)
5361
state.Put("sshConfig", &b.config.SSHConfig)
5462
state.Put("driverConfig", &b.config.DriverConfig)
55-
state.Put("temporaryDevices", []string{}) // Devices (in .vmx) created by packer during building
63+
state.Put("temporaryDevices", []string{}) // Devices (in .vmx) created during the build.
5664

65+
// Build the steps.
5766
steps := []multistep.Step{
5867
&vmwcommon.StepPrepareTools{
5968
RemoteType: b.config.RemoteType,
@@ -202,13 +211,16 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
202211
},
203212
}
204213

214+
// Run the steps.
205215
b.runner = commonsteps.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
206216
b.runner.Run(ctx, state)
207217

218+
// Report any errors.
208219
if rawErr, ok := state.GetOk("error"); ok {
209220
return nil, rawErr.(error)
210221
}
211222

223+
// If interrupted or cancelled, then return.
212224
if _, ok := state.GetOk(multistep.StateCancelled); ok {
213225
return nil, errors.New("build was cancelled")
214226
}
@@ -217,7 +229,8 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
217229
return nil, errors.New("build was halted")
218230
}
219231

220-
exportOutputPath := state.Get("export_output_path").(string) // set in StepOutputDir
232+
// Generate the artifact.
233+
exportOutputPath := state.Get("export_output_path").(string)
221234
return vmwcommon.NewArtifact(b.config.RemoteType, b.config.Format, exportOutputPath,
222235
b.config.VMName, b.config.SkipExport, b.config.KeepRegistered, state)
223236
}

builder/vmware/vmx/builder.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"errors"
99
"fmt"
10-
"log"
1110
"time"
1211

1312
"github.com/hashicorp/hcl/v2/hcldec"
@@ -18,15 +17,19 @@ import (
1817
vmwcommon "github.com/hashicorp/packer-plugin-vmware/builder/vmware/common"
1918
)
2019

21-
// Builder implements packersdk.Builder and builds the actual VMware
22-
// images.
20+
// Builder is responsible for constructing the virtual machine based on
21+
// provided settings and steps.
2322
type Builder struct {
2423
config Config
2524
runner multistep.Runner
2625
}
2726

27+
// ConfigSpec returns the HCL2 object specification for the builder's
28+
// configuration.
2829
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
2930

31+
// Prepare validates the raw configuration and updates the builder's settings
32+
// returning warnings and errors if any occur.
3033
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
3134
warnings, errs := b.config.Prepare(raws...)
3235
if errs != nil {
@@ -36,15 +39,14 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
3639
return nil, warnings, nil
3740
}
3841

39-
// Run executes a Packer build and returns a packersdk.Artifact representing
40-
// a VMware image.
42+
// Run executes the builder's steps in sequence, orchestrating the virtual
43+
// machine creation and returning the resulting artifact.
4144
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
4245
driver, err := vmwcommon.NewDriver(&b.config.DriverConfig, &b.config.SSHConfig, b.config.VMName)
4346
if err != nil {
4447
return nil, fmt.Errorf("failed creating driver : %s", err)
4548
}
46-
// Before we get deep into the build, make sure ovftool is present and
47-
// credentials are valid, if we're going to use ovftool.
49+
// Verify that ovftool is installed if exporting the virtual machine.
4850
if err := driver.VerifyOvfTool(b.config.SkipExport, b.config.SkipValidateCredentials); err != nil {
4951
return nil, err
5052
}
@@ -57,7 +59,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
5759
state.Put("ui", ui)
5860
state.Put("sshConfig", &b.config.SSHConfig)
5961
state.Put("driverConfig", &b.config.DriverConfig)
60-
state.Put("temporaryDevices", []string{}) // Devices (in .vmx) created by packer during building
62+
state.Put("temporaryDevices", []string{}) // Devices (in .vmx) created during the build.
6163

6264
// Build the steps.
6365
steps := []multistep.Step{
@@ -206,7 +208,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
206208
return nil, rawErr.(error)
207209
}
208210

209-
// If we were interrupted or cancelled, then just exit.
211+
// If interrupted or cancelled, then return.
210212
if _, ok := state.GetOk(multistep.StateCancelled); ok {
211213
return nil, errors.New("build was cancelled")
212214
}
@@ -215,9 +217,8 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
215217
return nil, errors.New("build was halted")
216218
}
217219

218-
// Artifact
219-
log.Printf("Generating artifact...")
220-
exportOutputPath := state.Get("export_output_path").(string) // set in StepOutputDir
220+
// Generate the artifact.
221+
exportOutputPath := state.Get("export_output_path").(string)
221222
return vmwcommon.NewArtifact(b.config.RemoteType, b.config.Format, exportOutputPath,
222223
b.config.VMName, b.config.SkipExport, b.config.KeepRegistered, state)
223224
}

0 commit comments

Comments
 (0)