From 00fcc3dfdc9407eef9d08f72e5b296872e08d964 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 5 Nov 2015 13:06:18 +0000 Subject: [PATCH] allow to specify checksum via url Signed-off-by: Vasiliy Tolstov --- common/iso_config.go | 102 ++++++++++++++---- .../docs/builders/parallels-iso.html.markdown | 3 +- .../source/docs/builders/qemu.html.markdown | 3 +- .../builders/virtualbox-iso.html.markdown | 3 +- .../docs/builders/vmware-iso.html.markdown | 3 +- 5 files changed, 89 insertions(+), 25 deletions(-) diff --git a/common/iso_config.go b/common/iso_config.go index 6e3cd1f61..f44428f1d 100644 --- a/common/iso_config.go +++ b/common/iso_config.go @@ -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 { diff --git a/website/source/docs/builders/parallels-iso.html.markdown b/website/source/docs/builders/parallels-iso.html.markdown index e243f54e5..f7b914197 100644 --- a/website/source/docs/builders/parallels-iso.html.markdown +++ b/website/source/docs/builders/parallels-iso.html.markdown @@ -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 diff --git a/website/source/docs/builders/qemu.html.markdown b/website/source/docs/builders/qemu.html.markdown index c074e6fac..02f976da2 100644 --- a/website/source/docs/builders/qemu.html.markdown +++ b/website/source/docs/builders/qemu.html.markdown @@ -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 diff --git a/website/source/docs/builders/virtualbox-iso.html.markdown b/website/source/docs/builders/virtualbox-iso.html.markdown index bd46512b2..240857626 100644 --- a/website/source/docs/builders/virtualbox-iso.html.markdown +++ b/website/source/docs/builders/virtualbox-iso.html.markdown @@ -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 diff --git a/website/source/docs/builders/vmware-iso.html.markdown b/website/source/docs/builders/vmware-iso.html.markdown index eccaabd1b..dbb7f2e3c 100644 --- a/website/source/docs/builders/vmware-iso.html.markdown +++ b/website/source/docs/builders/vmware-iso.html.markdown @@ -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