@@ -3114,27 +3114,107 @@ let emergency_clear_mandatory_guidance ~__context =
31143114 ) ;
31153115 Db.Host. set_pending_guidances ~__context ~self ~value: []
31163116
3117- let enable_ssh ~__context ~self =
3117+ let disable_ssh_internal ~__context ~self =
31183118 try
3119- Xapi_systemctl. enable ~wait_until_success: false " sshd" ;
3120- Xapi_systemctl. start ~wait_until_success: false " sshd"
3121- with _ ->
3122- raise
3123- (Api_errors. Server_error
3124- (Api_errors. enable_ssh_failed, [Ref. string_of self])
3125- )
3119+ debug " Disabling SSH for host %s" (Helpers. get_localhost_uuid () ) ;
3120+ Xapi_systemctl. disable ~wait_until_success: false ! Xapi_globs. ssh_service ;
3121+ Xapi_systemctl. stop ~wait_until_success: false ! Xapi_globs. ssh_service ;
3122+ Db.Host. set_ssh_enabled ~__context ~self ~value: false
3123+ with e ->
3124+ error " Failed to disable SSH for host %s: %s" (Ref. string_of self)
3125+ (Printexc. to_string e) ;
3126+ Helpers. internal_error " Failed to disable SSH: %s" (Printexc. to_string e)
3127+
3128+ let schedule_disable_ssh_job ~__context ~self ~timeout =
3129+ let host_uuid = Helpers. get_localhost_uuid () in
3130+ let expiry_time =
3131+ match
3132+ Ptime. add_span (Ptime_clock. now () )
3133+ (Ptime.Span. of_int_s (Int64. to_int timeout))
3134+ with
3135+ | None ->
3136+ error " Invalid SSH timeout: %Ld" timeout ;
3137+ raise
3138+ (Api_errors. Server_error
3139+ ( Api_errors. invalid_value
3140+ , [" ssh_enabled_timeout" ; Int64. to_string timeout]
3141+ )
3142+ )
3143+ | Some t ->
3144+ Ptime. to_float_s t |> Date. of_unix_time
3145+ in
31263146
3127- let disable_ssh ~__context ~self =
3147+ debug " Scheduling SSH disable job for host %s with timeout %Ld seconds"
3148+ host_uuid timeout ;
3149+
3150+ (* Remove any existing job first *)
3151+ Xapi_stdext_threads_scheduler.Scheduler. remove_from_queue
3152+ ! Xapi_globs. job_for_disable_ssh ;
3153+
3154+ Xapi_stdext_threads_scheduler.Scheduler. add_to_queue
3155+ ! Xapi_globs. job_for_disable_ssh
3156+ Xapi_stdext_threads_scheduler.Scheduler. OneShot (Int64. to_float timeout)
3157+ (fun () -> disable_ssh_internal ~__context ~self
3158+ ) ;
3159+
3160+ Db.Host. set_ssh_expiry ~__context ~self ~value: expiry_time
3161+
3162+ let enable_ssh ~__context ~self =
31283163 try
3129- Xapi_systemctl. disable ~wait_until_success: false " sshd" ;
3130- Xapi_systemctl. stop ~wait_until_success: false " sshd"
3131- with _ ->
3132- raise
3133- (Api_errors. Server_error
3134- (Api_errors. disable_ssh_failed, [Ref. string_of self])
3135- )
3164+ debug " Enabling SSH for host %s" (Helpers. get_localhost_uuid () ) ;
3165+
3166+ Xapi_systemctl. enable ~wait_until_success: false ! Xapi_globs. ssh_service ;
3167+ Xapi_systemctl. start ~wait_until_success: false ! Xapi_globs. ssh_service ;
3168+
3169+ let timeout = Db.Host. get_ssh_enabled_timeout ~__context ~self in
3170+ ( match timeout with
3171+ | 0L ->
3172+ Xapi_stdext_threads_scheduler.Scheduler. remove_from_queue
3173+ ! Xapi_globs. job_for_disable_ssh
3174+ | t ->
3175+ schedule_disable_ssh_job ~__context ~self ~timeout: t
3176+ ) ;
31363177
3137- let set_ssh_enabled_timeout ~__context ~self :_ ~value :_ = ()
3178+ Db.Host. set_ssh_enabled ~__context ~self ~value: true
3179+ with e ->
3180+ error " Failed to enable SSH on host %s: %s" (Ref. string_of self)
3181+ (Printexc. to_string e) ;
3182+ Helpers. internal_error " Failed to enable SSH: %s" (Printexc. to_string e)
3183+
3184+ let disable_ssh ~__context ~self =
3185+ Xapi_stdext_threads_scheduler.Scheduler. remove_from_queue
3186+ ! Xapi_globs. job_for_disable_ssh ;
3187+ disable_ssh_internal ~__context ~self ;
3188+ Db.Host. set_ssh_expiry ~__context ~self ~value: (Date. now () )
3189+
3190+ let set_ssh_enabled_timeout ~__context ~self ~value =
3191+ let validate_timeout value =
3192+ (* the max timeout is two days: 172800L = 2*24*60*60 *)
3193+ if value < 0L || value > 172800L then
3194+ raise
3195+ (Api_errors. Server_error
3196+ ( Api_errors. invalid_value
3197+ , [" ssh_enabled_timeout" ; Int64. to_string value]
3198+ )
3199+ )
3200+ in
3201+ validate_timeout value ;
3202+ debug " Setting SSH timeout for host %s to %Ld seconds"
3203+ (Db.Host. get_uuid ~__context ~self )
3204+ value ;
3205+ Db.Host. set_ssh_enabled_timeout ~__context ~self ~value ;
3206+ match Db.Host. get_ssh_enabled ~__context ~self with
3207+ | false ->
3208+ ()
3209+ | true -> (
3210+ match value with
3211+ | 0L ->
3212+ Xapi_stdext_threads_scheduler.Scheduler. remove_from_queue
3213+ ! Xapi_globs. job_for_disable_ssh ;
3214+ Db.Host. set_ssh_expiry ~__context ~self ~value: Date. epoch
3215+ | t ->
3216+ schedule_disable_ssh_job ~__context ~self ~timeout: t
3217+ )
31383218
31393219let set_console_idle_timeout ~__context ~self ~value =
31403220 let assert_timeout_valid timeout =
0 commit comments