2016-01-25 04:01:37 -05:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-01-25 04:01:37 -05:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/net"
|
2016-01-25 04:01:37 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step set iso_patch to available url
|
|
|
|
type stepSetISO struct {
|
|
|
|
ResultKey string
|
|
|
|
Url []string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepSetISO) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-01-25 04:01:37 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
iso_path := ""
|
|
|
|
|
|
|
|
for _, url := range s.Url {
|
|
|
|
req, err := http.NewRequest("HEAD", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("User-Agent", "Packer")
|
|
|
|
|
2020-11-04 17:50:34 -05:00
|
|
|
httpClient := net.HttpClientWithEnvironmentProxy()
|
2016-01-25 04:01:37 -05:00
|
|
|
|
|
|
|
res, err := httpClient.Do(req)
|
|
|
|
if err == nil && (res.StatusCode >= 200 && res.StatusCode < 300) {
|
|
|
|
if res.Header.Get("Accept-Ranges") == "bytes" {
|
|
|
|
iso_path = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if iso_path == "" {
|
|
|
|
err := fmt.Errorf("No byte serving support. The HTTP server must support Accept-Ranges=bytes")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put(s.ResultKey, iso_path)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepSetISO) Cleanup(state multistep.StateBag) {}
|