From c953e724cc3625b30ec09488b241e841d21daadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Fri, 12 Oct 2018 17:37:08 +0200 Subject: [PATCH] Test running sbatch command --- examples/test.rs | 31 +++++++++++++++++++- src/lib.rs | 75 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 97 insertions(+), 9 deletions(-) diff --git a/examples/test.rs b/examples/test.rs index ffd2dbb..ded0885 100644 --- a/examples/test.rs +++ b/examples/test.rs @@ -5,6 +5,8 @@ extern crate yaml_rust; use benchmark_repository::BenchmarkRepository; use std::path::Path; +use std::process::Command; + use yaml_rust::YamlLoader; fn main() @@ -19,6 +21,8 @@ fn main() let content = benchmark_repository.read_file(Path::new("instances.yml"), "config").unwrap(); let instances = &YamlLoader::load_from_str(&content).unwrap()[0]; + let mut i = 0; + for configuration in configurations.as_vec().unwrap() { for (instance_set_id, instance_set) in instances.as_hash().unwrap() @@ -40,8 +44,33 @@ fn main() continue; } - println!("to do: [{}, {}, {}/{}/{}]", configuration_id, instance_set_id, instance_ipc, instance_domain, instance_number); + let branch_name = benchmark_repository.create_branch_for_job(i); + + Command::new("sbatch") + .args(&["/home/pluehne/test-job.sh"]) + .env("JOB_BRANCH_NAME", &branch_name) + .output() + .expect("Could not execute command"); + + i += 1; + + if i > 10 + { + break; + } + } + + if i > 10 + { + break; } } + + if i > 10 + { + break; + } } + + benchmark_repository.join(); } diff --git a/src/lib.rs b/src/lib.rs index 9b7a0b6..7a97934 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,11 +6,11 @@ extern crate indicatif; extern crate tempfile; extern crate walkdir; -use git2::{Cred, Error, FetchOptions, Index, IndexEntry, IndexTime, Progress, PushOptions, RemoteCallbacks, Repository, Signature}; +use git2::{Cred, Error, FetchOptions, Index, IndexEntry, IndexTime, Oid, Progress, PushOptions, RemoteCallbacks, Repository, Signature}; use std::path::{Path, PathBuf}; use std::string::String; use std::str; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use indicatif::{ProgressBar, ProgressStyle}; use walkdir::WalkDir; @@ -24,7 +24,7 @@ pub struct BenchmarkRepositoryInner pub struct BenchmarkRepository { - inner: Arc> + inner: Arc } pub struct TargetPath @@ -64,7 +64,7 @@ impl BenchmarkRepository fn reset_origin(&self, remote_url: &str) -> &Self { - let inner = self.inner.lock().unwrap(); + let inner = self.inner.clone(); if let Err(error) = inner.repository.find_remote("origin") .or_else(|_| inner.repository.remote("origin", remote_url)) @@ -79,7 +79,7 @@ impl BenchmarkRepository fn fetch_branch(&self, branch_name: &str) -> &Self { - let inner = self.inner.lock().unwrap(); + let inner = self.inner.clone(); let mut progress_bar = ProgressBar::new(0); progress_bar.set_style(BenchmarkRepository::progress_bar_style()); @@ -157,7 +157,7 @@ impl BenchmarkRepository let benchmark_repository = BenchmarkRepository { - inner: Arc::new(Mutex::new(benchmark_repository_inner)), + inner: Arc::new(benchmark_repository_inner), }; benchmark_repository @@ -313,7 +313,7 @@ impl BenchmarkRepository pub fn file_exists(&self, file_path: &Path, branch_name: &str) -> bool { - let inner = self.inner.lock().unwrap(); + let inner = self.inner.clone(); let tip_reference_name = format!("refs/remotes/origin/{}", branch_name); let tip_reference = match inner.repository.find_reference(&tip_reference_name) @@ -344,7 +344,7 @@ impl BenchmarkRepository pub fn read_file(&self, file_path: &Path, branch_name: &str) -> Option { - let inner = self.inner.lock().unwrap(); + let inner = self.inner.clone(); let tip_reference_name = format!("refs/remotes/origin/{}", branch_name); let tip_reference = match inner.repository.find_reference(&tip_reference_name) @@ -379,6 +379,65 @@ impl BenchmarkRepository Some(content.to_owned()) } + + pub fn create_branch_for_job(&self, job_id: u32) -> String + { + let inner = self.inner.clone(); + + let empty_tree_hash = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"; + let empty_tree_object_id = Oid::from_str(&empty_tree_hash).unwrap(); + let empty_tree = match inner.repository.find_tree(empty_tree_object_id) + { + Ok(value) => value, + Err(error) => panic!("Could not find empty tree object: {}", error), + }; + + let signature = Signature::now(&inner.user_name, &inner.user_email).expect("Could not create signature"); + let message = format!("Initial commit for job {}", job_id); + + let commit_id = match inner.repository.commit(None, &signature, &signature, &message, &empty_tree, &[]) + { + Ok(value) => value, + Err(error) => panic!("Could not write commit: {}", error), + }; + + let commit = match inner.repository.find_commit(commit_id) + { + Ok(value) => value, + Err(error) => panic!("Could not find newly written commit: {}", error), + }; + + let branch_name = format!("__job_{}", job_id); + + match inner.repository.branch(&branch_name, &commit, true) + { + Ok(_) => println!("Created new branch “{}”", branch_name), + Err(error) => panic!("Could not create job branch “{}”: {}", branch_name, error), + }; + + branch_name + } + + pub fn join(&self) + { + loop + { + //let origin = self.inner.repository.find_remote("origin").expect("could not find remote “origin”"); + + for reference in self.inner.repository.references().unwrap().names() + { + println!("{}", reference.unwrap()); + } + + println!("================"); + + use std::{thread, time}; + + let time = time::Duration::from_secs(2); + + thread::sleep(time); + } + } } #[cfg(test)]