Exemplo n.º 1
0
        public void Save(Stream inStream, Session session)
        {
            if (session == null)
            {
                throw new IOException();
            }
            UriBuilder uri = new UriBuilder();

            uri.Scheme = Connection.UriScheme;
            uri.Host   = Connection.Hostname;
            uri.Port   = Connection.Port;
            uri.Path   = BLOB_URI;
            uri.Query  = String.Format("ref={0}&session_id={1}",
                                       opaque_ref, Uri.EscapeDataString(session.opaque_ref));

            using (Stream outStream = HTTPHelper.PUT(uri.Uri, inStream.Length, true))
            {
                HTTP.CopyStream(inStream, outStream, null, () => XenAdminConfigManager.Provider.ForcedExiting);
            }
        }
Exemplo n.º 2
0
 public static Stream PUT(Uri uri, long ContentLength, bool timeout)
 {
     return(HTTP.PUT(uri, XenAdminConfigManager.Provider.GetProxyFromSettings(null), ContentLength, XenAdminConfigManager.Provider.GetProxyTimeout(timeout)));
 }
Exemplo n.º 3
0
        /// <summary>
        /// HTTP GET file from HTTP action f, saving it to path (via a temporary file).
        /// </summary>
        /// <param name="action">Action on which to update the progress</param>
        /// <param name="timeout">Whether to apply a timeout</param>
        /// <param name="dataRxDelegate">Delegate called every 500ms with the data transferred</param>
        /// <param name="path">Path to save file to.</param>
        /// <returns>Result of the task used to GET the file</returns>
        public static String Get(AsyncAction action, bool timeout, HTTP.DataCopiedDelegate dataRxDelegate, string path, string hostname, Delegate f, params object[] p)
        {
            Session session = action.Session;
            action.RelatedTask = XenAPI.Task.create(session, "downloadTask", hostname);

            try
            {
                HTTP.UpdateProgressDelegate progressDelegate = delegate(int percent)
                    {
                        action.Tick(percent, action.Description);
                    };

                return HTTPHelper.Get(progressDelegate, action.GetCancelling, timeout, dataRxDelegate, action.Connection,
                    action.RelatedTask, ref session, path, hostname, f, p);
            }
            finally
            {
                action.Session = session;
                Task.destroy(session, action.RelatedTask);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// A general HttpPut method, with delegates for progress and cancelling
        /// </summary>
        /// <param name="progressDelegate">Delegate called periodically (500ms) with percent complete</param>
        /// <param name="cancellingDelegate">Delegate called periodically to see if need to cancel</param>
        /// <param name="timeout">Timeout value in ms</param>
        /// <param name="task">The task used to track this http get</param>
        /// <param name="session">Session used to make api calls (may be replaced if logged out, hence ref)</param>
        /// <param name="path">Path to file to put</param>
        /// <returns>The result of the task passed in</returns>
        public static String Put(XenAPI.HTTP.UpdateProgressDelegate progressDelegate, HTTP.FuncBool cancellingDelegate, int timeout,
            IXenConnection connection, XenRef<Task> task, ref Session session, string path,
            string hostname, Delegate f, params object[] p)
        {
            log.DebugFormat("HTTP PUTTING file from {0} to {1}", path, hostname);

            HTTP.FuncBool cancellingDelegate2 = (HTTP.FuncBool)delegate()
            {
                return (XenAdminConfigManager.Provider.ForcedExiting ||
                    cancellingDelegate != null && cancellingDelegate());
            };

            try
            {
                List<object> args = new List<object>();
                args.Add(progressDelegate);
                args.Add(cancellingDelegate2);
                args.Add(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.DebugFormat("Caught exception doing HTTP PUT from {0} to {1}", path, hostname);
                log.Debug(e, e);
                PollTaskForResult(connection, ref session, cancellingDelegate2, task, true);
                if (e is CancelledException || e.InnerException is CancelledException)
                    throw new XenAdmin.CancelledException();
                else
                    throw;
            }

            return PollTaskForResult(connection, ref session, cancellingDelegate2, task);
        }
Exemplo n.º 5
0
        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.DebugFormat("Caught exception doing HTTP GET from {0} to {1}", hostname, path);
                log.Debug(e, 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);
        }
Exemplo n.º 6
0
 public static void get_pool_xml_db_sync(HTTP.DataCopiedDelegate dataCopiedDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
     string hostname, IWebProxy proxy, string path, string task_id, string session_id)
 {
     Get(dataCopiedDelegate, cancellingDelegate, timeout_ms, hostname, "/pool/xmldbdump", proxy, path,
         "task_id", task_id, "session_id", session_id);
 }
Exemplo n.º 7
0
 public static void get_host_backup(HTTP.DataCopiedDelegate dataCopiedDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
     string hostname, IWebProxy proxy, string path, string task_id, string session_id)
 {
     Get(dataCopiedDelegate, cancellingDelegate, timeout_ms, hostname, "/host_backup", proxy, path,
         "task_id", task_id, "session_id", session_id);
 }
Exemplo n.º 8
0
 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);
 }
