Skip to content

Commit 9ef7380

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 9ef7380

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-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: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,18 +3112,43 @@ 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+
if value then (
3124+
Xapi_systemctl.enable ~wait_until_success:false
3125+
!Xapi_globs.ssh_monitor_service ;
3126+
Xapi_systemctl.start ~wait_until_success:false
3127+
!Xapi_globs.ssh_monitor_service
3128+
) else (
3129+
Xapi_systemctl.stop ~wait_until_success:false
3130+
!Xapi_globs.ssh_monitor_service ;
3131+
Xapi_systemctl.disable ~wait_until_success:false
3132+
!Xapi_globs.ssh_monitor_service
3133+
)
3134+
with e ->
3135+
error "Failed to configure SSH auto mode: %s" (Printexc.to_string e) ;
3136+
Helpers.internal_error "Failed to configure SSH auto mode: %s"
3137+
(Printexc.to_string e)
3138+
31153139
let disable_ssh_internal ~__context ~self =
31163140
try
31173141
debug "Disabling SSH for host %s" (Helpers.get_localhost_uuid ()) ;
3118-
Xapi_systemctl.disable ~wait_until_success:false !Xapi_globs.ssh_service ;
3142+
if Db.Host.get_ssh_auto_mode ~__context ~self = false then
3143+
Xapi_systemctl.disable ~wait_until_success:false !Xapi_globs.ssh_service ;
31193144
Xapi_systemctl.stop ~wait_until_success:false !Xapi_globs.ssh_service ;
31203145
Db.Host.set_ssh_enabled ~__context ~self ~value:false
31213146
with e ->
31223147
error "Failed to disable SSH for host %s: %s" (Ref.string_of self)
31233148
(Printexc.to_string e) ;
31243149
Helpers.internal_error "Failed to disable SSH: %s" (Printexc.to_string e)
31253150

3126-
let schedule_disable_ssh_job ~__context ~self ~timeout =
3151+
let schedule_disable_ssh_job ~__context ~self ~timeout ~auto_mode =
31273152
let host_uuid = Helpers.get_localhost_uuid () in
31283153
let expiry_time =
31293154
match
@@ -3152,7 +3177,11 @@ let schedule_disable_ssh_job ~__context ~self ~timeout =
31523177
Xapi_stdext_threads_scheduler.Scheduler.add_to_queue
31533178
!Xapi_globs.job_for_disable_ssh
31543179
Xapi_stdext_threads_scheduler.Scheduler.OneShot (Int64.to_float timeout)
3155-
(fun () -> disable_ssh_internal ~__context ~self
3180+
(fun () ->
3181+
disable_ssh_internal ~__context ~self ;
3182+
(* re-enable SSH auto mode if it was enabled before calling host.enable_ssh *)
3183+
if auto_mode then
3184+
set_ssh_auto_mode ~__context ~self ~value:true
31563185
) ;
31573186

31583187
Db.Host.set_ssh_expiry ~__context ~self ~value:expiry_time
@@ -3161,6 +3190,10 @@ let enable_ssh ~__context ~self =
31613190
try
31623191
debug "Enabling SSH for host %s" (Helpers.get_localhost_uuid ()) ;
31633192

3193+
let cached_ssh_auto_mode = Db.Host.get_ssh_auto_mode ~__context ~self in
3194+
(* Disable SSH auto mode when SSH is enabled manually *)
3195+
set_ssh_auto_mode ~__context ~self ~value:false ;
3196+
31643197
Xapi_systemctl.enable ~wait_until_success:false !Xapi_globs.ssh_service ;
31653198
Xapi_systemctl.start ~wait_until_success:false !Xapi_globs.ssh_service ;
31663199

@@ -3171,6 +3204,7 @@ let enable_ssh ~__context ~self =
31713204
!Xapi_globs.job_for_disable_ssh
31723205
| t ->
31733206
schedule_disable_ssh_job ~__context ~self ~timeout:t
3207+
~auto_mode:cached_ssh_auto_mode
31743208
) ;
31753209

31763210
Db.Host.set_ssh_enabled ~__context ~self ~value:true
@@ -3208,7 +3242,7 @@ let set_ssh_enabled_timeout ~__context ~self ~value =
32083242
!Xapi_globs.job_for_disable_ssh ;
32093243
Db.Host.set_ssh_expiry ~__context ~self ~value:Date.epoch
32103244
| t ->
3211-
schedule_disable_ssh_job ~__context ~self ~timeout:t
3245+
schedule_disable_ssh_job ~__context ~self ~timeout:t ~auto_mode:false
32123246

32133247
let set_console_idle_timeout ~__context ~self ~value =
32143248
let assert_timeout_valid timeout =
@@ -3243,5 +3277,3 @@ let set_console_idle_timeout ~__context ~self ~value =
32433277
error "Failed to configure console timeout: %s" (Printexc.to_string e) ;
32443278
Helpers.internal_error "Failed to set console timeout: %Ld: %s" value
32453279
(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)