diff --git a/opentofu/README.md b/opentofu/README.md index 5e17e7e..b7de6e5 100644 --- a/opentofu/README.md +++ b/opentofu/README.md @@ -10,6 +10,15 @@ https://opentofu.org/docs/intro/install/ https://learn.microsoft.com/en-us/cli/azure/install-azure-cli#install +### Se connecter à Azure et récupérer l'id de l'abonnement Azure + +Pour se connecter à Azure, faites la commande suivante + +`az login` + +Avec cette commande, vous allez sélectionner un abonnement Azure. Copiez l'id de l'abonnement, vous en aurez besoin +dans l'étape suivant. + ### Modifier les configurations Créer un fichier **terraform.tfvars** sur la base du fichier **terraform.tfvars.example** dans le répertoire **azure**. @@ -18,12 +27,8 @@ Toutes les variables, leur description et leur valeur par défaut sont disponibl 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. +L'url est défini comme suit: http://..cloudapp.azure.com. +Par défaut, l'url est http://evaluetonsavoir.canadacentral.cloudapp.azure.com/ ### Lancer le déploiement diff --git a/opentofu/azure/app.tf b/opentofu/azure/app.tf index 0aab5ef..a1dada5 100644 --- a/opentofu/azure/app.tf +++ b/opentofu/azure/app.tf @@ -1,64 +1,67 @@ -resource "azurerm_container_group" "app" { - name = var.container_group_app_name - location = azurerm_resource_group.resource_group.location +# Create Virtual Machine +resource "azurerm_linux_virtual_machine" "vm" { + name = var.vm_name resource_group_name = azurerm_resource_group.resource_group.name - os_type = var.container_group_os - dns_name_label = var.container_group_app_dns + location = azurerm_resource_group.resource_group.location + size = var.vm_size + admin_username = var.vm_user + admin_password = var.vm_password + disable_password_authentication = false - image_registry_credential { - server = var.image_registry_server - username = var.image_registry_user - password = var.image_registry_password + network_interface_ids = [azurerm_network_interface.nic.id] + + os_disk { + name = var.vm_os_disk_name + caching = "ReadWrite" + storage_account_type = var.vm_os_disk_type } - 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 - } + source_image_reference { + publisher = var.vm_image_publisher + offer = var.vm_image_offer + sku = var.vm_image_plan + version = var.vm_image_version } - container { - name = var.backend_image_name - image = var.backend_image - cpu = var.backend_image_cpu - memory = var.backend_image_memory + custom_data = base64encode(<<-EOT + #!/bin/bash + sudo apt-get update -y + sudo apt-get install -y docker.io + sudo apt-get install -y docker-compose + sudo systemctl start docker + sudo systemctl enable docker - 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 - } + sudo usermod -aG docker ${var.vm_user} + sudo newgrp docker - ports { - port = var.backend_port - } + su - ${var.vm_user} -c ' - 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 - } - } + curl -o auth_config.json \ + "https://${azurerm_storage_account.storage_account.name}.file.core.windows.net/${azurerm_storage_share.backend_storage_share.name}/auth_config.json${data.azurerm_storage_account_sas.storage_access.sas}" - depends_on = [azurerm_cosmosdb_mongo_collection.cosmosdb_mongo_collection] + curl -L -o docker-compose.yaml ${var.docker_compose_url} + + export VITE_BACKEND_URL=http://${var.dns}.${lower(replace(azurerm_resource_group.resource_group.location, " ", ""))}.cloudapp.azure.com + export PORT=${var.backend_port} + export MONGO_URI="${azurerm_cosmosdb_account.cosmosdb_account.primary_mongodb_connection_string}" + export MONGO_DATABASE=${azurerm_cosmosdb_mongo_collection.cosmosdb_mongo_collection.database_name} + export EMAIL_SERVICE=${var.backend_email_service} + export SENDER_EMAIL=${var.backend_email_sender} + export EMAIL_PSW="${var.backend_email_password}" + export JWT_SECRET=${var.backend_jwt_secret} + export SESSION_Secret=${var.backend_session_secret} + export SITE_URL=http://${var.dns}.${lower(replace(azurerm_resource_group.resource_group.location, " ", ""))}.cloudapp.azure.com + export FRONTEND_PORT=${var.frontend_port} + export USE_PORTS=${var.backend_use_port} + export AUTHENTICATED_ROOMS=${var.backend_use_auth_student} + export QUIZROOM_IMAGE=${var.quizroom_image} + + docker-compose up -d + ' + EOT + ) + + depends_on = [ + azurerm_cosmosdb_mongo_collection.cosmosdb_mongo_collection, + data.azurerm_storage_account_sas.storage_access] } diff --git a/opentofu/azure/database.tf b/opentofu/azure/database.tf index ba0858f..efc9fbf 100644 --- a/opentofu/azure/database.tf +++ b/opentofu/azure/database.tf @@ -1,9 +1,16 @@ 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" + 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" + mongo_server_version = "7.0" + + is_virtual_network_filter_enabled = true + + virtual_network_rule { + id = azurerm_subnet.subnet.id + } capabilities { name = "EnableMongo" diff --git a/opentofu/azure/main.tf b/opentofu/azure/main.tf index 9cc6ae4..1d1329d 100644 --- a/opentofu/azure/main.tf +++ b/opentofu/azure/main.tf @@ -2,7 +2,7 @@ terraform { required_providers { azurerm = { source = "hashicorp/azurerm" - version = "~> 3.0" + version = "~> 4.0" } } required_version = ">= 1.0" @@ -10,4 +10,5 @@ terraform { provider "azurerm" { features {} + subscription_id = var.subscription_id } \ No newline at end of file diff --git a/opentofu/azure/network.tf b/opentofu/azure/network.tf new file mode 100644 index 0000000..d9db2c8 --- /dev/null +++ b/opentofu/azure/network.tf @@ -0,0 +1,87 @@ +# Create Virtual Network +resource "azurerm_virtual_network" "vnet" { + name = var.vnet_name + location = azurerm_resource_group.resource_group.location + resource_group_name = azurerm_resource_group.resource_group.name + address_space = ["10.0.0.0/16"] +} + +# Create Subnet +resource "azurerm_subnet" "subnet" { + name = var.subnet_name + resource_group_name = azurerm_resource_group.resource_group.name + virtual_network_name = azurerm_virtual_network.vnet.name + address_prefixes = ["10.0.1.0/24"] + + service_endpoints = ["Microsoft.AzureCosmosDB"] +} + +# Create Public IP Address +resource "azurerm_public_ip" "public_ip" { + name = var.public_ip_name + location = azurerm_resource_group.resource_group.location + resource_group_name = azurerm_resource_group.resource_group.name + allocation_method = "Static" + domain_name_label = var.dns +} + +resource "azurerm_network_security_group" "nsg" { + name = var.nsg_name + location = azurerm_resource_group.resource_group.location + resource_group_name = azurerm_resource_group.resource_group.name + + security_rule { + name = "SSH" + priority = 1000 + direction = "Inbound" + access = "Allow" + protocol = "Tcp" + source_port_range = "*" + destination_port_range = "22" + source_address_prefix = var.nsg_ssh_ip_range + destination_address_prefix = "*" + } + + security_rule { + name = "HTTP" + priority = 1001 + direction = "Inbound" + access = "Allow" + protocol = "Tcp" + source_port_range = "*" + destination_port_range = "80" + source_address_prefix = var.nsg_http_ip_range + destination_address_prefix = "*" + } + + security_rule { + name = "HTTPS" + priority = 1002 + direction = "Inbound" + access = "Allow" + protocol = "Tcp" + source_port_range = "*" + destination_port_range = "443" + source_address_prefix = var.nsg_https_ip_range + destination_address_prefix = "*" + } +} + +# Create Network Interface +resource "azurerm_network_interface" "nic" { + name = var.network_interface_name + location = azurerm_resource_group.resource_group.location + resource_group_name = azurerm_resource_group.resource_group.name + + ip_configuration { + name = "internal" + subnet_id = azurerm_subnet.subnet.id + private_ip_address_allocation = "Dynamic" + public_ip_address_id = azurerm_public_ip.public_ip.id + } +} + +resource "azurerm_network_interface_security_group_association" "example" { + network_interface_id = azurerm_network_interface.nic.id + network_security_group_id = azurerm_network_security_group.nsg.id +} diff --git a/opentofu/azure/resource_group.tf b/opentofu/azure/resource_group.tf index 3b29d37..ed83082 100644 --- a/opentofu/azure/resource_group.tf +++ b/opentofu/azure/resource_group.tf @@ -1,3 +1,4 @@ +# Create Resource Group resource "azurerm_resource_group" "resource_group" { name = var.resource_group_name location = var.location diff --git a/opentofu/azure/router.tf b/opentofu/azure/router.tf deleted file mode 100644 index 199576e..0000000 --- a/opentofu/azure/router.tf +++ /dev/null @@ -1,34 +0,0 @@ -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 index 693fd23..1eb6db7 100644 --- a/opentofu/azure/storage.tf +++ b/opentofu/azure/storage.tf @@ -9,15 +9,7 @@ resource "azurerm_storage_account" "storage_account" { } 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 + name = var.backend_storage_share_name storage_account_name = azurerm_storage_account.storage_account.name quota = 1 @@ -35,18 +27,48 @@ resource "null_resource" "upload_file" { EOT } - provisioner "local-exec" { - command = <