Skip to content

Commit 1200a96

Browse files
committed
CP-54476 Merge ntp disable/enable to ntp mode
Signed-off-by: Changlei Li <changlei.li@cloud.com>
1 parent 442efae commit 1200a96

File tree

11 files changed

+73
-148
lines changed

11 files changed

+73
-148
lines changed

doc/content/design/host-ntp-time.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ cache for the getter APIs.
3838

3939
New fields:
4040
`host.ntp_mode`, enum host_ntp_mode(DHCP, Custom, Factory, Disabled)
41-
`host.ntp_custom_servers`, string set
42-
`host.ntp_enabled`, bool
41+
`host.ntp_custom_servers`, string set
4342

4443
New APIs: `host.set_ntp_mode`, `host.set_ntp_custom_servers`, `host.get_ntp_mode`,
4544
`host.get_ntp_custom_servers`, `host.get_ntp_servers_status`
@@ -201,7 +200,6 @@ host.timezone
201200
host.set_timezone
202201
host.list_timezones
203202
host.get_timezone
204-
host.get_ntp_enabled
205203
host.get_ntp_synchronized
206204
host.set_ntp_mode
207205
host.get_ntp_mode

ocaml/idl/datamodel_host.ml

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,11 +2579,10 @@ let host_ntp_mode =
25792579
Enum
25802580
( "host_ntp_mode"
25812581
, [
2582-
("ntp_mode_dhcp", "Using NTP servers assigned by DHCP to sync time")
2583-
; ( "ntp_mode_custom"
2584-
, "Using custom NTP servers configured by user to sync time"
2585-
)
2586-
; ("ntp_mode_default", "Using default NTP servers to sync time")
2582+
("DHCP", "Using NTP servers assigned by DHCP to sync time")
2583+
; ("Custom", "Using custom NTP servers configured by user to sync time")
2584+
; ("Factory", "Using built-in NTP servers to sync time")
2585+
; ("Disabled", "NTP is disabled on the host")
25872586
]
25882587
)
25892588

@@ -2606,16 +2605,6 @@ let set_ntp_custom_servers =
26062605
]
26072606
~allowed_roles:_R_POOL_OP ()
26082607

2609-
let disable_ntp =
2610-
call ~name:"disable_ntp" ~lifecycle:[] ~doc:"Disable NTP on the host"
2611-
~params:[(Ref _host, "self", "The host")]
2612-
~allowed_roles:_R_POOL_OP ()
2613-
2614-
let enable_ntp =
2615-
call ~name:"enable_ntp" ~lifecycle:[] ~doc:"Enable NTP on the host"
2616-
~params:[(Ref _host, "self", "The host")]
2617-
~allowed_roles:_R_POOL_OP ()
2618-
26192608
let get_ntp_servers_status =
26202609
call ~name:"get_ntp_servers_status" ~lifecycle:[]
26212610
~doc:"Get the NTP servers status on the host"
@@ -2822,8 +2811,6 @@ let t =
28222811
; set_max_cstate
28232812
; set_ntp_mode
28242813
; set_ntp_custom_servers
2825-
; disable_ntp
2826-
; enable_ntp
28272814
; get_ntp_servers_status
28282815
; set_timezone
28292816
; list_timezones
@@ -3297,15 +3284,12 @@ let t =
32973284
~default_value:(Some (VBool false)) "secure_boot"
32983285
"Whether the host has booted in secure boot mode"
32993286
; field ~qualifier:DynamicRO ~lifecycle:[] ~ty:host_ntp_mode
3300-
~default_value:(Some (VEnum "ntp_mode_dhcp")) "ntp_mode"
3287+
~default_value:(Some (VEnum "DHCP")) "ntp_mode"
33013288
"Indicates NTP servers are assigned by DHCP, or configured by \
3302-
user, or the default servers"
3289+
user, or the factory servers, or NTP is disabled"
33033290
; field ~qualifier:DynamicRO ~lifecycle:[] ~ty:(Set String)
33043291
~default_value:(Some (VSet [])) "ntp_custom_servers"
3305-
"The set of NTP servers configured for the host"
3306-
; field ~qualifier:DynamicRO ~lifecycle:[] ~ty:Bool
3307-
~default_value:(Some (VBool false)) "ntp_enabled"
3308-
"Reflects whether NTP is enabled on the host"
3292+
"Custom NTP servers configured by users, used in Custom NTP mode"
33093293
; field ~qualifier:DynamicRO ~lifecycle:[] ~ty:String
33103294
~default_value:(Some (VString "UTC")) "timezone"
33113295
"The time zone identifier as defined in the IANA Time Zone Database"

