2015-09-14 18:49:48 +00:00
|
|
|
{- git-annex special remote configuration
|
|
|
|
-
|
2019-10-10 16:32:05 +00:00
|
|
|
- Copyright 2011-2019 Joey Hess <id@joeyh.name>
|
2015-09-14 18:49:48 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2015-09-14 18:49:48 +00:00
|
|
|
-}
|
|
|
|
|
2019-10-11 19:32:56 +00:00
|
|
|
module Annex.SpecialRemote (
|
|
|
|
module Annex.SpecialRemote,
|
|
|
|
module Annex.SpecialRemote.Config
|
|
|
|
) where
|
2015-09-14 18:49:48 +00:00
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2019-10-10 16:48:26 +00:00
|
|
|
import Annex.SpecialRemote.Config
|
2019-10-11 19:32:56 +00:00
|
|
|
import Remote (remoteTypes)
|
2019-10-10 19:31:10 +00:00
|
|
|
import Types.Remote (RemoteConfig, SetupStage(..), typename, setup)
|
2017-08-17 16:26:14 +00:00
|
|
|
import Types.GitConfig
|
2019-10-11 19:32:56 +00:00
|
|
|
import Config
|
|
|
|
import Remote.List
|
2015-09-14 18:49:48 +00:00
|
|
|
import Logs.Remote
|
|
|
|
import Logs.Trust
|
|
|
|
import qualified Git.Config
|
2019-10-11 19:32:56 +00:00
|
|
|
import qualified Types.Remote as Remote
|
2016-11-30 18:35:24 +00:00
|
|
|
import Git.Types (RemoteName)
|
2015-09-14 18:49:48 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import Data.Ord
|
|
|
|
|
|
|
|
{- See if there's an existing special remote with this name.
|
|
|
|
-
|
|
|
|
- Prefer remotes that are not dead when a name appears multiple times. -}
|
2019-10-11 18:59:41 +00:00
|
|
|
findExisting :: RemoteName -> Annex (Maybe (UUID, RemoteConfig, Maybe (ConfigFrom UUID)))
|
2015-09-14 18:49:48 +00:00
|
|
|
findExisting name = do
|
|
|
|
t <- trustMap
|
2018-04-13 18:50:14 +00:00
|
|
|
headMaybe
|
2019-10-11 18:59:41 +00:00
|
|
|
. sortBy (comparing $ \(u, _, _) -> Down $ M.lookup u t)
|
2015-09-14 18:49:48 +00:00
|
|
|
. findByName name
|
|
|
|
<$> Logs.Remote.readRemoteLog
|
|
|
|
|
2019-10-11 18:59:41 +00:00
|
|
|
findByName :: RemoteName -> M.Map UUID RemoteConfig -> [(UUID, RemoteConfig, Maybe (ConfigFrom UUID))]
|
|
|
|
findByName n = map sameasuuid . filter (matching . snd) . M.toList
|
|
|
|
where
|
|
|
|
matching c = case lookupName c of
|
|
|
|
Nothing -> False
|
|
|
|
Just n'
|
|
|
|
| n' == n -> True
|
|
|
|
| otherwise -> False
|
|
|
|
sameasuuid (u, c) = case M.lookup sameasUUIDField c of
|
|
|
|
Nothing -> (u, c, Nothing)
|
|
|
|
Just u' -> (toUUID u', c, Just (ConfigFrom u))
|
|
|
|
|
2019-10-11 16:45:30 +00:00
|
|
|
newConfig
|
|
|
|
:: RemoteName
|
|
|
|
-> Maybe (Sameas UUID)
|
|
|
|
-> RemoteConfig
|
|
|
|
-- ^ configuration provided by the user
|
|
|
|
-> M.Map UUID RemoteConfig
|
|
|
|
-- ^ configuration of other special remotes, to inherit from
|
|
|
|
-- when sameas is used
|
|
|
|
-> RemoteConfig
|
|
|
|
newConfig name sameas fromuser m = case sameas of
|
|
|
|
Nothing -> M.insert nameField name fromuser
|
|
|
|
Just (Sameas u) -> addSameasInherited m $ M.fromList
|
|
|
|
[ (sameasNameField, name)
|
|
|
|
, (sameasUUIDField, fromUUID u)
|
|
|
|
] `M.union` fromuser
|
2015-09-14 18:49:48 +00:00
|
|
|
|
2015-10-26 18:55:40 +00:00
|
|
|
specialRemoteMap :: Annex (M.Map UUID RemoteName)
|
|
|
|
specialRemoteMap = do
|
2015-09-14 18:49:48 +00:00
|
|
|
m <- Logs.Remote.readRemoteLog
|
2015-10-26 18:55:40 +00:00
|
|
|
return $ M.fromList $ mapMaybe go (M.toList m)
|
|
|
|
where
|
2019-10-10 16:32:05 +00:00
|
|
|
go (u, c) = case lookupName c of
|
2015-10-26 18:55:40 +00:00
|
|
|
Nothing -> Nothing
|
|
|
|
Just n -> Just (u, n)
|
2015-09-14 18:49:48 +00:00
|
|
|
|
2019-10-10 16:32:05 +00:00
|
|
|
{- find the remote type -}
|
2015-09-14 18:49:48 +00:00
|
|
|
findType :: RemoteConfig -> Either String RemoteType
|
2019-10-10 19:31:10 +00:00
|
|
|
findType config = maybe unspecified specified $ M.lookup typeField config
|
2015-09-14 18:49:48 +00:00
|
|
|
where
|
|
|
|
unspecified = Left "Specify the type of remote with type="
|
|
|
|
specified s = case filter (findtype s) remoteTypes of
|
|
|
|
[] -> Left $ "Unknown remote type " ++ s
|
|
|
|
(t:_) -> Right t
|
|
|
|
findtype s i = typename i == s
|
|
|
|
|
|
|
|
autoEnable :: Annex ()
|
|
|
|
autoEnable = do
|
2015-09-14 19:34:15 +00:00
|
|
|
remotemap <- M.filter configured <$> readRemoteLog
|
2019-10-11 19:32:56 +00:00
|
|
|
enabled <- getenabledremotes
|
|
|
|
forM_ (M.toList remotemap) $ \(cu, c) -> unless (cu `M.member` enabled) $ do
|
|
|
|
let u = case findSameasUUID c of
|
|
|
|
Just (Sameas u') -> u'
|
|
|
|
Nothing -> cu
|
2019-10-10 16:32:05 +00:00
|
|
|
case (lookupName c, findType c) of
|
2015-09-14 19:34:15 +00:00
|
|
|
(Just name, Right t) -> whenM (canenable u) $ do
|
2015-09-14 18:49:48 +00:00
|
|
|
showSideAction $ "Auto enabling special remote " ++ name
|
2017-08-17 16:26:14 +00:00
|
|
|
dummycfg <- liftIO dummyRemoteGitConfig
|
2017-12-05 19:00:50 +00:00
|
|
|
tryNonAsync (setup t (Enable c) (Just u) Nothing c dummycfg) >>= \case
|
2015-09-14 18:49:48 +00:00
|
|
|
Left e -> warning (show e)
|
2019-10-11 19:32:56 +00:00
|
|
|
Right (_c, _u) ->
|
|
|
|
when (cu /= u) $
|
|
|
|
setConfig (remoteConfig c "config-uuid") (fromUUID cu)
|
2015-09-14 18:49:48 +00:00
|
|
|
_ -> return ()
|
|
|
|
where
|
2015-09-14 19:34:15 +00:00
|
|
|
configured rc = fromMaybe False $
|
2019-10-10 19:31:10 +00:00
|
|
|
Git.Config.isTrue =<< M.lookup autoEnableField rc
|
2015-09-14 19:34:15 +00:00
|
|
|
canenable u = (/= DeadTrusted) <$> lookupTrust u
|
2019-10-11 19:32:56 +00:00
|
|
|
getenabledremotes = M.fromList
|
|
|
|
. map (\r -> (getcu r, r))
|
|
|
|
<$> remoteList
|
|
|
|
getcu r = fromMaybe
|
|
|
|
(Remote.uuid r)
|
|
|
|
(remoteAnnexConfigUUID (Remote.gitconfig r))
|