Example #1
0
        public static void RunScript(string scriptText, string directory, Action <string> writeLineOutput, RedirectOutputOptions redirectOutputOption)
        {
            using (Runspace runspace = RunspaceFactory.CreateRunspace())
            {
                runspace.Open();
                if (!string.IsNullOrWhiteSpace(directory))
                {
                    runspace.SessionStateProxy.Path.SetLocation(directory);
                }

                using (Pipeline pipeline = runspace.CreatePipeline())
                {
                    pipeline.Commands.AddScript(scriptText);

                    pipeline.InvokeAsync();

                    var handles = new WaitHandle[2];
                    handles[0] = pipeline.Output.WaitHandle;
                    handles[1] = pipeline.Error.WaitHandle;
                    pipeline.Input.Close();

                    while (pipeline.PipelineStateInfo.State == PipelineState.Running)
                    {
                        switch (WaitHandle.WaitAny(handles))
                        {
                        case 0:
                            while (pipeline.Output.Count > 0)
                            {
                                foreach (PSObject result in pipeline.Output.NonBlockingRead())
                                {
                                    if (redirectOutputOption.HasFlag(RedirectOutputOptions.RedirectStandardOutput))
                                    {
                                        writeLineOutput(result.ToString());
                                    }
                                }
                            }
                            break;

                        case 1:
                            while (pipeline.Error.Count > 0)
                            {
                                foreach (PSObject result in pipeline.Error.NonBlockingRead())
                                {
                                    if (redirectOutputOption.HasFlag(RedirectOutputOptions.RedirectErrorOutput))
                                    {
                                        writeLineOutput(result.ToString());
                                    }
                                }
                            }
                            break;

                        default:

                            break;
                        }
                    }

                    runspace.Close();
                }
            }
        }
Example #2
0
        private Task <bool> StartDebuggerAsync(SshDeltaCopy.Options options, DebugOptions debugOptions, bool deploy, bool debug, Func <string, Task> writeOutput, RedirectOutputOptions redirectOutputOption)
        {
            NLogService.TraceEnteringMethod(Logger);

            return(Task.Run <bool>(async() =>
            {
                var errorHelpText = new StringBuilder();
                Action <string> writeLineOutput = s => writeOutput(s + Environment.NewLine).Wait();

                try
                {
                    errorHelpText.AppendLine($"SSH Login: {options.Username}@{options.Host}:{options.Port} Directory: {options.DestinationDirectory}");

                    using (SshDeltaCopy sshDeltaCopy = new SshDeltaCopy(options))
                    {
                        sshDeltaCopy.LogOutput = writeLineOutput;

                        if (deploy)
                        {
                            Logger.Info($"StartDebuggerAsync - deploy");

                            errorHelpText.AppendLine($"SSH: Start deployment from '{options.SourceDirectory}' to '{options.DestinationDirectory}'.");
                            sshDeltaCopy.DeployDirectory(options.SourceDirectory, options.DestinationDirectory);
                            errorHelpText.AppendLine($"SSH Deployment was successful.");
                            // We are creating mdb files on local machine with pdb2mdb
                            //var createMdbCommand = sshDeltaCopy.RunSSHCommand($@"find . -regex '.*\(exe\|dll\)' -exec {debugOptions.UserSettings.SSHPdb2mdbCommand} {{}} \;", false);
                            //msgOutput(createMdbCommand.Result);
                        }

                        if (debug)
                        {
                            Logger.Info($"StartDebuggerAsync - debug");

                            await DebugAsync(debugOptions, writeOutput, redirectOutputOption, errorHelpText, writeLineOutput, sshDeltaCopy);
                        }
                    }
                }
                catch (Exception ex)
                {
                    var additionalErrorMessage = $"SSHDebugger: {ex.Message}\n\nExecuted steps:\n{errorHelpText.ToString()}";
                    await writeOutput(additionalErrorMessage);
                    throw new Exception(additionalErrorMessage, ex);
                }

                return true;
            }));
        }