ocaml/tests/common/test_common.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ let make_host2 ~__context ?(ref = Ref.make ()) ?(uuid = make_uuid ())
224224
~pending_guidances_recommended:[] ~pending_guidances_full:[]
225225
~last_update_hash:"" ~ssh_enabled:true ~ssh_enabled_timeout:0L
226226
~ssh_expiry:Date.epoch ~console_idle_timeout:0L ~ssh_auto_mode:false
227-
~max_cstate:"" ~secure_boot:false ~ntp_mode:`ntp_mode_dhcp
228-
~ntp_custom_servers:[] ~ntp_enabled:false ~timezone:"UTC" ;
227+
~max_cstate:"" ~secure_boot:false ~ntp_mode:`DHCP ~ntp_custom_servers:[]
228+
~timezone:"UTC" ;
229229
ref
230230

231231
let make_pif ~__context ~network ~host ?(device = "eth0")

ocaml/xapi-cli-server/cli_frontend.ml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,24 +1098,6 @@ let rec cmdtable_data : (string * cmd_spec) list =
10981098
; flags= [Neverforward]
10991099
}
11001100
)
1101-
; ( "host-enable-ntp"
1102-
, {
1103-
reqd= []
1104-
; optn= []
1105-
; help= "Enable ntp service on the host."
1106-
; implementation= No_fd Cli_operations.host_enable_ntp
1107-
; flags= [Host_selectors]
1108-
}
1109-
)
1110-
; ( "host-disable-ntp"
1111-
, {
1112-
reqd= []
1113-
; optn= []
1114-
; help= "Disable ntp service on the host."
1115-
; implementation= No_fd Cli_operations.host_disable_ntp
1116-
; flags= [Host_selectors]
1117-
}
1118-
)
11191101
; ( "patch-upload"
11201102
, {
11211103
reqd= ["file-name"]

ocaml/xapi-cli-server/cli_operations.ml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7820,26 +7820,6 @@ let host_disable_ssh _printer rpc session_id params =
78207820
params []
78217821
)
78227822

7823-
let host_enable_ntp _printer rpc session_id params =
7824-
ignore
7825-
(do_host_op rpc session_id
7826-
(fun _ host ->
7827-
let host = host.getref () in
7828-
Client.Host.enable_ntp ~rpc ~session_id ~self:host
7829-
)
7830-
params []
7831-
)
7832-
7833-
let host_disable_ntp _printer rpc session_id params =
7834-
ignore
7835-
(do_host_op rpc session_id
7836-
(fun _ host ->
7837-
let host = host.getref () in
7838-
Client.Host.disable_ntp ~rpc ~session_id ~self:host
7839-
)
7840-
params []
7841-
)
7842-
78437823
module SDN_controller = struct
78447824
let introduce printer rpc session_id params =
78457825
let port =

ocaml/xapi-cli-server/records.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3423,9 +3423,6 @@ let host_record rpc session_id host =
34233423
)
34243424
)
34253425
()
3426-
; make_field ~name:"ntp_enabled"
3427-
~get:(fun () -> string_of_bool (x ()).API.host_ntp_enabled)
3428-
()
34293426
; make_field ~name:"timezone"
34303427
~get:(fun () -> (x ()).API.host_timezone)
34313428
~set:(fun value ->

ocaml/xapi/message_forwarding.ml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4152,18 +4152,6 @@ functor
41524152
let remote_fn = Client.Host.set_ntp_custom_servers ~self ~value in
41534153
do_op_on ~local_fn ~__context ~host:self ~remote_fn
41544154

4155-
let enable_ntp ~__context ~self =
4156-
info "Host.enable_ntp: host='%s'" (host_uuid ~__context self) ;
4157-
let local_fn = Local.Host.enable_ntp ~self in
4158-
let remote_fn = Client.Host.enable_ntp ~self in
4159-
do_op_on ~local_fn ~__context ~host:self ~remote_fn
4160-
4161-
let disable_ntp ~__context ~self =
4162-
info "Host.disable_ntp: host='%s'" (host_uuid ~__context self) ;
4163-
let local_fn = Local.Host.disable_ntp ~self in
4164-
let remote_fn = Client.Host.disable_ntp ~self in
4165-
do_op_on ~local_fn ~__context ~host:self ~remote_fn
4166-
41674155
let get_ntp_servers_status ~__context ~self =
41684156
info "Host.get_ntp_servers_status: host = '%s'"
41694157
(host_uuid ~__context self) ;
@@ -4195,7 +4183,7 @@ functor
41954183
info "Host.set_servertime : host = '%s'; value = '%s'"
41964184
(host_uuid ~__context self)
41974185
(Clock.Date.to_rfc3339 value) ;
4198-
if Db.Host.get_ntp_enabled ~__context ~self then
4186+
if Db.Host.get_ntp_mode ~__context ~self <> `Disabled then
41994187
raise
42004188
(Api_errors.Server_error
42014189
(Api_errors.not_allowed_when_ntp_is_enabled, [Ref.string_of self])

ocaml/xapi/xapi_globs.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,9 +1425,9 @@ let nvidia_gpumon_detach = ref false
14251425

14261426
let failed_login_alert_freq = ref 3600
14271427

1428-
let default_ntp_servers = ref []
1428+
let factory_ntp_servers = ref []
14291429

1430-
let legacy_default_ntp_servers = ref []
1430+
let legacy_factory_ntp_servers = ref []
14311431

14321432
let other_options =
14331433
[
@@ -1916,16 +1916,16 @@ let other_options =
19161916
, (fun () -> !timedatectl)
19171917
, "Path to the timedatectl executable"
19181918
)
1919-
; gen_list_option "legacy-default-ntp-servers"
1919+
; gen_list_option "legacy-factory-ntp-servers"
19201920
"space-separated list of legacy default NTP servers"
19211921
(fun s -> s)
19221922
(fun s -> s)
1923-
legacy_default_ntp_servers
1924-
; gen_list_option "default-ntp-servers"
1923+
legacy_factory_ntp_servers
1924+
; gen_list_option "factory-ntp-servers"
19251925
"space-separated list of default NTP servers"
19261926
(fun s -> s)
19271927
(fun s -> s)
1928-
default_ntp_servers
1928+
factory_ntp_servers
19291929
]
19301930

19311931
(* The options can be set with the variable xapiflags in /etc/sysconfig/xapi.

ocaml/xapi/xapi_host.ml

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,8 +1094,8 @@ let create ~__context ~uuid ~name_label ~name_description:_ ~hostname ~address
10941094
~recommended_guidances:[] ~latest_synced_updates_applied:`unknown
10951095
~pending_guidances_recommended:[] ~pending_guidances_full:[] ~ssh_enabled
10961096
~ssh_enabled_timeout ~ssh_expiry ~console_idle_timeout ~ssh_auto_mode
1097-
~max_cstate:"" ~secure_boot ~ntp_mode:`ntp_mode_dhcp ~ntp_custom_servers:[]
1098-
~ntp_enabled:false ~timezone:"UTC" ;
1097+
~max_cstate:"" ~secure_boot ~ntp_mode:`DHCP ~ntp_custom_servers:[]
1098+
~timezone:"UTC" ;
10991099
(* If the host we're creating is us, make sure its set to live *)
11001100
Db.Host_metrics.set_last_updated ~__context ~self:metrics ~value:(Date.now ()) ;
11011101
Db.Host_metrics.set_live ~__context ~self:metrics ~value:host_is_us ;
@@ -3471,90 +3471,90 @@ let sync_max_cstate ~__context ~host =
34713471

34723472
let set_ntp_mode ~__context ~self ~value =
34733473
let current_mode = Db.Host.get_ntp_mode ~__context ~self in
3474-
let ntp_enabled = Db.Host.get_ntp_enabled ~__context ~self in
34753474
if current_mode <> value then (
34763475
let open Xapi_host_ntp in
3477-
let ensure_custom_servers_exist servers =
3476+
let ensure_servers_exist servers msg =
34783477
if servers = [] then
3479-
raise
3480-
Api_errors.(
3481-
Server_error
3482-
( invalid_ntp_config
3483-
, ["Can't set ntp_mode_custom when ntp_custom_servers is empty"]
3484-
)
3485-
)
3478+
raise Api_errors.(Server_error (invalid_ntp_config, [msg]))
3479+
in
3480+
let enter_mode = function
3481+
| `DHCP ->
3482+
add_dhcp_ntp_servers ()
3483+
| `Custom ->
3484+
let custom_servers =
3485+
Db.Host.get_ntp_custom_servers ~__context ~self
3486+
in
3487+
ensure_servers_exist custom_servers
3488+
"Can't set ntp_mode to Custom when ntp_custom_servers is empty" ;
3489+
set_servers_in_conf custom_servers
3490+
| `Factory ->
3491+
let factory_servers = !Xapi_globs.factory_ntp_servers in
3492+
ensure_servers_exist factory_servers
3493+
"Can't set ntp_mode to Factory when factory ntp servers is empty" ;
3494+
set_servers_in_conf factory_servers
3495+
| `Disabled ->
3496+
()
3497+
in
3498+
let exit_mode = function
3499+
| `DHCP ->
3500+
remove_dhcp_ntp_servers ()
3501+
| `Custom | `Factory ->
3502+
clear_servers_in_conf ()
3503+
| `Disabled ->
3504+
()
34863505
in
3487-
let default_servers = !Xapi_globs.default_ntp_servers in
34883506
( match (current_mode, value) with
3489-
| `ntp_mode_dhcp, `ntp_mode_custom ->
3490-
let custom_servers = Db.Host.get_ntp_custom_servers ~__context ~self in
3491-
ensure_custom_servers_exist custom_servers ;
3492-
remove_dhcp_ntp_servers () ;
3493-
set_servers_in_conf custom_servers
3494-
| `ntp_mode_default, `ntp_mode_custom ->
3495-
let custom_servers = Db.Host.get_ntp_custom_servers ~__context ~self in
3496-
ensure_custom_servers_exist custom_servers ;
3497-
set_servers_in_conf custom_servers
3498-
| _, `ntp_mode_dhcp ->
3499-
clear_servers_in_conf () ; add_dhcp_ntp_servers ()
3500-
| `ntp_mode_dhcp, `ntp_mode_default ->
3501-
remove_dhcp_ntp_servers () ;
3502-
set_servers_in_conf default_servers
3503-
| `ntp_mode_custom, `ntp_mode_default ->
3504-
set_servers_in_conf default_servers
3505-
| _, _ ->
3506-
()
3507+
| `Disabled, next ->
3508+
enter_mode next ; enable_ntp_service ()
3509+
| _, `Disabled ->
3510+
exit_mode current_mode ; disable_ntp_service ()
3511+
| current, next ->
3512+
exit_mode current ;
3513+
enter_mode next ;
3514+
Xapi_host_ntp.restart_ntp_service ()
35073515
) ;
3508-
if ntp_enabled then Xapi_host_ntp.restart_ntp_service () ;
35093516
Db.Host.set_ntp_mode ~__context ~self ~value
35103517
)
35113518

35123519
let set_ntp_custom_servers ~__context ~self ~value =
35133520
let current_mode = Db.Host.get_ntp_mode ~__context ~self in
35143521
match (current_mode, value) with
3515-
| `ntp_mode_custom, [] ->
3522+
| `Custom, [] ->
35163523
raise
35173524
Api_errors.(
35183525
Server_error
35193526
( invalid_ntp_config
3520-
, ["Can't set ntp_custom_servers empty when ntp_mode is custom"]
3527+
, ["Can't set ntp_custom_servers empty when ntp_mode is Custom"]
35213528
)
35223529
)
3523-
| `ntp_mode_custom, servers ->
3530+
| `Custom, servers ->
35243531
Xapi_host_ntp.set_servers_in_conf servers ;
35253532
Xapi_host_ntp.restart_ntp_service () ;
35263533
Db.Host.set_ntp_custom_servers ~__context ~self ~value
35273534
| _ ->
35283535
Db.Host.set_ntp_custom_servers ~__context ~self ~value
35293536

3530-
let enable_ntp ~__context ~self =
3531-
Xapi_host_ntp.enable_ntp_service () ;
3532-
Db.Host.set_ntp_enabled ~__context ~self ~value:true
3533-
3534-
let disable_ntp ~__context ~self =
3535-
Xapi_host_ntp.disable_ntp_service () ;
3536-
Db.Host.set_ntp_enabled ~__context ~self ~value:false
3537-
35383537
let sync_ntp_config ~__context ~host =
35393538
Xapi_host_ntp.promote_legacy_default_servers () ;
35403539
let servers = Xapi_host_ntp.get_servers_from_conf () in
35413540
let is_ntp_dhcp_enabled = Xapi_host_ntp.is_ntp_dhcp_enabled () in
3541+
let ntp_enabled = Xapi_host_ntp.is_ntp_service_active () in
35423542
let ntp_mode =
3543-
match (is_ntp_dhcp_enabled, servers) with
3544-
| true, _ ->
3545-
`ntp_mode_dhcp
3546-
| false, s
3543+
match (ntp_enabled, is_ntp_dhcp_enabled, servers) with
3544+
| false, _, _ ->
3545+
`Disabled
3546+
| _, true, _ ->
3547+
`DHCP
3548+
| _, false, s
35473549
when Xapi_stdext_std.Listext.List.set_equiv s
3548-
!Xapi_globs.default_ntp_servers ->
3549-
`ntp_mode_default
3550-
| false, _ ->
3551-
`ntp_mode_custom
3550+
!Xapi_globs.factory_ntp_servers ->
3551+
`Factory
3552+
| _, false, _ ->
3553+
`Custom
35523554
in
35533555
Db.Host.set_ntp_mode ~__context ~self:host ~value:ntp_mode ;
3554-
if ntp_mode = `ntp_mode_custom then
3555-
Db.Host.set_ntp_custom_servers ~__context ~self:host ~value:servers ;
3556-
let ntp_enabled = Xapi_host_ntp.is_ntp_service_active () in
3557-
Db.Host.set_ntp_enabled ~__context ~self:host ~value:ntp_enabled
3556+
if ntp_mode = `Custom then
3557+
Db.Host.set_ntp_custom_servers ~__context ~self:host ~value:servers
35583558

35593559
let get_ntp_servers_status ~__context ~self:_ =
35603560
if Xapi_host_ntp.is_ntp_service_active () then

ocaml/xapi/xapi_host.mli

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,6 @@ val set_ntp_mode :
619619
val set_ntp_custom_servers :
620620
__context:Context.t -> self:API.ref_host -> value:string list -> unit
621621

622-
val enable_ntp : __context:Context.t -> self:API.ref_host -> unit
623-
624-
val disable_ntp : __context:Context.t -> self:API.ref_host -> unit
625-
626622
val sync_ntp_config : __context:Context.t -> host:API.ref_host -> unit
627623

628624
val get_ntp_servers_status :

0 commit comments

Comments
 (0)