示例#1
0
        private async Task ProcessReleaseActionContainerAsync(
            int stageId,
            IEnumerable <ReleaseAction> actions,
            string targetPath)
        {
            if (!actions.Any())
            {
                return;
            }

            var scriptActionElements             = new List <ScriptAction>();
            var scriptManualInterventionElements = new List <ScriptManualIntervention>();

            var enabledActions = actions.Where(a => a.Enabled);

            /* Find all of the actions that are not rollback blocks and create a ScriptAction that encompasses both the
             * stuff extracted from the RM XAML workflow and stuff we pull out of the RM database.
             */
            foreach (var action in actions)
            {
                if (action is RollbackBlock)
                {
                    continue;
                }

                if (!action.Enabled)
                {
                    this.ScriptGenerationNotification?.Invoke(this, GetActionGenerationArgs(action, Warning));
                    continue;
                }

                this.ScriptGenerationNotification?.Invoke(this, GetActionGenerationArgs(action, ActionStart));

                if (action.ItemType == BlockType.ManualIntervention)
                {
                    scriptManualInterventionElements.Add(
                        await this.CreateScriptManualIntervention((ManualIntervention)action));
                }
                else
                {
                    var component = await this.componentRepo.GetComponentByIdAsync(action.WorkflowActivityId, stageId);

                    await this.deployerToolRepo.WriteToolToDiskAsync(component.DeployerToolId, this.deployerToolsPath);

                    var scriptAction = CreateScriptAction(component, action);

                    scriptAction.CommandIsExtractedTool = possibleToolExtensions.Select(extension => this.fs.Exists(Path.Combine(this.deployerToolsPath, scriptAction.Command + extension))).Any(t => t == true);

                    scriptActionElements.Add(scriptAction);
                }

                this.ScriptGenerationNotification?.Invoke(this, GetActionGenerationArgs(action, ActionEnd));
            }

            // Resolve the rollback actions within this container -- a rollback is a subcontainer full of actions, tied to a specific step (or all steps, in the case of rollback always)
            var rollbackActions = await this.ResolveRollbackActions(stageId, enabledActions, scriptActionElements);

            /* We'll need to resolve unique script parameters, since RM can have non-unique parameters. For example,
             *  Action 1: __Hello __ = "placeholder"
             *  Action 2: __Hello__ = "Bar"
             *  We can't pass in a parameter called $Hello to a script, since it requires different values.
             *  Thus, we need to flatten out the list of parameters across the entire server/tag container and "uniqueify" them.
             */
            var elementsToResolve =
                rollbackActions.SelectMany(ra => ra.Value.Actions).Union(scriptActionElements).OrderBy(s => s.Sequence);

            UniquePropertyResolver.ResolveProperties(elementsToResolve);

            // Invoke the T4 template for the container and for any rollback/rollback always blocks and write them out to disk
            var rollbackParameters =
                rollbackActions.SelectMany(s => s.Value.Actions).SelectMany(s => s.ConfigurationVariables).ToList();
            var scriptParameters = scriptActionElements.SelectMany(s => s.ConfigurationVariables).ToList();

            var releaseScriptText = CreateScriptFromTemplate(
                scriptActionElements,
                scriptManualInterventionElements,
                scriptParameters.Union(rollbackParameters).Distinct(new ConfigurationVariableEqualityComparer()),
                this.generateInitializationScript);

            this.fs.WriteAllText(Path.Combine(targetPath, this.releaseScriptName), releaseScriptText);

            foreach (var rollbackGroup in rollbackActions)
            {
                string rollbackScriptName = $"{rollbackGroup.Key}.ps1";

                var rollbackScript = CreateScriptFromTemplate(
                    rollbackGroup.Value.Actions,
                    rollbackGroup.Value.ManualInterventions,
                    rollbackGroup.Value.Actions.SelectMany(s => s.ConfigurationVariables).Distinct(new ConfigurationVariableEqualityComparer()),
                    false);
                this.fs.WriteAllText(Path.Combine(targetPath, rollbackScriptName), rollbackScript);
            }

            if (this.generateInitializationScript)
            {
                var initScript = new InitializationScript
                {
                    Session =
                        new Dictionary <string, object>
                    {
                        {
                            "releaseActions",
                            scriptActionElements
                        },
                        {
                            "scriptParams",
                            scriptParameters
                            .Union(
                                rollbackParameters)
                            .Distinct(new ConfigurationVariableEqualityComparer())
                        }
                    }
                };
                initScript.Initialize();
                this.fs.WriteAllText(Path.Combine(targetPath, this.initScriptName), initScript.TransformText());
            }
        }
        // Module defining this command


        // Optional custom code for this activity


        /// <summary>
        /// Returns a configured instance of System.Management.Automation.PowerShell, pre-populated with the command to run.
        /// </summary>
        /// <param name="context">The NativeActivityContext for the currently running activity.</param>
        /// <returns>A populated instance of System.Management.Automation.PowerShell</returns>
        /// <remarks>The infrastructure takes responsibility for closing and disposing the PowerShell instance returned.</remarks>
        protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context)
        {
            System.Management.Automation.PowerShell invoker       = global::System.Management.Automation.PowerShell.Create();
            System.Management.Automation.PowerShell targetCommand = invoker.AddCommand(PSCommandName);

            // Initialize the arguments

            if (DefinitionName.Expression != null)
            {
                targetCommand.AddParameter("DefinitionName", DefinitionName.Get(context));
            }

            if (DefinitionPath.Expression != null)
            {
                targetCommand.AddParameter("DefinitionPath", DefinitionPath.Get(context));
            }

            if (Type.Expression != null)
            {
                targetCommand.AddParameter("Type", Type.Get(context));
            }

            if (Name.Expression != null)
            {
                targetCommand.AddParameter("Name", Name.Get(context));
            }

            if (ScriptBlock.Expression != null)
            {
                targetCommand.AddParameter("ScriptBlock", ScriptBlock.Get(context));
            }

            if (Credential.Expression != null)
            {
                targetCommand.AddParameter("Credential", Credential.Get(context));
            }

            if (FilePath.Expression != null)
            {
                targetCommand.AddParameter("FilePath", FilePath.Get(context));
            }

            if (LiteralPath.Expression != null)
            {
                targetCommand.AddParameter("LiteralPath", LiteralPath.Get(context));
            }

            if (Authentication.Expression != null)
            {
                targetCommand.AddParameter("Authentication", Authentication.Get(context));
            }

            if (InitializationScript.Expression != null)
            {
                targetCommand.AddParameter("InitializationScript", InitializationScript.Get(context));
            }

            if (RunAs32.Expression != null)
            {
                targetCommand.AddParameter("RunAs32", RunAs32.Get(context));
            }

            if (PSVersion.Expression != null)
            {
                targetCommand.AddParameter("PSVersion", PSVersion.Get(context));
            }

            if (InputObject.Expression != null)
            {
                targetCommand.AddParameter("InputObject", InputObject.Get(context));
            }

            if (ArgumentList.Expression != null)
            {
                targetCommand.AddParameter("ArgumentList", ArgumentList.Get(context));
            }


            return(new ActivityImplementationContext()
            {
                PowerShellInstance = invoker
            });
        }