Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / WConfd / Server.hs @ 13d26b66

History | View | Annotate | Download (2.7 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| The implementation of Ganeti WConfd daemon server.
4

    
5
As TemplateHaskell require that splices be defined in a separate
6
module, we combine all the TemplateHaskell functionality that HTools
7
needs in this module (except the one for unittests).
8

    
9
-}
10

    
11
{-
12

    
13
Copyright (C) 2013 Google Inc.
14

    
15
This program is free software; you can redistribute it and/or modify
16
it under the terms of the GNU General Public License as published by
17
the Free Software Foundation; either version 2 of the License, or
18
(at your option) any later version.
19

    
20
This program is distributed in the hope that it will be useful, but
21
WITHOUT ANY WARRANTY; without even the implied warranty of
22
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
General Public License for more details.
24

    
25
You should have received a copy of the GNU General Public License
26
along with this program; if not, write to the Free Software
27
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28
02110-1301, USA.
29

    
30
-}
31

    
32
module Ganeti.WConfd.Server where
33

    
34
import Control.Exception
35
import Control.Monad
36
import Control.Monad.Error
37

    
38
import Ganeti.BasicTypes
39
import Ganeti.Daemon
40
import Ganeti.Locking.Allocation
41
import qualified Ganeti.Path as Path
42
import Ganeti.THH.RPC
43
import Ganeti.UDSServer
44

    
45
import Ganeti.Runtime
46
import Ganeti.Utils.AsyncWorker
47
import Ganeti.WConfd.ConfigState
48
import Ganeti.WConfd.Core
49
import Ganeti.WConfd.Monad
50

    
51
handler :: DaemonHandle -> RpcServer WConfdMonadInt
52
handler ch = $( mkRpcM exportedFunctions )
53

    
54

    
55
-- | Type alias for prepMain results
56
type PrepResult = (Server, DaemonHandle)
57

    
58
-- | Check function for luxid.
59
checkMain :: CheckFn ()
60
checkMain _ = return $ Right ()
61

    
62
-- | Prepare function for luxid.
63
prepMain :: PrepFn () PrepResult
64
prepMain _ _ = do
65
  socket_path <- Path.defaultWConfdSocket
66
  cleanupSocket socket_path
67
  s <- describeError "binding to the socket" Nothing (Just socket_path)
68
         $ connectServer connectConfig True socket_path
69
  -- TODO: Lock the configuration file so that running the daemon twice fails?
70
  conf_file <- Path.clusterConfFile
71

    
72
  dhOpt <- runResultT $ mkDaemonHandle conf_file mkConfigState emptyAllocation
73
                                       (const $ mkAsyncWorker (return ()))
74
  -- TODO: read current lock allocation from disk
75
  dh <- withError (strMsg . ("Initialization of the daemon failed" ++) . show)
76
                  dhOpt
77

    
78
  return (s, dh)
79

    
80
connectConfig :: ConnectConfig
81
connectConfig = ConnectConfig GanetiLuxid 60 60
82

    
83
-- | Main function.
84
main :: MainFn () PrepResult
85
main _ _ (server, dh) =
86
  finally
87
    (forever $ runWConfdMonadInt (listener (handler dh) server) dh)
88
    (liftIO $ closeServer server)
89

    
90

    
91
-- | Options list and functions.
92
options :: [OptType]
93
options =
94
  [ oNoDaemonize
95
  , oNoUserChecks
96
  , oDebug
97
  , oSyslogUsage
98
  ]