File: step_set_iso.go

package info (click to toggle)
packer 1.6.6%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 32,016 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (54 lines) | stat: -rw-r--r-- 1,220 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package qemu

import (
	"context"
	"fmt"
	"net/http"

	"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) {}