Revision da45c352

b/htools/Ganeti/Config.hs
25 25

  
26 26
module Ganeti.Config
27 27
    ( LinkIpMap
28
    , NdParamObject(..)
28 29
    , loadConfig
29 30
    , getNodeInstances
31
    , getNodeRole
32
    , getNodeNdParams
30 33
    , getDefaultNicLink
31 34
    , getInstancesIpByLink
32 35
    , getNode
33 36
    , getInstance
34 37
    , getGroup
38
    , getGroupNdParams
39
    , getGroupOfNode
35 40
    , getInstPrimaryNode
36 41
    , getInstMinorsForNode
37 42
    , buildLinkIpInstnameMap
......
53 58
-- | Type alias for the link and ip map.
54 59
type LinkIpMap = M.Map String (M.Map String String)
55 60

  
61
-- | Type class denoting objects which have node parameters.
62
class NdParamObject a where
63
  getNdParamsOf :: ConfigData -> a -> Maybe FilledNDParams
64

  
56 65
-- | Reads the config file.
57 66
readConfig :: FilePath -> IO String
58 67
readConfig = readFile
......
99 108
        sec_inst = filter ((nname `S.member`) . instSecondaryNodes) all_inst
100 109
    in (pri_inst, sec_inst)
101 110

  
111
-- | Computes the role of a node.
112
getNodeRole :: ConfigData -> Node -> NodeRole
113
getNodeRole cfg node
114
  | nodeName node == (clusterMasterNode $ configCluster cfg) = NRMaster
115
  | nodeMasterCandidate node = NRCandidate
116
  | nodeDrained node = NRDrained
117
  | nodeOffline node = NROffline
118
  | otherwise = NRRegular
119

  
102 120
-- | Returns the default cluster link.
103 121
getDefaultNicLink :: ConfigData -> String
104 122
getDefaultNicLink =
......
145 163
                              (\k -> groupName ((M.!) groups k )) groups
146 164
                in getItem "NodeGroup" name by_name
147 165

  
166
-- | Computes a node group's node params.
167
getGroupNdParams :: ConfigData -> NodeGroup -> FilledNDParams
168
getGroupNdParams cfg ng =
169
  fillNDParams (clusterNdparams $ configCluster cfg) (groupNdparams ng)
170

  
148 171
-- | Looks up an instance's primary node.
149 172
getInstPrimaryNode :: ConfigData -> String -> Result Node
150 173
getInstPrimaryNode cfg name =
......
214 237
                                   newipmap = M.insert ip iname oldipmap
215 238
                               in M.insert link newipmap accum
216 239
            ) M.empty nics
240

  
241

  
242
-- | Returns a node's group, with optional failure if we can't find it
243
-- (configuration corrupt).
244
getGroupOfNode :: ConfigData -> Node -> Maybe NodeGroup
245
getGroupOfNode cfg node =
246
  M.lookup (nodeGroup node) (fromContainer . configNodegroups $ cfg)
247

  
248
-- | Returns a node's ndparams, filled.
249
getNodeNdParams :: ConfigData -> Node -> Maybe FilledNDParams
250
getNodeNdParams cfg node = do
251
  group <- getGroupOfNode cfg node
252
  let gparams = getGroupNdParams cfg group
253
  return $ fillNDParams gparams (nodeNdparams node)
254

  
255
instance NdParamObject Node where
256
  getNdParamsOf = getNodeNdParams
257

  
258
instance NdParamObject NodeGroup where
259
  getNdParamsOf cfg = Just . getGroupNdParams cfg
260

  
261
instance NdParamObject Cluster where
262
  getNdParamsOf _ = Just . clusterNdparams
b/htools/Ganeti/Objects.hs
53 53
  , FilledNDParams(..)
54 54
  , fillNDParams
55 55
  , Node(..)
56
  , NodeRole(..)
57
  , nodeRoleToRaw
58
  , roleDescription
56 59
  , AllocPolicy(..)
57 60
  , FilledISpecParams(..)
58 61
  , PartialISpecParams(..)
......
125 128
class TagsObject a where
126 129
  tagsOf :: a -> Set.Set String
127 130

  
131
-- * Node role object
132

  
133
$(declareSADT "NodeRole"
134
  [ ("NROffline",   'C.nrOffline)
135
  , ("NRDrained",   'C.nrDrained)
136
  , ("NRRegular",   'C.nrRegular)
137
  , ("NRCandidate", 'C.nrMcandidate)
138
  , ("NRMaster",    'C.nrMaster)
139
  ])
140
$(makeJSONInstance ''NodeRole)
141

  
142
-- | The description of the node role.
143
roleDescription :: NodeRole -> String
144
roleDescription NROffline   = "offline"
145
roleDescription NRDrained   = "drained"
146
roleDescription NRRegular   = "regular"
147
roleDescription NRCandidate = "master candidate"
148
roleDescription NRMaster    = "master"
149

  
128 150
-- * NIC definitions
129 151

  
130 152
$(declareSADT "NICMode"

Also available in: Unified diff