Skip to content

Commit ec0cfac

Browse files
author
Premdeep Saini
committed
add support for additional gitlab properties configuration
1 parent 2dcf470 commit ec0cfac

File tree

7 files changed

+92
-30
lines changed

7 files changed

+92
-30
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
nginx['redirect_http_to_https'] = false
2+
nginx['listen_port'] = 80
3+
nginx['listen_https'] = false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
postgresql['enable'] = false
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
external_url '${gitlab_url}'
2+
23
gitlab_rails['monitoring_whitelist'] = ['0.0.0.0/0','127.0.0.0/8', '::1/128']
4+
35
gitlab_rails['db_adapter'] = "postgresql"
46
gitlab_rails['db_encoding'] = "unicode"
57
gitlab_rails['db_database'] = "${gitlab_db_name}"
68
gitlab_rails['db_username'] = "${gitlab_db_username}"
79
gitlab_rails['db_password'] = "${gitlab_db_password}"
810
gitlab_rails['db_host'] = "${gitlab_db_host}"
11+
912
gitlab_rails['redis_host'] = "${gitlab_redis_host}"
1013
gitlab_rails['redis_port'] = 6379
11-
postgresql['enable'] = false
12-
redis['enable'] = false
13-
nginx['redirect_http_to_https'] = false
14-
nginx['listen_port'] = 80
15-
nginx['listen_https'] = false
14+
1615
letsencrypt['enable'] = false
1716

18-
################
19-
# S3 Backup
20-
################
2117
gitlab_rails['backup_upload_connection'] = {
2218
'provider' => 'AWS',
2319
'region' => '${aws_region}',
24-
# If using an IAM Profile, don't configure aws_access_key_id & aws_secret_access_key
2520
'use_iam_profile' => true
2621
}
2722
gitlab_rails['backup_upload_remote_directory'] = '${gitlab_backup_s3_bucket_name}'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
redis['enable'] = false

main.tf

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
locals {
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

913
resource "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

3536
resource "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
}

playbooks/gitlab_setup.yaml

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,50 @@
44
gather_facts: no
55
vars:
66
ansible_host_key_checking: false
7+
update_gitlab_config: false
78
tasks:
8-
- local_action: wait_for port=22 host="{{ instance_ip_address }}" delay=10 timeout=300
9+
- local_action: wait_for port=22 host="{{ instance_ip_address }}" delay=5 timeout=300
10+
11+
- name: stat for /etc/gitlab/gitlab.rb
12+
become: true
13+
stat:
14+
path: "/etc/gitlab/gitlab.rb"
15+
register: original_config_file
16+
- name: Checksum for original gitlab.rb"
17+
set_fact:
18+
original_config_file_checksum: "{{ original_config_file.stat.checksum }}"
19+
- name: print original original checksum
20+
debug:
21+
msg: "{{ original_config_file_checksum }}"
22+
23+
- name: stat for "{{ config_file }}"
24+
local_action: stat path={{ config_file }}
25+
register: new_config_file
26+
- name: Checksum for new gitlab.rb"
27+
set_fact:
28+
new_config_file_checksum: "{{ new_config_file.stat.checksum }}"
29+
- name: print new file checksum
30+
debug:
31+
msg: "{{ new_config_file_checksum }}"
32+
33+
- name: Update gitlab.rb
34+
set_fact:
35+
update_gitlab_config: true
36+
when: original_config_file_checksum != new_config_file_checksum
37+
938
- name: copy gitlab.rb to /etc/gitlab/
1039
become: true
40+
when: update_gitlab_config
1141
copy:
12-
src: "{{ file_path }}"
42+
src: "{{ config_file }}"
1343
dest: "/etc/gitlab/gitlab.rb"
1444
owner: "root"
1545
group: "root"
1646
mode: 0600
1747
- name: reconfigure Gitlab
1848
become: true
49+
when: update_gitlab_config
1950
command: gitlab-ctl reconfigure
51+
52+
- name: cleanup temp files
53+
local_action: command rm -rf {{ workdir }}

versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ terraform {
66
source = "hashicorp/aws"
77
version = ">= 4.40"
88
}
9+
null = {
10+
source = "hashicorp/null"
11+
version = ">= 3.2.1"
12+
}
913
}
1014
}

0 commit comments

Comments
 (0)