Skip to content

Commit

Permalink
CP-52074: Add start and stop ssh API on pool
Browse files Browse the repository at this point in the history
Signed-off-by: Bengang Yuan <[email protected]>
  • Loading branch information
BengangY committed Jan 10, 2025
1 parent 8de12f0 commit badbeaa
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ocaml/idl/datamodel_errors.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2016,6 +2016,12 @@ let _ =
error Api_errors.ssh_stop_failed ["host"] ~doc:"Failed to stop ssh service."
() ;

error Api_errors.ssh_start_partially_failed ["hosts"]
~doc:"Some of hosts failed to start ssh service." () ;

error Api_errors.ssh_stop_partially_failed ["hosts"]
~doc:"Some of hosts failed to stop ssh service." () ;

message
(fst Api_messages.ha_pool_overcommitted)
~doc:
Expand Down
20 changes: 20 additions & 0 deletions ocaml/idl/datamodel_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,24 @@ let get_guest_secureboot_readiness =
~result:(pool_guest_secureboot_readiness, "The readiness of the pool")
~allowed_roles:_R_POOL_OP ()

let ssh_start =
call ~name:"ssh_start"
~doc:
"Start and enable ssh service on all hosts in the pool. It's a helper \
which calls host.ssh_start for all the hosts in the pool."
~lifecycle:[]
~params:[(Ref _pool, "self", "The pool")]
~allowed_roles:_R_POOL_ADMIN ()

let ssh_stop =
call ~name:"ssh_stop"
~doc:
"Stop and disable ssh service on all hosts in the pool. It's a helper \
which calls host.ssh_stop for all the hosts in the pool."
~lifecycle:[]
~params:[(Ref _pool, "self", "The pool")]
~allowed_roles:_R_POOL_ADMIN ()

(** A pool class *)
let t =
create_obj ~in_db:true
Expand Down Expand Up @@ -1633,6 +1651,8 @@ let t =
; set_ext_auth_cache_size
; set_ext_auth_cache_expiry
; get_guest_secureboot_readiness
; ssh_start
; ssh_stop
]
~contents:
([
Expand Down
1 change: 1 addition & 0 deletions ocaml/sdk-gen/go/gen_go_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ let acronyms =
; "db"
; "xml"
; "eof"
; "ssh"
]
|> StringSet.of_list

Expand Down
22 changes: 22 additions & 0 deletions ocaml/xapi-cli-server/cli_frontend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3133,6 +3133,28 @@ let rec cmdtable_data : (string * cmd_spec) list =
; flags= []
}
)
; ( "pool-ssh-start"
, {
reqd= []
; optn= []
; help=
"Start and enable ssh service on all hosts in the pool. It's a \
helper which calls host.ssh_start for all the hosts in the pool."
; implementation= No_fd Cli_operations.pool_ssh_start
; flags= []
}
)
; ( "pool-ssh-stop"
, {
reqd= []
; optn= []
; help=
"Stop and disable ssh service on all hosts in the pool. It's a \
helper which calls host.ssh_stop for all the hosts in the pool."
; implementation= No_fd Cli_operations.pool_ssh_stop
; flags= []
}
)
; ( "host-ha-xapi-healthcheck"
, {
reqd= []
Expand Down
8 changes: 8 additions & 0 deletions ocaml/xapi-cli-server/cli_operations.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6779,6 +6779,14 @@ let pool_sync_bundle fd _printer rpc session_id params =
| None ->
failwith "Required parameter not found: filename"

let pool_ssh_start _printer rpc session_id params =
let pool = get_pool_with_default rpc session_id params "uuid" in
Client.Pool.ssh_start ~rpc ~session_id ~self:pool

let pool_ssh_stop _printer rpc session_id params =
let pool = get_pool_with_default rpc session_id params "uuid" in
Client.Pool.ssh_stop ~rpc ~session_id ~self:pool

let host_restore fd _printer rpc session_id params =
let filename = List.assoc "file-name" params in
let op _ host =
Expand Down
4 changes: 4 additions & 0 deletions ocaml/xapi-consts/api_errors.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1407,3 +1407,7 @@ let too_many_groups = add_error "TOO_MANY_GROUPS"
let ssh_start_failed = add_error "SSH_START_FAILED"

let ssh_stop_failed = add_error "SSH_STOP_FAILED"

let ssh_start_partially_failed = add_error "SSH_START_PARTIALLY_FAILED"

let ssh_stop_partially_failed = add_error "SSH_STOP_PARTIALLY_FAILED"
8 changes: 8 additions & 0 deletions ocaml/xapi/message_forwarding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,14 @@ functor
let get_guest_secureboot_readiness ~__context ~self =
info "%s: pool='%s'" __FUNCTION__ (pool_uuid ~__context self) ;
Local.Pool.get_guest_secureboot_readiness ~__context ~self

let ssh_start ~__context ~self =
info "%s: pool = '%s'" __FUNCTION__ (pool_uuid ~__context self) ;
Local.Pool.ssh_start ~__context ~self

let ssh_stop ~__context ~self =
info "%s: pool = '%s'" __FUNCTION__ (pool_uuid ~__context self) ;
Local.Pool.ssh_stop ~__context ~self
end

module VM = struct
Expand Down
28 changes: 28 additions & 0 deletions ocaml/xapi/xapi_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3952,3 +3952,31 @@ let put_bundle_handler (req : Request.t) s _ =
| None ->
()
)

let ssh_helper ~__context ~action ~error =
let hosts = Db.Host.get_all ~__context in
Helpers.call_api_functions ~__context (fun rpc session_id ->
let failed_hosts =
List.fold_left
(fun failed_hosts host ->
try
action ~rpc ~session_id ~self:host ;
failed_hosts
with _ -> Ref.string_of host :: failed_hosts
)
[] hosts
in
match failed_hosts with
| [] ->
()
| _ ->
raise (Api_errors.Server_error (error, failed_hosts))
)

let ssh_start ~__context ~self:_ =
ssh_helper ~__context ~action:Client.Host.ssh_start
~error:Api_errors.ssh_start_partially_failed

let ssh_stop ~__context ~self:_ =
ssh_helper ~__context ~action:Client.Host.ssh_stop
~error:Api_errors.ssh_stop_partially_failed
4 changes: 4 additions & 0 deletions ocaml/xapi/xapi_pool.mli
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,7 @@ val get_guest_secureboot_readiness :
-> API.pool_guest_secureboot_readiness

val put_bundle_handler : Http.Request.t -> Unix.file_descr -> 'a -> unit

val ssh_start : __context:Context.t -> self:API.ref_pool -> unit

val ssh_stop : __context:Context.t -> self:API.ref_pool -> unit

0 comments on commit badbeaa

Please sign in to comment.