Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Metad.hs @ 560ef132

History | View | Annotate | Download (3.2 kB)

1 3af1359f Jose A. Lopes
{-| Metadata daemon
2 3af1359f Jose A. Lopes
3 3af1359f Jose A. Lopes
-}
4 3af1359f Jose A. Lopes
5 3af1359f Jose A. Lopes
{-
6 3af1359f Jose A. Lopes
7 3af1359f Jose A. Lopes
Copyright (C) 2014 Google Inc.
8 3af1359f Jose A. Lopes
9 3af1359f Jose A. Lopes
This program is free software; you can redistribute it and/or modify
10 3af1359f Jose A. Lopes
it under the terms of the GNU General Public License as published by
11 3af1359f Jose A. Lopes
the Free Software Foundation; either version 2 of the License, or
12 3af1359f Jose A. Lopes
(at your option) any later version.
13 3af1359f Jose A. Lopes
14 3af1359f Jose A. Lopes
This program is distributed in the hope that it will be useful, but
15 3af1359f Jose A. Lopes
WITHOUT ANY WARRANTY; without even the implied warranty of
16 3af1359f Jose A. Lopes
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 3af1359f Jose A. Lopes
General Public License for more details.
18 3af1359f Jose A. Lopes
19 3af1359f Jose A. Lopes
You should have received a copy of the GNU General Public License
20 3af1359f Jose A. Lopes
along with this program; if not, write to the Free Software
21 3af1359f Jose A. Lopes
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 3af1359f Jose A. Lopes
02110-1301, USA.
23 3af1359f Jose A. Lopes
24 3af1359f Jose A. Lopes
-}
25 3af1359f Jose A. Lopes
26 3af1359f Jose A. Lopes
module Ganeti.Metad (start) where
27 3af1359f Jose A. Lopes
28 3af1359f Jose A. Lopes
import Control.Applicative
29 3af1359f Jose A. Lopes
import Control.Monad.IO.Class (liftIO)
30 3af1359f Jose A. Lopes
import qualified Data.ByteString.Char8 as ByteString (pack, unpack)
31 3af1359f Jose A. Lopes
import Snap.Core
32 3af1359f Jose A. Lopes
import Snap.Http.Server
33 3af1359f Jose A. Lopes
34 3af1359f Jose A. Lopes
import Ganeti.Daemon
35 3af1359f Jose A. Lopes
import qualified Ganeti.Constants as Constants
36 3af1359f Jose A. Lopes
import qualified Ganeti.Logging as Logging
37 3af1359f Jose A. Lopes
import Ganeti.Runtime (GanetiDaemon(..), ExtraLogReason(..))
38 3af1359f Jose A. Lopes
import qualified Ganeti.Runtime as Runtime
39 3af1359f Jose A. Lopes
40 3af1359f Jose A. Lopes
type MetaM = Snap ()
41 3af1359f Jose A. Lopes
42 3af1359f Jose A. Lopes
error404 :: MetaM
43 3af1359f Jose A. Lopes
error404 = do
44 3af1359f Jose A. Lopes
  modifyResponse . setResponseStatus 404 $ ByteString.pack "Not found"
45 3af1359f Jose A. Lopes
  writeBS $ ByteString.pack "Resource not found"
46 3af1359f Jose A. Lopes
47 3af1359f Jose A. Lopes
handleMetadata :: Method -> String -> String -> String -> MetaM
48 3af1359f Jose A. Lopes
handleMetadata GET  "ganeti" "latest" "meta_data.json" =
49 3af1359f Jose A. Lopes
  liftIO $ Logging.logInfo "ganeti metadata"
50 3af1359f Jose A. Lopes
handleMetadata GET  "ganeti" "latest" "os/parameters.json" =
51 3af1359f Jose A. Lopes
  liftIO $ Logging.logInfo "ganeti OS parameters"
52 3af1359f Jose A. Lopes
handleMetadata GET  "ganeti" "latest" "read" =
53 3af1359f Jose A. Lopes
  liftIO $ Logging.logInfo "ganeti READ"
54 3af1359f Jose A. Lopes
handleMetadata POST "ganeti" "latest" "write" =
55 3af1359f Jose A. Lopes
  liftIO $ Logging.logInfo "ganeti WRITE"
56 3af1359f Jose A. Lopes
handleMetadata _ _ _ _ = error404
57 3af1359f Jose A. Lopes
58 3af1359f Jose A. Lopes
routeMetadata :: MetaM
59 3af1359f Jose A. Lopes
routeMetadata =
60 3af1359f Jose A. Lopes
  route [ (providerRoute1, dispatchMetadata)
61 3af1359f Jose A. Lopes
        , (providerRoute2, dispatchMetadata)
62 3af1359f Jose A. Lopes
        ] <|> dispatchMetadata
63 3af1359f Jose A. Lopes
  where provider = "provider"
64 3af1359f Jose A. Lopes
        version  = "version"
65 3af1359f Jose A. Lopes
66 3af1359f Jose A. Lopes
        providerRoute1 = ByteString.pack $ ':':provider ++ "/" ++ ':':version
67 3af1359f Jose A. Lopes
        providerRoute2 = ByteString.pack $ ':':version
68 3af1359f Jose A. Lopes
69 3af1359f Jose A. Lopes
        getParamString :: String -> Snap String
70 3af1359f Jose A. Lopes
        getParamString =
71 3af1359f Jose A. Lopes
          fmap (maybe "" ByteString.unpack) . getParam . ByteString.pack
72 3af1359f Jose A. Lopes
73 3af1359f Jose A. Lopes
        dispatchMetadata =
74 3af1359f Jose A. Lopes
          do m <- rqMethod <$> getRequest
75 3af1359f Jose A. Lopes
             p <- getParamString provider
76 3af1359f Jose A. Lopes
             v <- getParamString version
77 3af1359f Jose A. Lopes
             r <- ByteString.unpack . rqPathInfo <$> getRequest
78 3af1359f Jose A. Lopes
             handleMetadata m p v r
79 3af1359f Jose A. Lopes
80 3af1359f Jose A. Lopes
defaultHttpConf :: DaemonOptions -> FilePath -> FilePath -> Config Snap ()
81 3af1359f Jose A. Lopes
defaultHttpConf opts accessLog errorLog =
82 3af1359f Jose A. Lopes
  maybe id (setBind . ByteString.pack) (optBindAddress opts) .
83 3af1359f Jose A. Lopes
  setAccessLog (ConfigFileLog accessLog) .
84 3af1359f Jose A. Lopes
  setCompression False .
85 3af1359f Jose A. Lopes
  setErrorLog (ConfigFileLog errorLog) .
86 3af1359f Jose A. Lopes
  setPort (maybe Constants.defaultMetadPort fromIntegral (optPort opts)) .
87 3af1359f Jose A. Lopes
  setVerbose False $
88 3af1359f Jose A. Lopes
  emptyConfig
89 3af1359f Jose A. Lopes
90 3af1359f Jose A. Lopes
start :: DaemonOptions -> IO ()
91 3af1359f Jose A. Lopes
start opts = do
92 3af1359f Jose A. Lopes
  accessLog <- Runtime.daemonsExtraLogFile GanetiMetad AccessLog
93 3af1359f Jose A. Lopes
  errorLog <- Runtime.daemonsExtraLogFile GanetiMetad ErrorLog
94 3af1359f Jose A. Lopes
  httpServe (defaultHttpConf opts accessLog errorLog) routeMetadata