2014-04-06 23:06:03 +00:00
|
|
|
{- git-remote-daemon, git-annex-shell over ssh transport
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2014 Joey Hess <id@joeyh.name>
|
2014-04-06 23:06:03 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2015-03-16 19:28:29 +00:00
|
|
|
module RemoteDaemon.Transport.Ssh (transport, transportUsingCmd) where
|
2014-04-06 23:06:03 +00:00
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2014-04-12 19:59:34 +00:00
|
|
|
import Annex.Ssh
|
2014-04-06 23:06:03 +00:00
|
|
|
import RemoteDaemon.Types
|
2014-04-08 17:41:36 +00:00
|
|
|
import RemoteDaemon.Common
|
2014-04-06 23:06:03 +00:00
|
|
|
import Remote.Helper.Ssh
|
2014-04-08 17:41:36 +00:00
|
|
|
import qualified RemoteDaemon.Transport.Ssh.Types as SshRemote
|
2014-04-06 23:06:03 +00:00
|
|
|
import Utility.SimpleProtocol
|
2014-04-09 18:10:29 +00:00
|
|
|
import qualified Git
|
2014-04-06 23:06:03 +00:00
|
|
|
import Git.Command
|
2014-04-09 18:10:29 +00:00
|
|
|
import Utility.ThreadScheduler
|
2014-04-06 23:06:03 +00:00
|
|
|
|
2015-01-15 19:37:48 +00:00
|
|
|
import Control.Concurrent.STM
|
2014-04-06 23:06:03 +00:00
|
|
|
import Control.Concurrent.Async
|
|
|
|
|
|
|
|
transport :: Transport
|
2015-03-16 19:28:29 +00:00
|
|
|
transport rr@(RemoteRepo r _) url h ichan ochan = do
|
|
|
|
v <- liftAnnex h $ git_annex_shell r "notifychanges" [] []
|
|
|
|
case v of
|
|
|
|
Nothing -> noop
|
|
|
|
Just (cmd, params) -> transportUsingCmd cmd params rr url h ichan ochan
|
|
|
|
|
|
|
|
transportUsingCmd :: FilePath -> [CommandParam] -> Transport
|
2016-06-02 20:34:52 +00:00
|
|
|
transportUsingCmd cmd params rr@(RemoteRepo r gc) url h@(TransportHandle (LocalRepo g) s) ichan ochan = do
|
2014-04-12 19:59:34 +00:00
|
|
|
-- enable ssh connection caching wherever inLocalRepo is called
|
2015-02-12 20:12:32 +00:00
|
|
|
g' <- liftAnnex h $ sshOptionsTo r gc g
|
2016-06-02 20:34:52 +00:00
|
|
|
let transporthandle = TransportHandle (LocalRepo g') s
|
2015-03-16 19:28:29 +00:00
|
|
|
transportUsingCmd' cmd params rr url transporthandle ichan ochan
|
2014-04-12 19:59:34 +00:00
|
|
|
|
2015-03-16 19:28:29 +00:00
|
|
|
transportUsingCmd' :: FilePath -> [CommandParam] -> Transport
|
|
|
|
transportUsingCmd' cmd params (RemoteRepo r _) url transporthandle ichan ochan =
|
|
|
|
robustly 1 $ do
|
2014-04-09 18:10:29 +00:00
|
|
|
(Just toh, Just fromh, Just errh, pid) <-
|
2015-03-16 19:28:29 +00:00
|
|
|
createProcess (proc cmd (toCommand params))
|
2014-04-06 23:06:03 +00:00
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = CreatePipe
|
2014-04-09 18:10:29 +00:00
|
|
|
, std_err = CreatePipe
|
2014-04-06 23:06:03 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 18:10:29 +00:00
|
|
|
-- Run all threads until one finishes and get the status
|
|
|
|
-- of the first to finish. Cancel the rest.
|
|
|
|
status <- catchDefaultIO (Right ConnectionClosed) $
|
|
|
|
handlestderr errh
|
|
|
|
`race` handlestdout fromh
|
|
|
|
`race` handlecontrol
|
2014-04-06 23:06:03 +00:00
|
|
|
|
2014-04-09 18:10:29 +00:00
|
|
|
send (DISCONNECTED url)
|
|
|
|
hClose toh
|
|
|
|
hClose fromh
|
|
|
|
void $ waitForProcess pid
|
2014-04-06 23:06:03 +00:00
|
|
|
|
2014-04-09 18:10:29 +00:00
|
|
|
return $ either (either id id) id status
|
2015-03-16 19:28:29 +00:00
|
|
|
where
|
2015-01-15 19:37:48 +00:00
|
|
|
send msg = atomically $ writeTChan ochan msg
|
2014-04-08 17:41:36 +00:00
|
|
|
|
|
|
|
fetch = do
|
2014-04-09 18:10:29 +00:00
|
|
|
send (SYNCING url)
|
2014-04-08 17:41:36 +00:00
|
|
|
ok <- inLocalRepo transporthandle $
|
2014-04-09 18:10:29 +00:00
|
|
|
runBool [Param "fetch", Param $ Git.repoDescribe r]
|
|
|
|
send (DONESYNCING url ok)
|
|
|
|
|
|
|
|
handlestdout fromh = do
|
2016-12-09 17:34:00 +00:00
|
|
|
ml <- getProtocolLine fromh
|
|
|
|
case parseMessage =<< ml of
|
2014-04-09 18:10:29 +00:00
|
|
|
Just SshRemote.READY -> do
|
|
|
|
send (CONNECTED url)
|
|
|
|
handlestdout fromh
|
|
|
|
Just (SshRemote.CHANGED shas) -> do
|
|
|
|
whenM (checkNewShas transporthandle shas) $
|
|
|
|
fetch
|
|
|
|
handlestdout fromh
|
|
|
|
-- avoid reconnect on protocol error
|
|
|
|
Nothing -> return Stopping
|
|
|
|
|
|
|
|
handlecontrol = do
|
2015-01-15 19:37:48 +00:00
|
|
|
msg <- atomically $ readTChan ichan
|
2014-04-09 18:10:29 +00:00
|
|
|
case msg of
|
|
|
|
STOP -> return Stopping
|
2014-04-12 20:32:59 +00:00
|
|
|
LOSTNET -> return Stopping
|
2014-04-09 18:10:29 +00:00
|
|
|
_ -> handlecontrol
|
|
|
|
|
|
|
|
-- Old versions of git-annex-shell that do not support
|
|
|
|
-- the notifychanges command will exit with a not very useful
|
|
|
|
-- error message. Detect that error, and avoid reconnecting.
|
|
|
|
-- Propigate all stderr.
|
|
|
|
handlestderr errh = do
|
|
|
|
s <- hGetSomeString errh 1024
|
|
|
|
hPutStr stderr s
|
|
|
|
hFlush stderr
|
|
|
|
if "git-annex-shell: git-shell failed" `isInfixOf` s
|
|
|
|
then do
|
|
|
|
send $ WARNING url $ unwords
|
|
|
|
[ "Remote", Git.repoDescribe r
|
|
|
|
, "needs its git-annex upgraded"
|
|
|
|
, "to 5.20140405 or newer"
|
|
|
|
]
|
|
|
|
return Stopping
|
|
|
|
else handlestderr errh
|
|
|
|
|
|
|
|
data Status = Stopping | ConnectionClosed
|
|
|
|
|
2015-03-16 19:28:29 +00:00
|
|
|
{- Make connection robustly, with exponential backoff on failure. -}
|
2014-04-09 18:10:29 +00:00
|
|
|
robustly :: Int -> IO Status -> IO ()
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
robustly backoff a = caught =<< catchDefaultIO ConnectionClosed a
|
2014-04-09 18:10:29 +00:00
|
|
|
where
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
caught Stopping = return ()
|
|
|
|
caught ConnectionClosed = do
|
2014-04-09 18:10:29 +00:00
|
|
|
threadDelaySeconds (Seconds backoff)
|
|
|
|
robustly increasedbackoff a
|
|
|
|
|
|
|
|
increasedbackoff
|
|
|
|
| b2 > maxbackoff = maxbackoff
|
|
|
|
| otherwise = b2
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
b2 = backoff * 2
|
2014-04-09 18:10:29 +00:00
|
|
|
maxbackoff = 3600 -- one hour
|