htools: return new state from new IAllocator modes
[ganeti-local] / htools / Ganeti / HTools / Instance.hs
index 9ac113c..4f8d5f5 100644 (file)
@@ -41,14 +41,17 @@ module Ganeti.HTools.Instance
     , specOf
     , shrinkByType
     , runningStates
+    , requiredNodes
+    , allNodes
     ) where
 
 import qualified Ganeti.HTools.Types as T
 import qualified Ganeti.HTools.Container as Container
+import qualified Ganeti.Constants as C
 
 -- * Type declarations
 
--- | The instance type
+-- | The instance type.
 data Instance = Instance
     { name         :: String    -- ^ The instance name
     , alias        :: String    -- ^ The shortened name
@@ -62,8 +65,9 @@ data Instance = Instance
     , idx          :: T.Idx     -- ^ Internal index
     , util         :: T.DynUtil -- ^ Dynamic resource usage
     , movable      :: Bool      -- ^ Can the instance be moved?
-    , auto_balance :: Bool      -- ^ Is the instance auto-balanced?
+    , autoBalance  :: Bool      -- ^ Is the instance auto-balanced?
     , tags         :: [String]  -- ^ List of instance tags
+    , diskTemplate :: T.DiskTemplate -- ^ The disk template of the instance
     } deriving (Show, Read)
 
 instance T.Element Instance where
@@ -73,9 +77,9 @@ instance T.Element Instance where
     setIdx   = setIdx
     allNames n = [name n, alias n]
 
--- | Running instance states.
+-- | Constant holding the running instance states.
 runningStates :: [String]
-runningStates = ["running", "ERROR_up"]
+runningStates = [C.inststRunning, C.inststErrorup]
 
 -- | A simple name for the int, instance association list.
 type AssocList = [(T.Idx, Instance)]
@@ -90,9 +94,9 @@ type List = Container.Container Instance
 -- Some parameters are not initialized by function, and must be set
 -- later (via 'setIdx' for example).
 create :: String -> Int -> Int -> Int -> String
-       -> [String] -> Bool -> T.Ndx -> T.Ndx -> Instance
+       -> [String] -> Bool -> T.Ndx -> T.Ndx -> T.DiskTemplate -> Instance
 create name_init mem_init dsk_init vcpus_init run_init tags_init
-       auto_balance_init pn sn =
+       auto_balance_init pn sn dt =
     Instance { name = name_init
              , alias = name_init
              , mem = mem_init
@@ -106,7 +110,8 @@ create name_init mem_init dsk_init vcpus_init run_init tags_init
              , util = T.baseUtil
              , tags = tags_init
              , movable = True
-             , auto_balance = auto_balance_init
+             , autoBalance = auto_balance_init
+             , diskTemplate = dt
              }
 
 -- | Changes the index.
@@ -180,3 +185,14 @@ shrinkByType _ f = T.Bad $ "Unhandled failure mode " ++ show f
 specOf :: Instance -> T.RSpec
 specOf Instance { mem = m, dsk = d, vcpus = c } =
     T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }
+
+-- | Computed the number of nodes for a given disk template
+requiredNodes :: T.DiskTemplate -> Int
+requiredNodes T.DTDrbd8 = 2
+requiredNodes _         = 1
+
+-- | Computes all nodes of an instance.
+allNodes :: Instance -> [T.Ndx]
+allNodes inst = case diskTemplate inst of
+                  T.DTDrbd8 -> [pNode inst, sNode inst]
+                  _ -> [pNode inst]