-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjiplib.py
313 lines (226 loc) · 9.55 KB
/
jiplib.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
307
308
309
310
311
312
313
import uuid
import jsonpickle
import json
import requests, shutil, os
from urllib.parse import urljoin
from urllib.request import urlretrieve
import pycurl
def init(faas_url_tmp, fileserver_url_tmp, jip_url_tmp):
global faas_url, fileserver_url, jip_url, faas_url_cluster, fileserver_url_cluster, jip_url_cluster
faas_url = faas_url_tmp
fileserver_url = fileserver_url_tmp
jip_url = jip_url_tmp
faas_url_cluster = 'http://gateway:8080/'
fileserver_url_cluster = 'http://fileserver-service:8000/'
jip_url_cluster = 'http://jip-service:8000/'
def create_uid():
return uuid.uuid4()
def convert_object2json(order_obj, unpicklable=True):
return jsonpickle.encode(order_obj, unpicklable=unpicklable)
def convert_json2object(json_str):
return jsonpickle.decode(json_str)
def get_uid():
uid = uuid.uuid4().hex
return uid
def translate_JipImageModel_to_JipImage(jip_image_model):
jip_image = JipImage()
jip_image.id_image = jip_image_model.id
jip_image.uploader = jip_image_model.author
jip_image.file_type = jip_image_model.file_type
jip_image.path_filesystem = jip_image_model.path_filesystem
jip_image.url_fileserver = jip_image_model.url_fileserver
jip_image.resolution = jip_image_model.resolution
jip_image.spacing = jip_image_model.spacing
return jip_image
def translate_JipOrderModel_to_JipOrder(jip_order_model, img_list):
jip_order = JipOrder()
jip_order.hash = jip_order_model.hash
jip_order.title = jip_order_model.title
jip_order.text = jip_order_model.text
jip_order.id = jip_order_model.id
author = JipUser(username=jip_order_model.author.username, id=jip_order_model.author.id, role="na")
jip_order.author = author
jip_order.target_function = jip_order_model.target_function
jip_order.status = jip_order_model.status
# jip_order.timestamp_creation = JipOrderModel.timestamp_creation
new_img_list = []
for img in img_list:
new_img_list.append(translate_JipImageModel_to_JipImage(img))
jip_order.image_list = new_img_list
return jip_order
class JipUser(object):
def __init__(self, username="na", id="na", role="na"):
self.username = username
self.id = id
self.role = role
class JipImage(object):
def __init__(self, id_image="na", uploader="na", file_type="na", path_filesystem="na", url_fileserver="na",
resolution="na",
spacing="na"):
self.id_image = id_image
self.uploader = uploader
self.file_type = file_type
self.path_filesystem = path_filesystem
self.url_fileserver = url_fileserver
self.resolution = resolution
self.spacing = spacing
class JipOrder(object):
def __init__(self, title="na", text="na", hash="na", id="na", author="na", order_path="na",
timestamp_creation="na", target_function="na", image_list="na", status="na"):
self.hash = hash
self.title = title
self.text = text
self.id = id
self.author = author
self.order_path = order_path
self.timestamp_creation = timestamp_creation
self.target_function = target_function
self.image_list = image_list
self.status = status
class JipUpdate(object):
def __init__(self, id_order="na", update_message="na", progress="na", type="na", status="na"):
self.id_order = id_order
self.update_message = update_message
self.progress = progress
self.type = type
self.status = status
class JipFunction(object):
def __init__(self, name="na", version="na", info="na", author="na", description="na", file_types_consumed="na",
file_types_produced="na", resolution_consumed="na", spacing_consumed="na", faas_id="na"):
self.name = name
self.version = version
self.info = info
self.author = author
self.description = description
self.file_types_consumed = file_types_consumed
self.file_types_produced = file_types_produced
self.resolution_consumed = resolution_consumed
self.spacing_consumed = spacing_consumed
self.faas_id = faas_id
def download_file_list(order, target_path):
try:
global fileserver_url
if not os.path.exists(target_path):
jip_log(order, ("MKDIR %s" % target_path))
os.makedirs(target_path)
image_list = order.image_list
for img_file in image_list:
filename = os.path.basename(img_file.url_fileserver)
server_url = urljoin(fileserver_url, img_file.url_fileserver)
file_target = os.path.join(target_path, filename)
urlretrieve(server_url, file_target)
except Exception as e:
jip_log(order, ("download_file_list() ERROR: %s" % str(e)))
print(str(e))
return str(e)
def upload_results(order, source_path):
try:
global fileserver_url
function_name = order.target_function
zip_file_path = os.path.join(os.path.split(source_path)[0], function_name + ".zip")
filename = os.path.basename(zip_file_path)
compress_file(source_path, zip_file_path)
if not os.path.isfile(zip_file_path):
jip_log(order, "upload_results() - File not Found!")
else:
jip_log(order, "Finished zipping...")
post_file(order, zip_file_path)
server_file_path = os.path.join("orders", str(order.author.id) + "_" + str(order.author.username),
order.hash,
function_name, filename)
update = JipUpdate()
update.id_order = order.id
update.update_message = server_file_path
update.progress = "100"
update.type = "result"
update.status = "finished"
send_update(update)
return "RESULT:" + zip_file_path
except Exception as e:
jip_log(order, ("upload_results() ERROR: %s" % str(e)))
print(str(e))
return "ERROR: " + str(e)
def send_order(jip_order, async=False):
try:
global faas_url, jip_url, fileserver_url
if async:
post_url = urljoin(faas_url + "/async-function/", jip_order.target_function)
else:
post_url = urljoin(faas_url + "/function/", jip_order.target_function)
json_string = convert_object2json(jip_order)
payload_dict = {'command': 'order', 'order': json_string, 'FILESERVER_URL': fileserver_url,
'FAAS_URL': faas_url,
'CALLBACK_URL': jip_url, 'X-Callback-Url': jip_url + "final/"}
payload_json = json.dumps(payload_dict)
response = requests.post(post_url, data=payload_json, timeout=5)
if response.text is not None:
print("Function response: %s %s" % (str(response.status_code), response.text))
except Exception as e:
print("IN SENDE ORDER: %s" % str(e))
return str(e)
def get_function_info(function_name, timeout=2, async=False):
try:
global faas_url
post_url = urljoin(faas_url + "/function/", function_name)
payload_dict = {'command': 'info'}
payload_json = json.dumps(payload_dict)
response = requests.post(post_url, data=payload_json, timeout=timeout)
response_text = response.text
if "py/object" in response_text:
response_text = response_text[response_text.find("{"): response_text.find("}") + 1]
function_info = convert_json2object(response_text)
else:
function_info = JipFunction(name=function_name)
function_info.faas_id = function_name
return function_info
except Exception as e:
function_info = JipFunction(name=function_name)
return function_info
def update_order(jip_update):
pass
def remove_tmp(path):
shutil.rmtree(path)
def jip_log(jip_order, msg):
update = JipUpdate()
update.id_order = jip_order.id
update.update_message = msg
update.progress = "-1"
update.type = "log"
update.status = "running"
send_update(update)
def send_update(update):
try:
global jip_url
update_json = convert_object2json(update)
response = requests.post(jip_url, data=update_json, timeout=3)
except Exception as e:
jip_log(order, ("send_update() ERROR: %s" % str(e)))
print(str(e))
return "ERROR: " + str(e)
def get_dict(json_string):
return json.loads(json_string)
def compress_file(source_path, zfilename):
zfilename = zfilename.replace(".zip", "")
shutil.make_archive(zfilename, 'zip', source_path)
def post_file(order, filepath):
global fileserver_url
try:
jip_log(order, "In post_file")
jip_log(order, "Fileserver-url: %s" % fileserver_url)
function_name = order.target_function
headers = {'user': str(order.author.id) + "_" + str(order.author.username), 'hash': order.hash,
'function_name': function_name}
c = pycurl.Curl()
c.setopt(c.URL, fileserver_url)
c.setopt(c.POST, 1)
c.setopt(c.HTTPPOST, [("images_file", (c.FORM_FILE, filepath))])
c.setopt(pycurl.HTTPHEADER,
['Accept-Language: en', 'user: %s' % (str(order.author.id) + "_" + str(order.author.username)),
'hash: %s' % order.hash, 'function_name: %s' % function_name])
c.perform()
c.close()
jip_log(order, "End post_file")
except Exception as e:
jip_log(order, ("post_file() ERROR: %s" % str(e)))
print(str(e))
return "ERROR: " + str(e)