Revision 670e954a src/Ganeti/Daemon.hs

b/src/Ganeti/Daemon.hs
50 50
import Data.Maybe (fromMaybe)
51 51
import Data.Word
52 52
import GHC.IO.Handle (hDuplicateTo)
53
import Network.BSD (getHostName)
53 54
import qualified Network.Socket as Socket
54 55
import System.Console.GetOpt
55 56
import System.Exit
......
291 292
                    (resolveAddr port saddr)
292 293
                    (ioErrorToResult $ "Invalid address " ++ saddr)
293 294

  
295
-- | Environment variable to override the assumed host name of the
296
-- current node.
297
vClusterHostNameEnvVar :: String
298
vClusterHostNameEnvVar = "GANETI_HOSTNAME"
299

  
300
-- | Returns if the current node is the master node.
301
isMaster :: IO Bool
302
isMaster = do
303
  let ioErrorToNothing :: IOError -> IO (Maybe String)
304
      ioErrorToNothing _ = return Nothing
305
  vcluster_node <- Control.Exception.catch
306
                     (liftM Just (getEnv vClusterHostNameEnvVar))
307
                     ioErrorToNothing
308
  curNode <- case vcluster_node of
309
    Just node_name -> return node_name
310
    Nothing -> getHostName
311
  masterNode <- Ssconf.getMasterNode Nothing
312
  case masterNode of
313
    Ok n -> return (curNode == n)
314
    Bad _ -> return False
315

  
316
-- | Ensures that the daemon runs on the right node (and exits
317
-- gracefully if it doesnt)
318
ensureNode :: GanetiDaemon -> IO ()
319
ensureNode daemon = do
320
  is_master <- isMaster
321
  when (daemonOnlyOnMaster daemon && not is_master) $ do
322
    putStrLn "Not master, exiting."
323
    exitWith (ExitFailure C.exitNotmaster)
324

  
294 325
-- | Run an I\/O action that might throw an I\/O error, under a
295 326
-- handler that will simply annotate and re-throw the exception.
296 327
describeError :: String -> Maybe Handle -> Maybe FilePath -> IO a -> IO a
......
337 368
            -> IO ()
338 369
genericMain daemon options check_fn prep_fn exec_fn = do
339 370
  let progname = daemonName daemon
371

  
340 372
  (opts, args) <- parseArgs progname options
341 373

  
374
  ensureNode daemon
375

  
342 376
  exitUnless (null args) "This program doesn't take any arguments"
343 377

  
344 378
  unless (optNoUserChecks opts) $ do

Also available in: Unified diff