-
Notifications
You must be signed in to change notification settings - Fork 3
/
albatross_json.ml
300 lines (286 loc) · 9.79 KB
/
albatross_json.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
open Utils.Json
let unikernel_info (unikernel_name, info) =
let typ = function `Solo5 -> `String "solo5"
and fail_behaviour = function
| `Quit -> `String "quit"
| `Restart ex ->
let els =
Option.value ~default:[] (Option.map Vmm_core.IS.elements ex)
in
`Assoc
[
("restart", `Null);
("exit_code", `List (List.map (fun e -> `Int e) els));
("all_exit_codes", `Bool (ex = None));
]
and cpuid c = `Int c
and memory m = `Int m
and block_devices bs =
let block (name, dev, sec) =
let dev = Option.value ~default:"none specified (name is used)" dev in
let s =
Option.value ~default:512
(* TODO we should find a path to query solo5 for the default sector size*)
sec
in
`Assoc
[
("name", `String name);
("host_device", `String dev);
("sector_size", `Int s);
]
in
`List (List.map block bs)
and bridges bs =
let bridge (name, dev, mac) =
let dev = Option.value ~default:name dev in
let mac =
Option.value ~default:(Vmm_core.Name.mac unikernel_name dev) mac
in
`Assoc
[
("name", `String name);
("host_device", `String dev);
("mac", `String (Macaddr.to_string mac));
]
in
`List (List.map bridge bs)
and argv args =
`List (List.map (fun a -> `String a) (Option.value ~default:[] args))
and digest d = `String (Ohex.encode d) in
`Assoc
[
("name", `String (Vmm_core.Name.to_string unikernel_name));
("typ", typ info.Vmm_core.Unikernel.typ);
("fail_behaviour", fail_behaviour info.fail_behaviour);
("cpuid", cpuid info.cpuid);
("memory", memory info.memory);
("block_devices", block_devices info.block_devices);
("network_interfaces", bridges info.bridges);
("arguments", argv info.argv);
("digest", digest info.digest);
]
let unikernel_infos is = `List (List.map unikernel_info is)
let block_info (name, size, used) =
`Assoc
[
("name", `String (Vmm_core.Name.to_string name));
("size", `Int size);
("used", `Bool used);
]
let block_infos bs = `List (List.map block_info bs)
let policy_info (name, policy) =
`Assoc
[
("name", `String (Vmm_core.Name.to_string name));
("allowed_unikernels", `Int policy.Vmm_core.Policy.unikernels);
( "allowed_cpuids",
`List
(List.map (fun id -> `Int id) (Vmm_core.IS.elements policy.cpuids)) );
("allowed_memory", `Int policy.memory);
("allowed_block_size", `Int (Option.value ~default:0 policy.block));
( "allowed_bridges",
`List
(List.map
(fun b -> `String b)
(Vmm_core.String_set.elements policy.bridges)) );
]
let policy_infos ps = `List (List.map policy_info ps)
let success = function
| `Empty -> `Null
| `String m -> `String m
| `Policies ps -> policy_infos ps
| `Old_unikernel_info is -> unikernel_infos is
| `Unikernel_info is -> unikernel_infos is
| `Block_devices bs -> block_infos bs
| `Old_unikernels _ -> `String "old unikernels not supported"
| `Unikernel_image _ -> `String "unikernel image not supported"
| `Block_device_image (_, bd) -> `String bd
let console_data_to_json (ts, data) =
`Assoc
[ ("timestamp", `String (Ptime.to_rfc3339 ts)); ("line", `String data) ]
let res = function
| `Command _ -> Error (`String "command not supported")
| `Success s -> Ok (success s)
| `Failure f -> Error (`String ("failure: " ^ f))
| `Data (`Console_data (ts, data)) -> Ok (console_data_to_json (ts, data))
| `Data (`Utc_console_data (ts, data)) -> Ok (console_data_to_json (ts, data))
| `Data (`Stats_data _) -> Error (`String "stats not supported")
let fail_behaviour_of_json js =
let ( let* ) = Result.bind in
match js with
| `String "quit" -> Ok `Quit
| `Assoc rs -> (
match (get "restart" rs, get "exit_code" rs, get "all_exit_codes" rs) with
| None, _, _ -> Error (`Msg "expected a restart")
| Some _, Some (`List codes), _ ->
let* codes =
try
Ok
(List.map
(function `Int n -> n | _ -> failwith "not an integer")
codes)
with Failure s ->
Error
(`Msg ("expected integer values as exit codes, error: " ^ s))
in
Ok (`Restart (Some (Vmm_core.IS.of_list codes)))
| Some _, Some _, _ ->
Error (`Msg "expected a list of integers as exit_code payload")
| Some _, _, _ -> Ok (`Restart None))
| _ -> Error (`Msg "fail behaviour must be quit or restart")
let bridge_of_json (js : Yojson.Basic.t) =
(* we support [ "foo" ] as well as [ { name: "foo" ; host_device: "myfoo" ; mac: "aa:bb:cc:dd:ee:ff" *)
let ( let* ) = Result.bind in
match js with
| `String bridge -> Ok (bridge, None, None)
| `Assoc xs -> (
match (get "name" xs, get "host_device" xs, get "mac" xs) with
| None, _, _ -> Error (`Msg "name must be present in the json")
| Some (`String name), None, None -> Ok (name, None, None)
| Some (`String name), Some (`String host), None ->
Ok (name, Some host, None)
| Some (`String name), None, Some (`String mac) ->
let* mac = Macaddr.of_string mac in
Ok (name, None, Some mac)
| Some (`String name), Some (`String host), Some (`String mac) ->
let* mac = Macaddr.of_string mac in
Ok (name, Some host, Some mac)
| _, _, _ -> Error (`Msg "couldn't decode json"))
| _ -> Error (`Msg "bad json, either string or assoc")
let block_device_of_json js =
(* we support [ "foo" ] as well as [ { name: "foo" ; host_device: "myfoo" ; sector_size: 20 *)
match js with
| `String name -> Ok (name, None, None)
| `Assoc xs -> (
match (get "name" xs, get "host_device" xs, get "sector_size" xs) with
| None, _, _ -> Error (`Msg "name must be present in the json")
| Some (`String name), None, None -> Ok (name, None, None)
| Some (`String name), Some (`String host), None ->
Ok (name, Some host, None)
| Some (`String name), None, Some (`Int sector_size) ->
Ok (name, None, Some sector_size)
| Some (`String name), Some (`String host), Some (`Int sector_size) ->
Ok (name, Some host, Some sector_size)
| _, _, _ -> Error (`Msg "couldn't decode json"))
| _ -> Error (`Msg "bad json, either string or assoc")
let policy_of_json json_dict =
match
Utils.Json.
( get "unikernels" json_dict,
get "memory" json_dict,
get "block" json_dict,
get "cpuids" json_dict,
get "bridges" json_dict )
with
| ( Some (`Int unikernels),
Some (`Int memory),
Some (`Int block),
Some (`String cpuids),
Some (`String bridges) ) ->
let policy =
{
Vmm_core.Policy.unikernels;
memory;
block = (if block = 0 then None else Some block);
cpuids =
(let parsed_cpuids =
List.filter_map
(fun s ->
match int_of_string_opt s with
| Some i -> Some i
| None ->
Logs.warn (fun m -> m "Ignoring invalid CPU id: %s" s);
None)
(String.split_on_char ',' cpuids)
in
Vmm_core.IS.of_list parsed_cpuids);
bridges =
Vmm_core.String_set.of_list (String.split_on_char ',' bridges);
}
in
let ( let* ) = Result.bind in
let* () = Vmm_core.Policy.usable policy in
Ok policy
| _ ->
Error
(`Msg
(Fmt.str "policy: unexpected types, got %s"
(Yojson.Basic.to_string (`Assoc json_dict))))
let config_of_json str =
let ( let* ) = Result.bind in
let* json =
try Ok (Yojson.Basic.from_string str)
with Yojson.Json_error s -> Error (`Msg s)
in
let* dict =
match json with `Assoc r -> Ok r | _ -> Error (`Msg "not a json assoc")
in
let* fail_behaviour =
Option.fold ~none:(Ok `Quit) ~some:fail_behaviour_of_json
(get "fail_behaviour" dict)
in
let* cpuid =
Option.fold ~none:(Ok 0)
~some:(function
| `Int n -> Ok n | _ -> Error (`Msg "cpuid must be an integer"))
(get "cpuid" dict)
in
let* memory =
Option.fold ~none:(Ok 32)
~some:(function
| `Int n -> Ok n | _ -> Error (`Msg "memory must be an integer"))
(get "memory" dict)
in
let* bridges =
match get "network_interfaces" dict with
| None -> Ok []
| Some (`List bs) ->
List.fold_left
(fun r x ->
let* r = r in
let* b = bridge_of_json x in
Ok (b :: r))
(Ok []) bs
| Some _ -> Error (`Msg "expected a list of network devices")
in
let* block_devices =
match get "block_devices" dict with
| None -> Ok []
| Some (`List bs) ->
List.fold_left
(fun r x ->
let* r = r in
let* b = block_device_of_json x in
Ok (b :: r))
(Ok []) bs
| Some _ -> Error (`Msg "expected a list of block devices")
in
let* argv =
try
Option.fold ~none:(Ok None)
~some:(function
| `List bs ->
Ok
(Some
(List.map
(function
| `String n -> n
| _ -> failwith "argument is not a string")
bs))
| _ -> Error (`Msg "arguments must be a list of strings"))
(get "arguments" dict)
with Failure s -> Error (`Msg ("expected strings as argv, error: " ^ s))
in
Ok
{
Vmm_core.Unikernel.typ = `Solo5;
compressed = false;
image = "";
fail_behaviour;
cpuid;
memory;
block_devices;
bridges;
argv;
}