-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.tf
237 lines (236 loc) · 7.5 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# GHE Server security group - https://help.github.com/enterprise/2.5/admin/guides/installation/network-ports-to-open/
resource "aws_security_group" "ghe-server" {
name = "${var.hostname}.${var.domain} sg"
description = "GitHub Enterprise"
vpc_id = "${var.aws_vpc_id}"
tags = {
Name = "${var.hostname}.${var.domain} sg"
}
}
# SSH - for git
resource "aws_security_group_rule" "ghe-server_allow_22_tcp" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["${split(",", var.allowed_commit_cidrs)}"]
security_group_id = "${aws_security_group.ghe-server.id}"
}
# SSH - for instance
resource "aws_security_group_rule" "ghe-server_allow_122_tcp" {
type = "ingress"
from_port = 122
to_port = 122
protocol = "tcp"
cidr_blocks = ["${split(",", var.allowed_cidrs)}"]
security_group_id = "${aws_security_group.ghe-server.id}"
}
# HTTP
resource "aws_security_group_rule" "ghe-server_allow_80_tcp" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.ghe-server.id}"
}
# HTTP @ 8080
resource "aws_security_group_rule" "ghe-server_allow_8080_tcp" {
type = "ingress"
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.ghe-server.id}"
}
# HTTPS
resource "aws_security_group_rule" "ghe-server_allow_443_tcp" {
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.ghe-server.id}"
}
# HTTPS @ 8443
resource "aws_security_group_rule" "ghe-server_allow_8443_tcp" {
type = "ingress"
from_port = 8443
to_port = 8443
protocol = "tcp"
cidr_blocks = ["${split(",", var.allowed_cidrs)}"]
security_group_id = "${aws_security_group.ghe-server.id}"
}
# GIT
resource "aws_security_group_rule" "ghe-server_allow_9418_tcp" {
type = "ingress"
from_port = 9418
to_port = 9418
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.ghe-server.id}"
}
# VPN
resource "aws_security_group_rule" "ghe-server_allow_1194_udp" {
type = "ingress"
from_port = 1194
to_port = 1194
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.ghe-server.id}"
}
## NTP
resource "aws_security_group_rule" "ghe-server_allow_123_udp" {
count = "${var.sgrule_ntp}"
type = "ingress"
from_port = 123
to_port = 123
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.ghe-server.id}"
}
# SNMP
resource "aws_security_group_rule" "ghe-server_allow_161_udp" {
count = "${var.sgrule_snmp}"
type = "ingress"
from_port = 161
to_port = 161
protocol = "udp"
cidr_blocks = ["${split(",", var.allowed_cidrs)}"]
security_group_id = "${aws_security_group.ghe-server.id}"
}
# SMTP
resource "aws_security_group_rule" "ghe-server_allow_25_tcp" {
count = "${var.sgrule_smtp}"
type = "ingress"
from_port = 25
to_port = 25
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.ghe-server.id}"
}
# Egress: ALL
resource "aws_security_group_rule" "ghe-server_allow_all" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.ghe-server.id}"
}
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "${var.aws_region}"
}
resource "template_file" "attributes-json" {
template = "${file("${path.module}/files/attributes-json.tpl")}"
vars {
chef_fqdn = "${var.chef_fqdn}"
chef_org = "${var.chef_org}"
host = "${var.hostname}"
domain = "${var.domain}"
}
}
#
# Wait on
#
resource "null_resource" "wait_on" {
provisioner "local-exec" {
command = "echo Waited on ${var.wait_on} before proceeding"
}
}
#
# Provision server
#
resource "aws_instance" "ghe-server" {
depends_on = ["null_resource.wait_on"]
ami = "${lookup(var.ami_map, "${var.aws_region}-${var.ghe_version}")}"
count = "${var.server_count}"
instance_type = "${var.aws_flavor}"
associate_public_ip_address = "${var.public_ip}"
subnet_id = "${var.aws_subnet_id}"
vpc_security_group_ids = ["${aws_security_group.ghe-server.id}"]
key_name = "${var.aws_key_name}"
tags = {
Name = "${var.hostname}.${var.domain}"
Description = "${var.tag_description}"
}
root_block_device = {
delete_on_termination = "${var.root_delete_termination}"
volume_size = "${var.root_volume_size}"
volume_type = "${var.root_volume_type}"
}
# seperate device for GHE to use at /data
ebs_block_device {
device_name = "/dev/xvdg"
volume_type = "gp2"
volume_size = 10
delete_on_termination = true
encrypted = true
}
connection {
host = "${self.public_ip}"
user = "${var.ami_user}"
private_key = "${var.aws_private_key_file}"
port = 122
}
# Setup directories
provisioner "remote-exec" {
inline = [
"mkdir -p ~/.ghe"
]
}
# Ensure previous invocations are dead to us
provisioner "local-exec" {
command = "knife node-delete ${var.hostname}.${var.domain} -y -c ${var.knife_rb} ; echo OK"
}
provisioner "local-exec" {
command = "knife client-delete ${var.hostname}.${var.domain} -y -c ${var.knife_rb} ; echo OK"
}
# Provision with Chef
provisioner "chef" {
attributes_json = "${template_file.attributes-json.rendered}"
environment = "${var.chef_env}"
run_list = ["system::default","recipe[chef-client::default]","recipe[chef-client::config]","recipe[chef-client::cron]","recipe[chef-client::delete_validation]"]
log_to_file = "${var.log_to_file}"
node_name = "${var.hostname}.${var.domain}"
server_url = "https://${var.chef_fqdn}/organizations/${var.chef_org}"
validation_client_name = "${var.chef_org}-validator"
validation_key = "${file("${var.chef_org_validator}")}"
version = "${var.client_version}"
}
# Hostname issues... rebooting
provisioner "remote-exec" {
inline = [
"sudo reboot"
]
}
provisioner "remote-exec" {
inline = [
"sleep 10 && echo 'ready'"
]
}
}
resource "template_file" "ghe-server-creds" {
template = "${file("${path.module}/files/ghe-server-creds.tpl")}"
vars {
pass = "${base64sha256(aws_instance.ghe-server.id)}"
fqdn = "${aws_instance.ghe-server.tags.Name}"
}
}
resource "null_resource" "ghe-configure" {
depends_on = ["aws_instance.ghe-server"]
# Use the API to setup the rest from JSON file
provisioner "local-exec" {
command = "sleep 5 && curl -kLs --resolve ${aws_instance.ghe-server.tags.Name}:8443:${aws_instance.ghe-server.public_ip} -X POST 'https://${aws_instance.ghe-server.tags.Name}:8443/setup/api/start' -F license=@${var.ghe_license} -F 'password=${base64sha256(aws_instance.ghe-server.id)}' -F 'settings=<${var.ghe_settings}'"
}
# Tell GHE to start configuring
provisioner "local-exec" {
command = "sleep 2 && curl -kLs --resolve ${aws_instance.ghe-server.tags.Name}:8443:${aws_instance.ghe-server.public_ip} -X POST 'https://api_key:${replace(base64sha256(aws_instance.ghe-server.id), "/", "%2F")}@${aws_instance.ghe-server.tags.Name}:8443/setup/api/configure'"
}
# Check configuration status
provisioner "local-exec" {
command = "sleep 2 && curl -kLs --resolve ${aws_instance.ghe-server.tags.Name}:8443:${aws_instance.ghe-server.public_ip} -X GET 'https://api_key:${replace(base64sha256(aws_instance.ghe-server.id), "/", "%2F")}@${aws_instance.ghe-server.tags.Name}:8443/setup/api/configcheck'"
}
}