root / src / Ganeti / Objects.hs @ 0359e5d0
History | View | Annotate | Download (24.6 kB)
1 |
{-# LANGUAGE TemplateHaskell #-} |
---|---|
2 |
|
3 |
{-| Implementation of the Ganeti config objects. |
4 |
|
5 |
Some object fields are not implemented yet, and as such they are |
6 |
commented out below. |
7 |
|
8 |
-} |
9 |
|
10 |
{- |
11 |
|
12 |
Copyright (C) 2011, 2012, 2013 Google Inc. |
13 |
|
14 |
This program is free software; you can redistribute it and/or modify |
15 |
it under the terms of the GNU General Public License as published by |
16 |
the Free Software Foundation; either version 2 of the License, or |
17 |
(at your option) any later version. |
18 |
|
19 |
This program is distributed in the hope that it will be useful, but |
20 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
22 |
General Public License for more details. |
23 |
|
24 |
You should have received a copy of the GNU General Public License |
25 |
along with this program; if not, write to the Free Software |
26 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
27 |
02110-1301, USA. |
28 |
|
29 |
-} |
30 |
|
31 |
module Ganeti.Objects |
32 |
( HvParams |
33 |
, OsParams |
34 |
, PartialNicParams(..) |
35 |
, FilledNicParams(..) |
36 |
, fillNicParams |
37 |
, allNicParamFields |
38 |
, PartialNic(..) |
39 |
, FileDriver(..) |
40 |
, DiskLogicalId(..) |
41 |
, Disk(..) |
42 |
, includesLogicalId |
43 |
, DiskTemplate(..) |
44 |
, PartialBeParams(..) |
45 |
, FilledBeParams(..) |
46 |
, fillBeParams |
47 |
, allBeParamFields |
48 |
, Instance(..) |
49 |
, toDictInstance |
50 |
, getDiskSizeRequirements |
51 |
, PartialNDParams(..) |
52 |
, FilledNDParams(..) |
53 |
, fillNDParams |
54 |
, allNDParamFields |
55 |
, Node(..) |
56 |
, AllocPolicy(..) |
57 |
, FilledISpecParams(..) |
58 |
, PartialISpecParams(..) |
59 |
, fillISpecParams |
60 |
, allISpecParamFields |
61 |
, MinMaxISpecs(..) |
62 |
, FilledIPolicy(..) |
63 |
, PartialIPolicy(..) |
64 |
, fillIPolicy |
65 |
, DiskParams |
66 |
, NodeGroup(..) |
67 |
, IpFamily(..) |
68 |
, ipFamilyToVersion |
69 |
, fillDict |
70 |
, ClusterHvParams |
71 |
, OsHvParams |
72 |
, ClusterBeParams |
73 |
, ClusterOsParams |
74 |
, ClusterNicParams |
75 |
, Cluster(..) |
76 |
, ConfigData(..) |
77 |
, TimeStampObject(..) |
78 |
, UuidObject(..) |
79 |
, SerialNoObject(..) |
80 |
, TagsObject(..) |
81 |
, DictObject(..) -- re-exported from THH |
82 |
, TagSet -- re-exported from THH |
83 |
, Network(..) |
84 |
, Ip4Address(..) |
85 |
, Ip4Network(..) |
86 |
, readIp4Address |
87 |
, nextIp4Address |
88 |
, IAllocatorParams |
89 |
) where |
90 |
|
91 |
import Control.Applicative |
92 |
import Data.List (foldl') |
93 |
import Data.Maybe |
94 |
import qualified Data.Map as Map |
95 |
import qualified Data.Set as Set |
96 |
import Data.Word |
97 |
import Text.JSON (showJSON, readJSON, JSON, JSValue(..), fromJSString) |
98 |
import qualified Text.JSON as J |
99 |
|
100 |
import qualified AutoConf |
101 |
import qualified Ganeti.Constants as C |
102 |
import qualified Ganeti.ConstantUtils as ConstantUtils |
103 |
import Ganeti.JSON |
104 |
import Ganeti.Types |
105 |
import Ganeti.THH |
106 |
import Ganeti.Utils (sepSplit, tryRead) |
107 |
|
108 |
-- * Generic definitions |
109 |
|
110 |
-- | Fills one map with keys from the other map, if not already |
111 |
-- existing. Mirrors objects.py:FillDict. |
112 |
fillDict :: (Ord k) => Map.Map k v -> Map.Map k v -> [k] -> Map.Map k v |
113 |
fillDict defaults custom skip_keys = |
114 |
let updated = Map.union custom defaults |
115 |
in foldl' (flip Map.delete) updated skip_keys |
116 |
|
117 |
-- | The hypervisor parameter type. This is currently a simple map, |
118 |
-- without type checking on key/value pairs. |
119 |
type HvParams = Container JSValue |
120 |
|
121 |
-- | The OS parameters type. This is, and will remain, a string |
122 |
-- container, since the keys are dynamically declared by the OSes, and |
123 |
-- the values are always strings. |
124 |
type OsParams = Container String |
125 |
|
126 |
-- | Class of objects that have timestamps. |
127 |
class TimeStampObject a where |
128 |
cTimeOf :: a -> Double |
129 |
mTimeOf :: a -> Double |
130 |
|
131 |
-- | Class of objects that have an UUID. |
132 |
class UuidObject a where |
133 |
uuidOf :: a -> String |
134 |
|
135 |
-- | Class of object that have a serial number. |
136 |
class SerialNoObject a where |
137 |
serialOf :: a -> Int |
138 |
|
139 |
-- | Class of objects that have tags. |
140 |
class TagsObject a where |
141 |
tagsOf :: a -> Set.Set String |
142 |
|
143 |
-- * Network definitions |
144 |
|
145 |
-- ** Ipv4 types |
146 |
|
147 |
-- | Custom type for a simple IPv4 address. |
148 |
data Ip4Address = Ip4Address Word8 Word8 Word8 Word8 |
149 |
deriving Eq |
150 |
|
151 |
instance Show Ip4Address where |
152 |
show (Ip4Address a b c d) = show a ++ "." ++ show b ++ "." ++ |
153 |
show c ++ "." ++ show d |
154 |
|
155 |
-- | Parses an IPv4 address from a string. |
156 |
readIp4Address :: (Applicative m, Monad m) => String -> m Ip4Address |
157 |
readIp4Address s = |
158 |
case sepSplit '.' s of |
159 |
[a, b, c, d] -> Ip4Address <$> |
160 |
tryRead "first octect" a <*> |
161 |
tryRead "second octet" b <*> |
162 |
tryRead "third octet" c <*> |
163 |
tryRead "fourth octet" d |
164 |
_ -> fail $ "Can't parse IPv4 address from string " ++ s |
165 |
|
166 |
-- | JSON instance for 'Ip4Address'. |
167 |
instance JSON Ip4Address where |
168 |
showJSON = showJSON . show |
169 |
readJSON (JSString s) = readIp4Address (fromJSString s) |
170 |
readJSON v = fail $ "Invalid JSON value " ++ show v ++ " for an IPv4 address" |
171 |
|
172 |
-- | \"Next\" address implementation for IPv4 addresses. |
173 |
-- |
174 |
-- Note that this loops! Note also that this is a very dumb |
175 |
-- implementation. |
176 |
nextIp4Address :: Ip4Address -> Ip4Address |
177 |
nextIp4Address (Ip4Address a b c d) = |
178 |
let inc xs y = if all (==0) xs then y + 1 else y |
179 |
d' = d + 1 |
180 |
c' = inc [d'] c |
181 |
b' = inc [c', d'] b |
182 |
a' = inc [b', c', d'] a |
183 |
in Ip4Address a' b' c' d' |
184 |
|
185 |
-- | Custom type for an IPv4 network. |
186 |
data Ip4Network = Ip4Network Ip4Address Word8 |
187 |
deriving Eq |
188 |
|
189 |
instance Show Ip4Network where |
190 |
show (Ip4Network ip netmask) = show ip ++ "/" ++ show netmask |
191 |
|
192 |
-- | JSON instance for 'Ip4Network'. |
193 |
instance JSON Ip4Network where |
194 |
showJSON = showJSON . show |
195 |
readJSON (JSString s) = |
196 |
case sepSplit '/' (fromJSString s) of |
197 |
[ip, nm] -> do |
198 |
ip' <- readIp4Address ip |
199 |
nm' <- tryRead "parsing netmask" nm |
200 |
if nm' >= 0 && nm' <= 32 |
201 |
then return $ Ip4Network ip' nm' |
202 |
else fail $ "Invalid netmask " ++ show nm' ++ " from string " ++ |
203 |
fromJSString s |
204 |
_ -> fail $ "Can't parse IPv4 network from string " ++ fromJSString s |
205 |
readJSON v = fail $ "Invalid JSON value " ++ show v ++ " for an IPv4 network" |
206 |
|
207 |
-- ** Ganeti \"network\" config object. |
208 |
|
209 |
-- FIXME: Not all types might be correct here, since they |
210 |
-- haven't been exhaustively deduced from the python code yet. |
211 |
$(buildObject "Network" "network" $ |
212 |
[ simpleField "name" [t| NonEmptyString |] |
213 |
, optionalField $ |
214 |
simpleField "mac_prefix" [t| String |] |
215 |
, simpleField "network" [t| Ip4Network |] |
216 |
, optionalField $ |
217 |
simpleField "network6" [t| String |] |
218 |
, optionalField $ |
219 |
simpleField "gateway" [t| Ip4Address |] |
220 |
, optionalField $ |
221 |
simpleField "gateway6" [t| String |] |
222 |
, optionalField $ |
223 |
simpleField "reservations" [t| String |] |
224 |
, optionalField $ |
225 |
simpleField "ext_reservations" [t| String |] |
226 |
] |
227 |
++ uuidFields |
228 |
++ timeStampFields |
229 |
++ serialFields |
230 |
++ tagsFields) |
231 |
|
232 |
instance SerialNoObject Network where |
233 |
serialOf = networkSerial |
234 |
|
235 |
instance TagsObject Network where |
236 |
tagsOf = networkTags |
237 |
|
238 |
instance UuidObject Network where |
239 |
uuidOf = networkUuid |
240 |
|
241 |
instance TimeStampObject Network where |
242 |
cTimeOf = networkCtime |
243 |
mTimeOf = networkMtime |
244 |
|
245 |
-- * NIC definitions |
246 |
|
247 |
$(buildParam "Nic" "nicp" |
248 |
[ simpleField "mode" [t| NICMode |] |
249 |
, simpleField "link" [t| String |] |
250 |
, simpleField "vlan" [t| String |] |
251 |
]) |
252 |
|
253 |
$(buildObject "PartialNic" "nic" $ |
254 |
[ simpleField "mac" [t| String |] |
255 |
, optionalField $ simpleField "ip" [t| String |] |
256 |
, simpleField "nicparams" [t| PartialNicParams |] |
257 |
, optionalField $ simpleField "network" [t| String |] |
258 |
, optionalField $ simpleField "name" [t| String |] |
259 |
] ++ uuidFields) |
260 |
|
261 |
instance UuidObject PartialNic where |
262 |
uuidOf = nicUuid |
263 |
|
264 |
-- * Disk definitions |
265 |
|
266 |
-- | Constant for the dev_type key entry in the disk config. |
267 |
devType :: String |
268 |
devType = "dev_type" |
269 |
|
270 |
-- | The disk configuration type. This includes the disk type itself, |
271 |
-- for a more complete consistency. Note that since in the Python |
272 |
-- code-base there's no authoritative place where we document the |
273 |
-- logical id, this is probably a good reference point. |
274 |
data DiskLogicalId |
275 |
= LIDPlain String String -- ^ Volume group, logical volume |
276 |
| LIDDrbd8 String String Int Int Int String |
277 |
-- ^ NodeA, NodeB, Port, MinorA, MinorB, Secret |
278 |
| LIDFile FileDriver String -- ^ Driver, path |
279 |
| LIDSharedFile FileDriver String -- ^ Driver, path |
280 |
| LIDBlockDev BlockDriver String -- ^ Driver, path (must be under /dev) |
281 |
| LIDRados String String -- ^ Unused, path |
282 |
| LIDExt String String -- ^ ExtProvider, unique name |
283 |
deriving (Show, Eq) |
284 |
|
285 |
-- | Mapping from a logical id to a disk type. |
286 |
lidDiskType :: DiskLogicalId -> DiskTemplate |
287 |
lidDiskType (LIDPlain {}) = DTPlain |
288 |
lidDiskType (LIDDrbd8 {}) = DTDrbd8 |
289 |
lidDiskType (LIDFile {}) = DTFile |
290 |
lidDiskType (LIDSharedFile {}) = DTSharedFile |
291 |
lidDiskType (LIDBlockDev {}) = DTBlock |
292 |
lidDiskType (LIDRados {}) = DTRbd |
293 |
lidDiskType (LIDExt {}) = DTExt |
294 |
|
295 |
-- | Builds the extra disk_type field for a given logical id. |
296 |
lidEncodeType :: DiskLogicalId -> [(String, JSValue)] |
297 |
lidEncodeType v = [(devType, showJSON . lidDiskType $ v)] |
298 |
|
299 |
-- | Custom encoder for DiskLogicalId (logical id only). |
300 |
encodeDLId :: DiskLogicalId -> JSValue |
301 |
encodeDLId (LIDPlain vg lv) = JSArray [showJSON vg, showJSON lv] |
302 |
encodeDLId (LIDDrbd8 nodeA nodeB port minorA minorB key) = |
303 |
JSArray [ showJSON nodeA, showJSON nodeB, showJSON port |
304 |
, showJSON minorA, showJSON minorB, showJSON key ] |
305 |
encodeDLId (LIDRados pool name) = JSArray [showJSON pool, showJSON name] |
306 |
encodeDLId (LIDFile driver name) = JSArray [showJSON driver, showJSON name] |
307 |
encodeDLId (LIDSharedFile driver name) = |
308 |
JSArray [showJSON driver, showJSON name] |
309 |
encodeDLId (LIDBlockDev driver name) = JSArray [showJSON driver, showJSON name] |
310 |
encodeDLId (LIDExt extprovider name) = |
311 |
JSArray [showJSON extprovider, showJSON name] |
312 |
|
313 |
-- | Custom encoder for DiskLogicalId, composing both the logical id |
314 |
-- and the extra disk_type field. |
315 |
encodeFullDLId :: DiskLogicalId -> (JSValue, [(String, JSValue)]) |
316 |
encodeFullDLId v = (encodeDLId v, lidEncodeType v) |
317 |
|
318 |
-- | Custom decoder for DiskLogicalId. This is manual for now, since |
319 |
-- we don't have yet automation for separate-key style fields. |
320 |
decodeDLId :: [(String, JSValue)] -> JSValue -> J.Result DiskLogicalId |
321 |
decodeDLId obj lid = do |
322 |
dtype <- fromObj obj devType |
323 |
case dtype of |
324 |
DTDrbd8 -> |
325 |
case lid of |
326 |
JSArray [nA, nB, p, mA, mB, k] -> do |
327 |
nA' <- readJSON nA |
328 |
nB' <- readJSON nB |
329 |
p' <- readJSON p |
330 |
mA' <- readJSON mA |
331 |
mB' <- readJSON mB |
332 |
k' <- readJSON k |
333 |
return $ LIDDrbd8 nA' nB' p' mA' mB' k' |
334 |
_ -> fail "Can't read logical_id for DRBD8 type" |
335 |
DTPlain -> |
336 |
case lid of |
337 |
JSArray [vg, lv] -> do |
338 |
vg' <- readJSON vg |
339 |
lv' <- readJSON lv |
340 |
return $ LIDPlain vg' lv' |
341 |
_ -> fail "Can't read logical_id for plain type" |
342 |
DTFile -> |
343 |
case lid of |
344 |
JSArray [driver, path] -> do |
345 |
driver' <- readJSON driver |
346 |
path' <- readJSON path |
347 |
return $ LIDFile driver' path' |
348 |
_ -> fail "Can't read logical_id for file type" |
349 |
DTSharedFile -> |
350 |
case lid of |
351 |
JSArray [driver, path] -> do |
352 |
driver' <- readJSON driver |
353 |
path' <- readJSON path |
354 |
return $ LIDSharedFile driver' path' |
355 |
_ -> fail "Can't read logical_id for shared file type" |
356 |
DTBlock -> |
357 |
case lid of |
358 |
JSArray [driver, path] -> do |
359 |
driver' <- readJSON driver |
360 |
path' <- readJSON path |
361 |
return $ LIDBlockDev driver' path' |
362 |
_ -> fail "Can't read logical_id for blockdev type" |
363 |
DTRbd -> |
364 |
case lid of |
365 |
JSArray [driver, path] -> do |
366 |
driver' <- readJSON driver |
367 |
path' <- readJSON path |
368 |
return $ LIDRados driver' path' |
369 |
_ -> fail "Can't read logical_id for rdb type" |
370 |
DTExt -> |
371 |
case lid of |
372 |
JSArray [extprovider, name] -> do |
373 |
extprovider' <- readJSON extprovider |
374 |
name' <- readJSON name |
375 |
return $ LIDExt extprovider' name' |
376 |
_ -> fail "Can't read logical_id for extstorage type" |
377 |
DTDiskless -> |
378 |
fail "Retrieved 'diskless' disk." |
379 |
|
380 |
-- | Disk data structure. |
381 |
-- |
382 |
-- This is declared manually as it's a recursive structure, and our TH |
383 |
-- code currently can't build it. |
384 |
data Disk = Disk |
385 |
{ diskLogicalId :: DiskLogicalId |
386 |
, diskChildren :: [Disk] |
387 |
, diskIvName :: String |
388 |
, diskSize :: Int |
389 |
, diskMode :: DiskMode |
390 |
, diskName :: Maybe String |
391 |
, diskSpindles :: Maybe Int |
392 |
, diskUuid :: String |
393 |
} deriving (Show, Eq) |
394 |
|
395 |
$(buildObjectSerialisation "Disk" $ |
396 |
[ customField 'decodeDLId 'encodeFullDLId ["dev_type"] $ |
397 |
simpleField "logical_id" [t| DiskLogicalId |] |
398 |
, defaultField [| [] |] $ simpleField "children" [t| [Disk] |] |
399 |
, defaultField [| "" |] $ simpleField "iv_name" [t| String |] |
400 |
, simpleField "size" [t| Int |] |
401 |
, defaultField [| DiskRdWr |] $ simpleField "mode" [t| DiskMode |] |
402 |
, optionalField $ simpleField "name" [t| String |] |
403 |
, optionalField $ simpleField "spindles" [t| Int |] |
404 |
] |
405 |
++ uuidFields) |
406 |
|
407 |
instance UuidObject Disk where |
408 |
uuidOf = diskUuid |
409 |
|
410 |
-- | Determines whether a disk or one of his children has the given logical id |
411 |
-- (determined by the volume group name and by the logical volume name). |
412 |
-- This can be true only for DRBD or LVM disks. |
413 |
includesLogicalId :: String -> String -> Disk -> Bool |
414 |
includesLogicalId vg_name lv_name disk = |
415 |
case diskLogicalId disk of |
416 |
LIDPlain vg lv -> vg_name == vg && lv_name == lv |
417 |
LIDDrbd8 {} -> |
418 |
any (includesLogicalId vg_name lv_name) $ diskChildren disk |
419 |
_ -> False |
420 |
|
421 |
-- * Instance definitions |
422 |
|
423 |
$(buildParam "Be" "bep" |
424 |
[ simpleField "minmem" [t| Int |] |
425 |
, simpleField "maxmem" [t| Int |] |
426 |
, simpleField "vcpus" [t| Int |] |
427 |
, simpleField "auto_balance" [t| Bool |] |
428 |
, simpleField "always_failover" [t| Bool |] |
429 |
, simpleField "spindle_use" [t| Int |] |
430 |
]) |
431 |
|
432 |
$(buildObject "Instance" "inst" $ |
433 |
[ simpleField "name" [t| String |] |
434 |
, simpleField "primary_node" [t| String |] |
435 |
, simpleField "os" [t| String |] |
436 |
, simpleField "hypervisor" [t| Hypervisor |] |
437 |
, simpleField "hvparams" [t| HvParams |] |
438 |
, simpleField "beparams" [t| PartialBeParams |] |
439 |
, simpleField "osparams" [t| OsParams |] |
440 |
, simpleField "admin_state" [t| AdminState |] |
441 |
, simpleField "nics" [t| [PartialNic] |] |
442 |
, simpleField "disks" [t| [Disk] |] |
443 |
, simpleField "disk_template" [t| DiskTemplate |] |
444 |
, simpleField "disks_active" [t| Bool |] |
445 |
, optionalField $ simpleField "network_port" [t| Int |] |
446 |
] |
447 |
++ timeStampFields |
448 |
++ uuidFields |
449 |
++ serialFields |
450 |
++ tagsFields) |
451 |
|
452 |
instance TimeStampObject Instance where |
453 |
cTimeOf = instCtime |
454 |
mTimeOf = instMtime |
455 |
|
456 |
instance UuidObject Instance where |
457 |
uuidOf = instUuid |
458 |
|
459 |
instance SerialNoObject Instance where |
460 |
serialOf = instSerial |
461 |
|
462 |
instance TagsObject Instance where |
463 |
tagsOf = instTags |
464 |
|
465 |
-- | Retrieves the real disk size requirements for all the disks of the |
466 |
-- instance. This includes the metadata etc. and is different from the values |
467 |
-- visible to the instance. |
468 |
getDiskSizeRequirements :: Instance -> Int |
469 |
getDiskSizeRequirements inst = |
470 |
sum . map |
471 |
(\disk -> case instDiskTemplate inst of |
472 |
DTDrbd8 -> diskSize disk + C.drbdMetaSize |
473 |
DTDiskless -> 0 |
474 |
DTBlock -> 0 |
475 |
_ -> diskSize disk ) |
476 |
$ instDisks inst |
477 |
|
478 |
-- * IPolicy definitions |
479 |
|
480 |
$(buildParam "ISpec" "ispec" |
481 |
[ simpleField ConstantUtils.ispecMemSize [t| Int |] |
482 |
, simpleField ConstantUtils.ispecDiskSize [t| Int |] |
483 |
, simpleField ConstantUtils.ispecDiskCount [t| Int |] |
484 |
, simpleField ConstantUtils.ispecCpuCount [t| Int |] |
485 |
, simpleField ConstantUtils.ispecNicCount [t| Int |] |
486 |
, simpleField ConstantUtils.ispecSpindleUse [t| Int |] |
487 |
]) |
488 |
|
489 |
$(buildObject "MinMaxISpecs" "mmis" |
490 |
[ renameField "MinSpec" $ simpleField "min" [t| FilledISpecParams |] |
491 |
, renameField "MaxSpec" $ simpleField "max" [t| FilledISpecParams |] |
492 |
]) |
493 |
|
494 |
-- | Custom partial ipolicy. This is not built via buildParam since it |
495 |
-- has a special 2-level inheritance mode. |
496 |
$(buildObject "PartialIPolicy" "ipolicy" |
497 |
[ optionalField . renameField "MinMaxISpecsP" $ |
498 |
simpleField ConstantUtils.ispecsMinmax [t| [MinMaxISpecs] |] |
499 |
, optionalField . renameField "StdSpecP" $ |
500 |
simpleField "std" [t| PartialISpecParams |] |
501 |
, optionalField . renameField "SpindleRatioP" $ |
502 |
simpleField "spindle-ratio" [t| Double |] |
503 |
, optionalField . renameField "VcpuRatioP" $ |
504 |
simpleField "vcpu-ratio" [t| Double |] |
505 |
, optionalField . renameField "DiskTemplatesP" $ |
506 |
simpleField "disk-templates" [t| [DiskTemplate] |] |
507 |
]) |
508 |
|
509 |
-- | Custom filled ipolicy. This is not built via buildParam since it |
510 |
-- has a special 2-level inheritance mode. |
511 |
$(buildObject "FilledIPolicy" "ipolicy" |
512 |
[ renameField "MinMaxISpecs" $ |
513 |
simpleField ConstantUtils.ispecsMinmax [t| [MinMaxISpecs] |] |
514 |
, renameField "StdSpec" $ simpleField "std" [t| FilledISpecParams |] |
515 |
, simpleField "spindle-ratio" [t| Double |] |
516 |
, simpleField "vcpu-ratio" [t| Double |] |
517 |
, simpleField "disk-templates" [t| [DiskTemplate] |] |
518 |
]) |
519 |
|
520 |
-- | Custom filler for the ipolicy types. |
521 |
fillIPolicy :: FilledIPolicy -> PartialIPolicy -> FilledIPolicy |
522 |
fillIPolicy (FilledIPolicy { ipolicyMinMaxISpecs = fminmax |
523 |
, ipolicyStdSpec = fstd |
524 |
, ipolicySpindleRatio = fspindleRatio |
525 |
, ipolicyVcpuRatio = fvcpuRatio |
526 |
, ipolicyDiskTemplates = fdiskTemplates}) |
527 |
(PartialIPolicy { ipolicyMinMaxISpecsP = pminmax |
528 |
, ipolicyStdSpecP = pstd |
529 |
, ipolicySpindleRatioP = pspindleRatio |
530 |
, ipolicyVcpuRatioP = pvcpuRatio |
531 |
, ipolicyDiskTemplatesP = pdiskTemplates}) = |
532 |
FilledIPolicy { ipolicyMinMaxISpecs = fromMaybe fminmax pminmax |
533 |
, ipolicyStdSpec = case pstd of |
534 |
Nothing -> fstd |
535 |
Just p -> fillISpecParams fstd p |
536 |
, ipolicySpindleRatio = fromMaybe fspindleRatio pspindleRatio |
537 |
, ipolicyVcpuRatio = fromMaybe fvcpuRatio pvcpuRatio |
538 |
, ipolicyDiskTemplates = fromMaybe fdiskTemplates |
539 |
pdiskTemplates |
540 |
} |
541 |
-- * Node definitions |
542 |
|
543 |
$(buildParam "ND" "ndp" |
544 |
[ simpleField "oob_program" [t| String |] |
545 |
, simpleField "spindle_count" [t| Int |] |
546 |
, simpleField "exclusive_storage" [t| Bool |] |
547 |
, simpleField "ovs" [t| Bool |] |
548 |
, simpleField "ovs_name" [t| String |] |
549 |
, simpleField "ovs_link" [t| String |] |
550 |
, simpleField "ssh_port" [t| Int |] |
551 |
]) |
552 |
|
553 |
$(buildObject "Node" "node" $ |
554 |
[ simpleField "name" [t| String |] |
555 |
, simpleField "primary_ip" [t| String |] |
556 |
, simpleField "secondary_ip" [t| String |] |
557 |
, simpleField "master_candidate" [t| Bool |] |
558 |
, simpleField "offline" [t| Bool |] |
559 |
, simpleField "drained" [t| Bool |] |
560 |
, simpleField "group" [t| String |] |
561 |
, simpleField "master_capable" [t| Bool |] |
562 |
, simpleField "vm_capable" [t| Bool |] |
563 |
, simpleField "ndparams" [t| PartialNDParams |] |
564 |
, simpleField "powered" [t| Bool |] |
565 |
] |
566 |
++ timeStampFields |
567 |
++ uuidFields |
568 |
++ serialFields |
569 |
++ tagsFields) |
570 |
|
571 |
instance TimeStampObject Node where |
572 |
cTimeOf = nodeCtime |
573 |
mTimeOf = nodeMtime |
574 |
|
575 |
instance UuidObject Node where |
576 |
uuidOf = nodeUuid |
577 |
|
578 |
instance SerialNoObject Node where |
579 |
serialOf = nodeSerial |
580 |
|
581 |
instance TagsObject Node where |
582 |
tagsOf = nodeTags |
583 |
|
584 |
-- * NodeGroup definitions |
585 |
|
586 |
-- | The disk parameters type. |
587 |
type DiskParams = Container (Container JSValue) |
588 |
|
589 |
-- | A mapping from network UUIDs to nic params of the networks. |
590 |
type Networks = Container PartialNicParams |
591 |
|
592 |
$(buildObject "NodeGroup" "group" $ |
593 |
[ simpleField "name" [t| String |] |
594 |
, defaultField [| [] |] $ simpleField "members" [t| [String] |] |
595 |
, simpleField "ndparams" [t| PartialNDParams |] |
596 |
, simpleField "alloc_policy" [t| AllocPolicy |] |
597 |
, simpleField "ipolicy" [t| PartialIPolicy |] |
598 |
, simpleField "diskparams" [t| DiskParams |] |
599 |
, simpleField "networks" [t| Networks |] |
600 |
] |
601 |
++ timeStampFields |
602 |
++ uuidFields |
603 |
++ serialFields |
604 |
++ tagsFields) |
605 |
|
606 |
instance TimeStampObject NodeGroup where |
607 |
cTimeOf = groupCtime |
608 |
mTimeOf = groupMtime |
609 |
|
610 |
instance UuidObject NodeGroup where |
611 |
uuidOf = groupUuid |
612 |
|
613 |
instance SerialNoObject NodeGroup where |
614 |
serialOf = groupSerial |
615 |
|
616 |
instance TagsObject NodeGroup where |
617 |
tagsOf = groupTags |
618 |
|
619 |
-- | IP family type |
620 |
$(declareIADT "IpFamily" |
621 |
[ ("IpFamilyV4", 'AutoConf.pyAfInet4) |
622 |
, ("IpFamilyV6", 'AutoConf.pyAfInet6) |
623 |
]) |
624 |
$(makeJSONInstance ''IpFamily) |
625 |
|
626 |
-- | Conversion from IP family to IP version. This is needed because |
627 |
-- Python uses both, depending on context. |
628 |
ipFamilyToVersion :: IpFamily -> Int |
629 |
ipFamilyToVersion IpFamilyV4 = C.ip4Version |
630 |
ipFamilyToVersion IpFamilyV6 = C.ip6Version |
631 |
|
632 |
-- | Cluster HvParams (hvtype to hvparams mapping). |
633 |
type ClusterHvParams = Container HvParams |
634 |
|
635 |
-- | Cluster Os-HvParams (os to hvparams mapping). |
636 |
type OsHvParams = Container ClusterHvParams |
637 |
|
638 |
-- | Cluser BeParams. |
639 |
type ClusterBeParams = Container FilledBeParams |
640 |
|
641 |
-- | Cluster OsParams. |
642 |
type ClusterOsParams = Container OsParams |
643 |
|
644 |
-- | Cluster NicParams. |
645 |
type ClusterNicParams = Container FilledNicParams |
646 |
|
647 |
-- | Cluster UID Pool, list (low, high) UID ranges. |
648 |
type UidPool = [(Int, Int)] |
649 |
|
650 |
-- | The iallocator parameters type. |
651 |
type IAllocatorParams = Container JSValue |
652 |
|
653 |
-- * Cluster definitions |
654 |
$(buildObject "Cluster" "cluster" $ |
655 |
[ simpleField "rsahostkeypub" [t| String |] |
656 |
, optionalField $ |
657 |
simpleField "dsahostkeypub" [t| String |] |
658 |
, simpleField "highest_used_port" [t| Int |] |
659 |
, simpleField "tcpudp_port_pool" [t| [Int] |] |
660 |
, simpleField "mac_prefix" [t| String |] |
661 |
, optionalField $ |
662 |
simpleField "volume_group_name" [t| String |] |
663 |
, simpleField "reserved_lvs" [t| [String] |] |
664 |
, optionalField $ |
665 |
simpleField "drbd_usermode_helper" [t| String |] |
666 |
, simpleField "master_node" [t| String |] |
667 |
, simpleField "master_ip" [t| String |] |
668 |
, simpleField "master_netdev" [t| String |] |
669 |
, simpleField "master_netmask" [t| Int |] |
670 |
, simpleField "use_external_mip_script" [t| Bool |] |
671 |
, simpleField "cluster_name" [t| String |] |
672 |
, simpleField "file_storage_dir" [t| String |] |
673 |
, simpleField "shared_file_storage_dir" [t| String |] |
674 |
, simpleField "enabled_hypervisors" [t| [Hypervisor] |] |
675 |
, simpleField "hvparams" [t| ClusterHvParams |] |
676 |
, simpleField "os_hvp" [t| OsHvParams |] |
677 |
, simpleField "beparams" [t| ClusterBeParams |] |
678 |
, simpleField "osparams" [t| ClusterOsParams |] |
679 |
, simpleField "nicparams" [t| ClusterNicParams |] |
680 |
, simpleField "ndparams" [t| FilledNDParams |] |
681 |
, simpleField "diskparams" [t| DiskParams |] |
682 |
, simpleField "candidate_pool_size" [t| Int |] |
683 |
, simpleField "modify_etc_hosts" [t| Bool |] |
684 |
, simpleField "modify_ssh_setup" [t| Bool |] |
685 |
, simpleField "maintain_node_health" [t| Bool |] |
686 |
, simpleField "uid_pool" [t| UidPool |] |
687 |
, simpleField "default_iallocator" [t| String |] |
688 |
, simpleField "default_iallocator_params" [t| IAllocatorParams |] |
689 |
, simpleField "hidden_os" [t| [String] |] |
690 |
, simpleField "blacklisted_os" [t| [String] |] |
691 |
, simpleField "primary_ip_family" [t| IpFamily |] |
692 |
, simpleField "prealloc_wipe_disks" [t| Bool |] |
693 |
, simpleField "ipolicy" [t| FilledIPolicy |] |
694 |
, simpleField "enabled_disk_templates" [t| [DiskTemplate] |] |
695 |
] |
696 |
++ timeStampFields |
697 |
++ uuidFields |
698 |
++ serialFields |
699 |
++ tagsFields) |
700 |
|
701 |
instance TimeStampObject Cluster where |
702 |
cTimeOf = clusterCtime |
703 |
mTimeOf = clusterMtime |
704 |
|
705 |
instance UuidObject Cluster where |
706 |
uuidOf = clusterUuid |
707 |
|
708 |
instance SerialNoObject Cluster where |
709 |
serialOf = clusterSerial |
710 |
|
711 |
instance TagsObject Cluster where |
712 |
tagsOf = clusterTags |
713 |
|
714 |
-- * ConfigData definitions |
715 |
|
716 |
$(buildObject "ConfigData" "config" $ |
717 |
-- timeStampFields ++ |
718 |
[ simpleField "version" [t| Int |] |
719 |
, simpleField "cluster" [t| Cluster |] |
720 |
, simpleField "nodes" [t| Container Node |] |
721 |
, simpleField "nodegroups" [t| Container NodeGroup |] |
722 |
, simpleField "instances" [t| Container Instance |] |
723 |
, simpleField "networks" [t| Container Network |] |
724 |
] |
725 |
++ serialFields) |
726 |
|
727 |
instance SerialNoObject ConfigData where |
728 |
serialOf = configSerial |