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 9, 2025
1 parent 7fe282e commit 77005e9
Show file tree
Hide file tree
Showing 9 changed files with 107 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.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:
Expand Down
12 changes: 12 additions & 0 deletions ocaml/idl/datamodel_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
([
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
18 changes: 18 additions & 0 deletions ocaml/xapi-cli-server/cli_frontend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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= []
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_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 =
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 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"
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 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
Expand Down
46 changes: 46 additions & 0 deletions ocaml/xapi/xapi_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
)
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 start_ssh : __context:Context.t -> self:API.ref_pool -> unit

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

0 comments on commit 77005e9

Please sign in to comment.