mirror of
https://github.com/mudler/luet.git
synced 2025-09-11 12:13:32 +00:00
Add support of incremental revision for repos
* on repository creation now if the repository.yaml is already present, the current revision is used. * add --reset-revision option for force reset revision of a specific repository
This commit is contained in:
@@ -38,6 +38,7 @@ var createrepoCmd = &cobra.Command{
|
|||||||
viper.BindPFlag("descr", cmd.Flags().Lookup("descr"))
|
viper.BindPFlag("descr", cmd.Flags().Lookup("descr"))
|
||||||
viper.BindPFlag("urls", cmd.Flags().Lookup("urls"))
|
viper.BindPFlag("urls", cmd.Flags().Lookup("urls"))
|
||||||
viper.BindPFlag("type", cmd.Flags().Lookup("type"))
|
viper.BindPFlag("type", cmd.Flags().Lookup("type"))
|
||||||
|
viper.BindPFlag("reset-revision", cmd.Flags().Lookup("reset-revision"))
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
@@ -48,12 +49,13 @@ var createrepoCmd = &cobra.Command{
|
|||||||
descr := viper.GetString("descr")
|
descr := viper.GetString("descr")
|
||||||
urls := viper.GetStringSlice("urls")
|
urls := viper.GetStringSlice("urls")
|
||||||
t := viper.GetString("type")
|
t := viper.GetString("type")
|
||||||
|
reset := viper.GetBool("reset-revision")
|
||||||
|
|
||||||
repo, err := installer.GenerateRepository(name, descr, t, urls, 1, packages, tree, pkg.NewInMemoryDatabase(false))
|
repo, err := installer.GenerateRepository(name, descr, t, urls, 1, packages, tree, pkg.NewInMemoryDatabase(false))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatal("Error: " + err.Error())
|
Fatal("Error: " + err.Error())
|
||||||
}
|
}
|
||||||
err = repo.Write(dst)
|
err = repo.Write(dst, reset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatal("Error: " + err.Error())
|
Fatal("Error: " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -72,6 +74,7 @@ func init() {
|
|||||||
createrepoCmd.Flags().String("descr", "luet", "Repository description")
|
createrepoCmd.Flags().String("descr", "luet", "Repository description")
|
||||||
createrepoCmd.Flags().StringSlice("urls", []string{}, "Repository URLs")
|
createrepoCmd.Flags().StringSlice("urls", []string{}, "Repository URLs")
|
||||||
createrepoCmd.Flags().String("type", "disk", "Repository type (disk)")
|
createrepoCmd.Flags().String("type", "disk", "Repository type (disk)")
|
||||||
|
createrepoCmd.Flags().Bool("reset-revision", false, "Reset repository revision.")
|
||||||
|
|
||||||
RootCmd.AddCommand(createrepoCmd)
|
RootCmd.AddCommand(createrepoCmd)
|
||||||
}
|
}
|
||||||
|
@@ -47,11 +47,15 @@ type Repository interface {
|
|||||||
GetIndex() compiler.ArtifactIndex
|
GetIndex() compiler.ArtifactIndex
|
||||||
GetTree() tree.Builder
|
GetTree() tree.Builder
|
||||||
SetTree(tree.Builder)
|
SetTree(tree.Builder)
|
||||||
Write(path string) error
|
Write(path string, resetRevision bool) error
|
||||||
Sync() (Repository, error)
|
Sync() (Repository, error)
|
||||||
GetTreePath() string
|
GetTreePath() string
|
||||||
SetTreePath(string)
|
SetTreePath(string)
|
||||||
GetType() string
|
GetType() string
|
||||||
SetType(string)
|
SetType(string)
|
||||||
|
GetRevision() int
|
||||||
|
IncrementRevision()
|
||||||
|
GetLastUpdate() string
|
||||||
|
SetLastUpdate(string)
|
||||||
Client() Client
|
Client() Client
|
||||||
}
|
}
|
||||||
|
@@ -16,6 +16,7 @@
|
|||||||
package installer
|
package installer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -37,6 +38,10 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
REPOSITORY_SPECFILE = "repository.yaml"
|
||||||
|
)
|
||||||
|
|
||||||
type LuetSystemRepository struct {
|
type LuetSystemRepository struct {
|
||||||
*config.LuetRepository
|
*config.LuetRepository
|
||||||
|
|
||||||
@@ -188,21 +193,71 @@ func (r *LuetSystemRepository) GetIndex() compiler.ArtifactIndex {
|
|||||||
func (r *LuetSystemRepository) GetTree() tree.Builder {
|
func (r *LuetSystemRepository) GetTree() tree.Builder {
|
||||||
return r.Tree
|
return r.Tree
|
||||||
}
|
}
|
||||||
func (r *LuetSystemRepository) Write(dst string) error {
|
func (r *LuetSystemRepository) GetRevision() int {
|
||||||
|
return r.Revision
|
||||||
|
}
|
||||||
|
func (r *LuetSystemRepository) GetLastUpdate() string {
|
||||||
|
return r.LastUpdate
|
||||||
|
}
|
||||||
|
func (r *LuetSystemRepository) SetLastUpdate(u string) {
|
||||||
|
r.LastUpdate = u
|
||||||
|
}
|
||||||
|
func (r *LuetSystemRepository) IncrementRevision() {
|
||||||
|
r.Revision++
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *LuetSystemRepository) ReadSpecFile(file string, removeFile bool) (Repository, error) {
|
||||||
|
dat, err := ioutil.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "Error reading file "+file)
|
||||||
|
}
|
||||||
|
if removeFile {
|
||||||
|
defer os.Remove(file)
|
||||||
|
}
|
||||||
|
|
||||||
|
var repo Repository
|
||||||
|
repo, err = NewLuetSystemRepositoryFromYaml(dat, pkg.NewInMemoryDatabase(false))
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "Error reading repository from file "+file)
|
||||||
|
}
|
||||||
|
|
||||||
|
return repo, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *LuetSystemRepository) Write(dst string, resetRevision bool) error {
|
||||||
err := os.MkdirAll(dst, os.ModePerm)
|
err := os.MkdirAll(dst, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
r.Index = r.Index.CleanPath()
|
r.Index = r.Index.CleanPath()
|
||||||
r.LastUpdate = strconv.FormatInt(time.Now().Unix(), 10)
|
r.LastUpdate = strconv.FormatInt(time.Now().Unix(), 10)
|
||||||
|
|
||||||
|
repospec := filepath.Join(dst, REPOSITORY_SPECFILE)
|
||||||
|
if resetRevision {
|
||||||
|
r.Revision = 0
|
||||||
|
} else {
|
||||||
|
if _, err := os.Stat(repospec); !os.IsNotExist(err) {
|
||||||
|
// Read existing file for retrieve revision
|
||||||
|
spec, err := r.ReadSpecFile(repospec, false)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
r.Revision = spec.GetRevision()
|
||||||
|
}
|
||||||
|
}
|
||||||
r.Revision++
|
r.Revision++
|
||||||
|
|
||||||
data, err := yaml.Marshal(r)
|
data, err := yaml.Marshal(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = ioutil.WriteFile(filepath.Join(dst, "repository.yaml"), data, os.ModePerm)
|
|
||||||
|
Info(fmt.Sprintf(
|
||||||
|
"For repository %s creating revision %d and last update %s...",
|
||||||
|
r.Name, r.Revision, r.LastUpdate,
|
||||||
|
))
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(repospec, data, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -238,32 +293,18 @@ func (r *LuetSystemRepository) Sync() (Repository, error) {
|
|||||||
if c == nil {
|
if c == nil {
|
||||||
return nil, errors.New("No client could be generated from repository.")
|
return nil, errors.New("No client could be generated from repository.")
|
||||||
}
|
}
|
||||||
file, err := c.DownloadFile("repository.yaml")
|
file, err := c.DownloadFile(REPOSITORY_SPECFILE)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "While downloading repository.yaml")
|
return nil, errors.Wrap(err, "While downloading "+REPOSITORY_SPECFILE)
|
||||||
}
|
}
|
||||||
dat, err := ioutil.ReadFile(file)
|
repo, err := r.ReadSpecFile(file, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "Error reading file "+file)
|
return nil, err
|
||||||
}
|
|
||||||
defer os.Remove(file)
|
|
||||||
|
|
||||||
var repo Repository
|
|
||||||
// if config.LuetCfg.GetSystem().DatabaseEngine == "boltdb" {
|
|
||||||
// repo, err = NewLuetSystemRepositoryFromYaml(dat,
|
|
||||||
// pkg.NewBoltDatabase(filepath.Join(helpers.GetRepoDatabaseDirPath(r.Name), "luet.db")),
|
|
||||||
// )
|
|
||||||
// } else {
|
|
||||||
repo, err = NewLuetSystemRepositoryFromYaml(dat, pkg.NewInMemoryDatabase(false))
|
|
||||||
// }
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "Error reading repository from file "+file)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
archivetree, err := c.DownloadFile("tree.tar")
|
archivetree, err := c.DownloadFile("tree.tar")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "While downloading repository.yaml")
|
return nil, errors.Wrap(err, "While downloading "+REPOSITORY_SPECFILE)
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(archivetree) // clean up
|
defer os.RemoveAll(archivetree) // clean up
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user