2018-01-24 09:56:14 -05:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
2019-05-25 01:45:53 -04:00
|
|
|
"context"
|
2020-01-13 18:52:05 -05:00
|
|
|
|
2020-01-08 13:22:55 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-01-07 19:59:31 -05:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/common"
|
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
2018-01-24 09:56:14 -05:00
|
|
|
packerCommon "github.com/hashicorp/packer/common"
|
2018-10-31 17:42:24 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-24 09:56:14 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Builder struct {
|
2020-01-13 15:33:56 -05:00
|
|
|
config Config
|
2018-01-24 09:56:14 -05:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:22:55 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2020-01-13 18:52:05 -05:00
|
|
|
warnings, errs := b.config.Prepare(raws...)
|
2018-01-24 09:56:14 -05:00
|
|
|
if errs != nil {
|
2020-01-13 18:52:05 -05:00
|
|
|
return nil, warnings, errs
|
2018-01-24 09:56:14 -05:00
|
|
|
}
|
|
|
|
|
2020-01-13 18:52:05 -05:00
|
|
|
return nil, warnings, nil
|
2018-01-24 09:56:14 -05:00
|
|
|
}
|
|
|
|
|
2019-05-25 01:45:53 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2018-01-24 09:56:14 -05:00
|
|
|
state := new(multistep.BasicStateBag)
|
2020-06-11 12:37:32 -04:00
|
|
|
state.Put("debug", b.config.PackerDebug)
|
2018-01-24 09:56:14 -05:00
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
2018-02-01 06:47:09 -05:00
|
|
|
var steps []multistep.Step
|
2018-01-24 09:56:14 -05:00
|
|
|
|
|
|
|
steps = append(steps,
|
|
|
|
&common.StepConnect{
|
|
|
|
Config: &b.config.ConnectConfig,
|
|
|
|
},
|
2020-09-14 17:59:51 -04:00
|
|
|
&packerCommon.StepDownload{
|
|
|
|
Checksum: b.config.ISOChecksum,
|
|
|
|
Description: "ISO",
|
|
|
|
Extension: b.config.TargetExtension,
|
|
|
|
ResultKey: "iso_path",
|
|
|
|
TargetPath: b.config.TargetPath,
|
|
|
|
Url: b.config.ISOUrls,
|
|
|
|
},
|
|
|
|
&packerCommon.StepCreateCD{
|
|
|
|
Files: b.config.CDConfig.CDFiles,
|
|
|
|
Label: b.config.CDConfig.CDLabel,
|
|
|
|
},
|
|
|
|
&StepRemoteUpload{
|
|
|
|
Datastore: b.config.Datastore,
|
|
|
|
Host: b.config.Host,
|
|
|
|
SetHostForDatastoreUploads: b.config.SetHostForDatastoreUploads,
|
|
|
|
},
|
2018-01-24 09:56:14 -05:00
|
|
|
&StepCreateVM{
|
2018-05-06 17:26:04 -04:00
|
|
|
Config: &b.config.CreateConfig,
|
|
|
|
Location: &b.config.LocationConfig,
|
2018-11-16 10:37:54 -05:00
|
|
|
Force: b.config.PackerConfig.PackerForce,
|
2018-01-24 09:56:14 -05:00
|
|
|
},
|
2018-05-05 17:41:14 -04:00
|
|
|
&common.StepConfigureHardware{
|
|
|
|
Config: &b.config.HardwareConfig,
|
|
|
|
},
|
2018-10-29 20:18:57 -04:00
|
|
|
&StepAddCDRom{
|
|
|
|
Config: &b.config.CDRomConfig,
|
|
|
|
},
|
2018-05-04 18:48:16 -04:00
|
|
|
&common.StepConfigParams{
|
2018-04-25 05:23:37 -04:00
|
|
|
Config: &b.config.ConfigParamsConfig,
|
2018-02-26 16:18:06 -05:00
|
|
|
},
|
2018-01-24 09:56:14 -05:00
|
|
|
)
|
|
|
|
|
2018-01-30 11:47:38 -05:00
|
|
|
if b.config.Comm.Type != "none" {
|
|
|
|
steps = append(steps,
|
2018-05-04 17:10:43 -04:00
|
|
|
&packerCommon.StepCreateFloppy{
|
|
|
|
Files: b.config.FloppyFiles,
|
|
|
|
Directories: b.config.FloppyDirectories,
|
2020-05-08 10:13:54 -04:00
|
|
|
Label: b.config.FloppyLabel,
|
2018-05-04 17:10:43 -04:00
|
|
|
},
|
|
|
|
&StepAddFloppy{
|
2020-05-05 14:34:15 -04:00
|
|
|
Config: &b.config.FloppyConfig,
|
|
|
|
Datastore: b.config.Datastore,
|
|
|
|
Host: b.config.Host,
|
|
|
|
SetHostForDatastoreUploads: b.config.SetHostForDatastoreUploads,
|
2018-05-04 17:10:43 -04:00
|
|
|
},
|
2020-06-11 12:37:32 -04:00
|
|
|
&common.StepHTTPIPDiscover{
|
2020-06-17 01:36:55 -04:00
|
|
|
HTTPIP: b.config.BootConfig.HTTPIP,
|
|
|
|
Network: b.config.WaitIpConfig.GetIPNet(),
|
2020-06-11 12:37:32 -04:00
|
|
|
},
|
2018-05-30 07:59:44 -04:00
|
|
|
&packerCommon.StepHTTPServer{
|
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
2020-05-27 16:34:24 -04:00
|
|
|
HTTPAddress: b.config.HTTPAddress,
|
2018-05-30 07:59:44 -04:00
|
|
|
},
|
2018-01-30 12:25:05 -05:00
|
|
|
&common.StepRun{
|
2018-10-31 17:42:24 -04:00
|
|
|
Config: &b.config.RunConfig,
|
2018-10-20 20:57:28 -04:00
|
|
|
SetOrder: true,
|
2018-01-30 12:25:05 -05:00
|
|
|
},
|
2020-06-24 05:14:30 -04:00
|
|
|
&common.StepBootCommand{
|
2018-02-17 21:13:56 -05:00
|
|
|
Config: &b.config.BootConfig,
|
2018-12-22 18:13:31 -05:00
|
|
|
Ctx: b.config.ctx,
|
|
|
|
VMName: b.config.VMName,
|
2018-02-17 21:13:56 -05:00
|
|
|
},
|
2018-05-30 07:52:27 -04:00
|
|
|
&common.StepWaitForIp{
|
|
|
|
Config: &b.config.WaitIpConfig,
|
|
|
|
},
|
2018-01-30 11:47:38 -05:00
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.Comm,
|
2020-01-30 18:22:22 -05:00
|
|
|
Host: common.CommHost(b.config.Comm.Host()),
|
2019-07-08 11:40:01 -04:00
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
2018-01-30 11:47:38 -05:00
|
|
|
},
|
|
|
|
&packerCommon.StepProvision{},
|
|
|
|
&common.StepShutdown{
|
|
|
|
Config: &b.config.ShutdownConfig,
|
|
|
|
},
|
2018-05-04 17:10:43 -04:00
|
|
|
&StepRemoveFloppy{
|
|
|
|
Datastore: b.config.Datastore,
|
|
|
|
Host: b.config.Host,
|
|
|
|
},
|
2018-01-30 11:47:38 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
steps = append(steps,
|
2020-02-05 10:25:49 -05:00
|
|
|
&StepRemoveCDRom{
|
2020-02-04 14:25:45 -05:00
|
|
|
Config: &b.config.RemoveCDRomConfig,
|
|
|
|
},
|
2018-01-30 11:47:38 -05:00
|
|
|
&common.StepCreateSnapshot{
|
|
|
|
CreateSnapshot: b.config.CreateSnapshot,
|
|
|
|
},
|
|
|
|
&common.StepConvertToTemplate{
|
|
|
|
ConvertToTemplate: b.config.ConvertToTemplate,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-07-10 05:01:10 -04:00
|
|
|
if b.config.ContentLibraryDestinationConfig != nil {
|
|
|
|
steps = append(steps, &common.StepImportToContentLibrary{
|
|
|
|
ContentLibConfig: b.config.ContentLibraryDestinationConfig,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-19 13:51:43 -04:00
|
|
|
if b.config.Export != nil {
|
|
|
|
steps = append(steps, &common.StepExport{
|
|
|
|
Name: b.config.Export.Name,
|
|
|
|
Force: b.config.Export.Force,
|
|
|
|
Images: b.config.Export.Images,
|
|
|
|
Manifest: b.config.Export.Manifest,
|
|
|
|
OutputDir: b.config.Export.OutputDir.OutputDir,
|
2020-03-19 14:46:30 -04:00
|
|
|
Options: b.config.Export.Options,
|
2020-03-19 13:51:43 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-24 05:14:30 -04:00
|
|
|
b.runner = packerCommon.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-05-25 01:45:53 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2018-01-24 09:56:14 -05:00
|
|
|
|
2018-05-06 17:26:04 -04:00
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
2018-01-24 09:56:14 -05:00
|
|
|
}
|
|
|
|
|
2018-05-06 17:26:04 -04:00
|
|
|
if _, ok := state.GetOk("vm"); !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-04-30 15:22:57 -04:00
|
|
|
|
2018-01-24 09:56:14 -05:00
|
|
|
artifact := &common.Artifact{
|
2020-01-30 05:27:58 -05:00
|
|
|
Name: b.config.VMName,
|
2020-08-31 04:34:41 -04:00
|
|
|
VM: state.Get("vm").(*driver.VirtualMachineDriver),
|
2020-01-30 05:27:58 -05:00
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2018-01-24 09:56:14 -05:00
|
|
|
}
|
2020-04-30 15:22:57 -04:00
|
|
|
|
|
|
|
if b.config.Export != nil {
|
|
|
|
artifact.Outconfig = &b.config.Export.OutputDir
|
|
|
|
}
|
|
|
|
|
2018-01-24 09:56:14 -05:00
|
|
|
return artifact, nil
|
|
|
|
}
|