From 77005e902dcdc0f5440e9d166c5b11c50bdc4ac3 Mon Sep 17 00:00:00 2001 From: Bengang Yuan Date: Thu, 2 Jan 2025 17:05:06 +0800 Subject: [PATCH] CP-52074: Add start and stop ssh API on pool Signed-off-by: Bengang Yuan --- ocaml/idl/datamodel_errors.ml | 6 ++++ ocaml/idl/datamodel_pool.ml | 12 +++++++ ocaml/sdk-gen/go/gen_go_helper.ml | 1 + ocaml/xapi-cli-server/cli_frontend.ml | 18 ++++++++++ ocaml/xapi-cli-server/cli_operations.ml | 8 +++++ ocaml/xapi-consts/api_errors.ml | 4 +++ ocaml/xapi/message_forwarding.ml | 8 +++++ ocaml/xapi/xapi_pool.ml | 46 +++++++++++++++++++++++++ ocaml/xapi/xapi_pool.mli | 4 +++ 9 files changed, 107 insertions(+) diff --git a/ocaml/idl/datamodel_errors.ml b/ocaml/idl/datamodel_errors.ml index 283e36d851d..a9838d61ad1 100644 --- a/ocaml/idl/datamodel_errors.ml +++ b/ocaml/idl/datamodel_errors.ml @@ -2016,6 +2016,12 @@ let _ = error Api_errors.stop_ssh_failed ["host"] ~doc:"Failed to stop ssh service." () ; + error Api_errors.start_ssh_partially_failed ["hosts"] + ~doc:"Some of hosts failed to start ssh service." () ; + + error Api_errors.stop_ssh_partially_failed ["hosts"] + ~doc:"Some of hosts failed to stop ssh service." () ; + message (fst Api_messages.ha_pool_overcommitted) ~doc: diff --git a/ocaml/idl/datamodel_pool.ml b/ocaml/idl/datamodel_pool.ml index ab0d1669788..b77453f0adc 100644 --- a/ocaml/idl/datamodel_pool.ml +++ b/ocaml/idl/datamodel_pool.ml @@ -1539,6 +1539,16 @@ let get_guest_secureboot_readiness = ~result:(pool_guest_secureboot_readiness, "The readiness of the pool") ~allowed_roles:_R_POOL_OP () +let start_ssh = + call ~name:"start_ssh" ~doc:"Start and enable ssh service" ~lifecycle:[] + ~params:[(Ref _pool, "self", "The pool")] + ~allowed_roles:_R_POOL_ADMIN () + +let stop_ssh = + call ~name:"stop_ssh" ~doc:"Stop and disable ssh service" ~lifecycle:[] + ~params:[(Ref _pool, "self", "The pool")] + ~allowed_roles:_R_POOL_ADMIN () + (** A pool class *) let t = create_obj ~in_db:true @@ -1633,6 +1643,8 @@ let t = ; set_ext_auth_cache_size ; set_ext_auth_cache_expiry ; get_guest_secureboot_readiness + ; start_ssh + ; stop_ssh ] ~contents: ([ diff --git a/ocaml/sdk-gen/go/gen_go_helper.ml b/ocaml/sdk-gen/go/gen_go_helper.ml index 47540f55ef7..84b91260ae2 100644 --- a/ocaml/sdk-gen/go/gen_go_helper.ml +++ b/ocaml/sdk-gen/go/gen_go_helper.ml @@ -38,6 +38,7 @@ let acronyms = ; "db" ; "xml" ; "eof" + ; "ssh" ] |> StringSet.of_list diff --git a/ocaml/xapi-cli-server/cli_frontend.ml b/ocaml/xapi-cli-server/cli_frontend.ml index 4c7e73cdd6e..d93672fb52b 100644 --- a/ocaml/xapi-cli-server/cli_frontend.ml +++ b/ocaml/xapi-cli-server/cli_frontend.ml @@ -3123,6 +3123,24 @@ let rec cmdtable_data : (string * cmd_spec) list = ; flags= [] } ) + ; ( "pool-start-ssh" + , { + reqd= [] + ; optn= [] + ; help= "Start and enable ssh service" + ; implementation= No_fd Cli_operations.pool_start_ssh + ; flags= [] + } + ) + ; ( "pool-stop-ssh" + , { + reqd= [] + ; optn= [] + ; help= "Stop and disable ssh service" + ; implementation= No_fd Cli_operations.pool_stop_ssh + ; flags= [] + } + ) ; ( "host-ha-xapi-healthcheck" , { reqd= [] diff --git a/ocaml/xapi-cli-server/cli_operations.ml b/ocaml/xapi-cli-server/cli_operations.ml index ea371ce7a19..7522141547f 100644 --- a/ocaml/xapi-cli-server/cli_operations.ml +++ b/ocaml/xapi-cli-server/cli_operations.ml @@ -6779,6 +6779,14 @@ let pool_sync_bundle fd _printer rpc session_id params = | None -> failwith "Required parameter not found: filename" +let pool_start_ssh _printer rpc session_id params = + let pool = get_pool_with_default rpc session_id params "uuid" in + Client.Pool.start_ssh ~rpc ~session_id ~self:pool + +let pool_stop_ssh _printer rpc session_id params = + let pool = get_pool_with_default rpc session_id params "uuid" in + Client.Pool.stop_ssh ~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 = diff --git a/ocaml/xapi-consts/api_errors.ml b/ocaml/xapi-consts/api_errors.ml index 03c7edfda55..930d5348fd8 100644 --- a/ocaml/xapi-consts/api_errors.ml +++ b/ocaml/xapi-consts/api_errors.ml @@ -1407,3 +1407,7 @@ let too_many_groups = add_error "TOO_MANY_GROUPS" let start_ssh_failed = add_error "START_SSH_FAILED" let stop_ssh_failed = add_error "STOP_SSH_FAILED" + +let start_ssh_partially_failed = add_error "START_SSH_PARTIALLY_FAILED" + +let stop_ssh_partially_failed = add_error "STOP_SSH_PARTIALLY_FAILED" diff --git a/ocaml/xapi/message_forwarding.ml b/ocaml/xapi/message_forwarding.ml index cb3d5b05c1f..10a287814bb 100644 --- a/ocaml/xapi/message_forwarding.ml +++ b/ocaml/xapi/message_forwarding.ml @@ -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 start_ssh ~__context ~self = + info "%s: pool = '%s'" __FUNCTION__ (pool_uuid ~__context self) ; + Local.Pool.start_ssh ~__context ~self + + let stop_ssh ~__context ~self = + info "%s: pool = '%s'" __FUNCTION__ (pool_uuid ~__context self) ; + Local.Pool.stop_ssh ~__context ~self end module VM = struct diff --git a/ocaml/xapi/xapi_pool.ml b/ocaml/xapi/xapi_pool.ml index 2f471932c14..2c2a0163579 100644 --- a/ocaml/xapi/xapi_pool.ml +++ b/ocaml/xapi/xapi_pool.ml @@ -3952,3 +3952,49 @@ let put_bundle_handler (req : Request.t) s _ = | None -> () ) + +let start_ssh ~__context ~self:_ = + 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 + Client.Host.start_ssh ~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 + (Api_errors.start_ssh_partially_failed, failed_hosts) + ) + ) + +let stop_ssh ~__context ~self:_ = + 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 + Client.Host.stop_ssh ~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 + (Api_errors.stop_ssh_partially_failed, failed_hosts) + ) + ) diff --git a/ocaml/xapi/xapi_pool.mli b/ocaml/xapi/xapi_pool.mli index 835a356f782..4522c5952d2 100644 --- a/ocaml/xapi/xapi_pool.mli +++ b/ocaml/xapi/xapi_pool.mli @@ -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 start_ssh : __context:Context.t -> self:API.ref_pool -> unit + +val stop_ssh : __context:Context.t -> self:API.ref_pool -> unit