2014-04-06 23:06:03 +00:00
|
|
|
{- git-remote-daemon, git-annex-shell over ssh transport
|
|
|
|
-
|
|
|
|
- Copyright 2014 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module RemoteDaemon.Transport.Ssh (transport) where
|
|
|
|
|
|
|
|
import Common.Annex
|
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
|
|
|
|
|
|
|
import Control.Concurrent.Chan
|
|
|
|
import Control.Concurrent.Async
|
|
|
|
|
|
|
|
transport :: Transport
|
2014-04-12 19:59:34 +00:00
|
|
|
transport r url h@(TransportHandle g s) ichan ochan = do
|
|
|
|
-- enable ssh connection caching wherever inLocalRepo is called
|
|
|
|
g' <- liftAnnex h $ sshCachingTo r g
|
|
|
|
transport' r url (TransportHandle g' s) ichan ochan
|
|
|
|
|
|
|
|
transport' :: Transport
|
|
|
|
transport' r url transporthandle ichan ochan = do
|
|
|
|
|
2014-04-08 17:41:36 +00:00
|
|
|
v <- liftAnnex transporthandle $ git_annex_shell r "notifychanges" [] []
|
2014-04-06 23:06:03 +00:00
|
|
|
case v of
|
|
|
|
Nothing -> noop
|
2014-04-09 18:10:29 +00:00
|
|
|
Just (cmd, params) -> robustly 1 $
|
|
|
|
connect cmd (toCommand params)
|
2014-04-06 23:06:03 +00:00
|
|
|
where
|
2014-04-09 18:10:29 +00:00
|
|
|
connect cmd params = do
|
|
|
|
(Just toh, Just fromh, Just errh, pid) <-
|
|
|
|
createProcess (proc cmd 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
|
2014-04-06 23:06:03 +00:00
|
|
|
|
2014-04-09 18:10:29 +00:00
|
|
|
send msg = writeChan 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
|
|
|
|
l <- hGetLine fromh
|
|
|
|
case parseMessage l of
|
|
|
|
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
|
|
|
|
msg <- readChan ichan
|
|
|
|
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
|
|
|
|
|
|
|
|
{- Make connection robustly, with exponentioal backoff on failure. -}
|
|
|
|
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
|