/// <summary>
        /// Starts the monitor mode.
        /// </summary>
        /// <param name="fsmonitor">The fs monitor.</param>
        /// <param name="sshClient">The SSH client.</param>
        /// <param name="sftpClient">The SFTP client.</param>
        /// <param name="shellStream">The shell stream.</param>
        /// <param name="verbOptions">The verb options.</param>
        private static void StartMonitorMode(
            FileSystemMonitor fsmonitor,
            SshClient sshClient,
            SftpClient sftpClient,
            ShellStream shellStream,
            MonitorVerbOptions verbOptions)
        {
            fsmonitor.FileSystemEntryChanged += (s, e) =>
            {
                // Detect changes to the monitor file by ignoring deletions and checking file paths.
                if (e.ChangeType != FileSystemEntryChangeType.FileAdded &&
                    e.ChangeType != FileSystemEntryChangeType.FileModified)
                {
                    return;
                }

                // If the change was not in the monitor file, then ignore it
                if (e.Path.ToLowerInvariant().Equals(verbOptions.MonitorFile.ToLowerInvariant()) == false)
                {
                    return;
                }

                // Create a new deployment once
                CreateNewDeployment(sshClient, sftpClient, shellStream, verbOptions);
            };

            Terminal.WriteLine("File System Monitor is now running.");
            Terminal.WriteLine("Writing a new monitor file will trigger a new deployment.");
            Terminal.WriteLine("Press H for help!");
            Terminal.WriteLine("Ground Control to Major Tom: Have a nice trip in space!.", ConsoleColor.DarkCyan);
        }
Esempio n. 2
0
        /// <summary>
        /// Executes the monitor verb. Using a legacy method.
        /// </summary>
        /// <param name="verbOptions">The verb options.</param>
        /// <exception cref="DirectoryNotFoundException">Source Path ' + sourcePath + ' was not found.</exception>
        internal static void ExecuteMonitorVerbLegacy(MonitorVerbOptions verbOptions)
        {
            // Initialize Variables
            _isDeploying      = false;
            _deploymentNumber = 1;

            // Normalize and show the options to the user so he knows what he's doing
            NormalizeMonitorVerbOptions(verbOptions);
            PrintMonitorOptions(verbOptions);

            // Create the FS Monitor and connection info
            var fsmonitor            = new FileSystemMonitor(1, verbOptions.SourcePath);
            var simpleConnectionInfo = new PasswordConnectionInfo(
                verbOptions.Host,
                verbOptions.Port,
                verbOptions.Username,
                verbOptions.Password);

            // Validate source path exists
            if (Directory.Exists(verbOptions.SourcePath) == false)
            {
                throw new DirectoryNotFoundException("Source Path '" + verbOptions.SourcePath + "' was not found.");
            }

            // Instantiate an SFTP client and an SSH client
            // SFTP will be used to transfer the files and SSH to execute pre-deployment and post-deployment commands
            using (var sftpClient = new SftpClient(simpleConnectionInfo))
            {
                // SSH will be used to execute commands and to get the output back from the program we are running
                using (var sshClient = new SshClient(simpleConnectionInfo))
                {
                    // Connect SSH and SFTP clients
                    EnsureMonitorConnection(sshClient, sftpClient, verbOptions);

                    // Create the shell stream so we can get debugging info from the post-deployment command
                    using (var shellStream = CreateShellStream(sshClient))
                    {
                        // Starts the FS Monitor and binds the event handler
                        StartMonitorMode(fsmonitor, sshClient, sftpClient, shellStream, verbOptions);

                        // Allows user interaction with the shell
                        StartUserInteraction(sshClient, sftpClient, shellStream, verbOptions);

                        // When we quit, we stop the monitor and disconnect the clients
                        StopMonitorMode(sftpClient, sshClient, fsmonitor);
                    }
                }
            }
        }
        private static void StopMonitorMode(SftpClient sftpClient, SshClient sshClient, FileSystemMonitor fsmonitor)
        {
            Terminal.WriteLine();

            fsmonitor.Stop();
            Terminal.WriteLine("File System monitor was stopped.");

            if (sftpClient.IsConnected)
            {
                sftpClient.Disconnect();
            }

            Terminal.WriteLine("SFTP client disconnected.");

            if (sshClient.IsConnected)
            {
                sshClient.Disconnect();
            }

            Terminal.WriteLine("SSH client disconnected.");
            Terminal.WriteLine("Application will exit now.");
        }
