From 806916410994e5dcde6b457dd895ceb25e17b0e8 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Tue, 24 Sep 2019 17:04:53 +0200 Subject: [PATCH] Don't return actions from SymbolFactory --- src/symbols/factory.rs | 125 +++++++++++++++++------------------------ src/symbols/list.rs | 20 +++++++ 2 files changed, 72 insertions(+), 73 deletions(-) diff --git a/src/symbols/factory.rs b/src/symbols/factory.rs index 414e3f6..ce83718 100644 --- a/src/symbols/factory.rs +++ b/src/symbols/factory.rs @@ -9,14 +9,14 @@ use symbols::cron::Cron; use symbols::file::File; use symbols::git::checkout::GitCheckout; use symbols::hook::Hook; -use symbols::list::ListAction; +use symbols::list::List; use symbols::mariadb::{DatabaseDump, MariaDBDatabase, MariaDBUser}; use symbols::nginx::server::{php_server_config_snippet, server_config, NginxServer}; use symbols::owner::Owner; use symbols::stored_directory::{StorageDirection, StoredDirectory}; use symbols::systemd::reload::ReloadService; use symbols::tls::SelfSignedTlsCert; -use symbols::{Action, Symbol, SymbolRunner}; +use symbols::{Symbol, SymbolRunner}; pub trait Policy { fn user_name_for_host(&self, host_name: &'static str) -> String; @@ -57,42 +57,37 @@ impl<'b, C: 'b + CommandRunner, R: 'b + SymbolRunner, P: 'b + Policy> SymbolFact &'c self, host: &'static str, nginx_server_symbol: S, - ) -> Box { - Box::new(ListAction::new(vec![ - Box::new(SelfSignedTlsCert::new(host.into(), self.command_runner)) - .into_action(self.symbol_runner), - Box::new(Hook::new( + ) -> impl Symbol + 'a { + List::from(( + SelfSignedTlsCert::new(host.into(), self.command_runner), + Hook::new( nginx_server_symbol, ReloadService::new("nginx", self.command_runner), - )) - .into_action(self.symbol_runner), - Box::new(AcmeCert::new(host.into(), &self.acme_command_runner)) - .into_action(self.symbol_runner), - Box::new(Hook::new( + ), + AcmeCert::new(host.into(), &self.acme_command_runner), + Hook::new( AcmeCertChain::new(host.into(), &self.acme_command_runner), ReloadService::new("nginx", self.command_runner), - )) - .into_action(self.symbol_runner), - ])) + ), + )) } - pub fn get_nginx_acme_challenge_config<'a>(&'a self) -> Box { - Box::new(File::new( + pub fn get_nginx_acme_challenge_config<'a>(&'a self) -> impl Symbol + 'a { + File::new( "/etc/nginx/snippets/acme-challenge.conf", "location ^~ /.well-known/acme-challenge/ { alias /home/acme/challenges/; try_files $uri =404; }", - )) - .into_action(self.symbol_runner) + ) } fn get_php_fpm_pool_socket_path<'a>(&'a self, user_name: &str) -> String { format!("/run/php/{}.sock", user_name) } - fn get_php_fpm_pool<'a>(&'a self, user_name: &str) -> Box { + fn get_php_fpm_pool<'a>(&'a self, user_name: &str) -> impl Symbol + 'a { let socket = self.get_php_fpm_pool_socket_path(user_name); - Box::new(Hook::new( + Hook::new( File::new( format!("/etc/php/7.0/fpm/pool.d/{}.conf", user_name), format!( @@ -111,8 +106,7 @@ env[PATH] = /usr/local/bin:/usr/bin:/bin ), ), ReloadService::new("php7.0-fpm", self.command_runner), - )) - .into_action(self.symbol_runner) + ) } pub fn serve_php<'a>( @@ -120,10 +114,10 @@ env[PATH] = /usr/local/bin:/usr/bin:/bin host_name: &'static str, root_dir: Cow<'a, str>, additional_config: &'a str, - ) -> Box { + ) -> impl Symbol + 'a { let user_name = self.policy.user_name_for_host(host_name); let socket = self.get_php_fpm_pool_socket_path(&user_name); - Box::new(ListAction::new(vec![ + List::from(( self.get_php_fpm_pool(&user_name), self.get_nginx_acme_server( host_name, @@ -135,22 +129,22 @@ env[PATH] = /usr/local/bin:/usr/bin:/bin additional_config, ), ), - ])) + )) } pub fn serve_wordpress<'a>( &'a self, host_name: &'static str, root_dir: Cow<'a, str>, - ) -> Box { + ) -> impl Symbol + 'a { self.serve_php( host_name, root_dir, " - location / {{ - try_files $uri $uri/ /index.php?$args; - }} - ", + location / {{ + try_files $uri $uri/ /index.php?$args; + }} + ", ) } @@ -158,10 +152,10 @@ env[PATH] = /usr/local/bin:/usr/bin:/bin &'a self, host_name: &'static str, root_dir: &'static str, - ) -> Box { + ) -> impl Symbol + 'a { let user_name = self.policy.user_name_for_host(host_name); let socket = self.get_php_fpm_pool_socket_path(&user_name); - Box::new(ListAction::new(vec![ + List::from(( self.get_php_fpm_pool(&user_name), self.get_nginx_acme_server(host_name, NginxServer::new( @@ -190,14 +184,14 @@ env[PATH] = /usr/local/bin:/usr/bin:/bin socket)), self.command_runner )) - ])) + )) } pub fn serve_nextcloud<'a>( &'a self, host_name: &'static str, root_dir: Cow<'a, str>, - ) -> Box { + ) -> impl Symbol + 'a { self.serve_php( host_name, root_dir, @@ -257,14 +251,14 @@ env[PATH] = /usr/local/bin:/usr/bin:/bin &'a self, host_name: &'static str, target: &'static str, - ) -> Box { + ) -> impl Symbol + 'a { self.get_nginx_acme_server( host_name, NginxServer::new_redir(host_name, target, self.command_runner), ) } - pub fn serve_static<'a>(&'a self, host_name: &'static str, dir: &'a str) -> Box { + pub fn serve_static<'a>(&'a self, host_name: &'static str, dir: &'a str) -> impl Symbol + 'a { self.get_nginx_acme_server( host_name, NginxServer::new_static(host_name, dir, self.command_runner), @@ -275,47 +269,42 @@ env[PATH] = /usr/local/bin:/usr/bin:/bin &'a self, storage_name: &'static str, target: T, - ) -> (Box, Box) { + ) -> (impl Symbol + 'a, impl Symbol + 'a) { let data = SimpleStorage::new("/root/data".to_string(), storage_name.to_string()); let string_target = target.into(); ( - Box::new(StoredDirectory::new( + StoredDirectory::new( string_target.clone().into(), data.clone(), StorageDirection::Save, self.command_runner, - )) - .into_action(self.symbol_runner), - Box::new(StoredDirectory::new( + ), + StoredDirectory::new( string_target.into(), data.clone(), StorageDirection::Load, self.command_runner, - )) - .into_action(self.symbol_runner), + ), ) } - pub fn get_mariadb_database<'a>(&'a self, name: &'static str) -> Box { + pub fn get_mariadb_database<'a>(&'a self, name: &'static str) -> impl Symbol + 'a { let db_dump = SimpleStorage::new("/root/data".to_string(), format!("{}.sql", name)); - Box::new(ListAction::new(vec![ - Box::new(MariaDBDatabase::new( + List::from(( + MariaDBDatabase::new( name.into(), db_dump .read_filename() .expect("Initial db dump missing") .into(), self.command_runner, - )) - .into_action(self.symbol_runner), - Box::new(DatabaseDump::new(name, db_dump, self.command_runner)) - .into_action(self.symbol_runner), - ])) + ), + DatabaseDump::new(name, db_dump, self.command_runner), + )) } - pub fn get_mariadb_user<'a>(&'a self, user_name: &'static str) -> Box { - Box::new(MariaDBUser::new(user_name.into(), self.command_runner)) - .into_action(self.symbol_runner) + pub fn get_mariadb_user<'a>(&'a self, user_name: &'static str) -> impl Symbol + 'a { + MariaDBUser::new(user_name.into(), self.command_runner) } pub fn get_git_checkout<'a, T: 'a + AsRef>( @@ -323,37 +312,27 @@ env[PATH] = /usr/local/bin:/usr/bin:/bin target: T, source: &'a str, branch: &'a str, - ) -> Box { - Box::new(GitCheckout::new( - target, - source, - branch, - self.command_runner, - )) - .into_action(self.symbol_runner) + ) -> impl Symbol + 'a { + GitCheckout::new(target, source, branch, self.command_runner) } - pub fn get_owner<'a, F: 'a + AsRef>( - &'a self, - file: F, - user: &'a str, - ) -> Box { - Box::new(Owner::new(file, user.into(), self.command_runner)).into_action(self.symbol_runner) + pub fn get_owner<'a, F: 'a + AsRef>(&'a self, file: F, user: &'a str) -> impl Symbol + 'a { + Owner::new(file, user.into(), self.command_runner) } pub fn get_file<'a, F: 'a + Deref, Q: 'a + AsRef>( &'a self, path: Q, content: F, - ) -> Box { - Box::new(File::new(path, content)).into_action(self.symbol_runner) + ) -> impl Symbol + 'a { + File::new(path, content) } pub fn get_cron<'a, T: 'a + Deref, U: 'a + Deref>( &'a self, user: U, content: T, - ) -> Box { - Box::new(Cron::new(user, content, self.command_runner)).into_action(self.symbol_runner) + ) -> impl Symbol + 'a { + Cron::new(user, content, self.command_runner) } } diff --git a/src/symbols/list.rs b/src/symbols/list.rs index 7d83523..dd827cf 100644 --- a/src/symbols/list.rs +++ b/src/symbols/list.rs @@ -75,6 +75,26 @@ impl<'a> Symbol for List<'a> { } } +impl<'a, A: 'a + Symbol, B: 'a + Symbol> From<(A, B)> for List<'a> { + fn from((a, b): (A, B)) -> Self { + Self::new(vec![Box::new(a), Box::new(b)]) + } +} + +impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol> From<(A, B, C)> for List<'a> { + fn from((a, b, c): (A, B, C)) -> Self { + Self::new(vec![Box::new(a), Box::new(b), Box::new(c)]) + } +} + +impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol> From<(A, B, C, D)> + for List<'a> +{ + fn from((a, b, c, d): (A, B, C, D)) -> Self { + Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d)]) + } +} + struct SymbolListAction<'a> { runner: &'a dyn SymbolRunner, symbols: &'a [Box],