Types.hs 11.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
{-# 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'.

-}

{-

Copyright (C) 2012 Google Inc.

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
49
50
51
  , NonNegative
  , fromNonNegative
  , mkNonNegative
  , Positive
  , fromPositive
  , mkPositive
  , NonEmpty
  , fromNonEmpty
  , mkNonEmpty
Iustin Pop's avatar
Iustin Pop committed
52
  , NonEmptyString
Iustin Pop's avatar
Iustin Pop committed
53
54
55
  , MigrationMode(..)
  , VerifyOptionalChecks(..)
  , DdmSimple(..)
56
  , DdmFull(..)
Iustin Pop's avatar
Iustin Pop committed
57
58
  , CVErrorCode(..)
  , cVErrorCodeToRaw
59
  , Hypervisor(..)
Iustin Pop's avatar
Iustin Pop committed
60
  , OobCommand(..)
61
  , StorageType(..)
Iustin Pop's avatar
Iustin Pop committed
62
  , NodeEvacMode(..)
63
  , FileDriver(..)
64
  , InstCreateMode(..)
65
  , RebootType(..)
Iustin Pop's avatar
Iustin Pop committed
66
  , ExportMode(..)
Iustin Pop's avatar
Iustin Pop committed
67
68
69
  , IAllocatorTestDir(..)
  , IAllocatorMode(..)
  , iAllocatorModeToRaw
70
71
  , NetworkType(..)
  , networkTypeToRaw
72
73
  , NICMode(..)
  , nICModeToRaw
74
75
  , FinalizedJobStatus(..)
  , finalizedJobStatusToRaw
Iustin Pop's avatar
Iustin Pop committed
76
77
78
  , JobId
  , fromJobId
  , makeJobId
79
80
  ) where

Iustin Pop's avatar
Iustin Pop committed
81
import qualified Text.JSON as JSON
Iustin Pop's avatar
Iustin Pop committed
82
import Data.Ratio (numerator, denominator)
Iustin Pop's avatar
Iustin Pop committed
83

84
85
import qualified Ganeti.Constants as C
import qualified Ganeti.THH as THH
86
import Ganeti.JSON
Iustin Pop's avatar
Iustin Pop committed
87
import Ganeti.Utils
88

Iustin Pop's avatar
Iustin Pop committed
89
90
91
92
-- * Generic types

-- | Type that holds a non-negative value.
newtype NonNegative a = NonNegative { fromNonNegative :: a }
93
  deriving (Show, Eq)
Iustin Pop's avatar
Iustin Pop committed
94
95
96
97
98
99
100
101
102
103
104
105
106

-- | 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 }
107
  deriving (Show, Eq)
Iustin Pop's avatar
Iustin Pop committed
108
109
110
111
112
113
114
115
116
117
118
119
120

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

-- | Type that holds a non-null list.
newtype NonEmpty a = NonEmpty { fromNonEmpty :: [a] }
121
  deriving (Show, Eq)
Iustin Pop's avatar
Iustin Pop committed
122
123
124
125
126
127
128
129
130
131

-- | 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
132
133
134
-- | A simple type alias for non-empty strings.
type NonEmptyString = NonEmpty Char

Iustin Pop's avatar
Iustin Pop committed
135
136
-- * Ganeti types

137
138
139
140
141
142
143
144
145
146
147
148
-- | 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)
       ])
$(THH.makeJSONInstance ''DiskTemplate)

149
150
151
152
instance HasStringRepr DiskTemplate where
  fromStringRepr = diskTemplateFromRaw
  toStringRepr = diskTemplateToRaw

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
-- | 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
179
180
181
182
183
184
185
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

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

