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