1 {-# LANGUAGE TemplateHaskell #-}
3 {-| Some common Ganeti types.
5 This holds types common to both core work, and to htools. Types that
6 are very core specific (e.g. configuration objects) should go in
7 'Ganeti.Objects', while types that are specific to htools in-memory
8 representation should go into 'Ganeti.HTools.Types'.
14 Copyright (C) 2012 Google Inc.
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
38 , instanceStatusFromRaw
54 , VerifyOptionalChecks(..)
67 , IAllocatorTestDir(..)
76 import qualified Text.JSON as JSON
78 import qualified Ganeti.Constants as C
79 import qualified Ganeti.THH as THH
84 -- | Type that holds a non-negative value.
85 newtype NonNegative a = NonNegative { fromNonNegative :: a }
88 -- | Smart constructor for 'NonNegative'.
89 mkNonNegative :: (Monad m, Num a, Ord a, Show a) => a -> m (NonNegative a)
90 mkNonNegative i | i >= 0 = return (NonNegative i)
91 | otherwise = fail $ "Invalid value for non-negative type '" ++
94 instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (NonNegative a) where
95 showJSON = JSON.showJSON . fromNonNegative
96 readJSON v = JSON.readJSON v >>= mkNonNegative
98 -- | Type that holds a positive value.
99 newtype Positive a = Positive { fromPositive :: a }
102 -- | Smart constructor for 'Positive'.
103 mkPositive :: (Monad m, Num a, Ord a, Show a) => a -> m (Positive a)
104 mkPositive i | i > 0 = return (Positive i)
105 | otherwise = fail $ "Invalid value for positive type '" ++
108 instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (Positive a) where
109 showJSON = JSON.showJSON . fromPositive
110 readJSON v = JSON.readJSON v >>= mkPositive
112 -- | Type that holds a non-null list.
113 newtype NonEmpty a = NonEmpty { fromNonEmpty :: [a] }
116 -- | Smart constructor for 'NonEmpty'.
117 mkNonEmpty :: (Monad m) => [a] -> m (NonEmpty a)
118 mkNonEmpty [] = fail "Received empty value for non-empty list"
119 mkNonEmpty xs = return (NonEmpty xs)
121 instance (JSON.JSON a) => JSON.JSON (NonEmpty a) where
122 showJSON = JSON.showJSON . fromNonEmpty
123 readJSON v = JSON.readJSON v >>= mkNonEmpty
125 -- | A simple type alias for non-empty strings.
126 type NonEmptyString = NonEmpty Char
130 -- | Instance disk template type.
131 $(THH.declareSADT "DiskTemplate"
132 [ ("DTDiskless", 'C.dtDiskless)
133 , ("DTFile", 'C.dtFile)
134 , ("DTSharedFile", 'C.dtSharedFile)
135 , ("DTPlain", 'C.dtPlain)
136 , ("DTBlock", 'C.dtBlock)
137 , ("DTDrbd8", 'C.dtDrbd8)
138 , ("DTRbd", 'C.dtRbd)
140 $(THH.makeJSONInstance ''DiskTemplate)
142 instance HasStringRepr DiskTemplate where
143 fromStringRepr = diskTemplateFromRaw
144 toStringRepr = diskTemplateToRaw
146 -- | The Group allocation policy type.
148 -- Note that the order of constructors is important as the automatic
149 -- Ord instance will order them in the order they are defined, so when
150 -- changing this data type be careful about the interaction with the
151 -- desired sorting order.
152 $(THH.declareSADT "AllocPolicy"
153 [ ("AllocPreferred", 'C.allocPolicyPreferred)
154 , ("AllocLastResort", 'C.allocPolicyLastResort)
155 , ("AllocUnallocable", 'C.allocPolicyUnallocable)
157 $(THH.makeJSONInstance ''AllocPolicy)
159 -- | The Instance real state type. FIXME: this could be improved to
160 -- just wrap a /NormalState AdminStatus | ErrorState ErrorCondition/.
161 $(THH.declareSADT "InstanceStatus"
162 [ ("StatusDown", 'C.inststAdmindown)
163 , ("StatusOffline", 'C.inststAdminoffline)
164 , ("ErrorDown", 'C.inststErrordown)
165 , ("ErrorUp", 'C.inststErrorup)
166 , ("NodeDown", 'C.inststNodedown)
167 , ("NodeOffline", 'C.inststNodeoffline)
168 , ("Running", 'C.inststRunning)
169 , ("WrongNode", 'C.inststWrongnode)
171 $(THH.makeJSONInstance ''InstanceStatus)
174 $(THH.declareSADT "MigrationMode"
175 [ ("MigrationLive", 'C.htMigrationLive)
176 , ("MigrationNonLive", 'C.htMigrationNonlive)
178 $(THH.makeJSONInstance ''MigrationMode)
180 -- | Verify optional checks.
181 $(THH.declareSADT "VerifyOptionalChecks"
182 [ ("VerifyNPlusOneMem", 'C.verifyNplusoneMem)
184 $(THH.makeJSONInstance ''VerifyOptionalChecks)
186 -- | Cluster verify error codes.
187 $(THH.declareSADT "CVErrorCode"
188 [ ("CvECLUSTERCFG", 'C.cvEclustercfgCode)
189 , ("CvECLUSTERCERT", 'C.cvEclustercertCode)
190 , ("CvECLUSTERFILECHECK", 'C.cvEclusterfilecheckCode)
191 , ("CvECLUSTERDANGLINGNODES", 'C.cvEclusterdanglingnodesCode)
192 , ("CvECLUSTERDANGLINGINST", 'C.cvEclusterdanglinginstCode)
193 , ("CvEINSTANCEBADNODE", 'C.cvEinstancebadnodeCode)
194 , ("CvEINSTANCEDOWN", 'C.cvEinstancedownCode)
195 , ("CvEINSTANCELAYOUT", 'C.cvEinstancelayoutCode)
196 , ("CvEINSTANCEMISSINGDISK", 'C.cvEinstancemissingdiskCode)
197 , ("CvEINSTANCEFAULTYDISK", 'C.cvEinstancefaultydiskCode)
198 , ("CvEINSTANCEWRONGNODE", 'C.cvEinstancewrongnodeCode)
199 , ("CvEINSTANCESPLITGROUPS", 'C.cvEinstancesplitgroupsCode)
200 , ("CvEINSTANCEPOLICY", 'C.cvEinstancepolicyCode)
201 , ("CvENODEDRBD", 'C.cvEnodedrbdCode)
202 , ("CvENODEDRBDHELPER", 'C.cvEnodedrbdhelperCode)
203 , ("CvENODEFILECHECK", 'C.cvEnodefilecheckCode)
204 , ("CvENODEHOOKS", 'C.cvEnodehooksCode)
205 , ("CvENODEHV", 'C.cvEnodehvCode)
206 , ("CvENODELVM", 'C.cvEnodelvmCode)
207 , ("CvENODEN1", 'C.cvEnoden1Code)
208 , ("CvENODENET", 'C.cvEnodenetCode)
209 , ("CvENODEOS", 'C.cvEnodeosCode)
210 , ("CvENODEORPHANINSTANCE", 'C.cvEnodeorphaninstanceCode)
211 , ("CvENODEORPHANLV", 'C.cvEnodeorphanlvCode)
212 , ("CvENODERPC", 'C.cvEnoderpcCode)
213 , ("CvENODESSH", 'C.cvEnodesshCode)
214 , ("CvENODEVERSION", 'C.cvEnodeversionCode)
215 , ("CvENODESETUP", 'C.cvEnodesetupCode)
216 , ("CvENODETIME", 'C.cvEnodetimeCode)
217 , ("CvENODEOOBPATH", 'C.cvEnodeoobpathCode)
218 , ("CvENODEUSERSCRIPTS", 'C.cvEnodeuserscriptsCode)
219 , ("CvENODEFILESTORAGEPATHS", 'C.cvEnodefilestoragepathsCode)
221 $(THH.makeJSONInstance ''CVErrorCode)
223 -- | Dynamic device modification, just add\/remove version.
224 $(THH.declareSADT "DdmSimple"
225 [ ("DdmSimpleAdd", 'C.ddmAdd)
226 , ("DdmSimpleRemove", 'C.ddmRemove)
228 $(THH.makeJSONInstance ''DdmSimple)
230 -- | Dynamic device modification, all operations version.
231 $(THH.declareSADT "DdmFull"
232 [ ("DdmFullAdd", 'C.ddmAdd)
233 , ("DdmFullRemove", 'C.ddmRemove)
234 , ("DdmFullModify", 'C.ddmModify)
236 $(THH.makeJSONInstance ''DdmFull)
238 -- | Hypervisor type definitions.
239 $(THH.declareSADT "Hypervisor"
240 [ ( "Kvm", 'C.htKvm )
241 , ( "XenPvm", 'C.htXenPvm )
242 , ( "Chroot", 'C.htChroot )
243 , ( "XenHvm", 'C.htXenHvm )
244 , ( "Lxc", 'C.htLxc )
245 , ( "Fake", 'C.htFake )
247 $(THH.makeJSONInstance ''Hypervisor)
249 -- | Oob command type.
250 $(THH.declareSADT "OobCommand"
251 [ ("OobHealth", 'C.oobHealth)
252 , ("OobPowerCycle", 'C.oobPowerCycle)
253 , ("OobPowerOff", 'C.oobPowerOff)
254 , ("OobPowerOn", 'C.oobPowerOn)
255 , ("OobPowerStatus", 'C.oobPowerStatus)
257 $(THH.makeJSONInstance ''OobCommand)
260 $(THH.declareSADT "StorageType"
261 [ ("StorageFile", 'C.stFile)
262 , ("StorageLvmPv", 'C.stLvmPv)
263 , ("StorageLvmVg", 'C.stLvmVg)
265 $(THH.makeJSONInstance ''StorageType)
267 -- | Node evac modes.
268 $(THH.declareSADT "NodeEvacMode"
269 [ ("NEvacPrimary", 'C.iallocatorNevacPri)
270 , ("NEvacSecondary", 'C.iallocatorNevacSec)
271 , ("NEvacAll", 'C.iallocatorNevacAll)
273 $(THH.makeJSONInstance ''NodeEvacMode)
275 -- | The file driver type.
276 $(THH.declareSADT "FileDriver"
277 [ ("FileLoop", 'C.fdLoop)
278 , ("FileBlktap", 'C.fdBlktap)
280 $(THH.makeJSONInstance ''FileDriver)
282 -- | The instance create mode.
283 $(THH.declareSADT "InstCreateMode"
284 [ ("InstCreate", 'C.instanceCreate)
285 , ("InstImport", 'C.instanceImport)
286 , ("InstRemoteImport", 'C.instanceRemoteImport)
288 $(THH.makeJSONInstance ''InstCreateMode)
291 $(THH.declareSADT "RebootType"
292 [ ("RebootSoft", 'C.instanceRebootSoft)
293 , ("RebootHard", 'C.instanceRebootHard)
294 , ("RebootFull", 'C.instanceRebootFull)
296 $(THH.makeJSONInstance ''RebootType)
299 $(THH.declareSADT "ExportMode"
300 [ ("ExportModeLocal", 'C.exportModeLocal)
301 , ("ExportModeRemove", 'C.exportModeRemote)
303 $(THH.makeJSONInstance ''ExportMode)
305 -- | IAllocator run types (OpTestIAllocator).
306 $(THH.declareSADT "IAllocatorTestDir"
307 [ ("IAllocatorDirIn", 'C.iallocatorDirIn)
308 , ("IAllocatorDirOut", 'C.iallocatorDirOut)
310 $(THH.makeJSONInstance ''IAllocatorTestDir)
312 -- | IAllocator mode. FIXME: use this in "HTools.Backend.IAlloc".
313 $(THH.declareSADT "IAllocatorMode"
314 [ ("IAllocatorAlloc", 'C.iallocatorModeAlloc)
315 , ("IAllocatorMultiAlloc", 'C.iallocatorModeMultiAlloc)
316 , ("IAllocatorReloc", 'C.iallocatorModeReloc)
317 , ("IAllocatorNodeEvac", 'C.iallocatorModeNodeEvac)
318 , ("IAllocatorChangeGroup", 'C.iallocatorModeChgGroup)
320 $(THH.makeJSONInstance ''IAllocatorMode)
323 $(THH.declareSADT "NetworkType"
324 [ ("PrivateNetwork", 'C.networkTypePrivate)
325 , ("PublicNetwork", 'C.networkTypePublic)
327 $(THH.makeJSONInstance ''NetworkType)
330 $(THH.declareSADT "NICMode"
331 [ ("NMBridged", 'C.nicModeBridged)
332 , ("NMRouted", 'C.nicModeRouted)
334 $(THH.makeJSONInstance ''NICMode)