2012-08-06 18:44:41 +04:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE NoImplicitPrelude #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2013-07-25 14:07:48 +04:00
|
|
|
module Keter.HostManager
|
2012-08-06 18:44:41 +04:00
|
|
|
( -- * Types
|
|
|
|
Port
|
|
|
|
, Host
|
2013-07-25 14:07:48 +04:00
|
|
|
, HostManager
|
|
|
|
, HostEntry (..)
|
2012-08-06 18:44:41 +04:00
|
|
|
-- * Actions
|
|
|
|
, getPort
|
|
|
|
, releasePort
|
|
|
|
, addEntry
|
|
|
|
, removeEntry
|
|
|
|
, lookupPort
|
|
|
|
-- * Initialize
|
|
|
|
, start
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Keter.Prelude
|
|
|
|
import qualified Control.Monad.Trans.State as S
|
|
|
|
import Control.Monad.Trans.Class (lift)
|
|
|
|
import qualified Data.Map as Map
|
2013-07-14 16:55:05 +04:00
|
|
|
import Control.Monad (forever, mplus)
|
2012-08-06 18:44:41 +04:00
|
|
|
import Data.ByteString.Char8 ()
|
|
|
|
import qualified Network
|
|
|
|
import qualified Data.ByteString as S
|
|
|
|
import Data.Text.Encoding (encodeUtf8)
|
2013-03-20 09:01:43 +04:00
|
|
|
import qualified Keter.ReverseProxy as ReverseProxy (RPEntry)
|
2013-07-14 16:55:05 +04:00
|
|
|
import Keter.Types
|
2012-08-06 18:44:41 +04:00
|
|
|
|
|
|
|
data Command = GetPort (Either SomeException Port -> KIO ())
|
|
|
|
| ReleasePort Port
|
2013-07-25 14:07:48 +04:00
|
|
|
| AddEntry Host HostEntry
|
2012-08-06 18:44:41 +04:00
|
|
|
| RemoveEntry Host
|
2013-07-25 14:07:48 +04:00
|
|
|
| AddDefaultEntry HostEntry
|
2013-01-02 18:55:15 +04:00
|
|
|
| RemoveDefaultEntry
|
2013-07-25 14:07:48 +04:00
|
|
|
| LookupPort S.ByteString (Maybe HostEntry -> KIO ())
|
2012-08-06 18:44:41 +04:00
|
|
|
|
|
|
|
-- | An abstract type which can accept commands and sends them to a background
|
|
|
|
-- nginx thread.
|
2013-07-25 14:07:48 +04:00
|
|
|
newtype HostManager = HostManager (Command -> KIO ())
|
2012-08-06 18:44:41 +04:00
|
|
|
|
|
|
|
-- | Start running a separate thread which will accept commands and modify
|
|
|
|
-- Nginx's behavior accordingly.
|
2013-07-25 14:07:48 +04:00
|
|
|
start :: PortSettings -> KIO (Either SomeException HostManager)
|
2013-07-14 16:55:05 +04:00
|
|
|
start PortSettings{..} = do
|
2012-08-06 18:44:41 +04:00
|
|
|
chan <- newChan
|
2013-01-02 18:55:15 +04:00
|
|
|
forkKIO $ flip S.evalStateT freshState $ forever $ do
|
2012-08-06 18:44:41 +04:00
|
|
|
command <- lift $ readChan chan
|
|
|
|
case command of
|
|
|
|
GetPort f -> do
|
|
|
|
ns0 <- S.get
|
|
|
|
let loop :: NState -> KIO (Either SomeException Port, NState)
|
|
|
|
loop ns =
|
|
|
|
case nsAvail ns of
|
|
|
|
p:ps -> do
|
|
|
|
res <- liftIO $ Network.listenOn $ Network.PortNumber $ fromIntegral p
|
|
|
|
case res of
|
|
|
|
Left (_ :: SomeException) -> do
|
|
|
|
log $ RemovingPort p
|
|
|
|
loop ns { nsAvail = ps }
|
|
|
|
Right socket -> do
|
|
|
|
res' <- liftIO $ Network.sClose socket
|
|
|
|
case res' of
|
|
|
|
Left e -> do
|
|
|
|
$logEx e
|
|
|
|
log $ RemovingPort p
|
|
|
|
loop ns { nsAvail = ps }
|
|
|
|
Right () -> return (Right p, ns { nsAvail = ps })
|
|
|
|
[] ->
|
|
|
|
case reverse $ nsRecycled ns of
|
|
|
|
[] -> return (Left $ toException NoPortsAvailable, ns)
|
|
|
|
ps -> loop ns { nsAvail = ps, nsRecycled = [] }
|
|
|
|
(eport, ns) <- lift $ loop ns0
|
|
|
|
S.put ns
|
|
|
|
lift $ f eport
|
|
|
|
ReleasePort p ->
|
|
|
|
S.modify $ \ns -> ns { nsRecycled = p : nsRecycled ns }
|
|
|
|
AddEntry h e -> change $ Map.insert (encodeUtf8 h) e
|
|
|
|
RemoveEntry h -> change $ Map.delete $ encodeUtf8 h
|
2013-01-02 18:55:15 +04:00
|
|
|
AddDefaultEntry e -> S.modify $ \ns -> ns { nsDefault = Just e }
|
|
|
|
RemoveDefaultEntry -> S.modify $ \ns -> ns { nsDefault = Nothing }
|
2012-08-06 18:44:41 +04:00
|
|
|
LookupPort h f -> do
|
|
|
|
NState {..} <- S.get
|
2013-01-02 18:55:15 +04:00
|
|
|
lift $ f $ mplus (Map.lookup h nsEntries) nsDefault
|
2013-07-25 14:07:48 +04:00
|
|
|
return $ Right $ HostManager $ writeChan chan
|
2012-08-06 18:44:41 +04:00
|
|
|
where
|
|
|
|
change f = do
|
|
|
|
ns <- S.get
|
|
|
|
let entries = f $ nsEntries ns
|
|
|
|
S.put $ ns { nsEntries = entries }
|
2013-01-02 18:55:15 +04:00
|
|
|
freshState = NState portRange [] Map.empty Nothing
|
2012-08-06 18:44:41 +04:00
|
|
|
|
|
|
|
data NState = NState
|
|
|
|
{ nsAvail :: [Port]
|
|
|
|
, nsRecycled :: [Port]
|
2013-07-25 14:07:48 +04:00
|
|
|
, nsEntries :: Map.Map S.ByteString HostEntry
|
|
|
|
, nsDefault :: Maybe HostEntry
|
2012-08-06 18:44:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
-- | Gets an unassigned port number.
|
2013-07-25 14:07:48 +04:00
|
|
|
getPort :: HostManager -> KIO (Either SomeException Port)
|
|
|
|
getPort (HostManager f) = do
|
2012-08-06 18:44:41 +04:00
|
|
|
x <- newEmptyMVar
|
|
|
|
f $ GetPort $ \p -> putMVar x p
|
|
|
|
takeMVar x
|
|
|
|
|
|
|
|
-- | Inform the nginx thread that the given port number is no longer being
|
|
|
|
-- used, and may be reused by a new process. Note that recycling puts the new
|
|
|
|
-- ports at the end of the queue (FIFO), so that if an application holds onto
|
|
|
|
-- the port longer than expected, there should be no issues.
|
2013-07-25 14:07:48 +04:00
|
|
|
releasePort :: HostManager -> Port -> KIO ()
|
|
|
|
releasePort (HostManager f) p = f $ ReleasePort p
|
2012-08-06 18:44:41 +04:00
|
|
|
|
|
|
|
-- | Add a new entry to the configuration for the given hostname and reload
|
|
|
|
-- nginx. Will overwrite any existing configuration for the given host. The
|
|
|
|
-- second point is important: it is how we achieve zero downtime transitions
|
|
|
|
-- between an old and new version of an app.
|
2013-07-25 14:07:48 +04:00
|
|
|
addEntry :: HostManager -> Host -> HostEntry -> KIO ()
|
|
|
|
addEntry (HostManager f) h p = f $ case h of
|
2013-01-02 18:55:15 +04:00
|
|
|
"*" -> AddDefaultEntry p
|
|
|
|
_ -> AddEntry h p
|
2012-08-06 18:44:41 +04:00
|
|
|
|
2013-07-25 14:07:48 +04:00
|
|
|
data HostEntry = PEPort Port | PEStatic FilePath | PERedirect S.ByteString | PEReverseProxy ReverseProxy.RPEntry
|
2012-10-21 09:07:26 +04:00
|
|
|
|
2012-08-06 18:44:41 +04:00
|
|
|
-- | Remove an entry from the configuration and reload nginx.
|
2013-07-25 14:07:48 +04:00
|
|
|
removeEntry :: HostManager -> Host -> KIO ()
|
|
|
|
removeEntry (HostManager f) h = f $ case h of
|
2013-01-02 18:55:15 +04:00
|
|
|
"*" -> RemoveDefaultEntry
|
|
|
|
_ -> RemoveEntry h
|
2012-08-06 18:44:41 +04:00
|
|
|
|
2013-07-25 14:07:48 +04:00
|
|
|
lookupPort :: HostManager -> S.ByteString -> KIO (Maybe HostEntry)
|
|
|
|
lookupPort (HostManager f) h = do
|
2012-08-06 18:44:41 +04:00
|
|
|
x <- newEmptyMVar
|
|
|
|
f $ LookupPort h $ \p -> putMVar x p
|
|
|
|
takeMVar x
|