luet/pkg/installer/client/local.go

121 lines
2.7 KiB
Go
Raw Normal View History

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"
"path"
2019-11-22 20:01:38 +00:00
"path/filepath"
"github.com/mudler/luet/pkg/config"
2020-02-12 10:05:13 +00:00
. "github.com/mudler/luet/pkg/logger"
2019-11-22 20:01:38 +00:00
"github.com/mudler/luet/pkg/compiler"
"github.com/mudler/luet/pkg/helpers"
)
type LocalClient struct {
RepoData RepoData
2019-11-22 20:01:38 +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) {
var err error
2020-11-07 23:00:15 +00:00
rootfs := ""
artifactName := path.Base(artifact.GetPath())
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) {
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
}
2020-02-01 18:35:30 +00:00
if !ok {
return nil, err
}
2019-11-22 20:01:38 +00:00
}
newart := artifact
2020-02-01 18:35:30 +00:00
newart.SetPath(cacheFile)
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) {
var err error
var file *os.File = nil
2019-11-22 20:01:38 +00:00
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 = helpers.CopyFile(filepath.Join(uri, name), file.Name())
if err != nil {
continue
}
ok = true
break
2019-11-22 20:01:38 +00:00
}
if ok {
return file.Name(), nil
}
2019-11-22 20:01:38 +00:00
return "", err
2019-11-22 20:01:38 +00:00
}