1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
use crate::db::MirrorDb;
use crate::discovery::DiscoveredRepo;
use crate::health::GraspServer;
use anyhow::{Context, Result};
use git2::RemoteCallbacks;
use std::path::Path;
pub struct GitMirror {
mirror_dir: std::path::PathBuf,
}
impl GitMirror {
pub fn new(mirror_dir: &Path) -> Self {
Self {
mirror_dir: mirror_dir.to_path_buf(),
}
}
fn repo_path(&self, pubkey: &str, identifier: &str) -> std::path::PathBuf {
self.mirror_dir
.join(pubkey)
.join(format!("{}.git", identifier))
}
pub async fn mirror_repo_to_servers(
&self,
db: &MirrorDb,
repo: &DiscoveredRepo,
target_servers: &[GraspServer],
) -> Result<()> {
if target_servers.is_empty() {
tracing::debug!(
identifier = %repo.identifier,
"no missing servers to mirror to"
);
return Ok(());
}
let pk_hex = repo.pubkey.to_hex();
let repo_path = self.repo_path(&pk_hex, &repo.identifier);
if !repo_path.exists() {
self.clone_bare(&repo_path, &repo.clone_urls)?;
}
for server in target_servers {
let target_url = server.clone_url(&pk_hex, &repo.identifier);
tracing::info!(
identifier = %repo.identifier,
server = %server.domain,
target = %target_url,
"mirroring git data"
);
let repo_id = db.get_all_repos().await.ok().and_then(|repos| {
repos
.iter()
.find(|r| r.pubkey == pk_hex && r.identifier == repo.identifier)
.map(|r| r.id)
});
match self.push_mirror(&repo_path, &target_url) {
Ok(()) => {
tracing::info!(
identifier = %repo.identifier,
server = %server.domain,
"git mirror succeeded"
);
if let Some(id) = repo_id {
let _ = db.mark_git_synced(id, &server.domain).await;
}
}
Err(e) => {
tracing::error!(
identifier = %repo.identifier,
server = %server.domain,
error = %e,
"git mirror failed"
);
if let Some(id) = repo_id {
let _ = db.mark_sync_error(id, &server.domain, &e.to_string()).await;
}
}
}
}
Ok(())
}
fn clone_bare(&self, repo_path: &Path, clone_urls: &[String]) -> Result<()> {
if let Some(parent) = repo_path.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("failed to create {:?}", parent))?;
}
let mut last_error = None;
for url in clone_urls {
if url.is_empty() {
continue;
}
tracing::info!(url = %url, path = ?repo_path, "cloning bare repo");
let mut callbacks = RemoteCallbacks::new();
callbacks.credentials(|_url, _username, _allowed| git2::Cred::default());
let mut fetch_opts = git2::FetchOptions::new();
fetch_opts.remote_callbacks(callbacks);
let mut builder = git2::build::RepoBuilder::new();
builder.bare(true).fetch_options(fetch_opts);
match builder.clone(url, repo_path) {
Ok(_) => {
tracing::info!(url = %url, "bare clone succeeded");
return Ok(());
}
Err(e) => {
tracing::warn!(url = %url, error = %e, "clone failed, trying next URL");
last_error = Some(e);
if repo_path.exists() {
let _ = std::fs::remove_dir_all(repo_path);
}
}
}
}
let err = last_error.unwrap_or_else(|| git2::Error::from_str("no clone URLs available"));
Err(err).with_context(|| format!("all clone attempts failed for {:?}", repo_path))
}
fn push_mirror(&self, repo_path: &Path, target_url: &str) -> Result<()> {
let repo = git2::Repository::open(repo_path)
.with_context(|| format!("failed to open bare repo at {:?}", repo_path))?;
let remote_name = "push_target";
match repo.find_remote(remote_name) {
Ok(_) => {
repo.remote_set_url(remote_name, target_url)?;
}
Err(_) => {
repo.remote(remote_name, target_url)?;
}
}
let mut remote = repo.find_remote(remote_name)?;
let mut callbacks = RemoteCallbacks::new();
callbacks.credentials(|_url, _username, _allowed| git2::Cred::default());
callbacks.push_update_reference(|_refname, status| {
if let Some(s) = status {
tracing::warn!(status = %s, "push rejected");
}
Ok(())
});
let mut push_opts = git2::PushOptions::new();
push_opts.remote_callbacks(callbacks);
let refspecs = ["+refs/*:refs/*"];
remote
.push(&refspecs, Some(&mut push_opts))
.with_context(|| format!("failed to push mirror to {}", target_url))?;
Ok(())
}
}
|