Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mirror Plugin: Mirror to mirror synchronization #49

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading