mirror of
https://github.com/mudler/luet.git
synced 2025-06-30 17:22:38 +00:00
* Reduce possibility of circular dependency Just by adding an import for bus to anything in the helper dir, we would run into a circular dependency due to how things are structured. That means that we cannot set any events for unpacking or docker helper pulling an image. This commit tries to work around this by doing several things. - Remove full imports of the helper module by segmentating some modules into their own submodule, like docker or match so just using a small match function doesnt bring the whole module - Removing a simple function to check if a dir exists from importing the full helper module and instead write the function (5 lines) - Using logrus in the bus module instead of logger, which avoids a circular dependency Signed-off-by: Itxaka <igarcia@suse.com> * Add two new events for unpacking an image Both pre and post unpacking an image Signed-off-by: Itxaka <igarcia@suse.com>
120 lines
2.7 KiB
Go
120 lines
2.7 KiB
Go
// 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"
|
|
"path"
|
|
"path/filepath"
|
|
|
|
"github.com/mudler/luet/pkg/compiler/types/artifact"
|
|
"github.com/mudler/luet/pkg/config"
|
|
fileHelper "github.com/mudler/luet/pkg/helpers/file"
|
|
. "github.com/mudler/luet/pkg/logger"
|
|
)
|
|
|
|
type LocalClient struct {
|
|
RepoData RepoData
|
|
}
|
|
|
|
func NewLocalClient(r RepoData) *LocalClient {
|
|
return &LocalClient{RepoData: r}
|
|
}
|
|
|
|
func (c *LocalClient) DownloadArtifact(a *artifact.PackageArtifact) (*artifact.PackageArtifact, error) {
|
|
var err error
|
|
|
|
rootfs := ""
|
|
artifactName := path.Base(a.Path)
|
|
cacheFile := filepath.Join(config.LuetCfg.GetSystem().GetSystemPkgsCacheDirPath(), artifactName)
|
|
|
|
if !config.LuetCfg.ConfigFromHost {
|
|
rootfs, err = config.LuetCfg.GetSystem().GetRootFsAbs()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
// Check if file is already in cache
|
|
if fileHelper.Exists(cacheFile) {
|
|
Debug("Use artifact", artifactName, "from cache.")
|
|
} else {
|
|
ok := false
|
|
for _, uri := range c.RepoData.Urls {
|
|
|
|
uri = filepath.Join(rootfs, uri)
|
|
|
|
Info("Downloading artifact", artifactName, "from", uri)
|
|
|
|
//defer os.Remove(file.Name())
|
|
err = fileHelper.CopyFile(filepath.Join(uri, artifactName), cacheFile)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
ok = true
|
|
break
|
|
}
|
|
|
|
if !ok {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
newart := a
|
|
newart.Path = cacheFile
|
|
return newart, nil
|
|
}
|
|
|
|
func (c *LocalClient) DownloadFile(name string) (string, error) {
|
|
var err error
|
|
var file *os.File = nil
|
|
|
|
rootfs := ""
|
|
|
|
if !config.LuetCfg.ConfigFromHost {
|
|
rootfs, err = config.LuetCfg.GetSystem().GetRootFsAbs()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
ok := false
|
|
for _, uri := range c.RepoData.Urls {
|
|
|
|
uri = filepath.Join(rootfs, uri)
|
|
|
|
Info("Downloading file", name, "from", uri)
|
|
file, err = config.LuetCfg.GetSystem().TempFile("localclient")
|
|
if err != nil {
|
|
continue
|
|
}
|
|
//defer os.Remove(file.Name())
|
|
|
|
err = fileHelper.CopyFile(filepath.Join(uri, name), file.Name())
|
|
if err != nil {
|
|
continue
|
|
}
|
|
ok = true
|
|
break
|
|
}
|
|
|
|
if ok {
|
|
return file.Name(), nil
|
|
}
|
|
|
|
return "", err
|
|
}
|