diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..ecb91aa --- /dev/null +++ b/.drone.yml @@ -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 \ No newline at end of file diff --git a/pythonProject/Dockerfile b/pythonProject/Dockerfile new file mode 100644 index 0000000..472965a --- /dev/null +++ b/pythonProject/Dockerfile @@ -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"] diff --git a/pythonProject/worker.py b/pythonProject/worker.py index 49f7d58..0c1b651 100644 --- a/pythonProject/worker.py +++ b/pythonProject/worker.py @@ -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,18 +50,20 @@ 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']}") - 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', - 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') - LOGGER.debug("signed file resend to amqp") + 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=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=OUT_ROUTING_KEY) + LOGGER.debug("signed file resend to amqp") channel.basic_ack(delivery_tag=method_frame.delivery_tag) except Exception as e: @@ -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()