Skip to content

Commit

Permalink
devops(deploy): configura o ambiente de deploy para o heroku (#131)
Browse files Browse the repository at this point in the history
* devops(docker): muda entrypoint de pasta

* devops(requirements): adiciona deps de produção

Co-authored-by: HenrikhKenino <[email protected]>

* devops(make): arruma caminho do entrypoint

* devops(heroku): arquivos de config do deploy

* devops(heroku): separa arquivos de config dev/prod

Co-authored-by: HenrikhKenino <[email protected]>

* devops(heroku): configura domínios permitidos

Co-authored-by: HenrikhKenino <[email protected]>

* devops(core): add redirect ssl to django

Co-authored-by: HenrikhKenino <[email protected]>

* devops(updatedb): cria melhor descrição de erro

* docs(executing): docs seguir novo padrão de pastas

* git(workflows): adiciona nomes aos jobs do actions

---------

Co-authored-by: HenrikhKenino <[email protected]>
  • Loading branch information
mateusvrs and henriquecq authored Dec 1, 2023
1 parent ea60e9e commit 14acfe6
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 90 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ permissions:

jobs:
docs:
name: mkdocs deploy
runs-on: ubuntu-latest
strategy:
max-parallel: 4
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ permissions:

jobs:
test:
name: api test coverage
runs-on: ubuntu-latest

steps:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ config-mock:
bash scripts/config.sh

entrypoint-chmod:
chmod +x ./api/entrypoint.sh
chmod +x ./api/config/entrypoint.sh

# Install Dependencies
install:
pip install -r requirements.txt
pip install -r ./api/requirements.txt
npm install --prefix ./web


Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE)
[![codecov](https://codecov.io/gh/unb-mds/2023-2-Squad11/branch/main/graph/badge.svg?token=ZQZQZQZQZQ)](https://codecov.io/gh/unb-mds/2023-2-Squad11)
[![GitHub issues](https://img.shields.io/github/issues/unb-mds/2023-2-Squad11)]()
[![GitHub contributors](https://img.shields.io/github/contributors/unb-mds/2023-2-Squad11)]()
[![GitHub stars](https://img.shields.io/github/stars/unb-mds/2023-2-Squad11)]()
[![GitHub issues](https://img.shields.io/github/issues/unb-mds/2023-2-Squad11)](https://img.shields.io/github/issues/unb-mds/2023-2-Squad11)
[![GitHub contributors](https://img.shields.io/github/contributors/unb-mds/2023-2-Squad11)](https://img.shields.io/github/contributors/unb-mds/2023-2-Squad11)
[![GitHub stars](https://img.shields.io/github/stars/unb-mds/2023-2-Squad11)](https://img.shields.io/github/stars/unb-mds/2023-2-Squad11)
[![Hit Counter](https://views.whatilearened.today/views/github/unb-mds/2023-2-Squad11.svg)](https://views.whatilearened.today/views/github/unb-mds/2023-2-Squad11.svg)
</br>

Expand All @@ -31,11 +31,15 @@ O projeto é software livre e está sob a licença [MIT](./LICENSE).
- [💻 Ambiente](#-ambiente)
- [📁 Dependências do projeto](#-dependências-do-projeto)
- [💾 Execução](#-execução)
- [Observações do Docker](#observações-do-docker)
- [✅ Autenticação do Google OAuth](#-autenticação-do-google-oauth)
- [🖱️ Acesso aos serviços](#-acesso-aos-serviços)
- [🖱️ Acesso aos serviços](#-acesso-aos-serviços)
- [📍 Migrations](#-migrations)
- [📚 Documentação](#-documentação)
- [📎 Extra](#-extra)
- [Story Map e Activity Flow](#story-map-e-activity-flow)
- [Arquitetura](#arquitetura)
- [Protótipo](#protótipo)

## 👥 Equipe

Expand Down Expand Up @@ -79,10 +83,10 @@ Para instalar as dependências do projeto, você pode rodar os seguintes comando

```bash
# Crie um ambiente virtual Python
python3 -m venv env
python3 -m venv api/env

# Ative o ambiente virtual
source env/bin/activate
source api/env/bin/activate

# Instale os pacotes do Python e Node
make install
Expand Down Expand Up @@ -136,7 +140,6 @@ Adicionando serviços:
- IAM Service Account Credentials API
- Identity and Access Management (IAM) API


### 🖱️ Acesso aos serviços

| Serviço | URL |
Expand Down Expand Up @@ -174,4 +177,4 @@ A documentação do projeto pode ser encontrada clicando [aqui](https://unb-mds.

### Protótipo

- Para acessar o protótipo do projeto, clique [aqui](https://www.figma.com/proto/o5Ffh1fWmmQz7KcDGuHrVP/Sua-grade-UNB?type=design&node-id=16-2775&scaling=scale-down&page-id=0%3A1&mode=design&t=L5JwoVdZsjyLBGdb-1).
- Para acessar o protótipo do projeto, clique [aqui](https://www.figma.com/proto/o5Ffh1fWmmQz7KcDGuHrVP/Sua-grade-UNB?type=design&node-id=16-2775&scaling=scale-down&page-id=0%3A1&mode=design&t=L5JwoVdZsjyLBGdb-1).
2 changes: 1 addition & 1 deletion api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Gerar secret key:
# python3 -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'
DJANGO_SECRET_KEY="your_secret_key"
SETTINGS_FILE_PATH="core.settings.base"
SETTINGS_FILE_PATH="core.settings.dev"

# Banco de Dados
DB_ENGINE="django.db.backends.postgresql"
Expand Down
2 changes: 2 additions & 0 deletions api/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
release: python3 manage.py migrate
web: gunicorn core.wsgi
17 changes: 13 additions & 4 deletions api/api/management/commands/updatedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def add_arguments(self, parser: CommandParser) -> None:
help="Atualiza o banco de dados com as disciplinas dos períodos atual e seguinte.")

parser.add_argument('-p', '--period', action='store', default=None,
choices=[".".join(sessions.get_current_year_and_period()), ".".join(sessions.get_next_period())],
choices=[".".join(sessions.get_current_year_and_period()), ".".join(
sessions.get_next_period())],
dest='period', help="Atualiza o banco de dados com as disciplinas do período especificado.")

parser.add_argument('-d', '--delete', action='store_true', dest='delete', default=False,
Expand Down Expand Up @@ -57,18 +58,26 @@ def handle(self, *args: Any, **options: Any):
print("Atualizando o banco de dados...")

for year, period in choices:
start_time = time()
self.update_departments(departments_ids=departments_ids, year=year, period=period)
self.display_success_update_message(operation=f"{year}/{period}", start_time=start_time)
try:
start_time = time()
self.update_departments(
departments_ids=departments_ids, year=year, period=period)
self.display_success_update_message(
operation=f"{year}/{period}", start_time=start_time)
except Exception as exception:
print("Houve um erro na atualização do bando de dados.")
print(f"Error: {exception}")

def update_departments(self, departments_ids: list, year: str, period: str) -> None:
"""Atualiza os departamentos do banco de dados e suas respectivas disciplinas."""
for department_id in departments_ids:
print(f"WebScraping do departamento: {department_id}")
disciplines_list = web_scraping.get_department_disciplines(
department_id=department_id, current_year=year, current_period=period)
department = get_or_create_department(
code=department_id, year=year, period=period)

print("Atualizando disciplinas do departamento...")
# Para cada disciplina do período atual, deleta as turmas previamente cadastradas e cadastra novas turmas no banco de dados
for discipline_code in disciplines_list:
classes_info = disciplines_list[discipline_code]
Expand Down
File renamed without changes.
21 changes: 0 additions & 21 deletions api/core/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config("DJANGO_SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ["*"]
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True

# Application definition

INSTALLED_APPS = [
Expand Down Expand Up @@ -119,20 +112,6 @@
WSGI_APPLICATION = 'core.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": config("DB_NAME"),
"USER": config("DB_USERNAME"),
"PASSWORD": config("DB_PASSWORD"),
"HOST": config("DB_HOSTNAME"),
"PORT": config("DB_PORT", cast=int),
}
}

# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

Expand Down
26 changes: 23 additions & 3 deletions api/core/settings/dev.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
from .base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = [
"localhost",
"0.0.0.0",
"127.0.0.1"
]

# django-cors-headers
# https://github.com/adamchainz/django-cors-headers

CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True


# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": config("DB_NAME"),
"USER": config("DB_USERNAME"),
"PASSWORD": config("DB_PASSWORD"),
"HOST": config("DB_HOSTNAME"),
"PORT": config("DB_PORT", cast=int),
}
}
56 changes: 55 additions & 1 deletion api/core/settings/prod.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,60 @@
from .base import *

import dj_database_url


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ["0.0.0.0:8000", "localhost:8000", "127.0.0.1:8000"]
ALLOWED_HOSTS = [
".herokuapp.com",
".suagradeunb.com.br"
]


# django-cors-headers
# https://github.com/adamchainz/django-cors-headers

CORS_ALLOWED_ORIGINS = [
"https://suagradeunb.com.br",
"https://api.suagradeunb.com.br"
]
# CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True


# SSL Redirect

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = True


# Application definition

INSTALLED_APPS.insert(0, "whitenoise.runserver_nostatic")

MIDDLEWARE.insert(1, "whitenoise.middleware.WhiteNoiseMiddleware")


# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
"default": dj_database_url.config(
conn_max_age=600,
conn_health_checks=True,
ssl_require=True,
),
}

STORAGES = {
# Enable WhiteNoise's GZip and Brotli compression of static assets:
# https://whitenoise.readthedocs.io/en/latest/django.html#add-compression-and-caching-support
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}

# Don't store the original (un-hashed filename) version of static files, to reduce slug size:
# https://whitenoise.readthedocs.io/en/latest/django.html#WHITENOISE_KEEP_ONLY_HASHED_FILES
WHITENOISE_KEEP_ONLY_HASHED_FILES = True
15 changes: 6 additions & 9 deletions requirements.txt → api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ asgiref==3.7.2
autopep8==2.0.4
Babel==2.12.1
beautifulsoup4==4.12.2
Brotli==1.1.0
cachetools==5.3.1
certifi==2023.7.22
cffi==1.16.0
Expand All @@ -11,18 +12,14 @@ colorama==0.4.6
coverage==7.3.2
cryptography==41.0.4
defusedxml==0.7.1
dj-database-url==2.1.0
Django==4.2.5
django-allauth==0.57.0
django-cors-headers==4.3.0
djangorestframework==3.14.0
djangorestframework-simplejwt==5.3.0
drf-yasg==1.21.7
ghp-import==2.1.0
google-api-core==2.12.0
google-api-python-client==2.102.0
google-auth==2.23.2
google-auth-httplib2==0.1.1
googleapis-common-protos==1.60.0
gunicorn==21.2.0
httplib2==0.22.0
idna==3.4
inflection==0.5.1
Expand All @@ -33,13 +30,13 @@ mergedeep==1.3.4
mkdocs==1.5.3
mkdocs-material==9.4.2
mkdocs-material-extensions==1.2
oauthlib==3.2.2
packaging==23.1
paginate==0.5.6
pathspec==0.11.2
platformdirs==3.10.0
protobuf==4.24.4
psycopg2-binary==2.9.7
psycopg==3.1.13
psycopg-binary==3.1.13
pyasn1==0.5.0
pyasn1-modules==0.3.0
pycodestyle==2.11.1
Expand All @@ -56,7 +53,6 @@ PyYAML==6.0.1
pyyaml_env_tag==0.1
regex==2023.8.8
requests==2.31.0
requests-oauthlib==1.3.1
rsa==4.9
six==1.16.0
soupsieve==2.5
Expand All @@ -66,3 +62,4 @@ Unidecode==1.3.7
uritemplate==4.1.1
urllib3==2.0.5
watchdog==3.0.0
whitenoise==6.6.0
1 change: 1 addition & 0 deletions api/runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-3.11.6
10 changes: 3 additions & 7 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@ WORKDIR /usr/src/api
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# instalar dependências do poastgres
# instalar dependências do postgres
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev

# copiar os requisitos python
COPY ./requirements.txt /usr/src/api/requirements.txt
COPY ./api/requirements.txt /usr/src/api/requirements.txt

# instalação das dependências
RUN pip install --upgrade pip && \
pip install -r /usr/src/api/requirements.txt

# copiar entrypoint.sh e dar permissão de execução
COPY ./api/entrypoint.sh /usr/src/api/entrypoint.sh
RUN chmod +x /usr/src/api/entrypoint.sh

# copiar o projeto
COPY ./api /usr/src/api/

# executar o arquivo de inicialização
ENTRYPOINT ["/usr/src/api/entrypoint.sh"]
ENTRYPOINT ["/usr/src/api/config/entrypoint.sh"]
Loading

0 comments on commit 14acfe6

Please sign in to comment.