From d185b469d964c7d4496d1922f37985f541f42e05 Mon Sep 17 00:00:00 2001 From: Shahar Glazner Date: Wed, 14 Feb 2024 11:17:42 +0200 Subject: [PATCH] fix: bash provider and examples (#828) --- examples/workflows/bash_example.yml | 29 ++++++++++++++++ keep/providers/bash_provider/bash_provider.py | 34 ++++++++++++++----- 2 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 examples/workflows/bash_example.yml diff --git a/examples/workflows/bash_example.yml b/examples/workflows/bash_example.yml new file mode 100644 index 0000000000..4b5518ef93 --- /dev/null +++ b/examples/workflows/bash_example.yml @@ -0,0 +1,29 @@ +workflow: + id: Resend-Python-service + description: Python Resend Mail + triggers: + - type: manual + owners: [] + services: [] + steps: + - name: run-script + provider: + config: '{{ providers.default-bash }}' + type: bash + with: + command: python3 test.py + timeout: 5 + actions: + - condition: + - assert: '{{ steps.run-script.results.return_code }} == 0' + name: assert-condition + type: assert + name: trigger-resend + provider: + type: resend + config: "{{ providers.resend-test }}" + with: + _from: "onboarding@resend.dev" + to: "youremail.dev@gmail.com" + subject: "Python test is up!" + html:

Python test is up!

diff --git a/keep/providers/bash_provider/bash_provider.py b/keep/providers/bash_provider/bash_provider.py index 63bde85c9c..cbf4184196 100644 --- a/keep/providers/bash_provider/bash_provider.py +++ b/keep/providers/bash_provider/bash_provider.py @@ -25,6 +25,7 @@ def _query(self, **kwargs): Returns: _type_: _description_ """ + timeout = kwargs.get("timeout", 60) command = kwargs.get("command", "") parsed_command = self.io_handler.parse(command) # parse by pipes @@ -57,14 +58,31 @@ def _query(self, **kwargs): processes.append(process) # Get the final output - stdout, stderr = processes[-1].communicate() - return_code = processes[-1].returncode - # stdout and stderr are strings or None - if stdout: - stdout = stdout.decode() - - if stderr: - stderr = stderr.decode() + try: + stdout, stderr = processes[-1].communicate(timeout=timeout) + return_code = processes[-1].returncode + # stdout and stderr are strings or None + if stdout or stdout == b"": + stdout = stdout.decode() + + if stderr or stderr == b"": + stderr = stderr.decode() + except subprocess.TimeoutExpired: + # use check_output to get the output of the last process + # this is some MacOS bug, where communicate() doesn't work and raise TimeoutExpired (idk why but it works on Linux) + try: + self.logger.warning("TimeoutExpired, using check_output - MacOS bug?") + stdout = subprocess.check_output( + cmd, stderr=subprocess.STDOUT, timeout=timeout, shell=True + ).decode() + stderr = None + return_code = 0 + self.logger.warning("check_output worked") + # todo: fix that + except Exception as e: + stdout = None + stderr = e.args[1].decode() + return_code = e.args[0] return { "stdout": str(stdout),