Skip to content

Commit 3c7d07c

Browse files
authored
Merge pull request xapi-project#3132 from gaborigloi/honolulu_backport_CA-253489_and_CA-226886
Backport CA-253489 and CA-226886 to Honolulu
2 parents 87e8e06 + 1cd60f1 commit 3c7d07c

File tree

4 files changed

+53
-44
lines changed

4 files changed

+53
-44
lines changed

ocaml/xapi/cli_frontend.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ let rec cmdtable_data : (string*cmd_spec) list =
855855
{
856856
reqd=["file-name"];
857857
optn=["sr-uuid"];
858-
help="Stream new update to the server.";
858+
help="Stream new update to the server. The update will be uploaded to the SR <sr-uuid>, or, if it is not specified, to the pool's default SR.";
859859
implementation=With_fd Cli_operations.update_upload;
860860
flags=[];
861861
};

ocaml/xapi/cli_operations.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4306,7 +4306,16 @@ let update_upload fd printer rpc session_id params =
43064306
let sr =
43074307
if List.mem_assoc "sr-uuid" params
43084308
then Client.SR.get_by_uuid rpc session_id (List.assoc "sr-uuid" params)
4309-
else Client.Pool.get_default_SR ~rpc ~session_id ~self:(List.hd pools)
4309+
else begin
4310+
let sr = Client.Pool.get_default_SR ~rpc ~session_id ~self:(List.hd pools) in
4311+
let ref_is_valid = Server_helpers.exec_with_new_task
4312+
~session_id "Checking default SR validity"
4313+
(fun __context -> Db.is_valid_ref __context sr) in
4314+
if ref_is_valid then sr
4315+
else failwith "No sr-uuid parameter was given, and the pool's default SR \
4316+
is unspecified or invalid. Please explicitly specify the SR to use \
4317+
in the sr-uuid parameter, or set the pool's default SR."
4318+
end
43104319
in
43114320
let uri = Printf.sprintf "%s%s?session_id=%s&sr_id=%s&task_id=%s"
43124321
prefix Constants.import_raw_vdi_uri (Ref.string_of session_id) (Ref.string_of sr)(Ref.string_of task_id) in

ocaml/xapi/import_raw_vdi.ml

100644100755
Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ open Unixext
2626
open Pervasiveext
2727
open Client
2828

29+
let fail_task_in_request (req: Request.t) (s: Unix.file_descr) e =
30+
ignore(Xapi_http.ref_param_of_req req "task_id"|> Stdext.Opt.map (fun task_id -> TaskHelper.failed ~__context:(Context.from_forwarded_task task_id) e));
31+
Http_svr.headers s (Http.http_400_badrequest ())
2932

3033
exception HandleError of exn * (string list) (* Exception to put into the task * headers to return to the client *)
3134

@@ -98,32 +101,37 @@ let import vdi (req: Request.t) (s: Unix.file_descr) _ =
98101

99102
(* Perform the SR reachability check using a fresh context/task because
100103
we don't want to complete the task in the forwarding case *)
104+
101105
Server_helpers.exec_with_new_task "VDI.import"
102106
(fun __context ->
103-
Helpers.call_api_functions ~__context
104-
(fun rpc session_id ->
105-
let sr_opt = match vdi, sr_of_req ~__context req with
106-
| Some vdi, _ -> Some (Db.VDI.get_SR ~__context ~self:vdi)
107-
| None, Some sr -> Some sr
108-
| None, None -> None
109-
in
110-
match sr_opt with
111-
| Some sr ->
112-
debug "Checking whether localhost can see SR: %s" (Ref.string_of sr);
113-
if (Importexport.check_sr_availability ~__context sr)
114-
then localhost_handler rpc session_id vdi req s
115-
else
116-
let host = Importexport.find_host_for_sr ~__context sr in
117-
let address = Db.Host.get_address ~__context ~self:host in
118-
return_302_redirect req s address;
107+
try
108+
Helpers.call_api_functions ~__context
109+
(fun rpc session_id ->
110+
let sr_opt = match vdi, sr_of_req ~__context req with
111+
| Some vdi, _ -> Some (Db.VDI.get_SR ~__context ~self:vdi)
112+
| None, Some sr -> Some sr
113+
| None, None -> None
114+
in
115+
match sr_opt with
116+
| Some sr ->
117+
debug "Checking whether localhost can see SR: %s" (Ref.string_of sr);
118+
if (Importexport.check_sr_availability ~__context sr)
119+
then localhost_handler rpc session_id vdi req s
120+
else
121+
let host = Importexport.find_host_for_sr ~__context sr in
122+
let address = Db.Host.get_address ~__context ~self:host in
123+
return_302_redirect req s address;
124+
None
125+
| None ->
126+
error "Require an SR or VDI to import";
127+
fail_task_in_request req s Api_errors.(Server_error(vdi_missing,[]));
119128
None
120-
| None ->
121-
error "Require an SR or VDI to import";
122-
TaskHelper.failed ~__context Api_errors.(Server_error(vdi_missing,[]));
123-
Http_svr.headers s (Http.http_400_badrequest ());
124-
None
129+
)
130+
with e ->
131+
error "Caught exception in import handler: %s" (ExnHelper.string_of_exn e);
132+
fail_task_in_request req s e;
133+
raise e
125134

126-
)
127135
)
128136

129137

ocaml/xapi/xapi_http.ml

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let inet_rpc xml =
2929
let version = "1.1" and path = "/" in
3030
let http = 80 and https = !Xapi_globs.https_port in
3131
(* Bypass SSL for localhost, this works even if the management interface
32-
is disabled. *)
32+
is disabled. *)
3333
let open Xmlrpc_client in
3434
let transport =
3535
if Pool_role.is_master ()
@@ -44,6 +44,12 @@ open Http
4444

