1 {-| Implementation of the RAPI client interface.
5 module Ganeti.HTools.Rapi
12 import Network.Curl.Types ()
13 import Network.Curl.Code
17 import Text.JSON (JSObject, JSValue)
18 import Text.Printf (printf)
19 import Ganeti.HTools.Utils
24 -- | The fixed drbd overhead per disk (only used with 1.2's sdx_size)
27 getUrl :: String -> IO (Result String)
29 (code, body) <- curlGetString url [CurlSSLVerifyPeer False,
33 _ -> Bad $ printf "Curl error for '%s', error %s"
36 getInstances :: String -> IO (Result String)
37 getInstances master = do
38 let url2 = printf "https://%s:5080/2/instances?bulk=1" master
40 return $ (body >>= \x -> do
42 ilist <- mapM parseInstance arr
43 return $ unlines ilist)
45 getNodes :: String -> IO (Result String)
47 let url2 = printf "https://%s:5080/2/nodes?bulk=1" master
49 return $ (body >>= \x -> do
51 nlist <- mapM parseNode arr
52 return $ unlines nlist)
54 parseInstance :: JSObject JSValue -> Result String
56 let name = getStringElement "name" a
57 disk = case getIntElement "disk_usage" a of
58 Bad _ -> let log_sz = liftM2 (+)
59 (getIntElement "sda_size" a)
60 (getIntElement "sdb_size" a)
61 in liftM2 (+) log_sz (Ok $ drbdOverhead * 2)
63 bep = fromObj "beparams" a
64 pnode = getStringElement "pnode" a
65 snode = (liftM head $ getListElement "snodes" a)
68 Bad _ -> getIntElement "admin_ram" a
69 Ok o -> getIntElement "memory" o
70 running = getStringElement "status" a
72 name |+ (show `liftM` mem) |+
73 (show `liftM` disk) |+
74 running |+ pnode |+ snode
76 boolToYN :: (Monad m) => Bool -> m String
77 boolToYN True = return "Y"
78 boolToYN _ = return "N"
80 parseNode :: JSObject JSValue -> Result String
82 let name = getStringElement "name" a
83 offline = getBoolElement "offline" a
84 drained = getBoolElement "drained" a
85 mtotal = getIntElement "mtotal" a
86 mnode = getIntElement "mnode" a
87 mfree = getIntElement "mfree" a
88 dtotal = getIntElement "dtotal" a
89 dfree = getIntElement "dfree" a
92 Ok True -> Ok "0|0|0|0|0|Y"
94 (show `liftM` mtotal) |+ (show `liftM` mnode) |+
95 (show `liftM` mfree) |+ (show `liftM` dtotal) |+
96 (show `liftM` dfree) |+
97 ((liftM2 (||) offline drained) >>= boolToYN)