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
45 import qualified Ganeti.HTools.Types as T
46 import qualified Ganeti.HTools.Container as Container
48 -- * Type declarations
50 -- | The instance type
51 data Instance = Instance { name :: String -- ^ The instance name
52 , mem :: Int -- ^ Memory of the instance
53 , dsk :: Int -- ^ Disk size of instance
54 , vcpus :: Int -- ^ Number of VCPUs
55 , running :: Bool -- ^ Is the instance running?
56 , runSt :: String -- ^ Original (text) run status
57 , pNode :: T.Ndx -- ^ Original primary node
58 , sNode :: T.Ndx -- ^ Original secondary node
59 , idx :: T.Idx -- ^ Internal index
60 , util :: T.DynUtil -- ^ Dynamic resource usage
61 , movable :: Bool -- ^ Can the instance be moved?
62 , tags :: [String] -- ^ List of instance tags
65 instance T.Element Instance where
71 -- | Base memory unit.
81 -- | Running instance states.
82 runningStates :: [String]
83 runningStates = ["running", "ERROR_up"]
85 -- | A simple name for the int, instance association list.
86 type AssocList = [(T.Idx, Instance)]
88 -- | A simple name for an instance map.
89 type List = Container.Container Instance
93 -- | Create an instance.
95 -- Some parameters are not initialized by function, and must be set
96 -- later (via 'setIdx' for example).
97 create :: String -> Int -> Int -> Int -> String
98 -> [String] -> T.Ndx -> T.Ndx -> Instance
99 create name_init mem_init dsk_init vcpus_init run_init tags_init pn sn =
100 Instance { name = name_init
104 , running = run_init `elem` runningStates
114 -- | Changes the index.
116 -- This is used only during the building of the data structures.
117 setIdx :: Instance -- ^ The original instance
118 -> T.Idx -- ^ New index
119 -> Instance -- ^ The modified instance
120 setIdx t i = t { idx = i }
122 -- | Changes the name.
124 -- This is used only during the building of the data structures.
125 setName :: Instance -- ^ The original instance
126 -> String -- ^ New name
127 -> Instance -- ^ The modified instance
128 setName t s = t { name = s }
130 -- * Update functions
132 -- | Changes the primary node of the instance.
133 setPri :: Instance -- ^ the original instance
134 -> T.Ndx -- ^ the new primary node
135 -> Instance -- ^ the modified instance
136 setPri t p = t { pNode = p }
138 -- | Changes the secondary node of the instance.
139 setSec :: Instance -- ^ the original instance
140 -> T.Ndx -- ^ the new secondary node
141 -> Instance -- ^ the modified instance
142 setSec t s = t { sNode = s }
144 -- | Changes both nodes of the instance.
145 setBoth :: Instance -- ^ the original instance
146 -> T.Ndx -- ^ new primary node index
147 -> T.Ndx -- ^ new secondary node index
148 -> Instance -- ^ the modified instance
149 setBoth t p s = t { pNode = p, sNode = s }
151 setMovable :: Instance -- ^ The original instance
152 -> Bool -- ^ New movable flag
153 -> Instance -- ^ The modified instance
154 setMovable t m = t { movable = m }
156 -- | Try to shrink the instance based on the reason why we can't
158 shrinkByType :: Instance -> T.FailMode -> T.Result Instance
159 shrinkByType inst T.FailMem = let v = mem inst - unitMem
161 then T.Bad "out of memory"
162 else T.Ok inst { mem = v }
163 shrinkByType inst T.FailDisk = let v = dsk inst - unitDsk
165 then T.Bad "out of disk"
166 else T.Ok inst { dsk = v }
167 shrinkByType inst T.FailCPU = let v = vcpus inst - unitCpu
169 then T.Bad "out of vcpus"
170 else T.Ok inst { vcpus = v }
171 shrinkByType _ f = T.Bad $ "Unhandled failure mode " ++ show f
173 -- | Return the spec of an instance.
174 specOf :: Instance -> T.RSpec
175 specOf Instance { mem = m, dsk = d, vcpus = c } =
176 T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }