Commit 1c74f891 authored by Pascal Bach's avatar Pascal Bach
Browse files

Let only one instance run against a specific mirror directory

parent c08dd6f5
[root]
name = "git-mirror"
version = "0.3.0"
version = "0.4.0"
dependencies = [
"chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.25.0 (registry+https://github.com/rust-lang/crates.io-index)",
"fs2 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper 0.10.12 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper-native-tls 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper-rustls 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_yaml 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"slug 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"stderrlog 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"threadpool 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"threadpool 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -148,6 +149,16 @@ name = "foreign-types"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "fs2"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "futures"
version = "0.1.14"
......@@ -471,12 +482,12 @@ dependencies = [
[[package]]
name = "serde"
version = "1.0.9"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde_derive"
version = "1.0.9"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -501,7 +512,7 @@ dependencies = [
"dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -511,7 +522,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
"yaml-rust 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
......@@ -584,7 +595,7 @@ dependencies = [
[[package]]
name = "threadpool"
version = "1.3.2"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
......@@ -729,6 +740,7 @@ dependencies = [
"checksum dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "80c8b71fd71146990a9742fc06dcbbde19161a267e0ad4e572c35162f4578c90"
"checksum either 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18785c1ba806c258137c937e44ada9ee7e69a37e3c72077542cd2f069d78562a"
"checksum foreign-types 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3e4056b9bd47f8ac5ba12be771f77a0dae796d1bbaaf5fd0b9c2d38b69b8a29d"
"checksum fs2 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9ab76cfd2aaa59b7bf6688ad9ba15bbae64bff97f04ea02144cfd3443e5c2866"
"checksum futures 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "4b63a4792d4f8f686defe3b39b92127fea6344de5d38202b2ee5a11bbbf29d6a"
"checksum gcc 0.3.51 (registry+https://github.com/rust-lang/crates.io-index)" = "120d07f202dcc3f72859422563522b66fe6463a4c513df062874daad05f85f0a"
"checksum httparse 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "af2f2dd97457e8fb1ae7c5a420db346af389926e36f43768b96f101546b04a07"
......@@ -769,8 +781,8 @@ dependencies = [
"checksum secur32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3f412dfa83308d893101dd59c10d6fda8283465976c28c287c5c855bf8d216bc"
"checksum security-framework 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "42ddf098d78d0b64564b23ee6345d07573e7d10e52ad86875d89ddf5f8378a02"
"checksum security-framework-sys 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "5bacdada57ea62022500c457c8571c17dfb5e6240b7c8eac5916ffa8c7138a55"
"checksum serde 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "6a7c6b751a2e8d5df57a5ff71b5b4fc8aaee9ee28ff1341d640dd130bb5f4f7a"
"checksum serde_derive 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "2f6ca58905ebd3c3b285a8a6d4f3ac92b92c0d7951d5649b1bdd212549c06639"
"checksum serde 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)" = "433d7d9f8530d5a939ad5e0e72a6243d2e42a24804f70bf592c679363dcacb2f"
"checksum serde_derive 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)" = "7b707cf0d4cab852084f573058def08879bb467fda89d99052485e7d00edd624"
"checksum serde_derive_internals 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "37aee4e0da52d801acfbc0cc219eb1eda7142112339726e427926a6f6ee65d3a"
"checksum serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "48b04779552e92037212c3615370f6bd57a40ebba7f20e554ff9f55e41a69a7b"
"checksum serde_yaml 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "49d983aa39d2884a4b422bb11bb38f4f48fa05186e17469bc31e47d01e381111"
......@@ -782,7 +794,7 @@ dependencies = [
"checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6"
"checksum term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2b6b55df3198cc93372e85dd2ed817f0e38ce8cc0f22eb32391bfad9c4bf209"
"checksum textwrap 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f86300c3e7416ee233abd7cda890c492007a3980f941f79185c753a701257167"
"checksum threadpool 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "59f6d3eff89920113dac9db44dde461d71d01e88a5b57b258a0466c32b5d7fe1"
"checksum threadpool 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f95666101aad0c8f42d0533b696aee6de31d2ee6fc5081aa839b9b3b8440c3a"
"checksum time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d788d3aa77bc0ef3e9621256885555368b47bd495c13dd2e7413c89f845520"
"checksum traitobject 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079"
"checksum typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887"
......
[package]
name = "git-mirror"
version = "0.3.0"
version = "0.4.0"
authors = ["Pascal Bach <pascal.bach@nextrem.ch>"]
[features]
......@@ -20,3 +20,4 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
serde_yaml = "0.7.0"
fs2 = "0.4.2"
......@@ -8,6 +8,10 @@ use std::process::{Command, Stdio, exit};
use std::fs;
use std::path::Path;
// File locking
extern crate fs2;
use fs2::FileExt;
// Used for error and debug logging
#[macro_use]
extern crate log;
......@@ -70,17 +74,6 @@ pub fn mirror_repo(
))
})?;
fs::DirBuilder::new()
.recursive(true)
.create(&mirror_dir)
.or_else(|e| {
Err(format!(
"Unable to create mirror dir: {:?} ({})",
mirror_dir,
e
))
})?;
if origin_dir.is_dir() {
info!("Local Update for {}", origin);
......@@ -203,14 +196,25 @@ fn run_sync_task(v: Vec<Mirror>, worker_count: usize, mirror_dir: &str, dry_run:
let tx = tx.clone();
let mirror_dir = mirror_dir.to_owned().clone();
pool.execute(move || {
println!("START [{}]: {} -> {}", Local::now(), x.origin, x.destination);
println!(
"START [{}]: {} -> {}",
Local::now(),
x.origin,
x.destination
);
let c = match mirror_repo(mirror_dir, &x.origin, &x.destination, dry_run) {
Ok(c) => {
println!("OK [{}]: {} -> {}", Local::now(), x.origin, x.destination);
c
}
Err(e) => {
println!("FAIL [{}]: {} -> {} ({})", Local::now(), x.origin, x.destination, e);
println!(
"FAIL [{}]: {} -> {} ({})",
Local::now(),
x.origin,
x.destination,
e
);
error!(
"Unable to sync repo {} -> {} ({})",
x.origin,
......@@ -225,13 +229,43 @@ fn run_sync_task(v: Vec<Mirror>, worker_count: usize, mirror_dir: &str, dry_run:
n += 1;
}
println!("DONE [{2}]: {0}/{1}", rx.iter().take(n).fold(0, |a, b| a + b), n, Local::now());
println!(
"DONE [{2}]: {0}/{1}",
rx.iter().take(n).fold(0, |a, b| a + b),
n,
Local::now()
);
}
pub fn do_mirror(provider: &Provider, worker_count: usize, mirror_dir: &str, dry_run: bool) {
// Make sure the mirror directory exists
trace!("Create mirror directory at {:?}", mirror_dir);
fs::create_dir_all(&mirror_dir).unwrap_or_else(|e| {
error!("Unable to create mirror dir: {:?} ({})", &mirror_dir, e);
exit(2);
});
// Check that only one instance is running against a mirror directory
let lockfile_path = Path::new(mirror_dir).join("git-mirror.lock");
let lockfile = fs::File::create(&lockfile_path).unwrap_or_else(|e| {
error!("Unable to open lockfile: {:?} ({})", &lockfile_path, e);
exit(3);
});
lockfile.try_lock_exclusive().unwrap_or_else(|e| {
error!(
"Another instance is already running aginst the same mirror directory: {:?} ({})",
&mirror_dir,
e
);
exit(4);
});
trace!("Aquired lockfile: {:?}", &lockfile);
// Get the list of repos to sync from gitlabsss
let v = provider.get_mirror_repos().unwrap_or_else(|e| {
error!("Unable to get mirror repos ({})", e);
......@@ -239,6 +273,7 @@ pub fn do_mirror(provider: &Provider, worker_count: usize, mirror_dir: &str, dry
});
run_sync_task(v, worker_count, mirror_dir, dry_run);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment