2022-09-08 17:34:24 +03:00
|
|
|
|
{-# LANGUAGE CPP #-}
|
2017-12-27 05:46:44 +03:00
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2015-09-24 23:57:44 +03:00
|
|
|
|
|
|
|
|
|
module Main (main) where
|
2014-06-04 01:15:39 +04:00
|
|
|
|
|
2017-12-27 20:44:53 +03:00
|
|
|
|
import Control.Concurrent.Async
|
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
|
import Control.Monad
|
2022-01-19 21:14:34 +03:00
|
|
|
|
#if !MIN_VERSION_base(4,13,0)
|
2022-09-08 17:34:24 +03:00
|
|
|
|
import Data.Monoid ((<>))
|
2022-01-19 21:14:34 +03:00
|
|
|
|
#endif
|
2022-09-08 17:34:24 +03:00
|
|
|
|
import Data.Version (showVersion)
|
|
|
|
|
import qualified Data.Yaml.Config as Yaml
|
2017-12-27 05:46:44 +03:00
|
|
|
|
import Development.GitRev
|
2022-09-08 17:34:24 +03:00
|
|
|
|
import Formatting (formatToString, string, (%))
|
|
|
|
|
import Options.Applicative hiding (str)
|
|
|
|
|
import Paths_hapistrano (version)
|
2017-12-27 20:44:53 +03:00
|
|
|
|
import System.Exit
|
2022-09-08 17:34:24 +03:00
|
|
|
|
import qualified System.Hapistrano as Hap
|
|
|
|
|
import qualified System.Hapistrano.Config as C
|
2022-04-19 22:03:17 +03:00
|
|
|
|
import qualified System.Hapistrano.Maintenance as Hap
|
2017-12-27 20:44:53 +03:00
|
|
|
|
import System.Hapistrano.Types
|
|
|
|
|
import System.IO
|
2017-01-28 16:43:45 +03:00
|
|
|
|
|
2017-02-06 18:04:00 +03:00
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
parserInfo :: ParserInfo Opts
|
2017-03-30 13:56:54 +03:00
|
|
|
|
parserInfo =
|
|
|
|
|
info
|
|
|
|
|
(helper <*> versionOption <*> optionParser)
|
|
|
|
|
(fullDesc <> progDesc "Deploy tool for Haskell applications" <>
|
|
|
|
|
header "Hapistrano - A deployment library for Haskell applications")
|
|
|
|
|
where
|
|
|
|
|
versionOption :: Parser (a -> a)
|
2017-12-27 05:46:44 +03:00
|
|
|
|
versionOption = infoOption
|
|
|
|
|
(formatToString
|
|
|
|
|
("Hapistrano: "% string
|
|
|
|
|
% "\nbranch: " % string
|
|
|
|
|
% "\nrevision: " % string)
|
2017-03-30 13:56:54 +03:00
|
|
|
|
(showVersion version)
|
2017-12-27 05:46:44 +03:00
|
|
|
|
$(gitBranch)
|
|
|
|
|
$(gitHash))
|
|
|
|
|
(long "version" <> short 'v' <> help "Show version information")
|
2017-02-06 18:04:00 +03:00
|
|
|
|
|
|
|
|
|
optionParser :: Parser Opts
|
|
|
|
|
optionParser = Opts
|
2017-05-16 05:38:22 +03:00
|
|
|
|
<$> hsubparser
|
2017-02-06 18:04:00 +03:00
|
|
|
|
( command "deploy"
|
|
|
|
|
(info deployParser (progDesc "Deploy a new release")) <>
|
|
|
|
|
command "rollback"
|
2022-04-19 22:03:17 +03:00
|
|
|
|
(info rollbackParser (progDesc "Roll back to Nth previous release")) <>
|
|
|
|
|
command "maintenance"
|
|
|
|
|
(info maintenanceParser (progDesc "Enable/Disable maintenance mode"))
|
|
|
|
|
)
|
2017-02-20 17:33:12 +03:00
|
|
|
|
<*> strOption
|
|
|
|
|
( long "config"
|
|
|
|
|
<> short 'c'
|
|
|
|
|
<> value "hap.yaml"
|
|
|
|
|
<> metavar "PATH"
|
|
|
|
|
<> showDefault
|
|
|
|
|
<> help "Configuration file to use" )
|
2017-02-06 18:04:00 +03:00
|
|
|
|
|
|
|
|
|
deployParser :: Parser Command
|
|
|
|
|
deployParser = Deploy
|
2018-10-19 22:01:28 +03:00
|
|
|
|
<$> optional
|
|
|
|
|
( option pReleaseFormat
|
|
|
|
|
( long "release-format"
|
|
|
|
|
<> short 'r'
|
|
|
|
|
<> help "Which format release timestamp format to use: ‘long’ or ‘short’, default is ‘short’."
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
<*> optional
|
|
|
|
|
( option auto
|
|
|
|
|
( long "keep-releases"
|
|
|
|
|
<> short 'k'
|
|
|
|
|
<> help "How many releases to keep, default is '5'"
|
|
|
|
|
)
|
|
|
|
|
)
|
2022-01-19 16:16:15 +03:00
|
|
|
|
<*> switch
|
2022-02-07 15:21:59 +03:00
|
|
|
|
( long "keep-one-failed"
|
2022-01-19 16:16:15 +03:00
|
|
|
|
<> help "Keep all failed releases or just one -the latest-, default (without using this flag) is to keep all failed releases."
|
2022-02-07 15:21:59 +03:00
|
|
|
|
)
|
2017-02-06 18:04:00 +03:00
|
|
|
|
|
|
|
|
|
rollbackParser :: Parser Command
|
|
|
|
|
rollbackParser = Rollback
|
|
|
|
|
<$> option auto
|
|
|
|
|
( long "use-nth"
|
|
|
|
|
<> short 'n'
|
|
|
|
|
<> value 1
|
2017-02-20 17:33:12 +03:00
|
|
|
|
<> showDefault
|
|
|
|
|
<> help "How many deployments back to go?" )
|
2017-02-06 18:04:00 +03:00
|
|
|
|
|
2022-04-19 22:03:17 +03:00
|
|
|
|
maintenanceParser :: Parser Command
|
|
|
|
|
maintenanceParser =
|
|
|
|
|
Maintenance
|
|
|
|
|
<$> hsubparser
|
|
|
|
|
( command "enable" (info (pure Enable) (progDesc "Enables maintenance mode"))
|
|
|
|
|
<> command "disable" (info (pure Disable) (progDesc "Disables maintenance mode"))
|
|
|
|
|
)
|
|
|
|
|
|
2017-02-06 18:04:00 +03:00
|
|
|
|
pReleaseFormat :: ReadM ReleaseFormat
|
|
|
|
|
pReleaseFormat = eitherReader $ \s ->
|
|
|
|
|
case s of
|
|
|
|
|
"long" -> Right ReleaseLong
|
|
|
|
|
"short" -> Right ReleaseShort
|
|
|
|
|
_ -> Left ("Unknown format: " ++ s ++ ", try ‘long’ or ‘short’.")
|
|
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
|
-- Main
|
2014-06-04 01:15:39 +04:00
|
|
|
|
|
2017-03-01 23:40:08 +03:00
|
|
|
|
-- | Message that is used for communication between worker threads and the
|
|
|
|
|
-- printer thread.
|
|
|
|
|
|
|
|
|
|
data Message
|
|
|
|
|
= PrintMsg OutputDest String -- ^ Print a message to specified 'OutputDest'
|
|
|
|
|
| FinishMsg -- ^ The worker has finished
|
|
|
|
|
deriving (Eq, Ord, Show, Read)
|
|
|
|
|
|
2014-06-04 01:15:39 +04:00
|
|
|
|
main :: IO ()
|
2017-02-06 18:04:00 +03:00
|
|
|
|
main = do
|
2022-01-24 22:25:58 +03:00
|
|
|
|
Opts{..} <- execParser parserInfo
|
2022-09-09 19:49:27 +03:00
|
|
|
|
hapConfig@C.Config{..} <- Yaml.loadYamlSettings [optsConfigFile] [] Yaml.useEnv
|
2018-09-21 16:32:23 +03:00
|
|
|
|
chan <- newTChanIO
|
|
|
|
|
let printFnc dest str = atomically $
|
|
|
|
|
writeTChan chan (PrintMsg dest str)
|
2023-02-24 16:59:59 +03:00
|
|
|
|
hap shell sshOpts executionMode = do
|
2022-01-31 21:04:35 +03:00
|
|
|
|
r <- Hap.runHapistrano sshOpts shell printFnc $
|
2018-09-21 16:32:23 +03:00
|
|
|
|
case optsCommand of
|
2022-01-31 21:04:35 +03:00
|
|
|
|
Deploy cliReleaseFormat cliKeepReleases cliKeepOneFailed ->
|
2022-09-09 19:49:27 +03:00
|
|
|
|
Hap.deploy
|
|
|
|
|
hapConfig
|
|
|
|
|
(fromMaybeReleaseFormat cliReleaseFormat configReleaseFormat)
|
|
|
|
|
(fromMaybeKeepReleases cliKeepReleases configKeepReleases)
|
|
|
|
|
(cliKeepOneFailed || configKeepOneFailed)
|
2023-02-24 16:59:59 +03:00
|
|
|
|
executionMode
|
2022-09-09 19:49:27 +03:00
|
|
|
|
Rollback n ->
|
|
|
|
|
Hap.rollback configTargetSystem configDeployPath n configRestartCommand
|
2022-04-19 22:03:17 +03:00
|
|
|
|
Maintenance Enable-> do
|
|
|
|
|
Hap.writeMaintenanceFile configDeployPath configMaintenanceDirectory configMaintenanceFileName
|
|
|
|
|
Maintenance _ -> do
|
|
|
|
|
Hap.deleteMaintenanceFile configDeployPath configMaintenanceDirectory configMaintenanceFileName
|
2018-09-21 16:32:23 +03:00
|
|
|
|
atomically (writeTChan chan FinishMsg)
|
|
|
|
|
return r
|
|
|
|
|
printer :: Int -> IO ()
|
|
|
|
|
printer n = when (n > 0) $ do
|
|
|
|
|
msg <- atomically (readTChan chan)
|
|
|
|
|
case msg of
|
|
|
|
|
PrintMsg StdoutDest str ->
|
|
|
|
|
putStr str >> printer n
|
|
|
|
|
PrintMsg StderrDest str ->
|
|
|
|
|
hPutStr stderr str >> printer n
|
|
|
|
|
FinishMsg ->
|
|
|
|
|
printer (n - 1)
|
|
|
|
|
haps :: [IO (Either Int ())]
|
|
|
|
|
haps =
|
|
|
|
|
case configHosts of
|
2023-02-24 16:59:59 +03:00
|
|
|
|
[] -> [hap Bash Nothing C.LeadTarget] -- localhost, no SSH
|
|
|
|
|
targets@(leadTarget : _) ->
|
|
|
|
|
let runHap currentTarget@C.Target{..} =
|
2019-10-29 00:23:01 +03:00
|
|
|
|
hap targetShell (Just $ SshOptions targetHost targetPort targetSshArgs)
|
2023-02-24 16:59:59 +03:00
|
|
|
|
(if leadTarget == currentTarget then C.LeadTarget else C.AllTargets)
|
|
|
|
|
in runHap <$> targets
|
2018-09-21 16:32:23 +03:00
|
|
|
|
results <- (runConcurrently . traverse Concurrently)
|
|
|
|
|
((Right () <$ printer (length haps)) : haps)
|
|
|
|
|
case sequence_ results of
|
|
|
|
|
Left n -> exitWith (ExitFailure n)
|
|
|
|
|
Right () -> putStrLn "Success."
|