2024-06-14 20:37:17 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2024 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Command.UpdateCluster where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import qualified Annex
|
|
|
|
import Types.Cluster
|
|
|
|
import Logs.Cluster
|
|
|
|
import qualified Remote as R
|
|
|
|
import qualified Types.Remote as R
|
|
|
|
import qualified Command.UpdateProxy
|
|
|
|
import Utility.SafeOutput
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import qualified Data.Set as S
|
|
|
|
|
|
|
|
cmd :: Command
|
|
|
|
cmd = noMessages $ command "updatecluster" SectionSetup
|
2024-06-14 21:13:23 +00:00
|
|
|
"update records of cluster nodes"
|
2024-06-14 20:37:17 +00:00
|
|
|
paramNothing (withParams seek)
|
|
|
|
|
|
|
|
seek :: CmdParams -> CommandSeek
|
|
|
|
seek = withNothing $ do
|
|
|
|
commandAction start
|
|
|
|
commandAction Command.UpdateProxy.start
|
|
|
|
|
|
|
|
start :: CommandStart
|
|
|
|
start = startingCustomOutput (ActionItemOther Nothing) $ do
|
|
|
|
rs <- R.remoteList
|
2024-08-07 16:27:24 +00:00
|
|
|
let getnode r = case remoteAnnexClusterNode (R.gitconfig r) of
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just [] -> return Nothing
|
|
|
|
Just clusternames ->
|
|
|
|
ifM (Command.UpdateProxy.checkCanProxy r "Cannot use this special remote as a cluster node.")
|
|
|
|
( return $ Just $ M.fromList $
|
|
|
|
zip clusternames (repeat (S.singleton r))
|
|
|
|
, return Nothing
|
|
|
|
)
|
|
|
|
myclusternodes <- M.unionsWith S.union . catMaybes
|
|
|
|
<$> mapM getnode rs
|
2024-06-14 20:37:17 +00:00
|
|
|
myclusters <- annexClusters <$> Annex.getGitConfig
|
|
|
|
recordedclusters <- getClusters
|
|
|
|
descs <- R.uuidDescriptions
|
2024-06-14 21:13:23 +00:00
|
|
|
|
|
|
|
-- Update the cluster log to list the currently configured nodes
|
|
|
|
-- of each configured cluster.
|
|
|
|
forM_ (M.toList myclusters) $ \(clustername, cu) -> do
|
2024-06-14 20:37:17 +00:00
|
|
|
let mynodesremotes = fromMaybe mempty $
|
|
|
|
M.lookup clustername myclusternodes
|
|
|
|
let mynodes = S.map (ClusterNodeUUID . R.uuid) mynodesremotes
|
|
|
|
let recordednodes = fromMaybe mempty $ M.lookup cu $
|
|
|
|
clusterUUIDs recordedclusters
|
2024-06-26 15:24:55 +00:00
|
|
|
proxiednodes <- findProxiedClusterNodes recordednodes
|
2024-06-26 14:51:14 +00:00
|
|
|
let allnodes = S.union mynodes proxiednodes
|
|
|
|
if recordednodes == allnodes
|
2024-06-14 20:37:17 +00:00
|
|
|
then liftIO $ putStrLn $ safeOutput $
|
|
|
|
"No cluster node changes for cluster: " ++ clustername
|
|
|
|
else do
|
2024-06-26 14:51:14 +00:00
|
|
|
describechanges descs clustername recordednodes allnodes mynodesremotes
|
|
|
|
recordCluster cu allnodes
|
2024-06-14 20:37:17 +00:00
|
|
|
|
|
|
|
next $ return True
|
|
|
|
where
|
2024-06-26 14:51:14 +00:00
|
|
|
describechanges descs clustername oldnodes allnodes mynodesremotes = do
|
2024-06-14 20:37:17 +00:00
|
|
|
forM_ (S.toList mynodesremotes) $ \r ->
|
|
|
|
unless (S.member (ClusterNodeUUID (R.uuid r)) oldnodes) $
|
|
|
|
liftIO $ putStrLn $ safeOutput $
|
|
|
|
"Added node " ++ R.name r ++ " to cluster: " ++ clustername
|
|
|
|
forM_ (S.toList oldnodes) $ \n ->
|
2024-06-26 14:51:14 +00:00
|
|
|
unless (S.member n allnodes) $ do
|
2024-06-14 20:37:17 +00:00
|
|
|
let desc = maybe (fromUUID (fromClusterNodeUUID n)) fromUUIDDesc $
|
|
|
|
M.lookup (fromClusterNodeUUID n) descs
|
|
|
|
liftIO $ putStrLn $ safeOutput $
|
|
|
|
"Removed node " ++ desc ++ " from cluster: " ++ clustername
|
2024-06-26 14:51:14 +00:00
|
|
|
|
|
|
|
-- Finds nodes that are proxied by other cluster gateways.
|
2024-06-26 15:24:55 +00:00
|
|
|
findProxiedClusterNodes :: S.Set ClusterNodeUUID -> Annex (S.Set ClusterNodeUUID)
|
|
|
|
findProxiedClusterNodes recordednodes =
|
2024-06-26 14:51:14 +00:00
|
|
|
(S.fromList . map asclusternode . filter isproxynode) <$> R.remoteList
|
|
|
|
where
|
|
|
|
isproxynode r =
|
|
|
|
asclusternode r `S.member` recordednodes
|
2024-06-26 16:56:16 +00:00
|
|
|
&& isJust (remoteAnnexProxiedBy (R.gitconfig r))
|
2024-06-26 14:51:14 +00:00
|
|
|
asclusternode = ClusterNodeUUID . R.uuid
|