root / htools / Ganeti / Query / Server.hs @ 5183e8be
History | View | Annotate | Download (9 kB)
1 |
{-# LANGUAGE BangPatterns #-} |
---|---|
2 |
|
3 |
{-| Implementation of the Ganeti Query2 server. |
4 |
|
5 |
-} |
6 |
|
7 |
{- |
8 |
|
9 |
Copyright (C) 2012 Google Inc. |
10 |
|
11 |
This program is free software; you can redistribute it and/or modify |
12 |
it under the terms of the GNU General Public License as published by |
13 |
the Free Software Foundation; either version 2 of the License, or |
14 |
(at your option) any later version. |
15 |
|
16 |
This program is distributed in the hope that it will be useful, but |
17 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 |
General Public License for more details. |
20 |
|
21 |
You should have received a copy of the GNU General Public License |
22 |
along with this program; if not, write to the Free Software |
23 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
24 |
02110-1301, USA. |
25 |
|
26 |
-} |
27 |
|
28 |
module Ganeti.Query.Server |
29 |
( ConfigReader |
30 |
, runQueryD |
31 |
) where |
32 |
|
33 |
import Control.Applicative |
34 |
import Control.Concurrent |
35 |
import Control.Exception |
36 |
import Data.Bits (bitSize) |
37 |
import Data.Maybe |
38 |
import qualified Network.Socket as S |
39 |
import qualified Text.JSON as J |
40 |
import Text.JSON (showJSON, JSValue(..)) |
41 |
import Text.JSON.Pretty (pp_value) |
42 |
import System.Info (arch) |
43 |
|
44 |
import qualified Ganeti.Constants as C |
45 |
import Ganeti.Errors |
46 |
import qualified Ganeti.Path as Path |
47 |
import Ganeti.Daemon |
48 |
import Ganeti.Objects |
49 |
import qualified Ganeti.Config as Config |
50 |
import Ganeti.BasicTypes |
51 |
import Ganeti.Logging |
52 |
import Ganeti.Luxi |
53 |
import qualified Ganeti.Query.Language as Qlang |
54 |
import Ganeti.Query.Query |
55 |
import Ganeti.Query.Filter (makeSimpleFilter) |
56 |
|
57 |
-- | A type for functions that can return the configuration when |
58 |
-- executed. |
59 |
type ConfigReader = IO (Result ConfigData) |
60 |
|
61 |
-- | Helper for classic queries. |
62 |
handleClassicQuery :: ConfigData -- ^ Cluster config |
63 |
-> Qlang.ItemType -- ^ Query type |
64 |
-> [String] -- ^ Requested names (empty means all) |
65 |
-> [String] -- ^ Requested fields |
66 |
-> Bool -- ^ Whether to do sync queries or not |
67 |
-> IO (GenericResult GanetiException JSValue) |
68 |
handleClassicQuery _ _ _ _ True = |
69 |
return . Bad $ OpPrereqError "Sync queries are not allowed" ECodeInval |
70 |
handleClassicQuery cfg qkind names fields _ = do |
71 |
let flt = makeSimpleFilter (nameField qkind) names |
72 |
qr <- query cfg True (Qlang.Query qkind fields flt) |
73 |
return $ showJSON <$> (qr >>= queryCompat) |
74 |
|
75 |
-- | Minimal wrapper to handle the missing config case. |
76 |
handleCallWrapper :: Result ConfigData -> LuxiOp -> IO (ErrorResult JSValue) |
77 |
handleCallWrapper (Bad msg) _ = |
78 |
return . Bad . ConfigurationError $ |
79 |
"I do not have access to a valid configuration, cannot\ |
80 |
\ process queries: " ++ msg |
81 |
handleCallWrapper (Ok config) op = handleCall config op |
82 |
|
83 |
-- | Actual luxi operation handler. |
84 |
handleCall :: ConfigData -> LuxiOp -> IO (ErrorResult JSValue) |
85 |
handleCall cdata QueryClusterInfo = |
86 |
let cluster = configCluster cdata |
87 |
hypervisors = clusterEnabledHypervisors cluster |
88 |
bits = show (bitSize (0::Int)) ++ "bits" |
89 |
arch_tuple = [bits, arch] |
90 |
obj = [ ("software_version", showJSON C.releaseVersion) |
91 |
, ("protocol_version", showJSON C.protocolVersion) |
92 |
, ("config_version", showJSON C.configVersion) |
93 |
, ("os_api_version", showJSON $ maximum C.osApiVersions) |
94 |
, ("export_version", showJSON C.exportVersion) |
95 |
, ("architecture", showJSON arch_tuple) |
96 |
, ("name", showJSON $ clusterClusterName cluster) |
97 |
, ("master", showJSON $ clusterMasterNode cluster) |
98 |
, ("default_hypervisor", showJSON $ head hypervisors) |
99 |
, ("enabled_hypervisors", showJSON hypervisors) |
100 |
, ("hvparams", showJSON $ clusterHvparams cluster) |
101 |
, ("os_hvp", showJSON $ clusterOsHvp cluster) |
102 |
, ("beparams", showJSON $ clusterBeparams cluster) |
103 |
, ("osparams", showJSON $ clusterOsparams cluster) |
104 |
, ("ipolicy", showJSON $ clusterIpolicy cluster) |
105 |
, ("nicparams", showJSON $ clusterNicparams cluster) |
106 |
, ("ndparams", showJSON $ clusterNdparams cluster) |
107 |
, ("diskparams", showJSON $ clusterDiskparams cluster) |
108 |
, ("candidate_pool_size", |
109 |
showJSON $ clusterCandidatePoolSize cluster) |
110 |
, ("master_netdev", showJSON $ clusterMasterNetdev cluster) |
111 |
, ("master_netmask", showJSON $ clusterMasterNetmask cluster) |
112 |
, ("use_external_mip_script", |
113 |
showJSON $ clusterUseExternalMipScript cluster) |
114 |
, ("volume_group_name", showJSON $ clusterVolumeGroupName cluster) |
115 |
, ("drbd_usermode_helper", |
116 |
maybe JSNull showJSON (clusterDrbdUsermodeHelper cluster)) |
117 |
, ("file_storage_dir", showJSON $ clusterFileStorageDir cluster) |
118 |
, ("shared_file_storage_dir", |
119 |
showJSON $ clusterSharedFileStorageDir cluster) |
120 |
, ("maintain_node_health", |
121 |
showJSON $ clusterMaintainNodeHealth cluster) |
122 |
, ("ctime", showJSON $ clusterCtime cluster) |
123 |
, ("mtime", showJSON $ clusterMtime cluster) |
124 |
, ("uuid", showJSON $ clusterUuid cluster) |
125 |
, ("tags", showJSON $ clusterTags cluster) |
126 |
, ("uid_pool", showJSON $ clusterUidPool cluster) |
127 |
, ("default_iallocator", |
128 |
showJSON $ clusterDefaultIallocator cluster) |
129 |
, ("reserved_lvs", showJSON $ clusterReservedLvs cluster) |
130 |
, ("primary_ip_version", |
131 |
showJSON . ipFamilyToVersion $ clusterPrimaryIpFamily cluster) |
132 |
, ("prealloc_wipe_disks", |
133 |
showJSON $ clusterPreallocWipeDisks cluster) |
134 |
, ("hidden_os", showJSON $ clusterHiddenOs cluster) |
135 |
, ("blacklisted_os", showJSON $ clusterBlacklistedOs cluster) |
136 |
] |
137 |
|
138 |
in return . Ok . J.makeObj $ obj |
139 |
|
140 |
handleCall cfg (QueryTags kind name) = |
141 |
let tags = case kind of |
142 |
TagCluster -> Ok . clusterTags $ configCluster cfg |
143 |
TagGroup -> groupTags <$> Config.getGroup cfg name |
144 |
TagNode -> nodeTags <$> Config.getNode cfg name |
145 |
TagInstance -> instTags <$> Config.getInstance cfg name |
146 |
in return (J.showJSON <$> tags) |
147 |
|
148 |
handleCall cfg (Query qkind qfields qfilter) = do |
149 |
result <- query cfg True (Qlang.Query qkind qfields qfilter) |
150 |
return $ J.showJSON <$> result |
151 |
|
152 |
handleCall _ (QueryFields qkind qfields) = do |
153 |
let result = queryFields (Qlang.QueryFields qkind qfields) |
154 |
return $ J.showJSON <$> result |
155 |
|
156 |
handleCall cfg (QueryNodes names fields lock) = |
157 |
handleClassicQuery cfg Qlang.QRNode names fields lock |
158 |
|
159 |
handleCall cfg (QueryGroups names fields lock) = |
160 |
handleClassicQuery cfg Qlang.QRGroup names fields lock |
161 |
|
162 |
handleCall _ op = |
163 |
return . Bad $ |
164 |
GenericError ("Luxi call '" ++ strOfOp op ++ "' not implemented") |
165 |
|
166 |
|
167 |
-- | Given a decoded luxi request, executes it and sends the luxi |
168 |
-- response back to the client. |
169 |
handleClientMsg :: Client -> ConfigReader -> LuxiOp -> IO Bool |
170 |
handleClientMsg client creader args = do |
171 |
cfg <- creader |
172 |
logDebug $ "Request: " ++ show args |
173 |
call_result <- handleCallWrapper cfg args |
174 |
(!status, !rval) <- |
175 |
case call_result of |
176 |
Bad err -> do |
177 |
logWarning $ "Failed to execute request: " ++ show err |
178 |
return (False, showJSON err) |
179 |
Ok result -> do |
180 |
logDebug $ "Result " ++ show (pp_value result) |
181 |
return (True, result) |
182 |
sendMsg client $ buildResponse status rval |
183 |
return True |
184 |
|
185 |
-- | Handles one iteration of the client protocol: receives message, |
186 |
-- checks for validity and decods, returns response. |
187 |
handleClient :: Client -> ConfigReader -> IO Bool |
188 |
handleClient client creader = do |
189 |
!msg <- recvMsgExt client |
190 |
case msg of |
191 |
RecvConnClosed -> logDebug "Connection closed" >> return False |
192 |
RecvError err -> logWarning ("Error during message receiving: " ++ err) >> |
193 |
return False |
194 |
RecvOk payload -> |
195 |
case validateCall payload >>= decodeCall of |
196 |
Bad err -> do |
197 |
let errmsg = "Failed to parse request: " ++ err |
198 |
logWarning errmsg |
199 |
sendMsg client $ buildResponse False (showJSON errmsg) |
200 |
return False |
201 |
Ok args -> handleClientMsg client creader args |
202 |
|
203 |
-- | Main client loop: runs one loop of 'handleClient', and if that |
204 |
-- doesn't repot a finished (closed) connection, restarts itself. |
205 |
clientLoop :: Client -> ConfigReader -> IO () |
206 |
clientLoop client creader = do |
207 |
result <- handleClient client creader |
208 |
if result |
209 |
then clientLoop client creader |
210 |
else closeClient client |
211 |
|
212 |
-- | Main loop: accepts clients, forks an I/O thread to handle that |
213 |
-- client, and then restarts. |
214 |
mainLoop :: ConfigReader -> S.Socket -> IO () |
215 |
mainLoop creader socket = do |
216 |
client <- acceptClient socket |
217 |
_ <- forkIO $ clientLoop client creader |
218 |
mainLoop creader socket |
219 |
|
220 |
-- | Main function that runs the query endpoint. This should be the |
221 |
-- only one exposed from this module. |
222 |
runQueryD :: Maybe FilePath -> ConfigReader -> IO () |
223 |
runQueryD fpath creader = do |
224 |
let socket_path = fromMaybe Path.defaultQuerySocket fpath |
225 |
cleanupSocket socket_path |
226 |
bracket |
227 |
(getServer socket_path) |
228 |
(closeServer socket_path) |
229 |
(mainLoop creader) |