hapistrano/app/Main.hs

97 lines
2.7 KiB
Haskell
Raw Normal View History

2017-01-28 16:43:45 +03:00
{-# LANGUAGE CPP #-}
module Main (main) where
2014-06-04 01:15:39 +04:00
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.Compat (lookupEnv)
2015-03-12 00:34:30 +03:00
2015-09-09 21:17:59 +03:00
import System.Hapistrano (ReleaseFormat(..))
2014-06-04 01:15:39 +04:00
2017-01-28 16:43:45 +03:00
import System.Exit
import Options
import Paths_hapistrano (version)
import Data.Version (showVersion)
2017-01-28 16:43:45 +03:00
import qualified Text.Read as Read
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
import System.IO
die :: String -> IO a
die err = hPutStrLn stderr err >> exitFailure
#endif
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-09-25 00:53:46 +03:00
maybeDeployPath <- lookupEnv "DEPLOY_PATH"
maybeRepository <- lookupEnv "REPOSITORY"
maybeRevision <- lookupEnv "REVISION"
2017-01-28 16:43:45 +03:00
deployPath <- maybe (die (noEnv "DEPLOY_PATH")) return maybeDeployPath
repository <- maybe (die (noEnv "REPOSITORY")) return maybeRepository
revision <- maybe (die (noEnv "REVISION")) return maybeRevision
2015-03-12 00:34:30 +03:00
2017-01-28 16:43:45 +03:00
port <- lookupEnv "PORT"
2015-03-12 00:34:30 +03:00
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
2017-01-28 16:43:45 +03:00
, Hap.port = parsePort port
2014-06-04 01:15:39 +04:00
}
2015-09-25 00:53:46 +03:00
where
noEnv env = env ++ " environment variable does not exist"
2017-01-28 16:43:45 +03:00
parsePort maybePort = maybePort >>= Read.readMaybe
2014-06-04 01:15:39 +04:00
main :: IO ()
main = execParser (info (helper <*> opts) hapistranoDesc) >>= runOption
runOption :: Option -> IO ()
runOption (Command command) = runCommand command
runOption (Flag flag) = runFlag flag
runCommand :: Command -> IO ()
runCommand Deploy = configFromEnv >>= deploy
runCommand Rollback = configFromEnv >>= rollback
runFlag :: Flag -> IO ()
runFlag Version = printVersion
printVersion :: IO ()
printVersion = putStrLn $ "Hapistrano " ++ showVersion version