root / src / Ganeti / Runtime.hs @ 13d26b66
History | View | Annotate | Download (7.9 kB)
1 |
{-| Implementation of the runtime configuration details. |
---|---|
2 |
|
3 |
-} |
4 |
|
5 |
{- |
6 |
|
7 |
Copyright (C) 2011, 2012, 2013, 2014 Google Inc. |
8 |
|
9 |
This program is free software; you can redistribute it and/or modify |
10 |
it under the terms of the GNU General Public License as published by |
11 |
the Free Software Foundation; either version 2 of the License, or |
12 |
(at your option) any later version. |
13 |
|
14 |
This program is distributed in the hope that it will be useful, but |
15 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 |
General Public License for more details. |
18 |
|
19 |
You should have received a copy of the GNU General Public License |
20 |
along with this program; if not, write to the Free Software |
21 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 |
02110-1301, USA. |
23 |
|
24 |
-} |
25 |
|
26 |
module Ganeti.Runtime |
27 |
( GanetiDaemon(..) |
28 |
, MiscGroup(..) |
29 |
, GanetiGroup(..) |
30 |
, RuntimeEnts |
31 |
, daemonName |
32 |
, daemonOnlyOnMaster |
33 |
, daemonLogBase |
34 |
, daemonUser |
35 |
, daemonGroup |
36 |
, ExtraLogReason(..) |
37 |
, daemonLogFile |
38 |
, daemonsExtraLogbase |
39 |
, daemonsExtraLogFile |
40 |
, daemonPidFile |
41 |
, getEnts |
42 |
, verifyDaemonUser |
43 |
) where |
44 |
|
45 |
import Control.Exception |
46 |
import Control.Monad |
47 |
import qualified Data.Map as M |
48 |
import System.Exit |
49 |
import System.FilePath |
50 |
import System.IO |
51 |
import System.IO.Error |
52 |
import System.Posix.Types |
53 |
import System.Posix.User |
54 |
import Text.Printf |
55 |
|
56 |
import qualified Ganeti.ConstantUtils as ConstantUtils |
57 |
import qualified Ganeti.Path as Path |
58 |
import Ganeti.BasicTypes |
59 |
|
60 |
import AutoConf |
61 |
|
62 |
data GanetiDaemon = GanetiMasterd |
63 |
| GanetiMetad |
64 |
| GanetiNoded |
65 |
| GanetiRapi |
66 |
| GanetiConfd |
67 |
| GanetiWConfd |
68 |
| GanetiKvmd |
69 |
| GanetiLuxid |
70 |
| GanetiMond |
71 |
deriving (Show, Enum, Bounded, Eq, Ord) |
72 |
|
73 |
data MiscGroup = DaemonsGroup |
74 |
| AdminGroup |
75 |
deriving (Show, Enum, Bounded, Eq, Ord) |
76 |
|
77 |
data GanetiGroup = DaemonGroup GanetiDaemon |
78 |
| ExtraGroup MiscGroup |
79 |
deriving (Show, Eq, Ord) |
80 |
|
81 |
type RuntimeEnts = (M.Map GanetiDaemon UserID, M.Map GanetiGroup GroupID) |
82 |
|
83 |
-- | Returns the daemon name for a given daemon. |
84 |
daemonName :: GanetiDaemon -> String |
85 |
daemonName GanetiMasterd = "ganeti-masterd" |
86 |
daemonName GanetiMetad = "ganeti-metad" |
87 |
daemonName GanetiNoded = "ganeti-noded" |
88 |
daemonName GanetiRapi = "ganeti-rapi" |
89 |
daemonName GanetiConfd = "ganeti-confd" |
90 |
daemonName GanetiWConfd = "ganeti-wconfd" |
91 |
daemonName GanetiKvmd = "ganeti-kvmd" |
92 |
daemonName GanetiLuxid = "ganeti-luxid" |
93 |
daemonName GanetiMond = "ganeti-mond" |
94 |
|
95 |
-- | Returns whether the daemon only runs on the master node. |
96 |
daemonOnlyOnMaster :: GanetiDaemon -> Bool |
97 |
daemonOnlyOnMaster GanetiMasterd = True |
98 |
daemonOnlyOnMaster GanetiMetad = False |
99 |
daemonOnlyOnMaster GanetiNoded = False |
100 |
daemonOnlyOnMaster GanetiRapi = False |
101 |
daemonOnlyOnMaster GanetiConfd = False |
102 |
daemonOnlyOnMaster GanetiWConfd = True |
103 |
daemonOnlyOnMaster GanetiKvmd = False |
104 |
daemonOnlyOnMaster GanetiLuxid = True |
105 |
daemonOnlyOnMaster GanetiMond = False |
106 |
|
107 |
-- | Returns the log file base for a daemon. |
108 |
daemonLogBase :: GanetiDaemon -> String |
109 |
daemonLogBase GanetiMasterd = "master-daemon" |
110 |
daemonLogBase GanetiMetad = "meta-daemon" |
111 |
daemonLogBase GanetiNoded = "node-daemon" |
112 |
daemonLogBase GanetiRapi = "rapi-daemon" |
113 |
daemonLogBase GanetiConfd = "conf-daemon" |
114 |
daemonLogBase GanetiWConfd = "wconf-daemon" |
115 |
daemonLogBase GanetiKvmd = "kvm-daemon" |
116 |
daemonLogBase GanetiLuxid = "luxi-daemon" |
117 |
daemonLogBase GanetiMond = "monitoring-daemon" |
118 |
|
119 |
-- | Returns the configured user name for a daemon. |
120 |
daemonUser :: GanetiDaemon -> String |
121 |
daemonUser GanetiMasterd = AutoConf.masterdUser |
122 |
daemonUser GanetiMetad = AutoConf.metadUser |
123 |
daemonUser GanetiNoded = AutoConf.nodedUser |
124 |
daemonUser GanetiRapi = AutoConf.rapiUser |
125 |
daemonUser GanetiConfd = AutoConf.confdUser |
126 |
daemonUser GanetiWConfd = AutoConf.wconfdUser |
127 |
daemonUser GanetiKvmd = AutoConf.kvmdUser |
128 |
daemonUser GanetiLuxid = AutoConf.luxidUser |
129 |
daemonUser GanetiMond = AutoConf.mondUser |
130 |
|
131 |
-- | Returns the configured group for a daemon. |
132 |
daemonGroup :: GanetiGroup -> String |
133 |
daemonGroup (DaemonGroup GanetiMasterd) = AutoConf.masterdGroup |
134 |
daemonGroup (DaemonGroup GanetiMetad) = AutoConf.metadGroup |
135 |
daemonGroup (DaemonGroup GanetiNoded) = AutoConf.nodedGroup |
136 |
daemonGroup (DaemonGroup GanetiRapi) = AutoConf.rapiGroup |
137 |
daemonGroup (DaemonGroup GanetiConfd) = AutoConf.confdGroup |
138 |
daemonGroup (DaemonGroup GanetiWConfd) = AutoConf.wconfdGroup |
139 |
daemonGroup (DaemonGroup GanetiLuxid) = AutoConf.luxidGroup |
140 |
daemonGroup (DaemonGroup GanetiKvmd) = AutoConf.kvmdGroup |
141 |
daemonGroup (DaemonGroup GanetiMond) = AutoConf.mondGroup |
142 |
daemonGroup (ExtraGroup DaemonsGroup) = AutoConf.daemonsGroup |
143 |
daemonGroup (ExtraGroup AdminGroup) = AutoConf.adminGroup |
144 |
|
145 |
data ExtraLogReason = AccessLog | ErrorLog |
146 |
|
147 |
-- | Some daemons might require more than one logfile. Specifically, |
148 |
-- right now only the Haskell http library "snap", used by the |
149 |
-- monitoring daemon, requires multiple log files. |
150 |
daemonsExtraLogbase :: GanetiDaemon -> ExtraLogReason -> String |
151 |
daemonsExtraLogbase daemon AccessLog = daemonLogBase daemon ++ "-access" |
152 |
daemonsExtraLogbase daemon ErrorLog = daemonLogBase daemon ++ "-error" |
153 |
|
154 |
-- | Returns the log file for a daemon. |
155 |
daemonLogFile :: GanetiDaemon -> IO FilePath |
156 |
daemonLogFile daemon = do |
157 |
logDir <- Path.logDir |
158 |
return $ logDir </> daemonLogBase daemon <.> "log" |
159 |
|
160 |
-- | Returns the extra log files for a daemon. |
161 |
daemonsExtraLogFile :: GanetiDaemon -> ExtraLogReason -> IO FilePath |
162 |
daemonsExtraLogFile daemon logreason = do |
163 |
logDir <- Path.logDir |
164 |
return $ logDir </> daemonsExtraLogbase daemon logreason <.> "log" |
165 |
|
166 |
-- | Returns the pid file name for a daemon. |
167 |
daemonPidFile :: GanetiDaemon -> IO FilePath |
168 |
daemonPidFile daemon = do |
169 |
runDir <- Path.runDir |
170 |
return $ runDir </> daemonName daemon <.> "pid" |
171 |
|
172 |
-- | All groups list. A bit hacking, as we can't enforce it's complete |
173 |
-- at compile time. |
174 |
allGroups :: [GanetiGroup] |
175 |
allGroups = map DaemonGroup [minBound..maxBound] ++ |
176 |
map ExtraGroup [minBound..maxBound] |
177 |
|
178 |
ignoreDoesNotExistErrors :: IO a -> IO (Result a) |
179 |
ignoreDoesNotExistErrors value = do |
180 |
result <- tryJust (\e -> if isDoesNotExistError e |
181 |
then Just (show e) |
182 |
else Nothing) value |
183 |
return $ eitherToResult result |
184 |
|
185 |
-- | Computes the group/user maps. |
186 |
getEnts :: IO (Result RuntimeEnts) |
187 |
getEnts = do |
188 |
users <- mapM (\daemon -> do |
189 |
entry <- ignoreDoesNotExistErrors . |
190 |
getUserEntryForName . |
191 |
daemonUser $ daemon |
192 |
return (entry >>= \e -> return (daemon, userID e)) |
193 |
) [minBound..maxBound] |
194 |
groups <- mapM (\group -> do |
195 |
entry <- ignoreDoesNotExistErrors . |
196 |
getGroupEntryForName . |
197 |
daemonGroup $ group |
198 |
return (entry >>= \e -> return (group, groupID e)) |
199 |
) allGroups |
200 |
return $ do -- 'Result' monad |
201 |
users' <- sequence users |
202 |
groups' <- sequence groups |
203 |
let usermap = M.fromList users' |
204 |
groupmap = M.fromList groups' |
205 |
return (usermap, groupmap) |
206 |
|
207 |
-- | Checks whether a daemon runs as the right user. |
208 |
verifyDaemonUser :: GanetiDaemon -> RuntimeEnts -> IO () |
209 |
verifyDaemonUser daemon ents = do |
210 |
myuid <- getEffectiveUserID |
211 |
-- note: we use directly ! as lookup failues shouldn't happen, due |
212 |
-- to the above map construction |
213 |
checkUidMatch (daemonName daemon) ((M.!) (fst ents) daemon) myuid |
214 |
|
215 |
-- | Check that two UIDs are matching or otherwise exit. |
216 |
checkUidMatch :: String -> UserID -> UserID -> IO () |
217 |
checkUidMatch name expected actual = |
218 |
when (expected /= actual) $ do |
219 |
hPrintf stderr "%s started using wrong user ID (%d), \ |
220 |
\expected %d\n" name |
221 |
(fromIntegral actual::Int) |
222 |
(fromIntegral expected::Int) :: IO () |
223 |
exitWith $ ExitFailure ConstantUtils.exitFailure |