Exemplo n.º 9
0
 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);
 }
Exemplo n.º 10
0
 private static void Get(HTTP.DataCopiedDelegate dataCopiedDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
                         string hostname, string remotePath, IWebProxy proxy, string localPath, params object[] args)
 {
     HTTP.Get(dataCopiedDelegate, cancellingDelegate, HTTP.BuildUri(hostname, remotePath, args), proxy, localPath, timeout_ms);
 }
Exemplo n.º 11
0
        /// <summary>
        /// Blocks until events are sent on the session, then processes any received events and adds them
        /// to eventQueue. Will always add at least one event to eventQueue.
        /// This function should be used with XenServer up to version 6.0. For XenServer higher than 6.0, GetEvents() should be used instead.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="eventQueue"></param>
        /// <param name="cancelled"></param>
        public static void GetNextEvents(Session session, LockFreeQueue<ObjectChange> eventQueue, HTTP.FuncBool cancelled)
        {
            Proxy_Event[] proxyEvents;

            try
            {
                proxyEvents = Event.next(session);
            }
            catch (WebException e)
            {
                // Catch timeout, and turn it into an EventNextBlockedException so we can recognise it later (CA-33145)
                if (e.Status == WebExceptionStatus.Timeout)
                    throw new EventNextBlockedException();
                else
                    throw;
            }

            if (proxyEvents.Length == 0)
                throw new IOException("Event.next() returned no events; the server is misbehaving.");

            if (cancelled())
                return;

            //We want to do the marshalling on this bg thread so as not to block the gui thread
            foreach (Proxy_Event proxyEvent in proxyEvents)
            {
                ObjectChange objectChange = ProcessEvent(proxyEvent);

                if (objectChange != null)
                    eventQueue.Enqueue(objectChange);
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Blocks until events are sent on the session, or timeout is reached, then processes any received events and adds them
        /// to eventQueue. This function implements the new event system, available in API version 1.9. 
        /// In the new event system, (GetAllRecords + GetEvents) sequence will replace (RegisterForEvents + DownloadObjects + GetNextEvents).
        /// </summary>
        /// <param name="session"></param>
        /// <param name="eventQueue"></param>
        /// <param name="cancelled"></param>
        /// <param name="legacyEventSystem">True if legacy event system (event.next) should be used.</param>
        /// <param name="token">A token used by event.from(). 
        /// It should be the empty string when event.from is first called, which is the replacement of get_all_records.
        /// </param>
        public static void GetEvents(Session session, LockFreeQueue<ObjectChange> eventQueue, HTTP.FuncBool cancelled, bool legacyEventSystem, ref string token)
        {
            if (legacyEventSystem)
            {
                GetNextEvents(session, eventQueue, cancelled);
                return;
            }

            Proxy_Event[] proxyEvents;
            try
            {
                var classes = new [] { "*" }; // classes that we are interested in receiving events from
                var eventResult = Event.from(session, classes, token, EVENT_FROM_TIMEOUT);
                token = eventResult.token;
                proxyEvents = eventResult.events;
            }
            catch (WebException e)
            {
                // Catch timeout, and turn it into an EventNextBlockedException so we can recognise it later (CA-33145)
                if (e.Status == WebExceptionStatus.Timeout)
                    throw new EventNextBlockedException();
                else
                    throw;
            }

            if (cancelled())
                return;

            //We want to do the marshalling on this bg thread so as not to block the gui thread
            foreach (Proxy_Event proxyEvent in proxyEvents)
            {
                ObjectChange objectChange = ProcessEvent(proxyEvent);

                if (objectChange != null)
                    eventQueue.Enqueue(objectChange);
            }
        }
Exemplo n.º 13
0
        /// <sumary>
        /// Gets all objects from the server. Used in order to fill the cache.
        /// This function implements the new event system, available from in API version 1.9.
        /// In the new event system, (GetAllRecords + GetEvents) sequence will replace (RegisterForEvents + DownloadObjects + GetNextEvents).
        /// </summary>
        /// <param name="session">The session over which to download the objects. Must not be null.</param>
        /// <param name="changes">The queue that the ObjectChanges will be put into. Must not be null.</param>
        /// <param name="cancelled">Used by GetEvents().</param>
        /// <param name="legacyEventSystem">True if legacy event system (event.next) should to be used.</param>
        /// <param name="token">Used by GetEvents().</param>
        public static void GetAllObjects(Session session, LockFreeQueue<ObjectChange> changes, HTTP.FuncBool cancelled, bool legacyEventSystem, ref string token)
        {
            if (legacyEventSystem)
            {
                DownloadObjects(session, changes);
                return;
            }

            // download objects that are not covered by event.from(), e.g. Roles
            List<ObjectChange> list = new List<ObjectChange>();
            Download_Role(session, list);
            foreach (ObjectChange o in list)
                changes.Enqueue(o);

            // get all objects with event.from()
            token = "";
            GetEvents(session, changes, cancelled, false, ref token);
        }
Exemplo n.º 14
0
 public static Stream GET(Uri uri, IXenConnection connection, bool timeout, bool do_log, bool isForXenServer = true)
 {
     if (do_log)
         log.DebugFormat("HTTP GETTING file from {0}", uri);
     return HTTP.GET(uri, XenAdminConfigManager.Provider.GetProxyFromSettings(connection, isForXenServer), XenAdminConfigManager.Provider.GetProxyTimeout(timeout));
 }
Exemplo n.º 15
0
 public static Stream PUT(Uri uri, long ContentLength, bool timeout, bool do_log)
 {
     if (do_log)
         log.DebugFormat("HTTP PUTTING file to {0}", uri);
     return HTTP.PUT(uri, XenAdminConfigManager.Provider.GetProxyFromSettings(null), ContentLength, XenAdminConfigManager.Provider.GetProxyTimeout(timeout));
 }
Exemplo n.º 16
0
 public static void get_system_status(HTTP.DataCopiedDelegate dataCopiedDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
     string hostname, IWebProxy proxy, string path, string task_id, string session_id, string entries, string output)
 {
     Get(dataCopiedDelegate, cancellingDelegate, timeout_ms, hostname, "/system-status", proxy, path,
         "task_id", task_id, "session_id", session_id, "entries", entries, "output", output);
 }
Exemplo n.º 17
0
 private static void Get(HTTP.DataCopiedDelegate dataCopiedDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
     string hostname, string remotePath, IWebProxy proxy, string localPath, params object[] args)
 {
     HTTP.Get(dataCopiedDelegate, cancellingDelegate, HTTP.BuildUri(hostname, remotePath, args), proxy, localPath, timeout_ms);
 }
Exemplo n.º 18
0
 public static void get_wlb_report(HTTP.DataCopiedDelegate dataCopiedDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
     string hostname, IWebProxy proxy, string path, string task_id, string session_id, string report, params string[] args /* alternate names & values */)
 {
     Get(dataCopiedDelegate, cancellingDelegate, timeout_ms, hostname, "/wlb_report", proxy, path,
         "task_id", task_id, "session_id", session_id, "report", report, args);
 }
Exemplo n.º 19
0
 public static void get_export_raw_vdi(HTTP.DataCopiedDelegate dataCopiedDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
     string hostname, IWebProxy proxy, string path, string task_id, string session_id, string vdi)
 {
     Get(dataCopiedDelegate, cancellingDelegate, timeout_ms, hostname, "/export_raw_vdi", proxy, path,
         "task_id", task_id, "session_id", session_id, "vdi", vdi);
 }
Exemplo n.º 20
0
 public static void host_rrd(HTTP.DataCopiedDelegate dataCopiedDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
     string hostname, IWebProxy proxy, string path, string task_id, string session_id, bool json)
 {
     Get(dataCopiedDelegate, cancellingDelegate, timeout_ms, hostname, "/host_rrd", proxy, path,
         "task_id", task_id, "session_id", session_id, "json", json);
 }
Exemplo n.º 21
0
 public static void get_pool_patch_download(HTTP.DataCopiedDelegate dataCopiedDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
     string hostname, IWebProxy proxy, string path, string task_id, string session_id, string uuid)
 {
     Get(dataCopiedDelegate, cancellingDelegate, timeout_ms, hostname, "/pool_patch_download", proxy, path,
         "task_id", task_id, "session_id", session_id, "uuid", uuid);
 }
Exemplo n.º 22
0
 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);
 }
