Skip to content

Commit ee1e4c8

Browse files
authored
Add file-upload support to xe host-call-plugin (#6497)
To pass file content to a plugin, vm-call-plugin supports passing the content of a client-side file as a parameter. This is missing for host-call-plugin - this patch adds it. Otherwise it is difficult to pass anything beyond a short string to a plugin.
2 parents 35c353c + 8a5111d commit ee1e4c8

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

ocaml/xapi-cli-server/cli_frontend.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,8 +958,9 @@ let rec cmdtable_data : (string * cmd_spec) list =
958958
; optn= ["args:"]
959959
; help=
960960
"Calls the function within the plugin on the given host with \
961-
optional arguments."
962-
; implementation= No_fd Cli_operations.host_call_plugin
961+
optional arguments. The syntax args:key:file=/path/file.ext passes \
962+
the content of /path/file.ext under key to the plugin."
963+
; implementation= With_fd Cli_operations.host_call_plugin
963964
; flags= []
964965
}
965966
)

ocaml/xapi-cli-server/cli_operations.ml

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3490,28 +3490,29 @@ let vm_memory_target_wait printer rpc session_id params =
34903490
params []
34913491
)
34923492

3493+
(** This implements the key:file=/path/to/file.txt syntax. The value for
3494+
key is the content of a file requested from the client *)
3495+
let args_file fd ((k, v) as p) =
3496+
match Astring.String.cut ~sep:":" k with
3497+
| Some (key, "file") -> (
3498+
match get_client_file fd v with
3499+
| Some s ->
3500+
(key, s)
3501+
| None ->
3502+
marshal fd
3503+
(Command (PrintStderr (Printf.sprintf "Failed to read file %s\n" v))) ;
3504+
raise (ExitWithError 1)
3505+
)
3506+
| _ ->
3507+
p
3508+
34933509
let vm_call_plugin fd printer rpc session_id params =
34943510
let vm_uuid = List.assoc "vm-uuid" params in
34953511
let vm = Client.VM.get_by_uuid ~rpc ~session_id ~uuid:vm_uuid in
34963512
let plugin = List.assoc "plugin" params in
34973513
let fn = List.assoc "fn" params in
34983514
let args = read_map_params "args" params in
3499-
(* Syntax interpretation: args:key:file=filename equals args:key=filename_content *)
3500-
let convert ((k, v) as p) =
3501-
match Astring.String.cut ~sep:":" k with
3502-
| Some (key, "file") -> (
3503-
match get_client_file fd v with
3504-
| Some s ->
3505-
(key, s)
3506-
| None ->
3507-
marshal fd
3508-
(Command (PrintStderr (Printf.sprintf "Failed to read file %s\n" v))) ;
3509-
raise (ExitWithError 1)
3510-
)
3511-
| _ ->
3512-
p
3513-
in
3514-
let args = List.map convert args in
3515+
let args = List.map (args_file fd) args in
35153516
let result = Client.VM.call_plugin ~rpc ~session_id ~vm ~plugin ~fn ~args in
35163517
printer (Cli_printer.PList [result])
35173518

@@ -6907,12 +6908,13 @@ let host_set_hostname_live _printer rpc session_id params =
69076908
let hostname = List.assoc "host-name" params in
69086909
Client.Host.set_hostname_live ~rpc ~session_id ~host ~hostname
69096910

6910-
let host_call_plugin printer rpc session_id params =
6911+
let host_call_plugin fd printer rpc session_id params =
69116912
let host_uuid = List.assoc "host-uuid" params in
69126913
let host = Client.Host.get_by_uuid ~rpc ~session_id ~uuid:host_uuid in
69136914
let plugin = List.assoc "plugin" params in
69146915
let fn = List.assoc "fn" params in
69156916
let args = read_map_params "args" params in
6917+
let args = List.map (args_file fd) args in
69166918
let result =
69176919
Client.Host.call_plugin ~rpc ~session_id ~host ~plugin ~fn ~args
69186920
in

0 commit comments

Comments
 (0)