Skip to content

Commit

Permalink
Mirror Plugin: Mirror to mirror synchronization
Browse files Browse the repository at this point in the history
Includes new api call to sync with a configuration supplied to the api
at sync time
  • Loading branch information
ikaneshiro committed Mar 29, 2024
1 parent 08d12a1 commit be08142
Show file tree
Hide file tree
Showing 10 changed files with 738 additions and 73 deletions.
7 changes: 7 additions & 0 deletions internal/plugins/mirror/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func (p *Plugin) SyncRepository(ctx context.Context, repository string, wait boo
return p.repositoryManager.Get(ctx, repository).SyncRepository(ctx, wait)
}

func (p *Plugin) SyncRepositoryWithConfig(ctx context.Context, repository string, mirrorConfigs []apiv1.MirrorConfig, webConfig *apiv1.WebConfig, wait bool) (err error) {
if err := checkRepository(repository); err != nil {
return err
}
return p.repositoryManager.Get(ctx, repository).SyncRepositoryWithConfig(ctx, mirrorConfigs, webConfig, wait)
}

func (p *Plugin) GenerateRepository(ctx context.Context, repository string) (err error) {
if err := checkRepository(repository); err != nil {
return err
Expand Down
61 changes: 61 additions & 0 deletions internal/plugins/mirror/pkg/mirrorrepository/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,53 @@ func (h *Handler) SyncRepository(_ context.Context, wait bool) (err error) {
return nil
}

func (h *Handler) SyncRepositoryWithConfig(_ context.Context, mirrorConfigs []apiv1.MirrorConfig, webConfig *apiv1.WebConfig, wait bool) (err error) {
if !h.Started() {
return werror.Wrap(gcode.ErrUnavailable, err)
} else if !h.getMirror() {
return werror.Wrap(gcode.ErrFailedPrecondition, errors.New("repository not setup as a mirror"))
}

// Set mirror configs if supplied.
if mirrorConfigs != nil {
if err := h.setMirrorConfigs(mirrorConfigs); err != nil {
return werror.Wrap(gcode.ErrInternal, err)
}
}

// Set web config if supplied.
if webConfig != nil {
if err := h.setWebConfig(webConfig); err != nil {
return werror.Wrap(gcode.ErrInternal, err)
}
}

if h.delete.Load() {
return werror.Wrap(gcode.ErrAlreadyExists, fmt.Errorf("repository %s is being deleted", h.Repository))
} else if h.syncing.Swap(true) {
return werror.Wrap(gcode.ErrAlreadyExists, errors.New("a repository sync is already running"))
}

var waitErrCh chan error

if wait {
waitErrCh = make(chan error, 1)
}

select {
case h.syncCh <- waitErrCh:
if waitErrCh != nil {
if err := <-waitErrCh; err != nil {
return werror.Wrap(gcode.ErrInternal, fmt.Errorf("synchronization failed: %w", err))
}
}
default:
return werror.Wrap(gcode.ErrUnavailable, errors.New("something goes wrong"))
}

return nil
}

func (h *Handler) GenerateRepository(_ context.Context) (err error) {
if !h.Started() {
return werror.Wrap(gcode.ErrUnavailable, err)
Expand Down Expand Up @@ -535,3 +582,17 @@ func toRepositoryFileAPI(file *mirrordb.RepositoryFile) *apiv1.RepositoryFile {
ConfigID: file.ConfigID,
}
}

func toRepositoryFileDB(file *apiv1.RepositoryFile) *mirrordb.RepositoryFile {
return &mirrordb.RepositoryFile{
Tag: file.Tag,
Name: file.Name,
Reference: file.Reference,
Parent: file.Parent,
Link: file.Link,
ModifiedTime: utils.StringToTime(file.ModifiedTime),
Mode: file.Mode,
Size: file.Size,
ConfigID: file.ConfigID,
}
}
Loading

0 comments on commit be08142

Please sign in to comment.