github.com/raghuse92/packer@v1.3.2/builder/qemu/step_set_iso.go (about)

     1  package qemu
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  // This step set iso_patch to available url
    13  type stepSetISO struct {
    14  	ResultKey string
    15  	Url       []string
    16  }
    17  
    18  func (s *stepSetISO) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    19  	ui := state.Get("ui").(packer.Ui)
    20  
    21  	iso_path := ""
    22  
    23  	for _, url := range s.Url {
    24  		req, err := http.NewRequest("HEAD", url, nil)
    25  		if err != nil {
    26  			continue
    27  		}
    28  
    29  		req.Header.Set("User-Agent", "Packer")
    30  
    31  		httpClient := &http.Client{
    32  			Transport: &http.Transport{
    33  				Proxy: http.ProxyFromEnvironment,
    34  			},
    35  		}
    36  
    37  		res, err := httpClient.Do(req)
    38  		if err == nil && (res.StatusCode >= 200 && res.StatusCode < 300) {
    39  			if res.Header.Get("Accept-Ranges") == "bytes" {
    40  				iso_path = url
    41  			}
    42  		}
    43  	}
    44  
    45  	if iso_path == "" {
    46  		err := fmt.Errorf("No byte serving support. The HTTP server must support Accept-Ranges=bytes")
    47  		state.Put("error", err)
    48  		ui.Error(err.Error())
    49  		return multistep.ActionHalt
    50  	}
    51  
    52  	state.Put(s.ResultKey, iso_path)
    53  
    54  	return multistep.ActionContinue
    55  }
    56  
    57  func (s *stepSetISO) Cleanup(state multistep.StateBag) {}