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 {
|
2018-01-09 21:08:06 -05:00
|
|
|
url, err := ValidatedURL(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 {
|
2017-07-26 15:37:00 -04:00
|
|
|
u, err := url.Parse(c.ISOUrls[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-05 18:32:39 -05:00
|
|
|
|
|
|
|
checksumurl, err := url.Parse(c.ISOChecksumURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-13 12:21:04 -04:00
|
|
|
absPath, err := filepath.Abs(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to generate absolute path from provided iso_url: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
relpath, err := filepath.Rel(filepath.Dir(checksumurl.Path), absPath)
|
2017-11-05 18:32:39 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-07-26 15:37:00 -04:00
|
|
|
filename := filepath.Base(u.Path)
|
|
|
|
|
2018-07-13 12:14:59 -04:00
|
|
|
errNotFound := fmt.Errorf("No checksum for %q, %q or %q found at: %s",
|
|
|
|
filename, relpath, u.Path, 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
|
|
|
|
}
|
2018-07-13 12:21:04 -04:00
|
|
|
options := []string{filename, relpath, "./" + relpath, absPath}
|
2016-02-08 09:51:43 -05:00
|
|
|
if strings.ToLower(parts[0]) == c.ISOChecksumType {
|
|
|
|
// BSD-style checksum
|
2018-07-13 12:14:59 -04:00
|
|
|
for _, match := range options {
|
|
|
|
if parts[1] == fmt.Sprintf("(%s)", match) {
|
|
|
|
c.ISOChecksum = parts[3]
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-08 09:51:43 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Standard checksum
|
|
|
|
if parts[1][0] == '*' {
|
|
|
|
// Binary mode
|
|
|
|
parts[1] = parts[1][1:]
|
|
|
|
}
|
2018-07-13 12:14:59 -04:00
|
|
|
for _, match := range options {
|
|
|
|
if parts[1] == match {
|
|
|
|
c.ISOChecksum = parts[0]
|
|
|
|
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
|
|
|
}
|