Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

4_Test_Branch #81

Open
wants to merge 98 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
b5b2f2d
(add new apps + add models) + make migration + add apps to settings
loutreceleste Mar 26, 2024
bb7e272
erase oc_lettings files + (changes urls + html) + mage migrations
loutreceleste Mar 27, 2024
d8df3c3
Merge branch 'chore/improved_modular_architecture'
loutreceleste Mar 28, 2024
b875852
add 404 and 500 error pages + correct flake8 issues + correct admin page
loutreceleste Mar 28, 2024
c71877f
Auto stash before merge of "master" and "chore/improved_modular_archi…
loutreceleste Mar 28, 2024
88a5506
add views and urls unit tests + add setup coverage
loutreceleste Mar 28, 2024
2f78ae2
add doc-strings + made some adjustments
loutreceleste Mar 29, 2024
55fbd55
Merge branch 'chore/reduction_of_various_problems'
loutreceleste Mar 29, 2024
f1ef293
add docker files + set parameters
loutreceleste Apr 2, 2024
be069dd
add pipeline setup
loutreceleste Apr 2, 2024
4a037b9
adjust pipeline
loutreceleste Apr 2, 2024
505cbe2
adjust pipeline
loutreceleste Apr 2, 2024
70e327a
adjust pipeline
loutreceleste Apr 2, 2024
5e9cbaf
adjust pipeline
loutreceleste Apr 2, 2024
1f389f3
adjust pipeline
loutreceleste Apr 2, 2024
e77203f
adjust pipeline
loutreceleste Apr 2, 2024
65310b7
adjust pipeline
loutreceleste Apr 3, 2024
8e1665f
adjust pipeline
loutreceleste Apr 3, 2024
1c05b21
adjust pipeline
loutreceleste Apr 5, 2024
2e35285
adjust pipeline
loutreceleste Apr 5, 2024
d45623e
add SAST to pipeline
loutreceleste Apr 5, 2024
beaf73d
adjust bandit config
loutreceleste Apr 5, 2024
88f4012
adjust bandit config
loutreceleste Apr 5, 2024
7e6ed9b
adjust bandit config
loutreceleste Apr 5, 2024
6210c64
adjust bandit config
loutreceleste Apr 5, 2024
6f51988
remouve SAST + add docker_image
loutreceleste Apr 5, 2024
cdfcc04
add deployment in pipeline
loutreceleste Apr 5, 2024
b0b323c
final pipeline and adjustments
loutreceleste Apr 5, 2024
21a12b8
Merge branch 'chore/set_up_CI_CD_pipeline_and_deployment'
loutreceleste Apr 5, 2024
04507cd
test new pipeline CI
loutreceleste Apr 8, 2024
9cf5ff8
adjust test new pipeline CI
loutreceleste Apr 8, 2024
466fc17
adjust test new pipeline CI
loutreceleste Apr 8, 2024
ea447b8
adjust test new pipeline CI
loutreceleste Apr 8, 2024
dd06489
adjust test new pipeline CI
loutreceleste Apr 8, 2024
f3d1604
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
12a80af
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
a864f9f
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
6a8efba
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
160b55b
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
34a54eb
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
a5dbc39
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
9be7a1e
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
a42bcd1
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
74bff48
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
bfcde5f
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
79ad313
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
e508d19
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
5083d9d
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
224cee8
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
0fd93e6
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
cd06568
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
c6edc73
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
f56894a
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
7025d3e
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
884deb8
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
a7fb528
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
e6d860b
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
d938d0d
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
2cc24d4
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
587df74
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
266afe5
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
91f2dac
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
b896b1b
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
47b22a3
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
b8714d5
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
d13a74f
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
0b42989
adjust test new pipeline CI/CD
loutreceleste Apr 8, 2024
9ee434a
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
55c3ecb
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
c34a89a
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
fa40eb7
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
13bce86
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
20b7ad7
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
da6e3ac
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
5d7dc0e
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
e282b1b
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
a30142b
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
120372f
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
b5b7d71
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
1c91507
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
5a3cea6
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
c35b846
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
89b0046
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
1b4475c
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
5de336d
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
56b7219
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
dbff89b
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
fcc6a02
adjust test new pipeline CI/CD
loutreceleste Apr 9, 2024
2331f5c
adding if conditions to deployment + create .zshrc file
loutreceleste Apr 9, 2024
5ef47c8
add unique command
loutreceleste Apr 9, 2024
cab489d
Merge branch 'chore/set_up_CI_CD_pipeline_and_deployment_V2'
loutreceleste Apr 9, 2024
1dacfba
add more environment variables + add unique command to README
loutreceleste Apr 11, 2024
ae16e1b
Merge branch 'chore/test_branch'
loutreceleste Apr 11, 2024
70b0e1f
adapt if conditions
loutreceleste Apr 11, 2024
d72e65e
Merge branch 'chore/2_test_branch'
loutreceleste Apr 11, 2024
32563d4
adapt if conditions
loutreceleste Apr 11, 2024
c4dcd02
Merge branch 'chore/3_test_branch'
loutreceleste Apr 11, 2024
087eb66
adapt if conditions
loutreceleste Apr 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.git
.env
99 changes: 99 additions & 0 deletions .github/workflows/oc_lettings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: GitHub Actions Integration

