@@ -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.
2022type Builder struct {
2123 config Config
2224 runner multistep.Runner
2325}
2426
27+ // ConfigSpec returns the HCL2 object specification for the builder's
28+ // configuration.
2529func (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.
2733func (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.
3644func (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}
0 commit comments