Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

teuth-suite: Add dry-run for scheduling suites #3

Merged
merged 5 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ Returns `{"root": "success"}`.
### Route `/suite`

POST `/suite/`: schedules a run.

Two query parameters:
- `dry_run` (boolean) - Do a dry run; do not schedule anything.
- `logs` (boolean) - Send scheduling logs in response.

Example:
```
curl --location --request POST 'http://localhost:8082/suite/' \
curl --location --request POST 'http://localhost:8082/suite?dry_run=false&logs=true' \
--header 'Content-Type: application/json' \
--data-raw '{
"--ceph": "wip-dis-testing-2",
Expand All @@ -59,5 +64,3 @@ curl --location --request POST 'http://localhost:8082/suite/' \
}'
```



6 changes: 3 additions & 3 deletions src/routes/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
)

@router.post("/", status_code=200)
def create_run(args: SuiteArgs):
def create_run(args: SuiteArgs, dry_run: bool = False, logs: bool = False):
try:
args = args.dict(by_alias=True)
results = run(args)
return {"run": results}
results = run(args, dry_run, logs)
return results
except Exception as exc:
raise HTTPException(status_code=404, detail=repr(exc))
31 changes: 31 additions & 0 deletions src/services/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import uuid, os
from multiprocessing import Process
import teuthology

def logs_run(func, args):
"""
Run the command function in a seperate process (to isolate logs),
and return logs printed during the execution of the function.
"""
id = str(uuid.uuid4())
log_file = f'/archive_dir/{id}.log'

teuthology_process = Process(
target=_execute_with_logs, args=(func, args, log_file,))
teuthology_process.start()
teuthology_process.join()

logs = ""
with open(log_file) as f:
logs = f.readlines()
if os.path.isfile(log_file):
os.remove(log_file)
return logs

def _execute_with_logs(func, args, log_file):
"""
To store logs, set a new FileHandler for teuthology root logger
and then execute the command function.
"""
teuthology.setup_log_file(log_file)
func(args)
23 changes: 20 additions & 3 deletions src/services/suite.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import teuthology.suite
from services.helpers import logs_run
import logging, requests # Note: import requests after teuthology
from datetime import datetime

Expand All @@ -8,10 +9,26 @@

log = logging.getLogger(__name__)

def run(args):

def run(args, dry_run: bool, send_logs: bool):
"""
Schedule a suite.
:returns: Run details (dict) and logs (list).
"""
try:
args["--timestamp"] = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
teuthology.suite.main(args)
if dry_run:
args['--dry-run'] = True
logs = logs_run(teuthology.suite.main, args)
return { "run": {}, "logs": logs }

logs = []
if send_logs:
logs = logs_run(teuthology.suite.main, args)
else:
teuthology.suite.main(args)

# get run details from paddles
run_name = make_run_name({
"machine_type": args["--machine-type"],
"user": args["--user"],
Expand All @@ -22,7 +39,7 @@ def run(args):
"flavor": args["--flavor"]
})
run_details = get_run_details(run_name)
return { "run": run_details }
return { "run": run_details, "logs": logs }
except Exception as exc:
log.error("teuthology.suite.main failed with the error: " + repr(exc))
raise
Expand Down