diff --git a/terraform/domains.tf b/terraform/domains.tf index d4a5111..c7e4467 100644 --- a/terraform/domains.tf +++ b/terraform/domains.tf @@ -1,17 +1,17 @@ -resource "gandi_livedns_record" "gateways_exploreko_org" { +resource "gandi_livedns_record" "mongo_gateways" { count = var.mongo_groups_count zone = var.domain_name - name = "gateway${count.index}.teaching" + name = "gateway${count.index}.${var.subdomain_suffix}" type = "A" ttl = 3600 - values = [openstack_compute_instance_v2.mongo_gateway[count.index].access_ip_v4] + values = [openstack_compute_instance_v2.mongo_gateways[count.index].access_ip_v4] } -resource "gandi_livedns_record" "mongos_exploreko_org" { - count = var.mongo_replicas_count * var.mongo_groups_count +resource "gandi_livedns_record" "mongo_servers" { + count = var.mongo_replicas_count * var.mongo_groups_count zone = var.domain_name - name = "mongo${count.index}.teaching" + name = "mongo${openstack_compute_instance_v2.mongo_servers[count.index].metadata.mongo_group_id}-${openstack_compute_instance_v2.mongo_servers[count.index].metadata.mongo_group_index}.${var.subdomain_suffix}" type = "A" ttl = 3600 values = [openstack_compute_instance_v2.mongo_servers[count.index].access_ip_v4] diff --git a/terraform/fdj.tfvars b/terraform/fdj.tfvars new file mode 100644 index 0000000..c3ebdd5 --- /dev/null +++ b/terraform/fdj.tfvars @@ -0,0 +1,9 @@ +# mongo_groups_count = 10 +mongo_gateways_enable = false +mongo_servers_enable = false +mongo_groups_count = 10 +mongo_replicas_count = 1 +ssh_private_key = "~/.ssh/keyring.syntaxio/francaise-des-jeux/id_ed25519" +ssh_public_key = "~/.ssh/keyring.syntaxio/francaise-des-jeux/id_ed25519.pub" +domain_name = "glenux.net" +subdomain_suffix = "teaching" diff --git a/terraform/instances.tf b/terraform/instances.tf index a4b828a..a7c23f7 100644 --- a/terraform/instances.tf +++ b/terraform/instances.tf @@ -1,62 +1,55 @@ # Création d'une ressource de paire de clés SSH resource "openstack_compute_keypair_v2" "provision_keypair" { - provider = openstack.ovh + provider = openstack.ovh name = "provision_keypair" - public_key = file(var.ssh_public_key) + public_key = file(var.ssh_public_key) } -resource "openstack_compute_instance_v2" "mongo_gateway" { +resource "openstack_compute_instance_v2" "mongo_gateways" { count = var.mongo_groups_count name = "prod-gateway${count.index}" # Nom de l'instance - provider = openstack.ovh # Nom du fournisseur - image_name = "Debian 11" # Nom de l'image - flavor_name = "s1-2" # Nom du type d'instance + provider = openstack.ovh # Nom du fournisseur + image_name = "Debian 11" # Nom de l'image + flavor_name = "s1-2" # Nom du type d'instance # flavor_name = "s1-8" # Nom du type d'instance # flavor_name = "d2-8" # Nom du type d'instance # Nom de la ressource openstack_compute_keypair_v2 nommée test_keypair key_pair = openstack_compute_keypair_v2.provision_keypair.name + power_state = var.mongo_servers_enable ? "active" : "shutoff" metadata = { - ansible-group = "gateways" - mongo-group-id = count.index + ansible_group = "gateways" + mongo_group_id = count.index } # Ajoute le composant réseau pour atteindre votre instance network { - name = "Ext-Net" + name = "Ext-Net" } - - # provisioner "local-exec" { - # command = "ansible-playbook -i inventories/terraform --private-key ${var.ssh_private_key} -e 'pub_key=${var.ssh_public_key}' playbook.yml --limit ${self.name}" - # working_dir = "../ansible" - # environment = { - # ANSIBLE_HOST_KEY_CHECKING = "False" - # } - - # } } # Création d'une instance resource "openstack_compute_instance_v2" "mongo_servers" { count = var.mongo_replicas_count * var.mongo_groups_count name = "prod-server${count.index}" # Nom de l'instance - provider = openstack.ovh # Nom du fournisseur - image_name = "Debian 11" # Nom de l'image - flavor_name = "s1-2" # Nom du type d'instance + provider = openstack.ovh # Nom du fournisseur + image_name = "Debian 11" # Nom de l'image + flavor_name = "s1-2" # Nom du type d'instance # flavor_name = "s1-8" # Nom du type d'instance # flavor_name = "d2-8" # Nom du type d'instance # Nom de la ressource openstack_compute_keypair_v2 nommée test_keypair key_pair = openstack_compute_keypair_v2.provision_keypair.name + power_state = var.mongo_servers_enable ? "active" : "shutoff" metadata = { - ansible-group = "mongos" - mongo-group-id = floor(count.index / var.mongo_replicas_count) - mongo-group-index = count.index % var.mongo_replicas_count + ansible_group = "mongos" + mongo_group_id = floor(count.index / var.mongo_replicas_count) + mongo_group_index = count.index % var.mongo_replicas_count } # Ajoute le composant réseau pour atteindre votre instance network { - name = "Ext-Net" + name = "Ext-Net" } # provisioner "local-exec" { diff --git a/terraform/output.tf b/terraform/output.tf deleted file mode 100644 index d5320f1..0000000 --- a/terraform/output.tf +++ /dev/null @@ -1,12 +0,0 @@ -resource "local_file" "ansible_inventory" { - content = templatefile("templates/inventory.tmpl", - { - mongo_gateways = openstack_compute_instance_v2.mongo_gateway.* - mongo_servers = openstack_compute_instance_v2.mongo_servers.* - mongo_groups_count = var.mongo_groups_count - mongo_replicas_count = var.mongo_replicas_count - } - ) - filename = "outputs/inventory" - file_permission = "0644" -} diff --git a/terraform/outputs.tf b/terraform/outputs.tf new file mode 100644 index 0000000..0b28e38 --- /dev/null +++ b/terraform/outputs.tf @@ -0,0 +1,16 @@ +resource "local_file" "ansible_inventory" { + content = templatefile("templates/inventory.tmpl", + { + dns_gateways = gandi_livedns_record.mongo_gateways.* + dns_servers = gandi_livedns_record.mongo_gateways.* + mongo_gateways = openstack_compute_instance_v2.mongo_gateways.* + mongo_servers = openstack_compute_instance_v2.mongo_servers.* + mongo_groups_count = var.mongo_groups_count + mongo_replicas_count = var.mongo_replicas_count + } + ) + filename = "outputs/inventory" + file_permission = "0644" + +} + diff --git a/terraform/provider.tf b/terraform/provider.tf index 1ccf73d..bffef29 100644 --- a/terraform/provider.tf +++ b/terraform/provider.tf @@ -12,14 +12,14 @@ terraform { } gandi = { - source = "go-gandi/gandi" + source = "go-gandi/gandi" version = "~> 2.0.1" } } } provider "gandi" { - key = "${var.gandi_key}" + key = var.gandi_key } provider "openstack" { diff --git a/terraform/provision.tf b/terraform/provision.tf new file mode 100644 index 0000000..c7f4a6b --- /dev/null +++ b/terraform/provision.tf @@ -0,0 +1,25 @@ + +resource "null_resource" "ansible" { + count = (var.mongo_gateways_enable && var.mongo_servers_enable) ? 1 : 0 + depends_on = [local_file.ansible_inventory] + + triggers = { + always_run = "${timestamp()}" + } + + provisioner "local-exec" { + environment = { + ANSIBLE_HOST_KEY_CHECKING = "False" + } + working_dir = "../ansible" + command = <<-EOT + ansible-playbook \ + -i inventories/terraform \ + --private-key ${var.ssh_private_key} \ + -e 'pub_key=${var.ssh_public_key}' \ + playbook.yml + EOT + } + +} + diff --git a/terraform/provisionners.tf b/terraform/provisionners.tf deleted file mode 100644 index b28dbe4..0000000 --- a/terraform/provisionners.tf +++ /dev/null @@ -1,4 +0,0 @@ - -# provisioner "local-exec" { -# command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i outputs/inventory --private-key ${var.private_key} -e 'pub_key=${var.pub_key}' playbook.yml" -# } diff --git a/terraform/templates/inventory.tmpl b/terraform/templates/inventory.tmpl index 0989403..30a0d98 100644 --- a/terraform/templates/inventory.tmpl +++ b/terraform/templates/inventory.tmpl @@ -1,8 +1,8 @@ %{ for index, instance in mongo_gateways ~} -${ instance.name } ansible_user=debian ansible_host=${instance.network[0].fixed_ip_v4} +${ instance.name } ansible_user=debian ansible_host=${instance.network[0].fixed_ip_v4} sshwifty_hostname=${dns_gateways[index].name}.${dns_gateways[index].zone} mongo_group_id=${instance.metadata.mongo_group_id} %{ endfor ~} %{ for index, instance in mongo_servers ~} -${ instance.name } ansible_user=debian ansible_host=${instance.network[0].fixed_ip_v4} +${ instance.name } ansible_user=debian ansible_host=${instance.network[0].fixed_ip_v4} mongo_group_id=${instance.metadata.mongo_group_id} mongo_group_index=${instance.metadata.mongo_group_index} %{ endfor ~} [stage_development] diff --git a/terraform/variables.tf b/terraform/variables.tf index bc5d72a..ce28a25 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -1,12 +1,22 @@ +variable "mongo_gateways_enable" { + default = false + type = bool +} + +variable "mongo_servers_enable" { + default = false + type = bool +} + variable "mongo_groups_count" { - default = "1" - description = "How many replicas per mongo" + default = "1" + description = "How many replicas per mongo" } variable "mongo_replicas_count" { - default = "2" - description = "How many replicas per mongo group" + default = "2" + description = "How many replicas per mongo group" } variable "ssh_private_key" {} @@ -17,4 +27,5 @@ variable "gandi_key" {} variable "domain_name" {} - +variable "subdomain_suffix" { +}