2014-04-06 23:06:03 +00:00
|
|
|
{- git-remote-daemon core
|
|
|
|
-
|
2016-11-20 18:39:26 +00:00
|
|
|
- Copyright 2014-2016 Joey Hess <id@joeyh.name>
|
2014-04-06 23:06:03 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2014-04-06 23:06:03 +00:00
|
|
|
-}
|
|
|
|
|
2016-11-20 18:39:26 +00:00
|
|
|
module RemoteDaemon.Core (runInteractive, runNonInteractive) where
|
2014-04-06 23:06:03 +00:00
|
|
|
|
|
|
|
import qualified Annex
|
|
|
|
import Common
|
|
|
|
import Types.GitConfig
|
2017-08-17 16:26:14 +00:00
|
|
|
import Config.DynamicConfig
|
2014-04-08 17:41:36 +00:00
|
|
|
import RemoteDaemon.Common
|
2014-04-06 23:06:03 +00:00
|
|
|
import RemoteDaemon.Types
|
|
|
|
import RemoteDaemon.Transport
|
|
|
|
import qualified Git
|
|
|
|
import qualified Git.Types as Git
|
|
|
|
import qualified Git.CurrentRepo
|
2018-01-09 19:36:56 +00:00
|
|
|
import qualified Git.Construct
|
2014-04-06 23:06:03 +00:00
|
|
|
import Utility.SimpleProtocol
|
2016-11-20 18:39:26 +00:00
|
|
|
import Utility.ThreadScheduler
|
2014-04-08 17:41:36 +00:00
|
|
|
import Config
|
2014-04-12 20:32:59 +00:00
|
|
|
import Annex.Ssh
|
2020-04-09 17:54:43 +00:00
|
|
|
import Annex.BranchState
|
2016-12-08 22:42:59 +00:00
|
|
|
import Types.Messages
|
2014-04-06 23:06:03 +00:00
|
|
|
|
2014-04-08 17:41:36 +00:00
|
|
|
import Control.Concurrent
|
2015-01-15 19:37:48 +00:00
|
|
|
import Control.Concurrent.Async
|
|
|
|
import Control.Concurrent.STM
|
2014-04-06 23:06:03 +00:00
|
|
|
import Network.URI
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
2016-11-20 18:39:26 +00:00
|
|
|
runInteractive :: IO ()
|
|
|
|
runInteractive = do
|
2015-04-03 19:33:28 +00:00
|
|
|
(readh, writeh) <- dupIoHandles
|
2015-01-15 19:37:48 +00:00
|
|
|
ichan <- newTChanIO :: IO (TChan Consumed)
|
|
|
|
ochan <- newTChanIO :: IO (TChan Emitted)
|
2014-04-06 23:06:03 +00:00
|
|
|
|
|
|
|
let reader = forever $ do
|
2014-04-08 18:02:25 +00:00
|
|
|
l <- hGetLine readh
|
2014-04-06 23:06:03 +00:00
|
|
|
case parseMessage l of
|
2023-04-10 17:38:14 +00:00
|
|
|
Nothing -> giveup $ "protocol error: " ++ l
|
2015-01-15 19:37:48 +00:00
|
|
|
Just cmd -> atomically $ writeTChan ichan cmd
|
2014-04-06 23:06:03 +00:00
|
|
|
let writer = forever $ do
|
2015-01-15 19:37:48 +00:00
|
|
|
msg <- atomically $ readTChan ochan
|
2014-04-08 18:02:25 +00:00
|
|
|
hPutStrLn writeh $ unwords $ formatMessage msg
|
|
|
|
hFlush writeh
|
2014-04-08 17:51:49 +00:00
|
|
|
let controller = runController ichan ochan
|
2014-04-06 23:06:03 +00:00
|
|
|
|
2014-04-08 17:51:49 +00:00
|
|
|
-- If any thread fails, the rest will be killed.
|
2016-11-20 19:45:01 +00:00
|
|
|
void $ tryIO $ reader
|
|
|
|
`concurrently` writer
|
|
|
|
`concurrently` controller
|
2016-11-20 18:39:26 +00:00
|
|
|
|
|
|
|
runNonInteractive :: IO ()
|
|
|
|
runNonInteractive = do
|
|
|
|
ichan <- newTChanIO :: IO (TChan Consumed)
|
|
|
|
ochan <- newTChanIO :: IO (TChan Emitted)
|
|
|
|
|
|
|
|
let reader = forever $ do
|
|
|
|
threadDelaySeconds (Seconds (60*60))
|
|
|
|
atomically $ writeTChan ichan RELOAD
|
|
|
|
let writer = forever $
|
|
|
|
void $ atomically $ readTChan ochan
|
|
|
|
let controller = runController ichan ochan
|
|
|
|
|
2016-11-20 19:45:01 +00:00
|
|
|
void $ tryIO $ reader
|
|
|
|
`concurrently` writer
|
|
|
|
`concurrently` controller
|
2014-04-06 23:06:03 +00:00
|
|
|
|
2015-01-15 19:37:48 +00:00
|
|
|
type RemoteMap = M.Map Git.Repo (IO (), TChan Consumed)
|
2014-04-06 23:06:03 +00:00
|
|
|
|
|
|
|
-- Runs the transports, dispatching messages to them, and handling
|
|
|
|
-- the main control messages.
|
2015-01-15 19:37:48 +00:00
|
|
|
runController :: TChan Consumed -> TChan Emitted -> IO ()
|
2014-04-08 17:51:49 +00:00
|
|
|
runController ichan ochan = do
|
2014-04-08 17:41:36 +00:00
|
|
|
h <- genTransportHandle
|
|
|
|
m <- genRemoteMap h ochan
|
2016-12-28 16:21:52 +00:00
|
|
|
starttransports m
|
|
|
|
serverchans <- mapM (startserver h) remoteServers
|
|
|
|
go h False m serverchans
|
2014-04-06 23:06:03 +00:00
|
|
|
where
|
2016-12-28 16:21:52 +00:00
|
|
|
go h paused m serverchans = do
|
2015-01-15 19:37:48 +00:00
|
|
|
cmd <- atomically $ readTChan ichan
|
2016-12-28 16:21:52 +00:00
|
|
|
broadcast cmd serverchans
|
2014-04-06 23:06:03 +00:00
|
|
|
case cmd of
|
|
|
|
RELOAD -> do
|
2014-04-20 19:45:14 +00:00
|
|
|
h' <- updateTransportHandle h
|
|
|
|
m' <- genRemoteMap h' ochan
|
2014-04-06 23:06:03 +00:00
|
|
|
let common = M.intersection m m'
|
|
|
|
let new = M.difference m' m
|
|
|
|
let old = M.difference m m'
|
2016-12-28 16:21:52 +00:00
|
|
|
broadcast STOP (mchans old)
|
2014-04-06 23:06:03 +00:00
|
|
|
unless paused $
|
2016-12-28 16:21:52 +00:00
|
|
|
starttransports new
|
|
|
|
go h' paused (M.union common new) serverchans
|
2014-04-12 20:32:59 +00:00
|
|
|
LOSTNET -> do
|
|
|
|
-- force close all cached ssh connections
|
|
|
|
-- (done here so that if there are multiple
|
|
|
|
-- ssh remotes, it's only done once)
|
|
|
|
liftAnnex h forceSshCleanup
|
2016-12-28 16:21:52 +00:00
|
|
|
broadcast LOSTNET transportchans
|
|
|
|
go h True m serverchans
|
2014-04-06 23:06:03 +00:00
|
|
|
PAUSE -> do
|
2016-12-28 16:21:52 +00:00
|
|
|
broadcast STOP transportchans
|
|
|
|
go h True m serverchans
|
2014-04-06 23:06:03 +00:00
|
|
|
RESUME -> do
|
|
|
|
when paused $
|
2016-12-28 16:21:52 +00:00
|
|
|
starttransports m
|
|
|
|
go h False m serverchans
|
2014-04-06 23:06:03 +00:00
|
|
|
STOP -> exitSuccess
|
|
|
|
-- All remaining messages are sent to
|
|
|
|
-- all Transports.
|
|
|
|
msg -> do
|
2016-12-28 16:21:52 +00:00
|
|
|
unless paused $
|
|
|
|
broadcast msg transportchans
|
|
|
|
go h paused m serverchans
|
2014-04-06 23:06:03 +00:00
|
|
|
where
|
2016-12-28 16:21:52 +00:00
|
|
|
transportchans = mchans m
|
|
|
|
mchans = map snd . M.elems
|
|
|
|
|
|
|
|
startserver h server = do
|
|
|
|
c <- newTChanIO
|
|
|
|
void $ async $ server c h
|
|
|
|
return c
|
2014-04-06 23:06:03 +00:00
|
|
|
|
2016-12-28 16:21:52 +00:00
|
|
|
starttransports m = forM_ (M.elems m) starttransports'
|
|
|
|
starttransports' (transport, c) = do
|
2015-01-15 19:37:48 +00:00
|
|
|
-- drain any old control messages from the channel
|
|
|
|
-- to avoid confusing the transport with them
|
|
|
|
atomically $ drain c
|
|
|
|
void $ async transport
|
|
|
|
|
|
|
|
drain c = maybe noop (const $ drain c) =<< tryReadTChan c
|
2014-04-06 23:06:03 +00:00
|
|
|
|
2016-12-28 16:21:52 +00:00
|
|
|
broadcast msg cs = atomically $ forM_ cs $ \c -> writeTChan c msg
|
2014-04-06 23:06:03 +00:00
|
|
|
|
|
|
|
-- Generates a map with a transport for each supported remote in the git repo,
|
|
|
|
-- except those that have annex.sync = false
|
2015-01-15 19:37:48 +00:00
|
|
|
genRemoteMap :: TransportHandle -> TChan Emitted -> IO RemoteMap
|
2021-04-02 19:26:21 +00:00
|
|
|
genRemoteMap h@(TransportHandle (LocalRepo g) _ _) ochan = do
|
2018-01-09 19:36:56 +00:00
|
|
|
rs <- Git.Construct.fromRemotes g
|
|
|
|
M.fromList . catMaybes <$> mapM gen rs
|
2014-04-06 23:06:03 +00:00
|
|
|
where
|
2017-08-17 16:26:14 +00:00
|
|
|
gen r = do
|
|
|
|
gc <- atomically $ extractRemoteGitConfig g (Git.repoDescribe r)
|
|
|
|
case Git.location r of
|
|
|
|
Git.Url u -> case M.lookup (uriScheme u) remoteTransports of
|
|
|
|
Just transport -> ifM (getDynamicConfig (remoteAnnexSync gc))
|
|
|
|
( do
|
|
|
|
ichan <- newTChanIO :: IO (TChan Consumed)
|
|
|
|
return $ Just
|
|
|
|
( r
|
|
|
|
, (transport (RemoteRepo r gc) (RemoteURI u) h ichan ochan, ichan)
|
|
|
|
)
|
|
|
|
, return Nothing
|
|
|
|
)
|
|
|
|
Nothing -> return Nothing
|
2014-04-06 23:06:03 +00:00
|
|
|
_ -> return Nothing
|
2014-04-08 17:41:36 +00:00
|
|
|
|
|
|
|
genTransportHandle :: IO TransportHandle
|
|
|
|
genTransportHandle = do
|
2021-04-02 19:26:21 +00:00
|
|
|
(st, rd) <- Annex.new =<< Git.CurrentRepo.get
|
|
|
|
mvar <- newMVar st
|
|
|
|
let g = Annex.repo st
|
|
|
|
let h = TransportHandle (LocalRepo g) mvar rd
|
2020-04-09 17:54:43 +00:00
|
|
|
liftAnnex h $ do
|
|
|
|
Annex.setOutput QuietOutput
|
2020-07-06 16:09:53 +00:00
|
|
|
enableInteractiveBranchAccess
|
2016-12-08 22:42:59 +00:00
|
|
|
return h
|
2014-04-20 19:45:14 +00:00
|
|
|
|
|
|
|
updateTransportHandle :: TransportHandle -> IO TransportHandle
|
2021-04-02 19:26:21 +00:00
|
|
|
updateTransportHandle h@(TransportHandle _g st rd) = do
|
2014-04-20 19:45:14 +00:00
|
|
|
g' <- liftAnnex h $ do
|
|
|
|
reloadConfig
|
2019-11-12 14:32:14 +00:00
|
|
|
Annex.gitRepo
|
2021-04-02 19:26:21 +00:00
|
|
|
return (TransportHandle (LocalRepo g') st rd)
|