Skip to content

Commit 34b308c

Browse files
LunfanZhanggangj
authored andcommitted
CP-54138: Sync SSH status during XAPI startup
- Ensure host.enabled_ssh reflects the actual SSH service state on startup, in case it was manually changed by the user. - Reschedule the "disable SSH" job if: - host.ssh_enabled_timeout is set to a positive value, and - host.ssh_expiry is in the future. - Disable the SSH if: - host.ssh_enabled_timeout is set to a positive value, and - host.ssh_expiry is in the past. Signed-off-by: Lunfan Zhang <[email protected]>
1 parent 365cd74 commit 34b308c

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

ocaml/xapi/dbsync_slave.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,5 +380,10 @@ let update_env __context sync_keys =
380380
Create_misc.create_chipset_info ~__context info
381381
) ;
382382
switched_sync Xapi_globs.sync_gpus (fun () -> Xapi_pgpu.update_gpus ~__context) ;
383+
switched_sync Xapi_globs.sync_ssh_status (fun () ->
384+
let ssh_service = !Xapi_globs.ssh_service in
385+
let status = Fe_systemctl.is_active ~service:ssh_service in
386+
Db.Host.set_ssh_enabled ~__context ~self:localhost ~value:status
387+
) ;
383388

384389
remove_pending_guidances ~__context

ocaml/xapi/xapi_globs.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ let sync_bios_strings = "sync_bios_strings"
368368

369369
let sync_chipset_info = "sync_chipset_info"
370370

371+
let sync_ssh_status = "sync_ssh_status"
372+
371373
let sync_pci_devices = "sync_pci_devices"
372374

373375
let sync_gpus = "sync_gpus"

ocaml/xapi/xapi_host.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,3 +577,6 @@ val set_ssh_enabled_timeout :
577577

578578
val set_console_idle_timeout :
579579
__context:Context.t -> self:API.ref_host -> value:int64 -> unit
580+
581+
val schedule_disable_ssh_job :
582+
__context:Context.t -> self:API.ref_host -> timeout:int64 -> unit

ocaml/xapi/xapi_periodic_scheduler_init.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*)
1414
(** Periodic scheduler for background tasks. *)
1515

16+
module Date = Clock.Date
17+
1618
module D = Debug.Make (struct let name = "backgroundscheduler" end)
1719

1820
open D
@@ -73,6 +75,25 @@ let register ~__context =
7375
(fun __context -> Xapi_subject.update_all_subjects ~__context
7476
)
7577
in
78+
let sync_ssh_status ~__context =
79+
let self = Helpers.get_localhost ~__context in
80+
let timeout = Db.Host.get_ssh_enabled_timeout ~__context ~self in
81+
82+
if timeout > 0L then
83+
let expiry_time =
84+
Db.Host.get_ssh_expiry ~__context ~self
85+
|> Date.to_unix_time
86+
|> Int64.of_float
87+
in
88+
let current_time = Unix.time () |> Int64.of_float in
89+
90+
if Int64.compare expiry_time current_time > 0 then
91+
let remaining = Int64.sub expiry_time current_time in
92+
Xapi_host.schedule_disable_ssh_job ~__context ~self ~timeout:remaining
93+
(* handle the case where XAPI is not active when the SSH timeout expires *)
94+
else if Fe_systemctl.is_active ~service:!Xapi_globs.ssh_service then
95+
Xapi_host.disable_ssh ~__context ~self
96+
in
7697
let update_all_subjects_delay = 10.0 in
7798
(* initial delay = 10 seconds *)
7899
if master then
@@ -133,6 +154,7 @@ let register ~__context =
133154
"Check stunnel cache expiry"
134155
(Xapi_stdext_threads_scheduler.Scheduler.Periodic stunnel_period)
135156
stunnel_period Stunnel_cache.gc ;
157+
sync_ssh_status ~__context ;
136158
if
137159
master
138160
&& Db.Pool.get_update_sync_enabled ~__context

0 commit comments

Comments
 (0)