2017-05-26 14:30:34 +00:00
|
|
|
(* (c) 2017 Hannes Mehnert, all rights reserved *)
|
|
|
|
|
2019-03-27 23:11:43 +00:00
|
|
|
open Albatross_cli
|
2018-10-25 23:11:41 +00:00
|
|
|
|
2018-11-13 00:02:05 +00:00
|
|
|
open Vmm_core
|
|
|
|
|
2017-05-26 14:30:34 +00:00
|
|
|
open Lwt.Infix
|
|
|
|
|
2019-11-11 20:49:51 +00:00
|
|
|
let state = ref (Vmm_vmmd.init ())
|
2018-09-09 18:52:04 +00:00
|
|
|
|
2019-10-10 23:10:33 +00:00
|
|
|
let stub_data_out _ = Lwt.return_unit
|
|
|
|
|
|
|
|
let create_lock = Lwt_mutex.create ()
|
2019-10-12 00:30:21 +00:00
|
|
|
(* the global lock held during execution of create -- and also while
|
|
|
|
Vmm_vmmd.handle is getting called, and while communicating via log /
|
|
|
|
console / stat socket communication. *)
|
2019-10-10 23:10:33 +00:00
|
|
|
|
2019-11-11 20:49:51 +00:00
|
|
|
let rec create stat_out log_out cons_out data_out name config =
|
|
|
|
(match Vmm_vmmd.handle_create !state name config with
|
2019-10-10 23:10:33 +00:00
|
|
|
| Error `Msg msg ->
|
|
|
|
Logs.err (fun m -> m "failed to create %a: %s" Name.pp name msg) ;
|
2019-11-11 20:49:51 +00:00
|
|
|
Lwt.return (None, `Failure msg)
|
2019-10-11 21:04:59 +00:00
|
|
|
| Ok (state', (cons, succ_cont, fail_cont)) ->
|
2019-10-10 23:10:33 +00:00
|
|
|
state := state';
|
|
|
|
cons_out "create" cons >>= function
|
|
|
|
| Error () -> Lwt.return (None, fail_cont ())
|
|
|
|
| Ok () -> match succ_cont !state with
|
|
|
|
| Error (`Msg msg) ->
|
|
|
|
Logs.err (fun m -> m "create (exec) failed %s" msg) ;
|
|
|
|
Lwt.return (None, fail_cont ())
|
|
|
|
| Ok (state', stat, log, data, name, vm) ->
|
|
|
|
state := state';
|
2019-10-11 22:01:36 +00:00
|
|
|
(if Unikernel.restart_handler config then
|
2019-10-10 23:10:33 +00:00
|
|
|
match Vmm_vmmd.register_restart !state name Lwt.task with
|
|
|
|
| None -> ()
|
|
|
|
| Some (state', task) ->
|
|
|
|
state := state';
|
|
|
|
Lwt.async (fun () ->
|
2019-10-11 22:01:36 +00:00
|
|
|
task >>= fun r ->
|
|
|
|
if should_restart config name r then
|
|
|
|
Lwt_mutex.with_lock create_lock (fun () ->
|
|
|
|
create stat_out log_out cons_out stub_data_out
|
2019-11-11 20:49:51 +00:00
|
|
|
name vm.Unikernel.config)
|
2019-10-11 22:01:36 +00:00
|
|
|
else
|
|
|
|
Lwt.return_unit));
|
2019-10-10 23:10:33 +00:00
|
|
|
stat_out "setting up stat" stat >>= fun () ->
|
|
|
|
log_out "setting up log" log >|= fun () ->
|
|
|
|
(Some vm, data)) >>= fun (started, data) ->
|
|
|
|
(match started with
|
|
|
|
| None -> ()
|
|
|
|
| Some vm ->
|
|
|
|
Lwt.async (fun () ->
|
|
|
|
Vmm_lwt.wait_and_clear vm.Unikernel.pid >>= fun r ->
|
|
|
|
Lwt_mutex.with_lock create_lock (fun () ->
|
|
|
|
let state', stat', log' = Vmm_vmmd.handle_shutdown !state name vm r in
|
|
|
|
state := state';
|
|
|
|
stat_out "handle shutdown stat" stat' >>= fun () ->
|
|
|
|
log_out "handle shutdown log" log' >|= fun () ->
|
|
|
|
let state', waiter_opt = Vmm_vmmd.waiter !state name in
|
|
|
|
state := state';
|
|
|
|
waiter_opt) >|= function
|
|
|
|
| None -> ()
|
|
|
|
| Some wakeme -> Lwt.wakeup wakeme r));
|
|
|
|
data_out data
|
2019-01-20 20:41:49 +00:00
|
|
|
|
2019-01-27 15:07:53 +00:00
|
|
|
let handle log_out cons_out stat_out fd addr =
|
2018-09-09 18:52:04 +00:00
|
|
|
Logs.debug (fun m -> m "connection from %a" Vmm_lwt.pp_sockaddr addr) ;
|
2018-10-31 21:40:09 +00:00
|
|
|
let rec loop () =
|
|
|
|
Logs.debug (fun m -> m "now reading") ;
|
|
|
|
Vmm_lwt.read_wire fd >>= function
|
2018-09-09 18:52:04 +00:00
|
|
|
| Error _ ->
|
|
|
|
Logs.err (fun m -> m "error while reading") ;
|
|
|
|
Lwt.return_unit
|
2019-11-11 20:49:51 +00:00
|
|
|
| Ok (hdr, wire) ->
|
|
|
|
let out wire' =
|
|
|
|
(* TODO should we terminate the connection on write failure? *)
|
|
|
|
Vmm_lwt.write_wire fd (hdr, wire') >|= fun _ -> ()
|
|
|
|
in
|
|
|
|
Logs.debug (fun m -> m "read %a" Vmm_commands.pp_wire (hdr, wire));
|
2019-10-10 23:10:33 +00:00
|
|
|
Lwt_mutex.lock create_lock >>= fun () ->
|
2019-11-11 20:49:51 +00:00
|
|
|
match Vmm_vmmd.handle_command !state (hdr, wire) with
|
|
|
|
| Error wire' -> Lwt_mutex.unlock create_lock; out wire'
|
2019-01-27 15:07:53 +00:00
|
|
|
| Ok (state', next) ->
|
|
|
|
state := state' ;
|
|
|
|
match next with
|
2019-10-10 23:10:33 +00:00
|
|
|
| `Loop wire -> Lwt_mutex.unlock create_lock; out wire >>= loop
|
|
|
|
| `End wire -> Lwt_mutex.unlock create_lock; out wire
|
2019-11-11 20:49:51 +00:00
|
|
|
| `Create (id, vm) ->
|
|
|
|
create stat_out log_out cons_out out id vm >|= fun () ->
|
2019-10-10 23:10:33 +00:00
|
|
|
Lwt_mutex.unlock create_lock
|
2019-01-27 15:07:53 +00:00
|
|
|
| `Wait (who, data) ->
|
2019-10-10 23:10:33 +00:00
|
|
|
let state', task = Vmm_vmmd.register !state who Lwt.task in
|
|
|
|
state := state';
|
|
|
|
Lwt_mutex.unlock create_lock;
|
|
|
|
task >>= fun r ->
|
|
|
|
out (data r)
|
2019-11-11 20:49:51 +00:00
|
|
|
| `Wait_and_create (who, (id, vm)) ->
|
2019-10-10 23:10:33 +00:00
|
|
|
let state', task = Vmm_vmmd.register !state who Lwt.task in
|
|
|
|
state := state';
|
|
|
|
Lwt_mutex.unlock create_lock;
|
|
|
|
task >>= fun r ->
|
|
|
|
Logs.info (fun m -> m "wait returned %a" pp_process_exit r);
|
|
|
|
Lwt_mutex.with_lock create_lock (fun () ->
|
2019-11-11 20:49:51 +00:00
|
|
|
create stat_out log_out cons_out out id vm)
|
2018-10-31 21:40:09 +00:00
|
|
|
in
|
|
|
|
loop () >>= fun () ->
|
2018-09-09 18:52:04 +00:00
|
|
|
Vmm_lwt.safe_close fd
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2019-11-11 20:49:51 +00:00
|
|
|
let write_reply name fd txt (hdr, cmd) =
|
|
|
|
Vmm_lwt.write_wire fd (hdr, cmd) >>= function
|
|
|
|
| Error `Exception ->
|
|
|
|
invalid_arg ("exception during " ^ txt ^ " while writing to " ^ name)
|
2019-10-10 23:10:33 +00:00
|
|
|
| Ok () ->
|
|
|
|
Vmm_lwt.read_wire fd >|= function
|
2019-11-11 20:49:51 +00:00
|
|
|
| Ok (hdr', reply) ->
|
|
|
|
if not Vmm_commands.(Int64.equal hdr.sequence hdr'.sequence) then begin
|
2019-10-10 23:10:33 +00:00
|
|
|
Logs.err (fun m -> m "%s: wrong id %Lu (expected %Lu) in reply from %s"
|
2019-11-11 20:49:51 +00:00
|
|
|
txt hdr'.Vmm_commands.sequence hdr.Vmm_commands.sequence name) ;
|
2019-10-10 23:10:33 +00:00
|
|
|
invalid_arg "wrong sequence number received"
|
|
|
|
end else begin
|
|
|
|
Logs.debug (fun m -> m "%s: received valid reply from %s %a (request %a)"
|
2019-11-11 20:49:51 +00:00
|
|
|
txt name Vmm_commands.pp_wire (hdr', reply) Vmm_commands.pp_wire (hdr, cmd)) ;
|
2019-10-10 23:10:33 +00:00
|
|
|
match reply with
|
|
|
|
| `Success _ -> Ok ()
|
|
|
|
| `Failure msg ->
|
|
|
|
Logs.err (fun m -> m "%s: received failure %s from %s" txt msg name) ;
|
|
|
|
Error ()
|
|
|
|
| _ ->
|
|
|
|
Logs.err (fun m -> m "%s: unexpected data from %s" txt name) ;
|
|
|
|
invalid_arg "unexpected data"
|
|
|
|
end
|
|
|
|
| Error _ ->
|
|
|
|
Logs.err (fun m -> m "error in read from %s" name) ;
|
|
|
|
invalid_arg "communication failure"
|
2019-01-27 15:07:53 +00:00
|
|
|
|
2019-10-10 20:26:36 +00:00
|
|
|
let m = conn_metrics "unix"
|
|
|
|
|
2020-05-19 18:51:32 +00:00
|
|
|
let jump _ influx tmpdir dbdir retries enable_stats =
|
2019-01-18 00:14:11 +00:00
|
|
|
Sys.(set_signal sigpipe Signal_ignore);
|
2019-10-29 22:37:42 +00:00
|
|
|
Albatross_cli.set_tmpdir tmpdir;
|
|
|
|
Albatross_cli.set_dbdir dbdir;
|
2019-10-27 19:46:07 +00:00
|
|
|
Rresult.R.error_msg_to_invalid_arg
|
|
|
|
(Vmm_unix.check_commands ());
|
2019-01-20 21:17:59 +00:00
|
|
|
match Vmm_vmmd.restore_unikernels () with
|
|
|
|
| Error (`Msg msg) -> Logs.err (fun m -> m "bailing out: %s" msg)
|
|
|
|
| Ok old_unikernels ->
|
|
|
|
Lwt_main.run
|
2020-05-19 18:51:32 +00:00
|
|
|
(let rec unix_connect ~retries s =
|
|
|
|
(Vmm_lwt.connect Lwt_unix.PF_UNIX (Lwt_unix.ADDR_UNIX (socket_path s)) >>= function
|
|
|
|
| Some x -> Lwt.return (Some x)
|
|
|
|
| None when (retries <> 0) ->
|
|
|
|
(Logs.err (fun m -> m "unable to connect to %s, retrying in 5 seconds" (socket_path s));
|
|
|
|
Lwt_unix.sleep 5.0 >>= fun () ->
|
|
|
|
unix_connect ~retries:(retries - 1) s)
|
|
|
|
| None -> Lwt.return_none)
|
2019-10-10 23:10:33 +00:00
|
|
|
in
|
|
|
|
init_influx "albatross" influx;
|
2020-05-19 18:51:32 +00:00
|
|
|
|
|
|
|
(unix_connect ~retries `Log >|= function
|
2019-01-20 21:17:59 +00:00
|
|
|
| None -> invalid_arg "cannot connect to log socket"
|
2019-01-27 15:07:53 +00:00
|
|
|
| Some l -> l) >>= fun l ->
|
2020-05-19 18:51:32 +00:00
|
|
|
|
|
|
|
(unix_connect ~retries `Console >|= function
|
|
|
|
| None -> invalid_arg "cannot connect to console socket"
|
|
|
|
| Some c -> c) >>= fun c ->
|
|
|
|
|
|
|
|
(if enable_stats then
|
|
|
|
(unix_connect ~retries `Stats >|= function
|
|
|
|
| None -> invalid_arg "cannot connect to stats socket"
|
|
|
|
| Some c -> Some c)
|
|
|
|
else
|
|
|
|
Lwt.return_none)
|
|
|
|
>>= fun s ->
|
|
|
|
|
|
|
|
Lwt.catch (fun () ->
|
|
|
|
Vmm_lwt.server_socket `Vmmd)
|
|
|
|
(fun _ -> invalid_arg ("unable to create server socket " ^ (socket_path `Vmmd)))
|
|
|
|
>>= fun ss ->
|
2019-01-20 21:17:59 +00:00
|
|
|
let self_destruct_mutex = Lwt_mutex.create () in
|
|
|
|
let self_destruct () =
|
|
|
|
Lwt_mutex.with_lock self_destruct_mutex (fun () ->
|
|
|
|
(if Vmm_vmmd.killall !state then
|
|
|
|
(* not too happy about the sleep here, but cleaning up resources
|
|
|
|
is really important (fifos, vm images, tap devices) - which
|
|
|
|
is done asynchronously (in the task waitpid() on the pid) *)
|
|
|
|
Lwt_unix.sleep 1.
|
|
|
|
else
|
|
|
|
Lwt.return_unit) >>= fun () ->
|
|
|
|
Vmm_lwt.safe_close ss)
|
|
|
|
in
|
|
|
|
Sys.(set_signal sigterm (Signal_handle (fun _ -> Lwt.async self_destruct)));
|
|
|
|
|
2019-01-27 15:07:53 +00:00
|
|
|
let log_out txt wire = write_reply "log" l txt wire >|= fun _ -> ()
|
|
|
|
and cons_out = write_reply "cons" c
|
|
|
|
and stat_out txt wire = match s with
|
|
|
|
| None -> Logs.info (fun m -> m "ignoring stat %s %a" txt Vmm_commands.pp_wire wire) ; Lwt.return_unit
|
|
|
|
| Some s -> write_reply "stat" s txt wire >|= fun _ -> ()
|
2019-01-20 21:17:59 +00:00
|
|
|
in
|
|
|
|
|
2019-11-09 20:29:05 +00:00
|
|
|
Lwt_list.iter_s (fun (name, config) ->
|
2019-11-11 21:11:22 +00:00
|
|
|
Lwt_mutex.with_lock create_lock (fun () ->
|
|
|
|
create stat_out log_out cons_out stub_data_out name config))
|
2019-10-10 23:10:33 +00:00
|
|
|
(Vmm_trie.all old_unikernels) >>= fun () ->
|
2019-01-20 21:17:59 +00:00
|
|
|
|
|
|
|
Lwt.catch (fun () ->
|
|
|
|
let rec loop () =
|
|
|
|
Lwt_unix.accept ss >>= fun (fd, addr) ->
|
|
|
|
Lwt_unix.set_close_on_exec fd ;
|
2019-10-10 20:26:36 +00:00
|
|
|
m `Open;
|
|
|
|
Lwt.async (fun () ->
|
|
|
|
handle log_out cons_out stat_out fd addr >|= fun () ->
|
|
|
|
m `Close) ;
|
2019-01-20 21:17:59 +00:00
|
|
|
loop ()
|
|
|
|
in
|
|
|
|
loop ())
|
|
|
|
(fun e ->
|
|
|
|
Logs.err (fun m -> m "exception %s, shutting down" (Printexc.to_string e));
|
|
|
|
self_destruct ()))
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
open Cmdliner
|
|
|
|
|
|
|
|
let cmd =
|
2020-05-19 18:51:32 +00:00
|
|
|
Term.(const jump $ setup_log $ influx $ tmpdir $ dbdir $ retry_connections $ enable_stats),
|
2019-11-11 20:49:51 +00:00
|
|
|
Term.info "albatrossd" ~version:Albatross_cli.version
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
let () = match Term.eval cmd with `Ok () -> exit 0 | _ -> exit 1
|