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.

106 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
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> where S: Storage {
  15. path: Cow<'a, str>,
  16. storage: S,
  17. dir: StorageDirection,
  18. command_runner: &'a CommandRunner
  19. }
  20. impl<'a, S> StoredDirectory<'a, S> where S: Storage {
  21. pub fn new(path: Cow<'a, str>, storage: S, dir: StorageDirection, command_runner: &'a CommandRunner) -> Self {
  22. StoredDirectory {
  23. path: path,
  24. storage: storage,
  25. dir: dir,
  26. command_runner: command_runner
  27. }
  28. }
  29. }
  30. impl<'a, S> fmt::Display for StoredDirectory<'a, S> where S: Storage {
  31. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  32. write!(f, "Stored directory {} ({:?})", self.path, self.dir)
  33. }
  34. }
  35. impl<'a, S> Symbol for StoredDirectory<'a, S> where S: Storage {
  36. fn target_reached(&self) -> Result<bool, Box<Error>> {
  37. let metadata = fs::metadata(self.path.as_ref());
  38. // Check if dir exists
  39. if let Err(e) = metadata {
  40. return if e.kind() == io::ErrorKind::NotFound {
  41. Ok(self.dir == StorageDirection::Save)
  42. } else {
  43. Err(Box::new(e))
  44. };
  45. }
  46. if !metadata.unwrap().is_dir() {
  47. return Err(Box::new(io::Error::new(io::ErrorKind::AlreadyExists, "Could not create a directory, non-directory file exists")));
  48. }
  49. let dump_date = try!(self.storage.recent_date());
  50. 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)]));
  51. let modified_date = try!(u64::from_str(try!(String::from_utf8(output)).trim_right()));
  52. if if self.dir == StorageDirection::Save { modified_date > dump_date } else { dump_date > modified_date } {
  53. let output = try!(self.command_runner.run_with_args("diff", &["-rq", &try!(self.storage.read_filename()), self.path.borrow()]));
  54. match output.status.code() {
  55. Some(0) => Ok(true),
  56. Some(1) => Ok(false),
  57. _ => Err(try!(String::from_utf8(output.stderr)).into())
  58. }
  59. } else { Ok(true) }
  60. }
  61. fn execute(&self) -> Result<(), Box<Error>> {
  62. if self.dir == StorageDirection::Load {
  63. try!(self.command_runner.run_successfully("rm", &["-rf", self.path.borrow()]));
  64. self.command_runner.run_successfully("cp", &["-a", &try!(self.storage.read_filename()), self.path.borrow()])
  65. } else {
  66. self.command_runner.run_successfully("cp", &["-a", self.path.borrow(), &self.storage.write_filename()])
  67. }
  68. }
  69. fn get_prerequisites(&self) -> Vec<Resource> {
  70. if self.dir == StorageDirection::Save { return vec![]; }
  71. if let Some(parent) = Path::new(self.path.as_ref()).parent() {
  72. vec![ Resource::new("dir", parent.to_string_lossy()) ]
  73. } else {
  74. vec![]
  75. }
  76. }
  77. fn provides(&self) -> Option<Vec<Resource>> {
  78. if self.dir == StorageDirection::Load {
  79. Some(vec![ Resource::new("dir", self.path.to_string()) ])
  80. } else {
  81. None
  82. }
  83. }
  84. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  85. Box::new(SymbolAction::new(runner, self))
  86. }
  87. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  88. Box::new(OwnedSymbolAction::new(runner, *self))
  89. }
  90. }
  91. #[cfg(test)]
  92. mod test {
  93. }