2017-01-28 16:43:45 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
2015-09-24 23:57:44 +03:00
|
|
|
|
|
|
|
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)
|
2015-03-09 23:26:46 +03:00
|
|
|
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
|
2017-01-28 16:05:37 +03:00
|
|
|
|
|
|
|
import Options
|
|
|
|
|
|
|
|
import Paths_hapistrano (version)
|
|
|
|
import Data.Version (showVersion)
|
2015-09-24 23:57:44 +03:00
|
|
|
|
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
|
2015-03-13 04:31:47 +03:00
|
|
|
_ <- 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 ()
|
2017-01-28 16:05:37 +03:00
|
|
|
main = execParser (info (helper <*> opts) hapistranoDesc) >>= runOption
|
2015-09-24 23:57:44 +03:00
|
|
|
|
2017-01-28 16:05:37 +03:00
|
|
|
runOption :: Option -> IO ()
|
|
|
|
runOption (Command command) = runCommand command
|
|
|
|
runOption (Flag flag) = runFlag flag
|
2015-09-24 23:57:44 +03:00
|
|
|
|
2017-01-28 16:05:37 +03:00
|
|
|
runCommand :: Command -> IO ()
|
|
|
|
runCommand Deploy = configFromEnv >>= deploy
|
|
|
|
runCommand Rollback = configFromEnv >>= rollback
|
2015-09-24 23:57:44 +03:00
|
|
|
|
2017-01-28 16:05:37 +03:00
|
|
|
runFlag :: Flag -> IO ()
|
|
|
|
runFlag Version = printVersion
|
2015-09-24 23:57:44 +03:00
|
|
|
|
2017-01-28 16:05:37 +03:00
|
|
|
printVersion :: IO ()
|
|
|
|
printVersion = putStrLn $ "Hapistrano " ++ showVersion version
|