Shorten some function names
[ganeti-local] / htools / Ganeti / HTools / Loader.hs
index a9c9e0e..169dd48 100644 (file)
@@ -7,7 +7,7 @@ has been loaded from external sources.
 
 {-
 
-Copyright (C) 2009, 2010, 2011 Google Inc.
+Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -90,7 +90,8 @@ data ClusterData = ClusterData
   , cdNodes     :: Node.List     -- ^ The node list
   , cdInstances :: Instance.List -- ^ The instance list
   , cdTags      :: [String]      -- ^ The cluster tags
-  } deriving (Show, Read)
+  , cdIPolicy   :: IPolicy       -- ^ The cluster instance policy
+  } deriving (Show, Read, Eq)
 
 -- | The priority of a match in a lookup result.
 data MatchPriority = ExactMatch
@@ -116,6 +117,7 @@ instance Ord LookupResult where
 -- | An empty cluster.
 emptyCluster :: ClusterData
 emptyCluster = ClusterData Container.empty Container.empty Container.empty []
+                 defIPolicy
 
 -- * Functions
 
@@ -217,6 +219,15 @@ fixNodes accu inst =
             in Container.add sdx snew ac2
        else ac2
 
+-- | Set the node's policy to its group one. Note that this requires
+-- the group to exist (should have been checked before), otherwise it
+-- will abort with a runtime error.
+setNodePolicy :: Group.List -> Node.Node -> Node.Node
+setNodePolicy gl node =
+  let grp = Container.find (Node.group node) gl
+      gpol = Group.iPolicy grp
+  in Node.setPolicy gpol node
+
 -- | Remove non-selected tags from the exclusion list.
 filterExTags :: [String] -> Instance.Instance -> Instance.Instance
 filterExTags tl inst =
@@ -267,7 +278,7 @@ mergeData :: [(String, DynUtil)]  -- ^ Instance utilisation data
           -> [String]             -- ^ Excluded instances
           -> ClusterData          -- ^ Data from backends
           -> Result ClusterData   -- ^ Fixed cluster data
-mergeData um extags selinsts exinsts cdata@(ClusterData _ nl il2 tags) =
+mergeData um extags selinsts exinsts cdata@(ClusterData gl nl il2 tags _) =
   let il = Container.elems il2
       il3 = foldl' (\im (name, n_util) ->
                         case Container.findByName im name of
@@ -283,16 +294,17 @@ mergeData um extags selinsts exinsts cdata@(ClusterData _ nl il2 tags) =
       lkp_unknown = filter (not . goodLookupResult) (selinst_lkp ++ exinst_lkp)
       selinst_names = map lrContent selinst_lkp
       exinst_names = map lrContent exinst_lkp
-      il4 = Container.map (filterExTags allextags .
-                           updateMovable selinst_names exinst_names) il3
-      nl2 = foldl' fixNodes nl (Container.elems il4)
-      nl3 = Container.map (`Node.buildPeers` il4) nl2
       node_names = map Node.name (Container.elems nl)
       common_suffix = longestDomain (node_names ++ inst_names)
-      snl = Container.map (computeAlias common_suffix) nl3
-      sil = Container.map (computeAlias common_suffix) il4
+      il4 = Container.map (computeAlias common_suffix .
+                           filterExTags allextags .
+                           updateMovable selinst_names exinst_names) il3
+      nl2 = foldl' fixNodes nl (Container.elems il4)
+      nl3 = Container.map (setNodePolicy gl .
+                           computeAlias common_suffix .
+                           (`Node.buildPeers` il4)) nl2
   in if' (null lkp_unknown)
-         (Ok cdata { cdNodes = snl, cdInstances = sil })
+         (Ok cdata { cdNodes = nl3, cdInstances = il4 })
          (Bad $ "Unknown instance(s): " ++ show(map lrContent lkp_unknown))
 
 -- | Checks the cluster data for consistency.
@@ -329,7 +341,7 @@ nodeImem :: Node.Node -> Instance.List -> Int
 nodeImem node il =
   let rfind = flip Container.find il
       il' = map rfind $ Node.pList node
-      oil' = filter (not . Instance.instanceOffline) il'
+      oil' = filter Instance.notOffline il'
   in sum . map Instance.mem $ oil'