1 {-| Implementation of the Ganeti configuration database.
7 Copyright (C) 2011, 2012 Google Inc.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34 , getInstancesIpByLink
44 , getInstMinorsForNode
45 , buildLinkIpInstnameMap
49 import Control.Monad (liftM)
50 import Data.List (foldl')
51 import qualified Data.Map as M
52 import qualified Data.Set as S
53 import qualified Text.JSON as J
56 import Ganeti.BasicTypes
58 import qualified Ganeti.Constants as C
61 -- | Type alias for the link and ip map.
62 type LinkIpMap = M.Map String (M.Map String String)
64 -- | Type class denoting objects which have node parameters.
65 class NdParamObject a where
66 getNdParamsOf :: ConfigData -> a -> Maybe FilledNDParams
68 -- | Reads the config file.
69 readConfig :: FilePath -> IO String
72 -- | Parses the configuration file.
73 parseConfig :: String -> Result ConfigData
74 parseConfig = fromJResult "parsing configuration" . J.decodeStrict
76 -- | Wrapper over 'readConfig' and 'parseConfig'.
77 loadConfig :: FilePath -> IO (Result ConfigData)
78 loadConfig = fmap parseConfig . readConfig
82 -- | Computes the nodes covered by a disk.
83 computeDiskNodes :: Disk -> S.Set String
84 computeDiskNodes dsk =
85 case diskLogicalId dsk of
86 LIDDrbd8 nodeA nodeB _ _ _ _ -> S.fromList [nodeA, nodeB]
89 -- | Computes all disk-related nodes of an instance. For non-DRBD,
90 -- this will be empty, for DRBD it will contain both the primary and
92 instDiskNodes :: Instance -> S.Set String
93 instDiskNodes = S.unions . map computeDiskNodes . instDisks
95 -- | Computes all nodes of an instance.
96 instNodes :: Instance -> S.Set String
97 instNodes inst = instPrimaryNode inst `S.insert` instDiskNodes inst
99 -- | Computes the secondary nodes of an instance. Since this is valid
100 -- only for DRBD, we call directly 'instDiskNodes', skipping over the
101 -- extra primary insert.
102 instSecondaryNodes :: Instance -> S.Set String
103 instSecondaryNodes inst =
104 instPrimaryNode inst `S.delete` instDiskNodes inst
106 -- | Get instances of a given node.
107 getNodeInstances :: ConfigData -> String -> ([Instance], [Instance])
108 getNodeInstances cfg nname =
109 let all_inst = M.elems . fromContainer . configInstances $ cfg
110 pri_inst = filter ((== nname) . instPrimaryNode) all_inst
111 sec_inst = filter ((nname `S.member`) . instSecondaryNodes) all_inst
112 in (pri_inst, sec_inst)
114 -- | Computes the role of a node.
115 getNodeRole :: ConfigData -> Node -> NodeRole
117 | nodeName node == clusterMasterNode (configCluster cfg) = NRMaster
118 | nodeMasterCandidate node = NRCandidate
119 | nodeDrained node = NRDrained
120 | nodeOffline node = NROffline
121 | otherwise = NRRegular
123 -- | Returns the default cluster link.
124 getDefaultNicLink :: ConfigData -> String
126 nicpLink . (M.! C.ppDefault) . fromContainer .
127 clusterNicparams . configCluster
129 -- | Returns instances of a given link.
130 getInstancesIpByLink :: LinkIpMap -> String -> [String]
131 getInstancesIpByLink linkipmap link =
132 M.keys $ M.findWithDefault M.empty link linkipmap
134 -- | Generic lookup function that converts from a possible abbreviated
135 -- name to a full name.
136 getItem :: String -> String -> M.Map String a -> Result a
137 getItem kind name allitems = do
138 let lresult = lookupName (M.keys allitems) name
139 err msg = Bad $ kind ++ " name " ++ name ++ " " ++ msg
140 fullname <- case lrMatchPriority lresult of
141 PartialMatch -> Ok $ lrContent lresult
142 ExactMatch -> Ok $ lrContent lresult
143 MultipleMatch -> err "has multiple matches"
144 FailMatch -> err "not found"
145 maybe (err "not found after successfull match?!") Ok $
146 M.lookup fullname allitems
148 -- | Looks up a node.
149 getNode :: ConfigData -> String -> Result Node
150 getNode cfg name = getItem "Node" name (fromContainer $ configNodes cfg)
152 -- | Looks up an instance.
153 getInstance :: ConfigData -> String -> Result Instance
154 getInstance cfg name =
155 getItem "Instance" name (fromContainer $ configInstances cfg)
157 -- | Looks up a node group. This is more tricky than for
158 -- node/instances since the groups map is indexed by uuid, not name.
159 getGroup :: ConfigData -> String -> Result NodeGroup
161 let groups = fromContainer (configNodegroups cfg)
162 in case getItem "NodeGroup" name groups of
163 -- if not found by uuid, we need to look it up by name, slow
165 Bad _ -> let by_name = M.mapKeys
166 (groupName . (M.!) groups) groups
167 in getItem "NodeGroup" name by_name
169 -- | Computes a node group's node params.
170 getGroupNdParams :: ConfigData -> NodeGroup -> FilledNDParams
171 getGroupNdParams cfg ng =
172 fillNDParams (clusterNdparams $ configCluster cfg) (groupNdparams ng)
174 -- | Computes a node group's ipolicy.
175 getGroupIpolicy :: ConfigData -> NodeGroup -> FilledIPolicy
176 getGroupIpolicy cfg ng =
177 fillIPolicy (clusterIpolicy $ configCluster cfg) (groupIpolicy ng)
179 -- | Get nodes of a given node group.
180 getGroupNodes :: ConfigData -> String -> [Node]
181 getGroupNodes cfg gname =
182 let all_nodes = M.elems . fromContainer . configNodes $ cfg in
183 filter ((==gname) . nodeGroup) all_nodes
185 -- | Get (primary, secondary) instances of a given node group.
186 getGroupInstances :: ConfigData -> String -> ([Instance], [Instance])
187 getGroupInstances cfg gname =
188 let gnodes = map nodeName (getGroupNodes cfg gname)
189 ginsts = map (getNodeInstances cfg) gnodes in
190 (concatMap fst ginsts, concatMap snd ginsts)
192 -- | Looks up an instance's primary node.
193 getInstPrimaryNode :: ConfigData -> String -> Result Node
194 getInstPrimaryNode cfg name =
195 liftM instPrimaryNode (getInstance cfg name) >>= getNode cfg
197 -- | Filters DRBD minors for a given node.
198 getDrbdMinorsForNode :: String -> Disk -> [(Int, String)]
199 getDrbdMinorsForNode node disk =
200 let child_minors = concatMap (getDrbdMinorsForNode node) (diskChildren disk)
202 case diskLogicalId disk of
203 LIDDrbd8 nodeA nodeB _ minorA minorB _
204 | nodeA == node -> [(minorA, nodeB)]
205 | nodeB == node -> [(minorB, nodeA)]
207 in this_minors ++ child_minors
209 -- | String for primary role.
210 rolePrimary :: String
211 rolePrimary = "primary"
213 -- | String for secondary role.
214 roleSecondary :: String
215 roleSecondary = "secondary"
217 -- | Gets the list of DRBD minors for an instance that are related to
219 getInstMinorsForNode :: String -> Instance
220 -> [(String, Int, String, String, String, String)]
221 getInstMinorsForNode node inst =
222 let role = if node == instPrimaryNode inst
225 iname = instName inst
226 -- FIXME: the disk/ build there is hack-ish; unify this in a
227 -- separate place, or reuse the iv_name (but that is deprecated on
229 in concatMap (\(idx, dsk) ->
230 [(node, minor, iname, "disk/" ++ show idx, role, peer)
231 | (minor, peer) <- getDrbdMinorsForNode node dsk]) .
232 zip [(0::Int)..] . instDisks $ inst
234 -- | Builds link -> ip -> instname map.
236 -- TODO: improve this by splitting it into multiple independent functions:
238 -- * abstract the \"fetch instance with filled params\" functionality
240 -- * abstsract the [instance] -> [(nic, instance_name)] part
243 buildLinkIpInstnameMap :: ConfigData -> LinkIpMap
244 buildLinkIpInstnameMap cfg =
245 let cluster = configCluster cfg
246 instances = M.elems . fromContainer . configInstances $ cfg
247 defparams = (M.!) (fromContainer $ clusterNicparams cluster) C.ppDefault
248 nics = concatMap (\i -> [(instName i, nic) | nic <- instNics i])
250 in foldl' (\accum (iname, nic) ->
251 let pparams = nicNicparams nic
252 fparams = fillNicParams defparams pparams
253 link = nicpLink fparams
256 Just ip -> let oldipmap = M.findWithDefault M.empty
258 newipmap = M.insert ip iname oldipmap
259 in M.insert link newipmap accum
263 -- | Returns a node's group, with optional failure if we can't find it
264 -- (configuration corrupt).
265 getGroupOfNode :: ConfigData -> Node -> Maybe NodeGroup
266 getGroupOfNode cfg node =
267 M.lookup (nodeGroup node) (fromContainer . configNodegroups $ cfg)
269 -- | Returns a node's ndparams, filled.
270 getNodeNdParams :: ConfigData -> Node -> Maybe FilledNDParams
271 getNodeNdParams cfg node = do
272 group <- getGroupOfNode cfg node
273 let gparams = getGroupNdParams cfg group
274 return $ fillNDParams gparams (nodeNdparams node)
276 instance NdParamObject Node where
277 getNdParamsOf = getNodeNdParams
279 instance NdParamObject NodeGroup where
280 getNdParamsOf cfg = Just . getGroupNdParams cfg
282 instance NdParamObject Cluster where
283 getNdParamsOf _ = Just . clusterNdparams