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

View File

@@ -42,3 +42,18 @@ func GetSystemRepoDatabaseDirPath() string {
} }
return dbpath 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,53 +39,54 @@ func NewHttpClient(r RepoData) *HttpClient {
} }
func (c *HttpClient) DownloadArtifact(artifact compiler.Artifact) (compiler.Artifact, error) { func (c *HttpClient) DownloadArtifact(artifact compiler.Artifact) (compiler.Artifact, error) {
var file *os.File = nil
var u *url.URL = nil var u *url.URL = nil
artifactName := path.Base(artifact.GetPath()) artifactName := path.Base(artifact.GetPath())
cacheFile := filepath.Join(helpers.GetSystemPkgsCacheDirPath(), artifactName)
ok := false ok := false
temp, err := ioutil.TempDir(os.TempDir(), "tree") // Check if file is already in cache
if err != nil { if helpers.Exists(cacheFile) {
return nil, err Info("Use artifact", artifactName, "from cache.")
} } else {
file, err = ioutil.TempFile(temp, "HttpClient") temp, err := ioutil.TempDir(os.TempDir(), "tree")
if err != nil {
return nil, err
}
for _, uri := range c.RepoData.Urls {
Info("Downloading artifact", artifactName, "from", uri)
u, err = url.Parse(uri)
if err != nil { if err != nil {
continue return nil, err
} }
u.Path = path.Join(u.Path, artifactName) defer os.RemoveAll(temp)
_, err = grab.Get(temp, u.String()) for _, uri := range c.RepoData.Urls {
if err != nil { Info("Downloading artifact", artifactName, "from", uri)
continue
u, err = url.Parse(uri)
if err != nil {
continue
}
u.Path = path.Join(u.Path, artifactName)
_, err = grab.Get(temp, u.String())
if err != nil {
continue
}
Debug("Copying file ", filepath.Join(temp, artifactName), "to", cacheFile)
err = helpers.CopyFile(filepath.Join(temp, artifactName), cacheFile)
if err != nil {
continue
}
ok = true
break
} }
Debug("Copying file ", filepath.Join(temp, artifactName), "to", file.Name()) if !ok {
err = helpers.CopyFile(filepath.Join(temp, artifactName), file.Name()) return nil, err
if err != nil {
continue
} }
defer os.RemoveAll(filepath.Join(temp, artifactName))
ok = true
break
}
if !ok {
return nil, err
} }
newart := artifact newart := artifact
newart.SetPath(file.Name()) newart.SetPath(cacheFile)
return newart, nil return newart, nil
} }

View File

@@ -37,33 +37,37 @@ func NewLocalClient(r RepoData) *LocalClient {
func (c *LocalClient) DownloadArtifact(artifact compiler.Artifact) (compiler.Artifact, error) { func (c *LocalClient) DownloadArtifact(artifact compiler.Artifact) (compiler.Artifact, error) {
var err error var err error
var file *os.File = nil
artifactName := path.Base(artifact.GetPath()) artifactName := path.Base(artifact.GetPath())
ok := false cacheFile := filepath.Join(helpers.GetSystemPkgsCacheDirPath(), artifactName)
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())
if err != nil {
continue
}
ok = true
break
}
if !ok { // Check if file is already in cache
return nil, err 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)
//defer os.Remove(file.Name())
err = helpers.CopyFile(filepath.Join(uri, artifactName), cacheFile)
if err != nil {
continue
}
ok = true
break
}
if !ok {
return nil, err
}
} }
newart := artifact newart := artifact
newart.SetPath(file.Name()) newart.SetPath(cacheFile)
return newart, nil return newart, nil
} }
func (c *LocalClient) DownloadFile(name string) (string, error) { func (c *LocalClient) DownloadFile(name string) (string, error) {
var err error var err error
var file *os.File = nil 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 { func (l *LuetInstaller) installPackage(a ArtifactMatch, s *System) error {
artifact, err := a.Repository.Client().DownloadArtifact(a.Artifact) artifact, err := a.Repository.Client().DownloadArtifact(a.Artifact)
defer os.Remove(artifact.GetPath())
err = artifact.Verify() err = artifact.Verify()
if err != nil { if err != nil {