github.com/rothwerx/packer@v0.9.0/builder/qemu/step_set_iso.go (about)

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