private void UpdateStatusFile(object state)
        {
            string changeset = (string)state;
            IDeploymentStatusFile statusFile = _status.Open(changeset);

            statusFile.UpdateProgress(String.Format(CultureInfo.CurrentCulture,
                                                    _failedCount == 0 ? Resources.Dropbox_SynchronizingProgress : Resources.Dropbox_SynchronizingProgressWithFailure,
                                                    ((_successCount + _failedCount) * 100) / _totals,
                                                    _totals,
                                                    _failedCount));
        }
        public ILogger Log(string value, LogEntryType type)
        {
            IDeploymentStatusFile statusFile = _status.Open(_id);

            if (statusFile != null)
            {
                statusFile.UpdateProgress(value);
            }

            // No need to wrap this as we only support top-level progress
            return(_innerLogger.Log(value, type));
        }
        internal async Task <ChangeSet> Sync(DropboxInfo dropboxInfo, string branch, IRepository repository, ITracer tracer)
        {
            DropboxDeployInfo deployInfo = dropboxInfo.DeployInfo;

            // use incoming tracer since it is background work
            _tracer = tracer;

            ResetStats();

            // for Dropbox OAuth V2, the delta is collected and applied by SCM
            // simply set OldCursor as current.
            if (dropboxInfo.OAuthVersion == 2)
            {
                deployInfo.OldCursor = _settings.GetValue(CursorKey);
            }
            else if (_settings.GetValue(CursorKey) != deployInfo.OldCursor)
            {
                throw new InvalidOperationException(Resources.Error_MismatchDropboxCursor);
            }

            // initial sync, remove default content
            // for simplicity, we do it blindly whether or not in-place
            // given the end result is the same
            if (String.IsNullOrEmpty(deployInfo.OldCursor) && DeploymentHelper.IsDefaultWebRootContent(_environment.WebRootPath))
            {
                string hoststarthtml = Path.Combine(_environment.WebRootPath, Constants.HostingStartHtml);
                FileSystemHelpers.DeleteFileSafe(hoststarthtml);
            }

            if (!repository.IsEmpty())
            {
                // git checkout --force <branch>
                repository.Update(branch);
            }

            ChangeSet changeSet = null;
            string    message   = null;

            try
            {
                using (_tracer.Step("Sync with Dropbox"))
                {
                    if (dropboxInfo.OAuthVersion == 2)
                    {
                        // Fetch the deltas
                        await UpdateDropboxDeployInfo(deployInfo);
                    }

                    // Sync dropbox => repository directory
                    await ApplyChanges(dropboxInfo, useOAuth20 : dropboxInfo.OAuthVersion == 2);
                }

                message = String.Format(CultureInfo.CurrentCulture,
                                        Resources.Dropbox_Synchronized,
                                        deployInfo.Deltas.Count);
            }
            catch (Exception)
            {
                message = String.Format(CultureInfo.CurrentCulture,
                                        Resources.Dropbox_SynchronizedWithFailure,
                                        _successCount,
                                        deployInfo.Deltas.Count,
                                        _failedCount);

                throw;
            }
            finally
            {
                Logger.Log(message);

                Logger.Log(String.Format("{0} downloaded files, {1} successful retries.", _fileCount, _retriedCount));

                IDeploymentStatusFile statusFile = _status.Open(dropboxInfo.TargetChangeset.Id);
                statusFile.UpdateMessage(message);
                statusFile.UpdateProgress(String.Format(CultureInfo.CurrentCulture, Resources.Dropbox_Committing, _successCount));

                // Commit anyway even partial change
                if (repository.Commit(message, deployInfo.UserName, deployInfo.Email ?? deployInfo.UserName))
                {
                    changeSet = repository.GetChangeSet("HEAD");
                }
            }

            // Save new dropbox cursor
            LogInfo("Update dropbox cursor");
            _settings.SetValue(CursorKey, deployInfo.NewCursor);

            return(changeSet);
        }
