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 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

View File

@ -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

View File

@ -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, [])]

View File

@ -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