diff --git a/src/System/Serverman/Actions/Install.hs b/src/System/Serverman/Actions/Install.hs index ae05b99..f8fc382 100644 --- a/src/System/Serverman/Actions/Install.hs +++ b/src/System/Serverman/Actions/Install.hs @@ -9,19 +9,26 @@ module System.Serverman.Actions.Install (installService) where import System.Process import Control.Concurrent.Async import Control.Monad.Free + import Control.Monad class Installable a where - dependencies :: a -> [String] + dependencies :: a -> [a] package :: a -> OS -> String instance Installable Service where + dependencies NGINX = [LetsEncrypt] dependencies _ = [] package NGINX _ = "nginx" package MySQL _ = "mysql" + package LetsEncrypt Arch = "certbot" + package LetsEncrypt _ = "letsencrypt" + installService :: Service -> OS -> IO () installService service os = do + forM_ (dependencies service) (`installService` os) + let base = case os of Arch -> ("pacman", ["-S", "--noconfirm", "--quiet"]) Debian -> ("apt-get", ["install", "-y"]) @@ -29,13 +36,11 @@ module System.Serverman.Actions.Install (installService) where _ -> ("echo", ["Unknown operating system"]) pkg = package service os - process <- async $ do result <- execute (fst base) (snd base ++ [pkg]) "" True case result of Left err -> return () - Right stdout -> do - putStrLn stdout + Right _ -> do putStrLn $ "installed " ++ show service ++ "." wait process diff --git a/src/System/Serverman/Actions/Nginx.hs b/src/System/Serverman/Actions/Nginx.hs index 307a460..1d9ffb1 100644 --- a/src/System/Serverman/Actions/Nginx.hs +++ b/src/System/Serverman/Actions/Nginx.hs @@ -13,6 +13,7 @@ module System.Serverman.Actions.Nginx (nginx) where import Control.Concurrent.Async import Control.Monad import Control.Monad.Free + import Data.List nginx :: ServerParams -> IO () nginx params@(ServerParams { ssl, serverService, domain, directory, serverType }) = @@ -40,15 +41,8 @@ module System.Serverman.Actions.Nginx (nginx) where when ssl $ do case serverType of Static -> do - letsencrypt <- async $ do - result <- execute "certbot" ["certonly", "--webroot", "--webroot-path", directory, "-d", domain] "" True - case result of - Left _ -> return () - Right _ -> do - putStrLn $ "created a certificate for " ++ domain - writeFile path (show params) - wait =<< restart - + letsencrypt <- async $ createCert path "letsencrypt" + wait letsencrypt _ -> do putStrLn $ "you should use letsencrypt to create a certificate for your domain" @@ -66,3 +60,15 @@ module System.Serverman.Actions.Nginx (nginx) where Right _ -> putStrLn $ "restarted " ++ show serverService + createCert path cmd = do + result <- execute cmd ["certonly", "--webroot", "--webroot-path", directory, "-d", domain] "" False + case result of + Left _ -> if cmd == "letsencrypt" then createCert path "certbot" else return () + Right stdout -> do + putStrLn stdout + + when (not ("error" `isInfixOf` stdout)) $ do + writeFile path (show params) + wait =<< restart + + diff --git a/src/System/Serverman/Services.hs b/src/System/Serverman/Services.hs index 5985c91..39903f5 100644 --- a/src/System/Serverman/Services.hs +++ b/src/System/Serverman/Services.hs @@ -3,6 +3,7 @@ module System.Serverman.Services ( Service(..) data Service = NGINX | MySQL + | LetsEncrypt deriving (Eq, Show) class Configurable a where @@ -16,3 +17,4 @@ module System.Serverman.Services ( Service(..) readsPrec _ service | service == "nginx" = [(NGINX, [])] | service == "mysql" = [(MySQL, [])] + | service == "letsencrypt" = [(LetsEncrypt, [])] diff --git a/src/System/Term.hs b/src/System/Term.hs index e6ab98c..5c5de83 100644 --- a/src/System/Term.hs +++ b/src/System/Term.hs @@ -91,14 +91,16 @@ module System.Term ( initialize ) where , S.serverType = serverType , S.serverService = serviceName } - S.run $ S.newServer params + S.run $ S.detectOS >>= (S.install serviceName) >> S.newServer params manualInstall (InstallParams { iService }) = do S.run $ S.detectOS >>= (S.install (read iService)) databaseSetup (DatabaseParams { databaseName, dService }) = do + let serviceName = read dService + let params = S.DatabaseParams { S.database = databaseName - , S.databaseService = read dService } + , S.databaseService = serviceName } - S.run $ S.newDatabase params + S.run $ S.detectOS >>= (S.install serviceName) >> S.newDatabase params