Add support for local packages cache

This commit is contained in:
Daniele Rondina
2020-02-01 19:35:30 +01:00
parent 524bbf990e
commit 78b5963a4f
5 changed files with 75 additions and 53 deletions

View File

@@ -48,6 +48,7 @@ type LuetSystemConfig struct {
DatabaseEngine string `yaml:"database_engine" mapstructure:"database_engine"`
DatabasePath string `yaml:"database_path" mapstructure:"database_path"`
Rootfs string `yaml:"rootfs" mapstructure:"rootfs"`
PkgsCachePath string `yaml:"pkgs_cache_path" mapstructure:"pkgs_cache_path"`
}
type LuetRepository struct {
@@ -148,6 +149,7 @@ func GenDefault(viper *v.Viper) {
viper.SetDefault("system.database_engine", "boltdb")
viper.SetDefault("system.database_path", "/var/cache/luet")
viper.SetDefault("system.rootfs", "/")
viper.SetDefault("system.pkgs_cache_path", "packages")
viper.SetDefault("repos_confdir", []string{"/etc/luet/repos.conf.d"})
viper.SetDefault("cache_repositories", []string{})
@@ -225,8 +227,9 @@ func (c *LuetSystemConfig) String() string {
system:
database_engine: %s
database_path: %s
pkgs_cache_path: %s
rootfs: %s`,
c.DatabaseEngine, c.DatabasePath, c.Rootfs)
c.DatabaseEngine, c.DatabasePath, c.PkgsCachePath, c.Rootfs)
return ans
}

View File

@@ -42,3 +42,18 @@ func GetSystemRepoDatabaseDirPath() string {
}
return dbpath
}
func GetSystemPkgsCacheDirPath() (ans string) {
cachepath := "packages"
if config.LuetCfg.GetSystem().PkgsCachePath != "" {
cachepath = config.LuetCfg.GetSystem().PkgsCachePath
}
if filepath.IsAbs(cachepath) {
ans = cachepath
} else {
ans = filepath.Join(GetSystemRepoDatabaseDirPath(), cachepath)
}
return
}

View File

@@ -39,21 +39,22 @@ func NewHttpClient(r RepoData) *HttpClient {
}
func (c *HttpClient) DownloadArtifact(artifact compiler.Artifact) (compiler.Artifact, error) {
var file *os.File = nil
var u *url.URL = nil
artifactName := path.Base(artifact.GetPath())
cacheFile := filepath.Join(helpers.GetSystemPkgsCacheDirPath(), artifactName)
ok := false
// Check if file is already in cache
if helpers.Exists(cacheFile) {
Info("Use artifact", artifactName, "from cache.")
} else {
temp, err := ioutil.TempDir(os.TempDir(), "tree")
if err != nil {
return nil, err
}
file, err = ioutil.TempFile(temp, "HttpClient")
if err != nil {
return nil, err
}
defer os.RemoveAll(temp)
for _, uri := range c.RepoData.Urls {
Info("Downloading artifact", artifactName, "from", uri)
@@ -69,12 +70,11 @@ func (c *HttpClient) DownloadArtifact(artifact compiler.Artifact) (compiler.Arti
continue
}
Debug("Copying file ", filepath.Join(temp, artifactName), "to", file.Name())
err = helpers.CopyFile(filepath.Join(temp, artifactName), file.Name())
Debug("Copying file ", filepath.Join(temp, artifactName), "to", cacheFile)
err = helpers.CopyFile(filepath.Join(temp, artifactName), cacheFile)
if err != nil {
continue
}
defer os.RemoveAll(filepath.Join(temp, artifactName))
ok = true
break
@@ -83,9 +83,10 @@ func (c *HttpClient) DownloadArtifact(artifact compiler.Artifact) (compiler.Arti
if !ok {
return nil, err
}
}
newart := artifact
newart.SetPath(file.Name())
newart.SetPath(cacheFile)
return newart, nil
}

View File

@@ -37,18 +37,20 @@ func NewLocalClient(r RepoData) *LocalClient {
func (c *LocalClient) DownloadArtifact(artifact compiler.Artifact) (compiler.Artifact, error) {
var err error
var file *os.File = nil
artifactName := path.Base(artifact.GetPath())
cacheFile := filepath.Join(helpers.GetSystemPkgsCacheDirPath(), artifactName)
// Check if file is already in cache
if helpers.Exists(cacheFile) {
Info("Use artifact", artifactName, "from cache.")
} else {
ok := false
for _, uri := range c.RepoData.Urls {
Info("Downloading artifact", artifactName, "from", uri)
file, err = ioutil.TempFile(os.TempDir(), "localclient")
if err != nil {
continue
}
//defer os.Remove(file.Name())
err = helpers.CopyFile(filepath.Join(uri, artifactName), file.Name())
err = helpers.CopyFile(filepath.Join(uri, artifactName), cacheFile)
if err != nil {
continue
}
@@ -59,11 +61,13 @@ func (c *LocalClient) DownloadArtifact(artifact compiler.Artifact) (compiler.Art
if !ok {
return nil, err
}
}
newart := artifact
newart.SetPath(file.Name())
newart.SetPath(cacheFile)
return newart, nil
}
func (c *LocalClient) DownloadFile(name string) (string, error) {
var err error
var file *os.File = nil

View File

@@ -285,7 +285,6 @@ func (l *LuetInstaller) Install(cp []pkg.Package, s *System) error {
func (l *LuetInstaller) installPackage(a ArtifactMatch, s *System) error {
artifact, err := a.Repository.Client().DownloadArtifact(a.Artifact)
defer os.Remove(artifact.GetPath())
err = artifact.Verify()
if err != nil {