Skip to content

Commit

Permalink
fix: implem condition for linux/windows and correction on initrepset …
Browse files Browse the repository at this point in the history
…for tls
  • Loading branch information
Nico-dl05 committed Dec 17, 2024
1 parent e2a937d commit 1578a2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion terraform/modules/monitoring/partition_metrics/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ resource "docker_container" "partition_metrics" {
external = var.exposed_port
}

dynamic "upload" {
dynamic "upload" {
for_each = var.mounts
content {
source = upload.value
Expand Down
30 changes: 23 additions & 7 deletions terraform/modules/storage/database/mongo/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ resource "docker_container" "database" {
for_each = var.mongodb_params.windows ? [] : [1]
content {
test = ["CMD", "mongosh", "--quiet", "--tls", "--tlsCAFile", "/mongo-certificate/ca.pem", "--eval", "db.runCommand('ping').ok"]
interval = "3s"
interval = "3s"
retries = "2"
timeout = "3s"
}
Expand All @@ -34,31 +34,47 @@ resource "docker_container" "database" {
file = "/mongo-certificate/key.pem"
content = local.server_key
}
upload {
file = "/mongo-init.js"
content = local.mongo_init_repset
}

upload {
file = "/mongo-certificate/ca.pem"
content = tls_locally_signed_cert.mongodb_certificate.ca_cert_pem
}
}
resource "time_sleep" "wait" {
create_duration = var.mongodb_params.windows ? "15s" : "0s"
create_duration = var.mongodb_params.windows ? "30s" : "0s"
depends_on = [docker_container.database]
}

locals {
linux_run = "docker exec ${docker_container.database.name} mongosh mongodb://localhost:27017/${var.mongodb_params.database_name} --tls --tlsCAFile /mongo-certificate/ca.pem"
linux_run = "docker exec ${docker_container.database.name} mongosh mongodb://127.0.0.1:27017/${var.mongodb_params.database_name} --tls --tlsCAFile /mongo-certificate/ca.pem"
// mongosh is not installed in windows docker images so we need it to be installed locally
windows_run = "mongosh.exe mongodb://localhost:${var.mongodb_params.exposed_port}/${var.mongodb_params.database_name} --tls --tlsCAFile ${local_sensitive_file.ca.filename}"
prefix_run = var.mongodb_params.windows ? local.windows_run : local.linux_run
windows_run = "mongosh.exe mongodb://127.0.0.1:${var.mongodb_params.exposed_port}/${var.mongodb_params.database_name} --tls --tlsCAFile ${local_sensitive_file.ca.filename}"
prefix_run = var.mongodb_params.windows ? local.windows_run : local.linux_run
mongo_init_repset = <<EOT
rs.initiate({
_id: "${var.mongodb_params.replica_set_name}",
members: [
{_id: 0, host: "127.0.0.1:27017"}
]
});
EOT
}
resource "local_file" "mongo_init" {
content = local.mongo_init_repset
filename = "${path.root}/generated/mongo/mongo_init.js"

}
resource "null_resource" "init_replica" {
provisioner "local-exec" {
command = "${local.prefix_run} --eval \"rs.initiate({_id: '${var.mongodb_params.replica_set_name}', members: [{_id: 0, host: 'localhost:27017'}]})\""
command = var.mongodb_params.windows ? "${local.windows_run} --file ${local_file.mongo_init.filename}" : "${local.linux_run} --file /mongo-init.js"
}
depends_on = [time_sleep.wait]
}


resource "null_resource" "partitions_in_db" {
for_each = var.partition_list
provisioner "local-exec" {
Expand Down

0 comments on commit 1578a2f

Please sign in to comment.