2014-05-15 18:44:00 +00:00
|
|
|
{- Using ddar as a remote. Based on bup and rsync remotes.
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2011 Joey Hess <id@joeyh.name>
|
2014-05-15 18:44:00 +00:00
|
|
|
- Copyright 2014 Robie Basak <robie@justgohome.co.uk>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Remote.Ddar (remote) where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
2014-08-03 05:12:24 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2014-05-15 18:44:00 +00:00
|
|
|
import System.IO.Error
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Types.Remote
|
|
|
|
import Types.Key
|
|
|
|
import Types.Creds
|
|
|
|
import qualified Git
|
|
|
|
import Config
|
|
|
|
import Config.Cost
|
|
|
|
import Remote.Helper.Special
|
|
|
|
import Annex.Ssh
|
|
|
|
import Annex.UUID
|
|
|
|
|
2015-02-12 19:44:10 +00:00
|
|
|
data DdarRepo = DdarRepo
|
|
|
|
{ ddarRepoConfig :: RemoteGitConfig
|
|
|
|
, ddarRepoLocation :: String
|
|
|
|
}
|
2014-05-15 18:44:00 +00:00
|
|
|
|
|
|
|
remote :: RemoteType
|
|
|
|
remote = RemoteType {
|
|
|
|
typename = "ddar",
|
2015-08-05 17:49:54 +00:00
|
|
|
enumerate = const (findSpecialRemotes "ddarrepo"),
|
2014-05-15 18:44:00 +00:00
|
|
|
generate = gen,
|
|
|
|
setup = ddarSetup
|
|
|
|
}
|
|
|
|
|
|
|
|
gen :: Git.Repo -> UUID -> RemoteConfig -> RemoteGitConfig -> Annex (Maybe Remote)
|
|
|
|
gen r u c gc = do
|
|
|
|
cst <- remoteCost gc $
|
|
|
|
if ddarLocal ddarrepo
|
|
|
|
then nearlyCheapRemoteCost
|
|
|
|
else expensiveRemoteCost
|
2014-08-03 19:35:23 +00:00
|
|
|
return $ Just $ specialRemote' specialcfg c
|
2014-08-02 22:58:38 +00:00
|
|
|
(simplyPrepare $ store ddarrepo)
|
|
|
|
(simplyPrepare $ retrieve ddarrepo)
|
run Preparer to get Remover and CheckPresent actions
This will allow special remotes to eg, open a http connection and reuse it,
while checking if chunks are present, or removing chunks.
S3 and WebDAV both need this to support chunks with reasonable speed.
Note that a special remote might want to cache a http connection across
multiple requests. A simple case of this is that CheckPresent is typically
called before Store or Remove. A remote using this interface can certianly
use a Preparer that eg, uses a MVar to cache a http connection.
However, it's up to the remote to then deal with things like stale or
stalled http connections when eg, doing a series of downloads from a remote
and other places. There could be long delays between calls to a remote,
which could lead to eg, http connection stalls; the machine might even
move to a new network, etc.
It might be nice to improve this interface later to allow
the simple case without needing to handle the full complex case.
One way to do it would be to have a `Transaction SpecialRemote cache`,
where SpecialRemote contains methods for Storer, Retriever, Remover, and
CheckPresent, that all expect to be passed a `cache`.
2014-08-06 18:28:36 +00:00
|
|
|
(simplyPrepare $ remove ddarrepo)
|
|
|
|
(simplyPrepare $ checkKey ddarrepo)
|
2014-08-02 22:58:38 +00:00
|
|
|
(this cst)
|
|
|
|
where
|
|
|
|
this cst = Remote
|
2014-05-15 18:44:00 +00:00
|
|
|
{ uuid = u
|
|
|
|
, cost = cst
|
|
|
|
, name = Git.repoDescribe r
|
2014-08-02 22:58:38 +00:00
|
|
|
, storeKey = storeKeyDummy
|
|
|
|
, retrieveKeyFile = retreiveKeyFileDummy
|
2014-05-15 18:44:00 +00:00
|
|
|
, retrieveKeyFileCheap = retrieveCheap
|
run Preparer to get Remover and CheckPresent actions
This will allow special remotes to eg, open a http connection and reuse it,
while checking if chunks are present, or removing chunks.
S3 and WebDAV both need this to support chunks with reasonable speed.
Note that a special remote might want to cache a http connection across
multiple requests. A simple case of this is that CheckPresent is typically
called before Store or Remove. A remote using this interface can certianly
use a Preparer that eg, uses a MVar to cache a http connection.
However, it's up to the remote to then deal with things like stale or
stalled http connections when eg, doing a series of downloads from a remote
and other places. There could be long delays between calls to a remote,
which could lead to eg, http connection stalls; the machine might even
move to a new network, etc.
It might be nice to improve this interface later to allow
the simple case without needing to handle the full complex case.
One way to do it would be to have a `Transaction SpecialRemote cache`,
where SpecialRemote contains methods for Storer, Retriever, Remover, and
CheckPresent, that all expect to be passed a `cache`.
2014-08-06 18:28:36 +00:00
|
|
|
, removeKey = removeKeyDummy
|
|
|
|
, checkPresent = checkPresentDummy
|
2014-08-06 17:45:19 +00:00
|
|
|
, checkPresentCheap = ddarLocal ddarrepo
|
2014-05-15 18:44:00 +00:00
|
|
|
, whereisKey = Nothing
|
|
|
|
, remoteFsck = Nothing
|
|
|
|
, repairRepo = Nothing
|
|
|
|
, config = c
|
|
|
|
, repo = r
|
|
|
|
, gitconfig = gc
|
2015-02-12 19:44:10 +00:00
|
|
|
, localpath = if ddarLocal ddarrepo && not (null $ ddarRepoLocation ddarrepo)
|
|
|
|
then Just $ ddarRepoLocation ddarrepo
|
2014-05-15 18:44:00 +00:00
|
|
|
else Nothing
|
|
|
|
, remotetype = remote
|
|
|
|
, availability = if ddarLocal ddarrepo then LocallyAvailable else GloballyAvailable
|
|
|
|
, readonly = False
|
2014-08-10 18:52:58 +00:00
|
|
|
, mkUnavailable = return Nothing
|
2015-02-12 19:44:10 +00:00
|
|
|
, getInfo = return [("repo", ddarRepoLocation ddarrepo)]
|
2014-12-08 17:40:15 +00:00
|
|
|
, claimUrl = Nothing
|
2014-12-11 19:32:42 +00:00
|
|
|
, checkUrl = Nothing
|
2014-05-15 18:44:00 +00:00
|
|
|
}
|
2015-02-12 19:44:10 +00:00
|
|
|
ddarrepo = maybe (error "missing ddarrepo") (DdarRepo gc) (remoteAnnexDdarRepo gc)
|
2014-08-03 19:35:23 +00:00
|
|
|
specialcfg = (specialRemoteCfg c)
|
|
|
|
-- chunking would not improve ddar
|
|
|
|
{ chunkConfig = NoChunks
|
|
|
|
}
|
2014-05-15 18:44:00 +00:00
|
|
|
|
|
|
|
ddarSetup :: Maybe UUID -> Maybe CredPair -> RemoteConfig -> Annex (RemoteConfig, UUID)
|
|
|
|
ddarSetup mu _ c = do
|
|
|
|
u <- maybe (liftIO genUUID) return mu
|
|
|
|
|
|
|
|
-- verify configuration is sane
|
|
|
|
let ddarrepo = fromMaybe (error "Specify ddarrepo=") $
|
|
|
|
M.lookup "ddarrepo" c
|
glacier, S3: Fix bug that caused embedded creds to not be encypted using the remote's key.
encryptionSetup must be called before setRemoteCredPair. Otherwise,
the RemoteConfig doesn't have the cipher in it, and so no cipher is used to
encrypt the embedded creds.
This is a security fix for non-shared encryption methods!
For encryption=shared, there's no security problem, just an
inconsistentency in whether the embedded creds are encrypted.
This is very important to get right, so used some types to help ensure that
setRemoteCredPair is only run after encryptionSetup. Note that the external
special remote bypasses the type safety, since creds can be set after the
initial remote config, if the external special remote program requests it.
Also note that IA remotes never use encryption, so encryptionSetup is not
run for them at all, and again the type safety is bypassed.
This leaves two open questions:
1. What to do about S3 and glacier remotes that were set up
using encryption=pubkey/hybrid with embedcreds?
Such a git repo has a security hole embedded in it, and this needs to be
communicated to the user. Is the changelog enough?
2. enableremote won't work in such a repo, because git-annex will
try to decrypt the embedded creds, which are not encrypted, so fails.
This needs to be dealt with, especially for ecryption=shared repos,
which are not really broken, just inconsistently configured.
Noticing that problem for encryption=shared is what led to commit
fbdeeeed5fa276d94be587c8916d725eddcaf546, which tried to
fix the problem by not decrypting the embedded creds.
This commit was sponsored by Josh Taylor.
2014-09-18 21:07:17 +00:00
|
|
|
(c', _encsetup) <- encryptionSetup c
|
2014-05-15 18:44:00 +00:00
|
|
|
|
|
|
|
-- The ddarrepo is stored in git config, as well as this repo's
|
|
|
|
-- persistant state, so it can vary between hosts.
|
|
|
|
gitConfigSpecialRemote u c' "ddarrepo" ddarrepo
|
|
|
|
|
|
|
|
return (c', u)
|
|
|
|
|
2014-08-02 22:58:38 +00:00
|
|
|
store :: DdarRepo -> Storer
|
|
|
|
store ddarrepo = fileStorer $ \k src _p -> do
|
2014-05-15 18:44:00 +00:00
|
|
|
let params =
|
|
|
|
[ Param "c"
|
|
|
|
, Param "-N"
|
|
|
|
, Param $ key2file k
|
2015-02-12 19:44:10 +00:00
|
|
|
, Param $ ddarRepoLocation ddarrepo
|
2014-05-15 18:44:00 +00:00
|
|
|
, File src
|
|
|
|
]
|
|
|
|
liftIO $ boolSystem "ddar" params
|
|
|
|
|
|
|
|
{- Convert remote DdarRepo to host and path on remote end -}
|
|
|
|
splitRemoteDdarRepo :: DdarRepo -> (String, String)
|
|
|
|
splitRemoteDdarRepo ddarrepo =
|
|
|
|
(host, ddarrepo')
|
|
|
|
where
|
2015-02-12 19:44:10 +00:00
|
|
|
(host, remainder) = span (/= ':') (ddarRepoLocation ddarrepo)
|
2014-05-15 18:44:00 +00:00
|
|
|
ddarrepo' = drop 1 remainder
|
|
|
|
|
|
|
|
{- Return the command and parameters to use for a ddar call that may need to be
|
|
|
|
- made on a remote repository. This will call ssh if needed. -}
|
|
|
|
ddarRemoteCall :: DdarRepo -> Char -> [CommandParam] -> Annex (String, [CommandParam])
|
|
|
|
ddarRemoteCall ddarrepo cmd params
|
|
|
|
| ddarLocal ddarrepo = return ("ddar", localParams)
|
|
|
|
| otherwise = do
|
2015-02-12 19:44:10 +00:00
|
|
|
os <- sshOptions (host, Nothing) (ddarRepoConfig ddarrepo) remoteParams
|
|
|
|
return ("ssh", os)
|
2014-05-15 18:44:00 +00:00
|
|
|
where
|
|
|
|
(host, ddarrepo') = splitRemoteDdarRepo ddarrepo
|
2015-02-12 19:44:10 +00:00
|
|
|
localParams = Param [cmd] : Param (ddarRepoLocation ddarrepo) : params
|
2014-05-15 18:44:00 +00:00
|
|
|
remoteParams = Param host : Param "ddar" : Param [cmd] : Param ddarrepo' : params
|
|
|
|
|
|
|
|
{- Specialized ddarRemoteCall that includes extraction command and flags -}
|
|
|
|
ddarExtractRemoteCall :: DdarRepo -> Key -> Annex (String, [CommandParam])
|
|
|
|
ddarExtractRemoteCall ddarrepo k =
|
|
|
|
ddarRemoteCall ddarrepo 'x' [Param "--force-stdout", Param $ key2file k]
|
|
|
|
|
2014-08-02 22:58:38 +00:00
|
|
|
retrieve :: DdarRepo -> Retriever
|
2014-08-03 05:12:24 +00:00
|
|
|
retrieve ddarrepo = byteRetriever $ \k sink -> do
|
2014-05-15 18:44:00 +00:00
|
|
|
(cmd, params) <- ddarExtractRemoteCall ddarrepo k
|
2014-08-03 05:12:24 +00:00
|
|
|
let p = (proc cmd $ toCommand params) { std_out = CreatePipe }
|
|
|
|
(_, Just h, _, pid) <- liftIO $ createProcess p
|
|
|
|
liftIO (hClose h >> forceSuccessProcess p pid)
|
|
|
|
`after` (sink =<< liftIO (L.hGetContents h))
|
2014-05-15 18:44:00 +00:00
|
|
|
|
2015-04-14 20:35:10 +00:00
|
|
|
retrieveCheap :: Key -> AssociatedFile -> FilePath -> Annex Bool
|
|
|
|
retrieveCheap _ _ _ = return False
|
2014-05-15 18:44:00 +00:00
|
|
|
|
run Preparer to get Remover and CheckPresent actions
This will allow special remotes to eg, open a http connection and reuse it,
while checking if chunks are present, or removing chunks.
S3 and WebDAV both need this to support chunks with reasonable speed.
Note that a special remote might want to cache a http connection across
multiple requests. A simple case of this is that CheckPresent is typically
called before Store or Remove. A remote using this interface can certianly
use a Preparer that eg, uses a MVar to cache a http connection.
However, it's up to the remote to then deal with things like stale or
stalled http connections when eg, doing a series of downloads from a remote
and other places. There could be long delays between calls to a remote,
which could lead to eg, http connection stalls; the machine might even
move to a new network, etc.
It might be nice to improve this interface later to allow
the simple case without needing to handle the full complex case.
One way to do it would be to have a `Transaction SpecialRemote cache`,
where SpecialRemote contains methods for Storer, Retriever, Remover, and
CheckPresent, that all expect to be passed a `cache`.
2014-08-06 18:28:36 +00:00
|
|
|
remove :: DdarRepo -> Remover
|
2014-05-15 18:44:00 +00:00
|
|
|
remove ddarrepo key = do
|
|
|
|
(cmd, params) <- ddarRemoteCall ddarrepo 'd' [Param $ key2file key]
|
|
|
|
liftIO $ boolSystem cmd params
|
|
|
|
|
|
|
|
ddarDirectoryExists :: DdarRepo -> Annex (Either String Bool)
|
|
|
|
ddarDirectoryExists ddarrepo
|
|
|
|
| ddarLocal ddarrepo = do
|
2015-02-12 19:44:10 +00:00
|
|
|
maybeStatus <- liftIO $ tryJust (guard . isDoesNotExistError) $ getFileStatus $ ddarRepoLocation ddarrepo
|
2014-05-15 18:44:00 +00:00
|
|
|
return $ case maybeStatus of
|
|
|
|
Left _ -> Right False
|
|
|
|
Right status -> Right $ isDirectory status
|
|
|
|
| otherwise = do
|
2015-02-12 19:44:10 +00:00
|
|
|
ps <- sshOptions (host, Nothing) (ddarRepoConfig ddarrepo) params
|
|
|
|
exitCode <- liftIO $ safeSystem "ssh" ps
|
2014-05-15 18:44:00 +00:00
|
|
|
case exitCode of
|
|
|
|
ExitSuccess -> return $ Right True
|
|
|
|
ExitFailure 1 -> return $ Right False
|
|
|
|
ExitFailure code -> return $ Left $ "ssh call " ++
|
2015-01-21 17:36:48 +00:00
|
|
|
show (unwords $ toCommand params) ++
|
2014-05-15 18:44:00 +00:00
|
|
|
" failed with status " ++ show code
|
|
|
|
where
|
|
|
|
(host, ddarrepo') = splitRemoteDdarRepo ddarrepo
|
|
|
|
params =
|
|
|
|
[ Param host
|
|
|
|
, Param "test"
|
|
|
|
, Param "-d"
|
|
|
|
, Param ddarrepo'
|
|
|
|
]
|
|
|
|
|
|
|
|
{- Use "ddar t" to determine if a given key is present in a ddar archive -}
|
|
|
|
inDdarManifest :: DdarRepo -> Key -> Annex (Either String Bool)
|
|
|
|
inDdarManifest ddarrepo k = do
|
|
|
|
(cmd, params) <- ddarRemoteCall ddarrepo 't' []
|
|
|
|
let p = proc cmd $ toCommand params
|
|
|
|
liftIO $ catchMsgIO $ withHandle StdoutHandle createProcessSuccess p $ \h -> do
|
|
|
|
contents <- hGetContents h
|
|
|
|
return $ elem k' $ lines contents
|
|
|
|
where
|
|
|
|
k' = key2file k
|
|
|
|
|
run Preparer to get Remover and CheckPresent actions
This will allow special remotes to eg, open a http connection and reuse it,
while checking if chunks are present, or removing chunks.
S3 and WebDAV both need this to support chunks with reasonable speed.
Note that a special remote might want to cache a http connection across
multiple requests. A simple case of this is that CheckPresent is typically
called before Store or Remove. A remote using this interface can certianly
use a Preparer that eg, uses a MVar to cache a http connection.
However, it's up to the remote to then deal with things like stale or
stalled http connections when eg, doing a series of downloads from a remote
and other places. There could be long delays between calls to a remote,
which could lead to eg, http connection stalls; the machine might even
move to a new network, etc.
It might be nice to improve this interface later to allow
the simple case without needing to handle the full complex case.
One way to do it would be to have a `Transaction SpecialRemote cache`,
where SpecialRemote contains methods for Storer, Retriever, Remover, and
CheckPresent, that all expect to be passed a `cache`.
2014-08-06 18:28:36 +00:00
|
|
|
checkKey :: DdarRepo -> CheckPresent
|
2014-08-06 17:45:19 +00:00
|
|
|
checkKey ddarrepo key = do
|
2014-05-15 18:44:00 +00:00
|
|
|
directoryExists <- ddarDirectoryExists ddarrepo
|
|
|
|
case directoryExists of
|
2014-08-06 17:45:19 +00:00
|
|
|
Left e -> error e
|
|
|
|
Right True -> either error return
|
|
|
|
=<< inDdarManifest ddarrepo key
|
|
|
|
Right False -> return False
|
2014-05-15 18:44:00 +00:00
|
|
|
|
|
|
|
ddarLocal :: DdarRepo -> Bool
|
2015-02-12 19:44:10 +00:00
|
|
|
ddarLocal = notElem ':' . ddarRepoLocation
|