Compare commits

..

2 commits
dev ... main

Author SHA1 Message Date
Csaba 797eff5b2a Change bearer token expiry to 1 week
All checks were successful
Amarillo/amarillo-gitea/amarillo-core/pipeline/head This commit looks good
2024-05-22 14:53:10 +02:00
Csaba b9b47dfc2a Updated Jenkinfile to use grfs-exporter
All checks were successful
Amarillo/amarillo-gitea/amarillo-core/pipeline/head This commit looks good
2024-04-22 15:06:02 +02:00
10 changed files with 48 additions and 97 deletions

View file

@ -18,13 +18,18 @@ RUN \
ENV ADMIN_TOKEN='' ENV ADMIN_TOKEN=''
ENV RIDE2GO_TOKEN='' ENV RIDE2GO_TOKEN=''
ENV SECRET_KEY='' ENV SECRET_KEY=''
ENV METRICS_USER=''
ENV MODULE_NAME=amarillo.main ENV METRICS_PASSWORD=''
EXPOSE 80 EXPOSE 80
ARG PACKAGE_REGISTRY_URL
ARG PLUGINS
COPY requirements.txt /app/requirements.txt COPY requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
RUN --mount=type=secret,id=AMARILLO_REGISTRY_CREDENTIALS \
pip install --no-cache-dir --upgrade --extra-index-url https://$(cat /run/secrets/AMARILLO_REGISTRY_CREDENTIALS)@${PACKAGE_REGISTRY_URL} ${PLUGINS}
COPY ./amarillo /app/amarillo COPY ./amarillo /app/amarillo
COPY ./amarillo/plugins /app/amarillo/plugins COPY ./amarillo/plugins /app/amarillo/plugins

58
Jenkinsfile vendored
View file

