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(..)
61 import Text.JSON (makeObj, showJSON, readJSON, JSON, JSValue(..))
62 import qualified Text.JSON as J
64 import qualified Ganeti.Constants as C
65 import Ganeti.HTools.JSON
71 $(declareSADT "NICMode"
72 [ ("NMBridged", 'C.nicModeBridged)
73 , ("NMRouted", 'C.nicModeRouted)
75 $(makeJSONInstance ''NICMode)
77 $(buildParam "NIC" "nicp"
78 [ simpleField "mode" [t| NICMode |]
79 , simpleField "link" [t| String |]
82 $(buildObject "PartialNIC" "nic"
83 [ simpleField "mac" [t| String |]
84 , optionalField $ simpleField "ip" [t| String |]
85 , simpleField "nicparams" [t| PartialNICParams |]
90 $(declareSADT "DiskMode"
91 [ ("DiskRdOnly", 'C.diskRdonly)
92 , ("DiskRdWr", 'C.diskRdwr)
94 $(makeJSONInstance ''DiskMode)
96 $(declareSADT "DiskType"
98 , ("LD_DRBD8", 'C.ldDrbd8)
99 , ("LD_FILE", 'C.ldFile)
100 , ("LD_BLOCKDEV", 'C.ldBlockdev)
101 , ("LD_RADOS", 'C.ldRbd)
103 $(makeJSONInstance ''DiskType)
105 -- | The file driver type.
106 $(declareSADT "FileDriver"
107 [ ("FileLoop", 'C.fdLoop)
108 , ("FileBlktap", 'C.fdBlktap)
110 $(makeJSONInstance ''FileDriver)
112 -- | The persistent block driver type. Currently only one type is allowed.
113 $(declareSADT "BlockDriver"
114 [ ("BlockDrvManual", 'C.blockdevDriverManual)
116 $(makeJSONInstance ''BlockDriver)
118 -- | Constant for the dev_type key entry in the disk config.
122 -- | The disk configuration type. This includes the disk type itself,
123 -- for a more complete consistency. Note that since in the Python
124 -- code-base there's no authoritative place where we document the
125 -- logical id, this is probably a good reference point.
127 = LIDPlain String String -- ^ Volume group, logical volume
128 | LIDDrbd8 String String Int Int Int String
129 -- ^ NodeA, NodeB, Port, MinorA, MinorB, Secret
130 | LIDFile FileDriver String -- ^ Driver, path
131 | LIDBlockDev BlockDriver String -- ^ Driver, path (must be under /dev)
132 | LIDRados String String -- ^ Unused, path
133 deriving (Read, Show, Eq)
135 -- | Mapping from a logical id to a disk type.
136 lidDiskType :: DiskLogicalId -> DiskType
137 lidDiskType (LIDPlain {}) = LD_LV
138 lidDiskType (LIDDrbd8 {}) = LD_DRBD8
139 lidDiskType (LIDFile {}) = LD_FILE
140 lidDiskType (LIDBlockDev {}) = LD_BLOCKDEV
141 lidDiskType (LIDRados {}) = LD_RADOS
143 -- | Builds the extra disk_type field for a given logical id.
144 lidEncodeType :: DiskLogicalId -> [(String, JSValue)]
145 lidEncodeType v = [(devType, showJSON . lidDiskType $ v)]
147 -- | Custom encoder for DiskLogicalId (logical id only).
148 encodeDLId :: DiskLogicalId -> JSValue
149 encodeDLId (LIDPlain vg lv) = JSArray [showJSON vg, showJSON lv]
150 encodeDLId (LIDDrbd8 nodeA nodeB port minorA minorB key) =
151 JSArray [ showJSON nodeA, showJSON nodeB, showJSON port
152 , showJSON minorA, showJSON minorB, showJSON key ]
153 encodeDLId (LIDRados pool name) = JSArray [showJSON pool, showJSON name]
154 encodeDLId (LIDFile driver name) = JSArray [showJSON driver, showJSON name]
155 encodeDLId (LIDBlockDev driver name) = JSArray [showJSON driver, showJSON name]
157 -- | Custom encoder for DiskLogicalId, composing both the logical id
158 -- and the extra disk_type field.
159 encodeFullDLId :: DiskLogicalId -> (JSValue, [(String, JSValue)])
160 encodeFullDLId v = (encodeDLId v, lidEncodeType v)
162 -- | Custom decoder for DiskLogicalId. This is manual for now, since
163 -- we don't have yet automation for separate-key style fields.
164 decodeDLId :: [(String, JSValue)] -> JSValue -> J.Result DiskLogicalId
165 decodeDLId obj lid = do
166 dtype <- fromObj obj devType
170 JSArray [nA, nB, p, mA, mB, k] -> do
177 return $ LIDDrbd8 nA' nB' p' mA' mB' k'
178 _ -> fail $ "Can't read logical_id for DRBD8 type"
181 JSArray [vg, lv] -> do
184 return $ LIDPlain vg' lv'
185 _ -> fail $ "Can't read logical_id for plain type"
188 JSArray [driver, path] -> do
189 driver' <- readJSON driver
190 path' <- readJSON path
191 return $ LIDFile driver' path'
192 _ -> fail $ "Can't read logical_id for file type"
195 JSArray [driver, path] -> do
196 driver' <- readJSON driver
197 path' <- readJSON path
198 return $ LIDBlockDev driver' path'
199 _ -> fail $ "Can't read logical_id for blockdev type"
202 JSArray [driver, path] -> do
203 driver' <- readJSON driver
204 path' <- readJSON path
205 return $ LIDRados driver' path'
206 _ -> fail $ "Can't read logical_id for rdb type"
208 -- | Disk data structure.
210 -- This is declared manually as it's a recursive structure, and our TH
211 -- code currently can't build it.
213 { diskLogicalId :: DiskLogicalId
214 -- , diskPhysicalId :: String
215 , diskChildren :: [Disk]
216 , diskIvName :: String
218 , diskMode :: DiskMode
219 } deriving (Read, Show, Eq)
221 $(buildObjectSerialisation "Disk"
222 [ customField 'decodeDLId 'encodeFullDLId $
223 simpleField "logical_id" [t| DiskLogicalId |]
224 -- , simpleField "physical_id" [t| String |]
225 , defaultField [| [] |] $ simpleField "children" [t| [Disk] |]
226 , defaultField [| "" |] $ simpleField "iv_name" [t| String |]
227 , simpleField "size" [t| Int |]
228 , defaultField [| DiskRdWr |] $ simpleField "mode" [t| DiskMode |]
231 -- * Hypervisor definitions
233 -- | This may be due to change when we add hypervisor parameters.
234 $(declareSADT "Hypervisor"
235 [ ( "Kvm", 'C.htKvm )
236 , ( "XenPvm", 'C.htXenPvm )
237 , ( "Chroot", 'C.htChroot )
238 , ( "XenHvm", 'C.htXenHvm )
239 , ( "Lxc", 'C.htLxc )
240 , ( "Fake", 'C.htFake )
242 $(makeJSONInstance ''Hypervisor)
244 -- * Instance definitions
246 -- | Instance disk template type. **Copied from HTools/Types.hs**
247 $(declareSADT "DiskTemplate"
248 [ ("DTDiskless", 'C.dtDiskless)
249 , ("DTFile", 'C.dtFile)
250 , ("DTSharedFile", 'C.dtSharedFile)
251 , ("DTPlain", 'C.dtPlain)
252 , ("DTBlock", 'C.dtBlock)
253 , ("DTDrbd8", 'C.dtDrbd8)
255 $(makeJSONInstance ''DiskTemplate)
257 $(declareSADT "AdminState"
258 [ ("AdminOffline", 'C.adminstOffline)
259 , ("AdminDown", 'C.adminstDown)
260 , ("AdminUp", 'C.adminstUp)
262 $(makeJSONInstance ''AdminState)
264 $(buildParam "BE" "bep" $
265 [ simpleField "minmem" [t| Int |]
266 , simpleField "maxmem" [t| Int |]
267 , simpleField "vcpus" [t| Int |]
268 , simpleField "auto_balance" [t| Bool |]
271 $(buildObject "Instance" "inst" $
272 [ simpleField "name" [t| String |]
273 , simpleField "primary_node" [t| String |]
274 , simpleField "os" [t| String |]
275 , simpleField "hypervisor" [t| String |]
276 -- , simpleField "hvparams" [t| [(String, String)] |]
277 , simpleField "beparams" [t| PartialBEParams |]
278 -- , simpleField "osparams" [t| [(String, String)] |]
279 , simpleField "admin_state" [t| AdminState |]
280 , simpleField "nics" [t| [PartialNIC] |]
281 , simpleField "disks" [t| [Disk] |]
282 , simpleField "disk_template" [t| DiskTemplate |]
283 , optionalField $ simpleField "network_port" [t| Int |]
289 -- * Node definitions
291 $(buildParam "ND" "ndp" $
292 [ simpleField "oob_program" [t| String |]
295 $(buildObject "Node" "node" $
296 [ simpleField "name" [t| String |]
297 , simpleField "primary_ip" [t| String |]
298 , simpleField "secondary_ip" [t| String |]
299 , simpleField "master_candidate" [t| Bool |]
300 , simpleField "offline" [t| Bool |]
301 , simpleField "drained" [t| Bool |]
302 , simpleField "group" [t| String |]
303 , simpleField "master_capable" [t| Bool |]
304 , simpleField "vm_capable" [t| Bool |]
305 -- , simpleField "ndparams" [t| PartialNDParams |]
306 , simpleField "powered" [t| Bool |]
312 -- * NodeGroup definitions
314 -- | The Group allocation policy type.
316 -- Note that the order of constructors is important as the automatic
317 -- Ord instance will order them in the order they are defined, so when
318 -- changing this data type be careful about the interaction with the
319 -- desired sorting order.
321 -- FIXME: COPIED from Types.hs; we need to eliminate this duplication later
322 $(declareSADT "AllocPolicy"
323 [ ("AllocPreferred", 'C.allocPolicyPreferred)
324 , ("AllocLastResort", 'C.allocPolicyLastResort)
325 , ("AllocUnallocable", 'C.allocPolicyUnallocable)
327 $(makeJSONInstance ''AllocPolicy)
329 $(buildObject "NodeGroup" "group" $
330 [ simpleField "name" [t| String |]
331 , defaultField [| [] |] $ simpleField "members" [t| [String] |]
332 -- , simpleField "ndparams" [t| PartialNDParams |]
333 , simpleField "alloc_policy" [t| AllocPolicy |]
339 -- * Cluster definitions
340 $(buildObject "Cluster" "cluster" $
341 [ simpleField "rsahostkeypub" [t| String |]
342 , simpleField "highest_used_port" [t| Int |]
343 , simpleField "tcpudp_port_pool" [t| [Int] |]
344 , simpleField "mac_prefix" [t| String |]
345 , simpleField "volume_group_name" [t| String |]
346 , simpleField "reserved_lvs" [t| [String] |]
347 -- , simpleField "drbd_usermode_helper" [t| String |]
348 -- , simpleField "default_bridge" [t| String |]
349 -- , simpleField "default_hypervisor" [t| String |]
350 , simpleField "master_node" [t| String |]
351 , simpleField "master_ip" [t| String |]
352 , simpleField "master_netdev" [t| String |]
353 -- , simpleField "master_netmask" [t| String |]
354 , simpleField "cluster_name" [t| String |]
355 , simpleField "file_storage_dir" [t| String |]
356 -- , simpleField "shared_file_storage_dir" [t| String |]
357 , simpleField "enabled_hypervisors" [t| [String] |]
358 -- , simpleField "hvparams" [t| [(String, [(String, String)])] |]
359 -- , simpleField "os_hvp" [t| [(String, String)] |]
360 , containerField $ simpleField "beparams" [t| FilledBEParams |]
361 -- , simpleField "osparams" [t| [(String, String)] |]
362 , containerField $ simpleField "nicparams" [t| FilledNICParams |]
363 -- , simpleField "ndparams" [t| FilledNDParams |]
364 , simpleField "candidate_pool_size" [t| Int |]
365 , simpleField "modify_etc_hosts" [t| Bool |]
366 , simpleField "modify_ssh_setup" [t| Bool |]
367 , simpleField "maintain_node_health" [t| Bool |]
368 , simpleField "uid_pool" [t| [Int] |]
369 , simpleField "default_iallocator" [t| String |]
370 , simpleField "hidden_os" [t| [String] |]
371 , simpleField "blacklisted_os" [t| [String] |]
372 , simpleField "primary_ip_family" [t| Int |]
373 , simpleField "prealloc_wipe_disks" [t| Bool |]
380 -- * ConfigData definitions
382 $(buildObject "ConfigData" "config" $
383 -- timeStampFields ++
384 [ simpleField "version" [t| Int |]
385 , simpleField "cluster" [t| Cluster |]
386 , containerField $ simpleField "nodes" [t| Node |]
387 , containerField $ simpleField "nodegroups" [t| NodeGroup |]
388 , containerField $ simpleField "instances" [t| Instance |]