diff --git a/.gitignore b/.gitignore index c6bba59..39efa4c 100644 --- a/.gitignore +++ b/.gitignore @@ -128,3 +128,12 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* + +# Opentofu state +opentofu/*/.terraform +opentofu/*/.terraform.lock* +opentofu/*/terraform.tfstate* +opentofu/*/terraform.tfvars + +# Opentofu auth config +opentofu/auth_config.json \ No newline at end of file diff --git a/opentofu/README.md b/opentofu/README.md new file mode 100644 index 0000000..5e17e7e --- /dev/null +++ b/opentofu/README.md @@ -0,0 +1,39 @@ +# Déploiement avec Opentofu + +## Microsoft Azure + +### Installer opentofu + +https://opentofu.org/docs/intro/install/ + +### Installer Azure CLI + +https://learn.microsoft.com/en-us/cli/azure/install-azure-cli#install + +### Modifier les configurations + +Créer un fichier **terraform.tfvars** sur la base du fichier **terraform.tfvars.example** dans le répertoire **azure**. +Vous pouvez changer toutes les variables utilisée lors du déploiement dans ce fichier. +Toutes les variables, leur description et leur valeur par défaut sont disponibles dans le fichier **variables.tf**. + +Créer un fichier **auth_config.json** sur la base du fichier **auth_config.json.example** dans le répertoire **opentofu**. + +Modifier le fichier **default.conf** afin de pointer vers le bon url pour le backend et le frontend. +L'url du frontend est défini comme suit: http://\.\.azurecontainer.io:\". +L'url du backend est défini comme suit: http://\.\.azurecontainer.io:\". +Location est sans espace et en minuscule. +Par défaut, l'url du frontend est http://evaluetonsavoir-app.canadacentral.azurecontainer.io:5173. +Par défaut, l'url du backend est http://evaluetonsavoir-app.canadacentral.azurecontainer.io:3000. + +### Lancer le déploiement + +Pour lancer le déploiement, faites les commandes suivantes + +`cd azure` +`az login` +`tofu init` +`tofu apply` + +Ensuite, opentofu va afficher toutes les actions qu'il va effectuer avec les valeurs configurées. +Entrez `yes` pour appliquer ces actions et lancer le déploiement. + diff --git a/opentofu/auth_config.json.example b/opentofu/auth_config.json.example new file mode 100644 index 0000000..6e15147 --- /dev/null +++ b/opentofu/auth_config.json.example @@ -0,0 +1,35 @@ +{ + auth: { + passportjs: [ + { + provider1: { + type: "oauth", + OAUTH_AUTHORIZATION_URL: "https://www.testurl.com/oauth2/authorize", + OAUTH_TOKEN_URL: "https://www.testurl.com/oauth2/token", + OAUTH_USERINFO_URL: "https://www.testurl.com/oauth2/userinfo/", + OAUTH_CLIENT_ID: "your_oauth_client_id", + OAUTH_CLIENT_SECRET: "your_oauth_client_secret", + OAUTH_ADD_SCOPE: "scopes", + OAUTH_ROLE_TEACHER_VALUE: "teacher-claim-value", + OAUTH_ROLE_STUDENT_VALUE: "student-claim-value", + }, + }, + { + provider2: { + type: "oidc", + OIDC_CLIENT_ID: "your_oidc_client_id", + OIDC_CLIENT_SECRET: "your_oidc_client_secret", + OIDC_CONFIG_URL: "https://your-issuer.com", + OIDC_ADD_SCOPE: "groups", + OIDC_ROLE_TEACHER_VALUE: "teacher-claim-value", + OIDC_ROLE_STUDENT_VALUE: "student-claim-value", + }, + }, + ], + "simpleauth": { + enabled: true, + name: "provider3", + SESSION_SECRET: "your_session_secret", + }, + }, +} \ No newline at end of file diff --git a/opentofu/azure/app.tf b/opentofu/azure/app.tf new file mode 100644 index 0000000..0aab5ef --- /dev/null +++ b/opentofu/azure/app.tf @@ -0,0 +1,64 @@ +resource "azurerm_container_group" "app" { + name = var.container_group_app_name + location = azurerm_resource_group.resource_group.location + resource_group_name = azurerm_resource_group.resource_group.name + os_type = var.container_group_os + dns_name_label = var.container_group_app_dns + + image_registry_credential { + server = var.image_registry_server + username = var.image_registry_user + password = var.image_registry_password + } + + container { + name = var.frontend_image_name + image = var.frontend_image + cpu = var.frontend_image_cpu + memory = var.frontend_image_memory + + environment_variables = { + VITE_BACKEND_URL = "http://${var.container_group_router_dns}.${lower(replace(azurerm_resource_group.resource_group.location, " ", ""))}.azurecontainer.io" + } + + ports { + port = var.frontend_port + } + } + + container { + name = var.backend_image_name + image = var.backend_image + cpu = var.backend_image_cpu + memory = var.backend_image_memory + + environment_variables = { + PORT = var.backend_port + MONGO_URI = azurerm_cosmosdb_account.cosmosdb_account.connection_strings[0] + MONGO_DATABASE = azurerm_cosmosdb_mongo_collection.cosmosdb_mongo_collection.database_name + EMAIL_SERVICE = var.backend_email_service + SENDER_EMAIL = var.backend_email_sender + EMAIL_PSW = var.backend_email_password + JWT_SECRET = var.backend_jwt_secret + SESSION_Secret = var.backend_session_secret + SITE_URL = "http://${var.container_group_router_dns}.${lower(replace(azurerm_resource_group.resource_group.location, " ", ""))}.azurecontainer.io" + FRONTEND_PORT = var.frontend_port + USE_PORTS = var.backend_use_port + AUTHENTICATED_ROOMS = var.backend_use_auth_student + } + + ports { + port = var.backend_port + } + + volume { + name = azurerm_storage_share.backend_storage_share.name + mount_path = var.backend_volume_mount_path + share_name = azurerm_storage_share.backend_storage_share.name + storage_account_name = azurerm_storage_account.storage_account.name + storage_account_key = azurerm_storage_account.storage_account.primary_access_key + } + } + + depends_on = [azurerm_cosmosdb_mongo_collection.cosmosdb_mongo_collection] +} diff --git a/opentofu/azure/database.tf b/opentofu/azure/database.tf new file mode 100644 index 0000000..ba0858f --- /dev/null +++ b/opentofu/azure/database.tf @@ -0,0 +1,36 @@ +resource "azurerm_cosmosdb_account" "cosmosdb_account" { + name = var.cosmosdb_account_name + resource_group_name = azurerm_resource_group.resource_group.name + location = azurerm_resource_group.resource_group.location + offer_type = "Standard" + kind = "MongoDB" + + capabilities { + name = "EnableMongo" + } + + consistency_policy { + consistency_level = "Session" + } + + geo_location { + failover_priority = 0 + location = azurerm_resource_group.resource_group.location + } + + depends_on = [azurerm_resource_group.resource_group] +} + +resource "azurerm_cosmosdb_mongo_collection" "cosmosdb_mongo_collection" { + name = var.mongo_database_name + resource_group_name = azurerm_resource_group.resource_group.name + account_name = azurerm_cosmosdb_account.cosmosdb_account.name + database_name = var.mongo_database_name + + index { + keys = ["_id"] + unique = true + } + + depends_on = [azurerm_cosmosdb_account.cosmosdb_account] +} \ No newline at end of file diff --git a/opentofu/azure/main.tf b/opentofu/azure/main.tf new file mode 100644 index 0000000..9cc6ae4 --- /dev/null +++ b/opentofu/azure/main.tf @@ -0,0 +1,13 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~> 3.0" + } + } + required_version = ">= 1.0" +} + +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/opentofu/azure/resource_group.tf b/opentofu/azure/resource_group.tf new file mode 100644 index 0000000..3b29d37 --- /dev/null +++ b/opentofu/azure/resource_group.tf @@ -0,0 +1,4 @@ +resource "azurerm_resource_group" "resource_group" { + name = var.resource_group_name + location = var.location +} \ No newline at end of file diff --git a/opentofu/azure/router.tf b/opentofu/azure/router.tf new file mode 100644 index 0000000..199576e --- /dev/null +++ b/opentofu/azure/router.tf @@ -0,0 +1,34 @@ +resource "azurerm_container_group" "router" { + name = var.container_group_router_name + location = azurerm_resource_group.resource_group.location + resource_group_name = azurerm_resource_group.resource_group.name + os_type = var.container_group_os + dns_name_label = var.container_group_router_dns + + image_registry_credential { + server = var.image_registry_server + username = var.image_registry_user + password = var.image_registry_password + } + + container { + name = var.router_image_name + image = var.router_image + cpu = var.router_image_cpu + memory = var.router_image_memory + + ports { + port = var.router_port + } + + volume { + name = azurerm_storage_share.router_storage_share.name + mount_path = var.router_volume_mount_path + share_name = azurerm_storage_share.router_storage_share.name + storage_account_name = azurerm_storage_account.storage_account.name + storage_account_key = azurerm_storage_account.storage_account.primary_access_key + } + } + + depends_on = [azurerm_container_group.app] +} diff --git a/opentofu/azure/storage.tf b/opentofu/azure/storage.tf new file mode 100644 index 0000000..693fd23 --- /dev/null +++ b/opentofu/azure/storage.tf @@ -0,0 +1,52 @@ +resource "azurerm_storage_account" "storage_account" { + name = var.config_volume_storage_account_name + resource_group_name = azurerm_resource_group.resource_group.name + location = azurerm_resource_group.resource_group.location + account_tier = "Standard" + account_replication_type = "LRS" + + depends_on = [azurerm_resource_group.resource_group] +} + +resource "azurerm_storage_share" "backend_storage_share" { + name = var.backend_volume_share_name + storage_account_name = azurerm_storage_account.storage_account.name + quota = 1 + + depends_on = [azurerm_storage_account.storage_account] +} + +resource "azurerm_storage_share" "router_storage_share" { + name = var.router_volume_share_name + storage_account_name = azurerm_storage_account.storage_account.name + quota = 1 + + depends_on = [azurerm_storage_account.storage_account] +} + +resource "null_resource" "upload_file" { + provisioner "local-exec" { + command = <