public static String Put(HTTP.UpdateProgressDelegate progressDelegate, int timeout, XenRef <Task> taskRef, ref Session session, string path, string hostname, Delegate f, params object[] p) { HTTP.FuncBool cancellingDelegate = (HTTP.FuncBool) delegate() { return(false); }; log.InfoFormat("HTTP导入文件从[{0}]到主机[{1}]", path, hostname); try { List <object> args = new List <object>(); args.Add(progressDelegate); args.Add(cancellingDelegate); args.Add(timeout); args.Add(hostname); args.Add(null); //IWebProxy args.Add(path); args.Add(taskRef.opaque_ref); // task_id args.AddRange(p); f.DynamicInvoke(args.ToArray()); } catch (Failure failure) { log.InfoFormat("HTTP导入文件失败:{0}", failure.ErrorDescription.ToString()); } catch (Exception ex) { log.InfoFormat("HTTP导入文件失败:{0}", ex.Message); } return(PollTaskForResult(ref ConnectManager.session, taskRef)); }
private Pool_patch UploadRetailPatch(Host host) { log.InfoFormat("Uploading file '{0}' to server '{1}'", Path.GetFileName(retailPatchPath), host.Name); this.Description = Messages.UPLOAD_PATCH_DESCRIPTION; long size = FileSize(retailPatchPath); Session session = NewSession(); try { Host h = Helpers.GetMaster(Connection); HTTP.UpdateProgressDelegate progressDelegate = delegate(int percent) { int actionPercent = (int)(((TotalUploaded * 90) + (size * percent)) / TotalSize); this.Tick(actionPercent, this.Description); }; RelatedTask = XenAPI.Task.create(session, "uploadTask", retailPatchPath); String result; try { result = HTTPHelper.Put(progressDelegate, GetCancelling, true, Connection, RelatedTask, ref session, retailPatchPath, h.address, (HTTP_actions.put_ss)HTTP_actions.put_pool_patch_upload, session.uuid); } catch (CancelledException) { if (deleteFileOnCancel && File.Exists(retailPatchPath)) { File.Delete(retailPatchPath); } throw; } finally { Task.destroy(session, RelatedTask); RelatedTask = null; } TotalUploaded += size; return(Connection.WaitForCache(new XenRef <Pool_patch>(result))); } finally { log.InfoFormat("File '{0}' uploaded to server '{1}'", Path.GetFileName(retailPatchPath), host.Name); Description = Messages.UPLOAD_PATCH_UPLOADED; } }
public static String Put(int timeout, string path, string hostname, Delegate f, params object[] p) { XenRef <Task> taskRef = Task.create(ConnectManager.session, "INSTALL WinCenter VM", hostname); log.InfoFormat("创建Task:{0}", taskRef.opaque_ref); try { HTTP.UpdateProgressDelegate progressDelegate = delegate(int percent) { Tick(percent); }; return(Put(progressDelegate, timeout, taskRef, ref ConnectManager.session, path, hostname, f, p)); } finally { Task.destroy(ConnectManager.session, taskRef); log.InfoFormat("销毁Task:{0}", taskRef.opaque_ref); } }
private void UploadEmbeddedPatch(Host host) { this.Description = String.Format(Messages.UPLOADING_PATCH_TO, host.Name); long size = FileSize(embeddedPatchPath); Session session = NewSession(); HTTP.UpdateProgressDelegate progressDelegate = delegate(int percent) { int actionPercent = (int)(((TotalUploaded * 90) + (size * percent)) / TotalSize); this.Tick(actionPercent, this.Description); }; RelatedTask = XenAPI.Task.create(session, "uploadTask", host.address); Connection = host.Connection; try { HTTPHelper.Put(progressDelegate, GetCancelling, true, Connection, RelatedTask, ref session, embeddedPatchPath, host.address, (HTTP_actions.put_ss)HTTP_actions.put_oem_patch_stream, session.uuid); } finally { Task.destroy(session, RelatedTask); Connection = null; RelatedTask = null; } TotalUploaded += size; try { RelatedTask = XenAPI.Pool.async_sync_database(session); PollToCompletion(90, 100); } catch (Exception exn) { log.Warn("Exception during pool.sync_database", exn); } this.Description = String.Format(Messages.PATCH_UPLOADED, host.Name); }
private Pool_patch UploadRetailPatch(Host host) { this.Description = String.Format(Messages.UPLOADING_PATCH_TO, host.Name); long size = FileSize(retailPatchPath); Session session = NewSession(); try { Host h = Helpers.GetMaster(Connection); HTTP.UpdateProgressDelegate progressDelegate = delegate(int percent) { int actionPercent = (int)(((TotalUploaded * 90) + (size * percent)) / TotalSize); this.Tick(actionPercent, this.Description); }; RelatedTask = XenAPI.Task.create(session, "uploadTask", retailPatchPath); String result; try { result = HTTPHelper.Put(progressDelegate, GetCancelling, true, Connection, RelatedTask, ref session, retailPatchPath, h.address, (HTTP_actions.put_ss)HTTP_actions.put_pool_patch_upload, session.uuid); } finally { Task.destroy(session, RelatedTask); RelatedTask = null; } TotalUploaded += size; return(Connection.WaitForCache(new XenRef <Pool_patch>(result))); } finally { Description = String.Format(Messages.PATCH_UPLOADED, host.Name); } }
/// <summary> /// HTTP PUT file from path to HTTP action f, updating action with progress every 500ms. /// </summary> /// <param name="action">Action on which to update the progress</param> /// <param name="timeout">Timeout value in ms</param> /// <param name="path">path of file to put</param> public static String Put(AsyncAction action, int timeout, string path, string hostname, Delegate f, params object[] p) { Session session = action.Session; action.RelatedTask = XenAPI.Task.create(session, "uploadTask", hostname); try { HTTP.UpdateProgressDelegate progressDelegate = delegate(int percent) { action.Tick(percent, action.Description); }; return Put(progressDelegate, action.GetCancelling, timeout, action.Connection, action.RelatedTask, ref session, path, hostname, f, p); } finally { action.Session = session; Task.destroy(session, action.RelatedTask); } }
public static String Get(HTTP.UpdateProgressDelegate progressDelegate, HTTP.FuncBool cancellingDelegate, bool timeout, HTTP.DataCopiedDelegate dataRxDelegate, IXenConnection connection, XenRef <Task> task, ref Session session, string path, string hostname, Delegate f, params object[] p) { log.DebugFormat("HTTP GETTING file from {0} to {1}", hostname, path); // Cannot use ref param in anonymous method, so save it here and restore it later Session _session = session; HTTP.DataCopiedDelegate dataCopiedDelegate = delegate(long bytes) { if (progressDelegate != null) { int progress = (int)(100 * (double)Task.DoWithSessionRetry(connection, ref _session, (Task.TaskProgressOp)Task.get_progress, task.opaque_ref)); progressDelegate(progress); } if (dataRxDelegate != null) { dataRxDelegate(bytes); } }; HTTP.FuncBool cancellingDelegate2 = (HTTP.FuncBool) delegate() { return(XenAdminConfigManager.Provider.ForcedExiting || cancellingDelegate != null && cancellingDelegate()); }; try { List <object> args = new List <object>(); args.Add(dataCopiedDelegate); args.Add(cancellingDelegate2); args.Add(XenAdminConfigManager.Provider.GetProxyTimeout(timeout)); args.Add(hostname); args.Add(XenAdminConfigManager.Provider.GetProxyFromSettings(connection)); args.Add(path); args.Add(task.opaque_ref); // task_id args.AddRange(p); f.DynamicInvoke(args.ToArray()); } catch (Exception e) { log.Debug($"Caught exception doing HTTP GET from {hostname} to {path}", e); if (e is WebException && e.InnerException is IOException && Win32.GetHResult(e.InnerException as IOException) == Win32.ERROR_DISK_FULL) { throw e.InnerException; } else if (e is CancelledException || e.InnerException is CancelledException) { throw new XenAdmin.CancelledException(); } else if (e.InnerException.Message == "Received error code HTTP/1.1 403 Forbidden\r\n from the server") { // RBAC Failure List <Role> roles = connection.Session.Roles; roles.Sort(); throw new Exception(String.Format(Messages.RBAC_HTTP_FAILURE, roles[0].FriendlyName()), e); } else { throw e.InnerException; } } return(PollTaskForResult(connection, ref session, cancellingDelegate2, task)); }
public static void put_import(HTTP.UpdateProgressDelegate progressDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms, string hostname, IWebProxy proxy, string path, string task_id, string session_id, bool restore, bool force, string sr_id) { Put(progressDelegate, cancellingDelegate, timeout_ms, hostname, "/import", proxy, path, new object[] { "task_id", task_id, "session_id", session_id, "restore", restore, "force", force, "sr_id", sr_id }); }
public static void put_host_restore(HTTP.UpdateProgressDelegate progressDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms, string hostname, IWebProxy proxy, string path, string task_id, string session_id) { Put(progressDelegate, cancellingDelegate, timeout_ms, hostname, "/host_restore", proxy, path, new object[] { "task_id", task_id, "session_id", session_id }); }
private static void Put(HTTP.UpdateProgressDelegate progressDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms, string hostname, string remotePath, IWebProxy proxy, string localPath, params object[] args) { HTTP.Put(progressDelegate, cancellingDelegate, HTTP.BuildUri(hostname, remotePath, args), proxy, localPath, timeout_ms); }
public static void put_import_raw_vdi(HTTP.UpdateProgressDelegate progressDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms, string hostname, IWebProxy proxy, string path, string task_id, string session_id, string vdi) { Put(progressDelegate, cancellingDelegate, timeout_ms, hostname, "/import_raw_vdi", proxy, path, new object[] { "task_id", task_id, "session_id", session_id, "vdi", vdi }); }
private string UploadSupplementalPack(SR sr) { this.Description = String.Format(Messages.SUPP_PACK_UPLOADING_TO, sr.Name()); String result; log.DebugFormat("Creating vdi of size {0} bytes on SR '{1}'", diskSize, sr.Name()); VDI vdi = NewVDI(sr); XenRef <VDI> vdiRef = null; try { vdiRef = VDI.create(Session, vdi); } catch (Exception ex) { log.ErrorFormat("{0} {1}", "Failed to create VDI", ex.Message); throw; } log.DebugFormat("Uploading file '{0}' to VDI '{1}' on SR '{2}'", suppPackFilePath, vdi.Name(), sr.Name()); Host localStorageHost = sr.GetStorageHost(); string hostUrl; if (localStorageHost == null) { Uri uri = new Uri(Session.Url); hostUrl = uri.Host; } else { log.DebugFormat("SR is not shared -- redirecting to {0}", localStorageHost.address); hostUrl = localStorageHost.address; } log.DebugFormat("Using {0} for import", hostUrl); try { HTTP.UpdateProgressDelegate progressDelegate = delegate(int percent) { var actionPercent = (int)(((totalUploaded * 100) + percent) / totalCount); Tick(actionPercent, Description); }; Session session = NewSession(); RelatedTask = Task.create(Session, "uploadTask", hostUrl); result = HTTPHelper.Put(progressDelegate, GetCancelling, true, Connection, RelatedTask, ref session, suppPackFilePath, hostUrl, (HTTP_actions.put_sss)HTTP_actions.put_import_raw_vdi, session.opaque_ref, vdiRef.opaque_ref); } catch (Exception ex) { log.ErrorFormat("{0} {1}", "Failed to import a virtual disk over HTTP.", ex.Message); if (vdiRef != null) { log.DebugFormat("Removing the VDI on a best effort basis."); try { RemoveVDI(Session, vdiRef); } catch (Exception removeEx) { //best effort log.Error("Failed to remove the VDI.", removeEx); } } //after having tried to remove the VDI, the original exception is thrown for the UI if (ex is TargetInvocationException && ex.InnerException != null) { throw ex.InnerException; } else { throw ex; } } finally { Task.destroy(Session, RelatedTask); RelatedTask = null; } if (localStorageHost != null) { VdiRefsToCleanUp.Add(localStorageHost, vdiRef); } else // shared SR { foreach (var server in servers) { VdiRefsToCleanUp.Add(server, vdiRef); } } //introduce ISO for Ely and higher if (Helpers.ElyOrGreater(Connection)) { try { var poolUpdateRef = Pool_update.introduce(Connection.Session, vdiRef); poolUpdate = Connection.WaitForCache(poolUpdateRef); if (poolUpdate == null) { throw new Exception(Messages.UPDATE_ERROR_INTRODUCE); // This should not happen, because such case will result in a XAPI Failure. But this code has to be protected at this point. } VdiRefsToCleanUp.Clear(); } catch (Failure ex) { if (ex.ErrorDescription != null && ex.ErrorDescription.Count > 1 && string.Equals("UPDATE_ALREADY_EXISTS", ex.ErrorDescription[0], StringComparison.InvariantCultureIgnoreCase)) { string uuidFound = ex.ErrorDescription[1]; poolUpdate = Connection.Cache.Pool_updates.FirstOrDefault(pu => string.Equals(pu.uuid, uuidFound, System.StringComparison.InvariantCultureIgnoreCase)); //clean-up the VDI we've just created try { RemoveVDI(Session, vdiRef); //remove the vdi that have just been cleaned up var remaining = VdiRefsToCleanUp.Where(kvp => kvp.Value != null && kvp.Value.opaque_ref != vdiRef.opaque_ref).ToList(); VdiRefsToCleanUp.Clear(); remaining.ForEach(rem => VdiRefsToCleanUp.Add(rem.Key, rem.Value)); } catch { //best effort cleanup } } else { throw; } } catch (Exception ex) { log.ErrorFormat("Upload failed when introducing update from VDI {0} on {1}: {2}", vdi.opaque_ref, Connection, ex.Message); poolUpdate = null; throw; } } else { poolUpdate = null; } totalUploaded++; Description = String.Format(Messages.SUPP_PACK_UPLOADED, sr.Name()); foreach (Host host in servers) { SrUploadedUpdates[host] = sr; } return(result); }
private string UploadSupplementalPack(SR sr) { this.Description = String.Format(Messages.SUPP_PACK_UPLOADING_TO, sr.Name); String result; log.DebugFormat("Creating vdi of size {0} bytes on SR '{1}'", diskSize, sr.Name); VDI vdi = NewVDI(sr); XenRef <VDI> vdiRef = null; try { vdiRef = VDI.create(Session, vdi); } catch (Exception ex) { log.ErrorFormat("{0} {1}", "Failed to create VDI", ex.Message); throw; } log.DebugFormat("Uploading file '{0}' to VDI '{1}' on SR '{2}'", suppPackFilePath, vdi.Name, sr.Name); Host localStorageHost = sr.GetStorageHost(); string hostUrl; if (localStorageHost == null) { Uri uri = new Uri(Session.Url); hostUrl = uri.Host; } else { log.DebugFormat("SR is not shared -- redirecting to {0}", localStorageHost.address); hostUrl = localStorageHost.address; } log.DebugFormat("Using {0} for import", hostUrl); try { HTTP.UpdateProgressDelegate progressDelegate = delegate(int percent) { var actionPercent = (int)(((totalUploaded * 100) + percent) / totalCount); Tick(actionPercent, Description); }; Session session = NewSession(); RelatedTask = Task.create(Session, "uploadTask", hostUrl); result = HTTPHelper.Put(progressDelegate, GetCancelling, true, Connection, RelatedTask, ref session, suppPackFilePath, hostUrl, (HTTP_actions.put_sss)HTTP_actions.put_import_raw_vdi, session.uuid, vdiRef.opaque_ref); } catch (Exception ex) { log.ErrorFormat("{0} {1}", "Failed to import a virtual disk via HTTP.", ex.Message); if (vdiRef != null) { RemoveVDI(Session, vdiRef); } throw; } finally { Task.destroy(Session, RelatedTask); RelatedTask = null; } if (localStorageHost != null) { VdiRefs.Add(localStorageHost, vdiRef); } else // shared SR { foreach (var server in servers) { VdiRefs.Add(server, vdiRef); } } totalUploaded++; Description = String.Format(Messages.SUPP_PACK_UPLOADED, sr.Name); return(result); }
private string UploadSupplementalPack(SR sr) { this.Description = String.Format(Messages.SUPP_PACK_UPLOADING_TO, _updateName, sr.Name()); log.DebugFormat("Creating vdi of size {0} bytes on SR '{1}'", _totalUpdateSize, sr.Name()); VDI vdi = NewVDI(sr); var vdiRef = VDI.create(Session, vdi); Host localStorageHost = sr.GetStorageHost(); string hostUrl; if (localStorageHost == null) { Uri uri = new Uri(Session.Url); hostUrl = uri.Host; } else { log.DebugFormat("SR is not shared -- redirecting to {0}", localStorageHost.address); hostUrl = localStorageHost.address; } log.DebugFormat("Using {0} for import", hostUrl); string result; try { log.DebugFormat("Uploading file '{0}' to VDI '{1}' on SR '{2}'", suppPackFilePath, vdi.Name(), sr.Name()); HTTP.UpdateProgressDelegate progressDelegate = delegate(int percent) { var sr1 = sr; var descr = string.Format(Messages.UPLOAD_PATCH_UPLOADING_TO_SR_PROGRESS_DESCRIPTION, _updateName, sr1.Name(), Util.DiskSizeString(percent * _totalUpdateSize / 100, "F1"), Util.DiskSizeString(_totalUpdateSize)); var actionPercent = (int)((totalUploaded * 100 + percent) / totalCount); ByteProgressDescription = descr; Tick(actionPercent, descr); }; Session session = NewSession(); RelatedTask = Task.create(Session, "uploadTask", hostUrl); result = HTTPHelper.Put(progressDelegate, GetCancelling, true, Connection, RelatedTask, ref session, suppPackFilePath, hostUrl, (HTTP_actions.put_sss)HTTP_actions.put_import_raw_vdi, session.opaque_ref, vdiRef.opaque_ref); } catch (Exception ex) { log.Error("Failed to import a virtual disk over HTTP", ex); if (vdiRef != null) { try { log.ErrorFormat("Deleting VDI '{0}' on a best effort basis.", vdiRef.opaque_ref); Thread.Sleep(1000); VDI.destroy(Session, vdiRef); } catch (Exception removeEx) { log.Error("Failed to remove VDI.", removeEx); } } //after having tried to remove the VDI, the original exception is thrown for the UI if (ex is TargetInvocationException && ex.InnerException != null) { throw ex.InnerException; } else { throw; } } finally { Task.destroy(Session, RelatedTask); RelatedTask = null; } //introduce ISO for Ely and higher if (Helpers.ElyOrGreater(Connection)) { try { var poolUpdateRef = Pool_update.introduce(Connection.Session, vdiRef); poolUpdate = Connection.WaitForCache(poolUpdateRef); if (poolUpdate == null) { throw new Exception(Messages.UPDATE_ERROR_INTRODUCE); // This should not happen, because such case will result in a XAPI Failure. But this code has to be protected at this point. } } catch (Exception ex) { //clean-up the VDI we've just created try { log.ErrorFormat("Deleting VDI '{0}' on a best effor basis.", vdiRef); VDI.destroy(Session, vdiRef); } catch (Exception removeEx) { log.Error("Failed to remove VDI", removeEx); } var failure = ex as Failure; if (failure != null && failure.ErrorDescription != null && failure.ErrorDescription.Count > 1 && failure.ErrorDescription[0] == Failure.UPDATE_ALREADY_EXISTS) { string uuidFound = failure.ErrorDescription[1]; poolUpdate = Connection.Cache.Pool_updates.FirstOrDefault(pu => string.Equals(pu.uuid, uuidFound, StringComparison.InvariantCultureIgnoreCase)); } else { log.Error("Failed to introduce the update", ex); poolUpdate = null; throw; } } } else { poolUpdate = null; } if (localStorageHost != null) { VdiRefsPerHost.Add(localStorageHost, vdiRef); } else // shared SR { foreach (var server in servers) { VdiRefsPerHost.Add(server, vdiRef); } } totalUploaded++; Description = string.Format(Messages.SUPP_PACK_UPLOADED, sr.Name()); foreach (Host host in servers) { SrsWithUploadedUpdatesPerHost[host] = sr; } return(result); }
public static void put_blob(HTTP.UpdateProgressDelegate progressDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms, string hostname, IWebProxy proxy, string path, string task_id, string session_id, string reff) { Put(progressDelegate, cancellingDelegate, timeout_ms, hostname, "/blob", proxy, path, "task_id", task_id, "session_id", session_id, "ref", reff); }
public static void put_oem_patch_stream(HTTP.UpdateProgressDelegate progressDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms, string hostname, IWebProxy proxy, string path, string task_id, string session_id) { Put(progressDelegate, cancellingDelegate, timeout_ms, hostname, "/oem_patch_stream", proxy, path, "task_id", task_id, "session_id", session_id); }