Skip to content

Commit 257c59c

Browse files
committed
Adding new plugin for File Trigger job support
1 parent 280cb64 commit 257c59c

4 files changed

Lines changed: 140 additions & 0 deletions

File tree

file-trigger/beer.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PLUGIN_ENTRY="-m file_trigger"

file-trigger/file_trigger/__init__.py

Whitespace-only changes.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from brewtils import parameter, Plugin, get_current_request_read_only, SystemClient
2+
import os
3+
4+
__version__ = "3.0.0.dev0"
5+
6+
class FileTriggerClient(object):
7+
"""Client that runs off File Trigger Scheduler Logic"""
8+
9+
def __init__(self):
10+
self.system_client = SystemClient()
11+
12+
@parameter(
13+
key="upload_file",
14+
type="Boolean",
15+
description="Should File be uploaded to Beer Garden prior to processing?",
16+
default=False,
17+
)
18+
@parameter(
19+
key="delete_file",
20+
type="Boolean",
21+
description="Should File be deleted after processing?",
22+
default=False,
23+
)
24+
def file_create_trigger(self, upload_file: bool=False, delete_file: bool = True):
25+
"""Used for File Trigger Jobs"""
26+
27+
# Grab the file path from the metadata. Currently there isn't a clean way to
28+
# inject the path into the input parameters
29+
current_request = get_current_request_read_only()
30+
31+
if "src_path" not in current_request.metadata:
32+
raise Exception("Missing 'src_path' metadata from file trigger")
33+
34+
file_path = current_request.metadata["src_path"]
35+
36+
if not os.path.exists(file_path):
37+
raise Exception(f"File path does not exist: {file_path}")
38+
39+
if upload_file:
40+
# Uploading the file is good for scenarios where the command that processes the file does
41+
# not have access to the file path
42+
43+
# Uploading to Base64 Parameters
44+
self.system_client.process_file_upload(uploaded_file=file_path)
45+
# The Base64 file can also be passed in via `open()`, if file path is provided
46+
# as a String, then Brewtils will invoke `open(path, 'rb')`
47+
# self.system_client.process_file_upload(uploaded_file=open(file_path, "r"))
48+
49+
# Uploading to Bytes Parameters
50+
self.system_client.process_file_upload_bytes(uploaded_file=open(file_path, "rb").read())
51+
else:
52+
self.system_client.process_file(file_path=file_path)
53+
54+
if delete_file:
55+
os.remove(file_path)
56+
57+
58+
@parameter(
59+
key="file_path",
60+
type="String",
61+
description="Path to file to be processed",
62+
)
63+
def process_file(self, file_path:str):
64+
"""Returns contents of file path provided"""
65+
66+
with open(file_path, 'r') as file:
67+
file_content = file.read()
68+
return file_content
69+
70+
71+
@parameter(
72+
key="uploaded_file",
73+
type="Base64",
74+
description="Path to file to be processed",
75+
)
76+
def process_file_upload(self, uploaded_file):
77+
"""Returns contents of file uploaded"""
78+
return uploaded_file.read()
79+
80+
81+
@parameter(
82+
key="uploaded_file",
83+
type="Bytes",
84+
description="Path to file to be processed",
85+
)
86+
def process_file_upload_bytes(self, uploaded_file):
87+
"""Returns contents of file uploaded"""
88+
return uploaded_file
89+
90+
91+
def main():
92+
plugin = Plugin(
93+
name="file_trigger",
94+
version=__version__,
95+
description="Example for how to handle file triggers",
96+
)
97+
plugin.client = FileTriggerClient()
98+
plugin.run()
99+
100+
101+
if __name__ == "__main__":
102+
main()

file-trigger/setup.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import re
2+
3+
from setuptools import setup
4+
5+
6+
def find_version(version_file):
7+
version_line = open(version_file, "rt").read()
8+
match_object = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", version_line, re.M)
9+
10+
if not match_object:
11+
raise RuntimeError("Unable to find version string in %s" % version_file)
12+
13+
return match_object.group(1)
14+
15+
16+
setup(
17+
name="file-trigger",
18+
version=find_version("file_trigger/__main__.py"),
19+
description="Client that runs off File Trigger Scheduler Logic",
20+
url="https://beer-garden.io",
21+
author="The Beergarden Team",
22+
author_email=" ",
23+
license="MIT",
24+
packages=["fle_trigger"],
25+
install_requires=["brewtils"],
26+
classifiers=[
27+
"Intended Audience :: Developers",
28+
"License :: OSI Approved :: MIT License",
29+
"Programming Language :: Python :: 2",
30+
"Programming Language :: Python :: 2.7",
31+
"Programming Language :: Python :: 3",
32+
"Programming Language :: Python :: 3.5",
33+
"Programming Language :: Python :: 3.6",
34+
"Programming Language :: Python :: 3.7",
35+
"Programming Language :: Python :: 3.8",
36+
],
37+
)

0 commit comments

Comments
 (0)