root / src / Ganeti / WConfd / Server.hs @ 1d4febbd
History | View | Annotate | Download (3.2 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 |
(writeLocksAsyncTask lock_file) |
89 |
dh <- withError (strMsg . ("Initialization of the daemon failed" ++) . show) |
90 |
dhOpt |
91 |
|
92 |
return (s, dh) |
93 |
|
94 |
connectConfig :: ConnectConfig |
95 |
connectConfig = ConnectConfig GanetiLuxid 60 60 |
96 |
|
97 |
-- | Main function. |
98 |
main :: MainFn () PrepResult |
99 |
main _ _ (server, dh) = |
100 |
finally |
101 |
(forever $ runWConfdMonadInt (listener (handler dh) server) dh) |
102 |
(liftIO $ closeServer server) |
103 |
|
104 |
|
105 |
-- | Options list and functions. |
106 |
options :: [OptType] |
107 |
options = |
108 |
[ oNoDaemonize |
109 |
, oNoUserChecks |
110 |
, oDebug |
111 |
, oSyslogUsage |
112 |
, oForceNode |
113 |
] |