module Ganeti.MaintD.CleanupIncidents
( cleanupIncidents
) where
import Control.Arrow ((&&&))
import Control.Monad (unless)
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString.UTF8 as UTF8
import Data.IORef (IORef)
import Ganeti.BasicTypes (ResultT, mkResultT)
import qualified Ganeti.HTools.Container as Container
import qualified Ganeti.HTools.Node as Node
import Ganeti.Logging.Lifted
import Ganeti.MaintD.MemoryState (MemoryState, getIncidents, rmIncident)
import Ganeti.Objects.Maintenance (Incident(..), RepairStatus(..))
import Ganeti.Utils (logAndBad)
cleanupIncident :: IORef MemoryState
-> Node.List
-> Incident
-> ResultT String IO ()
cleanupIncident memstate nl incident = do
let location = incidentNode incident
uuid = incidentUuid incident
tag = incidentTag incident
nodes = filter ((==) location . Node.name) $ Container.elems nl
case nodes of
[] -> do
logInfo $ "No node any more with name " ++ location
++ "; will forget event " ++ UTF8.toString uuid
liftIO . rmIncident memstate $ UTF8.toString uuid
[nd] -> unless (tag `elem` Node.nTags nd) $ do
logInfo $ "Tag " ++ tag ++ " removed on " ++ location
++ "; will forget event " ++ UTF8.toString uuid
liftIO . rmIncident memstate $ UTF8.toString uuid
_ -> mkResultT . logAndBad
$ "Found More than one node with name " ++ location
cleanupIncidents :: IORef MemoryState -> Node.List -> ResultT String IO ()
cleanupIncidents memstate nl = do
incidents <- getIncidents memstate
let finalized = filter ((> RSPending) . incidentRepairStatus) incidents
logDebug . (++) "Finalized incidents " . show
$ map (incidentNode &&& incidentUuid) finalized
mapM_ (cleanupIncident memstate nl) finalized