2012-11-20 20:43:58 +00:00
|
|
|
{- Amazon Glacier remotes.
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-11-29 18:49:20 +00:00
|
|
|
module Remote.Glacier (remote, jobList) where
|
2012-11-20 20:43:58 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
2012-12-01 18:11:37 +00:00
|
|
|
import qualified Data.Text as T
|
2012-11-20 20:43:58 +00:00
|
|
|
import System.Environment
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Types.Remote
|
|
|
|
import Types.Key
|
|
|
|
import qualified Git
|
|
|
|
import Config
|
2013-03-13 20:16:01 +00:00
|
|
|
import Config.Cost
|
2012-11-20 20:43:58 +00:00
|
|
|
import Remote.Helper.Special
|
|
|
|
import Remote.Helper.Encryptable
|
|
|
|
import qualified Remote.Helper.AWS as AWS
|
|
|
|
import Crypto
|
|
|
|
import Creds
|
2013-03-28 21:03:04 +00:00
|
|
|
import Utility.Metered
|
2012-11-20 20:43:58 +00:00
|
|
|
import qualified Annex
|
2013-01-06 18:29:01 +00:00
|
|
|
import Annex.Content
|
2012-11-20 20:43:58 +00:00
|
|
|
|
2012-11-25 17:27:20 +00:00
|
|
|
import System.Process
|
|
|
|
|
2012-11-20 20:43:58 +00:00
|
|
|
type Vault = String
|
|
|
|
type Archive = FilePath
|
|
|
|
|
|
|
|
remote :: RemoteType
|
|
|
|
remote = RemoteType {
|
|
|
|
typename = "glacier",
|
|
|
|
enumerate = findSpecialRemotes "glacier",
|
|
|
|
generate = gen,
|
|
|
|
setup = glacierSetup
|
|
|
|
}
|
|
|
|
|
2013-01-01 17:52:47 +00:00
|
|
|
gen :: Git.Repo -> UUID -> RemoteConfig -> RemoteGitConfig -> Annex Remote
|
|
|
|
gen r u c gc = new <$> remoteCost gc veryExpensiveRemoteCost
|
2012-11-30 04:55:59 +00:00
|
|
|
where
|
|
|
|
new cst = encryptableRemote c
|
2012-11-20 20:43:58 +00:00
|
|
|
(storeEncrypted this)
|
|
|
|
(retrieveEncrypted this)
|
|
|
|
this
|
2012-11-30 04:55:59 +00:00
|
|
|
where
|
|
|
|
this = Remote {
|
|
|
|
uuid = u,
|
|
|
|
cost = cst,
|
|
|
|
name = Git.repoDescribe r,
|
2012-12-13 04:45:27 +00:00
|
|
|
storeKey = store this,
|
2012-11-30 04:55:59 +00:00
|
|
|
retrieveKeyFile = retrieve this,
|
|
|
|
retrieveKeyFileCheap = retrieveCheap this,
|
|
|
|
removeKey = remove this,
|
|
|
|
hasKey = checkPresent this,
|
|
|
|
hasKeyCheap = False,
|
|
|
|
whereisKey = Nothing,
|
|
|
|
config = c,
|
|
|
|
repo = r,
|
2013-01-01 17:52:47 +00:00
|
|
|
gitconfig = gc,
|
2012-11-30 04:55:59 +00:00
|
|
|
localpath = Nothing,
|
|
|
|
readonly = False,
|
2013-03-15 23:16:13 +00:00
|
|
|
globallyAvailable = True,
|
2012-11-30 04:55:59 +00:00
|
|
|
remotetype = remote
|
|
|
|
}
|
2012-11-20 20:43:58 +00:00
|
|
|
|
|
|
|
glacierSetup :: UUID -> RemoteConfig -> Annex RemoteConfig
|
|
|
|
glacierSetup u c = do
|
|
|
|
c' <- encryptionSetup c
|
|
|
|
let fullconfig = c' `M.union` defaults
|
|
|
|
genVault fullconfig u
|
|
|
|
gitConfigSpecialRemote u fullconfig "glacier" "true"
|
|
|
|
setRemoteCredPair fullconfig (AWS.creds u)
|
|
|
|
where
|
|
|
|
remotename = fromJust (M.lookup "name" c)
|
|
|
|
defvault = remotename ++ "-" ++ fromUUID u
|
|
|
|
defaults = M.fromList
|
2012-12-01 18:11:37 +00:00
|
|
|
[ ("datacenter", T.unpack $ AWS.defaultRegion AWS.Glacier)
|
2012-11-20 20:43:58 +00:00
|
|
|
, ("vault", defvault)
|
|
|
|
]
|
|
|
|
|
|
|
|
store :: Remote -> Key -> AssociatedFile -> MeterUpdate -> Annex Bool
|
2013-04-11 21:15:45 +00:00
|
|
|
store r k _f p
|
2012-11-20 20:43:58 +00:00
|
|
|
| keySize k == Just 0 = do
|
|
|
|
warning "Cannot store empty files in Glacier."
|
|
|
|
return False
|
2013-01-09 22:42:29 +00:00
|
|
|
| otherwise = sendAnnex k (void $ remove r k) $ \src ->
|
2013-04-11 21:15:45 +00:00
|
|
|
metered (Just p) k $ \meterupdate ->
|
2012-11-25 17:27:20 +00:00
|
|
|
storeHelper r k $ streamMeteredFile src meterupdate
|
2012-11-20 20:43:58 +00:00
|
|
|
|
|
|
|
storeEncrypted :: Remote -> (Cipher, Key) -> Key -> MeterUpdate -> Annex Bool
|
2013-04-11 21:15:45 +00:00
|
|
|
storeEncrypted r (cipher, enck) k p = sendAnnex k (void $ remove r enck) $ \src -> do
|
|
|
|
metered (Just p) k $ \meterupdate ->
|
2012-11-25 17:27:20 +00:00
|
|
|
storeHelper r enck $ \h ->
|
2013-09-01 18:12:00 +00:00
|
|
|
encrypt (getGpgEncParams r) cipher (feedFile src)
|
2012-11-25 17:27:20 +00:00
|
|
|
(readBytes $ meteredWrite meterupdate h)
|
|
|
|
|
2013-04-11 21:15:45 +00:00
|
|
|
retrieve :: Remote -> Key -> AssociatedFile -> FilePath -> MeterUpdate -> Annex Bool
|
|
|
|
retrieve r k _f d p = metered (Just p) k $ \meterupdate ->
|
2012-11-25 17:42:28 +00:00
|
|
|
retrieveHelper r k $
|
|
|
|
readBytes $ meteredWriteFile meterupdate d
|
|
|
|
|
|
|
|
retrieveCheap :: Remote -> Key -> FilePath -> Annex Bool
|
|
|
|
retrieveCheap _ _ _ = return False
|
|
|
|
|
2013-04-11 21:15:45 +00:00
|
|
|
retrieveEncrypted :: Remote -> (Cipher, Key) -> Key -> FilePath -> MeterUpdate -> Annex Bool
|
|
|
|
retrieveEncrypted r (cipher, enck) k d p = metered (Just p) k $ \meterupdate ->
|
2012-11-25 17:42:28 +00:00
|
|
|
retrieveHelper r enck $ readBytes $ \b ->
|
|
|
|
decrypt cipher (feedBytes b) $
|
|
|
|
readBytes $ meteredWriteFile meterupdate d
|
|
|
|
|
2012-11-25 17:27:20 +00:00
|
|
|
storeHelper :: Remote -> Key -> (Handle -> IO ()) -> Annex Bool
|
|
|
|
storeHelper r k feeder = go =<< glacierEnv c u
|
|
|
|
where
|
2012-11-30 04:55:59 +00:00
|
|
|
c = config r
|
2012-11-25 17:27:20 +00:00
|
|
|
u = uuid r
|
|
|
|
params = glacierParams c
|
2012-11-20 20:43:58 +00:00
|
|
|
[ Param "archive"
|
|
|
|
, Param "upload"
|
|
|
|
, Param "--name", Param $ archive r k
|
2012-11-30 04:55:59 +00:00
|
|
|
, Param $ getVault $ config r
|
2012-11-25 17:27:20 +00:00
|
|
|
, Param "-"
|
2012-11-20 20:43:58 +00:00
|
|
|
]
|
2012-11-25 17:27:20 +00:00
|
|
|
go Nothing = return False
|
|
|
|
go (Just e) = do
|
|
|
|
let p = (proc "glacier" (toCommand params)) { env = Just e }
|
|
|
|
liftIO $ catchBoolIO $
|
|
|
|
withHandle StdinHandle createProcessSuccess p $ \h -> do
|
|
|
|
feeder h
|
|
|
|
return True
|
2012-11-20 20:43:58 +00:00
|
|
|
|
2012-11-25 17:42:28 +00:00
|
|
|
retrieveHelper :: Remote -> Key -> (Handle -> IO ()) -> Annex Bool
|
|
|
|
retrieveHelper r k reader = go =<< glacierEnv c u
|
|
|
|
where
|
2012-11-30 04:55:59 +00:00
|
|
|
c = config r
|
2012-11-25 17:42:28 +00:00
|
|
|
u = uuid r
|
|
|
|
params = glacierParams c
|
2012-11-20 20:43:58 +00:00
|
|
|
[ Param "archive"
|
|
|
|
, Param "retrieve"
|
2012-11-25 17:42:28 +00:00
|
|
|
, Param "-o-"
|
2012-11-30 04:55:59 +00:00
|
|
|
, Param $ getVault $ config r
|
2012-11-20 20:43:58 +00:00
|
|
|
, Param $ archive r k
|
|
|
|
]
|
2012-11-25 17:42:28 +00:00
|
|
|
go Nothing = return False
|
|
|
|
go (Just e) = do
|
|
|
|
let p = (proc "glacier" (toCommand params)) { env = Just e }
|
|
|
|
ok <- liftIO $ catchBoolIO $
|
|
|
|
withHandle StdoutHandle createProcessSuccess p $ \h ->
|
|
|
|
ifM (hIsEOF h)
|
|
|
|
( return False
|
|
|
|
, do
|
|
|
|
reader h
|
|
|
|
return True
|
|
|
|
)
|
|
|
|
unless ok later
|
|
|
|
return ok
|
|
|
|
later = showLongNote "Recommend you wait up to 4 hours, and then run this command again."
|
2012-11-20 20:43:58 +00:00
|
|
|
|
|
|
|
remove :: Remote -> Key -> Annex Bool
|
|
|
|
remove r k = glacierAction r
|
|
|
|
[ Param "archive"
|
|
|
|
, Param "delete"
|
2012-11-30 04:55:59 +00:00
|
|
|
, Param $ getVault $ config r
|
2012-11-20 20:43:58 +00:00
|
|
|
, Param $ archive r k
|
|
|
|
]
|
|
|
|
|
|
|
|
checkPresent :: Remote -> Key -> Annex (Either String Bool)
|
|
|
|
checkPresent r k = do
|
|
|
|
showAction $ "checking " ++ name r
|
2012-11-30 04:55:59 +00:00
|
|
|
go =<< glacierEnv (config r) (uuid r)
|
2012-11-20 20:43:58 +00:00
|
|
|
where
|
|
|
|
go Nothing = return $ Left "cannot check glacier"
|
2012-11-25 17:27:20 +00:00
|
|
|
go (Just e) = do
|
2012-11-20 20:43:58 +00:00
|
|
|
{- glacier checkpresent outputs the archive name to stdout if
|
|
|
|
- it's present. -}
|
|
|
|
v <- liftIO $ catchMsgIO $
|
2012-11-25 17:27:20 +00:00
|
|
|
readProcessEnv "glacier" (toCommand params) (Just e)
|
2012-11-20 20:43:58 +00:00
|
|
|
case v of
|
|
|
|
Right s -> do
|
|
|
|
let probablypresent = key2file k `elem` lines s
|
|
|
|
if probablypresent
|
|
|
|
then ifM (Annex.getFlag "trustglacier")
|
|
|
|
( return $ Right True, untrusted )
|
|
|
|
else return $ Right False
|
2012-11-25 17:27:20 +00:00
|
|
|
Left err -> return $ Left err
|
2012-11-20 20:43:58 +00:00
|
|
|
|
|
|
|
params =
|
|
|
|
[ Param "archive"
|
|
|
|
, Param "checkpresent"
|
2012-11-30 04:55:59 +00:00
|
|
|
, Param $ getVault $ config r
|
2012-11-21 23:35:28 +00:00
|
|
|
, Param "--quiet"
|
2012-11-20 20:43:58 +00:00
|
|
|
, Param $ archive r k
|
|
|
|
]
|
|
|
|
|
glacier: Better handling of the glacier inventory, which avoids duplicate uploads to the same glacier repository by `git annex copy`.
The checkpresent hook can return either True or, False, or fail with a message
if it cannot successfully check the remote. Currently for glacier, when
--trust-glacier is not set, it always returns False. Crucially, in the case
when a file is in glacier, this is telling git-annex it's not there, so copy
re-uploads it. This is not desirable; it breaks using glacier-cli to retreive
that file later, and it wastes money/bandwidth.
What if it instead, when the glacier inventory is missing a
file, it returns False. And when the glacier inventory has a file, unless
--trust-glacier is set, it *fails*.
The result would be:
* `git annex copy --to glacier` would only send things not listed in inventory. If a file is listed in the inventory, `copy`
would complain that --trust-glacier` is not set, and not re-upload the file.
* `git annex drop` would only trust that glacier has a file when --trust-glacier is set. Behavior unchanged.
* `git annex move --to glacier`, when the file is not listed in inventory, would send the file, and delete it locally. Behavior unchanged.
* `git annex move --to glacier`, when the file is listed in inventory, would only trust that glacier has the file when --trust-glacier is set
* `git annex copy --from glacier` / `git annex get`, when the file is located in glacier, would trust the location log, and attempt to get the file from glacier.
2013-05-29 17:52:42 +00:00
|
|
|
untrusted = return $ Left $ unlines
|
2012-11-20 20:43:58 +00:00
|
|
|
[ "Glacier's inventory says it has a copy."
|
|
|
|
, "However, the inventory could be out of date, if it was recently removed."
|
|
|
|
, "(Use --trust-glacier if you're sure it's still in Glacier.)"
|
|
|
|
, ""
|
|
|
|
]
|
|
|
|
|
|
|
|
glacierAction :: Remote -> [CommandParam] -> Annex Bool
|
2012-11-30 04:55:59 +00:00
|
|
|
glacierAction r params = runGlacier (config r) (uuid r) params
|
2012-11-20 20:43:58 +00:00
|
|
|
|
|
|
|
runGlacier :: RemoteConfig -> UUID -> [CommandParam] -> Annex Bool
|
|
|
|
runGlacier c u params = go =<< glacierEnv c u
|
|
|
|
where
|
|
|
|
go Nothing = return False
|
2012-11-25 17:27:20 +00:00
|
|
|
go (Just e) = liftIO $
|
|
|
|
boolSystemEnv "glacier" (glacierParams c params) (Just e)
|
2012-11-20 20:43:58 +00:00
|
|
|
|
2012-11-25 17:27:20 +00:00
|
|
|
glacierParams :: RemoteConfig -> [CommandParam] -> [CommandParam]
|
|
|
|
glacierParams c params = datacenter:params
|
|
|
|
where
|
2012-11-20 20:43:58 +00:00
|
|
|
datacenter = Param $ "--region=" ++
|
|
|
|
(fromJust $ M.lookup "datacenter" c)
|
|
|
|
|
|
|
|
glacierEnv :: RemoteConfig -> UUID -> Annex (Maybe [(String, String)])
|
2012-11-28 17:31:49 +00:00
|
|
|
glacierEnv c u = go =<< getRemoteCredPairFor "glacier" c creds
|
2012-11-20 20:43:58 +00:00
|
|
|
where
|
|
|
|
go Nothing = return Nothing
|
|
|
|
go (Just (user, pass)) = do
|
2012-11-25 17:27:20 +00:00
|
|
|
e <- liftIO getEnvironment
|
|
|
|
return $ Just $ (uk, user):(pk, pass):e
|
2012-11-20 20:43:58 +00:00
|
|
|
|
|
|
|
creds = AWS.creds u
|
|
|
|
(uk, pk) = credPairEnvironment creds
|
|
|
|
|
2012-11-29 18:49:20 +00:00
|
|
|
getVault :: RemoteConfig -> Vault
|
|
|
|
getVault = fromJust . M.lookup "vault"
|
2012-11-20 20:43:58 +00:00
|
|
|
|
|
|
|
archive :: Remote -> Key -> Archive
|
|
|
|
archive r k = fileprefix ++ key2file k
|
|
|
|
where
|
2012-11-30 04:55:59 +00:00
|
|
|
fileprefix = M.findWithDefault "" "fileprefix" $ config r
|
2012-11-20 20:43:58 +00:00
|
|
|
|
|
|
|
-- glacier vault create will succeed even if the vault already exists.
|
|
|
|
genVault :: RemoteConfig -> UUID -> Annex ()
|
|
|
|
genVault c u = unlessM (runGlacier c u params) $
|
|
|
|
error "Failed creating glacier vault."
|
|
|
|
where
|
|
|
|
params =
|
|
|
|
[ Param "vault"
|
|
|
|
, Param "create"
|
2012-11-29 18:49:20 +00:00
|
|
|
, Param $ getVault c
|
2012-11-20 20:43:58 +00:00
|
|
|
]
|
2012-11-29 18:49:20 +00:00
|
|
|
|
|
|
|
{- Partitions the input list of keys into ones which have
|
|
|
|
- glacier retieval jobs that have succeeded, or failed.
|
|
|
|
-
|
|
|
|
- A complication is that `glacier job list` will display the encrypted
|
|
|
|
- keys when the remote is encrypted.
|
|
|
|
-}
|
|
|
|
jobList :: Remote -> [Key] -> Annex ([Key], [Key])
|
2012-11-30 04:55:59 +00:00
|
|
|
jobList r keys = go =<< glacierEnv (config r) (uuid r)
|
2012-11-29 18:49:20 +00:00
|
|
|
where
|
|
|
|
params = [ Param "job", Param "list" ]
|
|
|
|
nada = ([], [])
|
2012-11-30 04:55:59 +00:00
|
|
|
myvault = getVault $ config r
|
2012-11-29 18:49:20 +00:00
|
|
|
|
|
|
|
go Nothing = return nada
|
|
|
|
go (Just e) = do
|
|
|
|
v <- liftIO $ catchMaybeIO $
|
|
|
|
readProcessEnv "glacier" (toCommand params) (Just e)
|
|
|
|
maybe (return nada) extract v
|
|
|
|
|
|
|
|
extract s = do
|
|
|
|
let result@(succeeded, failed) =
|
|
|
|
parse nada $ (map words . lines) s
|
|
|
|
if result == nada
|
|
|
|
then return nada
|
|
|
|
else do
|
|
|
|
enckeys <- forM keys $ \k ->
|
|
|
|
maybe k snd <$> cipherKey (config r) k
|
|
|
|
let keymap = M.fromList $ zip enckeys keys
|
|
|
|
let convert = catMaybes . map (`M.lookup` keymap)
|
|
|
|
return (convert succeeded, convert failed)
|
|
|
|
|
|
|
|
parse c [] = c
|
|
|
|
parse c@(succeeded, failed) ((status:_date:vault:key:[]):rest)
|
|
|
|
| vault == myvault =
|
|
|
|
case file2key key of
|
|
|
|
Nothing -> parse c rest
|
|
|
|
Just k
|
|
|
|
| "a/d" `isPrefixOf` status ->
|
|
|
|
parse (k:succeeded, failed) rest
|
|
|
|
| "a/e" `isPrefixOf` status ->
|
|
|
|
parse (succeeded, k:failed) rest
|
|
|
|
| otherwise ->
|
|
|
|
parse c rest
|
|
|
|
parse c (_:rest) = parse c rest
|