From a64cc9481be25e767d9b7c990c96dfe08805600d Mon Sep 17 00:00:00 2001 From: root Date: Fri, 11 Oct 2024 15:18:10 +0000 Subject: [PATCH 01/39] func: setup docker do projeto --- Dockerfile | 20 ++++++++ docker-compose.yml | 26 ++++++++++ ...rta_condicao_alter_alerta_nome_and_more.py | 48 +++++++++++++++++++ src/tupan/tupan/settings.py | 7 +-- 4 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..45016ce --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM python:3.11 + +# Definir o diretório de trabalho dentro do container +WORKDIR /app + +# Copiar o arquivo de requisitos para o container +COPY requirements.txt /app/ + +# Instalar as dependências +RUN pip install --upgrade pip && pip install -r requirements.txt + +# Copiar o código da aplicação +COPY . /app/ + +# Expôr a porta que o Django vai usar +EXPOSE 8000 + +# Comando para rodar o servidor +CMD ["python", "src/tupan/manage.py", "runserver", "0.0.0.0:8000"] + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d733ff4 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: '3' + +services: + web: + build: . + command: python src/tupan/manage.py runserver 0.0.0.0:8000 + volumes: + - .:/app + ports: + - "8000:8000" + environment: + - DEBUG=True + - DATABASE_URL=postgres://postgres:fatec@db:5432:/tupan + db: + image: postgres + environment: + POSTGRES_DB: tupan + POSTGRES_USER: postgres + POSTGRES_PASSWORD: fatec + ports: + - "5432:5432" + volumes: + - postgres_data:/var/lib/postgresql/data/ +volumes: + postgres_data: + diff --git a/src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py b/src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py new file mode 100644 index 0000000..0974190 --- /dev/null +++ b/src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py @@ -0,0 +1,48 @@ +# Generated by Django 5.1.1 on 2024-10-11 10:34 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('alertas', '0002_medicao'), + ] + + operations = [ + migrations.AlterField( + model_name='alerta', + name='condicao', + field=models.CharField(help_text='Condição para o alerta acontecer', max_length=4), + ), + migrations.AlterField( + model_name='alerta', + name='nome', + field=models.CharField(help_text='Nome do alerta', max_length=127, unique=True), + ), + migrations.AlterField( + model_name='historicoalerta', + name='timestamp', + field=models.BigIntegerField(help_text='Data/hora do alerta em timestamp'), + ), + migrations.AlterField( + model_name='historicoalerta', + name='timestamp_convertido', + field=models.DateTimeField(blank=True, help_text='Data/hora do alerta em datetime', null=True), + ), + migrations.AlterField( + model_name='medicao', + name='dados', + field=models.CharField(help_text='Valor dos dados da medição', max_length=63), + ), + migrations.AlterField( + model_name='medicao', + name='timestamp', + field=models.BigIntegerField(help_text='Data/hora da medição em timestamp'), + ), + migrations.AlterField( + model_name='medicao', + name='timestamp_convertido', + field=models.DateTimeField(blank=True, help_text='Data/hora da medição em datetime', null=True), + ), + ] diff --git a/src/tupan/tupan/settings.py b/src/tupan/tupan/settings.py index 98b3668..32e27d2 100644 --- a/src/tupan/tupan/settings.py +++ b/src/tupan/tupan/settings.py @@ -29,7 +29,7 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ["*"] AUTH_USER_MODEL = 'usuarios.Usuario' @@ -88,10 +88,7 @@ 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] -CORS_ALLOWED_ORIGINS = [ - 'http://localhost:3000', -] - +CORS_ALLOW_ALL_ORIGINS = True ROOT_URLCONF = 'tupan.urls' TEMPLATES = [ From ab6fa00680f19ba0665894c975ecc593a778c3fc Mon Sep 17 00:00:00 2001 From: Ryan Araujo Date: Mon, 21 Oct 2024 12:56:58 +0000 Subject: [PATCH 02/39] =?UTF-8?q?func:=20definindo=20restart=20da=20aplica?= =?UTF-8?q?=C3=A7=C3=A3o=20junto=20ao=20restart=20da=20m=C3=A1quina?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index d733ff4..000106c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,7 @@ services: environment: - DEBUG=True - DATABASE_URL=postgres://postgres:fatec@db:5432:/tupan + restart: always db: image: postgres environment: @@ -21,6 +22,7 @@ services: - "5432:5432" volumes: - postgres_data:/var/lib/postgresql/data/ + restart: always volumes: postgres_data: From e13dc376ff67e64af1e6f14183a13ce9a940b2b9 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Wed, 23 Oct 2024 10:39:41 -0300 Subject: [PATCH 03/39] oper: add deploy action e rodando testes em todas branches --- .github/workflows/deployEC2.yml | 43 +++++++++++++++++++++++++++++++++ .github/workflows/django.yml | 4 +-- docker-compose.yml | 1 + 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/deployEC2.yml diff --git a/.github/workflows/deployEC2.yml b/.github/workflows/deployEC2.yml new file mode 100644 index 0000000..1a395c2 --- /dev/null +++ b/.github/workflows/deployEC2.yml @@ -0,0 +1,43 @@ +name: Deploy to AWS EC2 +on: + push: + branches: + - main + pull_request: + branches: + - main +env: + AWS_PRIVATE_KEY: ${{ secrets.KEYAWS }} + SSH_OPTIONS: '-o StrictHostKeyChecking=no -i key.pem ubuntu@98.80.44.121' + +jobs: + deploy: + runs-on: ubuntu-24.04 + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set permissions for private key + run: | + echo "${{ env.AWS_PRIVATE_KEY }}" > key.pem + chmod 600 key.pem + + - name: Create target directory on AWS instance + run: | + ssh ${{ env.SSH_OPTIONS }} 'mkdir -p /home/ubuntu/tupan-back/' + + - name: Transfer code to AWS instance + run: | + scp -o StrictHostKeyChecking=no -i key.pem -r ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back + + - name: Stop running containers + run: | + ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose down' + + - name: Run Docker Compose + run: | + ssh ${{ env.SSH_OPTIONS }} 'sudo docker-compose up' + + - name: Cleanup SSH key + run: | + rm -f key.pem \ No newline at end of file diff --git a/.github/workflows/django.yml b/.github/workflows/django.yml index 2aa49ab..44ca2d5 100644 --- a/.github/workflows/django.yml +++ b/.github/workflows/django.yml @@ -2,9 +2,7 @@ name: Django CI on: push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] + branches: [ "**" ] jobs: build: diff --git a/docker-compose.yml b/docker-compose.yml index 000106c..f088f52 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,6 +2,7 @@ version: '3' services: web: + image: tupan-back build: . command: python src/tupan/manage.py runserver 0.0.0.0:8000 volumes: From 862a3ac9a2c41624c85d1739f8d99c09c1ace338 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Wed, 23 Oct 2024 10:42:37 -0300 Subject: [PATCH 04/39] oper: alterando extencao do arquivo da action --- .github/workflows/{deployEC2.yml => deployEC2.yaml} | 0 .github/workflows/{django.yml => django.yaml} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{deployEC2.yml => deployEC2.yaml} (100%) rename .github/workflows/{django.yml => django.yaml} (100%) diff --git a/.github/workflows/deployEC2.yml b/.github/workflows/deployEC2.yaml similarity index 100% rename from .github/workflows/deployEC2.yml rename to .github/workflows/deployEC2.yaml diff --git a/.github/workflows/django.yml b/.github/workflows/django.yaml similarity index 100% rename from .github/workflows/django.yml rename to .github/workflows/django.yaml From 49f9d47619b8d4fc6524db68fa23a6a7a30bf1a7 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Wed, 23 Oct 2024 13:24:38 -0300 Subject: [PATCH 05/39] corr: fix deploy action --- .github/workflows/deployEC2.yaml | 2 +- .github/workflows/django.yaml | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml index 1a395c2..53e1c11 100644 --- a/.github/workflows/deployEC2.yaml +++ b/.github/workflows/deployEC2.yaml @@ -36,7 +36,7 @@ jobs: - name: Run Docker Compose run: | - ssh ${{ env.SSH_OPTIONS }} 'sudo docker-compose up' + ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose up' - name: Cleanup SSH key run: | diff --git a/.github/workflows/django.yaml b/.github/workflows/django.yaml index 44ca2d5..ccb249d 100644 --- a/.github/workflows/django.yaml +++ b/.github/workflows/django.yaml @@ -2,7 +2,8 @@ name: Django CI on: push: - branches: [ "**" ] + branches: + - '*' jobs: build: From 1d4c8f054438081e1e5251f348cb3bd299dbc215 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Wed, 23 Oct 2024 13:44:12 -0300 Subject: [PATCH 06/39] oper: trocando scp por rsync para ignorar algumas pastas --- .github/workflows/deployEC2.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml index 53e1c11..6d603b0 100644 --- a/.github/workflows/deployEC2.yaml +++ b/.github/workflows/deployEC2.yaml @@ -28,7 +28,7 @@ jobs: - name: Transfer code to AWS instance run: | - scp -o StrictHostKeyChecking=no -i key.pem -r ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back + rsync -av --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back - name: Stop running containers run: | From 936c45cd9a30d338c27d1332656f2a756bc9446d Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Wed, 23 Oct 2024 14:20:11 -0300 Subject: [PATCH 07/39] oper: criando arquivo .env --- .github/workflows/deployEC2.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml index 6d603b0..ec9888f 100644 --- a/.github/workflows/deployEC2.yaml +++ b/.github/workflows/deployEC2.yaml @@ -25,6 +25,14 @@ jobs: - name: Create target directory on AWS instance run: | ssh ${{ env.SSH_OPTIONS }} 'mkdir -p /home/ubuntu/tupan-back/' + + - name: Create .env file + run: | + echo "DB_USER=${{ secrets.DB_USERNAME }}" >> .env + echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env + echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env + echo "DB_HOST=localhost" >> .env + echo "DB_PORT=5432" >> .env - name: Transfer code to AWS instance run: | From 64bbd8e27edbc99461e23445e77cdc3f25c601b9 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Wed, 23 Oct 2024 14:37:38 -0300 Subject: [PATCH 08/39] oper: pegando host e porta do banco no secret --- .github/workflows/deployEC2.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml index ec9888f..5ba9f0e 100644 --- a/.github/workflows/deployEC2.yaml +++ b/.github/workflows/deployEC2.yaml @@ -31,8 +31,8 @@ jobs: echo "DB_USER=${{ secrets.DB_USERNAME }}" >> .env echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env - echo "DB_HOST=localhost" >> .env - echo "DB_PORT=5432" >> .env + echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env + echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env - name: Transfer code to AWS instance run: | From 2f119ac7a93e36537358d8cdf5ff34366dd4cf76 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Wed, 23 Oct 2024 14:52:11 -0300 Subject: [PATCH 09/39] oper: rodando migracoes no dockerfile --- Dockerfile | 3 +++ docker-compose.yml | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 45016ce..20d8bd3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,6 +15,9 @@ COPY . /app/ # Expôr a porta que o Django vai usar EXPOSE 8000 +# Comandos para rodar as migrações +CMD ["python", "src/tupan/manage.py", "makemigrations"] +CMD ["python", "src/tupan/manage.py", "migrate"] # Comando para rodar o servidor CMD ["python", "src/tupan/manage.py", "runserver", "0.0.0.0:8000"] diff --git a/docker-compose.yml b/docker-compose.yml index f088f52..89d6e21 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,6 @@ services: web: image: tupan-back build: . - command: python src/tupan/manage.py runserver 0.0.0.0:8000 volumes: - .:/app ports: From 407493c88c18197f46b84398b5a87fab60b7e4c7 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Wed, 30 Oct 2024 10:11:53 -0300 Subject: [PATCH 10/39] corr: fix url do database --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 89d6e21..20d4d8c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,7 +10,7 @@ services: - "8000:8000" environment: - DEBUG=True - - DATABASE_URL=postgres://postgres:fatec@db:5432:/tupan + - DATABASE_URL=postgres://postgres:fatec@db:5432/tupan restart: always db: image: postgres From 33905ec4f42d2c57f64cb64cfe1068eef0b05fd6 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Wed, 30 Oct 2024 10:25:31 -0300 Subject: [PATCH 11/39] corr: adicionando sudo ao comando rsync --- .github/workflows/deployEC2.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml index 5ba9f0e..0d03380 100644 --- a/.github/workflows/deployEC2.yaml +++ b/.github/workflows/deployEC2.yaml @@ -36,7 +36,7 @@ jobs: - name: Transfer code to AWS instance run: | - rsync -av --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back + rsync -av --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" --rsync-path="sudo rsync" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back - name: Stop running containers run: | From 1c787c2a3ac8c54d6985206cd81d181cdf6e4a94 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Mon, 4 Nov 2024 12:18:02 -0300 Subject: [PATCH 12/39] corr: removendo arquivo de migracao antigo --- ...rta_condicao_alter_alerta_nome_and_more.py | 48 ------------------- 1 file changed, 48 deletions(-) delete mode 100644 src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py diff --git a/src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py b/src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py deleted file mode 100644 index 0974190..0000000 --- a/src/tupan/alertas/migrations/0003_alter_alerta_condicao_alter_alerta_nome_and_more.py +++ /dev/null @@ -1,48 +0,0 @@ -# Generated by Django 5.1.1 on 2024-10-11 10:34 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('alertas', '0002_medicao'), - ] - - operations = [ - migrations.AlterField( - model_name='alerta', - name='condicao', - field=models.CharField(help_text='Condição para o alerta acontecer', max_length=4), - ), - migrations.AlterField( - model_name='alerta', - name='nome', - field=models.CharField(help_text='Nome do alerta', max_length=127, unique=True), - ), - migrations.AlterField( - model_name='historicoalerta', - name='timestamp', - field=models.BigIntegerField(help_text='Data/hora do alerta em timestamp'), - ), - migrations.AlterField( - model_name='historicoalerta', - name='timestamp_convertido', - field=models.DateTimeField(blank=True, help_text='Data/hora do alerta em datetime', null=True), - ), - migrations.AlterField( - model_name='medicao', - name='dados', - field=models.CharField(help_text='Valor dos dados da medição', max_length=63), - ), - migrations.AlterField( - model_name='medicao', - name='timestamp', - field=models.BigIntegerField(help_text='Data/hora da medição em timestamp'), - ), - migrations.AlterField( - model_name='medicao', - name='timestamp_convertido', - field=models.DateTimeField(blank=True, help_text='Data/hora da medição em datetime', null=True), - ), - ] From 86ac8dde3f530e140fef9629a973e98651baa015 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Mon, 4 Nov 2024 12:34:51 -0300 Subject: [PATCH 13/39] oper: adicionando opcao para deletar arquivos antigos no comando rsync --- .github/workflows/deployEC2.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml index 0d03380..39a10c7 100644 --- a/.github/workflows/deployEC2.yaml +++ b/.github/workflows/deployEC2.yaml @@ -22,6 +22,10 @@ jobs: echo "${{ env.AWS_PRIVATE_KEY }}" > key.pem chmod 600 key.pem + - name: Stop running containers + run: | + ssh ${{ env.SSH_OPTIONS }} 'if [ -d /home/ubuntu/tupan-back/ ]; then cd /home/ubuntu/tupan-back/ && sudo docker-compose down; fi' + - name: Create target directory on AWS instance run: | ssh ${{ env.SSH_OPTIONS }} 'mkdir -p /home/ubuntu/tupan-back/' @@ -36,15 +40,11 @@ jobs: - name: Transfer code to AWS instance run: | - rsync -av --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" --rsync-path="sudo rsync" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back - - - name: Stop running containers - run: | - ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose down' + rsync -av --delete --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" --rsync-path="sudo rsync" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back - name: Run Docker Compose run: | - ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose up' + ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose up -d' - name: Cleanup SSH key run: | From 6a35afd64bfe84d32e1983a61748c4ac5cd1653b Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Mon, 4 Nov 2024 12:46:28 -0300 Subject: [PATCH 14/39] corr: criando .env apos copiar o codigo --- .github/workflows/deployEC2.yaml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml index 39a10c7..034195a 100644 --- a/.github/workflows/deployEC2.yaml +++ b/.github/workflows/deployEC2.yaml @@ -28,20 +28,20 @@ jobs: - name: Create target directory on AWS instance run: | - ssh ${{ env.SSH_OPTIONS }} 'mkdir -p /home/ubuntu/tupan-back/' - + ssh ${{ env.SSH_OPTIONS }} 'mkdir -p /home/ubuntu/tupan-back/' + + - name: Transfer code to AWS instance + run: | + rsync -av --delete --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" --rsync-path="sudo rsync" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back + - name: Create .env file run: | echo "DB_USER=${{ secrets.DB_USERNAME }}" >> .env echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env - echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env - - - name: Transfer code to AWS instance - run: | - rsync -av --delete --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" --rsync-path="sudo rsync" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back - + echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env + - name: Run Docker Compose run: | ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose up -d' From 7fea2410a5c42b653bce1ec74cc02f64883d6a61 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Mon, 4 Nov 2024 12:49:32 -0300 Subject: [PATCH 15/39] oper: removendo -d do docker-compose up --- .github/workflows/deployEC2.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml index 034195a..d83d12a 100644 --- a/.github/workflows/deployEC2.yaml +++ b/.github/workflows/deployEC2.yaml @@ -41,10 +41,10 @@ jobs: echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env - + - name: Run Docker Compose run: | - ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose up -d' + ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose up' - name: Cleanup SSH key run: | From 89e9be9b3a2981a10032a4d3ad6afa77f929cd58 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Mon, 4 Nov 2024 12:58:10 -0300 Subject: [PATCH 16/39] corr: fix criacao do dotenv --- .github/workflows/deployEC2.yaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml index d83d12a..d48bdcf 100644 --- a/.github/workflows/deployEC2.yaml +++ b/.github/workflows/deployEC2.yaml @@ -28,19 +28,19 @@ jobs: - name: Create target directory on AWS instance run: | - ssh ${{ env.SSH_OPTIONS }} 'mkdir -p /home/ubuntu/tupan-back/' - - - name: Transfer code to AWS instance - run: | - rsync -av --delete --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" --rsync-path="sudo rsync" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back - + ssh ${{ env.SSH_OPTIONS }} 'mkdir -p /home/ubuntu/tupan-back/' + - name: Create .env file run: | echo "DB_USER=${{ secrets.DB_USERNAME }}" >> .env echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env - echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env + echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env + + - name: Transfer code to AWS instance + run: | + rsync -av --delete --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" --rsync-path="sudo rsync" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back - name: Run Docker Compose run: | From fdff4a07320940f1f44eeff528e72ded087362a6 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Mon, 4 Nov 2024 13:01:43 -0300 Subject: [PATCH 17/39] oper: criando entrypoint --- Dockerfile | 7 +++++++ entrypoint.sh | 8 ++++++++ 2 files changed, 15 insertions(+) create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 20d8bd3..23e7cbd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,6 +15,13 @@ COPY . /app/ # Expôr a porta que o Django vai usar EXPOSE 8000 +# Criar um script de entrada +COPY entrypoint.sh /app/entrypoint.sh +RUN chmod +x /app/entrypoint.sh + +# Comando para rodar o script de entrada +CMD ["/app/entrypoint.sh"] + # Comandos para rodar as migrações CMD ["python", "src/tupan/manage.py", "makemigrations"] CMD ["python", "src/tupan/manage.py", "migrate"] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..9c78b48 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Rodar migrações +python src/tupan/manage.py makemigrations +python src/tupan/manage.py migrate + +# Iniciar o servidor +python src/tupan/manage.py runserver 0.0.0.0:8000 \ No newline at end of file From e071958d9367aedcee5fda29d65afb1a81af9b47 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Mon, 4 Nov 2024 13:05:17 -0300 Subject: [PATCH 18/39] oper: copiando entrypoint para o container --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index 23e7cbd..ec098a0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ WORKDIR /app # Copiar o arquivo de requisitos para o container COPY requirements.txt /app/ +COPY entrypoint.sh /app/ # Instalar as dependências RUN pip install --upgrade pip && pip install -r requirements.txt From c9744620d1b3a843e367ffdb421a84f1cccc25fe Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Mon, 4 Nov 2024 13:28:59 -0300 Subject: [PATCH 19/39] oper: adicionando secret key --- .github/workflows/deployEC2.yaml | 1 + entrypoint.sh | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml index d48bdcf..96cf4b0 100644 --- a/.github/workflows/deployEC2.yaml +++ b/.github/workflows/deployEC2.yaml @@ -32,6 +32,7 @@ jobs: - name: Create .env file run: | + echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env echo "DB_USER=${{ secrets.DB_USERNAME }}" >> .env echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env diff --git a/entrypoint.sh b/entrypoint.sh index 9c78b48..cc9df33 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,5 +1,12 @@ #!/bin/bash +# Aguarde o PostgreSQL estar pronto +echo "Aguardando o PostgreSQL estar pronto..." +while ! nc -z db 5432; do + sleep 0.1 # espera 1/10 de segundo +done +echo "PostgreSQL está pronto!" + # Rodar migrações python src/tupan/manage.py makemigrations python src/tupan/manage.py migrate From 167c167547aae5324b2292fbfb3ebf7b4ab7ae64 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Mon, 4 Nov 2024 13:48:07 -0300 Subject: [PATCH 20/39] corr: removendo comandos cmd apos o entrypoint --- Dockerfile | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index ec098a0..87561ce 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,9 +23,3 @@ RUN chmod +x /app/entrypoint.sh # Comando para rodar o script de entrada CMD ["/app/entrypoint.sh"] -# Comandos para rodar as migrações -CMD ["python", "src/tupan/manage.py", "makemigrations"] -CMD ["python", "src/tupan/manage.py", "migrate"] -# Comando para rodar o servidor -CMD ["python", "src/tupan/manage.py", "runserver", "0.0.0.0:8000"] - From ed606fccb487169dd05746e54db102ff3fd47742 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Mon, 4 Nov 2024 14:03:20 -0300 Subject: [PATCH 21/39] oper: adicionando rebuild do compose --- .github/workflows/deployEC2.yaml | 6 +++++- Dockerfile | 6 +++--- entrypoint.sh | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml index 96cf4b0..58f6cae 100644 --- a/.github/workflows/deployEC2.yaml +++ b/.github/workflows/deployEC2.yaml @@ -43,9 +43,13 @@ jobs: run: | rsync -av --delete --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" --rsync-path="sudo rsync" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back + - name: Build Docker Compose + run: | + ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose build' + - name: Run Docker Compose run: | - ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose up' + ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose up -d' - name: Cleanup SSH key run: | diff --git a/Dockerfile b/Dockerfile index 87561ce..55ef5bc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,13 +13,13 @@ RUN pip install --upgrade pip && pip install -r requirements.txt # Copiar o código da aplicação COPY . /app/ -# Expôr a porta que o Django vai usar -EXPOSE 8000 - # Criar um script de entrada COPY entrypoint.sh /app/entrypoint.sh RUN chmod +x /app/entrypoint.sh +# Expôr a porta que o Django vai usar +EXPOSE 8000 + # Comando para rodar o script de entrada CMD ["/app/entrypoint.sh"] diff --git a/entrypoint.sh b/entrypoint.sh index cc9df33..64231ee 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -9,7 +9,7 @@ echo "PostgreSQL está pronto!" # Rodar migrações python src/tupan/manage.py makemigrations -python src/tupan/manage.py migrate +python src/tupan/manage.py migrate --noinput # Iniciar o servidor -python src/tupan/manage.py runserver 0.0.0.0:8000 \ No newline at end of file +exec python src/tupan/manage.py runserver 0.0.0.0:8000 \ No newline at end of file From fa0e7c8428197288edbabc2bd1e99324eecc5db2 Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Mon, 4 Nov 2024 14:19:21 -0300 Subject: [PATCH 22/39] oper: adicionando permissoes ao arquivo entrypoint --- .github/workflows/deployEC2.yaml | 4 ++++ Dockerfile | 3 +++ 2 files changed, 7 insertions(+) diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml index 58f6cae..d329f7d 100644 --- a/.github/workflows/deployEC2.yaml +++ b/.github/workflows/deployEC2.yaml @@ -43,6 +43,10 @@ jobs: run: | rsync -av --delete --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" --rsync-path="sudo rsync" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back + - name: Set permissions for entrypoint + run: | + ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo chmod +x entrypoint.sh' + - name: Build Docker Compose run: | ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose build' diff --git a/Dockerfile b/Dockerfile index 55ef5bc..971e993 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,9 @@ FROM python:3.11 # Definir o diretório de trabalho dentro do container WORKDIR /app +# Instalar netcat +RUN apt-get update && apt-get install -y netcat + # Copiar o arquivo de requisitos para o container COPY requirements.txt /app/ COPY entrypoint.sh /app/ From 376bca899d43297ae63424d14e68cd667de051ba Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Mon, 4 Nov 2024 14:21:32 -0300 Subject: [PATCH 23/39] oper: alterando versao do netcat --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 971e993..eb9b704 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,8 +3,8 @@ FROM python:3.11 # Definir o diretório de trabalho dentro do container WORKDIR /app -# Instalar netcat -RUN apt-get update && apt-get install -y netcat +# Instalar netcat (openbsd) +RUN apt-get update && apt-get install -y netcat-openbsd # Copiar o arquivo de requisitos para o container COPY requirements.txt /app/ From 4b66ddde6fc503d037162b543837848e31da91cf Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 09:56:02 -0300 Subject: [PATCH 24/39] oper: juntando teste com deploy --- .github/workflows/deployEC2.yaml | 60 ------------- .github/workflows/django.yaml | 65 -------------- .github/workflows/testAndDeploy.yaml | 124 +++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 125 deletions(-) delete mode 100644 .github/workflows/deployEC2.yaml delete mode 100644 .github/workflows/django.yaml create mode 100644 .github/workflows/testAndDeploy.yaml diff --git a/.github/workflows/deployEC2.yaml b/.github/workflows/deployEC2.yaml deleted file mode 100644 index d329f7d..0000000 --- a/.github/workflows/deployEC2.yaml +++ /dev/null @@ -1,60 +0,0 @@ -name: Deploy to AWS EC2 -on: - push: - branches: - - main - pull_request: - branches: - - main -env: - AWS_PRIVATE_KEY: ${{ secrets.KEYAWS }} - SSH_OPTIONS: '-o StrictHostKeyChecking=no -i key.pem ubuntu@98.80.44.121' - -jobs: - deploy: - runs-on: ubuntu-24.04 - steps: - - name: Checkout code - uses: actions/checkout@v2 - - - name: Set permissions for private key - run: | - echo "${{ env.AWS_PRIVATE_KEY }}" > key.pem - chmod 600 key.pem - - - name: Stop running containers - run: | - ssh ${{ env.SSH_OPTIONS }} 'if [ -d /home/ubuntu/tupan-back/ ]; then cd /home/ubuntu/tupan-back/ && sudo docker-compose down; fi' - - - name: Create target directory on AWS instance - run: | - ssh ${{ env.SSH_OPTIONS }} 'mkdir -p /home/ubuntu/tupan-back/' - - - name: Create .env file - run: | - echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env - echo "DB_USER=${{ secrets.DB_USERNAME }}" >> .env - echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env - echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env - echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env - echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env - - - name: Transfer code to AWS instance - run: | - rsync -av --delete --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" --rsync-path="sudo rsync" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back - - - name: Set permissions for entrypoint - run: | - ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo chmod +x entrypoint.sh' - - - name: Build Docker Compose - run: | - ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose build' - - - name: Run Docker Compose - run: | - ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose up -d' - - - name: Cleanup SSH key - run: | - rm -f key.pem \ No newline at end of file diff --git a/.github/workflows/django.yaml b/.github/workflows/django.yaml deleted file mode 100644 index ccb249d..0000000 --- a/.github/workflows/django.yaml +++ /dev/null @@ -1,65 +0,0 @@ -name: Django CI - -on: - push: - branches: - - '*' - -jobs: - build: - runs-on: ubuntu-latest - strategy: - max-parallel: 4 - matrix: - python-version: [3.7, 3.8, 3.9] - - services: - postgres: - image: postgres:13 - env: - POSTGRES_USER: ${{ secrets.DB_USERNAME }} - POSTGRES_PASSWORD: ${{ secrets.DB_PASSWORD }} - POSTGRES_DB: ${{ secrets.DB_NAME }} - ports: - - 5432:5432 - options: >- - --health-cmd "pg_isready -U ${{ secrets.DB_USERNAME }}" - --health-interval 10s - --health-timeout 5s - --health-retries 5 - - env: - DB_USERNAME: ${{ secrets.DB_USERNAME }} - DB_PASSWORD: ${{ secrets.DB_PASSWORD }} - DB_NAME: ${{ secrets.DB_NAME }} - DB_HOST: localhost - DB_PORT: 5432 - - steps: - - uses: actions/checkout@v4 - - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v3 - with: - python-version: ${{ matrix.python-version }} - - - name: Install Dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - - - name: Wait for PostgreSQL - run: | - until pg_isready -h ${{ env.DB_HOST }} -p ${{ env.DB_PORT }} -U ${{ secrets.DB_USERNAME }}; do - echo "Waiting for PostgreSQL..."; - sleep 5; - done - - - name: Apply Django Migrations - run: | - python manage.py makemigrations - python manage.py migrate - - - name: Run Tests - run: | - pytest diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml new file mode 100644 index 0000000..9b8b99b --- /dev/null +++ b/.github/workflows/testAndDeploy.yaml @@ -0,0 +1,124 @@ +name: Django CI & Deploy + +on: + push: + branches: + - '*' # Rodar para qualquer push em qualquer branch + pull_request: + branches: + - main # Não é necessário, mas se quiser pode manter para PRs para 'main' + +jobs: + # Job para rodar os testes + build: + runs-on: ubuntu-latest + strategy: + max-parallel: 4 + matrix: + python-version: [3.7, 3.8, 3.9] + + services: + postgres: + image: postgres:13 + env: + POSTGRES_USER: ${{ secrets.DB_USERNAME }} + POSTGRES_PASSWORD: ${{ secrets.DB_PASSWORD }} + POSTGRES_DB: ${{ secrets.DB_NAME }} + ports: + - 5432:5432 + options: >- + --health-cmd "pg_isready -U ${{ secrets.DB_USERNAME }}" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + env: + DB_USERNAME: ${{ secrets.DB_USERNAME }} + DB_PASSWORD: ${{ secrets.DB_PASSWORD }} + DB_NAME: ${{ secrets.DB_NAME }} + DB_HOST: localhost + DB_PORT: 5432 + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + + - name: Install Dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Wait for PostgreSQL + run: | + until pg_isready -h ${{ env.DB_HOST }} -p ${{ env.DB_PORT }} -U ${{ secrets.DB_USERNAME }}; do + echo "Waiting for PostgreSQL..."; + sleep 5; + done + + - name: Apply Django Migrations + run: | + python manage.py makemigrations + python manage.py migrate + + - name: Run Tests + run: | + pytest + + # Job para fazer o deploy no AWS EC2, só é executado se o job de build for bem-sucedido + deploy: + runs-on: ubuntu-24.04 + needs: build # Este job só vai rodar se o job build for bem-sucedido + if: github.ref == 'refs/heads/main' # Executa apenas no push para a branch main + env: + AWS_PRIVATE_KEY: ${{ secrets.KEYAWS }} + SSH_OPTIONS: '-o StrictHostKeyChecking=no -i key.pem ubuntu@98.80.44.121' + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set permissions for private key + run: | + echo "${{ env.AWS_PRIVATE_KEY }}" > key.pem + chmod 600 key.pem + + - name: Stop running containers + run: | + ssh ${{ env.SSH_OPTIONS }} 'if [ -d /home/ubuntu/tupan-back/ ]; then cd /home/ubuntu/tupan-back/ && sudo docker-compose down; fi' + + - name: Create target directory on AWS instance + run: | + ssh ${{ env.SSH_OPTIONS }} 'mkdir -p /home/ubuntu/tupan-back/' + + - name: Create .env file + run: | + echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env + echo "DB_USER=${{ secrets.DB_USERNAME }}" >> .env + echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env + echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env + echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env + echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env + + - name: Transfer code to AWS instance + run: | + rsync -av --delete --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" --rsync-path="sudo rsync" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back + + - name: Set permissions for entrypoint + run: | + ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo chmod +x entrypoint.sh' + + - name: Build Docker Compose + run: | + ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose build' + + - name: Run Docker Compose + run: | + ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose up -d' + + - name: Cleanup SSH key + run: | + rm -f key.pem From 974bd363429a5733b8ac51e6394ae5ace5dab859 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 11:07:17 -0300 Subject: [PATCH 25/39] oper: testando ci/cd --- .github/workflows/testAndDeploy.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index 9b8b99b..7606b79 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -6,7 +6,7 @@ on: - '*' # Rodar para qualquer push em qualquer branch pull_request: branches: - - main # Não é necessário, mas se quiser pode manter para PRs para 'main' + - main jobs: # Job para rodar os testes From 0e2f0b7096e8a20b6ffac081f80a3f427cabd72d Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 11:19:12 -0300 Subject: [PATCH 26/39] corr: arrumando erro do secrets --- .github/workflows/testAndDeploy.yaml | 37 ++++++++++------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index 7606b79..064512d 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -3,13 +3,13 @@ name: Django CI & Deploy on: push: branches: - - '*' # Rodar para qualquer push em qualquer branch + - '*' # Trigger for push in any branch pull_request: branches: - - main + - main # Trigger for pull request to main branch jobs: - # Job para rodar os testes + # Job for running tests build: runs-on: ubuntu-latest strategy: @@ -20,10 +20,6 @@ jobs: services: postgres: image: postgres:13 - env: - POSTGRES_USER: ${{ secrets.DB_USERNAME }} - POSTGRES_PASSWORD: ${{ secrets.DB_PASSWORD }} - POSTGRES_DB: ${{ secrets.DB_NAME }} ports: - 5432:5432 options: >- @@ -32,13 +28,6 @@ jobs: --health-timeout 5s --health-retries 5 - env: - DB_USERNAME: ${{ secrets.DB_USERNAME }} - DB_PASSWORD: ${{ secrets.DB_PASSWORD }} - DB_NAME: ${{ secrets.DB_NAME }} - DB_HOST: localhost - DB_PORT: 5432 - steps: - uses: actions/checkout@v4 @@ -52,9 +41,9 @@ jobs: python -m pip install --upgrade pip pip install -r requirements.txt - - name: Wait for PostgreSQL + - name: Wait for PostgreSQL to be ready run: | - until pg_isready -h ${{ env.DB_HOST }} -p ${{ env.DB_PORT }} -U ${{ secrets.DB_USERNAME }}; do + until pg_isready -h localhost -p 5432 -U ${{ secrets.DB_USERNAME }}; do echo "Waiting for PostgreSQL..."; sleep 5; done @@ -68,19 +57,19 @@ jobs: run: | pytest - # Job para fazer o deploy no AWS EC2, só é executado se o job de build for bem-sucedido + # Job for deploying to AWS EC2, only if the build job is successful deploy: runs-on: ubuntu-24.04 - needs: build # Este job só vai rodar se o job build for bem-sucedido - if: github.ref == 'refs/heads/main' # Executa apenas no push para a branch main + needs: build # This job will only run if the build job is successful + if: github.ref == 'refs/heads/main' # Run only on push to the main branch env: AWS_PRIVATE_KEY: ${{ secrets.KEYAWS }} SSH_OPTIONS: '-o StrictHostKeyChecking=no -i key.pem ubuntu@98.80.44.121' steps: - name: Checkout code - uses: actions/checkout@v2 - + uses: actions/checkout@v4 + - name: Set permissions for private key run: | echo "${{ env.AWS_PRIVATE_KEY }}" > key.pem @@ -101,7 +90,7 @@ jobs: echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env - echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env + echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env - name: Transfer code to AWS instance run: | @@ -110,7 +99,7 @@ jobs: - name: Set permissions for entrypoint run: | ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo chmod +x entrypoint.sh' - + - name: Build Docker Compose run: | ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose build' @@ -118,7 +107,7 @@ jobs: - name: Run Docker Compose run: | ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose up -d' - + - name: Cleanup SSH key run: | rm -f key.pem From d2b310771078738111210816361108ce99a35b02 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 11:29:19 -0300 Subject: [PATCH 27/39] corr: alterando postgres teste de service para step --- .github/workflows/testAndDeploy.yaml | 32 ++++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index 064512d..607a5c8 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -3,10 +3,10 @@ name: Django CI & Deploy on: push: branches: - - '*' # Trigger for push in any branch + - '*' # Trigger on push to any branch pull_request: branches: - - main # Trigger for pull request to main branch + - main # Trigger on pull request to main branch jobs: # Job for running tests @@ -17,17 +17,6 @@ jobs: matrix: python-version: [3.7, 3.8, 3.9] - services: - postgres: - image: postgres:13 - ports: - - 5432:5432 - options: >- - --health-cmd "pg_isready -U ${{ secrets.DB_USERNAME }}" - --health-interval 10s - --health-timeout 5s - --health-retries 5 - steps: - uses: actions/checkout@v4 @@ -41,10 +30,16 @@ jobs: python -m pip install --upgrade pip pip install -r requirements.txt - - name: Wait for PostgreSQL to be ready + - name: Start PostgreSQL service run: | - until pg_isready -h localhost -p 5432 -U ${{ secrets.DB_USERNAME }}; do - echo "Waiting for PostgreSQL..."; + docker run --name postgres-db -d \ + -e POSTGRES_USER=${{ secrets.DB_USERNAME }} \ + -e POSTGRES_PASSWORD=${{ secrets.DB_PASSWORD }} \ + -e POSTGRES_DB=${{ secrets.DB_NAME }} \ + -p 5432:5432 postgres:13 + # Wait until PostgreSQL is ready + until docker exec postgres-db pg_isready -U ${{ secrets.DB_USERNAME }}; do + echo "Waiting for PostgreSQL to be ready..."; sleep 5; done @@ -57,6 +52,11 @@ jobs: run: | pytest + - name: Stop PostgreSQL container + run: | + docker stop postgres-db + docker rm postgres-db + # Job for deploying to AWS EC2, only if the build job is successful deploy: runs-on: ubuntu-24.04 From 81969c1f041b9a2b2cc801df3f9b608094a8dd44 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 11:32:13 -0300 Subject: [PATCH 28/39] oper: teste action --- .github/workflows/testAndDeploy.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index 607a5c8..02fc5ea 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -9,7 +9,6 @@ on: - main # Trigger on pull request to main branch jobs: - # Job for running tests build: runs-on: ubuntu-latest strategy: From b3f248b434e95af0a2f1afe9963cf6ef83dd1433 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 11:38:56 -0300 Subject: [PATCH 29/39] oper: add python 3.10 na matrix --- .github/workflows/testAndDeploy.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index 02fc5ea..035f142 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -3,7 +3,7 @@ name: Django CI & Deploy on: push: branches: - - '*' # Trigger on push to any branch + - * # Trigger on push to any branch pull_request: branches: - main # Trigger on pull request to main branch @@ -14,7 +14,7 @@ jobs: strategy: max-parallel: 4 matrix: - python-version: [3.7, 3.8, 3.9] + python-version: [3.7, 3.8, 3.9, 3.10] steps: - uses: actions/checkout@v4 From da79d6bfad25b9d7e0430389bc30096ea92b2834 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 11:42:19 -0300 Subject: [PATCH 30/39] oper: alterando para action rodar em branches que contem / --- .github/workflows/testAndDeploy.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index 035f142..601fb6a 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -3,7 +3,7 @@ name: Django CI & Deploy on: push: branches: - - * # Trigger on push to any branch + - '**' # Trigger on push to any branch pull_request: branches: - main # Trigger on pull request to main branch From 554e676b006764d91047e775cee7ba76eeaf745e Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 11:44:14 -0300 Subject: [PATCH 31/39] oper: alterando python 3.10 para 3.12 na action --- .github/workflows/testAndDeploy.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index 601fb6a..d604a8a 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -14,7 +14,7 @@ jobs: strategy: max-parallel: 4 matrix: - python-version: [3.7, 3.8, 3.9, 3.10] + python-version: [3.7, 3.8, 3.9, 3.12] steps: - uses: actions/checkout@v4 From d61b5958a3c84bd91d57f81c9aa72f687d8a3384 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 11:45:49 -0300 Subject: [PATCH 32/39] oper: alterando para rodar testes apenas na versao 3.12 --- .github/workflows/testAndDeploy.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index d604a8a..9827156 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -14,7 +14,7 @@ jobs: strategy: max-parallel: 4 matrix: - python-version: [3.7, 3.8, 3.9, 3.12] + python-version: [3.12] steps: - uses: actions/checkout@v4 From bd7dbfef51c1d3053d17baa40f9531c1a07dabf5 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 11:49:15 -0300 Subject: [PATCH 33/39] corr: corrigindo caminho do script que roda as migracoes --- .github/workflows/testAndDeploy.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index 9827156..d3d4450 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -44,8 +44,8 @@ jobs: - name: Apply Django Migrations run: | - python manage.py makemigrations - python manage.py migrate + python src/tupan/manage.py makemigrations + python src/tupan/manage.py migrate - name: Run Tests run: | From d5f95a1f66c96831497b499f42fa0234b5fa89a9 Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 11:53:34 -0300 Subject: [PATCH 34/39] oper: criando .env na maquina de teste do github --- .github/workflows/testAndDeploy.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index d3d4450..e5ee462 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -41,6 +41,15 @@ jobs: echo "Waiting for PostgreSQL to be ready..."; sleep 5; done + + - name: Create .env file on git machine + run: | + echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env + echo "DB_USER=${{ secrets.DB_USERNAME }}" >> .env + echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env + echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env + echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env + echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env - name: Apply Django Migrations run: | From 7b4cde935ca7a18f024b694625446789acaa9eff Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 11:57:08 -0300 Subject: [PATCH 35/39] corr: alterando para rodar teste no banco localhost --- .github/workflows/testAndDeploy.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index e5ee462..84b4ca0 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -48,7 +48,7 @@ jobs: echo "DB_USER=${{ secrets.DB_USERNAME }}" >> .env echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env - echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env + echo "DB_HOST=localhost" >> .env echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env - name: Apply Django Migrations From 720419461d9915c7b01291a137c0f179187e73ce Mon Sep 17 00:00:00 2001 From: Iago Souza Date: Tue, 5 Nov 2024 12:00:15 -0300 Subject: [PATCH 36/39] oper: trocando para requirements-dev na action de teste --- .github/workflows/testAndDeploy.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index 84b4ca0..dde31ab 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -27,7 +27,7 @@ jobs: - name: Install Dependencies run: | python -m pip install --upgrade pip - pip install -r requirements.txt + pip install -r requirements-dev.txt - name: Start PostgreSQL service run: | From 15c88a65968612aa34c3b193be956269cbd6296f Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Tue, 5 Nov 2024 20:42:00 -0300 Subject: [PATCH 37/39] oper: rodando pytest na pasta correta --- .github/workflows/testAndDeploy.yaml | 3 +- src/tupan/alertas/tests.py | 71 ---------------------------- 2 files changed, 1 insertion(+), 73 deletions(-) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index dde31ab..9364003 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -15,7 +15,6 @@ jobs: max-parallel: 4 matrix: python-version: [3.12] - steps: - uses: actions/checkout@v4 @@ -58,7 +57,7 @@ jobs: - name: Run Tests run: | - pytest + cd src/tupan && pytest - name: Stop PostgreSQL container run: | diff --git a/src/tupan/alertas/tests.py b/src/tupan/alertas/tests.py index 51eb514..a1ace31 100644 --- a/src/tupan/alertas/tests.py +++ b/src/tupan/alertas/tests.py @@ -1,73 +1,2 @@ import pytest -import json -from django.urls import reverse -from .models import Alerta, HistoricoAlerta -@pytest.fixture -def alerta_auxiliar(): - alerta = { - "nome": "Alerta1", - "condicao": "<2", - } - return alerta - - -class TestAlerta: - @pytest.mark.django_db - def teste_criar_alerta(self, alerta_auxiliar): - Alerta.objects.create(**alerta_auxiliar) - - assert Alerta.objects.count() == 1 - alerta_no_banco = Alerta.objects.first() - - assert alerta_no_banco.pk == 1 - assert alerta_no_banco.nome == alerta_auxiliar['nome'] - assert alerta_no_banco.condicao == alerta_auxiliar['condicao'] - assert alerta_no_banco.ativo == True - - @pytest.mark.django_db - def teste_url_listar_alertas(self, client, alerta_auxiliar): - Alerta.objects.create(**alerta_auxiliar) - - url = reverse("alertas") - response = client.get(url) - - json_data = response.json() - - assert response.status_code == 200 - assert len(json_data) == 1 - assert json_data[0]['nome'] == "Alerta1" - assert json_data[0]['condicao'] == "<2" - - @pytest.mark.django_db - def teste_url_cadastrar_alerta(self, client, alerta_auxiliar): - - url = reverse("alertas") - response = client.post(url, data=json.dumps(alerta_auxiliar), content_type="application/json") - - json_data = response.json() - assert response.status_code == 201 - assert json_data['nome'] == 'Alerta1' - assert json_data['ativo'] == True - - -class TestHistoricoAlerta: - @pytest.fixture - def historico_alerta_auxiliar(self, alerta_auxiliar): - alerta = Alerta.objects.create(**alerta_auxiliar) - hist = { - "timestamp": 1726067292, - "alerta": alerta - } - return hist - - @pytest.mark.django_db - def teste_criar_historico(self, historico_alerta_auxiliar): - HistoricoAlerta.objects.create(**historico_alerta_auxiliar) - - assert HistoricoAlerta.objects.count() == 1 - historico_no_banco = HistoricoAlerta.objects.first() - - assert historico_no_banco.pk == 1 - assert historico_no_banco.timestamp == historico_alerta_auxiliar['timestamp'] - assert historico_no_banco.alerta == historico_alerta_auxiliar['alerta'] From 91c542d646596b1f3b6a95000c8dee1990d247da Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Tue, 5 Nov 2024 20:48:46 -0300 Subject: [PATCH 38/39] oper: rodando actions apenas no push --- .github/workflows/testAndDeploy.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/testAndDeploy.yaml b/.github/workflows/testAndDeploy.yaml index 9364003..66f9bca 100644 --- a/.github/workflows/testAndDeploy.yaml +++ b/.github/workflows/testAndDeploy.yaml @@ -4,9 +4,6 @@ on: push: branches: - '**' # Trigger on push to any branch - pull_request: - branches: - - main # Trigger on pull request to main branch jobs: build: From ae3a45ac969f7afbbefde3f4edee030bf2bbc68d Mon Sep 17 00:00:00 2001 From: IagoSouza Date: Tue, 5 Nov 2024 21:28:52 -0300 Subject: [PATCH 39/39] oper: add teste de integracao --- src/tupan/usuarios/tests.py | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/tupan/usuarios/tests.py b/src/tupan/usuarios/tests.py index 9620999..999c30a 100644 --- a/src/tupan/usuarios/tests.py +++ b/src/tupan/usuarios/tests.py @@ -1,17 +1,18 @@ +import json import pytest from .models import Usuario +from rest_framework.test import APIClient class TestUsuario: - @pytest.mark.django_db def test_criacao_usuarios(self): user1 = Usuario.objects.create_user(email='user@gmail.com', password='senha1') user2 = Usuario.objects.create_user(email='user2@gmail.com', password='senha2') - + assert Usuario.objects.count() == 2 assert user1.email == 'user@gmail.com' assert user2.email == 'user2@gmail.com' - + assert user1.check_password("senha1") assert user2.check_password("senha2") @@ -22,25 +23,37 @@ def test_criacao_usuarios(self): def test_listagem_usuarios(self): user1 = Usuario.objects.create_user(email='user@gmail.com', password='senha1') user2 = Usuario.objects.create_user(email='user2@gmail.com', password='senha2') - + usuarios = Usuario.objects.all() assert len(usuarios) == 2 assert user1 in usuarios assert user2 in usuarios - + @pytest.mark.django_db def test_inativar_usuario(self): user = Usuario.objects.create_user(email="user@gmail.com", password="123123") user.ativo = False user.save() assert user.ativo == False - + @pytest.mark.django_db - def test_atualizar_usuario(self): + def test_atualizar_usuario(self, client): user = Usuario.objects.create_user(email="user@gmail.com", password="userpass") - user.email = "test@gmail.com" - user.password = "passwd" - user.save() + client = APIClient() + client.force_authenticate(user=user) + + atualizacao = { + "email": "test@gmail.com", + "password": "userpass1" + } + url = f"/usuarios/{user.pk}" + + response = client.put( + url, + data=json.dumps(atualizacao), + content_type="application/json" + ) + response_data = response.json() - assert user.email == "test@gmail.com" - assert user.password == "passwd" \ No newline at end of file + assert response.status_code == 200 + assert response_data["email"] == "test@gmail.com" \ No newline at end of file