allow to specify checksum via url

Signed-off-by: Vasiliy Tolstov <v.tolstov@selfip.ru>
This commit is contained in:
Vasiliy Tolstov 2015-11-05 13:06:18 +00:00 committed by Rickard von Essen
parent 382e2b4198
commit 00fcc3dfdc
5 changed files with 89 additions and 25 deletions

View File

@ -1,8 +1,12 @@
package common
import (
"bufio"
"errors"
"fmt"
"net/http"
"net/url"
"path/filepath"
"strings"
"github.com/mitchellh/packer/template/interpolate"
@ -23,27 +27,6 @@ func (c *ISOConfig) Prepare(ctx *interpolate.Context) ([]string, []error) {
var err error
var warnings []string
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 == "" {
errs = append(
errs, errors.New("Due to large file sizes, an iso_checksum is required"))
} else {
c.ISOChecksum = strings.ToLower(c.ISOChecksum)
}
if h := HashForType(c.ISOChecksumType); h == nil {
errs = append(
errs,
fmt.Errorf("Unsupported checksum type: %s", c.ISOChecksumType))
}
}
}
if c.RawSingleISOUrl == "" && len(c.ISOUrls) == 0 {
errs = append(
errs, errors.New("One of iso_url or iso_urls must be specified."))
@ -54,6 +37,83 @@ func (c *ISOConfig) Prepare(ctx *interpolate.Context) ([]string, []error) {
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 == "" {
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
}
u, err := url.Parse(c.ISOChecksum)
if err != nil {
errs = append(errs,
fmt.Errorf("Error parsing checksum: %s", err))
return warnings, errs
}
switch u.Scheme {
case "http", "https", "ftp", "ftps":
res, err := http.Get(c.ISOChecksum)
c.ISOChecksum = ""
if err != nil {
errs = append(errs,
fmt.Errorf("Error getting checksum from url: %s", c.ISOChecksum))
return warnings, errs
}
defer res.Body.Close()
rd := bufio.NewReader(res.Body)
for {
line, err := rd.ReadString('\n')
if err != nil {
errs = append(errs,
fmt.Errorf("Error getting checksum from url: %s , %s", c.ISOChecksum, err.Error()))
return warnings, errs
}
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]
break
}
} 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]
break
}
}
}
case "":
break
default:
errs = append(errs,
fmt.Errorf("Error parsing checksum url, scheme not supported: %s", u.Scheme))
return warnings, errs
}
}
}
}
c.ISOChecksum = strings.ToLower(c.ISOChecksum)
for i, url := range c.ISOUrls {
c.ISOUrls[i], err = DownloadableURL(url)
if err != nil {

View File

@ -59,7 +59,8 @@ builder.
- `iso_checksum` (string) - The checksum for the OS ISO file. Because ISO
files are so large, this is required and Packer will verify it prior to
booting a virtual machine with the ISO attached. The type of the checksum is
specified with `iso_checksum_type`, documented below.
specified with `iso_checksum_type`, documented below. Also can be url that
contains checksum file with `iso_checksum_type` type.
- `iso_checksum_type` (string) - The type of the checksum specified in
`iso_checksum`. Valid values are "none", "md5", "sha1", "sha256", or

View File

@ -83,7 +83,8 @@ builder.
- `iso_checksum` (string) - The checksum for the OS ISO file. Because ISO
files are so large, this is required and Packer will verify it prior to
booting a virtual machine with the ISO attached. The type of the checksum is
specified with `iso_checksum_type`, documented below.
specified with `iso_checksum_type`, documented below. Also can be url that
contains checksum file with `iso_checksum_type` type.
- `iso_checksum_type` (string) - The type of the checksum specified in
`iso_checksum`. Valid values are "md5", "sha1", "sha256", or

View File

@ -57,7 +57,8 @@ builder.
- `iso_checksum` (string) - The checksum for the OS ISO file. Because ISO
files are so large, this is required and Packer will verify it prior to
booting a virtual machine with the ISO attached. The type of the checksum is
specified with `iso_checksum_type`, documented below.
specified with `iso_checksum_type`, documented below. Also can be url that
contains checksum file with `iso_checksum_type` type.
- `iso_checksum_type` (string) - The type of the checksum specified in
`iso_checksum`. Valid values are "none", "md5", "sha1", "sha256", or

View File

@ -60,7 +60,8 @@ builder.
- `iso_checksum` (string) - The checksum for the OS ISO file. Because ISO
files are so large, this is required and Packer will verify it prior to
booting a virtual machine with the ISO attached. The type of the checksum is
specified with `iso_checksum_type`, documented below.
specified with `iso_checksum_type`, documented below. Also can be url that
contains checksum file with `iso_checksum_type` type.
- `iso_checksum_type` (string) - The type of the checksum specified in
`iso_checksum`. Valid values are "none", "md5", "sha1", "sha256", or