Compare commits
38
Commits
7eb8e5d2fd
...
test
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9513eb488b | ||
|
|
a51c49e8f4 | ||
|
|
dba476e78a | ||
|
|
87a738461c | ||
|
|
40c9b7442d | ||
|
|
c1aa6f3c41 | ||
|
|
9102d03ef4 | ||
|
|
5c21aa767f | ||
|
|
3187248fb9 | ||
|
|
ed05a8eca5 | ||
|
|
02ec54fd16 | ||
|
|
9fffa861e7 | ||
|
|
3a73c9cf6e | ||
|
|
387e739a07 | ||
|
|
39d1ac1474 | ||
|
|
2aabc9ab37 | ||
|
|
2f964dabcc | ||
|
|
47576392a9 | ||
|
|
31e771eb9f | ||
|
|
935adf98ad | ||
|
|
39b2afaf1f | ||
|
|
416e9de7ce | ||
|
|
70a087ee7b | ||
|
|
60112f1484 | ||
|
|
f0010d739b | ||
|
|
155a93456c | ||
|
|
fdd968e3a3 | ||
|
|
8c9880ecca | ||
|
|
56d0429bab | ||
|
|
78a2201a81 | ||
|
|
29a3545da4 | ||
|
|
08eaaa3b5e | ||
|
|
65d2e850b5 | ||
|
|
b2bb34250b | ||
|
|
9c357f52a1 | ||
|
|
4454ac1b46 | ||
|
|
344f29b77c | ||
|
|
fe64f2d8a3 |
@@ -0,0 +1 @@
|
|||||||
|
__pycache__
|
||||||
+4
-9
@@ -3,13 +3,7 @@
|
|||||||
###########
|
###########
|
||||||
|
|
||||||
# pull official base image
|
# pull official base image
|
||||||
FROM python:alpine as builder
|
FROM dvcorg/cml:0-dvc2-base1-gpu as builder
|
||||||
|
|
||||||
# update system
|
|
||||||
RUN apk update && \
|
|
||||||
apk add git && \
|
|
||||||
apk add rsync && \
|
|
||||||
apk add docker
|
|
||||||
|
|
||||||
# set work directory
|
# set work directory
|
||||||
WORKDIR /usr/src/app
|
WORKDIR /usr/src/app
|
||||||
@@ -20,7 +14,7 @@ ENV PYTHONUNBUFFERED 1
|
|||||||
|
|
||||||
# install dependencies
|
# install dependencies
|
||||||
RUN pip install --upgrade pip
|
RUN pip install --upgrade pip
|
||||||
COPY requirements.txt .
|
COPY req-prod.txt requirements.txt
|
||||||
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
|
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
|
||||||
|
|
||||||
#########
|
#########
|
||||||
@@ -28,12 +22,13 @@ RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requir
|
|||||||
#########
|
#########
|
||||||
|
|
||||||
# pull official base image
|
# pull official base image
|
||||||
FROM python:alpine
|
FROM dvcorg/cml:0-dvc2-base1-gpu
|
||||||
|
|
||||||
# create app home
|
# create app home
|
||||||
ENV APP_HOME = /home/app
|
ENV APP_HOME = /home/app
|
||||||
RUN mkdir -p $APP_HOME
|
RUN mkdir -p $APP_HOME
|
||||||
WORKDIR $APP_HOME
|
WORKDIR $APP_HOME
|
||||||
|
RUN mkdir -p /home/app/repo
|
||||||
|
|
||||||
# install packages from builder
|
# install packages from builder
|
||||||
COPY --from=builder /usr/src/app/wheels /wheels
|
COPY --from=builder /usr/src/app/wheels /wheels
|
||||||
|
|||||||
@@ -1,53 +0,0 @@
|
|||||||
# from waitress import serve
|
|
||||||
# from flask import Flask, request, jsonify
|
|
||||||
import uvicorn
|
|
||||||
from fastapi import FastAPI, Request
|
|
||||||
from dotenv import load_dotenv
|
|
||||||
import logging
|
|
||||||
from pydantic import BaseModel
|
|
||||||
from typing import List
|
|
||||||
|
|
||||||
from utils import clone_repository
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
|
|
||||||
@app.get('/')
|
|
||||||
async def root():
|
|
||||||
return '<h1>Welcome to Gitea Runner</h1>'
|
|
||||||
|
|
||||||
@app.post('/test')
|
|
||||||
async def test(request: Request):
|
|
||||||
""" Test connection to server. """
|
|
||||||
logging.debug('Content-Type: ', request.headers.get('content-type'))
|
|
||||||
return {'status': 'success'}
|
|
||||||
|
|
||||||
@app.post('/event')
|
|
||||||
def handle_event(request: GiteaRequest):
|
|
||||||
""" Handle event from webhook. """
|
|
||||||
# stop early if allowed
|
|
||||||
commit_msg = request.head_commit['message']
|
|
||||||
if commit_msg.lower().startswith('[skip ci]'):
|
|
||||||
return {'status': 'received task'}
|
|
||||||
# clone repository
|
|
||||||
repo_url = request.repository['clone_url']
|
|
||||||
local_repo_dir = clone_repository(repo_url)
|
|
||||||
print('local_repo_dir: ', local_repo_dir)
|
|
||||||
return {'status': 'success'}
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
uvicorn.run(
|
|
||||||
app=app,
|
|
||||||
host='0.0.0.0'
|
|
||||||
)
|
|
||||||
+75
-30
@@ -1,38 +1,83 @@
|
|||||||
from flask import Flask, request, jsonify
|
import uvicorn
|
||||||
import os
|
from fastapi import FastAPI, Request, HTTPException
|
||||||
|
from fastapi.responses import HTMLResponse
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from ipaddress import ip_address, ip_network
|
|
||||||
import logging
|
import logging
|
||||||
|
from typing import List
|
||||||
|
import os
|
||||||
|
|
||||||
load_dotenv()
|
from utils import clone_repository, load_runner_script
|
||||||
app = Flask(__name__)
|
from setup_logging import setup_logging
|
||||||
|
from gitea_request import GiteaRequest
|
||||||
|
|
||||||
@app.before_request
|
app = FastAPI()
|
||||||
def check_authorized():
|
|
||||||
|
@app.get('/', response_class=HTMLResponse)
|
||||||
|
async def root():
|
||||||
|
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>
|
||||||
"""
|
"""
|
||||||
Ensure request from allowed ip.
|
return HTMLResponse(content=content_str, status_code=200)
|
||||||
"""
|
|
||||||
# prepare variables
|
|
||||||
assert 'ALLOWED_IP_RANGE' in os.environ
|
|
||||||
allowed_ip_range = ip_network(os.getenv['ALLOWED_IP_RANGE'])
|
|
||||||
requesting_ip = ip_address(request.remote_addr)
|
|
||||||
# check that requesting ip is allowed
|
|
||||||
if requesting_ip not in allowed_ip_range:
|
|
||||||
logging.info(f'received request from unauthorised ip: {request.remote_addr}')
|
|
||||||
return jsonify(status='forbidden'), 403
|
|
||||||
logging.info(f'received request from {request.remote_addr}')
|
|
||||||
|
|
||||||
@app.before_request
|
@app.post('/test')
|
||||||
def check_media_type():
|
async def test(request: Request):
|
||||||
"""
|
""" Test connection to server. """
|
||||||
Ensure correct Content-Type of request
|
logging.debug('Content-Type: ', request.headers.get('content-type'))
|
||||||
"""
|
raise HTTPException(
|
||||||
if not request.headers.get('Content-Type').lower().startswith('application/json'):
|
status_code=200,
|
||||||
logging.error(f'received request with wrong content type')
|
detail='test completed'
|
||||||
return jsonify(status='unsupported media type'), 415
|
)
|
||||||
|
|
||||||
@app.route('/test', methods=['POST'])
|
@app.post('/event')
|
||||||
def test():
|
def handle_event(request: GiteaRequest):
|
||||||
logging.debug(request.get_json(force=True))
|
""" Handle event from webhook. """
|
||||||
return jsonify(status='success', sender=request.remote_addr)
|
# stop early if asked
|
||||||
|
commit_msg = request.head_commit['message']
|
||||||
|
if commit_msg.lower().startswith('[skip ci]'):
|
||||||
|
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)
|
||||||
|
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')
|
||||||
|
setup_logging()
|
||||||
|
listen_ip = os.getenv('LISTEN_IP', default='0.0.0.0')
|
||||||
|
listen_port = int(os.getenv('LISTEN_PORT', default=8000))
|
||||||
|
logging.info(f'starting FastAPI server ({listen_ip}:{listen_port})')
|
||||||
|
uvicorn.run(
|
||||||
|
app=app,
|
||||||
|
host=listen_ip,
|
||||||
|
port=listen_port
|
||||||
|
)
|
||||||
@@ -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
|
||||||
+11
-13
@@ -1,20 +1,18 @@
|
|||||||
"""
|
|
||||||
Run tasks based on webhooks configured in Gitea.
|
|
||||||
|
|
||||||
Command-line optinos:
|
|
||||||
--debug, -d Send more detailed log output to console.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from waitress import serve
|
import uvicorn
|
||||||
|
|
||||||
from setup_logging import setup_logging
|
from setup_logging import setup_logging
|
||||||
from app import app
|
from app import app
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# setup
|
# setup
|
||||||
load_dotenv()
|
|
||||||
setup_logging()
|
setup_logging()
|
||||||
# serve app
|
listen_ip = os.getenv('LISTEN_IP', default='0.0.0.0')
|
||||||
serve(app, host='0.0.0.0', port=1706)
|
listen_port = int(os.getenv('LISTEN_PORT', default=8000))
|
||||||
|
logging.info(f'starting FastAPI server ({listen_ip}:{listen_port})')
|
||||||
|
uvicorn.run(
|
||||||
|
app=app,
|
||||||
|
host=listen_ip,
|
||||||
|
port=listen_port
|
||||||
|
)
|
||||||
@@ -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)
|
||||||
@@ -5,10 +5,7 @@ import os
|
|||||||
def setup_logging():
|
def setup_logging():
|
||||||
fmt = '%(asctime)s | %(levelname)s | %(filename)s | %(funcName)s | %(message)s'
|
fmt = '%(asctime)s | %(levelname)s | %(filename)s | %(funcName)s | %(message)s'
|
||||||
datefmt = '%Y-%m-%d %H:%M:%S'
|
datefmt = '%Y-%m-%d %H:%M:%S'
|
||||||
if not 'LOGGER_LEVEL' in os.environ:
|
logger_level = os.getenv('LOGGER_LEVEL', default='info')
|
||||||
logger_level = 'info'
|
|
||||||
else:
|
|
||||||
logger_level = os.getenv('LOGGER_LEVEL')
|
|
||||||
level = getattr(logging, logger_level.upper())
|
level = getattr(logging, logger_level.upper())
|
||||||
logging.basicConfig(format=fmt, datefmt=datefmt, level=level)
|
logging.basicConfig(format=fmt, datefmt=datefmt, level=level)
|
||||||
logging.debug('finished')
|
logging.debug('finished')
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
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
|
||||||
|
repo_name = repo_url.split('/')[-1].replace('.git', '')
|
||||||
|
repo_dir = os.getenv('REPO_DIR', default=None)
|
||||||
|
assert repo_dir is not None
|
||||||
|
dst_dir = Path(repo_dir) / repo_name
|
||||||
|
# prepare repo download dir
|
||||||
|
if dst_dir.exists():
|
||||||
|
shutil.rmtree(dst_dir)
|
||||||
|
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
|
||||||
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
LOGGER_LEVEL = debug
|
LOGGER_LEVEL = debug
|
||||||
LISTEN_IP = 0.0.0.0
|
LISTEN_IP = 0.0.0.0
|
||||||
LISTEN_PORT = 1706
|
LISTEN_PORT = 8000
|
||||||
|
REPO_DIR = /usr/src/app/repo/
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
version: "3.9"
|
||||||
|
services:
|
||||||
|
gitea-runner:
|
||||||
|
build: .
|
||||||
|
container_name: gitea-runner
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "8000:8000"
|
||||||
|
environment:
|
||||||
|
- LOGGER_LEVEL=debug
|
||||||
|
- LISTEN_IP=0.0.0.0
|
||||||
|
- LISTEN_PORT=8000
|
||||||
|
- REPO_DIR=/home/app/repo/
|
||||||
@@ -37,6 +37,7 @@ pydantic==1.10.11
|
|||||||
Pygments==2.15.1
|
Pygments==2.15.1
|
||||||
python-dateutil==2.8.2
|
python-dateutil==2.8.2
|
||||||
python-dotenv==1.0.0
|
python-dotenv==1.0.0
|
||||||
|
PyYAML==6.0
|
||||||
pyzmq==25.1.0
|
pyzmq==25.1.0
|
||||||
six==1.16.0
|
six==1.16.0
|
||||||
smmap==5.0.0
|
smmap==5.0.0
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
uvicorn==0.16.0
|
||||||
|
fastapi==0.99.1
|
||||||
|
GitPython==3.1.31
|
||||||
|
python-dotenv==1.0.0
|
||||||
|
PyYAML==6.0
|
||||||
@@ -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
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
from pydantic import AnyHttpUrl
|
|
||||||
from pathlib import Path
|
|
||||||
from git import Repo
|
|
||||||
|
|
||||||
def clone_repository(repo_url: AnyHttpUrl) -> Path:
|
|
||||||
# extract repo name
|
|
||||||
repo_name = repo_url.split('/')[-1].replace('.git', '')
|
|
||||||
dst_dir = Path(__file__).parent / repo_name
|
|
||||||
# prepare repo download dir
|
|
||||||
dst_dir.mkdir(exist_ok=True)
|
|
||||||
# git clone repo
|
|
||||||
Repo.clone_from(url=repo_url, to_path=dst_dir)
|
|
||||||
return dst_dir
|
|
||||||
Reference in New Issue
Block a user