hapistrano/app/Main.hs

75 lines
2.1 KiB
Haskell
Raw Normal View History

2014-06-04 01:15:39 +04:00
module Main where
2015-03-12 00:34:30 +03:00
import qualified System.Hapistrano as Hap
2014-06-04 01:15:39 +04:00
import Control.Monad (void)
import System.Environment (getArgs, getEnv)
import System.Environment.Compat (lookupEnv)
2014-06-04 01:15:39 +04:00
import System.IO (hPutStrLn, stderr)
2015-03-12 00:34:30 +03:00
import System.Exit (exitFailure)
2015-09-09 21:17:59 +03:00
import System.Hapistrano (ReleaseFormat(..))
2014-06-04 01:15:39 +04:00
-- | Rolls back to previous release.
rollback :: Hap.Config -> IO ()
rollback cfg =
2015-03-12 22:51:52 +03:00
Hap.runRC errorHandler successHandler cfg $ do
2014-06-04 01:26:13 +04:00
2015-03-12 00:34:30 +03:00
_ <- Hap.rollback
2014-06-04 01:26:13 +04:00
void Hap.restartServerCommand
2014-06-04 01:15:39 +04:00
where
errorHandler = Hap.defaultErrorHandler
successHandler = Hap.defaultSuccessHandler
-- | Deploys the current release with Config options.
deploy :: Hap.Config -> IO ()
deploy cfg =
2015-03-12 22:51:52 +03:00
Hap.runRC errorHandler successHandler cfg $ do
_ <- Hap.pushRelease >>= Hap.runBuild >>= Hap.activateRelease
2014-06-04 01:15:39 +04:00
2014-06-04 01:26:13 +04:00
void Hap.restartServerCommand
2014-06-04 01:15:39 +04:00
where
errorHandler = Hap.defaultErrorHandler
successHandler = Hap.defaultSuccessHandler
-- | Retrieves the configuration from environment variables.
configFromEnv :: IO Hap.Config
configFromEnv = do
2015-03-12 00:34:30 +03:00
deployPath <- getEnv "DEPLOY_PATH"
repository <- getEnv "REPOSITORY"
revision <- getEnv "REVISION"
host <- lookupEnv "HOST"
2014-06-04 01:15:39 +04:00
buildScript <- lookupEnv "BUILD_SCRIPT"
restartCommand <- lookupEnv "RESTART_COMMAND"
2015-03-12 00:34:30 +03:00
return Hap.Config { Hap.deployPath = deployPath
, Hap.host = host
, Hap.releaseFormat = Short
, Hap.repository = repository
, Hap.revision = revision
, Hap.buildScript = buildScript
, Hap.restartCommand = restartCommand
2014-06-04 01:15:39 +04:00
}
main :: IO ()
main = do
args <- getArgs
case args of
[] -> do
hPutStrLn stderr
"First argument must be either 'deploy' or 'rollback'."
exitFailure
arg1:_ -> do
cfg <- configFromEnv
case arg1 of
"deploy" -> deploy cfg
"rollback" -> rollback cfg
_ -> do
hPutStrLn stderr $ "Invalid argument: " ++ arg1
exitFailure