Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Types.hs @ a3f02317

History | View | Annotate | Download (10.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
  , NonEmpty
50
  , fromNonEmpty
51
  , mkNonEmpty
52
  , NonEmptyString
53
  , MigrationMode(..)
54
  , VerifyOptionalChecks(..)
55
  , DdmSimple(..)
56
  , DdmFull(..)
57
  , CVErrorCode(..)
58
  , cVErrorCodeToRaw
59
  , Hypervisor(..)
60
  , OobCommand(..)
61
  , StorageType(..)
62
  , NodeEvacMode(..)
63
  , FileDriver(..)
64
  , InstCreateMode(..)
65
  , RebootType(..)
66
  , ExportMode(..)
67
  , IAllocatorTestDir(..)
68
  , IAllocatorMode(..)
69
  , iAllocatorModeToRaw
70
  ) where
71

    
72
import qualified Text.JSON as JSON
73

    
74
import qualified Ganeti.Constants as C
75
import qualified Ganeti.THH as THH
76
import Ganeti.JSON
77

    
78
-- * Generic types
79

    
80
-- | Type that holds a non-negative value.
81
newtype NonNegative a = NonNegative { fromNonNegative :: a }
82
  deriving (Show, Read, Eq)
83

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

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

    
94
-- | Type that holds a positive value.
95
newtype Positive a = Positive { fromPositive :: a }
96
  deriving (Show, Read, Eq)
97

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

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

    
108
-- | Type that holds a non-null list.
109
newtype NonEmpty a = NonEmpty { fromNonEmpty :: [a] }
110
  deriving (Show, Read, Eq)
111

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

    
117
instance (JSON.JSON a) => JSON.JSON (NonEmpty a) where
118
  showJSON = JSON.showJSON . fromNonEmpty
119
  readJSON v = JSON.readJSON v >>= mkNonEmpty
