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
33 , PartialNICParams(..)
55 , FilledISpecParams(..)
56 , PartialISpecParams(..)
69 import Text.JSON (makeObj, showJSON, readJSON, JSON, JSValue(..))
70 import qualified Text.JSON as J
72 import qualified Ganeti.Constants as C
73 import Ganeti.HTools.JSON
79 $(declareSADT "NICMode"
80 [ ("NMBridged", 'C.nicModeBridged)
81 , ("NMRouted", 'C.nicModeRouted)
83 $(makeJSONInstance ''NICMode)
85 $(buildParam "NIC" "nicp"
86 [ simpleField "mode" [t| NICMode |]
87 , simpleField "link" [t| String |]
90 $(buildObject "PartialNIC" "nic"
91 [ simpleField "mac" [t| String |]
92 , optionalField $ simpleField "ip" [t| String |]
93 , simpleField "nicparams" [t| PartialNICParams |]
98 $(declareSADT "DiskMode"
99 [ ("DiskRdOnly", 'C.diskRdonly)
100 , ("DiskRdWr", 'C.diskRdwr)
102 $(makeJSONInstance ''DiskMode)
104 $(declareSADT "DiskType"
106 , ("LD_DRBD8", 'C.ldDrbd8)
107 , ("LD_FILE", 'C.ldFile)
108 , ("LD_BLOCKDEV", 'C.ldBlockdev)
109 , ("LD_RADOS", 'C.ldRbd)
111 $(makeJSONInstance ''DiskType)
113 -- | The file driver type.
114 $(declareSADT "FileDriver"
115 [ ("FileLoop", 'C.fdLoop)
116 , ("FileBlktap", 'C.fdBlktap)
118 $(makeJSONInstance ''FileDriver)
120 -- | The persistent block driver type. Currently only one type is allowed.
121 $(declareSADT "BlockDriver"
122 [ ("BlockDrvManual", 'C.blockdevDriverManual)
124 $(makeJSONInstance ''BlockDriver)
126 -- | Constant for the dev_type key entry in the disk config.
130 -- | The disk configuration type. This includes the disk type itself,
131 -- for a more complete consistency. Note that since in the Python
132 -- code-base there's no authoritative place where we document the
133 -- logical id, this is probably a good reference point.
135 = LIDPlain String String -- ^ Volume group, logical volume
136 | LIDDrbd8 String String Int Int Int String
137 -- ^ NodeA, NodeB, Port, MinorA, MinorB, Secret
138 | LIDFile FileDriver String -- ^ Driver, path
139 | LIDBlockDev BlockDriver String -- ^ Driver, path (must be under /dev)
140 | LIDRados String String -- ^ Unused, path
141 deriving (Read, Show, Eq)
143 -- | Mapping from a logical id to a disk type.
144 lidDiskType :: DiskLogicalId -> DiskType
145 lidDiskType (LIDPlain {}) = LD_LV
146 lidDiskType (LIDDrbd8 {}) = LD_DRBD8
147 lidDiskType (LIDFile {}) = LD_FILE
148 lidDiskType (LIDBlockDev {}) = LD_BLOCKDEV
149 lidDiskType (LIDRados {}) = LD_RADOS
151 -- | Builds the extra disk_type field for a given logical id.
152 lidEncodeType :: DiskLogicalId -> [(String, JSValue)]
153 lidEncodeType v = [(devType, showJSON . lidDiskType $ v)]
155 -- | Custom encoder for DiskLogicalId (logical id only).
156 encodeDLId :: DiskLogicalId -> JSValue
157 encodeDLId (LIDPlain vg lv) = JSArray [showJSON vg, showJSON lv]
158 encodeDLId (LIDDrbd8 nodeA nodeB port minorA minorB key) =
159 JSArray [ showJSON nodeA, showJSON nodeB, showJSON port
160 , showJSON minorA, showJSON minorB, showJSON key ]
161 encodeDLId (LIDRados pool name) = JSArray [showJSON pool, showJSON name]
162 encodeDLId (LIDFile driver name) = JSArray [showJSON driver, showJSON name]
163 encodeDLId (LIDBlockDev driver name) = JSArray [showJSON driver, showJSON name]
165 -- | Custom encoder for DiskLogicalId, composing both the logical id
166 -- and the extra disk_type field.
167 encodeFullDLId :: DiskLogicalId -> (JSValue, [(String, JSValue)])
168 encodeFullDLId v = (encodeDLId v, lidEncodeType v)
170 -- | Custom decoder for DiskLogicalId. This is manual for now, since
171 -- we don't have yet automation for separate-key style fields.
172 decodeDLId :: [(String, JSValue)] -> JSValue -> J.Result DiskLogicalId
173 decodeDLId obj lid = do
174 dtype <- fromObj obj devType
178 JSArray [nA, nB, p, mA, mB, k] -> do
185 return $ LIDDrbd8 nA' nB' p' mA' mB' k'
186 _ -> fail $ "Can't read logical_id for DRBD8 type"
189 JSArray [vg, lv] -> do
192 return $ LIDPlain vg' lv'
193 _ -> fail $ "Can't read logical_id for plain type"
196 JSArray [driver, path] -> do
197 driver' <- readJSON driver
198 path' <- readJSON path
199 return $ LIDFile driver' path'
200 _ -> fail $ "Can't read logical_id for file type"
203 JSArray [driver, path] -> do
204 driver' <- readJSON driver
205 path' <- readJSON path
206 return $ LIDBlockDev driver' path'
207 _ -> fail $ "Can't read logical_id for blockdev type"
210 JSArray [driver, path] -> do
211 driver' <- readJSON driver
212 path' <- readJSON path
213 return $ LIDRados driver' path'
214 _ -> fail $ "Can't read logical_id for rdb type"
216 -- | Disk data structure.
218 -- This is declared manually as it's a recursive structure, and our TH
219 -- code currently can't build it.
221 { diskLogicalId :: DiskLogicalId
222 -- , diskPhysicalId :: String
223 , diskChildren :: [Disk]
224 , diskIvName :: String
226 , diskMode :: DiskMode
227 } deriving (Read, Show, Eq)
229 $(buildObjectSerialisation "Disk"
230 [ customField 'decodeDLId 'encodeFullDLId $
231 simpleField "logical_id" [t| DiskLogicalId |]
232 -- , simpleField "physical_id" [t| String |]
233 , defaultField [| [] |] $ simpleField "children" [t| [Disk] |]
234 , defaultField [| "" |] $ simpleField "iv_name" [t| String |]
235 , simpleField "size" [t| Int |]
236 , defaultField [| DiskRdWr |] $ simpleField "mode" [t| DiskMode |]
239 -- * Hypervisor definitions
241 -- | This may be due to change when we add hypervisor parameters.
242 $(declareSADT "Hypervisor"
243 [ ( "Kvm", 'C.htKvm )
244 , ( "XenPvm", 'C.htXenPvm )
245 , ( "Chroot", 'C.htChroot )
246 , ( "XenHvm", 'C.htXenHvm )
247 , ( "Lxc", 'C.htLxc )
248 , ( "Fake", 'C.htFake )
250 $(makeJSONInstance ''Hypervisor)
252 -- * Instance definitions
254 -- | Instance disk template type. **Copied from HTools/Types.hs**
255 $(declareSADT "DiskTemplate"
256 [ ("DTDiskless", 'C.dtDiskless)
257 , ("DTFile", 'C.dtFile)
258 , ("DTSharedFile", 'C.dtSharedFile)
259 , ("DTPlain", 'C.dtPlain)
260 , ("DTBlock", 'C.dtBlock)
261 , ("DTDrbd8", 'C.dtDrbd8)
262 , ("DTRados", 'C.dtRbd)
264 $(makeJSONInstance ''DiskTemplate)
266 $(declareSADT "AdminState"
267 [ ("AdminOffline", 'C.adminstOffline)
268 , ("AdminDown", 'C.adminstDown)
269 , ("AdminUp", 'C.adminstUp)
271 $(makeJSONInstance ''AdminState)
273 $(buildParam "BE" "bep" $
274 [ simpleField "minmem" [t| Int |]
275 , simpleField "maxmem" [t| Int |]
276 , simpleField "vcpus" [t| Int |]
277 , simpleField "auto_balance" [t| Bool |]
280 $(buildObject "Instance" "inst" $
281 [ simpleField "name" [t| String |]
282 , simpleField "primary_node" [t| String |]
283 , simpleField "os" [t| String |]
284 , simpleField "hypervisor" [t| String |]
285 -- , simpleField "hvparams" [t| [(String, String)] |]
286 , simpleField "beparams" [t| PartialBEParams |]
287 -- , simpleField "osparams" [t| [(String, String)] |]
288 , simpleField "admin_state" [t| AdminState |]
289 , simpleField "nics" [t| [PartialNIC] |]
290 , simpleField "disks" [t| [Disk] |]
291 , simpleField "disk_template" [t| DiskTemplate |]
292 , optionalField $ simpleField "network_port" [t| Int |]
299 -- * IPolicy definitions
301 $(buildParam "ISpec" "ispec" $
302 [ simpleField C.ispecMemSize [t| Int |]
303 , simpleField C.ispecDiskSize [t| Int |]
304 , simpleField C.ispecDiskCount [t| Int |]
305 , simpleField C.ispecCpuCount [t| Int |]
306 , simpleField C.ispecSpindleUse [t| Int |]
309 -- | Custom partial ipolicy. This is not built via buildParam since it
310 -- has a special 2-level inheritance mode.
311 $(buildObject "PartialIPolicy" "ipolicy" $
312 [ renameField "MinSpecP" $ simpleField "min" [t| PartialISpecParams |]
313 , renameField "MaxSpecP" $ simpleField "max" [t| PartialISpecParams |]
314 , renameField "StdSpecP" $ simpleField "std" [t| PartialISpecParams |]
315 , optionalField . renameField "SpindleRatioP"
316 $ simpleField "spindle-ratio" [t| Double |]
317 , optionalField . renameField "VcpuRatioP"
318 $ simpleField "vcpu-ratio" [t| Double |]
319 , optionalField . renameField "DiskTemplatesP"
320 $ simpleField "disk-templates" [t| [DiskTemplate] |]
323 -- | Custom filled ipolicy. This is not built via buildParam since it
324 -- has a special 2-level inheritance mode.
325 $(buildObject "FilledIPolicy" "ipolicy" $
326 [ renameField "MinSpec" $ simpleField "min" [t| FilledISpecParams |]
327 , renameField "MaxSpec" $ simpleField "max" [t| FilledISpecParams |]
328 , renameField "StdSpec" $ simpleField "std" [t| FilledISpecParams |]
329 , simpleField "spindle-ratio" [t| Double |]
330 , simpleField "vcpu-ratio" [t| Double |]
331 , simpleField "disk-templates" [t| [DiskTemplate] |]
334 -- | Custom filler for the ipolicy types.
335 fillIPolicy :: FilledIPolicy -> PartialIPolicy -> FilledIPolicy
336 fillIPolicy (FilledIPolicy { ipolicyMinSpec = fmin
337 , ipolicyMaxSpec = fmax
338 , ipolicyStdSpec = fstd
339 , ipolicySpindleRatio = fspindleRatio
340 , ipolicyVcpuRatio = fvcpuRatio
341 , ipolicyDiskTemplates = fdiskTemplates})
342 (PartialIPolicy { ipolicyMinSpecP = pmin
343 , ipolicyMaxSpecP = pmax
344 , ipolicyStdSpecP = pstd
345 , ipolicySpindleRatioP = pspindleRatio
346 , ipolicyVcpuRatioP = pvcpuRatio
347 , ipolicyDiskTemplatesP = pdiskTemplates}) =
348 FilledIPolicy { ipolicyMinSpec = fillISpecParams fmin pmin
349 , ipolicyMaxSpec = fillISpecParams fmax pmax
350 , ipolicyStdSpec = fillISpecParams fstd pstd
351 , ipolicySpindleRatio = fromMaybe fspindleRatio pspindleRatio
352 , ipolicyVcpuRatio = fromMaybe fvcpuRatio pvcpuRatio
353 , ipolicyDiskTemplates = fromMaybe fdiskTemplates
356 -- * Node definitions
358 $(buildParam "ND" "ndp" $
359 [ simpleField "oob_program" [t| String |]
360 , simpleField "spindle_count" [t| Int |]
363 $(buildObject "Node" "node" $
364 [ simpleField "name" [t| String |]
365 , simpleField "primary_ip" [t| String |]
366 , simpleField "secondary_ip" [t| String |]
367 , simpleField "master_candidate" [t| Bool |]
368 , simpleField "offline" [t| Bool |]
369 , simpleField "drained" [t| Bool |]
370 , simpleField "group" [t| String |]
371 , simpleField "master_capable" [t| Bool |]
372 , simpleField "vm_capable" [t| Bool |]
373 , simpleField "ndparams" [t| PartialNDParams |]
374 , simpleField "powered" [t| Bool |]
381 -- * NodeGroup definitions
383 -- | The Group allocation policy type.
385 -- Note that the order of constructors is important as the automatic
386 -- Ord instance will order them in the order they are defined, so when
387 -- changing this data type be careful about the interaction with the
388 -- desired sorting order.
390 -- FIXME: COPIED from Types.hs; we need to eliminate this duplication later
391 $(declareSADT "AllocPolicy"
392 [ ("AllocPreferred", 'C.allocPolicyPreferred)
393 , ("AllocLastResort", 'C.allocPolicyLastResort)
394 , ("AllocUnallocable", 'C.allocPolicyUnallocable)
396 $(makeJSONInstance ''AllocPolicy)
398 $(buildObject "NodeGroup" "group" $
399 [ simpleField "name" [t| String |]
400 , defaultField [| [] |] $ simpleField "members" [t| [String] |]
401 , simpleField "ndparams" [t| PartialNDParams |]
402 , simpleField "alloc_policy" [t| AllocPolicy |]
403 , simpleField "ipolicy" [t| PartialIPolicy |]
411 $(declareIADT "IpFamily"
412 [ ("IpFamilyV4", 'C.ip4Family)
413 , ("IpFamilyV6", 'C.ip6Family)
415 $(makeJSONInstance ''IpFamily)
417 -- | Conversion from IP family to IP version. This is needed because
418 -- Python uses both, depending on context.
419 ipFamilyToVersion :: IpFamily -> Int
420 ipFamilyToVersion IpFamilyV4 = C.ip4Version
421 ipFamilyToVersion IpFamilyV6 = C.ip6Version
423 -- * Cluster definitions
424 $(buildObject "Cluster" "cluster" $
425 [ simpleField "rsahostkeypub" [t| String |]
426 , simpleField "highest_used_port" [t| Int |]
427 , simpleField "tcpudp_port_pool" [t| [Int] |]
428 , simpleField "mac_prefix" [t| String |]
429 , simpleField "volume_group_name" [t| String |]
430 , simpleField "reserved_lvs" [t| [String] |]
431 , optionalField $ simpleField "drbd_usermode_helper" [t| String |]
432 -- , simpleField "default_bridge" [t| String |]
433 -- , simpleField "default_hypervisor" [t| String |]
434 , simpleField "master_node" [t| String |]
435 , simpleField "master_ip" [t| String |]
436 , simpleField "master_netdev" [t| String |]
437 , simpleField "master_netmask" [t| Int |]
438 , simpleField "use_external_mip_script" [t| Bool |]
439 , simpleField "cluster_name" [t| String |]
440 , simpleField "file_storage_dir" [t| String |]
441 , simpleField "shared_file_storage_dir" [t| String |]
442 , simpleField "enabled_hypervisors" [t| [String] |]
443 -- , simpleField "hvparams" [t| [(String, [(String, String)])] |]
444 -- , simpleField "os_hvp" [t| [(String, String)] |]
445 , simpleField "beparams" [t| Container FilledBEParams |]
446 , simpleField "osparams" [t| Container (Container String) |]
447 , simpleField "nicparams" [t| Container FilledNICParams |]
448 , simpleField "ndparams" [t| FilledNDParams |]
449 , simpleField "candidate_pool_size" [t| Int |]
450 , simpleField "modify_etc_hosts" [t| Bool |]
451 , simpleField "modify_ssh_setup" [t| Bool |]
452 , simpleField "maintain_node_health" [t| Bool |]
453 , simpleField "uid_pool" [t| [(Int, Int)] |]
454 , simpleField "default_iallocator" [t| String |]
455 , simpleField "hidden_os" [t| [String] |]
456 , simpleField "blacklisted_os" [t| [String] |]
457 , simpleField "primary_ip_family" [t| IpFamily |]
458 , simpleField "prealloc_wipe_disks" [t| Bool |]
459 , simpleField "ipolicy" [t| FilledIPolicy |]
466 -- * ConfigData definitions
468 $(buildObject "ConfigData" "config" $
469 -- timeStampFields ++
470 [ simpleField "version" [t| Int |]
471 , simpleField "cluster" [t| Cluster |]
472 , simpleField "nodes" [t| Container Node |]
473 , simpleField "nodegroups" [t| Container NodeGroup |]
474 , simpleField "instances" [t| Container Instance |]