-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.py
executable file
·359 lines (284 loc) · 11.7 KB
/
app.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
#!/usr/bin/env python3
# thoth-init-job
# Copyright(C) 2018, 2019, 2020 Fridolin Pokorny, Francesco Murdaca
#
# This program is free software: you can redistribute it and / or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Initialize a fresh Thoth deployment."""
import os
import logging
import yaml
import json
from typing import List
from typing import Generator
from urllib.parse import urljoin
import requests
import click
from bs4 import BeautifulSoup
from thoth.common import init_logging
from thoth.common import OpenShift
from thoth.common import __version__ as __common__version__
from thoth.python import Source
from thoth.python import __version__ as __python__version__
from thoth.storages import GraphDatabase
from thoth.storages import __version__ as __storage__version__
__version__ = "0.8.1"
__component_version__ = (
f"{__version__}+"
f"python.{__python__version__}."
f"storage.{__storage__version__}."
f"common.{__common__version__}"
)
init_logging()
_LOGGER = logging.getLogger("thoth.init_job")
_DEFAULT_INDEX_BASE_URL = "https://tensorflow.pypi.thoth-station.ninja/index"
_PYPI_SIMPLE_API_URL = "https://pypi.org/simple"
_PYPI_WAREHOUSE_JSON_API_URL = "https://pypi.org/pypi"
_CORE_PACKAGES = ["setuptools", "six", "pip"]
_LOGGER.info("Thoth Initializer v" + __component_version__)
def _html_parse_listing(url: str) -> Generator[str, None, None]:
"""Parse listing of a HTML document."""
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, "lxml")
for row in soup.find("table").find_all("tr"):
for cell in row.find_all("td"):
if cell.a:
if cell.a.text == "Parent Directory":
continue
if not cell.a.text.endswith("/"):
yield cell.a.text + "/"
yield cell.a.text
def _get_build_configuration(url: str) -> List[str]:
"""Get available configuration for a distro."""
result = []
for configuration in _html_parse_listing(url):
sub_url = urljoin(url, configuration)
present_subdirs = set(_html_parse_listing(sub_url))
if present_subdirs - {"simple/"}:
_LOGGER.error(
"Additional sub-directories not compliant with PEP-503 found (expected only 'simple/'): %r",
present_subdirs - {"simple/"},
)
if "simple/" not in present_subdirs:
_LOGGER.error("No 'simple/' found on URL %r, cannot treat as PEP-503 compliant simple API", url)
continue
_LOGGER.info("Detected build configuration %r at %s", configuration[:-1], url)
result.append(urljoin(url, configuration + "simple"))
return result
def _list_available_indexes(url: str) -> List[str]:
"""List available indexes on AICoE index."""
result = []
_LOGGER.info("Listing available indexes on AICoE index %r", url)
indexes = list(_html_parse_listing(url))
if not indexes:
_LOGGER.error("No AICoE indexes found at %r", url)
for item in indexes:
_LOGGER.info("Discovering compliant releases at %r", url)
result.extend(_get_build_configuration(urljoin(url, item)))
return result
def _register_indexes(graph: GraphDatabase, index_base_url: str, dry_run: bool = False) -> List[str]:
"""Register available AICoE indexes into Thoth's database."""
_LOGGER.info("Registering PyPI index %r", _PYPI_SIMPLE_API_URL)
index_urls = [_PYPI_SIMPLE_API_URL]
if not dry_run:
_LOGGER.info("Registering index %r", index_urls[0])
graph.register_python_package_index(
index_urls[0], warehouse_api_url=_PYPI_WAREHOUSE_JSON_API_URL, verify_ssl=True, enabled=True
)
aicoe_indexes = _list_available_indexes(index_base_url)
if not aicoe_indexes:
_LOGGER.error("No AICoE indexes to register")
for index_url in aicoe_indexes:
_LOGGER.info("Registering index %r", index_url)
if not dry_run:
graph.register_python_package_index(index_url, enabled=True)
index_urls.append(index_url)
return index_urls
def _take_data_science_packages() -> List[str]:
"""Take list of Python Packages for data science."""
data_science_packages_file = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "hundredsDatasciencePackages.yaml"
)
with open(data_science_packages_file) as yaml_file:
requested_packages = yaml.safe_load(yaml_file)
data_science_packages = []
for package_name in requested_packages["hundreds_datascience_packages"]:
_LOGGER.info("Registering package %r", package_name)
data_science_packages.append(package_name)
return data_science_packages
def _schedule_default_packages_solver_jobs(packages: List[str], index_urls: List[str]) -> int:
"""Run solver jobs for Python packages list selected."""
openshift = OpenShift()
counter = 0
for index_url in index_urls:
_LOGGER.debug("consider index %r", index_url)
source = Source(index_url)
for package_name in packages:
_LOGGER.debug("Obtaining %r versions", package_name)
versions = []
try:
versions = source.get_package_versions(package_name)
except Exception as exc:
_LOGGER.exception(str(exc))
if versions:
for version in versions:
_LOGGER.info("Scheduling package_name %r in package_version %r", package_name, version)
number_workflows = _do_schedule_solver_jobs(openshift, [index_url], package_name, version)
counter += number_workflows
_LOGGER.info(f"Already scheduled {counter} solver workflows...")
return counter
def _do_schedule_solver_jobs(
openshift: OpenShift, index_urls: List[str], package_name: str, package_version: str
) -> int:
"""Run Python solvers for the given package in specified version."""
_LOGGER.info(
"Running solver job for package %r in version %r, results will be scheduled using Argo workflow",
package_name,
package_version,
)
solvers_run = openshift.schedule_all_solvers(packages=f"{package_name}==={package_version}", indexes=index_urls)
_LOGGER.debug("Response when running solver jobs: %r", solvers_run)
return len(solvers_run)
@click.command()
@click.option(
"--verbose", "-v", is_flag=True, envvar="THOTH_VERBOSE_INIT_JOB", help="Be verbose about what's going on."
)
@click.option(
"--dry-run",
"-n",
is_flag=True,
envvar="THOTH_INIT_JOB_DRY_RUN",
help="Just print stuff on LOG, do not change any data.",
)
@click.option(
"--index-base-url",
"-i",
type=str,
default=_DEFAULT_INDEX_BASE_URL,
show_default=True,
help="AICoE URL base for discovering packages.",
)
@click.option(
"--initialize-schema",
required=False,
is_flag=True,
envvar="THOTH_INIT_JOB_INITIALIZE_SCHEMA",
help="Initialize schema in Thoth Knowledge Graph.",
)
@click.option(
"--register-indexes",
required=False,
is_flag=True,
envvar="THOTH_INIT_JOB_REGISTER_INDEXES",
help="Register indexes in Thoth Knowledge Graph.",
)
@click.option(
"--configure-solver-rules",
required=False,
is_flag=True,
envvar="THOTH_INIT_JOB_CONFIGURE_SOLVER_RULES",
help="Configure solver rules in the deployment.",
)
@click.option(
"--solve-core-packages",
required=False,
is_flag=True,
envvar="THOTH_INIT_JOB_REGISTER_CORE_PACKAGES",
help="Schedule solver jobs for core packages.",
)
@click.option(
"--solve-data-science-packages",
required=False,
is_flag=True,
envvar="THOTH_INIT_JOB_REGISTER_DATA_SCIENCE_PACKAGES",
help="Schedule solver jobs for data science packages.",
)
def cli(
verbose: bool = False,
dry_run: bool = False,
index_base_url: str = _DEFAULT_INDEX_BASE_URL,
initialize_schema: bool = False,
register_indexes: bool = False,
solve_core_packages: bool = False,
configure_solver_rules: bool = False,
solve_data_science_packages: bool = False,
):
"""Register AICoE indexes in Thoth's database."""
total_scheduled_solvers = 0
if not dry_run:
graph = GraphDatabase()
graph.connect()
elif dry_run:
_LOGGER.info("dry-run: not talking to Thoth Knowledge Graph...")
if verbose:
_LOGGER.setLevel(logging.DEBUG)
if not index_base_url.endswith("/"):
index_base_url += "/"
if initialize_schema:
if not dry_run:
_LOGGER.info("Initializing schema")
graph.initialize_schema()
elif dry_run:
_LOGGER.info("dry-run: not initializing schema...")
if configure_solver_rules:
if not dry_run:
_LOGGER.info("Configuring solver rules")
with open("solver_rules.json") as f:
solver_rules = json.load(f)
for rule in solver_rules:
try:
graph.create_python_rule(**rule)
except Exception:
_LOGGER.error("Failed to register Python rule %r", rule)
raise
else:
_LOGGER.info("dry-run: configuring solver rules")
if register_indexes:
if not dry_run:
_LOGGER.info("Registering indexes...")
elif dry_run:
_LOGGER.info("dry-run: not registering indexes...")
_register_indexes(graph, index_base_url, dry_run)
if solve_core_packages:
if not dry_run:
_LOGGER.info("Retrieving registered indexes from Thoth Knowledge Graph...")
registered_indexes = graph.get_python_package_index_urls_all()
if not registered_indexes:
raise ValueError("No registered indexes found in the database")
_LOGGER.info("Scheduling solver jobs for core packages...")
scheduled_solvers = _schedule_default_packages_solver_jobs(
packages=_CORE_PACKAGES, index_urls=registered_indexes
)
_LOGGER.info(f"Total number of solver workflows scheduled for core packages: {scheduled_solvers}!")
total_scheduled_solvers += scheduled_solvers
elif dry_run:
_LOGGER.info("dry-run: not scheduling core packages solver jobs!")
if solve_data_science_packages:
data_science_packages = _take_data_science_packages()
if not dry_run:
_LOGGER.info("Retrieving registered indexes from Thoth Knowledge Graph...")
registered_indexes = graph.get_python_package_index_urls_all()
if not registered_indexes:
raise ValueError("No registered indexes found in the database")
_LOGGER.info("Scheduling solver jobs for data science packages...")
scheduled_solvers = _schedule_default_packages_solver_jobs(
packages=data_science_packages, index_urls=registered_indexes
)
_LOGGER.info(f"Total number of solver workflows scheduled for DS packages: {scheduled_solvers}!")
total_scheduled_solvers += scheduled_solvers
elif dry_run:
_LOGGER.info("dry-run: not scheduling data science packages solver jobs!")
_LOGGER.info(f"Total number of solver workflows scheduled: {total_scheduled_solvers}!")
if __name__ == "__main__":
cli()