-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
306 lines (225 loc) · 9.27 KB
/
service.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import pathlib
try:
import zoo
except ImportError:
class ZooStub(object):
def __init__(self):
self.SERVICE_SUCCEEDED = 3
self.SERVICE_FAILED = 4
def update_status(self, conf, progress):
print(f"Status {progress}")
def _(self, message):
print(f"invoked _ with {message}")
zoo = ZooStub()
import os
import sys
import traceback
import yaml
import json
import boto3 # noqa: F401
import botocore
from loguru import logger
from urllib.parse import urlparse
from botocore.exceptions import ClientError
from botocore.client import Config
from pystac import read_file
from pystac.stac_io import DefaultStacIO, StacIO
from pystac.item_collection import ItemCollection
from zoo_calrissian_runner import ExecutionHandler, ZooCalrissianRunner
logger.remove()
logger.add(sys.stderr, level="INFO")
class CustomStacIO(DefaultStacIO):
"""Custom STAC IO class that uses boto3 to read from S3."""
def __init__(self):
self.session = botocore.session.Session()
self.s3_client = self.session.create_client(
service_name="s3",
region_name="us-east-1",
endpoint_url="http://eoap-zoo-project-localstack.eoap-zoo-project.svc.cluster.local:4566",
aws_access_key_id="test",
aws_secret_access_key="test",
)
def read_text(self, source, *args, **kwargs):
parsed = urlparse(source)
if parsed.scheme == "s3":
return (
self.s3_client.get_object(Bucket=parsed.netloc, Key=parsed.path[1:])[
"Body"
]
.read()
.decode("utf-8")
)
else:
return super().read_text(source, *args, **kwargs)
def write_text(self, dest, txt, *args, **kwargs):
parsed = urlparse(dest)
if parsed.scheme == "s3":
self.s3_client.put_object(
Body=txt.encode("UTF-8"),
Bucket=parsed.netloc,
Key=parsed.path[1:],
ContentType="application/geo+json",
)
else:
super().write_text(dest, txt, *args, **kwargs)
StacIO.set_default(CustomStacIO)
class SimpleExecutionHandler(ExecutionHandler):
def __init__(self, conf):
super().__init__()
self.conf = conf
self.results = None
def pre_execution_hook(self):
logger.info("Pre execution hook")
def post_execution_hook(self, log, output, usage_report, tool_logs):
# unset HTTP proxy or else the S3 client will use it and fail
os.environ.pop("HTTP_PROXY", None)
logger.info("Post execution hook")
StacIO.set_default(CustomStacIO)
logger.info(f"Read catalog => STAC Catalog URI: {output['s3_catalog_output']}")
cat = read_file(output["s3_catalog_output"])
collection_id = self.get_additional_parameters()["sub_path"]
logger.info(f"Create collection with ID {collection_id}")
collection = None
collection = next(cat.get_all_collections())
logger.info("Got collection from outputs")
items = []
for item in collection.get_all_items():
logger.info("Processing item {item.id}")
for asset_key in item.assets.keys():
logger.info(f"Processing asset {asset_key}")
temp_asset = item.assets[asset_key].to_dict()
temp_asset["storage:platform"] = "eoap"
temp_asset["storage:requester_pays"] = False
temp_asset["storage:tier"] = "Standard"
temp_asset["storage:region"] = self.get_additional_parameters()[
"region_name"
]
temp_asset["storage:endpoint"] = self.get_additional_parameters()[
"endpoint_url"
]
item.assets[asset_key] = item.assets[asset_key].from_dict(temp_asset)
item.collection_id = collection_id
items.append(item.clone())
item_collection = ItemCollection(items=items)
logger.info("Created collection from items")
# Trap the case of no output collection
if item_collection is None:
logger.error("The output collection is empty")
self.feature_collection = json.dumps({}, indent=2)
return
# Set the feature collection to be returned
self.results = item_collection.to_dict()
self.results["id"] = collection_id
def get_pod_env_vars(self):
logger.info("get_pod_env_vars")
env_vars = {"A": "1", "B": "2"}
return env_vars
def get_pod_node_selector(self):
logger.info("get_pod_node_selector")
node_selector = {}
return node_selector
def get_additional_parameters(self):
logger.info("get_additional_parameters")
additional_parameters = {
"s3_bucket": "results",
"sub_path": self.conf["lenv"]["usid"],
"region_name": "us-east-1",
"aws_secret_access_key": "test",
"aws_access_key_id": "test",
"endpoint_url": "http://eoap-zoo-project-localstack.eoap-zoo-project.svc.cluster.local:4566",
}
logger.info(f"additional_parameters: {additional_parameters.keys()}")
return additional_parameters
def handle_outputs(self, log, output, usage_report, tool_logs):
"""
Handle the output files of the execution.
:param log: The application log file of the execution.
:param output: The output file of the execution.
:param usage_report: The metrics file.
:param tool_logs: A list of paths to individual workflow step logs.
"""
try:
logger.info("handle_outputs")
logger.info(f"Set output to {output['s3_catalog_output']}")
self.results = {"url": output["s3_catalog_output"]}
self.conf["main"]["tmpUrl"] = self.conf["main"]["tmpUrl"].replace(
"temp/", self.conf["auth_env"]["user"] + "/temp/"
)
services_logs = [
{
"url": os.path.join(
self.conf["main"]["tmpUrl"],
f"{self.conf['lenv']['Identifier']}-{self.conf['lenv']['usid']}",
os.path.basename(tool_log),
),
"title": f"Tool log {os.path.basename(tool_log)}",
"rel": "related",
}
for tool_log in tool_logs
]
for i in range(len(services_logs)):
okeys = ["url", "title", "rel"]
keys = ["url", "title", "rel"]
if i > 0:
for j in range(len(keys)):
keys[j] = keys[j] + "_" + str(i)
if "service_logs" not in self.conf:
self.conf["service_logs"] = {}
for j in range(len(keys)):
self.conf["service_logs"][keys[j]] = services_logs[i][okeys[j]]
self.conf["service_logs"]["length"] = str(len(services_logs))
logger.info(f"service_logs: {self.conf['service_logs']}")
except Exception as e:
logger.error("ERROR in handle_outputs...")
logger.error(traceback.format_exc())
raise (e)
def get_secrets(self):
return {}
def {{cookiecutter.workflow_id |replace("-", "_") }}(conf, inputs, outputs): # noqa
try:
with open(
os.path.join(
pathlib.Path(os.path.realpath(__file__)).parent.absolute(),
"app-package.cwl",
),
"r",
) as stream:
cwl = yaml.safe_load(stream)
execution_handler = SimpleExecutionHandler(conf=conf)
runner = ZooCalrissianRunner(
cwl=cwl,
conf=conf,
inputs=inputs,
outputs=outputs,
execution_handler=execution_handler,
)
working_dir = os.path.join(conf["main"]["tmpPath"], runner.get_namespace_name())
os.makedirs(
working_dir,
mode=0o777,
exist_ok=True,
)
os.chdir(working_dir)
exit_status = runner.execute()
if exit_status == zoo.SERVICE_SUCCEEDED:
logger.info(f"Setting Collection into output key {list(outputs.keys())[0]}")
outputs["stac_catalog"]["value"] = json.dumps(
execution_handler.results, indent=2
)
return zoo.SERVICE_SUCCEEDED
else:
conf["lenv"]["message"] = zoo._("Execution failed")
logger.error("Execution failed")
return zoo.SERVICE_FAILED
except Exception as e:
logger.error("ERROR in processing execution template...")
logger.error("Try to fetch the tool logs if any...")
try:
tool_logs = runner.execution.get_tool_logs()
execution_handler.handle_outputs(None, None, None, tool_logs)
except Exception as e:
logger.error(f"Fetching tool logs failed! ({str(e)})")
stack = traceback.format_exc()
logger.error(stack)
conf["lenv"]["message"] = zoo._(f"Exception during execution...\n{stack}\n")
return zoo.SERVICE_FAILED