root / src / Ganeti / Runtime.hs @ 129bde01
History | View | Annotate | Download (7.5 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.Monad |
46 |
import Control.Monad.Error |
47 |
import qualified Data.Map as M |
48 |
import System.Exit |
49 |
import System.FilePath |
50 |
import System.IO |
51 |
import System.Posix.Types |
52 |
import System.Posix.User |
53 |
import Text.Printf |
54 |
|
55 |
import qualified Ganeti.ConstantUtils as ConstantUtils |
56 |
import qualified Ganeti.Path as Path |
57 |
import Ganeti.BasicTypes |
58 |
|
59 |
import AutoConf |
60 |
|
61 |
data GanetiDaemon = GanetiMasterd |
62 |
| GanetiMetad |
63 |
| GanetiNoded |
64 |
| GanetiRapi |
65 |
| GanetiConfd |
66 |
| GanetiWConfd |
67 |
| GanetiKvmd |
68 |
| GanetiLuxid |
69 |
| GanetiMond |
70 |
deriving (Show, Enum, Bounded, Eq, Ord) |
71 |
|
72 |
data MiscGroup = DaemonsGroup |
73 |
| AdminGroup |
74 |
deriving (Show, Enum, Bounded, Eq, Ord) |
75 |
|
76 |
data GanetiGroup = DaemonGroup GanetiDaemon |
77 |
| ExtraGroup MiscGroup |
78 |
deriving (Show, Eq, Ord) |
79 |
|
80 |
data RuntimeEnts = RuntimeEnts |
81 |
{ reUserToUid :: M.Map GanetiDaemon UserID |
82 |
, reUidToUser :: M.Map UserID String |
83 |
, reGroupToGid :: M.Map GanetiGroup GroupID |
84 |
, reGidToGroup :: M.Map GroupID String |
85 |
} |
86 |
|
87 |
-- | Returns the daemon name for a given daemon. |
88 |
daemonName :: GanetiDaemon -> String |
89 |
daemonName GanetiMasterd = "ganeti-masterd" |
90 |
daemonName GanetiMetad = "ganeti-metad" |
91 |
daemonName GanetiNoded = "ganeti-noded" |
92 |
daemonName GanetiRapi = "ganeti-rapi" |
93 |
daemonName GanetiConfd = "ganeti-confd" |
94 |
daemonName GanetiWConfd = "ganeti-wconfd" |
95 |
daemonName GanetiKvmd = "ganeti-kvmd" |
96 |
daemonName GanetiLuxid = "ganeti-luxid" |
97 |
daemonName GanetiMond = "ganeti-mond" |
98 |
|
99 |
-- | Returns whether the daemon only runs on the master node. |
100 |
daemonOnlyOnMaster :: GanetiDaemon -> Bool |
101 |
daemonOnlyOnMaster GanetiMasterd = True |
102 |
daemonOnlyOnMaster GanetiMetad = False |
103 |
daemonOnlyOnMaster GanetiNoded = False |
104 |
daemonOnlyOnMaster GanetiRapi = False |
105 |
daemonOnlyOnMaster GanetiConfd = False |
106 |
daemonOnlyOnMaster GanetiWConfd = True |
107 |
daemonOnlyOnMaster GanetiKvmd = False |
108 |
daemonOnlyOnMaster GanetiLuxid = True |
109 |
daemonOnlyOnMaster GanetiMond = False |
110 |
|
111 |
-- | Returns the log file base for a daemon. |
112 |
daemonLogBase :: GanetiDaemon -> String |
113 |
daemonLogBase GanetiMasterd = "master-daemon" |
114 |
daemonLogBase GanetiMetad = "meta-daemon" |
115 |
daemonLogBase GanetiNoded = "node-daemon" |
116 |
daemonLogBase GanetiRapi = "rapi-daemon" |
117 |
daemonLogBase GanetiConfd = "conf-daemon" |
118 |
daemonLogBase GanetiWConfd = "wconf-daemon" |
119 |
daemonLogBase GanetiKvmd = "kvm-daemon" |
120 |
daemonLogBase GanetiLuxid = "luxi-daemon" |
121 |
daemonLogBase GanetiMond = "monitoring-daemon" |
122 |
|
123 |
-- | Returns the configured user name for a daemon. |
124 |
daemonUser :: GanetiDaemon -> String |
125 |
daemonUser GanetiMasterd = AutoConf.masterdUser |
126 |
daemonUser GanetiMetad = AutoConf.metadUser |
127 |
daemonUser GanetiNoded = AutoConf.nodedUser |
128 |
daemonUser GanetiRapi = AutoConf.rapiUser |
129 |
daemonUser GanetiConfd = AutoConf.confdUser |
130 |
daemonUser GanetiWConfd = AutoConf.wconfdUser |
131 |
daemonUser GanetiKvmd = AutoConf.kvmdUser |
132 |
daemonUser GanetiLuxid = AutoConf.luxidUser |
133 |
daemonUser GanetiMond = AutoConf.mondUser |
134 |
|
135 |
-- | Returns the configured group for a daemon. |
136 |
daemonGroup :: GanetiGroup -> String |
137 |
daemonGroup (DaemonGroup GanetiMasterd) = AutoConf.masterdGroup |
138 |
daemonGroup (DaemonGroup GanetiMetad) = AutoConf.metadGroup |
139 |
daemonGroup (DaemonGroup GanetiNoded) = AutoConf.nodedGroup |
140 |
daemonGroup (DaemonGroup GanetiRapi) = AutoConf.rapiGroup |
141 |
daemonGroup (DaemonGroup GanetiConfd) = AutoConf.confdGroup |
142 |
daemonGroup (DaemonGroup GanetiWConfd) = AutoConf.wconfdGroup |
143 |
daemonGroup (DaemonGroup GanetiLuxid) = AutoConf.luxidGroup |
144 |
daemonGroup (DaemonGroup GanetiKvmd) = AutoConf.kvmdGroup |
145 |
daemonGroup (DaemonGroup GanetiMond) = AutoConf.mondGroup |
146 |
daemonGroup (ExtraGroup DaemonsGroup) = AutoConf.daemonsGroup |
147 |
daemonGroup (ExtraGroup AdminGroup) = AutoConf.adminGroup |
148 |
|
149 |
data ExtraLogReason = AccessLog | ErrorLog |
150 |
|
151 |
-- | Some daemons might require more than one logfile. Specifically, |
152 |
-- right now only the Haskell http library "snap", used by the |
153 |
-- monitoring daemon, requires multiple log files. |
154 |
daemonsExtraLogbase :: GanetiDaemon -> ExtraLogReason -> String |
155 |
daemonsExtraLogbase daemon AccessLog = daemonLogBase daemon ++ "-access" |
156 |
daemonsExtraLogbase daemon ErrorLog = daemonLogBase daemon ++ "-error" |
157 |
|
158 |
-- | Returns the log file for a daemon. |
159 |
daemonLogFile :: GanetiDaemon -> IO FilePath |
160 |
daemonLogFile daemon = do |
161 |
logDir <- Path.logDir |
162 |
return $ logDir </> daemonLogBase daemon <.> "log" |
163 |
|
164 |
-- | Returns the extra log files for a daemon. |
165 |
daemonsExtraLogFile :: GanetiDaemon -> ExtraLogReason -> IO FilePath |
166 |
daemonsExtraLogFile daemon logreason = do |
167 |
logDir <- Path.logDir |
168 |
return $ logDir </> daemonsExtraLogbase daemon logreason <.> "log" |
169 |
|
170 |
-- | Returns the pid file name for a daemon. |
171 |
daemonPidFile :: GanetiDaemon -> IO FilePath |
172 |
daemonPidFile daemon = do |
173 |
runDir <- Path.runDir |
174 |
return $ runDir </> daemonName daemon <.> "pid" |
175 |
|
176 |
-- | All groups list. A bit hacking, as we can't enforce it's complete |
177 |
-- at compile time. |
178 |
allGroups :: [GanetiGroup] |
179 |
allGroups = map DaemonGroup [minBound..maxBound] ++ |
180 |
map ExtraGroup [minBound..maxBound] |
181 |
|
182 |
-- | Computes the group/user maps. |
183 |
getEnts :: (Error e) => ResultT e IO RuntimeEnts |
184 |
getEnts = do |
185 |
let userOf = liftM userID . liftIO . getUserEntryForName . daemonUser |
186 |
let groupOf = liftM groupID . liftIO . getGroupEntryForName . daemonGroup |
187 |
let allDaemons = [minBound..maxBound] :: [GanetiDaemon] |
188 |
users <- mapM userOf allDaemons |
189 |
groups <- mapM groupOf allGroups |
190 |
return $ RuntimeEnts |
191 |
(M.fromList $ zip allDaemons users) |
192 |
(M.fromList $ zip users (map daemonUser allDaemons)) |
193 |
(M.fromList $ zip allGroups groups) |
194 |
(M.fromList $ zip groups (map daemonGroup allGroups)) |
195 |
|
196 |
-- | Checks whether a daemon runs as the right user. |
197 |
verifyDaemonUser :: GanetiDaemon -> RuntimeEnts -> IO () |
198 |
verifyDaemonUser daemon ents = do |
199 |
myuid <- getEffectiveUserID |
200 |
-- note: we use directly ! as lookup failues shouldn't happen, due |
201 |
-- to the above map construction |
202 |
checkUidMatch (daemonName daemon) ((M.!) (reUserToUid ents) daemon) myuid |
203 |
|
204 |
-- | Check that two UIDs are matching or otherwise exit. |
205 |
checkUidMatch :: String -> UserID -> UserID -> IO () |
206 |
checkUidMatch name expected actual = |
207 |
when (expected /= actual) $ do |
208 |
hPrintf stderr "%s started using wrong user ID (%d), \ |
209 |
\expected %d\n" name |
210 |
(fromIntegral actual::Int) |
211 |
(fromIntegral expected::Int) :: IO () |
212 |
exitWith $ ExitFailure ConstantUtils.exitFailure |