Compare commits

...
15 Commits
Author SHA1 Message Date
brb 70a087ee7b ignored python run files 2023-07-06 11:52:46 +02:00
brb 60112f1484 containerised service 2023-07-06 11:52:08 +02:00
brb f0010d739b added dotenv 2023-07-06 11:51:48 +02:00
brb 155a93456c updated downloading of repository 2023-07-06 11:09:28 +02:00
brb fdd968e3a3 updated var name 2023-07-06 11:08:27 +02:00
brb 8c9880ecca updated running 2023-07-06 11:06:46 +02:00
brb 56d0429bab added production environment variables 2023-07-06 11:05:28 +02:00
brb 78a2201a81 added logging and os.environ reading 2023-07-06 11:04:42 +02:00
brb 29a3545da4 added default value when getting os environ 2023-07-06 11:04:13 +02:00
brb 08eaaa3b5e added repo download dir 2023-07-06 10:58:35 +02:00
brb 65d2e850b5 moved file 2023-07-06 10:57:05 +02:00
brb b2bb34250b added production environment requirements 2023-07-06 10:56:17 +02:00
brb 9c357f52a1 deleted unused file 2023-07-06 10:55:55 +02:00
brb 4454ac1b46 updated code to latest version 2023-07-06 10:55:36 +02:00
brb 344f29b77c removed unused imports 2023-07-06 10:55:21 +02:00
11 changed files with 90 additions and 113 deletions
+1
View File
@@ -0,0 +1 @@
__pycache__
+3 -9
View File
@@ -3,13 +3,7 @@
###########
# pull official base image
FROM python:alpine as builder
# update system
RUN apk update && \
apk add git && \
apk add rsync && \
apk add docker
FROM dvcorg/cml:0-dvc2-base1-gpu as builder
# set work directory
WORKDIR /usr/src/app
@@ -20,7 +14,7 @@ ENV PYTHONUNBUFFERED 1
# install dependencies
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
#########
@@ -28,7 +22,7 @@ RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requir
#########
# pull official base image
FROM python:alpine
FROM dvcorg/cml:0-dvc2-base1-gpu
# create app home
ENV APP_HOME = /home/app
-53
View File
@@ -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'
)
+52 -31
View File
@@ -1,38 +1,59 @@
from flask import Flask, request, jsonify
import os
import uvicorn
from fastapi import FastAPI, Request
from dotenv import load_dotenv
from ipaddress import ip_address, ip_network
import logging
from pydantic import BaseModel
from typing import List
import os
load_dotenv()
app = Flask(__name__)
from utils import clone_repository
from setup_logging import setup_logging
@app.before_request
def check_authorized():
"""
Ensure request from allowed ip.
"""
# 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}')
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.before_request
def check_media_type():
"""
Ensure correct Content-Type of request
"""
if not request.headers.get('Content-Type').lower().startswith('application/json'):
logging.error(f'received request with wrong content type')
return jsonify(status='unsupported media type'), 415
app = FastAPI()
@app.route('/test', methods=['POST'])
def test():
logging.debug(request.get_json(force=True))
return jsonify(status='success', sender=request.remote_addr)
@app.get('/')
async def root():
return '<h1>Welcome to Gitea Runsner</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__':
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
)
+11 -13
View File
@@ -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 waitress import serve
import uvicorn
from setup_logging import setup_logging
from app import app
import logging
import os
if __name__ == '__main__':
# setup
load_dotenv()
setup_logging()
# serve app
serve(app, host='0.0.0.0', port=1706)
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
)
+1 -4
View File
@@ -5,10 +5,7 @@ import os
def setup_logging():
fmt = '%(asctime)s | %(levelname)s | %(filename)s | %(funcName)s | %(message)s'
datefmt = '%Y-%m-%d %H:%M:%S'
if not 'LOGGER_LEVEL' in os.environ:
logger_level = 'info'
else:
logger_level = os.getenv('LOGGER_LEVEL')
logger_level = os.getenv('LOGGER_LEVEL', default='info')
level = getattr(logging, logger_level.upper())
logging.basicConfig(format=fmt, datefmt=datefmt, level=level)
logging.debug('finished')
+4 -1
View File
@@ -2,11 +2,14 @@ from pydantic import AnyHttpUrl
from pathlib import Path
from git import Repo
import shutil
import os
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
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)
+2 -1
View File
@@ -1,3 +1,4 @@
LOGGER_LEVEL = debug
LISTEN_IP = 0.0.0.0
LISTEN_PORT = 1706
LISTEN_PORT = 8000
REPO_DIR = /usr/src/app/repo/
+7
View File
@@ -0,0 +1,7 @@
version: "3.9"
services:
gitea-runner:
build: .
container_name: gitea-runner
restart: unless-stopped
env_file: prod.env
+4
View File
@@ -0,0 +1,4 @@
LOGGER_LEVEL = info
LISTEN_IP = 0.0.0.0
LISTEN_PORT = 8000
REPO_DIR = /usr/src/app/repo/
+4
View File
@@ -0,0 +1,4 @@
uvicorn==0.16.0
fastapi==0.99.1
GitPython==3.1.31
python-dotenv==1.0.0