git-annex/Assistant/WebApp/Configurators/AWS.hs

234 lines
7.4 KiB
Haskell
Raw Normal View History

2012-11-24 20:30:15 +00:00
{- git-annex assistant webapp configurators for Amazon AWS services
-
- Copyright 2012 Joey Hess <id@joeyh.name>
2012-11-24 20:30:15 +00:00
-
- Licensed under the GNU AGPL version 3 or higher.
-}
2013-06-05 01:02:09 +00:00
{-# LANGUAGE CPP, QuasiQuotes, TemplateHaskell, OverloadedStrings #-}
2012-11-24 20:30:15 +00:00
module Assistant.WebApp.Configurators.AWS where
2012-11-25 04:26:46 +00:00
import Assistant.WebApp.Common
2013-10-28 15:33:14 +00:00
import Assistant.WebApp.MakeRemote
2012-11-24 20:30:15 +00:00
#ifdef WITH_S3
import qualified Remote.S3 as S3
import Logs.Remote
import qualified Remote
import qualified Types.Remote as Remote
#endif
import qualified Remote.Glacier as Glacier
import qualified Remote.Helper.AWS as AWS
2012-11-24 20:30:15 +00:00
import Types.Remote (RemoteConfig)
import Types.StandardGroups
import Creds
import Assistant.Gpg
import Git.Types (RemoteName)
import Annex.SpecialRemote.Config
2012-11-24 20:30:15 +00:00
import qualified Data.Text as T
import qualified Data.Map as M
import Data.Char
2012-11-24 20:30:15 +00:00
awsConfigurator :: Widget -> Handler Html
awsConfigurator = page "Add an Amazon repository" (Just Configuration)
2012-11-24 20:30:15 +00:00
glacierConfigurator :: Widget -> Handler Html
2012-11-24 20:30:15 +00:00
glacierConfigurator a = do
ifM (liftIO $ inPath "glacier")
( awsConfigurator a
, awsConfigurator needglaciercli
)
where
needglaciercli = $(widgetFile "configurators/needglaciercli")
data StorageClass = StandardRedundancy | StandardInfrequentAccess | ReducedRedundancy
2012-11-24 20:30:15 +00:00
deriving (Eq, Enum, Bounded)
instance Show StorageClass where
show StandardRedundancy = "STANDARD"
show StandardInfrequentAccess = "STANDARD_IA"
2012-11-24 20:30:15 +00:00
show ReducedRedundancy = "REDUCED_REDUNDANCY"
data AWSInput = AWSInput
{ accessKeyID :: Text
, secretAccessKey :: Text
, datacenter :: Text
-- Only used for S3, not Glacier.
, storageClass :: StorageClass
, repoName :: Text
, enableEncryption :: EnableEncryption
2012-11-24 20:30:15 +00:00
}
data AWSCreds = AWSCreds Text Text
extractCreds :: AWSInput -> AWSCreds
extractCreds i = AWSCreds (accessKeyID i) (secretAccessKey i)
2013-06-03 20:33:05 +00:00
s3InputAForm :: Maybe CredPair -> MkAForm AWSInput
s3InputAForm defcreds = AWSInput
<$> accessKeyIDFieldWithHelp (T.pack . fst <$> defcreds)
<*> secretAccessKeyField (T.pack . snd <$> defcreds)
<*> datacenterField AWS.S3
2014-04-18 00:07:09 +00:00
<*> areq (selectFieldList storageclasses) (bfs "Storage class") (Just StandardRedundancy)
<*> areq textField (bfs "Repository name") (Just "S3")
<*> enableEncryptionField
2012-11-24 20:30:15 +00:00
where
storageclasses :: [(Text, StorageClass)]
storageclasses =
[ ("Standard redundancy", StandardRedundancy)
2015-10-02 16:12:02 +00:00
#ifdef WITH_S3
, ("Infrequent access (cheaper for backups and archives)", StandardInfrequentAccess)
#endif
2012-11-24 20:30:15 +00:00
, ("Reduced redundancy (costs less)", ReducedRedundancy)
]
2013-06-03 20:33:05 +00:00
glacierInputAForm :: Maybe CredPair -> MkAForm AWSInput
glacierInputAForm defcreds = AWSInput
<$> accessKeyIDFieldWithHelp (T.pack . fst <$> defcreds)
<*> secretAccessKeyField (T.pack . snd <$> defcreds)
<*> datacenterField AWS.Glacier
2012-11-24 20:30:15 +00:00
<*> pure StandardRedundancy
2014-04-18 00:07:09 +00:00
<*> areq textField (bfs "Repository name") (Just "glacier")
<*> enableEncryptionField
2012-11-24 20:30:15 +00:00
2013-06-03 20:33:05 +00:00
awsCredsAForm :: Maybe CredPair -> MkAForm AWSCreds
awsCredsAForm defcreds = AWSCreds
<$> accessKeyIDFieldWithHelp (T.pack . fst <$> defcreds)
<*> secretAccessKeyField (T.pack . snd <$> defcreds)
2013-06-03 20:33:05 +00:00
accessKeyIDField :: Widget -> Maybe Text -> MkAForm Text
2014-04-18 00:07:09 +00:00
accessKeyIDField help = areq (textField `withNote` help) (bfs "Access Key ID")
2013-06-03 20:33:05 +00:00
accessKeyIDFieldWithHelp :: Maybe Text -> MkAForm Text
2013-10-02 05:06:59 +00:00
accessKeyIDFieldWithHelp = accessKeyIDField help
where
help = [whamlet|
<a href="https://portal.aws.amazon.com/gp/aws/securityCredentials#id_block">
Get Amazon access keys
|]
2013-06-03 20:33:05 +00:00
secretAccessKeyField :: Maybe Text -> MkAForm Text
2014-04-18 00:07:09 +00:00
secretAccessKeyField = areq passwordField (bfs "Secret Access Key")
2013-06-03 20:33:05 +00:00
datacenterField :: AWS.Service -> MkAForm Text
2014-04-18 00:07:09 +00:00
datacenterField service = areq (selectFieldList list) (bfs "Datacenter") defregion
where
list = M.toList $ AWS.regionMap service
defregion = Just $ AWS.defaultRegion service
2012-11-24 20:30:15 +00:00
getAddS3R :: Handler Html
2013-03-16 22:48:23 +00:00
getAddS3R = postAddS3R
postAddS3R :: Handler Html
2012-11-24 20:30:15 +00:00
#ifdef WITH_S3
2013-03-16 22:48:23 +00:00
postAddS3R = awsConfigurator $ do
defcreds <- liftAnnex previouslyUsedAWSCreds
((result, form), enctype) <- liftH $
2014-04-18 00:07:09 +00:00
runFormPostNoToken $ renderBootstrap3 bootstrapFormLayout $ s3InputAForm defcreds
2012-11-24 20:30:15 +00:00
case result of
FormSuccess input -> liftH $ do
2012-11-24 20:30:15 +00:00
let name = T.unpack $ repoName input
2013-09-27 04:15:50 +00:00
makeAWSRemote initSpecialRemote S3.remote TransferGroup (extractCreds input) name $ M.fromList
[ configureEncryption $ enableEncryption input
2012-11-24 20:30:15 +00:00
, ("type", "S3")
, ("datacenter", T.unpack $ datacenter input)
, ("storageclass", show $ storageClass input)
, ("chunk", "1MiB")
2012-11-24 20:30:15 +00:00
]
2012-11-25 04:38:11 +00:00
_ -> $(widgetFile "configurators/adds3")
2012-11-24 20:30:15 +00:00
#else
postAddS3R = giveup "S3 not supported by this build"
2012-11-24 20:30:15 +00:00
#endif
getAddGlacierR :: Handler Html
2013-03-16 22:48:23 +00:00
getAddGlacierR = postAddGlacierR
postAddGlacierR :: Handler Html
#ifdef WITH_S3
2013-03-16 22:48:23 +00:00
postAddGlacierR = glacierConfigurator $ do
defcreds <- liftAnnex previouslyUsedAWSCreds
((result, form), enctype) <- liftH $
2014-04-18 00:07:09 +00:00
runFormPostNoToken $ renderBootstrap3 bootstrapFormLayout $ glacierInputAForm defcreds
2012-11-24 20:30:15 +00:00
case result of
FormSuccess input -> liftH $ do
2012-11-24 20:30:15 +00:00
let name = T.unpack $ repoName input
2013-09-27 04:15:50 +00:00
makeAWSRemote initSpecialRemote Glacier.remote SmallArchiveGroup (extractCreds input) name $ M.fromList
[ configureEncryption $ enableEncryption input
2012-11-24 20:30:15 +00:00
, ("type", "glacier")
, ("datacenter", T.unpack $ datacenter input)
]
2012-11-25 04:38:11 +00:00
_ -> $(widgetFile "configurators/addglacier")
#else
postAddGlacierR = giveup "S3 not supported by this build"
#endif
2012-11-24 20:30:15 +00:00
getEnableS3R :: UUID -> Handler Html
2013-04-26 20:07:55 +00:00
#ifdef WITH_S3
2013-04-25 17:14:49 +00:00
getEnableS3R uuid = do
m <- liftAnnex readRemoteLog
2014-10-24 00:25:31 +00:00
if maybe False S3.configIA (M.lookup uuid m)
2013-04-25 17:14:49 +00:00
then redirect $ EnableIAR uuid
else postEnableS3R uuid
2013-04-26 20:07:55 +00:00
#else
getEnableS3R = postEnableS3R
#endif
2013-03-16 22:48:23 +00:00
postEnableS3R :: UUID -> Handler Html
2012-11-24 20:30:15 +00:00
#ifdef WITH_S3
2013-04-25 17:14:49 +00:00
postEnableS3R uuid = awsConfigurator $ enableAWSRemote S3.remote uuid
2012-11-24 20:30:15 +00:00
#else
postEnableS3R _ = giveup "S3 not supported by this build"
2012-11-24 20:30:15 +00:00
#endif
getEnableGlacierR :: UUID -> Handler Html
2013-03-16 22:48:23 +00:00
getEnableGlacierR = postEnableGlacierR
postEnableGlacierR :: UUID -> Handler Html
2013-03-16 22:48:23 +00:00
postEnableGlacierR = glacierConfigurator . enableAWSRemote Glacier.remote
2012-11-24 20:30:15 +00:00
enableAWSRemote :: RemoteType -> UUID -> Widget
#ifdef WITH_S3
2012-11-24 20:30:15 +00:00
enableAWSRemote remotetype uuid = do
defcreds <- liftAnnex previouslyUsedAWSCreds
((result, form), enctype) <- liftH $
2014-04-18 00:07:09 +00:00
runFormPostNoToken $ renderBootstrap3 bootstrapFormLayout $ awsCredsAForm defcreds
2012-11-24 20:30:15 +00:00
case result of
FormSuccess creds -> liftH $ do
m <- liftAnnex readRemoteLog
let name = fromJust $ lookupName $
2012-11-24 20:30:15 +00:00
fromJust $ M.lookup uuid m
2013-09-27 04:15:50 +00:00
makeAWSRemote enableSpecialRemote remotetype SmallArchiveGroup creds name M.empty
2012-11-24 20:30:15 +00:00
_ -> do
description <- liftAnnex $
T.pack <$> Remote.prettyUUID uuid
2012-11-24 20:30:15 +00:00
$(widgetFile "configurators/enableaws")
#else
enableAWSRemote _ _ = giveup "S3 not supported by this build"
#endif
2012-11-24 20:30:15 +00:00
2013-09-27 04:15:50 +00:00
makeAWSRemote :: SpecialRemoteMaker -> RemoteType -> StandardGroup -> AWSCreds -> RemoteName -> RemoteConfig -> Handler ()
makeAWSRemote maker remotetype defaultgroup (AWSCreds ak sk) name config =
setupCloudRemote defaultgroup Nothing $
maker hostname remotetype (Just creds) config
where
creds = (T.unpack ak, T.unpack sk)
{- AWS services use the remote name as the basis for a host
- name, so filter it to contain valid characters. -}
hostname = case filter isAlphaNum name of
[] -> "aws"
n -> n
getRepoInfo :: RemoteConfig -> Widget
getRepoInfo c = [whamlet|S3 remote using bucket: #{bucket}|]
where
bucket = fromMaybe "" $ M.lookup "bucket" c
#ifdef WITH_S3
previouslyUsedAWSCreds :: Annex (Maybe CredPair)
previouslyUsedAWSCreds = getM gettype [S3.remote, Glacier.remote]
where
gettype t = previouslyUsedCredPair AWS.creds t $
not . S3.configIA . Remote.config
#endif