11locals {
2- managed_by = " Terraform"
3- gitlab_config_template_file = " ${ path . module } /gitlab_config_templates/gitlab.rb.tftpl"
4- gitlab_config_generated_file = " ${ path . cwd } /gitlab_config/gitlab.rb"
5- gitlab_config_playbook_file = " ${ path . module } /playbooks/gitlab_setup.yaml"
6- gitlab_complete_url = join (" " , tolist ([" https://" , values (module. records . route53_record_name )[0 ]]))
2+ managed_by = " Terraform"
3+ gitlab_config_file_name = " gitlab.rb"
4+ rendered_gitlab_config_file_name = " gitlab_rendered.rb"
5+ gitlab_additional_config_file_name = " gitlab_additional.rb"
6+ gitlab_config_tmp_path = " /tmp/gitlab/gitlab_config"
7+ gitlab_config_template_file_path = " ${ path . module } /gitlab_config_templates"
8+ gitlab_config_file_path = " ${ path . cwd } /gitlab_config"
9+ gitlab_config_playbook_file = " ${ path . module } /playbooks/gitlab_setup.yaml"
10+ gitlab_complete_url = join (" " , tolist ([" https://" , values (module. records . route53_record_name )[0 ]]))
711}
812
913resource "aws_instance" "gitlab" {
@@ -21,15 +25,12 @@ resource "aws_instance" "gitlab" {
2125 delete_on_termination = false
2226 }
2327
24- provisioner "local-exec" {
25- command = " ansible-playbook -u ubuntu -i '${ self . private_ip } ,' --private-key ${ var . private_key } -e 'instance_ip_address=${ self . private_ip } file_path=${ local_file . gitlab_config_file . filename } ' ${ local . gitlab_config_playbook_file } "
26- }
2728 tags = {
2829 Name = " ${ var . environment_prefix } -gitlab"
2930 Environment = var.environment_prefix
3031 ManagedBy = local.managed_by
3132 }
32- depends_on = [ local_file . gitlab_config_file ]
33+
3334}
3435
3536resource "aws_key_pair" "gitlab_ssh" {
@@ -233,12 +234,6 @@ module "elb" {
233234 unhealthy_threshold = var.healthcheck_unhealthy_threshold
234235 timeout = var.healthcheck_timeout
235236 }
236- #
237- # access_logs = {
238- # bucket = "my-access-logs-bucket"
239- # }
240-
241- // ELB attachments
242237 number_of_instances = length (aws_instance. gitlab )
243238 instances = aws_instance. gitlab [* ]. id
244239
@@ -459,9 +454,11 @@ resource "aws_iam_instance_profile" "gitlab" {
459454 role = aws_iam_role. gitlab_backup . name
460455}
461456
462- resource "local_file" "gitlab_config_file" {
463- filename = local. gitlab_config_generated_file
464- content = templatefile (local. gitlab_config_template_file , {
457+ data "template_file" "gitlab_config_template" {
458+ template = join (" \n " , [
459+ for fn in fileset (" ." , " ${ local . gitlab_config_template_file_path } /**" ) : file (fn)
460+ ])
461+ vars = {
465462 gitlab_url = local.gitlab_complete_url,
466463 gitlab_db_name = module.gitlab_pg.db_instance_name,
467464 gitlab_db_username = module.gitlab_pg.db_instance_username,
@@ -470,5 +467,32 @@ resource "local_file" "gitlab_config_file" {
470467 gitlab_redis_host = aws_elasticache_cluster.gitlab_redis.cache_nodes[0 ].address,
471468 aws_region = aws_s3_bucket.gitlab_backup[0 ].region
472469 gitlab_backup_s3_bucket_name = aws_s3_bucket.gitlab_backup[0 ].bucket
473- })
470+ }
471+ }
472+
473+ resource "local_sensitive_file" "rendered_gitlab_config_file" {
474+ filename = " ${ local . gitlab_config_tmp_path } /${ local . rendered_gitlab_config_file_name } "
475+ content = data. template_file . gitlab_config_template . rendered
476+ }
477+
478+ data "local_sensitive_file" "gitlab_additional_config" {
479+ count = fileexists (" ${ local . gitlab_config_file_path } /${ local . gitlab_additional_config_file_name } " ) ? 1 : 0
480+ filename = " ${ local . gitlab_config_file_path } /${ local . gitlab_additional_config_file_name } "
481+ }
482+
483+ resource "local_sensitive_file" "gitlab_config_file" {
484+ filename = " ${ local . gitlab_config_tmp_path } /${ local . gitlab_config_file_name } "
485+ content = join (" \n " , tolist ([
486+ data . template_file . gitlab_config_template . rendered ,
487+ data . local_sensitive_file . gitlab_additional_config != [] ? data . local_sensitive_file . gitlab_additional_config [0 ]. content : " "
488+ ]))
489+ }
490+
491+ resource "null_resource" "gitlab_reconfigure" {
492+ triggers = {
493+ timestamp = timestamp ()
494+ }
495+ provisioner "local-exec" {
496+ command = " ansible-playbook -u ubuntu -i '${ aws_instance . gitlab [0 ]. private_ip } ,' --private-key ${ var . private_key } -e 'instance_ip_address=${ aws_instance . gitlab [0 ]. private_ip } workdir=${ local . gitlab_config_tmp_path } config_file=${ local_sensitive_file . gitlab_config_file . filename } ' ${ local . gitlab_config_playbook_file } "
497+ }
474498}
0 commit comments