Skip to content

Commit

Permalink
mudando de requirements txt para poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Ferino committed Apr 11, 2024
1 parent a231d26 commit aa65a38
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# PyCharm
.idea/

poetry.lock

.env

# Python specific
Expand Down
6 changes: 4 additions & 2 deletions app/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*
!api.py
!requirements.txt
!api
!config.py
!main.py
!pyproject.toml
2 changes: 1 addition & 1 deletion app/.python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.7.4
3.10.12
31 changes: 26 additions & 5 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
FROM python:3.7.4-alpine
FROM python:3.9-alpine AS builder

WORKDIR /opt/app

COPY requirements.txt .
ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_VIRTUALENVS_CREATE=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache

RUN pip install --no-cache-dir -r requirements.txt
COPY pyproject.toml ./
RUN pip install poetry==1.1.12

COPY ./api.py .
RUN poetry config virtualenvs.create false \
&& poetry install --no-dev --no-interaction --no-ansi


FROM python:3.9-alpine AS production

# COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
COPY --from=builder /opt/app/.venv /opt/app/.venv

ENV PATH="/opt/app/.venv/bin:$PATH"



WORKDIR /opt/app

COPY api ./api
COPY main.py .
COPY config.py .

EXPOSE 8000

CMD ["gunicorn", "-b", "0.0.0.0:8000", "--log-level", "debug", "api:app"]
CMD ["gunicorn", "-b", "0.0.0.0:8000", "--log-level", "debug", "main:app"]
17 changes: 0 additions & 17 deletions app/Dockerfile.bak

This file was deleted.

15 changes: 0 additions & 15 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,6 @@ clean:
@echo



clean:
@echo
@echo "******************** KILLING CONTAINER **********************************"
docker rm -f ${CONTAINER_NAME}
@echo
docker ps
@echo
@echo
@echo "******************** REMOVING DOCKER IMAGE **********************************"
docker image rm -f ${FLASK_APP_DOCKER}:${DOCKER_IMAGE_VERSION}
@echo
docker image ls
@echo

publish:
@echo
@echo "******************** BUILDING & TAGGING DOCKER IMAGE **********************************"
Expand Down
Empty file added app/api/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions app/api/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import logging
import logging.handlers
from logstash_formatter import LogstashFormatterV1

from config import LOGSTASH_HOST, LOGSTASH_PORT

logstash_handler = logging.handlers.SocketHandler(LOGSTASH_HOST, LOGSTASH_PORT)
logstash_handler.setFormatter(LogstashFormatterV1())
logger = logging.getLogger()
logger.addHandler(logstash_handler)
logger.setLevel(logging.DEBUG)
3 changes: 3 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
LOGSTASH_HOST='localhost'
LOGSTASH_PORT=5000
LOGGING_FORMAT='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
16 changes: 11 additions & 5 deletions app/api.py → app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from flask import jsonify
from flask import request

from http import HTTPStatus

from api.utils import logger

app_name = 'comentarios'
app = Flask(app_name)
Expand Down Expand Up @@ -30,27 +33,30 @@ def api_comment_new():

message = 'comment created and associated with content_id {}'.format(content_id)
response = {
'status': 'SUCCESS',
'status': HTTPStatus.OK,
'message': message,
}
return jsonify(response)
return jsonify(response), HTTPStatus.OK


@app.route('/api/comment/list/<content_id>')
def api_comment_list(content_id):
content_id = '{}'.format(content_id)
app.logger.info('Trying to retrieve comment')


if content_id in comments:
return jsonify(comments[content_id])
else:
app.logger.error('Comment id %s does not exist', content_id)
message = 'content_id {} not found'.format(content_id)
response = {
'status': 'NOT-FOUND',
'status': HTTPStatus.NOT_FOUND,
'message': message,
}
return jsonify(response), 404
return jsonify(response), HTTPStatus.NOT_FOUND


@app.route('/health')
def api_healthcheck():
return {}, 200
return {}, HTTPStatus.OK
22 changes: 22 additions & 0 deletions app/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[tool.poetry]
name = "app"
version = "0.1.0"
description = ""
authors = ["Samuel Ferino <sferino@lenovo.com>"]

[tool.poetry.dependencies]
python = "^3.9"
click = "7.1.2"
Flask = "1.1.2"
gunicorn = "20.0.4"
itsdangerous = "1.1.0"
Jinja2 = "2.11.2"
MarkupSafe = "1.1.1"
Werkzeug = "1.0.1"
logstash_formatter = "0.5.17"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
1 change: 1 addition & 0 deletions app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1
logstash_formatter==0.5.17
32 changes: 32 additions & 0 deletions local_infra/elk/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# include .env

all: run
.PHONY: all build run clean

build: clean
@echo
@echo "******************** BUILDING & TAGGING DOCKER IMAGE **********************************"
docker compose up -d --build
@echo
@echo
docker ps
@echo


run:
@echo
@echo "******************** RUNNING DOCKER INSTANCE **********************************"
docker compose up -d
@echo
@echo
docker ps
@echo


clean:
@echo
@echo "******************** KILLING CONTAINER **********************************"
docker compose down --volumes
@echo
docker ps
@echo
Empty file added local_infra/elk/README.md
Empty file.
37 changes: 37 additions & 0 deletions local_infra/elk/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: '3.8'

services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.17.0
environment:
- node.name=elasticsearch
- discovery.type=single-node
ports:
- "9200:9200"
volumes:
- elasticsearch_data:/usr/share/elasticsearch/data
restart: always

kibana:
image: docker.elastic.co/kibana/kibana:7.17.0
environment:
ELASTICSEARCH_URL: http://elasticsearch:9200
ports:
- "5601:5601"
depends_on:
- elasticsearch
restart: always

logstash:
image: docker.elastic.co/logstash/logstash:7.17.0
ports:
- "5000:5000"
volumes:
- ./logstash/logstash.conf:/usr/share/logstash/pipeline/logstash.conf
environment:
LS_JAVA_OPTS: "-Xmx256m -Xms256m"
restart: always

volumes:
elasticsearch_data:
driver: local
14 changes: 14 additions & 0 deletions local_infra/elk/logstash/logstash.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
input {
tcp {
port => 5000
codec => json
}
}

output {
elasticsearch {
hosts => ["elasticsearch:9200"]
# index => "comments"
}
stdout { codec => rubydebug }
}

0 comments on commit aa65a38

Please sign in to comment.