Browse Source

More flexible PHP FPM pool config

master
Adrian Heine 2 years ago
parent
commit
21018bd6f6
  1. 14
      src/builder.rs
  2. 6
      src/locator.rs
  3. 21
      src/resources/mod.rs
  4. 37
      src/templates/php.rs

14
src/builder.rs

@ -33,7 +33,7 @@ use crate::symbols::wordpress::{
Plugin as WordpressPluginSymbol, Translation as WordpressTranslationSymbol,
};
use crate::templates::nginx;
use crate::templates::php::fpm_pool_config as php_fpm_pool_config;
use crate::templates::php::{fpm_pool_config as php_fpm_pool_config, FpmPoolConfig as PhpFpmPoolConfig};
use crate::templates::systemd::{
nodejs_service as systemd_nodejs_service, socket_service as systemd_socket_service,
};
@ -250,16 +250,16 @@ impl<D: AsRef<str> + Clone + Display> ImplementationBuilder<ServeCustom<D>> for
}
}
impl<D: Clone + Display, P: AsRef<Path>> ImplementationBuilder<ServePhp<D, P>> for DefaultBuilder {
impl<D: Clone + Display, P: AsRef<Path>, C: Clone + Into<PhpFpmPoolConfig>> ImplementationBuilder<ServePhp<D, P, C>> for DefaultBuilder {
type Prerequisites = (
PhpFpmPool<D>,
CertChain<D>,
Key<D>,
AcmeChallengesNginxSnippet,
);
fn prerequisites(resource: &ServePhp<D, P>) -> Self::Prerequisites {
fn prerequisites(resource: &ServePhp<D, P, C>) -> Self::Prerequisites {
(
PhpFpmPool(resource.0.clone(), 10),
PhpFpmPool(resource.0.clone(), resource.4.clone().into()),
CertChain(resource.0.clone()),
Key(resource.0.clone()),
AcmeChallengesNginxSnippet,
@ -271,8 +271,8 @@ impl<D: Clone + Display, P: AsRef<Path>> ImplementationBuilder<ServePhp<D, P>> f
ReloadServiceSymbol<StdCommandRunner, StdCommandRunner, &'static str>,
);
fn create(
resource: &ServePhp<D, P>,
target: &<ServePhp<D, P> as Resource>::Artifact,
resource: &ServePhp<D, P, C>,
target: &<ServePhp<D, P, C> as Resource>::Artifact,
(pool, cert, key, challenges_snippet_path): <Self::Prerequisites as ToArtifact>::Artifact,
) -> Self::Implementation {
(
@ -428,7 +428,7 @@ impl<D: Clone> ImplementationBuilder<PhpFpmPool<D>> for DefaultBuilder {
(
FileSymbol::new(
conf_path.clone().into(),
php_fpm_pool_config(&user_name.0, &socket_path, resource.1),
php_fpm_pool_config(&user_name.0, &socket_path, &resource.1),
),
ReloadServiceSymbol::new(StdCommandRunner, service_name.0.clone()),
)

6
src/locator.rs

@ -298,11 +298,11 @@ impl<D: AsRef<Path>, POLICY> ResourceLocator<ServeCustom<D>> for DefaultLocator<
}
}
impl<D: AsRef<Path>, P, POLICY> ResourceLocator<ServePhp<D, P>> for DefaultLocator<POLICY> {
impl<D: AsRef<Path>, P, C, POLICY> ResourceLocator<ServePhp<D, P, C>> for DefaultLocator<POLICY> {
type Prerequisites = ();
fn locate(
resource: &ServePhp<D, P>,
) -> (<ServePhp<D, P> as Resource>::Artifact, Self::Prerequisites) {
resource: &ServePhp<D, P, C>,
) -> (<ServePhp<D, P, C> as Resource>::Artifact, Self::Prerequisites) {
(
PathArtifact::from(Path::new("/etc/nginx/sites-enabled/").join(&resource.0)),
(),

21
src/resources/mod.rs

@ -2,6 +2,7 @@ use crate::artifacts::{
DatabaseName as DatabaseNameArtifact, Path as PathArtifact, ServiceName as ServiceNameArtifact,
UserName as UserNameArtifact,
};
use crate::templates::php::FpmPoolConfig;
use std::hash::Hash;
use std::path::PathBuf;
@ -146,8 +147,14 @@ impl<D> Resource for ServeCustom<D> {
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct ServePhp<D, P>(pub D, pub P, pub &'static str, pub String, pub usize);
impl<D, P> Resource for ServePhp<D, P> {
pub struct ServePhp<D, P, C>(
pub D,
pub P,
pub &'static str,
pub String,
pub C,
);
impl<D, P, C> Resource for ServePhp<D, P, C> {
type Artifact = PathArtifact;
}
@ -177,7 +184,7 @@ impl Resource for DefaultServer {
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct PhpFpmPool<D>(pub D, pub usize);
pub struct PhpFpmPool<D>(pub D, pub FpmPoolConfig);
impl<D> Resource for PhpFpmPool<D> {
type Artifact = (
PathArtifact,
@ -275,6 +282,7 @@ macro_rules! default_resources {
}
}
// Only one enum entry per resource type, otherwise the equality checks fail
default_resources!(
AcmeAccountKey: AcmeAccountKey,
AcmeChallengesDir: AcmeChallengesDir,
@ -301,7 +309,7 @@ default_resources!(
PhpFpmPool: PhpFpmPool<D>,
ServeCustom: ServeCustom<D>,
ServeService: ServeService<D, PathBuf>,
ServePhp: ServePhp<D, PathBuf>,
ServePhp: ServePhp<D, PathBuf, FpmPoolConfig>,
ServeRedir: ServeRedir<D>,
ServeStatic: ServeStatic<D, PathBuf>,
StoredDirectory: StoredDirectory<PathBuf>,
@ -310,3 +318,8 @@ default_resources!(
WordpressPlugin: WordpressPlugin<PathBuf>,
WordpressTranslation: WordpressTranslation<PathBuf>,
);
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> {
ServePhp(
domain, path.into(), root_filename, nginx_config.into(), pool_config.into())
}

37
src/templates/php.rs

@ -1,9 +1,40 @@
use std::fmt::{Display, Error, Formatter};
use std::path::Path;
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
pub struct FpmPoolConfig {
max_children: usize,
custom: Option<String>,
}
impl Display for FpmPoolConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
match &self.custom {
None => write!(f, "pm.max_children = {}", self.max_children),
Some(custom) => write!(f, "pm.max_children = {}\n{}", self.max_children, custom),
}
}
}
impl From<usize> for FpmPoolConfig {
fn from(max_children: usize) -> Self {
Self {
max_children,
custom: None,
}
}
}
impl FpmPoolConfig {
pub fn new(max_children: usize, custom: impl Into<String>) -> Self {
Self { max_children, custom: Some(custom.into()) }
}
}
pub fn fpm_pool_config<U: AsRef<str>, S: AsRef<Path>>(
user_name: U,
socket_path: S,
max_children: usize,
config: &FpmPoolConfig,
) -> String {
format!(
"[{0}]
@ -13,12 +44,12 @@ group = www-data
listen = {1}
listen.owner = www-data
pm = ondemand
pm.max_children = {2}
catch_workers_output = yes
env[PATH] = /usr/local/bin:/usr/bin:/bin
{2}
",
user_name.as_ref(),
socket_path.as_ref().to_str().unwrap(),
max_children,
config
)
}
Loading…
Cancel
Save