Add a server-side Luxi implementation
[ganeti-local] / htools / Ganeti / Config.hs
1 {-| Implementation of the Ganeti configuration database.
2
3 -}
4
5 {-
6
7 Copyright (C) 2011, 2012 Google Inc.
8
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.
13
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.
18
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
22 02110-1301, USA.
23
24 -}
25
26 module Ganeti.Config
27     ( LinkIpMap
28     , loadConfig
29     , getNodeInstances
30     , getDefaultNicLink
31     , getInstancesIpByLink
32     , getNode
33     , getInstance
34     , getInstPrimaryNode
35     , getInstMinorsForNode
36     , buildLinkIpInstnameMap
37     , instNodes
38     ) where
39
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
44
45 import Ganeti.HTools.JSON
46 import Ganeti.BasicTypes
47
48 import qualified Ganeti.Constants as C
49 import Ganeti.Objects
50
51 -- | Type alias for the link and ip map.
52 type LinkIpMap = M.Map String (M.Map String String)
53
54 -- | Reads the config file.
55 readConfig :: FilePath -> IO String
56 readConfig = readFile
57
58 -- | Parses the configuration file.
59 parseConfig :: String -> Result ConfigData
60 parseConfig = fromJResult "parsing configuration" . J.decodeStrict
61
62 -- | Wrapper over 'readConfig' and 'parseConfig'.
63 loadConfig :: FilePath -> IO (Result ConfigData)
64 loadConfig = fmap parseConfig . readConfig
65
66 -- * Query functions
67
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]
73     _ -> S.empty
74
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
77 -- the secondaries.
78 instDiskNodes :: Instance -> S.Set String
79 instDiskNodes = S.unions . map computeDiskNodes . instDisks
80
81 -- | Computes all nodes of an instance.
82 instNodes :: Instance -> S.Set String
83 instNodes inst = instPrimaryNode inst `S.insert` instDiskNodes inst
84
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
91
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)
99
100 -- | Returns the default cluster link.
101 getDefaultNicLink :: ConfigData -> String
102 getDefaultNicLink =
103   nicpLink . (M.! C.ppDefault) . clusterNicparams . configCluster
104
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
109
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
123
124 -- | Looks up a node.
125 getNode :: ConfigData -> String -> Result Node
126 getNode cfg name = getItem "Node" name (configNodes cfg)
127
128 -- | Looks up an instance.
129 getInstance :: ConfigData -> String -> Result Instance
130 getInstance cfg name = getItem "Instance" name (configInstances cfg)
131
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
136
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)
141       this_minors =
142         case diskLogicalId disk of
143           LIDDrbd8 nodeA nodeB _ minorA minorB _
144             | nodeA == node -> [(minorA, nodeB)]
145             | nodeB == node -> [(minorB, nodeA)]
146           _ -> []
147   in this_minors ++ child_minors
148
149 -- | String for primary role.
150 rolePrimary :: String
151 rolePrimary = "primary"
152
153 -- | String for secondary role.
154 roleSecondary :: String
155 roleSecondary = "secondary"
156
157 -- | Gets the list of DRBD minors for an instance that are related to
158 -- a given node.
159 getInstMinorsForNode :: String -> Instance
160                      -> [(String, Int, String, String, String, String)]
161 getInstMinorsForNode node inst =
162   let role = if node == instPrimaryNode inst
163                then rolePrimary
164                else roleSecondary
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
168   -- the Python side)
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
173
174 -- | Builds link -> ip -> instname map.
175 --
176 -- TODO: improve this by splitting it into multiple independent functions:
177 --
178 -- * abstract the \"fetch instance with filled params\" functionality
179 --
180 -- * abstsract the [instance] -> [(nic, instance_name)] part
181 --
182 -- * etc.
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])
189              instances
190   in foldl' (\accum (iname, nic) ->
191                let pparams = nicNicparams nic
192                    fparams = fillNICParams defparams pparams
193                    link = nicpLink fparams
194                in case nicIp nic of
195                     Nothing -> accum
196                     Just ip -> let oldipmap = M.findWithDefault (M.empty)
197                                               link accum
198                                    newipmap = M.insert ip iname oldipmap
199                                in M.insert link newipmap accum
200             ) M.empty nics