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.

162 lines
3.8 KiB

7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 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 storage::Storage;
  11. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  12. #[derive(Debug, PartialEq)]
  13. pub enum StorageDirection {
  14. Load,
  15. Save,
  16. }
  17. pub struct StoredDirectory<'a, S, C: 'a + CommandRunner>
  18. where
  19. S: Storage,
  20. {
  21. path: Cow<'a, str>,
  22. storage: S,
  23. dir: StorageDirection,
  24. command_runner: &'a C,
  25. }
  26. impl<'a, S, C: CommandRunner> StoredDirectory<'a, S, C>
  27. where
  28. S: Storage,
  29. {
  30. pub fn new(path: Cow<'a, str>, storage: S, dir: StorageDirection, command_runner: &'a C) -> Self {
  31. StoredDirectory {
  32. path,
  33. storage,
  34. dir,
  35. command_runner,
  36. }
  37. }
  38. }
  39. impl<'a, S, C: CommandRunner> fmt::Display for StoredDirectory<'a, S, C>
  40. where
  41. S: Storage,
  42. {
  43. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  44. write!(f, "Stored directory {} ({:?})", self.path, self.dir)
  45. }
  46. }
  47. impl<'a, S, C: CommandRunner> Symbol for StoredDirectory<'a, S, C>
  48. where
  49. S: Storage,
  50. {
  51. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  52. let metadata = fs::metadata(self.path.as_ref());
  53. // Check if dir exists
  54. if let Err(e) = metadata {
  55. return if e.kind() == io::ErrorKind::NotFound {
  56. Ok(self.dir == StorageDirection::Save)
  57. } else {
  58. Err(Box::new(e))
  59. };
  60. }
  61. if !metadata.unwrap().is_dir() {
  62. return Err(Box::new(io::Error::new(
  63. io::ErrorKind::AlreadyExists,
  64. "Could not create a directory, non-directory file exists",
  65. )));
  66. }
  67. let dump_date = try!(self.storage.recent_date());
  68. let output = try!(self.command_runner.get_output(
  69. "sh",
  70. &[
  71. "-c",
  72. &format!(
  73. "find {} -printf '%T@\\n' | sort -r | head -n1 | grep '^[0-9]\\+' -o",
  74. self.path
  75. )
  76. ]
  77. ));
  78. let modified_date = try!(u64::from_str(try!(String::from_utf8(output)).trim_end()));
  79. if if self.dir == StorageDirection::Save {
  80. modified_date > dump_date
  81. } else {
  82. dump_date > modified_date
  83. } {
  84. let output = try!(self.command_runner.run_with_args(
  85. "diff",
  86. &[
  87. "-rq",
  88. &try!(self.storage.read_filename()),
  89. self.path.borrow()
  90. ]
  91. ));
  92. match output.status.code() {
  93. Some(0) => Ok(true),
  94. Some(1) => Ok(false),
  95. _ => Err(try!(String::from_utf8(output.stderr)).into()),
  96. }
  97. } else {
  98. Ok(true)
  99. }
  100. }
  101. fn execute(&self) -> Result<(), Box<dyn Error>> {
  102. if self.dir == StorageDirection::Load {
  103. try!(self
  104. .command_runner
  105. .run_successfully("rm", &["-rf", self.path.borrow()]));
  106. self.command_runner.run_successfully(
  107. "cp",
  108. &[
  109. "-a",
  110. &try!(self.storage.read_filename()),
  111. self.path.borrow(),
  112. ],
  113. )
  114. } else {
  115. self.command_runner.run_successfully(
  116. "cp",
  117. &["-a", self.path.borrow(), &self.storage.write_filename()],
  118. )
  119. }
  120. }
  121. fn get_prerequisites(&self) -> Vec<Resource> {
  122. if self.dir == StorageDirection::Save {
  123. return vec![];
  124. }
  125. if let Some(parent) = Path::new(self.path.as_ref()).parent() {
  126. vec![Resource::new("dir", parent.to_string_lossy())]
  127. } else {
  128. vec![]
  129. }
  130. }
  131. fn provides(&self) -> Option<Vec<Resource>> {
  132. if self.dir == StorageDirection::Load {
  133. Some(vec![Resource::new("dir", self.path.to_string())])
  134. } else {
  135. None
  136. }
  137. }
  138. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  139. Box::new(SymbolAction::new(runner, self))
  140. }
  141. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  142. where
  143. Self: 'b,
  144. {
  145. Box::new(OwnedSymbolAction::new(runner, *self))
  146. }
  147. }
  148. #[cfg(test)]
  149. mod test {}