Skip to content

Commit

Permalink
More logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tailhook committed Oct 1, 2015
1 parent 8a69326 commit 0964a04
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ yaml-rust = "0.2.2"
scan_dir = "0.2.0"
time = "0.1.32"
rand = "0.3.11"
libc = "0.1.10"
17 changes: 7 additions & 10 deletions src/apply/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use std::fmt::Debug;
use std::mem::replace;
use std::io::{Write, Seek};
use std::io::ErrorKind::NotFound;
use std::fs::{OpenOptions, File};
use std::fs::{create_dir, read_link, remove_file, rename, metadata};
use std::os::unix::fs::symlink;
use std::fs::{OpenOptions, File, read_link};
use std::os::unix::ffi::OsStrExt;
use std::io::SeekFrom::Current;
use std::fmt::Arguments;
Expand Down Expand Up @@ -162,7 +160,7 @@ impl<'a> Deployment<'a> {
{
let ptr = Pointer::Global(&self.segment, pos);
let entry: IndexEntry = (&time, &self.id, ptr, marker);
let mut buf = format!("{}\n", as_json(&entry));
let buf = format!("{}\n", as_json(&entry));
// We rely on POSIX semantics of atomic writes, but even if that
// doesn't work, it's better to continue writing entry instead of
// having error. For now we are only writer anyway, so atomic
Expand All @@ -178,7 +176,7 @@ impl<'a> Deployment<'a> {
let time = now_utc().rfc3339().to_string();
{
let entry: IndexEntry = (&time, &self.id, ptr, marker);
let mut buf = format!("{}\n", as_json(&entry));
let buf = format!("{}\n", as_json(&entry));
// We rely on POSIX semantics of atomic writes, but even if that
// doesn't work, it's better to continue writing entry instead of
// having error. For now we are only writer anyway, so atomic
Expand Down Expand Up @@ -222,7 +220,7 @@ impl<'a> Deployment<'a> {
-> Result<Role<'a, 'x>, Error>
{
let segment;
let mut log_file;
let log_file;

if self.index.stdout {
segment = "dry-run".to_string();
Expand Down Expand Up @@ -258,15 +256,14 @@ impl<'a> Deployment<'a> {
pub fn done(mut self) -> Vec<Error> {
self.done = true;
self.global_entry(Marker::DeploymentFinish);
let mut errors = replace(&mut self.errors, Vec::new());
return errors;
return replace(&mut self.errors, Vec::new());
}
}

impl<'a> Drop for Deployment<'a> {
fn drop(&mut self) {
if !self.done {
self.global_entry(Marker::DeploymentFinish);
self.global_entry(Marker::DeploymentError);
}
}
}
Expand Down Expand Up @@ -330,7 +327,7 @@ fn check_log(dir: &Path) -> Result<(String, File), io::Error> {
})
})
}
Ok(x) => {
Ok(_) => {
error!("errorneous segment");
None
}
Expand Down
40 changes: 33 additions & 7 deletions src/apply/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
use std::io;
use std::fmt::Arguments;
use std::process::ExitStatus;
use std::collections::HashMap;

use tempfile::NamedTempFile;

use config::Config;
use render::Error as RenderError;


mod root_command;
pub mod log;


pub type ApplyTask = HashMap<String,
Result<Vec<(String, Action, Source)>, RenderError>>;

pub struct Task<'a: 'b, 'b: 'c, 'c: 'd, 'd> {
pub runner: &'d str,
pub log: &'d mut log::Action<'a, 'b, 'c>,
pub dry_run: bool,
pub source: Source,
}

#[derive(RustcDecodable, Debug, Clone)]
pub enum Action {
RootCommand(Vec<String>),
Expand All @@ -24,7 +34,11 @@ pub enum Source {
quick_error!{
#[derive(Debug)]
pub enum Error {
Command(runner: String, cmd: String, err: io::Error) {
Command(runner: String, cmd: String, status: ExitStatus) {
display("Action {:?} failed to run {:?}: {}", runner, cmd, status)
description("error running command")
}
CantRun(runner: String, cmd: String, err: io::Error) {
display("Action {:?} failed to run {:?}: {}", runner, cmd, err)
description("error running command")
}
Expand All @@ -36,6 +50,16 @@ quick_error!{
}
}

impl<'a, 'b, 'c, 'd> Task<'a, 'b, 'c, 'd> {
fn log(&mut self, args: Arguments) {
if self.dry_run {
self.log.log(format_args!("(dry_run) {}", args));
} else {
self.log.log(args);
}
}
}

pub fn apply_list(name: &String,
task: Result<Vec<(String, Action, Source)>, RenderError>,
log: &mut log::Deployment, dry_run: bool)
Expand All @@ -56,9 +80,12 @@ pub fn apply_list(name: &String,
let mut action = role_log.action(&aname);
match cmd {
RootCommand(cmd) => {
root_command::execute(cmd, source,
&mut action, dry_run)
.map_err(|e| errors.push(e)).ok();
root_command::execute(cmd, Task {
runner: &aname,
log: &mut action,
dry_run: dry_run,
source: source,
}).map_err(|e| errors.push(e)).ok();
}
}
}
Expand All @@ -71,8 +98,7 @@ pub fn apply_list(name: &String,
return errors;
}

pub fn apply_all(cfg: &Config, task: ApplyTask,
mut log: log::Deployment, dry_run: bool)
pub fn apply_all(task: ApplyTask, mut log: log::Deployment, dry_run: bool)
-> (HashMap<String, Vec<Error>>, Vec<Error>)
{
let roles = task.into_iter().map(|(name, items)| {
Expand Down
39 changes: 33 additions & 6 deletions src/apply/root_command.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
use apply::{Source, Error, log};
use std::process::Command;

pub fn execute(cmd: Vec<String>, source: Source,
log: &mut log::Action, dry_run: bool)
-> Result<(), Error>
use libc::geteuid;

use apply::{Task, Error};


pub fn execute(args: Vec<String>, mut task: Task) -> Result<(), Error>
{
log.log(format_args!("RootCommand {:?}\n", cmd));
unimplemented!();
let uid = unsafe { geteuid() };
let mut cmd = if uid != 0 {
let mut cmd = Command::new("/usr/bin/sudo");
cmd.arg("-n");
cmd.arg(&args[0]);
cmd
} else {
Command::new(&args[0])
};
cmd.args(&args[1..]);
task.log(format_args!("RootCommand {:#?}\n", cmd));
if !task.dry_run {
cmd.status()
.map_err(|e| {
task.log.log(format_args!(
"RootCommand {:#?} failed to start: {}\n", cmd, e));
Error::CantRun(
task.runner.to_string(), format!("{:#?}", cmd), e)
}).and_then(|s| if s.success() { Ok(()) } else {
task.log.log(format_args!("RootCommand {:#?}: {}\n", cmd, s));
Err(Error::Command(
task.runner.to_string(), format!("{:#?}", cmd), s))
})
} else {
Ok(())
}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate rustc_serialize;
extern crate tempfile;
extern crate time;
extern crate rand;
extern crate libc;
extern crate lua;
extern crate scan_dir;
extern crate yaml_rust;
Expand Down Expand Up @@ -128,8 +129,7 @@ fn main() {
let mut dlog = index.deployment(id);
dlog.object("config", &config);
dlog.json("scheduler_result", &scheduler_result);
let (rerrors, gerrs) = apply::apply_all(&config, apply_task,
dlog, options.dry_run);
let (rerrors, gerrs) = apply::apply_all(apply_task, dlog, options.dry_run);
if log_enabled!(log::LogLevel::Debug) {
for e in gerrs {
error!("Error when applying config: {}", e);
Expand Down

0 comments on commit 0964a04

Please sign in to comment.