Commit bf3a5cc0 authored by Jose A. Lopes's avatar Jose A. Lopes
Browse files

Modify metadata daemon web server to serve OS parameters



Extend metadata web server to serve the OS parameters from the shared
MVar containing the instances' configurations, indexed by their IP
address.
Signed-off-by: default avatarJose A. Lopes <jabolopes@google.com>
Reviewed-by: default avatarHrvoje Ribicic <riba@google.com>
parent f007e290
{-| Metadata daemon
{-| Web server for the metadata daemon.
-}
......@@ -26,10 +26,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
module Ganeti.Metad.WebServer (start) where
import Control.Applicative
import Control.Concurrent (MVar, readMVar)
import Control.Monad.IO.Class (liftIO)
import qualified Data.Map as Map
import qualified Data.ByteString.Char8 as ByteString (pack, unpack)
import Snap.Core
import Snap.Http.Server
import qualified Text.JSON as JSON
import Ganeti.Daemon
import qualified Ganeti.Constants as Constants
......@@ -37,6 +40,8 @@ import qualified Ganeti.Logging as Logging
import Ganeti.Runtime (GanetiDaemon(..), ExtraLogReason(..))
import qualified Ganeti.Runtime as Runtime
import Ganeti.Metad.Types (InstanceParams)
type MetaM = Snap ()
error404 :: MetaM
......@@ -44,19 +49,31 @@ error404 = do
modifyResponse . setResponseStatus 404 $ ByteString.pack "Not found"
writeBS $ ByteString.pack "Resource not found"
handleMetadata :: Method -> String -> String -> String -> MetaM
handleMetadata GET "ganeti" "latest" "meta_data.json" =
handleMetadata
:: MVar InstanceParams -> Method -> String -> String -> String -> MetaM
handleMetadata _ GET "ganeti" "latest" "meta_data.json" =
liftIO $ Logging.logInfo "ganeti metadata"
handleMetadata GET "ganeti" "latest" "os/parameters.json" =
liftIO $ Logging.logInfo "ganeti OS parameters"
handleMetadata GET "ganeti" "latest" "read" =
handleMetadata params GET "ganeti" "latest" "os/parameters.json" =
do remoteAddr <- ByteString.unpack . rqRemoteAddr <$> getRequest
instanceParams <- liftIO $ do
Logging.logInfo $ "ganeti OS parameter for " ++ show remoteAddr
readMVar params
case Map.lookup remoteAddr instanceParams of
Nothing ->
error404
Just osParams ->
writeBS .
ByteString.pack .
JSON.encode $ osParams
handleMetadata _ GET "ganeti" "latest" "read" =
liftIO $ Logging.logInfo "ganeti READ"
handleMetadata POST "ganeti" "latest" "write" =
handleMetadata _ POST "ganeti" "latest" "write" =
liftIO $ Logging.logInfo "ganeti WRITE"
handleMetadata _ _ _ _ = error404
handleMetadata _ _ _ _ _ =
error404
routeMetadata :: MetaM
routeMetadata =
routeMetadata :: MVar InstanceParams -> MetaM
routeMetadata params =
route [ (providerRoute1, dispatchMetadata)
, (providerRoute2, dispatchMetadata)
] <|> dispatchMetadata
......@@ -75,7 +92,7 @@ routeMetadata =
p <- getParamString provider
v <- getParamString version
r <- ByteString.unpack . rqPathInfo <$> getRequest
handleMetadata m p v r
handleMetadata params m p v r
defaultHttpConf :: DaemonOptions -> FilePath -> FilePath -> Config Snap ()
defaultHttpConf opts accessLog errorLog =
......@@ -87,8 +104,8 @@ defaultHttpConf opts accessLog errorLog =
setVerbose False $
emptyConfig
start :: DaemonOptions -> IO ()
start opts = do
start :: DaemonOptions -> MVar InstanceParams -> IO ()
start opts params = do
accessLog <- Runtime.daemonsExtraLogFile GanetiMetad AccessLog
errorLog <- Runtime.daemonsExtraLogFile GanetiMetad ErrorLog
httpServe (defaultHttpConf opts accessLog errorLog) routeMetadata
httpServe (defaultHttpConf opts accessLog errorLog) (routeMetadata params)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment