2018-09-09 18:52:04 +00:00
|
|
|
(* (c) 2017, 2018 Hannes Mehnert, all rights reserved *)
|
|
|
|
|
|
|
|
open Lwt.Infix
|
|
|
|
|
2018-11-12 21:11:06 +00:00
|
|
|
let version = `AV3
|
2018-10-22 21:20:00 +00:00
|
|
|
|
2018-09-19 19:16:44 +00:00
|
|
|
let socket t = function
|
|
|
|
| Some x -> x
|
|
|
|
| None -> Vmm_core.socket_path t
|
|
|
|
|
|
|
|
let connect socket_path =
|
2018-09-09 18:52:04 +00:00
|
|
|
let c = Lwt_unix.(socket PF_UNIX SOCK_STREAM 0) in
|
|
|
|
Lwt_unix.set_close_on_exec c ;
|
2018-09-19 19:16:44 +00:00
|
|
|
Lwt_unix.connect c (Lwt_unix.ADDR_UNIX socket_path) >|= fun () ->
|
2018-09-09 18:52:04 +00:00
|
|
|
c
|
|
|
|
|
2018-11-01 00:51:39 +00:00
|
|
|
let process fd =
|
|
|
|
Vmm_lwt.read_wire fd >|= function
|
|
|
|
| Error _ -> Error ()
|
2019-03-27 23:11:43 +00:00
|
|
|
| Ok wire -> Ok (Albatross_cli.print_result version wire)
|
2018-11-01 00:51:39 +00:00
|
|
|
|
2018-10-14 00:18:33 +00:00
|
|
|
let read fd =
|
2018-10-13 23:02:52 +00:00
|
|
|
(* now we busy read and process output *)
|
|
|
|
let rec loop () =
|
2018-10-22 21:20:00 +00:00
|
|
|
process fd >>= function
|
2018-11-01 00:51:39 +00:00
|
|
|
| Error _ -> Lwt.return ()
|
2018-10-22 21:20:00 +00:00
|
|
|
| Ok () -> loop ()
|
2018-10-13 23:02:52 +00:00
|
|
|
in
|
|
|
|
loop ()
|
|
|
|
|
2018-11-11 00:21:12 +00:00
|
|
|
let handle opt_socket name (cmd : Vmm_commands.t) =
|
2018-10-23 22:03:36 +00:00
|
|
|
let sock, next = Vmm_commands.endpoint cmd in
|
2018-10-13 23:02:52 +00:00
|
|
|
connect (socket sock opt_socket) >>= fun fd ->
|
2018-11-11 00:21:12 +00:00
|
|
|
let header = Vmm_commands.{ version ; sequence = 0L ; name } in
|
2018-10-22 21:20:00 +00:00
|
|
|
Vmm_lwt.write_wire fd (header, `Command cmd) >>= function
|
2018-11-01 00:51:39 +00:00
|
|
|
| Error `Exception -> Lwt.return ()
|
2018-10-13 23:02:52 +00:00
|
|
|
| Ok () ->
|
|
|
|
(match next with
|
2018-10-14 00:18:33 +00:00
|
|
|
| `Read -> read fd
|
2018-11-01 00:51:39 +00:00
|
|
|
| `End -> process fd >|= ignore) >>= fun () ->
|
|
|
|
Vmm_lwt.safe_close fd
|
2018-10-13 23:02:52 +00:00
|
|
|
|
2018-10-22 21:20:00 +00:00
|
|
|
let jump opt_socket name cmd =
|
2019-04-08 14:40:58 +00:00
|
|
|
Ok (Lwt_main.run (handle opt_socket name cmd))
|
2018-10-13 23:02:52 +00:00
|
|
|
|
2018-10-28 21:14:39 +00:00
|
|
|
let info_policy _ opt_socket name =
|
|
|
|
jump opt_socket name (`Policy_cmd `Policy_info)
|
2018-10-14 00:18:33 +00:00
|
|
|
|
2018-10-22 21:20:00 +00:00
|
|
|
let remove_policy _ opt_socket name =
|
|
|
|
jump opt_socket name (`Policy_cmd `Policy_remove)
|
2018-10-12 23:05:21 +00:00
|
|
|
|
|
|
|
let add_policy _ opt_socket name vms memory cpus block bridges =
|
2019-03-27 23:11:43 +00:00
|
|
|
let p = Albatross_cli.policy vms memory cpus block bridges in
|
2018-10-28 21:14:39 +00:00
|
|
|
jump opt_socket name (`Policy_cmd (`Policy_add p))
|
2018-09-09 18:52:04 +00:00
|
|
|
|
2018-11-13 00:02:05 +00:00
|
|
|
let info_ _ opt_socket name =
|
|
|
|
jump opt_socket name (`Unikernel_cmd `Unikernel_info)
|
|
|
|
|
2018-09-19 19:16:44 +00:00
|
|
|
let destroy _ opt_socket name =
|
2018-11-13 00:02:05 +00:00
|
|
|
jump opt_socket name (`Unikernel_cmd `Unikernel_destroy)
|
2018-09-09 18:52:04 +00:00
|
|
|
|
2018-11-13 00:02:05 +00:00
|
|
|
let create _ opt_socket force name image cpuid memory argv block network compression =
|
2019-03-27 23:11:43 +00:00
|
|
|
match Albatross_cli.create_vm force image cpuid memory argv block network compression with
|
2018-11-13 00:02:05 +00:00
|
|
|
| Ok cmd -> jump opt_socket name (`Unikernel_cmd cmd)
|
2019-04-08 14:40:58 +00:00
|
|
|
| Error (`Msg msg) -> Error (`Msg msg)
|
2018-10-14 00:18:33 +00:00
|
|
|
|
2018-10-23 23:07:12 +00:00
|
|
|
let console _ opt_socket name since =
|
|
|
|
jump opt_socket name (`Console_cmd (`Console_subscribe since))
|
2018-10-14 00:18:33 +00:00
|
|
|
|
2019-01-14 23:25:59 +00:00
|
|
|
let stats_add _ opt_socket name vmmdev pid bridge_taps =
|
|
|
|
jump opt_socket name (`Stats_cmd (`Stats_add (vmmdev, pid, bridge_taps)))
|
|
|
|
|
|
|
|
let stats_remove _ opt_socket name =
|
|
|
|
jump opt_socket name (`Stats_cmd `Stats_remove)
|
|
|
|
|
|
|
|
let stats_subscribe _ opt_socket name =
|
2018-10-23 23:07:12 +00:00
|
|
|
jump opt_socket name (`Stats_cmd `Stats_subscribe)
|
2018-10-14 00:18:33 +00:00
|
|
|
|
2018-10-23 23:07:12 +00:00
|
|
|
let event_log _ opt_socket name since =
|
|
|
|
jump opt_socket name (`Log_cmd (`Log_subscribe since))
|
2018-09-28 20:44:38 +00:00
|
|
|
|
2018-11-10 00:02:07 +00:00
|
|
|
let block_info _ opt_socket block_name =
|
|
|
|
jump opt_socket block_name (`Block_cmd `Block_info)
|
|
|
|
|
|
|
|
let block_create _ opt_socket block_name block_size =
|
|
|
|
jump opt_socket block_name (`Block_cmd (`Block_add block_size))
|
|
|
|
|
|
|
|
let block_destroy _ opt_socket block_name =
|
|
|
|
jump opt_socket block_name (`Block_cmd `Block_remove)
|
|
|
|
|
2018-09-09 18:52:04 +00:00
|
|
|
let help _ _ man_format cmds = function
|
|
|
|
| None -> `Help (`Pager, None)
|
|
|
|
| Some t when List.mem t cmds -> `Help (man_format, Some t)
|
|
|
|
| Some _ -> List.iter print_endline cmds; `Ok ()
|
|
|
|
|
|
|
|
open Cmdliner
|
2019-03-27 23:11:43 +00:00
|
|
|
open Albatross_cli
|
2018-09-09 18:52:04 +00:00
|
|
|
|
|
|
|
let socket =
|
|
|
|
let doc = "Socket to connect to" in
|
2018-10-26 19:35:40 +00:00
|
|
|
Arg.(value & opt (some string) None & info [ "socket" ] ~doc)
|
2018-09-09 18:52:04 +00:00
|
|
|
|
|
|
|
let destroy_cmd =
|
|
|
|
let doc = "destroys a virtual machine" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
|
|
|
`P "Destroy a virtual machine."]
|
|
|
|
in
|
2019-04-08 14:40:58 +00:00
|
|
|
Term.(term_result (const destroy $ setup_log $ socket $ vm_name)),
|
2018-09-09 18:52:04 +00:00
|
|
|
Term.info "destroy" ~doc ~man
|
|
|
|
|
2018-10-12 23:05:21 +00:00
|
|
|
let remove_policy_cmd =
|
|
|
|
let doc = "removes a policy" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
|
|
|
`P "Removes a policy."]
|
|
|
|
in
|
2019-04-08 14:40:58 +00:00
|
|
|
Term.(term_result (const remove_policy $ setup_log $ socket $ opt_vm_name)),
|
2018-10-22 23:02:14 +00:00
|
|
|
Term.info "remove_policy" ~doc ~man
|
2018-10-12 23:05:21 +00:00
|
|
|
|
2018-09-09 18:52:04 +00:00
|
|
|
let info_cmd =
|
|
|
|
let doc = "information about VMs" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
|
|
|
`P "Shows information about VMs."]
|
|
|
|
in
|
2019-04-08 14:40:58 +00:00
|
|
|
Term.(term_result (const info_ $ setup_log $ socket $ opt_vm_name)),
|
2018-09-09 18:52:04 +00:00
|
|
|
Term.info "info" ~doc ~man
|
|
|
|
|
2018-10-12 23:05:21 +00:00
|
|
|
let policy_cmd =
|
|
|
|
let doc = "active policies" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
|
|
|
`P "Shows information about policies."]
|
|
|
|
in
|
2019-04-08 14:40:58 +00:00
|
|
|
Term.(term_result (const info_policy $ setup_log $ socket $ opt_vm_name)),
|
2018-10-12 23:05:21 +00:00
|
|
|
Term.info "policy" ~doc ~man
|
|
|
|
|
|
|
|
let add_policy_cmd =
|
|
|
|
let doc = "Add a policy" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
|
|
|
`P "Adds a policy."]
|
|
|
|
in
|
2019-04-08 14:40:58 +00:00
|
|
|
Term.(term_result (const add_policy $ setup_log $ socket $ vm_name $ vms $ mem $ cpus $ opt_block_size $ bridge)),
|
2018-10-12 23:05:21 +00:00
|
|
|
Term.info "add_policy" ~doc ~man
|
|
|
|
|
2018-09-09 18:52:04 +00:00
|
|
|
let create_cmd =
|
|
|
|
let doc = "creates a virtual machine" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
|
|
|
`P "Creates a virtual machine."]
|
|
|
|
in
|
2019-10-10 20:29:13 +00:00
|
|
|
Term.(term_result (const create $ setup_log $ socket $ force $ vm_name $ image $ cpu $ vm_mem $ args $ block $ net $ compress_level 0)),
|
2018-09-09 18:52:04 +00:00
|
|
|
Term.info "create" ~doc ~man
|
|
|
|
|
2018-09-19 19:16:44 +00:00
|
|
|
let console_cmd =
|
2018-09-20 20:53:42 +00:00
|
|
|
let doc = "console of a VM" in
|
2018-09-19 19:16:44 +00:00
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
2018-09-20 20:53:42 +00:00
|
|
|
`P "Shows console output of a VM."]
|
2018-09-19 19:16:44 +00:00
|
|
|
in
|
2019-04-08 14:40:58 +00:00
|
|
|
Term.(term_result (const console $ setup_log $ socket $ vm_name $ since)),
|
2018-09-19 19:16:44 +00:00
|
|
|
Term.info "console" ~doc ~man
|
|
|
|
|
2019-01-14 23:25:59 +00:00
|
|
|
let stats_subscribe_cmd =
|
2018-09-20 20:53:42 +00:00
|
|
|
let doc = "statistics of VMs" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
|
|
|
`P "Shows statistics of VMs."]
|
|
|
|
in
|
2019-04-08 14:40:58 +00:00
|
|
|
Term.(term_result (const stats_subscribe $ setup_log $ socket $ opt_vm_name)),
|
2018-09-20 20:53:42 +00:00
|
|
|
Term.info "stats" ~doc ~man
|
|
|
|
|
2019-01-14 23:25:59 +00:00
|
|
|
let stats_remove_cmd =
|
|
|
|
let doc = "remove statistics of VM" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
|
|
|
`P "Removes statistics of VM."]
|
|
|
|
in
|
2019-04-08 14:40:58 +00:00
|
|
|
Term.(term_result (const stats_remove $ setup_log $ socket $ opt_vm_name)),
|
2019-01-14 23:25:59 +00:00
|
|
|
Term.info "stats_remove" ~doc ~man
|
|
|
|
|
|
|
|
let stats_add_cmd =
|
|
|
|
let doc = "Add VM to statistics gathering" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
|
|
|
`P "Add VM to statistics gathering."]
|
|
|
|
in
|
2019-04-08 14:40:58 +00:00
|
|
|
Term.(term_result (const stats_add $ setup_log $ socket $ opt_vm_name $ vmm_dev_req0 $ pid_req1 $ bridge_taps)),
|
2019-01-14 23:25:59 +00:00
|
|
|
Term.info "stats_add" ~doc ~man
|
|
|
|
|
2018-09-28 20:44:38 +00:00
|
|
|
let log_cmd =
|
|
|
|
let doc = "Event log" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
|
|
|
`P "Shows event log of VM."]
|
|
|
|
in
|
2019-04-08 14:40:58 +00:00
|
|
|
Term.(term_result (const event_log $ setup_log $ socket $ opt_vm_name $ since)),
|
2018-09-28 20:44:38 +00:00
|
|
|
Term.info "log" ~doc ~man
|
|
|
|
|
2018-11-10 00:02:07 +00:00
|
|
|
let block_info_cmd =
|
|
|
|
let doc = "Information about block devices" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
|
|
|
`P "Block device information."]
|
|
|
|
in
|
2019-04-08 14:40:58 +00:00
|
|
|
Term.(term_result (const block_info $ setup_log $ socket $ opt_block_name)),
|
2018-11-10 00:02:07 +00:00
|
|
|
Term.info "block" ~doc ~man
|
|
|
|
|
|
|
|
let block_create_cmd =
|
|
|
|
let doc = "Create a block device" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
|
|
|
`P "Creation of a block device."]
|
|
|
|
in
|
2019-04-08 14:40:58 +00:00
|
|
|
Term.(term_result (const block_create $ setup_log $ socket $ block_name $ block_size)),
|
2018-11-10 00:02:07 +00:00
|
|
|
Term.info "create_block" ~doc ~man
|
|
|
|
|
|
|
|
let block_destroy_cmd =
|
|
|
|
let doc = "Destroys a block device" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
|
|
|
`P "Destroys a block device."]
|
|
|
|
in
|
2019-04-08 14:40:58 +00:00
|
|
|
Term.(term_result (const block_destroy $ setup_log $ socket $ block_name)),
|
2018-11-10 00:02:07 +00:00
|
|
|
Term.info "destroy_block" ~doc ~man
|
|
|
|
|
2018-09-09 18:52:04 +00:00
|
|
|
let help_cmd =
|
|
|
|
let topic =
|
|
|
|
let doc = "The topic to get help on. `topics' lists the topics." in
|
|
|
|
Arg.(value & pos 0 (some string) None & info [] ~docv:"TOPIC" ~doc)
|
|
|
|
in
|
|
|
|
let doc = "display help about vmmc" in
|
|
|
|
let man =
|
|
|
|
[`S "DESCRIPTION";
|
2018-10-28 21:14:39 +00:00
|
|
|
`P "Prints help about albatross local client commands and subcommands"]
|
2018-09-09 18:52:04 +00:00
|
|
|
in
|
|
|
|
Term.(ret (const help $ setup_log $ socket $ Term.man_format $ Term.choice_names $ topic)),
|
|
|
|
Term.info "help" ~doc ~man
|
|
|
|
|
|
|
|
let default_cmd =
|
2018-10-28 21:14:39 +00:00
|
|
|
let doc = "VMM local client" in
|
2018-09-09 18:52:04 +00:00
|
|
|
let man = [
|
|
|
|
`S "DESCRIPTION" ;
|
2019-03-27 23:11:43 +00:00
|
|
|
`P "$(tname) connects to albatrossd via a local socket" ]
|
2018-09-09 18:52:04 +00:00
|
|
|
in
|
|
|
|
Term.(ret (const help $ setup_log $ socket $ Term.man_format $ Term.choice_names $ Term.pure None)),
|
2019-03-27 23:11:43 +00:00
|
|
|
Term.info "albatross_client_local" ~version:"%%VERSION_NUM%%" ~doc ~man
|
2018-09-09 18:52:04 +00:00
|
|
|
|
2018-11-10 00:02:07 +00:00
|
|
|
let cmds = [ help_cmd ; info_cmd ;
|
|
|
|
policy_cmd ; remove_policy_cmd ; add_policy_cmd ;
|
|
|
|
destroy_cmd ; create_cmd ;
|
|
|
|
block_info_cmd ; block_create_cmd ; block_destroy_cmd ;
|
2019-01-14 23:25:59 +00:00
|
|
|
console_cmd ;
|
|
|
|
stats_subscribe_cmd ; stats_add_cmd ; stats_remove_cmd ; log_cmd ]
|
2018-09-09 18:52:04 +00:00
|
|
|
|
|
|
|
let () =
|
|
|
|
match Term.eval_choice default_cmd cmds
|
|
|
|
with `Ok () -> exit 0 | _ -> exit 1
|