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

Serve OS install package through metadata daemon



* Improve JSON conversion in 'getOsParams'
* Generalize 'serveOsPackage' to handle both OS packages and OS
  install packages
* Add 'os-install-package' endpoint
Signed-off-by: default avatarJose A. Lopes <jabolopes@google.com>
Reviewed-by: default avatarHrvoje Ribicic <riba@google.com>
parent c055e8dc
...@@ -47,8 +47,7 @@ getOsParams :: String -> String -> JSObject JSValue -> Result (JSObject JSValue) ...@@ -47,8 +47,7 @@ getOsParams :: String -> String -> JSObject JSValue -> Result (JSObject JSValue)
getOsParams key msg jsonObj = getOsParams key msg jsonObj =
case lookup key (fromJSObject jsonObj) of case lookup key (fromJSObject jsonObj) of
Nothing -> Error $ "Could not find " ++ msg ++ " OS parameters" Nothing -> Error $ "Could not find " ++ msg ++ " OS parameters"
Just (JSObject x) -> Ok x Just x -> readJSON x
_ -> Error "OS params is not a JSON object"
getPublicOsParams :: JSObject JSValue -> Result (JSObject JSValue) getPublicOsParams :: JSObject JSValue -> Result (JSObject JSValue)
getPublicOsParams = getOsParams "osparams" "public" getPublicOsParams = getOsParams "osparams" "public"
......
...@@ -81,26 +81,18 @@ serveOsParams inst params = ...@@ -81,26 +81,18 @@ serveOsParams inst params =
ByteString.pack . ByteString.pack .
JSON.encode $ osParams JSON.encode $ osParams
serveOsPackage :: String -> Map String JSValue -> MetaM serveOsPackage :: String -> Map String JSValue -> String -> MetaM
serveOsPackage inst instParams = serveOsPackage inst params key =
case Map.lookup inst instParams of do instParams <- lookupInstanceParams inst params
Just (JSON.JSObject osParams) -> do maybeResult (JSON.readJSON instParams >>=
let res = getOsPackage osParams Config.getPublicOsParams >>=
case res of getOsPackage) $ \package ->
Error err -> serveFile package `CatchIO.catch` \err ->
do liftIO $ Logging.logWarning err throwError $ "Could not serve OS package: " ++ show (err :: IOError)
error404
Ok package ->
serveFile package
`CatchIO.catch`
\err -> do liftIO . Logging.logWarning $
"Could not serve OS package: " ++ show (err :: IOError)
error404
_ -> error404
where getOsPackage osParams = where getOsPackage osParams =
case lookup "os-package" (JSON.fromJSObject osParams) of case lookup key (JSON.fromJSObject osParams) of
Nothing -> Error $ "Could not find OS package for " ++ show inst Nothing -> Error $ "Could not find OS package for " ++ show inst
Just x -> fst <$> (JSON.readJSON x :: Result (String, String)) Just x -> JSON.readJSON x
serveOsScript :: String -> Map String JSValue -> String -> MetaM serveOsScript :: String -> Map String JSValue -> String -> MetaM
serveOsScript inst params script = serveOsScript inst params script =
...@@ -128,12 +120,23 @@ handleMetadata ...@@ -128,12 +120,23 @@ handleMetadata
:: MVar InstanceParams -> Method -> String -> String -> String -> MetaM :: MVar InstanceParams -> Method -> String -> String -> String -> MetaM
handleMetadata _ GET "ganeti" "latest" "meta_data.json" = handleMetadata _ GET "ganeti" "latest" "meta_data.json" =
liftIO $ Logging.logInfo "ganeti metadata" liftIO $ Logging.logInfo "ganeti metadata"
handleMetadata params GET "ganeti" "latest" "os/os-install-package" =
do remoteAddr <- ByteString.unpack . rqRemoteAddr <$> getRequest
instanceParams <- liftIO $ do
Logging.logInfo $ "OS install package for " ++ show remoteAddr
readMVar params
serveOsPackage remoteAddr instanceParams "os-install-package"
`catchError`
\err -> do
liftIO .
Logging.logWarning $ "Could not serve OS install package: " ++ err
error404
handleMetadata params GET "ganeti" "latest" "os/package" = handleMetadata params GET "ganeti" "latest" "os/package" =
do remoteAddr <- ByteString.unpack . rqRemoteAddr <$> getRequest do remoteAddr <- ByteString.unpack . rqRemoteAddr <$> getRequest
instanceParams <- liftIO $ do instanceParams <- liftIO $ do
Logging.logInfo $ "OS package for " ++ show remoteAddr Logging.logInfo $ "OS package for " ++ show remoteAddr
readMVar params readMVar params
serveOsPackage remoteAddr instanceParams serveOsPackage remoteAddr instanceParams "os-package"
handleMetadata params GET "ganeti" "latest" "os/parameters.json" = handleMetadata params GET "ganeti" "latest" "os/parameters.json" =
do remoteAddr <- ByteString.unpack . rqRemoteAddr <$> getRequest do remoteAddr <- ByteString.unpack . rqRemoteAddr <$> getRequest
instanceParams <- liftIO $ do instanceParams <- liftIO $ do
......
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