Example #3
0
 protected abstract Task DebugAsync(DebugOptions debugOptions, Func <string, Task> writeOutput, RedirectOutputOptions redirectOutputOption, StringBuilder errorHelpText, Action <string> writeLineOutput, SshDeltaCopy sshDeltaCopy);
Example #4
0
        private Task <bool> StartDebuggerAsync(DebugOptions debugOptions, bool deploy, bool debug, Func <string, Task> writeOutput, RedirectOutputOptions redirectOutputOption)
        {
            NLogService.TraceEnteringMethod(Logger);

            return(Task.Run <bool>(async() =>
            {
                var errorHelpText = new StringBuilder();
                Action <string> writeLineOutput = s => writeOutput(s + Environment.NewLine).Wait();

                try
                {
                    var destinationDirectory = debugOptions.UserSettings.WindowsDeployPath;
                    if (string.IsNullOrWhiteSpace(destinationDirectory))
                    {
                        destinationDirectory = debugOptions.OutputDirectory;
                    }
                    else if (!Path.IsPathRooted(destinationDirectory))
                    {
                        destinationDirectory = Path.Combine(debugOptions.OutputDirectory, destinationDirectory);
                    }

                    errorHelpText.AppendLine($"Directory: {destinationDirectory}");

                    if (deploy)
                    {
                        Logger.Info($"StartDebuggerAsync - deploy");

                        errorHelpText.AppendLine($"Local: Start deployment from '{debugOptions.OutputDirectory}' to '{destinationDirectory}'.");
                        Directory.CreateDirectory(destinationDirectory);
                        DirectoryCopy(debugOptions.OutputDirectory, destinationDirectory, copySubDirs: true, overwrite: true, writeOutput: writeOutput);
                        errorHelpText.AppendLine($"Local Deployment was successful.");
                    }

                    if (debug)
                    {
                        Logger.Info($"StartDebuggerAsync - debug");

                        var killCommandText = debugOptions.PreDebugScript;

                        errorHelpText.AppendLine($"Local: Stop previous mono processes with the PreDebugScript");
                        errorHelpText.AppendLine(killCommandText);
                        Logger.Info($"Run PreDebugScript: {killCommandText}");

                        PowershellExecuter.RunScript(killCommandText, destinationDirectory, writeLineOutput, redirectOutputOption);

                        // TODO
                        //if (killCommand.ExitStatus != 0 || !string.IsNullOrWhiteSpace(killCommand.Error))
                        //{
                        //    var error = $"SSH script error in PreDebugScript:\n{killCommand.CommandText}\n{killCommand.Error}";
                        //    //errorHelpText.AppendLine(error);
                        //    Logger.Error(error);
                        //}

                        var monoDebugCommand = debugOptions.DebugScript;

                        errorHelpText.AppendLine($"Local: Start mono debugger");
                        errorHelpText.AppendLine(monoDebugCommand);
                        Logger.Info($"Run DebugScript: {monoDebugCommand}");

                        // TODO if DebugScript fails no error is shown - very bad!
                        await writeOutput(errorHelpText.ToString());
                        PowershellExecuter.RunScript(monoDebugCommand, destinationDirectory, writeLineOutput, redirectOutputOption);

                        // TODO
                        //await RunCommandAndRedirectOutputAsync(cmd, writeOutput, redirectOutputOption);

                        //if (cmd.ExitStatus != 0 || !string.IsNullOrWhiteSpace(cmd.Error))
                        //{
                        //    var error = $"SSH script error in DebugScript:\n{cmd.CommandText}\n{cmd.Error}";
                        //    //errorHelpText.AppendLine(error);
                        //    Logger.Error(error);

                        //    throw new Exception(error);
                        //}
                    }
                }
                catch (Exception ex)
                {
                    var additionalErrorMessage = $"SSHDebugger: {ex.Message}\n\nExecuted steps:\n{errorHelpText.ToString()}";
                    await writeOutput(additionalErrorMessage);
                    throw new Exception(additionalErrorMessage, ex);
                }

                return true;
            }));
        }
