2012-07-31 05:11:32 +00:00
|
|
|
{- git-annex assistant webapp configurators
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
2012-09-24 18:48:47 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-07-31 05:11:32 +00:00
|
|
|
-}
|
|
|
|
|
2012-10-18 14:14:49 +00:00
|
|
|
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes, CPP #-}
|
2012-07-31 05:11:32 +00:00
|
|
|
|
|
|
|
module Assistant.WebApp.Configurators where
|
|
|
|
|
|
|
|
import Assistant.Common
|
2012-10-30 18:44:18 +00:00
|
|
|
import Assistant.DaemonStatus
|
2012-07-31 05:11:32 +00:00
|
|
|
import Assistant.WebApp
|
2012-09-02 04:27:48 +00:00
|
|
|
import Assistant.WebApp.Types
|
2012-07-31 05:11:32 +00:00
|
|
|
import Assistant.WebApp.SideBar
|
2012-10-12 16:45:16 +00:00
|
|
|
import Assistant.WebApp.Utility
|
2012-08-31 19:17:12 +00:00
|
|
|
import Assistant.WebApp.Configurators.Local
|
2012-07-31 05:11:32 +00:00
|
|
|
import Utility.Yesod
|
|
|
|
import qualified Remote
|
2012-08-26 19:39:02 +00:00
|
|
|
import qualified Types.Remote as Remote
|
2012-07-31 05:11:32 +00:00
|
|
|
import Annex.UUID (getUUID)
|
2012-09-13 20:47:44 +00:00
|
|
|
import Logs.Remote
|
|
|
|
import Logs.Trust
|
2012-10-11 23:22:29 +00:00
|
|
|
import Config
|
2012-11-11 00:38:52 +00:00
|
|
|
import qualified Git
|
2012-11-02 16:59:31 +00:00
|
|
|
#ifdef WITH_XMPP
|
|
|
|
import Assistant.XMPP.Client
|
|
|
|
#endif
|
2012-07-31 05:11:32 +00:00
|
|
|
|
|
|
|
import Yesod
|
2012-08-01 01:06:30 +00:00
|
|
|
import Data.Text (Text)
|
2012-09-13 20:47:44 +00:00
|
|
|
import qualified Data.Map as M
|
2012-07-31 05:11:32 +00:00
|
|
|
|
2012-08-03 18:36:16 +00:00
|
|
|
{- The main configuration screen. -}
|
|
|
|
getConfigR :: Handler RepHtml
|
|
|
|
getConfigR = ifM (inFirstRun)
|
|
|
|
( getFirstRepositoryR
|
|
|
|
, bootstrap (Just Config) $ do
|
2012-11-02 16:59:31 +00:00
|
|
|
#ifdef WITH_XMPP
|
2012-10-26 21:13:30 +00:00
|
|
|
xmppconfigured <- lift $ runAnnex False $ isJust <$> getXMPPCreds
|
2012-11-02 16:59:31 +00:00
|
|
|
#else
|
|
|
|
let xmppconfigured = False
|
|
|
|
#endif
|
2012-08-04 00:40:34 +00:00
|
|
|
sideBarDisplay
|
2012-08-03 18:36:16 +00:00
|
|
|
setTitle "Configuration"
|
|
|
|
$(widgetFile "configurators/main")
|
|
|
|
)
|
|
|
|
|
2012-10-12 05:09:28 +00:00
|
|
|
{- An intro message, list of repositories, and nudge to make more. -}
|
|
|
|
introDisplay :: Text -> Widget
|
|
|
|
introDisplay ident = do
|
|
|
|
webapp <- lift getYesod
|
2012-11-11 00:38:52 +00:00
|
|
|
repolist <- lift $ repoList False True False
|
2012-10-12 05:09:28 +00:00
|
|
|
let n = length repolist
|
|
|
|
let numrepos = show n
|
|
|
|
$(widgetFile "configurators/intro")
|
|
|
|
lift $ modifyWebAppState $ \s -> s { showIntro = False }
|
|
|
|
|
2012-11-10 21:08:43 +00:00
|
|
|
makeMiscRepositories :: Widget
|
|
|
|
makeMiscRepositories = $(widgetFile "configurators/repositories/misc")
|
|
|
|
|
|
|
|
makeCloudRepositories :: Widget
|
|
|
|
makeCloudRepositories = $(widgetFile "configurators/repositories/cloud")
|
|
|
|
|
2012-08-05 23:55:06 +00:00
|
|
|
{- Lists known repositories, followed by options to add more. -}
|
|
|
|
getRepositoriesR :: Handler RepHtml
|
|
|
|
getRepositoriesR = bootstrap (Just Config) $ do
|
2012-08-04 00:40:34 +00:00
|
|
|
sideBarDisplay
|
2012-08-05 23:55:06 +00:00
|
|
|
setTitle "Repositories"
|
2012-11-11 00:38:52 +00:00
|
|
|
repolist <- lift $ repoList False False True
|
2012-08-05 23:55:06 +00:00
|
|
|
$(widgetFile "configurators/repositories")
|
2012-08-04 00:40:34 +00:00
|
|
|
|
2012-10-12 05:09:28 +00:00
|
|
|
data Actions
|
|
|
|
= DisabledRepoActions
|
|
|
|
{ setupRepoLink :: Route WebApp }
|
|
|
|
| SyncingRepoActions
|
|
|
|
{ setupRepoLink :: Route WebApp
|
|
|
|
, syncToggleLink :: Route WebApp
|
|
|
|
}
|
|
|
|
| NotSyncingRepoActions
|
|
|
|
{ setupRepoLink :: Route WebApp
|
|
|
|
, syncToggleLink :: Route WebApp
|
|
|
|
}
|
2012-10-09 18:43:53 +00:00
|
|
|
|
2012-10-12 05:09:28 +00:00
|
|
|
mkSyncingRepoActions :: UUID -> Actions
|
|
|
|
mkSyncingRepoActions u = SyncingRepoActions
|
|
|
|
{ setupRepoLink = EditRepositoryR u
|
|
|
|
, syncToggleLink = DisableSyncR u
|
|
|
|
}
|
2012-10-09 18:43:53 +00:00
|
|
|
|
2012-10-12 05:09:28 +00:00
|
|
|
mkNotSyncingRepoActions :: UUID -> Actions
|
|
|
|
mkNotSyncingRepoActions u = NotSyncingRepoActions
|
|
|
|
{ setupRepoLink = EditRepositoryR u
|
|
|
|
, syncToggleLink = EnableSyncR u
|
|
|
|
}
|
2012-10-11 23:22:29 +00:00
|
|
|
|
2012-10-12 05:09:28 +00:00
|
|
|
needsEnabled :: Actions -> Bool
|
|
|
|
needsEnabled (DisabledRepoActions _) = True
|
|
|
|
needsEnabled _ = False
|
|
|
|
|
|
|
|
notSyncing :: Actions -> Bool
|
|
|
|
notSyncing (SyncingRepoActions _ _) = False
|
|
|
|
notSyncing _ = True
|
2012-10-09 18:43:53 +00:00
|
|
|
|
2012-11-11 00:38:52 +00:00
|
|
|
repoTable :: RepoList -> Widget
|
|
|
|
repoTable repolist = $(widgetFile "configurators/repositories/table")
|
|
|
|
|
|
|
|
type RepoList = [(String, String, Actions)]
|
|
|
|
|
2012-10-12 05:09:28 +00:00
|
|
|
{- A numbered list of known repositories,
|
|
|
|
- with actions that can be taken on them. -}
|
2012-11-11 00:38:52 +00:00
|
|
|
repoList :: Bool -> Bool -> Bool -> Handler RepoList
|
|
|
|
repoList onlycloud onlyconfigured includehere
|
2012-09-13 20:47:44 +00:00
|
|
|
| onlyconfigured = list =<< configured
|
2012-10-11 23:22:29 +00:00
|
|
|
| otherwise = list =<< (++) <$> configured <*> rest
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
configured = do
|
2012-11-11 00:38:52 +00:00
|
|
|
rs <- filter wantedrepo . syncRemotes
|
2012-10-31 06:34:03 +00:00
|
|
|
<$> liftAssistant getDaemonStatus
|
|
|
|
runAnnex [] $ do
|
|
|
|
u <- getUUID
|
|
|
|
let l = map Remote.uuid rs
|
|
|
|
let l' = if includehere then u : l else l
|
|
|
|
return $ zip l' $ map mkSyncingRepoActions l'
|
|
|
|
rest = runAnnex [] $ do
|
|
|
|
m <- readRemoteLog
|
2012-11-11 00:38:52 +00:00
|
|
|
unconfigured <- map snd . catMaybes . filter wantedremote
|
2012-11-11 04:26:29 +00:00
|
|
|
. map (findinfo m)
|
|
|
|
<$> (trustExclude DeadTrusted $ M.keys m)
|
2012-11-11 00:38:52 +00:00
|
|
|
unsyncable <- map Remote.uuid . filter wantedrepo <$>
|
2012-10-31 06:34:03 +00:00
|
|
|
(filterM (\r -> not <$> repoSyncable (Remote.repo r))
|
|
|
|
=<< Remote.enabledRemoteList)
|
|
|
|
return $ zip unsyncable (map mkNotSyncingRepoActions unsyncable) ++ unconfigured
|
2012-11-11 00:38:52 +00:00
|
|
|
wantedrepo r
|
|
|
|
| Remote.readonly r = False
|
|
|
|
| onlycloud = Git.repoIsUrl (Remote.repo r) && not (isXMPPRemote r)
|
|
|
|
| otherwise = True
|
|
|
|
wantedremote Nothing = False
|
|
|
|
wantedremote (Just (iscloud, _))
|
|
|
|
| onlycloud = iscloud
|
|
|
|
| otherwise = True
|
|
|
|
findinfo m u = case M.lookup u m of
|
2012-10-31 06:34:03 +00:00
|
|
|
Nothing -> Nothing
|
|
|
|
Just c -> case M.lookup "type" c of
|
2012-11-11 00:38:52 +00:00
|
|
|
Just "rsync" -> val True EnableRsyncR
|
|
|
|
Just "directory" -> val False EnableDirectoryR
|
2012-10-18 14:14:49 +00:00
|
|
|
#ifdef WITH_S3
|
2012-11-11 00:38:52 +00:00
|
|
|
Just "S3" -> val True EnableS3R
|
2012-10-18 14:14:49 +00:00
|
|
|
#endif
|
2012-10-31 06:34:03 +00:00
|
|
|
_ -> Nothing
|
2012-11-11 00:38:52 +00:00
|
|
|
where
|
|
|
|
val iscloud r = Just (iscloud, (u, DisabledRepoActions $ r u))
|
2012-10-31 06:34:03 +00:00
|
|
|
list l = runAnnex [] $ do
|
|
|
|
let l' = nubBy (\x y -> fst x == fst y) l
|
|
|
|
zip3
|
|
|
|
<$> pure counter
|
|
|
|
<*> Remote.prettyListUUIDs (map fst l')
|
|
|
|
<*> pure (map snd l')
|
|
|
|
counter = map show ([1..] :: [Int])
|
2012-08-03 18:36:16 +00:00
|
|
|
|
2012-10-12 05:09:28 +00:00
|
|
|
getEnableSyncR :: UUID -> Handler ()
|
2012-10-12 16:45:16 +00:00
|
|
|
getEnableSyncR = flipSync True
|
2012-10-12 05:09:28 +00:00
|
|
|
|
|
|
|
getDisableSyncR :: UUID -> Handler ()
|
2012-10-12 16:45:16 +00:00
|
|
|
getDisableSyncR = flipSync False
|
|
|
|
|
|
|
|
flipSync :: Bool -> UUID -> Handler ()
|
|
|
|
flipSync enable uuid = do
|
|
|
|
mremote <- runAnnex undefined $ snd <$> Remote.repoFromUUID uuid
|
|
|
|
changeSyncable mremote enable
|
2012-10-12 05:09:28 +00:00
|
|
|
redirect RepositoriesR
|