Skip to content

Commit 387355a

Browse files
committed
CP-53723 Implement Console timeout configure API for Dom0 SSH control
Implemented XAPI APIs: - `set_console_idle_timeout` This API allow XAPI to configure timeouts for the idle console sessions. Signed-off-by: Lunfan Zhang <[email protected]>
1 parent 2f00a21 commit 387355a

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

ocaml/idl/datamodel_errors.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,9 @@ let _ =
20402040
error Api_errors.disable_ssh_partially_failed ["hosts"]
20412041
~doc:"Some of hosts failed to disable SSH access." () ;
20422042

2043+
error Api_errors.set_console_timeout_partially_failed ["hosts"]
2044+
~doc:"Some hosts failed to set console timeout." () ;
2045+
20432046
error Api_errors.host_driver_no_hardware ["driver variant"]
20442047
~doc:"No hardware present for this host driver variant" () ;
20452048

ocaml/xapi-consts/api_errors.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,9 @@ let enable_ssh_partially_failed = add_error "ENABLE_SSH_PARTIALLY_FAILED"
14201420

14211421
let disable_ssh_partially_failed = add_error "DISABLE_SSH_PARTIALLY_FAILED"
14221422

1423+
let set_console_timeout_partially_failed =
1424+
add_error "SET_CONSOLE_TIMEOUT_PARTIALLY_FAILED"
1425+
14231426
let host_driver_no_hardware = add_error "HOST_DRIVER_NO_HARDWARE"
14241427

14251428
let tls_verification_not_enabled_in_pool =

ocaml/xapi/xapi_host.ml

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3136,4 +3136,54 @@ let disable_ssh ~__context ~self =
31363136

31373137
let set_ssh_enabled_timeout ~__context ~self:_ ~value:_ = ()
31383138

3139-
let set_console_idle_timeout ~__context ~self:_ ~value:_ = ()
3139+
let set_console_idle_timeout ~__context ~self ~value =
3140+
let validate_timeout = function
3141+
| timeout when timeout >= 0L ->
3142+
timeout
3143+
| timeout ->
3144+
raise
3145+
(Api_errors.Server_error
3146+
( Api_errors.invalid_value
3147+
, [
3148+
"console_timeout"
3149+
; Int64.to_string timeout
3150+
; "must be a positive integer"
3151+
]
3152+
)
3153+
)
3154+
in
3155+
3156+
let console_timeout = validate_timeout value in
3157+
let bashrc = "/root/.bashrc" in
3158+
3159+
try
3160+
let lines = Unixext.read_lines ~path:bashrc in
3161+
let filtered =
3162+
List.filter
3163+
(fun line -> not (String.starts_with ~prefix:"export TMOUT=" line))
3164+
lines
3165+
in
3166+
3167+
let new_lines =
3168+
match console_timeout with
3169+
| 0L ->
3170+
filtered
3171+
| timeout ->
3172+
filtered @ [Printf.sprintf "export TMOUT=%Ld" timeout]
3173+
in
3174+
let content = String.concat "\n" new_lines ^ "\n" in
3175+
3176+
Unixext.atomic_write_to_file bashrc 0o0644 (fun fd ->
3177+
Unix.write fd (Bytes.of_string content) 0 (String.length content)
3178+
|> ignore
3179+
) ;
3180+
3181+
Db.Host.set_console_idle_timeout ~__context ~self ~value:console_timeout
3182+
with e ->
3183+
error "Failed to configure console timeout: %s" (Printexc.to_string e) ;
3184+
raise
3185+
(Api_errors.Server_error
3186+
( Api_errors.set_console_idle_timeout_failed
3187+
, ["Failed to configure console timeout"; Printexc.to_string e]
3188+
)
3189+
)

ocaml/xapi/xapi_pool.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4003,6 +4003,13 @@ module Ssh = struct
40034003
let disable ~__context ~self:_ =
40044004
operate ~__context ~action:Client.Host.disable_ssh
40054005
~error:Api_errors.disable_ssh_partially_failed
4006+
4007+
let set_console_timeout ~__context ~self:_ ~value =
4008+
operate ~__context
4009+
~action:(fun ~rpc ~session_id ~self ->
4010+
Client.Host.set_console_idle_timeout ~rpc ~session_id ~self ~value
4011+
)
4012+
~error:Api_errors.set_console_timeout_partially_failed
40064013
end
40074014

40084015
let enable_ssh = Ssh.enable
@@ -4011,4 +4018,4 @@ let disable_ssh = Ssh.disable
40114018

40124019
let set_ssh_enabled_timeout ~__context ~self:_ ~value:_ = ()
40134020

4014-
let set_console_idle_timeout ~__context ~self:_ ~value:_ = ()
4021+
let set_console_idle_timeout = Ssh.set_console_timeout

0 commit comments

Comments
 (0)