Пример #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());
            }
        }