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.

154 lines
3.7 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
5 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
5 years ago
5 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
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 = self.storage.recent_date()?;
  68. let output = 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 = u64::from_str(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 = self.command_runner.run_with_args(
  85. "diff",
  86. &["-rq", &self.storage.read_filename()?, self.path.borrow()],
  87. )?;
  88. match output.status.code() {
  89. Some(0) => Ok(true),
  90. Some(1) => Ok(false),
  91. _ => Err(String::from_utf8(output.stderr)?.into()),
  92. }
  93. } else {
  94. Ok(true)
  95. }
  96. }
  97. fn execute(&self) -> Result<(), Box<dyn Error>> {
  98. if self.dir == StorageDirection::Load {
  99. self
  100. .command_runner
  101. .run_successfully("rm", &["-rf", self.path.borrow()])?;
  102. self.command_runner.run_successfully(
  103. "cp",
  104. &["-a", &self.storage.read_filename()?, self.path.borrow()],
  105. )
  106. } else {
  107. self.command_runner.run_successfully(
  108. "cp",
  109. &["-a", self.path.borrow(), &self.storage.write_filename()],
  110. )
  111. }
  112. }
  113. fn get_prerequisites(&self) -> Vec<Resource> {
  114. if self.dir == StorageDirection::Save {
  115. return vec![];
  116. }
  117. if let Some(parent) = Path::new(self.path.as_ref()).parent() {
  118. vec![Resource::new("dir", parent.to_string_lossy())]
  119. } else {
  120. vec![]
  121. }
  122. }
  123. fn provides(&self) -> Option<Vec<Resource>> {
  124. if self.dir == StorageDirection::Load {
  125. Some(vec![Resource::new("dir", self.path.to_string())])
  126. } else {
  127. None
  128. }
  129. }
  130. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  131. Box::new(SymbolAction::new(runner, self))
  132. }
  133. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  134. where
  135. Self: 'b,
  136. {
  137. Box::new(OwnedSymbolAction::new(runner, *self))
  138. }
  139. }
  140. #[cfg(test)]
  141. mod test {}