2019-11-22 20:01:38 +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 (
|
|
|
|
"os"
|
2019-11-23 14:42:05 +00:00
|
|
|
"path"
|
2019-11-22 20:01:38 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
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/logger"
|
2019-11-23 14:42:05 +00:00
|
|
|
|
2019-11-22 20:01:38 +00:00
|
|
|
"github.com/mudler/luet/pkg/compiler"
|
|
|
|
"github.com/mudler/luet/pkg/helpers"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LocalClient struct {
|
2019-11-22 22:12:03 +00:00
|
|
|
RepoData RepoData
|
2019-11-22 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 22:12:03 +00:00
|
|
|
func NewLocalClient(r RepoData) *LocalClient {
|
|
|
|
return &LocalClient{RepoData: r}
|
2019-11-22 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LocalClient) DownloadArtifact(artifact compiler.Artifact) (compiler.Artifact, error) {
|
2019-12-30 21:51:51 +00:00
|
|
|
var err error
|
|
|
|
|
2020-11-07 23:00:15 +00:00
|
|
|
rootfs := ""
|
2019-11-23 14:42:05 +00:00
|
|
|
artifactName := path.Base(artifact.GetPath())
|
2020-02-12 09:20:07 +00:00
|
|
|
cacheFile := filepath.Join(config.LuetCfg.GetSystem().GetSystemPkgsCacheDirPath(), artifactName)
|
2020-02-01 18:35:30 +00:00
|
|
|
|
2020-11-07 23:00:15 +00:00
|
|
|
if !config.LuetCfg.ConfigFromHost {
|
|
|
|
rootfs, err = config.LuetCfg.GetSystem().GetRootFsAbs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
ok := false
|
|
|
|
for _, uri := range c.RepoData.Urls {
|
2020-11-07 23:00:15 +00:00
|
|
|
|
|
|
|
uri = filepath.Join(rootfs, uri)
|
|
|
|
|
2020-02-01 18:35:30 +00:00
|
|
|
Info("Downloading artifact", artifactName, "from", uri)
|
|
|
|
|
|
|
|
//defer os.Remove(file.Name())
|
|
|
|
err = helpers.CopyFile(filepath.Join(uri, artifactName), cacheFile)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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-11-22 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 15:33:00 +00:00
|
|
|
newart := artifact
|
2020-02-01 18:35:30 +00:00
|
|
|
newart.SetPath(cacheFile)
|
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 := ""
|
|
|
|
|
|
|
|
if !config.LuetCfg.ConfigFromHost {
|
|
|
|
rootfs, err = config.LuetCfg.GetSystem().GetRootFsAbs()
|
|
|
|
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)
|
|
|
|
|
2019-12-30 21:51:51 +00:00
|
|
|
Info("Downloading file", name, "from", uri)
|
2020-04-30 18:29:28 +00:00
|
|
|
file, err = config.LuetCfg.GetSystem().TempFile("localclient")
|
2019-12-30 21:51:51 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
//defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
err = helpers.CopyFile(filepath.Join(uri, name), file.Name())
|
|
|
|
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
|
|
|
}
|