Work in progress

This commit is contained in:
Patrick Lühne 2019-03-02 00:53:57 +01:00
parent 9a75617bad
commit 42deecf958
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
3 changed files with 70 additions and 61 deletions

View File

@ -8,10 +8,10 @@
tmp_dir=/home/pluehne/tmp tmp_dir=/home/pluehne/tmp
echo "job ID: $JOB_ID" echo "job ID: $JOB_ID"
echo "result repository: $GIT_REMOTE_URL" echo "result repository: $JOB_RESULT_REPOSITORY_URL"
mkdir -p "$tmp_dir" mkdir -p "$tmp_dir"
dir="$tmp_dir"/"$JOB_BRANCH_NAME" dir="$tmp_dir"/"job-$JOB_ID"
if [[ -d $dir ]] if [[ -d $dir ]]
then then
@ -21,15 +21,16 @@ fi
mkdir -p "$dir" mkdir -p "$dir"
pushd "$dir" pushd "$dir"
git clone "$GIT_REMOTE_URL" repository git clone "$JOB_RESULT_REPOSITORY_URL" repository
pushd repository pushd repository
git config user.name "Potassco Bot" git config user.name "Potassco Bot"
git config user.email "bot@potassco.org" git config user.email "bot@potassco.org"
echo "$JOB_BRANCH_NAME" > test-output echo "start of benchmark output of job $JOB_ID" > test-output
sleep 10 sleep 10
start_time=$(date +%s%N) start_time=$(date +%s%N)
echo "end of benchmark output of job $JOB_ID" >> test-output
git add test-output git add test-output
git commit -m "Test update" git commit -m "Test update"
git push git push

View File

@ -14,7 +14,7 @@ fn main()
{ {
pretty_env_logger::init(); pretty_env_logger::init();
let benchmark_repository = BenchmarkRepository::new("git@git.luehne.de:patrick/tplp-planning-benchmark.git", Path::new("cache").to_path_buf(), "git", "Potassco Bot", "bot@potassco.org"); let mut benchmark_repository = BenchmarkRepository::new("gitea@git.luehne.de:patrick/tplp-planning-benchmark.git", Path::new("cache").to_path_buf(), "gitea", "Potassco Bot", "bot@potassco.org");
let content = benchmark_repository.read_file(Path::new("configurations.yml"), "config").unwrap(); let content = benchmark_repository.read_file(Path::new("configurations.yml"), "config").unwrap();
let configurations = &YamlLoader::load_from_str(&content).unwrap()[0]["configurations"]; let configurations = &YamlLoader::load_from_str(&content).unwrap()[0]["configurations"];
@ -45,12 +45,12 @@ fn main()
continue; continue;
} }
let result_repository = benchmark_repository.create_result_repository_for_job(i); let job = benchmark_repository.create_result_repository();
Command::new("sbatch") Command::new("sbatch")
.args(&["/home/pluehne/test-job.sh"]) .args(&["/home/pluehne/test-job.sh"])
.env("GIT_REMOTE_URL", &format!("file://{}", fs::canonicalize(&result_repository).unwrap().display())) .env("JOB_RESULT_REPOSITORY_URL", &format!("file://{}", fs::canonicalize(&job.result_repository_path).unwrap().display()))
.env("JOB_ID", format!("{}", i)) .env("JOB_ID", format!("{}", job.id))
.output() .output()
.expect("Could not execute command"); .expect("Could not execute command");

View File

