From a64cc9481be25e767d9b7c990c96dfe08805600d Mon Sep 17 00:00:00 2001 From: root Date: Fri, 11 Oct 2024 15:18:10 +0000 Subject: [PATCH 1/7] 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 2/7] =?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 3/7] 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 4/7] 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 5/7] 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 6/7] 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 7/7] 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: |