-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtrigger_transfer_publish_flow.py
executable file
·141 lines (117 loc) · 4.94 KB
/
trigger_transfer_publish_flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
import argparse
import glob
import os
# This could go into a different file and be invoked without the file watcher
from flows_service import create_flows_client
from user import UserIdentity
def run_flow(event_file):
# TODO: Specify the flow to run when triggered
flow_id = "REPLACE_WITH_FLOW_ID"
fc = create_flows_client(flow_id=flow_id)
# TODO: Set a label for the flow run
# Default includes the file name that triggered the run
flow_label = f"Trigger transfer->publish: {os.path.basename(event_file)}"
# TODO: Modify source collection ID
# Source collection must be on the endpoint where this trigger code is running
source_id = "REPLACE_WITH_SOURCE_COLLECTION_ID"
# TODO: Modify destination collection ID
# Destination must be a guest collection so permission can be set
# Default is "Globus Tutorials on ALCF Eagle"
destination_id = "a6f165fa-aee2-4fe5-95f3-97429c28bf82"
# TODO: Modify destination collection path
# Update path to include your user name e.g. /automate-tutorial/dev1/
destination_base_path = "/automation-tutorial/USERNAME/"
# TODO: Modify identity/group ID to share with
# Default is "Tutorial Users" group
sharee_id = "50b6a29c-63ac-11e4-8062-22000ab68755"
# TODO: Specify the search index to publish your metadata
search_index = "REPLACE_WITH_GLOBUS_SEARCH_INDEX_ID"
# Get the directory where the triggering file is stored and
# add trailing '/' to satisfy Transfer requirements for moving a directory
event_folder = os.path.dirname(event_file)
source_path = os.path.join(event_folder, "")
# Get name of monitored folder to use as destination path
# and for setting permissions
event_folder_name = os.path.basename(event_folder)
# Add a trailing '/' to meet Transfer requirements for directory transfer
destination_path = os.path.join(destination_base_path, event_folder_name, "")
# Gather some information about the transfer to include in metadata
file_names = glob.glob(source_path + "*")
# Use a helper-class to login, to get the user's Globus identity uuid.
# This ID is used to set access control on search records in the flow below.
user_identity = UserIdentity()
# Inputs to the flow
flow_input = {
"input": {
# local endpoint where the event listener is running
"source": {"id": source_id, "path": source_path},
"destination": {"id": destination_id, "path": destination_path},
"recursive_tx": True,
"principal": {
"type": "group",
"id": sharee_id,
},
# Metadata to ingest into Globus Search index
# TODO: Update "search_content_metadata" with your own
"search_ingest_document": {
"search_index": search_index,
"search_subject": event_folder_name,
"search_entry_id": "PUB00001",
"search_visible_to": ["public"],
"search_content_metadata": {
"title": event_folder_name,
"filecount": len(file_names),
"filenames": file_names,
},
# Principal URNs look like urn:globus:auth:identity:GLOBUS_AUTH_IDENTITY_UUID
# Ref: https://docs.globus.org/api/search/overview/#principal_urns
"search_restricted_entry_id": "RES00001",
"search_restricted_visible_to": [user_identity.principal_urn],
"search_content_restricted_metadata": {
"secret": "Formula 20220730",
"pathogen": "XLKFT-34895",
"antidote": "Unavailable",
},
},
}
}
flow_run_request = fc.run_flow(
body=flow_input,
label=flow_label,
tags=["Trigger_Tutorial"],
)
print(f"Transferring and publishing: {event_folder_name}")
print(f"Metadata published to search index: {search_index}")
print(f"https://app.globus.org/runs/{flow_run_request['run_id']}")
# Parse input arguments
def parse_args():
parser = argparse.ArgumentParser(
description="""
Watch a directory and trigger a transfer-and-share flow."""
)
parser.add_argument(
"--watchdir",
type=str,
default=os.path.abspath("."),
help=f"Directory path to watch. [default: current directory]",
)
parser.add_argument(
"--patterns",
type=str,
default="",
nargs="*",
help='Filename suffix pattern(s) that will trigger the flow. [default: ""]',
)
parser.set_defaults(verbose=True)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
# Creates and starts the watcher
from watch import FileTrigger
trigger = FileTrigger(
watch_dir=os.path.expanduser(args.watchdir),
patterns=args.patterns,
FlowRunner=run_flow,
)
trigger.run()