Example #5
0
 public Task <bool> RunAndDebugAsync(DebugOptions debugOptions, Func <string, Task> writeOutput, RedirectOutputOptions redirectOutputOption = RedirectOutputOptions.None)
 {
     NLogService.TraceEnteringMethod(Logger);
     writeOutput("Start RunAndDebug over SSH ...");
     return(StartDebuggerAsync(_sshOptions, debugOptions, false, true, writeOutput, redirectOutputOption));
 }
Example #6
0
        private async Task RunCommandAndRedirectOutputAsync(Renci.SshNet.SshCommand cmd, Func <string, Task> writeOutput, RedirectOutputOptions redirectOutputOption)
        {
            await Task.Run(() =>
            {
                var asynch = cmd.BeginExecute();

                var taskList = new List <Task>();

                if (redirectOutputOption.HasFlag(RedirectOutputOptions.RedirectStandardOutput))
                {
                    var stream = cmd.OutputStream;
                    taskList.Add(RedirectStreamAsync(writeOutput, asynch, stream));
                }

                if (redirectOutputOption.HasFlag(RedirectOutputOptions.RedirectErrorOutput))
                {
                    var stream = cmd.ExtendedOutputStream;
                    taskList.Add(RedirectStreamAsync(writeOutput, asynch, stream));
                }

                Task.WaitAny(taskList.ToArray());

                cmd.EndExecute(asynch);
            });
        }
Example #7
0
 public Task <bool> DeployAsync(DebugOptions debugOptions, Func <string, Task> writeOutput, RedirectOutputOptions redirectOutputOption = RedirectOutputOptions.None)
 {
     NLogService.TraceEnteringMethod(Logger);
     writeOutput("Start Deploy locally ...");
     return(StartDebuggerAsync(debugOptions, true, false, writeOutput, redirectOutputOption));
 }
Example #8
0
        private Task <bool> StartDebuggerAsync(SshDeltaCopy.Options options, DebugOptions debugOptions, bool deploy, bool debug, Func <string, Task> writeOutput, RedirectOutputOptions redirectOutputOption)
        {
            NLogService.TraceEnteringMethod();

            return(Task.Run <bool>(async() =>
            {
                var errorHelpText = new StringBuilder();
                Action <string> writeLineOutput = s => writeOutput(s + Environment.NewLine).Wait();

                try
                {
                    errorHelpText.AppendLine($"SSH Login: {options.Username}@{options.Host}:{options.Port} Directory: {options.DestinationDirectory}");

                    using (SshDeltaCopy sshDeltaCopy = new SshDeltaCopy(options))
                    {
                        sshDeltaCopy.LogOutput = writeLineOutput;

                        if (deploy)
                        {
                            NLogService.Logger.Info($"StartDebuggerAsync - deploy");

                            errorHelpText.AppendLine($"SSH: Start deployment from '{options.SourceDirectory}' to '{options.DestinationDirectory}'.");
                            sshDeltaCopy.DeployDirectory(options.SourceDirectory, options.DestinationDirectory);
                            errorHelpText.AppendLine($"SSH Deployment was successful.");
                            // We are creating mdb files on local machine with pdb2mdb
                            //var createMdbCommand = sshDeltaCopy.RunSSHCommand($@"find . -regex '.*\(exe\|dll\)' -exec {debugOptions.UserSettings.SSHPdb2mdbCommand} {{}} \;", false);
                            //msgOutput(createMdbCommand.Result);
                        }

                        if (debug)
                        {
                            NLogService.Logger.Info($"StartDebuggerAsync - debug");

                            errorHelpText.AppendLine($"SSH: Stop previous mono processes.");

                            var killCommandText = debugOptions.PreDebugScript;
                            var killCommand = sshDeltaCopy.RunSSHCommand(killCommandText, false);
                            writeLineOutput(killCommand.Result);

                            NLogService.Logger.Info($"Run PreDebugScript: {killCommandText}");

                            if (killCommand.ExitStatus != 0 || !string.IsNullOrWhiteSpace(killCommand.Error))
                            {
                                var error = $"SSH script error in PreDebugScript:\n{killCommand.CommandText}\n{killCommand.Error}";
                                //errorHelpText.AppendLine(error);
                                NLogService.Logger.Error(error);
                            }

                            var monoDebugCommand = debugOptions.DebugScript;

                            errorHelpText.AppendLine($"SSH: Start mono debugger");
                            errorHelpText.AppendLine(monoDebugCommand);

                            NLogService.Logger.Info($"Run DebugScript: {monoDebugCommand}");

                            // TODO if DebugScript fails no error is shown - very bad!
                            await writeOutput(errorHelpText.ToString());
                            var cmd = sshDeltaCopy.CreateSSHCommand(monoDebugCommand);
                            await RunCommandAndRedirectOutputAsync(cmd, writeOutput, redirectOutputOption);

                            if (cmd.ExitStatus != 0 || !string.IsNullOrWhiteSpace(cmd.Error))
                            {
                                var error = $"SSH script error in DebugScript:\n{cmd.CommandText}\n{cmd.Error}";
                                //errorHelpText.AppendLine(error);
                                NLogService.Logger.Error(error);

                                throw new Exception(error);
                            }

                            //var monoDebugCommandResult = await Task.Factory.FromAsync(cmd.BeginExecute(), result => cmd.Result);
                            //msgOutput(monoDebugCommandResult);
                        }
                    }
                }
                catch (Exception ex)
                {
                    var additionalErrorMessage = $"SSHDebugger: {ex.Message}\n\nExecuted steps:\n{errorHelpText.ToString()}";
                    await writeOutput(additionalErrorMessage);
                    throw new Exception(additionalErrorMessage, ex);
                }

                return true;
            }));
        }
