Skip to content

Commit 7e5c1ea

Browse files
committed
CP-53721 Implement SSH set auto mode API for Dom0 SSH control
Implemented XAPI APIs for SSH auto mode configuration: - `host.set_ssh_auto_mode`: Configures SSH auto mode for a specific host. - `pool.set_ssh_auto_mode`: Configures SSH auto mode for all hosts in the pool. Additionally: - `host.enable_ssh` now automatically sets SSH auto mode to `false`. Signed-off-by: Lunfan Zhang <[email protected]>
1 parent f1a993e commit 7e5c1ea

File tree

5 files changed

+58
-8
lines changed

5 files changed

+58
-8
lines changed

ocaml/xapi/xapi_globs.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,8 @@ let job_for_disable_ssh = ref "Disable SSH"
12971297

12981298
let ssh_service = ref "sshd"
12991299

1300+
let ssh_monitor_service = ref "xapi-ssh-monitor"
1301+
13001302
(* Fingerprint of default patch key *)
13011303
let citrix_patch_key =
13021304
"NERDNTUzMDMwRUMwNDFFNDI4N0M4OEVCRUFEMzlGOTJEOEE5REUyNg=="

ocaml/xapi/xapi_host.ml

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,18 +3112,47 @@ let emergency_clear_mandatory_guidance ~__context =
31123112
) ;
31133113
Db.Host.set_pending_guidances ~__context ~self ~value:[]
31143114

3115+
let set_ssh_auto_mode ~__context ~self ~value =
3116+
debug "Setting SSH auto mode for host %s to %B"
3117+
(Helpers.get_localhost_uuid ())
3118+
value ;
3119+
3120+
Db.Host.set_ssh_auto_mode ~__context ~self ~value ;
3121+
3122+
try
3123+
(* When enabled, the ssh_monitor_service regularly checks XAPI status to manage SSH availability.
3124+
During normal operation when XAPI is running properly, SSH is automatically disabled.
3125+
SSH is only enabled during emergency scenarios
3126+
(e.g., when XAPI is down) to allow administrative access for troubleshooting. *)
3127+
if value then (
3128+
Xapi_systemctl.enable ~wait_until_success:false
3129+
!Xapi_globs.ssh_monitor_service ;
3130+
Xapi_systemctl.start ~wait_until_success:false
3131+
!Xapi_globs.ssh_monitor_service
3132+
) else (
3133+
Xapi_systemctl.stop ~wait_until_success:false
3134+
!Xapi_globs.ssh_monitor_service ;
3135+
Xapi_systemctl.disable ~wait_until_success:false
3136+
!Xapi_globs.ssh_monitor_service
3137+
)
3138+
with e ->
3139+
error "Failed to configure SSH auto mode: %s" (Printexc.to_string e) ;
3140+
Helpers.internal_error "Failed to configure SSH auto mode: %s"
3141+
(Printexc.to_string e)
3142+
31153143
let disable_ssh_internal ~__context ~self =
31163144
try
31173145
debug "Disabling SSH for host %s" (Helpers.get_localhost_uuid ()) ;
3118-
Xapi_systemctl.disable ~wait_until_success:false !Xapi_globs.ssh_service ;
3146+
if not (Db.Host.get_ssh_auto_mode ~__context ~self) then
3147+
Xapi_systemctl.disable ~wait_until_success:false !Xapi_globs.ssh_service ;
31193148
Xapi_systemctl.stop ~wait_until_success:false !Xapi_globs.ssh_service ;
31203149
Db.Host.set_ssh_enabled ~__context ~self ~value:false
31213150
with e ->
31223151
error "Failed to disable SSH for host %s: %s" (Ref.string_of self)
31233152
(Printexc.to_string e) ;
31243153
Helpers.internal_error "Failed to disable SSH: %s" (Printexc.to_string e)
31253154

