Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / WConfd / Server.hs @ 8c337f87

History | View | Annotate | Download (3.4 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, 2014 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.Concurrent (forkIO)
35
import Control.Exception
36
import Control.Monad
37
import Control.Monad.Error
38
import System.Directory (doesFileExist)
39

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

    
49
import Ganeti.Runtime
50
import Ganeti.WConfd.ConfigState
51
import Ganeti.WConfd.ConfigWriter
52
import Ganeti.WConfd.Core
53
import Ganeti.WConfd.DeathDetection (cleanupLocksTask)
54
import Ganeti.WConfd.Monad
55

    
56
handler :: DaemonHandle -> RpcServer WConfdMonadInt
57
handler ch = $( mkRpcM exportedFunctions )
58

    
59

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

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

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

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

    
94
  return (s, dh)
95

    
96
connectConfig :: ConnectConfig
97
connectConfig = ConnectConfig GanetiLuxid 60 60
98

    
99
-- | Main function.
100
main :: MainFn () PrepResult
101
main _ _ (server, dh) = do
102
  logDebug "Starting the cleanup task"
103
  _ <- forkIO $ runWConfdMonadInt cleanupLocksTask dh
104
  finally
105
    (forever $ runWConfdMonadInt (listener (handler dh) server) dh)
106
    (liftIO $ closeServer server)
107

    
108

    
109
-- | Options list and functions.
110
options :: [OptType]
111
options =
112
  [ oNoDaemonize
113
  , oNoUserChecks
114
  , oDebug
115
  , oSyslogUsage
116
  , oForceNode
117
  ]