Exemplo n.º 23
0
        private static String PollTaskForResult(IXenConnection connection, ref Session session,
            HTTP.FuncBool cancellingDelegate, XenRef<Task> task)
        {
            //Program.AssertOffEventThread();

            task_status_type status;

            do
            {
                if (cancellingDelegate != null && cancellingDelegate())
                    throw new XenAdmin.CancelledException();

                System.Threading.Thread.Sleep(500);

                status = (task_status_type)Task.DoWithSessionRetry(connection, ref session,
                    (Task.TaskStatusOp)Task.get_status, task.opaque_ref);
            }
            while (status == task_status_type.pending || status == task_status_type.cancelling);

            if (cancellingDelegate != null && cancellingDelegate())
                throw new XenAdmin.CancelledException();

            if (status == task_status_type.failure)
            {
                throw new Failure(Task.get_error_info(session, task));
            }
            else
            {
                return Task.get_result(session, task);
            }
        }
Exemplo n.º 24
0
 public static void put_import_metadata(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)
 {
     Put(progressDelegate, cancellingDelegate, timeout_ms, hostname, "/import_metadata", proxy, path,
         "task_id", task_id, "session_id", session_id, "restore", restore, "force", force);
 }
Exemplo n.º 25
0
 /// <summary>
 /// A general HttpPut method, with delegates for progress and cancelling
 /// </summary>
 /// <param name="progressDelegate">Delegate called periodically (500ms) with percent complete</param>
 /// <param name="cancellingDelegate">Delegate called periodically to see if need to cancel</param>
 /// <param name="timeout">Whether to apply a timeout</param>
 /// <param name="task">The task used to track this http get</param>
 /// <param name="session">Session used to make api calls (may be replaced if logged out, hence ref)</param>
 /// <param name="path">Path to file to put</param>
 /// <returns>The result of the task passed in</returns>
 public static String Put(XenAPI.HTTP.UpdateProgressDelegate progressDelegate, HTTP.FuncBool cancellingDelegate, bool timeout,
     IXenConnection connection, XenRef<Task> task, ref Session session, string path,
     string hostname, Delegate f, params object[] p)
 {
     return Put(progressDelegate, cancellingDelegate, XenAdminConfigManager.Provider.GetProxyTimeout(timeout),
                connection, task, ref session, path, hostname, f, p);
 }
