I need to commit more often so I don't commit commit-message-bankruptcy

This commit is contained in:
Graham Christensen 2017-11-09 07:33:10 -05:00
parent f83854dbfe
commit dcca963ba7
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C
9 changed files with 164 additions and 36 deletions

View file

@ -1,6 +1,7 @@
extern crate ofborg; extern crate ofborg;
extern crate amqp; extern crate amqp;
use std::collections::LinkedList;
use std::env; use std::env;
use amqp::{Consumer, Channel}; use amqp::{Consumer, Channel};
use amqp::protocol::basic::{Deliver,BasicProperties}; use amqp::protocol::basic::{Deliver,BasicProperties};
@ -12,6 +13,9 @@ use amqp::Session;
use amqp::Table; use amqp::Table;
use std::process; use std::process;
use std::io::Error; use std::io::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::BufRead;
use ofborg::config; use ofborg::config;
use ofborg::checkout; use ofborg::checkout;
@ -34,11 +38,14 @@ fn main() {
process::exit(1); process::exit(1);
} }
let cloner = checkout::cached_cloner(Path::new("/home/grahamc/.nix-test-rs")); let cloner = checkout::cached_cloner(Path::new(&cfg.checkout.root));
let nix = nix::new(cfg.nix.system.clone(), cfg.nix.remote);
channel.basic_consume( channel.basic_consume(
worker::new(BuildWorker{ worker::new(BuildWorker{
cloner: cloner cloner: cloner,
nix: nix,
system: cfg.nix.system.clone()
}), }),
"build-inputs-samples", "build-inputs-samples",
"lmao1", "lmao1",
@ -58,6 +65,8 @@ fn main() {
struct BuildWorker { struct BuildWorker {
cloner: checkout::CachedCloner, cloner: checkout::CachedCloner,
nix: nix::Nix,
system: String,
} }
impl worker::SimpleWorker for BuildWorker { impl worker::SimpleWorker for BuildWorker {
@ -76,8 +85,12 @@ impl worker::SimpleWorker for BuildWorker {
} }
} }
fn job_to_actions(&self, channel: &mut amqp::Channel, job: &buildjob::BuildJob) -> buildjob::Actions { fn job_to_actions(&self, channel: amqp::Channel, job: buildjob::BuildJob) -> buildjob::Actions {
return buildjob::Actions{}; return buildjob::Actions{
system: self.system.clone(),
channel: channel,
job: job,
};
} }
@ -95,9 +108,48 @@ impl worker::SimpleWorker for BuildWorker {
co.fetch_pr(job.pr.number).unwrap(); co.fetch_pr(job.pr.number).unwrap();
co.merge_commit(job.pr.head_sha.as_ref()).unwrap(); co.merge_commit(job.pr.head_sha.as_ref()).unwrap();
println!("Got path: {:?}", refpath); println!("Got path: {:?}, building", refpath);
let cmd = nix::safely_build_attrs_cmd(refpath, job.attrs);
let success: bool;
let reader: BufReader<File>;
match self.nix.safely_build_attrs(refpath.as_ref(), job.attrs) {
Ok(r) => {
success = true;
reader = BufReader::new(r);
}
Err(r) => {
success = false;
reader = BufReader::new(r);
}
}
println!("ok built ({:?}), building", success);
let l10 = reader.lines().fold(LinkedList::new(),
|mut coll, line|
{
match line {
Ok(e) => { coll.push_back(e); }
Err(wtf) => {
println!("Got err in lines: {:?}", wtf);
coll.push_back(String::from("<line omitted due to error>"));
}
}
if coll.len() == 11 {
coll.pop_front();
}
return coll
}
);
println!("Lines: {:?}", l10);
resp.build_finished(
success,
l10.into_iter().collect::<Vec<_>>()
);
return Ok(()) return Ok(())
} }

View file

@ -5,6 +5,8 @@ use std::io::Read;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Config { pub struct Config {
pub checkout: CheckoutConfig,
pub nix: NixConfig,
pub rabbitmq: RabbitMQConfig, pub rabbitmq: RabbitMQConfig,
} }
@ -15,6 +17,18 @@ pub struct RabbitMQConfig {
pub password: String, pub password: String,
} }
#[derive(Serialize, Deserialize, Debug)]
pub struct NixConfig {
pub system: String,
pub remote: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CheckoutConfig {
pub root: String,
}
impl RabbitMQConfig { impl RabbitMQConfig {
pub fn as_uri(&self) -> String{ pub fn as_uri(&self) -> String{
return format!("amqps://{}:{}@{}//", self.username, self.password, self.host); return format!("amqps://{}:{}@{}//", self.username, self.password, self.host);

View file

@ -1,15 +1,41 @@
use ofborg::message::{Pr,Repo}; use ofborg::message::{Pr,Repo};
use ofborg::message::buildresult;
use serde_json; use serde_json;
use amqp::{Basic, Channel, protocol};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct BuildJob { pub struct BuildJob {
pub repo: Repo, pub repo: Repo,
pub pr: Pr, pub pr: Pr,
pub attrs: Vec<String>, pub attrs: Vec<String>
} }
pub fn from(data: &Vec<u8>) -> Result<BuildJob, serde_json::error::Error> { pub fn from(data: &Vec<u8>) -> Result<BuildJob, serde_json::error::Error> {
return serde_json::from_slice(&data); return serde_json::from_slice(&data);
} }
pub struct Actions{} pub struct Actions {
pub system: String,
pub job: BuildJob,
pub channel: Channel
}
impl Actions {
pub fn build_finished(&mut self, success: bool, lines: Vec<String>) {
let msg = buildresult::BuildResult {
repo: self.job.repo.clone(),
pr: self.job.pr.clone(),
system: self.system.clone(),
output: lines,
success: success
};
let props = protocol::basic::BasicProperties {
content_type: Some("application/json".to_owned()),
..Default::default()
};
self.channel.basic_publish("build-results", "", true, true,
props, serde_json::to_string(&msg).unwrap().into_bytes()).unwrap();
}
}

View file

@ -0,0 +1,10 @@
use ofborg::message::{Pr,Repo};
#[derive(Serialize, Deserialize, Debug)]
pub struct BuildResult {
pub repo: Repo,
pub pr: Pr,
pub system: String,
pub output: Vec<String>,
pub success: bool
}

View file

@ -1,11 +1,11 @@
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Repo { pub struct Repo {
pub full_name: String, pub full_name: String,
pub clone_url: String, pub clone_url: String,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Pr { pub struct Pr {
pub target_branch: Option<String>, pub target_branch: Option<String>,
pub number: i64, pub number: i64,

View file

@ -1,4 +1,5 @@
mod common; mod common;
pub mod buildjob; pub mod buildjob;
pub mod buildresult;
use self::common::{Pr,Repo}; use self::common::{Pr,Repo};

View file

@ -3,34 +3,60 @@ use std::ffi::OsString;
use std::process::{Command,Stdio}; use std::process::{Command,Stdio};
use tempfile::tempfile; use tempfile::tempfile;
use std::fs::File; use std::fs::File;
use std::io::Seek;
use std::io::SeekFrom;
pub fn safely_build_attrs(nixpkgs: &Path, attrs: Vec<String>) -> Result<File,File> { pub struct Nix {
system: String,
remote: String
}
pub fn new(system: String, remote: String) -> Nix {
return Nix{
system: system,
remote: remote,
}
}
impl Nix {
pub fn safely_build_attrs(&self, nixpkgs: &Path, attrs: Vec<String>) -> Result<File,File> {
let mut nixpath = OsString::new(); let mut nixpath = OsString::new();
nixpath.push("nixpkgs="); nixpath.push("nixpkgs=");
nixpath.push(nixpkgs.as_os_str()); nixpath.push(nixpkgs.as_os_str());
let mut attrargs: Vec<String> = Vec::with_capacity(attrs.len() * 2);
for attr in attrs {
attrargs.push(String::from("-A"));
attrargs.push(attr);
}
let stdout = tempfile().unwrap(); let stdout = tempfile().unwrap();
let stderr = stdout.try_clone().unwrap(); let stderr = stdout.try_clone().unwrap();
let reader = stderr.try_clone().unwrap(); let mut reader = stderr.try_clone().unwrap();
let mut cmd = Command::new("nix-build") let status = Command::new("nix-build")
.env_clear() .env_clear()
.current_dir(nixpkgs) .current_dir(nixpkgs)
.stdout(Stdio::from(stdout)) .stdout(Stdio::from(stdout))
.stderr(Stdio::from(stderr)) .stderr(Stdio::from(stderr))
.env("NIX_PATH", nixpath); .env("NIX_PATH", nixpath)
.env("NIX_REMOTE", &self.remote)
for attr in attrs { .arg("--no-out-link")
cmd.arg("-A"); .args(&["--option", "restrict-eval", "true"])
cmd.arg(attr); .args(&["--argstr", "system", &self.system])
} .arg("--keep-going")
.args(attrargs)
let stat = cmd
.status() .status()
.unwrap(); .unwrap();
reader.seek(SeekFrom::Start(0)).unwrap();
return Ok(reader); if status.success() {
return Ok(reader)
} else {
return Err(reader)
}
}
} }
/* /*

View file

@ -19,7 +19,7 @@ pub trait SimpleWorker {
fn msg_to_job(&self, method: &Deliver, headers: &BasicProperties, fn msg_to_job(&self, method: &Deliver, headers: &BasicProperties,
body: &Vec<u8>) -> Result<Self::J, String>; body: &Vec<u8>) -> Result<Self::J, String>;
fn job_to_actions(&self, channel: &mut Channel, job: &Self::J) -> Self::A; fn job_to_actions(&self, channel: Channel, job: Self::J) -> Self::A;
} }
pub fn new<T: SimpleWorker>(worker: T) -> Worker<T> { pub fn new<T: SimpleWorker>(worker: T) -> Worker<T> {

View file

@ -12,7 +12,7 @@ let
phpEnv = stdenv.mkDerivation rec { phpEnv = stdenv.mkDerivation rec {
name = "gh-event-forwarder"; name = "gh-event-forwarder";
src = ./.; src = null;
buildInputs = with pkgs; [ buildInputs = with pkgs; [
php php
phpPackages.composer phpPackages.composer
@ -29,7 +29,6 @@ let
rustEnv = stdenv.mkDerivation rec { rustEnv = stdenv.mkDerivation rec {
name = "gh-event-forwarder"; name = "gh-event-forwarder";
src = ./.;
buildInputs = with pkgs; [ buildInputs = with pkgs; [
php php
phpPackages.composer phpPackages.composer
@ -38,7 +37,7 @@ let
openssl openssl
]; ];
HISTFILE = "${src}/.bash_hist"; HISTFILE = "${toString ./.}/.bash_hist";
}; };