configure for building worker within a docker image

This commit is contained in:
Julien Fastré 2024-06-27 12:26:23 +02:00
parent 75ac2587a0
commit 04c4709c84
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
3 changed files with 79 additions and 19 deletions

29
.drone.yml Normal file
View File

@ -0,0 +1,29 @@
---
kind: pipeline
type: docker
name: build-images
image_pull_secrets:
- dockerconfig
trigger:
event:
- cron
- push
cron:
- build-image
steps:
- name: build-base-image
image: plugins/docker
settings:
username:
from_secret: docker_username
password:
from_secret: docker_password
registry: h3m6q87t.gra7.container-registry.ovh.net
repo: h3m6q87t.gra7.container-registry.ovh.net/sign-pdf-worker/worker
tag:
- latest
pull_image: true
dockerfile: ./pythonProject/Dockerfile

17
pythonProject/Dockerfile Normal file
View File

@ -0,0 +1,17 @@
# Set base Python image version
FROM python:3.10-alpine
# Set working directory
WORKDIR /app
# Copy requirements.txt to the Docker container
COPY requirements.txt .
# Install required Python packages
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of your app's source code
COPY . .
# Command to run the worker.py script
CMD ["python", "./worker.py"]

View File

@ -2,21 +2,33 @@ import base64
import io
import json
import logging
import os
import pika
import sign
dsn = 'amqp://guest:guest@localhost:32773/%2f/to_python_sign'
LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(name) -30s %(funcName) '
'-35s %(lineno) -5d: %(message)s')
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)
LOGGER.setLevel(os.environ.get('LOG_LEVEL', logging.INFO))
orchestrator = sign.SignOrchestrator('./assets/dummy.p12',
'http://freetsa.org/tsr', pkcs12_password=None)
for v in ['AMQP_URL', 'PKCS12_PATH', 'TIMESTAMP_URL', 'QUEUE_IN', 'EXCHANGE_OUT', 'OUT_ROUTING_KEY']:
if v not in os.environ:
LOGGER.error('Missing environment variable: %s', v)
raise ValueError('Missing environment variable: ' + v)
parameters = pika.URLParameters(dsn)
DSN = os.environ.get('AMQP_URL')
PKCS12_PATH = os.environ.get('PKCS12_PATH')
TIMESTAMP_URL = os.environ.get('TIMESTAMP_URL')
QUEUE_IN = os.environ.get('QUEUE_IN')
EXCHANGE_OUT = os.environ.get('EXCHANGE_OUT')
OUT_ROUTING_KEY = os.environ.get('OUT_ROUTING_KEY')
orchestrator = sign.SignOrchestrator(PKCS12_PATH, TIMESTAMP_URL, pkcs12_password=os.environ.get('PKCS12_PASSWORD', None))
parameters = pika.URLParameters(DSN)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.confirm_delivery()
@ -38,17 +50,19 @@ def on_message(channel, method_frame, header_frame, body):
input_content=base64.b64decode(body_content['content']))
LOGGER.info(f"signature obtained, signatureId: {body_content['signatureId']}")
if bool(os.environ.get('DEBUG', 'false')):
with open(f"./assets/new.{method_frame.consumer_tag}.{method_frame.delivery_tag}.pdf", 'wb') as f:
f.write(signed.read())
LOGGER.debug("signed file saved")
# because we consumed the buffer to write a file, we have to rewind it
signed.seek(0)
channel.basic_publish(exchange='signed_docs',
channel.basic_publish(exchange=EXCHANGE_OUT,
body=json.dumps({'signatureId': body_content['signatureId'],
'content': base64.b64encode(signed.read()).decode('utf-8')}),
properties=pika.BasicProperties(content_type='application/json',
delivery_mode=pika.DeliveryMode.Transient),
routing_key='signed_doc')
routing_key=OUT_ROUTING_KEY)
LOGGER.debug("signed file resend to amqp")
channel.basic_ack(delivery_tag=method_frame.delivery_tag)
@ -65,7 +79,7 @@ def on_message(channel, method_frame, header_frame, body):
if __name__ == '__main__':
LOGGER.info('starting worker')
channel.basic_consume('to_python_sign', on_message)
channel.basic_consume(QUEUE_IN, on_message)
try:
LOGGER.info("start consuming")
channel.start_consuming()