root / Ganeti / HTools / Rapi.hs @ 00b15752
History | View | Annotate | Download (3.8 kB)
1 | a7654563 | Iustin Pop | {-| Implementation of the RAPI client interface. |
---|---|---|---|
2 | a7654563 | Iustin Pop | |
3 | a7654563 | Iustin Pop | -} |
4 | a7654563 | Iustin Pop | |
5 | 669d7e3d | Iustin Pop | module Ganeti.HTools.Rapi |
6 | dd4c56ed | Iustin Pop | ( |
7 | dd4c56ed | Iustin Pop | getNodes |
8 | dd4c56ed | Iustin Pop | , getInstances |
9 | dd4c56ed | Iustin Pop | ) where |
10 | a7654563 | Iustin Pop | |
11 | a7654563 | Iustin Pop | import Network.Curl |
12 | b8b9a53c | Iustin Pop | import Network.Curl.Types () |
13 | a7654563 | Iustin Pop | import Network.Curl.Code |
14 | b8b9a53c | Iustin Pop | import Data.Either () |
15 | aab26f2d | Iustin Pop | import Data.Maybe |
16 | a7654563 | Iustin Pop | import Control.Monad |
17 | a7654563 | Iustin Pop | import Text.JSON |
18 | a7654563 | Iustin Pop | import Text.Printf (printf) |
19 | 9ba5c28f | Iustin Pop | import Ganeti.HTools.Utils |
20 | a7654563 | Iustin Pop | |
21 | a7654563 | Iustin Pop | |
22 | 190ce47c | Iustin Pop | -- Some constants |
23 | 190ce47c | Iustin Pop | |
24 | 190ce47c | Iustin Pop | -- | The fixed drbd overhead per disk (only used with 1.2's sdx_size) |
25 | 190ce47c | Iustin Pop | drbdOverhead = 128 |
26 | 190ce47c | Iustin Pop | |
27 | a7654563 | Iustin Pop | getUrl :: String -> IO (Either String String) |
28 | a7654563 | Iustin Pop | getUrl url = do |
29 | a7654563 | Iustin Pop | (code, body) <- curlGetString url [CurlSSLVerifyPeer False, |
30 | a7654563 | Iustin Pop | CurlSSLVerifyHost 0] |
31 | a7654563 | Iustin Pop | return (case code of |
32 | a7654563 | Iustin Pop | CurlOK -> Right body |
33 | aab26f2d | Iustin Pop | _ -> Left $ printf "Curl error for '%s', error %s" |
34 | aab26f2d | Iustin Pop | url (show code)) |
35 | aab26f2d | Iustin Pop | |
36 | aab26f2d | Iustin Pop | tryRapi :: String -> String -> IO (Either String String) |
37 | aab26f2d | Iustin Pop | tryRapi url1 url2 = |
38 | aab26f2d | Iustin Pop | do |
39 | aab26f2d | Iustin Pop | body1 <- getUrl url1 |
40 | 9b9a5931 | Iustin Pop | (case body1 of |
41 | 9b9a5931 | Iustin Pop | Left _ -> getUrl url2 |
42 | 9b9a5931 | Iustin Pop | Right _ -> return body1) |
43 | a7654563 | Iustin Pop | |
44 | a7654563 | Iustin Pop | getInstances :: String -> IO (Either String String) |
45 | a7654563 | Iustin Pop | getInstances master = |
46 | aab26f2d | Iustin Pop | let |
47 | aab26f2d | Iustin Pop | url2 = printf "https://%s:5080/2/instances?bulk=1" master |
48 | aab26f2d | Iustin Pop | url1 = printf "http://%s:5080/instances?bulk=1" master |
49 | a7654563 | Iustin Pop | in do |
50 | aab26f2d | Iustin Pop | body <- tryRapi url1 url2 |
51 | 9ba5c28f | Iustin Pop | let inst = body `combineEithers` |
52 | 9ba5c28f | Iustin Pop | loadJSArray `combineEithers` |
53 | 9ba5c28f | Iustin Pop | (parseEitherList parseInstance) |
54 | a7654563 | Iustin Pop | return inst |
55 | a7654563 | Iustin Pop | |
56 | a7654563 | Iustin Pop | getNodes :: String -> IO (Either String String) |
57 | a7654563 | Iustin Pop | getNodes master = |
58 | aab26f2d | Iustin Pop | let |
59 | aab26f2d | Iustin Pop | url2 = printf "https://%s:5080/2/nodes?bulk=1" master |
60 | aab26f2d | Iustin Pop | url1 = printf "http://%s:5080/nodes?bulk=1" master |
61 | a7654563 | Iustin Pop | in do |
62 | aab26f2d | Iustin Pop | body <- tryRapi url1 url2 |
63 | 9ba5c28f | Iustin Pop | let inst = body `combineEithers` |
64 | 9ba5c28f | Iustin Pop | loadJSArray `combineEithers` |
65 | 9ba5c28f | Iustin Pop | (parseEitherList parseNode) |
66 | a7654563 | Iustin Pop | return inst |
67 | a7654563 | Iustin Pop | |
68 | a7654563 | Iustin Pop | parseInstance :: JSObject JSValue -> Either String String |
69 | a7654563 | Iustin Pop | parseInstance a = |
70 | a7654563 | Iustin Pop | let name = getStringElement "name" a |
71 | a7654563 | Iustin Pop | disk = case getIntElement "disk_usage" a of |
72 | 9ba5c28f | Iustin Pop | Left _ -> let log_sz = applyEither2 (+) |
73 | 190ce47c | Iustin Pop | (getIntElement "sda_size" a) |
74 | 190ce47c | Iustin Pop | (getIntElement "sdb_size" a) |
75 | 9ba5c28f | Iustin Pop | in applyEither2 (+) log_sz |
76 | 9ba5c28f | Iustin Pop | (Right $ drbdOverhead * 2) |
77 | a7654563 | Iustin Pop | Right x -> Right x |
78 | aab26f2d | Iustin Pop | bep = fromObj "beparams" a |
79 | b8b9a53c | Iustin Pop | pnode = getStringElement "pnode" a |
80 | 9ba5c28f | Iustin Pop | snode = (eitherListHead $ getListElement "snodes" a) |
81 | 9ba5c28f | Iustin Pop | `combineEithers` readEitherString |
82 | aab26f2d | Iustin Pop | mem = case bep of |
83 | aab26f2d | Iustin Pop | Left _ -> getIntElement "admin_ram" a |
84 | d7cf83bf | Iustin Pop | Right o -> getIntElement "memory" o |
85 | f82f1f39 | Iustin Pop | running = getStringElement "status" a |
86 | a7654563 | Iustin Pop | in |
87 | 9ba5c28f | Iustin Pop | concatEitherElems name $ |
88 | 9ba5c28f | Iustin Pop | concatEitherElems (show `applyEither1` mem) $ |
89 | 9ba5c28f | Iustin Pop | concatEitherElems (show `applyEither1` disk) $ |
90 | 9ba5c28f | Iustin Pop | concatEitherElems running $ |
91 | 9ba5c28f | Iustin Pop | concatEitherElems pnode snode |
92 | a7654563 | Iustin Pop | |
93 | 00b15752 | Iustin Pop | boolToYN :: Bool -> Either String String |
94 | 00b15752 | Iustin Pop | boolToYN True = Right "Y" |
95 | 00b15752 | Iustin Pop | boolToYN _ = Right "N" |
96 | 00b15752 | Iustin Pop | |
97 | a7654563 | Iustin Pop | parseNode :: JSObject JSValue -> Either String String |
98 | a7654563 | Iustin Pop | parseNode a = |
99 | a7654563 | Iustin Pop | let name = getStringElement "name" a |
100 | 00b15752 | Iustin Pop | offline = getBoolElement "offline" a |
101 | 00b15752 | Iustin Pop | drained = getBoolElement "drained" a |
102 | a7654563 | Iustin Pop | mtotal = getIntElement "mtotal" a |
103 | 04be800a | Iustin Pop | mnode = getIntElement "mnode" a |
104 | a7654563 | Iustin Pop | mfree = getIntElement "mfree" a |
105 | a7654563 | Iustin Pop | dtotal = getIntElement "dtotal" a |
106 | a7654563 | Iustin Pop | dfree = getIntElement "dfree" a |
107 | 9ba5c28f | Iustin Pop | in concatEitherElems name $ |
108 | 00b15752 | Iustin Pop | (case offline of |
109 | 00b15752 | Iustin Pop | Right True -> Right "0|0|0|0|0|Y" |
110 | 00b15752 | Iustin Pop | _ -> |
111 | 00b15752 | Iustin Pop | concatEitherElems (show `applyEither1` mtotal) $ |
112 | 00b15752 | Iustin Pop | concatEitherElems (show `applyEither1` mnode) $ |
113 | 00b15752 | Iustin Pop | concatEitherElems (show `applyEither1` mfree) $ |
114 | 00b15752 | Iustin Pop | concatEitherElems (show `applyEither1` dtotal) $ |
115 | 00b15752 | Iustin Pop | concatEitherElems (show `applyEither1` dfree) |
116 | 00b15752 | Iustin Pop | ((applyEither2 (||) offline drained) `combineEithers` boolToYN) |
117 | 00b15752 | Iustin Pop | ) |