configure for building worker within a docker image
This commit is contained in:
parent
75ac2587a0
commit
04c4709c84
29
.drone.yml
Normal file
29
.drone.yml
Normal 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
17
pythonProject/Dockerfile
Normal 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"]
|
@ -2,21 +2,33 @@ import base64
|
|||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
import pika
|
import pika
|
||||||
import sign
|
import sign
|
||||||
|
|
||||||
dsn = 'amqp://guest:guest@localhost:32773/%2f/to_python_sign'
|
|
||||||
|
|
||||||
LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(name) -30s %(funcName) '
|
LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(name) -30s %(funcName) '
|
||||||
'-35s %(lineno) -5d: %(message)s')
|
'-35s %(lineno) -5d: %(message)s')
|
||||||
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
|
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
|
||||||
LOGGER = logging.getLogger(__name__)
|
LOGGER = logging.getLogger(__name__)
|
||||||
LOGGER.setLevel(logging.DEBUG)
|
LOGGER.setLevel(os.environ.get('LOG_LEVEL', logging.INFO))
|
||||||
|
|
||||||
orchestrator = sign.SignOrchestrator('./assets/dummy.p12',
|
for v in ['AMQP_URL', 'PKCS12_PATH', 'TIMESTAMP_URL', 'QUEUE_IN', 'EXCHANGE_OUT', 'OUT_ROUTING_KEY']:
|
||||||
'http://freetsa.org/tsr', pkcs12_password=None)
|
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)
|
connection = pika.BlockingConnection(parameters)
|
||||||
channel = connection.channel()
|
channel = connection.channel()
|
||||||
channel.confirm_delivery()
|
channel.confirm_delivery()
|
||||||
@ -38,18 +50,20 @@ def on_message(channel, method_frame, header_frame, body):
|
|||||||
input_content=base64.b64decode(body_content['content']))
|
input_content=base64.b64decode(body_content['content']))
|
||||||
LOGGER.info(f"signature obtained, signatureId: {body_content['signatureId']}")
|
LOGGER.info(f"signature obtained, signatureId: {body_content['signatureId']}")
|
||||||
|
|
||||||
with open(f"./assets/new.{method_frame.consumer_tag}.{method_frame.delivery_tag}.pdf", 'wb') as f:
|
if bool(os.environ.get('DEBUG', 'false')):
|
||||||
f.write(signed.read())
|
with open(f"./assets/new.{method_frame.consumer_tag}.{method_frame.delivery_tag}.pdf", 'wb') as f:
|
||||||
LOGGER.debug("signed file saved")
|
f.write(signed.read())
|
||||||
# because we consumed the buffer to write a file, we have to rewind it
|
LOGGER.debug("signed file saved")
|
||||||
signed.seek(0)
|
# because we consumed the buffer to write a file, we have to rewind it
|
||||||
channel.basic_publish(exchange='signed_docs',
|
signed.seek(0)
|
||||||
body=json.dumps({'signatureId': body_content['signatureId'],
|
|
||||||
'content': base64.b64encode(signed.read()).decode('utf-8')}),
|
channel.basic_publish(exchange=EXCHANGE_OUT,
|
||||||
properties=pika.BasicProperties(content_type='application/json',
|
body=json.dumps({'signatureId': body_content['signatureId'],
|
||||||
delivery_mode=pika.DeliveryMode.Transient),
|
'content': base64.b64encode(signed.read()).decode('utf-8')}),
|
||||||
routing_key='signed_doc')
|
properties=pika.BasicProperties(content_type='application/json',
|
||||||
LOGGER.debug("signed file resend to amqp")
|
delivery_mode=pika.DeliveryMode.Transient),
|
||||||
|
routing_key=OUT_ROUTING_KEY)
|
||||||
|
LOGGER.debug("signed file resend to amqp")
|
||||||
channel.basic_ack(delivery_tag=method_frame.delivery_tag)
|
channel.basic_ack(delivery_tag=method_frame.delivery_tag)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -65,7 +79,7 @@ def on_message(channel, method_frame, header_frame, body):
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
LOGGER.info('starting worker')
|
LOGGER.info('starting worker')
|
||||||
channel.basic_consume('to_python_sign', on_message)
|
channel.basic_consume(QUEUE_IN, on_message)
|
||||||
try:
|
try:
|
||||||
LOGGER.info("start consuming")
|
LOGGER.info("start consuming")
|
||||||
channel.start_consuming()
|
channel.start_consuming()
|
||||||
|
Loading…
Reference in New Issue
Block a user