Beispiel #4
0
        internal async Task <ChangeSet> Sync(DropboxHandler.DropboxInfo deploymentInfo, string branch, ILogger logger, IRepository repository)
        {
            DropboxDeployInfo info = deploymentInfo.DeployInfo;

            _logger = logger;

            _totals       = 0;
            _successCount = 0;
            _fileCount    = 0;
            _failedCount  = 0;
            _retriedCount = 0;

            if (_settings.GetValue(CursorKey) != info.OldCursor)
            {
                throw new InvalidOperationException(Resources.Error_MismatchDropboxCursor);
            }

            if (!repository.IsEmpty())
            {
                // git checkout --force <branch>
                repository.ClearLock();
                repository.Update(branch);
            }

            ChangeSet changeSet;
            string    message = null;

            try
            {
                using (_tracer.Step("Synch with Dropbox"))
                {
                    // Sync dropbox => repository directory
                    await ApplyChanges(deploymentInfo);
                }

                message = String.Format(CultureInfo.CurrentCulture,
                                        Resources.Dropbox_Synchronized,
                                        deploymentInfo.DeployInfo.Deltas.Count());
            }
            catch (Exception)
            {
                message = String.Format(CultureInfo.CurrentCulture,
                                        Resources.Dropbox_SynchronizedWithFailure,
                                        _successCount,
                                        deploymentInfo.DeployInfo.Deltas.Count(),
                                        _failedCount);

                throw;
            }
            finally
            {
                _logger.Log(message);

                _logger.Log(String.Format("{0} downloaded files, {1} successful retries.", _fileCount, _retriedCount));

                IDeploymentStatusFile statusFile = _status.Open(deploymentInfo.TargetChangeset.Id);
                statusFile.UpdateMessage(message);
                statusFile.UpdateProgress(String.Format(CultureInfo.CurrentCulture, Resources.Dropbox_Committing, _successCount));

                // Commit anyway even partial change
                changeSet = repository.Commit(message, String.Format("{0} <{1}>", info.UserName, info.Email));
            }

            // Save new dropboc cursor
            LogInfo("Update dropbox cursor");
            _settings.SetValue(CursorKey, info.NewCursor);

            return(changeSet);
        }
Beispiel #5
0
        private async Task <ChangesResult> GetChanges(string changeset, string accessToken, string requestUri, string cursor)
        {
            string rootUri = GetOneDriveRootUri(requestUri);

            requestUri = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", await GetItemUri(accessToken, requestUri, rootUri), "view.changes");

            ChangesResult         result     = new ChangesResult();
            IDeploymentStatusFile statusFile = _status.Open(changeset);
            var loop = 0;

            using (var client = CreateHttpClient(accessToken))
            {
                var next = cursor;
                var ids  = new Dictionary <string, OneDriveModel.ItemInfo>();
                Dictionary <string, object> changes = null;
                var serializer = new JavaScriptSerializer();
                do
                {
                    var uri = requestUri;
                    if (!string.IsNullOrWhiteSpace(next))
                    {
                        uri = string.Format(CultureInfo.InvariantCulture, "{0}?token={1}", requestUri, next);
                    }

                    ++loop;
                    statusFile.UpdateProgress(string.Format(CultureInfo.CurrentCulture, "Getting delta changes batch #{0}", loop));
                    using (_tracer.Step("Getting delta changes batch #{0}", loop))
                    {
                        using (var response = await client.GetAsync(uri))
                        {
                            changes = await ProcessResponse <Dictionary <string, object> >("GetChanges", response);
                        }
                    }

                    if (changes.ContainsKey("@changes.resync"))
                    {
                        if (string.IsNullOrEmpty(next))
                        {
                            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.OneDriveUnableToSync, changes["@changes.resync"]));
                        }

                        // resync
                        loop = 0;
                        next = null;
                        changes["@changes.hasMoreChanges"] = true;
                        result = new ChangesResult();
                        continue;
                    }

                    var items = serializer.ConvertToType <OneDriveModel.OneDriveItemCollection>(changes);

                    // changes
                    if (items != null && items.value != null && items.value.Count > 0)
                    {
                        var subResults = GetChanges(items, ids, rootUri);
                        result.DeletionChanges.AddRange(subResults.DeletionChanges);
                        result.DirectoryChanges.AddRange(subResults.DirectoryChanges);
                        result.FileChanges.AddRange(subResults.FileChanges);
                    }

                    // set next token
                    next = (string)changes["@changes.token"];
                } while ((bool)changes["@changes.hasMoreChanges"]);

                result.Cursor = next;
                return(result);
            }
        }