Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Types.hs @ 3673a326

History | View | Annotate | Download (15.3 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| Some common Ganeti types.
4

    
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'.
9

    
10
-}
11

    
12
{-
13

    
14
Copyright (C) 2012 Google Inc.
15

    
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.
20

    
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.
25

    
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
29
02110-1301, USA.
30

    
31
-}
32

    
33
module Ganeti.Types
34
  ( AllocPolicy(..)
35
  , allocPolicyFromRaw
36
  , allocPolicyToRaw
37
  , InstanceStatus(..)
38
  , instanceStatusFromRaw
39
  , instanceStatusToRaw
40
  , DiskTemplate(..)
41
  , diskTemplateToRaw
42
  , diskTemplateFromRaw
43
  , NonNegative
44
  , fromNonNegative
45
  , mkNonNegative
46
  , Positive
47
  , fromPositive
48
  , mkPositive
49
  , Negative
50
  , fromNegative
51
  , mkNegative
52
  , NonEmpty
53
  , fromNonEmpty
54
  , mkNonEmpty
55
  , NonEmptyString
56
  , MigrationMode(..)
57
  , VerifyOptionalChecks(..)
58
  , DdmSimple(..)
59
  , DdmFull(..)
60
  , CVErrorCode(..)
61
  , cVErrorCodeToRaw
62
  , Hypervisor(..)
63
  , OobCommand(..)
64
  , StorageType(..)
65
  , NodeEvacMode(..)
66
  , FileDriver(..)
67
  , InstCreateMode(..)
68
  , RebootType(..)
69
  , ExportMode(..)
70
  , IAllocatorTestDir(..)
71
  , IAllocatorMode(..)
72
  , iAllocatorModeToRaw
73
  , NetworkType(..)
74
  , networkTypeToRaw
75
  , NICMode(..)
76
  , nICModeToRaw
77
  , JobStatus(..)
78
  , jobStatusToRaw
79
  , jobStatusFromRaw
80
  , FinalizedJobStatus(..)
81
  , finalizedJobStatusToRaw
82
  , JobId
83
  , fromJobId
84
  , makeJobId
85
  , makeJobIdS
86
  , RelativeJobId
87
  , JobIdDep(..)
88
  , JobDependency(..)
89
  , OpSubmitPriority(..)
90
  , opSubmitPriorityToRaw
91
  , OpStatus(..)
92
  , opStatusToRaw
93
  , opStatusFromRaw
94
  , ELogType(..)
95
  ) where
96

    
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)
101

    
102
import qualified Ganeti.Constants as C
103
import qualified Ganeti.THH as THH
104
import Ganeti.JSON
105
import Ganeti.Utils
106

    
107
-- * Generic types
108

    
109
-- | Type that holds a non-negative value.
110
newtype NonNegative a = NonNegative { fromNonNegative :: a }
111
  deriving (Show, Eq)
112

    
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 '" ++
117
                              show i ++ "'"
118

    
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
122

    
123
-- | Type that holds a positive value.
124
newtype Positive a = Positive { fromPositive :: a }
125
  deriving (Show, Eq)
126

    
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 '" ++
131
                           show i ++ "'"
132

    
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
136

    
137
-- | Type that holds a negative value.
138
newtype Negative a = Negative { fromNegative :: a }
139
  deriving (Show, Eq)
140

    
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 '" ++
145
                           show i ++ "'"
146

    
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
150

    
151
-- | Type that holds a non-null list.
152
newtype NonEmpty a = NonEmpty { fromNonEmpty :: [a] }
153
  deriving (Show, Eq)
154

    
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)
159

    
160
instance (JSON.JSON a) => JSON.JSON (NonEmpty a) where
161
  showJSON = JSON.showJSON . fromNonEmpty
162
  readJSON v = JSON.readJSON v >>= mkNonEmpty
163

    
164
-- | A simple type alias for non-empty strings.
165
type NonEmptyString = NonEmpty Char
166

    
167
-- * Ganeti types
168

    
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)
179
       ])
180
$(THH.makeJSONInstance ''DiskTemplate)
181

    
182
instance HasStringRepr DiskTemplate where
183
  fromStringRepr = diskTemplateFromRaw
