1 {-# LANGUAGE TemplateHaskell #-}
3 {-| Some common Ganeti types.
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'.
14 Copyright (C) 2012 Google Inc.
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.
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.
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
38 , instanceStatusFromRaw
54 , VerifyOptionalChecks(..)
64 import qualified Text.JSON as JSON
66 import qualified Ganeti.Constants as C
67 import qualified Ganeti.THH as THH
72 -- | Type that holds a non-negative value.
73 newtype NonNegative a = NonNegative { fromNonNegative :: a }
74 deriving (Show, Read, Eq)
76 -- | Smart constructor for 'NonNegative'.
77 mkNonNegative :: (Monad m, Num a, Ord a, Show a) => a -> m (NonNegative a)
78 mkNonNegative i | i >= 0 = return (NonNegative i)
79 | otherwise = fail $ "Invalid value for non-negative type '" ++
82 instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (NonNegative a) where
83 showJSON = JSON.showJSON . fromNonNegative
84 readJSON v = JSON.readJSON v >>= mkNonNegative
86 -- | Type that holds a positive value.
87 newtype Positive a = Positive { fromPositive :: a }
88 deriving (Show, Read, Eq)
90 -- | Smart constructor for 'Positive'.
91 mkPositive :: (Monad m, Num a, Ord a, Show a) => a -> m (Positive a)
92 mkPositive i | i > 0 = return (Positive i)
93 | otherwise = fail $ "Invalid value for positive type '" ++
96 instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (Positive a) where
97 showJSON = JSON.showJSON . fromPositive
98 readJSON v = JSON.readJSON v >>= mkPositive
100 -- | Type that holds a non-null list.
101 newtype NonEmpty a = NonEmpty { fromNonEmpty :: [a] }
102 deriving (Show, Read, Eq)
104 -- | Smart constructor for 'NonEmpty'.
105 mkNonEmpty :: (Monad m) => [a] -> m (NonEmpty a)
106 mkNonEmpty [] = fail "Received empty value for non-empty list"
107 mkNonEmpty xs = return (NonEmpty xs)
109 instance (JSON.JSON a) => JSON.JSON (NonEmpty a) where
110 showJSON = JSON.showJSON . fromNonEmpty
111 readJSON v = JSON.readJSON v >>= mkNonEmpty
113 -- | A simple type alias for non-empty strings.
114 type NonEmptyString = NonEmpty Char
118 -- | Instance disk template type.
119 $(THH.declareSADT "DiskTemplate"
120 [ ("DTDiskless", 'C.dtDiskless)
121 , ("DTFile", 'C.dtFile)
122 , ("DTSharedFile", 'C.dtSharedFile)
123 , ("DTPlain", 'C.dtPlain)
124 , ("DTBlock", 'C.dtBlock)
125 , ("DTDrbd8", 'C.dtDrbd8)
126 , ("DTRbd", 'C.dtRbd)
128 $(THH.makeJSONInstance ''DiskTemplate)
130 instance HasStringRepr DiskTemplate where
131 fromStringRepr = diskTemplateFromRaw
132 toStringRepr = diskTemplateToRaw
134 -- | The Group allocation policy type.
136 -- Note that the order of constructors is important as the automatic
137 -- Ord instance will order them in the order they are defined, so when
138 -- changing this data type be careful about the interaction with the
139 -- desired sorting order.
140 $(THH.declareSADT "AllocPolicy"
141 [ ("AllocPreferred", 'C.allocPolicyPreferred)
142 , ("AllocLastResort", 'C.allocPolicyLastResort)
143 , ("AllocUnallocable", 'C.allocPolicyUnallocable)
145 $(THH.makeJSONInstance ''AllocPolicy)
147 -- | The Instance real state type. FIXME: this could be improved to
148 -- just wrap a /NormalState AdminStatus | ErrorState ErrorCondition/.
149 $(THH.declareSADT "InstanceStatus"
150 [ ("StatusDown", 'C.inststAdmindown)
151 , ("StatusOffline", 'C.inststAdminoffline)
152 , ("ErrorDown", 'C.inststErrordown)
153 , ("ErrorUp", 'C.inststErrorup)
154 , ("NodeDown", 'C.inststNodedown)
155 , ("NodeOffline", 'C.inststNodeoffline)
156 , ("Running", 'C.inststRunning)
157 , ("WrongNode", 'C.inststWrongnode)
159 $(THH.makeJSONInstance ''InstanceStatus)
162 $(THH.declareSADT "MigrationMode"
163 [ ("MigrationLive", 'C.htMigrationLive)
164 , ("MigrationNonLive", 'C.htMigrationNonlive)
166 $(THH.makeJSONInstance ''MigrationMode)
168 -- | Verify optional checks.
169 $(THH.declareSADT "VerifyOptionalChecks"
170 [ ("VerifyNPlusOneMem", 'C.verifyNplusoneMem)
172 $(THH.makeJSONInstance ''VerifyOptionalChecks)
174 -- | Cluster verify error codes.
175 $(THH.declareSADT "CVErrorCode"
176 [ ("CvECLUSTERCFG", 'C.cvEclustercfgCode)
177 , ("CvECLUSTERCERT", 'C.cvEclustercertCode)
178 , ("CvECLUSTERFILECHECK", 'C.cvEclusterfilecheckCode)
179 , ("CvECLUSTERDANGLINGNODES", 'C.cvEclusterdanglingnodesCode)
180 , ("CvECLUSTERDANGLINGINST", 'C.cvEclusterdanglinginstCode)
181 , ("CvEINSTANCEBADNODE", 'C.cvEinstancebadnodeCode)
182 , ("CvEINSTANCEDOWN", 'C.cvEinstancedownCode)
183 , ("CvEINSTANCELAYOUT", 'C.cvEinstancelayoutCode)
184 , ("CvEINSTANCEMISSINGDISK", 'C.cvEinstancemissingdiskCode)
185 , ("CvEINSTANCEFAULTYDISK", 'C.cvEinstancefaultydiskCode)
186 , ("CvEINSTANCEWRONGNODE", 'C.cvEinstancewrongnodeCode)
187 , ("CvEINSTANCESPLITGROUPS", 'C.cvEinstancesplitgroupsCode)
188 , ("CvEINSTANCEPOLICY", 'C.cvEinstancepolicyCode)
189 , ("CvENODEDRBD", 'C.cvEnodedrbdCode)
190 , ("CvENODEDRBDHELPER", 'C.cvEnodedrbdhelperCode)
191 , ("CvENODEFILECHECK", 'C.cvEnodefilecheckCode)
192 , ("CvENODEHOOKS", 'C.cvEnodehooksCode)
193 , ("CvENODEHV", 'C.cvEnodehvCode)
194 , ("CvENODELVM", 'C.cvEnodelvmCode)
195 , ("CvENODEN1", 'C.cvEnoden1Code)
196 , ("CvENODENET", 'C.cvEnodenetCode)
197 , ("CvENODEOS", 'C.cvEnodeosCode)
198 , ("CvENODEORPHANINSTANCE", 'C.cvEnodeorphaninstanceCode)
199 , ("CvENODEORPHANLV", 'C.cvEnodeorphanlvCode)
200 , ("CvENODERPC", 'C.cvEnoderpcCode)
201 , ("CvENODESSH", 'C.cvEnodesshCode)
202 , ("CvENODEVERSION", 'C.cvEnodeversionCode)
203 , ("CvENODESETUP", 'C.cvEnodesetupCode)
204 , ("CvENODETIME", 'C.cvEnodetimeCode)
205 , ("CvENODEOOBPATH", 'C.cvEnodeoobpathCode)
206 , ("CvENODEUSERSCRIPTS", 'C.cvEnodeuserscriptsCode)
207 , ("CvENODEFILESTORAGEPATHS", 'C.cvEnodefilestoragepathsCode)
209 $(THH.makeJSONInstance ''CVErrorCode)
211 -- | Dynamic device modification, just add\/remove version.
212 $(THH.declareSADT "DdmSimple"
213 [ ("DdmSimpleAdd", 'C.ddmAdd)
214 , ("DdmSimpleRemove", 'C.ddmRemove)
216 $(THH.makeJSONInstance ''DdmSimple)
218 -- | Hypervisor type definitions.
219 $(THH.declareSADT "Hypervisor"
220 [ ( "Kvm", 'C.htKvm )
221 , ( "XenPvm", 'C.htXenPvm )
222 , ( "Chroot", 'C.htChroot )
223 , ( "XenHvm", 'C.htXenHvm )
224 , ( "Lxc", 'C.htLxc )
225 , ( "Fake", 'C.htFake )
227 $(THH.makeJSONInstance ''Hypervisor)
229 -- | Oob command type.
230 $(THH.declareSADT "OobCommand"
231 [ ("OobHealth", 'C.oobHealth)
232 , ("OobPowerCycle", 'C.oobPowerCycle)
233 , ("OobPowerOff", 'C.oobPowerOff)
234 , ("OobPowerOn", 'C.oobPowerOn)
235 , ("OobPowerStatus", 'C.oobPowerStatus)
237 $(THH.makeJSONInstance ''OobCommand)
240 $(THH.declareSADT "StorageType"
241 [ ("StorageFile", 'C.stFile)
242 , ("StorageLvmPv", 'C.stLvmPv)
243 , ("StorageLvmVg", 'C.stLvmVg)
245 $(THH.makeJSONInstance ''StorageType)
247 -- | Node evac modes.
248 $(THH.declareSADT "NodeEvacMode"
249 [ ("NEvacPrimary", 'C.iallocatorNevacPri)
250 , ("NEvacSecondary", 'C.iallocatorNevacSec)
251 , ("NEvacAll", 'C.iallocatorNevacAll)
253 $(THH.makeJSONInstance ''NodeEvacMode)