1 {-# LANGUAGE TemplateHaskell #-}
3 {-| Implementation of the Ganeti config objects.
5 Some object fields are not implemented yet, and as such they are
12 Copyright (C) 2011, 2012 Google Inc.
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.
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.
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
36 , PartialNicParams(..)
65 , FilledISpecParams(..)
66 , PartialISpecParams(..)
88 , DictObject(..) -- re-exported from THH
89 , TagSet -- re-exported from THH
93 import Data.List (foldl')
95 import qualified Data.Map as Map
96 import qualified Data.Set as Set
97 import Text.JSON (showJSON, readJSON, JSON, JSValue(..))
98 import qualified Text.JSON as J
100 import qualified Ganeti.Constants as C
105 -- * Generic definitions
107 -- | Fills one map with keys from the other map, if not already
108 -- existing. Mirrors objects.py:FillDict.
109 fillDict :: (Ord k) => Map.Map k v -> Map.Map k v -> [k] -> Map.Map k v
110 fillDict defaults custom skip_keys =
111 let updated = Map.union custom defaults
112 in foldl' (flip Map.delete) updated skip_keys
114 -- | The VTYPES, a mini-type system in Python.
115 $(declareSADT "VType"
116 [ ("VTypeString", 'C.vtypeString)
117 , ("VTypeMaybeString", 'C.vtypeMaybeString)
118 , ("VTypeBool", 'C.vtypeBool)
119 , ("VTypeSize", 'C.vtypeSize)
120 , ("VTypeInt", 'C.vtypeInt)
122 $(makeJSONInstance ''VType)
124 -- | The hypervisor parameter type. This is currently a simple map,
125 -- without type checking on key/value pairs.
126 type HvParams = Container JSValue
128 -- | The OS parameters type. This is, and will remain, a string
129 -- container, since the keys are dynamically declared by the OSes, and
130 -- the values are always strings.
131 type OsParams = Container String
133 -- | Class of objects that have timestamps.
134 class TimeStampObject a where
135 cTimeOf :: a -> Double
136 mTimeOf :: a -> Double
138 -- | Class of objects that have an UUID.
139 class UuidObject a where
140 uuidOf :: a -> String
142 -- | Class of object that have a serial number.
143 class SerialNoObject a where
146 -- | Class of objects that have tags.
147 class TagsObject a where
148 tagsOf :: a -> Set.Set String
150 -- * Node role object
152 $(declareSADT "NodeRole"
153 [ ("NROffline", 'C.nrOffline)
154 , ("NRDrained", 'C.nrDrained)
155 , ("NRRegular", 'C.nrRegular)
156 , ("NRCandidate", 'C.nrMcandidate)
157 , ("NRMaster", 'C.nrMaster)
159 $(makeJSONInstance ''NodeRole)
161 -- | The description of the node role.
162 roleDescription :: NodeRole -> String
163 roleDescription NROffline = "offline"
164 roleDescription NRDrained = "drained"
165 roleDescription NRRegular = "regular"
166 roleDescription NRCandidate = "master candidate"
167 roleDescription NRMaster = "master"
171 $(buildParam "Nic" "nicp"
172 [ simpleField "mode" [t| NICMode |]
173 , simpleField "link" [t| String |]
176 $(buildObject "PartialNic" "nic"
177 [ simpleField "mac" [t| String |]
178 , optionalField $ simpleField "ip" [t| String |]
179 , simpleField "nicparams" [t| PartialNicParams |]
182 -- * Disk definitions
184 $(declareSADT "DiskMode"
185 [ ("DiskRdOnly", 'C.diskRdonly)
186 , ("DiskRdWr", 'C.diskRdwr)
188 $(makeJSONInstance ''DiskMode)
190 $(declareSADT "DiskType"
192 , ("LD_DRBD8", 'C.ldDrbd8)
193 , ("LD_FILE", 'C.ldFile)
194 , ("LD_BLOCKDEV", 'C.ldBlockdev)
195 , ("LD_RADOS", 'C.ldRbd)
197 $(makeJSONInstance ''DiskType)
199 -- | The persistent block driver type. Currently only one type is allowed.
200 $(declareSADT "BlockDriver"
201 [ ("BlockDrvManual", 'C.blockdevDriverManual)
203 $(makeJSONInstance ''BlockDriver)
205 -- | Constant for the dev_type key entry in the disk config.
209 -- | The disk configuration type. This includes the disk type itself,
210 -- for a more complete consistency. Note that since in the Python
211 -- code-base there's no authoritative place where we document the
212 -- logical id, this is probably a good reference point.
214 = LIDPlain String String -- ^ Volume group, logical volume
215 | LIDDrbd8 String String Int Int Int String
216 -- ^ NodeA, NodeB, Port, MinorA, MinorB, Secret
217 | LIDFile FileDriver String -- ^ Driver, path
218 | LIDBlockDev BlockDriver String -- ^ Driver, path (must be under /dev)
219 | LIDRados String String -- ^ Unused, path
222 -- | Mapping from a logical id to a disk type.
223 lidDiskType :: DiskLogicalId -> DiskType
224 lidDiskType (LIDPlain {}) = LD_LV
225 lidDiskType (LIDDrbd8 {}) = LD_DRBD8
226 lidDiskType (LIDFile {}) = LD_FILE
227 lidDiskType (LIDBlockDev {}) = LD_BLOCKDEV
228 lidDiskType (LIDRados {}) = LD_RADOS
230 -- | Builds the extra disk_type field for a given logical id.
231 lidEncodeType :: DiskLogicalId -> [(String, JSValue)]
232 lidEncodeType v = [(devType, showJSON . lidDiskType $ v)]
234 -- | Custom encoder for DiskLogicalId (logical id only).
235 encodeDLId :: DiskLogicalId -> JSValue
236 encodeDLId (LIDPlain vg lv) = JSArray [showJSON vg, showJSON lv]
237 encodeDLId (LIDDrbd8 nodeA nodeB port minorA minorB key) =
238 JSArray [ showJSON nodeA, showJSON nodeB, showJSON port
239 , showJSON minorA, showJSON minorB, showJSON key ]
240 encodeDLId (LIDRados pool name) = JSArray [showJSON pool, showJSON name]
241 encodeDLId (LIDFile driver name) = JSArray [showJSON driver, showJSON name]
242 encodeDLId (LIDBlockDev driver name) = JSArray [showJSON driver, showJSON name]
244 -- | Custom encoder for DiskLogicalId, composing both the logical id
245 -- and the extra disk_type field.
246 encodeFullDLId :: DiskLogicalId -> (JSValue, [(String, JSValue)])
247 encodeFullDLId v = (encodeDLId v, lidEncodeType v)
249 -- | Custom decoder for DiskLogicalId. This is manual for now, since
250 -- we don't have yet automation for separate-key style fields.
251 decodeDLId :: [(String, JSValue)] -> JSValue -> J.Result DiskLogicalId
252 decodeDLId obj lid = do
253 dtype <- fromObj obj devType
257 JSArray [nA, nB, p, mA, mB, k] -> do
264 return $ LIDDrbd8 nA' nB' p' mA' mB' k'
265 _ -> fail "Can't read logical_id for DRBD8 type"
268 JSArray [vg, lv] -> do
271 return $ LIDPlain vg' lv'
272 _ -> fail "Can't read logical_id for plain type"
275 JSArray [driver, path] -> do
276 driver' <- readJSON driver
277 path' <- readJSON path
278 return $ LIDFile driver' path'
279 _ -> fail "Can't read logical_id for file type"
282 JSArray [driver, path] -> do
283 driver' <- readJSON driver
284 path' <- readJSON path
285 return $ LIDBlockDev driver' path'
286 _ -> fail "Can't read logical_id for blockdev type"
289 JSArray [driver, path] -> do
290 driver' <- readJSON driver
291 path' <- readJSON path
292 return $ LIDRados driver' path'
293 _ -> fail "Can't read logical_id for rdb type"
295 -- | Disk data structure.
297 -- This is declared manually as it's a recursive structure, and our TH
298 -- code currently can't build it.
300 { diskLogicalId :: DiskLogicalId
301 -- , diskPhysicalId :: String
302 , diskChildren :: [Disk]
303 , diskIvName :: String
305 , diskMode :: DiskMode
306 } deriving (Show, Eq)
308 $(buildObjectSerialisation "Disk"
309 [ customField 'decodeDLId 'encodeFullDLId ["dev_type"] $
310 simpleField "logical_id" [t| DiskLogicalId |]
311 -- , simpleField "physical_id" [t| String |]
312 , defaultField [| [] |] $ simpleField "children" [t| [Disk] |]
313 , defaultField [| "" |] $ simpleField "iv_name" [t| String |]
314 , simpleField "size" [t| Int |]
315 , defaultField [| DiskRdWr |] $ simpleField "mode" [t| DiskMode |]
318 -- * Instance definitions
320 $(declareSADT "AdminState"
321 [ ("AdminOffline", 'C.adminstOffline)
322 , ("AdminDown", 'C.adminstDown)
323 , ("AdminUp", 'C.adminstUp)
325 $(makeJSONInstance ''AdminState)
327 $(buildParam "Be" "bep"
328 [ simpleField "minmem" [t| Int |]
329 , simpleField "maxmem" [t| Int |]
330 , simpleField "vcpus" [t| Int |]
331 , simpleField "auto_balance" [t| Bool |]
334 $(buildObject "Instance" "inst" $
335 [ simpleField "name" [t| String |]
336 , simpleField "primary_node" [t| String |]
337 , simpleField "os" [t| String |]
338 , simpleField "hypervisor" [t| Hypervisor |]
339 , simpleField "hvparams" [t| HvParams |]
340 , simpleField "beparams" [t| PartialBeParams |]
341 , simpleField "osparams" [t| OsParams |]
342 , simpleField "admin_state" [t| AdminState |]
343 , simpleField "nics" [t| [PartialNic] |]
344 , simpleField "disks" [t| [Disk] |]
345 , simpleField "disk_template" [t| DiskTemplate |]
346 , optionalField $ simpleField "network_port" [t| Int |]
353 instance TimeStampObject Instance where
357 instance UuidObject Instance where
360 instance SerialNoObject Instance where
361 serialOf = instSerial
363 instance TagsObject Instance where
366 -- * IPolicy definitions
368 $(buildParam "ISpec" "ispec"
369 [ simpleField C.ispecMemSize [t| Int |]
370 , simpleField C.ispecDiskSize [t| Int |]
371 , simpleField C.ispecDiskCount [t| Int |]
372 , simpleField C.ispecCpuCount [t| Int |]
373 , simpleField C.ispecNicCount [t| Int |]
374 , simpleField C.ispecSpindleUse [t| Int |]
377 -- | Custom partial ipolicy. This is not built via buildParam since it
378 -- has a special 2-level inheritance mode.
379 $(buildObject "PartialIPolicy" "ipolicy"
380 [ renameField "MinSpecP" $ simpleField "min" [t| PartialISpecParams |]
381 , renameField "MaxSpecP" $ simpleField "max" [t| PartialISpecParams |]
382 , renameField "StdSpecP" $ simpleField "std" [t| PartialISpecParams |]
383 , optionalField . renameField "SpindleRatioP"
384 $ simpleField "spindle-ratio" [t| Double |]
385 , optionalField . renameField "VcpuRatioP"
386 $ simpleField "vcpu-ratio" [t| Double |]
387 , optionalField . renameField "DiskTemplatesP"
388 $ simpleField "disk-templates" [t| [DiskTemplate] |]
391 -- | Custom filled ipolicy. This is not built via buildParam since it
392 -- has a special 2-level inheritance mode.
393 $(buildObject "FilledIPolicy" "ipolicy"
394 [ renameField "MinSpec" $ simpleField "min" [t| FilledISpecParams |]
395 , renameField "MaxSpec" $ simpleField "max" [t| FilledISpecParams |]
396 , renameField "StdSpec" $ simpleField "std" [t| FilledISpecParams |]
397 , simpleField "spindle-ratio" [t| Double |]
398 , simpleField "vcpu-ratio" [t| Double |]
399 , simpleField "disk-templates" [t| [DiskTemplate] |]
402 -- | Custom filler for the ipolicy types.
403 fillIPolicy :: FilledIPolicy -> PartialIPolicy -> FilledIPolicy
404 fillIPolicy (FilledIPolicy { ipolicyMinSpec = fmin
405 , ipolicyMaxSpec = fmax
406 , ipolicyStdSpec = fstd
407 , ipolicySpindleRatio = fspindleRatio
408 , ipolicyVcpuRatio = fvcpuRatio
409 , ipolicyDiskTemplates = fdiskTemplates})
410 (PartialIPolicy { ipolicyMinSpecP = pmin
411 , ipolicyMaxSpecP = pmax
412 , ipolicyStdSpecP = pstd
413 , ipolicySpindleRatioP = pspindleRatio
414 , ipolicyVcpuRatioP = pvcpuRatio
415 , ipolicyDiskTemplatesP = pdiskTemplates}) =
416 FilledIPolicy { ipolicyMinSpec = fillISpecParams fmin pmin
417 , ipolicyMaxSpec = fillISpecParams fmax pmax
418 , ipolicyStdSpec = fillISpecParams fstd pstd
419 , ipolicySpindleRatio = fromMaybe fspindleRatio pspindleRatio
420 , ipolicyVcpuRatio = fromMaybe fvcpuRatio pvcpuRatio
421 , ipolicyDiskTemplates = fromMaybe fdiskTemplates
424 -- * Node definitions
426 $(buildParam "ND" "ndp"
427 [ simpleField "oob_program" [t| String |]
428 , simpleField "spindle_count" [t| Int |]
431 $(buildObject "Node" "node" $
432 [ simpleField "name" [t| String |]
433 , simpleField "primary_ip" [t| String |]
434 , simpleField "secondary_ip" [t| String |]
435 , simpleField "master_candidate" [t| Bool |]
436 , simpleField "offline" [t| Bool |]
437 , simpleField "drained" [t| Bool |]
438 , simpleField "group" [t| String |]
439 , simpleField "master_capable" [t| Bool |]
440 , simpleField "vm_capable" [t| Bool |]
441 , simpleField "ndparams" [t| PartialNDParams |]
442 , simpleField "powered" [t| Bool |]
449 instance TimeStampObject Node where
453 instance UuidObject Node where
456 instance SerialNoObject Node where
457 serialOf = nodeSerial
459 instance TagsObject Node where
462 -- * NodeGroup definitions
464 -- | The disk parameters type.
465 type DiskParams = Container (Container JSValue)
467 $(buildObject "NodeGroup" "group" $
468 [ simpleField "name" [t| String |]
469 , defaultField [| [] |] $ simpleField "members" [t| [String] |]
470 , simpleField "ndparams" [t| PartialNDParams |]
471 , simpleField "alloc_policy" [t| AllocPolicy |]
472 , simpleField "ipolicy" [t| PartialIPolicy |]
473 , simpleField "diskparams" [t| DiskParams |]
480 instance TimeStampObject NodeGroup where
484 instance UuidObject NodeGroup where
487 instance SerialNoObject NodeGroup where
488 serialOf = groupSerial
490 instance TagsObject NodeGroup where
494 $(declareIADT "IpFamily"
495 [ ("IpFamilyV4", 'C.ip4Family)
496 , ("IpFamilyV6", 'C.ip6Family)
498 $(makeJSONInstance ''IpFamily)
500 -- | Conversion from IP family to IP version. This is needed because
501 -- Python uses both, depending on context.
502 ipFamilyToVersion :: IpFamily -> Int
503 ipFamilyToVersion IpFamilyV4 = C.ip4Version
504 ipFamilyToVersion IpFamilyV6 = C.ip6Version
506 -- | Cluster HvParams (hvtype to hvparams mapping).
507 type ClusterHvParams = Container HvParams
509 -- | Cluster Os-HvParams (os to hvparams mapping).
510 type OsHvParams = Container ClusterHvParams
512 -- | Cluser BeParams.
513 type ClusterBeParams = Container FilledBeParams
515 -- | Cluster OsParams.
516 type ClusterOsParams = Container OsParams
518 -- | Cluster NicParams.
519 type ClusterNicParams = Container FilledNicParams
521 -- | Cluster UID Pool, list (low, high) UID ranges.
522 type UidPool = [(Int, Int)]
524 -- * Cluster definitions
525 $(buildObject "Cluster" "cluster" $
526 [ simpleField "rsahostkeypub" [t| String |]
527 , simpleField "highest_used_port" [t| Int |]
528 , simpleField "tcpudp_port_pool" [t| [Int] |]
529 , simpleField "mac_prefix" [t| String |]
530 , simpleField "volume_group_name" [t| String |]
531 , simpleField "reserved_lvs" [t| [String] |]
533 simpleField "drbd_usermode_helper" [t| String |]
534 , simpleField "master_node" [t| String |]
535 , simpleField "master_ip" [t| String |]
536 , simpleField "master_netdev" [t| String |]
537 , simpleField "master_netmask" [t| Int |]
538 , simpleField "use_external_mip_script" [t| Bool |]
539 , simpleField "cluster_name" [t| String |]
540 , simpleField "file_storage_dir" [t| String |]
541 , simpleField "shared_file_storage_dir" [t| String |]
542 , simpleField "enabled_hypervisors" [t| [Hypervisor] |]
543 , simpleField "hvparams" [t| ClusterHvParams |]
544 , simpleField "os_hvp" [t| OsHvParams |]
545 , simpleField "beparams" [t| ClusterBeParams |]
546 , simpleField "osparams" [t| ClusterOsParams |]
547 , simpleField "nicparams" [t| ClusterNicParams |]
548 , simpleField "ndparams" [t| FilledNDParams |]
549 , simpleField "diskparams" [t| DiskParams |]
550 , simpleField "candidate_pool_size" [t| Int |]
551 , simpleField "modify_etc_hosts" [t| Bool |]
552 , simpleField "modify_ssh_setup" [t| Bool |]
553 , simpleField "maintain_node_health" [t| Bool |]
554 , simpleField "uid_pool" [t| UidPool |]
555 , simpleField "default_iallocator" [t| String |]
556 , simpleField "hidden_os" [t| [String] |]
557 , simpleField "blacklisted_os" [t| [String] |]
558 , simpleField "primary_ip_family" [t| IpFamily |]
559 , simpleField "prealloc_wipe_disks" [t| Bool |]
560 , simpleField "ipolicy" [t| FilledIPolicy |]
567 instance TimeStampObject Cluster where
568 cTimeOf = clusterCtime
569 mTimeOf = clusterMtime
571 instance UuidObject Cluster where
574 instance SerialNoObject Cluster where
575 serialOf = clusterSerial
577 instance TagsObject Cluster where
580 -- * ConfigData definitions
582 $(buildObject "ConfigData" "config" $
583 -- timeStampFields ++
584 [ simpleField "version" [t| Int |]
585 , simpleField "cluster" [t| Cluster |]
586 , simpleField "nodes" [t| Container Node |]
587 , simpleField "nodegroups" [t| Container NodeGroup |]
588 , simpleField "instances" [t| Container Instance |]
592 instance SerialNoObject ConfigData where
593 serialOf = configSerial
595 -- * Network definitions
597 -- FIXME: Not all types might be correct here, since they
598 -- haven't been exhaustively deduced from the python code yet.
599 $(buildObject "Network" "network" $
600 [ simpleField "name" [t| NonEmptyString |]
602 simpleField "network_type" [t| NetworkType |]
604 simpleField "mac_prefix" [t| String |]
606 simpleField "family" [t| Int |]
607 , simpleField "network" [t| NonEmptyString |]
609 simpleField "network6" [t| String |]
611 simpleField "gateway" [t| String |]
613 simpleField "gateway6" [t| String |]
615 simpleField "size" [t| J.JSValue |]
617 simpleField "reservations" [t| String |]
619 simpleField "ext_reservations" [t| String |]
624 instance SerialNoObject Network where
625 serialOf = networkSerial
627 instance TagsObject Network where