2023-05-13 10:46:43 +00:00
|
|
|
// Copyright 2023 Mikhail Klementev. All rights reserved.
|
|
|
|
// Use of this source code is governed by a AGPLv3 license
|
|
|
|
// (or later) that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-05-15 13:07:56 +00:00
|
|
|
"net/http"
|
2023-05-13 10:46:43 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2023-05-13 11:04:35 +00:00
|
|
|
"path/filepath"
|
2023-05-13 10:46:43 +00:00
|
|
|
|
|
|
|
"github.com/cavaliergopher/grab/v3"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2023-05-15 13:07:56 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/config"
|
2023-05-13 12:33:12 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/fs"
|
2023-05-13 10:46:43 +00:00
|
|
|
)
|
|
|
|
|
2023-05-13 12:08:55 +00:00
|
|
|
var URL = "https://out-of-tree.fra1.digitaloceanspaces.com/1.0.0/"
|
2023-05-13 10:46:43 +00:00
|
|
|
|
|
|
|
func unpackTar(archive, destination string) (err error) {
|
|
|
|
// NOTE: If you're change anything in tar command please check also
|
|
|
|
// BSD tar (or if you're using macOS, do not forget to check GNU Tar)
|
|
|
|
// Also make sure that sparse files are extracting correctly
|
|
|
|
cmd := exec.Command("tar", "-Sxf", archive)
|
|
|
|
cmd.Dir = destination + "/"
|
|
|
|
|
|
|
|
log.Debug().Msgf("%v", cmd)
|
|
|
|
|
|
|
|
rawOutput, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("%v: %s", err, rawOutput)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func DownloadQemuImage(path, file string) (err error) {
|
2023-05-13 12:33:12 +00:00
|
|
|
tmp, err := fs.TempDir()
|
2023-05-13 10:46:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
fileurl, err := url.JoinPath(URL, file+".tar.gz")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-13 12:56:54 +00:00
|
|
|
log.Debug().Msgf("download qemu image from %s", fileurl)
|
|
|
|
|
2023-05-13 10:46:43 +00:00
|
|
|
resp, err := grab.Get(tmp, fileurl)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Cannot download %s. It looks like you need "+
|
|
|
|
"to generate it manually and place it "+
|
|
|
|
"to ~/.out-of-tree/images/. "+
|
|
|
|
"Check documentation for additional information.",
|
|
|
|
fileurl)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = unpackTar(resp.Filename, path)
|
|
|
|
return
|
|
|
|
}
|
2023-05-13 11:04:35 +00:00
|
|
|
|
|
|
|
func DownloadDebianCache(cachePath string) (err error) {
|
2023-05-13 12:33:12 +00:00
|
|
|
tmp, err := fs.TempDir()
|
2023-05-13 11:04:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
file := filepath.Base(cachePath)
|
|
|
|
|
|
|
|
fileurl, err := url.JoinPath(URL, file)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-13 12:56:54 +00:00
|
|
|
log.Debug().Msgf("download debian cache from %s", fileurl)
|
|
|
|
|
2023-05-13 11:04:35 +00:00
|
|
|
resp, err := grab.Get(tmp, fileurl)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-13 12:50:26 +00:00
|
|
|
return os.Rename(resp.Filename, cachePath)
|
2023-05-13 11:04:35 +00:00
|
|
|
}
|
2023-05-15 13:07:56 +00:00
|
|
|
|
|
|
|
func PackageURL(dt config.DistroType, orig string) (found bool, fileurl string) {
|
|
|
|
if dt != config.Debian {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := filepath.Base(orig)
|
|
|
|
|
|
|
|
fileurl, err := url.JoinPath(URL, "packages/debian", filename)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Head(fileurl)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
found = true
|
|
|
|
return
|
|
|
|
}
|