2011-07-06 00:36:43 +00:00
|
|
|
{- git-annex remote access with ssh
|
2011-03-05 19:47:00 +00:00
|
|
|
-
|
2012-11-11 04:51:07 +00:00
|
|
|
- Copyright 2011,2012 Joey Hess <joey@kitenet.net>
|
2011-03-05 19:47:00 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-01-10 19:29:10 +00:00
|
|
|
module Remote.Helper.Ssh where
|
2011-03-05 19:47:00 +00:00
|
|
|
|
2012-01-20 19:34:52 +00:00
|
|
|
import Common.Annex
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-12-14 19:30:14 +00:00
|
|
|
import qualified Git.Url
|
2011-10-15 21:47:03 +00:00
|
|
|
import Annex.UUID
|
2012-01-20 19:34:52 +00:00
|
|
|
import Annex.Ssh
|
2012-07-02 14:57:51 +00:00
|
|
|
import Fields
|
2013-01-01 17:52:47 +00:00
|
|
|
import Types.GitConfig
|
2011-03-05 19:47:00 +00:00
|
|
|
|
|
|
|
{- Generates parameters to ssh to a repository's host and run a command.
|
|
|
|
- Caller is responsible for doing any neccessary shellEscaping of the
|
|
|
|
- passed command. -}
|
|
|
|
sshToRepo :: Git.Repo -> [CommandParam] -> Annex [CommandParam]
|
|
|
|
sshToRepo repo sshcmd = do
|
2013-01-01 17:52:47 +00:00
|
|
|
g <- fromRepo id
|
|
|
|
let c = extractRemoteGitConfig g (Git.repoDescribe repo)
|
2013-04-13 22:10:49 +00:00
|
|
|
opts = map Param $ remoteAnnexSshOptions c
|
|
|
|
host = Git.Url.hostuser repo
|
|
|
|
params <- sshCachingOptions (host, Git.Url.port repo) opts
|
|
|
|
return $ params ++ Param host : sshcmd
|
2011-04-09 18:26:32 +00:00
|
|
|
|
|
|
|
{- Generates parameters to run a git-annex-shell command on a remote
|
|
|
|
- repository. -}
|
2012-07-02 14:57:51 +00:00
|
|
|
git_annex_shell :: Git.Repo -> String -> [CommandParam] -> [(Field, String)] -> Annex (Maybe (FilePath, [CommandParam]))
|
|
|
|
git_annex_shell r command params fields
|
|
|
|
| not $ Git.repoIsUrl r = return $ Just (shellcmd, shellopts ++ fieldopts)
|
2011-04-09 18:26:32 +00:00
|
|
|
| Git.repoIsSsh r = do
|
2011-10-11 18:43:45 +00:00
|
|
|
uuid <- getRepoUUID r
|
2011-10-06 23:11:30 +00:00
|
|
|
sshparams <- sshToRepo r [Param $ sshcmd uuid ]
|
2011-04-09 18:26:32 +00:00
|
|
|
return $ Just ("ssh", sshparams)
|
|
|
|
| otherwise = return Nothing
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
dir = Git.repoPath r
|
|
|
|
shellcmd = "git-annex-shell"
|
|
|
|
shellopts = Param command : File dir : params
|
|
|
|
sshcmd uuid = unwords $
|
|
|
|
shellcmd : map shellEscape (toCommand shellopts) ++
|
|
|
|
uuidcheck uuid ++
|
|
|
|
map shellEscape (toCommand fieldopts)
|
|
|
|
uuidcheck NoUUID = []
|
|
|
|
uuidcheck (UUID u) = ["--uuid", u]
|
|
|
|
fieldopts
|
|
|
|
| null fields = []
|
|
|
|
| otherwise = fieldsep : map fieldopt fields ++ [fieldsep]
|
|
|
|
fieldsep = Param "--"
|
|
|
|
fieldopt (field, value) = Param $
|
|
|
|
fieldName field ++ "=" ++ value
|
2011-04-09 18:26:32 +00:00
|
|
|
|
|
|
|
{- Uses a supplied function (such as boolSystem) to run a git-annex-shell
|
|
|
|
- command on a remote.
|
|
|
|
-
|
|
|
|
- Or, if the remote does not support running remote commands, returns
|
|
|
|
- a specified error value. -}
|
|
|
|
onRemote
|
|
|
|
:: Git.Repo
|
|
|
|
-> (FilePath -> [CommandParam] -> IO a, a)
|
|
|
|
-> String
|
|
|
|
-> [CommandParam]
|
2012-07-02 14:57:51 +00:00
|
|
|
-> [(Field, String)]
|
2011-04-09 18:26:32 +00:00
|
|
|
-> Annex a
|
2012-07-02 14:57:51 +00:00
|
|
|
onRemote r (with, errorval) command params fields = do
|
|
|
|
s <- git_annex_shell r command params fields
|
2011-04-09 18:26:32 +00:00
|
|
|
case s of
|
|
|
|
Just (c, ps) -> liftIO $ with c ps
|
|
|
|
Nothing -> return errorval
|