From 77c3fd9e3f99c27e3124d440e912dd6c0890ecf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edwin=20T=C3=B6r=C3=B6k?= Date: Mon, 22 Jan 2024 11:36:30 +0000 Subject: [PATCH] [maintenance]: make format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Edwin Török --- ocaml/libs/xapi-inventory/lib/inventory.ml | 113 +++++++++++++-------- ocaml/libs/xapi-rrd/lib/rrd_utils.ml | 6 +- ocaml/libs/xapi-rrd/lib_test/unit_tests.ml | 10 +- 3 files changed, 77 insertions(+), 52 deletions(-) diff --git a/ocaml/libs/xapi-inventory/lib/inventory.ml b/ocaml/libs/xapi-inventory/lib/inventory.ml index d4b5046808e..374780a09f8 100644 --- a/ocaml/libs/xapi-inventory/lib/inventory.ml +++ b/ocaml/libs/xapi-inventory/lib/inventory.ml @@ -20,23 +20,36 @@ let inventory_filename = ref "/etc/xensource-inventory" (* Keys which must exist: *) let _installation_uuid = "INSTALLATION_UUID" + let _control_domain_uuid = "CONTROL_DOMAIN_UUID" + let _management_interface = "MANAGEMENT_INTERFACE" + let _management_address_type = "MANAGEMENT_ADDRESS_TYPE" + let _build_number = "BUILD_NUMBER" (* Optional keys: *) let _current_interfaces = "CURRENT_INTERFACES" + let _oem_manufacturer = "OEM_MANUFACTURER" + let _oem_model = "OEM_MODEL" + let _oem_build_number = "OEM_BUILD_NUMBER" + let _machine_serial_number = "MACHINE_SERIAL_NUMBER" + let _machine_serial_name = "MACHINE_SERIAL_NAME" + let _stunnel_idle_timeout = "STUNNEL_IDLE_TIMEOUT" + let _stunnel_legacy = "STUNNEL_LEGACY" let loaded_inventory = ref false + let inventory = Hashtbl.create 10 + let inventory_m = Mutex.create () (* Compute the minimum necessary inventory file contents *) @@ -44,77 +57,91 @@ let minimum_default_entries () = let host_uuid = Uuidm.to_string (Uuidm.v `V4) in let dom0_uuid = Uuidm.to_string (Uuidm.v `V4) in [ - _installation_uuid, host_uuid; - _control_domain_uuid, dom0_uuid; - _management_interface, ""; - _management_address_type, "IPv4"; - _build_number, "0" + (_installation_uuid, host_uuid) + ; (_control_domain_uuid, dom0_uuid) + ; (_management_interface, "") + ; (_management_address_type, "IPv4") + ; (_build_number, "0") ] (* trim any quotes off the ends *) let strip_quotes v = - if String.length v >= 2 - && v.[0] = '\'' - && v.[String.length v - 1] = '\'' - then String.sub v 1 (String.length v - 2) - else v + if String.length v >= 2 && v.[0] = '\'' && v.[String.length v - 1] = '\'' then + String.sub v 1 (String.length v - 2) + else + v let parse_inventory_entry line = match Astring.String.cut ~sep:"=" line with - | Some(k, v) -> - (* trim whitespace *) - Some (k, v |> strip_quotes |> String.trim) - | None -> None + | Some (k, v) -> + (* trim whitespace *) + Some (k, v |> strip_quotes |> String.trim) + | None -> + None let string_of_table h = - let lines = List.fold_left (fun acc (k, v) -> - Printf.sprintf "%s='%s'\n" k v :: acc) [] h in + let lines = + List.fold_left + (fun acc (k, v) -> Printf.sprintf "%s='%s'\n" k v :: acc) + [] h + in String.concat "" lines let read_inventory_contents () = - if not (Sys.file_exists !inventory_filename) then begin - Unixext.write_string_to_file !inventory_filename ( - string_of_table (minimum_default_entries ())) - end; + if not (Sys.file_exists !inventory_filename) then + Unixext.write_string_to_file !inventory_filename + (string_of_table (minimum_default_entries ())) ; (* Perhaps we should blank the old inventory before we read the new one? What is the desired behaviour? *) - Unixext.file_lines_iter (fun line -> + Unixext.file_lines_iter + (fun line -> match parse_inventory_entry line with - | Some (k, v) -> Hashtbl.add inventory k v - | None -> ()) - !inventory_filename; + | Some (k, v) -> + Hashtbl.add inventory k v + | None -> + () + ) + !inventory_filename ; loaded_inventory := true let read_inventory () = M.execute inventory_m read_inventory_contents -let reread_inventory () = M.execute inventory_m (fun () -> - Hashtbl.clear inventory; - read_inventory_contents ()) + +let reread_inventory () = + M.execute inventory_m (fun () -> + Hashtbl.clear inventory ; read_inventory_contents () + ) exception Missing_inventory_key of string let lookup ?default key = M.execute inventory_m (fun () -> - (if not (!loaded_inventory) then read_inventory_contents ()); - if (Hashtbl.mem inventory key) - then + if not !loaded_inventory then read_inventory_contents () ; + if Hashtbl.mem inventory key then Hashtbl.find inventory key else match default with - | None -> raise (Missing_inventory_key key) - | Some v -> v) + | None -> + raise (Missing_inventory_key key) + | Some v -> + v + ) let flush_to_disk_locked () = let h = Hashtbl.fold (fun k v acc -> (k, v) :: acc) inventory [] in Unixext.write_string_to_file !inventory_filename (string_of_table h) -let update key value = M.execute inventory_m (fun () -> - Hashtbl.clear inventory; - read_inventory_contents (); - Hashtbl.replace inventory key value; - flush_to_disk_locked ()) - -let remove key = M.execute inventory_m (fun () -> - Hashtbl.clear inventory; - read_inventory_contents (); - Hashtbl.remove inventory key; - flush_to_disk_locked ()) +let update key value = + M.execute inventory_m (fun () -> + Hashtbl.clear inventory ; + read_inventory_contents () ; + Hashtbl.replace inventory key value ; + flush_to_disk_locked () + ) + +let remove key = + M.execute inventory_m (fun () -> + Hashtbl.clear inventory ; + read_inventory_contents () ; + Hashtbl.remove inventory key ; + flush_to_disk_locked () + ) diff --git a/ocaml/libs/xapi-rrd/lib/rrd_utils.ml b/ocaml/libs/xapi-rrd/lib/rrd_utils.ml index 61bdb6f22ac..c0863d0175f 100644 --- a/ocaml/libs/xapi-rrd/lib/rrd_utils.ml +++ b/ocaml/libs/xapi-rrd/lib/rrd_utils.ml @@ -122,8 +122,7 @@ module Xmlm_utils = struct | _ -> raise Parse_error in - accept `El_end i ; - d + accept `El_end i ; d ) else raise Parse_error @@ -136,6 +135,5 @@ module Xmlm_utils = struct let read_block t f i = accept (start_tag t) i ; let res = f i in - accept `El_end i ; - res + accept `El_end i ; res end diff --git a/ocaml/libs/xapi-rrd/lib_test/unit_tests.ml b/ocaml/libs/xapi-rrd/lib_test/unit_tests.ml index aa15b94f745..d1938b68a42 100644 --- a/ocaml/libs/xapi-rrd/lib_test/unit_tests.ml +++ b/ocaml/libs/xapi-rrd/lib_test/unit_tests.ml @@ -92,10 +92,10 @@ let test_marshall rrd ~json () = ignore ( if json then Rrd.json_to_string rrd - else - let out = Buffer.create 2048 in - Rrd.xml_to_output rrd (Xmlm.make_output (`Buffer out)) ; - Buffer.contents out + else + let out = Buffer.create 2048 in + Rrd.xml_to_output rrd (Xmlm.make_output (`Buffer out)) ; + Buffer.contents out ) let test_marshall_unmarshall rrd () = @@ -313,7 +313,7 @@ let suite_create_multi = ) in let test_no_rrds () = - Alcotest.check_raises "should raise error" (No_RRA_Available) (fun () -> + Alcotest.check_raises "should raise error" No_RRA_Available (fun () -> let _ = RU.create_multi [] 0L 1L None in () )