module Ganeti.Runtime
( GanetiDaemon(..)
, MiscGroup(..)
, GanetiGroup(..)
, RuntimeEnts
, daemonName
, daemonOnlyOnMaster
, daemonLogBase
, daemonUser
, daemonGroup
, ExtraLogReason(..)
, daemonLogFile
, daemonsExtraLogbase
, daemonsExtraLogFile
, daemonPidFile
, getEnts
, verifyDaemonUser
) where
import Control.Exception
import Control.Monad
import qualified Data.Map as M
import System.Exit
import System.FilePath
import System.IO
import System.IO.Error
import System.Posix.Types
import System.Posix.User
import Text.Printf
import qualified Ganeti.ConstantUtils as ConstantUtils
import qualified Ganeti.Path as Path
import Ganeti.BasicTypes
import AutoConf
data GanetiDaemon = GanetiMasterd
| GanetiNoded
| GanetiRapi
| GanetiConfd
| GanetiKvmd
| GanetiLuxid
| GanetiMond
deriving (Show, Enum, Bounded, Eq, Ord)
data MiscGroup = DaemonsGroup
| AdminGroup
deriving (Show, Enum, Bounded, Eq, Ord)
data GanetiGroup = DaemonGroup GanetiDaemon
| ExtraGroup MiscGroup
deriving (Show, Eq, Ord)
type RuntimeEnts = (M.Map GanetiDaemon UserID, M.Map GanetiGroup GroupID)
daemonName :: GanetiDaemon -> String
daemonName GanetiMasterd = "ganeti-masterd"
daemonName GanetiNoded = "ganeti-noded"
daemonName GanetiRapi = "ganeti-rapi"
daemonName GanetiConfd = "ganeti-confd"
daemonName GanetiKvmd = "ganeti-kvmd"
daemonName GanetiLuxid = "ganeti-luxid"
daemonName GanetiMond = "ganeti-mond"
daemonOnlyOnMaster :: GanetiDaemon -> Bool
daemonOnlyOnMaster GanetiMasterd = True
daemonOnlyOnMaster GanetiNoded = False
daemonOnlyOnMaster GanetiRapi = False
daemonOnlyOnMaster GanetiConfd = False
daemonOnlyOnMaster GanetiKvmd = False
daemonOnlyOnMaster GanetiLuxid = True
daemonOnlyOnMaster GanetiMond = False
daemonLogBase :: GanetiDaemon -> String
daemonLogBase GanetiMasterd = "master-daemon"
daemonLogBase GanetiNoded = "node-daemon"
daemonLogBase GanetiRapi = "rapi-daemon"
daemonLogBase GanetiConfd = "conf-daemon"
daemonLogBase GanetiKvmd = "kvm-daemon"
daemonLogBase GanetiLuxid = "luxi-daemon"
daemonLogBase GanetiMond = "monitoring-daemon"
daemonUser :: GanetiDaemon -> String
daemonUser GanetiMasterd = AutoConf.masterdUser
daemonUser GanetiNoded = AutoConf.nodedUser
daemonUser GanetiRapi = AutoConf.rapiUser
daemonUser GanetiConfd = AutoConf.confdUser
daemonUser GanetiKvmd = AutoConf.kvmdUser
daemonUser GanetiLuxid = AutoConf.luxidUser
daemonUser GanetiMond = AutoConf.mondUser
daemonGroup :: GanetiGroup -> String
daemonGroup (DaemonGroup GanetiMasterd) = AutoConf.masterdGroup
daemonGroup (DaemonGroup GanetiNoded) = AutoConf.nodedGroup
daemonGroup (DaemonGroup GanetiRapi) = AutoConf.rapiGroup
daemonGroup (DaemonGroup GanetiConfd) = AutoConf.confdGroup
daemonGroup (DaemonGroup GanetiLuxid) = AutoConf.luxidGroup
daemonGroup (DaemonGroup GanetiKvmd) = AutoConf.kvmdGroup
daemonGroup (DaemonGroup GanetiMond) = AutoConf.mondGroup
daemonGroup (ExtraGroup DaemonsGroup) = AutoConf.daemonsGroup
daemonGroup (ExtraGroup AdminGroup) = AutoConf.adminGroup
data ExtraLogReason = AccessLog | ErrorLog
daemonsExtraLogbase :: GanetiDaemon -> ExtraLogReason -> String
daemonsExtraLogbase daemon AccessLog = daemonLogBase daemon ++ "-access"
daemonsExtraLogbase daemon ErrorLog = daemonLogBase daemon ++ "-error"
daemonLogFile :: GanetiDaemon -> IO FilePath
daemonLogFile daemon = do
logDir <- Path.logDir
return $ logDir </> daemonLogBase daemon <.> "log"
daemonsExtraLogFile :: GanetiDaemon -> ExtraLogReason -> IO FilePath
daemonsExtraLogFile daemon logreason = do
logDir <- Path.logDir
return $ logDir </> daemonsExtraLogbase daemon logreason <.> "log"
daemonPidFile :: GanetiDaemon -> IO FilePath
daemonPidFile daemon = do
runDir <- Path.runDir
return $ runDir </> daemonName daemon <.> "pid"
allGroups :: [GanetiGroup]
allGroups = map DaemonGroup [minBound..maxBound] ++
map ExtraGroup [minBound..maxBound]
ignoreDoesNotExistErrors :: IO a -> IO (Result a)
ignoreDoesNotExistErrors value = do
result <- tryJust (\e -> if isDoesNotExistError e
then Just (show e)
else Nothing) value
return $ eitherToResult result
getEnts :: IO (Result RuntimeEnts)
getEnts = do
users <- mapM (\daemon -> do
entry <- ignoreDoesNotExistErrors .
getUserEntryForName .
daemonUser $ daemon
return (entry >>= \e -> return (daemon, userID e))
) [minBound..maxBound]
groups <- mapM (\group -> do
entry <- ignoreDoesNotExistErrors .
getGroupEntryForName .
daemonGroup $ group
return (entry >>= \e -> return (group, groupID e))
) allGroups
return $ do
users' <- sequence users
groups' <- sequence groups
let usermap = M.fromList users'
groupmap = M.fromList groups'
return (usermap, groupmap)
verifyDaemonUser :: GanetiDaemon -> RuntimeEnts -> IO ()
verifyDaemonUser daemon ents = do
myuid <- getEffectiveUserID
checkUidMatch (daemonName daemon) ((M.!) (fst ents) daemon) myuid
checkUidMatch :: String -> UserID -> UserID -> IO ()
checkUidMatch name expected actual =
when (expected /= actual) $ do
hPrintf stderr "%s started using wrong user ID (%d), \
\expected %d\n" name
(fromIntegral actual::Int)
(fromIntegral expected::Int) :: IO ()
exitWith $ ExitFailure ConstantUtils.exitFailure