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
37 , PartialNicParams(..)
67 , FilledISpecParams(..)
68 , PartialISpecParams(..)
90 , DictObject(..) -- re-exported from THH
91 , TagSet -- re-exported from THH
94 import Data.List (foldl')
96 import qualified Data.Map as Map
97 import qualified Data.Set as Set
98 import Text.JSON (showJSON, readJSON, JSON, JSValue(..))
99 import qualified Text.JSON as J
101 import qualified Ganeti.Constants as C
106 -- * Generic definitions
108 -- | Fills one map with keys from the other map, if not already
109 -- existing. Mirrors objects.py:FillDict.
110 fillDict :: (Ord k) => Map.Map k v -> Map.Map k v -> [k] -> Map.Map k v
111 fillDict defaults custom skip_keys =
112 let updated = Map.union custom defaults
113 in foldl' (flip Map.delete) updated skip_keys
115 -- | The VTYPES, a mini-type system in Python.
116 $(declareSADT "VType"
117 [ ("VTypeString", 'C.vtypeString)
118 , ("VTypeMaybeString", 'C.vtypeMaybeString)
119 , ("VTypeBool", 'C.vtypeBool)
120 , ("VTypeSize", 'C.vtypeSize)
121 , ("VTypeInt", 'C.vtypeInt)
123 $(makeJSONInstance ''VType)
125 -- | The hypervisor parameter type. This is currently a simple map,
126 -- without type checking on key/value pairs.
127 type HvParams = Container JSValue
129 -- | The OS parameters type. This is, and will remain, a string
130 -- container, since the keys are dynamically declared by the OSes, and
131 -- the values are always strings.
132 type OsParams = Container String
134 -- | Class of objects that have timestamps.
135 class TimeStampObject a where
136 cTimeOf :: a -> Double
137 mTimeOf :: a -> Double
139 -- | Class of objects that have an UUID.
140 class UuidObject a where
141 uuidOf :: a -> String
143 -- | Class of object that have a serial number.
144 class SerialNoObject a where
147 -- | Class of objects that have tags.
148 class TagsObject a where
149 tagsOf :: a -> Set.Set String
151 -- * Node role object
153 $(declareSADT "NodeRole"
154 [ ("NROffline", 'C.nrOffline)
155 , ("NRDrained", 'C.nrDrained)
156 , ("NRRegular", 'C.nrRegular)
157 , ("NRCandidate", 'C.nrMcandidate)
158 , ("NRMaster", 'C.nrMaster)
160 $(makeJSONInstance ''NodeRole)
162 -- | The description of the node role.
163 roleDescription :: NodeRole -> String
164 roleDescription NROffline = "offline"
165 roleDescription NRDrained = "drained"
166 roleDescription NRRegular = "regular"
167 roleDescription NRCandidate = "master candidate"
168 roleDescription NRMaster = "master"
172 $(declareSADT "NICMode"
173 [ ("NMBridged", 'C.nicModeBridged)
174 , ("NMRouted", 'C.nicModeRouted)
176 $(makeJSONInstance ''NICMode)
178 $(buildParam "Nic" "nicp"
179 [ simpleField "mode" [t| NICMode |]
180 , simpleField "link" [t| String |]
183 $(buildObject "PartialNic" "nic"
184 [ simpleField "mac" [t| String |]
185 , optionalField $ simpleField "ip" [t| String |]
186 , simpleField "nicparams" [t| PartialNicParams |]
189 -- * Disk definitions
191 $(declareSADT "DiskMode"
192 [ ("DiskRdOnly", 'C.diskRdonly)
193 , ("DiskRdWr", 'C.diskRdwr)
195 $(makeJSONInstance ''DiskMode)
197 $(declareSADT "DiskType"
199 , ("LD_DRBD8", 'C.ldDrbd8)
200 , ("LD_FILE", 'C.ldFile)
201 , ("LD_BLOCKDEV", 'C.ldBlockdev)
202 , ("LD_RADOS", 'C.ldRbd)
204 $(makeJSONInstance ''DiskType)
206 -- | The file driver type.
207 $(declareSADT "FileDriver"
208 [ ("FileLoop", 'C.fdLoop)
209 , ("FileBlktap", 'C.fdBlktap)
211 $(makeJSONInstance ''FileDriver)
213 -- | The persistent block driver type. Currently only one type is allowed.
214 $(declareSADT "BlockDriver"
215 [ ("BlockDrvManual", 'C.blockdevDriverManual)
217 $(makeJSONInstance ''BlockDriver)
219 -- | Constant for the dev_type key entry in the disk config.
223 -- | The disk configuration type. This includes the disk type itself,
224 -- for a more complete consistency. Note that since in the Python
225 -- code-base there's no authoritative place where we document the
226 -- logical id, this is probably a good reference point.
228 = LIDPlain String String -- ^ Volume group, logical volume
229 | LIDDrbd8 String String Int Int Int String
230 -- ^ NodeA, NodeB, Port, MinorA, MinorB, Secret
231 | LIDFile FileDriver String -- ^ Driver, path
232 | LIDBlockDev BlockDriver String -- ^ Driver, path (must be under /dev)
233 | LIDRados String String -- ^ Unused, path
234 deriving (Read, Show, Eq)
236 -- | Mapping from a logical id to a disk type.
237 lidDiskType :: DiskLogicalId -> DiskType
238 lidDiskType (LIDPlain {}) = LD_LV
239 lidDiskType (LIDDrbd8 {}) = LD_DRBD8
240 lidDiskType (LIDFile {}) = LD_FILE
241 lidDiskType (LIDBlockDev {}) = LD_BLOCKDEV
242 lidDiskType (LIDRados {}) = LD_RADOS
244 -- | Builds the extra disk_type field for a given logical id.
245 lidEncodeType :: DiskLogicalId -> [(String, JSValue)]
246 lidEncodeType v = [(devType, showJSON . lidDiskType $ v)]
248 -- | Custom encoder for DiskLogicalId (logical id only).
249 encodeDLId :: DiskLogicalId -> JSValue
250 encodeDLId (LIDPlain vg lv) = JSArray [showJSON vg, showJSON lv]
251 encodeDLId (LIDDrbd8 nodeA nodeB port minorA minorB key) =
252 JSArray [ showJSON nodeA, showJSON nodeB, showJSON port
253 , showJSON minorA, showJSON minorB, showJSON key ]
254 encodeDLId (LIDRados pool name) = JSArray [showJSON pool, showJSON name]
255 encodeDLId (LIDFile driver name) = JSArray [showJSON driver, showJSON name]
256 encodeDLId (LIDBlockDev driver name) = JSArray [showJSON driver, showJSON name]
258 -- | Custom encoder for DiskLogicalId, composing both the logical id
259 -- and the extra disk_type field.
260 encodeFullDLId :: DiskLogicalId -> (JSValue, [(String, JSValue)])
261 encodeFullDLId v = (encodeDLId v, lidEncodeType v)
263 -- | Custom decoder for DiskLogicalId. This is manual for now, since
264 -- we don't have yet automation for separate-key style fields.
265 decodeDLId :: [(String, JSValue)] -> JSValue -> J.Result DiskLogicalId
266 decodeDLId obj lid = do
267 dtype <- fromObj obj devType
271 JSArray [nA, nB, p, mA, mB, k] -> do
278 return $ LIDDrbd8 nA' nB' p' mA' mB' k'
279 _ -> fail "Can't read logical_id for DRBD8 type"
282 JSArray [vg, lv] -> do
285 return $ LIDPlain vg' lv'
286 _ -> fail "Can't read logical_id for plain type"
289 JSArray [driver, path] -> do
290 driver' <- readJSON driver
291 path' <- readJSON path
292 return $ LIDFile driver' path'
293 _ -> fail "Can't read logical_id for file type"
296 JSArray [driver, path] -> do
297 driver' <- readJSON driver
298 path' <- readJSON path
299 return $ LIDBlockDev driver' path'
300 _ -> fail "Can't read logical_id for blockdev type"
303 JSArray [driver, path] -> do
304 driver' <- readJSON driver
305 path' <- readJSON path
306 return $ LIDRados driver' path'
307 _ -> fail "Can't read logical_id for rdb type"
309 -- | Disk data structure.
311 -- This is declared manually as it's a recursive structure, and our TH
312 -- code currently can't build it.
314 { diskLogicalId :: DiskLogicalId
315 -- , diskPhysicalId :: String
316 , diskChildren :: [Disk]
317 , diskIvName :: String
319 , diskMode :: DiskMode
320 } deriving (Read, Show, Eq)
322 $(buildObjectSerialisation "Disk"
323 [ customField 'decodeDLId 'encodeFullDLId $
324 simpleField "logical_id" [t| DiskLogicalId |]
325 -- , simpleField "physical_id" [t| String |]
326 , defaultField [| [] |] $ simpleField "children" [t| [Disk] |]
327 , defaultField [| "" |] $ simpleField "iv_name" [t| String |]
328 , simpleField "size" [t| Int |]
329 , defaultField [| DiskRdWr |] $ simpleField "mode" [t| DiskMode |]
332 -- * Hypervisor definitions
334 -- | This may be due to change when we add hypervisor parameters.
335 $(declareSADT "Hypervisor"
336 [ ( "Kvm", 'C.htKvm )
337 , ( "XenPvm", 'C.htXenPvm )
338 , ( "Chroot", 'C.htChroot )
339 , ( "XenHvm", 'C.htXenHvm )
340 , ( "Lxc", 'C.htLxc )
341 , ( "Fake", 'C.htFake )
343 $(makeJSONInstance ''Hypervisor)
345 -- * Instance definitions
347 $(declareSADT "AdminState"
348 [ ("AdminOffline", 'C.adminstOffline)
349 , ("AdminDown", 'C.adminstDown)
350 , ("AdminUp", 'C.adminstUp)
352 $(makeJSONInstance ''AdminState)
354 $(buildParam "Be" "bep"
355 [ simpleField "minmem" [t| Int |]
356 , simpleField "maxmem" [t| Int |]
357 , simpleField "vcpus" [t| Int |]
358 , simpleField "auto_balance" [t| Bool |]
361 $(buildObject "Instance" "inst" $
362 [ simpleField "name" [t| String |]
363 , simpleField "primary_node" [t| String |]
364 , simpleField "os" [t| String |]
365 , simpleField "hypervisor" [t| Hypervisor |]
366 , simpleField "hvparams" [t| HvParams |]
367 , simpleField "beparams" [t| PartialBeParams |]
368 , simpleField "osparams" [t| OsParams |]
369 , simpleField "admin_state" [t| AdminState |]
370 , simpleField "nics" [t| [PartialNic] |]
371 , simpleField "disks" [t| [Disk] |]
372 , simpleField "disk_template" [t| DiskTemplate |]
373 , optionalField $ simpleField "network_port" [t| Int |]
380 instance TimeStampObject Instance where
384 instance UuidObject Instance where
387 instance SerialNoObject Instance where
388 serialOf = instSerial
390 instance TagsObject Instance where
393 -- * IPolicy definitions
395 $(buildParam "ISpec" "ispec"
396 [ simpleField C.ispecMemSize [t| Int |]
397 , simpleField C.ispecDiskSize [t| Int |]
398 , simpleField C.ispecDiskCount [t| Int |]
399 , simpleField C.ispecCpuCount [t| Int |]
400 , simpleField C.ispecNicCount [t| Int |]
401 , simpleField C.ispecSpindleUse [t| Int |]
404 -- | Custom partial ipolicy. This is not built via buildParam since it
405 -- has a special 2-level inheritance mode.
406 $(buildObject "PartialIPolicy" "ipolicy"
407 [ renameField "MinSpecP" $ simpleField "min" [t| PartialISpecParams |]
408 , renameField "MaxSpecP" $ simpleField "max" [t| PartialISpecParams |]
409 , renameField "StdSpecP" $ simpleField "std" [t| PartialISpecParams |]
410 , optionalField . renameField "SpindleRatioP"
411 $ simpleField "spindle-ratio" [t| Double |]
412 , optionalField . renameField "VcpuRatioP"
413 $ simpleField "vcpu-ratio" [t| Double |]
414 , optionalField . renameField "DiskTemplatesP"
415 $ simpleField "disk-templates" [t| [DiskTemplate] |]
418 -- | Custom filled ipolicy. This is not built via buildParam since it
419 -- has a special 2-level inheritance mode.
420 $(buildObject "FilledIPolicy" "ipolicy"
421 [ renameField "MinSpec" $ simpleField "min" [t| FilledISpecParams |]
422 , renameField "MaxSpec" $ simpleField "max" [t| FilledISpecParams |]
423 , renameField "StdSpec" $ simpleField "std" [t| FilledISpecParams |]
424 , simpleField "spindle-ratio" [t| Double |]
425 , simpleField "vcpu-ratio" [t| Double |]
426 , simpleField "disk-templates" [t| [DiskTemplate] |]
429 -- | Custom filler for the ipolicy types.
430 fillIPolicy :: FilledIPolicy -> PartialIPolicy -> FilledIPolicy
431 fillIPolicy (FilledIPolicy { ipolicyMinSpec = fmin
432 , ipolicyMaxSpec = fmax
433 , ipolicyStdSpec = fstd
434 , ipolicySpindleRatio = fspindleRatio
435 , ipolicyVcpuRatio = fvcpuRatio
436 , ipolicyDiskTemplates = fdiskTemplates})
437 (PartialIPolicy { ipolicyMinSpecP = pmin
438 , ipolicyMaxSpecP = pmax
439 , ipolicyStdSpecP = pstd
440 , ipolicySpindleRatioP = pspindleRatio
441 , ipolicyVcpuRatioP = pvcpuRatio
442 , ipolicyDiskTemplatesP = pdiskTemplates}) =
443 FilledIPolicy { ipolicyMinSpec = fillISpecParams fmin pmin
444 , ipolicyMaxSpec = fillISpecParams fmax pmax
445 , ipolicyStdSpec = fillISpecParams fstd pstd
446 , ipolicySpindleRatio = fromMaybe fspindleRatio pspindleRatio
447 , ipolicyVcpuRatio = fromMaybe fvcpuRatio pvcpuRatio
448 , ipolicyDiskTemplates = fromMaybe fdiskTemplates
451 -- * Node definitions
453 $(buildParam "ND" "ndp"
454 [ simpleField "oob_program" [t| String |]
455 , simpleField "spindle_count" [t| Int |]
458 $(buildObject "Node" "node" $
459 [ simpleField "name" [t| String |]
460 , simpleField "primary_ip" [t| String |]
461 , simpleField "secondary_ip" [t| String |]
462 , simpleField "master_candidate" [t| Bool |]
463 , simpleField "offline" [t| Bool |]
464 , simpleField "drained" [t| Bool |]
465 , simpleField "group" [t| String |]
466 , simpleField "master_capable" [t| Bool |]
467 , simpleField "vm_capable" [t| Bool |]
468 , simpleField "ndparams" [t| PartialNDParams |]
469 , simpleField "powered" [t| Bool |]
476 instance TimeStampObject Node where
480 instance UuidObject Node where
483 instance SerialNoObject Node where
484 serialOf = nodeSerial
486 instance TagsObject Node where
489 -- * NodeGroup definitions
491 -- | The disk parameters type.
492 type DiskParams = Container (Container JSValue)
494 $(buildObject "NodeGroup" "group" $
495 [ simpleField "name" [t| String |]
496 , defaultField [| [] |] $ simpleField "members" [t| [String] |]
497 , simpleField "ndparams" [t| PartialNDParams |]
498 , simpleField "alloc_policy" [t| AllocPolicy |]
499 , simpleField "ipolicy" [t| PartialIPolicy |]
500 , simpleField "diskparams" [t| DiskParams |]
507 instance TimeStampObject NodeGroup where
511 instance UuidObject NodeGroup where
514 instance SerialNoObject NodeGroup where
515 serialOf = groupSerial
517 instance TagsObject NodeGroup where
521 $(declareIADT "IpFamily"
522 [ ("IpFamilyV4", 'C.ip4Family)
523 , ("IpFamilyV6", 'C.ip6Family)
525 $(makeJSONInstance ''IpFamily)
527 -- | Conversion from IP family to IP version. This is needed because
528 -- Python uses both, depending on context.
529 ipFamilyToVersion :: IpFamily -> Int
530 ipFamilyToVersion IpFamilyV4 = C.ip4Version
531 ipFamilyToVersion IpFamilyV6 = C.ip6Version
533 -- | Cluster HvParams (hvtype to hvparams mapping).
534 type ClusterHvParams = Container HvParams
536 -- | Cluster Os-HvParams (os to hvparams mapping).
537 type OsHvParams = Container ClusterHvParams
539 -- | Cluser BeParams.
540 type ClusterBeParams = Container FilledBeParams
542 -- | Cluster OsParams.
543 type ClusterOsParams = Container OsParams
545 -- | Cluster NicParams.
546 type ClusterNicParams = Container FilledNicParams
548 -- | Cluster UID Pool, list (low, high) UID ranges.
549 type UidPool = [(Int, Int)]
551 -- * Cluster definitions
552 $(buildObject "Cluster" "cluster" $
553 [ simpleField "rsahostkeypub" [t| String |]
554 , simpleField "highest_used_port" [t| Int |]
555 , simpleField "tcpudp_port_pool" [t| [Int] |]
556 , simpleField "mac_prefix" [t| String |]
557 , simpleField "volume_group_name" [t| String |]
558 , simpleField "reserved_lvs" [t| [String] |]
560 simpleField "drbd_usermode_helper" [t| String |]
561 , simpleField "master_node" [t| String |]
562 , simpleField "master_ip" [t| String |]
563 , simpleField "master_netdev" [t| String |]
564 , simpleField "master_netmask" [t| Int |]
565 , simpleField "use_external_mip_script" [t| Bool |]
566 , simpleField "cluster_name" [t| String |]
567 , simpleField "file_storage_dir" [t| String |]
568 , simpleField "shared_file_storage_dir" [t| String |]
569 , simpleField "enabled_hypervisors" [t| [Hypervisor] |]
570 , simpleField "hvparams" [t| ClusterHvParams |]
571 , simpleField "os_hvp" [t| OsHvParams |]
572 , simpleField "beparams" [t| ClusterBeParams |]
573 , simpleField "osparams" [t| ClusterOsParams |]
574 , simpleField "nicparams" [t| ClusterNicParams |]
575 , simpleField "ndparams" [t| FilledNDParams |]
576 , simpleField "diskparams" [t| DiskParams |]
577 , simpleField "candidate_pool_size" [t| Int |]
578 , simpleField "modify_etc_hosts" [t| Bool |]
579 , simpleField "modify_ssh_setup" [t| Bool |]
580 , simpleField "maintain_node_health" [t| Bool |]
581 , simpleField "uid_pool" [t| UidPool |]
582 , simpleField "default_iallocator" [t| String |]
583 , simpleField "hidden_os" [t| [String] |]
584 , simpleField "blacklisted_os" [t| [String] |]
585 , simpleField "primary_ip_family" [t| IpFamily |]
586 , simpleField "prealloc_wipe_disks" [t| Bool |]
587 , simpleField "ipolicy" [t| FilledIPolicy |]
594 instance TimeStampObject Cluster where
595 cTimeOf = clusterCtime
596 mTimeOf = clusterMtime
598 instance UuidObject Cluster where
601 instance SerialNoObject Cluster where
602 serialOf = clusterSerial
604 instance TagsObject Cluster where
607 -- * ConfigData definitions
609 $(buildObject "ConfigData" "config" $
610 -- timeStampFields ++
611 [ simpleField "version" [t| Int |]
612 , simpleField "cluster" [t| Cluster |]
613 , simpleField "nodes" [t| Container Node |]
614 , simpleField "nodegroups" [t| Container NodeGroup |]
615 , simpleField "instances" [t| Container Instance |]
619 instance SerialNoObject ConfigData where
620 serialOf = configSerial