120

    
121
-- | A simple type alias for non-empty strings.
122
type NonEmptyString = NonEmpty Char
123

    
124
-- * Ganeti types
125

    
126
-- | Instance disk template type.
127
$(THH.declareSADT "DiskTemplate"
128
       [ ("DTDiskless",   'C.dtDiskless)
129
       , ("DTFile",       'C.dtFile)
130
       , ("DTSharedFile", 'C.dtSharedFile)
131
       , ("DTPlain",      'C.dtPlain)
132
       , ("DTBlock",      'C.dtBlock)
133
       , ("DTDrbd8",      'C.dtDrbd8)
134
       , ("DTRbd",        'C.dtRbd)
135
       ])
136
$(THH.makeJSONInstance ''DiskTemplate)
137

    
138
instance HasStringRepr DiskTemplate where
139
  fromStringRepr = diskTemplateFromRaw
140
  toStringRepr = diskTemplateToRaw
141

    
142
-- | The Group allocation policy type.
143
--
144
-- Note that the order of constructors is important as the automatic
145
-- Ord instance will order them in the order they are defined, so when
146
-- changing this data type be careful about the interaction with the
147
-- desired sorting order.
148
$(THH.declareSADT "AllocPolicy"
149
       [ ("AllocPreferred",   'C.allocPolicyPreferred)
150
       , ("AllocLastResort",  'C.allocPolicyLastResort)
151
       , ("AllocUnallocable", 'C.allocPolicyUnallocable)
152
       ])
153
$(THH.makeJSONInstance ''AllocPolicy)
154

    
155
-- | The Instance real state type. FIXME: this could be improved to
156
-- just wrap a /NormalState AdminStatus | ErrorState ErrorCondition/.
157
$(THH.declareSADT "InstanceStatus"
158
       [ ("StatusDown",    'C.inststAdmindown)
159
       , ("StatusOffline", 'C.inststAdminoffline)
160
       , ("ErrorDown",     'C.inststErrordown)
161
       , ("ErrorUp",       'C.inststErrorup)
162
       , ("NodeDown",      'C.inststNodedown)
163
       , ("NodeOffline",   'C.inststNodeoffline)
164
       , ("Running",       'C.inststRunning)
165
       , ("WrongNode",     'C.inststWrongnode)
166
       ])
167
$(THH.makeJSONInstance ''InstanceStatus)
168

    
169
-- | Migration mode.
170
$(THH.declareSADT "MigrationMode"
171
     [ ("MigrationLive",    'C.htMigrationLive)
172
     , ("MigrationNonLive", 'C.htMigrationNonlive)
173
     ])
174
$(THH.makeJSONInstance ''MigrationMode)
175

    
176
-- | Verify optional checks.
177
$(THH.declareSADT "VerifyOptionalChecks"
178
     [ ("VerifyNPlusOneMem", 'C.verifyNplusoneMem)
179
     ])
180
$(THH.makeJSONInstance ''VerifyOptionalChecks)
181

    
182
-- | Cluster verify error codes.
183
$(THH.declareSADT "CVErrorCode"
184
  [ ("CvECLUSTERCFG",           'C.cvEclustercfgCode)
185
  , ("CvECLUSTERCERT",          'C.cvEclustercertCode)
186
  , ("CvECLUSTERFILECHECK",     'C.cvEclusterfilecheckCode)
187
  , ("CvECLUSTERDANGLINGNODES", 'C.cvEclusterdanglingnodesCode)
188
  , ("CvECLUSTERDANGLINGINST",  'C.cvEclusterdanglinginstCode)
189
  , ("CvEINSTANCEBADNODE",      'C.cvEinstancebadnodeCode)
190
  , ("CvEINSTANCEDOWN",         'C.cvEinstancedownCode)
191
  , ("CvEINSTANCELAYOUT",       'C.cvEinstancelayoutCode)
192
  , ("CvEINSTANCEMISSINGDISK",  'C.cvEinstancemissingdiskCode)
193
  , ("CvEINSTANCEFAULTYDISK",   'C.cvEinstancefaultydiskCode)
194
  , ("CvEINSTANCEWRONGNODE",    'C.cvEinstancewrongnodeCode)
195
  , ("CvEINSTANCESPLITGROUPS",  'C.cvEinstancesplitgroupsCode)
196
  , ("CvEINSTANCEPOLICY",       'C.cvEinstancepolicyCode)
197
  , ("CvENODEDRBD",             'C.cvEnodedrbdCode)
198
  , ("CvENODEDRBDHELPER",       'C.cvEnodedrbdhelperCode)
199
  , ("CvENODEFILECHECK",        'C.cvEnodefilecheckCode)
200
  , ("CvENODEHOOKS",            'C.cvEnodehooksCode)
201
  , ("CvENODEHV",               'C.cvEnodehvCode)
202
  , ("CvENODELVM",              'C.cvEnodelvmCode)
203
  , ("CvENODEN1",               'C.cvEnoden1Code)
204
  , ("CvENODENET",              'C.cvEnodenetCode)
205
  , ("CvENODEOS",               'C.cvEnodeosCode)
206
  , ("CvENODEORPHANINSTANCE",   'C.cvEnodeorphaninstanceCode)
207
  , ("CvENODEORPHANLV",         'C.cvEnodeorphanlvCode)
208
  , ("CvENODERPC",              'C.cvEnoderpcCode)
209
  , ("CvENODESSH",              'C.cvEnodesshCode)
210
  , ("CvENODEVERSION",          'C.cvEnodeversionCode)
211
  , ("CvENODESETUP",            'C.cvEnodesetupCode)
212
  , ("CvENODETIME",             'C.cvEnodetimeCode)
213
  , ("CvENODEOOBPATH",          'C.cvEnodeoobpathCode)
214
  , ("CvENODEUSERSCRIPTS",      'C.cvEnodeuserscriptsCode)
215
  , ("CvENODEFILESTORAGEPATHS", 'C.cvEnodefilestoragepathsCode)
216
  ])
217
$(THH.makeJSONInstance ''CVErrorCode)
218

    
219
-- | Dynamic device modification, just add\/remove version.
220
$(THH.declareSADT "DdmSimple"
221
     [ ("DdmSimpleAdd",    'C.ddmAdd)
222
     , ("DdmSimpleRemove", 'C.ddmRemove)
223
     ])
224
$(THH.makeJSONInstance ''DdmSimple)
225

    
226
-- | Dynamic device modification, all operations version.
227
$(THH.declareSADT "DdmFull"
228
     [ ("DdmFullAdd",    'C.ddmAdd)
229
     , ("DdmFullRemove", 'C.ddmRemove)
230
     , ("DdmFullModify", 'C.ddmModify)
231
     ])
232
$(THH.makeJSONInstance ''DdmFull)
233

    
234
-- | Hypervisor type definitions.
235
$(THH.declareSADT "Hypervisor"
236
  [ ( "Kvm",    'C.htKvm )
237
  , ( "XenPvm", 'C.htXenPvm )
238
  , ( "Chroot", 'C.htChroot )
239
  , ( "XenHvm", 'C.htXenHvm )
240
  , ( "Lxc",    'C.htLxc )
241
  , ( "Fake",   'C.htFake )
242
  ])
243
$(THH.makeJSONInstance ''Hypervisor)
244

    
245
-- | Oob command type.
246
$(THH.declareSADT "OobCommand"
247
  [ ("OobHealth",      'C.oobHealth)
248
  , ("OobPowerCycle",  'C.oobPowerCycle)
249
  , ("OobPowerOff",    'C.oobPowerOff)
250
  , ("OobPowerOn",     'C.oobPowerOn)
251
  , ("OobPowerStatus", 'C.oobPowerStatus)
252
  ])
253
$(THH.makeJSONInstance ''OobCommand)
254

    
255
-- | Storage type.
256
$(THH.declareSADT "StorageType"
257
  [ ("StorageFile", 'C.stFile)
258
  , ("StorageLvmPv", 'C.stLvmPv)
259
  , ("StorageLvmVg", 'C.stLvmVg)
260
  ])
261
$(THH.makeJSONInstance ''StorageType)
262

    
263
-- | Node evac modes.
264
$(THH.declareSADT "NodeEvacMode"
265
  [ ("NEvacPrimary",   'C.iallocatorNevacPri)
266
  , ("NEvacSecondary", 'C.iallocatorNevacSec)
267
  , ("NEvacAll",       'C.iallocatorNevacAll)
268
  ])
269
$(THH.makeJSONInstance ''NodeEvacMode)
270

    
271
-- | The file driver type.
272
$(THH.declareSADT "FileDriver"
273
  [ ("FileLoop",   'C.fdLoop)
274
  , ("FileBlktap", 'C.fdBlktap)
275
  ])
276
$(THH.makeJSONInstance ''FileDriver)
277

    
278
-- | The instance create mode.
279
$(THH.declareSADT "InstCreateMode"
280
  [ ("InstCreate",       'C.instanceCreate)
281
  , ("InstImport",       'C.instanceImport)
282
  , ("InstRemoteImport", 'C.instanceRemoteImport)
283
  ])
284
$(THH.makeJSONInstance ''InstCreateMode)
285

    
286
-- | Reboot type.
287
$(THH.declareSADT "RebootType"
288
  [ ("RebootSoft", 'C.instanceRebootSoft)
289
  , ("RebootHard", 'C.instanceRebootHard)
290
  , ("RebootFull", 'C.instanceRebootFull)
291
  ])
292
$(THH.makeJSONInstance ''RebootType)
293

    
294
-- | Export modes.
295
$(THH.declareSADT "ExportMode"
296
  [ ("ExportModeLocal",  'C.exportModeLocal)
297
  , ("ExportModeRemove", 'C.exportModeRemote)
298
  ])
299
$(THH.makeJSONInstance ''ExportMode)
300

    
301
-- | IAllocator run types (OpTestIAllocator).
302
$(THH.declareSADT "IAllocatorTestDir"
303
  [ ("IAllocatorDirIn",  'C.iallocatorDirIn)
304
  , ("IAllocatorDirOut", 'C.iallocatorDirOut)
305
  ])
306
$(THH.makeJSONInstance ''IAllocatorTestDir)
307

    
308
-- | IAllocator mode. FIXME: use this in "HTools.Backend.IAlloc".
309
$(THH.declareSADT "IAllocatorMode"
310
  [ ("IAllocatorAlloc",       'C.iallocatorModeAlloc)
311
  , ("IAllocatorMultiAlloc",  'C.iallocatorModeMultiAlloc)
312
  , ("IAllocatorReloc",       'C.iallocatorModeReloc)
313
  , ("IAllocatorNodeEvac",    'C.iallocatorModeNodeEvac)
314
  , ("IAllocatorChangeGroup", 'C.iallocatorModeChgGroup)
315
  ])
316
$(THH.makeJSONInstance ''IAllocatorMode)