2021-10-20 22:13:02 +00:00
|
|
|
// Copyright © 2019-2021 Ettore Di Giacinto <mudler@gentoo.org>
|
2019-11-22 20:01:38 +00:00
|
|
|
//
|
|
|
|
// 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 (
|
|
|
|
"os"
|
2019-11-23 14:42:05 +00:00
|
|
|
"path"
|
2019-11-22 20:01:38 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
"github.com/mudler/luet/pkg/api/core/types"
|
2021-10-19 15:06:48 +00:00
|
|
|
"github.com/mudler/luet/pkg/api/core/types/artifact"
|
2021-06-01 14:43:31 +00:00
|
|
|
fileHelper "github.com/mudler/luet/pkg/helpers/file"
|
2021-10-19 15:06:48 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-11-22 20:01:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LocalClient struct {
|
2019-11-22 22:12:03 +00:00
|
|
|
RepoData RepoData
|
2021-10-19 15:06:48 +00:00
|
|
|
Cache *artifact.ArtifactCache
|
2021-10-20 22:13:02 +00:00
|
|
|
context *types.Context
|
2019-11-22 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
func NewLocalClient(r RepoData, ctx *types.Context) *LocalClient {
|
2021-10-19 15:06:48 +00:00
|
|
|
return &LocalClient{
|
2021-10-20 22:13:02 +00:00
|
|
|
Cache: artifact.NewCache(ctx.Config.GetSystem().GetSystemPkgsCacheDirPath()),
|
2021-10-19 15:06:48 +00:00
|
|
|
RepoData: r,
|
2021-10-20 22:13:02 +00:00
|
|
|
context: ctx,
|
2021-10-19 15:06:48 +00:00
|
|
|
}
|
2019-11-22 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
func (c *LocalClient) DownloadArtifact(a *artifact.PackageArtifact) (*artifact.PackageArtifact, error) {
|
2019-12-30 21:51:51 +00:00
|
|
|
var err error
|
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
artifactName := path.Base(a.Path)
|
2021-10-19 15:06:48 +00:00
|
|
|
newart := a.ShallowCopy()
|
2020-11-07 23:00:15 +00:00
|
|
|
|
2021-10-19 15:06:48 +00:00
|
|
|
fileName, err := c.Cache.Get(a)
|
2020-02-01 18:35:30 +00:00
|
|
|
// Check if file is already in cache
|
2021-10-19 15:06:48 +00:00
|
|
|
if err == nil {
|
|
|
|
newart.Path = fileName
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Debug("Use artifact", artifactName, "from cache.")
|
2020-02-01 18:35:30 +00:00
|
|
|
} else {
|
2021-10-19 15:06:48 +00:00
|
|
|
d, err := c.DownloadFile(artifactName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed downloading %s", artifactName)
|
2019-12-30 21:51:51 +00:00
|
|
|
}
|
2021-10-21 19:29:24 +00:00
|
|
|
defer os.RemoveAll(d)
|
2019-12-30 21:51:51 +00:00
|
|
|
|
2021-10-19 15:06:48 +00:00
|
|
|
newart.Path = d
|
|
|
|
c.Cache.Put(newart)
|
2021-10-21 19:29:24 +00:00
|
|
|
|
|
|
|
fileName, err := c.Cache.Get(newart)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed getting file from cache %v", newart)
|
|
|
|
}
|
|
|
|
|
|
|
|
newart.Path = fileName
|
2019-11-22 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 15:33:00 +00:00
|
|
|
return newart, nil
|
2019-11-22 20:01:38 +00:00
|
|
|
}
|
2020-02-01 18:35:30 +00:00
|
|
|
|
2019-11-22 20:01:38 +00:00
|
|
|
func (c *LocalClient) DownloadFile(name string) (string, error) {
|
2019-12-30 21:51:51 +00:00
|
|
|
var err error
|
|
|
|
var file *os.File = nil
|
2019-11-22 20:01:38 +00:00
|
|
|
|
2020-11-08 16:06:05 +00:00
|
|
|
rootfs := ""
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
if !c.context.Config.ConfigFromHost {
|
|
|
|
rootfs, err = c.context.Config.GetSystem().GetRootFsAbs()
|
2020-11-08 16:06:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 21:51:51 +00:00
|
|
|
ok := false
|
|
|
|
for _, uri := range c.RepoData.Urls {
|
2020-11-08 16:06:05 +00:00
|
|
|
|
|
|
|
uri = filepath.Join(rootfs, uri)
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Info("Copying file", name, "from", uri)
|
|
|
|
file, err = c.context.Config.GetSystem().TempFile("localclient")
|
2019-12-30 21:51:51 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
//defer os.Remove(file.Name())
|
|
|
|
|
2021-06-01 14:43:31 +00:00
|
|
|
err = fileHelper.CopyFile(filepath.Join(uri, name), file.Name())
|
2019-12-30 21:51:51 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ok = true
|
|
|
|
break
|
2019-11-22 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 21:51:51 +00:00
|
|
|
if ok {
|
|
|
|
return file.Name(), nil
|
|
|
|
}
|
2019-11-22 20:01:38 +00:00
|
|
|
|
2019-12-30 21:51:51 +00:00
|
|
|
return "", err
|
2019-11-22 20:01:38 +00:00
|
|
|
}
|