11locals {
2- managed_by = " Terraform"
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 ]]))
311}
412
513resource "aws_instance" "gitlab" {
@@ -16,11 +24,13 @@ resource "aws_instance" "gitlab" {
1624 volume_size = var. volume_size
1725 delete_on_termination = false
1826 }
27+
1928 tags = {
2029 Name = " ${ var . environment_prefix } -gitlab"
2130 Environment = var.environment_prefix
2231 ManagedBy = local.managed_by
2332 }
33+
2434}
2535
2636resource "aws_key_pair" "gitlab_ssh" {
@@ -224,12 +234,6 @@ module "elb" {
224234 unhealthy_threshold = var.healthcheck_unhealthy_threshold
225235 timeout = var.healthcheck_timeout
226236 }
227- #
228- # access_logs = {
229- # bucket = "my-access-logs-bucket"
230- # }
231-
232- // ELB attachments
233237 number_of_instances = length (aws_instance. gitlab )
234238 instances = aws_instance. gitlab [* ]. id
235239
@@ -449,3 +453,46 @@ resource "aws_iam_instance_profile" "gitlab" {
449453 name = " gitlab"
450454 role = aws_iam_role. gitlab_backup . name
451455}
456+
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 = {
462+ gitlab_url = local.gitlab_complete_url,
463+ gitlab_db_name = module.gitlab_pg.db_instance_name,
464+ gitlab_db_username = module.gitlab_pg.db_instance_username,
465+ gitlab_db_password = module.gitlab_pg.db_instance_password,
466+ gitlab_db_host = module.gitlab_pg.db_instance_address,
467+ gitlab_redis_host = aws_elasticache_cluster.gitlab_redis.cache_nodes[0 ].address,
468+ aws_region = aws_s3_bucket.gitlab_backup[0 ].region
469+ gitlab_backup_s3_bucket_name = aws_s3_bucket.gitlab_backup[0 ].bucket
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+ }
498+ }
0 commit comments