184
  toStringRepr = diskTemplateToRaw
185

    
186
-- | The Group allocation policy type.
187
--
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)
196
       ])
197
$(THH.makeJSONInstance ''AllocPolicy)
198

    
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)
210
       ])
211
$(THH.makeJSONInstance ''InstanceStatus)
212

    
213
-- | Migration mode.
214
$(THH.declareSADT "MigrationMode"
215
     [ ("MigrationLive",    'C.htMigrationLive)
216
     , ("MigrationNonLive", 'C.htMigrationNonlive)
217
     ])
218
$(THH.makeJSONInstance ''MigrationMode)
219

    
220
-- | Verify optional checks.
221
$(THH.declareSADT "VerifyOptionalChecks"
222
     [ ("VerifyNPlusOneMem", 'C.verifyNplusoneMem)
223
     ])
224
$(THH.makeJSONInstance ''VerifyOptionalChecks)
225

    
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)
260
  ])
261
$(THH.makeJSONInstance ''CVErrorCode)
262

    
263
-- | Dynamic device modification, just add\/remove version.
264
$(THH.declareSADT "DdmSimple"
265
     [ ("DdmSimpleAdd",    'C.ddmAdd)
266
     , ("DdmSimpleRemove", 'C.ddmRemove)
267
     ])
268
$(THH.makeJSONInstance ''DdmSimple)
269

    
270
-- | Dynamic device modification, all operations version.
271
$(THH.declareSADT "DdmFull"
272
     [ ("DdmFullAdd",    'C.ddmAdd)
273
     , ("DdmFullRemove", 'C.ddmRemove)
274
     , ("DdmFullModify", 'C.ddmModify)
275
     ])
276
$(THH.makeJSONInstance ''DdmFull)
277

    
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 )
286
  ])
287
$(THH.makeJSONInstance ''Hypervisor)
288

    
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)
296
  ])
