Compare commits

...
22 Commits
Author SHA1 Message Date
brb 9513eb488b adapted to new definition 2023-07-06 15:58:25 +02:00
brb a51c49e8f4 adapted to new definition 2023-07-06 15:58:15 +02:00
brb dba476e78a updated definition 2023-07-06 15:58:03 +02:00
brb 87a738461c defined runner script 2023-07-06 15:54:20 +02:00
brb 40c9b7442d added printing of content from runner script 2023-07-06 15:53:34 +02:00
brb c1aa6f3c41 added function to read runner script 2023-07-06 15:53:15 +02:00
brb 9102d03ef4 created runner script 2023-07-06 15:53:00 +02:00
brb 5c21aa767f installed packages 2023-07-06 13:27:32 +02:00
brb 3187248fb9 [skip ci] fixed bug when accessing /test 2023-07-06 13:22:32 +02:00
brb ed05a8eca5 update root page 2023-07-06 13:20:24 +02:00
brb 02ec54fd16 updated response text 2023-07-06 13:15:16 +02:00
brb 9fffa861e7 added checking for presence of runner script 2023-07-06 13:10:13 +02:00
brb 3a73c9cf6e added more detailed responses to requests 2023-07-06 12:58:09 +02:00
brb 387e739a07 [skip ci] testing if ci is skipped correctly 2023-07-06 12:54:12 +02:00
brb 39d1ac1474 moved definition of class to separate file 2023-07-06 12:50:30 +02:00
brb 2aabc9ab37 added more logging 2023-07-06 12:20:02 +02:00
brb 2f964dabcc added more logging 2023-07-06 12:19:49 +02:00
brb 47576392a9 updated logging level 2023-07-06 12:19:33 +02:00
brb 31e771eb9f fixed bug when defining env vars 2023-07-06 11:59:21 +02:00
brb 935adf98ad removed unused file 2023-07-06 11:58:36 +02:00
brb 39b2afaf1f added creation of repo dir and moved env vars into docker compose 2023-07-06 11:58:21 +02:00
brb 416e9de7ce added port mapping 2023-07-06 11:54:01 +02:00
10 changed files with 119 additions and 29 deletions
+1
View File
@@ -28,6 +28,7 @@ FROM dvcorg/cml:0-dvc2-base1-gpu
ENV APP_HOME = /home/app
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
RUN mkdir -p /home/app/repo
# install packages from builder
COPY --from=builder /usr/src/app/wheels /wheels
+46 -22
View File
@@ -1,50 +1,74 @@
import uvicorn
from fastapi import FastAPI, Request
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import HTMLResponse
from dotenv import load_dotenv
import logging
from pydantic import BaseModel
from typing import List
import os
from utils import clone_repository
from utils import clone_repository, load_runner_script
from setup_logging import setup_logging
class GiteaRequest(BaseModel):
ref: str
before: str
after: str
compare_url: str
commits: List[dict]
total_commits: int
head_commit: dict
repository: dict
pusher: dict
sender: dict
from gitea_request import GiteaRequest
app = FastAPI()
@app.get('/')
@app.get('/', response_class=HTMLResponse)
async def root():
return '<h1>Welcome to Gitea Runsner</h1>'
content_str = """
<html>
<head>
<title>Gitea Runner</title>
</head>
<body>
<h1>Welcome to Gitea Runner</h1>
<p>see /docs for more information</p>
</body>
</html>
"""
return HTMLResponse(content=content_str, status_code=200)
@app.post('/test')
async def test(request: Request):
""" Test connection to server. """
logging.debug('Content-Type: ', request.headers.get('content-type'))
return {'status': 'success'}
raise HTTPException(
status_code=200,
detail='test completed'
)
@app.post('/event')
def handle_event(request: GiteaRequest):
""" Handle event from webhook. """
# stop early if allowed
# stop early if asked
commit_msg = request.head_commit['message']
if commit_msg.lower().startswith('[skip ci]'):
return {'status': 'received task'}
raise HTTPException(
status_code=200,
detail='skipping ci'
)
# clone repository
repo_url = request.repository['clone_url']
logging.info('cloning repository: {repo_url}')
local_repo_dir = clone_repository(repo_url)
print('local_repo_dir: ', local_repo_dir)
return {'status': 'success'}
logging.info(f'local_repo_dir: {local_repo_dir}')
# locate runner script
runner_script_path = local_repo_dir / 'runner_script.yml'
if not runner_script_path.exists():
raise HTTPException(
status_code=200,
detail="no runner_script.yml in repository root"
)
# load runner commands
runner_script = load_runner_script(path=runner_script_path)
for pipeline_name, pipeline_step in runner_script.pipelines.items():
print(pipeline_name)
print(pipeline_step)
# send response
raise HTTPException(
status_code=202,
detail='received task'
)
if __name__ == '__main__':
load_dotenv('dev.env')
+14
View File
@@ -0,0 +1,14 @@
from pydantic import BaseModel
from typing import List
class GiteaRequest(BaseModel):
ref: str
before: str
after: str
compare_url: str
commits: List[dict]
total_commits: int
head_commit: dict
repository: dict
pusher: dict
sender: dict
+22
View File
@@ -0,0 +1,22 @@
from pydantic import BaseModel
from typing import List, Dict
import yaml
from pathlib import Path
class Pipeline(BaseModel):
name: str
requirements: str
script: List[str]
class RunnerScript(BaseModel):
pipelines: Dict[str, Pipeline]
if __name__ == '__main__':
path = Path(__file__).parent.parent / 'runner_script.yml'
with open(path, 'r') as fh:
script = yaml.safe_load(fh)
runner_script = RunnerScript.parse_obj(script)
print(runner_script)
for k, v in runner_script.pipelines.items():
print(k)
print(v)
+13
View File
@@ -1,8 +1,12 @@
from pydantic import AnyHttpUrl
from typing import List
from pathlib import Path
from git import Repo
import logging
import shutil
import os
import yaml
from runner_script import RunnerScript
def clone_repository(repo_url: AnyHttpUrl) -> Path:
# extract repo name
@@ -16,4 +20,13 @@ def clone_repository(repo_url: AnyHttpUrl) -> Path:
dst_dir.mkdir()
# git clone repo
Repo.clone_from(url=repo_url, to_path=dst_dir)
logging.debug('finished')
return dst_dir
def load_runner_script(path: Path) -> RunnerScript:
assert path.exists()
with open(path, 'r') as fh:
script = yaml.safe_load(fh)
runner_script = RunnerScript.parse_obj(script)
return runner_script
+7 -1
View File
@@ -4,4 +4,10 @@ services:
build: .
container_name: gitea-runner
restart: unless-stopped
env_file: prod.env
ports:
- "8000:8000"
environment:
- LOGGER_LEVEL=debug
- LISTEN_IP=0.0.0.0
- LISTEN_PORT=8000
- REPO_DIR=/home/app/repo/
-4
View File
@@ -1,4 +0,0 @@
LOGGER_LEVEL = info
LISTEN_IP = 0.0.0.0
LISTEN_PORT = 8000
REPO_DIR = /usr/src/app/repo/
+1
View File
@@ -37,6 +37,7 @@ pydantic==1.10.11
Pygments==2.15.1
python-dateutil==2.8.2
python-dotenv==1.0.0
PyYAML==6.0
pyzmq==25.1.0
six==1.16.0
smmap==5.0.0
+1
View File
@@ -2,3 +2,4 @@ uvicorn==0.16.0
fastapi==0.99.1
GitPython==3.1.31
python-dotenv==1.0.0
PyYAML==6.0
+12
View File
@@ -0,0 +1,12 @@
definitions:
steps:
step: &step-test
name: test
requirements: req-prod.txt
script:
- command arg1 arg2
- command2 arg1 arg2
pipelines:
default:
<<: *step-test