root / src / Ganeti / Runtime.hs @ 9411474b
History | View | Annotate | Download (5.4 kB)
1 |
{-| Implementation of the runtime configuration details. |
---|---|
2 |
|
3 |
-} |
4 |
|
5 |
{- |
6 |
|
7 |
Copyright (C) 2011, 2012, 2013 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 |
, daemonUser |
33 |
, daemonGroup |
34 |
, daemonLogFile |
35 |
, daemonPidFile |
36 |
, getEnts |
37 |
, verifyDaemonUser |
38 |
) where |
39 |
|
40 |
import Control.Exception |
41 |
import Control.Monad |
42 |
import qualified Data.Map as M |
43 |
import System.Exit |
44 |
import System.FilePath |
45 |
import System.IO |
46 |
import System.IO.Error |
47 |
import System.Posix.Types |
48 |
import System.Posix.User |
49 |
import Text.Printf |
50 |
|
51 |
import qualified Ganeti.Constants as C |
52 |
import qualified Ganeti.Path as Path |
53 |
import Ganeti.BasicTypes |
54 |
|
55 |
data GanetiDaemon = GanetiMasterd |
56 |
| GanetiNoded |
57 |
| GanetiRapi |
58 |
| GanetiConfd |
59 |
deriving (Show, Enum, Bounded, Eq, Ord) |
60 |
|
61 |
data MiscGroup = DaemonsGroup |
62 |
| AdminGroup |
63 |
deriving (Show, Enum, Bounded, Eq, Ord) |
64 |
|
65 |
data GanetiGroup = DaemonGroup GanetiDaemon |
66 |
| ExtraGroup MiscGroup |
67 |
deriving (Show, Eq, Ord) |
68 |
|
69 |
type RuntimeEnts = (M.Map GanetiDaemon UserID, M.Map GanetiGroup GroupID) |
70 |
|
71 |
-- | Returns the daemon name for a given daemon. |
72 |
daemonName :: GanetiDaemon -> String |
73 |
daemonName GanetiMasterd = C.masterd |
74 |
daemonName GanetiNoded = C.noded |
75 |
daemonName GanetiRapi = C.rapi |
76 |
daemonName GanetiConfd = C.confd |
77 |
|
78 |
-- | Returns the log file base for a daemon. |
79 |
daemonLogBase :: GanetiDaemon -> String |
80 |
daemonLogBase GanetiMasterd = C.daemonsLogbaseGanetiMasterd |
81 |
daemonLogBase GanetiNoded = C.daemonsLogbaseGanetiNoded |
82 |
daemonLogBase GanetiRapi = C.daemonsLogbaseGanetiRapi |
83 |
daemonLogBase GanetiConfd = C.daemonsLogbaseGanetiConfd |
84 |
|
85 |
-- | Returns the configured user name for a daemon. |
86 |
daemonUser :: GanetiDaemon -> String |
87 |
daemonUser GanetiMasterd = C.masterdUser |
88 |
daemonUser GanetiNoded = C.nodedUser |
89 |
daemonUser GanetiRapi = C.rapiUser |
90 |
daemonUser GanetiConfd = C.confdUser |
91 |
|
92 |
-- | Returns the configured group for a daemon. |
93 |
daemonGroup :: GanetiGroup -> String |
94 |
daemonGroup (DaemonGroup GanetiMasterd) = C.masterdGroup |
95 |
daemonGroup (DaemonGroup GanetiNoded) = C.nodedGroup |
96 |
daemonGroup (DaemonGroup GanetiRapi) = C.rapiGroup |
97 |
daemonGroup (DaemonGroup GanetiConfd) = C.confdGroup |
98 |
daemonGroup (ExtraGroup DaemonsGroup) = C.daemonsGroup |
99 |
daemonGroup (ExtraGroup AdminGroup) = C.adminGroup |
100 |
|
101 |
-- | Returns the log file for a daemon. |
102 |
daemonLogFile :: GanetiDaemon -> IO FilePath |
103 |
daemonLogFile daemon = do |
104 |
logDir <- Path.logDir |
105 |
return $ logDir </> daemonLogBase daemon <.> "log" |
106 |
|
107 |
-- | Returns the pid file name for a daemon. |
108 |
daemonPidFile :: GanetiDaemon -> IO FilePath |
109 |
daemonPidFile daemon = do |
110 |
runDir <- Path.runDir |
111 |
return $ runDir </> daemonName daemon <.> "pid" |
112 |
|
113 |
-- | All groups list. A bit hacking, as we can't enforce it's complete |
114 |
-- at compile time. |
115 |
allGroups :: [GanetiGroup] |
116 |
allGroups = map DaemonGroup [minBound..maxBound] ++ |
117 |
map ExtraGroup [minBound..maxBound] |
118 |
|
119 |
ignoreDoesNotExistErrors :: IO a -> IO (Result a) |
120 |
ignoreDoesNotExistErrors value = do |
121 |
result <- tryJust (\e -> if isDoesNotExistError e |
122 |
then Just (show e) |
123 |
else Nothing) value |
124 |
return $ eitherToResult result |
125 |
|
126 |
-- | Computes the group/user maps. |
127 |
getEnts :: IO (Result RuntimeEnts) |
128 |
getEnts = do |
129 |
users <- mapM (\daemon -> do |
130 |
entry <- ignoreDoesNotExistErrors . |
131 |
getUserEntryForName . |
132 |
daemonUser $ daemon |
133 |
return (entry >>= \e -> return (daemon, userID e)) |
134 |
) [minBound..maxBound] |
135 |
groups <- mapM (\group -> do |
136 |
entry <- ignoreDoesNotExistErrors . |
137 |
getGroupEntryForName . |
138 |
daemonGroup $ group |
139 |
return (entry >>= \e -> return (group, groupID e)) |
140 |
) allGroups |
141 |
return $ do -- 'Result' monad |
142 |
users' <- sequence users |
143 |
groups' <- sequence groups |
144 |
let usermap = M.fromList users' |
145 |
groupmap = M.fromList groups' |
146 |
return (usermap, groupmap) |
147 |
|
148 |
|
149 |
-- | Checks whether a daemon runs as the right user. |
150 |
verifyDaemonUser :: GanetiDaemon -> RuntimeEnts -> IO () |
151 |
verifyDaemonUser daemon ents = do |
152 |
myuid <- getEffectiveUserID |
153 |
-- note: we use directly ! as lookup failues shouldn't happen, due |
154 |
-- to the above map construction |
155 |
checkUidMatch (daemonName daemon) ((M.!) (fst ents) daemon) myuid |
156 |
|
157 |
-- | Check that two UIDs are matching or otherwise exit. |
158 |
checkUidMatch :: String -> UserID -> UserID -> IO () |
159 |
checkUidMatch name expected actual = |
160 |
when (expected /= actual) $ do |
161 |
hPrintf stderr "%s started using wrong user ID (%d), \ |
162 |
\expected %d\n" name |
163 |
(fromIntegral actual::Int) |
164 |
(fromIntegral expected::Int) :: IO () |
165 |
exitWith $ ExitFailure C.exitFailure |