on:
push:
branches:
- '**'

jobs:
compilations_and_tests:
runs-on: ubuntu-latest
if: github.event_name == 'push'

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install Python and dependencies
run: |
sudo apt update
sudo apt install python3-pip
pip3 install -r requirements.txt
pip3 install pytest pytest-cov

- name: Run Tests whit Coverage
run: pytest

- name: Run Linting
run: flake8

containerization_and_push:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
needs: compilations_and_tests

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Build Docker image to ECR and DockerHub
run: |
cd $(git rev-parse --show-toplevel) && COMMIT_HASH=$(git rev-parse --short HEAD)
docker build -t python-oc-lettings-fr:$COMMIT_HASH -f Dockerfile .

- name: Push Docker image to ECR
run: |
cd $(git rev-parse --show-toplevel) && COMMIT_HASH=$(git rev-parse --short HEAD)
aws ecr-public get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_URL_REPOSITORY
docker tag python-oc-lettings-fr:$COMMIT_HASH $ECR_URL_REPOSITORY:$COMMIT_HASH
docker push $ECR_URL_REPOSITORY:$COMMIT_HASH
env:
ECR_URL_REPOSITORY: ${{ secrets.ECR_URL_REPOSITORY }}
AWS_REGION: ${{ secrets.AWS_REGION }}

- name: Push Docker image to DockerHub and to local
run: |
cd $(git rev-parse --show-toplevel) && COMMIT_HASH=$(git rev-parse --short HEAD)
echo "$DOCKERHUB_PASSWORD" | docker login -u $DOCKERHUB_USERNAME --password-stdin
docker tag python-oc-lettings-fr:$COMMIT_HASH $DOCKERHUB_USERNAME/python-oc-lettings-fr:$COMMIT_HASH
docker push $DOCKERHUB_USERNAME/python-oc-lettings-fr:$COMMIT_HASH
docker pull $DOCKERHUB_USERNAME/python-oc-lettings-fr:$COMMIT_HASH
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}


build:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
needs: containerization_and_push

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Update ECS Task Definition
run: |
cd $(git rev-parse --show-toplevel) && COMMIT_HASH=$(git rev-parse --short HEAD)
TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition lettings-oc-docker --region $AWS_REGION)
NEW_TASK_DEFINITION=$(echo "$TASK_DEFINITION" | jq --arg IMAGE $ECR_URL_REPOSITORY:$COMMIT_HASH '.taskDefinition | .containerDefinitions[0].image = $IMAGE | del(.taskDefinitionArn) | del(.revision) | del(.status) | del(.requiresAttributes) | del(.compatibilities) | del(.registeredAt) | del(.registeredBy)')
NEW_TASK_INFO=$(aws ecs register-task-definition --region $AWS_REGION --cli-input-json "$NEW_TASK_DEFINITION")
NEW_REVISION=$(echo $NEW_TASK_INFO | jq '.taskDefinition.revision')
aws ecs update-service --cluster lettings-fr --service python-oc-lettings-fr --task-definition lettings-oc-docker:$NEW_REVISION --force-new-deployment --region $AWS_REGION
env:
ECR_URL_REPOSITORY: ${{ secrets.ECR_URL_REPOSITORY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/__pycache__
*.pyc
venv
.idea
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.10

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -75,3 +75,51 @@ Utilisation de PowerShell, comme ci-dessus sauf :

- Pour activer l'environnement virtuel, `.\venv\Scripts\Activate.ps1`
- Remplacer `which <my-command>` par `(Get-Command <my-command>).Path`

### Déploiement

Ce projet utilise GitHub Actions pour automatiser le déploiement de l'application sur ECS

#### Fonctionnement du déploiement

1. Lorsqu'un push est créé sur n'importe quelle branche, les jobs suivants sont déclenchées :
- compilations_and_tests : compile et teste le code.
2. Lorsqu'une pull request est créée sur la branche master, les jobs suivants sont déclenchés :
- compilations_and_tests : compile et teste le code.
- containerization_and_push : construit une image Docker, puis la pousse vers ECR et DockerHub.
- build : met à jour la tâche ECS avec la nouvelle image Docker et déploie l'application mise à jour.

#### Configuration requise

Pour que le déploiement fonctionne correctement, vous devez disposer des éléments suivants :

1. Un compte AWS avec les autorisations pour Amazon ECS et Amazon ECR.
2. Un compte Docker Hub pour stocker les images Docker.
3. Les secrets GitHub :
- AWS_ACCESS_KEY_ID : votre clé d'accès AWS.
- AWS_SECRET_ACCESS_KEY : votre clé secrète AWS.
- ECR_URL_REPOSITORY: l'url' de votre repository sur ECR.
- AWS_REGION: la region associé a votre compte AWS.
- DOCKERHUB_PASSWORD : le mot de passe de votre compte Docker Hub.
- DOCKERHUB_USERNAME : l'identifiant de votre compte Docker Hub.
4. Un cluster Amazon ECS avec un service comprenant la définition de tâche lettings-oc-docker.

#### Étapes de déploiement

1. Assurez-vous davoir la configuration requise.
2. Créez une nouvelle branche à partir de la branche main.
3. Apportez les modifications souhaitées au code et validez-les dans votre nouvelle branche.
4. Créez une pull request à partir de votre branche vers la branche master.
5. Attendez que les GitHub Actions se terminent et vérifiez que le déploiement a réussi.
6. Une fois le déploiement réussi, vous pouvez fusionner la pull request dans la branche main.

#### Commande unique de déploiement
Pour déployer votre site en une seule commande, suivez les étapes suivantes :

1. Assurez-vous de vous trouver dans le dossier racine du projet.
2. Assurez-vous de posséder un compte Docker Hub.
3. Tapez la commande suivante dans le terminal : `./deploy.sh`

Cette commande construira une image Docker de votre site avec le tag du hash du commit actuel, la poussera vers votre compte Docker Hub et la tirera localement pour que vous puissiez exécuter le site en utilisant l'image Docker.

Vous disposez maintenant dans votre Docker Hub et localement de l'image de votre site actuel.
19 changes: 19 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

deploy_docker() {
cd $(git rev-parse --show-toplevel) && COMMIT_HASH=$(git rev-parse --short HEAD)
export DOCKER_USERNAME="$1"
export DOCKER_PASSWORD="$2"
docker build -t python-oc-lettings-fr:$COMMIT_HASH -f Dockerfile .
docker login -u "$DOCKER_USERNAME" -p "$DOCKER_PASSWORD"
docker push $DOCKER_USERNAME/python-oc-lettings-fr:$COMMIT_HASH
docker pull $DOCKER_USERNAME/python-oc-lettings-fr:$COMMIT_HASH
}

echo "Entrez votre nom d'utilisateur Docker : "
read DOCKER_USERNAME
echo "Entrez votre mot de passe Docker : "
read -s DOCKER_PASSWORD

deploy_docker "$DOCKER_USERNAME" "$DOCKER_PASSWORD"

File renamed without changes.
7 changes: 7 additions & 0 deletions lettings/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from .models import Letting, Address

"""This code registers the Letting and Address models with the Django admin interface."""

admin.site.register(Letting)
admin.site.register(Address)
14 changes: 14 additions & 0 deletions lettings/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.apps import AppConfig


class LettingsConfig(AppConfig):
"""Configuration class for the 'lettings' app.

This class defines configuration settings for the 'lettings' app.
It primarily sets the name of the app.

Attributes:
name (str): The name of the 'lettings' app.
"""

name = 'lettings'
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Generated by Django 3.0 on 2020-06-14 09:35
# Generated by Django 3.0 on 2024-03-26 13:33

from django.conf import settings
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
@@ -11,7 +10,6 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
@@ -27,20 +25,12 @@ class Migration(migrations.Migration):
('country_iso_code', models.CharField(max_length=3, validators=[django.core.validators.MinLengthValidator(3)])),
],
),
migrations.CreateModel(
name='Profile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('favorite_city', models.CharField(blank=True, max_length=64)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Letting',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=256)),
('address', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='oc_lettings_site.Address')),
('address', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='lettings.Address')),
],
),
]
21 changes: 21 additions & 0 deletions lettings/migrations/0002_auto_20240326_1424.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.0 on 2024-03-26 14:24

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('lettings', '0001_initial'),
]

