2019-11-26 17:05:41 +00:00
|
|
|
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2020-02-02 23:58:55 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2019-11-26 17:05:41 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2020-11-18 23:56:28 +00:00
|
|
|
"time"
|
2019-11-26 17:05:41 +00:00
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
"github.com/mudler/luet/pkg/compiler/types/artifact"
|
2019-11-26 17:05:41 +00:00
|
|
|
. "github.com/mudler/luet/pkg/logger"
|
|
|
|
|
2020-02-12 09:20:07 +00:00
|
|
|
"github.com/mudler/luet/pkg/config"
|
2020-02-12 10:05:13 +00:00
|
|
|
"github.com/mudler/luet/pkg/helpers"
|
2019-11-26 17:05:41 +00:00
|
|
|
|
|
|
|
"github.com/cavaliercoder/grab"
|
2020-11-20 17:12:23 +00:00
|
|
|
|
|
|
|
"github.com/schollz/progressbar/v3"
|
2019-11-26 17:05:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HttpClient struct {
|
|
|
|
RepoData RepoData
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHttpClient(r RepoData) *HttpClient {
|
|
|
|
return &HttpClient{RepoData: r}
|
|
|
|
}
|
|
|
|
|
2020-02-02 23:58:55 +00:00
|
|
|
func (c *HttpClient) PrepareReq(dst, url string) (*grab.Request, error) {
|
|
|
|
|
|
|
|
req, err := grab.NewRequest(dst, url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if val, ok := c.RepoData.Authentication["token"]; ok {
|
|
|
|
req.HTTPRequest.Header.Set("Authorization", "token "+val)
|
|
|
|
} else if val, ok := c.RepoData.Authentication["basic"]; ok {
|
|
|
|
req.HTTPRequest.Header.Set("Authorization", "Basic "+val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return req, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func Round(input float64) float64 {
|
|
|
|
if input < 0 {
|
|
|
|
return math.Ceil(input - 0.5)
|
|
|
|
}
|
|
|
|
return math.Floor(input + 0.5)
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
func (c *HttpClient) DownloadArtifact(a *artifact.PackageArtifact) (*artifact.PackageArtifact, error) {
|
2019-12-30 21:51:51 +00:00
|
|
|
var u *url.URL = nil
|
2020-02-02 23:58:55 +00:00
|
|
|
var err error
|
|
|
|
var req *grab.Request
|
|
|
|
var temp string
|
2019-12-30 21:51:51 +00:00
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
artifactName := path.Base(a.Path)
|
2020-02-12 09:20:07 +00:00
|
|
|
cacheFile := filepath.Join(config.LuetCfg.GetSystem().GetSystemPkgsCacheDirPath(), artifactName)
|
2019-12-30 21:51:51 +00:00
|
|
|
ok := false
|
2019-11-26 17:05:41 +00:00
|
|
|
|
2020-02-01 18:35:30 +00:00
|
|
|
// Check if file is already in cache
|
|
|
|
if helpers.Exists(cacheFile) {
|
2021-01-20 11:31:36 +00:00
|
|
|
Debug("Use artifact", artifactName, "from cache.")
|
2020-02-01 18:35:30 +00:00
|
|
|
} else {
|
2019-12-30 21:51:51 +00:00
|
|
|
|
2020-04-30 18:29:28 +00:00
|
|
|
temp, err = config.LuetCfg.GetSystem().TempDir("tree")
|
2019-12-30 21:51:51 +00:00
|
|
|
if err != nil {
|
2020-02-01 18:35:30 +00:00
|
|
|
return nil, err
|
2019-12-30 21:51:51 +00:00
|
|
|
}
|
2020-02-01 18:35:30 +00:00
|
|
|
defer os.RemoveAll(temp)
|
|
|
|
|
2020-02-02 23:58:55 +00:00
|
|
|
client := grab.NewClient()
|
|
|
|
|
2020-02-01 18:35:30 +00:00
|
|
|
for _, uri := range c.RepoData.Urls {
|
2020-12-27 19:21:05 +00:00
|
|
|
Debug("Downloading artifact", artifactName, "from", uri)
|
2020-02-01 18:35:30 +00:00
|
|
|
|
|
|
|
u, err = url.Parse(uri)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
u.Path = path.Join(u.Path, artifactName)
|
|
|
|
|
2020-02-02 23:58:55 +00:00
|
|
|
req, err = c.PrepareReq(temp, u.String())
|
2020-02-01 18:35:30 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-02 23:58:55 +00:00
|
|
|
resp := client.Do(req)
|
2020-11-18 23:56:28 +00:00
|
|
|
|
2020-11-20 17:12:23 +00:00
|
|
|
bar := progressbar.NewOptions64(
|
|
|
|
resp.Size(),
|
|
|
|
progressbar.OptionSetDescription(
|
|
|
|
fmt.Sprintf("[cyan] %s - [reset]",
|
|
|
|
filepath.Base(resp.Request.HTTPRequest.URL.RequestURI()))),
|
|
|
|
progressbar.OptionSetRenderBlankState(true),
|
|
|
|
progressbar.OptionEnableColorCodes(config.LuetCfg.GetLogging().Color),
|
|
|
|
progressbar.OptionClearOnFinish(),
|
|
|
|
progressbar.OptionShowBytes(true),
|
|
|
|
progressbar.OptionShowCount(),
|
|
|
|
progressbar.OptionSetPredictTime(true),
|
|
|
|
progressbar.OptionFullWidth(),
|
|
|
|
progressbar.OptionSetTheme(progressbar.Theme{
|
|
|
|
Saucer: "[white]=[reset]",
|
|
|
|
SaucerHead: "[white]>[reset]",
|
|
|
|
SaucerPadding: " ",
|
|
|
|
BarStart: "[",
|
|
|
|
BarEnd: "]",
|
|
|
|
}))
|
|
|
|
|
|
|
|
bar.Reset()
|
2020-11-18 23:56:28 +00:00
|
|
|
// start download loop
|
|
|
|
t := time.NewTicker(500 * time.Millisecond)
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
download_loop:
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
2020-11-20 17:12:23 +00:00
|
|
|
bar.Set64(resp.BytesComplete())
|
2020-11-18 23:56:28 +00:00
|
|
|
|
|
|
|
case <-resp.Done:
|
|
|
|
// download is complete
|
|
|
|
break download_loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 23:58:55 +00:00
|
|
|
if err = resp.Err(); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-20 17:12:23 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
Info("\nDownloaded", artifactName, "of",
|
2020-02-02 23:58:55 +00:00
|
|
|
fmt.Sprintf("%.2f", (float64(resp.BytesComplete())/1000)/1000), "MB (",
|
|
|
|
fmt.Sprintf("%.2f", (float64(resp.BytesPerSecond())/1024)/1024), "MiB/s )")
|
|
|
|
|
2020-11-20 17:12:23 +00:00
|
|
|
Debug("\nCopying file ", filepath.Join(temp, artifactName), "to", cacheFile)
|
2020-02-01 18:35:30 +00:00
|
|
|
err = helpers.CopyFile(filepath.Join(temp, artifactName), cacheFile)
|
|
|
|
|
2020-11-20 17:12:23 +00:00
|
|
|
bar.Finish()
|
2020-02-01 18:35:30 +00:00
|
|
|
ok = true
|
|
|
|
break
|
2019-12-30 21:51:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 18:35:30 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, err
|
2019-12-30 21:51:51 +00:00
|
|
|
}
|
2019-11-26 17:05:41 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
newart := a
|
|
|
|
newart.Path = cacheFile
|
2019-12-30 15:33:00 +00:00
|
|
|
return newart, nil
|
2019-11-26 17:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HttpClient) DownloadFile(name string) (string, error) {
|
2019-12-30 21:51:51 +00:00
|
|
|
var file *os.File = nil
|
|
|
|
var u *url.URL = nil
|
2020-02-02 23:58:55 +00:00
|
|
|
var err error
|
|
|
|
var req *grab.Request
|
|
|
|
var temp string
|
|
|
|
|
2019-12-30 21:51:51 +00:00
|
|
|
ok := false
|
|
|
|
|
2020-04-30 18:29:28 +00:00
|
|
|
temp, err = config.LuetCfg.GetSystem().TempDir("tree")
|
2019-11-26 17:05:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-02-02 23:58:55 +00:00
|
|
|
client := grab.NewClient()
|
|
|
|
|
2019-12-30 21:51:51 +00:00
|
|
|
for _, uri := range c.RepoData.Urls {
|
|
|
|
|
2020-04-30 18:29:28 +00:00
|
|
|
file, err = config.LuetCfg.GetSystem().TempFile("HttpClient")
|
2019-12-30 21:51:51 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-02 23:58:55 +00:00
|
|
|
|
2019-12-30 21:51:51 +00:00
|
|
|
u, err = url.Parse(uri)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
u.Path = path.Join(u.Path, name)
|
|
|
|
|
2020-12-27 19:21:05 +00:00
|
|
|
Debug("Downloading", u.String())
|
2019-12-30 21:51:51 +00:00
|
|
|
|
2020-02-02 23:58:55 +00:00
|
|
|
req, err = c.PrepareReq(temp, u.String())
|
2019-12-30 21:51:51 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-02 23:58:55 +00:00
|
|
|
resp := client.Do(req)
|
|
|
|
if err = resp.Err(); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
Info("Downloaded", filepath.Base(resp.Filename), "of",
|
|
|
|
fmt.Sprintf("%.2f", (float64(resp.BytesComplete())/1000)/1000), "MB (",
|
|
|
|
fmt.Sprintf("%.2f", (float64(resp.BytesPerSecond())/1024)/1024), "MiB/s )")
|
|
|
|
|
2019-12-30 21:51:51 +00:00
|
|
|
err = helpers.CopyFile(filepath.Join(temp, name), file.Name())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
2019-11-26 17:05:41 +00:00
|
|
|
|
2019-12-30 21:51:51 +00:00
|
|
|
if !ok {
|
2019-11-26 17:05:41 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.Name(), err
|
|
|
|
}
|