Skip to content

Commit

Permalink
Add services/auto-suite.py
Browse files Browse the repository at this point in the history
Signed-off-by: Vallari Agrawal <[email protected]>
  • Loading branch information
VallariAg committed Sep 29, 2023
1 parent a4433d1 commit 17bf22e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 55 deletions.
36 changes: 2 additions & 34 deletions src/models/auto_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


class AutoSuite(Base):
__tablename__ = 'auto_suite'
id = Column(Integer, primary_key=True, index=True)
__tablename__ = "auto_suite"
id = Column(Integer, primary_key=True, index=True)
username = Column(String)
status = Column(String)
created_at = Column(DateTime(timezone=True))
Expand All @@ -17,35 +17,3 @@ class AutoSuite(Base):
suite = Column(String)
log_path = Column(String)
cmd = Column(String)


class AutoSuiteDatabaseException(Exception):
def __init__(self, message, code):
super().__init__(message)
self.code = code


def create_autosuite(db: Session, auto_suite):
new_autosuite = AutoSuite(**auto_suite)
db.add(new_autosuite)
db.commit()
db.refresh(new_autosuite)
return new_autosuite


def get_autosuites_by_username(db: Session, username: str):
db_autosuite = db.query(AutoSuite).filter(AutoSuite.username == username).all()
return db_autosuite


def update_autosuite(db: Session, record_id: int, update_data):
autosuite_query = db.query(AutoSuite).filter(AutoSuite.id == record_id)
db_autosuite = autosuite_query.first()
if not db_autosuite:
raise AutoSuiteDatabaseException("AutoSuite object does not exist - unable to update.", 404)
autosuite_query.filter(AutoSuite.id == record_id).update(
update_data, synchronize_session=False
)
db.commit()
db.refresh(db_autosuite)
return db_autosuite
26 changes: 16 additions & 10 deletions src/routes/auto_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from sqlalchemy.orm import Session

from models import get_db
from schemas.auto_suite import AutoSuiteSchema, BuildStatusWebhookArgs
from models import auto_suite as auto_suite_model
from services.auto_suite import AutoSuiteService, auto_schedule
from schemas.auto_suite import AutoSuiteSchema, BuildStatusWebhook

log = logging.getLogger(__name__)

Expand All @@ -16,18 +16,24 @@

@router.post("/", status_code=200)
def create_auto_suite(payload: AutoSuiteSchema, db: Session = Depends(get_db)):
return auto_suite_model.create_autosuite(db, payload.model_dump())
return AutoSuiteService(db).create(payload.model_dump())


@router.get("/", status_code=200)
def get_auto_suite(username: str, db: Session = Depends(get_db)):
db_presets = auto_suite_model.get_autosuites_by_username(db, username)
if not db_presets:
raise HTTPException(status_code=404, detail=f"User has no auto_suite scheduled.")
return db_presets
db_record = AutoSuiteService(db).get_by_username(username)
if not db_record:
raise HTTPException(
status_code=404, detail=f"User has no auto_suite scheduled."
)
return db_record


@router.post("/webhook/build-status", status_code=201)
def build_status_webhook(payload: BuildStatusWebhookArgs):
# TODO: need to implement this
return payload
def build_status_webhook(
ready_builds: BuildStatusWebhook, db: Session = Depends(get_db)
):
db_records = AutoSuiteService(db).get_by_build_data(ready_builds)
if db_records:
auto_schedule(db_records)
return db_records
8 changes: 7 additions & 1 deletion src/routes/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ def create_run(
logs: bool = False,
):
args = args.model_dump(by_alias=True)
return run(args, dry_run, logs, access_token)
if not access_token:
raise HTTPException(
status_code=401,
detail="You need to be logged in",
headers={"WWW-Authenticate": "Bearer"},
)
return run(args, dry_run, logs)
5 changes: 2 additions & 3 deletions src/schemas/auto_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
from .suite import SuiteArgs


class BuildStatusWebhookArgs(BaseModel):
class BuildStatusWebhook(BaseModel):
# pylint: disable=too-few-public-methods
"""
Class for Build Status Webhook Args.
Class for Build Status Webhook.
"""
status: str = Field(default="")
distro: Union[str, None] = Field(default="")
distro_version: Union[str, None] = Field(default="")
distro_arch: Union[str, None] = Field(default="")
ref: Union[str, None] = Field(default="")
sha1: Union[str, None] = Field(default="")
flavor: Union[str, None] = Field(default="")
Expand Down
64 changes: 64 additions & 0 deletions src/services/auto_suite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from sqlalchemy.orm import Session
from models.auto_suite import AutoSuite
from schemas.auto_suite import BuildStatusWebhook
from services.suite import run


def auto_schedule(autosuite_records):
for auto_suite in autosuite_records:
run(auto_suite.cmd, dry_run=False, send_logs=True)


class AutoSuiteDatabaseException(Exception):
def __init__(self, message, code):
super().__init__(message)
self.code = code


class AutoSuiteService:
def __init__(self, db: Session) -> None:
self.db = db

def get_by_username(self, username: str):
db_autosuite = (
self.db.query(AutoSuite).filter(AutoSuite.username == username).all()
)
return db_autosuite

def get_by_build_data(self, build_data: BuildStatusWebhook):
distro = build_data.distro
distro_version = build_data.distro_version
flavor = build_data.flavor
branch = build_data.ref
db_preset = (
self.db.query(AutoSuite)
.filter(
AutoSuite.distro == distro,
AutoSuite.distro_version == distro_version,
AutoSuite.flavor == flavor,
AutoSuite.branch == branch,
)
.first()
)
return db_preset

def create(self, new_obj: dict):
new_autosuite = AutoSuite(**new_obj)
self.db.add(new_autosuite)
self.db.commit()
self.db.refresh(new_autosuite)
return new_autosuite

def update(self, id: int, update_data: dict):
autosuite_query = self.db.query(AutoSuite).filter(AutoSuite.id == id)
db_autosuite = autosuite_query.first()
if not db_autosuite:
raise AutoSuiteDatabaseException(
"AutoSuite object does not exist - unable to update.", 404
)
autosuite_query.filter(AutoSuite.id == id).update(
update_data, synchronize_session=False
)
self.db.commit()
self.db.refresh(db_autosuite)
return db_autosuite
8 changes: 1 addition & 7 deletions src/services/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@
log = logging.getLogger(__name__)


def run(args, dry_run: bool, send_logs: bool, access_token: str):
def run(args, dry_run: bool, send_logs: bool):
"""
Schedule a suite.
:returns: Run details (dict) and logs (list).
"""
if not access_token:
raise HTTPException(
status_code=401,
detail="You need to be logged in",
headers={"WWW-Authenticate": "Bearer"},
)
try:
args["--timestamp"] = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
if dry_run:
Expand Down

0 comments on commit 17bf22e

Please sign in to comment.