add cd_files

This commit is contained in:
Megan Marsh 2020-09-14 14:59:51 -07:00
parent e00f6afff8
commit e70e8c8651
4 changed files with 93 additions and 47 deletions

View File

@ -40,27 +40,23 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&common.StepConnect{
Config: &b.config.ConnectConfig,
},
)
if b.config.ISOUrls != nil {
steps = append(steps,
&packerCommon.StepDownload{
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,
&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,
},
&StepCreateVM{
Config: &b.config.CreateConfig,
Location: &b.config.LocationConfig,

View File

@ -15,6 +15,7 @@ import (
type Config struct {
packerCommon.PackerConfig `mapstructure:",squash"`
packerCommon.HTTPConfig `mapstructure:",squash"`
packerCommon.CDConfig `mapstructure:",squash"`
common.ConnectConfig `mapstructure:",squash"`
CreateConfig `mapstructure:",squash"`
@ -80,6 +81,7 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
errs = packer.MultiErrorAppend(errs, c.HTTPConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.CDRomConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.CDConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.BootConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.WaitIpConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...)

View File

@ -62,6 +62,14 @@ func (s *StepAddCDRom) Run(_ context.Context, state multistep.StateBag) multiste
}
}
// Add our custom CD, if it exists
cd_path, ok := state.Get("cd_path").(string)
if ok {
if cd_path != "" {
s.Config.ISOPaths = append(s.Config.ISOPaths, cd_path)
}
}
if len(s.Config.ISOPaths) > 0 {
for _, path := range s.Config.ISOPaths {
if err := vm.AddCdrom(s.Config.CdromType, path); err != nil {

View File

@ -21,39 +21,79 @@ func (s *StepRemoteUpload) Run(_ context.Context, state multistep.StateBag) mult
d := state.Get("driver").(driver.Driver)
if path, ok := state.GetOk("iso_path"); ok {
filename := filepath.Base(path.(string))
ds, err := d.FindDatastore(s.Datastore, s.Host)
// user-supplied boot iso
fullRemotePath, err := s.uploadFile(path.(string), d, ui)
if err != nil {
state.Put("error", fmt.Errorf("datastore doesn't exist: %v", err))
return multistep.ActionHalt
}
remotePath := fmt.Sprintf("packer_cache/%s", filename)
remoteDirectory := fmt.Sprintf("[%s] packer_cache/", ds.Name())
fullRemotePath := fmt.Sprintf("%s/%s", remoteDirectory, filename)
ui.Say(fmt.Sprintf("Uploading %s to %s", filename, remotePath))
if exists := ds.FileExists(remotePath); exists == true {
ui.Say("File already uploaded; continuing")
state.Put("iso_remote_path", fullRemotePath)
return multistep.ActionContinue
}
if err := ds.MakeDirectory(remoteDirectory); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
if err := ds.UploadFile(path.(string), remotePath, s.Host, s.SetHostForDatastoreUploads); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("iso_remote_path", fullRemotePath)
}
if cdPath, ok := state.GetOk("cd_path"); ok {
// Packer-created cd_files disk
fullRemotePath, err := s.uploadFile(cdPath.(string), d, ui)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("cd_path", fullRemotePath)
}
return multistep.ActionContinue
}
func (s *StepRemoteUpload) Cleanup(state multistep.StateBag) {}
func (s *StepRemoteUpload) uploadFile(path string, d driver.Driver, ui packer.Ui) (string, error) {
ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
return "", fmt.Errorf("datastore doesn't exist: %v", err)
}
filename := filepath.Base(path)
remotePath := fmt.Sprintf("packer_cache/%s", filename)
remoteDirectory := fmt.Sprintf("[%s] packer_cache/", ds.Name())
fullRemotePath := fmt.Sprintf("%s/%s", remoteDirectory, filename)
ui.Say(fmt.Sprintf("Uploading %s to %s", filename, remotePath))
if exists := ds.FileExists(remotePath); exists == true {
ui.Say(fmt.Sprintf("File %s already uploaded; continuing", filename))
return fullRemotePath, nil
}
if err := ds.MakeDirectory(remoteDirectory); err != nil {
return "", err
}
if err := ds.UploadFile(path, remotePath, s.Host, s.SetHostForDatastoreUploads); err != nil {
return "", err
}
return fullRemotePath, nil
}
func (s *StepRemoteUpload) Cleanup(state multistep.StateBag) {
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if !cancelled && !halted {
return
}
ui := state.Get("ui").(packer.Ui)
d := state.Get("driver").(*driver.VCenterDriver)
if UploadedCDPath, ok := state.GetOk("cd_path"); ok {
ui.Say("Deleting cd_files image from remote datastore ...")
ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
state.Put("error", err)
return
}
err = ds.Delete(UploadedCDPath.(string))
if err != nil {
state.Put("error", err)
return
}
}
}