Exemple #1
0
        /// <summary>
        ///     Installs.
        /// </summary>
        public async Task <ActionResult <InstallProgressResultModel> > PostPerformInstall(InstallInstructions installModel)
        {
            if (installModel == null)
            {
                throw new ArgumentNullException(nameof(installModel));
            }

            var status = InstallStatusTracker.GetStatus().ToArray();

            //there won't be any statuses returned if the app pool has restarted so we need to re-read from file.
            if (status.Any() == false)
            {
                status = _installStatusTracker.InitializeFromFile(installModel.InstallId).ToArray();
            }

            //create a new queue of the non-finished ones
            var queue = new Queue <InstallTrackingItem>(status.Where(x => x.IsComplete == false));

            while (queue.Count > 0)
            {
                var item = queue.Dequeue();
                var step = _installSteps.GetAllSteps().Single(x => x.Name == item.Name);

                // if this step has any instructions then extract them
                var instruction = GetInstruction(installModel, item, step);

                // if this step doesn't require execution then continue to the next one, this is just a fail-safe check.
                if (StepRequiresExecution(step, instruction) == false)
                {
                    // set this as complete and continue
                    _installStatusTracker.SetComplete(installModel.InstallId, item.Name);
                    continue;
                }

                try
                {
                    var setupData = await ExecuteStepAsync(step, instruction);

                    // update the status
                    _installStatusTracker.SetComplete(installModel.InstallId, step.Name, setupData?.SavedStepData);

                    // determine's the next step in the queue and dequeue's any items that don't need to execute
                    var nextStep = IterateSteps(step, queue, installModel.InstallId, installModel);

                    // check if there's a custom view to return for this step
                    if (setupData != null && setupData.View.IsNullOrWhiteSpace() == false)
                    {
                        return(new InstallProgressResultModel(false, step.Name, nextStep, setupData.View,
                                                              setupData.ViewModel));
                    }

                    return(new InstallProgressResultModel(false, step.Name, nextStep));
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "An error occurred during installation step {Step}",
                                     step.Name);

                    if (ex is TargetInvocationException && ex.InnerException != null)
                    {
                        ex = ex.InnerException;
                    }

                    var installException = ex as InstallException;
                    if (installException != null)
                    {
                        return(new ValidationErrorResult(new
                        {
                            view = installException.View,
                            model = installException.ViewModel,
                            message = installException.Message
                        }));
                    }

                    return(new ValidationErrorResult(new
                    {
                        step = step.Name,
                        view = "error",
                        message = ex.Message
                    }));
                }
            }

            _installStatusTracker.Reset();
            return(new InstallProgressResultModel(true, "", ""));
        }
Exemple #2
0
        /// <summary>
        /// Does the install
        /// </summary>
        /// <returns></returns>
        public InstallProgressResultModel PostPerformInstall(InstallInstructions installModel)
        {
            if (installModel == null)
            {
                throw new ArgumentNullException("installModel");
            }

            var status = InstallStatusTracker.GetStatus().ToArray();

            //there won't be any statuses returned if the app pool has restarted so we need to re-read from file.
            if (status.Any() == false)
            {
                status = InstallStatusTracker.InitializeFromFile(installModel.InstallId).ToArray();
            }

            //create a new queue of the non-finished ones
            var queue = new Queue <InstallTrackingItem>(status.Where(x => x.IsComplete == false));

            while (queue.Count > 0)
            {
                var stepStatus = queue.Dequeue();

                var step = InstallHelper.GetAllSteps().Single(x => x.Name == stepStatus.Name);

                JToken instruction = null;
                //If this step has any instructions then extract them
                if (installModel.Instructions.Any(x => x.Key == step.Name))
                {
                    instruction = installModel.Instructions[step.Name];
                }

                //If this step doesn't require execution then continue to the next one, this is just a fail-safe check.
                if (StepRequiresExecution(step, instruction) == false)
                {
                    //set this as complete and continue
                    InstallStatusTracker.SetComplete(installModel.InstallId, stepStatus.Name, null);
                    continue;
                }

                try
                {
                    var setupData = ExecuteStep(step, instruction);

                    //update the status
                    InstallStatusTracker.SetComplete(installModel.InstallId, step.Name, setupData != null ? setupData.SavedStepData : null);

                    //Determine's the next step in the queue and dequeue's any items that don't need to execute
                    var nextStep = IterateNextRequiredStep(step, queue, installModel.InstallId, installModel);

                    //check if there's a custom view to return for this step
                    if (setupData != null && setupData.View.IsNullOrWhiteSpace() == false)
                    {
                        return(new InstallProgressResultModel(false, step.Name, nextStep, setupData.View, setupData.ViewModel));
                    }

                    return(new InstallProgressResultModel(false, step.Name, nextStep));
                }
                catch (Exception ex)
                {
                    LogHelper.Error <InstallApiController>("An error occurred during installation step " + step.Name, ex);

                    if (ex is TargetInvocationException && ex.InnerException != null)
                    {
                        ex = ex.InnerException;
                    }

                    var installException = ex as InstallException;
                    if (installException != null)
                    {
                        throw new HttpResponseException(Request.CreateValidationErrorResponse(new
                        {
                            view    = installException.View,
                            model   = installException.ViewModel,
                            message = installException.Message
                        }));
                    }

                    throw new HttpResponseException(Request.CreateValidationErrorResponse(new
                    {
                        step    = step.Name,
                        view    = "error",
                        message = ex.Message
                    }));
                }
            }

            InstallStatusTracker.Reset();
            return(new InstallProgressResultModel(true, "", ""));
        }