-
Notifications
You must be signed in to change notification settings - Fork 47
/
client.py
976 lines (868 loc) · 34.1 KB
/
client.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# -*- coding: utf-8 -*-
#
# This file is part of REANA.
# Copyright (C) 2017, 2018 CERN.
#
# REANA is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""REANA REST API client."""
import json
import logging
import os
import traceback
from functools import partial
import requests
from bravado.exception import HTTPError
from reana_client.config import ERROR_MESSAGES, WORKFLOW_ENGINES
from reana_client.errors import FileDeletionError, FileUploadError
from reana_client.utils import _validate_reana_yaml, is_uuid_v4
from reana_commons.api_client import get_current_api_client
from reana_commons.errors import REANASecretAlreadyExists, REANASecretDoesNotExist
from werkzeug.local import LocalProxy
try:
from urllib.parse import urljoin
except ImportError:
from urlparse import urljoin
current_rs_api_client = LocalProxy(
partial(get_current_api_client, component="reana-server")
)
def ping(access_token):
"""Health check REANA server."""
try:
response, http_response = current_rs_api_client.api.get_you(
access_token=access_token
).result()
if http_response.status_code == 200:
response["status"] = "Connected"
response["error"] = False
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"REANA server health check failed: "
"\nStatus: {}\nReason: {}".format(e.response.status_code, e.response.reason)
)
if e.response.status_code == 404:
return {"status": "ERROR: INVALID SERVER", "error": True}
if e.response.status_code == 403:
return {"status": "ERROR: INVALID ACCESS TOKEN", "error": True}
raise Exception(e.response)
except Exception:
return {"status": "ERROR: INVALID SERVER", "error": True}
def get_user_quota(access_token):
"""Retrieve user quota usage and limits."""
try:
response, http_response = current_rs_api_client.api.get_you(
access_token=access_token
).result()
if http_response.status_code == 200:
return response["quota"]
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"User quotas could not be retrieved: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def get_workflows(
access_token, type, verbose=False, page=None, size=None, status=None, search=None,
):
"""List all existing workflows."""
try:
response, http_response = current_rs_api_client.api.get_workflows(
access_token=access_token,
verbose=verbose,
type=type,
page=page,
size=size,
status=status,
search=search,
).result()
if http_response.status_code == 200:
return response.get("items")
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"The list of workflows could not be retrieved: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def get_workflow_status(workflow, access_token):
"""Get status of previously created workflow."""
try:
response, http_response = current_rs_api_client.api.get_workflow_status(
workflow_id_or_name=workflow, access_token=access_token
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Analysis status could not be retrieved: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def create_workflow(reana_specification, name, access_token):
"""Create a workflow."""
try:
(response, http_response) = current_rs_api_client.api.create_workflow(
reana_specification=json.loads(
json.dumps(reana_specification, sort_keys=True)
),
workflow_name=name,
access_token=access_token,
).result()
if http_response.status_code == 201:
return response
else:
raise Exception(
"Expected status code 201 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Workflow creation failed: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def create_workflow_from_json(
workflow_json,
name,
access_token,
parameters=None,
workflow_engine="yadage",
outputs=None,
):
"""Create a workflow from json specification.
:param workflow_json: workflow specification in json format.
:param name: name or UUID of the workflow to be started.
:param access_token: Access token of the current user.
:param parameters: workflow input parameters dictionary.
:param workflow_engine: one of the workflow engines (yadage, serial, cwl)
:param outputs: dictionary with expected workflow outputs.
:Example:
.. code:: python
create_workflow_from_json(
workflow_json=workflow_json,
name='workflow_name.1',
access_token='access_token',
parameters={'files': ['file.txt'], 'parameters': {'key': 'value'}},
workflow_engine='serial')
"""
if is_uuid_v4(name):
raise ValueError("Workflow name cannot be a valid UUIDv4")
if not access_token:
raise Exception(ERROR_MESSAGES["missing_access_token"])
if os.environ.get("REANA_SERVER_URL") is None:
raise Exception("Environment variable REANA_SERVER_URL is not set")
workflow_engine = workflow_engine.lower()
if workflow_engine not in WORKFLOW_ENGINES:
raise Exception(
"Workflow engine - {} not found. You must use one of "
"these engines - {}".format(workflow_engine, WORKFLOW_ENGINES)
)
try:
reana_yaml = {}
reana_yaml["workflow"] = {"specification": workflow_json}
reana_yaml["workflow"]["type"] = workflow_engine
if parameters:
reana_yaml["inputs"] = parameters
if outputs:
reana_yaml["outputs"] = outputs
_validate_reana_yaml(reana_yaml)
reana_specification = reana_yaml
(response, http_response) = current_rs_api_client.api.create_workflow(
reana_specification=json.loads(
json.dumps(reana_specification, sort_keys=True)
),
workflow_name=name,
access_token=access_token,
).result()
if http_response.status_code == 201:
return response
else:
raise Exception(
"Expected status code 201 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Workflow creation failed: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def start_workflow(workflow, access_token, parameters):
"""Start a workflow.
:param workflow: name or id of previously created workflow.
:param access_token: access token of the current user.
:param parameters: dict of workflow parameters to override the original
ones (after workflow creation).
:param restart: boolean if workflow should be restarted on the same
workspace.
"""
try:
(response, http_response) = current_rs_api_client.api.start_workflow(
workflow_id_or_name=workflow,
access_token=access_token,
parameters=parameters,
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Workflow run could not be started: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def upload_file(workflow_id, file_, file_name, access_token):
"""Upload file to workflow workspace.
:param workflow_id: UID which identifies the workflow.
:param file_: content of a file that will be uploaded.
:param file_name: name of a file that will be uploaded.
:param access_token: access token of the current user.
"""
from reana_client.utils import get_api_url
try:
endpoint = current_rs_api_client.api.upload_file.operation.path_name.format(
workflow_id_or_name=workflow_id
)
http_response = requests.post(
urljoin(get_api_url(), endpoint),
data=file_,
params={"file_name": file_name, "access_token": access_token},
headers={"Content-Type": "application/octet-stream"},
verify=False,
)
if http_response.ok:
return http_response.json()
raise Exception(http_response.json().get("message"))
except requests.exceptions.ConnectionError:
logging.debug("File could not be uploaded.", exc_info=True)
raise Exception("Could not connect to the server {}".format(get_api_url()))
except requests.exceptions.HTTPError as e:
logging.debug("The server responded with an HTTP error code.", exc_info=True)
raise Exception("Unexpected response from the server: \n{}".format(e.response))
except requests.exceptions.Timeout:
logging.debug("Timeout while trying to establish connection.", exc_info=True)
raise Exception("The request to the server has timed out.")
except requests.exceptions.RequestException:
logging.debug(
"Something went wrong while connecting to the server.", exc_info=True
)
raise Exception("The request to the server has failed for an unknown reason.")
except Exception as e:
raise e
def get_workflow_logs(workflow_id, access_token, steps=None, page=None, size=None):
"""Get logs from a workflow engine."""
try:
(response, http_response) = current_rs_api_client.api.get_workflow_logs(
workflow_id_or_name=workflow_id,
steps=steps,
access_token=access_token,
page=page,
size=size,
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Workflow logs could not be retrieved: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def download_file(workflow_id, file_name, access_token):
"""Download the requested file if it exists.
:param workflow_id: UUID which identifies the workflow.
:param file_name: File name or path to the file requested.
:returns: .
"""
try:
from reana_client.utils import get_api_url
logging.getLogger("urllib3").setLevel(logging.CRITICAL)
endpoint = current_rs_api_client.api.download_file.operation.path_name.format(
workflow_id_or_name=workflow_id, file_name=file_name
)
http_response = requests.get(
urljoin(get_api_url(), endpoint),
params={"file_name": file_name, "access_token": access_token},
verify=False,
)
if http_response.status_code == 200:
return http_response.content
else:
raise Exception(
"Error {status_code} {reason} {message}".format(
status_code=http_response.status_code,
reason=http_response.reason,
message=http_response.json().get("message"),
)
)
except HTTPError as e:
logging.debug(
"Output file could not be downloaded: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def delete_file(workflow_id, file_name, access_token):
"""Delete the requested file if it exists.
:param workflow_id: UUID which identifies the workflow.
:param file_name: File name or path to the file requested.
"""
try:
(response, http_response) = current_rs_api_client.api.delete_file(
workflow_id_or_name=workflow_id,
file_name=file_name,
access_token=access_token,
).result()
if http_response.status_code == 200 and (
response["deleted"] or response["failed"]
):
return response
elif not (response["deleted"] or response["failed"]):
raise FileDeletionError(
"{} did not match any existing " "file.".format(file_name)
)
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"File could not be downloaded: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def list_files(workflow_id, access_token, page=None, size=None):
"""Return the list of file for a given workflow workspace.
:param workflow_id: UUID which identifies the workflow.
:returns: A list of dictionaries composed by the `name`, `size` and
`last-modified`.
"""
try:
(response, http_response) = current_rs_api_client.api.get_files(
workflow_id_or_name=workflow_id,
access_token=access_token,
page=page,
size=size,
).result()
if http_response.status_code == 200:
return response.get("items")
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"File list could not be retrieved: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def upload_to_server(workflow, paths, access_token):
"""Upload file or directory to REANA server.
Shared e.g. by `code upload` and `inputs upload`.
:param workflow: ID of that workflow whose workspace should be
used to store the files.
:param paths: Absolute filepath(s) of files to be uploaded.
"""
if not workflow:
raise ValueError("Workflow name or id must be provided")
if not paths:
raise ValueError(
"Please provide path(s) to file(s) that " "should be uploaded to workspace."
)
logging.info('Workflow "{}" selected'.format(workflow))
# Check if multiple paths were given and iterate over them
if type(paths) is list or type(paths) is tuple:
for path in paths:
upload_to_server(workflow, path, access_token)
# `paths` points to a single file or directory
else:
path = paths
if ".." in paths.split("/"):
raise FileUploadError('Path cannot contain ".."')
# Check if input is a directory and upload everything
# including subdirectories.
if os.path.isdir(path):
logging.debug("'{}' is a directory.".format(path))
logging.info("Uploading contents of folder '{}' ...".format(path))
for root, dirs, files in os.walk(path, topdown=False):
uploaded_files = []
for next_path in files + dirs:
next_uploaded_files = upload_to_server(
workflow, os.path.join(root, next_path), access_token
)
if next_uploaded_files:
uploaded_files.extend(next_uploaded_files)
return uploaded_files
# Check if input is an absolute path and upload file.
else:
symlink = False
if os.path.islink(path):
path = os.path.realpath(path)
logging.info("Symlink {} found, uploading" " hard copy.".format(path))
symlink = True
with open(path, "rb") as f:
fname = os.path.basename(f.name)
# Calculate the path that will store the file
# in the workflow controller, by subtracting
# the workflow root path from the file path
save_path = path.replace(os.getcwd(), "")
# Remove prepending dirs named "." or as the upload type
while len(save_path.split("/")) > 1 and save_path.split("/")[0] == ".":
save_path = "/".join(save_path.strip("/").split("/")[1:])
logging.debug(
"'{}' is an absolute filepath.".format(os.path.basename(fname))
)
logging.info("Uploading '{}' ...".format(fname))
try:
upload_file(workflow, f, save_path, access_token)
logging.info("File '{}' was successfully uploaded.".format(fname))
if symlink:
save_path = "symlink:{}".format(save_path)
return [save_path]
except Exception as e:
logging.debug(traceback.format_exc())
logging.debug(str(e))
logging.info(
"Something went wrong while uploading {}".format(fname)
)
raise e
def get_workflow_parameters(workflow, access_token):
"""Get parameters of previously created workflow."""
try:
response, http_response = current_rs_api_client.api.get_workflow_parameters(
workflow_id_or_name=workflow, access_token=access_token
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Workflow parameters could not be retrieved: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def get_workflow_specification(workflow, access_token):
"""Get specification of previously created workflow."""
try:
response, http_response = current_rs_api_client.api.get_workflow_specification(
workflow_id_or_name=workflow, access_token=access_token
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Workflow specification could not be retrieved: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def delete_workflow(workflow, all_runs, workspace, access_token):
"""Delete a workflow."""
try:
parameters = {
"all_runs": all_runs,
"workspace": workspace,
}
(response, http_response) = current_rs_api_client.api.set_workflow_status(
workflow_id_or_name=workflow,
status="deleted",
access_token=access_token,
parameters=parameters,
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Workflow run could not be deleted: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def stop_workflow(workflow, force_stop, access_token):
"""Stop a workflow."""
try:
parameters = {"force_stop": force_stop}
(response, http_response) = current_rs_api_client.api.set_workflow_status(
workflow_id_or_name=workflow,
status="stop",
access_token=access_token,
parameters=parameters,
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Workflow run could not be stopped: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def diff_workflows(workflow_id_a, workflow_id_b, brief, access_token, context_lines):
"""Return the list of differences between two workflows.
:param workflow_id_a: UUID which identifies the first workflow.
:param workflow_id_b: UUID which identifies the second workflow.
:param brief: Flag specifying desired detail in diff.
:param context_lines: Optional parameter to set the number of
context lines shown in the diff output.
:param access_token: API token of user requesting diff.
:returns: A list of dictionaries composed by `asset`, `type`, `lines`,
`a` and `b`. Asset refers to the workflow asset where a
difference was found, type refers to the asset type, lines refer
to the lines of the file where the differences are and a, b fields
are the actual lines that differ.
"""
try:
(response, http_response) = current_rs_api_client.api.get_workflow_diff(
workflow_id_or_name_a=workflow_id_a,
workflow_id_or_name_b=workflow_id_b,
brief=brief,
context_lines=context_lines,
access_token=access_token,
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"File list could not be retrieved: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def open_interactive_session(
workflow, access_token, interactive_session_type, interactive_session_configuration
):
"""Open an interactive session inside the workflow workspace.
:param workflow: Workflow which workspace will be available inside the
interactive session.
:param access_token: Workflow owner REANA access token.
:param interactive_session_type: Type of interactive session to spawn.
:param interactive_session_configuration: Specific configuration for
the interactive session.
:return: Gives the relative path to the interactive service.
"""
try:
(response, http_response) = current_rs_api_client.api.open_interactive_session(
workflow_id_or_name=workflow,
access_token=access_token,
interactive_session_type=interactive_session_type,
interactive_session_configuration=interactive_session_configuration,
).result()
if http_response.status_code == 200:
return response["path"]
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Interactive session could not be opened: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def close_interactive_session(workflow, access_token):
"""Close an interactive workflow session.
:param workflow: Workflow name to close.
:param access_token: Workflow owner REANA access token.
:return: Gives the relative path to the interactive service.
"""
try:
(response, http_response) = current_rs_api_client.api.close_interactive_session(
workflow_id_or_name=workflow, access_token=access_token,
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Interactive session could not be closed: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def mv_files(source, target, workflow, access_token):
"""Move target file(s) within workspace.
:param source: source filename or path.
:param target: target filename or path.
:param workflow_id: UUID which identifies the workflowself.
:param access_token: token of user.
"""
try:
(response, http_response) = current_rs_api_client.api.move_files(
source=source,
target=target,
workflow_id_or_name=workflow,
access_token=access_token,
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Files move command failed: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def get_workflow_disk_usage(workflow, parameters, access_token):
"""Display disk usage workflow."""
try:
(response, http_response) = current_rs_api_client.api.get_workflow_disk_usage(
workflow_id_or_name=workflow,
parameters=parameters,
access_token=access_token,
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Workflow disk usage could not be retrieved: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def add_secrets(secrets, overwrite, access_token):
"""Add new secrets.
:param secrets: dictionary containing all the secrets to be sent.
The dictionary with secret names for keys and for each key there is
a dictionary with two fields:
- 'value': a base64 encoded file or literal string
- 'type': 'file' or 'env'
:param overwrite: whether secrets should be overwritten when they
already exist.
:param access_token: access token of the current user.
"""
try:
(response, http_response) = current_rs_api_client.api.add_secrets(
secrets=secrets, access_token=access_token, overwrite=overwrite
).result()
if http_response.status_code == 201:
return response
else:
raise Exception(
"Expected status code 201 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Secrets could not be added: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
if e.status_code == 409:
raise REANASecretAlreadyExists()
else:
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def delete_secrets(secrets, access_token):
"""Delete a list of secrets.
:param secrets: List of secret names to be deleted.
:param access_token: access token of the current user.
"""
try:
(response, http_response) = current_rs_api_client.api.delete_secrets(
secrets=secrets, access_token=access_token
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
if e.response.status_code == 404:
raise REANASecretDoesNotExist(e.response.json())
else:
logging.debug(
"Secrets could not be deleted: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code,
e.response.reason,
e.response.json()["message"],
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e
def list_secrets(access_token):
"""List user secrets.
:param access_token: access token of the current user.
"""
try:
(response, http_response) = current_rs_api_client.api.get_secrets(
access_token=access_token
).result()
if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(status_code=http_response.status_code)
)
except HTTPError as e:
logging.debug(
"Secrets could not be listed: "
"\nStatus: {}\nReason: {}\n"
"Message: {}".format(
e.response.status_code, e.response.reason, e.response.json()["message"]
)
)
raise Exception(e.response.json()["message"])
except Exception as e:
raise e