Prefer the UuidObject type class over specific functions
[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, second)
46 import Data.Foldable (Foldable(..), toList)
47 import Data.List (partition)
48 import Data.Maybe (mapMaybe)
49 import qualified Data.Map as M
50 import qualified Text.JSON as J
51
52 import Ganeti.BasicTypes
53 import Ganeti.Config
54 import Ganeti.Constants
55 import Ganeti.JSON
56 import Ganeti.Objects
57 import Ganeti.Ssconf
58 import Ganeti.Utils
59 import Ganeti.Types
60
61 eqPair :: (String, String) -> String
62 eqPair (x, y) = x ++ "=" ++ y
63
64 mkSSConfHvparams :: Cluster -> [(Hypervisor, [String])]
65 mkSSConfHvparams cluster = map (id &&& hvparams) [minBound..maxBound]
66 where
67 hvparams :: Hypervisor -> [String]
68 hvparams h = maybe [] hvparamsStrings
69 $ lookupContainer Nothing h (clusterHvparams cluster)
70
71 -- | Convert a collection of hypervisor parameters to strings in the form
72 -- @key=value@.
73 hvparamsStrings :: HvParams -> [String]
74 hvparamsStrings =
75 map (eqPair . second hvparamShow) . M.toList . fromContainer
76
77 -- | Convert a hypervisor parameter in its JSON representation to a String.
78 -- Strings, numbers and booleans are just printed (without quotes), booleans
79 -- printed as @True@/@False@ and other JSON values (should they exist) as
80 -- their JSON representations.
81 hvparamShow :: J.JSValue -> String
82 hvparamShow (J.JSString s) = J.fromJSString s
83 hvparamShow (J.JSRational _ r) = J.showJSRational r []
84 hvparamShow (J.JSBool b) = show b
85 hvparamShow x = J.encode x
86
87 mkSSConf :: ConfigData -> SSConf
88 mkSSConf cdata = SSConf . M.fromList $
89 [ (SSClusterName, return $ clusterClusterName cluster)
90 , (SSClusterTags, toList $ tagsOf cluster)
91 , (SSFileStorageDir, return $ clusterFileStorageDir cluster)
92 , (SSSharedFileStorageDir, return $ clusterSharedFileStorageDir cluster)
93 , (SSGlusterStorageDir, return $ clusterGlusterStorageDir cluster)
94 , (SSMasterCandidates, mapLines nodeName mcs)
95 , (SSMasterCandidatesIps, mapLines nodePrimaryIp mcs)
96 , (SSMasterCandidatesCerts, mapLines eqPair . toPairs
97 . clusterCandidateCerts $ cluster)
98 , (SSMasterIp, return $ clusterMasterIp cluster)
99 , (SSMasterNetdev, return $ clusterMasterNetdev cluster)
100 , (SSMasterNetmask, return . show $ clusterMasterNetmask cluster)
101 , (SSMasterNode, return
102 . genericResult (const "NO MASTER") nodeName
103 . getNode cdata $ clusterMasterNode cluster)
104 , (SSNodeList, mapLines nodeName nodes)
105 , (SSNodePrimaryIps, mapLines (spcPair . (nodeName &&& nodePrimaryIp))
106 nodes )
107 , (SSNodeSecondaryIps, mapLines (spcPair . (nodeName &&& nodeSecondaryIp))
108 nodes )
109 , (SSNodeVmCapable, mapLines (eqPair . (nodeName &&& show . nodeVmCapable))
110 nodes)
111 , (SSOfflineNodes, mapLines nodeName offline )
112 , (SSOnlineNodes, mapLines nodeName online )
113 , (SSPrimaryIpFamily, return . show . ipFamilyToRaw
114 . clusterPrimaryIpFamily $ cluster)
115 , (SSInstanceList, niceSort . mapMaybe instName
116 . toList . configInstances $ cdata)
117 , (SSReleaseVersion, return releaseVersion)
118 , (SSHypervisorList, mapLines hypervisorToRaw
119 . clusterEnabledHypervisors $ cluster)
120 , (SSMaintainNodeHealth, return . show . clusterMaintainNodeHealth
121 $ cluster)
122 , (SSUidPool, mapLines formatUidRange . clusterUidPool $ cluster)
123 , (SSNodegroups, mapLines (spcPair . (uuidOf &&& groupName))
124 nodeGroups)
125 , (SSNetworks, mapLines (spcPair . (uuidOf
126 &&& (fromNonEmpty . networkName)))
127 . configNetworks $ cdata)
128 , (SSEnabledUserShutdown, return . show . clusterEnabledUserShutdown
129 $ cluster)
130 , (SSSshPorts, mapLines (eqPair . (nodeName
131 &&& getSshPort cdata)) nodes)
132 ] ++
133 map (first hvparamsSSKey) (mkSSConfHvparams cluster)
134 where
135 mapLines :: (Foldable f) => (a -> String) -> f a -> [String]
136 mapLines f = map f . toList
137 spcPair (x, y) = x ++ " " ++ y
138 toPairs = M.assocs . fromContainer
139
140 cluster = configCluster cdata
141 mcs = getMasterOrCandidates cdata
142 nodes = niceSortKey nodeName . toList $ configNodes cdata
143 (offline, online) = partition nodeOffline nodes
144 nodeGroups = niceSortKey groupName . toList $ configNodegroups cdata
145
146 -- This will return the empty string only for the situation where the
147 -- configuration is corrupted and no nodegroup can be found for that node.
148 getSshPort :: ConfigData -> Node -> String
149 getSshPort cfg node = maybe "" (show . ndpSshPort)
150 $ getNodeNdParams cfg node