Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / ConstantUtils.hs @ 3a715da0

History | View | Annotate | Download (5.2 kB)

1
{-| ConstantUtils contains the helper functions for constants
2

    
3
This module cannot be merged with 'Ganeti.Utils' because it would
4
create a circular dependency if imported, for example, from
5
'Ganeti.Constants'.
6

    
7
-}
8

    
9
{-
10

    
11
Copyright (C) 2013 Google Inc.
12

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

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

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

    
28
-}
29
module Ganeti.ConstantUtils where
30

    
31
import Data.Char (ord)
32
import Data.Set (Set)
33
import qualified Data.Set as Set (difference, fromList, toList, union)
34

    
35
import Ganeti.THH (PyValue(..))
36
import Ganeti.PyValueInstances ()
37

    
38
-- | PythonChar wraps a Python 'char'
39
newtype PythonChar = PythonChar { unPythonChar :: Char }
40
  deriving (Show)
41

    
42
instance PyValue PythonChar where
43
  showValue c = "chr(" ++ show (ord (unPythonChar c)) ++ ")"
44

    
45
-- | FrozenSet wraps a Haskell 'Set'
46
--
47
-- See 'PyValue' instance for 'FrozenSet'.
48
newtype FrozenSet a = FrozenSet { unFrozenSet :: Set a }
49
  deriving (Eq, Ord, Show)
50

    
51
-- | Converts a Haskell 'Set' into a Python 'frozenset'
52
--
53
-- This instance was supposed to be for 'Set' instead of 'FrozenSet'.
54
-- However, 'ghc-6.12.1' seems to be crashing with 'segmentation
55
-- fault' due to the presence of more than one instance of 'Set',
56
-- namely, this one and the one in 'Ganeti.OpCodes'.  For this reason,
57
-- we wrap 'Set' into 'FrozenSet'.
58
instance PyValue a => PyValue (FrozenSet a) where
59
  showValue s = "frozenset(" ++ showValue (Set.toList (unFrozenSet s)) ++ ")"
60

    
61
mkSet :: Ord a => [a] -> FrozenSet a
62
mkSet = FrozenSet . Set.fromList
63

    
64
union :: Ord a => FrozenSet a -> FrozenSet a -> FrozenSet a
65
union x y = FrozenSet (unFrozenSet x `Set.union` unFrozenSet y)
66

    
67
difference :: Ord a => FrozenSet a -> FrozenSet a -> FrozenSet a
68
difference x y = FrozenSet (unFrozenSet x `Set.difference` unFrozenSet y)
69

    
70
-- | 'Protocol' represents the protocols used by the daemons
71
data Protocol = Tcp | Udp
72
  deriving (Show)
73

    
74
-- | 'PyValue' instance of 'Protocol'
75
--
76
-- This instance is used by the Haskell to Python constants
77
instance PyValue Protocol where
78
  showValue Tcp = "\"tcp\""
79
  showValue Udp = "\"udp\""
80

    
81
-- | Failure exit code
82
--
83
-- These are defined here and not in 'Ganeti.HsConstants' together with
84
-- the other exit codes in order to avoid a circular dependency
85
-- between 'Ganeti.HsConstants' and 'Ganeti.Runtime'
86
exitFailure :: Int
87
exitFailure = 1
88

    
89
-- | Console device
90
--
91
-- This is defined here and not in 'Ganeti.HsConstants' order to avoid
92
-- a circular dependency between 'Ganeti.HsConstants' and
93
-- 'Ganeti.Logging'
94
devConsole :: String
95
devConsole = "/dev/console"
96

    
97
-- | Random uuid generator
98
--
99
-- This is defined here and not in 'Ganeti.HsConstants' order to avoid
100
-- a circular dependendy between 'Ganeti.HsConstants' and
101
-- 'Ganeti.Types'
102
randomUuidFile :: String
103
randomUuidFile = "/proc/sys/kernel/random/uuid"
104

    
105
-- * Priority levels
106
--
107
-- This is defined here and not in 'Ganeti.Types' in order to avoid a
108
-- GHC stage restriction and because there is no suitable 'declareADT'
109
-- variant that handles integer values directly.
110

    
111
priorityLow :: Int
112
priorityLow = 10
113

    
114
priorityNormal :: Int
115
priorityNormal = 0
116

    
117
priorityHigh :: Int
118
priorityHigh = -10
119

    
120
-- | Calculates int version number from major, minor and revision
121
-- numbers.
122
buildVersion :: Int -> Int -> Int -> Int
123
buildVersion major minor revision =
124
  1000000 * major + 10000 * minor + 1 * revision
125

    
126
-- | Confd protocol version
127
--
128
-- This is defined here in order to avoid a circular dependency
129
-- between 'Ganeti.Confd.Types' and 'Ganeti.HsConstants'.
130
confdProtocolVersion :: Int
131
confdProtocolVersion = 1
132

    
133
-- * Confd request query fields
134
--
135
-- These are defined here and not in 'Ganeti.Types' due to GHC stage
136
-- restrictions concerning Template Haskell.  They are also not
137
-- defined in 'Ganeti.HsConstants' in order to avoid a circular
138
-- dependency between that module and 'Ganeti.Types'.
139

    
140
confdReqqLink :: String
141
confdReqqLink = "0"
142

    
143
confdReqqIp :: String
144
confdReqqIp = "1"
145

    
146
confdReqqIplist :: String
147
confdReqqIplist = "2"
148

    
149
confdReqqFields :: String
150
confdReqqFields = "3"
151

    
152
-- * ISpec
153

    
154
ispecMemSize :: String
155
ispecMemSize = "memory-size"
156

    
157
ispecCpuCount :: String
158
ispecCpuCount = "cpu-count"
159

    
160
ispecDiskCount :: String
161
ispecDiskCount = "disk-count"
162

    
163
ispecDiskSize :: String
164
ispecDiskSize = "disk-size"
165

    
166
ispecNicCount :: String
167
ispecNicCount = "nic-count"
168

    
169
ispecSpindleUse :: String
170
ispecSpindleUse = "spindle-use"
171

    
172
ispecsMinmax :: String
173
ispecsMinmax = "minmax"
174

    
175
ispecsStd :: String
176
ispecsStd = "std"
177

    
178
ipolicyDts :: String
179
ipolicyDts = "disk-templates"
180

    
181
ipolicyVcpuRatio :: String
182
ipolicyVcpuRatio = "vcpu-ratio"
183

    
184
ipolicySpindleRatio :: String
185
ipolicySpindleRatio = "spindle-ratio"
186

    
187
ipolicyDefaultsVcpuRatio :: Double
188
ipolicyDefaultsVcpuRatio = 4.0
189

    
190
ipolicyDefaultsSpindleRatio :: Double
191
ipolicyDefaultsSpindleRatio = 32.0