diff --git a/.gitignore b/.gitignore index 0139c4a..0adf06b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ config.php vendor *.log test.php +config.json diff --git a/ofborg/Cargo.lock b/ofborg/Cargo.lock index 904ee0a..b786213 100644 --- a/ofborg/Cargo.lock +++ b/ofborg/Cargo.lock @@ -5,6 +5,9 @@ dependencies = [ "amqp 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "fs2 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "md5 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -97,6 +100,11 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "dtoa" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "enum_primitive" version = "0.1.1" @@ -172,6 +180,11 @@ dependencies = [ "unicode-normalization 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "itoa" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -281,6 +294,11 @@ dependencies = [ "walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "quote" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rand" version = "0.3.17" @@ -321,6 +339,59 @@ dependencies = [ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "serde" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde_derive" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive_internals 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive_internals" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_json" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "synom" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tempdir" version = "0.3.5" @@ -359,6 +430,11 @@ name = "unicode-normalization" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-xid" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "url" version = "1.6.0" @@ -415,6 +491,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum cc 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a9b13a57efd6b30ecd6598ebdb302cca617930b5470647570468a65d12ef9719" "checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de" "checksum dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97590ba53bcb8ac28279161ca943a924d1fd4a8fb3fa63302591647c4fc5b850" +"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" "checksum enum_primitive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "be4551092f4d519593039259a9ed8daedf0da12e5109c5280338073eaeb81180" "checksum env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "15abd780e45b3ea4f76b4e9a26ff4843258dd8a3eed2775a0e7368c2e7936c2f" "checksum error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9435d864e017c3c6afeac1654189b06cdb491cf2ff73dbf0d73b0f292f42ff8" @@ -424,6 +501,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb" "checksum gdi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0912515a8ff24ba900422ecda800b52f4016a56251922d397c576bf92c690518" "checksum idna 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "014b298351066f1512874135335d62a789ffe78a9974f94b43ed5621951eaf7d" +"checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lazy_static 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c9e5e58fa1a4c3b915a561a78a22ee0cac6ab97dca2504428bc1cb074375f8d5" "checksum libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "5ba3df4dcb460b9dfbd070d41c94c19209620c191b0340b929ce748a2bcd42d2" @@ -439,16 +517,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum percent-encoding 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de154f638187706bde41d9b4738748933d64e6b37bdbffc0b47a97d16a6ae356" "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903" "checksum pnacl-build-helper 1.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dfbe13ee77c06fb633d71c72438bd983286bb3521863a753ade8e951c7efb090" +"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum rand 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "61efcbcd9fa8d8fbb07c84e34a8af18a1ff177b449689ad38a6e9457ecc7b2ae" "checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" "checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957" "checksum rustc-demangle 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "aee45432acc62f7b9a108cc054142dac51f979e69e71ddce7d6fc7adf29e817e" "checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7" +"checksum serde 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)" = "395993cac4e3599c7c1b70a6a92d3b3f55f4443df9f0b5294e362285ad7c9ecb" +"checksum serde_derive 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)" = "7fa060f679fe2d5a9f7374dd4553dc907eba4f9acf183e4c7baf69eae02e6ca8" +"checksum serde_derive_internals 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bd381f6d01a6616cdba8530492d453b7761b456ba974e98768a18cad2cd76f58" +"checksum serde_json 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ae1e67ce320daa7e494c578e34d4b00689f23bb94512fe0ca0dfaf02ea53fb67" +"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" +"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6" "checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" "checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5" "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" "checksum unicode-normalization 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "51ccda9ef9efa3f7ef5d91e8f9b83bbe6955f9bf86aec89d5cce2c874625920f" +"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum url 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fa35e768d4daf1d85733418a49fb42e10d7f633e394fccab4ab7aba897053fe2" "checksum user32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4ef4711d107b21b410a3a974b1204d9accc8b10dad75d8324b5d755de1617d47" "checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f" diff --git a/ofborg/Cargo.toml b/ofborg/Cargo.toml index d8a9d4f..3c76ecb 100644 --- a/ofborg/Cargo.toml +++ b/ofborg/Cargo.toml @@ -7,3 +7,6 @@ authors = ["Graham Christensen "] amqp = "0.1.0" md5 = "0.3.5" fs2 = "0.4.2" +serde = "1.0" +serde_derive = "1.0" +serde_json = "1.0" \ No newline at end of file diff --git a/ofborg/src/bin/builder.rs b/ofborg/src/bin/builder.rs index 18b1308..ad5daec 100644 --- a/ofborg/src/bin/builder.rs +++ b/ofborg/src/bin/builder.rs @@ -1,6 +1,10 @@ extern crate ofborg; extern crate amqp; +use std::env; +use amqp::{Consumer, Channel}; +use amqp::protocol::basic::{Deliver,BasicProperties}; + use std::path::Path; use amqp::Basic; use amqp::protocol; @@ -9,22 +13,19 @@ use amqp::Table; use std::process; use std::io::Error; +use ofborg::config; use ofborg::checkout; use ofborg::worker; -use ofborg::worker::{Actions,StdPr,StdRepo}; - - -pub struct BuildJob { - pub repo: StdRepo, - pub pr: StdPr, -} - +use ofborg::message::buildjob; +use ofborg::worker::Actions; fn main() { + let cfg = config::load(env::args().nth(1).unwrap().as_ref()); + println!("Hello, world!"); - let mut session = Session::open_url("amqps://grahamc:cCbKQmwnRcd8kvPW9cjmMSkp@events.nix.gsc.io//").unwrap(); + let mut session = Session::open_url(&cfg.rabbitmq.as_uri()).unwrap(); let mut channel = session.open_channel(1).unwrap(); //queue: &str, passive: bool, durable: bool, exclusive: bool, auto_delete: bool, nowait: bool, arguments: Table @@ -39,7 +40,7 @@ fn main() { worker::new(BuildWorker{ cloner: cloner }), - "my_queue_name", + "build-inputs-samples", "lmao1", false, false, @@ -48,11 +49,11 @@ fn main() { Table::new() ); - if let Err(result) = channel.basic_publish("", "my_queue_name", true, false, - protocol::basic::BasicProperties{ content_type: Some("text".to_string()), ..Default::default()}, (b"Hello from rust!").to_vec()) { - println!("Failed to publish: {:?}", result); - process::exit(1); - } + channel.start_consuming(); + + channel.close(200, "Bye").unwrap(); + session.close(200, "Good Bye"); + } struct BuildWorker { @@ -60,14 +61,31 @@ struct BuildWorker { } impl worker::SimpleWorker for BuildWorker { - type J = BuildJob; + type J = buildjob::BuildJob; - fn consumer(&self, job: BuildJob, resp: Actions) -> Result<(), Error> { + fn msg_to_job(&self, method: &Deliver, headers: &BasicProperties, + body: &Vec) -> Result { + println!("lmao I got a job?"); + return match buildjob::from(body) { + Ok(e) => { return Ok(e) } + Err(e) => { + println!("{:?}", String::from_utf8(body.clone())); + panic!("{:?}", e); + } + } + } + + fn consumer(&self, job: buildjob::BuildJob, resp: Actions) -> Result<(), Error> { let project = self.cloner.project(job.repo.full_name, job.repo.clone_url); let co = project.clone_for("builder".to_string(), job.pr.number.to_string())?; - let refpath = co.checkout_ref(job.pr.target_branch.as_ref()); + let target_branch = match job.pr.target_branch { + Some(x) => { x } + None => { String::from("origin/master") } + }; + + let refpath = co.checkout_ref(target_branch.as_ref()); co.fetch_pr(job.pr.number).unwrap(); co.merge_commit(job.pr.head_sha.as_ref()).unwrap(); diff --git a/ofborg/src/config.rs b/ofborg/src/config.rs new file mode 100644 index 0000000..90793a5 --- /dev/null +++ b/ofborg/src/config.rs @@ -0,0 +1,32 @@ +use serde_json; +use std::fs::File; +use std::path::Path; +use std::io::Read; + +#[derive(Serialize, Deserialize, Debug)] +pub struct Config { + pub rabbitmq: RabbitMQConfig, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct RabbitMQConfig { + pub host: String, + pub username: String, + pub password: String, +} + +impl RabbitMQConfig { + pub fn as_uri(&self) -> String{ + return format!("amqps://{}:{}@{}//", self.username, self.password, self.host); + } +} + +pub fn load(filename: &Path) -> Config { + let mut file = File::open(filename).unwrap(); + let mut contents = String::new(); + file.read_to_string(&mut contents).unwrap(); + + let deserialized: Config = serde_json::from_str(&contents).unwrap(); + + return deserialized; +} diff --git a/ofborg/src/lib.rs b/ofborg/src/lib.rs index 8e2e17f..a3ce06d 100644 --- a/ofborg/src/lib.rs +++ b/ofborg/src/lib.rs @@ -1,3 +1,8 @@ +#[macro_use] +extern crate serde_derive; +extern crate serde; +extern crate serde_json; + extern crate amqp; extern crate fs2; extern crate md5; @@ -6,10 +11,15 @@ pub mod checkout; pub mod locks; pub mod clone; pub mod worker; +pub mod config; +pub mod message; pub mod ofborg { + pub use config; pub use checkout; pub use locks; pub use clone; pub use worker; + pub use message; + } diff --git a/ofborg/src/message/buildjob.rs b/ofborg/src/message/buildjob.rs new file mode 100644 index 0000000..2f6a61a --- /dev/null +++ b/ofborg/src/message/buildjob.rs @@ -0,0 +1,12 @@ +use ofborg::message::{Pr,Repo}; +use serde_json; + +#[derive(Serialize, Deserialize, Debug)] +pub struct BuildJob { + pub repo: Repo, + pub pr: Pr, +} + +pub fn from(data: &Vec) -> Result { + return serde_json::from_slice(&data); +} diff --git a/ofborg/src/message/common.rs b/ofborg/src/message/common.rs new file mode 100644 index 0000000..4071b8d --- /dev/null +++ b/ofborg/src/message/common.rs @@ -0,0 +1,13 @@ + +#[derive(Serialize, Deserialize, Debug)] +pub struct Repo { + pub full_name: String, + pub clone_url: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct Pr { + pub target_branch: Option, + pub number: i64, + pub head_sha: String, +} diff --git a/ofborg/src/message/mod.rs b/ofborg/src/message/mod.rs new file mode 100644 index 0000000..3564393 --- /dev/null +++ b/ofborg/src/message/mod.rs @@ -0,0 +1,4 @@ +mod common; +pub mod buildjob; + +use self::common::{Pr,Repo}; diff --git a/ofborg/src/worker.rs b/ofborg/src/worker.rs index d7ce25d..6f3bf8f 100644 --- a/ofborg/src/worker.rs +++ b/ofborg/src/worker.rs @@ -8,23 +8,15 @@ pub struct Worker { internal: T } -pub struct StdRepo { - pub full_name: String, - pub clone_url: String, -} - -pub struct StdPr { - pub target_branch: String, - pub number: i64, - pub head_sha: String, -} - pub struct Actions { } pub trait SimpleWorker { type J; fn consumer(&self, job: Self::J, resp: Actions) -> Result<(), Error>; + + fn msg_to_job(&self, method: &Deliver, headers: &BasicProperties, + body: &Vec) -> Result; } pub fn new(worker: T) -> Worker { @@ -33,11 +25,28 @@ pub fn new(worker: T) -> Worker { }; } + + impl Consumer for Worker { fn handle_delivery(&mut self, channel: &mut Channel, method: Deliver, headers: BasicProperties, body: Vec) { + + match self.internal.msg_to_job(&method, &headers, &body) { + Ok(job) => { + let actions = Actions{}; + match self.internal.consumer(job, actions) { + Ok(_) => { /* :) */ } + Err(_) => { + panic!("failed to run job!"); + } + } + } + Err(e) => { + panic!(e); + } + } } }