*: Download to specific directory
Signed-off-by: Thomas Hipp <thomas.hipp@canonical.com>
This commit is contained in:
parent
fb00879244
commit
7bf831db43
@ -18,12 +18,20 @@ import (
|
||||
|
||||
// DownloadHash downloads a file. If a checksum file is provided, it will try and
|
||||
// match the hash.
|
||||
func DownloadHash(file, checksum string, hashFunc hash.Hash) error {
|
||||
func DownloadHash(def DefinitionImage, file, checksum string, hashFunc hash.Hash) (string, error) {
|
||||
var (
|
||||
client http.Client
|
||||
hash string
|
||||
err error
|
||||
)
|
||||
targetDir := filepath.Join(os.TempDir(), fmt.Sprintf("%s-%s-%s", def.Distribution, def.Release, def.ArchitectureMapped))
|
||||
targetDir = strings.Replace(targetDir, " ", "", -1)
|
||||
targetDir = strings.ToLower(targetDir)
|
||||
|
||||
err = os.MkdirAll(targetDir, 0755)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if checksum != "" {
|
||||
if hashFunc != nil {
|
||||
@ -35,19 +43,19 @@ func DownloadHash(file, checksum string, hashFunc hash.Hash) error {
|
||||
hashLen = hashFunc.Size() * 2
|
||||
}
|
||||
|
||||
hash, err = downloadChecksum(checksum, file, hashFunc, hashLen)
|
||||
hash, err = downloadChecksum(targetDir, checksum, file, hashFunc, hashLen)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error while downloading checksum: %s", err)
|
||||
return "", fmt.Errorf("Error while downloading checksum: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
imagePath := filepath.Join(os.TempDir(), filepath.Base(file))
|
||||
imagePath := filepath.Join(targetDir, filepath.Base(file))
|
||||
|
||||
stat, err := os.Stat(imagePath)
|
||||
if err == nil && stat.Size() > 0 {
|
||||
image, err := os.Open(imagePath)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
defer image.Close()
|
||||
|
||||
@ -58,21 +66,21 @@ func DownloadHash(file, checksum string, hashFunc hash.Hash) error {
|
||||
|
||||
_, err = io.Copy(hashFunc, image)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
result := fmt.Sprintf("%x", hashFunc.Sum(nil))
|
||||
if result != hash {
|
||||
return fmt.Errorf("Hash mismatch for %s: %s != %s", imagePath, result, hash)
|
||||
return "", fmt.Errorf("Hash mismatch for %s: %s != %s", imagePath, result, hash)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return targetDir, nil
|
||||
}
|
||||
|
||||
image, err := os.Create(imagePath)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
defer image.Close()
|
||||
|
||||
@ -86,19 +94,19 @@ func DownloadHash(file, checksum string, hashFunc hash.Hash) error {
|
||||
_, err = lxd.DownloadFileHash(&client, "", progress, nil, imagePath, file, hash, hashFunc, image)
|
||||
if err != nil {
|
||||
if checksum == "" && strings.HasPrefix(err.Error(), "Hash mismatch") {
|
||||
return nil
|
||||
return targetDir, nil
|
||||
}
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
|
||||
return nil
|
||||
return targetDir, nil
|
||||
}
|
||||
|
||||
// downloadChecksum downloads or opens URL, and matches fname against the
|
||||
// checksums inside of the downloaded or opened file.
|
||||
func downloadChecksum(URL string, fname string, hashFunc hash.Hash, hashLen int) (string, error) {
|
||||
func downloadChecksum(targetDir string, URL string, fname string, hashFunc hash.Hash, hashLen int) (string, error) {
|
||||
var (
|
||||
client http.Client
|
||||
tempFile *os.File
|
||||
@ -106,15 +114,15 @@ func downloadChecksum(URL string, fname string, hashFunc hash.Hash, hashLen int)
|
||||
)
|
||||
|
||||
// do not re-download checksum file if it's already present
|
||||
fi, err := os.Stat(filepath.Join(os.TempDir(), URL))
|
||||
fi, err := os.Stat(filepath.Join(targetDir, URL))
|
||||
if err == nil && !fi.IsDir() {
|
||||
tempFile, err = os.Open(filepath.Join(os.TempDir(), URL))
|
||||
tempFile, err = os.Open(filepath.Join(targetDir, URL))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer os.Remove(tempFile.Name())
|
||||
} else {
|
||||
tempFile, err = ioutil.TempFile(os.TempDir(), "hash.")
|
||||
tempFile, err = ioutil.TempFile(targetDir, "hash.")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -62,10 +62,12 @@ func (s *AlpineLinuxHTTP) Run(definition shared.Definition, rootfsDir string) er
|
||||
return errors.New("GPG keys are required if downloading from HTTP")
|
||||
}
|
||||
|
||||
var fpath string
|
||||
|
||||
if definition.Source.SkipVerification {
|
||||
err = shared.DownloadHash(tarball, "", nil)
|
||||
fpath, err = shared.DownloadHash(definition.Image, tarball, "", nil)
|
||||
} else {
|
||||
err = shared.DownloadHash(tarball, tarball+".sha256", sha256.New())
|
||||
fpath, err = shared.DownloadHash(definition.Image, tarball, tarball+".sha256", sha256.New())
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
@ -73,10 +75,10 @@ func (s *AlpineLinuxHTTP) Run(definition shared.Definition, rootfsDir string) er
|
||||
|
||||
// Force gpg checks when using http
|
||||
if !definition.Source.SkipVerification && url.Scheme != "https" {
|
||||
shared.DownloadHash(tarball+".asc", "", nil)
|
||||
shared.DownloadHash(definition.Image, tarball+".asc", "", nil)
|
||||
valid, err := shared.VerifyFile(
|
||||
filepath.Join(os.TempDir(), fname),
|
||||
filepath.Join(os.TempDir(), fname+".asc"),
|
||||
filepath.Join(fpath, fname),
|
||||
filepath.Join(fpath, fname+".asc"),
|
||||
definition.Source.Keys,
|
||||
definition.Source.Keyserver)
|
||||
if err != nil {
|
||||
@ -88,7 +90,7 @@ func (s *AlpineLinuxHTTP) Run(definition shared.Definition, rootfsDir string) er
|
||||
}
|
||||
|
||||
// Unpack
|
||||
err = lxd.Unpack(filepath.Join(os.TempDir(), fname), rootfsDir, false, false, nil)
|
||||
err = lxd.Unpack(filepath.Join(fpath, fname), rootfsDir, false, false, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -63,18 +63,18 @@ func (s *ArchLinuxHTTP) Run(definition shared.Definition, rootfsDir string) erro
|
||||
return errors.New("GPG keys are required if downloading from HTTP")
|
||||
}
|
||||
|
||||
err = shared.DownloadHash(tarball, "", nil)
|
||||
fpath, err := shared.DownloadHash(definition.Image, tarball, "", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Force gpg checks when using http
|
||||
if !definition.Source.SkipVerification && url.Scheme != "https" {
|
||||
shared.DownloadHash(tarball+".sig", "", nil)
|
||||
shared.DownloadHash(definition.Image, tarball+".sig", "", nil)
|
||||
|
||||
valid, err := shared.VerifyFile(
|
||||
filepath.Join(os.TempDir(), fname),
|
||||
filepath.Join(os.TempDir(), fname+".sig"),
|
||||
filepath.Join(fpath, fname),
|
||||
filepath.Join(fpath, fname+".sig"),
|
||||
definition.Source.Keys,
|
||||
definition.Source.Keyserver)
|
||||
if err != nil {
|
||||
@ -86,7 +86,7 @@ func (s *ArchLinuxHTTP) Run(definition shared.Definition, rootfsDir string) erro
|
||||
}
|
||||
|
||||
// Unpack
|
||||
err = lxd.Unpack(filepath.Join(os.TempDir(), fname), rootfsDir, false, false, nil)
|
||||
err = lxd.Unpack(filepath.Join(fpath, fname), rootfsDir, false, false, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -73,8 +73,12 @@ func (s *CentOSHTTP) Run(definition shared.Definition, rootfsDir string) error {
|
||||
checksumFile = "sha256sum.txt.asc"
|
||||
}
|
||||
|
||||
shared.DownloadHash(baseURL+checksumFile, "", nil)
|
||||
valid, err := shared.VerifyFile(filepath.Join(os.TempDir(), checksumFile), "",
|
||||
fpath, err := shared.DownloadHash(definition.Image, baseURL+checksumFile, "", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
valid, err := shared.VerifyFile(filepath.Join(fpath, checksumFile), "",
|
||||
definition.Source.Keys, definition.Source.Keyserver)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -85,16 +89,16 @@ func (s *CentOSHTTP) Run(definition shared.Definition, rootfsDir string) error {
|
||||
}
|
||||
}
|
||||
|
||||
err = shared.DownloadHash(baseURL+s.fname, checksumFile, sha256.New())
|
||||
fpath, err := shared.DownloadHash(definition.Image, baseURL+s.fname, checksumFile, sha256.New())
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error downloading CentOS image: %s", err)
|
||||
}
|
||||
|
||||
if strings.HasSuffix(s.fname, ".raw.xz") || strings.HasSuffix(s.fname, ".raw") {
|
||||
return s.unpackRaw(filepath.Join(os.TempDir(), s.fname), rootfsDir)
|
||||
return s.unpackRaw(filepath.Join(fpath, s.fname), rootfsDir)
|
||||
}
|
||||
|
||||
return s.unpackISO(filepath.Join(os.TempDir(), s.fname), rootfsDir)
|
||||
return s.unpackISO(filepath.Join(fpath, s.fname), rootfsDir)
|
||||
}
|
||||
|
||||
func (s CentOSHTTP) unpackRaw(filePath, rootfsDir string) error {
|
||||
|
@ -41,14 +41,14 @@ func (s *FedoraHTTP) Run(definition shared.Definition, rootfsDir string) error {
|
||||
definition.Image.Release, build, definition.Image.ArchitectureMapped)
|
||||
|
||||
// Download image
|
||||
err = shared.DownloadHash(fmt.Sprintf("%s/%s/%s/images/%s",
|
||||
fpath, err := shared.DownloadHash(definition.Image, fmt.Sprintf("%s/%s/%s/images/%s",
|
||||
baseURL, definition.Image.Release, build, fname), "", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Unpack the base image
|
||||
err = lxd.Unpack(filepath.Join(os.TempDir(), fname), rootfsDir, false, false, nil)
|
||||
err = lxd.Unpack(filepath.Join(fpath, fname), rootfsDir, false, false, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
@ -62,10 +61,12 @@ func (s *GentooHTTP) Run(definition shared.Definition, rootfsDir string) error {
|
||||
return errors.New("GPG keys are required if downloading from HTTP")
|
||||
}
|
||||
|
||||
var fpath string
|
||||
|
||||
if definition.Source.SkipVerification {
|
||||
err = shared.DownloadHash(tarball, "", nil)
|
||||
fpath, err = shared.DownloadHash(definition.Image, tarball, "", nil)
|
||||
} else {
|
||||
err = shared.DownloadHash(tarball, tarball+".DIGESTS", sha512.New())
|
||||
fpath, err = shared.DownloadHash(definition.Image, tarball, tarball+".DIGESTS", sha512.New())
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
@ -73,9 +74,9 @@ func (s *GentooHTTP) Run(definition shared.Definition, rootfsDir string) error {
|
||||
|
||||
// Force gpg checks when using http
|
||||
if !definition.Source.SkipVerification && url.Scheme != "https" {
|
||||
shared.DownloadHash(tarball+".DIGESTS.asc", "", nil)
|
||||
shared.DownloadHash(definition.Image, tarball+".DIGESTS.asc", "", nil)
|
||||
valid, err := shared.VerifyFile(
|
||||
filepath.Join(os.TempDir(), fname+".DIGESTS.asc"),
|
||||
filepath.Join(fpath, fname+".DIGESTS.asc"),
|
||||
"",
|
||||
definition.Source.Keys,
|
||||
definition.Source.Keyserver)
|
||||
@ -88,7 +89,7 @@ func (s *GentooHTTP) Run(definition shared.Definition, rootfsDir string) error {
|
||||
}
|
||||
|
||||
// Unpack
|
||||
err = lxd.Unpack(filepath.Join(os.TempDir(), fname), rootfsDir, false, false, nil)
|
||||
err = lxd.Unpack(filepath.Join(fpath, fname), rootfsDir, false, false, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -38,13 +38,13 @@ func (s *OracleLinuxHTTP) Run(definition shared.Definition, rootfsDir string) er
|
||||
return err
|
||||
}
|
||||
|
||||
err = shared.DownloadHash(fmt.Sprintf("%s/%s/%s/%s", baseURL, latestUpdate, s.architecture, fname),
|
||||
fpath, err := shared.DownloadHash(definition.Image, fmt.Sprintf("%s/%s/%s/%s", baseURL, latestUpdate, s.architecture, fname),
|
||||
"", nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error downloading Oracle Linux image: %s", err)
|
||||
}
|
||||
|
||||
return s.unpackISO(latestUpdate[1:], filepath.Join(os.TempDir(), fname), rootfsDir)
|
||||
return s.unpackISO(latestUpdate[1:], filepath.Join(fpath, fname), rootfsDir)
|
||||
}
|
||||
|
||||
func (s *OracleLinuxHTTP) unpackISO(latestUpdate, filePath, rootfsDir string) error {
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
lxd "github.com/lxc/lxd/shared"
|
||||
@ -30,18 +29,20 @@ func (s *SabayonHTTP) Run(definition shared.Definition, rootfsDir string) error
|
||||
return err
|
||||
}
|
||||
|
||||
var fpath string
|
||||
|
||||
// From sabayon currently we have only MD5 checksum for now.
|
||||
if definition.Source.SkipVerification {
|
||||
err = shared.DownloadHash(tarball, "", nil)
|
||||
fpath, err = shared.DownloadHash(definition.Image, tarball, "", nil)
|
||||
} else {
|
||||
err = shared.DownloadHash(tarball, tarball+".md5", md5.New())
|
||||
fpath, err = shared.DownloadHash(definition.Image, tarball, tarball+".md5", md5.New())
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Unpack
|
||||
err = lxd.Unpack(filepath.Join(os.TempDir(), fname), rootfsDir, false, false, nil)
|
||||
err = lxd.Unpack(filepath.Join(fpath, fname), rootfsDir, false, false, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -50,6 +50,8 @@ func (s *UbuntuHTTP) Run(definition shared.Definition, rootfsDir string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
var fpath string
|
||||
|
||||
checksumFile := ""
|
||||
// Force gpg checks when using http
|
||||
if !definition.Source.SkipVerification && url.Scheme != "https" {
|
||||
@ -58,12 +60,16 @@ func (s *UbuntuHTTP) Run(definition shared.Definition, rootfsDir string) error {
|
||||
}
|
||||
|
||||
checksumFile = baseURL + "SHA256SUMS"
|
||||
shared.DownloadHash(baseURL+"SHA256SUMS.gpg", "", nil)
|
||||
shared.DownloadHash(checksumFile, "", nil)
|
||||
fpath, err = shared.DownloadHash(definition.Image, baseURL+"SHA256SUMS.gpg", "", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
shared.DownloadHash(definition.Image, checksumFile, "", nil)
|
||||
|
||||
valid, err := shared.VerifyFile(
|
||||
filepath.Join(os.TempDir(), "SHA256SUMS"),
|
||||
filepath.Join(os.TempDir(), "SHA256SUMS.gpg"),
|
||||
filepath.Join(fpath, "SHA256SUMS"),
|
||||
filepath.Join(fpath, "SHA256SUMS.gpg"),
|
||||
definition.Source.Keys,
|
||||
definition.Source.Keyserver)
|
||||
if err != nil {
|
||||
@ -74,12 +80,12 @@ func (s *UbuntuHTTP) Run(definition shared.Definition, rootfsDir string) error {
|
||||
}
|
||||
}
|
||||
|
||||
err = shared.DownloadHash(baseURL+s.fname, checksumFile, sha256.New())
|
||||
fpath, err = shared.DownloadHash(definition.Image, baseURL+s.fname, checksumFile, sha256.New())
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error downloading Ubuntu image: %s", err)
|
||||
}
|
||||
|
||||
err = s.unpack(filepath.Join(os.TempDir(), s.fname), rootfsDir)
|
||||
err = s.unpack(filepath.Join(fpath, s.fname), rootfsDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user