From 9eeb0aa5d07eb718896dc53b0ee770369beab506 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Mon, 17 Sep 2012 19:19:28 +0200 Subject: [PATCH] Use autoconf-based paths from Haskell instead of constants Future changes will change Path.hs to use an environment variable. Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- Makefile.am | 1 + autotools/convert-constants | 4 ++ htools/Ganeti/Confd/Server.hs | 7 +-- htools/Ganeti/Confd/Utils.hs | 3 +- htools/Ganeti/HTools/CLI.hs | 13 +---- htools/Ganeti/HTools/Program/Hscan.hs | 3 +- htools/Ganeti/Path.hs | 70 +++++++++++++++++++++++++++ htools/Ganeti/Query/Server.hs | 3 +- htools/Ganeti/Runtime.hs | 5 +- htools/Ganeti/Ssconf.hs | 3 +- 10 files changed, 92 insertions(+), 20 deletions(-) create mode 100644 htools/Ganeti/Path.hs diff --git a/Makefile.am b/Makefile.am index 3c7820a19..5a6e137a0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -429,6 +429,7 @@ HS_LIB_SRCS = \ htools/Ganeti/Luxi.hs \ htools/Ganeti/Objects.hs \ htools/Ganeti/OpCodes.hs \ + htools/Ganeti/Path.hs \ htools/Ganeti/Query/Common.hs \ htools/Ganeti/Query/Filter.hs \ htools/Ganeti/Query/Language.hs \ diff --git a/autotools/convert-constants b/autotools/convert-constants index e051821e6..b08f6f33e 100755 --- a/autotools/convert-constants +++ b/autotools/convert-constants @@ -29,6 +29,8 @@ from ganeti import compat from ganeti import constants from ganeti import luxi from ganeti import qlang +from ganeti import _autoconf + #: Constant name regex CONSTANT_RE = re.compile("^[A-Z][A-Z0-9_-]+$") @@ -46,6 +48,7 @@ RE_TYPE = type(CONSTANT_RE) # have strings instead of easily looked-up names. IGNORED_DECL_NAMES = ["DEFAULT_ENABLED_HYPERVISOR"] + def NameRules(name): """Converts the upper-cased Python name to Haskell camelCase. @@ -289,6 +292,7 @@ def main(): print Convert(constants, "") print Convert(luxi, "luxi") print Convert(qlang, "qlang") + print Convert(_autoconf, "autoconf") if __name__ == "__main__": diff --git a/htools/Ganeti/Confd/Server.hs b/htools/Ganeti/Confd/Server.hs index 6bedee94a..b14eb4353 100644 --- a/htools/Ganeti/Confd/Server.hs +++ b/htools/Ganeti/Confd/Server.hs @@ -55,6 +55,7 @@ import Ganeti.Config import Ganeti.Hash import Ganeti.Logging import qualified Ganeti.Constants as C +import qualified Ganeti.Path as Path import Ganeti.Query.Server (runQueryD) -- * Types and constants definitions @@ -503,11 +504,11 @@ main opts = do hmac <- getClusterHmac -- Inotify setup inotify <- initINotify - let inotiaction = addNotifier inotify C.clusterConfFile cref statemvar + let inotiaction = addNotifier inotify Path.clusterConfFile cref statemvar -- fork the timeout timer - _ <- forkIO $ onTimeoutTimer inotiaction C.clusterConfFile cref statemvar + _ <- forkIO $ onTimeoutTimer inotiaction Path.clusterConfFile cref statemvar -- fork the polling timer - _ <- forkIO $ onReloadTimer inotiaction C.clusterConfFile cref statemvar + _ <- forkIO $ onReloadTimer inotiaction Path.clusterConfFile cref statemvar -- launch the queryd listener _ <- forkIO $ runQueryD Nothing (configReader cref) -- and finally enter the responder loop diff --git a/htools/Ganeti/Confd/Utils.hs b/htools/Ganeti/Confd/Utils.hs index 180e32725..043ded07b 100644 --- a/htools/Ganeti/Confd/Utils.hs +++ b/htools/Ganeti/Confd/Utils.hs @@ -40,12 +40,13 @@ import Ganeti.BasicTypes import Ganeti.Confd import Ganeti.Hash import qualified Ganeti.Constants as C +import qualified Ganeti.Path as Path import Ganeti.JSON import Ganeti.HTools.Utils -- | Returns the HMAC key. getClusterHmac :: IO HashKey -getClusterHmac = fmap B.unpack $ B.readFile C.confdHmacKey +getClusterHmac = fmap B.unpack $ B.readFile Path.confdHmacKey -- | Parses a signed request. parseRequest :: HashKey -> String -> Result (String, String, ConfdRequest) diff --git a/htools/Ganeti/HTools/CLI.hs b/htools/Ganeti/HTools/CLI.hs index 056149035..e4580136d 100644 --- a/htools/Ganeti/HTools/CLI.hs +++ b/htools/Ganeti/HTools/CLI.hs @@ -36,7 +36,6 @@ module Ganeti.HTools.CLI , parseYesNo , parseISpecString , shTemplate - , defaultLuxiSocket , maybePrintNodes , maybePrintInsts , maybeShowWarnings @@ -92,20 +91,12 @@ import Text.Printf (printf) import qualified Ganeti.HTools.Container as Container import qualified Ganeti.HTools.Node as Node -import qualified Ganeti.Constants as C +import qualified Ganeti.Path as Path import Ganeti.HTools.Types import Ganeti.HTools.Utils import Ganeti.BasicTypes import Ganeti.Common as Common --- * Constants - --- | The default value for the luxi socket. --- --- This is re-exported from the "Ganeti.Constants" module. -defaultLuxiSocket :: FilePath -defaultLuxiSocket = C.masterSocket - -- * Data types -- | Command line options structure. @@ -301,7 +292,7 @@ oIAllocSrc = Option "I" ["ialloc-src"] oLuxiSocket :: OptType oLuxiSocket = Option "L" ["luxi"] (OptArg ((\ f opts -> Ok opts { optLuxi = Just f }) . - fromMaybe defaultLuxiSocket) "SOCKET") + fromMaybe Path.defaultLuxiSocket) "SOCKET") "collect data via Luxi, optionally using the given SOCKET path" oMachineReadable :: OptType diff --git a/htools/Ganeti/HTools/Program/Hscan.hs b/htools/Ganeti/HTools/Program/Hscan.hs index 5c89de90a..b83cc57fc 100644 --- a/htools/Ganeti/HTools/Program/Hscan.hs +++ b/htools/Ganeti/HTools/Program/Hscan.hs @@ -39,6 +39,7 @@ import qualified Ganeti.HTools.Node as Node import qualified Ganeti.HTools.Instance as Instance import qualified Ganeti.HTools.Rapi as Rapi import qualified Ganeti.HTools.Luxi as Luxi +import qualified Ganeti.Path as Path import Ganeti.HTools.Loader (checkData, mergeData, ClusterData(..)) import Ganeti.HTools.Text (serializeCluster) @@ -138,7 +139,7 @@ main opts clusters = do "t_disk" "f_disk" "Score" when (null clusters) $ do - let lsock = fromMaybe defaultLuxiSocket (optLuxi opts) + let lsock = fromMaybe Path.defaultLuxiSocket (optLuxi opts) let name = local input_data <- Luxi.loadData lsock result <- writeData nlen name opts input_data diff --git a/htools/Ganeti/Path.hs b/htools/Ganeti/Path.hs new file mode 100644 index 000000000..5f439dd36 --- /dev/null +++ b/htools/Ganeti/Path.hs @@ -0,0 +1,70 @@ +{-| Path-related helper functions. + +-} + +{- + +Copyright (C) 2012 Google Inc. + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301, USA. + +-} + +module Ganeti.Path + ( defaultLuxiSocket + , defaultQuerySocket + , dataDir + , logDir + , runDir + , confdHmacKey + , clusterConfFile + ) where + +import qualified Ganeti.Constants as C +import System.FilePath + + +-- | Directory for data +dataDir :: FilePath +dataDir = C.autoconfLocalstatedir </> "lib" </> "ganeti" + +-- | Directory for runtime files +runDir :: FilePath +runDir = C.autoconfLocalstatedir </> "run" </> "ganeti" + +-- | Directory for log files +logDir :: FilePath +logDir = C.autoconfLocalstatedir </> "log" </> "ganeti" + +-- | Directory for Unix sockets +socketDir :: FilePath +socketDir = runDir </> "socket" + +-- | The default LUXI socket path. +defaultLuxiSocket :: FilePath +defaultLuxiSocket = socketDir </> "ganeti-master" + +-- | The default LUXI socket for queries. +defaultQuerySocket :: FilePath +defaultQuerySocket = socketDir </> "ganeti-query" + +-- | Path to file containing confd's HMAC key +confdHmacKey :: FilePath +confdHmacKey = dataDir </> "hmac.key" + +-- | Path to cluster configuration file +clusterConfFile :: FilePath +clusterConfFile = dataDir </> "config.data" diff --git a/htools/Ganeti/Query/Server.hs b/htools/Ganeti/Query/Server.hs index 80be42c0b..876aba04e 100644 --- a/htools/Ganeti/Query/Server.hs +++ b/htools/Ganeti/Query/Server.hs @@ -42,6 +42,7 @@ import Text.JSON.Pretty (pp_value) import System.Info (arch) import qualified Ganeti.Constants as C +import qualified Ganeti.Path as Path import Ganeti.Daemon import Ganeti.Objects import qualified Ganeti.Config as Config @@ -197,7 +198,7 @@ mainLoop creader socket = do -- only one exposed from this module. runQueryD :: Maybe FilePath -> ConfigReader -> IO () runQueryD fpath creader = do - let socket_path = fromMaybe C.querySocket fpath + let socket_path = fromMaybe Path.defaultQuerySocket fpath cleanupSocket socket_path bracket (getServer socket_path) diff --git a/htools/Ganeti/Runtime.hs b/htools/Ganeti/Runtime.hs index 7a8acca2b..82957369d 100644 --- a/htools/Ganeti/Runtime.hs +++ b/htools/Ganeti/Runtime.hs @@ -49,6 +49,7 @@ import System.Posix.User import Text.Printf import qualified Ganeti.Constants as C +import qualified Ganeti.Path as Path import Ganeti.BasicTypes data GanetiDaemon = GanetiMasterd @@ -92,11 +93,11 @@ daemonGroup (ExtraGroup AdminGroup) = C.adminGroup -- | Returns the log file for a daemon. daemonLogFile :: GanetiDaemon -> FilePath -daemonLogFile daemon = C.logDir </> daemonName daemon <.> "log" +daemonLogFile daemon = Path.logDir </> daemonName daemon <.> "log" -- | Returns the pid file name for a daemon. daemonPidFile :: GanetiDaemon -> FilePath -daemonPidFile daemon = C.runDir </> daemonName daemon <.> "pid" +daemonPidFile daemon = Path.runDir </> daemonName daemon <.> "pid" -- | All groups list. A bit hacking, as we can't enforce it's complete -- at compile time. diff --git a/htools/Ganeti/Ssconf.hs b/htools/Ganeti/Ssconf.hs index cc3639561..47a2e04ce 100644 --- a/htools/Ganeti/Ssconf.hs +++ b/htools/Ganeti/Ssconf.hs @@ -46,6 +46,7 @@ import System.FilePath ((</>)) import System.IO.Error (isDoesNotExistError) import qualified Ganeti.Constants as C +import qualified Ganeti.Path as Path import Ganeti.BasicTypes import Ganeti.HTools.Utils @@ -86,7 +87,7 @@ $(declareSADT "SSKey" keyToFilename :: Maybe FilePath -- ^ Optional config path override -> SSKey -- ^ ssconf key -> FilePath -keyToFilename optpath key = fromMaybe C.dataDir optpath </> +keyToFilename optpath key = fromMaybe Path.dataDir optpath </> sSFilePrefix ++ sSKeyToRaw key -- | Runs an IO action while transforming any error into 'Bad' -- GitLab