From f954955dd099e9f06421e5f2d934b0b444146fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Sat, 14 Nov 2020 22:27:55 +0100 Subject: [PATCH] Ergonomics (#41) The info subcommands for {unikernel,block,policy} never error Before, running the commands block, info or policy when no block devices, unikernels or policies respectively were set up the command would report an error and give the user a dangerous-looking WARNING saying the command failed: $ albatross-client-local block albatross-client-local: [WARNING] host [vm: ]: command failed block: not found Now instead the commands will report success with a message stating there are no objects. Co-authored-by: Hannes Mehnert --- src/vmm_commands.ml | 11 ++++++++--- src/vmm_vmmd.ml | 22 +++------------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/vmm_commands.ml b/src/vmm_commands.ml index f62f184..cfb8574 100644 --- a/src/vmm_commands.ml +++ b/src/vmm_commands.ml @@ -141,12 +141,17 @@ type success = [ let pp_block ppf (id, size, active) = Fmt.pf ppf "block %a size %d MB active %B" Name.pp id size active +let my_fmt_list empty pp_elt ppf xs = + match xs with + | [] -> Fmt.string ppf empty + | _ -> Fmt.(list ~sep:(unit "@.") pp_elt ppf xs) + let pp_success ppf = function | `Empty -> Fmt.string ppf "success" | `String data -> Fmt.pf ppf "success: %s" data - | `Policies ps -> Fmt.(list ~sep:(unit "@.") (pair ~sep:(unit ": ") Name.pp Policy.pp)) ppf ps - | `Unikernels vms -> Fmt.(list ~sep:(unit "@.") (pair ~sep:(unit ": ") Name.pp Unikernel.pp_config)) ppf vms - | `Block_devices blocks -> Fmt.(list ~sep:(unit "@.") pp_block) ppf blocks + | `Policies ps -> my_fmt_list "no policies" Fmt.(pair ~sep:(unit ": ") Name.pp Policy.pp) ppf ps + | `Unikernels vms -> my_fmt_list "no unikernels" Fmt.(pair ~sep:(unit ": ") Name.pp Unikernel.pp_config) ppf vms + | `Block_devices blocks -> my_fmt_list "no block devices" pp_block ppf blocks type res = [ | `Command of t diff --git a/src/vmm_vmmd.ml b/src/vmm_vmmd.ml index 7b58f89..bca0d4a 100644 --- a/src/vmm_vmmd.ml +++ b/src/vmm_vmmd.ml @@ -212,12 +212,7 @@ let handle_policy_cmd t id = function (fun prefix policy policies-> (prefix, policy) :: policies) [] in - match policies with - | [] -> - Logs.debug (fun m -> m "policies: couldn't find %a" Name.pp id) ; - Error (`Msg "policy: not found") - | _ -> - Ok (t, `End (`Success (`Policies policies))) + Ok (t, `End (`Success (`Policies policies))) let handle_unikernel_cmd t id = function | `Unikernel_info -> @@ -229,13 +224,7 @@ let handle_unikernel_cmd t id = function (id, cfg) :: vms) [] in - begin match vms with - | [] -> - Logs.debug (fun m -> m "info: couldn't find %a" Name.pp id) ; - Error (`Msg "info: no unikernel found") - | _ -> - Ok (t, `End (`Success (`Unikernels vms))) - end + Ok (t, `End (`Success (`Unikernels vms))) | `Unikernel_get -> Logs.debug (fun m -> m "get %a" Name.pp id) ; begin match Vmm_trie.find id t.resources.Vmm_resources.unikernels with @@ -304,12 +293,7 @@ let handle_block_cmd t id = function (fun prefix (size, active) blocks -> (prefix, size, active) :: blocks) [] in - match blocks with - | [] -> - Logs.debug (fun m -> m "block: couldn't find %a" Name.pp id) ; - Error (`Msg "block: not found") - | _ -> - Ok (t, `End (`Success (`Block_devices blocks))) + Ok (t, `End (`Success (`Block_devices blocks))) let handle_command t (header, payload) = let msg_to_err = function