Esempio n. 4
0
        /// <summary>
        /// Starts the user interaction.
        /// </summary>
        /// <param name="fsMonitor">The fs monitor.</param>
        /// <param name="sshClient">The SSH client.</param>
        /// <param name="sftpClient">The SFTP client.</param>
        /// <param name="shellStream">The shell stream.</param>
        /// <param name="verbOptions">The verb options.</param>
        private static void StartUserInteraction(FileSystemMonitor fsMonitor, SshClient sshClient, SftpClient sftpClient, ShellStream shellStream, MonitorVerbOptions verbOptions)
        {
            ForwardShellStreamInput = false;

            while (true)
            {
                var readKey = Console.ReadKey(true);

                if (readKey.Key == ConsoleKey.F1)
                {
                    ForwardShellStreamInput = !ForwardShellStreamInput;
                    if (ForwardShellStreamInput)
                    {
                        ConsoleManager.WriteLine("    >> Entered console input forwarding.", ConsoleColor.Green);
                        ForwardShellStreamOutput = true;
                    }
                    else
                    {
                        ConsoleManager.WriteLine("    >> Left console input forwarding.", ConsoleColor.Red);
                    }


                    continue;
                }

                if (ForwardShellStreamInput)
                {
                    if (readKey.Key == ConsoleKey.Enter)
                    {
                        shellStream.WriteLine(string.Empty);
                    }
                    else
                    {
                        shellStream.WriteByte((byte)readKey.KeyChar);
                    }

                    shellStream.Flush();
                    continue;
                }

                if (readKey.Key == ConsoleKey.Q)
                {
                    break;
                }

                if (readKey.Key == ConsoleKey.C)
                {
                    ConsoleManager.Clear();
                    continue;
                }

                if (readKey.Key == ConsoleKey.N)
                {
                    CreateNewDeployment(sshClient, sftpClient, shellStream, verbOptions);
                    continue;
                }

                if (readKey.Key == ConsoleKey.E)
                {
                    RunSshClientCommand(sshClient, verbOptions);
                    continue;
                }

                if (readKey.Key == ConsoleKey.S)
                {
                    RunShellStreamCommand(shellStream, verbOptions);
                    continue;
                }

                if (readKey.Key != ConsoleKey.H)
                {
                    ConsoleManager.WriteLine("Unrecognized command '" + readKey.KeyChar + "' -- Press 'H' to get a list of available commands.", ConsoleColor.Red);
                }

                if (readKey.Key == ConsoleKey.H)
                {
                    var helpColor = ConsoleColor.Cyan;
                    ConsoleManager.WriteLine("Console help", helpColor);
                    ConsoleManager.WriteLine("    H    Prints this screen", helpColor);
                    ConsoleManager.WriteLine("    Q    Quits this application", helpColor);
                    ConsoleManager.WriteLine("    C    Clears the screen", helpColor);
                    ConsoleManager.WriteLine("    N    Force a deployment cycle", helpColor);
                    ConsoleManager.WriteLine("    E    Run the Pre-deployment command", helpColor);
                    ConsoleManager.WriteLine("    S    Run the Post-deployment command", helpColor);
                    ConsoleManager.WriteLine("    F1   Toggle shell-interactive mode", helpColor);

                    ConsoleManager.WriteLine(string.Empty);
                    continue;
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Stops the monitor mode by closing SFTP and SSH connections, and stopping the File System Monitor.
        /// </summary>
        /// <param name="sftpClient">The SFTP client.</param>
        /// <param name="sshClient">The SSH client.</param>
        /// <param name="fsMonitor">The fs monitor.</param>
        private static void StopMonitorMode(SftpClient sftpClient, SshClient sshClient, FileSystemMonitor fsMonitor)
        {
            ConsoleManager.WriteLine(string.Empty);

            fsMonitor.Stop();
            ConsoleManager.WriteLine("File System monitor was stopped.");

            if (sftpClient.IsConnected == true)
            {
                sftpClient.Disconnect();
            }

            ConsoleManager.WriteLine("SFTP client disconnected.");

            if (sshClient.IsConnected == true)
            {
                sshClient.Disconnect();
            }

            ConsoleManager.WriteLine("SSH client disconnected.");
            ConsoleManager.WriteLine("Application will exit now.");
        }