2011-03-28 02:00:44 +00:00
|
|
|
{- Amazon S3 remotes.
|
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2011-03-29 03:51:07 +00:00
|
|
|
module Remote.S3 (remote) where
|
2011-03-28 02:00:44 +00:00
|
|
|
|
2011-03-29 21:20:22 +00:00
|
|
|
import Control.Exception.Extensible (IOException)
|
2011-03-28 05:32:47 +00:00
|
|
|
import Network.AWS.AWSConnection
|
|
|
|
import Network.AWS.S3Object
|
|
|
|
import Network.AWS.S3Bucket
|
|
|
|
import Network.AWS.AWSResult
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as L
|
2011-03-29 17:49:54 +00:00
|
|
|
import qualified Data.Map as M
|
2011-03-29 20:21:21 +00:00
|
|
|
import Data.Maybe
|
2011-03-29 22:28:37 +00:00
|
|
|
import Control.Monad (when)
|
2011-03-28 05:32:47 +00:00
|
|
|
import Control.Monad.State (liftIO)
|
|
|
|
import System.Environment
|
2011-03-28 02:00:44 +00:00
|
|
|
|
|
|
|
import RemoteClass
|
|
|
|
import Types
|
|
|
|
import qualified GitRepo as Git
|
|
|
|
import qualified Annex
|
|
|
|
import UUID
|
2011-03-29 20:21:21 +00:00
|
|
|
import Messages
|
2011-03-29 21:57:20 +00:00
|
|
|
import Locations
|
2011-03-30 18:00:54 +00:00
|
|
|
import Remote.Special
|
2011-03-28 02:00:44 +00:00
|
|
|
|
2011-03-29 03:51:07 +00:00
|
|
|
remote :: RemoteType Annex
|
2011-03-29 18:55:59 +00:00
|
|
|
remote = RemoteType {
|
|
|
|
typename = "S3",
|
2011-03-30 18:00:54 +00:00
|
|
|
enumerate = findSpecialRemotes "s3",
|
|
|
|
generate = gen,
|
2011-03-29 18:55:59 +00:00
|
|
|
setup = s3Setup
|
|
|
|
}
|
2011-03-29 03:51:07 +00:00
|
|
|
|
2011-03-30 18:00:54 +00:00
|
|
|
gen :: Git.Repo -> UUID -> Cost -> Maybe (M.Map String String) -> Annex (Remote Annex)
|
|
|
|
gen r u cst c = return this
|
2011-03-29 21:57:20 +00:00
|
|
|
where
|
|
|
|
this = Remote {
|
|
|
|
uuid = u,
|
|
|
|
cost = cst,
|
|
|
|
name = Git.repoDescribe r,
|
2011-03-30 18:00:54 +00:00
|
|
|
storeKey = store this,
|
|
|
|
retrieveKeyFile = retrieve this,
|
|
|
|
removeKey = remove this,
|
|
|
|
hasKey = checkPresent this,
|
2011-03-29 21:57:20 +00:00
|
|
|
hasKeyCheap = False,
|
|
|
|
config = c
|
|
|
|
}
|
2011-03-28 05:32:47 +00:00
|
|
|
|
2011-03-29 20:21:21 +00:00
|
|
|
s3Connection :: M.Map String String -> IO AWSConnection
|
|
|
|
s3Connection c = do
|
|
|
|
ak <- getEnvKey "AWS_ACCESS_KEY_ID"
|
|
|
|
sk <- getEnvKey "AWS_SECRET_ACCESS_KEY"
|
|
|
|
return $ AWSConnection host port ak sk
|
2011-03-28 05:32:47 +00:00
|
|
|
where
|
2011-03-29 20:21:21 +00:00
|
|
|
host = fromJust $ (M.lookup "host" c)
|
|
|
|
port = let s = fromJust $ (M.lookup "port" c) in
|
|
|
|
case reads s of
|
|
|
|
[(p, _)] -> p
|
|
|
|
_ -> error $ "bad S3 port value: " ++ s
|
|
|
|
getEnvKey s = catch (getEnv s) (error $ "Set " ++ s)
|
2011-03-28 05:32:47 +00:00
|
|
|
|
2011-03-29 18:55:59 +00:00
|
|
|
s3Setup :: UUID -> M.Map String String -> Annex (M.Map String String)
|
|
|
|
s3Setup u c = do
|
2011-03-29 20:21:21 +00:00
|
|
|
-- verify configuration is sane
|
|
|
|
case M.lookup "encryption" c of
|
|
|
|
Nothing -> error "Specify encryption=key or encryption=none"
|
|
|
|
Just "none" -> return ()
|
2011-03-29 21:20:22 +00:00
|
|
|
Just _ -> error "encryption keys not yet supported"
|
2011-03-29 20:21:21 +00:00
|
|
|
let fullconfig = M.union c defaults
|
|
|
|
|
2011-03-29 21:20:22 +00:00
|
|
|
-- check bucket location to see if the bucket exists, and create it
|
2011-03-29 20:21:21 +00:00
|
|
|
let datacenter = fromJust $ M.lookup "datacenter" fullconfig
|
|
|
|
conn <- liftIO $ s3Connection fullconfig
|
|
|
|
showNote "checking bucket"
|
|
|
|
loc <- liftIO $ getBucketLocation conn bucket
|
|
|
|
case loc of
|
|
|
|
Right _ -> return ()
|
|
|
|
Left err@(NetworkError _) -> error $ prettyReqError err
|
|
|
|
Left (AWSError _ _) -> do
|
2011-03-29 21:20:22 +00:00
|
|
|
showNote $ "creating bucket in " ++ datacenter
|
2011-03-29 20:21:21 +00:00
|
|
|
res <- liftIO $ createBucketIn conn bucket datacenter
|
|
|
|
case res of
|
|
|
|
Right _ -> return ()
|
|
|
|
Left err -> error $ prettyReqError err
|
|
|
|
|
2011-03-30 18:32:08 +00:00
|
|
|
gitConfigSpecialRemote u fullconfig "s3" "true"
|
2011-03-29 20:21:21 +00:00
|
|
|
return fullconfig
|
|
|
|
where
|
2011-03-29 21:20:22 +00:00
|
|
|
remotename = fromJust (M.lookup "name" c)
|
|
|
|
bucket = remotename ++ "-" ++ u
|
2011-03-29 20:21:21 +00:00
|
|
|
defaults = M.fromList
|
|
|
|
[ ("datacenter", "US")
|
|
|
|
, ("storageclass", "STANDARD")
|
|
|
|
, ("host", defaultAmazonS3Host)
|
|
|
|
, ("port", show defaultAmazonS3Port)
|
|
|
|
, ("bucket", bucket)
|
|
|
|
]
|
|
|
|
|
2011-03-29 21:20:22 +00:00
|
|
|
s3Action :: Remote Annex -> ((AWSConnection, String) -> Annex a) -> Annex a
|
|
|
|
s3Action r a = do
|
|
|
|
when (config r == Nothing) $
|
|
|
|
error $ "Missing configuration for special remote " ++ name r
|
|
|
|
conn <- liftIO $ s3Connection (fromJust $ config r)
|
|
|
|
let bucket = fromJust $ M.lookup "bucket" $ fromJust $ config r
|
|
|
|
a (conn, bucket)
|
|
|
|
|
2011-03-29 22:09:22 +00:00
|
|
|
bucketKey :: String -> Key -> L.ByteString -> S3Object
|
|
|
|
bucketKey bucket k content = S3Object bucket (show k) "" [] content
|
2011-03-29 18:55:59 +00:00
|
|
|
|
2011-03-30 18:00:54 +00:00
|
|
|
checkPresent :: Remote Annex -> Key -> Annex (Either IOException Bool)
|
|
|
|
checkPresent r k = s3Action r $ \(conn, bucket) -> do
|
2011-03-29 21:20:22 +00:00
|
|
|
showNote ("checking " ++ name r ++ "...")
|
2011-03-29 22:09:22 +00:00
|
|
|
res <- liftIO $ getObjectInfo conn $ bucketKey bucket k L.empty
|
2011-03-28 05:32:47 +00:00
|
|
|
case res of
|
2011-03-29 21:20:22 +00:00
|
|
|
Right _ -> return $ Right True
|
|
|
|
Left (AWSError _ _) -> return $ Right False
|
|
|
|
Left e -> return $ Left (error $ prettyReqError e)
|
2011-03-29 20:21:21 +00:00
|
|
|
|
2011-03-30 18:00:54 +00:00
|
|
|
store :: Remote Annex -> Key -> Annex Bool
|
|
|
|
store r k = s3Action r $ \(conn, bucket) -> do
|
2011-03-29 21:57:20 +00:00
|
|
|
g <- Annex.gitRepo
|
|
|
|
content <- liftIO $ L.readFile $ gitAnnexLocation g k
|
2011-03-29 22:09:22 +00:00
|
|
|
let object = setStorageClass storageclass $ bucketKey bucket k content
|
2011-03-29 21:20:22 +00:00
|
|
|
res <- liftIO $ sendObject conn object
|
|
|
|
case res of
|
|
|
|
Right _ -> return True
|
|
|
|
Left e -> do
|
|
|
|
warning $ prettyReqError e
|
|
|
|
return False
|
2011-03-29 20:21:21 +00:00
|
|
|
where
|
2011-03-29 21:20:22 +00:00
|
|
|
storageclass =
|
|
|
|
case fromJust $ M.lookup "storageclass" $ fromJust $ config r of
|
|
|
|
"REDUCED_REDUNDANCY" -> REDUCED_REDUNDANCY
|
|
|
|
_ -> STANDARD
|
2011-03-29 22:09:22 +00:00
|
|
|
|
2011-03-30 18:00:54 +00:00
|
|
|
retrieve :: Remote Annex -> Key -> FilePath -> Annex Bool
|
|
|
|
retrieve r k f = s3Action r $ \(conn, bucket) -> do
|
2011-03-29 22:09:22 +00:00
|
|
|
res <- liftIO $ getObject conn $ bucketKey bucket k L.empty
|
|
|
|
case res of
|
|
|
|
Right o -> do
|
|
|
|
liftIO $ L.writeFile f (obj_data o)
|
|
|
|
return True
|
|
|
|
Left e -> do
|
|
|
|
warning $ prettyReqError e
|
|
|
|
return False
|
2011-03-29 22:21:05 +00:00
|
|
|
|
2011-03-30 18:00:54 +00:00
|
|
|
remove :: Remote Annex -> Key -> Annex Bool
|
|
|
|
remove r k = s3Action r $ \(conn, bucket) -> do
|
2011-03-29 22:21:05 +00:00
|
|
|
res <- liftIO $ deleteObject conn $ bucketKey bucket k L.empty
|
|
|
|
case res of
|
|
|
|
Right _ -> return True
|
|
|
|
Left e -> do
|
|
|
|
warning $ prettyReqError e
|
|
|
|
return False
|