Compare commits

...

3 commits

Author SHA1 Message Date
4938a51ad7 Pass &str instead of cloning String 2021-12-26 00:07:44 +01:00
426b531e4c fmt 2021-12-26 00:07:34 +01:00
fb23353453 Use bytes for command stdin 2021-12-26 00:07:11 +01:00
7 changed files with 43 additions and 28 deletions

View file

@ -33,7 +33,9 @@ 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, FpmPoolConfig as PhpFpmPoolConfig};
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,7 +252,9 @@ impl<D: AsRef<str> + Clone + Display> ImplementationBuilder<ServeCustom<D>> for
}
}
impl<D: Clone + Display, P: AsRef<Path>, C: Clone + Into<PhpFpmPoolConfig>> ImplementationBuilder<ServePhp<D, P, C>> 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>,
@ -786,6 +790,6 @@ impl<D: Clone> ImplementationBuilder<Cron<D>> for DefaultBuilder {
(): &<Cron<D> as Resource>::Artifact,
user_name: <Self::Prerequisites as ToArtifact>::Artifact,
) -> Self::Implementation {
CronSymbol::new((user_name.0).0, resource.1.clone(), &StdCommandRunner)
CronSymbol::new((user_name.0).0, &resource.1, &StdCommandRunner)
}
}

View file

@ -33,17 +33,17 @@ pub fn get_output(output: Output) -> Result<Vec<u8>, Box<dyn Error>> {
#[async_trait(?Send)]
pub trait CommandRunner {
async fn run(&self, program: &str, args: &[&OsStr], stdin: &str) -> IoResult<Output>;
async fn run(&self, program: &str, args: &[&OsStr], stdin: &[u8]) -> IoResult<Output>;
async fn run_with_args(&self, program: &str, args: &[&OsStr]) -> IoResult<Output> {
self.run(program, args, "").await
self.run(program, args, b"").await
}
async fn get_output(&self, program: &str, args: &[&OsStr]) -> Result<Vec<u8>, Box<dyn Error>> {
let output = self.run_with_args(program, args).await?;
get_output(output)
}
async fn run_successfully(&self, program: &str, args: &[&OsStr]) -> Result<(), Box<dyn Error>> {
is_success(self.run(program, args, "").await)?;
is_success(self.run(program, args, b"").await)?;
Ok(())
}
async fn get_stderr(&self, program: &str, args: &[&OsStr]) -> Result<Vec<u8>, Box<dyn Error>> {
@ -56,7 +56,7 @@ pub struct StdCommandRunner;
#[async_trait(?Send)]
impl CommandRunner for StdCommandRunner {
async fn run(&self, program: &str, args: &[&OsStr], input: &str) -> IoResult<Output> {
async fn run(&self, program: &str, args: &[&OsStr], input: &[u8]) -> IoResult<Output> {
//println!("{} {:?}", program, args);
let mut child = Command::new(program)
.args(args)
@ -67,7 +67,7 @@ impl CommandRunner for StdCommandRunner {
.expect("Failed to spawn child process");
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
stdin
.write_all(input.as_bytes())
.write_all(input)
.await
.expect("Failed to write to stdin");
let res = child.wait_with_output().await;
@ -122,7 +122,7 @@ impl Drop for TempSetEnv<'_> {
#[async_trait(?Send)]
impl<U: AsRef<str>, C: CommandRunner> CommandRunner for SetuidCommandRunner<'_, U, C> {
async fn run(&self, program: &str, args: &[&OsStr], input: &str) -> IoResult<Output> {
async fn run(&self, program: &str, args: &[&OsStr], input: &[u8]) -> IoResult<Output> {
let uid = get_user_by_name(self.user_name.as_ref())
.expect("User does not exist")
.uid();
@ -140,7 +140,7 @@ impl<U: AsRef<str>, C: CommandRunner> CommandRunner for SetuidCommandRunner<'_,
.expect("Failed to spawn child process");
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
stdin
.write_all(input.as_bytes())
.write_all(input)
.await
.expect("Failed to write to stdin");
let res = child.wait_with_output().await;
@ -179,7 +179,7 @@ impl<'a, C> CommandRunner for SuCommandRunner<'a, C>
where
C: 'a + CommandRunner,
{
async fn run(&self, program: &str, args: &[&OsStr], input: &str) -> IoResult<Output> {
async fn run(&self, program: &str, args: &[&OsStr], input: &[u8]) -> IoResult<Output> {
let raw_new_args = [self.user_name, "-s", "/usr/bin/env", "--", program];
let mut new_args: Vec<&OsStr> = raw_new_args.iter().map(AsRef::as_ref).collect();
new_args.extend_from_slice(args);
@ -201,8 +201,8 @@ mod test {
run(async {
let args = args!["1"];
let start = Instant::now();
let res = c.run("sleep", args, "").fuse();
let ps = c.run("ps", args![], "").fuse();
let res = c.run("sleep", args, b"").fuse();
let ps = c.run("ps", args![], b"").fuse();
futures_util::pin_mut!(res, ps);
loop {
futures_util::select! {

View file

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

View file

@ -147,13 +147,7 @@ impl<D> Resource for ServeCustom<D> {
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct ServePhp<D, P, C>(
pub D,
pub P,
pub &'static str,
pub String,
pub C,
);
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;
}
@ -319,7 +313,18 @@ default_resources!(
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> {
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())
domain,
path.into(),
root_filename,
nginx_config.into(),
pool_config.into(),
)
}

View file

@ -21,13 +21,13 @@ impl<'r, U, R> Cron<'r, String, U, R> {
}
#[async_trait(?Send)]
impl<C: AsRef<str>, U: AsRef<str>, R: CommandRunner> Symbol for Cron<'_, C, U, R> {
impl<C: AsRef<[u8]>, U: AsRef<str>, R: CommandRunner> Symbol for Cron<'_, C, U, R> {
async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
let tab = self
.command_runner
.get_output("crontab", args!["-l", "-u", self.user.as_ref()])
.await?;
Ok(tab == self.content.as_ref().as_bytes())
Ok(tab == self.content.as_ref())
}
async fn execute(&self) -> Result<(), Box<dyn Error>> {

View file

@ -110,9 +110,9 @@ mod test {
}
#[async_trait(?Send)]
impl CommandRunner for DummyCommandRunner {
async fn run(&self, program: &str, args: &[&OsStr], stdin: &str) -> IoResult<Output> {
async fn run(&self, program: &str, args: &[&OsStr], stdin: &[u8]) -> IoResult<Output> {
assert_eq!(program, "git");
assert_eq!(stdin, "");
assert_eq!(stdin, b"");
sleep(Duration::from_millis(50)).await;
self
.args

View file

@ -27,7 +27,10 @@ impl From<usize> for FpmPoolConfig {
impl FpmPoolConfig {
pub fn new(max_children: usize, custom: impl Into<String>) -> Self {
Self { max_children, custom: Some(custom.into()) }
Self {
max_children,
custom: Some(custom.into()),
}
}
}