2020-09-04 17:53:09 -04:00
|
|
|
package proxmoxiso
|
2020-01-04 16:50:35 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-09-04 17:53:09 -04:00
|
|
|
"io"
|
2020-01-04 16:50:35 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/Telmate/proxmox-api-go/proxmox"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2020-01-04 16:50:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// stepUploadISO uploads an ISO file to Proxmox so we can boot from it
|
|
|
|
type stepUploadISO struct{}
|
|
|
|
|
2020-09-04 17:53:09 -04:00
|
|
|
type uploader interface {
|
|
|
|
Upload(node string, storage string, contentType string, filename string, file io.Reader) error
|
|
|
|
}
|
|
|
|
|
2020-01-04 16:50:35 -05:00
|
|
|
var _ uploader = &proxmox.Client{}
|
|
|
|
|
|
|
|
func (s *stepUploadISO) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2020-01-04 16:50:35 -05:00
|
|
|
client := state.Get("proxmoxClient").(uploader)
|
2020-09-04 17:53:09 -04:00
|
|
|
c := state.Get("iso-config").(*Config)
|
2020-01-04 16:50:35 -05:00
|
|
|
|
|
|
|
if !c.shouldUploadISO {
|
|
|
|
state.Put("iso_file", c.ISOFile)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
p := state.Get(downloadPathKey).(string)
|
|
|
|
if p == "" {
|
|
|
|
err := fmt.Errorf("Path to downloaded ISO was empty")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// All failure cases in resolving the symlink are caught anyway in os.Open
|
|
|
|
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.ISOUrls[0])
|
|
|
|
err = client.Upload(c.Node, c.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.ISOStoragePool, filename)
|
|
|
|
state.Put("iso_file", isoStoragePath)
|
2020-09-04 17:53:09 -04:00
|
|
|
|
2020-01-04 16:50:35 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepUploadISO) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|