Exemplo n.º 26
0
 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,
         "task_id", task_id, "session_id", session_id, "vdi", vdi);
 }
Exemplo n.º 27
0
        private static String PollTaskForResult(IXenConnection connection, ref Session session,
            HTTP.FuncBool cancellingDelegate, XenRef<Task> task, bool timeout = false)
        {
            //Program.AssertOffEventThread();

            task_status_type status;
            int tries = POLL_FOR_TASK_RESULT_TIMEOUT / POLL_FOR_TASK_RESULT_SLEEP_INTERVAL;
            do
            {
                if (cancellingDelegate != null && cancellingDelegate())
                    throw new XenAdmin.CancelledException();

                System.Threading.Thread.Sleep(POLL_FOR_TASK_RESULT_SLEEP_INTERVAL);
                tries--;

                status = (task_status_type)Task.DoWithSessionRetry(connection, ref session,
                    (Task.TaskStatusOp)Task.get_status, task.opaque_ref);
            }
            while ((status == task_status_type.pending || status == task_status_type.cancelling) && (!timeout || tries > 0));

            if (cancellingDelegate != null && cancellingDelegate())
                throw new XenAdmin.CancelledException();

            if (status == task_status_type.failure)
            {
                throw new Failure(Task.get_error_info(session, task));
            }
            else
            {
                return Task.get_result(session, task);
            }
        }
Exemplo n.º 28
0
 public static void put_pool_patch_upload(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, "/pool_patch_upload", proxy, path,
         "task_id", task_id, "session_id", session_id);
 }
Exemplo n.º 29
0
 public static Stream CONNECT(Uri uri, IXenConnection connection, string session, bool timeout)
 {
     return(HTTP.CONNECT(uri, XenAdminConfigManager.Provider.GetProxyFromSettings(connection), session, XenAdminConfigManager.Provider.GetProxyTimeout(timeout)));
 }
Exemplo n.º 30
0
 public static void rrd_updates(HTTP.DataCopiedDelegate dataCopiedDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
     string hostname, IWebProxy proxy, string path, string task_id, string session_id, long start, string cf, long interval, bool host, string uuid, bool json)
 {
     Get(dataCopiedDelegate, cancellingDelegate, timeout_ms, hostname, "/rrd_updates", proxy, path,
         "task_id", task_id, "session_id", session_id, "start", start, "cf", cf, "interval", interval, "host", host, "uuid", uuid, "json", json);
 }
Exemplo n.º 31
0
 public static Stream GET(Uri uri, IXenConnection connection, bool timeout)
 {
     return(HTTP.GET(uri, XenAdminConfigManager.Provider.GetProxyFromSettings(connection, true), XenAdminConfigManager.Provider.GetProxyTimeout(timeout)));
 }
Exemplo n.º 32
0
 public static Stream CONNECT(Uri uri, IXenConnection connection, string session, bool timeout, bool do_log)
 {
     if (do_log)
         log.DebugFormat("HTTP CONNECTING to {0}", uri);
     return HTTP.CONNECT(uri, XenAdminConfigManager.Provider.GetProxyFromSettings(connection), session, XenAdminConfigManager.Provider.GetProxyTimeout(timeout));
 }