Example #9
0
 public static Task <Task> DebugAsync(SshDeltaCopy.Options options, DebugOptions debugOptions, Func <string, Task> writeOutput, RedirectOutputOptions redirectOutputOption = RedirectOutputOptions.None)
 {
     writeOutput("Start DeployAndDebug over SSH ...");
     return(StartDebuggerAsync(options, debugOptions, false, true, writeOutput, redirectOutputOption));
 }
Example #10
0
        private static Task <Task> StartDebuggerAsync(SshDeltaCopy.Options options, DebugOptions debugOptions, bool deploy, bool debug, Action <string> writeOutput, RedirectOutputOptions redirectOutputOption)
        {
            return(Task.Factory.StartNew(async() =>
            {
                var errorHelpText = new StringBuilder();
                Action <string> writeLineOutput = s => writeOutput(s + Environment.NewLine);

                try
                {
                    errorHelpText.AppendLine($"SSH Login: {options.Username}@{options.Host}:{options.Port} Directory: {options.DestinationDirectory}");

                    using (SshDeltaCopy sshDeltaCopy = new SshDeltaCopy(options))
                    {
                        sshDeltaCopy.LogOutput = writeLineOutput;

                        if (deploy)
                        {
                            errorHelpText.AppendLine($"SSH: Start deployment from '{options.SourceDirectory}' to '{options.DestinationDirectory}'.");
                            sshDeltaCopy.DeployDirectory(options.SourceDirectory, options.DestinationDirectory);
                            errorHelpText.AppendLine($"SSH Deployment was successful.");
                            // We are creating mdb files on local machine with pdb2mdb
                            //var createMdbCommand = sshDeltaCopy.RunSSHCommand($@"find . -regex '.*\(exe\|dll\)' -exec {debugOptions.UserSettings.SSHPdb2mdbCommand} {{}} \;", false);
                            //msgOutput(createMdbCommand.Result);
                        }

                        if (debug)
                        {
                            errorHelpText.AppendLine($"SSH: Stop previous mono processes.");

                            var killCommandTextOld = $"kill $(lsof -i | grep 'mono' | grep '\\*:{debugOptions.UserSettings.SSHMonoDebugPort}' | awk '{{print $2}}')";//$"kill $(ps w | grep '[m]ono --debugger-agent=address' | awk '{{print $1}}')";
                            var killCommandText = debugOptions.PreDebugScript;
                            var killCommand = sshDeltaCopy.RunSSHCommand(killCommandText, false);
                            writeLineOutput(killCommand.Result);

                            //errorHelpText.AppendLine($"SSH: Stop previous mono processes. Second try.");

                            //// If lsof is unknown and ps aux has an bug (https://bugs.launchpad.net/linaro-oe/+bug/1192942)
                            //killCommandText = $"kill $(ps w | grep '[m]ono --debugger-agent=address' | awk '{{print $1}}')";
                            //var killCommand2 = sshDeltaCopy.RunSSHCommand(killCommandText, false);
                            //writeLineOutput(killCommand2.Result);

                            var monoDebugCommandOld = $"mono --debugger-agent=address=0.0.0.0:{debugOptions.UserSettings.SSHMonoDebugPort},transport=dt_socket,server=y --debug=mdb-optimizations {debugOptions.TargetExeFileName} {debugOptions.StartArguments} &";
                            var monoDebugCommand = debugOptions.DebugScript;

                            errorHelpText.AppendLine($"SSH: Start mono debugger");
                            errorHelpText.AppendLine(monoDebugCommand);

                            var cmd = sshDeltaCopy.CreateSSHCommand(monoDebugCommand);
                            await RunCommandAndRedirectOutput(cmd, writeOutput, redirectOutputOption);

                            //var monoDebugCommandResult = await Task.Factory.FromAsync(cmd.BeginExecute(), result => cmd.Result);
                            //msgOutput(monoDebugCommandResult);
                        }
                    }
                }
                catch (Exception ex)
                {
                    var additionalErrorMessage = $"SSHDebugger: {ex.Message}\n\nExecuted steps:\n{errorHelpText.ToString()}";
                    writeOutput(additionalErrorMessage);
                    throw new Exception(additionalErrorMessage, ex);
                }
            }));
        }
 protected override Task DebugAsync(DebugOptions debugOptions, Func <string, Task> writeOutput, RedirectOutputOptions redirectOutputOption, StringBuilder errorHelpText, Action <string> writeLineOutput, SshDeltaCopy sshDeltaCopy)
 {
     // will be handled in MonoVisualStudioExtension
     return(Task.CompletedTask);
 }
