Source file albatross_cli.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
open Vmm_core
open Lwt.Infix
let process =
Metrics.field ~doc:"name of the process" "vm" Metrics.String
let init_influx name data =
match data with
| None -> ()
| Some (ip, port) ->
Logs.info (fun m -> m "stats connecting to %a:%d" Ipaddr.pp ip port);
Metrics.enable_all ();
Metrics_lwt.init_periodic (fun () -> Lwt_unix.sleep 10.);
Metrics_lwt.periodically (Metrics_rusage.rusage_src ~tags:[]);
Metrics_lwt.periodically (Metrics_rusage.kinfo_mem_src ~tags:[]);
let get_cache, reporter = Metrics.cache_reporter () in
Metrics.set_reporter reporter;
let fd = ref None in
let rec report () =
let send () =
(match !fd with
| Some _ -> Lwt.return_unit
| None ->
let addr = Lwt_unix.ADDR_INET (Ipaddr_unix.to_inet_addr ip, port)
and fam = Lwt_unix.(match ip with Ipaddr.V4 _ -> PF_INET | Ipaddr.V6 _ -> PF_INET6)
in
Vmm_lwt.connect fam addr >|= function
| None -> Logs.err (fun m -> m "connection failure to stats")
| Some fd' -> fd := Some fd') >>= fun () ->
match !fd with
| None -> Lwt.return_unit
| Some socket ->
let tag = process name in
let datas = Metrics.SM.fold (fun src (tags, data) acc ->
let name = Metrics.Src.name src in
Metrics_influx.encode_line_protocol (tag :: tags) data name :: acc)
(get_cache ()) []
in
let datas = String.concat "" datas in
Vmm_lwt.write_raw socket (Bytes.unsafe_of_string datas) >|= function
| Ok () -> ()
| Error `Exception ->
Logs.warn (fun m -> m "error on stats write");
fd := None
and sleep () = Lwt_unix.sleep 10.
in
Lwt.join [ send () ; sleep () ] >>= report
in
Lwt.async report
type exit_status =
| Success
| Local_authentication_failed
| Remote_authentication_failed
| Communication_failed
| Connect_failed
| Remote_command_failed
| Cli_failed
| Internal_error
| Http_error
let output_result ((hdr, reply) as wire) =
let verbose = match Logs.level () with Some Logs.Debug -> true | _ -> false in
match reply with
| `Success s ->
Logs.app (fun m -> m "%a" (Vmm_commands.pp_wire ~verbose) wire);
let write_to_file name compressed data =
let filename =
let ts = Ptime.to_rfc3339 (Ptime_clock.now ()) in
Fpath.(v (Filename.get_temp_dir_name ()) / Name.to_string name + ts)
in
let write data =
match Bos.OS.File.write filename data with
| Ok () -> Logs.app (fun m -> m "dumped image to %a" Fpath.pp filename)
| Error (`Msg e) -> Logs.err (fun m -> m "failed to write image: %s" e)
in
if compressed then
match Vmm_compress.uncompress (Cstruct.to_string data) with
| Ok blob -> write blob
| Error `Msg msg ->
Logs.err (fun m -> m "failed to uncompress image: %s" msg)
else
write (Cstruct.to_string data)
in
begin match s with
| `Unikernel_image (compressed, image) ->
let name = hdr.Vmm_commands.name in
write_to_file name compressed image
| `Old_unikernels vms ->
List.iter (fun (name, cfg) ->
if Cstruct.length cfg.Unikernel.image > 0 then
write_to_file name cfg.Unikernel.compressed cfg.Unikernel.image)
vms
| `Block_device_image (compressed, image) ->
let name = hdr.Vmm_commands.name in
write_to_file name compressed image
| _ -> ()
end;
Ok ()
| `Data _ ->
Logs.app (fun m -> m "%a" (Vmm_commands.pp_wire ~verbose) wire);
Ok ()
| `Failure _ ->
Logs.warn (fun m -> m "%a" (Vmm_commands.pp_wire ~verbose) wire);
Error Remote_command_failed
| `Command _ ->
Logs.err (fun m -> m "received unexpected command %a"
(Vmm_commands.pp_wire ~verbose) wire);
Error Internal_error
let setup_log style_renderer level =
Fmt_tty.setup_std_outputs ?style_renderer ();
Logs.set_level level;
Logs.set_reporter (Logs_fmt.reporter ~dst:Format.std_formatter ())
let create_vm force image cpuid memory argv block_devices bridges compression restart_on_fail exit_codes =
let ( let* ) = Result.bind in
let img_file = Fpath.v image in
let* image = Bos.OS.File.read img_file in
let* () = Vmm_unix.manifest_devices_match ~bridges ~block_devices (Cstruct.of_string image) in
let image, compressed = match compression with
| 0 -> Cstruct.of_string image, false
| level ->
let img = Vmm_compress.compress ~level image in
Cstruct.of_string img, true
and argv = match argv with [] -> None | xs -> Some xs
and fail_behaviour =
let exits = match exit_codes with [] -> None | xs -> Some (IS.of_list xs) in
if restart_on_fail then `Restart exits else `Quit
in
let config = { Unikernel.typ = `Solo5 ; compressed ; image ; fail_behaviour ; cpuid ; memory ; block_devices ; bridges ; argv } in
if force then Ok (`Unikernel_force_create config) else Ok (`Unikernel_create config)
let create_block size compression data =
let ( let* ) = Result.bind in
match data with
| None -> Ok (`Block_add (size, false, None))
| Some image ->
let* size_in_mb = Vmm_unix.bytes_of_mb size in
if size_in_mb >= Cstruct.length image then
let compressed, img =
if compression > 0 then
true, Vmm_compress.compress_cs compression image
else
false, image
in
Ok (`Block_add (size, compressed, Some img))
else
Error (`Msg "data exceeds size")
let policy vms memory cpus block bridges =
let bridges = String_set.of_list bridges
and cpuids = IS.of_list cpus
in
Policy.{ vms ; cpuids ; memory ; block ; bridges }
open Cmdliner
let setup_log =
Term.(const setup_log
$ Fmt_cli.style_renderer ()
$ Logs_cli.level ())
let ip_port =
let pp ppf (ip, port) = Format.fprintf ppf "%a:%d" Ipaddr.pp ip port in
Arg.conv (Ipaddr.with_port_of_string ~default:8094, pp)
let influx =
let doc = "IP address and port (default: 8094) to report metrics to in influx line protocol" in
Arg.(value & opt (some ip_port) None & info [ "influx" ] ~doc ~docv:"INFLUXHOST[:PORT]")
let host_port =
let parse s =
match List.rev (String.split_on_char ':' s) with
| port :: host ->
begin try
Ok (String.concat ":" (List.rev host), int_of_string port)
with
Not_found -> Error (`Msg "failed to parse port")
end
| _ -> Error (`Msg "broken: no port specified")
in
Arg.conv (parse, fun ppf (h, p) -> Format.fprintf ppf "%s:%d" h p)
let label_c =
Arg.conv
((fun s ->
if Name.valid_label s then Ok s else Error (`Msg "invalid label")),
Fmt.string)
let opt_path =
let doc = "path to virtual machines." in
Arg.(value & opt label_c "." & info [ "p" ; "path"] ~doc)
let path =
let doc = "path to virtual machines." in
Arg.(required & pos 0 (some label_c) None & info [] ~doc ~docv:"PATH")
let bridge_tap_c =
let parse s = match String.split_on_char ':' s with
| [ bridge ; tap ] -> Ok (bridge, tap)
| _ -> Error (`Msg "broken, format is bridge:tap")
and pp ppf (bridge, tap) =
Format.fprintf ppf "%s:%s" bridge tap
in
Arg.conv (parse, pp)
let bridge_taps =
let doc = "Bridge and tap device names" in
Arg.(value & opt_all bridge_tap_c [] & info [ "bridge" ] ~doc)
let pid_req1 =
let doc = "Process id" in
Arg.(required & pos 1 (some int) None & info [] ~doc ~docv:"PID")
let vmm_dev_req0 =
let doc = "VMM device name" in
Arg.(required & pos 0 (some string) None & info [] ~doc ~docv:"VMMDEV")
let opt_vm_name =
let doc = "name of virtual machine." in
Arg.(value & opt label_c "." & info [ "n" ; "name"] ~doc)
let uri_c =
let parse s =
match String.split_on_char '/' s with
| ("http:" | "https:") :: "" :: _host :: [] -> Ok s
| ("http:" | "https:") :: "" :: _host :: "" :: [] ->
Ok (String.sub s 0 (String.length s - 1))
| _ -> Error (`Msg ("expected http[s]://hostname"))
in
Arg.conv (parse, Fmt.string)
let http_host =
let doc = "Base-URL of binary unikernel repository." in
Arg.(value & opt uri_c "https://builds.robur.coop" & info [ "http-host" ] ~doc)
let compress_level default =
let doc = "Compression level (0 - 9), a higher value results in smaller data, but uses more CPU " in
Arg.(value & opt int default & info [ "compression-level" ] ~doc)
let force =
let doc = "force VM creation." in
Arg.(value & flag & info [ "f" ; "force" ] ~doc)
let dryrun =
let doc = "dry run - do not make any changes." in
Arg.(value & flag & info [ "dryrun" ] ~doc)
let cpus =
let doc = "CPUids to allow" in
Arg.(value & opt_all int [] & info [ "cpu" ] ~doc)
let vms =
let doc = "Number of VMs to allow" in
Arg.(required & pos 1 (some int) None & info [] ~doc ~docv:"VMS")
let image =
let doc = "File of virtual machine image." in
Arg.(required & pos 1 (some file) None & info [] ~doc ~docv:"IMAGE")
let vm_name =
let doc = "Name virtual machine." in
Arg.(required & pos 0 (some label_c) None & info [] ~doc ~docv:"VM")
let block_name =
let doc = "Name of block device." in
Arg.(required & pos 0 (some label_c) None & info [] ~doc ~docv:"BLOCK")
let block_size =
let doc = "Block size in MB." in
Arg.(required & pos 1 (some int) None & info [] ~doc ~docv:"SIZE")
let data_c =
let parse s =
Result.map Cstruct.of_string (Bos.OS.File.read (Fpath.v s))
and pp ppf data =
Format.fprintf ppf "file with %d bytes" (Cstruct.length data)
in
Arg.conv (parse, pp)
let block_data =
let doc = "Block device content." in
Arg.(required & pos 1 (some data_c) None & info [] ~doc ~docv:"FILE")
let opt_block_data =
let doc = "Block device content." in
Arg.(value & opt (some data_c) None & info [ "data" ] ~doc ~docv:"FILE")
let opt_block_name =
let doc = "Name of block device." in
Arg.(value & opt label_c "." & info [ "name" ] ~doc)
let opt_block_size =
let doc = "Block storage to allow in MB" in
Arg.(value & opt (some int) None & info [ "size" ] ~doc)
let mem =
let doc = "Memory to allow in MB" in
Arg.(value & opt int 512 & info [ "mem" ] ~doc)
let bridge =
let doc = "Bridges to allow" in
Arg.(value & opt_all string [] & info [ "bridge" ] ~doc)
let cpu =
let doc = "CPUid to use" in
Arg.(value & opt int 0 & info [ "cpu" ] ~doc)
let vm_mem =
let doc = "Assigned memory in MB" in
Arg.(value & opt int 32 & info [ "mem" ] ~doc)
let args =
let doc = "Boot arguments" in
Arg.(value & opt_all string [] & info [ "arg" ] ~doc)
let colon_separated_c =
let parse s =
match String.split_on_char ':' s with
| [ a ; b ] -> Ok (a, Some b)
| [ _ ] -> Ok (s, None)
| _ -> Error (`Msg "format is 'name' or 'name:device-name'")
and pp ppf (a, b) =
Fmt.pf ppf "%s:%s" a (match b with None -> a | Some b -> b)
in
parse, pp
let block_c =
let parse_block, pp_block = colon_separated_c in
let parse s =
let ( let* ) = Result.bind in
match String.split_on_char '@' s with
| [ block ] ->
let* (name, device_name) = parse_block block in
Ok (name, device_name, None)
| [ block; sector_size ] ->
let* sector_size =
try
let sector_size = int_of_string sector_size in
if sector_size < 512 || sector_size land (sector_size - 1) <> 0 then
Error (`Msg "sector size must be a power of two greater than or equal 512")
else
Ok sector_size
with Failure _ -> Error (`Msg "sector size must be an integer")
in
let* (name, device_name) = parse_block block in
Ok (name, device_name, Some sector_size)
| _ -> Error (`Msg "format is 'name[@sector-size]' or 'name:device-name[@sector-size]'")
and pp ppf (a, b, c) =
Fmt.pf ppf "%a%a" pp_block (a, b) Fmt.(option ((any "@") ++ int)) c
in
Arg.conv (parse, pp)
let block =
let doc = "Block device name (block[@sector-size] or name:block-device-name[@sector-size])" in
Arg.(value & opt_all block_c [] & info [ "block" ] ~doc)
let net_with_mac =
let parse_net, pp_net = colon_separated_c in
let parse s =
let ( let* ) = Result.bind in
match String.split_on_char '@' s with
| [ net ] ->
let* (name, device_name) = parse_net net in
Ok (name, device_name, None)
| [ net; mac ] ->
let* mac = Macaddr.of_string mac in
let* (name, device_name) = parse_net net in
Ok (name, device_name, Some mac)
| _ -> Error (`Msg "format is [name:]bridge[@mac]")
and pp ppf (a, b, c) =
Fmt.pf ppf "%a%a" pp_net (a, b) Fmt.(option ((any "@") ++ Macaddr.pp)) c
in
Arg.conv (parse, pp)
let net =
let doc = "Network device names ([name:]bridge[@mac])" in
Arg.(value & opt_all net_with_mac [] & info [ "net" ] ~doc)
let restart_on_fail =
let doc = "Restart on fail" in
Arg.(value & flag & info [ "restart-on-fail" ] ~doc)
let exit_code =
let doc = "Exit code to restart on" in
Arg.(value & opt_all int [] & info [ "exit-code" ] ~doc)
let timestamp_c =
let parse s = match Ptime.of_rfc3339 s with
| Ok (t, _, _) -> Ok t
| Error _ ->
match Ptime.of_rfc3339 (s ^ "T00:00:00-00:00") with
| Ok (t, _, _) -> Ok t
| Error _ -> Error (`Msg "couldn't parse timestamp")
in
Arg.conv (parse, Ptime.pp_rfc3339 ())
let since =
let doc = "Receive data since a specified timestamp (RFC 3339 encoded)" in
Arg.(value & opt (some timestamp_c) None & info [ "since" ] ~doc)
let count =
let doc = "Receive N data records" in
Arg.(value & opt int 20 & info [ "count" ] ~doc)
let since_count since count = match since with
| None -> `Count count
| Some since -> `Since since
let version =
Fmt.str "version v1.5.4 protocol version %a"
Vmm_commands.pp_version Vmm_commands.current
type supported = FreeBSD | Linux | Darwin
let uname =
let cmd = Bos.Cmd.(v "uname" % "-s") in
match Bos.OS.Cmd.(run_out cmd |> out_string |> success) with
| Ok "FreeBSD" -> FreeBSD
| Ok "Linux" -> Linux
| Ok "Darwin" -> Darwin
| Ok s -> Fmt.invalid_arg "OS %s not supported" s
| Error (`Msg e) -> invalid_arg e
let default_tmpdir =
match uname with
| FreeBSD | Darwin -> "/var/run/albatross"
| Linux -> "/run/albatross"
let tmpdir =
let doc = "Albatross temporary directory" in
Arg.(value & opt dir default_tmpdir & info [ "tmpdir" ] ~doc)
let set_tmpdir path =
match Fpath.of_string path with
| Ok path -> Vmm_core.set_tmpdir path
| Error `Msg m -> invalid_arg m
let default_dbdir =
match uname with
| FreeBSD | Darwin -> "/var/db/albatross"
| Linux -> "/var/lib/albatross"
let dbdir =
let doc = "Albatross database directory" in
Arg.(value & opt dir default_dbdir & info [ "dbdir" ] ~doc)
let set_dbdir path =
match Fpath.of_string path with
| Ok path -> Vmm_unix.set_dbdir path
| Error `Msg m -> invalid_arg m
let systemd_socket_activation =
match uname with
| FreeBSD | Darwin -> Term.const false
| Linux ->
let doc = "Pass this flag when systemd socket activation is being used" in
Arg.(value & flag & info [ "systemd-socket-activation" ] ~doc)
let port_or_socket ~default_port =
let open Term in
let port =
let doc = "TCP listen port." and absent = string_of_int default_port in
Arg.(value & opt (some int) None & info [ "port" ] ~doc ~absent)
in
term_result
(const (fun port socket ->
match (port, socket) with
| Some _, true ->
Error
(`Msg
"Options --port and --systemd-socket-activation are not \
compatible")
| None, true -> Ok `Systemd_socket
| Some p, false -> Ok (`Port p)
| None, false -> Ok (`Port default_port))
$ port $ systemd_socket_activation)
let pub_key_type =
let doc = "Asymmetric key type to use" in
Arg.(value & opt (Arg.enum X509.Key_type.strings) `ED25519 & info [ "key-type" ] ~doc)
let key_bits =
let doc = "Public key bits to use (only relevant for RSA)" in
Arg.(value & opt int 4096 & info [ "bits" ] ~doc)
let exit_status = function
| Ok () -> Ok Success
| Error e -> Ok e
let remote_command_failed = 117
let http_failed = 118
let local_authentication_failed = 119
let remote_authentication_failed = 120
let communication_failed = 121
let connect_failed = 122
let exit_status_to_int = function
| Success -> Cmd.Exit.ok
| Local_authentication_failed -> local_authentication_failed
| Remote_authentication_failed -> remote_authentication_failed
| Communication_failed -> communication_failed
| Connect_failed -> connect_failed
| Remote_command_failed -> remote_command_failed
| Cli_failed -> Cmd.Exit.cli_error
| Internal_error -> Cmd.Exit.internal_error
| Http_error -> http_failed
let exit_status_of_result = function
| Ok (`Help | `Version) -> Cmd.Exit.ok
| Ok `Ok a -> exit_status_to_int a
| Error `Term -> Cmd.Exit.cli_error
| Error `Parse -> Cmd.Exit.cli_error
| Error `Exn -> Cmd.Exit.internal_error
let exits =
Cmd.Exit.info ~doc:"on communication (read or write) failure"
communication_failed ::
Cmd.Exit.info ~doc:"on connection failure" connect_failed ::
Cmd.Exit.info ~doc:"on remote command execution failure"
remote_command_failed ::
Cmd.Exit.info ~doc:"on HTTP interaction failure" http_failed ::
Cmd.Exit.defaults
let auth_exits =
[ Cmd.Exit.info ~doc:"on local authentication failure \
(certificate not accepted by remote)"
local_authentication_failed ;
Cmd.Exit.info ~doc:"on remote authentication failure \
(couldn't validate trust anchor)"
remote_authentication_failed ]