Beispiel #1
0
        public override bool Execute(Machine machine)
        {
            bool result = false;


            LinuxMachine lMachine = machine as LinuxMachine;

            int retryCount = 0;

            while (!result && retryCount <= RetryTimes)
            {
                try
                {
                    string output = string.Empty;
                    string error  = string.Empty;
                    lMachine.Connect(machine.IP,
                                     OctopusLib.Command.Normalize(machine.Username, ParameterCollection),
                                     OctopusLib.Command.Normalize(machine.Password, ParameterCollection));

                    if (SshType == SSHType.Exec)
                    {
                        int res = lMachine.SSH.RunCommand(OctopusLib.Command.Normalize(CommandText, ParameterCollection, machine), out output, out error);
                        this.OutputValue = output;
                        if (res != 0)
                        {
                            result = false;
                            if (!string.IsNullOrEmpty(error))
                            {
                                this.Message = error;
                            }
                        }
                        else
                        {
                            result = true;
                        }
                    }
                    else if (SshType == SSHType.Stream)
                    {
                        int res = lMachine.SSH.SendCommand(
                            OctopusLib.Command.Normalize(CommandText, ParameterCollection, machine),
                            OctopusLib.Command.Normalize(ExpectedPrompt, ParameterCollection, machine),
                            TimeOutSeconds,
                            out output);
                        result           = true;
                        this.OutputValue = output;
                    }


                    if (result)
                    {
                        if (string.IsNullOrEmpty(ExpectedResult))
                        {
                            result = true;
                        }
                        else
                        {
                            if (Regex.IsMatch(OutputValue, OctopusLib.Command.Normalize(ExpectedResult, ParameterCollection, machine), RegexOptions.Multiline))
                            {
                                result = true;
                            }
                            else
                            {
                                result  = false;
                                Message = string.Format("The command result is not as expected. Expected Result: {0}, Actual Result: {1}", OctopusLib.Command.Normalize(ExpectedResult, ParameterCollection, machine), OutputValue);
                            }
                        }
                    }

                    if (result)
                    {
                        if (IsRebootRequired)
                        {
                            lMachine.Reboot(
                                machine.IP,
                                OctopusLib.Command.Normalize(machine.Username, ParameterCollection),
                                OctopusLib.Command.Normalize(machine.Password, ParameterCollection));
                            Console.WriteLine("Wait until active");
                            if (!machine.IsActive(
                                    machine.IP,
                                    OctopusLib.Command.Normalize(machine.Username, ParameterCollection),
                                    OctopusLib.Command.Normalize(machine.Password, ParameterCollection)))
                            {
                                result  = false;
                                Message = "Reboot machine fail, it is still not back yet.";
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Message = ex.Message;
                }
                finally
                {
                    lMachine.Disconnect();
                }
                retryCount++;
                System.Threading.Thread.Sleep(RetryIntervalSeconds * 1000);
                Console.WriteLine("Result: " + result);
            }

            return(result);
        }
Beispiel #2
0
        private bool RemoteExec(Machine machine)
        {
            bool result   = false;
            int  exitcode = -1;

            string cmd = OctopusLib.Command.Normalize(CommandText, ParameterCollection, machine);
            string exe = string.Format(@"{0}\PaExec.exe", Environment.CurrentDirectory);

            if (!System.IO.File.Exists(exe))
            {
                throw new Exception(string.Format("{0} is not exists.", exe));
            }
            string args = null;
            string output = null, error = null;

            string remoterunas_username = null;
            string remoterunas_password = null;

            remoterunas_username = string.IsNullOrEmpty(RemoteRunAsUsername) ?
                                   (string.IsNullOrEmpty(machine.Domain) ? OctopusLib.Command.Normalize(machine.Username, ParameterCollection) :
                                    string.Format(@"{0}\{1}", OctopusLib.Command.Normalize(machine.Domain, ParameterCollection), OctopusLib.Command.Normalize(machine.Username, ParameterCollection))) :
                                   OctopusLib.Command.Normalize(RemoteRunAsUsername, ParameterCollection);
            remoterunas_password = string.IsNullOrEmpty(RemoteRunAsPassword) ? OctopusLib.Command.Normalize(machine.Password, ParameterCollection) : OctopusLib.Command.Normalize(RemoteRunAsPassword, ParameterCollection);

            args = string.Format("-accepteula \\\\{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}",
                                 machine.IP,
                                 string.Format("-u {0}", remoterunas_username),
                                 string.Format("-p {0}", remoterunas_password),
                                 IsUIInteractive ? "-i 1" : string.Empty,
                                 IsNotLoadProfile ? "-e" : string.Empty,
                                 IsRunAsSystemAccount ?"-s" : string.Empty,
                                 IsRunAsLimittedUser ? "-l" : string.Empty,
                                 IsNotWaitForTerminate ? "-d" : string.Empty,
                                 string.IsNullOrEmpty(WorkingDirectory) ? string.Empty : string.Format("-w \"{0}\"", OctopusLib.Command.Normalize(WorkingDirectory, ParameterCollection)),
                                 cmd);

            exitcode    = ProcessHelper.Execute(exe, string.Empty, args, TimeOutSeconds, out output, out error);
            OutputValue = output.Replace("\r\n", string.Empty);
            ExitCode    = exitcode;
            Message     = string.Format("ExitCode: {0}\r\n{1}", exitcode, OutputValue);
            if (exitcode == Convert.ToInt32(ExpectedResult))
            {
                result = true;
            }
            else
            {
                result = false;
            }

            if (IsRebootRequired)
            {
                machine.Reboot(machine.IP,
                               string.IsNullOrEmpty(RemoteRunAsUsername) ? OctopusLib.Command.Normalize(machine.Username, ParameterCollection) : OctopusLib.Command.Normalize(RemoteRunAsUsername, ParameterCollection),
                               string.IsNullOrEmpty(RemoteRunAsPassword) ? OctopusLib.Command.Normalize(machine.Password, ParameterCollection) : OctopusLib.Command.Normalize(RemoteRunAsPassword, ParameterCollection));
                if (!machine.IsActive(machine.IP,
                                      string.IsNullOrEmpty(RemoteRunAsUsername) ? OctopusLib.Command.Normalize(machine.Username, ParameterCollection) : OctopusLib.Command.Normalize(RemoteRunAsUsername, ParameterCollection),
                                      string.IsNullOrEmpty(RemoteRunAsPassword) ? OctopusLib.Command.Normalize(machine.Password, ParameterCollection) : OctopusLib.Command.Normalize(RemoteRunAsPassword, ParameterCollection)))
                {
                    result  = false;
                    Message = "Reboot machine fail, it is still not back yet.";
                }
            }

            return(result);
        }