Skip to content

Commit fd5cc88

Browse files
authored
Merge SSH auto-mode feature to next (#6601)
2 parents 7a444fe + 07bf863 commit fd5cc88

17 files changed

+215
-22
lines changed

ocaml/idl/datamodel_errors.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,9 @@ let _ =
20422042
error Api_errors.set_console_timeout_partially_failed ["hosts"]
20432043
~doc:"Some hosts failed to set console timeout." () ;
20442044

2045+
error Api_errors.set_ssh_auto_mode_partially_failed ["hosts"]
2046+
~doc:"Some hosts failed to set SSH auto mode." () ;
2047+
20452048
error Api_errors.host_driver_no_hardware ["driver variant"]
20462049
~doc:"No hardware present for this host driver variant" () ;
20472050

ocaml/idl/datamodel_host.ml

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,13 @@ let create_params =
13351335
; param_release= numbered_release "25.21.0"
13361336
; param_default= Some (VInt Constants.default_console_idle_timeout)
13371337
}
1338+
; {
1339+
param_type= Bool
1340+
; param_name= "ssh_auto_mode"
1341+
; param_doc= "True if SSH auto mode is enabled for the host"
1342+
; param_release= numbered_release "25.26.0-next"
1343+
; param_default= Some (VBool Constants.default_ssh_auto_mode)
1344+
}
13381345
]
13391346

13401347
let create =
@@ -1350,8 +1357,8 @@ let create =
13501357
; ( Changed
13511358
, "25.21.0"
13521359
, "Added --ssh_enabled --ssh_enabled_timeout --ssh_expiry \
1353-
--console_idle_timeout options to allow them to be configured for \
1354-
new host"
1360+
--console_idle_timeout --ssh_auto_mode options to allow them to be \
1361+
configured for new host"
13551362
)
13561363
]
13571364
~versioned_params:create_params ~doc:"Create a new host record"
@@ -2440,6 +2447,21 @@ let set_console_idle_timeout =
24402447
]
24412448
~allowed_roles:_R_POOL_ADMIN ()
24422449

2450+
let set_ssh_auto_mode =
2451+
call ~name:"set_ssh_auto_mode" ~lifecycle:[]
2452+
~doc:"Set the SSH auto mode for the host"
2453+
~params:
2454+
[
2455+
(Ref _host, "self", "The host")
2456+
; ( Bool
2457+
, "value"
2458+
, "The SSH auto mode for the host,when set to true, SSH to normally be \
2459+
disabled and SSH to be enabled only in case of emergency e.g., xapi \
2460+
is down"
2461+
)
2462+
]
2463+
~allowed_roles:_R_POOL_ADMIN ()
2464+
24432465
let latest_synced_updates_applied_state =
24442466
Enum
24452467
( "latest_synced_updates_applied_state"
@@ -2601,6 +2623,7 @@ let t =
26012623
; disable_ssh
26022624
; set_ssh_enabled_timeout
26032625
; set_console_idle_timeout
2626+
; set_ssh_auto_mode
26042627
]
26052628
~contents:
26062629
([
@@ -3056,6 +3079,10 @@ let t =
30563079
"console_idle_timeout"
30573080
"The timeout in seconds after which idle console will be \
30583081
automatically terminated (0 means never)"
3082+
; field ~qualifier:DynamicRO ~lifecycle:[] ~ty:Bool
3083+
~default_value:(Some (VBool Constants.default_ssh_auto_mode))
3084+
"ssh_auto_mode"
3085+
"Reflects whether SSH auto mode is enabled for the host"
30593086
]
30603087
)
30613088
()

ocaml/idl/datamodel_pool.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,21 @@ let set_console_idle_timeout =
16061606
]
16071607
~allowed_roles:_R_POOL_ADMIN ()
16081608

