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.

325 lines
8.4 KiB

7 years ago
  1. use crate::artifacts::{
  2. DatabaseName as DatabaseNameArtifact, Path as PathArtifact, ServiceName as ServiceNameArtifact,
  3. UserName as UserNameArtifact,
  4. };
  5. use crate::templates::php::FpmPoolConfig;
  6. use std::hash::Hash;
  7. use std::path::PathBuf;
  8. pub trait Resource {
  9. type Artifact;
  10. }
  11. #[derive(Debug, Hash, PartialEq, Eq)]
  12. pub struct Key<D>(pub D);
  13. impl<D> Resource for Key<D> {
  14. type Artifact = PathArtifact;
  15. }
  16. #[derive(Debug, Hash, PartialEq, Eq)]
  17. pub struct Csr<D>(pub D);
  18. impl<D> Resource for Csr<D> {
  19. type Artifact = PathArtifact;
  20. }
  21. #[derive(Debug, Hash, PartialEq, Eq)]
  22. pub struct Cert<D>(pub D);
  23. impl<D> Resource for Cert<D> {
  24. type Artifact = PathArtifact;
  25. }
  26. #[derive(Debug, Hash, PartialEq, Eq)]
  27. pub struct CertChain<D>(pub D);
  28. impl<D> Resource for CertChain<D> {
  29. type Artifact = PathArtifact;
  30. }
  31. #[derive(Debug, Hash, PartialEq, Eq)]
  32. pub struct KeyAndCertBundle<D>(pub D);
  33. impl<D> Resource for KeyAndCertBundle<D> {
  34. type Artifact = PathArtifact;
  35. }
  36. #[derive(Debug, Hash, PartialEq, Eq)]
  37. pub struct File<P>(pub P, pub String);
  38. impl<'a, P> Resource for File<P> {
  39. type Artifact = ();
  40. }
  41. #[derive(Debug, Hash, PartialEq, Eq)]
  42. pub struct GitCheckout<'a, P>(pub P, pub &'a str, pub &'a str);
  43. impl<'a, P> Resource for GitCheckout<'a, P> {
  44. type Artifact = ();
  45. }
  46. #[derive(Debug, Hash, PartialEq, Eq)]
  47. pub struct Dir<P>(pub P);
  48. impl<P> Resource for Dir<P> {
  49. type Artifact = ();
  50. }
  51. #[derive(Debug, Hash, PartialEq, Eq)]
  52. pub struct UserForDomain<D>(pub D);
  53. impl<D> Resource for UserForDomain<D> {
  54. type Artifact = (UserNameArtifact, PathArtifact);
  55. }
  56. #[derive(Debug, Hash, PartialEq, Eq)]
  57. pub struct AcmeAccountKey;
  58. impl Resource for AcmeAccountKey {
  59. type Artifact = PathArtifact;
  60. }
  61. #[derive(Debug, Hash, PartialEq, Eq)]
  62. pub struct AcmeUser;
  63. impl Resource for AcmeUser {
  64. type Artifact = UserNameArtifact;
  65. }
  66. #[derive(Debug, Hash, PartialEq, Eq)]
  67. pub struct AcmeRootCert;
  68. impl Resource for AcmeRootCert {
  69. type Artifact = PathArtifact;
  70. }
  71. #[derive(Debug, Hash, PartialEq, Eq)]
  72. pub struct AcmeChallengesDir;
  73. impl Resource for AcmeChallengesDir {
  74. type Artifact = PathArtifact;
  75. }
  76. #[derive(Debug, Hash, PartialEq, Eq)]
  77. pub struct AcmeChallengesNginxSnippet;
  78. impl Resource for AcmeChallengesNginxSnippet {
  79. type Artifact = PathArtifact;
  80. }
  81. #[derive(Debug, Hash, PartialEq, Eq)]
  82. pub struct StoredDirectory<P>(pub &'static str, pub P);
  83. impl<P> Resource for StoredDirectory<P> {
  84. type Artifact = PathArtifact;
  85. }
  86. #[derive(Debug, Hash, PartialEq, Eq)]
  87. pub struct LoadedDirectory<P>(pub &'static str, pub P);
  88. impl<P> Resource for LoadedDirectory<P> {
  89. type Artifact = PathArtifact;
  90. }
  91. pub fn get_saved_directory<P: Clone>(
  92. storage_name: &'static str,
  93. target: P,
  94. ) -> (StoredDirectory<P>, LoadedDirectory<P>) {
  95. (
  96. StoredDirectory(storage_name, target.clone()),
  97. LoadedDirectory(storage_name, target),
  98. )
  99. }
  100. #[derive(Debug, Hash, PartialEq, Eq)]
  101. pub struct User(pub String);
  102. impl Resource for User {
  103. type Artifact = ();
  104. }
  105. #[derive(Debug, Hash, PartialEq, Eq)]
  106. pub struct SystemdSocketService<D, P>(pub D, pub &'static str, pub P, pub P, pub bool);
  107. impl<D, P> Resource for SystemdSocketService<D, P> {
  108. type Artifact = (PathArtifact, PathArtifact, UserNameArtifact);
  109. }
  110. #[derive(Debug, Hash, PartialEq, Eq)]
  111. pub struct NpmInstall<P>(pub P);
  112. impl<P> Resource for NpmInstall<P> {
  113. type Artifact = ();
  114. }
  115. #[derive(Debug, Hash, PartialEq, Eq)]
  116. pub struct Owner<P>(pub String, pub P);
  117. impl<P> Resource for Owner<P> {
  118. type Artifact = ();
  119. }
  120. #[derive(Debug, Hash, PartialEq, Eq)]
  121. pub struct ServeCustom<D>(pub D, pub String);
  122. impl<D> Resource for ServeCustom<D> {
  123. type Artifact = PathArtifact;
  124. }
  125. #[derive(Debug, Hash, PartialEq, Eq)]
  126. pub struct ServePhp<D, P, C>(
  127. pub D,
  128. pub P,
  129. pub &'static str,
  130. pub String,
  131. pub C,
  132. );
  133. impl<D, P, C> Resource for ServePhp<D, P, C> {
  134. type Artifact = PathArtifact;
  135. }
  136. // Domain, service name, exec, static, dir
  137. #[derive(Debug, Hash, PartialEq, Eq)]
  138. pub struct ServeService<D, P>(pub D, pub &'static str, pub P, pub P, pub P, pub bool);
  139. impl<D, P> Resource for ServeService<D, P> {
  140. type Artifact = PathArtifact;
  141. }
  142. #[derive(Debug, Hash, PartialEq, Eq)]
  143. pub struct ServeRedir<D>(pub D, pub D);
  144. impl<D> Resource for ServeRedir<D> {
  145. type Artifact = PathArtifact;
  146. }
  147. #[derive(Debug, Hash, PartialEq, Eq)]
  148. pub struct ServeStatic<D, P>(pub D, pub P);
  149. impl<D, P> Resource for ServeStatic<D, P> {
  150. type Artifact = PathArtifact;
  151. }
  152. #[derive(Debug, Hash, PartialEq, Eq)]
  153. pub struct DefaultServer;
  154. impl Resource for DefaultServer {
  155. type Artifact = PathArtifact;
  156. }
  157. #[derive(Debug, Hash, PartialEq, Eq)]
  158. pub struct PhpFpmPool<D>(pub D, pub FpmPoolConfig);
  159. impl<D> Resource for PhpFpmPool<D> {
  160. type Artifact = (
  161. PathArtifact,
  162. PathArtifact,
  163. UserNameArtifact,
  164. ServiceNameArtifact,
  165. );
  166. }
  167. // A single domain could possibly need multiple databases
  168. #[derive(Debug, Hash, PartialEq, Eq)]
  169. pub struct MariaDbDatabase<D>(pub D);
  170. impl<D> Resource for MariaDbDatabase<D> {
  171. type Artifact = (DatabaseNameArtifact, UserNameArtifact, PathArtifact);
  172. }
  173. #[derive(Debug, Hash, PartialEq, Eq)]
  174. pub struct MariaDbUser<D>(pub D);
  175. impl<D> Resource for MariaDbUser<D> {
  176. type Artifact = UserNameArtifact;
  177. }
  178. #[derive(Debug, Hash, PartialEq, Eq)]
  179. pub struct PostgresqlDatabase<D>(pub D);
  180. impl<D> Resource for PostgresqlDatabase<D> {
  181. type Artifact = (DatabaseNameArtifact, PathArtifact);
  182. }
  183. #[derive(Debug, Hash, PartialEq, Eq)]
  184. pub struct WordpressPlugin<P>(pub P, pub &'static str);
  185. impl<P> Resource for WordpressPlugin<P> {
  186. type Artifact = ();
  187. }
  188. #[derive(Debug, Hash, PartialEq, Eq)]
  189. pub struct WordpressTranslation<P>(pub P, pub &'static str, pub &'static str);
  190. impl<P> Resource for WordpressTranslation<P> {
  191. type Artifact = ();
  192. }
  193. #[derive(Debug, Hash, PartialEq, Eq)]
  194. pub struct Cron<D>(pub D, pub String);
  195. impl<D> Resource for Cron<D> {
  196. type Artifact = ();
  197. }
  198. use std::rc::{Rc, Weak};
  199. pub trait FromResource<R> {
  200. fn from_resource(from: R) -> (Self, Weak<R>)
  201. where
  202. Self: Sized;
  203. }
  204. pub trait FromArtifact<R: Resource> {
  205. fn from_artifact(from: R::Artifact) -> Self
  206. where
  207. Self: Sized;
  208. fn into_artifact(self) -> R::Artifact
  209. where
  210. Self: Sized;
  211. }
  212. macro_rules! default_resources {
  213. ( $($name:ident: $type:ty,)* ) => {
  214. #[derive(Debug, PartialEq, Eq, Hash)]
  215. pub enum DefaultResources<'a, D> {
  216. $( $name(Rc<$type>) ),*
  217. }
  218. $(impl<'a, D> FromResource<$type> for DefaultResources<'a, D> {
  219. fn from_resource(from: $type) -> (Self, Weak<$type>) {
  220. let inner = Rc::new(from);
  221. (Self::$name(Rc::clone(&inner)), Rc::downgrade(&inner))
  222. }
  223. })*
  224. #[derive(Clone, Debug)]
  225. pub enum DefaultArtifacts<'a, D> {
  226. $( $name(<$type as Resource>::Artifact) ),*
  227. }
  228. $(impl<'a, D> FromArtifact<$type> for DefaultArtifacts<'a, D> {
  229. fn from_artifact(from: <$type as Resource>::Artifact) -> Self {
  230. Self::$name(from)
  231. }
  232. fn into_artifact(self) -> <$type as Resource>::Artifact {
  233. match self {
  234. Self::$name(inner) => inner,
  235. _ => panic!()
  236. }
  237. }
  238. })*
  239. }
  240. }
  241. // Only one enum entry per resource type, otherwise the equality checks fail
  242. default_resources!(
  243. AcmeAccountKey: AcmeAccountKey,
  244. AcmeChallengesDir: AcmeChallengesDir,
  245. AcmeChallengesNginxSnippet: AcmeChallengesNginxSnippet,
  246. AcmeRootCert: AcmeRootCert,
  247. AcmeUser: AcmeUser,
  248. Cert: Cert<D>,
  249. CertChain: CertChain<D>,
  250. Cron: Cron<D>,
  251. Csr: Csr<D>,
  252. DefaultServer: DefaultServer,
  253. Dir: Dir<PathBuf>,
  254. File: File<PathBuf>,
  255. GitCheckout: GitCheckout<'a, PathBuf>,
  256. Key: Key<D>,
  257. KeyAndCertBundle: KeyAndCertBundle<D>,
  258. LoadedDirectory: LoadedDirectory<PathBuf>,
  259. MariaDbDatabase: MariaDbDatabase<D>,
  260. MariaDbUser: MariaDbUser<D>,
  261. PostgresqlDatabase: PostgresqlDatabase<D>,
  262. SystemdSocketService: SystemdSocketService<D, PathBuf>,
  263. NpmInstall: NpmInstall<PathBuf>,
  264. Owner: Owner<PathBuf>,
  265. PhpFpmPool: PhpFpmPool<D>,
  266. ServeCustom: ServeCustom<D>,
  267. ServeService: ServeService<D, PathBuf>,
  268. ServePhp: ServePhp<D, PathBuf, FpmPoolConfig>,
  269. ServeRedir: ServeRedir<D>,
  270. ServeStatic: ServeStatic<D, PathBuf>,
  271. StoredDirectory: StoredDirectory<PathBuf>,
  272. User: User,
  273. UserForDomain: UserForDomain<D>,
  274. WordpressPlugin: WordpressPlugin<PathBuf>,
  275. WordpressTranslation: WordpressTranslation<PathBuf>,
  276. );
  277. pub fn serve_php<D, P: Into<PathBuf>, C: Into<FpmPoolConfig>>(domain: D, path: P, root_filename: &'static str, nginx_config: impl Into<String>, pool_config: C) -> ServePhp<D, PathBuf, FpmPoolConfig> {
  278. ServePhp(
  279. domain, path.into(), root_filename, nginx_config.into(), pool_config.into())
  280. }