Skip to content

Commit

Permalink
Move the decision to init repo based on cached content to its own method
Browse files Browse the repository at this point in the history
Signed-off-by: Nassim 'Nass' Eddequiouaq <[email protected]>
  • Loading branch information
n4ss committed Mar 2, 2017
1 parent 923cc15 commit fd3ad27
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
50 changes: 20 additions & 30 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,23 +633,6 @@ func (r *NotaryRepository) Publish() error {
return nil
}

func (r *NotaryRepository) isMetaCached() (bool, error) {
for _, role := range data.BaseRoles {
_, err := r.cache.GetSized(role.String(), store.NoSizeLimit)
if err != nil {
if _, ok := err.(store.ErrMetaNotFound); ok {
continue
}

return false, err
}

return true, nil
}

return false, nil
}

// publish pushes the changes in the given changelist to the remote notary-server
// Conceptually it performs an operation similar to a `git rebase`
func (r *NotaryRepository) publish(cl changelist.Changelist) error {
Expand All @@ -659,20 +642,8 @@ func (r *NotaryRepository) publish(cl changelist.Changelist) error {
// If the remote is not aware of the repo, then this is being published
// for the first time. Try to initialize the repository before publishing.
if _, ok := err.(ErrRepositoryNotExist); ok {
metaCached, err := r.isMetaCached()
err := r.initializeFromCache()
if err != nil {
logrus.Debugf("Unable to verify on-disk cache: %s", err.Error())
return err
}

if metaCached {
err = r.bootstrapRepo()
} else {
err = r.Initialize(nil)
}
if err != nil {
logrus.Debugf("Unable to initialize repository at publish-time: %s",
err.Error())
return err
}

Expand Down Expand Up @@ -884,6 +855,25 @@ func (r *NotaryRepository) bootstrapRepo() error {
return nil
}

func (r *NotaryRepository) initializeFromCache() error {
metaCached, err := isMetaCached(r.cache)
if err != nil {
logrus.Debugf("Unable to verify on-disk cache: %s", err.Error())
return err
}

if metaCached {
err = r.bootstrapRepo()
} else {
err = r.Initialize(nil)
}
if err != nil {
logrus.Debugf("Unable to initialize repository at publish-time: %s",
err.Error())
return err
}
}

// saveMetadata saves contents of r.tufRepo onto the local disk, creating
// signatures as necessary, possibly prompting for passphrases.
func (r *NotaryRepository) saveMetadata(ignoreSnapshot bool) error {
Expand Down
22 changes: 22 additions & 0 deletions client/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/Sirupsen/logrus"
"github.com/docker/distribution/registry/storage/cache"
"github.com/docker/notary/client/changelist"
store "github.com/docker/notary/storage"
"github.com/docker/notary/tuf"
Expand Down Expand Up @@ -268,3 +269,24 @@ func serializeCanonicalRole(tufRepo *tuf.Repo, role data.RoleName, extraSigningK

return json.Marshal(s)
}

func isMetaCached(cache store.MetadataStore) (bool, error) {
if cache == nil {
return false, nil
}

for _, role := range data.BaseRoles {
_, err := cache.GetSized(role.String(), store.NoSizeLimit)
if err != nil {
if _, ok := err.(store.ErrMetaNotFound); ok {
continue
}

return false, err
}

return true, nil
}

return false, nil
}

0 comments on commit fd3ad27

Please sign in to comment.