From b53267f406a7d882288bd08e82a6f2c1dcdac38a Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Sat, 17 Oct 2020 23:32:53 +0200 Subject: [PATCH] Add postgresql database --- src/builder.rs | 27 +++++++++++++++++++++++++-- src/locator.rs | 23 +++++++++++++++++++++-- src/resources/mod.rs | 7 +++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 48ebca6..7a89085 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -2,12 +2,13 @@ use crate::command_runner::{SetuidCommandRunner, StdCommandRunner}; use crate::resources::{ AcmeAccountKey, AcmeChallengesDir, AcmeChallengesNginxSnippet, AcmeRootCert, AcmeUser, Cert, CertChain, Cron, Csr, DefaultServer, Dir, File, GitCheckout, Key, KeyAndCertBundle, - LoadedDirectory, MariaDbDatabase, MariaDbUser, NpmInstall, Owner, PhpFpmPool, Resource, - ServeCustom, ServePhp, ServeRedir, ServeService, ServeStatic, StoredDirectory, + LoadedDirectory, MariaDbDatabase, MariaDbUser, NpmInstall, Owner, PhpFpmPool, PostgresqlDatabase, + Resource, ServeCustom, ServePhp, ServeRedir, ServeService, ServeStatic, StoredDirectory, SystemdSocketService, User, UserForDomain, WordpressPlugin, WordpressTranslation, }; use crate::static_files::LETS_ENCRYPT_X3_CROSS_SIGNED; use crate::storage::SimpleStorage; +use crate::storage::Storage; use crate::symbols::acme::Cert as CertSymbol; use crate::symbols::concat::Concat as ConcatSymbol; use crate::symbols::cron::Cron as CronSymbol; @@ -19,6 +20,7 @@ use crate::symbols::mariadb::{ }; use crate::symbols::npm::Install as NpmInstallSymbol; use crate::symbols::owner::Owner as OwnerSymbol; +use crate::symbols::postgresql::PostgreSQLDatabase as PostgreSQLDatabaseSymbol; use crate::symbols::saved_directory::{SavedDirectory as SavedDirectorySymbol, StorageDirection}; use crate::symbols::systemd::{ ReloadService as ReloadServiceSymbol, UserService as UserServiceSymbol, @@ -715,6 +717,27 @@ impl ImplementationBuilder> for DefaultBuilder { } } +impl ImplementationBuilder> for DefaultBuilder { + type Prerequisites = (); + fn prerequisites(resource: &PostgresqlDatabase) -> Self::Prerequisites { + () + } + + type Implementation = (PostgreSQLDatabaseSymbol<'static, String, String, StdCommandRunner>,); + fn create( + _resource: &PostgresqlDatabase, + (db_name, data_path): & as Resource>::Artifact, + _: ::Artifact, + ) -> Self::Implementation { + let db_dump = SimpleStorage::new(data_path.clone().into()); + (PostgreSQLDatabaseSymbol::new( + db_name.0.clone(), + db_dump.read_filename().unwrap().to_str().unwrap().into(), + &StdCommandRunner, + ),) + } +} + impl> ImplementationBuilder> for DefaultBuilder { type Prerequisites = Dir; fn prerequisites(resource: &WordpressPlugin

) -> Self::Prerequisites { diff --git a/src/locator.rs b/src/locator.rs index d1f5251..bdf17a7 100644 --- a/src/locator.rs +++ b/src/locator.rs @@ -5,8 +5,8 @@ use crate::artifacts::{ use crate::resources::{ AcmeAccountKey, AcmeChallengesDir, AcmeChallengesNginxSnippet, AcmeRootCert, AcmeUser, Cert, CertChain, Cron, Csr, DefaultServer, Dir, File, GitCheckout, Key, KeyAndCertBundle, - LoadedDirectory, MariaDbDatabase, MariaDbUser, NpmInstall, Owner, PhpFpmPool, Resource, - ServeCustom, ServePhp, ServeRedir, ServeService, ServeStatic, StoredDirectory, + LoadedDirectory, MariaDbDatabase, MariaDbUser, NpmInstall, Owner, PhpFpmPool, PostgresqlDatabase, + Resource, ServeCustom, ServePhp, ServeRedir, ServeService, ServeStatic, StoredDirectory, SystemdSocketService, User, UserForDomain, WordpressPlugin, WordpressTranslation, }; use crate::to_artifact::ToArtifact; @@ -422,6 +422,25 @@ impl, P: Policy> ResourceLocator> for DefaultLocato } } +impl, P: Policy> ResourceLocator> for DefaultLocator

{ + type Prerequisites = (); + fn locate( + resource: &PostgresqlDatabase, + ) -> ( + as Resource>::Artifact, + Self::Prerequisites, + ) { + let ((user_name, _), ()) = Self::locate(&UserForDomain(&resource.0)); + ( + ( + DatabaseNameArtifact(user_name.0.clone()), + PathArtifact::from(P::path_for_data(format!("{}.sql", user_name.0))), + ), + (), + ) + } +} + impl ResourceLocator> for DefaultLocator { type Prerequisites = (); fn locate( diff --git a/src/resources/mod.rs b/src/resources/mod.rs index a590c47..75f6d3e 100644 --- a/src/resources/mod.rs +++ b/src/resources/mod.rs @@ -200,6 +200,12 @@ impl Resource for MariaDbUser { type Artifact = UserNameArtifact; } +#[derive(Debug, Hash, PartialEq, Eq)] +pub struct PostgresqlDatabase(pub D); +impl Resource for PostgresqlDatabase { + type Artifact = (DatabaseNameArtifact, PathArtifact); +} + #[derive(Debug, Hash, PartialEq, Eq)] pub struct WordpressPlugin

(pub P, pub &'static str); impl

Resource for WordpressPlugin

{ @@ -288,6 +294,7 @@ default_resources!( LoadedDirectory: LoadedDirectory, MariaDbDatabase: MariaDbDatabase, MariaDbUser: MariaDbUser, + PostgresqlDatabase: PostgresqlDatabase, SystemdSocketService: SystemdSocketService, NpmInstall: NpmInstall, Owner: Owner,