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
31 , getInstancesIpByLink
35 , getInstMinorsForNode
36 , buildLinkIpInstnameMap
40 import Data.List (foldl')
41 import qualified Data.Map as M
42 import qualified Data.Set as S
43 import qualified Text.JSON as J
45 import Ganeti.HTools.JSON
46 import Ganeti.BasicTypes
48 import qualified Ganeti.Constants as C
51 -- | Type alias for the link and ip map.
52 type LinkIpMap = M.Map String (M.Map String String)
54 -- | Reads the config file.
55 readConfig :: FilePath -> IO String
58 -- | Parses the configuration file.
59 parseConfig :: String -> Result ConfigData
60 parseConfig = fromJResult "parsing configuration" . J.decodeStrict
62 -- | Wrapper over 'readConfig' and 'parseConfig'.
63 loadConfig :: FilePath -> IO (Result ConfigData)
64 loadConfig = fmap parseConfig . readConfig
68 -- | Computes the nodes covered by a disk.
69 computeDiskNodes :: Disk -> S.Set String
70 computeDiskNodes dsk =
71 case diskLogicalId dsk of
72 LIDDrbd8 nodeA nodeB _ _ _ _ -> S.fromList [nodeA, nodeB]
75 -- | Computes all disk-related nodes of an instance. For non-DRBD,
76 -- this will be empty, for DRBD it will contain both the primary and
78 instDiskNodes :: Instance -> S.Set String
79 instDiskNodes = S.unions . map computeDiskNodes . instDisks
81 -- | Computes all nodes of an instance.
82 instNodes :: Instance -> S.Set String
83 instNodes inst = instPrimaryNode inst `S.insert` instDiskNodes inst
85 -- | Computes the secondary nodes of an instance. Since this is valid
86 -- only for DRBD, we call directly 'instDiskNodes', skipping over the
87 -- extra primary insert.
88 instSecondaryNodes :: Instance -> S.Set String
89 instSecondaryNodes inst =
90 instPrimaryNode inst `S.delete` instDiskNodes inst
92 -- | Get instances of a given node.
93 getNodeInstances :: ConfigData -> String -> ([Instance], [Instance])
94 getNodeInstances cfg nname =
95 let all_inst = M.elems . configInstances $ cfg
96 pri_inst = filter ((== nname) . instPrimaryNode) all_inst
97 sec_inst = filter ((nname `S.member`) . instSecondaryNodes) all_inst
98 in (pri_inst, sec_inst)
100 -- | Returns the default cluster link.
101 getDefaultNicLink :: ConfigData -> String
103 nicpLink . (M.! C.ppDefault) . clusterNicparams . configCluster
105 -- | Returns instances of a given link.
106 getInstancesIpByLink :: LinkIpMap -> String -> [String]
107 getInstancesIpByLink linkipmap link =
108 M.keys $ M.findWithDefault M.empty link linkipmap
110 -- | Generic lookup function that converts from a possible abbreviated
111 -- name to a full name.
112 getItem :: String -> String -> M.Map String a -> Result a
113 getItem kind name allitems = do
114 let lresult = lookupName (M.keys allitems) name
115 err = \details -> Bad $ kind ++ " name " ++ name ++ " " ++ details
116 fullname <- case lrMatchPriority lresult of
117 PartialMatch -> Ok $ lrContent lresult
118 ExactMatch -> Ok $ lrContent lresult
119 MultipleMatch -> err "has multiple matches"
120 FailMatch -> err "not found"
121 maybe (err "not found after successfull match?!") Ok $
122 M.lookup fullname allitems
124 -- | Looks up a node.
125 getNode :: ConfigData -> String -> Result Node
126 getNode cfg name = getItem "Node" name (configNodes cfg)
128 -- | Looks up an instance.
129 getInstance :: ConfigData -> String -> Result Instance
130 getInstance cfg name = getItem "Instance" name (configInstances cfg)
132 -- | Looks up an instance's primary node.
133 getInstPrimaryNode :: ConfigData -> String -> Result Node
134 getInstPrimaryNode cfg name =
135 getInstance cfg name >>= return . instPrimaryNode >>= getNode cfg
137 -- | Filters DRBD minors for a given node.
138 getDrbdMinorsForNode :: String -> Disk -> [(Int, String)]
139 getDrbdMinorsForNode node disk =
140 let child_minors = concatMap (getDrbdMinorsForNode node) (diskChildren disk)
142 case diskLogicalId disk of
143 LIDDrbd8 nodeA nodeB _ minorA minorB _
144 | nodeA == node -> [(minorA, nodeB)]
145 | nodeB == node -> [(minorB, nodeA)]
147 in this_minors ++ child_minors
149 -- | String for primary role.
150 rolePrimary :: String
151 rolePrimary = "primary"
153 -- | String for secondary role.
154 roleSecondary :: String
155 roleSecondary = "secondary"
157 -- | Gets the list of DRBD minors for an instance that are related to
159 getInstMinorsForNode :: String -> Instance
160 -> [(String, Int, String, String, String, String)]
161 getInstMinorsForNode node inst =
162 let role = if node == instPrimaryNode inst
165 iname = instName inst
166 -- FIXME: the disk/ build there is hack-ish; unify this in a
167 -- separate place, or reuse the iv_name (but that is deprecated on
169 in concatMap (\(idx, dsk) ->
170 [(node, minor, iname, "disk/" ++ show idx, role, peer)
171 | (minor, peer) <- getDrbdMinorsForNode node dsk]) .
172 zip [(0::Int)..] . instDisks $ inst
174 -- | Builds link -> ip -> instname map.
176 -- TODO: improve this by splitting it into multiple independent functions:
178 -- * abstract the \"fetch instance with filled params\" functionality
180 -- * abstsract the [instance] -> [(nic, instance_name)] part
183 buildLinkIpInstnameMap :: ConfigData -> LinkIpMap
184 buildLinkIpInstnameMap cfg =
185 let cluster = configCluster cfg
186 instances = M.elems . configInstances $ cfg
187 defparams = (M.!) (clusterNicparams cluster) C.ppDefault
188 nics = concatMap (\i -> [(instName i, nic) | nic <- instNics i])
190 in foldl' (\accum (iname, nic) ->
191 let pparams = nicNicparams nic
192 fparams = fillNICParams defparams pparams
193 link = nicpLink fparams
196 Just ip -> let oldipmap = M.findWithDefault (M.empty)
198 newipmap = M.insert ip iname oldipmap
199 in M.insert link newipmap accum