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"
|
|
|
|
|
2019-01-23 19:45:07 -05:00
|
|
|
commonhelper "github.com/hashicorp/packer/helper/common"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
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")
|
|
|
|
|
2019-01-23 19:45:07 -05:00
|
|
|
httpClient := commonhelper.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) {}
|