forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iso_config.go
168 lines (153 loc) · 4.42 KB
/
iso_config.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package common
import (
"bufio"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/hashicorp/packer/template/interpolate"
)
// ISOConfig contains configuration for downloading ISO images.
type ISOConfig struct {
ISOChecksum string `mapstructure:"iso_checksum"`
ISOChecksumURL string `mapstructure:"iso_checksum_url"`
ISOChecksumType string `mapstructure:"iso_checksum_type"`
ISOUrls []string `mapstructure:"iso_urls"`
TargetPath string `mapstructure:"iso_target_path"`
TargetExtension string `mapstructure:"iso_target_extension"`
RawSingleISOUrl string `mapstructure:"iso_url"`
}
func (c *ISOConfig) Prepare(ctx *interpolate.Context) (warnings []string, errs []error) {
if c.RawSingleISOUrl == "" && len(c.ISOUrls) == 0 {
errs = append(
errs, errors.New("One of iso_url or iso_urls must be specified."))
return
} else if c.RawSingleISOUrl != "" && len(c.ISOUrls) > 0 {
errs = append(
errs, errors.New("Only one of iso_url or iso_urls may be specified."))
return
} else if c.RawSingleISOUrl != "" {
c.ISOUrls = []string{c.RawSingleISOUrl}
}
if c.ISOChecksumType == "" {
errs = append(
errs, errors.New("The iso_checksum_type must be specified."))
} else {
c.ISOChecksumType = strings.ToLower(c.ISOChecksumType)
if c.ISOChecksumType != "none" {
if c.ISOChecksum == "" && c.ISOChecksumURL == "" {
errs = append(
errs, errors.New("Due to large file sizes, an iso_checksum is required"))
return warnings, errs
} else {
if h := HashForType(c.ISOChecksumType); h == nil {
errs = append(
errs, fmt.Errorf("Unsupported checksum type: %s", c.ISOChecksumType))
return warnings, errs
}
// If iso_checksum has no value use iso_checksum_url instead.
if c.ISOChecksum == "" {
u, err := url.Parse(c.ISOChecksumURL)
if err != nil {
errs = append(errs,
fmt.Errorf("Error parsing checksum: %s", err))
return warnings, errs
}
switch u.Scheme {
case "http", "https":
res, err := http.Get(c.ISOChecksumURL)
c.ISOChecksum = ""
if err != nil {
errs = append(errs,
fmt.Errorf("Error getting checksum from url: %s", c.ISOChecksumURL))
return warnings, errs
}
defer res.Body.Close()
err = c.parseCheckSumFile(bufio.NewReader(res.Body))
if err != nil {
errs = append(errs, err)
return warnings, errs
}
case "file":
path := u.Path
if runtime.GOOS == "windows" && len(path) > 2 && path[0] == '/' && path[2] == ':' {
path = strings.TrimLeft(path, "/")
}
file, err := os.Open(path)
if err != nil {
errs = append(errs, err)
return warnings, errs
}
err = c.parseCheckSumFile(bufio.NewReader(file))
if err != nil {
errs = append(errs, err)
return warnings, errs
}
case "":
break
default:
errs = append(errs,
fmt.Errorf("Error parsing checksum url: %s, scheme not supported: %s", c.ISOChecksumURL, u.Scheme))
return warnings, errs
}
}
}
}
}
c.ISOChecksum = strings.ToLower(c.ISOChecksum)
for i, url := range c.ISOUrls {
url, err := DownloadableURL(url)
if err != nil {
errs = append(
errs, fmt.Errorf("Failed to parse iso_url %d: %s", i+1, err))
} else {
c.ISOUrls[i] = url
}
}
if c.TargetExtension == "" {
c.TargetExtension = "iso"
}
c.TargetExtension = strings.ToLower(c.TargetExtension)
// Warnings
if c.ISOChecksumType == "none" {
warnings = append(warnings,
"A checksum type of 'none' was specified. Since ISO files are so big,\n"+
"a checksum is highly recommended.")
}
return warnings, errs
}
func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) error {
errNotFound := fmt.Errorf("No checksum for %q found at: %s", filepath.Base(c.ISOUrls[0]), c.ISOChecksumURL)
for {
line, err := rd.ReadString('\n')
if err != nil && line == "" {
break
}
parts := strings.Fields(line)
if len(parts) < 2 {
continue
}
if strings.ToLower(parts[0]) == c.ISOChecksumType {
// BSD-style checksum
if parts[1] == fmt.Sprintf("(%s)", filepath.Base(c.ISOUrls[0])) {
c.ISOChecksum = parts[3]
return nil
}
} else {
// Standard checksum
if parts[1][0] == '*' {
// Binary mode
parts[1] = parts[1][1:]
}
if parts[1] == filepath.Base(c.ISOUrls[0]) {
c.ISOChecksum = parts[0]
return nil
}
}
}
return errNotFound
}