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
57 , VerifyOptionalChecks(..)
70 , IAllocatorTestDir(..)
80 , FinalizedJobStatus(..)
81 , finalizedJobStatusToRaw
89 , OpSubmitPriority(..)
90 , opSubmitPriorityToRaw
97 import Control.Monad (liftM)
98 import qualified Text.JSON as JSON
99 import Text.JSON (JSON, readJSON, showJSON)
100 import Data.Ratio (numerator, denominator)
102 import qualified Ganeti.Constants as C
103 import qualified Ganeti.THH as THH
109 -- | Type that holds a non-negative value.
110 newtype NonNegative a = NonNegative { fromNonNegative :: a }
113 -- | Smart constructor for 'NonNegative'.
114 mkNonNegative :: (Monad m, Num a, Ord a, Show a) => a -> m (NonNegative a)
115 mkNonNegative i | i >= 0 = return (NonNegative i)
116 | otherwise = fail $ "Invalid value for non-negative type '" ++
119 instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (NonNegative a) where
120 showJSON = JSON.showJSON . fromNonNegative
121 readJSON v = JSON.readJSON v >>= mkNonNegative
123 -- | Type that holds a positive value.
124 newtype Positive a = Positive { fromPositive :: a }
127 -- | Smart constructor for 'Positive'.
128 mkPositive :: (Monad m, Num a, Ord a, Show a) => a -> m (Positive a)
129 mkPositive i | i > 0 = return (Positive i)
130 | otherwise = fail $ "Invalid value for positive type '" ++
133 instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (Positive a) where
134 showJSON = JSON.showJSON . fromPositive
135 readJSON v = JSON.readJSON v >>= mkPositive
137 -- | Type that holds a negative value.
138 newtype Negative a = Negative { fromNegative :: a }
141 -- | Smart constructor for 'Negative'.
142 mkNegative :: (Monad m, Num a, Ord a, Show a) => a -> m (Negative a)
143 mkNegative i | i < 0 = return (Negative i)
144 | otherwise = fail $ "Invalid value for negative type '" ++
147 instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (Negative a) where
148 showJSON = JSON.showJSON . fromNegative
149 readJSON v = JSON.readJSON v >>= mkNegative
151 -- | Type that holds a non-null list.
152 newtype NonEmpty a = NonEmpty { fromNonEmpty :: [a] }
155 -- | Smart constructor for 'NonEmpty'.
156 mkNonEmpty :: (Monad m) => [a] -> m (NonEmpty a)
157 mkNonEmpty [] = fail "Received empty value for non-empty list"
158 mkNonEmpty xs = return (NonEmpty xs)
160 instance (JSON.JSON a) => JSON.JSON (NonEmpty a) where
161 showJSON = JSON.showJSON . fromNonEmpty
162 readJSON v = JSON.readJSON v >>= mkNonEmpty
164 -- | A simple type alias for non-empty strings.
165 type NonEmptyString = NonEmpty Char
169 -- | Instance disk template type.
170 $(THH.declareSADT "DiskTemplate"
171 [ ("DTDiskless", 'C.dtDiskless)
172 , ("DTFile", 'C.dtFile)
173 , ("DTSharedFile", 'C.dtSharedFile)
174 , ("DTPlain", 'C.dtPlain)
175 , ("DTBlock", 'C.dtBlock)
176 , ("DTDrbd8", 'C.dtDrbd8)
177 , ("DTRbd", 'C.dtRbd)
178 , ("DTExt", 'C.dtExt)
180 $(THH.makeJSONInstance ''DiskTemplate)
182 instance HasStringRepr DiskTemplate where
183 fromStringRepr = diskTemplateFromRaw
184 toStringRepr = diskTemplateToRaw
186 -- | The Group allocation policy type.
188 -- Note that the order of constructors is important as the automatic
189 -- Ord instance will order them in the order they are defined, so when
190 -- changing this data type be careful about the interaction with the
191 -- desired sorting order.
192 $(THH.declareSADT "AllocPolicy"
193 [ ("AllocPreferred", 'C.allocPolicyPreferred)
194 , ("AllocLastResort", 'C.allocPolicyLastResort)
195 , ("AllocUnallocable", 'C.allocPolicyUnallocable)
197 $(THH.makeJSONInstance ''AllocPolicy)
199 -- | The Instance real state type. FIXME: this could be improved to
200 -- just wrap a /NormalState AdminStatus | ErrorState ErrorCondition/.
201 $(THH.declareSADT "InstanceStatus"
202 [ ("StatusDown", 'C.inststAdmindown)
203 , ("StatusOffline", 'C.inststAdminoffline)
204 , ("ErrorDown", 'C.inststErrordown)
205 , ("ErrorUp", 'C.inststErrorup)
206 , ("NodeDown", 'C.inststNodedown)
207 , ("NodeOffline", 'C.inststNodeoffline)
208 , ("Running", 'C.inststRunning)
209 , ("WrongNode", 'C.inststWrongnode)
211 $(THH.makeJSONInstance ''InstanceStatus)
214 $(THH.declareSADT "MigrationMode"
215 [ ("MigrationLive", 'C.htMigrationLive)
216 , ("MigrationNonLive", 'C.htMigrationNonlive)
218 $(THH.makeJSONInstance ''MigrationMode)
220 -- | Verify optional checks.
221 $(THH.declareSADT "VerifyOptionalChecks"
222 [ ("VerifyNPlusOneMem", 'C.verifyNplusoneMem)
224 $(THH.makeJSONInstance ''VerifyOptionalChecks)
226 -- | Cluster verify error codes.
227 $(THH.declareSADT "CVErrorCode"
228 [ ("CvECLUSTERCFG", 'C.cvEclustercfgCode)
229 , ("CvECLUSTERCERT", 'C.cvEclustercertCode)
230 , ("CvECLUSTERFILECHECK", 'C.cvEclusterfilecheckCode)
231 , ("CvECLUSTERDANGLINGNODES", 'C.cvEclusterdanglingnodesCode)
232 , ("CvECLUSTERDANGLINGINST", 'C.cvEclusterdanglinginstCode)
233 , ("CvEINSTANCEBADNODE", 'C.cvEinstancebadnodeCode)
234 , ("CvEINSTANCEDOWN", 'C.cvEinstancedownCode)
235 , ("CvEINSTANCELAYOUT", 'C.cvEinstancelayoutCode)
236 , ("CvEINSTANCEMISSINGDISK", 'C.cvEinstancemissingdiskCode)
237 , ("CvEINSTANCEFAULTYDISK", 'C.cvEinstancefaultydiskCode)
238 , ("CvEINSTANCEWRONGNODE", 'C.cvEinstancewrongnodeCode)
239 , ("CvEINSTANCESPLITGROUPS", 'C.cvEinstancesplitgroupsCode)
240 , ("CvEINSTANCEPOLICY", 'C.cvEinstancepolicyCode)
241 , ("CvENODEDRBD", 'C.cvEnodedrbdCode)
242 , ("CvENODEDRBDHELPER", 'C.cvEnodedrbdhelperCode)
243 , ("CvENODEFILECHECK", 'C.cvEnodefilecheckCode)
244 , ("CvENODEHOOKS", 'C.cvEnodehooksCode)
245 , ("CvENODEHV", 'C.cvEnodehvCode)
246 , ("CvENODELVM", 'C.cvEnodelvmCode)
247 , ("CvENODEN1", 'C.cvEnoden1Code)
248 , ("CvENODENET", 'C.cvEnodenetCode)
249 , ("CvENODEOS", 'C.cvEnodeosCode)
250 , ("CvENODEORPHANINSTANCE", 'C.cvEnodeorphaninstanceCode)
251 , ("CvENODEORPHANLV", 'C.cvEnodeorphanlvCode)
252 , ("CvENODERPC", 'C.cvEnoderpcCode)
253 , ("CvENODESSH", 'C.cvEnodesshCode)
254 , ("CvENODEVERSION", 'C.cvEnodeversionCode)
255 , ("CvENODESETUP", 'C.cvEnodesetupCode)
256 , ("CvENODETIME", 'C.cvEnodetimeCode)
257 , ("CvENODEOOBPATH", 'C.cvEnodeoobpathCode)
258 , ("CvENODEUSERSCRIPTS", 'C.cvEnodeuserscriptsCode)
259 , ("CvENODEFILESTORAGEPATHS", 'C.cvEnodefilestoragepathsCode)
261 $(THH.makeJSONInstance ''CVErrorCode)
263 -- | Dynamic device modification, just add\/remove version.
264 $(THH.declareSADT "DdmSimple"
265 [ ("DdmSimpleAdd", 'C.ddmAdd)
266 , ("DdmSimpleRemove", 'C.ddmRemove)
268 $(THH.makeJSONInstance ''DdmSimple)
270 -- | Dynamic device modification, all operations version.
271 $(THH.declareSADT "DdmFull"
272 [ ("DdmFullAdd", 'C.ddmAdd)
273 , ("DdmFullRemove", 'C.ddmRemove)
274 , ("DdmFullModify", 'C.ddmModify)
276 $(THH.makeJSONInstance ''DdmFull)
278 -- | Hypervisor type definitions.
279 $(THH.declareSADT "Hypervisor"
280 [ ( "Kvm", 'C.htKvm )
281 , ( "XenPvm", 'C.htXenPvm )
282 , ( "Chroot", 'C.htChroot )
283 , ( "XenHvm", 'C.htXenHvm )
284 , ( "Lxc", 'C.htLxc )
285 , ( "Fake", 'C.htFake )
287 $(THH.makeJSONInstance ''Hypervisor)
289 -- | Oob command type.
290 $(THH.declareSADT "OobCommand"
291 [ ("OobHealth", 'C.oobHealth)
292 , ("OobPowerCycle", 'C.oobPowerCycle)
293 , ("OobPowerOff", 'C.oobPowerOff)
294 , ("OobPowerOn", 'C.oobPowerOn)
295 , ("OobPowerStatus", 'C.oobPowerStatus)
297 $(THH.makeJSONInstance ''OobCommand)
300 $(THH.declareSADT "StorageType"
301 [ ("StorageFile", 'C.stFile)
302 , ("StorageLvmPv", 'C.stLvmPv)
303 , ("StorageLvmVg", 'C.stLvmVg)
305 $(THH.makeJSONInstance ''StorageType)
307 -- | Node evac modes.
308 $(THH.declareSADT "NodeEvacMode"
309 [ ("NEvacPrimary", 'C.iallocatorNevacPri)
310 , ("NEvacSecondary", 'C.iallocatorNevacSec)
311 , ("NEvacAll", 'C.iallocatorNevacAll)
313 $(THH.makeJSONInstance ''NodeEvacMode)
315 -- | The file driver type.
316 $(THH.declareSADT "FileDriver"
317 [ ("FileLoop", 'C.fdLoop)
318 , ("FileBlktap", 'C.fdBlktap)
320 $(THH.makeJSONInstance ''FileDriver)
322 -- | The instance create mode.
323 $(THH.declareSADT "InstCreateMode"
324 [ ("InstCreate", 'C.instanceCreate)
325 , ("InstImport", 'C.instanceImport)
326 , ("InstRemoteImport", 'C.instanceRemoteImport)
328 $(THH.makeJSONInstance ''InstCreateMode)
331 $(THH.declareSADT "RebootType"
332 [ ("RebootSoft", 'C.instanceRebootSoft)
333 , ("RebootHard", 'C.instanceRebootHard)
334 , ("RebootFull", 'C.instanceRebootFull)
336 $(THH.makeJSONInstance ''RebootType)
339 $(THH.declareSADT "ExportMode"
340 [ ("ExportModeLocal", 'C.exportModeLocal)
341 , ("ExportModeRemove", 'C.exportModeRemote)
343 $(THH.makeJSONInstance ''ExportMode)
345 -- | IAllocator run types (OpTestIAllocator).
346 $(THH.declareSADT "IAllocatorTestDir"
347 [ ("IAllocatorDirIn", 'C.iallocatorDirIn)
348 , ("IAllocatorDirOut", 'C.iallocatorDirOut)
350 $(THH.makeJSONInstance ''IAllocatorTestDir)
352 -- | IAllocator mode. FIXME: use this in "HTools.Backend.IAlloc".
353 $(THH.declareSADT "IAllocatorMode"
354 [ ("IAllocatorAlloc", 'C.iallocatorModeAlloc)
355 , ("IAllocatorMultiAlloc", 'C.iallocatorModeMultiAlloc)
356 , ("IAllocatorReloc", 'C.iallocatorModeReloc)
357 , ("IAllocatorNodeEvac", 'C.iallocatorModeNodeEvac)
358 , ("IAllocatorChangeGroup", 'C.iallocatorModeChgGroup)
360 $(THH.makeJSONInstance ''IAllocatorMode)
363 $(THH.declareSADT "NetworkType"
364 [ ("PrivateNetwork", 'C.networkTypePrivate)
365 , ("PublicNetwork", 'C.networkTypePublic)
367 $(THH.makeJSONInstance ''NetworkType)
370 $(THH.declareSADT "NICMode"
371 [ ("NMBridged", 'C.nicModeBridged)
372 , ("NMRouted", 'C.nicModeRouted)
373 , ("NMOvs", 'C.nicModeOvs)
375 $(THH.makeJSONInstance ''NICMode)
377 -- | The JobStatus data type. Note that this is ordered especially
378 -- such that greater\/lesser comparison on values of this type makes
380 $(THH.declareSADT "JobStatus"
381 [ ("JOB_STATUS_QUEUED", 'C.jobStatusQueued)
382 , ("JOB_STATUS_WAITING", 'C.jobStatusWaiting)
383 , ("JOB_STATUS_CANCELING", 'C.jobStatusCanceling)
384 , ("JOB_STATUS_RUNNING", 'C.jobStatusRunning)
385 , ("JOB_STATUS_CANCELED", 'C.jobStatusCanceled)
386 , ("JOB_STATUS_SUCCESS", 'C.jobStatusSuccess)
387 , ("JOB_STATUS_ERROR", 'C.jobStatusError)
389 $(THH.makeJSONInstance ''JobStatus)
391 -- | Finalized job status.
392 $(THH.declareSADT "FinalizedJobStatus"
393 [ ("JobStatusCanceled", 'C.jobStatusCanceled)
394 , ("JobStatusSuccessful", 'C.jobStatusSuccess)
395 , ("JobStatusFailed", 'C.jobStatusError)
397 $(THH.makeJSONInstance ''FinalizedJobStatus)
399 -- | The Ganeti job type.
400 newtype JobId = JobId { fromJobId :: Int }
403 -- | Builds a job ID.
404 makeJobId :: (Monad m) => Int -> m JobId
405 makeJobId i | i >= 0 = return $ JobId i
406 | otherwise = fail $ "Invalid value for job ID ' " ++ show i ++ "'"
408 -- | Builds a job ID from a string.
409 makeJobIdS :: (Monad m) => String -> m JobId
410 makeJobIdS s = tryRead "parsing job id" s >>= makeJobId
412 -- | Parses a job ID.
413 parseJobId :: (Monad m) => JSON.JSValue -> m JobId
414 parseJobId (JSON.JSString x) = makeJobIdS $ JSON.fromJSString x
415 parseJobId (JSON.JSRational _ x) =
416 if denominator x /= 1
417 then fail $ "Got fractional job ID from master daemon?! Value:" ++ show x
418 -- FIXME: potential integer overflow here on 32-bit platforms
419 else makeJobId . fromIntegral . numerator $ x
420 parseJobId x = fail $ "Wrong type/value for job id: " ++ show x
422 instance JSON.JSON JobId where
423 showJSON = JSON.showJSON . fromJobId
424 readJSON = parseJobId
426 -- | Relative job ID type alias.
427 type RelativeJobId = Negative Int
429 -- | Job ID dependency.
430 data JobIdDep = JobDepRelative RelativeJobId
431 | JobDepAbsolute JobId
434 instance JSON.JSON JobIdDep where
435 showJSON (JobDepRelative i) = showJSON i
436 showJSON (JobDepAbsolute i) = showJSON i
438 case JSON.readJSON v::JSON.Result (Negative Int) of
439 -- first try relative dependency, usually most common
440 JSON.Ok r -> return $ JobDepRelative r
441 JSON.Error _ -> liftM JobDepAbsolute (parseJobId v)
443 -- | Job Dependency type.
444 data JobDependency = JobDependency JobIdDep [FinalizedJobStatus]
447 instance JSON JobDependency where
448 showJSON (JobDependency dep status) = showJSON (dep, status)
449 readJSON = liftM (uncurry JobDependency) . readJSON
451 -- | Valid opcode priorities for submit.
452 $(THH.declareIADT "OpSubmitPriority"
453 [ ("OpPrioLow", 'C.opPrioLow)
454 , ("OpPrioNormal", 'C.opPrioNormal)
455 , ("OpPrioHigh", 'C.opPrioHigh)
457 $(THH.makeJSONInstance ''OpSubmitPriority)
459 -- | Our ADT for the OpCode status at runtime (while in a job).
460 $(THH.declareSADT "OpStatus"
461 [ ("OP_STATUS_QUEUED", 'C.opStatusQueued)
462 , ("OP_STATUS_WAITING", 'C.opStatusWaiting)
463 , ("OP_STATUS_CANCELING", 'C.opStatusCanceling)
464 , ("OP_STATUS_RUNNING", 'C.opStatusRunning)
465 , ("OP_STATUS_CANCELED", 'C.opStatusCanceled)
466 , ("OP_STATUS_SUCCESS", 'C.opStatusSuccess)
467 , ("OP_STATUS_ERROR", 'C.opStatusError)
469 $(THH.makeJSONInstance ''OpStatus)
471 -- | Type for the job message type.
472 $(THH.declareSADT "ELogType"
473 [ ("ELogMessage", 'C.elogMessage)
474 , ("ELogRemoteImport", 'C.elogRemoteImport)
475 , ("ELogJqueueTest", 'C.elogJqueueTest)
477 $(THH.makeJSONInstance ''ELogType)