Store keys as ByteStrings
[ganeti-github.git] / src / Ganeti / WConfd / Ssconf.hs
1 {-| Converts a configuration state into a Ssconf map.
2
3 As TemplateHaskell require that splices be defined in a separate
4 module, we combine all the TemplateHaskell functionality that HTools
5 needs in this module (except the one for unittests).
6
7 -}
8
9 {-
10
11 Copyright (C) 2014 Google Inc.
12 All rights reserved.
13
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are
16 met:
17
18 1. Redistributions of source code must retain the above copyright notice,
19 this list of conditions and the following disclaimer.
20
21 2. Redistributions in binary form must reproduce the above copyright
22 notice, this list of conditions and the following disclaimer in the
23 documentation and/or other materials provided with the distribution.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
29 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
37 -}
38
39 module Ganeti.WConfd.Ssconf
40 ( SSConf(..)
41 , emptySSConf
42 , mkSSConf
43 ) where
44
45 import Control.Arrow ((&&&), (***), first)
46 import qualified Data.ByteString.UTF8 as UTF8
47 import Data.Foldable (Foldable(..), toList)
48 import Data.List (partition)
49 import Data.Maybe (mapMaybe)
50 import qualified Data.Map as M
51 import qualified Text.JSON as J
52
53 import Ganeti.BasicTypes
54 import Ganeti.Config
55 import Ganeti.Constants
56 import Ganeti.JSON
57 import Ganeti.Objects
58 import Ganeti.Ssconf
59 import Ganeti.Utils
60 import Ganeti.Types
61
62 eqPair :: (String, String) -> String
63 eqPair (x, y) = x ++ "=" ++ y
64
65 mkSSConfHvparams :: Cluster -> [(Hypervisor, [String])]
66 mkSSConfHvparams cluster = map (id &&& hvparams) [minBound..maxBound]
67 where
68 hvparams :: Hypervisor -> [String]
69 hvparams h = maybe [] hvparamsStrings
70 $ lookupContainer Nothing h (clusterHvparams cluster)
71
72 -- | Convert a collection of hypervisor parameters to strings in the form
73 -- @key=value@.
74 hvparamsStrings :: HvParams -> [String]
75 hvparamsStrings =
76 map (eqPair . (UTF8.toString *** hvparamShow)) . M.toList . fromContainer
77
78 -- | Convert a hypervisor parameter in its JSON representation to a String.
79 -- Strings, numbers and booleans are just printed (without quotes), booleans
80 -- printed as @True@/@False@ and other JSON values (should they exist) as
81 -- their JSON representations.
82 hvparamShow :: J.JSValue -> String
83 hvparamShow (J.JSString s) = J.fromJSString s
84 hvparamShow (J.JSRational _ r) = J.showJSRational r []
85 hvparamShow (J.JSBool b) = show b
86 hvparamShow x = J.encode x
87
88 mkSSConf :: ConfigData -> SSConf
89 mkSSConf cdata = SSConf . M.fromList $
90 [ (SSClusterName, return $ clusterClusterName cluster)
91 , (SSClusterTags, toList $ tagsOf cluster)
92 , (SSFileStorageDir, return $ clusterFileStorageDir cluster)
93 , (SSSharedFileStorageDir, return $ clusterSharedFileStorageDir cluster)
94 , (SSGlusterStorageDir, return $ clusterGlusterStorageDir cluster)
95 , (SSMasterCandidates, mapLines nodeName mcs)
96 , (SSMasterCandidatesIps, mapLines nodePrimaryIp mcs)
97 , (SSMasterCandidatesCerts, mapLines eqPair . toPairs
98 . clusterCandidateCerts $ cluster)
99 , (SSMasterIp, return $ clusterMasterIp cluster)
100 , (SSMasterNetdev, return $ clusterMasterNetdev cluster)
101 , (SSMasterNetmask, return . show $ clusterMasterNetmask cluster)
102 , (SSMasterNode, return
103 . genericResult (const "NO MASTER") nodeName
104 . getNode cdata $ clusterMasterNode cluster)
105 , (SSNodeList, mapLines nodeName nodes)
106 , (SSNodePrimaryIps, mapLines (spcPair . (nodeName &&& nodePrimaryIp))
107 nodes )
108 , (SSNodeSecondaryIps, mapLines (spcPair . (nodeName &&& nodeSecondaryIp))
109 nodes )
110 , (SSNodeVmCapable, mapLines (eqPair . (nodeName &&& show . nodeVmCapable))
111 nodes)
112 , (SSOfflineNodes, mapLines nodeName offline )
113 , (SSOnlineNodes, mapLines nodeName online )
114 , (SSPrimaryIpFamily, return . show . ipFamilyToRaw
115 . clusterPrimaryIpFamily $ cluster)
116 , (SSInstanceList, niceSort . mapMaybe instName
117 . toList . configInstances $ cdata)
118 , (SSReleaseVersion, return releaseVersion)
119 , (SSHypervisorList, mapLines hypervisorToRaw
120 . clusterEnabledHypervisors $ cluster)
121 , (SSMaintainNodeHealth, return . show . clusterMaintainNodeHealth
122 $ cluster)
123 , (SSUidPool, mapLines formatUidRange . clusterUidPool $ cluster)
124 , (SSNodegroups, mapLines (spcPair . (uuidOf &&& groupName))
125 nodeGroups)
126 , (SSNetworks, mapLines (spcPair . (uuidOf
127 &&& (fromNonEmpty . networkName)))
128 . configNetworks $ cdata)
129 , (SSEnabledUserShutdown, return . show . clusterEnabledUserShutdown
130 $ cluster)
131 , (SSSshPorts, mapLines (eqPair . (nodeName
132 &&& getSshPort cdata)) nodes)
133 ] ++
134 map (first hvparamsSSKey) (mkSSConfHvparams cluster)
135 where
136 mapLines :: (Foldable f) => (a -> String) -> f a -> [String]
137 mapLines f = map f . toList
138 spcPair (x, y) = x ++ " " ++ y
139 toPairs = M.assocs . M.mapKeys UTF8.toString . fromContainer
140
141 cluster = configCluster cdata
142 mcs = getMasterOrCandidates cdata
143 nodes = niceSortKey nodeName . toList $ configNodes cdata
144 (offline, online) = partition nodeOffline nodes
145 nodeGroups = niceSortKey groupName . toList $ configNodegroups cdata
146
147 -- This will return the empty string only for the situation where the
148 -- configuration is corrupted and no nodegroup can be found for that node.
149 getSshPort :: ConfigData -> Node -> String
150 getSshPort cfg node = maybe "" (show . ndpSshPort)
151 $ getNodeNdParams cfg node