237
238
239
240
241
242
243
244
-- | Dynamic device modification, all operations version.
$(THH.declareSADT "DdmFull"
     [ ("DdmFullAdd",    'C.ddmAdd)
     , ("DdmFullRemove", 'C.ddmRemove)
     , ("DdmFullModify", 'C.ddmModify)
     ])
$(THH.makeJSONInstance ''DdmFull)

245
246
247
248
249
250
251
252
253
254
-- | 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)
255

Iustin Pop's avatar
Iustin Pop committed
256
257
258
259
260
261
262
263
264
265
-- | Oob command type.
$(THH.declareSADT "OobCommand"
  [ ("OobHealth",      'C.oobHealth)
  , ("OobPowerCycle",  'C.oobPowerCycle)
  , ("OobPowerOff",    'C.oobPowerOff)
  , ("OobPowerOn",     'C.oobPowerOn)
  , ("OobPowerStatus", 'C.oobPowerStatus)
  ])
$(THH.makeJSONInstance ''OobCommand)

266
267
268
269
270
271
272
-- | Storage type.
$(THH.declareSADT "StorageType"
  [ ("StorageFile", 'C.stFile)
  , ("StorageLvmPv", 'C.stLvmPv)
  , ("StorageLvmVg", 'C.stLvmVg)
  ])
$(THH.makeJSONInstance ''StorageType)
Iustin Pop's avatar
Iustin Pop committed
273
274
275
276
277
278
279
280

-- | Node evac modes.
$(THH.declareSADT "NodeEvacMode"
  [ ("NEvacPrimary",   'C.iallocatorNevacPri)
  , ("NEvacSecondary", 'C.iallocatorNevacSec)
  , ("NEvacAll",       'C.iallocatorNevacAll)
  ])
$(THH.makeJSONInstance ''NodeEvacMode)
281
282
283
284
285
286
287

-- | The file driver type.
$(THH.declareSADT "FileDriver"
  [ ("FileLoop",   'C.fdLoop)
  , ("FileBlktap", 'C.fdBlktap)
  ])
$(THH.makeJSONInstance ''FileDriver)
288
289
290
291
292
293
294
295

-- | The instance create mode.
$(THH.declareSADT "InstCreateMode"
  [ ("InstCreate",       'C.instanceCreate)
  , ("InstImport",       'C.instanceImport)
  , ("InstRemoteImport", 'C.instanceRemoteImport)
  ])
$(THH.makeJSONInstance ''InstCreateMode)
296
297
298
299
300
301
302
303

-- | Reboot type.
$(THH.declareSADT "RebootType"
  [ ("RebootSoft", 'C.instanceRebootSoft)
  , ("RebootHard", 'C.instanceRebootHard)
  , ("RebootFull", 'C.instanceRebootFull)
  ])
$(THH.makeJSONInstance ''RebootType)
Iustin Pop's avatar
Iustin Pop committed
304
305
306
307
308
309
310

-- | Export modes.
$(THH.declareSADT "ExportMode"
  [ ("ExportModeLocal",  'C.exportModeLocal)
  , ("ExportModeRemove", 'C.exportModeRemote)
  ])
$(THH.makeJSONInstance ''ExportMode)
Iustin Pop's avatar
Iustin Pop committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327

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

329
330
331
332
333
334
335
-- | Network type.
$(THH.declareSADT "NetworkType"
  [ ("PrivateNetwork", 'C.networkTypePrivate)
  , ("PublicNetwork",  'C.networkTypePublic)
  ])
$(THH.makeJSONInstance ''NetworkType)

336
337
338
339
340
341
-- | Netork mode.
$(THH.declareSADT "NICMode"
  [ ("NMBridged", 'C.nicModeBridged)
  , ("NMRouted",  'C.nicModeRouted)
  ])
$(THH.makeJSONInstance ''NICMode)
342
343
344
345
346
347
348
349

-- | 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

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

-- | Parses a job ID.
parseJobId :: (Monad m) => JSON.JSValue -> m JobId
parseJobId (JSON.JSString x) =
  tryRead "parsing job id" (JSON.fromJSString x) >>= makeJobId
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