-
Notifications
You must be signed in to change notification settings - Fork 0
/
perun_propagation_service.py
272 lines (220 loc) · 9.85 KB
/
perun_propagation_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
#!/usr/bin/env python
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Simple Perun propagation service.
Using this service in a real world scenario makes it possibly necessary
to implement the upload function in a thread. But the 'process_tarball'
method shouldn't run parallel.
"""
import json
import logging
import os
import shutil
import sys
import tarfile
import tempfile
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from denbi.perun.endpoint import Endpoint
from denbi.perun.keystone import KeyStone
from flask import Flask
from flask import request
import traceback
# logging formatter
fmt = logging.Formatter('[%(asctime)s] - (%(name)s/%(levelname)s) - %(message)s')
# create a StreamHandler for reporting
report_ch = logging.StreamHandler(sys.stdout)
report_ch.setLevel(logging.INFO)
report_ch.setFormatter(fmt)
# configure 'report' logger
report = logging.getLogger('report')
report.setLevel(logging.INFO)
report.addHandler(report_ch)
app = Flask(__name__)
# check if config file named 'pka.json' is found
if os.path.isfile("/etc/pka.json"):
report.info(f"Load Configuration from file '/etc/pka.json'")
app.config.from_file(os.getcwd() + "/etc/pka.json", load=json.load)
elif os.path.isfile(os.getcwd()+"/pka.json"):
report.info(f"Load Configuration from file '{os.getcwd()}/pka.json'")
app.config.from_file(os.getcwd() + "/pka.json", load=json.load)
else:
report.info("Get Configuration from environment")
app.config.from_prefixed_env("PKA")
report.info("Check CONFIG options.")
if not app.config.get('BASE_DIR', False):
app.config['BASE_DIR'] = tempfile.mkdtemp()
if not app.config.get('LOG_DIR', False):
app.config['LOG_DIR'] = "."
if not app.config.get('LOG_LEVEL', False):
app.config['LOG_LEVEL'] = "INFO"
elif app.config.get('LOG_LEVEL') not in ("ERROR","WARNING","INFO","DEBUG"):
report.error(f"Unsupported LOG_LEVEL '{app.config.get('LOG_LEVEL')}', must be one of ERROR, WARNING, INFO or DEBUG")
sys.exit(4)
if not app.config.get('TARGET_DOMAIN_NAME', False):
app.config['TARGET_DOMAIN_NAME'] = 'elixir'
if not app.config.get('DEFAULT_ROLE', False):
app.config['DEFAULT_ROLE'] = 'user'
if app.config.get('SUPPORT_DEFAULT_SSH_SGRULE', False) and not app.config.get('SUPPORT_NETWORK', False):
app.config['SUPPORT_NETWORK'] = True
if app.config.get('SUPPORT_NETWORK', False) and not app.config.get('SUPPORT_ROUTER', False):
app.config['SUPPORT_ROUTER'] = True
if app.config.get('SUPPORT_ROUTER', False) and not app.config.get('EXTERNAL_NETWORK_ID', False):
report.error("if 'SUPPORT_ROUTER' is enabled, 'EXTERNAL_NETWORK_ID' must be set.")
sys.exit(4)
if not app.config.get('SSH_KEY_BLOCKLIST', False):
app.config['SSH_KEY_BLOCKLIST'] = []
PKA_KEYS = ('BASE_DIR', 'KEYSTONE_READ_ONLY', 'CLEANUP',
'TARGET_DOMAIN_NAME', 'DEFAULT_ROLE', 'NESTED',
'ELIXIR_NAME', 'SUPPORT_QUOTAS', 'SUPPORT_ROUTER',
'SUPPORT_NETWORK', 'SUPPORT_DEFAULT_SSH_SGRULE',
'EXTERNAL_NETWORK_ID', 'LOG_DIR', 'LOG_LEVEL',
'SSH_KEY_BLOCKLIST')
config_str_list = []
config_str_list.append("I'm using the following configuration:")
config_str_list.append(f"+{'-' * 32}+{'-' * 42}+")
for key in sorted(PKA_KEYS):
config_str_list.append(f"| {key:30} | {str(app.config.get(key, 'False')):40} |")
config_str_list.append(f"+{'-' * 32}+{'-' * 42}+")
report.info('\n'.join(config_str_list))
# if app.config contains Openstack specific variables (starting with OS_)
# put them in an extra environment
# check if minimum set of OS keys is provided.
local_environment = {}
necessary_keys = ["OS_AUTH_URL",
"OS_USERNAME",
"OS_USER_DOMAIN_NAME",
"OS_PROJECT_NAME",
"OS_PASSWORD"]
for key in app.config.keys():
if key.startswith("OS_"):
local_environment[key] = app.config.get(key)
if key in necessary_keys:
necessary_keys.remove(key)
if not local_environment:
local_environment = None
else:
if necessary_keys:
report.error(f"{','.join(necessary_keys)} is/are mandatory and is/are missing in configuration or environment.")
sys.exit(4)
# create a FileHandler for logging
log_ch = logging.FileHandler(app.config.get("LOG_DIR", ".") + "/pka.log")
log_ch.setLevel(app.config.get("LOG_LEVEL"))
log_ch.setFormatter(fmt)
# configure 'denbi' logger
denbi = logging.getLogger('denbi')
denbi.setLevel(app.config.get("LOG_LEVEL"))
denbi.addHandler(log_ch)
# adjust 'report' logger Log Level according to pka configuration
report.setLevel(app.config.get("LOG_LEVEL"))
# Create thread executor
executor = ThreadPoolExecutor(max_workers=1)
def strtobool(val):
"""Convert a string representation of truth to true or false .
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.
"""
val = str(val).lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return True
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
return False
else:
raise ValueError("invalid truth value %r" % (val,))
def process_tarball(tarball_path,
base_dir=tempfile.mkdtemp(),
read_only=False,
cleanup=False,
target_domain_name='elixir',
default_role='user',
nested=False,
support_elixir_name=False,
support_quotas=False,
support_router=False,
external_network_id='',
support_network=False,
support_default_ssh_sgrule=False,
ssh_key_blocklist=None):
"""
Process Perun propagated tarball.
"""
if ssh_key_blocklist is None:
ssh_key_blocklist = []
d = datetime.today()
dir = f"{base_dir}/{d.year}_{d.month}_{d.day}_{d.hour}:{d.minute}:{d.second}.{d.microsecond}"
os.mkdir(dir)
report.info("Processing data uploaded by Perun: %s" % tarball_path)
# extract tar file
tar = tarfile.open(tarball_path, "r:gz")
tar.extractall(path=dir)
tar.close()
# import into keystone
keystone = KeyStone(default_role=default_role,
create_default_role=True,
target_domain_name=target_domain_name,
read_only=read_only,
nested=nested,
environ=local_environment)
endpoint = Endpoint(keystone=keystone,
mode="denbi_portal_compute_center",
support_elixir_name=support_elixir_name,
support_quotas=support_quotas,
support_router=support_router,
external_network_id=external_network_id,
support_network=support_network,
support_default_ssh_sgrule=support_default_ssh_sgrule,
ssh_key_blocklist=ssh_key_blocklist
)
endpoint.import_data(dir + '/users.scim', dir + '/groups.scim')
report.info("Finished processing %s" % tarball_path)
# Cleanup
if cleanup:
shutil.rmtree(dir)
@app.route("/upload", methods=['PUT'])
def upload():
"""Receive a perun tarball, store it in a temporary file and process it."""
# Create a tempfile to write the data to. delete=False because we will
# close after writing, before processing, and this would normally cause a
# tempfile to disappear.
file = tempfile.NamedTemporaryFile(prefix='perun_upload', suffix='.tar.gz', delete=False)
# store uploaded data
file.write(request.get_data())
file.close()
# execute task
result = executor.submit(process_tarball,
file.name,
base_dir=app.config.get('BASE_DIR'),
read_only=strtobool(app.config.get('KEYSTONE_READ_ONLY', "False")),
cleanup=strtobool(app.config.get('CLEANUP', "False")),
target_domain_name=app.config.get('TARGET_DOMAIN_NAME'),
default_role=app.config.get('DEFAULT_ROLE'),
nested=strtobool(app.config.get('NESTED', "False")),
support_elixir_name=strtobool(app.config.get('ELIXIR_NAME', "False")),
support_quotas=strtobool(app.config.get('SUPPORT_QUOTAS', "False")),
support_router=strtobool(app.config.get('SUPPORT_ROUTER', "False")),
external_network_id=app.config.get('EXTERNAL_NETWORK_ID'),
support_network=strtobool(app.config.get('SUPPORT_NETWORK', "False")),
support_default_ssh_sgrule=strtobool(app.config.get('SUPPORT_DEFAULT_SSH_SGRULE', "False")),
ssh_key_blocklist=app.config.get('SSH_KEY_BLOCKLIST', None)
)
# if task fails with an exception, the thread pool catches the exception,
# stores it, then re-raises it when we call the result() function.
try:
result.result()
except Exception:
traceback.print_exc()
if app.config.get('CLEANUP', False):
os.unlink(file)
return ""
if __name__ == "__main__":
app.run(host=app.config['HOST'], port=app.config['PORT'])