mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-06 12:28:03 +02:00
SSH Push/Pull Mirroring & Migrations
This commit is contained in:
1 parent
9854df3e87
commit
4c8e222242
30 files changed
+1495
-28
No files matched your search
@@ -22,6 +22,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/proxy"
|
||||
repo_module "code.gitea.io/gitea/modules/repository"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
ssh_module "code.gitea.io/gitea/modules/ssh"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
notify_service "code.gitea.io/gitea/services/notify"
|
||||
@@ -254,6 +255,36 @@ func checkRecoverableSyncError(stderrMessage string) bool {
|
||||
}
|
||||
|
||||
// runSync returns true if sync finished without error.
|
||||
// setupSSHAuth sets up SSH authentication for git operations if needed
|
||||
func setupSSHAuth(ctx context.Context, repo *repo_model.Repository, remoteURL string, runOpts *git.RunOpts) (func(), error) {
|
||||
if !IsSSHURL(remoteURL) {
|
||||
return func() {}, nil
|
||||
}
|
||||
|
||||
keypair, err := GetSSHKeypairForURL(ctx, repo, remoteURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get SSH keypair: %w", err)
|
||||
}
|
||||
if keypair == nil {
|
||||
return func() {}, nil
|
||||
}
|
||||
|
||||
privateKey, err := keypair.GetDecryptedPrivateKey()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decrypt private key: %w", err)
|
||||
}
|
||||
|
||||
socketPath, cleanup, err := ssh_module.CreateTemporaryAgent(privateKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create SSH agent: %w", err)
|
||||
}
|
||||
|
||||
runOpts.SSHAuthSock = socketPath
|
||||
|
||||
log.Debug("SSH agent created for repository %s with socket: %s", repo.FullName(), socketPath)
|
||||
return cleanup, nil
|
||||
}
|
||||
|
||||
func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bool) {
|
||||
repoPath := m.Repo.RepoPath()
|
||||
wikiPath := m.Repo.WikiPath()
|
||||
@@ -278,13 +309,23 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
|
||||
|
||||
stdoutBuilder := strings.Builder{}
|
||||
stderrBuilder := strings.Builder{}
|
||||
if err := cmd.Run(ctx, &git.RunOpts{
|
||||
|
||||
runOpts := &git.RunOpts{
|
||||
Timeout: timeout,
|
||||
Dir: repoPath,
|
||||
Env: envs,
|
||||
Stdout: &stdoutBuilder,
|
||||
Stderr: &stderrBuilder,
|
||||
}); err != nil {
|
||||
}
|
||||
|
||||
cleanup, err := setupSSHAuth(ctx, m.Repo, remoteURL.String(), runOpts)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: SSH setup error %v", m.Repo, err)
|
||||
return nil, false
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
if err := cmd.Run(ctx, runOpts); err != nil {
|
||||
stdout := stdoutBuilder.String()
|
||||
stderr := stderrBuilder.String()
|
||||
|
||||
@@ -303,12 +344,21 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
|
||||
// Successful prune - reattempt mirror
|
||||
stderrBuilder.Reset()
|
||||
stdoutBuilder.Reset()
|
||||
if err = cmd.Run(ctx, &git.RunOpts{
|
||||
retryRunOpts := &git.RunOpts{
|
||||
Timeout: timeout,
|
||||
Dir: repoPath,
|
||||
Stdout: &stdoutBuilder,
|
||||
Stderr: &stderrBuilder,
|
||||
}); err != nil {
|
||||
}
|
||||
|
||||
retryCleanup, sshErr := setupSSHAuth(ctx, m.Repo, remoteURL.String(), retryRunOpts)
|
||||
if sshErr != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: SSH setup error on retry %v", m.Repo, sshErr)
|
||||
return nil, false
|
||||
}
|
||||
defer retryCleanup()
|
||||
|
||||
if err = cmd.Run(ctx, retryRunOpts); err != nil {
|
||||
stdout := stdoutBuilder.String()
|
||||
stderr := stderrBuilder.String()
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/proxy"
|
||||
"code.gitea.io/gitea/modules/repository"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
ssh_module "code.gitea.io/gitea/modules/ssh"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
@@ -97,7 +98,10 @@ func SyncPushMirror(ctx context.Context, mirrorID int64) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
_ = m.GetRepository(ctx)
|
||||
if m.GetRepository(ctx) == nil {
|
||||
log.Error("GetRepository [%d]: repository not found", mirrorID)
|
||||
return false
|
||||
}
|
||||
|
||||
m.LastError = ""
|
||||
|
||||
@@ -138,7 +142,7 @@ func runPushSync(ctx context.Context, m *repo_model.PushMirror) error {
|
||||
return errors.New("Unexpected error")
|
||||
}
|
||||
|
||||
if setting.LFS.StartServer {
|
||||
if setting.LFS.StartServer && !IsSSHURL(remoteURL.String()) {
|
||||
log.Trace("SyncMirrors [repo: %-v]: syncing LFS objects...", m.Repo)
|
||||
|
||||
var gitRepo *git.Repository
|
||||
@@ -163,13 +167,48 @@ func runPushSync(ctx context.Context, m *repo_model.PushMirror) error {
|
||||
log.Trace("Pushing %s mirror[%d] remote %s", path, m.ID, m.RemoteName)
|
||||
|
||||
envs := proxy.EnvWithProxy(remoteURL.URL)
|
||||
if err := git.Push(ctx, path, git.PushOptions{
|
||||
|
||||
pushOpts := git.PushOptions{
|
||||
Remote: m.RemoteName,
|
||||
Force: true,
|
||||
Mirror: true,
|
||||
Timeout: timeout,
|
||||
Env: envs,
|
||||
}); err != nil {
|
||||
}
|
||||
|
||||
// Setup SSH authentication
|
||||
if IsSSHURL(remoteURL.String()) {
|
||||
if repo.Owner == nil {
|
||||
if err := repo.LoadOwner(ctx); err != nil {
|
||||
log.Error("Failed to load repository owner for %s: %v", repo.FullName(), err)
|
||||
return util.SanitizeErrorCredentialURLs(err)
|
||||
}
|
||||
}
|
||||
keypair, err := GetSSHKeypairForRepository(ctx, repo)
|
||||
if err != nil {
|
||||
log.Error("Failed to get SSH keypair for repository %s: %v", repo.FullName(), err)
|
||||
return util.SanitizeErrorCredentialURLs(err)
|
||||
}
|
||||
if keypair != nil {
|
||||
privateKey, err := keypair.GetDecryptedPrivateKey()
|
||||
if err != nil {
|
||||
log.Error("Failed to decrypt private key for repository %s: %v", repo.FullName(), err)
|
||||
return util.SanitizeErrorCredentialURLs(err)
|
||||
}
|
||||
|
||||
socketPath, cleanup, err := ssh_module.CreateTemporaryAgent(privateKey)
|
||||
if err != nil {
|
||||
log.Error("Failed to create SSH agent for repository %s: %v", repo.FullName(), err)
|
||||
return util.SanitizeErrorCredentialURLs(err)
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
pushOpts.SSHAuthSock = socketPath
|
||||
log.Debug("SSH agent created for push mirror %s with socket: %s", repo.FullName(), socketPath)
|
||||
}
|
||||
}
|
||||
|
||||
if err := git.Push(ctx, path, pushOpts); err != nil {
|
||||
log.Error("Error pushing %s mirror[%d] remote %s: %v", path, m.ID, m.RemoteName, err)
|
||||
|
||||
return util.SanitizeErrorCredentialURLs(err)
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package mirror
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
ssh_module "code.gitea.io/gitea/modules/ssh"
|
||||
)
|
||||
|
||||
// GetOrCreateSSHKeypairForUser gets or creates an SSH keypair for the given user
|
||||
func GetOrCreateSSHKeypairForUser(ctx context.Context, userID int64) (*repo_model.MirrorSSHKeypair, error) {
|
||||
return ssh_module.GetOrCreateSSHKeypairForUser(ctx, userID)
|
||||
}
|
||||
|
||||
// GetOrCreateSSHKeypairForOrg gets or creates an SSH keypair for the given organization
|
||||
func GetOrCreateSSHKeypairForOrg(ctx context.Context, orgID int64) (*repo_model.MirrorSSHKeypair, error) {
|
||||
return ssh_module.GetOrCreateSSHKeypairForOrg(ctx, orgID)
|
||||
}
|
||||
|
||||
// GetSSHKeypairForRepository gets the appropriate SSH keypair for a repository
|
||||
// If the repository belongs to an organization, it uses the org's keypair,
|
||||
// otherwise it uses the user's keypair
|
||||
func GetSSHKeypairForRepository(ctx context.Context, repo *repo_model.Repository) (*repo_model.MirrorSSHKeypair, error) {
|
||||
return ssh_module.GetSSHKeypairForRepository(ctx, repo)
|
||||
}
|
||||
|
||||
// RegenerateSSHKeypairForUser regenerates the SSH keypair for a user
|
||||
func RegenerateSSHKeypairForUser(ctx context.Context, userID int64) (*repo_model.MirrorSSHKeypair, error) {
|
||||
log.Info("Regenerating SSH keypair for user %d", userID)
|
||||
return repo_model.RegenerateMirrorSSHKeypair(ctx, userID)
|
||||
}
|
||||
|
||||
// RegenerateSSHKeypairForOrg regenerates the SSH keypair for an organization
|
||||
func RegenerateSSHKeypairForOrg(ctx context.Context, orgID int64) (*repo_model.MirrorSSHKeypair, error) {
|
||||
log.Info("Regenerating SSH keypair for organization %d", orgID)
|
||||
return repo_model.RegenerateMirrorSSHKeypair(ctx, orgID)
|
||||
}
|
||||
|
||||
// IsSSHURL checks if a URL is an SSH URL
|
||||
func IsSSHURL(url string) bool {
|
||||
return ssh_module.IsSSHURL(url)
|
||||
}
|
||||
|
||||
// GetSSHKeypairForURL gets the appropriate SSH keypair for a given repository and URL
|
||||
// Returns nil if the URL is not an SSH URL
|
||||
func GetSSHKeypairForURL(ctx context.Context, repo *repo_model.Repository, url string) (*repo_model.MirrorSSHKeypair, error) {
|
||||
return ssh_module.GetSSHKeypairForURL(ctx, repo, url)
|
||||
}
|
||||
Reference in new issue
Block a user