Пример #1
0
        public override void RunCommand(object sender, Script.ScriptAction parentCommand)
        {
            //get engine
            var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;

            //get indexes of commands
            var startIndex   = 0;
            var catchIndex   = parentCommand.AdditionalScriptCommands.FindIndex(a => a.ScriptCommand is CatchExceptionCommand);
            var finallyIndex = parentCommand.AdditionalScriptCommands.FindIndex(a => a.ScriptCommand is FinallyCommand);
            var endTryIndex  = parentCommand.AdditionalScriptCommands.FindIndex(a => a.ScriptCommand is EndTryCommand);

            var lineNumber = 0;

            for (int i = startIndex; i < catchIndex; i++)
            {
                if ((engine.IsCancellationPending) || (engine.CurrentLoopCancelled))
                {
                    return;
                }
                try
                {
                    Script.ScriptAction cmd = parentCommand.AdditionalScriptCommands[i];
                    lineNumber = cmd.ScriptCommand.LineNumber;
                    engine.ExecuteCommand(cmd);
                }
                catch (Exception ex)
                {
                    //error occured so start processing from catch index onwards
                    var catchCommandItem = parentCommand.AdditionalScriptCommands[catchIndex];
                    var catchCommand     = (CatchExceptionCommand)catchCommandItem.ScriptCommand;

                    catchCommand.StackTrace   = ex.ToString();
                    catchCommand.ErrorMessage = ex.Message;
                    engine.AddVariable("Catch:StackTrace", catchCommand.StackTrace);
                    engine.AddVariable("Catch:ErrorMessage", catchCommand.ErrorMessage);

                    //assify = (input >= 0) ? "nonnegative" : "negative";
                    var endCatch = (finallyIndex != -1) ? finallyIndex : endTryIndex;

                    for (int j = catchIndex; j < endCatch; j++)
                    {
                        engine.ExecuteCommand(parentCommand.AdditionalScriptCommands[j]);
                    }
                    break;
                }
            }
            //handle finally block if exists
            if (finallyIndex != -1)
            {
                for (int k = finallyIndex; k < endTryIndex; k++)
                {
                    engine.ExecuteCommand(parentCommand.AdditionalScriptCommands[k]);
                }
            }
        }
