fix: Install Nginx before trying to create a webserver

fix: Nginx depends on LetsEncrypt/certbot
This commit is contained in:
Mahdi Dibaiee 2017-02-22 12:49:18 +03:30
parent d86daa863d
commit 62fe10ece6
4 changed files with 31 additions and 16 deletions

View File

@ -9,19 +9,26 @@ module System.Serverman.Actions.Install (installService) where
import System.Process import System.Process
import Control.Concurrent.Async import Control.Concurrent.Async
import Control.Monad.Free import Control.Monad.Free
import Control.Monad
class Installable a where class Installable a where
dependencies :: a -> [String] dependencies :: a -> [a]
package :: a -> OS -> String package :: a -> OS -> String
instance Installable Service where instance Installable Service where
dependencies NGINX = [LetsEncrypt]
dependencies _ = [] dependencies _ = []
package NGINX _ = "nginx" package NGINX _ = "nginx"
package MySQL _ = "mysql" package MySQL _ = "mysql"
package LetsEncrypt Arch = "certbot"
package LetsEncrypt _ = "letsencrypt"
installService :: Service -> OS -> IO () installService :: Service -> OS -> IO ()
installService service os = do installService service os = do
forM_ (dependencies service) (`installService` os)
let base = case os of let base = case os of
Arch -> ("pacman", ["-S", "--noconfirm", "--quiet"]) Arch -> ("pacman", ["-S", "--noconfirm", "--quiet"])
Debian -> ("apt-get", ["install", "-y"]) Debian -> ("apt-get", ["install", "-y"])
@ -29,13 +36,11 @@ module System.Serverman.Actions.Install (installService) where
_ -> ("echo", ["Unknown operating system"]) _ -> ("echo", ["Unknown operating system"])
pkg = package service os pkg = package service os
process <- async $ do process <- async $ do
result <- execute (fst base) (snd base ++ [pkg]) "" True result <- execute (fst base) (snd base ++ [pkg]) "" True
case result of case result of
Left err -> return () Left err -> return ()
Right stdout -> do Right _ -> do
putStrLn stdout
putStrLn $ "installed " ++ show service ++ "." putStrLn $ "installed " ++ show service ++ "."
wait process wait process

View File

@ -13,6 +13,7 @@ module System.Serverman.Actions.Nginx (nginx) where
import Control.Concurrent.Async import Control.Concurrent.Async
import Control.Monad import Control.Monad
import Control.Monad.Free import Control.Monad.Free
import Data.List
nginx :: ServerParams -> IO () nginx :: ServerParams -> IO ()
nginx params@(ServerParams { ssl, serverService, domain, directory, serverType }) = nginx params@(ServerParams { ssl, serverService, domain, directory, serverType }) =
@ -40,14 +41,7 @@ module System.Serverman.Actions.Nginx (nginx) where
when ssl $ do when ssl $ do
case serverType of case serverType of
Static -> do Static -> do
letsencrypt <- async $ do letsencrypt <- async $ createCert path "letsencrypt"
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
wait letsencrypt wait letsencrypt
_ -> do _ -> do
@ -66,3 +60,15 @@ module System.Serverman.Actions.Nginx (nginx) where
Right _ -> Right _ ->
putStrLn $ "restarted " ++ show serverService 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

View File

@ -3,6 +3,7 @@ module System.Serverman.Services ( Service(..)
data Service = NGINX data Service = NGINX
| MySQL | MySQL
| LetsEncrypt
deriving (Eq, Show) deriving (Eq, Show)
class Configurable a where class Configurable a where
@ -16,3 +17,4 @@ module System.Serverman.Services ( Service(..)
readsPrec _ service readsPrec _ service
| service == "nginx" = [(NGINX, [])] | service == "nginx" = [(NGINX, [])]
| service == "mysql" = [(MySQL, [])] | service == "mysql" = [(MySQL, [])]
| service == "letsencrypt" = [(LetsEncrypt, [])]

View File

@ -91,14 +91,16 @@ module System.Term ( initialize ) where
, S.serverType = serverType , S.serverType = serverType
, S.serverService = serviceName , S.serverService = serviceName
} }
S.run $ S.newServer params S.run $ S.detectOS >>= (S.install serviceName) >> S.newServer params
manualInstall (InstallParams { iService }) = do manualInstall (InstallParams { iService }) = do
S.run $ S.detectOS >>= (S.install (read iService)) S.run $ S.detectOS >>= (S.install (read iService))
databaseSetup (DatabaseParams { databaseName, dService }) = do databaseSetup (DatabaseParams { databaseName, dService }) = do
let serviceName = read dService
let params = S.DatabaseParams { S.database = databaseName 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