-
Notifications
You must be signed in to change notification settings - Fork 31
/
models.py
687 lines (566 loc) · 21.6 KB
/
models.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
# -*- coding: utf-8 -*-
#
# This file is part of REANA.
# Copyright (C) 2018, 2019 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.
"""Models for REANA Components."""
from __future__ import absolute_import
import enum
import uuid
from datetime import datetime
from reana_commons.utils import get_disk_usage
from reana_db.config import DB_SECRET_KEY, DEFAULT_QUOTA_RESOURCES
from reana_db.utils import build_workspace_path
from requests.sessions import session
from sqlalchemy import (
BigInteger,
Boolean,
Column,
DateTime,
Enum,
Float,
ForeignKey,
String,
Text,
UniqueConstraint,
event,
func,
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import relationship
from sqlalchemy_utils import EncryptedType, JSONType, UUIDType
from sqlalchemy_utils.models import Timestamp
from sqlalchemy_utils.types.encrypted.encrypted_type import AesEngine
Base = declarative_base()
def generate_uuid():
"""Generate new uuid."""
return str(uuid.uuid4())
class User(Base, Timestamp):
"""User table."""
__tablename__ = "user_"
__table_args__ = {"schema": "__reana"}
id_ = Column(UUIDType, primary_key=True, unique=True, default=generate_uuid)
email = Column(String(length=255), unique=True, primary_key=True)
full_name = Column(String(length=255))
username = Column(String(length=255))
tokens = relationship("UserToken", backref="user_", lazy="dynamic")
workflows = relationship("Workflow", backref="user_", lazy="dynamic")
audit_logs = relationship("AuditLog", backref="user_")
resources = relationship("UserResource", backref="user_")
def __init__(self, access_token=None, **kwargs):
"""Initialize user model."""
for k, v in kwargs.items():
setattr(self, k, v)
if access_token:
self.access_token = access_token
self.initialize_user_quota_limits()
@hybrid_property
def active_token(self):
"""REANA active access token object."""
return self.tokens.filter_by(
status=UserTokenStatus.active, type_=UserTokenType.reana
).one_or_none()
@hybrid_property
def access_token(self):
"""REANA active access token value."""
return self.active_token.token if self.active_token else None
@access_token.setter
def access_token(self, value):
"""REANA access token setter."""
from .database import Session
if self.tokens.count() and self.active_token:
raise Exception("User {} has already an active access token.".format(self))
if (
self.tokens.count()
and self.access_token_status == UserTokenStatus.requested.name
):
self.latest_access_token.status = UserTokenStatus.active
self.latest_access_token.token = value
else:
user_token = UserToken(
user_=self,
token=value,
status=UserTokenStatus.active,
type_=UserTokenType.reana,
)
Session.add(user_token)
@hybrid_property
def latest_access_token(self):
"""REANA most recent access token."""
latest_reana_token = (
self.tokens.filter_by(type_=UserTokenType.reana).order_by(
UserToken.created.desc()
)
).first()
return latest_reana_token or None
@hybrid_property
def access_token_status(self):
"""REANA most recent access token status."""
return (
self.latest_access_token.status.name if self.latest_access_token else None
)
def get_user_workspace(self):
"""Build user's workspace directory path.
:return: Path to the user's workspace directory.
"""
return build_workspace_path(self.id_)
def get_user_disk_usage(self, block_size=None):
"""Retrieve user disk usage information."""
try:
workspace_path = self.get_user_workspace()
disk_usage = get_disk_usage(workspace_path, True, block_size)
return disk_usage[0]["size"]
except Exception:
return -1
def get_user_quota_by_type(self, resource_type):
"""Aggregate user quota usage by resource type."""
def _get_health_status(usage, limit):
"""Calculate quota health status."""
health = QuotaHealth.healthy
if limit:
percentage = usage / limit * 100
if percentage >= 60:
if percentage >= 85:
health = QuotaHealth.critical
else:
health = QuotaHealth.warning
return health.name
quota_usage = 0
quota_limit = 0
for user_resource in self.resources:
if user_resource.resource.type_ == resource_type:
quota_usage += user_resource.quota_used
quota_limit += user_resource.quota_limit
return {
"usage": quota_usage,
"limit": quota_limit,
"health": _get_health_status(quota_usage, quota_limit),
}
def request_access_token(self):
"""Create user token and mark it as requested."""
from .database import Session
if self.tokens.count() and self.active_token:
raise Exception("User {} has already an active access token.".format(self))
if (
self.tokens.count()
and self.access_token_status == UserTokenStatus.requested.name
):
raise Exception(
"User {} has already requested an access" " token.".format(self)
)
user_token = UserToken(
user_=self,
token=None,
status=UserTokenStatus.requested,
type_=UserTokenType.reana,
)
Session.add(user_token)
Session.commit()
def log_action(self, action, details=None):
"""Create audit log entry for the user.
:param action: Type of action.
:type action: AuditLogAction
:param details: JSON field containing action details.
"""
from .database import Session
audit_log = AuditLog(user_id=self.id_, action=action, details=details)
Session.add(audit_log)
Session.commit()
return audit_log
def initialize_user_quota_limits(self):
"""Initialize user quota limits."""
resources = Resource.query.all()
for resource in resources:
self.resources.append(
UserResource(
user_id=self.id_,
resource_id=resource.id_,
quota_limit=0,
quota_used=0,
)
)
def get_quota_usage(self):
"""Get user quota usage information."""
return dict(
disk=dict(
usage=self.get_user_disk_usage(),
limit=self.get_user_quota_by_type(ResourceType.disk)["limit"],
health=self.get_user_quota_by_type(ResourceType.disk)["health"],
),
cpu=self.get_user_quota_by_type(ResourceType.cpu),
)
def __repr__(self):
"""User string represetantion."""
return "<User %r>" % self.id_
class UserTokenStatus(enum.Enum):
"""Enumeration of possible user token statuses."""
requested = 0
active = 1
revoked = 2
class UserTokenType(enum.Enum):
"""Enumeration of possible user token types."""
reana = 0
class UserToken(Base, Timestamp):
"""User tokens table."""
__tablename__ = "user_token"
__table_args__ = {"schema": "__reana"}
id_ = Column(UUIDType, primary_key=True, default=generate_uuid)
token = Column(
EncryptedType(String(length=255), DB_SECRET_KEY, AesEngine, "pkcs5"),
unique=True,
)
status = Column(Enum(UserTokenStatus))
user_id = Column(UUIDType, ForeignKey("__reana.user_.id_"), nullable=False)
type_ = Column(Enum(UserTokenType), nullable=False)
class WorkflowStatus(enum.Enum):
"""Enumeration of possible workflow statuses."""
created = 0
running = 1
finished = 2
failed = 3
deleted = 4
stopped = 5
queued = 6
ALLOWED_WORKFLOW_STATUS_TRANSITIONS = [
# Creation
(WorkflowStatus.created, WorkflowStatus.deleted),
(WorkflowStatus.created, WorkflowStatus.running),
# Running
(WorkflowStatus.running, WorkflowStatus.failed),
(WorkflowStatus.running, WorkflowStatus.finished),
(WorkflowStatus.running, WorkflowStatus.stopped),
(WorkflowStatus.running, WorkflowStatus.running),
# Stopped
(WorkflowStatus.stopped, WorkflowStatus.deleted),
# Failed
(WorkflowStatus.failed, WorkflowStatus.deleted),
(WorkflowStatus.failed, WorkflowStatus.running),
# Finished
(WorkflowStatus.finished, WorkflowStatus.deleted),
(WorkflowStatus.finished, WorkflowStatus.running),
]
class JobStatus(enum.Enum):
"""Enumeration of possible job statuses."""
created = 0
running = 1
finished = 2
failed = 3
stopped = 4
queued = 5
class Workflow(Base, Timestamp):
"""Workflow table."""
__tablename__ = "workflow"
id_ = Column(UUIDType, primary_key=True)
name = Column(String(255))
status = Column(Enum(WorkflowStatus), default=WorkflowStatus.created)
owner_id = Column(UUIDType, ForeignKey("__reana.user_.id_"))
reana_specification = Column(JSONType)
input_parameters = Column(JSONType)
operational_options = Column(JSONType)
type_ = Column(String(30))
interactive_session = Column(Text)
interactive_session_name = Column(Text)
interactive_session_type = Column(Text)
logs = Column(String)
run_started_at = Column(DateTime)
run_finished_at = Column(DateTime)
run_stopped_at = Column(DateTime)
_run_number = Column("run_number", Float)
job_progress = Column(JSONType, default=dict)
workspace_path = Column(String)
restart = Column(Boolean, default=False)
# job_progress = {
# jobs_total = {total: job_number}
# jobs_running = {job_ids: [], total: c}
# jobs_finished = {job_ids: [], total: c}
# jobs_failed = {job_ids: [], total: c}}
engine_specific = Column(JSONType)
git_ref = Column(String(40))
git_repo = Column(String(255))
git_provider = Column(String(255))
__table_args__ = (
UniqueConstraint(
"name", "owner_id", "run_number", name="_user_workflow_run_uc"
),
{"schema": "__reana"},
)
def __init__(
self,
id_,
name,
owner_id,
reana_specification,
type_,
logs="",
interactive_session=None,
interactive_session_name=None,
interactive_session_type=None,
input_parameters={},
operational_options={},
status=WorkflowStatus.created,
git_ref="",
git_repo=None,
git_provider=None,
workspace_path=None,
restart=False,
run_number=None,
):
"""Initialize workflow model."""
self.id_ = id_
self.name = name
self.status = status
self.owner_id = owner_id
self.reana_specification = reana_specification
self.input_parameters = input_parameters
self.operational_options = operational_options
self.type_ = type_
self.logs = logs or ""
self.interactive_session = interactive_session
self.interactive_session_name = interactive_session_name
self.interactive_session_type = interactive_session_type
self.git_ref = git_ref
self.git_repo = git_repo
self.git_provider = git_provider
self.restart = restart
self._run_number = self.assign_run_number(run_number)
self.workspace_path = workspace_path or build_workspace_path(
self.owner_id, self.id_
)
def __repr__(self):
"""Workflow string represetantion."""
return "<Workflow %r>" % self.id_
@hybrid_property
def run_number(self):
"""Property of run_number."""
if self._run_number.is_integer():
return int(self._run_number)
return self._run_number
@run_number.expression
def run_number(cls):
return func.abs(cls._run_number)
def assign_run_number(self, run_number):
"""Assing run number."""
from .database import Session
if run_number:
last_workflow = (
Session.query(Workflow)
.filter(
Workflow.name == self.name,
Workflow.run_number >= int(run_number),
Workflow.run_number < int(run_number) + 1,
Workflow.owner_id == self.owner_id,
)
.order_by(Workflow.run_number.desc())
.first()
)
else:
last_workflow = (
Session.query(Workflow)
.filter_by(name=self.name, restart=False, owner_id=self.owner_id)
.order_by(Workflow.run_number.desc())
.first()
)
if last_workflow and self.restart:
return round(last_workflow.run_number + 0.1, 1)
else:
if not last_workflow:
return 1
else:
return last_workflow.run_number + 1
def get_input_parameters(self):
"""Return workflow parameters."""
return self.reana_specification.get("inputs", {}).get("parameters", {})
def get_specification(self):
"""Return workflow specification."""
return self.reana_specification["workflow"].get("specification", {})
def get_owner_access_token(self):
"""Return workflow owner access token."""
from .database import Session
db_session = Session.object_session(self)
owner = db_session.query(User).filter_by(id_=self.owner_id).first()
return owner.access_token
def get_full_workflow_name(self):
"""Return full workflow name including run number."""
return "{}.{}".format(self.name, str(self.run_number))
def get_workspace_disk_usage(self, summarize=False, block_size=None):
"""Retrieve disk usage information of a workspace."""
return get_disk_usage(self.workspace_path, summarize, block_size)
@staticmethod
def update_workflow_status(
db_session, workflow_uuid, status, new_logs="", message=None
):
"""Update database workflow status.
:param workflow_uuid: UUID which represents the workflow.
:param status: String that represents the workflow status.
:param new_logs: New logs from workflow execution.
:param message: Unused.
"""
try:
workflow = db_session.query(Workflow).filter_by(id_=workflow_uuid).first()
if not workflow:
raise Exception(
"Workflow {0} doesn't exist in database.".format(workflow_uuid)
)
if status:
workflow.status = status
if new_logs:
workflow.logs = (workflow.logs or "") + new_logs + "\n"
db_session.commit()
except Exception as e:
raise e
def can_transition_to(self, next_status):
"""Whether the provided workflow can transition to the next status."""
current_transition = (self.status, next_status)
return current_transition in ALLOWED_WORKFLOW_STATUS_TRANSITIONS
@event.listens_for(Workflow.status, "set")
def workflow_status_change_listener(workflow, new_status, old_status, initiator):
"""Workflow status change listener."""
if new_status in [
WorkflowStatus.finished,
WorkflowStatus.failed,
]:
workflow.run_finished_at = datetime.now()
elif new_status in [WorkflowStatus.stopped]:
workflow.run_stopped_at = datetime.now()
elif new_status in [WorkflowStatus.running]:
workflow.run_started_at = datetime.now()
finished_at = workflow.run_finished_at or workflow.run_stopped_at
if workflow.run_started_at and finished_at:
cpu_time = finished_at - workflow.run_started_at
cpu_milliseconds = int(cpu_time.total_seconds() * 1000)
cpu_resource = Resource.query.filter_by(
name=DEFAULT_QUOTA_RESOURCES["cpu"]
).one_or_none()
if cpu_resource:
from .database import Session
workflow_resource = WorkflowResource(
workflow_id=workflow.id_,
resource_id=cpu_resource.id_,
quantity_used=cpu_milliseconds,
)
user_resource_quota = UserResource.query.filter_by(
user_id=workflow.owner_id, resource_id=cpu_resource.id_
).first()
user_resource_quota.quota_used += cpu_milliseconds
Session.add(workflow_resource)
Session.commit()
return new_status
class Job(Base, Timestamp):
"""Job table."""
__tablename__ = "job"
__table_args__ = {"schema": "__reana"}
id_ = Column(UUIDType, primary_key=True, default=generate_uuid)
backend_job_id = Column(String(256))
workflow_uuid = Column(UUIDType)
status = Column(Enum(JobStatus), default=JobStatus.created)
compute_backend = Column(String(30))
cvmfs_mounts = Column(Text)
shared_file_system = Column(Boolean)
docker_img = Column(String(256))
cmd = Column(JSONType)
env_vars = Column(JSONType)
deleted = Column(Boolean)
logs = Column(String, nullable=True)
prettified_cmd = Column(JSONType)
job_name = Column(Text)
class JobCache(Base, Timestamp):
"""Job Cache table."""
__tablename__ = "job_cache"
__table_args__ = {"schema": "__reana"}
id_ = Column(UUIDType, primary_key=True, default=generate_uuid)
job_id = Column(UUIDType, ForeignKey("__reana.job.id_"), primary_key=True)
parameters = Column(String(1024))
result_path = Column(String(1024))
workspace_hash = Column(String(1024))
access_times = Column(JSONType)
class AuditLogAction(enum.Enum):
"""Enumeration of audit log actions."""
request_token = 0
grant_token = 1
revoke_token = 2
class AuditLog(Base, Timestamp):
"""Audit log table."""
__tablename__ = "audit_log"
__table_args__ = {"schema": "__reana"}
id_ = Column(UUIDType, primary_key=True, default=generate_uuid)
user_id = Column(UUIDType, ForeignKey("__reana.user_.id_"), nullable=False)
action = Column(Enum(AuditLogAction), nullable=False)
details = Column(JSONType)
def __repr__(self):
"""Audit log string representation."""
return "<AuditLog {} {}>".format(self.id_, self.action)
class ResourceType(enum.Enum):
"""Enumeration of resource types."""
cpu = 0
disk = 1
class ResourceUnit(enum.Enum):
"""Enumeration of resource usage units."""
bytes_ = 0
milliseconds = 1
class Resource(Base, Timestamp):
"""Resource table."""
__tablename__ = "resource"
__table_args__ = {"schema": "__reana"}
id_ = Column(UUIDType, primary_key=True, default=generate_uuid)
name = Column(String(1024), unique=True, nullable=False)
type_ = Column(Enum(ResourceType), nullable=False)
unit = Column(Enum(ResourceUnit), nullable=False)
title = Column(String(1024))
def __repr__(self):
"""Resource string representation."""
return "<Resource {}>".format(self.id_)
@staticmethod
def initialise_default_resources():
"""Initialise default Resources."""
from reana_db.database import Session
existing_resources = [r.name for r in Resource.query.all()]
default_resources = []
resource_type_to_unit = {
ResourceType.cpu: ResourceUnit.milliseconds,
ResourceType.disk: ResourceUnit.bytes_,
}
for type_, name in DEFAULT_QUOTA_RESOURCES.items():
if name not in existing_resources:
default_resources.append(
Resource(
name=name,
type_=ResourceType[type_],
unit=resource_type_to_unit[ResourceType[type_]],
title=f"Default {type_} resource.",
)
)
if default_resources:
Session.add_all(default_resources)
Session.commit()
return default_resources
class UserResource(Base, Timestamp):
"""User Resource table."""
__tablename__ = "user_resource"
__table_args__ = {"schema": "__reana"}
user_id = Column(UUIDType, ForeignKey("__reana.user_.id_"), primary_key=True)
resource_id = Column(UUIDType, ForeignKey("__reana.resource.id_"), primary_key=True)
quota_limit = Column(BigInteger())
quota_used = Column(BigInteger())
user = relationship("User", backref="user_resource")
resource = relationship("Resource", backref="user_resource")
def __repr__(self):
"""User Resource string representation."""
return "<UserResource {} {}>".format(self.user_id, self.resource_id)
class WorkflowResource(Base, Timestamp):
"""Workflow Resource table."""
__tablename__ = "workflow_resource"
__table_args__ = {"schema": "__reana"}
workflow_id = Column(UUIDType, ForeignKey("__reana.workflow.id_"), primary_key=True)
resource_id = Column(UUIDType, ForeignKey("__reana.resource.id_"), primary_key=True)
quantity_used = Column(BigInteger())
def __repr__(self):
"""Workflow Resource string representation."""
return "<WorkflowResource {} {}>".format(self.workflow_id, self.resource_id)
class QuotaHealth(enum.Enum):
"""Enumeration of quota health statuses."""
healthy = 0
warning = 1
critical = 2