packer-cn/builder/vsphere/iso/builder.go

183 lines
4.6 KiB
Go
Raw Normal View History

package iso
import (
"context"
2020-01-13 18:52:05 -05:00
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer/builder/vsphere/common"
"github.com/hashicorp/packer/builder/vsphere/driver"
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"
"github.com/hashicorp/packer/packer"
)
type Builder struct {
config Config
runner multistep.Runner
}
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...)
if errs != nil {
2020-01-13 18:52:05 -05:00
return nil, warnings, errs
}
2020-01-13 18:52:05 -05:00
return nil, warnings, nil
}
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
state := new(multistep.BasicStateBag)
state.Put("debug", b.config.PackerDebug)
state.Put("hook", hook)
state.Put("ui", ui)
var steps []multistep.Step
steps = append(steps,
&common.StepConnect{
Config: &b.config.ConnectConfig,
},
)
if b.config.ISOUrls != nil {
steps = append(steps,
&packerCommon.StepDownload{
Drop the iso_checksum_type & iso_checksum_url fields (#8437) * Drop the iso_checksum_type & iso_checksum_url fields In favor of simply using iso_checksum that will know what to do. * fix after master merge * Update builder_test.go * Update builder_test.go * Update builder_test.go * Update builder_test.go * Update builder_test.go * remove checksum lowercasing tests * Update builder_test.go * Update builder_test.go * better docs * Update builder_test.go * even better docs * Update config.go * Update builder_test.go * Update step_create_vmx_test.go * make generate * better docs * fix imports * up tests * Update _ISOConfig-required.html.md * Update builder_test.go * don't use sha1.Sum("none") as a caching path * Update builder_test.go * better docs * Update iso_config_test.go remove ISOChecksumType/ISOChecksumURL references * Update step_download_test.go * add iso_checksum_url and iso_checksum_type fixers + tests * add concrete examples of checksum values * add examples of checksumming from local file * update go-getter dep * up deps * use new go-getter version * up ESX5Driver.VerifyChecksum: use go-getter's checksumming * ISOConfig.Prepare: get checksum there in case we need it as a string in ESX5Driver.VerifyChecksum * Update iso_config.go * get go-getter from v2 branch * Update driver_esx5.go add more comments * Update driver_esx5.go * show better error message when the checksum is invalid * Update builder_test.go put in a valid checksum to fix tests, checksum is md5("packer") * Update builder_test.go test invalid and valid checksum * more test updating * fix default md5 string to be a valid md5 * TestChecksumFileNameMixedCaseBug: use 'file:' prefix for file checksumming * Update iso_config_test.go * Update iso_config_test.go * Update builder_test.go * Update builder_test.go * Update builder_test.go * Update CHANGELOG.md * Update CHANGELOG.md * Update go.mod * Update go.mod * Update CHANGELOG.md
2020-05-28 05:02:09 -04:00
Checksum: b.config.ISOChecksum,
Description: "ISO",
Extension: b.config.TargetExtension,
ResultKey: "iso_path",
TargetPath: b.config.TargetPath,
Url: b.config.ISOUrls,
},
&StepRemoteUpload{
Datastore: b.config.Datastore,
Host: b.config.Host,
SetHostForDatastoreUploads: b.config.SetHostForDatastoreUploads,
},
)
}
steps = append(steps,
&StepCreateVM{
2018-05-06 17:26:04 -04:00
Config: &b.config.CreateConfig,
Location: &b.config.LocationConfig,
Force: b.config.PackerConfig.PackerForce,
},
2018-05-05 17:41:14 -04:00
&common.StepConfigureHardware{
Config: &b.config.HardwareConfig,
},
&StepAddCDRom{
Config: &b.config.CDRomConfig,
},
&common.StepConfigParams{
Config: &b.config.ConfigParamsConfig,
2018-02-26 16:18:06 -05:00
},
)
if b.config.Comm.Type != "none" {
steps = append(steps,
&packerCommon.StepCreateFloppy{
Files: b.config.FloppyFiles,
Directories: b.config.FloppyDirectories,
Label: b.config.FloppyLabel,
},
&StepAddFloppy{
Config: &b.config.FloppyConfig,
Datastore: b.config.Datastore,
Host: b.config.Host,
SetHostForDatastoreUploads: b.config.SetHostForDatastoreUploads,
},
&common.StepHTTPIPDiscover{
HTTPIP: b.config.BootConfig.HTTPIP,
Network: b.config.WaitIpConfig.GetIPNet(),
},
2018-05-30 07:59:44 -04:00
&packerCommon.StepHTTPServer{
HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin,
HTTPPortMax: b.config.HTTPPortMax,
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,
SetOrder: true,
2018-01-30 12:25:05 -05:00
},
&common.StepBootCommand{
2018-02-17 21:13:56 -05:00
Config: &b.config.BootConfig,
Ctx: b.config.ctx,
VMName: b.config.VMName,
2018-02-17 21:13:56 -05:00
},
&common.StepWaitForIp{
Config: &b.config.WaitIpConfig,
},
&communicator.StepConnect{
Config: &b.config.Comm,
Host: common.CommHost(b.config.Comm.Host()),
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
&packerCommon.StepProvision{},
&common.StepShutdown{
Config: &b.config.ShutdownConfig,
},
&StepRemoveFloppy{
Datastore: b.config.Datastore,
Host: b.config.Host,
},
)
}
steps = append(steps,
2020-02-05 10:25:49 -05:00
&StepRemoveCDRom{
Config: &b.config.RemoveCDRomConfig,
},
&common.StepCreateSnapshot{
CreateSnapshot: b.config.CreateSnapshot,
},
&common.StepConvertToTemplate{
ConvertToTemplate: b.config.ConvertToTemplate,
},
)
if b.config.ContentLibraryDestinationConfig != nil {
steps = append(steps, &common.StepImportToContentLibrary{
ContentLibConfig: b.config.ContentLibraryDestinationConfig,
})
}
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,
Options: b.config.Export.Options,
})
}
b.runner = packerCommon.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(ctx, state)
2018-05-06 17:26:04 -04:00
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
2018-05-06 17:26:04 -04:00
if _, ok := state.GetOk("vm"); !ok {
return nil, nil
}
artifact := &common.Artifact{
Name: b.config.VMName,
VM: state.Get("vm").(*driver.VirtualMachine),
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
}
if b.config.Export != nil {
artifact.Outconfig = &b.config.Export.OutputDir
}
return artifact, nil
}