Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Types.hs @ 5cd95d46

History | View | Annotate | Download (15.2 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
  , RelativeJobId
86
  , JobIdDep(..)
87
  , JobDependency(..)
88
  , OpSubmitPriority(..)
89
  , OpStatus(..)
90
  , opStatusToRaw
91
  , opStatusFromRaw
92
  , ELogType(..)
93
  ) where
94

    
95
import Control.Monad (liftM)
96
import qualified Text.JSON as JSON
97
import Text.JSON (JSON, readJSON, showJSON)
98
import Data.Ratio (numerator, denominator)
99

    
100
import qualified Ganeti.Constants as C
101
import qualified Ganeti.THH as THH
102
import Ganeti.JSON
103
import Ganeti.Utils
104

    
105
-- * Generic types
106

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

    
111
-- | Smart constructor for 'NonNegative'.
112
mkNonNegative :: (Monad m, Num a, Ord a, Show a) => a -> m (NonNegative a)
113
mkNonNegative i | i >= 0 = return (NonNegative i)
114
                | otherwise = fail $ "Invalid value for non-negative type '" ++
115
                              show i ++ "'"
116

    
117
instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (NonNegative a) where
118
  showJSON = JSON.showJSON . fromNonNegative
119
  readJSON v = JSON.readJSON v >>= mkNonNegative
120

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

    
125
-- | Smart constructor for 'Positive'.
126
mkPositive :: (Monad m, Num a, Ord a, Show a) => a -> m (Positive a)
127
mkPositive i | i > 0 = return (Positive i)
128
             | otherwise = fail $ "Invalid value for positive type '" ++
129
                           show i ++ "'"
130

    
131
instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (Positive a) where
132
  showJSON = JSON.showJSON . fromPositive
133
  readJSON v = JSON.readJSON v >>= mkPositive
134

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

    
139
-- | Smart constructor for 'Negative'.
140
mkNegative :: (Monad m, Num a, Ord a, Show a) => a -> m (Negative a)
141
mkNegative i | i < 0 = return (Negative i)
142
             | otherwise = fail $ "Invalid value for negative type '" ++
143
                           show i ++ "'"
144

    
145
instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (Negative a) where
146
  showJSON = JSON.showJSON . fromNegative
147
  readJSON v = JSON.readJSON v >>= mkNegative