Example #12
0
        protected override async Task DebugAsync(DebugOptions debugOptions, Func <string, Task> writeOutput, RedirectOutputOptions redirectOutputOption, StringBuilder errorHelpText, Action <string> writeLineOutput, SshDeltaCopy sshDeltaCopy)
        {
            errorHelpText.AppendLine($"SSH: Stop previous mono processes.");

            var killCommandText = debugOptions.PreDebugScript;
            var killCommand     = sshDeltaCopy.RunSSHCommand(killCommandText, false);

            writeLineOutput(killCommand.Result);

            Logger.Info($"Run PreDebugScript: {killCommandText}");

            if (killCommand.ExitStatus != 0 || !string.IsNullOrWhiteSpace(killCommand.Error))
            {
                var error = $"SSH script error in PreDebugScript:\n{killCommand.CommandText}\n{killCommand.Error}";
                //errorHelpText.AppendLine(error);
                Logger.Error(error);
            }

            var monoDebugCommand = debugOptions.DebugScript;

            errorHelpText.AppendLine($"SSH: Start mono debugger");
            errorHelpText.AppendLine(monoDebugCommand);

            Logger.Info($"Run DebugScript: {monoDebugCommand}");

            // TODO if DebugScript fails no error is shown - very bad!
            await writeOutput(errorHelpText.ToString());

            var cmd = sshDeltaCopy.CreateSSHCommand(monoDebugCommand);

            await RunCommandAndRedirectOutputAsync(cmd, writeOutput, redirectOutputOption);

            if (cmd.ExitStatus != 0 || !string.IsNullOrWhiteSpace(cmd.Error))
            {
                var error = $"SSH script error in DebugScript:\n{cmd.CommandText}\n{cmd.Error}";
                //errorHelpText.AppendLine(error);
                Logger.Error(error);

                throw new Exception(error);
            }

            //var monoDebugCommandResult = await Task.Factory.FromAsync(cmd.BeginExecute(), result => cmd.Result);
            //msgOutput(monoDebugCommandResult);
        }