-
Notifications
You must be signed in to change notification settings - Fork 26
/
services.py
598 lines (524 loc) · 20.6 KB
/
services.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
"""
NOTE: to dump json-schema from CLI use
python -c "from models_library.services import ServiceDockerData as cls; print(cls.schema_json(indent=2))" > services-schema.json
"""
from datetime import datetime
from enum import Enum
from typing import Any, Optional, Union
from pydantic import (
BaseModel,
EmailStr,
Extra,
Field,
HttpUrl,
StrictBool,
StrictFloat,
StrictInt,
constr,
validator,
)
from .basic_regex import VERSION_RE
from .boot_options import BootOption, BootOptions
from .services_ui import Widget
from .utils.json_schema import (
InvalidJsonSchema,
any_ref_key,
jsonschema_validate_schema,
)
# CONSTANTS -------------------------------------------
# NOTE: needs to end with / !!
SERVICE_KEY_RE = r"^(simcore)/(services)/(comp|dynamic|frontend)(/[\w/-]+)+$"
DYNAMIC_SERVICE_KEY_RE = r"^(simcore)/(services)/dynamic(/[\w/-]+)+$"
DYNAMIC_SERVICE_KEY_FORMAT = "simcore/services/dynamic/{service_name}"
COMPUTATIONAL_SERVICE_KEY_RE = r"^(simcore)/(services)/comp(/[\w/-]+)+$"
COMPUTATIONAL_SERVICE_KEY_FORMAT = "simcore/services/comp/{service_name}"
KEY_RE = SERVICE_KEY_RE # TODO: deprecate this global constant by SERVICE_KEY_RE
PROPERTY_TYPE_RE = r"^(number|integer|boolean|string|ref_contentSchema|data:([^/\s,]+/[^/\s,]+|\[[^/\s,]+/[^/\s,]+(,[^/\s]+/[^/,\s]+)*\]))$"
PROPERTY_KEY_RE = r"^[-_a-zA-Z0-9]+$" # TODO: PC->* it would be advisable to have this "variable friendly" (see VARIABLE_NAME_RE)
FILENAME_RE = r".+"
LATEST_INTEGRATION_VERSION = "1.0.0"
# CONSTRAINT TYPES -------------------------------------------
PropertyName = constr(regex=PROPERTY_KEY_RE)
FileName = constr(regex=FILENAME_RE)
ServiceKey = constr(regex=KEY_RE)
ServiceVersion = constr(regex=VERSION_RE)
class ServiceType(str, Enum):
COMPUTATIONAL = "computational"
DYNAMIC = "dynamic"
FRONTEND = "frontend"
BACKEND = "backend"
# TODO: create a flags enum that accounts for every column
#
# | service name | defininition | implementation | runs | ``ServiceType`` | |
# | --------------- | ------------ | -------------- | ----------------------- | ----------------------------- | --------------- |
# | ``file-picker`` | BE | FE | FE | ``ServiceType.FRONTEND`` | function |
# | ``isolve`` | DI-labels | DI | Dask-BE (own container) | ``ServiceType.COMPUTATIONAL`` | container |
# | ``jupyter-*`` | DI-labels | DI | DySC-BE (own container) | ``ServiceType.DYNAMIC`` | container |
# | ``iterator-*`` | BE | BE | BE (webserver) | ``ServiceType.BACKEND`` | function |
# | ``pyfun-*`` | BE | BE | Dask-BE (dask-sidecar) | ``ServiceType.COMPUTATIONAL`` | function |
#
#
# where FE (front-end), DI (docker image), Dask/DySC (dask/dynamic sidecar), BE (backend).
# MODELS -------------------------------------------
class Badge(BaseModel):
name: str = Field(
...,
description="Name of the subject",
examples=["travis-ci", "coverals.io", "github.io"],
)
image: HttpUrl = Field(
...,
description="Url to the badge",
examples=[
"https://travis-ci.org/ITISFoundation/osparc-simcore.svg?branch=master",
"https://coveralls.io/repos/github/ITISFoundation/osparc-simcore/badge.svg?branch=master",
"https://img.shields.io/website-up-down-green-red/https/itisfoundation.github.io.svg?label=documentation",
],
)
url: HttpUrl = Field(
...,
description="Link to the status",
examples=[
"https://travis-ci.org/ITISFoundation/osparc-simcore 'State of CI: build, test and pushing images'",
"https://coveralls.io/github/ITISFoundation/osparc-simcore?branch=master 'Test coverage'",
"https://itisfoundation.github.io/",
],
)
class Config:
extra = Extra.forbid
class Author(BaseModel):
name: str = Field(..., description="Name of the author", example="Jim Knopf")
email: EmailStr = Field(
...,
examples=["sun@sense.eight", "deleen@minbar.bab"],
description="Email address",
)
affiliation: Optional[str] = Field(
None, examples=["Sense8", "Babylon 5"], description="Affiliation of the author"
)
class Config:
extra = Extra.forbid
class BaseServiceIOModel(BaseModel):
"""
Base class for service input/outputs
"""
## management
### human readable descriptors
display_order: Optional[float] = Field(
None,
alias="displayOrder",
deprecated=True,
description="DEPRECATED: new display order is taken from the item position. This will be removed.",
)
label: str = Field(..., description="short name for the property", example="Age")
description: str = Field(
...,
description="description of the property",
example="Age in seconds since 1970",
)
# mathematical and physics descriptors
property_type: str = Field(
...,
alias="type",
description="data type expected on this input glob matching for data type is allowed",
examples=[
"number",
"boolean",
"data:*/*",
"data:text/*",
"data:[image/jpeg,image/png]",
"data:application/json",
"data:application/json;schema=https://my-schema/not/really/schema.json",
"data:application/vnd.ms-excel",
"data:text/plain",
"data:application/hdf5",
"data:application/edu.ucdavis@ceclancy.xyz",
],
regex=PROPERTY_TYPE_RE,
)
content_schema: Optional[dict[str, Any]] = Field(
None,
description="jsonschema of this input/output. Required when type='ref_contentSchema'",
alias="contentSchema",
)
# value
file_to_key_map: Optional[dict[FileName, PropertyName]] = Field(
None,
alias="fileToKeyMap",
description="Place the data associated with the named keys in files",
examples=[{"dir/input1.txt": "key_1", "dir33/input2.txt": "key2"}],
)
# TODO: should deprecate since content_schema include units
unit: Optional[str] = Field(
None,
description="Units, when it refers to a physical quantity",
)
class Config:
extra = Extra.forbid
@validator("content_schema")
@classmethod
def check_type_is_set_to_schema(cls, v, values):
if v is not None:
if (ptype := values["property_type"]) != "ref_contentSchema":
raise ValueError(
"content_schema is defined but set the wrong type."
f"Expected type=ref_contentSchema but got ={ptype}."
)
return v
@validator("content_schema")
@classmethod
def check_valid_json_schema(cls, v):
if v is not None:
try:
jsonschema_validate_schema(schema=v)
if any_ref_key(v):
# SEE https://github.com/ITISFoundation/osparc-simcore/issues/3030
raise ValueError("Schemas with $ref are still not supported")
except InvalidJsonSchema as err:
failed_path = "->".join(map(str, err.path))
raise ValueError(
f"Invalid json-schema at {failed_path}: {err.message}"
) from err
return v
@classmethod
def _from_json_schema_base_implementation(
cls, port_schema: dict[str, Any]
) -> dict[str, Any]:
description = port_schema.pop("description", port_schema["title"])
data = {
"label": port_schema["title"],
"description": description,
"type": "ref_contentSchema",
"contentSchema": port_schema,
}
return data
class ServiceInput(BaseServiceIOModel):
"""
Metadata on a service input port
"""
# TODO: should deprecate since content_schema include defaults as well
default_value: Optional[Union[StrictBool, StrictInt, StrictFloat, str]] = Field(
None, alias="defaultValue", examples=["Dog", True]
)
widget: Optional[Widget] = Field(
None,
description="custom widget to use instead of the default one determined from the data-type",
)
class Config(BaseServiceIOModel.Config):
schema_extra = {
"examples": [
# file-wo-widget:
{
"displayOrder": 1,
"label": "Input files",
"description": "Files downloaded from service connected at the input",
"type": "data:*/*",
},
# v2
{
"displayOrder": 2,
"label": "Sleep Time",
"description": "Time to wait before completion",
"type": "number",
"defaultValue": 0,
"unit": "second",
"widget": {"type": "TextArea", "details": {"minHeight": 3}},
},
# latest:
{
"label": "Sleep Time",
"description": "Time to wait before completion",
"type": "number",
"defaultValue": 0,
"unit": "second",
"widget": {"type": "TextArea", "details": {"minHeight": 3}},
},
{
"label": "array_numbers",
"description": "Some array of numbers",
"type": "ref_contentSchema",
"contentSchema": {
"title": "list[number]",
"type": "array",
"items": {"type": "number"},
},
},
{
"label": "my_object",
"description": "Some object",
"type": "ref_contentSchema",
"contentSchema": {
"title": "an object named A",
"type": "object",
"properties": {
"i": {"title": "Int", "type": "integer", "default": 3},
"b": {"title": "Bool", "type": "boolean"},
"s": {"title": "Str", "type": "string"},
},
"required": ["b", "s"],
},
},
],
}
@classmethod
def from_json_schema(cls, port_schema: dict[str, Any]) -> "ServiceInput":
"""Creates input port model from a json-schema"""
data = cls._from_json_schema_base_implementation(port_schema)
return cls.parse_obj(data)
class ServiceOutput(BaseServiceIOModel):
widget: Optional[Widget] = Field(
None,
description="custom widget to use instead of the default one determined from the data-type",
deprecated=True,
)
class Config(BaseServiceIOModel.Config):
schema_extra = {
"examples": [
{
"displayOrder": 2,
"label": "Time Slept",
"description": "Time the service waited before completion",
"type": "number",
},
{
"displayOrder": 2,
"label": "Time Slept",
"description": "Time the service waited before completion",
"type": "number",
"unit": "second",
},
{
"label": "Time Slept",
"description": "Time the service waited before completion",
"type": "number",
"unit": "second",
},
{
"label": "Output file 1",
"displayOrder": 4.0,
"description": "Output file uploaded from the outputs folder",
"type": "data:*/*",
},
]
}
@classmethod
def from_json_schema(cls, port_schema: dict[str, Any]) -> "ServiceOutput":
"""Creates output port model from a json-schema"""
data = cls._from_json_schema_base_implementation(port_schema)
return cls.parse_obj(data)
class ServiceKeyVersion(BaseModel):
"""This pair uniquely identifies a services"""
key: str = Field(
...,
description="distinctive name for the node based on the docker registry path",
regex=KEY_RE,
examples=[
"simcore/services/comp/itis/sleeper",
"simcore/services/dynamic/3dviewer",
],
)
version: str = Field(
...,
description="service version number",
regex=VERSION_RE,
examples=["1.0.0", "0.0.1"],
)
class _BaseServiceCommonDataModel(BaseModel):
name: str = Field(
...,
description="short, human readable name for the node",
example="Fast Counter",
)
thumbnail: Optional[HttpUrl] = Field(
None,
description="url to the thumbnail",
examples=[
"https://user-images.githubusercontent.com/32800795/61083844-ff48fb00-a42c-11e9-8e63-fa2d709c8baf.png"
],
)
description: str = Field(
...,
description="human readable description of the purpose of the node",
examples=[
"Our best node type",
"The mother of all nodes, makes your numbers shine!",
],
)
@validator("thumbnail", pre=True, always=False)
@classmethod
def validate_thumbnail(cls, value): # pylint: disable=no-self-argument,no-self-use
if value == "":
return None
return value
ServiceInputsDict = dict[PropertyName, ServiceInput]
ServiceOutputsDict = dict[PropertyName, ServiceOutput]
class ServiceDockerData(ServiceKeyVersion, _BaseServiceCommonDataModel):
"""
Static metadata for a service injected in the image labels
This is one to one with node-meta-v0.0.1.json
"""
integration_version: Optional[str] = Field(
None,
alias="integration-version",
description="integration version number",
regex=VERSION_RE,
examples=["1.0.0"],
)
service_type: ServiceType = Field(
...,
alias="type",
description="service type",
examples=["computational"],
)
badges: Optional[list[Badge]] = Field(None)
authors: list[Author] = Field(..., min_items=1)
contact: EmailStr = Field(
...,
description="email to correspond to the authors about the node",
examples=["lab@net.flix"],
)
inputs: Optional[ServiceInputsDict] = Field(
..., description="definition of the inputs of this node"
)
outputs: Optional[ServiceOutputsDict] = Field(
..., description="definition of the outputs of this node"
)
boot_options: Optional[BootOptions] = Field(
None,
alias="boot-options",
description="Service defined boot options. These get injected in the service as env variables.",
)
class Config:
description = "Description of a simcore node 'class' with input and output"
extra = Extra.forbid
schema_extra = {
"examples": [
{
"name": "oSparc Python Runner",
"key": "simcore/services/comp/osparc-python-runner",
"type": "computational",
"integration-version": "1.0.0",
"version": "1.7.0",
"description": "oSparc Python Runner",
"contact": "smith@company.com",
"authors": [
{
"name": "John Smith",
"email": "smith@company.com",
"affiliation": "Company",
},
{
"name": "Richard Brown",
"email": "brown@uni.edu",
"affiliation": "University",
},
],
"inputs": {
"input_1": {
"displayOrder": 1,
"label": "Input data",
"description": "Any code, requirements or data file",
"type": "data:*/*",
}
},
"outputs": {
"output_1": {
"displayOrder": 1,
"label": "Output data",
"description": "All data produced by the script is zipped as output_data.zip",
"type": "data:*/*",
"fileToKeyMap": {"output_data.zip": "output_1"},
}
},
},
# latest
{
"name": "oSparc Python Runner",
"key": "simcore/services/comp/osparc-python-runner",
"type": "computational",
"integration-version": "1.0.0",
"version": "1.7.0",
"description": "oSparc Python Runner",
"contact": "smith@company.com",
"authors": [
{
"name": "John Smith",
"email": "smith@company.com",
"affiliation": "Company",
},
{
"name": "Richard Brown",
"email": "brown@uni.edu",
"affiliation": "University",
},
],
"inputs": {
"input_1": {
"label": "Input data",
"description": "Any code, requirements or data file",
"type": "data:*/*",
}
},
"outputs": {
"output_1": {
"label": "Output data",
"description": "All data produced by the script is zipped as output_data.zip",
"type": "data:*/*",
"fileToKeyMap": {"output_data.zip": "output_1"},
}
},
"boot-options": {
"example_service_defined_boot_mode": BootOption.Config.schema_extra[
"examples"
][
0
],
"example_service_defined_theme_selection": BootOption.Config.schema_extra[
"examples"
][
1
],
},
},
]
}
class ServiceMetaData(_BaseServiceCommonDataModel):
# Overrides all fields of _BaseServiceCommonDataModel:
# - for a partial update all members must be Optional
# FIXME: if API entry needs a schema to allow partial updates (e.g. patch/put),
# it should be implemented with a different model e.g. ServiceMetaDataUpdate
#
name: Optional[str]
thumbnail: Optional[HttpUrl]
description: Optional[str]
deprecated: Optional[datetime]
# user-defined metatada
classifiers: Optional[list[str]]
quality: dict[str, Any] = {}
class Config:
schema_extra = {
"example": {
"key": "simcore/services/dynamic/sim4life",
"version": "1.0.9",
"name": "sim4life",
"description": "s4l web",
"thumbnail": "http://thumbnailit.org/image",
"quality": {
"enabled": True,
"tsr_target": {
f"r{n:02d}": {"level": 4, "references": ""}
for n in range(1, 11)
},
"annotations": {
"vandv": "",
"limitations": "",
"certificationLink": "",
"certificationStatus": "Uncertified",
},
"tsr_current": {
f"r{n:02d}": {"level": 0, "references": ""}
for n in range(1, 11)
},
},
}
}