@ -1,17 +1,16 @@
pipeline { pipeline {
agent { label 'builtin' } agent any
environment { environment {
GITEA_CREDS = credentials('AMARILLO-JENKINS-GITEA-USER') GITEA_CREDS = credentials('AMARILLO-JENKINS-GITEA-USER')
TWINE_REPO_URL = "https://git.gerhardt.io/api/packages/amarillo/pypi" TWINE_REPO_URL = "https://git.gerhardt.io/api/packages/amarillo/pypi"
PLUGINS_REPO_URL = "git.gerhardt.io/api/packages/amarillo/pypi/simple" PLUGINS_REPO_URL = "git.gerhardt.io/api/packages/amarillo/pypi/simple"
DOCKER_REGISTRY = 'git.gerhardt.io' DOCKER_REGISTRY_URL = 'https://git.gerhardt.io'
DERIVED_DOCKERFILE = 'standard.Dockerfile'
OWNER = 'amarillo' OWNER = 'amarillo'
BASE_IMAGE_NAME = 'amarillo-base'
IMAGE_NAME = 'amarillo' IMAGE_NAME = 'amarillo'
AMARILLO_DISTRIBUTION = '0.3' AMARILLO_DISTRIBUTION = '0.2'
TAG = "${AMARILLO_DISTRIBUTION}.${BUILD_NUMBER}${env.BRANCH_NAME == 'main' ? '' : '-dev'}" TAG = "${AMARILLO_DISTRIBUTION}.${BUILD_NUMBER}"
DEPLOY_WEBHOOK_URL = "${env.BRANCH_NAME == 'main' ? 'http://amarillo.mfdz.de:8888/mitanand' : 'http://amarillo.mfdz.de:8888/dev'}" PLUGINS = 'amarillo-metrics amarillo-enhancer amarillo-grfs-exporter'
DEPLOY_WEBHOOK_URL = 'http://amarillo.mfdz.de:8888/mitanand'
DEPLOY_SECRET = credentials('AMARILLO-JENKINS-DEPLOY-SECRET') DEPLOY_SECRET = credentials('AMARILLO-JENKINS-DEPLOY-SECRET')
} }
stages { stages {
@ -42,58 +41,27 @@ pipeline {
sh 'python3 -m twine upload --skip-existing --verbose --repository-url $TWINE_REPO_URL --username $GITEA_CREDS_USR --password $GITEA_CREDS_PSW ./dist/*' sh 'python3 -m twine upload --skip-existing --verbose --repository-url $TWINE_REPO_URL --username $GITEA_CREDS_USR --password $GITEA_CREDS_PSW ./dist/*'
} }
} }
stage('Build base docker image') { stage('Build docker image') {
when { when {
anyOf { branch 'main'; branch 'dev' } branch 'main'
} }
steps { steps {
echo 'Building image' echo 'Building image'
script { script {
docker.build("${OWNER}/${BASE_IMAGE_NAME}:${TAG}")
// "--no-cache --build-arg='PACKAGE_REGISTRY_URL=${PLUGINS_REPO_URL}' --build-arg='PLUGINS=${PLUGINS}' --secret id=AMARILLO_REGISTRY_CREDENTIALS,env=GITEA_CREDS .")
}
}
}
stage('Push base image to container registry') {
when {
anyOf { branch 'main'; branch 'dev' }
}
steps {
echo 'Pushing image to registry'
script {
docker.withRegistry("https://${DOCKER_REGISTRY}", 'AMARILLO-JENKINS-GITEA-USER'){
def image = docker.image("${OWNER}/${BASE_IMAGE_NAME}:${TAG}")
image.push()
image.push('latest')
}
}
}
}
stage('Build derived docker image') {
when {
anyOf { branch 'main'; branch 'dev' }
}
steps {
echo 'Building image'
script {
docker.withRegistry("https://${DOCKER_REGISTRY}", 'AMARILLO-JENKINS-GITEA-USER'){
docker.build("${OWNER}/${IMAGE_NAME}:${TAG}", docker.build("${OWNER}/${IMAGE_NAME}:${TAG}",
//--no-cache to make sure plugins are updated //--no-cache to make sure plugins are updated
"-f ${DERIVED_DOCKERFILE} --no-cache --build-arg='PACKAGE_REGISTRY_URL=${PLUGINS_REPO_URL}' --build-arg='DOCKER_REGISTRY=${DOCKER_REGISTRY}' --secret id=AMARILLO_REGISTRY_CREDENTIALS,env=GITEA_CREDS .") "--no-cache --build-arg='PACKAGE_REGISTRY_URL=${PLUGINS_REPO_URL}' --build-arg='PLUGINS=${PLUGINS}' --secret id=AMARILLO_REGISTRY_CREDENTIALS,env=GITEA_CREDS .")
// "--no-cache --build-arg='PACKAGE_REGISTRY_URL=${PLUGINS_REPO_URL}' --build-arg='PLUGINS=${PLUGINS}' --secret id=AMARILLO_REGISTRY_CREDENTIALS,env=GITEA_CREDS .")
}
} }
} }
} }
stage('Push derived image to container registry') { stage('Push image to container registry') {
when { when {
anyOf { branch 'main'; branch 'dev' } branch 'main'
} }
steps { steps {
echo 'Pushing image to registry' echo 'Pushing image to registry'
script { script {
docker.withRegistry("https://${DOCKER_REGISTRY}", 'AMARILLO-JENKINS-GITEA-USER'){ docker.withRegistry(DOCKER_REGISTRY_URL, 'AMARILLO-JENKINS-GITEA-USER'){
def image = docker.image("${OWNER}/${IMAGE_NAME}:${TAG}") def image = docker.image("${OWNER}/${IMAGE_NAME}:${TAG}")
image.push() image.push()
image.push('latest') image.push('latest')
@ -103,7 +71,7 @@ pipeline {
} }
stage('Notify CD script') { stage('Notify CD script') {
when { when {
anyOf { branch 'main'; branch 'dev' } branch 'main'
} }
steps { steps {
echo 'Triggering deploy webhook' echo 'Triggering deploy webhook'

View file

@ -5,8 +5,7 @@ import os.path
import re import re
from glob import glob from glob import glob
from fastapi import APIRouter, Body, HTTPException, status, Depends, BackgroundTasks from fastapi import APIRouter, Body, HTTPException, status, Depends
import requests
from datetime import datetime from datetime import datetime
from amarillo.models.Carpool import Carpool from amarillo.models.Carpool import Carpool
@ -14,8 +13,6 @@ from amarillo.models.User import User
from amarillo.services.oauth2 import get_current_user, verify_permission from amarillo.services.oauth2 import get_current_user, verify_permission
from amarillo.tests.sampledata import examples from amarillo.tests.sampledata import examples
from amarillo.services.config import config
from amarillo.utils.utils import assert_folder_exists
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -24,20 +21,6 @@ router = APIRouter(
tags=["carpool"] tags=["carpool"]
) )
#TODO: housekeeping for outdated trips
def enhance_trip(carpool: Carpool):
response = requests.post(f"{config.enhancer_url}", carpool.model_dump_json())
enhanced_carpool = Carpool(**json.loads(response.content))
#TODO: use data/enhanced directory
folder = f'data/enhanced/{carpool.agency}'
filename = f'{folder}/{carpool.id}.json'
assert_folder_exists(folder)
with open(filename, 'w', encoding='utf-8') as f:
f.write(enhanced_carpool.model_dump_json())
@router.post("/", @router.post("/",
operation_id="addcarpool", operation_id="addcarpool",
summary="Add a new or update existing carpool", summary="Add a new or update existing carpool",
@ -49,7 +32,7 @@ def enhance_trip(carpool: Carpool):
"description": "Agency does not exist"}, "description": "Agency does not exist"},
}) })
async def post_carpool(background_tasks: BackgroundTasks, carpool: Carpool = Body(..., examples=examples), async def post_carpool(carpool: Carpool = Body(..., examples=examples),
requesting_user: User = Depends(get_current_user)) -> Carpool: requesting_user: User = Depends(get_current_user)) -> Carpool:
verify_permission(f"{carpool.agency}:write", requesting_user) verify_permission(f"{carpool.agency}:write", requesting_user)
@ -58,8 +41,6 @@ async def post_carpool(background_tasks: BackgroundTasks, carpool: Carpool = Bod
await store_carpool(carpool) await store_carpool(carpool)
background_tasks.add_task(enhance_trip, carpool)
return carpool return carpool
# TODO 403 # TODO 403
@ -110,10 +91,12 @@ async def _delete_carpool(agency_id: str, carpool_id: str):
# load and store, to receive pyinotify events and have file timestamp updated # load and store, to receive pyinotify events and have file timestamp updated
await save_carpool(cp, 'data/trash') await save_carpool(cp, 'data/trash')
logger.info(f"Saved carpool {agency_id}:{carpool_id} in trash.") logger.info(f"Saved carpool {agency_id}:{carpool_id} in trash.")
try:
os.remove(f"data/carpool/{agency_id}/{carpool_id}.json") os.remove(f"data/carpool/{agency_id}/{carpool_id}.json")
os.remove(f"data/enhanced/{agency_id}/{carpool_id}.json", )
except FileNotFoundError: try:
from amarillo.plugins.metrics import trips_deleted_counter
trips_deleted_counter.inc()
except ImportError:
pass pass
@ -123,6 +106,17 @@ async def store_carpool(carpool: Carpool) -> Carpool:
await set_lastUpdated_if_unset(carpool) await set_lastUpdated_if_unset(carpool)
await save_carpool(carpool) await save_carpool(carpool)
try:
from amarillo.plugins.metrics import trips_created_counter, trips_updated_counter
if(carpool_exists):
# logger.info("Incrementing trips updated")
trips_updated_counter.inc()
else:
# logger.info("Incrementing trips created")
trips_created_counter.inc()
except ImportError:
pass
return carpool return carpool
async def set_lastUpdated_if_unset(carpool): async def set_lastUpdated_if_unset(carpool):

View file

@ -37,6 +37,11 @@ class CarpoolService():
if cp and self.is_outdated(cp): if cp and self.is_outdated(cp):
logger.info("Purge outdated offer %s", key) logger.info("Purge outdated offer %s", key)
self.delete(cp.agency, cp.id) self.delete(cp.agency, cp.id)
try:
from amarillo.plugins.metrics import trips_deleted_counter
trips_deleted_counter.inc()
except ImportError:
pass
def get(self, agency_id: str, carpool_id: str): def get(self, agency_id: str, carpool_id: str):
return self.carpools.get(f"{agency_id}:{carpool_id}") return self.carpools.get(f"{agency_id}:{carpool_id}")

View file

@ -8,6 +8,5 @@ class Config(BaseSettings):
env: str = 'DEV' env: str = 'DEV'
graphhopper_base_url: str = 'https://api.mfdz.de/gh' graphhopper_base_url: str = 'https://api.mfdz.de/gh'
stop_sources_file: str = 'data/stop_sources.json' stop_sources_file: str = 'data/stop_sources.json'
enhancer_url: str = 'http://localhost:8001'
config = Config(_env_file='config', _env_file_encoding='utf-8') config = Config(_env_file='config', _env_file_encoding='utf-8')

View file

@ -20,7 +20,7 @@ from amarillo.services.secrets import secrets
SECRET_KEY = secrets.secret_key SECRET_KEY = secrets.secret_key
ALGORITHM = "HS256" ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30 ACCESS_TOKEN_EXPIRE_MINUTES = 7*24*60
logging.config.fileConfig('logging.conf', disable_existing_loggers=False) logging.config.fileConfig('logging.conf', disable_existing_loggers=False)
logger = logging.getLogger("main") logger = logging.getLogger("main")

View file

@ -65,7 +65,6 @@ class UserService:
logger.error(message) logger.error(message)
raise HTTPException(status_code=400, detail=message) raise HTTPException(status_code=400, detail=message)
#TODO: fix duplicate None api key
def add(self, user_conf: User): def add(self, user_conf: User):
user_id = user_conf.user_id user_id = user_conf.user_id

View file

@ -22,7 +22,7 @@ args=(sys.stdout,)
class=handlers.RotatingFileHandler class=handlers.RotatingFileHandler
level=ERROR level=ERROR
formatter=simpleFormatter formatter=simpleFormatter
args=('data/error.log', 'a', 1000000, 3) # Filename, mode, maxBytes, backupCount args=('error.log', 'a', 1000000, 3) # Filename, mode, maxBytes, backupCount
[formatter_simpleFormatter] [formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

View file

@ -1,6 +1,6 @@
[project] [project]
name = "amarillo" name = "amarillo"
version = "0.0.16a1" version = "0.0.15a4"
description = "Aggregates and enhances carpooling-offers and publishes them as GTFS(-RT)" description = "Aggregates and enhances carpooling-offers and publishes them as GTFS(-RT)"
readme = "README.md" readme = "README.md"
license = {file = "LICENSE"} license = {file = "LICENSE"}

View file

@ -1,19 +0,0 @@
ARG DOCKER_REGISTRY
FROM ${DOCKER_REGISTRY}/amarillo/amarillo-base
ARG PLUGINS=\
"amarillo-metrics "\
"amarillo-gtfs-exporter "
ARG PACKAGE_REGISTRY_URL
ENV METRICS_USER=''
ENV METRICS_PASSWORD=''
# RUN pip install $PLUGINS
RUN --mount=type=secret,id=AMARILLO_REGISTRY_CREDENTIALS \
pip install --no-cache-dir --upgrade --extra-index-url https://$(cat /run/secrets/AMARILLO_REGISTRY_CREDENTIALS)@${PACKAGE_REGISTRY_URL} ${PLUGINS}
RUN useradd amarillo
USER amarillo