Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.3 kB)

1
{-# LANGUAGE BangPatterns #-}
2

    
3
{-| Implementation of the Ganeti Query2 server.
4

    
5
-}
6

    
7
{-
8

    
9
Copyright (C) 2012, 2013 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
  ( main
30
  , checkMain
31
  , prepMain
32
  ) where
33

    
34
import Control.Applicative
35
import Control.Concurrent
36
import Control.Exception
37
import Control.Monad (forever, when)
38
import Data.Bits (bitSize)
39
import qualified Data.Set as Set (toList)
40
import Data.IORef
41
import qualified Network.Socket as S
42
import qualified Text.JSON as J
43
import Text.JSON (showJSON, JSValue(..))
44
import System.Info (arch)
45

    
46
import qualified Ganeti.Constants as C
47
import qualified Ganeti.ConstantUtils as ConstantUtils (unFrozenSet)
48
import Ganeti.Errors
49
import qualified Ganeti.Path as Path
50
import Ganeti.Daemon
51
import Ganeti.Objects
52
import qualified Ganeti.Config as Config
53
import Ganeti.ConfigReader
54
import Ganeti.BasicTypes
55
import Ganeti.JQueue
56
import Ganeti.Logging
57
import Ganeti.Luxi
58
import qualified Ganeti.Query.Language as Qlang
59
import qualified Ganeti.Query.Cluster as QCluster
60
import Ganeti.Path (queueDir, jobQueueLockFile, defaultLuxiSocket)
61
import Ganeti.Query.Query
62
import Ganeti.Query.Filter (makeSimpleFilter)
63
import Ganeti.Types
64
import Ganeti.Utils (lockFile, exitIfBad)
65

    
66
-- | Helper for classic queries.
67
handleClassicQuery :: ConfigData      -- ^ Cluster config
68
                   -> Qlang.ItemType  -- ^ Query type
69
                   -> [Either String Integer] -- ^ Requested names
70
                                              -- (empty means all)
71
                   -> [String]        -- ^ Requested fields
72
                   -> Bool            -- ^ Whether to do sync queries or not
73
                   -> IO (GenericResult GanetiException JSValue)
74
handleClassicQuery _ _ _ _ True =
75
  return . Bad $ OpPrereqError "Sync queries are not allowed" ECodeInval
76
handleClassicQuery cfg qkind names fields _ = do
77
  let flt = makeSimpleFilter (nameField qkind) names
78
  qr <- query cfg True (Qlang.Query qkind fields flt)
79
  return $ showJSON <$> (qr >>= queryCompat)
80

    
81
-- | Minimal wrapper to handle the missing config case.
82
handleCallWrapper :: MVar () -> Result ConfigData 
83
                     -> LuxiOp -> IO (ErrorResult JSValue)
84
handleCallWrapper _ (Bad msg) _ =
85
  return . Bad . ConfigurationError $
86
           "I do not have access to a valid configuration, cannot\
87
           \ process queries: " ++ msg
88
handleCallWrapper qlock (Ok config) op = handleCall qlock config op
89

    
90
-- | Actual luxi operation handler.
91
handleCall :: MVar () -> ConfigData -> LuxiOp -> IO (ErrorResult JSValue)
92
handleCall _ cdata QueryClusterInfo =
93
  let cluster = configCluster cdata
94
      master = QCluster.clusterMasterNodeName cdata
95
      hypervisors = clusterEnabledHypervisors cluster
96
      diskTemplates = clusterEnabledDiskTemplates cluster
97
      def_hv = case hypervisors of
98
                 x:_ -> showJSON x
99
                 [] -> JSNull
100
      bits = show (bitSize (0::Int)) ++ "bits"
101
      arch_tuple = [bits, arch]
102
      obj = [ ("software_version", showJSON C.releaseVersion)
103
            , ("protocol_version", showJSON C.protocolVersion)
104
            , ("config_version", showJSON C.configVersion)
105
            , ("os_api_version", showJSON . maximum .
106
                                 Set.toList . ConstantUtils.unFrozenSet $
107
                                 C.osApiVersions)
108
            , ("export_version", showJSON C.exportVersion)
109
            , ("vcs_version", showJSON C.vcsVersion)
110
            , ("architecture", showJSON arch_tuple)
111
            , ("name", showJSON $ clusterClusterName cluster)
112
            , ("master", showJSON (case master of
113
                                     Ok name -> name
114
                                     _ -> undefined))
115
            , ("default_hypervisor", def_hv)
116
            , ("enabled_hypervisors", showJSON hypervisors)
117
            , ("hvparams", showJSON $ clusterHvparams cluster)
118
            , ("os_hvp", showJSON $ clusterOsHvp cluster)
119
            , ("beparams", showJSON $ clusterBeparams cluster)
120
            , ("osparams", showJSON $ clusterOsparams cluster)
121
            , ("ipolicy", showJSON $ clusterIpolicy cluster)
122
            , ("nicparams", showJSON $ clusterNicparams cluster)
123
            , ("ndparams", showJSON $ clusterNdparams cluster)
124
            , ("diskparams", showJSON $ clusterDiskparams cluster)
125
            , ("candidate_pool_size",
126
               showJSON $ clusterCandidatePoolSize cluster)
127
            , ("master_netdev",  showJSON $ clusterMasterNetdev cluster)
128
            , ("master_netmask", showJSON $ clusterMasterNetmask cluster)
129
            , ("use_external_mip_script",
130
               showJSON $ clusterUseExternalMipScript cluster)
131
            , ("volume_group_name",
132
               maybe JSNull showJSON (clusterVolumeGroupName cluster))
133
            , ("drbd_usermode_helper",
134
               maybe JSNull showJSON (clusterDrbdUsermodeHelper cluster))
135
            , ("file_storage_dir", showJSON $ clusterFileStorageDir cluster)
136
            , ("shared_file_storage_dir",
137
               showJSON $ clusterSharedFileStorageDir cluster)
138
            , ("maintain_node_health",
139
               showJSON $ clusterMaintainNodeHealth cluster)
140
            , ("ctime", showJSON $ clusterCtime cluster)
141
            , ("mtime", showJSON $ clusterMtime cluster)
142
            , ("uuid", showJSON $ clusterUuid cluster)
143
            , ("tags", showJSON $ clusterTags cluster)
144
            , ("uid_pool", showJSON $ clusterUidPool cluster)
145
            , ("default_iallocator",
146
               showJSON $ clusterDefaultIallocator cluster)
147
            , ("reserved_lvs", showJSON $ clusterReservedLvs cluster)
148
            , ("primary_ip_version",
149
               showJSON . ipFamilyToVersion $ clusterPrimaryIpFamily cluster)
150
            , ("prealloc_wipe_disks",
151
               showJSON $ clusterPreallocWipeDisks cluster)
152
            , ("hidden_os", showJSON $ clusterHiddenOs cluster)
153
            , ("blacklisted_os", showJSON $ clusterBlacklistedOs cluster)
154
            , ("enabled_disk_templates", showJSON diskTemplates)
155
            ]
156

    
157
  in case master of
158
    Ok _ -> return . Ok . J.makeObj $ obj
159
    Bad ex -> return $ Bad ex
160

    
161
handleCall _ cfg (QueryTags kind name) = do
162
  let tags = case kind of
163
               TagKindCluster  -> Ok . clusterTags $ configCluster cfg
164
               TagKindGroup    -> groupTags <$> Config.getGroup    cfg name
165
               TagKindNode     -> nodeTags  <$> Config.getNode     cfg name
166
               TagKindInstance -> instTags  <$> Config.getInstance cfg name
167
               TagKindNetwork  -> Bad $ OpPrereqError
168
                                        "Network tag is not allowed"
169
                                        ECodeInval
170
  return (J.showJSON <$> tags)
171

    
172
handleCall _ cfg (Query qkind qfields qfilter) = do
173
  result <- query cfg True (Qlang.Query qkind qfields qfilter)
174
  return $ J.showJSON <$> result
175

    
176
handleCall _ _ (QueryFields qkind qfields) = do
177
  let result = queryFields (Qlang.QueryFields qkind qfields)
178
  return $ J.showJSON <$> result
179

    
180
handleCall _ cfg (QueryNodes names fields lock) =
181
  handleClassicQuery cfg (Qlang.ItemTypeOpCode Qlang.QRNode)
182
    (map Left names) fields lock
183

    
184
handleCall _ cfg (QueryGroups names fields lock) =
185
  handleClassicQuery cfg (Qlang.ItemTypeOpCode Qlang.QRGroup)
186
    (map Left names) fields lock
187

    
188
handleCall _ cfg (QueryJobs names fields) =
189
  handleClassicQuery cfg (Qlang.ItemTypeLuxi Qlang.QRJob)
190
    (map (Right . fromIntegral . fromJobId) names)  fields False
191

    
192
handleCall _ cfg (QueryNetworks names fields lock) =
193
  handleClassicQuery cfg (Qlang.ItemTypeOpCode Qlang.QRNetwork)
194
    (map Left names) fields lock
195

    
196
handleCall qlock cfg (SubmitJobToDrainedQueue ops) =
197
  do
198
    jobid <- allocateJobId (Config.getMasterCandidates cfg) qlock
199
    case jobid of
200
      Bad s -> return . Bad . GenericError $ s
201
      Ok jid -> do
202
        qDir <- queueDir
203
        job <- queuedJobFromOpCodes jid ops
204
        write_result <- writeJobToDisk qDir job
205
        case write_result of
206
          Bad s -> return . Bad . GenericError $ s
207
          Ok () -> do
208
            socketpath <- defaultLuxiSocket
209
            client <- getClient socketpath
210
            pickupResult <- callMethod (PickupJob jid) client
211
            closeClient client
212
            case pickupResult of
213
              Ok _ -> return ()
214
              Bad e -> logWarning $ "Failded to notify masterd: " ++ show e
215
            return . Ok . showJSON . fromJobId $ jid
216

    
217
handleCall qlock cfg (SubmitJob ops) =
218
  do
219
    open <- isQueueOpen
220
    if not open
221
       then return . Bad . GenericError $ "Queue drained"
222
       else handleCall qlock cfg (SubmitJobToDrainedQueue ops)
223

    
224
handleCall _ _ op =
225
  return . Bad $
226
    GenericError ("Luxi call '" ++ strOfOp op ++ "' not implemented")
227

    
228
-- | Given a decoded luxi request, executes it and sends the luxi
229
-- response back to the client.
230
handleClientMsg :: MVar () -> Client -> ConfigReader -> LuxiOp -> IO Bool
231
handleClientMsg qlock client creader args = do
232
  cfg <- creader
233
  logDebug $ "Request: " ++ show args
234
  call_result <- handleCallWrapper qlock cfg args
235
  (!status, !rval) <-
236
    case call_result of
237
      Bad err -> do
238
        logWarning $ "Failed to execute request " ++ show args ++ ": "
239
                     ++ show err
240
        return (False, showJSON err)
241
      Ok result -> do
242
        -- only log the first 2,000 chars of the result
243
        logDebug $ "Result (truncated): " ++ take 2000 (J.encode result)
244
        logInfo $ "Successfully handled " ++ strOfOp args
245
        return (True, result)
246
  sendMsg client $ buildResponse status rval
247
  return True
248

    
249
-- | Handles one iteration of the client protocol: receives message,
250
-- checks it for validity and decodes it, returns response.
251
handleClient :: MVar () -> Client -> ConfigReader -> IO Bool
252
handleClient qlock client creader = do
253
  !msg <- recvMsgExt client
254
  logDebug $ "Received message: " ++ show msg
255
  case msg of
256
    RecvConnClosed -> logDebug "Connection closed" >> return False
257
    RecvError err -> logWarning ("Error during message receiving: " ++ err) >>
258
                     return False
259
    RecvOk payload ->
260
      case validateCall payload >>= decodeCall of
261
        Bad err -> do
262
             let errmsg = "Failed to parse request: " ++ err
263
             logWarning errmsg
264
             sendMsg client $ buildResponse False (showJSON errmsg)
265
             return False
266
        Ok args -> handleClientMsg qlock client creader args
267

    
268
-- | Main client loop: runs one loop of 'handleClient', and if that
269
-- doesn't report a finished (closed) connection, restarts itself.
270
clientLoop :: MVar () -> Client -> ConfigReader -> IO ()
271
clientLoop qlock client creader = do
272
  result <- handleClient qlock client creader
273
  if result
274
    then clientLoop qlock client creader
275
    else closeClient client
276

    
277
-- | Main listener loop: accepts clients, forks an I/O thread to handle
278
-- that client.
279
listener :: MVar () -> ConfigReader -> S.Socket -> IO ()
280
listener qlock creader socket = do
281
  client <- acceptClient socket
282
  _ <- forkIO $ clientLoop qlock client creader
283
  return ()
284

    
285
-- | Type alias for prepMain results
286
type PrepResult = (FilePath, S.Socket, IORef (Result ConfigData))
287

    
288
-- | Check function for luxid.
289
checkMain :: CheckFn ()
290
checkMain _ = return $ Right ()
291

    
292
-- | Prepare function for luxid.
293
prepMain :: PrepFn () PrepResult
294
prepMain _ _ = do
295
  socket_path <- Path.defaultQuerySocket
296
  cleanupSocket socket_path
297
  s <- describeError "binding to the Luxi socket"
298
         Nothing (Just socket_path) $ getServer True socket_path
299
  cref <- newIORef (Bad "Configuration not yet loaded")
300
  return (socket_path, s, cref)
301

    
302
-- | Main function.
303
main :: MainFn () PrepResult
304
main _ _ (socket_path, server, cref) = do
305
  initConfigReader id cref
306
  let creader = readIORef cref
307
  
308
  qlockFile <- jobQueueLockFile
309
  lockFile qlockFile >>= exitIfBad "Failed to obtain the job-queue lock"
310
  qlock <- newMVar ()
311

    
312
  finally
313
    (forever $ listener qlock creader server)
314
    (closeServer socket_path server)