297
$(THH.makeJSONInstance ''OobCommand)
298

    
299
-- | Storage type.
300
$(THH.declareSADT "StorageType"
301
  [ ("StorageFile", 'C.stFile)
302
  , ("StorageLvmPv", 'C.stLvmPv)
303
  , ("StorageLvmVg", 'C.stLvmVg)
304
  ])
305
$(THH.makeJSONInstance ''StorageType)
306

    
307
-- | Node evac modes.
308
$(THH.declareSADT "NodeEvacMode"
309
  [ ("NEvacPrimary",   'C.iallocatorNevacPri)
310
  , ("NEvacSecondary", 'C.iallocatorNevacSec)
311
  , ("NEvacAll",       'C.iallocatorNevacAll)
312
  ])
313
$(THH.makeJSONInstance ''NodeEvacMode)
314

    
315
-- | The file driver type.
316
$(THH.declareSADT "FileDriver"
317
  [ ("FileLoop",   'C.fdLoop)
318
  , ("FileBlktap", 'C.fdBlktap)
319
  ])
320
$(THH.makeJSONInstance ''FileDriver)
321

    
322
-- | The instance create mode.
323
$(THH.declareSADT "InstCreateMode"
324
  [ ("InstCreate",       'C.instanceCreate)
325
  , ("InstImport",       'C.instanceImport)
326
  , ("InstRemoteImport", 'C.instanceRemoteImport)
327
  ])
328
$(THH.makeJSONInstance ''InstCreateMode)
329

    
330
-- | Reboot type.
331
$(THH.declareSADT "RebootType"
332
  [ ("RebootSoft", 'C.instanceRebootSoft)
333
  , ("RebootHard", 'C.instanceRebootHard)
334
  , ("RebootFull", 'C.instanceRebootFull)
335
  ])
336
$(THH.makeJSONInstance ''RebootType)
337

    
338
-- | Export modes.
339
$(THH.declareSADT "ExportMode"
340
  [ ("ExportModeLocal",  'C.exportModeLocal)
341
  , ("ExportModeRemove", 'C.exportModeRemote)
342
  ])
343
$(THH.makeJSONInstance ''ExportMode)
344

    
345
-- | IAllocator run types (OpTestIAllocator).
346
$(THH.declareSADT "IAllocatorTestDir"
347
  [ ("IAllocatorDirIn",  'C.iallocatorDirIn)
348
  , ("IAllocatorDirOut", 'C.iallocatorDirOut)
349
  ])
350
$(THH.makeJSONInstance ''IAllocatorTestDir)
351

    
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)
359
  ])
360
$(THH.makeJSONInstance ''IAllocatorMode)
361

    
362
-- | Network type.
363
$(THH.declareSADT "NetworkType"
364
  [ ("PrivateNetwork", 'C.networkTypePrivate)
365
  , ("PublicNetwork",  'C.networkTypePublic)
366
  ])
367
$(THH.makeJSONInstance ''NetworkType)
368

    
369
-- | Network mode.
370
$(THH.declareSADT "NICMode"
371
  [ ("NMBridged", 'C.nicModeBridged)
372
  , ("NMRouted",  'C.nicModeRouted)
373
  , ("NMOvs",     'C.nicModeOvs)
374
  ])
375
$(THH.makeJSONInstance ''NICMode)
376

    
377
-- | The JobStatus data type. Note that this is ordered especially
378
-- such that greater\/lesser comparison on values of this type makes
379
-- sense.
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)
388
       ])
389
$(THH.makeJSONInstance ''JobStatus)
390

    
391
-- | Finalized job status.
392
$(THH.declareSADT "FinalizedJobStatus"
393
  [ ("JobStatusCanceled",   'C.jobStatusCanceled)
394
  , ("JobStatusSuccessful", 'C.jobStatusSuccess)
395
  , ("JobStatusFailed",     'C.jobStatusError)
396
  ])
397
$(THH.makeJSONInstance ''FinalizedJobStatus)
398

    
399
-- | The Ganeti job type.
400
newtype JobId = JobId { fromJobId :: Int }
401
  deriving (Show, Eq)
402

    
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 ++ "'"
407

    
408
-- | Builds a job ID from a string.
409
makeJobIdS :: (Monad m) => String -> m JobId
410
makeJobIdS s = tryRead "parsing job id" s >>= makeJobId
411

    
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
421

    
422
instance JSON.JSON JobId where
423
  showJSON = JSON.showJSON . fromJobId
424
  readJSON = parseJobId
425

    
426
-- | Relative job ID type alias.
427
type RelativeJobId = Negative Int
428

    
429
-- | Job ID dependency.
430
data JobIdDep = JobDepRelative RelativeJobId
431
              | JobDepAbsolute JobId
432
                deriving (Show, Eq)
433

    
434
instance JSON.JSON JobIdDep where
435
  showJSON (JobDepRelative i) = showJSON i
436
  showJSON (JobDepAbsolute i) = showJSON i
437
  readJSON v =
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)
442

    
443
-- | Job Dependency type.
444
data JobDependency = JobDependency JobIdDep [FinalizedJobStatus]
445
                     deriving (Show, Eq)
446

    
447
instance JSON JobDependency where
448
  showJSON (JobDependency dep status) = showJSON (dep, status)
449
  readJSON = liftM (uncurry JobDependency) . readJSON
450

    
451
-- | Valid opcode priorities for submit.
452
$(THH.declareIADT "OpSubmitPriority"
453
  [ ("OpPrioLow",    'C.opPrioLow)
454
  , ("OpPrioNormal", 'C.opPrioNormal)
455
  , ("OpPrioHigh",   'C.opPrioHigh)
456
  ])
457
$(THH.makeJSONInstance ''OpSubmitPriority)
458

    
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)
468
  ])
469
$(THH.makeJSONInstance ''OpStatus)
470

    
471
-- | Type for the job message type.
472
$(THH.declareSADT "ELogType"
473
  [ ("ELogMessage",      'C.elogMessage)
474
  , ("ELogRemoteImport", 'C.elogRemoteImport)
475
  , ("ELogJqueueTest",   'C.elogJqueueTest)
476
  ])
477
$(THH.makeJSONInstance ''ELogType)