A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. use std::borrow::{Borrow, Cow};
  2. use std::error::Error;
  3. use std::fmt;
  4. use std::fs;
  5. use std::io;
  6. use std::path::Path;
  7. use std::str::FromStr;
  8. use command_runner::CommandRunner;
  9. use resources::Resource;
  10. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  11. use storage::Storage;
  12. #[derive(Debug, PartialEq)]
  13. pub enum StorageDirection { Load, Save }
  14. pub struct StoredDirectory<'a, S, C: 'a + CommandRunner> where S: Storage {
  15. path: Cow<'a, str>,
  16. storage: S,
  17. dir: StorageDirection,
  18. command_runner: &'a C
  19. }
  20. impl<'a, S, C: CommandRunner> StoredDirectory<'a, S, C> where S: Storage {
  21. pub fn new(path: Cow<'a, str>, storage: S, dir: StorageDirection, command_runner: &'a C) -> Self {
  22. StoredDirectory { path, storage, dir, command_runner }
  23. }
  24. }
  25. impl<'a, S, C: CommandRunner> fmt::Display for StoredDirectory<'a, S, C> where S: Storage {
  26. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  27. write!(f, "Stored directory {} ({:?})", self.path, self.dir)
  28. }
  29. }
  30. impl<'a, S, C: CommandRunner> Symbol for StoredDirectory<'a, S, C> where S: Storage {
  31. fn target_reached(&self) -> Result<bool, Box<Error>> {
  32. let metadata = fs::metadata(self.path.as_ref());
  33. // Check if dir exists
  34. if let Err(e) = metadata {
  35. return if e.kind() == io::ErrorKind::NotFound {
  36. Ok(self.dir == StorageDirection::Save)
  37. } else {
  38. Err(Box::new(e))
  39. };
  40. }
  41. if !metadata.unwrap().is_dir() {
  42. return Err(Box::new(io::Error::new(io::ErrorKind::AlreadyExists, "Could not create a directory, non-directory file exists")));
  43. }
  44. let dump_date = try!(self.storage.recent_date());
  45. let output = try!(self.command_runner.get_output("sh", &["-c", &format!("find {} -printf '%T@\\n' | sort -r | head -n1 | grep '^[0-9]\\+' -o", self.path)]));
  46. let modified_date = try!(u64::from_str(try!(String::from_utf8(output)).trim_right()));
  47. if if self.dir == StorageDirection::Save { modified_date > dump_date } else { dump_date > modified_date } {
  48. let output = try!(self.command_runner.run_with_args("diff", &["-rq", &try!(self.storage.read_filename()), self.path.borrow()]));
  49. match output.status.code() {
  50. Some(0) => Ok(true),
  51. Some(1) => Ok(false),
  52. _ => Err(try!(String::from_utf8(output.stderr)).into())
  53. }
  54. } else { Ok(true) }
  55. }
  56. fn execute(&self) -> Result<(), Box<Error>> {
  57. if self.dir == StorageDirection::Load {
  58. try!(self.command_runner.run_successfully("rm", &["-rf", self.path.borrow()]));
  59. self.command_runner.run_successfully("cp", &["-a", &try!(self.storage.read_filename()), self.path.borrow()])
  60. } else {
  61. self.command_runner.run_successfully("cp", &["-a", self.path.borrow(), &self.storage.write_filename()])
  62. }
  63. }
  64. fn get_prerequisites(&self) -> Vec<Resource> {
  65. if self.dir == StorageDirection::Save { return vec![]; }
  66. if let Some(parent) = Path::new(self.path.as_ref()).parent() {
  67. vec![ Resource::new("dir", parent.to_string_lossy()) ]
  68. } else {
  69. vec![]
  70. }
  71. }
  72. fn provides(&self) -> Option<Vec<Resource>> {
  73. if self.dir == StorageDirection::Load {
  74. Some(vec![ Resource::new("dir", self.path.to_string()) ])
  75. } else {
  76. None
  77. }
  78. }
  79. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  80. Box::new(SymbolAction::new(runner, self))
  81. }
  82. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  83. Box::new(OwnedSymbolAction::new(runner, *self))
  84. }
  85. }
  86. #[cfg(test)]
  87. mod test {
  88. }