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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Copyright (c) 2017 Pascal Bach
 *
 * SPDX-License-Identifier:     MIT
 */

pub mod error;
mod git;
pub mod provider;

use std::fs;
use std::fs::File;
use std::path::Path;
use std::path::PathBuf;

// File locking
use fs2::FileExt;

// Used for error and debug logging
use log::{debug, error, info, trace};

// Used to create sane local directory names
use slug::slugify;

// Macros for serde
#[macro_use]
extern crate serde_derive;

// Used to allow multiple paralell sync tasks
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};

// Time handling
use time::OffsetDateTime;

use junit_report::{ReportBuilder, TestCase, TestCaseBuilder, TestSuite, TestSuiteBuilder};

// Monitoring;
use prometheus::register_gauge_vec;
use prometheus::{Encoder, TextEncoder};

use provider::{MirrorError, MirrorResult, Provider};

use git::{Git, GitWrapper};

use error::{GitMirrorError, Result};

pub fn mirror_repo(
    origin: &str,
    destination: &str,
    refspec: &Option<Vec<String>>,
    lfs: bool,
    opts: &MirrorOptions,
) -> Result<()> {
    if opts.dry_run {
        return Ok(());
    }

    let origin_dir = Path::new(&opts.mirror_dir).join(slugify(origin));
    debug!("Using origin dir: {0:?}", origin_dir);

    let git = Git::new(opts.git_executable.clone(), opts.mirror_lfs);

    git.git_version()?;

    if opts.mirror_lfs {
        git.git_lfs_version()?;
    }

    if origin_dir.is_dir() {
        info!("Local Update for {}", origin);

        git.git_update_mirror(origin, &origin_dir, lfs)?;
    } else if !origin_dir.exists() {
        info!("Local Checkout for {}", origin);

        git.git_clone_mirror(origin, &origin_dir, lfs)?;
    } else {
        return Err(GitMirrorError::GenericError(format!(
            "Local origin dir is a file: {origin_dir:?}"
        )));
    }

    info!("Push to destination {}", destination);

    git.git_push_mirror(destination, &origin_dir, refspec, lfs)?;

    if opts.remove_workrepo {
        fs::remove_dir_all(&origin_dir).map_err(|e| {
            GitMirrorError::GenericError(format!(
                "Unable to delete working repository: {} because of error: {}",
                &origin_dir.to_string_lossy(),
                e
            ))
        })?;
    }

    Ok(())
}

fn run_sync_task(v: &[MirrorResult], label: &str, opts: &MirrorOptions) -> TestSuite {
    // Give the work to the worker pool
    rayon::ThreadPoolBuilder::new()
        .num_threads(opts.worker_count)
        .build_global()
        .unwrap();

    let proj_total =
        register_gauge_vec!("git_mirror_total", "Total projects", &["mirror"]).unwrap();
    let proj_skip =
        register_gauge_vec!("git_mirror_skip", "Skipped projects", &["mirror"]).unwrap();
    let proj_fail = register_gauge_vec!("git_mirror_fail", "Failed projects", &["mirror"]).unwrap();
    let proj_ok = register_gauge_vec!("git_mirror_ok", "OK projects", &["mirror"]).unwrap();
    let proj_start = register_gauge_vec!(
        "git_mirror_project_start",
        "Start of project mirror as unix timestamp",
        &["origin", "destination", "mirror"]
    )
    .unwrap();
    let proj_end = register_gauge_vec!(
        "git_mirror_project_end",
        "End of project mirror as unix timestamp",
        &["origin", "destination", "mirror"]
    )
    .unwrap();

    let total = v.len();
    let results = v
        .par_iter()
        .enumerate()
        .map(|(i, x)| {
            proj_total.with_label_values(&[label]).inc();
            let start = OffsetDateTime::now_utc();
            match x {
                Ok(x) => {
                    let name = format!("{} -> {}", x.origin, x.destination);
                    let proj_fail = proj_fail.clone();
                    let proj_ok = proj_ok.clone();
                    let proj_start = proj_start.clone();
                    let proj_end = proj_end.clone();
                    let label = label.to_string();
                    println!(
                        "START {}/{} [{}]: {}",
                        i,
                        total,
                        OffsetDateTime::now_utc(),
                        name
                    );
                    proj_start
                        .with_label_values(&[&x.origin, &x.destination, &label])
                        .set(OffsetDateTime::now_utc().unix_timestamp() as f64);
                    let refspec = match &x.refspec {
                        Some(r) => {
                            debug!("Using repo specific refspec: {:?}", r);
                            &x.refspec
                        }
                        None => {
                            match opts.refspec.clone() {
                                Some(r) => {
                                    debug!("Using global custom refspec: {:?}", r);
                                }
                                None => {
                                    debug!("Using no custom refspec.");
                                }
                            }
                            &opts.refspec
                        }
                    };
                    trace!("Refspec used: {:?}", refspec);
                    match mirror_repo(&x.origin, &x.destination, refspec, x.lfs, opts) {
                        Ok(_) => {
                            println!(
                                "END(OK) {}/{} [{}]: {}",
                                i,
                                total,
                                OffsetDateTime::now_utc(),
                                name
                            );
                            proj_end
                                .with_label_values(&[&x.origin, &x.destination, &label])
                                .set(OffsetDateTime::now_utc().unix_timestamp() as f64);
                            proj_ok.with_label_values(&[&label]).inc();
                            TestCaseBuilder::success(&name, OffsetDateTime::now_utc() - start)
                                .build()
                        }
                        Err(e) => {
                            println!(
                                "END(FAIL) {}/{} [{}]: {} ({})",
                                i,
                                total,
                                OffsetDateTime::now_utc(),
                                name,
                                e
                            );
                            proj_end
                                .with_label_values(&[&x.origin, &x.destination, &label])
                                .set(OffsetDateTime::now_utc().unix_timestamp() as f64);
                            proj_fail.with_label_values(&[&label]).inc();
                            error!("Unable to sync repo {} ({})", name, e);
                            TestCaseBuilder::error(
                                &name,
                                OffsetDateTime::now_utc() - start,
                                "sync error",
                                &format!("{e:?}"),
                            )
                            .build()
                        }
                    }
                }
                Err(e) => {
                    proj_skip.with_label_values(&[label]).inc();
                    let duration = OffsetDateTime::now_utc() - start;

                    match e {
                        MirrorError::Description(d, se) => {
                            error!("Error parsing YAML: {}, Error: {:?}", d, se);
                            TestCaseBuilder::error("", duration, "parse error", &format!("{e:?}"))
                                .build()
                        }
                        MirrorError::Skip(url) => {
                            println!(
                                "SKIP {}/{} [{}]: {}",
                                i,
                                total,
                                OffsetDateTime::now_utc(),
                                url
                            );
                            TestCaseBuilder::skipped(url).build()
                        }
                    }
                }
            }
        })
        .collect::<Vec<TestCase>>();

    let success = results.iter().filter(|x| x.is_success()).count();
    let ts = TestSuiteBuilder::new("Sync Job")
        .add_testcases(results)
        .build();
    println!(
        "DONE [{2}]: {0}/{1}",
        success,
        total,
        OffsetDateTime::now_utc()
    );
    ts
}

