Repository navigation
Expand file tree
/
Copy pathmock_workflow_server.py
More file actions
112 lines (89 loc) · 2.77 KB
/
Copy pathmock_workflow_server.py
File metadata and controls
112 lines (89 loc) · 2.77 KB
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
"""Mock Workflow Server.
Delivers dummy JSON for workflows that it reads from workflows.json
"""
from __future__ import with_statement
import os
try:
import json
except ImportError:
import simplejson as json
from flask import Flask, abort
app = Flask(__name__)
@app.route('/', methods=['GET'])
def hello():
return ('<h1>Welcome to the workflow server</h1>'
'<p>'
'<a href="workflows/">List workflows</a>'
'</p>'
)
@app.route('/workflows/', methods=['GET'])
def list_workflows():
"""Return a list of existing workflows."""
global workflows
return (
json.dumps(workflows.keys()),
200,
{'Content-Type': "application/json"}
)
@app.route('/workflows/<identifier>', methods=['GET'])
def get_workflow(identifier):
"""Get and return the requested workflow."""
global workflows
try:
wf = workflows[identifier]
except KeyError:
abort(404)
else:
# Replace URL with values from environment
for i, wf_item in enumerate(wf['wf']):
wf['wf'][i] = update_url(wf_item)
return (
json.dumps(wf),
200,
{'Content-Type': "application/json"}
)
@app.route('/workflows/<identifier>/<int:step>', methods=['GET'])
def get_workflow_step(identifier, step):
"""Get and return the requested workflow step."""
global workflows
try:
wf_item = workflows[identifier]['wf'][step]
except (KeyError, IndexError):
abort(404)
else:
# Replace URL with values from environment
wf_item = update_url(wf_item)
return (
json.dumps(wf_item),
200,
{'Content-Type': "application/json"}
)
def update_url(wf_item):
"""Update URL for a workflow step and return the step."""
global COMPONENT_HOST, COMPONENT_PORT
if COMPONENT_HOST is not None:
if COMPONENT_PORT is not None:
wf_item['url'] = wf_item['url'].replace(
'://127.0.0.1',
'://{0}:{1}'.format(COMPONENT_HOST, COMPONENT_PORT)
)
else:
wf_item['url'] = wf_item['url'].replace(
'://127.0.0.1',
'://{0}'.format(COMPONENT_HOST)
)
if COMPONENT_PORT is not None:
wf_item['url'] = wf_item['url'].replace(
'://127.0.0.1',
'://127.0.0.1:{0}'.format(COMPONENT_PORT)
)
return wf_item
# Load dummy workflows from json
with open('workflows.json') as fh:
workflow_list = json.load(fh)
workflows = {wf['id']: wf for wf in workflow_list}
app.debug = False
COMPONENT_HOST = os.environ.get('PLANK_HOST')
COMPONENT_PORT = os.environ.get('PLANK_PORT')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)