4545
exception NoAuth
4646

47+
let ref_param_of_req (req: Http.Request.t) param_name =
48+
let all = Http.Request.(req.cookie @ req.query) in
49+
try Some (Ref.of_string (List.assoc param_name all))
50+
with Not_found -> None
51+
52+
4753
let get_session_id (req: Request.t) =
4854
let all = req.Request.cookie @ req.Request.query in
4955
if List.mem_assoc "session_id" all
@@ -88,15 +94,8 @@ let rbac_audit_params_of (req: Request.t) =
8894
let assert_credentials_ok realm ?(http_action=realm) ?(fn=Rbac.nofn) (req: Request.t) ic =
8995
let http_permission = Datamodel.rbac_http_permission_prefix ^ http_action in
9096
let all = req.Request.cookie @ req.Request.query in
91-
let subtask_of =
92-
if List.mem_assoc "subtask_of" all
93-
then Some (Ref.of_string (List.assoc "subtask_of" all))
94-
else None in
95-
let task_id =
96-
if List.mem_assoc "task_id" all
97-
then Some (Ref.of_string (List.assoc "task_id" all))
98-
else None
99-
in
97+
let subtask_of = ref_param_of_req req "subtask_of" in
98+
let task_id = ref_param_of_req req "task_id" in
10099
let rbac_raise permission msg exc =
101100
(match task_id with
102101
| None -> ()
@@ -163,14 +162,8 @@ let assert_credentials_ok realm ?(http_action=realm) ?(fn=Rbac.nofn) (req: Reque
163162

164163
let with_context ?(dummy=false) label (req: Request.t) (s: Unix.file_descr) f =
165164
let all = req.Request.cookie @ req.Request.query in
166-
let task_id =
167-
if List.mem_assoc "task_id" all
168-
then Some (Ref.of_string (List.assoc "task_id" all))
169-
else None in
170-
let subtask_of =
171-
if List.mem_assoc "subtask_of" all
172-
then Some (Ref.of_string (List.assoc "subtask_of" all))
173-
else None in
165+
let task_id = ref_param_of_req req "task_id" in
166+
let subtask_of = ref_param_of_req req "subtask_of" in
174167
let localhost = Server_helpers.exec_with_new_task "with_context" (fun __context -> Helpers.get_localhost ~__context) in
175168
try
176169
let session_id,must_logout =
@@ -240,7 +233,7 @@ let bind inetaddr =
240233
| Unix.ADDR_INET(ip, port) -> Printf.sprintf "INET %s:%d" (Unix.string_of_inet_addr ip) port
241234
| Unix.ADDR_UNIX path -> Printf.sprintf "UNIX %s" path in
242235
(* Sometimes we see failures which we hope are transient. If this
243-
happens then we'll retry a couple of times before failing. *)
236+
happens then we'll retry a couple of times before failing. *)
244237
let start = Unix.gettimeofday () in
245238
let timeout = 30.0 in (* 30s *)
246239
let rec bind' () =
@@ -315,4 +308,3 @@ let add_handler (name, handler) =
315308
| Datamodel.Connect -> Http.Connect
316309
| Datamodel.Options -> Http.Options
317310
in Http_svr.Server.add_handler server ty uri h
318-

0 commit comments

Comments
 (0)