protected string SshExec(ResourceNode node, string command, string args = "", AErrorResolver errorResolver = null)
        {
            int sshResult = 0;
            string sshOut = "";
            string sshErr = "";
            string sshCommand = command + " " + args;

            var sshCommands = new[] {sshCommand};

            if (sshCommand.Contains('\n'))
            {
                sshCommands = sshCommand.Split('\n');
            }
            /*
            var connectionInfo = new Ssh2.ConnectionInfo(node.Services.ExecutionUrl, node.Credentials.Username, new Ssh2.AuthenticationMethod[] {
                new Ssh2.KeyboardInteractiveAuthenticationMethod(node.Credentials.Username),
                new Ssh2.PasswordAuthenticationMethod(node.Credentials.Username, node.Credentials.Password)
            });
            */

            var sshExec = _sshPool.GetSshSession(true, node);

                //Ssh.SshExec(node.NodeAddress, node.Credentials.Username, node.Credentials.Password);
            //Log.Info("Ssh command to execute : " + command + " ");
            try
            {
                foreach (var s in sshCommands)
                {
                    if (!s.Contains("pbsnodes") && !s.Contains("qstat"))
                        Log.Info("Ssh command to execute: " + s);

                    /*
                    sshExec.Connect();
                    sshResult = sshExec.RunCommand(sshCommand, ref sshOut, ref sshErr);
                    /**/

                    var ssh = sshExec.RunCommand(s); // todo : using (var ssh = new...)
                    //ssh.Execute();

                    sshResult = ssh.ExitStatus;
                    sshErr = ssh.Error;
                    sshOut = ssh.Result;

                    if (!String.IsNullOrWhiteSpace(sshErr))
                    {
                        break;
                    }
                }

                /**/
            }
            catch (Exception e)
            {
                Log.Warn(e.Message);
                throw;
            }
            finally
            {
                /*/*
                sshExec.Close();
                /*
                if (sshExec.IsConnected)
                    sshExec.Disconnect();
                /***/
                _sshPool.PushSession(sshExec);
            }

            //sshErr = sshErr.Replace('.', ' '); // Cert creation emits many dots
            if (errorResolver == null && sshResult != 0 /*!String.IsNullOrWhiteSpace(sshErr)*/)
            {
                throw new Exception(String.Format("Ssh execution error. Command: \"{0}\". Code: {1}, StdOut: {2}, StdErr: {3}", sshCommand, sshResult, sshOut, sshErr));
            }
            if (errorResolver != null && !String.IsNullOrWhiteSpace(sshErr))
            {
                var resIn = new Dictionary<String, Object>();
                resIn[AErrorResolver.SSH_RESULT] = sshOut;
                resIn[AErrorResolver.SSH_EXIT_CODE] = sshResult;
                resIn[AErrorResolver.SSH_COMMAND] = sshCommand;
                resIn[AErrorResolver.SSH_ERROR] = sshErr;

                errorResolver.Resolve(resIn);
            }

            if (!command.Contains("pbsnodes") && !command.Contains("qstat"))
                Log.Info("ssh execution result : " + sshOut);

            return sshOut;
        }
Exemple #2
0
        protected string SshExec(ResourceNode node, string command, string args = "", AErrorResolver errorResolver = null)
        {
            int    sshResult  = 0;
            string sshOut     = "";
            string sshErr     = "";
            string sshCommand = command + " " + args;

            var sshCommands = new[] { sshCommand };

            if (sshCommand.Contains('\n'))
            {
                sshCommands = sshCommand.Split('\n');
            }

            /*
             * var connectionInfo = new Ssh2.ConnectionInfo(node.Services.ExecutionUrl, node.Credentials.Username, new Ssh2.AuthenticationMethod[] {
             *  new Ssh2.KeyboardInteractiveAuthenticationMethod(node.Credentials.Username),
             *  new Ssh2.PasswordAuthenticationMethod(node.Credentials.Username, node.Credentials.Password)
             * });
             */


            var sshExec = _sshPool.GetSshSession(true, node);

            //Ssh.SshExec(node.NodeAddress, node.Credentials.Username, node.Credentials.Password);
            //Log.Info("Ssh command to execute : " + command + " ");
            try
            {
                foreach (var s in sshCommands)
                {
                    if (!s.Contains("pbsnodes") && !s.Contains("qstat"))
                    {
                        Log.Info("Ssh command to execute: " + s);
                    }

                    /*
                     * sshExec.Connect();
                     * sshResult = sshExec.RunCommand(sshCommand, ref sshOut, ref sshErr);
                     * /**/

                    var ssh = sshExec.RunCommand(s); // todo : using (var ssh = new...)
                    //ssh.Execute();

                    sshResult = ssh.ExitStatus;
                    sshErr    = ssh.Error;
                    sshOut    = ssh.Result;

                    if (!String.IsNullOrWhiteSpace(sshErr))
                    {
                        break;
                    }
                }

                /**/
            }
            catch (Exception e)
            {
                Log.Warn(e.Message);
                throw;
            }
            finally
            {
                /*/*
                 * sshExec.Close();
                 * /*
                 * if (sshExec.IsConnected)
                 *  sshExec.Disconnect();
                 * /***/
                _sshPool.PushSession(sshExec);
            }

            //sshErr = sshErr.Replace('.', ' '); // Cert creation emits many dots
            if (errorResolver == null && sshResult != 0 /*!String.IsNullOrWhiteSpace(sshErr)*/)
            {
                throw new Exception(String.Format("Ssh execution error. Command: \"{0}\". Code: {1}, StdOut: {2}, StdErr: {3}", sshCommand, sshResult, sshOut, sshErr));
            }
            if (errorResolver != null && !String.IsNullOrWhiteSpace(sshErr))
            {
                var resIn = new Dictionary <String, Object>();
                resIn[AErrorResolver.SSH_RESULT]    = sshOut;
                resIn[AErrorResolver.SSH_EXIT_CODE] = sshResult;
                resIn[AErrorResolver.SSH_COMMAND]   = sshCommand;
                resIn[AErrorResolver.SSH_ERROR]     = sshErr;

                errorResolver.Resolve(resIn);
            }

            if (!command.Contains("pbsnodes") && !command.Contains("qstat"))
            {
                Log.Info("ssh execution result : " + sshOut);
            }

            return(sshOut);
        }