Example #1
0
        public bool AddStep(string type, string command, string message, string[] stepParams)
        {
            // creat step
            DeploymentModelStepsStep step = new DeploymentModelStepsStep();

            step.Type    = type;
            step.Command = command;
            step.Message = message;

            // add command params to step
            List <DeploymentModelStepsStepCommandParam> commandParams = new List <DeploymentModelStepsStepCommandParam>();

            for (int iString = 0; iString < stepParams.Length;)
            {
                DeploymentModelStepsStepCommandParam commandParam = new DeploymentModelStepsStepCommandParam();
                commandParam.Name          = stepParams[iString++];
                commandParam.ParameterName = stepParams[iString++];
                commandParams.Add(commandParam);
            }

            step.CommandParam = commandParams.ToArray();

            // add step to steps
            return(AddStep(step));
        }
        private string GetCommandForStep(DeploymentModelStepsStep step)
        {
            StringBuilder command = new StringBuilder(step.Command + "  ");

            if (step.CommandParam != null)
            {
                for (int i = 0; i < step.CommandParam.Length; i++)
                {
                    String paramValue = GetParamValue(step.CommandParam[i].ParameterName);

                    //Handle the cases where user need to just add switch to command..
                    paramValue = String.IsNullOrEmpty(paramValue) == true ? String.Empty : paramValue;
                    if (String.IsNullOrEmpty(paramValue) == true)
                    {
                        command.AppendFormat(" -{0} ", step.CommandParam[i].Name);
                    }
                    //Handle the cases where param value begins with @..which indicates that value is hashtable..so dont wrap it in double quotes.
                    else if (paramValue.StartsWith("@", StringComparison.OrdinalIgnoreCase) == true)
                    {
                        command.AppendFormat(" -{0} {1} ", step.CommandParam[i].Name, paramValue);
                    }
                    else
                    {
                        command.AppendFormat(" -{0} \"{1}\" ", step.CommandParam[i].Name, paramValue);
                    }
                }
            }

            return(command.ToString());
        }
Example #3
0
        private static List <StepInfo> ParseCommandParamsFile(string commandParamsFileName)
        {
            // read command params file
            string commandParamsFileText = File.ReadAllText(commandParamsFileName, Encoding.Default);

            string[] commandParamsLines = commandParamsFileText.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            // parse command params
            List <StepInfo> stepInfos = commandParamsLines.Select(commandParamsLine =>
            {
                if (commandParamsLine.StartsWith("Command,"))
                {
                    return(null);
                }

                var commandParamsSplitLine = commandParamsLine.CsvSplit();

                DeploymentModelStepsStep step = new DeploymentModelStepsStep();

                // command string format: <type>:<command>[.<index>]
                // index is used in case we have multiple steps with the same command e.g. Cmdlet:Add-LoadAssembly.1, Cmdlet:Add-LoadAssembly.2 etc.
                string commandString        = commandParamsSplitLine[0];
                string[] commandStringParts = commandString.Split(":".ToCharArray());

                step.Type    = commandStringParts[0];
                step.Command = commandStringParts[1].Split(".".ToCharArray())[0];

                List <DeploymentModelStepsStepCommandParam> commandParams = new List <DeploymentModelStepsStepCommandParam>();
                for (int iCol = 1; iCol < commandParamsSplitLine.Length;)
                {
                    if (string.IsNullOrEmpty(commandParamsSplitLine[iCol]))
                    {
                        break;
                    }

                    DeploymentModelStepsStepCommandParam commandParam = new DeploymentModelStepsStepCommandParam();
                    commandParam.Name          = commandParamsSplitLine[iCol++];
                    commandParam.ParameterName = commandParamsSplitLine[iCol++];
                    commandParams.Add(commandParam);
                }

                step.CommandParam = commandParams.ToArray();

                StepInfo stepInfo      = new StepInfo();
                stepInfo.commandString = commandString;
                stepInfo.step          = step;

                return(stepInfo);
            }
                                                                  ).ToList <StepInfo>();

            stepInfos.RemoveAll(s => s == null); // remove nulls

            return(stepInfos);
        }
