Accept both full and short names in CLI
[ganeti-local] / Ganeti / HTools / Types.hs
1 {-| Some common types.
2
3 -}
4
5 {-
6
7 Copyright (C) 2009 Google Inc.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA.
23
24 -}
25
26 module Ganeti.HTools.Types
27     ( Idx
28     , Ndx
29     , NameAssoc
30     , Score
31     , Weight
32     , RSpec(..)
33     , DynUtil(..)
34     , zeroUtil
35     , baseUtil
36     , addUtil
37     , subUtil
38     , defVcpuRatio
39     , defReservedDiskRatio
40     , unitMem
41     , unitCpu
42     , unitDsk
43     , Placement
44     , IMove(..)
45     , MoveJob
46     , JobSet
47     , Result(..)
48     , Element(..)
49     , FailMode(..)
50     , FailStats
51     , OpResult(..)
52     , connTimeout
53     , queryTimeout
54     ) where
55
56 -- | The instance index type.
57 type Idx = Int
58
59 -- | The node index type.
60 type Ndx = Int
61
62 -- | The type used to hold name-to-idx mappings.
63 type NameAssoc = [(String, Int)]
64
65 -- | A separate name for the cluster score type.
66 type Score = Double
67
68 -- | A separate name for a weight metric.
69 type Weight = Double
70
71 -- | The resource spec type.
72 data RSpec = RSpec
73     { rspecCpu  :: Int  -- ^ Requested VCPUs
74     , rspecMem  :: Int  -- ^ Requested memory
75     , rspecDsk  :: Int  -- ^ Requested disk
76     } deriving (Show, Eq)
77
78 -- | The dynamic resource specs of a machine (i.e. load or load
79 -- capacity, as opposed to size).
80 data DynUtil = DynUtil
81     { cpuWeight :: Weight -- ^ Standardised CPU usage
82     , memWeight :: Weight -- ^ Standardised memory load
83     , dskWeight :: Weight -- ^ Standardised disk I\/O usage
84     , netWeight :: Weight -- ^ Standardised network usage
85     } deriving (Show)
86
87 -- | Initial empty utilisation
88 zeroUtil :: DynUtil
89 zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0
90                    , dskWeight = 0, netWeight = 0 }
91
92 baseUtil :: DynUtil
93 baseUtil = DynUtil { cpuWeight = 1, memWeight = 1
94                    , dskWeight = 1, netWeight = 1 }
95
96 addUtil :: DynUtil -> DynUtil -> DynUtil
97 addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
98     DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4)
99
100 subUtil :: DynUtil -> DynUtil -> DynUtil
101 subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
102     DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4)
103
104 -- | The description of an instance placement. It contains the
105 -- instance index, the new primary and secondary node, the move being
106 -- performed and the score of the cluster after the move.
107 type Placement = (Idx, Ndx, Ndx, IMove, Score)
108
109 -- | An instance move definition
110 data IMove = Failover                -- ^ Failover the instance (f)
111            | ReplacePrimary Ndx      -- ^ Replace primary (f, r:np, f)
112            | ReplaceSecondary Ndx    -- ^ Replace secondary (r:ns)
113            | ReplaceAndFailover Ndx  -- ^ Replace secondary, failover (r:np, f)
114            | FailoverAndReplace Ndx  -- ^ Failover, replace secondary (f, r:ns)
115              deriving (Show)
116
117 -- | Formatted solution output for one move (involved nodes and
118 -- commands
119 type MoveJob = ([Ndx], Idx, IMove, [String])
120
121 -- | A list of command elements
122 type JobSet = [MoveJob]
123
124 -- | Connection timeout (when using non-file methods).
125 connTimeout :: Int
126 connTimeout = 15
127
128 -- | The default timeout for queries (when using non-file methods).
129 queryTimeout :: Int
130 queryTimeout = 60
131
132 -- | Default vcpu-to-pcpu ratio (randomly chosen value).
133 defVcpuRatio :: Double
134 defVcpuRatio = 64
135
136 -- | Default max disk usage ratio.
137 defReservedDiskRatio :: Double
138 defReservedDiskRatio = 0
139
140 -- | Base memory unit.
141 unitMem :: Int
142 unitMem = 64
143
144 -- | Base disk unit.
145 unitDsk :: Int
146 unitDsk = 256
147
148 -- | Base vcpus unit.
149 unitCpu :: Int
150 unitCpu = 1
151
152 {-|
153
154 This is similar to the JSON library Result type - *very* similar, but
155 we want to use it in multiple places, so we abstract it into a
156 mini-library here
157
158 -}
159 data Result a
160     = Bad String
161     | Ok a
162     deriving (Show)
163
164 instance Monad Result where
165     (>>=) (Bad x) _ = Bad x
166     (>>=) (Ok x) fn = fn x
167     return = Ok
168     fail = Bad
169
170 -- | Reason for an operation's falure
171 data FailMode = FailMem  -- ^ Failed due to not enough RAM
172               | FailDisk -- ^ Failed due to not enough disk
173               | FailCPU  -- ^ Failed due to not enough CPU capacity
174               | FailN1   -- ^ Failed due to not passing N1 checks
175               | FailTags -- ^ Failed due to tag exclusion
176                 deriving (Eq, Enum, Bounded, Show)
177
178 -- | List with failure statistics
179 type FailStats = [(FailMode, Int)]
180
181 -- | Either-like data-type customized for our failure modes
182 data OpResult a = OpFail FailMode -- ^ Failed operation
183                 | OpGood a        -- ^ Success operation
184                   deriving (Show)
185
186 instance Monad OpResult where
187     (OpGood x) >>= fn = fn x
188     (OpFail y) >>= _ = OpFail y
189     return = OpGood
190
191 -- | A generic class for items that have updateable names and indices.
192 class Element a where
193     -- | Returns the name of the element
194     nameOf  :: a -> String
195     -- | Returns all the known names of the element
196     allNames :: a -> [String]
197     -- | Returns the index of the element
198     idxOf   :: a -> Int
199     -- | Updates the alias of the element
200     setAlias :: a -> String -> a
201     -- | Compute the alias by stripping a given suffix (domain) from
202     -- | the name
203     computeAlias :: String -> a -> a
204     computeAlias dom e = setAlias e alias
205         where alias = take (length name - length dom) name
206               name = nameOf e
207     -- | Updates the index of the element
208     setIdx  :: a -> Int -> a