@ -1,6 +1,7 @@
use git2::{Cred, Error, FetchOptions, Index, IndexEntry, IndexTime, Oid, Progress, PushOptions, RemoteCallbacks, Repository, Signature}; use git2::{Cred, Error, FetchOptions, Index, IndexEntry, IndexTime, Oid, Progress, PushOptions, RemoteCallbacks, Repository, Signature};
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressStyle};
use log::{info, trace}; use log::{info, trace};
use std::collections::HashSet;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::string::String; use std::string::String;
use std::str; use std::str;
@ -22,6 +23,12 @@ pub struct TargetPath
pub destination: PathBuf, pub destination: PathBuf,
} }
pub struct Job
{
pub id: u32,
pub result_repository_path: PathBuf,
}
impl BenchmarkRepository impl BenchmarkRepository
{ {
fn progress_bar_style() -> ProgressStyle fn progress_bar_style() -> ProgressStyle
@ -359,87 +366,88 @@ impl BenchmarkRepository
Some(content.to_owned()) Some(content.to_owned())
} }
pub fn create_result_repository(&mut self) -> PathBuf pub fn create_result_repository(&mut self) -> Job
{ {
let job_id = self.jobs; let job_id = self.jobs;
self.jobs += 1; self.jobs += 1;
let repository_path = self.base_path.join(format!("job-{}", job_id)); let result_repository_path = self.base_path.join(format!("job-{}", job_id));
if repository_path.exists() if result_repository_path.exists()
{ {
match std::fs::remove_dir_all(&repository_path) match std::fs::remove_dir_all(&result_repository_path)
{ {
Ok(_) => (), Ok(_) => (),
Err(error) => panic!("failed to initialize result Git repository for job {} in “{}”: {}", job_id, repository_path.display(), error), Err(error) => panic!("failed to initialize result Git repository for job {} in “{}”: {}", job_id, result_repository_path.display(), error),
}; };
} }
match Repository::init_bare(&repository_path) match Repository::init_bare(&result_repository_path)
{ {
Ok(repository) => repository, Ok(repository) => repository,
Err(error) => panic!("failed to initialize result Git repository for job {} in “{}”: {}", job_id, repository_path.display(), error), Err(error) => panic!("failed to initialize result Git repository for job {} in “{}”: {}", job_id, result_repository_path.display(), error),
}; };
info!("Initialized result Git repository for job {} in “{}”", job_id, repository_path.display()); info!("Initialized result Git repository for job {} in “{}”", job_id, result_repository_path.display());
repository_path Job{id: job_id, result_repository_path}
}
fn tip_commit<'repository>(repository: &'repository git2::Repository, branch_name: &str) -> Result<git2::Commit<'repository>, git2::Error>
{
let tip_reference_name = format!("refs/remotes/origin/{}", branch_name);
let tip_reference = match repository.find_reference(&tip_reference_name)
{
Ok(value) => value,
Err(error) => panic!("Could not find reference “{}”: {}", tip_reference_name, error),
};
tip_reference.peel_to_commit()
} }
pub fn join(&self) pub fn join(&self)
{ {
let mut Vec<git2::Oid> job_commit_ids; let mut active_job_ids = HashSet::new();
for job_id in 0..self.jobs
{
active_job_ids.insert(job_id);
}
loop loop
{ {
for job_id in 0..self.jobs while !active_job_ids.is_empty()
{ {
let repository_path = self.base_path.join(format!("job-{}", job_id)); active_job_ids.retain
(
|job_id|
{
let job_repository_path = self.base_path.join(format!("job-{}", job_id));
let repository = match Repository::open(&repository_path) let job_repository = match Repository::open(&job_repository_path)
{ {
Ok(repository) => repository, Ok(value) => value,
Err(error) => panic!("cannot access result repository for job {}: {}", job_id, error), Err(error) => panic!("cannot access result repository for job {}: {}", job_id, error),
}; };
let tip_reference_name = "refs/remotes/origin/master"; let job_commit = match Self::tip_commit(&job_repository, "master")
let tip_reference = match self.repository.find_reference(&tip_reference_name) {
{ Ok(value) => value,
Ok(value) => value, // Job is not done yet, so skip it until it is
Err(error) => panic!("error reading result repository for job {}: {}", job_id, error), Err(_) => return true,
}; };
let commit = match tip_reference.peel_to_commit() info!("job {} finished", job_id);
{
Ok(value) => value,
Err(error) => panic!("error reading result repository for job {}: {}", job_id, error),
};
let tree = match tip_reference.peel_to_tree() let remote_commit = match Self::tip_commit(&self.repository, "results")
{ {
Ok(value) => value, Ok(value) => value,
Err(error) => panic!("Could not peel reference to tree: {}", error), Err(error) => panic!("could not access tip commit of “results” branch: {}", error),
}; };
let object_id = match tree.get_path(file_path) false
{ }
Ok(tree_entry) => tree_entry.id(), );
Err(_) => return None,
};
let blob = match self.repository.find_blob(object_id)
{
Ok(blob) => blob,
Err(_) => return None,
};
let content = match std::str::from_utf8(blob.content())
{
Ok(content) => content,
Err(error) => panic!("Could not interpret file “{}” as UTF-8: {}", file_path.display(), error),
};
Some(content.to_owned())
} }
//let origin = self.inner.repository.find_remote("origin").expect("could not find remote “origin”"); //let origin = self.inner.repository.find_remote("origin").expect("could not find remote “origin”");