148

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

    
153
-- | Smart constructor for 'NonEmpty'.
154
mkNonEmpty :: (Monad m) => [a] -> m (NonEmpty a)
155
mkNonEmpty [] = fail "Received empty value for non-empty list"
156
mkNonEmpty xs = return (NonEmpty xs)
157

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

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

    
165
-- * Ganeti types
166

    
167
-- | Instance disk template type.
168
$(THH.declareSADT "DiskTemplate"
169
       [ ("DTDiskless",   'C.dtDiskless)
170
       , ("DTFile",       'C.dtFile)
171
       , ("DTSharedFile", 'C.dtSharedFile)
172
       , ("DTPlain",      'C.dtPlain)
173
       , ("DTBlock",      'C.dtBlock)
174
       , ("DTDrbd8",      'C.dtDrbd8)
175
       , ("DTRbd",        'C.dtRbd)
176
       ])
177
$(THH.makeJSONInstance ''DiskTemplate)
178

    
179
instance HasStringRepr DiskTemplate where
180
  fromStringRepr = diskTemplateFromRaw
181
  toStringRepr = diskTemplateToRaw
182

    
183
-- | The Group allocation policy type.
184
--
185
-- Note that the order of constructors is important as the automatic
186
-- Ord instance will order them in the order they are defined, so when
187
-- changing this data type be careful about the interaction with the
188
-- desired sorting order.
189
$(THH.declareSADT "AllocPolicy"
190
       [ ("AllocPreferred",   'C.allocPolicyPreferred)
191
       , ("AllocLastResort",  'C.allocPolicyLastResort)
192
       , ("AllocUnallocable", 'C.allocPolicyUnallocable)
193
       ])
194
$(THH.makeJSONInstance ''AllocPolicy)
195

    
196
-- | The Instance real state type. FIXME: this could be improved to
197
-- just wrap a /NormalState AdminStatus | ErrorState ErrorCondition/.
198
$(THH.declareSADT "InstanceStatus"
199
       [ ("StatusDown",    'C.inststAdmindown)
200
       , ("StatusOffline", 'C.inststAdminoffline)
201
       , ("ErrorDown",     'C.inststErrordown)
202
       , ("ErrorUp",       'C.inststErrorup)
203
       , ("NodeDown",      'C.inststNodedown)
204
       , ("NodeOffline",   'C.inststNodeoffline)
205
       , ("Running",       'C.inststRunning)
206
       , ("WrongNode",     'C.inststWrongnode)
207
       ])
208
$(THH.makeJSONInstance ''InstanceStatus)
209

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

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

    
223
-- | Cluster verify error codes.
224
$(THH.declareSADT "CVErrorCode"
225
  [ ("CvECLUSTERCFG",           'C.cvEclustercfgCode)
226
  , ("CvECLUSTERCERT",          'C.cvEclustercertCode)
227
  , ("CvECLUSTERFILECHECK",     'C.cvEclusterfilecheckCode)
228
  , ("CvECLUSTERDANGLINGNODES", 'C.cvEclusterdanglingnodesCode)
229
  , ("CvECLUSTERDANGLINGINST",  'C.cvEclusterdanglinginstCode)
230
  , ("CvEINSTANCEBADNODE",      'C.cvEinstancebadnodeCode)
231
  , ("CvEINSTANCEDOWN",         'C.cvEinstancedownCode)
232
  , ("CvEINSTANCELAYOUT",       'C.cvEinstancelayoutCode)
233
  , ("CvEINSTANCEMISSINGDISK",  'C.cvEinstancemissingdiskCode)
234
  , ("CvEINSTANCEFAULTYDISK",   'C.cvEinstancefaultydiskCode)
235
  , ("CvEINSTANCEWRONGNODE",    'C.cvEinstancewrongnodeCode)
236
  , ("CvEINSTANCESPLITGROUPS",  'C.cvEinstancesplitgroupsCode)
237
  , ("CvEINSTANCEPOLICY",       'C.cvEinstancepolicyCode)
238
  , ("CvENODEDRBD",             'C.cvEnodedrbdCode)
239
  , ("CvENODEDRBDHELPER",       'C.cvEnodedrbdhelperCode)
240
  , ("CvENODEFILECHECK",        'C.cvEnodefilecheckCode)
241
  , ("CvENODEHOOKS",            'C.cvEnodehooksCode)
242
  , ("CvENODEHV",               'C.cvEnodehvCode)
243
  , ("CvENODELVM",              'C.cvEnodelvmCode)
244
  , ("CvENODEN1",               'C.cvEnoden1Code)
245
  , ("CvENODENET",              'C.cvEnodenetCode)
246
  , ("CvENODEOS",               'C.cvEnodeosCode)
247
  , ("CvENODEORPHANINSTANCE",   'C.cvEnodeorphaninstanceCode)
248
  , ("CvENODEORPHANLV",         'C.cvEnodeorphanlvCode)
249
  , ("CvENODERPC",              'C.cvEnoderpcCode)
250
  , ("CvENODESSH",              'C.cvEnodesshCode)
251
  , ("CvENODEVERSION",          'C.cvEnodeversionCode)
252
  , ("CvENODESETUP",            'C.cvEnodesetupCode)
253
  , ("CvENODETIME",             'C.cvEnodetimeCode)
254
  , ("CvENODEOOBPATH",          'C.cvEnodeoobpathCode)
255
  , ("CvENODEUSERSCRIPTS",      'C.cvEnodeuserscriptsCode)
256
  , ("CvENODEFILESTORAGEPATHS", 'C.cvEnodefilestoragepathsCode)
257
  ])
258
$(THH.makeJSONInstance ''CVErrorCode)
259

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

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

    
275
-- | Hypervisor type definitions.
276
$(THH.declareSADT "Hypervisor"
277
  [ ( "Kvm",    'C.htKvm )
278
  , ( "XenPvm", 'C.htXenPvm )
279
  , ( "Chroot", 'C.htChroot )
280
  , ( "XenHvm", 'C.htXenHvm )
281
  , ( "Lxc",    'C.htLxc )
282
  , ( "Fake",   'C.htFake )
283
  ])
284
$(THH.makeJSONInstance ''Hypervisor)
285

    
286
-- | Oob command type.
287
$(THH.declareSADT "OobCommand"
288
  [ ("OobHealth",      'C.oobHealth)
289
  , ("OobPowerCycle",  'C.oobPowerCycle)
290
  , ("OobPowerOff",    'C.oobPowerOff)
291
  , ("OobPowerOn",     'C.oobPowerOn)
292
  , ("OobPowerStatus", 'C.oobPowerStatus)
293
  ])
294
$(THH.makeJSONInstance ''OobCommand)
295

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

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

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

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

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

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

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

    
349
-- | IAllocator mode. FIXME: use this in "HTools.Backend.IAlloc".
350
$(THH.declareSADT "IAllocatorMode"
351
  [ ("IAllocatorAlloc",       'C.iallocatorModeAlloc)
352
  , ("IAllocatorMultiAlloc",  'C.iallocatorModeMultiAlloc)
353
  , ("IAllocatorReloc",       'C.iallocatorModeReloc)
354
  , ("IAllocatorNodeEvac",    'C.iallocatorModeNodeEvac)
355
  , ("IAllocatorChangeGroup", 'C.iallocatorModeChgGroup)
356
  ])
357
$(THH.makeJSONInstance ''IAllocatorMode)
358

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

    
366
-- | Netork mode.
367
$(THH.declareSADT "NICMode"
368
  [ ("NMBridged", 'C.nicModeBridged)
369
  , ("NMRouted",  'C.nicModeRouted)
370
  , ("NMOvs",     'C.nicModeOvs)
371
  ])
372
$(THH.makeJSONInstance ''NICMode)
373

    
374
-- | The JobStatus data type. Note that this is ordered especially
375
-- such that greater\/lesser comparison on values of this type makes
376
-- sense.
377
$(THH.declareSADT "JobStatus"
378
       [ ("JOB_STATUS_QUEUED",    'C.jobStatusQueued)
379
       , ("JOB_STATUS_WAITING",   'C.jobStatusWaiting)
380
       , ("JOB_STATUS_CANCELING", 'C.jobStatusCanceling)
381
       , ("JOB_STATUS_RUNNING",   'C.jobStatusRunning)
382
       , ("JOB_STATUS_CANCELED",  'C.jobStatusCanceled)
383
       , ("JOB_STATUS_SUCCESS",   'C.jobStatusSuccess)
384
       , ("JOB_STATUS_ERROR",     'C.jobStatusError)
385
       ])
386
$(THH.makeJSONInstance ''JobStatus)
387

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

    
396
-- | The Ganeti job type.
397
newtype JobId = JobId { fromJobId :: Int }
398
  deriving (Show, Eq)
399

    
400
-- | Builds a job ID.
401
makeJobId :: (Monad m) => Int -> m JobId
402
makeJobId i | i >= 0 = return $ JobId i
403
            | otherwise = fail $ "Invalid value for job ID ' " ++ show i ++ "'"
404

    
405
-- | Parses a job ID.
406
parseJobId :: (Monad m) => JSON.JSValue -> m JobId
407
parseJobId (JSON.JSString x) =
408
  tryRead "parsing job id" (JSON.fromJSString x) >>= makeJobId
409
parseJobId (JSON.JSRational _ x) =
410
  if denominator x /= 1
411
    then fail $ "Got fractional job ID from master daemon?! Value:" ++ show x
412
    -- FIXME: potential integer overflow here on 32-bit platforms
413
    else makeJobId . fromIntegral . numerator $ x
414
parseJobId x = fail $ "Wrong type/value for job id: " ++ show x
415

    
416
instance JSON.JSON JobId where
417
  showJSON = JSON.showJSON . fromJobId
418
  readJSON = parseJobId
419

    
420
-- | Relative job ID type alias.
421
type RelativeJobId = Negative Int
422

    
423
-- | Job ID dependency.
424
data JobIdDep = JobDepRelative RelativeJobId
425
              | JobDepAbsolute JobId
426
                deriving (Show, Eq)
427

    
428
instance JSON.JSON JobIdDep where
429
  showJSON (JobDepRelative i) = showJSON i
430
  showJSON (JobDepAbsolute i) = showJSON i
431
  readJSON v =
432
    case JSON.readJSON v::JSON.Result (Negative Int) of
433
      -- first try relative dependency, usually most common
434
      JSON.Ok r -> return $ JobDepRelative r
435
      JSON.Error _ -> liftM JobDepAbsolute
436
                      (fromJResult "parsing absolute job id" (readJSON v) >>=
437
                       makeJobId)
438

    
439
-- | Job Dependency type.
440
data JobDependency = JobDependency JobIdDep [FinalizedJobStatus]
441
                     deriving (Show, Eq)
442

    
443
instance JSON JobDependency where
444
  showJSON (JobDependency dep status) = showJSON (dep, status)
445
  readJSON = liftM (uncurry JobDependency) . readJSON
446

    
447
-- | Valid opcode priorities for submit.
448
$(THH.declareIADT "OpSubmitPriority"
449
  [ ("OpPrioLow",    'C.opPrioLow)
450
  , ("OpPrioNormal", 'C.opPrioNormal)
451
  , ("OpPrioHigh",   'C.opPrioHigh)
452
  ])
453
$(THH.makeJSONInstance ''OpSubmitPriority)
454

    
455
-- | Our ADT for the OpCode status at runtime (while in a job).
456
$(THH.declareSADT "OpStatus"
457
  [ ("OP_STATUS_QUEUED",    'C.opStatusQueued)
458
  , ("OP_STATUS_WAITING",   'C.opStatusWaiting)
459
  , ("OP_STATUS_CANCELING", 'C.opStatusCanceling)
460
  , ("OP_STATUS_RUNNING",   'C.opStatusRunning)
461
  , ("OP_STATUS_CANCELED",  'C.opStatusCanceled)
462
  , ("OP_STATUS_SUCCESS",   'C.opStatusSuccess)
463
  , ("OP_STATUS_ERROR",     'C.opStatusError)
464
  ])
465
$(THH.makeJSONInstance ''OpStatus)
466

    
467
-- | Type for the job message type.
468
$(THH.declareSADT "ELogType"
469
  [ ("ELogMessage",      'C.elogMessage)
470
  , ("ELogRemoteImport", 'C.elogRemoteImport)
471
  , ("ELogJqueueTest",   'C.elogJqueueTest)
472
  ])
473
$(THH.makeJSONInstance ''ELogType)