2014-04-08 19:23:50 +00:00
|
|
|
{- git-annex assistant communication with remotedaemon
|
|
|
|
-
|
|
|
|
- Copyright 2014 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.Threads.RemoteControl where
|
|
|
|
|
|
|
|
import Assistant.Common
|
|
|
|
import RemoteDaemon.Types
|
|
|
|
import Config.Files
|
|
|
|
import Utility.Batch
|
|
|
|
import Utility.SimpleProtocol
|
|
|
|
import Assistant.Alert
|
|
|
|
import Assistant.Alert.Utility
|
|
|
|
import Assistant.DaemonStatus
|
2014-04-09 18:10:29 +00:00
|
|
|
import qualified Git
|
|
|
|
import qualified Git.Types as Git
|
|
|
|
import qualified Remote
|
|
|
|
import qualified Types.Remote as Remote
|
2014-04-08 19:23:50 +00:00
|
|
|
|
|
|
|
import Control.Concurrent
|
|
|
|
import Control.Concurrent.Async
|
|
|
|
import System.Process (std_in, std_out)
|
2014-04-09 18:10:29 +00:00
|
|
|
import Network.URI
|
2014-04-09 19:26:41 +00:00
|
|
|
import qualified Data.Map as M
|
|
|
|
import qualified Data.Set as S
|
2014-04-08 19:23:50 +00:00
|
|
|
|
|
|
|
remoteControlThread :: NamedThread
|
|
|
|
remoteControlThread = namedThread "RemoteControl" $ do
|
|
|
|
program <- liftIO readProgramFile
|
|
|
|
(cmd, params) <- liftIO $ toBatchCommand
|
|
|
|
(program, [Param "remotedaemon"])
|
|
|
|
let p = proc cmd (toCommand params)
|
|
|
|
(Just toh, Just fromh, _, pid) <- liftIO $ createProcess p
|
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = CreatePipe
|
|
|
|
}
|
|
|
|
|
2014-04-09 18:10:29 +00:00
|
|
|
urimap <- liftIO . newMVar =<< liftAnnex getURIMap
|
|
|
|
|
2014-04-08 19:23:50 +00:00
|
|
|
controller <- asIO $ remoteControllerThread toh
|
2014-04-09 18:10:29 +00:00
|
|
|
responder <- asIO $ remoteResponderThread fromh urimap
|
2014-04-08 19:23:50 +00:00
|
|
|
|
|
|
|
-- run controller and responder until the remotedaemon dies
|
2014-04-09 19:26:41 +00:00
|
|
|
liftIO $ void $ tryNonAsync $ controller `concurrently` responder
|
|
|
|
debug ["remotedaemon exited"]
|
|
|
|
liftIO $ forceSuccessProcess p pid
|
2014-04-08 19:23:50 +00:00
|
|
|
|
|
|
|
-- feed from the remoteControl channel into the remotedaemon
|
|
|
|
remoteControllerThread :: Handle -> Assistant ()
|
|
|
|
remoteControllerThread toh = do
|
|
|
|
clicker <- getAssistant remoteControl
|
|
|
|
liftIO $ forever $ do
|
|
|
|
msg <- readChan clicker
|
|
|
|
hPutStrLn toh $ unwords $ formatMessage msg
|
|
|
|
hFlush toh
|
|
|
|
|
|
|
|
-- read status messages emitted by the remotedaemon and handle them
|
2014-04-09 18:10:29 +00:00
|
|
|
remoteResponderThread :: Handle -> MVar (M.Map URI Remote) -> Assistant ()
|
|
|
|
remoteResponderThread fromh urimap = go M.empty
|
2014-04-08 19:23:50 +00:00
|
|
|
where
|
|
|
|
go syncalerts = do
|
|
|
|
l <- liftIO $ hGetLine fromh
|
|
|
|
case parseMessage l of
|
2014-04-09 19:26:41 +00:00
|
|
|
Just (CONNECTED uri) -> changeconnected S.insert uri
|
|
|
|
Just (DISCONNECTED uri) -> changeconnected S.delete uri
|
2014-04-09 18:10:29 +00:00
|
|
|
Just (SYNCING uri) -> withr uri $ \r ->
|
|
|
|
if M.member (Remote.uuid r) syncalerts
|
|
|
|
then go syncalerts
|
|
|
|
else do
|
|
|
|
i <- addAlert $ syncAlert [r]
|
|
|
|
go (M.insert (Remote.uuid r) i syncalerts)
|
|
|
|
Just (DONESYNCING uri status) -> withr uri $ \r ->
|
|
|
|
case M.lookup (Remote.uuid r) syncalerts of
|
|
|
|
Nothing -> cont
|
2014-04-08 19:23:50 +00:00
|
|
|
Just i -> do
|
|
|
|
let (succeeded, failed) = if status
|
2014-04-09 18:10:29 +00:00
|
|
|
then ([r], [])
|
|
|
|
else ([], [r])
|
2014-04-08 19:23:50 +00:00
|
|
|
updateAlertMap $ mergeAlert i $
|
2014-04-09 18:10:29 +00:00
|
|
|
syncResultAlert succeeded failed
|
|
|
|
go (M.delete (Remote.uuid r) syncalerts)
|
|
|
|
Just (WARNING (RemoteURI uri) msg) -> do
|
|
|
|
void $ addAlert $
|
|
|
|
warningAlert ("RemoteControl "++ show uri) msg
|
|
|
|
cont
|
2014-04-08 19:23:50 +00:00
|
|
|
Nothing -> do
|
|
|
|
debug ["protocol error from remotedaemon: ", l]
|
2014-04-09 18:10:29 +00:00
|
|
|
cont
|
2014-04-09 19:26:41 +00:00
|
|
|
where
|
|
|
|
cont = go syncalerts
|
|
|
|
withr uri = withRemote uri urimap cont
|
|
|
|
changeconnected sm uri = withr uri $ \r -> do
|
|
|
|
changeCurrentlyConnected $ sm $ Remote.uuid r
|
|
|
|
cont
|
2014-04-09 18:10:29 +00:00
|
|
|
|
|
|
|
getURIMap :: Annex (M.Map URI Remote)
|
|
|
|
getURIMap = Remote.remoteMap' id (mkk . Git.location . Remote.repo)
|
|
|
|
where
|
|
|
|
mkk (Git.Url u) = Just u
|
|
|
|
mkk _ = Nothing
|
|
|
|
|
|
|
|
withRemote
|
|
|
|
:: RemoteURI
|
|
|
|
-> MVar (M.Map URI Remote)
|
|
|
|
-> Assistant a
|
|
|
|
-> (Remote -> Assistant a)
|
|
|
|
-> Assistant a
|
|
|
|
withRemote (RemoteURI uri) remotemap noremote a = do
|
|
|
|
m <- liftIO $ readMVar remotemap
|
|
|
|
case M.lookup uri m of
|
|
|
|
Just r -> a r
|
|
|
|
Nothing -> do
|
|
|
|
{- Reload map, in case a new remote has been added. -}
|
|
|
|
m' <- liftAnnex getURIMap
|
|
|
|
void $ liftIO $ swapMVar remotemap $ m'
|
|
|
|
maybe noremote a (M.lookup uri m')
|