module Ganeti.MaintD.MemoryState
( MemoryState(..)
, emptyMemoryState
, getJobs
, clearJobs
, appendJobs
, getEvacuated
, addEvacuated
, rmEvacuated
, getIncidents
, updateIncident
, rmIncident
) where
import Control.Monad.IO.Class (liftIO)
import Data.IORef (IORef)
import Ganeti.BasicTypes (ResultT, withErrorT)
import Ganeti.Lens (makeCustomLenses)
import Ganeti.Objects.Maintenance (Incident)
import Ganeti.Types (JobId, uuidOf)
import Ganeti.Utils (ordNub)
import Ganeti.Utils.IORef (atomicModifyWithLens_)
import Ganeti.WConfd.Client ( runNewWConfdClient, maintenanceJobs, runModifyRpc
, clearMaintdJobs, appendMaintdJobs
, maintenanceEvacuated, addMaintdEvacuated
, rmMaintdEvacuated
, maintenanceIncidents, updateMaintdIncident
, rmMaintdIncident )
data MemoryState = MemoryState
{ msJobs :: [ JobId ]
, msEvacuated :: [ String ]
, msIncidents :: [ Incident ]
}
$(makeCustomLenses ''MemoryState)
emptyMemoryState :: MemoryState
emptyMemoryState = MemoryState { msJobs = []
, msEvacuated = []
, msIncidents = []
}
getJobs :: IORef MemoryState -> ResultT String IO [JobId]
getJobs memstate = do
jobs <- withErrorT show $ runNewWConfdClient maintenanceJobs
liftIO . atomicModifyWithLens_ memstate msJobsL $ const jobs
return jobs
clearJobs :: IORef MemoryState -> IO ()
clearJobs memstate = do
runModifyRpc clearMaintdJobs
atomicModifyWithLens_ memstate msJobsL $ const []
appendJobs :: IORef MemoryState -> [JobId] -> IO ()
appendJobs memstate jobs = do
runModifyRpc $ appendMaintdJobs jobs
atomicModifyWithLens_ memstate msJobsL $ ordNub . (++ jobs)
getEvacuated :: IORef MemoryState -> ResultT String IO [String]
getEvacuated memstate = do
evac <- withErrorT show $ runNewWConfdClient maintenanceEvacuated
liftIO . atomicModifyWithLens_ memstate msEvacuatedL $ const evac
return evac
addEvacuated :: IORef MemoryState -> [String] -> IO ()
addEvacuated memstate names = do
runModifyRpc $ addMaintdEvacuated names
atomicModifyWithLens_ memstate msEvacuatedL $ ordNub . (++ names)
rmEvacuated :: IORef MemoryState -> String -> IO ()
rmEvacuated memstate name = do
runModifyRpc $ rmMaintdEvacuated name
atomicModifyWithLens_ memstate msEvacuatedL $ filter (/= name)
getIncidents :: IORef MemoryState -> ResultT String IO [Incident]
getIncidents memstate = do
incidents <- withErrorT show $ runNewWConfdClient maintenanceIncidents
liftIO . atomicModifyWithLens_ memstate msIncidentsL $ const incidents
return incidents
updateIncident :: IORef MemoryState -> Incident -> IO ()
updateIncident memstate incident = do
runModifyRpc $ updateMaintdIncident incident
atomicModifyWithLens_ memstate msIncidentsL
$ (incident :) . filter ((/= uuidOf incident) . uuidOf)
rmIncident :: IORef MemoryState -> String -> IO ()
rmIncident memstate uuid = do
runModifyRpc $ rmMaintdIncident uuid
atomicModifyWithLens_ memstate msIncidentsL
$ filter ((/= uuid) . uuidOf)