1609+
let set_ssh_auto_mode =
1610+
call ~name:"set_ssh_auto_mode" ~lifecycle:[]
1611+
~doc:"Set the SSH auto mode for all hosts in the pool"
1612+
~params:
1613+
[
1614+
(Ref _pool, "self", "The pool")
1615+
; ( Bool
1616+
, "value"
1617+
, "The SSH auto mode for all hosts in the pool,when set to true, SSH \
1618+
to normally be disabled and SSH to be enabled only in case of \
1619+
emergency e.g., xapi is down"
1620+
)
1621+
]
1622+
~allowed_roles:_R_POOL_ADMIN ()
1623+
16091624
(** A pool class *)
16101625
let t =
16111626
create_obj ~in_db:true
@@ -1704,6 +1719,7 @@ let t =
17041719
; disable_ssh
17051720
; set_ssh_enabled_timeout
17061721
; set_console_idle_timeout
1722+
; set_ssh_auto_mode
17071723
]
17081724
~contents:
17091725
([

ocaml/idl/schematest.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ let hash x = Digest.string x |> Digest.to_hex
33
(* BEWARE: if this changes, check that schema has been bumped accordingly in
44
ocaml/idl/datamodel_common.ml, usually schema_minor_vsn *)
55

6-
let last_known_schema_hash = "9cd32d98d092440c36617546a3d995bd"
6+
let last_known_schema_hash = "7586cb039918e573594fc358e90b0f04"
77

88
let current_schema_hash : string =
99
let open Datamodel_types in

ocaml/tests/common/test_common.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ let make_host ~__context ?(uuid = make_uuid ()) ?(name_label = "host")
172172
?(local_cache_sr = Ref.null) ?(chipset_info = []) ?(ssl_legacy = false)
173173
?(last_software_update = Date.epoch) ?(last_update_hash = "")
174174
?(ssh_enabled = true) ?(ssh_enabled_timeout = 0L) ?(ssh_expiry = Date.epoch)
175-
?(console_idle_timeout = 0L) () =
175+
?(console_idle_timeout = 0L) ?(ssh_auto_mode = false) () =
176176
let host =
177177
Xapi_host.create ~__context ~uuid ~name_label ~name_description ~hostname
178178
~address ~external_auth_type ~external_auth_service_name
179179
~external_auth_configuration ~license_params ~edition ~license_server
180180
~local_cache_sr ~chipset_info ~ssl_legacy ~last_software_update
181181
~last_update_hash ~ssh_enabled ~ssh_enabled_timeout ~ssh_expiry
182-
~console_idle_timeout
182+
~console_idle_timeout ~ssh_auto_mode
183183
in
184184
Db.Host.set_cpu_info ~__context ~self:host ~value:default_cpu_info ;
185185
host
@@ -219,7 +219,7 @@ let make_host2 ~__context ?(ref = Ref.make ()) ?(uuid = make_uuid ())
219219
~recommended_guidances:[] ~latest_synced_updates_applied:`unknown
220220
~pending_guidances_recommended:[] ~pending_guidances_full:[]
221221
~last_update_hash:"" ~ssh_enabled:true ~ssh_enabled_timeout:0L
222-
~ssh_expiry:Date.epoch ~console_idle_timeout:0L ;
222+
~ssh_expiry:Date.epoch ~console_idle_timeout:0L ~ssh_auto_mode:false ;
223223
ref
224224

225225
let make_pif ~__context ~network ~host ?(device = "eth0")

ocaml/tests/test_host.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let add_host __context name =
2525
~local_cache_sr:Ref.null ~chipset_info:[] ~ssl_legacy:false
2626
~last_software_update:Clock.Date.epoch ~last_update_hash:""
2727
~ssh_enabled:true ~ssh_enabled_timeout:0L ~ssh_expiry:Clock.Date.epoch
28-
~console_idle_timeout:0L
28+
~console_idle_timeout:0L ~ssh_auto_mode:false
2929
)
3030

3131
(* Creates an unlicensed pool with the maximum number of hosts *)

ocaml/xapi-cli-server/records.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,17 @@ let pool_record rpc session_id pool =
15841584
~value:(safe_i64_of_string "console-idle-timeout" value)
15851585
)
15861586
()
1587+
; make_field ~name:"ssh-auto-mode"
1588+
~get:(fun () ->
1589+
get_consistent_field_or_default ~rpc ~session_id
1590+
~getter:Client.Host.get_ssh_auto_mode ~transform:string_of_bool
1591+
~default:inconsistent
1592+
)
1593+
~set:(fun value ->
1594+
Client.Pool.set_ssh_auto_mode ~rpc ~session_id ~self:pool
1595+
~value:(safe_bool_of_string "ssh-auto-mode" value)
1596+
)
1597+
()
15871598
]
15881599
}
15891600

@@ -3375,6 +3386,13 @@ let host_record rpc session_id host =
33753386
~value:(safe_i64_of_string "console-idle-timeout" value)
33763387
)
33773388
()
3389+
; make_field ~name:"ssh-auto-mode"
3390+
~get:(fun () -> string_of_bool (x ()).API.host_ssh_auto_mode)
3391+
~set:(fun value ->
3392+
Client.Host.set_ssh_auto_mode ~rpc ~session_id ~self:host
3393+
~value:(safe_bool_of_string "ssh-auto-mode" value)
3394+
)
3395+
()
33783396
]
33793397
}
33803398

ocaml/xapi-consts/api_errors.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,9 @@ let set_ssh_timeout_partially_failed =
14271427
let set_console_timeout_partially_failed =
14281428
add_error "SET_CONSOLE_TIMEOUT_PARTIALLY_FAILED"
14291429

1430+
let set_ssh_auto_mode_partially_failed =
1431+
add_error "SET_SSH_AUTO_MODE_PARTIALLY_FAILED"
1432+
14301433
let host_driver_no_hardware = add_error "HOST_DRIVER_NO_HARDWARE"
14311434

14321435
let tls_verification_not_enabled_in_pool =

ocaml/xapi-consts/constants.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,5 @@ let default_ssh_enabled = true
442442
let default_ssh_enabled_timeout = 0L
443443

444444
let default_console_idle_timeout = 0L
445+
446+
let default_ssh_auto_mode = false

ocaml/xapi/dbsync_slave.ml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ let create_localhost ~__context info =
6464
~ssh_enabled_timeout:Constants.default_ssh_enabled_timeout
6565
~ssh_expiry:Date.epoch
6666
~console_idle_timeout:Constants.default_console_idle_timeout
67+
~ssh_auto_mode:!Xapi_globs.ssh_auto_mode_default
6768
in
6869
()
6970

@@ -383,7 +384,29 @@ let update_env __context sync_keys =
383384
switched_sync Xapi_globs.sync_ssh_status (fun () ->
384385
let ssh_service = !Xapi_globs.ssh_service in
385386
let status = Fe_systemctl.is_active ~service:ssh_service in
386-
Db.Host.set_ssh_enabled ~__context ~self:localhost ~value:status
387+
Db.Host.set_ssh_enabled ~__context ~self:localhost ~value:status ;
388+
let auto_mode_in_db =
389+
Db.Host.get_ssh_auto_mode ~__context ~self:localhost
390+
in
391+
let ssh_monitor_enabled =
392+
Fe_systemctl.is_active ~service:!Xapi_globs.ssh_monitor_service
393+
in
394+
(* For xs9 when fresh install, the ssh_monitor service is not enabled by default.
395+
If the auto_mode is enabled, we need to enable the ssh_monitor service.
396+
and user may have disabled monitor service by mistake as well, so we need to check the status. *)
397+
if auto_mode_in_db <> ssh_monitor_enabled then
398+
Xapi_host.set_ssh_auto_mode ~__context ~self:localhost
399+
~value:auto_mode_in_db ;
400+
let console_timeout =
401+
Db.Host.get_console_idle_timeout ~__context ~self:localhost
402+
in
403+
let console_timeout_file_exists =
404+
Sys.file_exists !Xapi_globs.console_timeout_profile_path
405+
in
406+
(* Ensure the console timeout profile file exists if the timeout is configured *)
407+
if console_timeout > 0L && not console_timeout_file_exists then
408+
Xapi_host.set_console_idle_timeout ~__context ~self:localhost
409+
~value:console_timeout
387410
) ;
388411

389412
remove_pending_guidances ~__context

0 commit comments

Comments
 (0)