This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 170
/
circleci.tf
388 lines (326 loc) · 10.1 KB
/
circleci.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
data "aws_subnet" "subnet" {
id = var.aws_subnet_id
}
data "template_file" "services_user_data" {
template = file("templates/services_user_data.tpl")
vars = {
circle_secret_passphrase = var.circle_secret_passphrase
sqs_queue_url = module.shutdown_sqs.sqs_id
s3_bucket = aws_s3_bucket.circleci_bucket.id
aws_region = var.aws_region
subnet_id = var.aws_subnet_id
vm_sg_id = aws_security_group.circleci_vm_sg.id
http_proxy = var.http_proxy
https_proxy = var.https_proxy
no_proxy = var.no_proxy
}
}
data "template_file" "circleci_policy" {
template = file("templates/circleci_policy.tpl")
vars = {
aws_partition = var.enable_govcloud == true ? "aws-us-gov" : "aws"
bucket_arn = aws_s3_bucket.circleci_bucket.arn
sqs_queue_arn = module.shutdown_sqs.sqs_arn
role_name = aws_iam_role.circleci_role.name
role_path = aws_iam_role.circleci_role.path
aws_region = var.aws_region
}
}
data "template_file" "output" {
template = file("templates/output.tpl")
vars = {
services_public_ip = aws_instance.services.public_ip
ssh_key = var.aws_ssh_key_name
}
}
# Access Secret Key and ID should be set using envvars
# AWS_ACCESS_KEY_ID
# AWS_SECRET_ACCESS_KEY
provider "aws" {
access_key = var.aws_access_key
secret_key = var.aws_secret_key
region = var.aws_region
}
module "shutdown_sqs" {
source = "./modules/aws_sqs"
name = "shutdown"
prefix = var.prefix
}
# Single general-purpose bucket
resource "aws_s3_bucket" "circleci_bucket" {
# VPC ID is used here to make bucket name globally unique(ish) while
# uuid/ignore_changes have some lingering issues
bucket = "${replace(var.prefix, "_", "-")}-bucket-${replace(var.aws_vpc_id, "vpc-", "")}"
cors_rule {
allowed_methods = ["GET"]
allowed_origins = ["*"]
max_age_seconds = 3600
}
force_destroy = var.force_destroy_s3_bucket
}
## IAM for instances
resource "aws_iam_role" "circleci_role" {
name = "${var.prefix}_role"
path = "/"
assume_role_policy = file("files/circleci_role.json")
}
resource "aws_iam_role_policy" "circleci_policy" {
name = "${var.prefix}_policy"
role = aws_iam_role.circleci_role.id
policy = data.template_file.circleci_policy.rendered
}
resource "aws_iam_instance_profile" "circleci_profile" {
name = "${var.prefix}_profile"
role = aws_iam_role.circleci_role.name
}
## Configure the services machine
resource "aws_security_group" "circleci_builders_sg" {
name = "${var.prefix}_builders_sg"
description = "SG for CircleCI Builder instances"
vpc_id = var.aws_vpc_id
ingress {
self = true
from_port = 0
to_port = 0
protocol = "-1"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "circleci_services_sg" {
name = "${var.prefix}_services_sg"
description = "SG for CircleCI services/database instances"
vpc_id = var.aws_vpc_id
ingress {
security_groups = [aws_security_group.circleci_builders_sg.id]
protocol = "-1"
from_port = 0
to_port = 0
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
# If using github.com (not GitHub Enterprise) whitelist GitHub cidr block
# https://help.github.com/articles/what-ip-addresses-does-github-use-that-i-should-whitelist/
#
#ingress {
# cidr_blocks = ["192.30.252.0/22"]
# protocol = "tcp"
# from_port = 443
# to_port = 443
#}
#ingress {
# cidr_blocks = ["192.30.252.0/22"]
# protocol = "tcp"
# from_port = 80
# to_port = 80
#}
}
resource "aws_security_group" "circleci_builders_admin_sg" {
name = "${var.prefix}_builders_admin_sg"
description = "SG for services to masters communication - avoids circular dependency"
vpc_id = var.aws_vpc_id
ingress {
security_groups = [aws_security_group.circleci_services_sg.id]
protocol = "tcp"
from_port = 443
to_port = 443
}
}
#
# This should be configured by admins to restrict access to machines
# TODO: Make this more extensible
#
resource "aws_security_group" "circleci_users_sg" {
name = "${var.prefix}_users_sg"
description = "SG representing users of CircleCI Enterprise"
vpc_id = var.aws_vpc_id
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 22
to_port = 22
}
# For Web traffic to services
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 80
to_port = 80
}
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 443
to_port = 443
}
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 8800
to_port = 8800
}
# For Nomad server in 2.0 clustered installation
ingress {
cidr_blocks = [data.aws_subnet.subnet.cidr_block]
protocol = "tcp"
from_port = 4647
to_port = 4647
}
# For output-processor in 2.0 clustered installation
ingress {
cidr_blocks = [data.aws_subnet.subnet.cidr_block]
protocol = "tcp"
from_port = 8585
to_port = 8585
}
# For embedded storage in 2.0 clustered installation
ingress {
cidr_blocks = [data.aws_subnet.subnet.cidr_block]
protocol = "tcp"
from_port = 7171
to_port = 7171
}
# For build-agent to talk to vm-service
ingress {
cidr_blocks = [data.aws_subnet.subnet.cidr_block]
protocol = "tcp"
from_port = 3001
to_port = 3001
}
# For SSH traffic to builder boxes
# TODO: Update once services box has ngrok
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 64535
to_port = 65535
}
}
resource "aws_security_group" "circleci_vm_sg" {
name = "${var.prefix}_vm_sg"
description = "SG for VMs allocated by CircleCI for Remote Docker and machine executor"
vpc_id = var.aws_vpc_id
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 22
to_port = 22
}
# For Web traffic to services
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 2376
to_port = 2376
}
# For SSHing into 2.0 build
ingress {
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
from_port = 54782
to_port = 54782
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "services" {
instance_type = var.services_instance_type
ami = var.services_ami != "" ? var.services_ami : var.ubuntu_ami[var.aws_region]
key_name = var.aws_ssh_key_name
subnet_id = var.aws_subnet_id
associate_public_ip_address = true
disable_api_termination = var.services_disable_api_termination
iam_instance_profile = aws_iam_instance_profile.circleci_profile.name
vpc_security_group_ids = [
aws_security_group.circleci_services_sg.id,
aws_security_group.circleci_users_sg.id,
]
tags = {
Name = "${var.prefix}_services"
}
root_block_device {
volume_type = "gp2"
volume_size = "150"
delete_on_termination = var.services_delete_on_termination
}
user_data = var.services_user_data_enabled ? data.template_file.services_user_data.rendered : ""
lifecycle {
prevent_destroy = false
}
}
resource "aws_route53_record" "services_route" {
count = var.enable_route
zone_id = var.route_zone_id
name = var.route_name
type = "A"
ttl = "300"
records = [aws_instance.services.public_ip]
}
## Builders ASG
module "legacy_builder_user_data" {
source = "./modules/legacy-builder-cloudinit-ubuntu-docker-v1"
services_private_ip = aws_instance.services.private_ip
circle_secret_passphrase = var.circle_secret_passphrase
https_proxy = var.https_proxy
http_proxy = var.http_proxy
no_proxy = var.no_proxy
}
module "legacy_builder" {
source = "./modules/legacy-builder"
prefix = var.prefix
name = "builders"
aws_subnet_id = var.aws_subnet_id
aws_ssh_key_name = var.aws_ssh_key_name
aws_instance_profile_name = aws_iam_instance_profile.circleci_profile.name
builder_security_group_ids = [
aws_security_group.circleci_builders_sg.id,
aws_security_group.circleci_builders_admin_sg.id,
aws_security_group.circleci_users_sg.id,
]
asg_max_size = var.max_builders_count
asg_min_size = 0
asg_desired_size = var.desired_builders_count
user_data = module.legacy_builder_user_data.rendered
delete_volume_on_termination = var.services_delete_on_termination
image_id = var.ubuntu_ami[var.aws_region]
instance_type = var.builder_instance_type
spot_price = var.legacy_builder_spot_price
shutdown_queue_target_sqs_arn = module.shutdown_sqs.sqs_arn
shutdown_queue_role_arn = module.shutdown_sqs.queue_role_arn
}
module "nomad" {
source = "./modules/nomad"
enabled = var.enable_nomad
prefix = var.prefix
instance_type = var.nomad_client_instance_type
aws_vpc_id = var.aws_vpc_id
aws_subnet_id = var.aws_subnet_id
aws_ssh_key_name = var.aws_ssh_key_name
http_proxy = var.http_proxy
https_proxy = var.https_proxy
no_proxy = var.no_proxy
ami_id = var.services_ami != "" ? var.services_ami : var.ubuntu_ami[var.aws_region]
aws_subnet_cidr_block = data.aws_subnet.subnet.cidr_block
services_private_ip = aws_instance.services.private_ip
}
output "success_message" {
value = data.template_file.output.rendered
}
output "install_url" {
value = "http://${aws_instance.services.public_ip}/"
}
output "ssh-services" {
value = "ssh ubuntu@${aws_instance.services.public_ip}"
}