pub struct MirrorOptions {
    pub mirror_dir: PathBuf,
    pub dry_run: bool,
    pub metrics_file: Option<PathBuf>,
    pub junit_file: Option<PathBuf>,
    pub worker_count: usize,
    pub git_executable: String,
    pub refspec: Option<Vec<String>>,
    pub remove_workrepo: bool,
    pub fail_on_sync_error: bool,
    pub mirror_lfs: bool,
}

pub fn do_mirror(provider: Box<dyn Provider>, opts: &MirrorOptions) -> Result<()> {
    let start_time = register_gauge_vec!(
        "git_mirror_start_time",
        "Start time of the sync as unix timestamp",
        &["mirror"]
    )
    .unwrap();
    let end_time = register_gauge_vec!(
        "git_mirror_end_time",
        "End time of the sync as unix timestamp",
        &["mirror"]
    )
    .unwrap();

    // Make sure the mirror directory exists
    trace!("Create mirror directory at {:?}", opts.mirror_dir);
    fs::create_dir_all(&opts.mirror_dir).map_err(|e| {
        GitMirrorError::GenericError(format!(
            "Unable to create mirror dir: {:?} ({})",
            &opts.mirror_dir, e
        ))
    })?;

    // Check that only one instance is running against a mirror directory
    let lockfile_path = opts.mirror_dir.join("git-mirror.lock");
    let lockfile = fs::File::create(&lockfile_path).map_err(|e| {
        GitMirrorError::GenericError(format!(
            "Unable to open lockfile: {:?} ({})",
            &lockfile_path, e
        ))
    })?;

    lockfile.try_lock_exclusive().map_err(|e| {
        GitMirrorError::GenericError(format!(
            "Another instance is already running against the same mirror directory: {:?} ({})",
            &opts.mirror_dir, e
        ))
    })?;

    trace!("Aquired lockfile: {:?}", &lockfile);

    // Get the list of repos to sync from gitlabsss
    let v = provider.get_mirror_repos().map_err(|e| -> GitMirrorError {
        GitMirrorError::GenericError(format!("Unable to get mirror repos ({e})"))
    })?;

    start_time
        .with_label_values(&[&provider.get_label()])
        .set(OffsetDateTime::now_utc().unix_timestamp() as f64);

    let ts = run_sync_task(&v, &provider.get_label(), opts);

    end_time
        .with_label_values(&[&provider.get_label()])
        .set(OffsetDateTime::now_utc().unix_timestamp() as f64);

    match opts.metrics_file {
        Some(ref f) => write_metrics(f),
        None => trace!("Skipping metrics file creation"),
    };

    // Check if any tasks failed
    let error_count = ts.errors() + ts.failures();

    match opts.junit_file {
        Some(ref f) => write_junit_report(f, ts),
        None => trace!("Skipping junit report"),
    }

    if opts.fail_on_sync_error && error_count > 0 {
        Err(GitMirrorError::SyncError(error_count))
    } else {
        Ok(())
    }
}

fn write_metrics(f: &Path) {
    let mut file = File::create(f).unwrap();
    let encoder = TextEncoder::new();
    let metric_familys = prometheus::gather();
    encoder.encode(&metric_familys, &mut file).unwrap();
}

fn write_junit_report(f: &Path, ts: TestSuite) {
    let report = ReportBuilder::default().add_testsuite(ts).build();
    let mut file = File::create(f).unwrap();
    report.write_xml(&mut file).unwrap();
}