Revision 55416810 src/Ganeti/HTools/Loader.hs

b/src/Ganeti/HTools/Loader.hs
40 40
  , emptyCluster
41 41
  ) where
42 42

  
43
import Control.Monad
43 44
import Data.List
44 45
import qualified Data.Map as M
45 46
import Data.Maybe
46 47
import Text.Printf (printf)
48
import System.Time (ClockTime(..))
47 49

  
48 50
import qualified Ganeti.HTools.Container as Container
49 51
import qualified Ganeti.HTools.Instance as Instance
......
52 54
import qualified Ganeti.HTools.Cluster as Cluster
53 55

  
54 56
import Ganeti.BasicTypes
57
import qualified Ganeti.Constants as C
55 58
import Ganeti.HTools.Types
56 59
import Ganeti.Utils
57 60

  
......
174 177
    then Instance.setMovable inst False
175 178
    else inst
176 179

  
180
-- | Set the auto-repair policy for an instance.
181
setArPolicy :: [String]       -- ^ Cluster tags
182
            -> Group.List     -- ^ List of node groups
183
            -> Node.List      -- ^ List of nodes
184
            -> Instance.List  -- ^ List of instances
185
            -> Instance.List  -- ^ Updated list of instances
186
setArPolicy ctags gl nl il =
187
  let cpol = fromMaybe ArNotEnabled $ getArPolicy ctags
188
      gpols = Container.map (fromMaybe cpol . getArPolicy . Group.allTags) gl
189
      ipolfn = getArPolicy . Instance.allTags
190
      nlookup = flip Container.find nl . Instance.pNode
191
      glookup = flip Container.find gpols . Node.group . nlookup
192
      updateInstance inst = inst {
193
        Instance.arPolicy = fromMaybe (glookup inst) $ ipolfn inst }
194
  in
195
   Container.map updateInstance il
196

  
197
-- | Get the auto-repair policy from a list of tags.
198
--
199
-- This examines the ganeti:watcher:autorepair and
200
-- ganeti:watcher:autorepair:suspend tags to determine the policy. If none of
201
-- these tags are present, Nothing (and not ArNotEnabled) is returned.
202
getArPolicy :: [String] -> Maybe AutoRepairPolicy
203
getArPolicy tags =
204
  let enabled = mapMaybe (autoRepairTypeFromRaw <=<
205
                          chompPrefix C.autoRepairTagEnabled) tags
206
      suspended = mapMaybe (chompPrefix C.autoRepairTagSuspended) tags
207
      suspTime = if "" `elem` suspended
208
                   then Forever
209
                   else Until . flip TOD 0 . maximum $
210
                        mapMaybe (tryRead "auto-repair suspend time") suspended
211
  in
212
   case () of
213
     -- Note how we must return ArSuspended even if "enabled" is empty, so that
214
     -- node groups or instances can suspend repairs that were enabled at an
215
     -- upper scope (cluster or node group).
216
     _ | not $ null suspended -> Just $ ArSuspended suspTime
217
       | not $ null enabled   -> Just $ ArEnabled (minimum enabled)
218
       | otherwise            -> Nothing
219

  
177 220
-- | Compute the longest common suffix of a list of strings that
178 221
-- starts with a dot.
179 222
longestDomain :: [String] -> String
......
203 246
          -> [String]             -- ^ Excluded instances
204 247
          -> ClusterData          -- ^ Data from backends
205 248
          -> Result ClusterData   -- ^ Fixed cluster data
206
mergeData um extags selinsts exinsts cdata@(ClusterData gl nl il2 tags _) =
207
  let il = Container.elems il2
249
mergeData um extags selinsts exinsts cdata@(ClusterData gl nl il ctags _) =
250
  let il2 = setArPolicy ctags gl nl il
208 251
      il3 = foldl' (\im (name, n_util) ->
209 252
                        case Container.findByName im name of
210 253
                          Nothing -> im -- skipping unknown instance
......
212 255
                              let new_i = inst { Instance.util = n_util }
213 256
                              in Container.add (Instance.idx inst) new_i im
214 257
                   ) il2 um
215
      allextags = extags ++ extractExTags tags
216
      inst_names = map Instance.name il
258
      allextags = extags ++ extractExTags ctags
259
      inst_names = map Instance.name $ Container.elems il3
217 260
      selinst_lkp = map (lookupName inst_names) selinsts
218 261
      exinst_lkp = map (lookupName inst_names) exinsts
219 262
      lkp_unknown = filter (not . goodLookupResult) (selinst_lkp ++ exinst_lkp)

Also available in: Unified diff