Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Server.hs @ e10c4a69

History | View | Annotate | Download (10.9 kB)

1 25b54de0 Iustin Pop
{-# LANGUAGE BangPatterns #-}
2 25b54de0 Iustin Pop
3 d120506c Agata Murawska
{-| Implementation of the Ganeti Query2 server.
4 25b54de0 Iustin Pop
5 25b54de0 Iustin Pop
-}
6 25b54de0 Iustin Pop
7 25b54de0 Iustin Pop
{-
8 25b54de0 Iustin Pop
9 72747d91 Iustin Pop
Copyright (C) 2012, 2013 Google Inc.
10 25b54de0 Iustin Pop
11 25b54de0 Iustin Pop
This program is free software; you can redistribute it and/or modify
12 25b54de0 Iustin Pop
it under the terms of the GNU General Public License as published by
13 25b54de0 Iustin Pop
the Free Software Foundation; either version 2 of the License, or
14 25b54de0 Iustin Pop
(at your option) any later version.
15 25b54de0 Iustin Pop
16 25b54de0 Iustin Pop
This program is distributed in the hope that it will be useful, but
17 25b54de0 Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
18 25b54de0 Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 25b54de0 Iustin Pop
General Public License for more details.
20 25b54de0 Iustin Pop
21 25b54de0 Iustin Pop
You should have received a copy of the GNU General Public License
22 25b54de0 Iustin Pop
along with this program; if not, write to the Free Software
23 25b54de0 Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 25b54de0 Iustin Pop
02110-1301, USA.
25 25b54de0 Iustin Pop
26 25b54de0 Iustin Pop
-}
27 25b54de0 Iustin Pop
28 4cab6703 Iustin Pop
module Ganeti.Query.Server
29 670e954a Thomas Thrainer
  ( main
30 670e954a Thomas Thrainer
  , checkMain
31 670e954a Thomas Thrainer
  , prepMain
32 0d0ac025 Iustin Pop
  ) where
33 25b54de0 Iustin Pop
34 f2374060 Iustin Pop
import Control.Applicative
35 25b54de0 Iustin Pop
import Control.Concurrent
36 25b54de0 Iustin Pop
import Control.Exception
37 670e954a Thomas Thrainer
import Control.Monad (forever)
38 25b54de0 Iustin Pop
import Data.Bits (bitSize)
39 c87997d2 Jose A. Lopes
import qualified Data.Set as Set (toList)
40 670e954a Thomas Thrainer
import Data.IORef
41 25b54de0 Iustin Pop
import qualified Network.Socket as S
42 25b54de0 Iustin Pop
import qualified Text.JSON as J
43 25b54de0 Iustin Pop
import Text.JSON (showJSON, JSValue(..))
44 25b54de0 Iustin Pop
import System.Info (arch)
45 25b54de0 Iustin Pop
46 25b54de0 Iustin Pop
import qualified Ganeti.Constants as C
47 c87997d2 Jose A. Lopes
import qualified Ganeti.ConstantUtils as ConstantUtils (unFrozenSet)
48 5183e8be Iustin Pop
import Ganeti.Errors
49 9eeb0aa5 Michael Hanselmann
import qualified Ganeti.Path as Path
50 0d0ac025 Iustin Pop
import Ganeti.Daemon
51 25b54de0 Iustin Pop
import Ganeti.Objects
52 f2374060 Iustin Pop
import qualified Ganeti.Config as Config
53 218e3b0f Thomas Thrainer
import Ganeti.ConfigReader
54 25b54de0 Iustin Pop
import Ganeti.BasicTypes
55 25b54de0 Iustin Pop
import Ganeti.Logging
56 25b54de0 Iustin Pop
import Ganeti.Luxi
57 4cab6703 Iustin Pop
import qualified Ganeti.Query.Language as Qlang
58 1c3231aa Thomas Thrainer
import qualified Ganeti.Query.Cluster as QCluster
59 4cbe9bda Iustin Pop
import Ganeti.Query.Query
60 c4e0d065 Klaus Aehlig
import Ganeti.Query.Filter (makeSimpleFilter)
61 6e94b75c Jose A. Lopes
import Ganeti.Types
62 3cb9bd38 Jose A. Lopes
import qualified Ganeti.Version as Version
63 25b54de0 Iustin Pop
64 cd67e337 Iustin Pop
-- | Helper for classic queries.
65 cd67e337 Iustin Pop
handleClassicQuery :: ConfigData      -- ^ Cluster config
66 cd67e337 Iustin Pop
                   -> Qlang.ItemType  -- ^ Query type
67 037762a9 Iustin Pop
                   -> [Either String Integer] -- ^ Requested names
68 037762a9 Iustin Pop
                                              -- (empty means all)
69 cd67e337 Iustin Pop
                   -> [String]        -- ^ Requested fields
70 cd67e337 Iustin Pop
                   -> Bool            -- ^ Whether to do sync queries or not
71 5183e8be Iustin Pop
                   -> IO (GenericResult GanetiException JSValue)
72 c4e0d065 Klaus Aehlig
handleClassicQuery _ _ _ _ True =
73 5183e8be Iustin Pop
  return . Bad $ OpPrereqError "Sync queries are not allowed" ECodeInval
74 c4e0d065 Klaus Aehlig
handleClassicQuery cfg qkind names fields _ = do
75 c4e0d065 Klaus Aehlig
  let flt = makeSimpleFilter (nameField qkind) names
76 cd67e337 Iustin Pop
  qr <- query cfg True (Qlang.Query qkind fields flt)
77 cd67e337 Iustin Pop
  return $ showJSON <$> (qr >>= queryCompat)
78 cd67e337 Iustin Pop
79 25b54de0 Iustin Pop
-- | Minimal wrapper to handle the missing config case.
80 5183e8be Iustin Pop
handleCallWrapper :: Result ConfigData -> LuxiOp -> IO (ErrorResult JSValue)
81 25b54de0 Iustin Pop
handleCallWrapper (Bad msg) _ =
82 5183e8be Iustin Pop
  return . Bad . ConfigurationError $
83 5183e8be Iustin Pop
           "I do not have access to a valid configuration, cannot\
84 5183e8be Iustin Pop
           \ process queries: " ++ msg
85 25b54de0 Iustin Pop
handleCallWrapper (Ok config) op = handleCall config op
86 25b54de0 Iustin Pop
87 25b54de0 Iustin Pop
-- | Actual luxi operation handler.
88 5183e8be Iustin Pop
handleCall :: ConfigData -> LuxiOp -> IO (ErrorResult JSValue)
89 25b54de0 Iustin Pop
handleCall cdata QueryClusterInfo =
90 25b54de0 Iustin Pop
  let cluster = configCluster cdata
91 1c3231aa Thomas Thrainer
      master = QCluster.clusterMasterNodeName cdata
92 25b54de0 Iustin Pop
      hypervisors = clusterEnabledHypervisors cluster
93 966e1580 Helga Velroyen
      diskTemplates = clusterEnabledDiskTemplates cluster
94 72747d91 Iustin Pop
      def_hv = case hypervisors of
95 72747d91 Iustin Pop
                 x:_ -> showJSON x
96 72747d91 Iustin Pop
                 [] -> JSNull
97 25b54de0 Iustin Pop
      bits = show (bitSize (0::Int)) ++ "bits"
98 25b54de0 Iustin Pop
      arch_tuple = [bits, arch]
99 5b11f8db Iustin Pop
      obj = [ ("software_version", showJSON C.releaseVersion)
100 5b11f8db Iustin Pop
            , ("protocol_version", showJSON C.protocolVersion)
101 5b11f8db Iustin Pop
            , ("config_version", showJSON C.configVersion)
102 c87997d2 Jose A. Lopes
            , ("os_api_version", showJSON . maximum .
103 c87997d2 Jose A. Lopes
                                 Set.toList . ConstantUtils.unFrozenSet $
104 c87997d2 Jose A. Lopes
                                 C.osApiVersions)
105 5b11f8db Iustin Pop
            , ("export_version", showJSON C.exportVersion)
106 3cb9bd38 Jose A. Lopes
            , ("vcs_version", showJSON Version.version)
107 5b11f8db Iustin Pop
            , ("architecture", showJSON arch_tuple)
108 25b54de0 Iustin Pop
            , ("name", showJSON $ clusterClusterName cluster)
109 1c3231aa Thomas Thrainer
            , ("master", showJSON (case master of
110 1c3231aa Thomas Thrainer
                                     Ok name -> name
111 1c3231aa Thomas Thrainer
                                     _ -> undefined))
112 72747d91 Iustin Pop
            , ("default_hypervisor", def_hv)
113 5b11f8db Iustin Pop
            , ("enabled_hypervisors", showJSON hypervisors)
114 a2160e57 Iustin Pop
            , ("hvparams", showJSON $ clusterHvparams cluster)
115 a2160e57 Iustin Pop
            , ("os_hvp", showJSON $ clusterOsHvp cluster)
116 25b54de0 Iustin Pop
            , ("beparams", showJSON $ clusterBeparams cluster)
117 25b54de0 Iustin Pop
            , ("osparams", showJSON $ clusterOsparams cluster)
118 25b54de0 Iustin Pop
            , ("ipolicy", showJSON $ clusterIpolicy cluster)
119 25b54de0 Iustin Pop
            , ("nicparams", showJSON $ clusterNicparams cluster)
120 25b54de0 Iustin Pop
            , ("ndparams", showJSON $ clusterNdparams cluster)
121 a2160e57 Iustin Pop
            , ("diskparams", showJSON $ clusterDiskparams cluster)
122 25b54de0 Iustin Pop
            , ("candidate_pool_size",
123 25b54de0 Iustin Pop
               showJSON $ clusterCandidatePoolSize cluster)
124 25b54de0 Iustin Pop
            , ("master_netdev",  showJSON $ clusterMasterNetdev cluster)
125 25b54de0 Iustin Pop
            , ("master_netmask", showJSON $ clusterMasterNetmask cluster)
126 25b54de0 Iustin Pop
            , ("use_external_mip_script",
127 25b54de0 Iustin Pop
               showJSON $ clusterUseExternalMipScript cluster)
128 64b0309a Dimitris Aragiorgis
            , ("volume_group_name",
129 64b0309a Dimitris Aragiorgis
               maybe JSNull showJSON (clusterVolumeGroupName cluster))
130 25b54de0 Iustin Pop
            , ("drbd_usermode_helper",
131 25b54de0 Iustin Pop
               maybe JSNull showJSON (clusterDrbdUsermodeHelper cluster))
132 25b54de0 Iustin Pop
            , ("file_storage_dir", showJSON $ clusterFileStorageDir cluster)
133 25b54de0 Iustin Pop
            , ("shared_file_storage_dir",
134 25b54de0 Iustin Pop
               showJSON $ clusterSharedFileStorageDir cluster)
135 25b54de0 Iustin Pop
            , ("maintain_node_health",
136 25b54de0 Iustin Pop
               showJSON $ clusterMaintainNodeHealth cluster)
137 25b54de0 Iustin Pop
            , ("ctime", showJSON $ clusterCtime cluster)
138 25b54de0 Iustin Pop
            , ("mtime", showJSON $ clusterMtime cluster)
139 25b54de0 Iustin Pop
            , ("uuid", showJSON $ clusterUuid cluster)
140 25b54de0 Iustin Pop
            , ("tags", showJSON $ clusterTags cluster)
141 25b54de0 Iustin Pop
            , ("uid_pool", showJSON $ clusterUidPool cluster)
142 25b54de0 Iustin Pop
            , ("default_iallocator",
143 25b54de0 Iustin Pop
               showJSON $ clusterDefaultIallocator cluster)
144 25b54de0 Iustin Pop
            , ("reserved_lvs", showJSON $ clusterReservedLvs cluster)
145 25b54de0 Iustin Pop
            , ("primary_ip_version",
146 25b54de0 Iustin Pop
               showJSON . ipFamilyToVersion $ clusterPrimaryIpFamily cluster)
147 7b9ceea7 Helga Velroyen
            , ("prealloc_wipe_disks",
148 7b9ceea7 Helga Velroyen
               showJSON $ clusterPreallocWipeDisks cluster)
149 7b9ceea7 Helga Velroyen
            , ("hidden_os", showJSON $ clusterHiddenOs cluster)
150 7b9ceea7 Helga Velroyen
            , ("blacklisted_os", showJSON $ clusterBlacklistedOs cluster)
151 966e1580 Helga Velroyen
            , ("enabled_disk_templates", showJSON diskTemplates)
152 25b54de0 Iustin Pop
            ]
153 25b54de0 Iustin Pop
154 1c3231aa Thomas Thrainer
  in case master of
155 1c3231aa Thomas Thrainer
    Ok _ -> return . Ok . J.makeObj $ obj
156 1c3231aa Thomas Thrainer
    Bad ex -> return $ Bad ex
157 25b54de0 Iustin Pop
158 6e94b75c Jose A. Lopes
handleCall cfg (QueryTags kind name) = do
159 f2374060 Iustin Pop
  let tags = case kind of
160 6e94b75c Jose A. Lopes
               TagKindCluster  -> Ok . clusterTags $ configCluster cfg
161 e10c4a69 Hrvoje Ribicic
               TagKindGroup    -> groupTags   <$> Config.getGroup    cfg name
162 e10c4a69 Hrvoje Ribicic
               TagKindNode     -> nodeTags    <$> Config.getNode     cfg name
163 e10c4a69 Hrvoje Ribicic
               TagKindInstance -> instTags    <$> Config.getInstance cfg name
164 e10c4a69 Hrvoje Ribicic
               TagKindNetwork  -> networkTags <$> Config.getNetwork  cfg name
165 6e94b75c Jose A. Lopes
  return (J.showJSON <$> tags)
166 f2374060 Iustin Pop
167 4cbe9bda Iustin Pop
handleCall cfg (Query qkind qfields qfilter) = do
168 fa2c927c Agata Murawska
  result <- query cfg True (Qlang.Query qkind qfields qfilter)
169 4cbe9bda Iustin Pop
  return $ J.showJSON <$> result
170 4cbe9bda Iustin Pop
171 518023a9 Iustin Pop
handleCall _ (QueryFields qkind qfields) = do
172 518023a9 Iustin Pop
  let result = queryFields (Qlang.QueryFields qkind qfields)
173 518023a9 Iustin Pop
  return $ J.showJSON <$> result
174 518023a9 Iustin Pop
175 cd67e337 Iustin Pop
handleCall cfg (QueryNodes names fields lock) =
176 037762a9 Iustin Pop
  handleClassicQuery cfg (Qlang.ItemTypeOpCode Qlang.QRNode)
177 c4e0d065 Klaus Aehlig
    (map Left names) fields lock
178 cd67e337 Iustin Pop
179 cd67e337 Iustin Pop
handleCall cfg (QueryGroups names fields lock) =
180 037762a9 Iustin Pop
  handleClassicQuery cfg (Qlang.ItemTypeOpCode Qlang.QRGroup)
181 c4e0d065 Klaus Aehlig
    (map Left names) fields lock
182 cd67e337 Iustin Pop
183 a7e484c4 Iustin Pop
handleCall cfg (QueryJobs names fields) =
184 a7e484c4 Iustin Pop
  handleClassicQuery cfg (Qlang.ItemTypeLuxi Qlang.QRJob)
185 c4e0d065 Klaus Aehlig
    (map (Right . fromIntegral . fromJobId) names)  fields False
186 a7e484c4 Iustin Pop
187 795d035d Klaus Aehlig
handleCall cfg (QueryNetworks names fields lock) =
188 795d035d Klaus Aehlig
  handleClassicQuery cfg (Qlang.ItemTypeOpCode Qlang.QRNetwork)
189 795d035d Klaus Aehlig
    (map Left names) fields lock
190 795d035d Klaus Aehlig
191 25b54de0 Iustin Pop
handleCall _ op =
192 5183e8be Iustin Pop
  return . Bad $
193 5183e8be Iustin Pop
    GenericError ("Luxi call '" ++ strOfOp op ++ "' not implemented")
194 25b54de0 Iustin Pop
195 25b54de0 Iustin Pop
-- | Given a decoded luxi request, executes it and sends the luxi
196 25b54de0 Iustin Pop
-- response back to the client.
197 25b54de0 Iustin Pop
handleClientMsg :: Client -> ConfigReader -> LuxiOp -> IO Bool
198 25b54de0 Iustin Pop
handleClientMsg client creader args = do
199 25b54de0 Iustin Pop
  cfg <- creader
200 25b54de0 Iustin Pop
  logDebug $ "Request: " ++ show args
201 25b54de0 Iustin Pop
  call_result <- handleCallWrapper cfg args
202 25b54de0 Iustin Pop
  (!status, !rval) <-
203 25b54de0 Iustin Pop
    case call_result of
204 9abbb084 Iustin Pop
      Bad err -> do
205 3e0c2a24 Klaus Aehlig
        logWarning $ "Failed to execute request " ++ show args ++ ": "
206 3e0c2a24 Klaus Aehlig
                     ++ show err
207 5183e8be Iustin Pop
        return (False, showJSON err)
208 25b54de0 Iustin Pop
      Ok result -> do
209 f74b88fa Iustin Pop
        -- only log the first 2,000 chars of the result
210 f74b88fa Iustin Pop
        logDebug $ "Result (truncated): " ++ take 2000 (J.encode result)
211 3e0c2a24 Klaus Aehlig
        logInfo $ "Successfully handled " ++ strOfOp args
212 25b54de0 Iustin Pop
        return (True, result)
213 25b54de0 Iustin Pop
  sendMsg client $ buildResponse status rval
214 25b54de0 Iustin Pop
  return True
215 25b54de0 Iustin Pop
216 25b54de0 Iustin Pop
-- | Handles one iteration of the client protocol: receives message,
217 3e02cd3c Michele Tartara
-- checks it for validity and decodes it, returns response.
218 25b54de0 Iustin Pop
handleClient :: Client -> ConfigReader -> IO Bool
219 25b54de0 Iustin Pop
handleClient client creader = do
220 25b54de0 Iustin Pop
  !msg <- recvMsgExt client
221 385d4574 Klaus Aehlig
  logDebug $ "Received message: " ++ show msg
222 25b54de0 Iustin Pop
  case msg of
223 25b54de0 Iustin Pop
    RecvConnClosed -> logDebug "Connection closed" >> return False
224 25b54de0 Iustin Pop
    RecvError err -> logWarning ("Error during message receiving: " ++ err) >>
225 25b54de0 Iustin Pop
                     return False
226 25b54de0 Iustin Pop
    RecvOk payload ->
227 25b54de0 Iustin Pop
      case validateCall payload >>= decodeCall of
228 9abbb084 Iustin Pop
        Bad err -> do
229 9abbb084 Iustin Pop
             let errmsg = "Failed to parse request: " ++ err
230 9abbb084 Iustin Pop
             logWarning errmsg
231 9abbb084 Iustin Pop
             sendMsg client $ buildResponse False (showJSON errmsg)
232 9abbb084 Iustin Pop
             return False
233 25b54de0 Iustin Pop
        Ok args -> handleClientMsg client creader args
234 25b54de0 Iustin Pop
235 25b54de0 Iustin Pop
-- | Main client loop: runs one loop of 'handleClient', and if that
236 cb44e3db Helga Velroyen
-- doesn't report a finished (closed) connection, restarts itself.
237 25b54de0 Iustin Pop
clientLoop :: Client -> ConfigReader -> IO ()
238 25b54de0 Iustin Pop
clientLoop client creader = do
239 25b54de0 Iustin Pop
  result <- handleClient client creader
240 25b54de0 Iustin Pop
  if result
241 25b54de0 Iustin Pop
    then clientLoop client creader
242 25b54de0 Iustin Pop
    else closeClient client
243 25b54de0 Iustin Pop
244 670e954a Thomas Thrainer
-- | Main listener loop: accepts clients, forks an I/O thread to handle
245 670e954a Thomas Thrainer
-- that client.
246 670e954a Thomas Thrainer
listener :: ConfigReader -> S.Socket -> IO ()
247 670e954a Thomas Thrainer
listener creader socket = do
248 25b54de0 Iustin Pop
  client <- acceptClient socket
249 25b54de0 Iustin Pop
  _ <- forkIO $ clientLoop client creader
250 670e954a Thomas Thrainer
  return ()
251 25b54de0 Iustin Pop
252 670e954a Thomas Thrainer
-- | Type alias for prepMain results
253 670e954a Thomas Thrainer
type PrepResult = (FilePath, S.Socket, IORef (Result ConfigData))
254 25b54de0 Iustin Pop
255 3695a4e0 Thomas Thrainer
-- | Check function for luxid.
256 670e954a Thomas Thrainer
checkMain :: CheckFn ()
257 670e954a Thomas Thrainer
checkMain _ = return $ Right ()
258 670e954a Thomas Thrainer
259 3695a4e0 Thomas Thrainer
-- | Prepare function for luxid.
260 670e954a Thomas Thrainer
prepMain :: PrepFn () PrepResult
261 670e954a Thomas Thrainer
prepMain _ _ = do
262 670e954a Thomas Thrainer
  socket_path <- Path.defaultQuerySocket
263 0d0ac025 Iustin Pop
  cleanupSocket socket_path
264 73b16ca1 Iustin Pop
  s <- describeError "binding to the Luxi socket"
265 e455a3e8 Michele Tartara
         Nothing (Just socket_path) $ getServer True socket_path
266 670e954a Thomas Thrainer
  cref <- newIORef (Bad "Configuration not yet loaded")
267 670e954a Thomas Thrainer
  return (socket_path, s, cref)
268 670e954a Thomas Thrainer
269 670e954a Thomas Thrainer
-- | Main function.
270 670e954a Thomas Thrainer
main :: MainFn () PrepResult
271 670e954a Thomas Thrainer
main _ _ (socket_path, server, cref) = do
272 670e954a Thomas Thrainer
  initConfigReader id cref
273 670e954a Thomas Thrainer
  let creader = readIORef cref
274 4c3f55b8 Iustin Pop
275 4c3f55b8 Iustin Pop
  finally
276 670e954a Thomas Thrainer
    (forever $ listener creader server)
277 4c3f55b8 Iustin Pop
    (closeServer socket_path server)