hapistrano/app/Main.hs

143 lines
3.8 KiB
Haskell
Raw Normal View History

{-# LANGUAGE RecordWildCards #-}
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 (getEnv)
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
import qualified Control.Monad as Monad
import qualified System.Console.GetOpt as GetOpt
import qualified System.Environment as Environment
import qualified System.Exit as Exit
import qualified System.Exit.Compat as Exit
import qualified System.IO as IO
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
}
data HapCommand
= HapDeploy
| HapRollback
deriving Show
parseHapCommand :: String -> Maybe HapCommand
parseHapCommand "deploy" = Just HapDeploy
parseHapCommand "rollback" = Just HapRollback
parseHapCommand _ = Nothing
data HapOptions =
HapOptions
{ hapCommand :: Maybe HapCommand
, hapHelp :: Bool
}
deriving Show
defaultHapOptions :: HapOptions
defaultHapOptions =
HapOptions
{ hapCommand = Nothing
, hapHelp = False
}
hapOptionDescriptions :: [GetOpt.OptDescr (HapOptions -> HapOptions)]
hapOptionDescriptions =
[ GetOpt.Option
['h']
["help"]
(GetOpt.NoArg (\hapOptions -> hapOptions { hapHelp = True }))
"Show this help text"
]
parseHapOptions :: [String] -> Either String HapOptions
parseHapOptions args =
case GetOpt.getOpt GetOpt.Permute hapOptionDescriptions args of
(options, [], []) ->
Right (foldl (flip id) defaultHapOptions options)
(options, [command], []) ->
case parseHapCommand command of
Nothing ->
Left ("Invalid argument: " ++ command)
maybeHC ->
Right (foldl (flip id) defaultHapOptions {hapCommand = maybeHC} options)
_ ->
Left "First argument must be either 'deploy' or 'rollback'."
hapHelpAction :: Maybe HapCommand -> IO ()
hapHelpAction _ =
IO.hPutStrLn IO.stdout hapUsage >> Exit.exitSuccess
hapUsage :: String
hapUsage =
GetOpt.usageInfo hapUsageHeader hapOptionDescriptions
hapUsageHeader :: String
hapUsageHeader =
"usage: hap [-h | --help] <command>\n"
2014-06-04 01:15:39 +04:00
main :: IO ()
main = do
eitherHapOptions <- fmap parseHapOptions Environment.getArgs
HapOptions{..} <- either Exit.die return eitherHapOptions
Monad.when hapHelp (hapHelpAction hapCommand)
hapConfiguration <- configFromEnv
case hapCommand of
Just HapDeploy -> deploy hapConfiguration
Just HapRollback -> rollback hapConfiguration
Nothing -> hapHelpAction Nothing