Skip to content

Commit 137eebb

Browse files
committed
CP-53725 Create SSH-related xe CLI for Dom0 SSH control
Updated `records.ml` to support `host-param-set/get/list` and `pool-param-set/get/list` for SSH-related fields. Signed-off-by: Lunfan Zhang <[email protected]>
1 parent 2da29e2 commit 137eebb

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

ocaml/xapi-cli-server/records.ml

+87
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ let nullref = Ref.string_of Ref.null
2020

2121
let nid = "<not in database>"
2222

23+
let inconsistent = "<inconsistent>"
24+
2325
let unknown_time = "<unknown time>"
2426

2527
let string_of_float f = Printf.sprintf "%.3f" f
@@ -204,6 +206,31 @@ let get_pbds_host rpc session_id pbds =
204206
let get_sr_host rpc session_id record =
205207
get_pbds_host rpc session_id record.API.sR_PBDs
206208

209+
(** Get a field from the first host, or return a default value if the field
210+
is not the same on all hosts. *)
211+
let get_consistent_field_or_default ~rpc ~session_id ~getter ~transform ~default
212+
=
213+
match Client.Host.get_all ~rpc ~session_id with
214+
| [] ->
215+
default
216+
| hosts -> (
217+
let result =
218+
List.fold_left
219+
(fun acc host ->
220+
match acc with
221+
| `Inconsistent ->
222+
`Inconsistent
223+
| `NotSet ->
224+
`Value (getter ~rpc ~session_id ~self:host |> transform)
225+
| `Value v ->
226+
let current = getter ~rpc ~session_id ~self:host |> transform in
227+
if v = current then `Value v else `Inconsistent
228+
)
229+
`NotSet hosts
230+
in
231+
match result with `Value v -> v | _ -> default
232+
)
233+
207234
let bond_record rpc session_id bond =
208235
let _ref = ref bond in
209236
let empty_record =
@@ -1506,6 +1533,44 @@ let pool_record rpc session_id pool =
15061533
)
15071534
~get_map:(fun () -> (x ()).API.pool_license_server)
15081535
()
1536+
; make_field ~name:"ssh-enabled"
1537+
~get:(fun () ->
1538+
get_consistent_field_or_default ~rpc ~session_id
1539+
~getter:Client.Host.get_ssh_enabled ~transform:string_of_bool
1540+
~default:inconsistent
1541+
)
1542+
()
1543+
; make_field ~name:"ssh-enabled-timeout"
1544+
~get:(fun () ->
1545+
get_consistent_field_or_default ~rpc ~session_id
1546+
~getter:Client.Host.get_ssh_enabled_timeout
1547+
~transform:Int64.to_string ~default:inconsistent
1548+
)
1549+
~set:(fun value ->
1550+
let minutes = safe_i64_of_string "ssh-enabled-timeout" value in
1551+
let seconds = Int64.mul minutes 60L in
1552+
Client.Pool.set_ssh_enabled_timeout ~rpc ~session_id ~self:pool
1553+
~value:seconds
1554+
)
1555+
()
1556+
; make_field ~name:"ssh-expiry"
1557+
~get:(fun () ->
1558+
get_consistent_field_or_default ~rpc ~session_id
1559+
~getter:Client.Host.get_ssh_expiry ~transform:Date.to_rfc3339
1560+
~default:inconsistent
1561+
)
1562+
()
1563+
; make_field ~name:"console-idle-timeout"
1564+
~get:(fun () ->
1565+
get_consistent_field_or_default ~rpc ~session_id
1566+
~getter:Client.Host.get_console_idle_timeout
1567+
~transform:Int64.to_string ~default:inconsistent
1568+
)
1569+
~set:(fun value ->
1570+
Client.Pool.set_console_idle_timeout ~rpc ~session_id ~self:pool
1571+
~value:(Int64.of_string value)
1572+
)
1573+
()
15091574
]
15101575
}
15111576

@@ -3265,6 +3330,28 @@ let host_record rpc session_id host =
32653330
; make_field ~name:"last-update-hash"
32663331
~get:(fun () -> (x ()).API.host_last_update_hash)
32673332
()
3333+
; make_field ~name:"ssh-enabled"
3334+
~get:(fun () -> string_of_bool (x ()).API.host_ssh_enabled)
3335+
()
3336+
; make_field ~name:"ssh-enabled-timeout"
3337+
~get:(fun () -> Int64.to_string (x ()).API.host_ssh_enabled_timeout)
3338+
~set:(fun value ->
3339+
let minutes = safe_i64_of_string "ssh-enabled-timeout" value in
3340+
let seconds = Int64.mul minutes 60L in
3341+
Client.Host.set_ssh_enabled_timeout ~rpc ~session_id ~self:host
3342+
~value:seconds
3343+
)
3344+
()
3345+
; make_field ~name:"ssh-expiry"
3346+
~get:(fun () -> Date.to_rfc3339 (x ()).API.host_ssh_expiry)
3347+
()
3348+
; make_field ~name:"console-idle-timeout"
3349+
~get:(fun () -> Int64.to_string (x ()).API.host_console_idle_timeout)
3350+
~set:(fun value ->
3351+
Client.Host.set_console_idle_timeout ~rpc ~session_id ~self:host
3352+
~value:(safe_i64_of_string "console-idle-timeout" value)
3353+
)
3354+
()
32683355
]
32693356
}
32703357

0 commit comments

Comments
 (0)