operations = [
migrations.RenameModel(
old_name='Address',
new_name='NewAddress',
),
migrations.RenameModel(
old_name='Letting',
new_name='NewLetting',
),
]
29 changes: 29 additions & 0 deletions lettings/migrations/0003_auto_20240326_1507.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.db import migrations


def new_tables(apps, schema_editor):
old_address_table = apps.get_model('oc_lettings_site', 'Address')
new_address_table = apps.get_model('lettings', 'NewAddress')
old_letting_table = apps.get_model('oc_lettings_site', 'Letting')
new_letting_table = apps.get_model('lettings', 'NewLetting')

old_address_datas = old_address_table.objects.all()
old_letting_datas = old_letting_table.objects.all()

for data in old_address_datas:
new_letting_data = new_address_table(id=data.id, number=data.number, street=data.street, city=data.city, state=data.state, zip_code=data.zip_code, country_iso_code=data.country_iso_code)
new_letting_data.save()

for data in old_letting_datas:
new_letting_data = new_letting_table(id=data.id, title=data.title, address_id=data.address_id)
new_letting_data.save()

class Migration(migrations.Migration):

dependencies = [
('lettings', '0002_auto_20240326_1424'),
]

operations = [
migrations.RunPython(new_tables),
]
21 changes: 21 additions & 0 deletions lettings/migrations/0004_auto_20240327_1335.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.0 on 2024-03-27 13:35

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('lettings', '0003_auto_20240326_1507'),
]

operations = [
migrations.RenameModel(
old_name='NewAddress',
new_name='Address',
),
migrations.RenameModel(
old_name='NewLetting',
new_name='Letting',
),
]
17 changes: 17 additions & 0 deletions lettings/migrations/0005_auto_20240328_1224.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.0 on 2024-03-28 12:24

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('lettings', '0004_auto_20240327_1335'),
]

operations = [
migrations.AlterModelOptions(
name='address',
options={'verbose_name': 'Address', 'verbose_name_plural': 'Addresses'},
),
]
Empty file added lettings/migrations/__init__.py
Empty file.
Loading