Example #4
0
        public bool AddStep(DeploymentModelStepsStep stepToAdd)
        {
            _steps.Add(stepToAdd);

            // update model
            DeploymentModelSteps            steps    = _model.Items[1] as DeploymentModelSteps;
            List <DeploymentModelStepsStep> stepList = steps.Step.ToList();

            stepList.Add(stepToAdd);
            steps.Step      = stepList.ToArray();
            _model.Items[1] = steps;

            return(true);
        }
        private bool ChangeWorkingDir(DeploymentModelStepsStep step)
        {
            try
            {
                String location = GetParamValue(step.CommandParam[0].ParameterName);
                SessionState.Path.SetLocation(location);
            }
            catch (Exception exc)
            {
                Log(LogType.Error, "Exception while changing working directory: " + exc.Message);
                return(false);
            }

            return(true);
        }
        private bool ExecutePS1File(DeploymentModelStepsStep step)
        {
            bool bRet = true;

            Console.WriteLine(step.Message);
            Console.WriteLine("File: " + step.Command);
            try
            {
                string filePath = step.Command;
                if (!Path.IsPathRooted(filePath))
                {
                    filePath = Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), step.Command);
                }

                Runspace executePs1FileRunspace = Runspace.DefaultRunspace;
                using (Pipeline executePsCmdletPipeline = executePs1FileRunspace.CreateNestedPipeline())
                {
                    Command scriptCommand = new Command(filePath);
                    if (step.CommandParam != null)
                    {
                        for (int i = 0; i < step.CommandParam.Length; i++)
                        {
                            DeploymentModelStepsStepCommandParam param = step.CommandParam[i];
                            String paramValue = GetParamValue(param.ParameterName);
                            if (String.IsNullOrEmpty(param.Name) == true)
                            {
                                scriptCommand.Parameters.Add(null, paramValue);
                            }
                            else
                            {
                                scriptCommand.Parameters.Add(param.Name, paramValue);
                            }
                        }
                    }
                    executePsCmdletPipeline.Error.DataReady  += new EventHandler(Error_DataReadyExecutePsCmdlet);
                    executePsCmdletPipeline.Output.DataReady += new EventHandler(Output_DataReadyExecutePsCmdlet);
                    executePsCmdletPipeline.Commands.Add(scriptCommand);
                    executePsCmdletPipeline.Invoke();
                }
            }
            catch (Exception exc)
            {
                Log(LogType.Error, "Exception while executing PS1 file: " + exc.Message);
                bRet = false;
            }
            return(bRet);
        }
        protected override void ProcessRecord()
        {
            if (_isXmlPathValid == false || !ValidateParameters())
            {
                return;
            }

            Init();

            if (IsEmulator() == false)
            {
                string pubSettingsFilePath = string.Empty;
                if (TryParsePubSettingFilePathParam(out pubSettingsFilePath) == false)
                {
                    if (!DownloadPublishSettings())
                    {
                        return;
                    }
                }
                else
                {
                    _controller.SetParameterByName("PublishSettingsFilePath", pubSettingsFilePath);
                }
            }

            for (int i = 0; ; i++)
            {
                DeploymentModelStepsStep step = _controller.GetStepAtIndex(i);
                Console.WriteLine("===============================");

                if (step == null)
                {
                    break;
                }

                if (!ProcessStep(step))
                {
                    break;
                }
            }
        }
        private bool ProcessStep(DeploymentModelStepsStep step)
        {
            bool bRet = true;

            switch (step.Type)
            {
            case StepType.CmdLet:
                String command = GetCommandForStep(step);
                Log(LogType.Begin, command);
                bRet = ExecutePsCmdlet(step.Message, command);
                Log(LogType.End, command);
                break;

            case StepType.ChangeWorkingDir:
                Log(LogType.Begin, StepType.ChangeWorkingDir);
                bRet = ChangeWorkingDir(step);
                Log(LogType.End, StepType.ChangeWorkingDir);
                break;

            case StepType.PowershellScript:
                Log(LogType.Begin, step.Command);
                bRet = ExecuteCommand(step.Command);
                Log(LogType.End, step.Command);
                break;

            case StepType.PS1File:
                Log(LogType.Begin, step.Command);
                bRet = ExecutePS1File(step);
                Log(LogType.End, step.Command);
                break;

            default:
                Log(LogType.Error, "Unrecognized step type inside deployment model xml: " + step.Type);
                bRet = false;
                break;
            }

            return(bRet);
        }
