diff --git a/cmd/create-repo.go b/cmd/create-repo.go index 20631a89..44705dc8 100644 --- a/cmd/create-repo.go +++ b/cmd/create-repo.go @@ -100,6 +100,7 @@ Create a repository from the metadata description defined in the luet.yaml confi helpers.CheckErr(err) force := viper.GetBool("force-push") imagePush := viper.GetBool("push-images") + snapshotID, _ := cmd.Flags().GetString("snapshot-id") opts := []installer.RepositoryOption{ installer.WithSource(viper.GetString("packages")), @@ -163,7 +164,7 @@ Create a repository from the metadata description defined in the luet.yaml confi if metaName != "" { metaFile.SetFileName(metaName) } - + repo.SetSnapshotID(snapshotID) repo.SetRepositoryFile(installer.REPOFILE_TREE_KEY, treeFile) repo.SetRepositoryFile(installer.REPOFILE_META_KEY, metaFile) @@ -197,6 +198,7 @@ func init() { createrepoCmd.Flags().String("meta-compression", "none", "Compression alg: none, gzip, zstd") createrepoCmd.Flags().String("meta-filename", installer.REPOSITORY_METAFILE+".tar", "Repository metadata filename") createrepoCmd.Flags().Bool("from-repositories", false, "Consume the user-defined repositories to pull specfiles from") + createrepoCmd.Flags().String("snapshot-id", "", "Unique ID to use when creating repository snapshots") RootCmd.AddCommand(createrepoCmd) } diff --git a/pkg/installer/repository.go b/pkg/installer/repository.go index 8e00177b..5b5a82b6 100644 --- a/pkg/installer/repository.go +++ b/pkg/installer/repository.go @@ -74,7 +74,7 @@ type LuetSystemRepository struct { PushImages bool `json:"-"` ForcePush bool `json:"-"` - imagePrefix string + imagePrefix, snapshotID string } type LuetSystemRepositoryMetadata struct { @@ -379,7 +379,7 @@ func (r *LuetSystemRepository) SetPriority(n int) { } func (r *LuetSystemRepository) initialize(ctx *types.Context, src string) error { - generator, err := r.getGenerator(ctx) + generator, err := r.getGenerator(ctx, r.snapshotID) if err != nil { return errors.Wrap(err, "while constructing repository generator") } @@ -430,6 +430,11 @@ func (r *LuetSystemRepository) SetType(p string) { r.LuetRepository.Type = p } +// Sets snapshot ID +func (r *LuetSystemRepository) SetSnapshotID(i string) { + r.snapshotID = i +} + func (r *LuetSystemRepository) GetVerify() bool { return r.LuetRepository.Verify } @@ -705,11 +710,15 @@ type RepositoryGenerator interface { Initialize(string, pkg.PackageDatabase) ([]*artifact.PackageArtifact, error) } -func (r *LuetSystemRepository) getGenerator(ctx *types.Context) (RepositoryGenerator, error) { +func (r *LuetSystemRepository) getGenerator(ctx *types.Context, snapshotID string) (RepositoryGenerator, error) { + if snapshotID == "" { + snapshotID = time.Now().Format("20060102150405") + } + var rg RepositoryGenerator switch r.GetType() { case DiskRepositoryType, HttpRepositoryType: - rg = &localRepositoryGenerator{context: ctx} + rg = &localRepositoryGenerator{context: ctx, snapshotID: snapshotID} case DockerRepositoryType: rg = &dockerRepositoryGenerator{ b: r.Backend, @@ -717,6 +726,7 @@ func (r *LuetSystemRepository) getGenerator(ctx *types.Context) (RepositoryGener imagePush: r.PushImages, force: r.ForcePush, context: ctx, + snapshotID: snapshotID, } default: return nil, errors.New("invalid repository type") @@ -726,7 +736,7 @@ func (r *LuetSystemRepository) getGenerator(ctx *types.Context) (RepositoryGener // Write writes the repository metadata to the supplied destination func (r *LuetSystemRepository) Write(ctx *types.Context, dst string, resetRevision, force bool) error { - rg, err := r.getGenerator(ctx) + rg, err := r.getGenerator(ctx, r.snapshotID) if err != nil { return err } diff --git a/pkg/installer/repository_docker.go b/pkg/installer/repository_docker.go index e13cd53a..a2049c02 100644 --- a/pkg/installer/repository_docker.go +++ b/pkg/installer/repository_docker.go @@ -37,10 +37,10 @@ import ( ) type dockerRepositoryGenerator struct { - b compiler.CompilerBackend - imagePrefix string - imagePush, force bool - context *types.Context + b compiler.CompilerBackend + imagePrefix, snapshotID string + imagePush, force bool + context *types.Context } func (l *dockerRepositoryGenerator) Initialize(path string, db pkg.PackageDatabase) ([]*artifact.PackageArtifact, error) { @@ -270,8 +270,7 @@ func (d *dockerRepositoryGenerator) Generate(r *LuetSystemRepository, imagePrefi // Create a named snapshot and push it. // It edits the metadata pointing at the repository files associated with the snapshot // And copies the new files - id := time.Now().Format("20060102150405") - artifacts, snapshotRepoFile, err := r.Snapshot(id, repoTemp) + artifacts, snapshotRepoFile, err := r.Snapshot(d.snapshotID, repoTemp) if err != nil { return errors.Wrap(err, "while creating snapshot") } diff --git a/pkg/installer/repository_local.go b/pkg/installer/repository_local.go index 55f1eb86..355ed190 100644 --- a/pkg/installer/repository_local.go +++ b/pkg/installer/repository_local.go @@ -32,7 +32,10 @@ import ( "github.com/pkg/errors" ) -type localRepositoryGenerator struct{ context *types.Context } +type localRepositoryGenerator struct { + context *types.Context + snapshotID string +} func (l *localRepositoryGenerator) Initialize(path string, db pkg.PackageDatabase) ([]*artifact.PackageArtifact, error) { return buildPackageIndex(l.context, path, db) @@ -124,7 +127,7 @@ func (g *localRepositoryGenerator) Generate(r *LuetSystemRepository, dst string, // Create named snapshot. // It edits the metadata pointing at the repository files associated with the snapshot // And copies the new files - if _, _, err := r.Snapshot(time.Now().Format("20060102150405"), dst); err != nil { + if _, _, err := r.Snapshot(g.snapshotID, dst); err != nil { return errors.Wrap(err, "while creating snapshot") }