Skip to content

Commit

Permalink
[maintenance]: make format
Browse files Browse the repository at this point in the history
Signed-off-by: Edwin Török <[email protected]>
  • Loading branch information
edwintorok committed Jan 22, 2024
1 parent d43ea80 commit 77c3fd9
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 52 deletions.
113 changes: 70 additions & 43 deletions ocaml/libs/xapi-inventory/lib/inventory.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,101 +20,128 @@ 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 *)
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 ()
)
6 changes: 2 additions & 4 deletions ocaml/libs/xapi-rrd/lib/rrd_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
10 changes: 5 additions & 5 deletions ocaml/libs/xapi-rrd/lib_test/unit_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 () =
Expand Down Expand Up @@ -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
()
)
Expand Down

0 comments on commit 77c3fd9

Please sign in to comment.