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, 2010 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
46 import qualified Ganeti.HTools.Types as T
47 import qualified Ganeti.HTools.Container as Container
49 -- * Type declarations
51 -- | The instance type
52 data Instance = Instance { name :: String -- ^ The instance name
53 , alias :: String -- ^ The shortened name
54 , mem :: Int -- ^ Memory of the instance
55 , dsk :: Int -- ^ Disk size of instance
56 , vcpus :: Int -- ^ Number of VCPUs
57 , running :: Bool -- ^ Is the instance running?
58 , runSt :: String -- ^ Original (text) run status
59 , pNode :: T.Ndx -- ^ Original primary node
60 , sNode :: T.Ndx -- ^ Original secondary node
61 , idx :: T.Idx -- ^ Internal index
62 , util :: T.DynUtil -- ^ Dynamic resource usage
63 , movable :: Bool -- ^ Can the instance be moved?
64 , tags :: [String] -- ^ List of instance tags
65 } deriving (Show, Read)
67 instance T.Element Instance where
72 allNames n = [name n, alias n]
74 -- | Running instance states.
75 runningStates :: [String]
76 runningStates = ["running", "ERROR_up"]
78 -- | A simple name for the int, instance association list.
79 type AssocList = [(T.Idx, Instance)]
81 -- | A simple name for an instance map.
82 type List = Container.Container Instance
86 -- | Create an instance.
88 -- Some parameters are not initialized by function, and must be set
89 -- later (via 'setIdx' for example).
90 create :: String -> Int -> Int -> Int -> String
91 -> [String] -> T.Ndx -> T.Ndx -> Instance
92 create name_init mem_init dsk_init vcpus_init run_init tags_init pn sn =
93 Instance { name = name_init
98 , running = run_init `elem` runningStates
108 -- | Changes the index.
110 -- This is used only during the building of the data structures.
111 setIdx :: Instance -- ^ The original instance
112 -> T.Idx -- ^ New index
113 -> Instance -- ^ The modified instance
114 setIdx t i = t { idx = i }
116 -- | Changes the name.
118 -- This is used only during the building of the data structures.
119 setName :: Instance -- ^ The original instance
120 -> String -- ^ New name
121 -> Instance -- ^ The modified instance
122 setName t s = t { name = s, alias = s }
124 -- | Changes the alias.
126 -- This is used only during the building of the data structures.
127 setAlias :: Instance -- ^ The original instance
128 -> String -- ^ New alias
129 -> Instance -- ^ The modified instance
130 setAlias t s = t { alias = s }
132 -- * Update functions
134 -- | Changes the primary node of the instance.
135 setPri :: Instance -- ^ the original instance
136 -> T.Ndx -- ^ the new primary node
137 -> Instance -- ^ the modified instance
138 setPri t p = t { pNode = p }
140 -- | Changes the secondary node of the instance.
141 setSec :: Instance -- ^ the original instance
142 -> T.Ndx -- ^ the new secondary node
143 -> Instance -- ^ the modified instance
144 setSec t s = t { sNode = s }
146 -- | Changes both nodes of the instance.
147 setBoth :: Instance -- ^ the original instance
148 -> T.Ndx -- ^ new primary node index
149 -> T.Ndx -- ^ new secondary node index
150 -> Instance -- ^ the modified instance
151 setBoth t p s = t { pNode = p, sNode = s }
153 setMovable :: Instance -- ^ The original instance
154 -> Bool -- ^ New movable flag
155 -> Instance -- ^ The modified instance
156 setMovable t m = t { movable = m }
158 -- | Try to shrink the instance based on the reason why we can't
160 shrinkByType :: Instance -> T.FailMode -> T.Result Instance
161 shrinkByType inst T.FailMem = let v = mem inst - T.unitMem
163 then T.Bad "out of memory"
164 else T.Ok inst { mem = v }
165 shrinkByType inst T.FailDisk = let v = dsk inst - T.unitDsk
167 then T.Bad "out of disk"
168 else T.Ok inst { dsk = v }
169 shrinkByType inst T.FailCPU = let v = vcpus inst - T.unitCpu
171 then T.Bad "out of vcpus"
172 else T.Ok inst { vcpus = v }
173 shrinkByType _ f = T.Bad $ "Unhandled failure mode " ++ show f
175 -- | Return the spec of an instance.
176 specOf :: Instance -> T.RSpec
177 specOf Instance { mem = m, dsk = d, vcpus = c } =
178 T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }