2015-10-20 19:27:47 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2015-11-05 08:06:18 -05:00
|
|
|
"bufio"
|
2015-10-20 19:27:47 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-11-05 08:06:18 -05:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-02-08 09:51:43 -05:00
|
|
|
"os"
|
2015-11-05 08:06:18 -05:00
|
|
|
"path/filepath"
|
2016-04-01 05:55:02 -04:00
|
|
|
"runtime"
|
2015-10-20 19:27:47 -04:00
|
|
|
"strings"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2015-10-20 19:27:47 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ISOConfig contains configuration for downloading ISO images.
|
|
|
|
type ISOConfig struct {
|
|
|
|
ISOChecksum string `mapstructure:"iso_checksum"`
|
2016-02-08 09:51:43 -05:00
|
|
|
ISOChecksumURL string `mapstructure:"iso_checksum_url"`
|
2015-10-20 19:27:47 -04:00
|
|
|
ISOChecksumType string `mapstructure:"iso_checksum_type"`
|
|
|
|
ISOUrls []string `mapstructure:"iso_urls"`
|
|
|
|
TargetPath string `mapstructure:"iso_target_path"`
|
2016-12-17 05:49:54 -05:00
|
|
|
TargetExtension string `mapstructure:"iso_target_extension"`
|
2015-10-20 19:27:47 -04:00
|
|
|
RawSingleISOUrl string `mapstructure:"iso_url"`
|
|
|
|
}
|
|
|
|
|
2016-10-14 03:56:05 -04:00
|
|
|
func (c *ISOConfig) Prepare(ctx *interpolate.Context) (warnings []string, errs []error) {
|
2015-11-05 08:06:18 -05:00
|
|
|
if c.RawSingleISOUrl == "" && len(c.ISOUrls) == 0 {
|
|
|
|
errs = append(
|
|
|
|
errs, errors.New("One of iso_url or iso_urls must be specified."))
|
2016-10-14 03:56:05 -04:00
|
|
|
return
|
2015-11-05 08:06:18 -05:00
|
|
|
} else if c.RawSingleISOUrl != "" && len(c.ISOUrls) > 0 {
|
|
|
|
errs = append(
|
|
|
|
errs, errors.New("Only one of iso_url or iso_urls may be specified."))
|
2016-10-14 03:56:05 -04:00
|
|
|
return
|
2015-11-05 08:06:18 -05:00
|
|
|
} else if c.RawSingleISOUrl != "" {
|
|
|
|
c.ISOUrls = []string{c.RawSingleISOUrl}
|
|
|
|
}
|
|
|
|
|
2015-10-20 19:27:47 -04:00
|
|
|
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" {
|
2016-02-08 09:51:43 -05:00
|
|
|
if c.ISOChecksum == "" && c.ISOChecksumURL == "" {
|
2015-10-20 19:27:47 -04:00
|
|
|
errs = append(
|
|
|
|
errs, errors.New("Due to large file sizes, an iso_checksum is required"))
|
2015-11-05 08:06:18 -05:00
|
|
|
return warnings, errs
|
2015-10-20 19:27:47 -04:00
|
|
|
} else {
|
2015-11-05 08:06:18 -05:00
|
|
|
if h := HashForType(c.ISOChecksumType); h == nil {
|
|
|
|
errs = append(
|
|
|
|
errs, fmt.Errorf("Unsupported checksum type: %s", c.ISOChecksumType))
|
|
|
|
return warnings, errs
|
|
|
|
}
|
2015-10-20 19:27:47 -04:00
|
|
|
|
2016-02-08 09:51:43 -05:00
|
|
|
// If iso_checksum has no value use iso_checksum_url instead.
|
|
|
|
if c.ISOChecksum == "" {
|
|
|
|
u, err := url.Parse(c.ISOChecksumURL)
|
2015-11-05 08:06:18 -05:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs,
|
2016-02-08 09:51:43 -05:00
|
|
|
fmt.Errorf("Error parsing checksum: %s", err))
|
2015-11-05 08:06:18 -05:00
|
|
|
return warnings, errs
|
|
|
|
}
|
2016-02-08 09:51:43 -05:00
|
|
|
switch u.Scheme {
|
2016-02-09 07:02:42 -05:00
|
|
|
case "http", "https":
|
2016-02-08 09:51:43 -05:00
|
|
|
res, err := http.Get(c.ISOChecksumURL)
|
|
|
|
c.ISOChecksum = ""
|
2015-11-05 08:06:18 -05:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs,
|
2016-02-08 09:51:43 -05:00
|
|
|
fmt.Errorf("Error getting checksum from url: %s", c.ISOChecksumURL))
|
2015-11-05 08:06:18 -05:00
|
|
|
return warnings, errs
|
|
|
|
}
|
2016-02-08 09:51:43 -05:00
|
|
|
defer res.Body.Close()
|
|
|
|
err = c.parseCheckSumFile(bufio.NewReader(res.Body))
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
return warnings, errs
|
2015-11-05 08:06:18 -05:00
|
|
|
}
|
2016-02-08 09:51:43 -05:00
|
|
|
case "file":
|
2016-04-01 05:55:02 -04:00
|
|
|
path := u.Path
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" && len(path) > 2 && path[0] == '/' && path[2] == ':' {
|
|
|
|
path = strings.TrimLeft(path, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(path)
|
2016-02-08 09:51:43 -05:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
return warnings, errs
|
2015-11-05 08:06:18 -05:00
|
|
|
}
|
2016-02-08 09:51:43 -05:00
|
|
|
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
|
2015-11-05 08:06:18 -05:00
|
|
|
}
|
|
|
|
}
|
2015-10-20 19:27:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 08:06:18 -05:00
|
|
|
c.ISOChecksum = strings.ToLower(c.ISOChecksum)
|
2015-10-20 19:27:47 -04:00
|
|
|
|
|
|
|
for i, url := range c.ISOUrls {
|
2016-10-14 03:56:05 -04:00
|
|
|
url, err := DownloadableURL(url)
|
2015-10-20 19:27:47 -04:00
|
|
|
if err != nil {
|
|
|
|
errs = append(
|
|
|
|
errs, fmt.Errorf("Failed to parse iso_url %d: %s", i+1, err))
|
2016-10-14 03:56:05 -04:00
|
|
|
} else {
|
|
|
|
c.ISOUrls[i] = url
|
2015-10-20 19:27:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 05:49:54 -05:00
|
|
|
if c.TargetExtension == "" {
|
|
|
|
c.TargetExtension = "iso"
|
|
|
|
}
|
|
|
|
c.TargetExtension = strings.ToLower(c.TargetExtension)
|
|
|
|
|
2015-10-20 19:27:47 -04:00
|
|
|
// 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
|
|
|
|
}
|
2016-02-08 09:51:43 -05:00
|
|
|
|
|
|
|
func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) error {
|
2016-03-31 03:05:00 -04:00
|
|
|
errNotFound := fmt.Errorf("No checksum for %q found at: %s", filepath.Base(c.ISOUrls[0]), c.ISOChecksumURL)
|
2016-02-08 09:51:43 -05:00
|
|
|
for {
|
|
|
|
line, err := rd.ReadString('\n')
|
2016-03-31 03:05:00 -04:00
|
|
|
if err != nil && line == "" {
|
|
|
|
break
|
2016-02-08 09:51:43 -05:00
|
|
|
}
|
|
|
|
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]
|
2016-03-31 03:05:00 -04:00
|
|
|
return nil
|
2016-02-08 09:51:43 -05:00
|
|
|
}
|
|
|
|
} 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]
|
2016-03-31 03:05:00 -04:00
|
|
|
return nil
|
2016-02-08 09:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 03:05:00 -04:00
|
|
|
return errNotFound
|
2016-02-08 09:51:43 -05:00
|
|
|
}
|