1 {-| Implementation of the RAPI client interface.
5 module Ganeti.HTools.Rapi
12 import Network.Curl.Types ()
13 import Network.Curl.Code
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 (Either String String)
29 (code, body) <- curlGetString url [CurlSSLVerifyPeer False,
33 _ -> Left $ printf "Curl error for '%s', error %s"
36 tryRapi :: String -> String -> IO (Either String String)
42 Right _ -> return body1)
44 getInstances :: String -> IO (Either String String)
47 url2 = printf "https://%s:5080/2/instances?bulk=1" master
48 url1 = printf "http://%s:5080/instances?bulk=1" master
50 body <- tryRapi url1 url2
51 let inst = body `combineEithers`
52 loadJSArray `combineEithers`
53 (parseEitherList parseInstance)
56 getNodes :: String -> IO (Either String String)
59 url2 = printf "https://%s:5080/2/nodes?bulk=1" master
60 url1 = printf "http://%s:5080/nodes?bulk=1" master
62 body <- tryRapi url1 url2
63 let inst = body `combineEithers`
64 loadJSArray `combineEithers`
65 (parseEitherList parseNode)
68 parseInstance :: JSObject JSValue -> Either String String
70 let name = getStringElement "name" a
71 disk = case getIntElement "disk_usage" a of
72 Left _ -> let log_sz = applyEither2 (+)
73 (getIntElement "sda_size" a)
74 (getIntElement "sdb_size" a)
75 in applyEither2 (+) log_sz
76 (Right $ drbdOverhead * 2)
78 bep = fromObj "beparams" a
79 pnode = getStringElement "pnode" a
80 snode = (eitherListHead $ getListElement "snodes" a)
81 `combineEithers` readEitherString
83 Left _ -> getIntElement "admin_ram" a
84 Right o -> getIntElement "memory" o
85 running = getStringElement "status" a
87 concatEitherElems name $
88 concatEitherElems (show `applyEither1` mem) $
89 concatEitherElems (show `applyEither1` disk) $
90 concatEitherElems running $
91 concatEitherElems pnode snode
93 boolToYN :: Bool -> Either String String
94 boolToYN True = Right "Y"
95 boolToYN _ = Right "N"
97 parseNode :: JSObject JSValue -> Either String String
99 let name = getStringElement "name" a
100 offline = getBoolElement "offline" a
101 drained = getBoolElement "drained" a
102 mtotal = getIntElement "mtotal" a
103 mnode = getIntElement "mnode" a
104 mfree = getIntElement "mfree" a
105 dtotal = getIntElement "dtotal" a
106 dfree = getIntElement "dfree" a
107 in concatEitherElems name $
109 Right True -> Right "0|0|0|0|0|Y"
111 concatEitherElems (show `applyEither1` mtotal) $
112 concatEitherElems (show `applyEither1` mnode) $
113 concatEitherElems (show `applyEither1` mfree) $
114 concatEitherElems (show `applyEither1` dtotal) $
115 concatEitherElems (show `applyEither1` dfree)
116 ((applyEither2 (||) offline drained) `combineEithers` boolToYN)