Skip to content

Commit b355672

Browse files
author
Premdeep Saini
committed
add support for gitlab backup upload to S3
1 parent 03ae095 commit b355672

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
external_url '${gitlab_url}'
2+
gitlab_rails['monitoring_whitelist'] = ['0.0.0.0/0','127.0.0.0/8', '::1/128']
3+
gitlab_rails['db_adapter'] = "postgresql"
4+
gitlab_rails['db_encoding'] = "unicode"
5+
gitlab_rails['db_database'] = "${gitlab_db_name}"
6+
gitlab_rails['db_username'] = "${gitlab_db_username}"
7+
gitlab_rails['db_password'] = "${gitlab_db_password}"
8+
gitlab_rails['db_host'] = "${gitlab_db_host}"
9+
gitlab_rails['redis_host'] = "${gitlab_redis_host}"
10+
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
16+
letsencrypt['enable'] = false
17+
18+
################
19+
# S3 Backup
20+
################
21+
gitlab_rails['backup_upload_connection'] = {
22+
'provider' => 'AWS',
23+
'region' => '${aws_region}',
24+
# If using an IAM Profile, don't configure aws_access_key_id & aws_secret_access_key
25+
'use_iam_profile' => true
26+
}
27+
gitlab_rails['backup_upload_remote_directory'] = '${gitlab_backup_s3_bucket_name}'

main.tf

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
locals {
2-
managed_by = "Terraform"
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]]))
37
}
48

59
resource "aws_instance" "gitlab" {
@@ -16,11 +20,16 @@ resource "aws_instance" "gitlab" {
1620
volume_size = var.volume_size
1721
delete_on_termination = false
1822
}
23+
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+
}
1927
tags = {
2028
Name = "${var.environment_prefix}-gitlab"
2129
Environment = var.environment_prefix
2230
ManagedBy = local.managed_by
2331
}
32+
depends_on = [local_file.gitlab_config_file]
2433
}
2534

2635
resource "aws_key_pair" "gitlab_ssh" {
@@ -449,3 +458,17 @@ resource "aws_iam_instance_profile" "gitlab" {
449458
name = "gitlab"
450459
role = aws_iam_role.gitlab_backup.name
451460
}
461+
462+
resource "local_file" "gitlab_config_file" {
463+
filename = local.gitlab_config_generated_file
464+
content = templatefile(local.gitlab_config_template_file, {
465+
gitlab_url = local.gitlab_complete_url,
466+
gitlab_db_name = module.gitlab_pg.db_instance_name,
467+
gitlab_db_username = module.gitlab_pg.db_instance_username,
468+
gitlab_db_password = module.gitlab_pg.db_instance_password,
469+
gitlab_db_host = module.gitlab_pg.db_instance_address,
470+
gitlab_redis_host = aws_elasticache_cluster.gitlab_redis.cache_nodes[0].address,
471+
aws_region = aws_s3_bucket.gitlab_backup[0].region
472+
gitlab_backup_s3_bucket_name = aws_s3_bucket.gitlab_backup[0].bucket
473+
})
474+
}

outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ output "gitlab_redis_address" {
3737
value = aws_elasticache_cluster.gitlab_redis.cache_nodes[0].address
3838
description = "Gitlab Redis cluster address"
3939
}
40+
41+
output "gitlab_complete_url" {
42+
value = local.gitlab_complete_url
43+
}

playbooks/gitlab_setup.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
- name: Configure Gitlab
3+
hosts: "{{ instance_ip_address }}"
4+
gather_facts: no
5+
vars:
6+
ansible_host_key_checking: false
7+
tasks:
8+
- local_action: wait_for port=22 host="{{ instance_ip_address }}" delay=10 timeout=300
9+
- name: copy gitlab.rb to /etc/gitlab/
10+
become: true
11+
copy:
12+
src: "{{ file_path }}"
13+
dest: "/etc/gitlab/gitlab.rb"
14+
owner: "root"
15+
group: "root"
16+
mode: 0600
17+
- name: reconfigure Gitlab
18+
become: true
19+
command: gitlab-ctl reconfigure

variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,8 @@ variable "gitlab_backup_bucket_name" {
268268
default = null
269269
description = "Name of S3 bucket to be used for Gitlab backup"
270270
}
271+
272+
variable "private_key" {
273+
type = string
274+
description = "Private key to execute ansible playbook on Gitlab instance."
275+
}

0 commit comments

Comments
 (0)