Пример #2
0
        private static void ProcessRequest(string data, string[] messageContent, NetworkStream stream)
        {
            if ((data.StartsWith("POST /ExecuteScript")) || (data.StartsWith("POST /AwaitScript")))
            {
                automationLogger.Information($"Client Requests Script Execution");

                //locate the body content
                string dataParameter  = "";
                bool   isFileLocation = false;

                //find the script parameter
                foreach (var item in messageContent)
                {
                    if (item.StartsWith("ScriptData: "))
                    {
                        dataParameter  = item.Replace("ScriptData: ", "");
                        isFileLocation = false;

                        break;
                    }

                    else if (item.StartsWith("ScriptLocation: "))
                    {
                        dataParameter  = item.Replace("ScriptLocation: ", "");
                        isFileLocation = true;
                        break;
                    }
                }

                //check to see if nothing was provided
                if (string.IsNullOrEmpty(dataParameter))
                {
                    automationLogger.Information($"Client Script Data Not Found");
                    return;
                }


                if (dataParameter.TryParseBase64(out var base64SourceString))
                {
                    automationLogger.Information($"Client Passed Base64 String: {base64SourceString}");
                    dataParameter = base64SourceString;
                }
                else
                {
                    automationLogger.Information($"Client Did Not Pass Base64 String");
                }


                //check if data parameter references file location
                if (isFileLocation)
                {
                    if (File.Exists(dataParameter))
                    {
                        //file was found at path provided
                        dataParameter = File.ReadAllText(dataParameter);
                    }
                    else if (File.Exists(Path.Combine(IO.Folders.GetFolder(IO.Folders.FolderType.ScriptsFolder), dataParameter)))
                    {
                        //file was found at fallback to scripts folder
                        dataParameter = Path.Combine(IO.Folders.GetFolder(IO.Folders.FolderType.ScriptsFolder), dataParameter);
                        dataParameter = File.ReadAllText(dataParameter);
                    }
                    else
                    {
                        //file not found
                        automationLogger.Information($"Client Script Location Not Found: {dataParameter}");
                        SendResponse(ResponseCode.InternalServerError, $"Client Script Location Not Found: {dataParameter}", stream);
                        return;
                    }
                }

                //log execution
                automationLogger.Information($"Executing Script: {dataParameter}");


                //invoke builder and pass it script data to execute
                associatedBuilder.Invoke(new MethodInvoker(delegate()
                {
                    UI.Forms.frmScriptEngine newEngine = new UI.Forms.frmScriptEngine();
                    newEngine.xmlData      = dataParameter;
                    newEngine.callBackForm = null;
                    //instance = newEngine.engineInstance;
                    newEngine.Show();
                }));


                if (data.StartsWith("POST /AwaitScript"))
                {
                    //reset result value
                    TasktResult = "";

                    //add reference to script finished event
                    automationInstance.ScriptFinishedEvent += AutomationInstance_ScriptFinishedEvent;

                    //wait for script to finish before returning
                    do
                    {
                        Thread.Sleep(1000);
                    } while (TasktResult == string.Empty);

                    //send response back to client
                    SendResponse(ResponseCode.OK, automationInstance.TasktResult, stream);
                }
                else
                {
                    //return success immediately
                    SendResponse(ResponseCode.OK, "Script Launched Successfully", stream);
                }
            }
            else if (data.StartsWith("POST /ExecuteCommand"))
            {
                automationLogger.Information($"Client Requests Command Execution");

                //locate the body content
                string dataParameter = "";

                //find the script parameter
                foreach (var item in messageContent)
                {
                    if (item.StartsWith("CommandData: "))
                    {
                        dataParameter = item.Replace("CommandData: ", "");
                        break;
                    }
                }

                //check to see if nothing was provided
                if (string.IsNullOrEmpty(dataParameter))
                {
                    automationLogger.Information($"Client Command Data Not Found");
                    return;
                }


                if (dataParameter.TryParseBase64(out var base64SourceString))
                {
                    automationLogger.Information($"Client Passed Base64 String: {base64SourceString}");
                    dataParameter = base64SourceString;
                }
                else
                {
                    automationLogger.Information($"Client Did Not Pass Base64 String");
                }

                try
                {
                    //deserialize command
                    var command = Newtonsoft.Json.JsonConvert.DeserializeObject(dataParameter, new Newtonsoft.Json.JsonSerializerSettings()
                    {
                        TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All
                    });

                    //log execution
                    automationLogger.Information($"Executing Command: {dataParameter}");

                    //define script action
                    var scriptAction = new Script.ScriptAction()
                    {
                        ScriptCommand = (Core.Automation.Commands.ScriptCommand)command
                    };

                    //execute command
                    ExecuteCommandEngine.ExecuteCommand(scriptAction);

                    //send back response
                    SendResponse(ResponseCode.OK, "Command Executed Successfully", stream);
                }
                catch (Exception ex)
                {
                    SendResponse(ResponseCode.InternalServerError, $"An error occured: {ex.ToString()}", stream);
                }
            }

            else if (data.StartsWith("POST /EngineStatus"))
            {
                automationLogger.Information($"Returning Engine Status: {Client.ClientStatus}");
                SendResponse(ResponseCode.OK, Core.Client.ClientStatus, stream);
            }
            else if (data.StartsWith("POST /RestartTaskt"))
            {
                automationLogger.Information($"Restarting taskt");
                SendResponse(ResponseCode.OK, "taskt is being Restarted", stream);
                Application.Restart();
            }
            else
            {
                automationLogger.Information($"Invalid Client Request");
                SendResponse(ResponseCode.InternalServerError, "Invalid Client Request", stream);
            }
        }
Пример #3
0
 public override void RunCommand(object sender, Script.ScriptAction parentCommand)
 {
     //no execution required, used as a marker by the Automation Engine
 }