2020-09-04 17:53:09 -04:00
|
|
|
package proxmoxiso
|
2020-08-31 04:48:24 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/Telmate/proxmox-api-go/proxmox"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// stepUploadAdditionalISOs uploads all additional ISO files that are mountet
|
|
|
|
// to the VM
|
|
|
|
type stepUploadAdditionalISOs struct{}
|
|
|
|
|
|
|
|
var _ uploader = &proxmox.Client{}
|
|
|
|
|
|
|
|
func (s *stepUploadAdditionalISOs) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
client := state.Get("proxmoxClient").(uploader)
|
2020-09-04 17:53:09 -04:00
|
|
|
c := state.Get("iso-config").(*Config)
|
2020-08-31 04:48:24 -04:00
|
|
|
|
|
|
|
for idx := range c.AdditionalISOFiles {
|
2020-10-06 05:42:49 -04:00
|
|
|
if !c.AdditionalISOFiles[idx].ShouldUploadISO {
|
2020-08-31 04:48:24 -04:00
|
|
|
state.Put("additional_iso_files", c.AdditionalISOFiles)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-06 05:42:49 -04:00
|
|
|
p := state.Get(c.AdditionalISOFiles[idx].DownloadPathKey).(string)
|
2020-08-31 04:48:24 -04:00
|
|
|
if p == "" {
|
|
|
|
err := fmt.Errorf("Path to downloaded ISO was empty")
|
|
|
|
state.Put("erroe", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
isoPath, _ := filepath.EvalSymlinks(p)
|
|
|
|
r, err := os.Open(isoPath)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := filepath.Base(c.AdditionalISOFiles[idx].ISOConfig.ISOUrls[0])
|
|
|
|
err = client.Upload(c.Node, c.AdditionalISOFiles[idx].ISOStoragePool, "iso", filename, r)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
isoStoragePath := fmt.Sprintf("%s:iso/%s", c.AdditionalISOFiles[idx].ISOStoragePool, filename)
|
|
|
|
c.AdditionalISOFiles[idx].ISOFile = isoStoragePath
|
|
|
|
state.Put("additional_iso_files", c.AdditionalISOFiles)
|
|
|
|
}
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepUploadAdditionalISOs) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|