Example #1
0
        internal static PackagingResult ExecutePhase(XmlDocument manifestXml, int phase, TextWriter console = null)
        {
            var manifest         = Manifest.Parse(manifestXml, phase, true, new PackageParameter[0]);
            var executionContext = ExecutionContext.CreateForTest("packagePath", "targetPath", new string[0], "sandboxPath", manifest, phase, manifest.CountOfPhases, null, console ?? new StringWriter());
            var result           = PackageManager.ExecuteCurrentPhase(manifest, executionContext);

            RepositoryVersionInfo.Reset();
            return(result);
        }
Example #2
0
 internal static void SavePackage(Manifest manifest, ExecutionContext executionContext, bool successful, Exception execError)
 {
     SavePackage(manifest, successful ? ExecutionResult.Successful : ExecutionResult.Faulty, execError);
 }
Example #3
0
        public static PackagingResult Execute(string packagePath, string targetPath, int currentPhase,
                                              PackageParameter[] parameters, TextWriter console,
                                              RepositoryBuilder builder, bool editConnectionString = false)
        {
            var packageParameters = parameters ?? new PackageParameter[0];
            var forcedReinstall   = "true" == (packageParameters
                                               .FirstOrDefault(p => p.PropertyName.ToLowerInvariant() == "forcedreinstall")?
                                               .Value?.ToLowerInvariant() ?? "");

            var phaseCount = 1;

            var files = Directory.GetFiles(packagePath);

            Manifest  manifest = null;
            Exception manifestParsingException = null;

            if (files.Length == 1)
            {
                try
                {
                    manifest = Manifest.Parse(files[0], currentPhase, currentPhase == 0, packageParameters,
                                              forcedReinstall, editConnectionString);
                    phaseCount = manifest.CountOfPhases;
                }
                catch (Exception e)
                {
                    manifestParsingException = e;
                }
            }

            if (files.Length == 0)
            {
                throw new InvalidPackageException(SR.Errors.ManifestNotFound);
            }
            if (files.Length > 1)
            {
                throw new InvalidPackageException(SR.Errors.PackageCanContainOnlyOneFileInTheRoot);
            }
            if (manifestParsingException != null)
            {
                throw new PackagingException("Manifest parsing error. See inner exception.", manifestParsingException);
            }
            if (manifest == null)
            {
                throw new PackagingException("Manifest was not found.");
            }

            Logger.LogTitle(String.Format("Executing phase {0}/{1}", currentPhase + 1, phaseCount));

            var sandboxDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var executionContext = ExecutionContext.Create(packagePath, targetPath, Configuration.Packaging.NetworkTargets,
                                                           sandboxDirectory, manifest, currentPhase, manifest.CountOfPhases, packageParameters, console, builder);

            executionContext.LogVariables();

            PackagingResult result;

            try
            {
                result = ExecuteCurrentPhase(manifest, executionContext);
            }
            finally
            {
                if (Repository.Started())
                {
                    console?.WriteLine("-------------------------------------------------------------");
                    console?.Write("Stopping repository ... ");
                    Repository.Shutdown();
                    console?.WriteLine("Ok.");
                }
            }

            return(result);
        }
Example #4
0
        internal static PackagingResult ExecuteCurrentPhase(Manifest manifest, ExecutionContext executionContext)
        {
            var sysInstall   = manifest.SystemInstall;
            var currentPhase = executionContext.CurrentPhase;

            if (0 == currentPhase - (sysInstall ? 1 : 0))
            {
                SaveInitialPackage(manifest);
            }

            var stepElements = manifest.GetPhase(executionContext.CurrentPhase);

            var stopper = Stopwatch.StartNew();

            Logger.LogMessage("Executing steps");

            Exception phaseException = null;
            var       successful     = false;

            try
            {
                var maxStepId = stepElements.Count;
                for (int i = 0; i < maxStepId; i++)
                {
                    var stepElement = stepElements[i];
                    var step        = Step.Parse(stepElement, i, executionContext);

                    var stepStopper = Stopwatch.StartNew();
                    Logger.LogStep(step, maxStepId);
                    step.Execute(executionContext);
                    stepStopper.Stop();
                    Logger.LogMessage("-------------------------------------------------------------");
                    Logger.LogMessage("Time: " + stepStopper.Elapsed);
                    if (executionContext.Terminated)
                    {
                        LogTermination(executionContext);
                        break;
                    }
                }
                stopper.Stop();
                Logger.LogMessage("=============================================================");
                Logger.LogMessage("All steps were executed.");
                Logger.LogMessage("Aggregated time: " + stopper.Elapsed);
                Logger.LogMessage("Errors: " + Logger.Errors);
                successful = true;
            }
            catch (Exception e)
            {
                phaseException = e;
            }

            var finished = executionContext.Terminated || (executionContext.CurrentPhase == manifest.CountOfPhases - 1);

            if (successful && !finished)
            {
                return new PackagingResult {
                           NeedRestart = true, Successful = true, Errors = Logger.Errors
                }
            }
            ;

            if (executionContext.Terminated && executionContext.TerminationReason == TerminationReason.Warning)
            {
                successful = false;

                phaseException = new PackageTerminatedException(executionContext.TerminationMessage);
            }

            try
            {
                SavePackage(manifest, executionContext, successful, phaseException);
            }
            catch (Exception e)
            {
                if (phaseException != null)
                {
                    Logger.LogException(phaseException);
                }
                throw new PackagingException("Cannot save the package.", e);
            }
            finally
            {
                RepositoryVersionInfo.Reset();

                // we need to shut down messaging, because the line above uses it
                if (!executionContext.Test)
                {
                    DistributedApplication.ClusterChannel.ShutDownAsync(CancellationToken.None).GetAwaiter().GetResult();
                }
                else
                {
                    Diagnostics.SnTrace.Test.Write("DistributedApplication.ClusterChannel.ShutDown SKIPPED because it is a test context.");
                }
            }
            if (!successful && !executionContext.Terminated)
            {
                throw new ApplicationException(String.Format(SR.Errors.PhaseFinishedWithError_1, phaseException.Message), phaseException);
            }

            return(new PackagingResult {
                NeedRestart = false, Successful = successful, Terminated = executionContext.Terminated && !successful, Errors = Logger.Errors
            });
        }