diff --git a/cmd/install.go b/cmd/install.go index 175d96dd..2fc8bac6 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -22,6 +22,7 @@ import ( installer "github.com/mudler/luet/pkg/installer" . "github.com/mudler/luet/pkg/config" + "github.com/mudler/luet/pkg/helpers" . "github.com/mudler/luet/pkg/logger" pkg "github.com/mudler/luet/pkg/package" @@ -62,31 +63,22 @@ var installCmd = &cobra.Command{ } // This shouldn't be necessary, but we need to unmarshal the repositories to a concrete struct, thus we need to port them back to the Repositories type - synced := installer.Repositories{} + repos := installer.Repositories{} for _, repo := range LuetCfg.SystemRepositories { if !repo.Enable { continue } - - toSync := installer.NewSystemRepository(&repo) - s, err := toSync.Sync() - if err != nil { - Fatal("Error: " + err.Error()) - } - synced = append(synced, s) + repo := installer.NewSystemRepository(&repo) + repos = append(repos, repo) } inst := installer.NewLuetInstaller(LuetCfg.GetGeneral().Concurrency) - inst.Repositories(synced) + inst.Repositories(repos) + inst.SyncRepositories() if LuetCfg.GetSystem().DatabaseEngine == "boltdb" { - os.MkdirAll( - filepath.Join(LuetCfg.GetSystem().Rootfs, LuetCfg.GetSystem().DatabasePath), - os.ModePerm, - ) systemDB = pkg.NewBoltDatabase( - filepath.Join(LuetCfg.GetSystem().Rootfs, - filepath.Join(LuetCfg.GetSystem().DatabasePath, "luet.db"))) + filepath.Join(helpers.GetSystemRepoDatabaseDirPath(), "luet.db")) } else { systemDB = pkg.NewInMemoryDatabase(true) } diff --git a/pkg/helpers/repository.go b/pkg/helpers/repository.go new file mode 100644 index 00000000..0162d3bb --- /dev/null +++ b/pkg/helpers/repository.go @@ -0,0 +1,37 @@ +// Copyright © 2019 Ettore Di Giacinto +// Daniele Rondina +// +// 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 . + +package helpers + +import ( + "os" + "path/filepath" + + "github.com/mudler/luet/pkg/config" +) + +func GetRepoDatabaseDirPath(name string) string { + dbpath := filepath.Join(config.LuetCfg.GetSystem().Rootfs, config.LuetCfg.GetSystem().DatabasePath) + dbpath = filepath.Join(dbpath, "repos/"+name) + os.MkdirAll(dbpath, os.ModePerm) + return dbpath +} + +func GetSystemRepoDatabaseDirPath() string { + dbpath := filepath.Join(config.LuetCfg.GetSystem().Rootfs, config.LuetCfg.GetSystem().DatabasePath) + os.MkdirAll(dbpath, os.ModePerm) + return dbpath +} diff --git a/pkg/installer/client/http.go b/pkg/installer/client/http.go index 1068294e..c7cf630a 100644 --- a/pkg/installer/client/http.go +++ b/pkg/installer/client/http.go @@ -69,6 +69,7 @@ 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()) if err != nil { continue diff --git a/pkg/installer/installer.go b/pkg/installer/installer.go index b44837bc..1ddc498a 100644 --- a/pkg/installer/installer.go +++ b/pkg/installer/installer.go @@ -91,26 +91,15 @@ func NewLuetInstaller(concurrency int) Installer { } func (l *LuetInstaller) Upgrade(s *System) error { - Spinner(32) - defer SpinnerStop() - syncedRepos := Repositories{} - for _, r := range l.PackageRepositories { - repo, err := r.Sync() - if err != nil { - return errors.Wrap(err, "Failed syncing repository: "+r.GetName()) - } - syncedRepos = append(syncedRepos, repo) - } - // compute what to install and from where - sort.Sort(syncedRepos) + sort.Sort(l.PackageRepositories) // First match packages against repositories by priority // matches := syncedRepos.PackageMatches(p) + allRepos := pkg.NewInMemoryDatabase(false) + l.PackageRepositories.SyncDatabase(allRepos) // compute a "big" world - allRepos := pkg.NewInMemoryDatabase(false) - syncedRepos.SyncDatabase(allRepos) solv := solver.NewSolver(s.Database, allRepos, pkg.NewInMemoryDatabase(false)) uninstall, solution, err := solv.Upgrade() if err != nil { @@ -134,8 +123,27 @@ func (l *LuetInstaller) Upgrade(s *System) error { return l.Install(toInstall, s) } -func (l *LuetInstaller) Install(cp []pkg.Package, s *System) error { +func (l *LuetInstaller) SyncRepositories() error { + Spinner(32) + defer SpinnerStop() + syncedRepos := Repositories{} + for _, r := range l.PackageRepositories { + repo, err := r.Sync() + if err != nil { + return errors.Wrap(err, "Failed syncing repository: "+r.GetName()) + } + syncedRepos = append(syncedRepos, repo) + } + // compute what to install and from where + sort.Sort(syncedRepos) + + l.PackageRepositories = syncedRepos + + return nil +} + +func (l *LuetInstaller) Install(cp []pkg.Package, s *System) error { var p []pkg.Package // Check if the package is installed first @@ -156,29 +164,14 @@ func (l *LuetInstaller) Install(cp []pkg.Package, s *System) error { Warning("No package to install, bailing out with no errors") return nil } - // First get metas from all repos (and decodes trees) - Spinner(32) - defer SpinnerStop() - syncedRepos := Repositories{} - for _, r := range l.PackageRepositories { - repo, err := r.Sync() - if err != nil { - return errors.Wrap(err, "Failed syncing repository: "+r.GetName()) - } - syncedRepos = append(syncedRepos, repo) - } - - // compute what to install and from where - sort.Sort(syncedRepos) - // First match packages against repositories by priority // matches := syncedRepos.PackageMatches(p) // compute a "big" world allRepos := pkg.NewInMemoryDatabase(false) - syncedRepos.SyncDatabase(allRepos) + l.PackageRepositories.SyncDatabase(allRepos) solv := solver.NewSolver(s.Database, allRepos, pkg.NewInMemoryDatabase(false)) solution, err := solv.Install(p) @@ -189,7 +182,7 @@ func (l *LuetInstaller) Install(cp []pkg.Package, s *System) error { toInstall := map[string]ArtifactMatch{} for _, assertion := range solution { if assertion.Value { - matches := syncedRepos.PackageMatches([]pkg.Package{assertion.Package}) + matches := l.PackageRepositories.PackageMatches([]pkg.Package{assertion.Package}) if len(matches) != 1 { return errors.New("Failed matching solutions against repository - where are definitions coming from?!") } @@ -284,8 +277,6 @@ func (l *LuetInstaller) Install(cp []pkg.Package, s *System) error { func (l *LuetInstaller) installPackage(a ArtifactMatch, s *System) error { - Info("Installing", a.Package.GetName()) - artifact, err := a.Repository.Client().DownloadArtifact(a.Artifact) defer os.Remove(artifact.GetPath()) @@ -296,7 +287,7 @@ func (l *LuetInstaller) installPackage(a ArtifactMatch, s *System) error { files, err := artifact.FileList() if err != nil { - return errors.Wrap(err, "Could not get file list") + return errors.Wrap(err, "Could not open package archive") } err = artifact.Unpack(s.Target, true) diff --git a/pkg/installer/interface.go b/pkg/installer/interface.go index 9da8b2c5..9899bb86 100644 --- a/pkg/installer/interface.go +++ b/pkg/installer/interface.go @@ -27,6 +27,7 @@ type Installer interface { Uninstall(pkg.Package, *System) error Upgrade(s *System) error Repositories([]Repository) + SyncRepositories() error } type Client interface { diff --git a/pkg/installer/repository.go b/pkg/installer/repository.go index db550432..350d0ed2 100644 --- a/pkg/installer/repository.go +++ b/pkg/installer/repository.go @@ -233,15 +233,8 @@ func (r *LuetSystemRepository) Sync() (Repository, error) { var repo Repository if config.LuetCfg.GetSystem().DatabaseEngine == "boltdb" { - os.MkdirAll( - filepath.Join(config.LuetCfg.GetSystem().Rootfs, config.LuetCfg.GetSystem().DatabasePath), - os.ModePerm, - ) - repo, err = NewLuetSystemRepositoryFromYaml( - dat, - pkg.NewBoltDatabase( - filepath.Join(config.LuetCfg.GetSystem().Rootfs, - filepath.Join(config.LuetCfg.GetSystem().DatabasePath, "luet.db"))), + repo, err = NewLuetSystemRepositoryFromYaml(dat, + pkg.NewBoltDatabase(filepath.Join(helpers.GetRepoDatabaseDirPath(r.Name), "luet.db")), ) } else { repo, err = NewLuetSystemRepositoryFromYaml(dat, pkg.NewInMemoryDatabase(false)) @@ -277,13 +270,7 @@ func (r *LuetSystemRepository) Sync() (Repository, error) { var systemDB pkg.PackageDatabase = nil if config.LuetCfg.GetSystem().DatabaseEngine == "boltdb" { - os.MkdirAll( - filepath.Join(config.LuetCfg.GetSystem().Rootfs, config.LuetCfg.GetSystem().DatabasePath), - os.ModePerm, - ) - systemDB = pkg.NewBoltDatabase( - filepath.Join(config.LuetCfg.GetSystem().Rootfs, - filepath.Join(config.LuetCfg.GetSystem().DatabasePath, "luet.db"))) + systemDB = pkg.NewBoltDatabase(filepath.Join(helpers.GetSystemRepoDatabaseDirPath(), "luet.db")) } else { systemDB = pkg.NewInMemoryDatabase(false) }