Skip to content

Commit 680f30d

Browse files
committed
CP-54471 Add ntp config dbsync
At XAPI start, check the actual NTP config to determine the ntp mode, ntp enabled, ntp custom servers and store in xapi DB. Signed-off-by: Changlei Li <changlei.li@cloud.com>
1 parent ce0ece9 commit 680f30d

File tree

6 files changed

+51
-0
lines changed

6 files changed

+51
-0
lines changed

ocaml/xapi/dbsync_slave.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ let update_env __context sync_keys =
412412
switched_sync Xapi_globs.sync_max_cstate (fun () ->
413413
Xapi_host.sync_max_cstate ~__context ~host:localhost
414414
) ;
415+
switched_sync Xapi_globs.sync_ntp_config (fun () ->
416+
Xapi_host.sync_ntp_config ~__context ~host:localhost
417+
) ;
415418

416419
switched_sync Xapi_globs.sync_secure_boot (fun () ->
417420
let result =

ocaml/xapi/xapi_globs.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ let sync_ssh_status = "sync_ssh_status"
376376

377377
let sync_max_cstate = "sync_max_cstate"
378378

379+
let sync_ntp_config = "sync_ntp_config"
380+
379381
let sync_secure_boot = "sync_secure_boot"
380382

381383
let sync_pci_devices = "sync_pci_devices"

ocaml/xapi/xapi_host.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,3 +3441,23 @@ let enable_ntp ~__context ~self =
34413441
let disable_ntp ~__context ~self =
34423442
Xapi_host_ntp.disable_ntp_service () ;
34433443
Db.Host.set_ntp_enabled ~__context ~self ~value:false
3444+
3445+
let sync_ntp_config ~__context ~host =
3446+
let servers = Xapi_host_ntp.get_servers_from_conf () in
3447+
let is_ntp_dhcp_enabled = Xapi_host_ntp.is_ntp_dhcp_enabled () in
3448+
let ntp_mode =
3449+
match (is_ntp_dhcp_enabled, servers) with
3450+
| true, _ ->
3451+
`ntp_mode_dhcp
3452+
| false, s
3453+
when Xapi_stdext_std.Listext.List.set_equiv s
3454+
!Xapi_globs.default_ntp_servers ->
3455+
`ntp_mode_default
3456+
| false, _ ->
3457+
`ntp_mode_custom
3458+
in
3459+
Db.Host.set_ntp_mode ~__context ~self:host ~value:ntp_mode ;
3460+
if ntp_mode = `ntp_mode_custom then
3461+
Db.Host.set_ntp_custom_servers ~__context ~self:host ~value:servers ;
3462+
let ntp_enabled = Xapi_host_ntp.is_ntp_service_active () in
3463+
Db.Host.set_ntp_enabled ~__context ~self:host ~value:ntp_enabled

ocaml/xapi/xapi_host.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,5 @@ val set_ntp_custom_servers :
605605
val enable_ntp : __context:Context.t -> self:API.ref_host -> unit
606606

607607
val disable_ntp : __context:Context.t -> self:API.ref_host -> unit
608+
609+
val sync_ntp_config : __context:Context.t -> host:API.ref_host -> unit

ocaml/xapi/xapi_host_ntp.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ let disable_ntp_service () =
7979
Xapi_systemctl.stop ~wait_until_success:false !Xapi_globs.ntp_service ;
8080
Xapi_systemctl.disable ~wait_until_success:false !Xapi_globs.ntp_service
8181

82+
let is_ntp_service_active () =
83+
Fe_systemctl.is_active ~service:!Xapi_globs.ntp_service
84+
8285
let parse_ntp_conf () =
8386
try
8487
Xapi_stdext_unix.Unixext.read_lines ~path:!Xapi_globs.ntp_conf
@@ -99,3 +102,18 @@ let set_servers_in_conf servers =
99102
write_ntp_conf other servers
100103

101104
let clear_servers_in_conf () = set_servers_in_conf []
105+
106+
let get_servers_from_conf () =
107+
let servers, _ = parse_ntp_conf () in
108+
List.filter_map
109+
(fun line ->
110+
try Scanf.sscanf line "server %s@ iburst%!" Option.some with _ -> None
111+
)
112+
servers
113+
114+
let is_ntp_dhcp_enabled () =
115+
(* check ntp_dhcp_script exec permission *)
116+
try
117+
let stat = Unix.stat !Xapi_globs.ntp_dhcp_script in
118+
stat.Unix.st_perm land 0o100 <> 0
119+
with _ -> false

ocaml/xapi/xapi_host_ntp.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ val restart_ntp_service : unit -> unit
2525
val enable_ntp_service : unit -> unit
2626

2727
val disable_ntp_service : unit -> unit
28+
29+
val is_ntp_service_active : unit -> bool
30+
31+
val get_servers_from_conf : unit -> string list
32+
33+
val is_ntp_dhcp_enabled : unit -> bool

0 commit comments

Comments
 (0)