packer-cn/builder/qemu/step_set_iso.go

55 lines
1.2 KiB
Go
Raw Normal View History

package qemu
import (
"context"
"fmt"
"net/http"
2020-11-17 19:31:03 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer/packer-plugin-sdk/net"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
)
// This step set iso_patch to available url
type stepSetISO struct {
ResultKey string
Url []string
}
func (s *stepSetISO) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packersdk.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")
httpClient := net.HttpClientWithEnvironmentProxy()
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) {}