Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / WConfd / Server.hs @ 5ca6adf5

History | View | Annotate | Download (3.1 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
import System.Directory (doesFileExist)
38

    
39
import Ganeti.BasicTypes
40
import Ganeti.Daemon
41
import Ganeti.Logging (logInfo)
42
import Ganeti.Locking.Allocation
43
import Ganeti.Locking.Locks
44
import qualified Ganeti.Path as Path
45
import Ganeti.THH.RPC
46
import Ganeti.UDSServer
47

    
48
import Ganeti.Runtime
49
import Ganeti.WConfd.ConfigState
50
import Ganeti.WConfd.ConfigWriter
51
import Ganeti.WConfd.Core
52
import Ganeti.WConfd.Monad
53

    
54
handler :: DaemonHandle -> RpcServer WConfdMonadInt
55
handler ch = $( mkRpcM exportedFunctions )
56

    
57

    
58
-- | Type alias for prepMain results
59
type PrepResult = (Server, DaemonHandle)
60

    
61
-- | Check function for luxid.
62
checkMain :: CheckFn ()
63
checkMain _ = return $ Right ()
64

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

    
75
  lock_file <- Path.lockStatusFile
76
  lock_file_present <- doesFileExist lock_file
77
  unless lock_file_present
78
    $ logInfo "No saved lock status; assuming all locks free"
79
  dhOpt <- runResultT $ do
80
    (cdata, cstat) <- loadConfigFromFile conf_file
81
    lock <- if lock_file_present
82
              then loadLockAllocation lock_file
83
              else return emptyAllocation
84
    mkDaemonHandle conf_file
85
                   (mkConfigState cdata)
86
                   lock
87
                   (saveConfigAsyncTask conf_file cstat)
88
  dh <- withError (strMsg . ("Initialization of the daemon failed" ++) . show)
89
                  dhOpt
90

    
91
  return (s, dh)
92

    
93
connectConfig :: ConnectConfig
94
connectConfig = ConnectConfig GanetiLuxid 60 60
95

    
96
-- | Main function.
97
main :: MainFn () PrepResult
98
main _ _ (server, dh) =
99
  finally
100
    (forever $ runWConfdMonadInt (listener (handler dh) server) dh)
101
    (liftIO $ closeServer server)
102

    
103

    
104
-- | Options list and functions.
105
options :: [OptType]
106
options =
107
  [ oNoDaemonize
108
  , oNoUserChecks
109
  , oDebug
110
  , oSyslogUsage
111
  ]