1 {-| Module describing an instance.
3 The instance data type holds very few fields, the algorithm
4 intelligence is in the "Node" and "Cluster" modules.
10 Copyright (C) 2009 Google Inc.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 module Ganeti.HTools.Instance
42 import qualified Ganeti.HTools.Types as T
43 import qualified Ganeti.HTools.Container as Container
45 -- * Type declarations
47 -- | The instance type
48 data Instance = Instance { name :: String -- ^ The instance name
49 , mem :: Int -- ^ Memory of the instance
50 , dsk :: Int -- ^ Disk size of instance
51 , vcpus :: Int -- ^ Number of VCPUs
52 , running :: Bool -- ^ Is the instance running?
53 , runSt :: String -- ^ Original (text) run status
54 , pNode :: T.Ndx -- ^ Original primary node
55 , sNode :: T.Ndx -- ^ Original secondary node
56 , idx :: T.Idx -- ^ Internal index
57 , util :: T.DynUtil -- ^ Dynamic resource usage
60 instance T.Element Instance where
66 -- | Base memory unit.
76 -- | A simple name for the int, instance association list.
77 type AssocList = [(T.Idx, Instance)]
79 -- | A simple name for an instance map.
80 type List = Container.Container Instance
84 -- | Create an instance.
86 -- Some parameters are not initialized by function, and must be set
87 -- later (via 'setIdx' for example).
88 create :: String -> Int -> Int -> Int -> String -> T.Ndx -> T.Ndx -> Instance
89 create name_init mem_init dsk_init vcpus_init run_init pn sn =
90 Instance { name = name_init
94 , running = run_init == "running" || run_init == "ERROR_up"
102 -- | Changes the index.
104 -- This is used only during the building of the data structures.
105 setIdx :: Instance -- ^ The original instance
106 -> T.Idx -- ^ New index
107 -> Instance -- ^ The modified instance
108 setIdx t i = t { idx = i }
110 -- | Changes the name.
112 -- This is used only during the building of the data structures.
113 setName :: Instance -- ^ The original instance
114 -> String -- ^ New name
115 -> Instance -- ^ The modified instance
116 setName t s = t { name = s }
118 -- * Update functions
120 -- | Changes the primary node of the instance.
121 setPri :: Instance -- ^ the original instance
122 -> T.Ndx -- ^ the new primary node
123 -> Instance -- ^ the modified instance
124 setPri t p = t { pNode = p }
126 -- | Changes the secondary node of the instance.
127 setSec :: Instance -- ^ the original instance
128 -> T.Ndx -- ^ the new secondary node
129 -> Instance -- ^ the modified instance
130 setSec t s = t { sNode = s }
132 -- | Changes both nodes of the instance.
133 setBoth :: Instance -- ^ the original instance
134 -> T.Ndx -- ^ new primary node index
135 -> T.Ndx -- ^ new secondary node index
136 -> Instance -- ^ the modified instance
137 setBoth t p s = t { pNode = p, sNode = s }
139 -- | Try to shrink the instance based on the reason why we can't
141 shrinkByType :: Instance -> T.FailMode -> T.Result Instance
142 shrinkByType inst T.FailMem = let v = mem inst - unitMem
144 then T.Bad "out of memory"
145 else T.Ok inst { mem = v }
146 shrinkByType inst T.FailDisk = let v = dsk inst - unitDsk
148 then T.Bad "out of disk"
149 else T.Ok inst { dsk = v }
150 shrinkByType inst T.FailCPU = let v = vcpus inst - unitCpu
152 then T.Bad "out of vcpus"
153 else T.Ok inst { vcpus = v }
154 shrinkByType _ f = T.Bad $ "Unhandled failure mode " ++ show f