3126-
let schedule_disable_ssh_job ~__context ~self ~timeout =
3155+
let schedule_disable_ssh_job ~__context ~self ~timeout ~auto_mode =
31273156
let host_uuid = Helpers.get_localhost_uuid () in
31283157
let expiry_time =
31293158
match
@@ -3152,7 +3181,11 @@ let schedule_disable_ssh_job ~__context ~self ~timeout =
31523181
Xapi_stdext_threads_scheduler.Scheduler.add_to_queue
31533182
!Xapi_globs.job_for_disable_ssh
31543183
Xapi_stdext_threads_scheduler.Scheduler.OneShot (Int64.to_float timeout)
3155-
(fun () -> disable_ssh_internal ~__context ~self
3184+
(fun () ->
3185+
disable_ssh_internal ~__context ~self ;
3186+
(* re-enable SSH auto mode if it was enabled before calling host.enable_ssh *)
3187+
if auto_mode then
3188+
set_ssh_auto_mode ~__context ~self ~value:true
31563189
) ;
31573190

31583191
Db.Host.set_ssh_expiry ~__context ~self ~value:expiry_time
@@ -3161,6 +3194,10 @@ let enable_ssh ~__context ~self =
31613194
try
31623195
debug "Enabling SSH for host %s" (Helpers.get_localhost_uuid ()) ;
31633196

3197+
let cached_ssh_auto_mode = Db.Host.get_ssh_auto_mode ~__context ~self in
3198+
(* Disable SSH auto mode when SSH is enabled manually *)
3199+
set_ssh_auto_mode ~__context ~self ~value:false ;
3200+
31643201
Xapi_systemctl.enable ~wait_until_success:false !Xapi_globs.ssh_service ;
31653202
Xapi_systemctl.start ~wait_until_success:false !Xapi_globs.ssh_service ;
31663203

@@ -3171,6 +3208,7 @@ let enable_ssh ~__context ~self =
31713208
!Xapi_globs.job_for_disable_ssh
31723209
| t ->
31733210
schedule_disable_ssh_job ~__context ~self ~timeout:t
3211+
~auto_mode:cached_ssh_auto_mode
31743212
) ;
31753213

31763214
Db.Host.set_ssh_enabled ~__context ~self ~value:true
@@ -3208,7 +3246,7 @@ let set_ssh_enabled_timeout ~__context ~self ~value =
32083246
!Xapi_globs.job_for_disable_ssh ;
32093247
Db.Host.set_ssh_expiry ~__context ~self ~value:Date.epoch
32103248
| t ->
3211-
schedule_disable_ssh_job ~__context ~self ~timeout:t
3249+
schedule_disable_ssh_job ~__context ~self ~timeout:t ~auto_mode:false
32123250

32133251
let set_console_idle_timeout ~__context ~self ~value =
32143252
let assert_timeout_valid timeout =
@@ -3243,5 +3281,3 @@ let set_console_idle_timeout ~__context ~self ~value =
32433281
error "Failed to configure console timeout: %s" (Printexc.to_string e) ;
32443282
Helpers.internal_error "Failed to set console timeout: %Ld: %s" value
32453283
(Printexc.to_string e)
3246-
3247-
let set_ssh_auto_mode ~__context ~self:_ ~value:_ = ()

ocaml/xapi/xapi_host.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,11 @@ val set_console_idle_timeout :
580580
__context:Context.t -> self:API.ref_host -> value:int64 -> unit
581581

582582
val schedule_disable_ssh_job :
583-
__context:Context.t -> self:API.ref_host -> timeout:int64 -> unit
583+
__context:Context.t
584+
-> self:API.ref_host
585+
-> timeout:int64
586+
-> auto_mode:bool
587+
-> unit
584588

585589
val set_ssh_auto_mode :
586590
__context:Context.t -> self:API.ref_host -> value:bool -> unit

ocaml/xapi/xapi_periodic_scheduler_init.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ let register ~__context =
9090
if Int64.compare expiry_time current_time > 0 then
9191
let remaining = Int64.sub expiry_time current_time in
9292
Xapi_host.schedule_disable_ssh_job ~__context ~self ~timeout:remaining
93+
~auto_mode:true
9394
(* handle the case where XAPI is not active when the SSH timeout expires *)
9495
else if Fe_systemctl.is_active ~service:!Xapi_globs.ssh_service then
9596
Xapi_host.disable_ssh ~__context ~self

ocaml/xapi/xapi_pool.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4071,6 +4071,13 @@ module Ssh = struct
40714071
Client.Host.set_console_idle_timeout ~rpc ~session_id ~self ~value
40724072
)
40734073
~error:Api_errors.set_console_timeout_partially_failed
4074+
4075+
let set_ssh_auto_mode ~__context ~self:_ ~value =
4076+
operate ~__context
4077+
~action:(fun ~rpc ~session_id ~self ->
4078+
Client.Host.set_ssh_auto_mode ~rpc ~session_id ~self ~value
4079+
)
4080+
~error:Api_errors.set_ssh_auto_mode_partially_failed
40744081
end
40754082

40764083
let enable_ssh = Ssh.enable
@@ -4081,4 +4088,4 @@ let set_ssh_enabled_timeout = Ssh.set_enabled_timeout
40814088

40824089
let set_console_idle_timeout = Ssh.set_console_timeout
40834090

4084-
let set_ssh_auto_mode ~__context ~self:_ ~value:_ = ()
4091+
let set_ssh_auto_mode = Ssh.set_ssh_auto_mode

0 commit comments

Comments
 (0)