root / htools / Ganeti / Types.hs @ c48711d5
History | View | Annotate | Download (11.9 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 |
, NetworkType(..) |
71 |
, networkTypeToRaw |
72 |
, NICMode(..) |
73 |
, nICModeToRaw |
74 |
, FinalizedJobStatus(..) |
75 |
, finalizedJobStatusToRaw |
76 |
, JobId |
77 |
, fromJobId |
78 |
, makeJobId |
79 |
) where |
80 |
|
81 |
import qualified Text.JSON as JSON |
82 |
import Data.Ratio (numerator, denominator) |
83 |
|
84 |
import qualified Ganeti.Constants as C |
85 |
import qualified Ganeti.THH as THH |
86 |
import Ganeti.JSON |
87 |
import Ganeti.Utils |
88 |
|
89 |
-- * Generic types |
90 |
|
91 |
-- | Type that holds a non-negative value. |
92 |
newtype NonNegative a = NonNegative { fromNonNegative :: a } |
93 |
deriving (Show, Eq) |
94 |
|
95 |
-- | Smart constructor for 'NonNegative'. |
96 |
mkNonNegative :: (Monad m, Num a, Ord a, Show a) => a -> m (NonNegative a) |
97 |
mkNonNegative i | i >= 0 = return (NonNegative i) |
98 |
| otherwise = fail $ "Invalid value for non-negative type '" ++ |
99 |
show i ++ "'" |
100 |
|
101 |
instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (NonNegative a) where |
102 |
showJSON = JSON.showJSON . fromNonNegative |
103 |
readJSON v = JSON.readJSON v >>= mkNonNegative |
104 |
|
105 |
-- | Type that holds a positive value. |
106 |
newtype Positive a = Positive { fromPositive :: a } |
107 |
deriving (Show, Eq) |
108 |
|
109 |
-- | Smart constructor for 'Positive'. |
110 |
mkPositive :: (Monad m, Num a, Ord a, Show a) => a -> m (Positive a) |
111 |
mkPositive i | i > 0 = return (Positive i) |
112 |
| otherwise = fail $ "Invalid value for positive type '" ++ |
113 |
show i ++ "'" |
114 |
|
115 |
instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (Positive a) where |
116 |
showJSON = JSON.showJSON . fromPositive |
117 |
readJSON v = JSON.readJSON v >>= mkPositive |
118 |
|
119 |
-- | Type that holds a non-null list. |
120 |
newtype NonEmpty a = NonEmpty { fromNonEmpty :: [a] } |
121 |
deriving (Show, Eq) |
122 |
|
123 |
-- | Smart constructor for 'NonEmpty'. |
124 |
mkNonEmpty :: (Monad m) => [a] -> m (NonEmpty a) |
125 |
mkNonEmpty [] = fail "Received empty value for non-empty list" |
126 |
mkNonEmpty xs = return (NonEmpty xs) |
127 |
|
128 |
instance (JSON.JSON a) => JSON.JSON (NonEmpty a) where |
129 |
showJSON = JSON.showJSON . fromNonEmpty |
130 |
readJSON v = JSON.readJSON v >>= mkNonEmpty |
131 |
|
132 |
-- | A simple type alias for non-empty strings. |
133 |
type NonEmptyString = NonEmpty Char |
134 |
|
135 |
-- * Ganeti types |
136 |
|
137 |
-- | Instance disk template type. |
138 |
$(THH.declareSADT "DiskTemplate" |
139 |
[ ("DTDiskless", 'C.dtDiskless) |
140 |
, ("DTFile", 'C.dtFile) |
141 |
, ("DTSharedFile", 'C.dtSharedFile) |
142 |
, ("DTPlain", 'C.dtPlain) |
143 |
, ("DTBlock", 'C.dtBlock) |
144 |
, ("DTDrbd8", 'C.dtDrbd8) |
145 |
, ("DTRbd", 'C.dtRbd) |
146 |
]) |
147 |
$(THH.makeJSONInstance ''DiskTemplate) |
148 |
|
149 |
instance HasStringRepr DiskTemplate where |
150 |
fromStringRepr = diskTemplateFromRaw |
151 |
toStringRepr = diskTemplateToRaw |
152 |
|
153 |
-- | The Group allocation policy type. |
154 |
-- |
155 |
-- Note that the order of constructors is important as the automatic |
156 |
-- Ord instance will order them in the order they are defined, so when |
157 |
-- changing this data type be careful about the interaction with the |
158 |
-- desired sorting order. |
159 |
$(THH.declareSADT "AllocPolicy" |
160 |
[ ("AllocPreferred", 'C.allocPolicyPreferred) |
161 |
, ("AllocLastResort", 'C.allocPolicyLastResort) |
162 |
, ("AllocUnallocable", 'C.allocPolicyUnallocable) |
163 |
]) |
164 |
$(THH.makeJSONInstance ''AllocPolicy) |
165 |
|
166 |
-- | The Instance real state type. FIXME: this could be improved to |
167 |
-- just wrap a /NormalState AdminStatus | ErrorState ErrorCondition/. |
168 |
$(THH.declareSADT "InstanceStatus" |
169 |
[ ("StatusDown", 'C.inststAdmindown) |
170 |
, ("StatusOffline", 'C.inststAdminoffline) |
171 |
, ("ErrorDown", 'C.inststErrordown) |
172 |
, ("ErrorUp", 'C.inststErrorup) |
173 |
, ("NodeDown", 'C.inststNodedown) |
174 |
, ("NodeOffline", 'C.inststNodeoffline) |
175 |
, ("Running", 'C.inststRunning) |
176 |
, ("WrongNode", 'C.inststWrongnode) |
177 |
]) |
178 |
$(THH.makeJSONInstance ''InstanceStatus) |
179 |
|
180 |
-- | Migration mode. |
181 |
$(THH.declareSADT "MigrationMode" |
182 |
[ ("MigrationLive", 'C.htMigrationLive) |
183 |
, ("MigrationNonLive", 'C.htMigrationNonlive) |
184 |
]) |
185 |
$(THH.makeJSONInstance ''MigrationMode) |
186 |
|
187 |
-- | Verify optional checks. |
188 |
$(THH.declareSADT "VerifyOptionalChecks" |
189 |
[ ("VerifyNPlusOneMem", 'C.verifyNplusoneMem) |
190 |
]) |
191 |
$(THH.makeJSONInstance ''VerifyOptionalChecks) |
192 |
|
193 |
-- | Cluster verify error codes. |
194 |
$(THH.declareSADT "CVErrorCode" |
195 |
[ ("CvECLUSTERCFG", 'C.cvEclustercfgCode) |
196 |
, ("CvECLUSTERCERT", 'C.cvEclustercertCode) |
197 |
, ("CvECLUSTERFILECHECK", 'C.cvEclusterfilecheckCode) |
198 |
, ("CvECLUSTERDANGLINGNODES", 'C.cvEclusterdanglingnodesCode) |
199 |
, ("CvECLUSTERDANGLINGINST", 'C.cvEclusterdanglinginstCode) |
200 |
, ("CvEINSTANCEBADNODE", 'C.cvEinstancebadnodeCode) |
201 |
, ("CvEINSTANCEDOWN", 'C.cvEinstancedownCode) |
202 |
, ("CvEINSTANCELAYOUT", 'C.cvEinstancelayoutCode) |
203 |
, ("CvEINSTANCEMISSINGDISK", 'C.cvEinstancemissingdiskCode) |
204 |
, ("CvEINSTANCEFAULTYDISK", 'C.cvEinstancefaultydiskCode) |
205 |
, ("CvEINSTANCEWRONGNODE", 'C.cvEinstancewrongnodeCode) |
206 |
, ("CvEINSTANCESPLITGROUPS", 'C.cvEinstancesplitgroupsCode) |
207 |
, ("CvEINSTANCEPOLICY", 'C.cvEinstancepolicyCode) |
208 |
, ("CvENODEDRBD", 'C.cvEnodedrbdCode) |
209 |
, ("CvENODEDRBDHELPER", 'C.cvEnodedrbdhelperCode) |
210 |
, ("CvENODEFILECHECK", 'C.cvEnodefilecheckCode) |
211 |
, ("CvENODEHOOKS", 'C.cvEnodehooksCode) |
212 |
, ("CvENODEHV", 'C.cvEnodehvCode) |
213 |
, ("CvENODELVM", 'C.cvEnodelvmCode) |
214 |
, ("CvENODEN1", 'C.cvEnoden1Code) |
215 |
, ("CvENODENET", 'C.cvEnodenetCode) |
216 |
, ("CvENODEOS", 'C.cvEnodeosCode) |
217 |
, ("CvENODEORPHANINSTANCE", 'C.cvEnodeorphaninstanceCode) |
218 |
, ("CvENODEORPHANLV", 'C.cvEnodeorphanlvCode) |
219 |
, ("CvENODERPC", 'C.cvEnoderpcCode) |
220 |
, ("CvENODESSH", 'C.cvEnodesshCode) |
221 |
, ("CvENODEVERSION", 'C.cvEnodeversionCode) |
222 |
, ("CvENODESETUP", 'C.cvEnodesetupCode) |
223 |
, ("CvENODETIME", 'C.cvEnodetimeCode) |
224 |
, ("CvENODEOOBPATH", 'C.cvEnodeoobpathCode) |
225 |
, ("CvENODEUSERSCRIPTS", 'C.cvEnodeuserscriptsCode) |
226 |
, ("CvENODEFILESTORAGEPATHS", 'C.cvEnodefilestoragepathsCode) |
227 |
]) |
228 |
$(THH.makeJSONInstance ''CVErrorCode) |
229 |
|
230 |
-- | Dynamic device modification, just add\/remove version. |
231 |
$(THH.declareSADT "DdmSimple" |
232 |
[ ("DdmSimpleAdd", 'C.ddmAdd) |
233 |
, ("DdmSimpleRemove", 'C.ddmRemove) |
234 |
]) |
235 |
$(THH.makeJSONInstance ''DdmSimple) |
236 |
|
237 |
-- | Dynamic device modification, all operations version. |
238 |
$(THH.declareSADT "DdmFull" |
239 |
[ ("DdmFullAdd", 'C.ddmAdd) |
240 |
, ("DdmFullRemove", 'C.ddmRemove) |
241 |
, ("DdmFullModify", 'C.ddmModify) |
242 |
]) |
243 |
$(THH.makeJSONInstance ''DdmFull) |
244 |
|
245 |
-- | Hypervisor type definitions. |
246 |
$(THH.declareSADT "Hypervisor" |
247 |
[ ( "Kvm", 'C.htKvm ) |
248 |
, ( "XenPvm", 'C.htXenPvm ) |
249 |
, ( "Chroot", 'C.htChroot ) |
250 |
, ( "XenHvm", 'C.htXenHvm ) |
251 |
, ( "Lxc", 'C.htLxc ) |
252 |
, ( "Fake", 'C.htFake ) |
253 |
]) |
254 |
$(THH.makeJSONInstance ''Hypervisor) |
255 |
|
256 |
-- | Oob command type. |
257 |
$(THH.declareSADT "OobCommand" |
258 |
[ ("OobHealth", 'C.oobHealth) |
259 |
, ("OobPowerCycle", 'C.oobPowerCycle) |
260 |
, ("OobPowerOff", 'C.oobPowerOff) |
261 |
, ("OobPowerOn", 'C.oobPowerOn) |
262 |
, ("OobPowerStatus", 'C.oobPowerStatus) |
263 |
]) |
264 |
$(THH.makeJSONInstance ''OobCommand) |
265 |
|
266 |
-- | Storage type. |
267 |
$(THH.declareSADT "StorageType" |
268 |
[ ("StorageFile", 'C.stFile) |
269 |
, ("StorageLvmPv", 'C.stLvmPv) |
270 |
, ("StorageLvmVg", 'C.stLvmVg) |
271 |
]) |
272 |
$(THH.makeJSONInstance ''StorageType) |
273 |
|
274 |
-- | Node evac modes. |
275 |
$(THH.declareSADT "NodeEvacMode" |
276 |
[ ("NEvacPrimary", 'C.iallocatorNevacPri) |
277 |
, ("NEvacSecondary", 'C.iallocatorNevacSec) |
278 |
, ("NEvacAll", 'C.iallocatorNevacAll) |
279 |
]) |
280 |
$(THH.makeJSONInstance ''NodeEvacMode) |
281 |
|
282 |
-- | The file driver type. |
283 |
$(THH.declareSADT "FileDriver" |
284 |
[ ("FileLoop", 'C.fdLoop) |
285 |
, ("FileBlktap", 'C.fdBlktap) |
286 |
]) |
287 |
$(THH.makeJSONInstance ''FileDriver) |
288 |
|
289 |
-- | The instance create mode. |
290 |
$(THH.declareSADT "InstCreateMode" |
291 |
[ ("InstCreate", 'C.instanceCreate) |
292 |
, ("InstImport", 'C.instanceImport) |
293 |
, ("InstRemoteImport", 'C.instanceRemoteImport) |
294 |
]) |
295 |
$(THH.makeJSONInstance ''InstCreateMode) |
296 |
|
297 |
-- | Reboot type. |
298 |
$(THH.declareSADT "RebootType" |
299 |
[ ("RebootSoft", 'C.instanceRebootSoft) |
300 |
, ("RebootHard", 'C.instanceRebootHard) |
301 |
, ("RebootFull", 'C.instanceRebootFull) |
302 |
]) |
303 |
$(THH.makeJSONInstance ''RebootType) |
304 |
|
305 |
-- | Export modes. |
306 |
$(THH.declareSADT "ExportMode" |
307 |
[ ("ExportModeLocal", 'C.exportModeLocal) |
308 |
, ("ExportModeRemove", 'C.exportModeRemote) |
309 |
]) |
310 |
$(THH.makeJSONInstance ''ExportMode) |
311 |
|
312 |
-- | IAllocator run types (OpTestIAllocator). |
313 |
$(THH.declareSADT "IAllocatorTestDir" |
314 |
[ ("IAllocatorDirIn", 'C.iallocatorDirIn) |
315 |
, ("IAllocatorDirOut", 'C.iallocatorDirOut) |
316 |
]) |
317 |
$(THH.makeJSONInstance ''IAllocatorTestDir) |
318 |
|
319 |
-- | IAllocator mode. FIXME: use this in "HTools.Backend.IAlloc". |
320 |
$(THH.declareSADT "IAllocatorMode" |
321 |
[ ("IAllocatorAlloc", 'C.iallocatorModeAlloc) |
322 |
, ("IAllocatorMultiAlloc", 'C.iallocatorModeMultiAlloc) |
323 |
, ("IAllocatorReloc", 'C.iallocatorModeReloc) |
324 |
, ("IAllocatorNodeEvac", 'C.iallocatorModeNodeEvac) |
325 |
, ("IAllocatorChangeGroup", 'C.iallocatorModeChgGroup) |
326 |
]) |
327 |
$(THH.makeJSONInstance ''IAllocatorMode) |
328 |
|
329 |
-- | Network type. |
330 |
$(THH.declareSADT "NetworkType" |
331 |
[ ("PrivateNetwork", 'C.networkTypePrivate) |
332 |
, ("PublicNetwork", 'C.networkTypePublic) |
333 |
]) |
334 |
$(THH.makeJSONInstance ''NetworkType) |
335 |
|
336 |
-- | Netork mode. |
337 |
$(THH.declareSADT "NICMode" |
338 |
[ ("NMBridged", 'C.nicModeBridged) |
339 |
, ("NMRouted", 'C.nicModeRouted) |
340 |
]) |
341 |
$(THH.makeJSONInstance ''NICMode) |
342 |
|
343 |
-- | Finalized job status. |
344 |
$(THH.declareSADT "FinalizedJobStatus" |
345 |
[ ("JobStatusCanceled", 'C.jobStatusCanceled) |
346 |
, ("JobStatusSuccessful", 'C.jobStatusSuccess) |
347 |
, ("JobStatusFailed", 'C.jobStatusError) |
348 |
]) |
349 |
$(THH.makeJSONInstance ''FinalizedJobStatus) |
350 |
|
351 |
-- | The Ganeti job type. |
352 |
newtype JobId = JobId { fromJobId :: Int } |
353 |
deriving (Show, Eq) |
354 |
|
355 |
-- | Builds a job ID. |
356 |
makeJobId :: (Monad m) => Int -> m JobId |
357 |
makeJobId i | i >= 0 = return $ JobId i |
358 |
| otherwise = fail $ "Invalid value for job ID ' " ++ show i ++ "'" |
359 |
|
360 |
-- | Parses a job ID. |
361 |
parseJobId :: (Monad m) => JSON.JSValue -> m JobId |
362 |
parseJobId (JSON.JSString x) = |
363 |
tryRead "parsing job id" (JSON.fromJSString x) >>= makeJobId |
364 |
parseJobId (JSON.JSRational _ x) = |
365 |
if denominator x /= 1 |
366 |
then fail $ "Got fractional job ID from master daemon?! Value:" ++ show x |
367 |
-- FIXME: potential integer overflow here on 32-bit platforms |
368 |
else makeJobId . fromIntegral . numerator $ x |
369 |
parseJobId x = fail $ "Wrong type/value for job id: " ++ show x |
370 |
|
371 |
instance JSON.JSON JobId where |
372 |
showJSON = JSON.showJSON . fromJobId |
373 |
readJSON = parseJobId |