Types.hs 15.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
{-# LANGUAGE TemplateHaskell #-}

{-| Some common Ganeti types.

This holds types common to both core work, and to htools. Types that
are very core specific (e.g. configuration objects) should go in
'Ganeti.Objects', while types that are specific to htools in-memory
representation should go into 'Ganeti.HTools.Types'.

-}

{-

14
Copyright (C) 2012, 2013 Google Inc.
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

-}

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

97
import Control.Monad (liftM)
Iustin Pop's avatar
Iustin Pop committed
98
import qualified Text.JSON as JSON
99
import Text.JSON (JSON, readJSON, showJSON)
Iustin Pop's avatar
Iustin Pop committed
100
import Data.Ratio (numerator, denominator)
Iustin Pop's avatar
Iustin Pop committed
101

102
103
import qualified Ganeti.Constants as C
import qualified Ganeti.THH as THH
104
import Ganeti.JSON
Iustin Pop's avatar
Iustin Pop committed
105
import Ganeti.Utils
106

Iustin Pop's avatar
Iustin Pop committed
107
108
109
110
-- * Generic types

-- | Type that holds a non-negative value.
newtype NonNegative a = NonNegative { fromNonNegative :: a }
111
  deriving (Show, Eq)
Iustin Pop's avatar
Iustin Pop committed
112
113
114
115
116
117
118
119
120
121
122
123
124

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

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

-- | Type that holds a positive value.
newtype Positive a = Positive { fromPositive :: a }
125
  deriving (Show, Eq)
Iustin Pop's avatar
Iustin Pop committed
126
127
128
129
130
131
132
133
134
135
136

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

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

Iustin Pop's avatar
Iustin Pop committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
-- | Type that holds a negative value.
newtype Negative a = Negative { fromNegative :: a }
  deriving (Show, Eq)

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

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

Iustin Pop's avatar
Iustin Pop committed
151
152
-- | Type that holds a non-null list.
newtype NonEmpty a = NonEmpty { fromNonEmpty :: [a] }
153
  deriving (Show, Eq)
Iustin Pop's avatar
Iustin Pop committed
154
155
156
157
158
159
160
161
162
163

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

instance (JSON.JSON a) => JSON.JSON (NonEmpty a) where
  showJSON = JSON.showJSON . fromNonEmpty
  readJSON v = JSON.readJSON v >>= mkNonEmpty

Iustin Pop's avatar
Iustin Pop committed
164
165
166
-- | A simple type alias for non-empty strings.
type NonEmptyString = NonEmpty Char

Iustin Pop's avatar
Iustin Pop committed
167
168
-- * Ganeti types

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

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

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
-- | The Group allocation policy type.
--
-- Note that the order of constructors is important as the automatic
-- Ord instance will order them in the order they are defined, so when
-- changing this data type be careful about the interaction with the
-- desired sorting order.
$(THH.declareSADT "AllocPolicy"
       [ ("AllocPreferred",   'C.allocPolicyPreferred)
       , ("AllocLastResort",  'C.allocPolicyLastResort)
       , ("AllocUnallocable", 'C.allocPolicyUnallocable)
       ])
$(THH.makeJSONInstance ''AllocPolicy)

-- | The Instance real state type. FIXME: this could be improved to
-- just wrap a /NormalState AdminStatus | ErrorState ErrorCondition/.
$(THH.declareSADT "InstanceStatus"
       [ ("StatusDown",    'C.inststAdmindown)
       , ("StatusOffline", 'C.inststAdminoffline)
       , ("ErrorDown",     'C.inststErrordown)
       , ("ErrorUp",       'C.inststErrorup)
       , ("NodeDown",      'C.inststNodedown)
       , ("NodeOffline",   'C.inststNodeoffline)
       , ("Running",       'C.inststRunning)
       , ("WrongNode",     'C.inststWrongnode)
       ])
$(THH.makeJSONInstance ''InstanceStatus)
Iustin Pop's avatar
Iustin Pop committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

-- | Migration mode.
$(THH.declareSADT "MigrationMode"
     [ ("MigrationLive",    'C.htMigrationLive)
     , ("MigrationNonLive", 'C.htMigrationNonlive)
     ])
$(THH.makeJSONInstance ''MigrationMode)

-- | Verify optional checks.
$(THH.declareSADT "VerifyOptionalChecks"
     [ ("VerifyNPlusOneMem", 'C.verifyNplusoneMem)
     ])
$(THH.makeJSONInstance ''VerifyOptionalChecks)

-- | Cluster verify error codes.
$(THH.declareSADT "CVErrorCode"
  [ ("CvECLUSTERCFG",           'C.cvEclustercfgCode)
  , ("CvECLUSTERCERT",          'C.cvEclustercertCode)
  , ("CvECLUSTERFILECHECK",     'C.cvEclusterfilecheckCode)
  , ("CvECLUSTERDANGLINGNODES", 'C.cvEclusterdanglingnodesCode)
  , ("CvECLUSTERDANGLINGINST",  'C.cvEclusterdanglinginstCode)
  , ("CvEINSTANCEBADNODE",      'C.cvEinstancebadnodeCode)
  , ("CvEINSTANCEDOWN",         'C.cvEinstancedownCode)
  , ("CvEINSTANCELAYOUT",       'C.cvEinstancelayoutCode)
  , ("CvEINSTANCEMISSINGDISK",  'C.cvEinstancemissingdiskCode)
  , ("CvEINSTANCEFAULTYDISK",   'C.cvEinstancefaultydiskCode)
  , ("CvEINSTANCEWRONGNODE",    'C.cvEinstancewrongnodeCode)
  , ("CvEINSTANCESPLITGROUPS",  'C.cvEinstancesplitgroupsCode)
  , ("CvEINSTANCEPOLICY",       'C.cvEinstancepolicyCode)
  , ("CvENODEDRBD",             'C.cvEnodedrbdCode)
  , ("CvENODEDRBDHELPER",       'C.cvEnodedrbdhelperCode)
  , ("CvENODEFILECHECK",        'C.cvEnodefilecheckCode)
  , ("CvENODEHOOKS",            'C.cvEnodehooksCode)
  , ("CvENODEHV",               'C.cvEnodehvCode)
  , ("CvENODELVM",              'C.cvEnodelvmCode)
  , ("CvENODEN1",               'C.cvEnoden1Code)
  , ("CvENODENET",              'C.cvEnodenetCode)
  , ("CvENODEOS",               'C.cvEnodeosCode)
  , ("CvENODEORPHANINSTANCE",   'C.cvEnodeorphaninstanceCode)
  , ("CvENODEORPHANLV",         'C.cvEnodeorphanlvCode)
  , ("CvENODERPC",              'C.cvEnoderpcCode)
  , ("CvENODESSH",              'C.cvEnodesshCode)
  , ("CvENODEVERSION",          'C.cvEnodeversionCode)
  , ("CvENODESETUP",            'C.cvEnodesetupCode)
  , ("CvENODETIME",             'C.cvEnodetimeCode)
  , ("CvENODEOOBPATH",          'C.cvEnodeoobpathCode)
  , ("CvENODEUSERSCRIPTS",      'C.cvEnodeuserscriptsCode)
  , ("CvENODEFILESTORAGEPATHS", 'C.cvEnodefilestoragepathsCode)
  ])
$(THH.makeJSONInstance ''CVErrorCode)

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

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

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

Iustin Pop's avatar
Iustin Pop committed
289
290
291
292
293
294
295
296
297
298
-- | Oob command type.
$(THH.declareSADT "OobCommand"
  [ ("OobHealth",      'C.oobHealth)
  , ("OobPowerCycle",  'C.oobPowerCycle)
  , ("OobPowerOff",    'C.oobPowerOff)
  , ("OobPowerOn",     'C.oobPowerOn)
  , ("OobPowerStatus", 'C.oobPowerStatus)
  ])
$(THH.makeJSONInstance ''OobCommand)

299
300
301
302
303
304
305
-- | Storage type.
$(THH.declareSADT "StorageType"
  [ ("StorageFile", 'C.stFile)
  , ("StorageLvmPv", 'C.stLvmPv)
  , ("StorageLvmVg", 'C.stLvmVg)
  ])
$(THH.makeJSONInstance ''StorageType)
Iustin Pop's avatar
Iustin Pop committed
306
307
308
309
310
311
312
313

-- | Node evac modes.
$(THH.declareSADT "NodeEvacMode"
  [ ("NEvacPrimary",   'C.iallocatorNevacPri)
  , ("NEvacSecondary", 'C.iallocatorNevacSec)
  , ("NEvacAll",       'C.iallocatorNevacAll)
  ])
$(THH.makeJSONInstance ''NodeEvacMode)
314
315
316
317
318
319
320

-- | The file driver type.
$(THH.declareSADT "FileDriver"
  [ ("FileLoop",   'C.fdLoop)
  , ("FileBlktap", 'C.fdBlktap)
  ])
$(THH.makeJSONInstance ''FileDriver)
321
322
323
324
325
326
327
328

-- | The instance create mode.
$(THH.declareSADT "InstCreateMode"
  [ ("InstCreate",       'C.instanceCreate)
  , ("InstImport",       'C.instanceImport)
  , ("InstRemoteImport", 'C.instanceRemoteImport)
  ])
$(THH.makeJSONInstance ''InstCreateMode)
329
330
331
332
333
334
335
336

-- | Reboot type.
$(THH.declareSADT "RebootType"
  [ ("RebootSoft", 'C.instanceRebootSoft)
  , ("RebootHard", 'C.instanceRebootHard)
  , ("RebootFull", 'C.instanceRebootFull)
  ])
$(THH.makeJSONInstance ''RebootType)
Iustin Pop's avatar
Iustin Pop committed
337
338
339
340
341
342
343

-- | Export modes.
$(THH.declareSADT "ExportMode"
  [ ("ExportModeLocal",  'C.exportModeLocal)
  , ("ExportModeRemove", 'C.exportModeRemote)
  ])
$(THH.makeJSONInstance ''ExportMode)
Iustin Pop's avatar
Iustin Pop committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360

-- | IAllocator run types (OpTestIAllocator).
$(THH.declareSADT "IAllocatorTestDir"
  [ ("IAllocatorDirIn",  'C.iallocatorDirIn)
  , ("IAllocatorDirOut", 'C.iallocatorDirOut)
  ])
$(THH.makeJSONInstance ''IAllocatorTestDir)

-- | IAllocator mode. FIXME: use this in "HTools.Backend.IAlloc".
$(THH.declareSADT "IAllocatorMode"
  [ ("IAllocatorAlloc",       'C.iallocatorModeAlloc)
  , ("IAllocatorMultiAlloc",  'C.iallocatorModeMultiAlloc)
  , ("IAllocatorReloc",       'C.iallocatorModeReloc)
  , ("IAllocatorNodeEvac",    'C.iallocatorModeNodeEvac)
  , ("IAllocatorChangeGroup", 'C.iallocatorModeChgGroup)
  ])
$(THH.makeJSONInstance ''IAllocatorMode)
361
362
363
364
365

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

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

384
385
386
387
388
389
390
-- | Finalized job status.
$(THH.declareSADT "FinalizedJobStatus"
  [ ("JobStatusCanceled",   'C.jobStatusCanceled)
  , ("JobStatusSuccessful", 'C.jobStatusSuccess)
  , ("JobStatusFailed",     'C.jobStatusError)
  ])
$(THH.makeJSONInstance ''FinalizedJobStatus)
Iustin Pop's avatar
Iustin Pop committed
391
392
393
394
395
396
397
398
399
400

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

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

401
402
403
404
-- | Builds a job ID from a string.
makeJobIdS :: (Monad m) => String -> m JobId
makeJobIdS s = tryRead "parsing job id" s >>= makeJobId

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

instance JSON.JSON JobId where
  showJSON = JSON.showJSON . fromJobId
  readJSON = parseJobId
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433

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

-- | Job ID dependency.
data JobIdDep = JobDepRelative RelativeJobId
              | JobDepAbsolute JobId
                deriving (Show, Eq)

instance JSON.JSON JobIdDep where
  showJSON (JobDepRelative i) = showJSON i
  showJSON (JobDepAbsolute i) = showJSON i
  readJSON v =
    case JSON.readJSON v::JSON.Result (Negative Int) of
      -- first try relative dependency, usually most common
      JSON.Ok r -> return $ JobDepRelative r
434
      JSON.Error _ -> liftM JobDepAbsolute (parseJobId v)
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450

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

instance JSON JobDependency where
  showJSON (JobDependency dep status) = showJSON (dep, status)
  readJSON = liftM (uncurry JobDependency) . readJSON

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

452
453
454
455
456
457
458
459
460
461
462
463
464
-- | Parse submit priorities from a string.
parseSubmitPriority :: (Monad m) => String -> m OpSubmitPriority
parseSubmitPriority "low"    = return OpPrioLow
parseSubmitPriority "normal" = return OpPrioNormal
parseSubmitPriority "high"   = return OpPrioHigh
parseSubmitPriority str      = fail $ "Unknown priority '" ++ str ++ "'"

-- | Format a submit priority as string.
fmtSubmitPriority :: OpSubmitPriority -> String
fmtSubmitPriority OpPrioLow    = "low"
fmtSubmitPriority OpPrioNormal = "normal"
fmtSubmitPriority OpPrioHigh   = "high"

465
466
-- | Our ADT for the OpCode status at runtime (while in a job).
$(THH.declareSADT "OpStatus"
467
468
469
470
471
472
473
474
  [ ("OP_STATUS_QUEUED",    'C.opStatusQueued)
  , ("OP_STATUS_WAITING",   'C.opStatusWaiting)
  , ("OP_STATUS_CANCELING", 'C.opStatusCanceling)
  , ("OP_STATUS_RUNNING",   'C.opStatusRunning)
  , ("OP_STATUS_CANCELED",  'C.opStatusCanceled)
  , ("OP_STATUS_SUCCESS",   'C.opStatusSuccess)
  , ("OP_STATUS_ERROR",     'C.opStatusError)
  ])
475
$(THH.makeJSONInstance ''OpStatus)
476
477
478
479
480
481
482
483

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