Example #9
0
        public bool AddStep(string type, string command, string message, string[] stepParams)
        {
            // creat step
            DeploymentModelStepsStep step = new DeploymentModelStepsStep();
            step.Type = type;
            step.Command = command;
            step.Message = message;

            // add command params to step
            List<DeploymentModelStepsStepCommandParam> commandParams = new List<DeploymentModelStepsStepCommandParam>();

            for (int iString = 0; iString < stepParams.Length; )
            {
                DeploymentModelStepsStepCommandParam commandParam = new DeploymentModelStepsStepCommandParam();
                commandParam.Name = stepParams[iString++];
                commandParam.ParameterName = stepParams[iString++];
                commandParams.Add(commandParam);
            }

            step.CommandParam = commandParams.ToArray();

            // add step to steps
            return AddStep(step);
        }
        private bool ProcessStep(DeploymentModelStepsStep step)
        {
            bool bRet = true;

            switch (step.Type)
            {
                case StepType.CmdLet:
                    String command = GetCommandForStep(step);
                    Log(LogType.Begin, command);
                    bRet = ExecutePsCmdlet(step.Message, command);
                    Log(LogType.End, command);
                    break;

                case StepType.ChangeWorkingDir:
                    Log(LogType.Begin, StepType.ChangeWorkingDir);
                    bRet = ChangeWorkingDir(step);
                    Log(LogType.End, StepType.ChangeWorkingDir);
                    break;

                case StepType.PowershellScript:
                    Log(LogType.Begin, step.Command);
                    bRet = ExecuteCommand(step.Command);
                    Log(LogType.End, step.Command);
                    break;

                case StepType.PS1File:
                    Log(LogType.Begin, step.Command);
                    bRet = ExecutePS1File(step);
                    Log(LogType.End, step.Command);
                    break;

                default:
                    Log(LogType.Error, "Unrecognized step type inside deployment model xml: " + step.Type);
                    bRet = false;
                    break;
            }

            return bRet;
        }
        private string GetCommandForStep(DeploymentModelStepsStep step)
        {
            StringBuilder command = new StringBuilder(step.Command + "  ");

            if (step.CommandParam != null)
            {
                for (int i = 0; i < step.CommandParam.Length; i++)
                {
                    String paramValue = GetParamValue(step.CommandParam[i].ParameterName);

                    //Handle the cases where user need to just add switch to command..
                    paramValue = String.IsNullOrEmpty(paramValue) == true ? String.Empty : paramValue;
                    if (String.IsNullOrEmpty(paramValue) == true)
                    {
                        command.AppendFormat(" -{0} ", step.CommandParam[i].Name);
                    }
                    //Handle the cases where param value begins with @..which indicates that value is hashtable..so dont wrap it in double quotes.
                    else if (paramValue.StartsWith("@", StringComparison.OrdinalIgnoreCase) == true)
                    {
                        command.AppendFormat(" -{0} {1} ", step.CommandParam[i].Name, paramValue);
                    }
                    else
                    {
                        command.AppendFormat(" -{0} \"{1}\" ", step.CommandParam[i].Name, paramValue);
                    }
                }
            }

            return command.ToString();
        }
        private bool ExecutePS1File(DeploymentModelStepsStep step)
        {
            bool bRet = true;
            Console.WriteLine(step.Message);
            Console.WriteLine("File: " + step.Command);
            try
            {
                string filePath = step.Command;
                if (!Path.IsPathRooted(filePath))
                    filePath = Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), step.Command);

                Runspace executePs1FileRunspace = Runspace.DefaultRunspace;
                using (Pipeline executePsCmdletPipeline = executePs1FileRunspace.CreateNestedPipeline())
                {
                    Command scriptCommand = new Command(filePath);
                    if (step.CommandParam != null)
                    {
                        for (int i = 0; i < step.CommandParam.Length; i++)
                        {
                            DeploymentModelStepsStepCommandParam param = step.CommandParam[i];
                            String paramValue = GetParamValue(param.ParameterName);
                            if (String.IsNullOrEmpty(param.Name) == true)
                            {
                                scriptCommand.Parameters.Add(null, paramValue);
                            }
                            else
                            {
                                scriptCommand.Parameters.Add(param.Name, paramValue);
                            }
                        }
                    }
                    executePsCmdletPipeline.Error.DataReady += new EventHandler(Error_DataReadyExecutePsCmdlet);
                    executePsCmdletPipeline.Output.DataReady += new EventHandler(Output_DataReadyExecutePsCmdlet);
                    executePsCmdletPipeline.Commands.Add(scriptCommand);
                    executePsCmdletPipeline.Invoke();
                }
            }
            catch (Exception exc)
            {
                Log(LogType.Error, "Exception while executing PS1 file: " + exc.Message);
                bRet = false;
            }
            return bRet;
        }
        private bool ChangeWorkingDir(DeploymentModelStepsStep step)
        {
            try
            {
                String location = GetParamValue(step.CommandParam[0].ParameterName);
                SessionState.Path.SetLocation(location);
            }
            catch (Exception exc)
            {
                Log(LogType.Error, "Exception while changing working directory: " + exc.Message);
                return false;
            }

            return true;
        }
        private string GetCommandForStep(DeploymentModelStepsStep step)
        {
            StringBuilder command = new StringBuilder(step.Command + "  ");

            if (step.CommandParam != null)
            {
                for (int i = 0; i < step.CommandParam.Length; i++)
                {
                    String paramValue = GetParamValue(step.CommandParam[i].ParameterName);

                    //Handle the cases where user need to just add switch to command..
                    paramValue = String.IsNullOrEmpty(paramValue) == true ? String.Empty : paramValue;
                    command.AppendFormat(" -{0} \"{1}\" ", step.CommandParam[i].Name, paramValue);
                }
            }

            return command.ToString();
        }
        private bool ExecutePS1File(DeploymentModelStepsStep step)
        {
            bool bRet = true;
            Console.WriteLine(step.Message);
            Console.WriteLine("File: " + step.Command);

            try
            {
                using (Runspace space = RunspaceFactory.CreateRunspace(this.Host))
                {
                    space.Open();
                    using (PowerShell scriptExecuter = PowerShell.Create())
                    {
                        scriptExecuter.Runspace = space;
                        // if relative, path is relative to dll
                        string filePath = step.Command;
                        if (!Path.IsPathRooted(filePath))
                            filePath = Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), step.Command);

                        scriptExecuter.AddCommand(filePath);
                        if (step.CommandParam != null)
                        {
                            for (int i = 0; i < step.CommandParam.Length; i++)
                            {
                                DeploymentModelStepsStepCommandParam param = step.CommandParam[i];
                                String paramValue = GetParamValue(param.ParameterName);
                                if (String.IsNullOrEmpty(param.Name) == true)
                                {
                                    scriptExecuter.AddArgument(paramValue);
                                }
                                else
                                {
                                    scriptExecuter.AddParameter(param.Name, paramValue);
                                }
                            }
                        }

                        _executePS1Blocker = new AutoResetEvent(false);
                        PSDataCollection<PSObject> output = new PSDataCollection<PSObject>();

                        output.DataAdded += new EventHandler<DataAddedEventArgs>(output_DataAdded);
                        scriptExecuter.InvocationStateChanged += new EventHandler<PSInvocationStateChangedEventArgs>(scriptExecuter_InvocationStateChanged);

                        IAsyncResult asyncResult = scriptExecuter.BeginInvoke<PSObject, PSObject>(null, output);
                        _executePS1Blocker.WaitOne();

                        PSDataStreams errorStream = scriptExecuter.Streams;
                        foreach (ErrorRecord error in errorStream.Error)
                        {
                            Console.WriteLine(error.Exception.Message);
                            bRet = false;
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine("Exception while executing PS1 file: " + exc.Message);
                bRet = false;
            }

            return bRet;
        }
Example #16
0
        private static List<StepInfo> ParseCommandParamsFile(string commandParamsFileName)
        {
            // read command params file
            string commandParamsFileText = File.ReadAllText(commandParamsFileName, Encoding.Default);
            string[] commandParamsLines = commandParamsFileText.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            // parse command params
            List<StepInfo> stepInfos = commandParamsLines.Select(commandParamsLine =>
                {
                    if (commandParamsLine.StartsWith("Command,"))
                        return null;

                    var commandParamsSplitLine = commandParamsLine.CsvSplit();

                    DeploymentModelStepsStep step = new DeploymentModelStepsStep();

                    // command string format: <type>:<command>[.<index>]
                    // index is used in case we have multiple steps with the same command e.g. Cmdlet:Add-LoadAssembly.1, Cmdlet:Add-LoadAssembly.2 etc.
                    string commandString = commandParamsSplitLine[0];
                    string[] commandStringParts = commandString.Split(":".ToCharArray());

                    step.Type = commandStringParts[0];
                    step.Command = commandStringParts[1].Split(".".ToCharArray())[0];

                    List<DeploymentModelStepsStepCommandParam> commandParams = new List<DeploymentModelStepsStepCommandParam>();
                    for (int iCol = 1; iCol < commandParamsSplitLine.Length; )
                    {
                        if (string.IsNullOrEmpty(commandParamsSplitLine[iCol]))
                            break;

                        DeploymentModelStepsStepCommandParam commandParam = new DeploymentModelStepsStepCommandParam();
                        commandParam.Name = commandParamsSplitLine[iCol++];
                        commandParam.ParameterName = commandParamsSplitLine[iCol++];
                        commandParams.Add(commandParam);
                    }

                    step.CommandParam = commandParams.ToArray();

                    StepInfo stepInfo = new StepInfo();
                    stepInfo.commandString = commandString;
                    stepInfo.step = step;

                    return stepInfo;
                }
            ).ToList<StepInfo>();

            stepInfos.RemoveAll(s => s == null); // remove nulls

            return stepInfos;
        }
Example #17
0
        public bool AddStep(DeploymentModelStepsStep stepToAdd)
        {
            _steps.Add(stepToAdd);

            // update model
            DeploymentModelSteps steps = _model.Items[1] as DeploymentModelSteps;
            List<DeploymentModelStepsStep> stepList = steps.Step.ToList();
            stepList.Add(stepToAdd);
            steps.Step = stepList.ToArray();
            _model.Items[1] = steps;

            return true;
        }